pikuri-core 0.0.7 → 0.2.0
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 +4 -4
- data/README.md +17 -30
- data/lib/pikuri/agent/chat_transport.rb +168 -107
- data/lib/pikuri/agent/configurator.rb +194 -173
- data/lib/pikuri/agent/control/cancellable.rb +100 -70
- data/lib/pikuri/agent/control/handoff.rb +75 -0
- data/lib/pikuri/agent/control/interloper.rb +107 -118
- data/lib/pikuri/agent/control.rb +13 -35
- data/lib/pikuri/agent/event.rb +144 -197
- data/lib/pikuri/agent/extension.rb +83 -95
- data/lib/pikuri/agent/history.rb +247 -0
- data/lib/pikuri/agent/listener/in_memory_event_list.rb +1 -1
- data/lib/pikuri/agent/listener/terminal.rb +169 -136
- data/lib/pikuri/agent/listener/token_log.rb +84 -157
- data/lib/pikuri/agent/listener.rb +24 -44
- data/lib/pikuri/agent/listener_list.rb +32 -47
- data/lib/pikuri/agent/request_instrumenter.rb +102 -0
- data/lib/pikuri/agent.rb +1095 -577
- data/lib/pikuri/attachment.rb +65 -0
- data/lib/pikuri/bundler_env.rb +68 -0
- data/lib/pikuri/extractor/passthrough.rb +24 -31
- data/lib/pikuri/extractor.rb +125 -155
- data/lib/pikuri/file_type.rb +64 -136
- data/lib/pikuri/finalizers.rb +32 -47
- data/lib/pikuri/paths.rb +104 -13
- data/lib/pikuri/sanitizer.rb +46 -67
- data/lib/pikuri/subprocess.rb +86 -118
- data/lib/pikuri/testing.rb +369 -0
- data/lib/pikuri/tool/calculator.rb +168 -74
- data/lib/pikuri/tool/execute_context.rb +42 -0
- data/lib/pikuri/tool/parameters.rb +31 -29
- data/lib/pikuri/tool/result.rb +164 -0
- data/lib/pikuri/tool/trifecta_legs.rb +248 -0
- data/lib/pikuri/tool.rb +416 -67
- data/lib/pikuri/trifecta/contribution.rb +43 -0
- data/lib/pikuri/trifecta/node.rb +47 -0
- data/lib/pikuri/trifecta/report.rb +228 -0
- data/lib/pikuri/trifecta.rb +126 -0
- data/lib/pikuri/version.rb +1 -1
- data/lib/pikuri-core.rb +70 -92
- data/prompts/agent-loop.txt +5 -0
- data/prompts/pikuri-chat.txt +3 -12
- metadata +26 -72
- data/lib/pikuri/agent/context_window_detector.rb +0 -222
- data/lib/pikuri/agent/control/step_limit.rb +0 -111
- data/lib/pikuri/agent/extension_context.rb +0 -147
- data/lib/pikuri/agent/listener/rate_limited.rb +0 -172
- data/lib/pikuri/agent/synthesizer.rb +0 -171
- data/lib/pikuri/extractor/html.rb +0 -303
- data/lib/pikuri/tool/fetch.rb +0 -172
- data/lib/pikuri/tool/scraper.rb +0 -186
- data/lib/pikuri/tool/search/brave.rb +0 -198
- data/lib/pikuri/tool/search/duckduckgo.rb +0 -207
- data/lib/pikuri/tool/search/engines.rb +0 -186
- data/lib/pikuri/tool/search/exa.rb +0 -229
- data/lib/pikuri/tool/search/rate_limiter.rb +0 -92
- data/lib/pikuri/tool/search/result.rb +0 -29
- data/lib/pikuri/tool/web_scrape.rb +0 -121
- data/lib/pikuri/tool/web_search.rb +0 -57
- data/lib/pikuri/url_cache.rb +0 -112
|
@@ -1,80 +1,89 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'rainbow'
|
|
4
|
+
require 'io/console'
|
|
4
5
|
|
|
5
6
|
module Pikuri
|
|
6
7
|
class Agent
|
|
7
8
|
module Listener
|
|
8
|
-
# Terminal renderer for the normalized event stream: dim grey
|
|
9
|
-
#
|
|
10
|
-
#
|
|
11
|
-
#
|
|
12
|
-
#
|
|
13
|
-
# memory / context an extension injected) renders dim grey
|
|
14
|
-
# with a +⊕+ marker. {Event::UserTurn} is intentionally silent
|
|
15
|
-
# (the terminal user just typed the message, so re-rendering
|
|
16
|
-
# it adds nothing); {Event::Tokens} and {Event::ContextCap}
|
|
17
|
-
# are silent too (their consumer is {TokenLog}).
|
|
9
|
+
# Terminal renderer for the normalized event stream: dim grey reasoning,
|
|
10
|
+
# assistant Markdown printed unrendered, tool-call/result lines in Kuri's coat
|
|
11
|
+
# ({TOOL_COLOR}), yellow fallback notice, red cancelled, magenta
|
|
12
|
+
# model-switch. A tool call renders as the two strings the tool composed,
|
|
13
|
+
# never an argument dump or a slice of the observation:
|
|
18
14
|
#
|
|
19
|
-
#
|
|
20
|
-
#
|
|
21
|
-
# used to sit on the non-streaming path; it was dropped:
|
|
22
|
-
# rendering can never apply to the streaming path anyway
|
|
23
|
-
# (half-finished Markdown — broken code fences, half-built
|
|
24
|
-
# tables — doesn't render), the gem hadn't shipped a release
|
|
25
|
-
# since 2023 (its known ANSI-in-table crashes forced a
|
|
26
|
-
# rescue-and-degrade carve-out here), and it pulled seven
|
|
27
|
-
# transitive gems into the audit surface. Raw Markdown is
|
|
28
|
-
# perfectly readable in a terminal; proper rendering belongs
|
|
29
|
-
# to a richer host (the planned pikuri-tui).
|
|
15
|
+
# → Write(lib/shared/config.rb)
|
|
16
|
+
# = Added 2 lines, removed 3 lines
|
|
30
17
|
#
|
|
31
|
-
#
|
|
32
|
-
#
|
|
33
|
-
#
|
|
34
|
-
#
|
|
35
|
-
#
|
|
18
|
+
# {Event::UserTurn} / {Event::Tokens} are silent
|
|
19
|
+
# (echo, or {TokenLog}'s business); {Event::SystemInjected} renders dim
|
|
20
|
+
# grey with a +⊕+ marker. Any unrecognized event — notably a gem
|
|
21
|
+
# *domain* event — renders generically via its +to_s+ (dim grey, +·+
|
|
22
|
+
# marker), so a new variant shows in the demo without a change here. Its
|
|
23
|
+
# text is defanged through {Pikuri::Sanitizer} — a +to_s+ may carry a
|
|
24
|
+
# filename, a skill name or a server's own words.
|
|
36
25
|
#
|
|
37
|
-
#
|
|
26
|
+
# Prose — reasoning, assistant Markdown, an injected block — prints as
|
|
27
|
+
# written except for the bytes a terminal *acts on*, which show escaped:
|
|
28
|
+
#
|
|
29
|
+
# "done\e[1A\e[2K" # prints done\x1b[1A\x1b[2K
|
|
38
30
|
#
|
|
39
|
-
#
|
|
40
|
-
#
|
|
31
|
+
# A model echoing an attacker's bytes could otherwise recolor, overwrite
|
|
32
|
+
# or erase lines already on screen, a guard's warning among them. Tabs
|
|
33
|
+
# and zero-width joiners pass, since honest prose carries them (Go code,
|
|
34
|
+
# 👩💻) — which is why this isn't {Pikuri::Sanitizer} wholesale.
|
|
41
35
|
#
|
|
42
|
-
#
|
|
43
|
-
#
|
|
44
|
-
#
|
|
45
|
-
# -
|
|
46
|
-
#
|
|
47
|
-
# - {Event::Thinking} and {Event::Assistant} bookends print
|
|
48
|
-
# a single blank line as a stream terminator, not their
|
|
49
|
-
# content (the content already landed via the deltas). The
|
|
50
|
-
# terminator gives the next event (tool call, next round,
|
|
51
|
-
# final REPL prompt) a clean line to start on.
|
|
36
|
+
# Assistant Markdown prints unrendered, no Markdown-to-ANSI: rendering can't
|
|
37
|
+
# apply to the streaming path anyway (half-built fences/tables), and the
|
|
38
|
+
# one gem for it (tty-markdown) was unmaintained, crashed on
|
|
39
|
+
# ANSI-in-tables, and pulled seven transitive deps — proper rendering
|
|
40
|
+
# belongs to a richer host (the planned pikuri-tui).
|
|
52
41
|
#
|
|
53
|
-
#
|
|
54
|
-
#
|
|
55
|
-
#
|
|
42
|
+
# +padding:+ prepends a fixed number of leading spaces to every line;
|
|
43
|
+
# sub-agents get a fresh padded instance via {#for_sub_agent}.
|
|
44
|
+
#
|
|
45
|
+
# == Streaming mode
|
|
46
|
+
#
|
|
47
|
+
# With +streaming: true+ (matching the {Agent}'s flag):
|
|
48
|
+
# {Event::ThinkingDelta} / {Event::AssistantDelta} print live (dim grey /
|
|
49
|
+
# uncolored, no trailing newline), and the {Event::Thinking} /
|
|
50
|
+
# {Event::Assistant} bookends print a single blank line as a terminator,
|
|
51
|
+
# not their content (already landed via deltas). With +streaming: false+
|
|
52
|
+
# (default) the deltas are ignored and the bookends render full text.
|
|
56
53
|
class Terminal < Base
|
|
57
|
-
#
|
|
58
|
-
#
|
|
59
|
-
#
|
|
60
|
-
#
|
|
61
|
-
|
|
62
|
-
|
|
54
|
+
# Char cap on a tool's display strings ({Event::ToolResult}'s summary, a
|
|
55
|
+
# display name). Generous — these are one short line by construction,
|
|
56
|
+
# and the cap is a backstop against an +"Error: ..."+ observation
|
|
57
|
+
# passing a long message through.
|
|
58
|
+
MAX_SUMMARY_CHARS = 200
|
|
59
|
+
|
|
60
|
+
# Floor on the room a tool-call argument gets, for the narrow terminal
|
|
61
|
+
# where the budget arithmetic would otherwise leave it nothing.
|
|
62
|
+
MIN_ARGUMENT_CHARS = 20
|
|
63
|
+
|
|
64
|
+
# The bytes a terminal acts on rather than prints, as prose may carry
|
|
65
|
+
# them: C0 controls bar tab and newline, DEL, C1, the bidi overrides.
|
|
66
|
+
# {Pikuri::Sanitizer::SUSPECT} minus tab and the zero-width set.
|
|
67
|
+
PROSE_SUSPECT = /[\u0000-\u0008\u000b-\u001f\u007f-\u009f--]/
|
|
63
68
|
|
|
64
|
-
#
|
|
65
|
-
#
|
|
66
|
-
# (
|
|
67
|
-
#
|
|
68
|
-
#
|
|
69
|
+
# Color of the +→ call+ / += result+ lines: Kuri's Talus coat
|
|
70
|
+
# (+assets/kuri.svg+) with the chroma lifted, because the coat verbatim
|
|
71
|
+
# (+#a89c8e+, 13% saturation) lands on the dim reasoning grey's
|
|
72
|
+
# lightness and reads as a second shade of it. An absolute RGB rather
|
|
73
|
+
# than a named ANSI color, so a remapped terminal palette can't repaint
|
|
74
|
+
# pikuri's own color — the same hex the tui and web shells paint.
|
|
75
|
+
# @return [String]
|
|
76
|
+
TOOL_COLOR = '#c1a586'
|
|
77
|
+
|
|
78
|
+
# Padding for a sub-agent's stream. Absolute, not additive — sub-agent
|
|
79
|
+
# recursion is blocked (the sub-agent's toolset excludes +sub_agent+),
|
|
80
|
+
# so depth never exceeds 1 and a fixed indent reads cleanly.
|
|
69
81
|
SUB_AGENT_PADDING = 2
|
|
70
82
|
|
|
71
|
-
# @param padding [Integer] non-negative
|
|
72
|
-
#
|
|
73
|
-
#
|
|
74
|
-
#
|
|
75
|
-
# @param streaming [Boolean] render the chunk-level delta
|
|
76
|
-
# stream live. See the class header's "Streaming mode"
|
|
77
|
-
# section for the behavior swap. Defaults to +false+.
|
|
83
|
+
# @param padding [Integer] non-negative leading spaces per line
|
|
84
|
+
# (default 0; sub-agents get {SUB_AGENT_PADDING} via {#for_sub_agent}).
|
|
85
|
+
# @param streaming [Boolean] render the chunk-level delta stream live
|
|
86
|
+
# (default +false+; see "Streaming mode").
|
|
78
87
|
def initialize(padding: 0, streaming: false)
|
|
79
88
|
super()
|
|
80
89
|
@padding = padding
|
|
@@ -82,22 +91,15 @@ module Pikuri
|
|
|
82
91
|
@at_line_start = true
|
|
83
92
|
end
|
|
84
93
|
|
|
85
|
-
# @return [Integer] current padding width
|
|
86
|
-
# callers can introspect it (and so tests can assert it).
|
|
94
|
+
# @return [Integer] current padding width.
|
|
87
95
|
attr_reader :padding
|
|
88
96
|
|
|
89
|
-
# @return [Boolean] +true+
|
|
90
|
-
# streaming mode (deltas rendered live; bookends emit a
|
|
91
|
-
# terminator newline only).
|
|
97
|
+
# @return [Boolean] +true+ in streaming mode.
|
|
92
98
|
attr_reader :streaming
|
|
93
99
|
|
|
94
|
-
# Sub-agent variant: a fresh +Terminal+ at
|
|
95
|
-
#
|
|
96
|
-
#
|
|
97
|
-
# under the parent's stream and the stream mode stays
|
|
98
|
-
# consistent across the agent tree. Called by
|
|
99
|
-
# {ListenerList#for_sub_agent}; ignores any params it's
|
|
100
|
-
# handed (Terminal has no caller-provided knobs).
|
|
100
|
+
# Sub-agent variant: a fresh +Terminal+ at {SUB_AGENT_PADDING} carrying
|
|
101
|
+
# the parent's +streaming+ flag, so sub-agent output indents under the
|
|
102
|
+
# parent's stream. Ignores any params (Terminal has no caller knobs).
|
|
101
103
|
#
|
|
102
104
|
# @return [Terminal]
|
|
103
105
|
def for_sub_agent(**)
|
|
@@ -113,40 +115,41 @@ module Pikuri
|
|
|
113
115
|
if @streaming
|
|
114
116
|
terminate_stream
|
|
115
117
|
else
|
|
116
|
-
println(indent(Rainbow(content).color(85, 85, 85)))
|
|
118
|
+
println(indent(Rainbow(defang_prose(content)).color(85, 85, 85)))
|
|
117
119
|
end
|
|
118
120
|
in Event::Assistant(content:)
|
|
119
121
|
if @streaming
|
|
120
122
|
terminate_stream
|
|
121
123
|
else
|
|
122
|
-
println(indent(content))
|
|
124
|
+
println(indent(defang_prose(content)))
|
|
123
125
|
end
|
|
124
126
|
in Event::ThinkingDelta(content:)
|
|
125
|
-
stream_fragment(Rainbow(content).color(85, 85, 85)) if @streaming
|
|
127
|
+
stream_fragment(Rainbow(defang_prose(content)).color(85, 85, 85)) if @streaming
|
|
126
128
|
in Event::AssistantDelta(content:)
|
|
127
|
-
stream_fragment(content) if @streaming
|
|
129
|
+
stream_fragment(defang_prose(content)) if @streaming
|
|
128
130
|
in Event::SystemInjected(content:)
|
|
129
|
-
println(indent(Rainbow("⊕ #{content}").color(85, 85, 85)))
|
|
130
|
-
in Event::ToolCall(
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
println(indent(Rainbow("= #{truncate_tool_result(content)}").cyan))
|
|
131
|
+
println(indent(Rainbow("⊕ #{defang_prose(content)}").color(85, 85, 85)))
|
|
132
|
+
in Event::ToolCall(display_name:, argument:)
|
|
133
|
+
println(indent(Rainbow("→ #{tool_call_line(display_name, argument)}").color(TOOL_COLOR)))
|
|
134
|
+
in Event::ToolResult(summary:)
|
|
135
|
+
println(indent(Rainbow("= #{defang(summary, MAX_SUMMARY_CHARS)}").color(TOOL_COLOR)))
|
|
135
136
|
in Event::ModelSwitched(from:, to:)
|
|
136
137
|
println(indent(Rainbow("⇄ model: #{from.model} → #{to.model}").magenta))
|
|
137
|
-
in Event::
|
|
138
|
-
println(indent(Rainbow("
|
|
138
|
+
in Event::HistoryCompacted(messages_before:, messages_after:)
|
|
139
|
+
println(indent(Rainbow("⇣ compacted: #{messages_before} → #{messages_after} messages").magenta))
|
|
139
140
|
in Event::Cancelled
|
|
140
141
|
println(indent(Rainbow('! cancelled').red))
|
|
142
|
+
in Event::UserTurn | Event::Tokens
|
|
143
|
+
# Silent: UserTurn echoes just-typed input; Tokens are TokenLog's. (Non-streaming deltas no-op in their branches above.)
|
|
141
144
|
else
|
|
142
|
-
#
|
|
143
|
-
#
|
|
144
|
-
#
|
|
145
|
-
|
|
145
|
+
# Any other event — notably a gem *domain* event — renders via its
|
|
146
|
+
# own to_s; core needs no knowledge of the type. A richer host
|
|
147
|
+
# pattern-matches the variants it styles.
|
|
148
|
+
domain_event(event)
|
|
146
149
|
end
|
|
147
150
|
end
|
|
148
151
|
|
|
149
|
-
# @return [String] short label for {
|
|
152
|
+
# @return [String] short label for {ListenerList#to_s}; appends
|
|
150
153
|
# each non-default knob (+padding+, +streaming+) so the
|
|
151
154
|
# default form stays +"Terminal"+
|
|
152
155
|
def to_s
|
|
@@ -158,12 +161,9 @@ module Pikuri
|
|
|
158
161
|
|
|
159
162
|
private
|
|
160
163
|
|
|
161
|
-
# +puts+ wrapper that
|
|
162
|
-
#
|
|
163
|
-
#
|
|
164
|
-
# printed in streaming mode knows to insert padding before
|
|
165
|
-
# its first character. Used by every branch in
|
|
166
|
-
# {#on_event} except the delta branches.
|
|
164
|
+
# +puts+ wrapper that resets the streaming line-state to "at line
|
|
165
|
+
# start" (a full-line print leaves the cursor at column 0, so the next
|
|
166
|
+
# streamed fragment knows to insert padding first).
|
|
167
167
|
#
|
|
168
168
|
# @param text [String]
|
|
169
169
|
# @return [void]
|
|
@@ -172,12 +172,18 @@ module Pikuri
|
|
|
172
172
|
@at_line_start = true
|
|
173
173
|
end
|
|
174
174
|
|
|
175
|
-
#
|
|
176
|
-
#
|
|
177
|
-
#
|
|
178
|
-
#
|
|
179
|
-
|
|
180
|
-
|
|
175
|
+
# A gem domain event as one defanged, dimmed line.
|
|
176
|
+
#
|
|
177
|
+
# @param event [Object] any event variant this listener does not style.
|
|
178
|
+
# @return [void]
|
|
179
|
+
def domain_event(event)
|
|
180
|
+
text = Sanitizer.sanitize(event.to_s.gsub(/\s+/, ' ').strip).text
|
|
181
|
+
println(indent(Rainbow("· #{text}").color(85, 85, 85)))
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
# Emit a newline as a stream terminator and reset line-state — the
|
|
185
|
+
# {Event::Thinking}/{Event::Assistant} bookend in streaming mode, where
|
|
186
|
+
# the content already rendered via deltas.
|
|
181
187
|
#
|
|
182
188
|
# @return [void]
|
|
183
189
|
def terminate_stream
|
|
@@ -185,16 +191,12 @@ module Pikuri
|
|
|
185
191
|
@at_line_start = true
|
|
186
192
|
end
|
|
187
193
|
|
|
188
|
-
# Print one streaming fragment
|
|
189
|
-
#
|
|
190
|
-
#
|
|
191
|
-
#
|
|
192
|
-
# newlines: a fragment that contains "foo\nbar" with
|
|
193
|
-
# padding 2 prints +" foo\n bar"+ when the cursor was
|
|
194
|
-
# at line start.
|
|
194
|
+
# Print one streaming fragment without a trailing newline (so the next
|
|
195
|
+
# continues the line) and flush. Threads padding through mid-fragment
|
|
196
|
+
# newlines: "foo\nbar" at padding 2 from line start prints
|
|
197
|
+
# +" foo\n bar"+.
|
|
195
198
|
#
|
|
196
|
-
# @param text [String] the fragment
|
|
197
|
-
# short-circuits
|
|
199
|
+
# @param text [String] the fragment; +nil+/empty short-circuits
|
|
198
200
|
# @return [void]
|
|
199
201
|
def stream_fragment(text)
|
|
200
202
|
return if text.nil? || text.empty?
|
|
@@ -215,11 +217,9 @@ module Pikuri
|
|
|
215
217
|
$stdout.flush
|
|
216
218
|
end
|
|
217
219
|
|
|
218
|
-
# Prepend +@padding+ spaces to every line of +text+.
|
|
219
|
-
#
|
|
220
|
-
#
|
|
221
|
-
# line at the end — +puts+ adds the final newline if
|
|
222
|
-
# missing, same as before.
|
|
220
|
+
# Prepend +@padding+ spaces to every line of +text+. Uses +each_line+,
|
|
221
|
+
# not a +gsub+, so a trailing newline doesn't produce a stray padded
|
|
222
|
+
# blank line.
|
|
223
223
|
#
|
|
224
224
|
# @param text [String]
|
|
225
225
|
# @return [String]
|
|
@@ -230,30 +230,63 @@ module Pikuri
|
|
|
230
230
|
text.to_s.each_line.map { |line| prefix + line }.join
|
|
231
231
|
end
|
|
232
232
|
|
|
233
|
-
#
|
|
234
|
-
#
|
|
235
|
-
#
|
|
236
|
-
# on a truncated result distinguishes "tool returned
|
|
237
|
-
# exactly this" from "tool returned much more, you're
|
|
238
|
-
# seeing a slice."
|
|
233
|
+
# The +Name(argument)+ half of a tool-call line, the argument capped to
|
|
234
|
+
# whatever the row has left — a +command+ can be a 4 kB script and a
|
|
235
|
+
# +path+ a very long one, and only a renderer knows its width.
|
|
239
236
|
#
|
|
240
|
-
#
|
|
241
|
-
#
|
|
242
|
-
# echo is a status line, not an approval artifact), then the
|
|
243
|
-
# result runs through {Pikuri::Sanitizer} so a tool observation
|
|
244
|
-
# can't smuggle an ESC or other control byte into this cyan line.
|
|
245
|
-
# The sanitizer's warnings are dropped — they belong at a
|
|
246
|
-
# confirmation prompt, not the stream narration.
|
|
237
|
+
# tool_call_line('Write', 'lib/shared/config.rb') # => "Write(lib/shared/config.rb)"
|
|
238
|
+
# tool_call_line('Status', '') # => "Status"
|
|
247
239
|
#
|
|
248
|
-
# @param
|
|
249
|
-
# @
|
|
250
|
-
#
|
|
251
|
-
def
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
240
|
+
# @param display_name [String] see {Pikuri::Tool#display_name}
|
|
241
|
+
# @param argument [String] see {Pikuri::Tool#describe_call}
|
|
242
|
+
# @return [String]
|
|
243
|
+
def tool_call_line(display_name, argument)
|
|
244
|
+
name = defang(display_name, MAX_SUMMARY_CHARS)
|
|
245
|
+
return name if argument.to_s.empty?
|
|
246
|
+
|
|
247
|
+
# "→ ", the parens, and the padding are all on this row too.
|
|
248
|
+
budget = terminal_width - @padding - name.length - 5
|
|
249
|
+
"#{name}(#{defang(argument, [budget, MIN_ARGUMENT_CHARS].max)})"
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
# Flatten whitespace, defang, and cap to +limit+ — every display string
|
|
253
|
+
# off the event stream goes through this.
|
|
254
|
+
#
|
|
255
|
+
# Whitespace flattening is safe here (a passive status line, not an
|
|
256
|
+
# approval artifact); {Pikuri::Sanitizer} is what stops a path carrying
|
|
257
|
+
# +ESC[+ or a CR from recoloring or overwriting the transcript, now that
|
|
258
|
+
# these strings no longer pass through +inspect+. Its warnings are
|
|
259
|
+
# dropped — they belong at a confirmation prompt.
|
|
260
|
+
#
|
|
261
|
+
# @param text [String, nil]
|
|
262
|
+
# @param limit [Integer] max characters, +…+ marking a cut
|
|
263
|
+
# @return [String]
|
|
264
|
+
def defang(text, limit)
|
|
265
|
+
flattened = Sanitizer.sanitize(text.to_s.gsub(/\s+/, ' ').strip).text
|
|
266
|
+
return flattened if flattened.length <= limit
|
|
267
|
+
|
|
268
|
+
"#{flattened[0, limit - 1]}…"
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
# Escape the {PROSE_SUSPECT} bytes in Sanitizer's notation, +\x1b+ /
|
|
272
|
+
# +\u{202e}+; per character, so an escape split across two streamed
|
|
273
|
+
# fragments is caught in whichever half its ESC lands.
|
|
274
|
+
#
|
|
275
|
+
# @param text [String] model- or host-authored prose
|
|
276
|
+
# @return [String]
|
|
277
|
+
def defang_prose(text)
|
|
278
|
+
text.gsub(PROSE_SUSPECT) do |ch|
|
|
279
|
+
cp = ch.ord
|
|
280
|
+
cp >= 0x202a ? format('\\u{%04x}', cp) : format('\\x%02x', cp)
|
|
281
|
+
end
|
|
282
|
+
end
|
|
255
283
|
|
|
256
|
-
|
|
284
|
+
# @return [Integer] terminal columns; 80 when stdout has no window size
|
|
285
|
+
# (a pipe, a file, a captured StringIO).
|
|
286
|
+
def terminal_width
|
|
287
|
+
$stdout.winsize[1]
|
|
288
|
+
rescue StandardError
|
|
289
|
+
80
|
|
257
290
|
end
|
|
258
291
|
end
|
|
259
292
|
end
|