pikuri-workspace 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 +23 -16
- data/lib/pikuri/workspace/confirmer/terminal.rb +252 -0
- data/lib/pikuri/workspace/confirmer.rb +184 -123
- data/lib/pikuri/workspace/edit.rb +261 -101
- data/lib/pikuri/workspace/extension.rb +163 -0
- data/lib/pikuri/workspace/file_list.rb +125 -0
- data/lib/pikuri/workspace/filesystem.rb +160 -446
- data/lib/pikuri/workspace/listing.rb +204 -0
- data/lib/pikuri/workspace/read.rb +139 -225
- data/lib/pikuri/workspace/search/glob.rb +310 -0
- data/lib/pikuri/workspace/search/grep.rb +353 -0
- data/lib/pikuri/workspace/search/utils.rb +173 -0
- data/lib/pikuri/workspace/workspace.rb +105 -0
- data/lib/pikuri/workspace/write.rb +129 -78
- data/lib/pikuri/workspace/write_gate.rb +74 -0
- data/lib/pikuri-workspace.rb +8 -10
- data/prompts/pikuri-files.txt +6 -0
- metadata +32 -11
- data/lib/pikuri/workspace/glob.rb +0 -322
- data/lib/pikuri/workspace/grep.rb +0 -350
- data/lib/pikuri/workspace/project_root.rb +0 -102
|
@@ -4,117 +4,71 @@ require 'ruby_llm'
|
|
|
4
4
|
|
|
5
5
|
module Pikuri
|
|
6
6
|
module Workspace
|
|
7
|
-
# The +read+ tool
|
|
8
|
-
#
|
|
9
|
-
#
|
|
10
|
-
# tool's, so ruby_llm sees nothing special about it. Same shape as
|
|
11
|
-
# the +agent+ tool from +pikuri-subagents+ — workspace is captured by
|
|
12
|
-
# the +execute+ closure at construction.
|
|
7
|
+
# The +read+ tool as a {Pikuri::Tool} subclass: +Read.new(workspace: ws)+
|
|
8
|
+
# produces a tool whose {Pikuri::Tool#to_ruby_llm_tool} wiring is identical
|
|
9
|
+
# to any bundled tool's. Workspace is captured by the +execute+ closure.
|
|
13
10
|
#
|
|
14
11
|
# == Output format
|
|
15
12
|
#
|
|
16
|
-
# cat-n: each line
|
|
17
|
-
#
|
|
18
|
-
#
|
|
19
|
-
#
|
|
20
|
-
#
|
|
21
|
-
#
|
|
22
|
-
#
|
|
23
|
-
#
|
|
24
|
-
# boundaries precisely).
|
|
13
|
+
# cat-n: each line +"%6d\t%s"+ (6-col line number, tab, content). Chosen for
|
|
14
|
+
# training-data familiarity — +cat -n+ is everywhere, so even small local
|
|
15
|
+
# models recognize it; opencode's +"<n>: "+ saves tokens but trades
|
|
16
|
+
# familiarity, pi omits numbers (cheapest, but no range citation or {Edit}
|
|
17
|
+
# boundaries).
|
|
18
|
+
# The numbering and {DESCRIPTION}'s read-a-larger-window nudge were
|
|
19
|
+
# confirmed as a pair by an outside-model review (Grok 4.6, 2026-08) —
|
|
20
|
+
# don't trade either back for tokens.
|
|
25
21
|
#
|
|
26
22
|
# == Truncation rules
|
|
27
23
|
#
|
|
28
|
-
#
|
|
29
|
-
# {Pikuri::
|
|
30
|
-
# {Pikuri::
|
|
31
|
-
#
|
|
32
|
-
#
|
|
24
|
+
# Line/byte windowing is delegated to {Pikuri::FileType.read_as_text_paged},
|
|
25
|
+
# which returns a {Pikuri::Extractor::Page} this tool renders through
|
|
26
|
+
# {Pikuri::Tool::Result.page} (the one paging trailer every reader shares).
|
|
27
|
+
# Two limits, first to fire wins: a line
|
|
28
|
+
# limit ({DEFAULT_LIMIT}, overridable via +limit+) and a byte cap
|
|
29
|
+
# ({MAX_BYTES}, not a parameter, bypassable by paging via +offset+). Lines
|
|
30
|
+
# over {MAX_LINE_LENGTH} are truncated with {LINE_TRUNCATION_MARKER}. (The
|
|
31
|
+
# constants alias the +PAGE_*+ ones on {Pikuri::Extractor}.)
|
|
33
32
|
#
|
|
34
|
-
#
|
|
35
|
-
# * *Byte cap* — {MAX_BYTES} bytes of input content; not exposed as a
|
|
36
|
-
# parameter. Bypassable in practice by paging via +offset+.
|
|
33
|
+
# == PDF and other extracted formats
|
|
37
34
|
#
|
|
38
|
-
#
|
|
39
|
-
# are
|
|
40
|
-
#
|
|
41
|
-
#
|
|
42
|
-
#
|
|
43
|
-
#
|
|
44
|
-
#
|
|
45
|
-
#
|
|
46
|
-
# Which formats read as text is the {Pikuri::Extractor} registry's
|
|
47
|
-
# business, not this tool's: with pikuri-pdf's extractor
|
|
48
|
-
# registered, PDFs are claimed by their +%PDF-+ magic prefix ahead
|
|
49
|
-
# of the binary refusal and extracted with one synthetic
|
|
50
|
-
# +"--- Page N ---"+ header line per page (see
|
|
51
|
-
# +Pikuri::Extractors::PDF+); a gem plugging another extractor
|
|
52
|
-
# into the registry extends this tool for free. Extraction is lazy
|
|
53
|
-
# where the format allows (+extract_lines+): reading the first
|
|
54
|
-
# window of a 500-page PDF parses only the pages the window needs.
|
|
55
|
-
# Formats without a lazy line shape (HTML) are extracted in full
|
|
56
|
-
# and then windowed. Line numbers in PDF output are for citation
|
|
57
|
-
# back to the user only; PDFs are not editable through {Edit}.
|
|
58
|
-
#
|
|
59
|
-
# PDFs with no extractable text (scanned images, empty documents) come
|
|
60
|
-
# back with an LLM-actionable hint string rather than an empty
|
|
61
|
-
# observation. Encrypted / malformed / XFA-form PDFs surface as
|
|
62
|
-
# +"Error: ..."+ — same convention as other tool errors the model
|
|
63
|
-
# can react to. No OCR.
|
|
35
|
+
# Which formats read as text is the {Pikuri::Extractor} registry's business:
|
|
36
|
+
# with pikuri-pdf registered, PDFs are claimed by +%PDF-+ ahead of the
|
|
37
|
+
# binary refusal and extracted with one +"--- Page N ---"+ header per page,
|
|
38
|
+
# lazily where the format allows (a 500-page PDF's first window parses only
|
|
39
|
+
# its pages). PDF line numbers are for citation only — PDFs aren't editable.
|
|
40
|
+
# No extractable text (scanned/empty) → an LLM-actionable hint;
|
|
41
|
+
# encrypted/malformed → +"Error: ..."+. No OCR.
|
|
64
42
|
#
|
|
65
43
|
# == Image attachments
|
|
66
44
|
#
|
|
67
|
-
# PNG
|
|
68
|
-
#
|
|
69
|
-
#
|
|
70
|
-
#
|
|
71
|
-
#
|
|
72
|
-
#
|
|
73
|
-
#
|
|
74
|
-
#
|
|
75
|
-
#
|
|
76
|
-
#
|
|
77
|
-
# +offset+ / +limit+ are ignored on the image path — there's no
|
|
78
|
-
# line-paging concept. The hard size cap is {MAX_IMAGE_BYTES}; files
|
|
79
|
-
# above that come back as +"Error: image too large…"+ rather than
|
|
80
|
-
# being silently encoded into a payload the upstream API would
|
|
81
|
-
# reject. No auto-resize: pikuri doesn't pull in an image-processing
|
|
82
|
-
# dep for one tool's ergonomics. See +IDEAS.md+ for the deferred
|
|
83
|
-
# auto-resize discussion.
|
|
45
|
+
# PNG/JPEG/GIF/WebP (magic-byte detected) route to {.format_image} ahead of
|
|
46
|
+
# the binary sniff, answering the one {Pikuri::Tool::Result} in pikuri with
|
|
47
|
+
# +attachments+: a metadata note ("Read image: …") as +content+ for the
|
|
48
|
+
# model to cite, plus the file as an attachment the model looks at.
|
|
49
|
+
# +offset+/+limit+ are ignored (no line paging). Over
|
|
50
|
+
# {Pikuri::Attachment::MAX_IMAGE_BYTES} → +"Error: image too large…"+ rather than a payload the
|
|
51
|
+
# provider rejects; no auto-resize (see +design/ideas/coding-agent.md+). Vision
|
|
52
|
+
# capability is not checked — a non-vision model's provider error is
|
|
53
|
+
# something the LLM reacts to, cheaper than tracking model metadata.
|
|
84
54
|
#
|
|
85
|
-
#
|
|
86
|
-
# Sending an image to a non-vision model produces a provider error
|
|
87
|
-
# the LLM can react to on the next turn; coupling Read to model
|
|
88
|
-
# capability metadata (notoriously incomplete for local servers)
|
|
89
|
-
# buys less than the friction it adds.
|
|
55
|
+
# == Refusals (all as +"Error: ..."+)
|
|
90
56
|
#
|
|
91
|
-
#
|
|
57
|
+
# Path outside the workspace ({Filesystem::Error}), not found, EACCES, a
|
|
58
|
+
# directory (refused, but the refusal carries the directory's own entries —
|
|
59
|
+
# see {.directory_refusal}), image over {Pikuri::Attachment::MAX_IMAGE_BYTES}, binary content
|
|
60
|
+
# (nothing in the registry claims it — the {Pikuri::FileType.binary?}
|
|
61
|
+
# heuristic catches archives/artifacts with no extension list), offset past
|
|
62
|
+
# EOF.
|
|
92
63
|
#
|
|
93
|
-
#
|
|
94
|
-
#
|
|
95
|
-
#
|
|
96
|
-
# * Path is a directory → +"Error: ... use the glob tool"+, keeping
|
|
97
|
-
# directory listing as the glob tool's responsibility (Step 9).
|
|
98
|
-
# * Image larger than {MAX_IMAGE_BYTES} → +"Error: image too large…"+,
|
|
99
|
-
# leaving the model to pick a different file or ask the user to
|
|
100
|
-
# resize.
|
|
101
|
-
# * Binary content → nothing in the {Pikuri::Extractor} registry
|
|
102
|
-
# claims it ({Pikuri::Extractor::Passthrough} declines on the
|
|
103
|
-
# {Pikuri::FileType.binary?} heuristic: any +NUL+ byte or a
|
|
104
|
-
# sample dense in control characters). Catches archives and
|
|
105
|
-
# compiled artifacts without an extension list to maintain.
|
|
106
|
-
# Registered extractors (pikuri-pdf's PDF, pikuri-extractors'
|
|
107
|
-
# office formats) claim their bytes ahead of that refusal;
|
|
108
|
-
# images are intercepted here via {Pikuri::FileType.detect_mime}
|
|
109
|
-
# before extraction is attempted — see above.
|
|
110
|
-
# * Offset past EOF → +"Error: offset N is beyond end of file (M lines total)"+.
|
|
64
|
+
# Sharing: +P_one_agent+ — every successful read marks the path on its
|
|
65
|
+
# {Workspace}, which *is* the read-before-edit gate's state, so a shared
|
|
66
|
+
# instance would let one agent's read unlock another's {Edit}.
|
|
111
67
|
class Read < Pikuri::Tool
|
|
112
|
-
#
|
|
113
|
-
# with +
|
|
114
|
-
# description and specs reference pointing at the single source.
|
|
68
|
+
# These alias the +PAGE_*+ constants on {Pikuri::Extractor} (one source of
|
|
69
|
+
# truth, shared with +Fileindex::Read+).
|
|
115
70
|
|
|
116
|
-
# @return [Integer] default value of the +limit+ parameter (
|
|
117
|
-
# of lines to read per call).
|
|
71
|
+
# @return [Integer] default value of the +limit+ parameter (lines per call).
|
|
118
72
|
DEFAULT_LIMIT = Pikuri::Extractor::PAGE_DEFAULT_LIMIT
|
|
119
73
|
|
|
120
74
|
# @return [Integer] per-line character cap; longer lines are
|
|
@@ -125,30 +79,16 @@ module Pikuri
|
|
|
125
79
|
# {MAX_LINE_LENGTH}.
|
|
126
80
|
LINE_TRUNCATION_MARKER = Pikuri::Extractor::PAGE_LINE_TRUNCATION_MARKER
|
|
127
81
|
|
|
128
|
-
# @return [Integer] hard byte cap on input content
|
|
129
|
-
#
|
|
130
|
-
#
|
|
131
|
-
# per-line +"%6d\t"+ prefix.
|
|
82
|
+
# @return [Integer] hard byte cap on input content per call (line bytes +
|
|
83
|
+
# the joining newline; rendered output is larger due to the +"%6d\t"+
|
|
84
|
+
# prefix).
|
|
132
85
|
MAX_BYTES = Pikuri::Extractor::PAGE_MAX_BYTES
|
|
133
86
|
|
|
134
|
-
# @return [String] human-readable
|
|
135
|
-
#
|
|
136
|
-
MAX_BYTES_LABEL = "#{MAX_BYTES / 1024} KB"
|
|
87
|
+
# @return [String] human-readable {Pikuri::Attachment::MAX_IMAGE_BYTES} for refusal messages.
|
|
88
|
+
MAX_IMAGE_BYTES_LABEL = "#{Pikuri::Attachment::MAX_IMAGE_BYTES / (1024 * 1024)} MB"
|
|
137
89
|
|
|
138
|
-
#
|
|
139
|
-
#
|
|
140
|
-
# OpenAI / Gemini. Above this we refuse rather than encode a
|
|
141
|
-
# payload the provider would reject.
|
|
142
|
-
MAX_IMAGE_BYTES = 5 * 1024 * 1024
|
|
143
|
-
|
|
144
|
-
# @return [String] human-readable form of {MAX_IMAGE_BYTES} for
|
|
145
|
-
# refusal messages.
|
|
146
|
-
MAX_IMAGE_BYTES_LABEL = "#{MAX_IMAGE_BYTES / (1024 * 1024)} MB"
|
|
147
|
-
|
|
148
|
-
# Description shown to the LLM. Follows the opencode-shape (summary
|
|
149
|
-
# + +Usage:+ bullets) prescribed by the project's tool-description
|
|
150
|
-
# convention. Per-parameter constraints (defaults, format) live in
|
|
151
|
-
# the parameter descriptions, not here.
|
|
90
|
+
# Description shown to the LLM (opencode-shape). Per-parameter constraints
|
|
91
|
+
# live in the parameter descriptions.
|
|
152
92
|
#
|
|
153
93
|
# @return [String]
|
|
154
94
|
DESCRIPTION = <<~DESC
|
|
@@ -161,13 +101,12 @@ module Pikuri
|
|
|
161
101
|
- PDFs are text-extracted page-by-page with `--- Page N ---` markers in the output. Cite pages back to the user from those markers. PDFs cannot be modified with `edit`.
|
|
162
102
|
- PNG / JPEG / GIF / WebP files are attached as images you can see directly, alongside a short text note with the path and size. Requires a vision-capable model; on a text-only model the provider will reject the call. Images cannot be modified with `edit`.
|
|
163
103
|
- Other binary files (archives, compiled artifacts) are refused; this tool reads text otherwise.
|
|
164
|
-
- Directories are refused — use the `glob` tool to list files.
|
|
165
104
|
- If unsure of the path, use `glob` first to look up filenames.
|
|
166
105
|
- Avoid tiny repeated slices — if you need more context, read a larger window.
|
|
167
106
|
DESC
|
|
168
107
|
|
|
169
|
-
# @param workspace [
|
|
170
|
-
#
|
|
108
|
+
# @param workspace [Workspace] captured for path resolution and
|
|
109
|
+
# read-ledger marking (+resolve_for_read+ / +mark_read+).
|
|
171
110
|
# @return [Read]
|
|
172
111
|
def initialize(workspace:)
|
|
173
112
|
super(
|
|
@@ -187,41 +126,35 @@ module Pikuri
|
|
|
187
126
|
},
|
|
188
127
|
execute: ->(path:, offset: 1, limit: DEFAULT_LIMIT) {
|
|
189
128
|
Read.read(workspace: workspace, path: path, offset: offset, limit: limit)
|
|
190
|
-
}
|
|
129
|
+
},
|
|
130
|
+
trifecta_legs: Tool::TrifectaLegs.new(
|
|
131
|
+
private: workspace.filesystem.private?,
|
|
132
|
+
untrusted: workspace.filesystem.trusted? ? :none : :hard,
|
|
133
|
+
egress_payload_review: :no_egress
|
|
134
|
+
)
|
|
191
135
|
)
|
|
192
136
|
end
|
|
193
137
|
|
|
194
|
-
#
|
|
195
|
-
#
|
|
196
|
-
# +workspace_factory:+ — the parent's instance is rebuilt for
|
|
197
|
-
# the sub-agent's fresh workspace so paths resolve against the
|
|
198
|
-
# right root.
|
|
138
|
+
# Resolve +path+ against +workspace+, refuse directories/binaries/missing
|
|
139
|
+
# files, return the cat-n slice.
|
|
199
140
|
#
|
|
200
|
-
# @param workspace [
|
|
201
|
-
# @
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
#
|
|
207
|
-
#
|
|
208
|
-
# +"Error: ..."+ observation.
|
|
209
|
-
#
|
|
210
|
-
# @param workspace [Filesystem]
|
|
211
|
-
# @param path [String] raw path as supplied by the LLM
|
|
212
|
-
# @param offset [Integer] 1-indexed line number to start at
|
|
213
|
-
# @param limit [Integer] maximum lines to return
|
|
214
|
-
# @return [String, RubyLLM::Content] tool observation. Text and PDF
|
|
215
|
-
# paths return a +String+; supported images return a
|
|
216
|
-
# +RubyLLM::Content+ with the file attached so the model receives
|
|
217
|
-
# the image inline. See "Image attachments" above.
|
|
141
|
+
# @param workspace [Workspace]
|
|
142
|
+
# @param path [String] raw path from the LLM
|
|
143
|
+
# @param offset [Integer] 1-indexed start line
|
|
144
|
+
# @param limit [Integer] maximum lines
|
|
145
|
+
# @return [Tool::Result] text/PDF → the cat-n slice (+"Read 214 lines"+);
|
|
146
|
+
# a supported image → a note with the file in
|
|
147
|
+
# {Tool::Result#attachments} (see "Image attachments"); else
|
|
148
|
+
# {Pikuri::Tool::Result.error}
|
|
218
149
|
def self.read(workspace:, path:, offset:, limit:)
|
|
219
|
-
return "
|
|
220
|
-
return "
|
|
150
|
+
return Tool::Result.error("offset must be >= 1, got #{offset}") if offset < 1
|
|
151
|
+
return Tool::Result.error("limit must be >= 1, got #{limit}") if limit < 1
|
|
221
152
|
|
|
222
153
|
resolved = workspace.resolve_for_read(path)
|
|
223
|
-
return
|
|
224
|
-
|
|
154
|
+
return Tool::Result.error(workspace.not_found(path)) unless resolved.exist?
|
|
155
|
+
if resolved.directory?
|
|
156
|
+
return Tool::Result.error(directory_refusal(workspace: workspace, path: path, resolved: resolved))
|
|
157
|
+
end
|
|
225
158
|
|
|
226
159
|
mime = Pikuri::FileType.detect_mime(resolved)
|
|
227
160
|
return format_image(path: path, resolved: resolved, mime: mime) if mime&.start_with?('image/')
|
|
@@ -230,104 +163,85 @@ module Pikuri
|
|
|
230
163
|
resolved, offset: offset, limit: limit,
|
|
231
164
|
max_bytes: MAX_BYTES, max_line_length: MAX_LINE_LENGTH
|
|
232
165
|
)
|
|
233
|
-
|
|
166
|
+
# Record the read so a later edit/overwrite clears the read-before-edit
|
|
167
|
+
# gate. Any successful text/PDF read counts (even a partial window) —
|
|
168
|
+
# the exact-bytes guard is Edit's +old_string+ match; the ledger only
|
|
169
|
+
# attests "engaged via read".
|
|
170
|
+
workspace.mark_read(resolved)
|
|
171
|
+
Tool::Result.page(page)
|
|
234
172
|
rescue Filesystem::Error => e
|
|
235
|
-
|
|
173
|
+
Tool::Result.error(e.message)
|
|
236
174
|
rescue Errno::EACCES => e
|
|
237
|
-
"
|
|
175
|
+
Tool::Result.error("cannot read #{path}: #{e.message}")
|
|
238
176
|
rescue ArgumentError
|
|
239
177
|
# Nothing in the Extractor registry claimed the content —
|
|
240
178
|
# read_as_text_paged's binary refusal (directories and images
|
|
241
179
|
# were already handled above).
|
|
242
|
-
"
|
|
180
|
+
Tool::Result.error("cannot read binary file: #{path}")
|
|
243
181
|
rescue RuntimeError => e
|
|
244
182
|
# Extraction failure (malformed / unsupported PDF, ...)
|
|
245
183
|
# surfaced by read_as_text_paged.
|
|
246
|
-
|
|
184
|
+
Tool::Result.error(e.message)
|
|
247
185
|
end
|
|
248
186
|
|
|
249
|
-
#
|
|
250
|
-
#
|
|
251
|
-
#
|
|
252
|
-
# page on. PDF pages carry +"--- Page N ---"+ marker lines from
|
|
253
|
-
# the extractor; the +kind+ only changes trailer wording here.
|
|
187
|
+
# Refuses, but hands back the directory's entries so the model recovers
|
|
188
|
+
# with no extra round-trip — the {Search::Utils.oversized_root_refusal}
|
|
189
|
+
# move:
|
|
254
190
|
#
|
|
255
|
-
#
|
|
256
|
-
#
|
|
257
|
-
|
|
258
|
-
if page.lines.empty?
|
|
259
|
-
return empty_message(page) if page.total_lines.zero?
|
|
260
|
-
|
|
261
|
-
return "Error: offset #{page.start_line} is beyond end of file " \
|
|
262
|
-
"(#{page.total_lines} lines total)"
|
|
263
|
-
end
|
|
264
|
-
|
|
265
|
-
noun = page.kind == :pdf ? 'PDF ' : ''
|
|
266
|
-
last = page.start_line + page.lines.length - 1
|
|
267
|
-
body = page.lines.each_with_index.map { |line, i| format("%6d\t%s", i + page.start_line, line) }.join("\n")
|
|
268
|
-
|
|
269
|
-
trailer =
|
|
270
|
-
if page.byte_capped
|
|
271
|
-
"(Output capped at #{MAX_BYTES_LABEL}. Showing #{noun}lines #{page.start_line}-#{last}. " \
|
|
272
|
-
"Use offset=#{last + 1} to continue.)"
|
|
273
|
-
elsif page.more
|
|
274
|
-
total = page.total_lines ? " of #{page.total_lines}" : ''
|
|
275
|
-
"(Showing #{noun}lines #{page.start_line}-#{last}#{total}. " \
|
|
276
|
-
"Use offset=#{last + 1} to continue.)"
|
|
277
|
-
else
|
|
278
|
-
"(End of #{page.kind == :pdf ? 'PDF' : 'file'} - total #{page.total_lines} lines)"
|
|
279
|
-
end
|
|
280
|
-
|
|
281
|
-
"#{body}\n\n#{trailer}"
|
|
282
|
-
end
|
|
283
|
-
private_class_method :render_page
|
|
284
|
-
|
|
285
|
-
# The empty-document message, worded by content kind: a scanned /
|
|
286
|
-
# text-free PDF gets an LLM-actionable hint rather than the
|
|
287
|
-
# plain-file "(Empty file)".
|
|
191
|
+
# Error: lib is a directory, not a file. It contains (directories end with `/`):
|
|
192
|
+
# pikuri/ [3 dirs, 1 file]
|
|
193
|
+
# version.rb 48B
|
|
288
194
|
#
|
|
289
|
-
#
|
|
290
|
-
#
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
195
|
+
# **Keep this out of {DESCRIPTION}.** It is error recovery, not a
|
|
196
|
+
# documented capability: teaching +read+ as a second way to list hands
|
|
197
|
+
# the model a two-tools-no-criterion coin flip (+D_directory_listing+).
|
|
198
|
+
#
|
|
199
|
+
# Never marks the read ledger — a directory can't be edited, so counting
|
|
200
|
+
# it would clear the read-before-edit gate for a path nobody read.
|
|
201
|
+
#
|
|
202
|
+
# @param workspace [Workspace]
|
|
203
|
+
# @param path [String] raw path from the LLM (for the message)
|
|
204
|
+
# @param resolved [Pathname] workspace-resolved path
|
|
205
|
+
# @return [String] the refusal message, carrying the depth-1 listing —
|
|
206
|
+
# text for {Tool::Result.error} to wrap, not an observation itself
|
|
207
|
+
def self.directory_refusal(workspace:, path:, resolved:)
|
|
208
|
+
base = "Error: #{path} is a directory, not a file."
|
|
209
|
+
listing = Listing.render(resolved, filesystem: workspace.filesystem)
|
|
210
|
+
return "#{base} It is empty." if listing.empty?
|
|
211
|
+
|
|
212
|
+
"#{base} It contains (directories end with `/`):\n#{listing}"
|
|
213
|
+
rescue SystemCallError => e
|
|
214
|
+
"#{base} It cannot be listed: #{e.message}"
|
|
297
215
|
end
|
|
298
|
-
private_class_method :
|
|
299
|
-
|
|
300
|
-
# Build a multimodal observation: a
|
|
301
|
-
#
|
|
302
|
-
#
|
|
303
|
-
#
|
|
216
|
+
private_class_method :directory_refusal
|
|
217
|
+
|
|
218
|
+
# Build a multimodal observation: a metadata note ("Read image: …") plus
|
|
219
|
+
# the file attached, both arriving in the same +tool_result+ turn. Over
|
|
220
|
+
# {Pikuri::Attachment::MAX_IMAGE_BYTES} we refuse (an +"Error: ..."+ the model reacts to)
|
|
221
|
+
# rather than encoding a payload the provider rejects. The bytes are read
|
|
222
|
+
# here, once, at most one past the cap — what the model sees is what was
|
|
223
|
+
# checked, and an oversized file is never loaded whole.
|
|
304
224
|
#
|
|
305
|
-
#
|
|
306
|
-
#
|
|
307
|
-
#
|
|
308
|
-
#
|
|
309
|
-
# is something the model can react to (pick a different file or
|
|
310
|
-
# ask the user to resize). Reading the file size via
|
|
311
|
-
# +Pathname#size+ avoids loading the bytes just to discover they
|
|
312
|
-
# were over-cap; +RubyLLM::Attachment+ does the actual base64
|
|
313
|
-
# encoding lazily, once the message is being formatted for the
|
|
314
|
-
# provider.
|
|
225
|
+
# The one place a {Tool::Result} carries +attachments+: the attached
|
|
226
|
+
# bytes *are* the observation, so the display
|
|
227
|
+
# line is derived from mime and size rather than authored here (see
|
|
228
|
+
# {Pikuri::Tool::Result}'s +== Text or an attachment+).
|
|
315
229
|
#
|
|
316
|
-
# @param path [String] raw path
|
|
317
|
-
# the metadata note)
|
|
230
|
+
# @param path [String] raw path from the LLM (for the note)
|
|
318
231
|
# @param resolved [Pathname] workspace-resolved path
|
|
319
|
-
# @param mime [String] MIME type
|
|
320
|
-
# @return [
|
|
321
|
-
#
|
|
232
|
+
# @param mime [String] MIME type
|
|
233
|
+
# @return [Tool::Result] attachment-carrying on success;
|
|
234
|
+
# {Pikuri::Tool::Result.error} on size refusal
|
|
322
235
|
def self.format_image(path:, resolved:, mime:)
|
|
323
|
-
|
|
324
|
-
if
|
|
325
|
-
return "
|
|
326
|
-
|
|
236
|
+
bytes = File.binread(resolved, Pikuri::Attachment::MAX_IMAGE_BYTES + 1)
|
|
237
|
+
if bytes.bytesize > Pikuri::Attachment::MAX_IMAGE_BYTES
|
|
238
|
+
return Tool::Result.error("image too large to inline: #{path} is " \
|
|
239
|
+
"#{resolved.size} bytes; max #{MAX_IMAGE_BYTES_LABEL}.")
|
|
327
240
|
end
|
|
328
241
|
|
|
329
|
-
|
|
330
|
-
|
|
242
|
+
image = Pikuri::Attachment.new(mime: mime, filename: resolved.basename.to_s, bytes: bytes)
|
|
243
|
+
note = "Read image: #{path} (#{bytes.bytesize} bytes, #{mime})"
|
|
244
|
+
Tool::Result.new(content: note, attachments: [image], summary: Pikuri::Tool.one_line(note))
|
|
331
245
|
end
|
|
332
246
|
private_class_method :format_image
|
|
333
247
|
end
|