1// Package zotext is the Go SDK for writing zot extensions.
2//
3// An extension is a subprocess that talks to zot over its stdin/stdout
4// in newline-delimited JSON. This package wraps the wire format so
5// extension authors can write straightforward Go without reimplementing
6// the protocol.
7//
8// Minimal example (registers /hello and replies with a static prompt):
9//
10// package main
11//
12// import "github.com/patriceckhart/zot/pkg/zotext"
13//
14// func main() {
15// ext := zotext.New("hello", "1.0.0")
16// ext.Command("hello", "say hi", func(args string) zotext.Response {
17// return zotext.Prompt("Greet me in three different languages.")
18// })
19// ext.Run()
20// }
21//
22// Build it, drop the binary + an extension.json next to it under
23// `$ZOT_HOME/extensions/hello/`, and zot picks it up on next launch.
24//
25// The same wire format also has reference clients in TypeScript and
26// Python under examples/extensions/. Use whichever language fits.
27package zotext
28
29import (
30 "bufio"
31 "encoding/base64"
32 "encoding/json"
33 "fmt"
34 "io"
35 "os"
36 "sync"
37
38 "github.com/patriceckhart/zot/internal/extproto"
39)
40
41func base64Encode(b []byte) string { return base64.StdEncoding.EncodeToString(b) }
42
43// CommandHandler is invoked when the user runs the extension's
44// registered slash command. args is everything the user typed after
45// the command name (already trimmed). Return a Response describing
46// what zot should do next.
47type CommandHandler func(args string) Response
48
49// ToolHandler is invoked when the LLM calls a tool the extension
50// registered. args is the raw JSON object the model produced; the
51// handler is responsible for parsing/validating it. Return a
52// ToolResult describing what zot should send back to the model.
53type ToolHandler func(args json.RawMessage) ToolResult
54
55// EventHandler is called for each lifecycle event the extension
56// subscribed to via Subscribe. The handler is invoked synchronously
57// on the read goroutine; keep it quick or hand off to your own
58// worker.
59type EventHandler func(ev Event)
60
61// Event is a lifecycle notification from zot. The fields populated
62// depend on Name (the host's event_name string):
63//
64// session_start : (no extra fields)
65// turn_start : Step
66// turn_end : Stop, optional Error
67// tool_call : ToolID, ToolName, ToolArgs
68// assistant_message: Text
69type Event struct {
70 Name string
71
72 Step int
73 Stop string
74 Error string
75
76 ToolID string
77 ToolName string
78 ToolArgs json.RawMessage
79
80 Text string
81}
82
83// InterceptHandler decides whether a tool call may proceed. Return
84// (allow=true) to permit, (allow=false, reason) to refuse. The
85// reason is shown to the model as the tool's error text.
86//
87// This is the original 2-value handler. For richer control (arg
88// rewrites, turn_start / assistant_message interception, etc.)
89// register via InterceptToolCallX, InterceptTurnStart, or
90// InterceptAssistantMessage.
91type InterceptHandler func(toolName string, args json.RawMessage) (allow bool, reason string)
92
93// ToolCallDecision is the richer reply an InterceptToolCallHandler
94// can return. Zero value means "allow, pass through".
95type ToolCallDecision struct {
96 // Block refuses the call. The model sees Reason as the tool's
97 // error text and typically proposes a different action.
98 Block bool
99 // Reason is the refusal text shown to the model on Block, or a
100 // logged note when passing through (optional).
101 Reason string
102 // ModifiedArgs, when non-nil and Block is false, replaces the
103 // JSON args the tool actually runs with. Lets guards redact /
104 // augment / patch the model's request without rewriting the
105 // transcript. Must encode to a JSON object.
106 ModifiedArgs json.RawMessage
107}
108
109// ToolCallHandler is the richer form of an interceptor. Use this
110// when you want to rewrite args instead of only blocking.
111type ToolCallHandler func(toolName string, args json.RawMessage) ToolCallDecision
112
113// TurnStartDecision controls whether the next model call runs. Zero
114// value means "allow".
115type TurnStartDecision struct {
116 Block bool
117 Reason string
118}
119
120// TurnStartHandler is called before every turn's model call. Return
121// Block=true with Reason to abort the turn (shown to the user as a
122// status line). Useful for rate-limiting and business-hour gates.
123type TurnStartHandler func(step int) TurnStartDecision
124
125// AssistantMessageDecision controls the final assistant-text
126// rendering. Zero value means "allow, show as-is".
127type AssistantMessageDecision struct {
128 // Block suppresses the message from the user entirely. The
129 // transcript still records the model's original output so the
130 // model sees what it said on the next turn.
131 Block bool
132 // Reason is logged when blocking (optional).
133 Reason string
134 // ReplaceText, when non-empty and Block is false, is what the
135 // user sees. The model's original text still lives in the
136 // transcript.
137 ReplaceText string
138}
139
140// AssistantMessageHandler is called after the model's final text is
141// assembled but before it's shown. Use it to scrub secrets, expand
142// templates, enforce tone, or suppress responses entirely.
143type AssistantMessageHandler func(text string) AssistantMessageDecision
144
145// ToolResult is the extension's reply to a tool invocation. Build
146// one with TextResult, ImageResult, or directly when you need to
147// combine multiple blocks.
148type ToolResult struct {
149 Content []ToolContent
150 IsError bool
151}
152
153// ToolContent is one block of tool output. Either Text is set, or
154// MimeType+Data (base64-encoded). Use the Text/Image helpers below.
155type ToolContent struct {
156 Type string // "text" | "image"
157 Text string
158 MimeType string
159 Data string // base64
160}
161
162// Text returns a text content block.
163func Text(s string) ToolContent { return ToolContent{Type: "text", Text: s} }
164
165// Image returns an image content block. data must already be
166// base64-encoded; use ImageBytes to encode raw bytes.
167func Image(mimeType, base64Data string) ToolContent {
168 return ToolContent{Type: "image", MimeType: mimeType, Data: base64Data}
169}
170
171// ImageBytes returns an image content block from raw bytes,
172// encoding them to base64 for the wire.
173func ImageBytes(mimeType string, data []byte) ToolContent {
174 return ToolContent{Type: "image", MimeType: mimeType, Data: base64Encode(data)}
175}
176
177// TextResult returns a tool result with one text block, success.
178func TextResult(s string) ToolResult { return ToolResult{Content: []ToolContent{Text(s)}} }
179
180// TextErrorResult returns a tool result with one text block, marked
181// as an error to the model.
182func TextErrorResult(s string) ToolResult {
183 return ToolResult{Content: []ToolContent{Text(s)}, IsError: true}
184}
185
186// Response tells zot how to react to a command invocation. Construct
187// one with Prompt(), Insert(), Display(), or Noop().
188type Response struct {
189 Action string // "prompt", "insert", "display", "noop"
190 Prompt string
191 Insert string
192 Display string
193 Error string
194}
195
196// Prompt returns a Response that submits text as a fresh user message
197// to the agent (running it through the model loop as if the user had
198// typed and pressed enter).
199func Prompt(text string) Response { return Response{Action: "prompt", Prompt: text} }
200
201// Insert returns a Response that drops text into the editor at the
202// cursor without submitting.
203func Insert(text string) Response { return Response{Action: "insert", Insert: text} }
204
205// Display returns a Response that adds a one-shot styled note to the
206// chat without invoking the model. Useful for showing a result without
207// burning tokens.
208func Display(text string) Response { return Response{Action: "display", Display: text} }
209
210// Noop returns a Response that signals "I handled it, no UI change".
211// Use after pushing your own state or notifications.
212func Noop() Response { return Response{Action: "noop"} }
213
214// Errorf returns a Response that surfaces the error in the chat as a
215// red status line.
216func Errorf(format string, args ...any) Response {
217 return Response{Action: "noop", Error: fmt.Sprintf(format, args...)}
218}
219
220// Extension is one zot extension. Construct with New, register
221// commands, then call Run.
222type Extension struct {
223 name string
224 version string
225
226 in io.Reader
227 out io.Writer
228 stderr io.Writer
229 writeMu sync.Mutex
230
231 mu sync.Mutex
232 commands map[string]CommandHandler
233 descriptions []descTuple // ordered so register frames arrive in registration order
234 tools map[string]ToolHandler
235 toolDefs []toolDef // ordered so register frames arrive in registration order
236 eventHandlers map[string]EventHandler
237 eventNames []string // declared subscription order
238
239 interceptTool InterceptHandler
240 interceptToolRich ToolCallHandler
241 interceptOn bool
242 interceptTurn TurnStartHandler
243 interceptAssistant AssistantMessageHandler
244
245 // Caps reported in the hello frame.
246 caps []string
247
248 // hostInfo is filled in once HelloAck arrives.
249 host HostInfo
250}
251
252type descTuple struct {
253 name, desc string
254}
255
256type toolDef struct {
257 name string
258 description string
259 schema json.RawMessage
260}
261
262// HostInfo is what the host (zot) tells us in HelloAck. Useful for
263// extensions that want to behave differently per provider.
264type HostInfo struct {
265 ProtocolVersion int
266 ZotVersion string
267 Provider string
268 Model string
269 CWD string
270}
271
272// New constructs an Extension with the given identifier. name should
273// match the name field in extension.json.
274func New(name, version string) *Extension {
275 return &Extension{
276 name: name,
277 version: version,
278 in: os.Stdin,
279 out: os.Stdout,
280 stderr: os.Stderr,
281 commands: map[string]CommandHandler{},
282 tools: map[string]ToolHandler{},
283 eventHandlers: map[string]EventHandler{},
284 caps: []string{"commands", "tools", "events"},
285 }
286}
287
288// Host returns the HostInfo received during the hello handshake.
289// Returns the zero value if Run hasn't started yet.
290func (e *Extension) Host() HostInfo { return e.host }
291
292// Logf writes a line to the extension's stderr, which zot captures to
293// $ZOT_HOME/logs/ext-<name>.log. Use this for debug output: anything
294// you print to stdout would corrupt the JSON wire protocol.
295func (e *Extension) Logf(format string, args ...any) {
296 fmt.Fprintf(e.stderr, "["+e.name+"] "+format+"\n", args...)
297}
298
299// Command registers a slash-command handler. Call this BEFORE Run().
300// Once Run is going, when the user runs /name in zot, fn is invoked
301// with the remaining args.
302//
303// Naming conflicts with built-in commands (e.g. /help) are silently
304// shadowed by the built-in; check the user's "ext logs" if a command
305// you registered isn't taking effect.
306func (e *Extension) Command(name, description string, fn CommandHandler) {
307 e.mu.Lock()
308 e.commands[name] = fn
309 e.descriptions = append(e.descriptions, descTuple{name: name, desc: description})
310 e.mu.Unlock()
311}
312
313// Tool registers an LLM-callable tool. schema is a JSON Schema
314// object describing the tool's args (the same shape Anthropic /
315// OpenAI accept). Call this BEFORE Run(); zot folds extension tools
316// into the agent's registry once the extension's ready frame fires.
317//
318// Naming conflicts with built-in tools (read, write, edit, bash,
319// skill) are silently shadowed by the built-in.
320func (e *Extension) Tool(name, description string, schema json.RawMessage, fn ToolHandler) {
321 e.mu.Lock()
322 e.tools[name] = fn
323 e.toolDefs = append(e.toolDefs, toolDef{name: name, description: description, schema: schema})
324 e.mu.Unlock()
325}
326
327// On subscribes to a lifecycle event. fn is called for each
328// notification; the same name can only have one handler (later
329// registrations replace earlier ones). Recognised names:
330// session_start, turn_start, turn_end, tool_call,
331// assistant_message.
332func (e *Extension) On(name string, fn EventHandler) {
333 e.mu.Lock()
334 if _, exists := e.eventHandlers[name]; !exists {
335 e.eventNames = append(e.eventNames, name)
336 }
337 e.eventHandlers[name] = fn
338 e.mu.Unlock()
339}
340
341// InterceptToolCall registers a guard that runs immediately before
342// each tool call. Returning (false, reason) refuses the call; the
343// model sees reason as the tool error text. Multiple extensions can
344// install interceptors; if any one refuses, the call is blocked.
345//
346// Use this to build permission gates: refuse `bash` calls containing
347// `rm -rf`, ask the user for confirmation on dangerous patterns,
348// audit-log every call, etc.
349//
350// For the richer form (arg rewrites, structured decisions) use
351// InterceptToolCallX.
352func (e *Extension) InterceptToolCall(fn InterceptHandler) {
353 e.mu.Lock()
354 e.interceptTool = fn
355 e.interceptOn = true
356 e.mu.Unlock()
357}
358
359// InterceptToolCallX is the richer variant. Return a ToolCallDecision
360// to block, allow, or rewrite args mid-flight. If both
361// InterceptToolCall and InterceptToolCallX are set, the X form wins.
362func (e *Extension) InterceptToolCallX(fn ToolCallHandler) {
363 e.mu.Lock()
364 e.interceptToolRich = fn
365 e.interceptOn = true
366 e.mu.Unlock()
367}
368
369// InterceptTurnStart registers a guard that runs before every turn's
370// model call. Return Block=true with Reason to abort the turn.
371// Useful for deny-by-default gates and usage quotas.
372func (e *Extension) InterceptTurnStart(fn TurnStartHandler) {
373 e.mu.Lock()
374 e.interceptTurn = fn
375 e.mu.Unlock()
376}
377
378// InterceptAssistantMessage registers a guard that runs after the
379// model's final text is assembled but before it's shown. Return
380// Block=true to suppress entirely, or ReplaceText to rewrite what
381// the user sees. The model's original output stays in the transcript.
382func (e *Extension) InterceptAssistantMessage(fn AssistantMessageHandler) {
383 e.mu.Lock()
384 e.interceptAssistant = fn
385 e.mu.Unlock()
386}
387
388// Notify pushes an info-level status note into zot's chat without
389// requiring a slash command from the user.
390func (e *Extension) Notify(level, message string) {
391 _ = e.send(extproto.NotifyFromExt{
392 Type: "notify",
393 Level: level,
394 Message: message,
395 })
396}
397
398// Run starts the protocol loop. Blocks until stdin closes (zot has
399// shut us down). Returns the first fatal error, or nil on clean exit.
400func (e *Extension) Run() error {
401 // Send hello, then re-announce all commands (covers Command calls
402 // made before Run, and is also fine for those made after via
403 // Command()'s direct send).
404 if err := e.send(extproto.HelloFromExt{
405 Type: "hello",
406 Name: e.name,
407 Version: e.version,
408 Capabilities: e.caps,
409 }); err != nil {
410 return err
411 }
412 e.mu.Lock()
413 descs := append([]descTuple(nil), e.descriptions...)
414 toolDefs := append([]toolDef(nil), e.toolDefs...)
415 eventNames := append([]string(nil), e.eventNames...)
416 interceptTool := e.interceptOn
417 interceptTurn := e.interceptTurn != nil
418 interceptAsst := e.interceptAssistant != nil
419 e.mu.Unlock()
420 for _, d := range descs {
421 _ = e.send(extproto.RegisterCommandFromExt{
422 Type: "register_command",
423 Name: d.name,
424 Description: d.desc,
425 })
426 }
427 for _, td := range toolDefs {
428 _ = e.send(extproto.RegisterToolFromExt{
429 Type: "register_tool",
430 Name: td.name,
431 Description: td.description,
432 Schema: td.schema,
433 })
434 }
435 var intercepts []string
436 if interceptTool {
437 intercepts = append(intercepts, "tool_call")
438 }
439 if interceptTurn {
440 intercepts = append(intercepts, "turn_start")
441 }
442 if interceptAsst {
443 intercepts = append(intercepts, "assistant_message")
444 }
445 if len(eventNames) > 0 || len(intercepts) > 0 {
446 _ = e.send(extproto.SubscribeFromExt{
447 Type: "subscribe",
448 Events: eventNames,
449 Intercept: intercepts,
450 })
451 }
452 // Sentinel: tells the host that all initial registrations have
453 // been flushed and the agent registry can be built. Never block
454 // on this; if the host doesn't act on it, registrations still
455 // land in time for the typical use case.
456 _ = e.send(extproto.ReadyFromExt{Type: "ready"})
457
458 scanner := bufio.NewScanner(e.in)
459 scanner.Buffer(make([]byte, 0, 64*1024), 4*1024*1024)
460 for scanner.Scan() {
461 line := scanner.Bytes()
462 var frame extproto.Frame
463 if err := json.Unmarshal(line, &frame); err != nil {
464 e.Logf("malformed frame from host: %v", err)
465 continue
466 }
467 switch frame.Type {
468 case "hello_ack":
469 var ack extproto.HelloAckFromHost
470 if err := json.Unmarshal(line, &ack); err == nil {
471 e.host = HostInfo{
472 ProtocolVersion: ack.ProtocolVersion,
473 ZotVersion: ack.ZotVersion,
474 Provider: ack.Provider,
475 Model: ack.Model,
476 CWD: ack.CWD,
477 }
478 }
479 case "command_invoked":
480 var ci extproto.CommandInvokedFromHost
481 if err := json.Unmarshal(line, &ci); err != nil {
482 continue
483 }
484 e.mu.Lock()
485 fn := e.commands[ci.Name]
486 e.mu.Unlock()
487 if fn == nil {
488 e.respond(ci.ID, Errorf("no handler for /%s", ci.Name))
489 continue
490 }
491 // Run the handler on its own goroutine so a slow handler
492 // doesn't block subsequent commands. Order isn't promised.
493 go func(id string, fn CommandHandler, args string) {
494 defer func() {
495 if r := recover(); r != nil {
496 e.respond(id, Errorf("panic: %v", r))
497 }
498 }()
499 resp := fn(args)
500 e.respond(id, resp)
501 }(ci.ID, fn, ci.Args)
502 case "tool_call":
503 var tc extproto.ToolCallFromHost
504 if err := json.Unmarshal(line, &tc); err != nil {
505 continue
506 }
507 e.mu.Lock()
508 fn := e.tools[tc.Name]
509 e.mu.Unlock()
510 if fn == nil {
511 e.respondTool(tc.ID, TextErrorResult(fmt.Sprintf("no handler for tool %q", tc.Name)))
512 continue
513 }
514 go func(id string, fn ToolHandler, args json.RawMessage) {
515 defer func() {
516 if r := recover(); r != nil {
517 e.respondTool(id, TextErrorResult(fmt.Sprintf("panic: %v", r)))
518 }
519 }()
520 res := fn(args)
521 e.respondTool(id, res)
522 }(tc.ID, fn, tc.Args)
523 case "event":
524 var ef extproto.EventFromHost
525 if err := json.Unmarshal(line, &ef); err != nil {
526 continue
527 }
528 e.mu.Lock()
529 handler := e.eventHandlers[ef.Event]
530 e.mu.Unlock()
531 if handler != nil {
532 func() {
533 defer func() {
534 if r := recover(); r != nil {
535 e.Logf("event %s handler panicked: %v", ef.Event, r)
536 }
537 }()
538 handler(Event{
539 Name: ef.Event, Step: ef.Step, Stop: ef.Stop,
540 Error: ef.Error, ToolID: ef.ToolID, ToolName: ef.ToolName,
541 ToolArgs: ef.ToolArgs, Text: ef.Text,
542 })
543 }()
544 }
545 case "event_intercept":
546 var ei extproto.EventInterceptFromHost
547 if err := json.Unmarshal(line, &ei); err != nil {
548 continue
549 }
550 go e.dispatchIntercept(ei)
551 case "shutdown":
552 _ = e.send(extproto.ShutdownAckFromExt{Type: "shutdown_ack"})
553 return nil
554 default:
555 e.Logf("unknown frame type %q", frame.Type)
556 }
557 }
558 return scanner.Err()
559}
560
561// respond serialises a CommandResponseFromExt for the given id.
562func (e *Extension) respond(id string, r Response) {
563 if r.Action == "" {
564 r.Action = "noop"
565 }
566 _ = e.send(extproto.CommandResponseFromExt{
567 Type: "command_response",
568 ID: id,
569 Action: r.Action,
570 Prompt: r.Prompt,
571 Insert: r.Insert,
572 Display: r.Display,
573 Error: r.Error,
574 })
575}
576
577// respondTool serialises a ToolResultFromExt for the given id.
578func (e *Extension) respondTool(id string, r ToolResult) {
579 blocks := make([]extproto.ContentBlock, 0, len(r.Content))
580 for _, c := range r.Content {
581 blocks = append(blocks, extproto.ContentBlock{
582 Type: c.Type,
583 Text: c.Text,
584 MimeType: c.MimeType,
585 Data: c.Data,
586 })
587 }
588 _ = e.send(extproto.ToolResultFromExt{
589 Type: "tool_result",
590 ID: id,
591 Content: blocks,
592 IsError: r.IsError,
593 })
594}
595
596// send marshals v + LF and writes it under a mutex (so concurrent
597// goroutines don't interleave bytes on stdout).
598func (e *Extension) send(v any) error {
599 b, err := extproto.Encode(v)
600 if err != nil {
601 return err
602 }
603 e.writeMu.Lock()
604 defer e.writeMu.Unlock()
605 _, err = e.out.Write(b)
606 return err
607}
608
609// dispatchIntercept runs the per-event handler (tool_call / turn_start
610// / assistant_message) on its own goroutine, catches panics, and
611// always emits exactly one event_intercept_response. Called from the
612// Run loop.
613func (e *Extension) dispatchIntercept(ei extproto.EventInterceptFromHost) {
614 defer func() {
615 if r := recover(); r != nil {
616 _ = e.send(extproto.EventInterceptResponseFromExt{
617 Type: "event_intercept_response",
618 ID: ei.ID,
619 Block: true,
620 Reason: fmt.Sprintf("intercept panic: %v", r),
621 })
622 }
623 }()
624
625 resp := extproto.EventInterceptResponseFromExt{
626 Type: "event_intercept_response",
627 ID: ei.ID,
628 }
629
630 switch ei.Event {
631 case "tool_call":
632 e.mu.Lock()
633 rich := e.interceptToolRich
634 plain := e.interceptTool
635 e.mu.Unlock()
636 if rich != nil {
637 d := rich(ei.ToolName, ei.ToolArgs)
638 resp.Block = d.Block
639 resp.Reason = d.Reason
640 if !d.Block && len(d.ModifiedArgs) > 0 && json.Valid(d.ModifiedArgs) {
641 resp.ModifiedArgs = d.ModifiedArgs
642 }
643 } else if plain != nil {
644 allow, reason := plain(ei.ToolName, ei.ToolArgs)
645 resp.Block = !allow
646 resp.Reason = reason
647 }
648 case "turn_start":
649 e.mu.Lock()
650 fn := e.interceptTurn
651 e.mu.Unlock()
652 if fn != nil {
653 d := fn(ei.Step)
654 resp.Block = d.Block
655 resp.Reason = d.Reason
656 }
657 case "assistant_message":
658 e.mu.Lock()
659 fn := e.interceptAssistant
660 e.mu.Unlock()
661 if fn != nil {
662 d := fn(ei.Text)
663 resp.Block = d.Block
664 resp.Reason = d.Reason
665 if !d.Block && d.ReplaceText != "" && d.ReplaceText != ei.Text {
666 resp.ReplaceText = d.ReplaceText
667 }
668 }
669 }
670
671 _ = e.send(resp)
672}