lyman 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c688da4f3dbae3ea1eae1ddadbbcb9c8024ca15d6b396d6a2b8aa85dd77bb91c
4
- data.tar.gz: 31ead51a810ff07e2ef56943f36cc379ef8df8cbb7dcdfc611ad92c2d098c61e
3
+ metadata.gz: 10c9bb49a3623d7ef8179f561101dca35a940a5efb510a05b349c527a1141764
4
+ data.tar.gz: 6fa324e06604573e06c658991618a4715968ac097c3fff616abf0ba4f20a3f22
5
5
  SHA512:
6
- metadata.gz: f5c814ed46a0ff60e7058850901a13a4b5367a87afe935efcdc86c881619580a656e5387e9cedd47c9e7ea47caf958ffb2ddb73b6a4697563f6dac21995a4a05
7
- data.tar.gz: a6b3450171f5db5c04eb8ce3bb5e3fb742abf692d45f30c1dcb1077afda75570e73b2e0a054fbeadcbca92d1876a5b17ed51cf8f9b07c6c444e1b86d9c6fddfe
6
+ metadata.gz: db9944dbe1f9258b65e9e682aedaf41cd4cdcba895a68e90e64390fbe0dfebb81d335df410338c3dd35ebfbbae553b592d7c642139a83e2c0cbdbf9d279360ff
7
+ data.tar.gz: 8ccdb31708d12bdbdaef0ff9438148165d7bdfe15dd19bcbc26f86cd600964fc046e9589185fc62824b55be427deff93287524b2f6e799a898533d58873ce6d1
data/harness/chat.rb CHANGED
@@ -42,15 +42,40 @@ schemas = TOOLS.values.map { |tool| tool[:schema] }
42
42
  handlers = TOOLS.transform_values { |tool| tool[:handler] }
43
43
 
44
44
  # ── Display: fitting this harness to its models ─────────────────────────────
45
+ # Everything from here to "Shell state" is presentation. Its dependencies —
46
+ # cli-ui for color and glyphs, reline (stdlib) for prompt line-editing and
47
+ # history — are confined to this section; restyle or delete freely without
48
+ # touching the circuit.
49
+ require "cli/ui"
50
+ require "reline"
51
+
52
+ # Styling codes used across streamed fragments, where CLI::UI.fmt can't help
53
+ # (fmt resets at the end of each call; a think preview arrives in pieces).
54
+ # Empty when piped, matching cli-ui's own no-tty behavior.
55
+ TTY = $stdout.tty?
56
+ GRAY = TTY ? CLI::UI.resolve_color(:gray).code : ""
57
+ DIM = TTY ? "\e[2;3m" : "" # faint italic, for think previews
58
+ RESET = TTY ? CLI::UI::Color::RESET.code : ""
59
+
60
+ # Paint without CLI::UI.fmt so text we don't control (tool arguments and
61
+ # results) can't be misread as {{markup}}.
62
+ def gray(text) = "#{GRAY}#{text}#{RESET}"
45
63
 
46
64
  # Thinking models prefix replies with <think>...</think> (the worker
47
65
  # normalizes separate-reasoning-field servers to the same convention). When
48
66
  # the reply arrives one fragment at a time we can't regex the whole thing, so
49
- # this streams a short preview of the thinking — the first few lines — then
50
- # elides the rest, closing with "...</think>" once the model stops thinking.
67
+ # this streams a short preview of the thinking — the first few lines,
68
+ # rendered as a dim "✻ " aside rather than the literal tags — then elides
69
+ # the rest once the model stops thinking.
70
+ #
71
+ # Emits [think_text, reply_text] pairs. Both stream straight to the terminal
72
+ # today, but the split is the seam for reply-side processing (a markdown
73
+ # renderer, say — mind that anything buffering the reply trades away its
74
+ # token-by-token streaming, which is why this harness doesn't ship one).
51
75
  class ThinkFilter
52
76
  OPEN = "<think>"
53
77
  CLOSE = "</think>"
78
+ MARK = "✻ "
54
79
  SNIPPET_LINES = 3
55
80
  SNIPPET_CHARS = 240 # think blocks are often one long unwrapped paragraph
56
81
 
@@ -62,25 +87,26 @@ class ThinkFilter
62
87
  @truncated = false
63
88
  end
64
89
 
65
- # Returns the printable portion of this fragment.
90
+ # Returns the printable portion of this fragment as a
91
+ # [think_text, reply_text] pair (either may be empty).
66
92
  def filter(fragment)
67
93
  @buffer << fragment
68
94
  case @state
69
95
  when :start then filter_start
70
96
  when :thinking then filter_thinking
71
- when :after_think then filter_after_think
72
- when :passing then take_buffer
97
+ when :after_think then ["", filter_after_think]
98
+ when :passing then ["", take_buffer]
73
99
  end
74
100
  end
75
101
 
76
102
  # Call when the message is complete: releases anything still held back
77
- # (e.g. a reply that was nothing but "<thin"), and closes a think block
78
- # the model never closed itself.
103
+ # (e.g. a reply that was nothing but "<thin"), and closes out the styling
104
+ # of a think block the model never closed itself.
79
105
  def flush
80
106
  case @state
81
- when :start then take_buffer
82
- when :thinking then (@truncated ? "..." : "") + CLOSE
83
- else ""
107
+ when :start then ["", take_buffer]
108
+ when :thinking then [(@truncated ? "" : "") + RESET, ""]
109
+ else ["", ""]
84
110
  end
85
111
  end
86
112
 
@@ -96,12 +122,13 @@ class ThinkFilter
96
122
  if @buffer.start_with?(OPEN)
97
123
  @state = :thinking
98
124
  @buffer = @buffer[OPEN.length..]
99
- OPEN + filter_thinking
125
+ think, reply = filter_thinking
126
+ [DIM + MARK + think, reply]
100
127
  elsif OPEN.start_with?(@buffer)
101
- "" # could still become <think>; wait for more
128
+ ["", ""] # could still become <think>; wait for more
102
129
  else
103
130
  @state = :passing
104
- take_buffer
131
+ ["", take_buffer]
105
132
  end
106
133
  end
107
134
 
@@ -110,14 +137,14 @@ class ThinkFilter
110
137
  thought = @buffer[0...idx]
111
138
  @buffer = @buffer[(idx + CLOSE.length)..]
112
139
  @state = :after_think
113
- snippet(thought) + (@truncated ? "..." : "") + CLOSE + "\n\n" + filter_after_think
140
+ [snippet(thought) + (@truncated ? "" : "") + RESET + "\n\n", filter_after_think]
114
141
  else
115
142
  # Keep any tail that could be the start of CLOSE split across
116
143
  # fragments; preview the rest.
117
144
  keep = partial_close_suffix
118
145
  thought = @buffer[0, @buffer.length - keep.length]
119
146
  @buffer = keep
120
- snippet(thought)
147
+ [snippet(thought), ""]
121
148
  end
122
149
  end
123
150
 
@@ -137,8 +164,8 @@ class ThinkFilter
137
164
  out
138
165
  end
139
166
 
140
- # We print "</think>\n\n" ourselves, so swallow the model's own
141
- # whitespace between the close tag and the reply.
167
+ # We end the think preview with "\n\n" ourselves, so swallow the model's
168
+ # own whitespace between the close tag and the reply.
142
169
  def filter_after_think
143
170
  stripped = @buffer.lstrip
144
171
  if stripped.empty?
@@ -160,25 +187,59 @@ class ThinkFilter
160
187
  end
161
188
  end
162
189
 
163
- # Streams one round's content to the terminal, printing the model label
164
- # before the first visible text so silent rounds (pure tool calls) don't
165
- # leave an empty prompt behind.
190
+ # The silence between sending a prompt and the first streamed token can be
191
+ # long on a local model (prefill). cli-ui's spinner owns the calling thread
192
+ # for the duration of a block, which can't express "spin until the first
193
+ # delta arrives" — so this is the one hand-rolled widget: a background
194
+ # spinner with a stop method. Quiet when output is piped.
195
+ class WaitSpinner
196
+ FRAMES = %w[⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏]
197
+
198
+ def start(label)
199
+ return unless TTY
200
+ stop
201
+ @thread = Thread.new do
202
+ FRAMES.cycle do |frame|
203
+ print "\r#{GRAY}#{frame} #{label}#{RESET}"
204
+ sleep 0.08
205
+ end
206
+ end
207
+ end
208
+
209
+ def stop
210
+ return unless @thread
211
+ @thread.kill.join
212
+ @thread = nil
213
+ print "\r\e[K" # erase the spinner line
214
+ end
215
+ end
216
+
217
+ # Streams one round's content to the terminal: a spinner while waiting on
218
+ # the model, then the model label before the first visible text — so silent
219
+ # rounds (pure tool calls) don't leave an empty prompt behind.
166
220
  class RoundPrinter
167
221
  def initialize(label)
168
222
  @label = label
223
+ @spinner = WaitSpinner.new
169
224
  end
170
225
 
171
226
  def start_round
172
- @filter = ThinkFilter.new
227
+ @think = ThinkFilter.new
173
228
  @printed = false
229
+ @spinner.start("waiting for #{@label}…")
174
230
  end
175
231
 
176
232
  def delta(text)
177
- emit(@filter.filter(text))
233
+ think, reply = @think.filter(text)
234
+ emit(think)
235
+ emit(reply)
178
236
  end
179
237
 
180
238
  def finish_round
181
- emit(@filter.flush)
239
+ @spinner.stop
240
+ think, reply = @think.flush
241
+ emit(think)
242
+ emit(reply)
182
243
  puts if @printed
183
244
  end
184
245
 
@@ -186,18 +247,56 @@ class RoundPrinter
186
247
 
187
248
  def emit(text)
188
249
  return if text.empty?
189
- print "\n#{@label}> " unless @printed
250
+ @spinner.stop
251
+ print CLI::UI.fmt("\n{{magenta:#{@label}}}> ") unless @printed
190
252
  @printed = true
191
253
  print text
192
254
  end
193
255
  end
194
256
 
257
+ # Prints tool activity around the execution stage: the calls the model
258
+ # requested on the way in, a ✓-and-result line on the way out. Results are
259
+ # summarized to one line here — the full text is on the conversation, where
260
+ # the model (and any logging side-worker) sees it.
261
+ class ToolPrinter
262
+ RESULT_WIDTH = 60
263
+
264
+ def calls(conversation)
265
+ conversation.pending_tool_calls.each do |tool_call|
266
+ puts gray(" ⚙ #{tool_call.dig("function", "name")} #{tool_call.dig("function", "arguments")}")
267
+ end
268
+ end
269
+
270
+ def results(conversation)
271
+ messages = conversation.messages
272
+ request = messages.rindex { |m| m["role"] == "assistant" }
273
+ return unless request
274
+ names = (messages[request]["tool_calls"] || [])
275
+ .to_h { |tc| [tc["id"], tc.dig("function", "name")] }
276
+ messages[(request + 1)..].each do |message|
277
+ next unless message["role"] == "tool"
278
+ summary = gray("#{names[message["tool_call_id"]]} → #{summarize(message["content"])}")
279
+ puts " #{CLI::UI.fmt("{{v}}")} #{summary}"
280
+ end
281
+ end
282
+
283
+ private
284
+
285
+ def summarize(text)
286
+ line = text.to_s.gsub(/\s+/, " ").strip
287
+ (line.length > RESULT_WIDTH) ? "#{line[0, RESULT_WIDTH - 1]}…" : line
288
+ end
289
+ end
290
+
195
291
  # ── Shell state ─────────────────────────────────────────────────────────────
196
292
  conversation = Lyman::Conversation.new(
197
- system_prompt: "You are a helpful assistant. Keep replies brief."
293
+ system_prompt: "You are a helpful assistant. Keep replies brief. " \
294
+ "Your replies are printed verbatim in a plain-text terminal: write prose, " \
295
+ "and avoid markdown and LaTeX markup."
198
296
  )
199
297
  rounds = [] # the circuit's queue — visible right here, not smuggled
200
298
  printer = RoundPrinter.new(MODEL)
299
+ tool_printer = ToolPrinter.new
201
300
 
202
301
  # ── The circuit ─────────────────────────────────────────────────────────────
203
302
  pipeline =
@@ -212,31 +311,36 @@ pipeline =
212
311
  c.finish! if c.pending_tool_calls.empty? || c.runaway?
213
312
  c
214
313
  } |
215
- side_worker do |c|
216
- unless c.finished?
217
- c.pending_tool_calls.each do |tc|
218
- puts " ⚙ #{tc.dig("function", "name")} #{tc.dig("function", "arguments")}"
219
- end
220
- end
221
- end |
314
+ side_worker { |c| tool_printer.calls(c) unless c.finished? } |
222
315
  Lyman::Workers.tool_execution(handlers) |
316
+ side_worker { |c| tool_printer.results(c) unless c.finished? } |
223
317
  side_worker { |c| rounds << c unless c.finished? } |
224
318
  filter_worker { |c| c.finished? }
225
319
 
226
320
  # ── Shell process ───────────────────────────────────────────────────────────
227
- puts <<~PREAMBLE
228
- lyman ⇢ #{MODEL} @ #{BASE_URL}
229
-
230
- exit: blank line or ctrl-d
231
- model: LYMAN_MODEL=qwen3.5:2b #{$PROGRAM_NAME}
232
- endpoint: LYMAN_BASE_URL=http://localhost:1234/v1 #{$PROGRAM_NAME}
233
- tools: #{TOOLS.keys.join(", ")} — a ⚙ line appears when the model calls one
234
-
235
- PREAMBLE
321
+ puts CLI::UI.fmt("{{bold:lyman}} ⇢ {{magenta:#{MODEL}}} @ {{blue:#{BASE_URL}}}")
322
+ puts
323
+ puts gray(<<~HINTS.gsub(/^/, " "))
324
+ exit: blank line, ctrl-c, or ctrl-d
325
+ change the model: LYMAN_MODEL=qwen3.5:2b #{$PROGRAM_NAME}
326
+ change the endpoint: LYMAN_BASE_URL=http://localhost:1234/v1 #{$PROGRAM_NAME}
327
+ available tools: #{TOOLS.keys.join(", ")} — a ⚙ line appears when the model calls one
328
+ HINTS
236
329
 
237
330
  loop do
238
- print "\nyou> "
239
- input = $stdin.gets&.strip
331
+ puts
332
+ # Reline gives the prompt line editing and history, but emits screen-
333
+ # redraw escapes even when input is piped — so scripted runs get gets.
334
+ input = begin
335
+ if $stdin.tty?
336
+ Reline.readline(CLI::UI.fmt("{{cyan:you}}> "), true)&.strip
337
+ else
338
+ print "you> "
339
+ $stdin.gets&.strip
340
+ end
341
+ rescue Interrupt
342
+ nil # ctrl-c leaves like ctrl-d, not with a stack trace
343
+ end
240
344
  break if input.nil? || input.empty?
241
345
 
242
346
  rounds << conversation.add_user_message(input)
@@ -61,7 +61,7 @@ module Lyman
61
61
  source: "templates/Gemfile",
62
62
  dest: "Gemfile",
63
63
  role: :owned,
64
- description: "Client dependencies: shifty (and ostruct for ruby >= 4)"
64
+ description: "Client dependencies: shifty (plus ostruct for ruby >= 4, cli-ui for the harness display)"
65
65
  },
66
66
  "gitignore" => {
67
67
  source: "templates/gitignore",
@@ -2,6 +2,6 @@ module Lyman
2
2
  module CLI
3
3
  # The gem's own version — distinct from any artifact's `planted_at`.
4
4
  # This is what the manifest's top-level `lyman:` key records.
5
- VERSION = "0.2.0"
5
+ VERSION = "0.2.1"
6
6
  end
7
7
  end
data/templates/CLAUDE.md CHANGED
@@ -7,8 +7,9 @@ against any OpenAI-compatible chat completions endpoint (Ollama by default).
7
7
 
8
8
  ## Running it
9
9
 
10
- - `bundle install` — install dependencies (just `shifty` and `ostruct`; this
11
- project has no runtime dependency on the `lyman` gem itself).
10
+ - `bundle install` — install dependencies (`shifty`, `ostruct`, and `cli-ui`
11
+ for the harness's display layer; this project has no runtime dependency on
12
+ the `lyman` gem itself).
12
13
  - `ruby harness/chat.rb` — run the interactive chat harness. Defaults to
13
14
  Ollama at `http://localhost:11434/v1`; override with `LYMAN_MODEL` and
14
15
  `LYMAN_BASE_URL` env vars.
data/templates/Gemfile CHANGED
@@ -2,3 +2,6 @@ source "https://rubygems.org"
2
2
 
3
3
  gem "shifty"
4
4
  gem "ostruct" # shifty dependency; no longer a default gem as of ruby 4.0
5
+ # Used only by harness/chat.rb's display layer; drop them if you restyle.
6
+ gem "cli-ui"
7
+ gem "reline"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lyman
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joel Helbling
@@ -105,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
105
105
  - !ruby/object:Gem::Version
106
106
  version: '0'
107
107
  requirements: []
108
- rubygems_version: 4.0.10
108
+ rubygems_version: 4.0.15
109
109
  specification_version: 4
110
110
  summary: A composable agentic harness, delivered as code you own — a pure generator
111
111
  in the shadcn/ui mold, built on shifty