Class: CoachZed

Inherits:
Object
  • Object
show all
Defined in:
lib/coach_zed.rb,
lib/coach_zed/catalog.rb,
lib/coach_zed/version.rb,
lib/coach_zed/feed_reader.rb,
lib/coach_zed/feed_writer.rb,
lib/coach_zed/prompt_builder.rb,
lib/coach_zed/schedule_parser.rb,
lib/coach_zed/clients/ruby_openai.rb

Defined Under Namespace

Modules: Catalog, Clients Classes: Config, FeedReader, FeedWriter, PromptBuilder, Result, ScheduleParser

Constant Summary collapse

VERSION =
"0.5.0"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:, workout_catalog_dir: nil, model: nil, output_dir: nil, feed_output_basename: nil, existing_feed_path: nil) ⇒ CoachZed

Returns a new instance of CoachZed.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/coach_zed.rb', line 93

def initialize(
  client:,
  workout_catalog_dir: nil,
  model: nil,
  output_dir: nil,
  feed_output_basename: nil,
  existing_feed_path: nil
)
  config = self.class.config

  @workout_catalog_dir = Pathname(workout_catalog_dir || config.workout_catalog_dir || raise(ArgumentError, "workout_catalog_dir is required"))
  model_name = model || config.model || "gpt-4.1"
  @ai_client = wrap_client(client, model: model_name)
  @output_dir = Pathname(output_dir || config.output_dir || "results")
  @schedule_output_dir = @output_dir.join("schedules")
  @feed_output_dir = @output_dir.join("feeds")
  @feed_output_basename = feed_output_basename.nil? ? config.feed_output_basename : feed_output_basename
  resolved_existing_feed_path = existing_feed_path.nil? ? config.existing_feed_path : existing_feed_path
  @existing_feed_path = resolved_existing_feed_path && Pathname(resolved_existing_feed_path)
end

Class Method Details

.configObject



45
46
47
48
49
# File 'lib/coach_zed.rb', line 45

def config
  @config ||= default_config
  load_config_file
  @config
end

.config_file_pathsObject



88
89
90
# File 'lib/coach_zed.rb', line 88

def config_file_paths
  [".coach_zed.yml", File.expand_path("~/.config/coach_zed.yml")]
end

.configure {|config| ... } ⇒ Object

Yields:



51
52
53
54
# File 'lib/coach_zed.rb', line 51

def configure
  load_config_file
  yield config
end

.default_configObject



81
82
83
84
85
86
# File 'lib/coach_zed.rb', line 81

def default_config
  Config.new(
    model: "gpt-4.1",
    output_dir: "results"
  )
end

.load_config_fileObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/coach_zed.rb', line 56

def load_config_file
  return if @config_file_loaded

  @config ||= default_config

  config_file_paths.each do |path|
    next unless File.exist?(path)

    parsed = YAML.load_file(path)
    next unless parsed.is_a?(Hash)

    @config.apply(parsed.transform_keys(&:to_sym))
    @config_file_loaded = true
    return path
  end

  @config_file_loaded = true
  nil
end

.reset_config!Object



76
77
78
79
# File 'lib/coach_zed.rb', line 76

def reset_config!
  @config = nil
  @config_file_loaded = false
end

Instance Method Details

#generate_schedule(start_date:, consultation_prompt: nil, consultation_prompt_path: nil) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/coach_zed.rb', line 114

def generate_schedule(start_date:, consultation_prompt: nil, consultation_prompt_path: nil)
  prompt_text = resolve_prompt_text(consultation_prompt, consultation_prompt_path)
  catalog = Catalog::Loader.new(@workout_catalog_dir).load
  existing_feed = load_existing_feed
  start_date = generation_start_date(start_date, existing_feed:)
  generation_days = existing_feed ? 7 : 28
  existing_feed_context = existing_feed&.to_context(limit_days: 28)
  schedule_key = schedule_key_for(prompt_text, start_date, catalog, generation_days, existing_feed_context)
  prompt = PromptBuilder.new(
    consultation_prompt: prompt_text,
    catalog: catalog,
    start_date: start_date,
    schedule_key: schedule_key,
    generation_days: generation_days,
    existing_feed_context: existing_feed_context
  ).build
  raw_schedule = @ai_client.generate(prompt:)
  schedule = ScheduleParser.parse(raw_schedule)
  schedule = normalize_schedule(schedule, start_date:, prompt_text:, schedule_key:, catalog:, generation_days:)

  schedule_path = write_schedule(schedule, schedule_key)
  feed_paths = write_feeds(schedule, start_date:, schedule_key:, existing_feed:)

  Result.new(
    schedule_path: schedule_path,
    ics_path: feed_paths.fetch(:ics),
    webcal_path: feed_paths.fetch(:webcal),
    schedule: schedule
  )
end