61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
# File 'lib/docforge/cli.rb', line 61
def generate(folder)
cfg = build_config
cfg.instance_variable_set(:@model, options[:model]) if options[:model]
cfg.validate! unless options[:dry_run] || cache_hit?(folder)
puts "→ Reading inputs from #{File.expand_path(folder)}"
if cfg.loaded_config_files.any?
puts " · Config: #{cfg.loaded_config_files.join(", ")}"
end
inputs = Inputs.read(folder, config: cfg)
f = cfg.input_filenames
puts " ✓ #{f["prd"]} (#{inputs.prd.length} chars)"
puts " ✓ #{f["spec"]} (#{inputs.spec.length} chars)"
puts " #{inputs.notes ? "✓" : "·"} #{f["notes"]} #{inputs.notes ? "(#{inputs.notes.length} chars)" : "(none)"}"
puts " #{inputs.assets.any? ? "✓" : "·"} _assets/ (#{inputs.assets.size} files)"
cache_path = File.join(inputs.folder, ".brief-cache.json")
cached = !options[:fresh] && File.exist?(cache_path)
if cached
puts "\n→ Using cached brief (#{cache_path})"
puts " · pass --fresh to re-call the API"
cache_data = JSON.parse(File.read(cache_path, encoding: "UTF-8"))
payload = cache_data["payload"]
puts " ✓ Cached on #{cache_data["generated_at"]} via #{cache_data["model"]}"
else
interview_answers = options[:no_interview] ? {} : run_interview(cfg.interview_questions)
user_message = Prompt.user_message(inputs: inputs, interview_answers: interview_answers)
if options[:dry_run]
puts "\n→ DRY RUN — would call Anthropic with:"
puts " Model: #{cfg.model}"
puts " System prompt length: #{Prompt.system_prompt(config: cfg).length} chars"
puts " User message length: #{user_message.length} chars"
puts " Output would land at: #{File.join(cfg.output_dir, inputs.slug)}.docx"
puts "\nSet ANTHROPIC_API_KEY and re-run without --dry-run to actually generate."
return
end
spinner = TTY::Spinner.new("[:spinner] Calling Anthropic (#{cfg.model})...", format: :dots)
spinner.auto_spin
begin
client = Client.new(cfg)
payload = client.generate_brief(
system_prompt: Prompt.system_prompt(config: cfg),
user_message: user_message
)
spinner.success("(done)")
rescue StandardError => e
spinner.error("(failed)")
raise e
end
File.write(cache_path, JSON.pretty_generate(
"generated_at" => Time.now.utc.iso8601,
"model" => cfg.model,
"payload" => payload
))
puts " ✓ Cached response → #{cache_path}"
end
brief = Brief.new(payload)
puts " ✓ Brief — #{brief.title.inspect}"
puts "\n→ Rendering .docx..."
path = Renderers::Docx.new(brief: brief, config: cfg, slug: inputs.slug).render
puts " ✓ Wrote #{path}"
puts "\nDone. Open it: open \"#{path}\""
rescue Error => e
warn "ERROR: #{e.message}"
exit 1
end
|