Repository navigation
Expand file tree
/
Copy pathagentoutput.go
More file actions
431 lines (403 loc) · 13.3 KB
/
Copy pathagentoutput.go
File metadata and controls
431 lines (403 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"strings"
"sync"
"github.com/lsegal/glorp/agents"
"github.com/lsegal/glorp/core"
)
// outputDecoder wraps the writer an agent's stdout is forwarded to, turning
// whatever the agent prints into the human-readable lines glorp shows. Which
// decoder a run gets is the agent definition's to say, so a CLI with its own
// event stream is read by describing it rather than by adding another arm to
// runOnce.
type outputDecoder interface {
io.Writer
// Flush renders whatever the agent left without a trailing newline.
Flush() error
// recordEventsTo sends the events the decoder reads to events as well as
// rendering them (issue #715).
recordEventsTo(events jobEventRecorder)
}
// newOutputDecoder builds the decoder an output block selects, or nil when the
// agent's output is shown exactly as it is written. The definition has already
// been validated, so a format that selects the generic decoder carries its
// configuration.
func newOutputDecoder(output agents.Output, w io.Writer) outputDecoder {
switch output.Decoder() {
case agents.FormatStreamJSON:
return newClaudeJSONOutputWriter(w)
case agents.FormatJSONL:
return newJSONLOutputWriter(w, *output.JSONL)
}
return nil
}
// passthroughEventWriter forwards an agent's output unchanged while recording
// each of its lines as an output event, for an agent whose output has no
// events of its own to decode (issue #715).
type passthroughEventWriter struct {
*lineWriter
output io.Writer
events jobEventRecorder
}
func newPassthroughEventWriter(output io.Writer) *passthroughEventWriter {
w := &passthroughEventWriter{lineWriter: &lineWriter{}, output: output}
w.lineWriter.line = func(line []byte) error {
if text := strings.TrimRight(string(line), "\r"); strings.TrimSpace(text) != "" && w.events != nil {
w.events.recordJobEvent(core.JobEvent{Kind: core.JobEventOutput, Text: text})
}
return nil
}
return w
}
func (w *passthroughEventWriter) Write(p []byte) (int, error) {
if _, err := w.output.Write(p); err != nil {
return 0, err
}
return w.lineWriter.Write(p)
}
func (w *passthroughEventWriter) recordEventsTo(events jobEventRecorder) {
w.lineWriter.mu.Lock()
w.events = events
w.lineWriter.mu.Unlock()
}
// lineWriter buffers writes into whole lines, since a streaming agent's output
// arrives in chunks that do not respect event boundaries.
type lineWriter struct {
mu sync.Mutex
buffer []byte
line func(line []byte) error
}
func (w *lineWriter) Write(p []byte) (int, error) {
w.mu.Lock()
defer w.mu.Unlock()
w.buffer = append(w.buffer, p...)
for {
newline := bytes.IndexByte(w.buffer, '\n')
if newline < 0 {
break
}
line := append([]byte(nil), w.buffer[:newline]...)
w.buffer = w.buffer[newline+1:]
if err := w.line(line); err != nil {
return 0, err
}
}
return len(p), nil
}
func (w *lineWriter) Flush() error {
w.mu.Lock()
defer w.mu.Unlock()
if len(w.buffer) == 0 {
return nil
}
line := append([]byte(nil), w.buffer...)
w.buffer = nil
return w.line(line)
}
// jsonlOutputWriter decodes a line-delimited JSON event stream using the field
// paths the agent's own definition names. Most agents with a --json or
// streaming mode differ only in where the text and the tool calls live in each
// event, which is configuration rather than code.
type jsonlOutputWriter struct {
*lineWriter
config agents.JSONL
output io.Writer
ignored map[string]bool
// pending holds the text fragments of a message still being streamed,
// used only when the definition declares its text events are deltas. It
// is guarded by lineWriter.mu along with the line buffer itself.
pending []string
// events receives each decoded message and tool call as a typed job
// event (issue #715), also guarded by lineWriter.mu.
events jobEventRecorder
}
func (w *jsonlOutputWriter) recordEventsTo(events jobEventRecorder) {
w.lineWriter.mu.Lock()
w.events = events
w.lineWriter.mu.Unlock()
}
func (w *jsonlOutputWriter) record(event core.JobEvent) {
if w.events != nil {
w.events.recordJobEvent(event)
}
}
// jsonlToolCall is one tool call read out of an event.
type jsonlToolCall struct {
name string
input json.RawMessage
summary string
}
func newJSONLOutputWriter(output io.Writer, config agents.JSONL) *jsonlOutputWriter {
w := &jsonlOutputWriter{lineWriter: &lineWriter{}, config: config, output: output}
if len(config.Ignore) > 0 {
w.ignored = make(map[string]bool, len(config.Ignore))
for _, event := range config.Ignore {
w.ignored[event] = true
}
}
w.lineWriter.line = w.writeLine
return w
}
func (w *jsonlOutputWriter) writeLine(line []byte) error {
line = bytes.TrimSpace(line)
if len(line) == 0 {
return nil
}
var event any
if err := json.Unmarshal(line, &event); err != nil {
// A line that is not an event at all is still the agent talking:
// banners, warnings, and stack traces all arrive this way, and a
// dropped line is exactly the output someone debugging a failed run
// came looking for. A message being streamed as deltas ends first, so
// the banner does not land in the middle of the sentence.
if w.config.TextDelta {
if err := w.flushPending(nil); err != nil {
return err
}
}
w.record(core.JobEvent{Kind: core.JobEventOutput, Text: string(line)})
_, err = fmt.Fprintln(w.output, string(line))
return err
}
if w.skips(event) {
return nil
}
paths := w.paths(event)
if w.config.TextDelta {
return w.writeDelta(event, paths)
}
texts := jsonlStrings(event, paths.Text)
for _, text := range texts {
w.record(core.JobEvent{Kind: core.JobEventText, Text: text})
}
for _, call := range w.toolCalls(event, paths) {
texts = append(texts, "Running: "+call.summary)
w.record(core.JobEvent{Kind: core.JobEventTool, Tool: call.name, Input: call.input, Summary: call.summary})
}
if len(texts) == 0 {
// An event of a shape the definition does not describe renders
// nothing, and the ones that follow it are decoded as usual.
return nil
}
_, err := fmt.Fprintln(w.output, strings.Join(texts, "\n"))
return err
}
// writeDelta decodes one event of a stream whose text arrives as token-sized
// fragments. Buffering them and ending the line only when the text stops is
// what turns "a.txt ", "contains ", "hi" into the sentence it spells, rather
// than the three lines the event-per-line decoder would write.
func (w *jsonlOutputWriter) writeDelta(event any, paths agents.JSONLEvent) error {
fragments := jsonlFragments(event, paths.Text)
w.pending = append(w.pending, fragments...)
calls := w.toolCalls(event, paths)
if len(fragments) > 0 && len(calls) == 0 {
// The message is still being written. It ends on the next event that
// adds nothing to it, or at the end of the stream.
return nil
}
return w.flushPending(calls)
}
// flushPending writes the buffered message followed by the progress lines of
// the event that ended it. Either may be empty: an event of a shape the
// definition does not describe ends a message without adding a line of its own.
func (w *jsonlOutputWriter) flushPending(calls []jsonlToolCall) error {
var texts []string
if joined := strings.TrimSpace(strings.Join(w.pending, "")); joined != "" {
texts = append(texts, joined)
w.record(core.JobEvent{Kind: core.JobEventText, Text: joined})
}
w.pending = nil
for _, call := range calls {
texts = append(texts, "Running: "+call.summary)
w.record(core.JobEvent{Kind: core.JobEventTool, Tool: call.name, Input: call.input, Summary: call.summary})
}
if len(texts) == 0 {
return nil
}
_, err := fmt.Fprintln(w.output, strings.Join(texts, "\n"))
return err
}
// Flush ends the partial line the agent left, then writes the message the
// stream stopped in the middle of, which for a delta stream is the last thing
// the agent said.
func (w *jsonlOutputWriter) Flush() error {
if err := w.lineWriter.Flush(); err != nil {
return err
}
w.lineWriter.mu.Lock()
defer w.lineWriter.mu.Unlock()
return w.flushPending(nil)
}
// skips reports whether the event's type is one the definition drops.
func (w *jsonlOutputWriter) skips(event any) bool {
if len(w.ignored) == 0 {
return false
}
for _, kind := range jsonlStrings(event, w.config.Type) {
if w.ignored[kind] {
return true
}
}
return false
}
// toolCalls renders the event's tool calls the way Claude's decoder renders
// its own, so a progress line reads the same whichever agent produced it.
func (w *jsonlOutputWriter) toolCalls(event any, paths agents.JSONLEvent) []jsonlToolCall {
if paths.ToolName == "" {
return nil
}
// Name and input are read from the same block when their paths share a
// prefix, so a call is never rendered with another call's arguments.
prefix, name, input := splitJSONLPaths(paths.ToolName, paths.ToolInput)
var calls []jsonlToolCall
for _, block := range jsonlLookup(event, prefix) {
tool := firstJSONLString(jsonlLookup(block, name))
if tool == "" {
continue
}
if paths.ToolNamePrefix != "" {
// The name field doubles as a kind covering more than tool
// calls, so what does not carry the prefix is another kind of
// turn rather than a call rendered under its internal name.
trimmed, ok := strings.CutPrefix(tool, paths.ToolNamePrefix)
if !ok || strings.TrimSpace(trimmed) == "" {
continue
}
tool = trimmed
}
var arguments json.RawMessage
if values := jsonlLookup(block, input); paths.ToolInput != "" && len(values) > 0 {
if encoded, err := json.Marshal(values[0]); err == nil {
arguments = encoded
}
}
calls = append(calls, jsonlToolCall{name: tool, input: arguments, summary: claudeToolUseSummary(tool, arguments)})
}
return calls
}
// paths resolves the field paths that apply to one event: the definition's
// shared ones, with the override for the event's own type laid over them. It
// is what lets a stream that spreads a logical event over several typed events
// describe each of them, rather than pointing one path at all of them.
func (w *jsonlOutputWriter) paths(event any) agents.JSONLEvent {
paths := agents.JSONLEvent{
Text: w.config.Text, ToolName: w.config.ToolName,
ToolInput: w.config.ToolInput, ToolNamePrefix: w.config.ToolNamePrefix,
}
if len(w.config.Events) == 0 {
return paths
}
for _, kind := range jsonlStrings(event, w.config.Type) {
override, ok := w.config.Events[kind]
if !ok {
continue
}
if override.Text != "" {
paths.Text = override.Text
}
if override.ToolName != "" {
// A call's name, input, and prefix replace the shared ones
// together: half an override would pair this type's name with
// another type's arguments.
paths.ToolName, paths.ToolInput, paths.ToolNamePrefix = override.ToolName, override.ToolInput, override.ToolNamePrefix
}
return paths
}
return paths
}
// splitJSONLPaths returns the longest path both fields share along with what
// remains of each, so the shared part is walked once and its elements paired.
func splitJSONLPaths(name, input string) (prefix, nameRest, inputRest string) {
if input == "" {
return "", name, ""
}
nameSegments, inputSegments := strings.Split(name, "."), strings.Split(input, ".")
shared := 0
for shared < len(nameSegments)-1 && shared < len(inputSegments)-1 && nameSegments[shared] == inputSegments[shared] {
shared++
}
return strings.Join(nameSegments[:shared], "."), strings.Join(nameSegments[shared:], "."), strings.Join(inputSegments[shared:], ".")
}
// jsonlLookup walks a field path through a decoded event, returning every
// value it names. A key suffixed with [] steps into an array and continues
// into each of its elements, so one path can name a value per content block.
// An empty path names the value it was given.
func jsonlLookup(value any, path string) []any {
current := []any{value}
if path == "" {
return current
}
for _, segment := range strings.Split(path, ".") {
key, array := strings.CutSuffix(segment, "[]")
var next []any
for _, item := range current {
object, ok := item.(map[string]any)
if !ok {
continue
}
field, ok := object[key]
if !ok || field == nil {
continue
}
if !array {
next = append(next, field)
continue
}
items, ok := field.([]any)
if !ok {
continue
}
next = append(next, items...)
}
if current = next; len(current) == 0 {
return nil
}
}
return current
}
// jsonlStrings returns the non-empty strings a path names, ignoring values of
// any other type: a field the agent sometimes fills with an object is skipped
// rather than rendered as Go's idea of one.
func jsonlStrings(value any, path string) []string {
if path == "" {
return nil
}
var found []string
for _, item := range jsonlLookup(value, path) {
text, ok := item.(string)
if !ok {
continue
}
if text = strings.TrimSpace(text); text != "" {
found = append(found, text)
}
}
return found
}
// jsonlFragments returns the strings a path names without trimming them, since
// the space between two deltas is part of the message they spell. Empty
// strings are dropped: they add nothing to join.
func jsonlFragments(value any, path string) []string {
if path == "" {
return nil
}
var found []string
for _, item := range jsonlLookup(value, path) {
if text, ok := item.(string); ok && text != "" {
found = append(found, text)
}
}
return found
}
func firstJSONLString(values []any) string {
for _, value := range values {
if text, ok := value.(string); ok {
return strings.TrimSpace(text)
}
}
return ""
}