1package agent
2
3import (
4 "bufio"
5 "context"
6 "encoding/json"
7 "errors"
8 "fmt"
9 "io"
10 "os"
11 "strings"
12 "sync"
13 "time"
14
15 "github.com/patriceckhart/zot/packages/agent/extensions"
16 "github.com/patriceckhart/zot/packages/agent/extproto"
17 "github.com/patriceckhart/zot/packages/agent/modes"
18 "github.com/patriceckhart/zot/packages/core"
19 "github.com/patriceckhart/zot/packages/provider"
20)
21
22// runRPCMode implements the JSON-over-stdin/stdout RPC protocol.
23//
24// Wire format: one JSON object per line in both directions.
25//
26// Commands (stdin):
27//
28// {"id":"1","type":"prompt","message":"hello","images":[]}
29// {"id":"2","type":"abort"}
30// {"id":"3","type":"compact"}
31// {"id":"4","type":"get_state"}
32// {"id":"5","type":"set_model","model":"claude-opus-4-5"}
33// {"id":"6","type":"set_reasoning","reasoning":"max"}
34// {"id":"6","type":"get_messages"}
35// {"id":"7","type":"clear"}
36// {"id":"8","type":"get_models"}
37//
38// Responses (stdout): {"type":"response","id":"1","command":"prompt","success":true}
39// Events (stdout): one JSON object per AgentEvent (same schema as --json mode).
40//
41// Auth: if $ZOTCORE_RPC_TOKEN is set, the first command must be
42// {"type":"hello","token":"..."} or the connection is closed.
43func runRPCMode(ctx context.Context, args Args, version string) error {
44 if args.NoYolo {
45 fmt.Fprintln(os.Stderr, "warning: --no-yolo has no effect in rpc mode (no interactive prompt available); tools will run without confirmation")
46 }
47 r, err := Resolve(args, true)
48 if err != nil {
49 return err
50 }
51 reportSkillDiagnostics(os.Stderr, r.SkillDiagnostics)
52
53 // Extensions: same lifecycle as interactive mode, minus the
54 // host-hooks integration. Notify/Display calls from extensions
55 // emit RPC events instead of TUI lines so any consumer can react.
56 extHooks := &rpcExtHooks{}
57 extMgr := extensions.New(ZotHome(), r.CWD, version, r.Provider, r.Model, extHooks)
58 for _, e := range extMgr.LoadExplicit(ctx, args.Exts) {
59 fmt.Fprintln(os.Stderr, "extension load:", e)
60 }
61 if !args.NoExt {
62 for _, e := range extMgr.Discover(ctx) {
63 fmt.Fprintln(os.Stderr, "extension load:", e)
64 }
65 }
66 extMgr.WaitForReady(3 * time.Second)
67 defer extMgr.Stop(2 * time.Second)
68 r.MergeExtensionTools(&extToolAdapter{mgr: extMgr})
69
70 ag := r.NewAgent()
71 wireNonInteractiveAgentExtHooks(ctx, ag, extMgr)
72
73 // /reload-ext hot-reload callback (also triggered via rpc
74 // `reload_ext` if/when added). Rebuilds the tool registry on the
75 // current agent so freshly-registered extension tools become
76 // callable without restarting the rpc process.
77 adapter := &extToolAdapter{mgr: extMgr}
78 extMgr.SetOnReload(func() {
79 resolved, err := Resolve(args, true)
80 if err != nil {
81 return
82 }
83 resolved.MergeExtensionTools(adapter)
84 ag.SetTools(resolved.ToolRegistry)
85 })
86
87 startExtensionSession(extMgr, ag, r.CWD, "")
88
89 server := &rpcServer{
90 ctx: ctx,
91 args: args,
92 agent: ag,
93 provider: r.Provider,
94 model: r.Model,
95 out: os.Stdout,
96 version: version,
97 }
98 extHooks.server = server
99 return server.run(os.Stdin)
100}
101
102// rpcExtHooks implements extensions.HostHooks for the headless RPC
103// loop. Notify and Display surface as `event` frames so any RPC
104// client can render them; Submit and Insert are no-ops because the
105// RPC loop has no editor and the prompt comes from the client.
106type rpcExtHooks struct {
107 server *rpcServer
108}
109
110func (h *rpcExtHooks) Notify(extName, level, message string) {
111 if h.server != nil {
112 h.server.writeEvent(map[string]any{
113 "type": "ext_notify",
114 "extension": extName,
115 "level": level,
116 "message": message,
117 })
118 }
119}
120func (h *rpcExtHooks) Display(extName, text string) {
121 if h.server != nil {
122 h.server.writeEvent(map[string]any{
123 "type": "ext_display",
124 "extension": extName,
125 "text": text,
126 })
127 }
128}
129func (h *rpcExtHooks) ClearNotes(extName string) {
130 if h.server != nil {
131 h.server.writeEvent(map[string]any{
132 "type": "ext_clear_notes",
133 "extension": extName,
134 })
135 }
136}
137func (h *rpcExtHooks) Submit(string) {} // ignored in rpc mode
138func (h *rpcExtHooks) SubmitSlash(string) {} // ignored in rpc mode
139func (h *rpcExtHooks) Insert(string) {} // ignored in rpc mode
140func (h *rpcExtHooks) OpenPanel(string, extproto.PanelSpec) {}
141func (h *rpcExtHooks) UpdatePanel(string, string, string, []string, string) {}
142func (h *rpcExtHooks) ClosePanel(string, string) {}
143
144type rpcServer struct {
145 ctx context.Context
146 args Args
147 agent *core.Agent
148 provider string
149 model string
150 out io.Writer
151 version string
152
153 writeMu sync.Mutex
154 turnMu sync.Mutex // serialises one prompt at a time
155 activeCancel context.CancelFunc
156 authed bool
157
158 // inFlight tracks long-running command goroutines so run() can
159 // wait for them before returning when stdin closes. Without this,
160 // piping a single 'prompt' command into 'zot rpc' would race the
161 // process exit against the agent loop and the prompt would never
162 // produce output.
163 inFlight sync.WaitGroup
164}
165
166// run reads NDJSON commands from in and dispatches them. Returns when
167// in is closed AND every in-flight long-running command (prompt /
168// compact) has finished, so a quick `echo cmd | zot rpc` invocation
169// still produces full output before the process exits.
170func (s *rpcServer) run(in io.Reader) error {
171 requireToken := os.Getenv("ZOTCORE_RPC_TOKEN") != ""
172 s.authed = !requireToken
173
174 sc := bufio.NewScanner(in)
175 sc.Buffer(make([]byte, 0, 64*1024), 16*1024*1024)
176 for sc.Scan() {
177 line := strings.TrimSpace(sc.Text())
178 if line == "" {
179 continue
180 }
181 var head struct {
182 Type string `json:"type"`
183 ID string `json:"id,omitempty"`
184 }
185 if err := json.Unmarshal([]byte(line), &head); err != nil {
186 s.writeError("", "", fmt.Sprintf("malformed json: %v", err))
187 continue
188 }
189 if !s.authed {
190 if head.Type != "hello" {
191 s.writeError(head.ID, head.Type, "auth required: send hello with token first")
192 continue
193 }
194 var hello struct {
195 Token string `json:"token"`
196 }
197 _ = json.Unmarshal([]byte(line), &hello)
198 if hello.Token != os.Getenv("ZOTCORE_RPC_TOKEN") {
199 s.writeError(head.ID, head.Type, "invalid token")
200 return fmt.Errorf("rpc: bad auth token")
201 }
202 s.authed = true
203 s.writeResponse(head.ID, head.Type, map[string]any{
204 "protocol_version": 1,
205 "version": s.version,
206 "provider": s.provider,
207 "model": s.model,
208 })
209 continue
210 }
211 s.dispatch(head.Type, head.ID, []byte(line))
212 }
213 err := sc.Err()
214 s.inFlight.Wait()
215 return err
216}
217
218// dispatch routes a command. Long-running commands (prompt, compact)
219// run on their own goroutine so the read loop stays responsive.
220func (s *rpcServer) dispatch(cmd, id string, raw []byte) {
221 // Runtime mutations must not race with prompt preparation, model/tool
222 // execution, or compaction. Do not wait here: this is also the read loop
223 // that must keep accepting abort while an extension or provider is busy.
224 switch cmd {
225 case "clear", "set_model", "set_reasoning":
226 if !s.turnMu.TryLock() {
227 s.writeError(id, cmd, "agent is busy; wait for completion or abort before retrying")
228 return
229 }
230 defer s.turnMu.Unlock()
231 }
232
233 switch cmd {
234 case "hello":
235 s.writeResponse(id, cmd, map[string]any{
236 "protocol_version": 1,
237 "version": s.version,
238 "provider": s.provider,
239 "model": s.model,
240 })
241 case "prompt":
242 var req struct {
243 Message string `json:"message"`
244 Images []struct {
245 MimeType string `json:"mime_type"`
246 Data []byte `json:"data"`
247 } `json:"images"`
248 }
249 if err := json.Unmarshal(raw, &req); err != nil {
250 s.writeError(id, cmd, err.Error())
251 return
252 }
253 s.inFlight.Add(1)
254 go func() {
255 defer s.inFlight.Done()
256 s.runPrompt(id, req.Message, req.Images)
257 }()
258
259 case "abort":
260 if c := s.takeCancel(); c != nil {
261 c()
262 }
263 s.writeResponse(id, cmd, nil)
264
265 case "compact":
266 s.inFlight.Add(1)
267 go func() {
268 defer s.inFlight.Done()
269 s.runCompact(id)
270 }()
271
272 case "get_state":
273 s.writeResponse(id, cmd, s.snapshotState())
274
275 case "get_messages":
276 s.writeResponse(id, cmd, map[string]any{
277 "messages": messagesToJSON(s.agent.Messages()),
278 })
279
280 case "clear":
281 s.agent.SetMessages(nil)
282 s.writeResponse(id, cmd, nil)
283
284 case "set_model":
285 var req struct {
286 Model string `json:"model"`
287 }
288 if err := json.Unmarshal(raw, &req); err != nil {
289 s.writeError(id, cmd, err.Error())
290 return
291 }
292 if _, err := provider.FindModel(s.provider, req.Model); err != nil {
293 s.writeError(id, cmd, err.Error())
294 return
295 }
296 s.agent.Model = req.Model
297 s.model = req.Model
298 s.writeResponse(id, cmd, map[string]any{"model": req.Model})
299
300 case "set_reasoning":
301 var req struct {
302 Reasoning string `json:"reasoning"`
303 }
304 if err := json.Unmarshal(raw, &req); err != nil {
305 s.writeError(id, cmd, err.Error())
306 return
307 }
308 reasoning := provider.NormalizeReasoning(req.Reasoning)
309 switch reasoning {
310 case "", "minimum", "low", "medium", "high", "xhigh", "max":
311 default:
312 s.writeError(id, cmd, "reasoning must be off|minimum|low|medium|high|xhigh|max")
313 return
314 }
315 s.agent.Reasoning = reasoning
316 s.writeResponse(id, cmd, map[string]any{"reasoning": reasoning})
317
318 case "get_models":
319 out := []map[string]any{}
320 for _, m := range provider.ModelsForProvider(s.provider) {
321 out = append(out, map[string]any{
322 "id": m.ID,
323 "provider": m.Provider,
324 "context_window": m.ContextWindow,
325 "max_output": m.MaxOutput,
326 "reasoning": m.Reasoning,
327 })
328 }
329 s.writeResponse(id, cmd, map[string]any{"models": out})
330
331 case "ping":
332 s.writeResponse(id, cmd, map[string]any{"pong": true})
333
334 default:
335 s.writeError(id, cmd, "unknown command")
336 }
337}
338
339// runPrompt executes a single prompt turn and streams events out.
340// Holds turnMu so a second concurrent prompt blocks until this one
341// finishes; the user can abort with the abort command.
342func (s *rpcServer) runPrompt(id, message string, images []struct {
343 MimeType string `json:"mime_type"`
344 Data []byte `json:"data"`
345}) {
346 s.turnMu.Lock()
347 defer s.turnMu.Unlock()
348
349 subCtx, cancel := context.WithCancel(s.ctx)
350 s.setCancel(cancel)
351 defer s.setCancel(nil)
352
353 s.writeResponse(id, "prompt", map[string]any{"started": true})
354
355 imgs := make([]provider.ImageBlock, 0, len(images))
356 for _, im := range images {
357 imgs = append(imgs, provider.ImageBlock{MimeType: im.MimeType, Data: im.Data})
358 }
359
360 err := s.agent.Prompt(subCtx, message, imgs, func(ev core.AgentEvent) {
361 // EvDone is emitted by the agent loop and we re-emit our own
362 // 'done' below; suppressing it here avoids duplicate frames.
363 if _, ok := ev.(core.EvDone); ok {
364 return
365 }
366 s.writeEvent(modes.EventToJSON(ev))
367 })
368 // Don't emit a stand-alone error event for cancellation; the prior
369 // turn_end with stop=aborted already carries that signal.
370 if err != nil && !errors.Is(err, context.Canceled) {
371 s.writeEvent(map[string]any{"type": "error", "message": err.Error()})
372 }
373 s.writeEvent(map[string]any{"type": "done"})
374}
375
376// runCompact mirrors runPrompt but for compaction.
377func (s *rpcServer) runCompact(id string) {
378 s.turnMu.Lock()
379 defer s.turnMu.Unlock()
380
381 subCtx, cancel := context.WithCancel(s.ctx)
382 s.setCancel(cancel)
383 defer s.setCancel(nil)
384
385 s.writeResponse(id, "compact", map[string]any{"started": true})
386 summary, err := s.agent.Compact(subCtx, 4, nil)
387 if err != nil {
388 if !errors.Is(err, context.Canceled) {
389 s.writeEvent(map[string]any{"type": "error", "message": err.Error()})
390 }
391 return
392 }
393 s.writeEvent(map[string]any{
394 "type": "compact_done",
395 "summary": summary,
396 })
397}
398
399// snapshotState builds the get_state response.
400func (s *rpcServer) snapshotState() map[string]any {
401 cum := s.agent.Cost()
402 return map[string]any{
403 "provider": s.provider,
404 "model": s.model,
405 "reasoning": s.agent.Reasoning,
406 "cwd": s.args.CWD,
407 "message_count": len(s.agent.Messages()),
408 "busy": s.activeCancel != nil,
409 "usage": map[string]any{
410 "input": cum.InputTokens,
411 "output": cum.OutputTokens,
412 "reasoning": rpcReasoningTokens(cum),
413 "cache_read": cum.CacheReadTokens,
414 "cache_write": cum.CacheWriteTokens,
415 "cost_usd": cum.CostUSD,
416 },
417 }
418}
419
420func rpcReasoningTokens(usage provider.Usage) any {
421 if !usage.ReasoningTokensKnown {
422 return nil
423 }
424 return usage.ReasoningTokens
425}
426
427// ---- write helpers (single-line JSON, mutex-guarded) ----
428
429func (s *rpcServer) writeResponse(id, cmd string, data any) {
430 frame := map[string]any{
431 "type": "response",
432 "command": cmd,
433 "success": true,
434 }
435 if id != "" {
436 frame["id"] = id
437 }
438 if data != nil {
439 frame["data"] = data
440 }
441 s.write(frame)
442}
443
444func (s *rpcServer) writeError(id, cmd, msg string) {
445 frame := map[string]any{
446 "type": "response",
447 "command": cmd,
448 "success": false,
449 "error": msg,
450 }
451 if id != "" {
452 frame["id"] = id
453 }
454 s.write(frame)
455}
456
457func (s *rpcServer) writeEvent(payload map[string]any) {
458 s.write(payload)
459}
460
461func (s *rpcServer) write(v any) {
462 b, err := json.Marshal(v)
463 if err != nil {
464 return
465 }
466 s.writeMu.Lock()
467 defer s.writeMu.Unlock()
468 _, _ = s.out.Write(b)
469 _, _ = s.out.Write([]byte("\n"))
470}
471
472func (s *rpcServer) setCancel(c context.CancelFunc) {
473 s.writeMu.Lock()
474 s.activeCancel = c
475 s.writeMu.Unlock()
476}
477
478func (s *rpcServer) takeCancel() context.CancelFunc {
479 s.writeMu.Lock()
480 defer s.writeMu.Unlock()
481 c := s.activeCancel
482 s.activeCancel = nil
483 return c
484}
485
486// messagesToJSON serialises a transcript using the same schema as the
487// --json event mode for cross-format consistency.
488func messagesToJSON(msgs []provider.Message) []map[string]any {
489 out := make([]map[string]any, 0, len(msgs))
490 for _, m := range msgs {
491 out = append(out, map[string]any{
492 "role": string(m.Role),
493 "content": modes.ContentToJSON(m.Content),
494 "time": m.Time,
495 })
496 }
497 return out
498}