v0.4.4 e5506c1

json.go RawHistoryFolder

1package modes
2
3import (
4	"context"
5	"encoding/json"
6	"fmt"
7	"io"
8
9	"github.com/patriceckhart/zot/packages/core"
10	"github.com/patriceckhart/zot/packages/provider"
11)
12
13// RunJSON runs the agent to completion, writing one JSON object per
14// AgentEvent as newline-delimited JSON.
15func RunJSON(ctx context.Context, ag *core.Agent, prompt string, images []provider.ImageBlock, out io.Writer) error {
16	enc := json.NewEncoder(out)
17	write := func(v any) {
18		_ = enc.Encode(v)
19	}
20
21	var runErr error
22	sink := func(ev core.AgentEvent) {
23		write(EventToJSON(ev))
24	}
25
26	if err := ag.Prompt(ctx, prompt, images, sink); err != nil {
27		runErr = err
28	}
29
30	if runErr != nil {
31		fmt.Fprintln(out, `{"type":"error","message":`+jsonString(runErr.Error())+`}`)
32	}
33	return runErr
34}
35
36// EventToJSON converts an AgentEvent to a JSON-friendly map. The on-wire
37// schema is deliberately simple and flat. Exported so the RPC mode can
38// reuse the same serialisation as `zot --json`.
39func EventToJSON(ev core.AgentEvent) map[string]any {
40	m := map[string]any{"type": ev.Type()}
41	switch e := ev.(type) {
42	case core.EvTurnStart:
43		m["step"] = e.Step
44	case core.EvUserMessage:
45		m["content"] = ContentToJSON(e.Message.Content)
46		m["time"] = e.Message.Time
47	case core.EvAssistantMessage:
48		m["content"] = ContentToJSON(e.Message.Content)
49		m["time"] = e.Message.Time
50	case core.EvTextDelta:
51		m["delta"] = e.Delta
52	case core.EvToolUseStart:
53		m["id"] = e.ID
54		m["name"] = e.Name
55	case core.EvToolUseArgs:
56		m["id"] = e.ID
57		m["delta"] = e.Delta
58	case core.EvToolUseEnd:
59		m["id"] = e.ID
60	case core.EvToolCall:
61		m["id"] = e.ID
62		m["name"] = e.Name
63		var args any
64		_ = json.Unmarshal(e.Args, &args)
65		m["args"] = args
66	case core.EvToolProgress:
67		m["id"] = e.ID
68		m["text"] = e.Text
69	case core.EvToolResult:
70		m["id"] = e.ID
71		m["is_error"] = e.Result.IsError
72		m["content"] = ContentToJSON(e.Result.Content)
73	case core.EvUsage:
74		m["input"] = e.Usage.InputTokens
75		m["output"] = e.Usage.OutputTokens
76		m["reasoning"] = nullableReasoningTokens(e.Usage)
77		m["cache_read"] = e.Usage.CacheReadTokens
78		m["cache_write"] = e.Usage.CacheWriteTokens
79		m["cost_usd"] = e.Usage.CostUSD
80		m["cumulative"] = map[string]any{
81			"input":       e.Cumulative.InputTokens,
82			"output":      e.Cumulative.OutputTokens,
83			"reasoning":   nullableReasoningTokens(e.Cumulative),
84			"cache_read":  e.Cumulative.CacheReadTokens,
85			"cache_write": e.Cumulative.CacheWriteTokens,
86			"cost_usd":    e.Cumulative.CostUSD,
87		}
88	case core.EvTurnEnd:
89		m["stop"] = string(e.Stop)
90		if e.Err != nil {
91			m["error"] = e.Err.Error()
92		}
93	}
94	return m
95}
96
97// ContentToJSON serialises a transcript content slice into the same
98// shape used in EventToJSON. Exported alongside EventToJSON for the
99// RPC mode.
100func ContentToJSON(blocks []provider.Content) []map[string]any {
101	out := make([]map[string]any, 0, len(blocks))
102	for _, b := range blocks {
103		switch v := b.(type) {
104		case provider.TextBlock:
105			out = append(out, map[string]any{"type": "text", "text": v.Text})
106		case provider.ImageBlock:
107			out = append(out, map[string]any{"type": "image", "mime_type": v.MimeType, "bytes": len(v.Data)})
108		case provider.ToolCallBlock:
109			var args any
110			_ = json.Unmarshal(v.Arguments, &args)
111			out = append(out, map[string]any{"type": "tool_call", "id": v.ID, "name": v.Name, "args": args})
112		case provider.ToolResultBlock:
113			out = append(out, map[string]any{
114				"type":     "tool_result",
115				"call_id":  v.CallID,
116				"is_error": v.IsError,
117				"content":  ContentToJSON(v.Content),
118			})
119		}
120	}
121	return out
122}
123
124func nullableReasoningTokens(usage provider.Usage) any {
125	if !usage.ReasoningTokensKnown {
126		return nil
127	}
128	return usage.ReasoningTokens
129}
130
131func jsonString(s string) string {
132	b, _ := json.Marshal(s)
133	return string(b)
134}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 476

↑↓ moveenter openesc close