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/schedule_schema.rb,
lib/coach_zed/clients/ruby_openai.rb

Defined Under Namespace

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

Constant Summary collapse

VERSION =
"0.5.6"

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, feed_title: nil, existing_feed_path: nil) ⇒ CoachZed

Returns a new instance of CoachZed.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/coach_zed.rb', line 96

def initialize(
  client:,
  workout_catalog_dir: nil,
  model: nil,
  output_dir: nil,
  feed_output_basename: nil,
  feed_title: 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
  @feed_title = feed_title.nil? ? config.feed_title : feed_title
  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



48
49
50
51
52
# File 'lib/coach_zed.rb', line 48

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

.config_file_pathsObject



91
92
93
# File 'lib/coach_zed.rb', line 91

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

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

Yields:



54
55
56
57
# File 'lib/coach_zed.rb', line 54

def configure
  load_config_file
  yield config
end

.default_configObject



84
85
86
87
88
89
# File 'lib/coach_zed.rb', line 84

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

.load_config_fileObject



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

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



79
80
81
82
# File 'lib/coach_zed.rb', line 79

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

Instance Method Details

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



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
144
145
146
147
148
149
# File 'lib/coach_zed.rb', line 119

def generate_schedule(start_date:, consultation_prompt: nil, consultation_prompt_path: nil, generation_mode: nil)
  prompt_text = resolve_prompt_text(consultation_prompt, consultation_prompt_path)
  catalog = Catalog::Loader.new(@workout_catalog_dir).load
  generation_mode = normalize_generation_mode(generation_mode)
  existing_feed = load_existing_feed if generation_mode != :refresh
  start_date = generation_start_date(start_date, existing_feed:, generation_mode:)
  generation_days = generation_days_for(start_date, generation_mode:, existing_feed:)
  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:, existing_feed:)

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