Class: Optimize::Demo::Claude::Transcript

Inherits:
Object
  • Object
show all
Defined in:
lib/optimize/demo/claude/transcript.rb

Overview

Accumulates the per-iteration state of a Claude “gag pass” run and renders a Markdown transcript suitable for inclusion in demo artifacts. Stateful — instantiate one per fixture run.

Instance Method Summary collapse

Constructor Details

#initialize(fixture:, source:, cases:) ⇒ Transcript

Returns a new instance of Transcript.



11
12
13
14
15
16
17
# File 'lib/optimize/demo/claude/transcript.rb', line 11

def initialize(fixture:, source:, cases:)
  @fixture = fixture
  @source = source
  @cases = cases
  @iterations = []
  @outcome = nil
end

Instance Method Details

#finish(outcome:) ⇒ Object



29
30
31
32
33
34
# File 'lib/optimize/demo/claude/transcript.rb', line 29

def finish(outcome:)
  unless %i[success gave_up].include?(outcome)
    raise ArgumentError, "unknown outcome: #{outcome.inspect}"
  end
  @outcome = outcome
end

#record(iteration:, prompt:, raw:, parsed:, errors:) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/optimize/demo/claude/transcript.rb', line 19

def record(iteration:, prompt:, raw:, parsed:, errors:)
  @iterations << {
    iteration: iteration,
    prompt: prompt,
    raw: raw,
    parsed: parsed,
    errors: errors,
  }
end

#renderObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
# File 'lib/optimize/demo/claude/transcript.rb', line 36

def render
  unless %i[success gave_up].include?(@outcome)
    raise ArgumentError, "transcript outcome not set (got #{@outcome.inspect}); call finish(outcome:) first"
  end

  out = +""
  out << "# Claude gag — #{@fixture}\n"
  out << "\n"
  out << "**Fixture source:**\n"
  out << "\n"
  out << "```ruby\n"
  out << "#{@source.strip}\n"
  out << "```\n"
  out << "\n"
  out << "**Validation cases:**\n"
  out << "\n"
  @cases.each { |entry, expected| out << "- `#{entry}` → `#{expected.inspect}`\n" }

  @iterations.each do |rec|
    out << "\n"
    out << "## Iteration #{rec[:iteration]}\n"
    out << "\n"
    out << "**Prompt:**\n"
    out << "\n"
    out << "```\n"
    out << "#{rec[:prompt]}\n"
    out << "```\n"
    out << "\n"
    out << "**Raw response:**\n"
    out << "\n"
    out << "```\n"
    out << "#{rec[:raw]}\n"
    out << "```\n"
    out << "\n"
    out << "**Parsed IR:**\n"
    out << "\n"
    out << "```json\n"
    out << "#{JSON.generate(rec[:parsed])}\n"
    out << "```\n"
    out << "\n"
    if rec[:errors].empty?
      out << "**Validator errors:** (none)\n"
    else
      out << "**Validator errors:**\n"
      rec[:errors].each { |e| out << "- #{e}\n" }
    end
  end

  out << "\n"
  case @outcome
  when :success
    out << "## Outcome: success\n"
  when :gave_up
    out << "## Outcome: gave up after #{@iterations.length} attempts\n"
  end

  out
end