1// Package extensions implements the host side of zot's subprocess
2// extension protocol. The Manager discovers extensions in well-known
3// directories, spawns each one, completes the hello handshake, and
4// routes slash commands to the right extension.
5//
6// Each extension is its own process, communicating with zot over its
7// stdin/stdout in newline-delimited JSON. Stderr is redirected to a
8// per-extension log file under $ZOT_HOME/logs/. Crashing one
9// extension does not affect the others or the host.
10//
11// See docs/extensions.md for the user-facing reference and
12// packages/agent/extproto for the wire format.
13package extensions
14
15import (
16 "bufio"
17 "context"
18 "encoding/json"
19 "errors"
20 "fmt"
21 "io"
22 "os"
23 "os/exec"
24 "path/filepath"
25 "sort"
26 "strings"
27 "sync"
28 "syscall"
29 "time"
30
31 "github.com/patriceckhart/zot/packages/agent/extproto"
32 "github.com/patriceckhart/zot/packages/tui"
33)
34
35// Manifest is the extension.json file shipped alongside an
36// extension's executable. It tells zot how to launch the extension
37// and provides display metadata.
38type Manifest struct {
39 Name string `json:"name"`
40 Version string `json:"version,omitempty"`
41 Exec string `json:"exec"` // executable path, relative to manifest dir
42 Args []string `json:"args,omitempty"` // extra argv passed to exec
43 Language string `json:"language,omitempty"` // informational ("go", "python", "typescript", ...)
44 Enabled *bool `json:"enabled,omitempty"` // nil = enabled
45 Description string `json:"description,omitempty"`
46}
47
48// IsEnabled returns the manifest's effective enabled state. Default
49// is true so adding a new extension folder Just Works without an
50// extra zot ext enable command.
51func (m Manifest) IsEnabled() bool {
52 return m.Enabled == nil || *m.Enabled
53}
54
55// Extension is a running extension subprocess and the metadata zot
56// tracks about it.
57type Extension struct {
58 Manifest Manifest
59 Dir string // absolute path to extension directory
60 LogPath string
61
62 cmd *exec.Cmd
63 stdin io.WriteCloser
64 stdout io.ReadCloser
65 logFile *os.File
66 helloAck bool
67 commands []extproto.RegisterCommandFromExt
68 tools []extproto.RegisterToolFromExt
69
70 // readyCh is closed when the extension sends a ReadyFromExt
71 // frame, or when the host gives up waiting (registrationGrace).
72 readyCh chan struct{}
73 readyOnce sync.Once
74
75 // pending command invocations waiting on a CommandResponseFromExt
76 // keyed by the id we sent in CommandInvokedFromHost.
77 // pendingTool is the same idea for tool calls.
78 // pendingIntercept is the same idea for event_intercept calls.
79 mu sync.Mutex
80 pending map[string]chan extproto.CommandResponseFromExt
81 pendingTool map[string]chan extproto.ToolResultFromExt
82 pendingIntercept map[string]chan extproto.EventInterceptResponseFromExt
83
84 // lastFrameTime is updated by the read loop on every frame it
85 // processes. Used by the auto-ready idle watchdog so legacy
86 // extensions (no `ready` frame) don't pin the WaitForReady wait
87 // to its full grace.
88 lastFrameTime time.Time
89
90 // eventSubs and interceptSubs are the sets of event names this
91 // extension subscribed to via SubscribeFromExt. Used by
92 // EmitEvent / InterceptToolCall to filter recipients.
93 eventSubs map[string]struct{}
94 interceptSubs map[string]struct{}
95
96 autoReady bool
97 readyTimedOut bool
98 diagnostics []string
99}
100
101// HostHooks is the small interface the manager calls back into the
102// running TUI through. Decouples extensions from interactive.go.
103type HostHooks interface {
104 // Notify pushes an ext-originated status message into the chat.
105 // Level is one of "info", "warn", "error", "success".
106 Notify(extName, level, message string)
107
108 // Submit feeds text as if the user had typed and pressed enter,
109 // running it through the agent loop.
110 Submit(text string)
111
112 // SubmitSlash runs text as a slash command in the TUI as if the
113 // user had typed it (text must start with '/'). Unlike Submit it
114 // does NOT run text through the model. Wired to the spontaneous
115 // submit_slash frame from extensions; ignored when the host is
116 // not interactive.
117 SubmitSlash(text string)
118
119 // Insert places text at the cursor in the editor.
120 Insert(text string)
121
122 // Display appends a one-shot styled note to the chat without
123 // invoking the model and without writing to the transcript.
124 Display(extName, text string)
125
126 // ClearNotes removes any notes previously pushed by extName via
127 // Notify/Display so transient status lines do not stack forever.
128 ClearNotes(extName string)
129
130 OpenPanel(extName string, spec extproto.PanelSpec)
131 UpdatePanel(extName, panelID, title string, lines []string, footer string)
132 ClosePanel(extName, panelID string)
133}
134
135type commandRegistration struct {
136 ext *Extension
137 name string
138}
139
140// Manager owns every extension subprocess for the lifetime of zot.
141type Manager struct {
142 zotHome string
143 cwd string
144 zotVersion string
145 provider string
146 model string
147 hooks HostHooks
148
149 mu sync.RWMutex
150 ext map[string]*Extension // keyed by manifest name
151
152 // commandIndex maps a lower-cased slash-command name (without the
153 // leading /) to its canonical registration. First-come-first-served:
154 // later registrations that differ only by case are dropped with a warning.
155 commandIndex map[string]commandRegistration
156
157 // toolIndex maps an extension-defined tool name to its owning
158 // extension. Same first-come-first-served rule as commandIndex.
159 toolIndex map[string]*Extension
160
161 // explicitPaths remembers ad-hoc paths passed via --ext so
162 // Reload can respawn them alongside the discovered set.
163 explicitPaths []string
164
165 // onReload, if set, is invoked after a successful Reload. Used
166 // by the host so it can rebuild the agent's tool registry with
167 // the freshly-registered extension tools.
168 onReload func()
169}
170
171// New constructs an empty Manager. Call Discover to populate it from
172// the on-disk extension directories.
173func New(zotHome, cwd, zotVersion, provider, model string, hooks HostHooks) *Manager {
174 return &Manager{
175 zotHome: zotHome,
176 cwd: cwd,
177 zotVersion: zotVersion,
178 provider: provider,
179 model: model,
180 hooks: hooks,
181 ext: map[string]*Extension{},
182 commandIndex: map[string]commandRegistration{},
183 toolIndex: map[string]*Extension{},
184 }
185}
186
187// Discover scans the global and project extension dirs and starts
188// every extension whose manifest is enabled. Spawns happen in
189// parallel so a slow runtime (e.g. `npx tsx` cold-start, ~1.5s)
190// doesn't block other extensions from starting. Returns a slice of
191// errors encountered (one per extension); a single bad extension
192// doesn't abort the rest.
193func (m *Manager) Discover(ctx context.Context) []error {
194 type loadJob struct {
195 dir string
196 }
197 var jobs []loadJob
198 seenDirs := map[string]bool{} // dedup by basename so project wins
199 for _, dir := range m.searchDirs() {
200 entries, err := os.ReadDir(dir)
201 if err != nil {
202 continue // missing directory is fine
203 }
204 for _, e := range entries {
205 if !e.IsDir() {
206 continue
207 }
208 if seenDirs[e.Name()] {
209 continue // higher-priority location already queued
210 }
211 seenDirs[e.Name()] = true
212 jobs = append(jobs, loadJob{dir: filepath.Join(dir, e.Name())})
213 }
214 }
215
216 var wg sync.WaitGroup
217 errCh := make(chan error, len(jobs))
218 for _, j := range jobs {
219 wg.Add(1)
220 go func(extDir string) {
221 defer wg.Done()
222 if err := m.loadOne(ctx, extDir); err != nil {
223 errCh <- fmt.Errorf("%s: %w", extDir, err)
224 }
225 }(j.dir)
226 }
227 wg.Wait()
228 close(errCh)
229
230 var errs []error
231 for e := range errCh {
232 errs = append(errs, e)
233 }
234 return errs
235}
236
237// searchDirs returns the directories the discoverer walks, in
238// priority order: project-local first (so a project can override
239// global behavior), then global.
240func (m *Manager) searchDirs() []string {
241 var dirs []string
242 if m.cwd != "" {
243 dirs = append(dirs, filepath.Join(m.cwd, ".zot", "extensions"))
244 }
245 if m.zotHome != "" {
246 dirs = append(dirs, filepath.Join(m.zotHome, "extensions"))
247 }
248 return dirs
249}
250
251// loadOne reads a single extension's manifest and, if enabled,
252// spawns its subprocess + completes the hello handshake.
253func (m *Manager) loadOne(ctx context.Context, dir string) error {
254 manifestPath := filepath.Join(dir, "extension.json")
255 raw, err := os.ReadFile(manifestPath)
256 if err != nil {
257 return fmt.Errorf("read manifest: %w", err)
258 }
259 var mf Manifest
260 if err := json.Unmarshal(raw, &mf); err != nil {
261 return fmt.Errorf("parse manifest: %w", err)
262 }
263 if mf.Name == "" {
264 return errors.New("manifest: name is required")
265 }
266 hasTheme := HasExtensionTheme(dir)
267 if mf.Exec == "" && !hasTheme {
268 return errors.New("manifest: exec is required")
269 }
270 if !mf.IsEnabled() {
271 // Quietly skip disabled extensions; zot ext list will show them.
272 return nil
273 }
274
275 m.mu.RLock()
276 _, dup := m.ext[mf.Name]
277 m.mu.RUnlock()
278 if dup {
279 // Project-local copy already won; ignore the global one.
280 return nil
281 }
282
283 ext := &Extension{
284 Manifest: mf,
285 Dir: dir,
286 readyCh: make(chan struct{}),
287 pending: map[string]chan extproto.CommandResponseFromExt{},
288 pendingTool: map[string]chan extproto.ToolResultFromExt{},
289 pendingIntercept: map[string]chan extproto.EventInterceptResponseFromExt{},
290 eventSubs: map[string]struct{}{},
291 interceptSubs: map[string]struct{}{},
292 }
293 if mf.Exec != "" {
294 if err := m.spawn(ctx, ext); err != nil {
295 return err
296 }
297 } else {
298 ext.readyOnce.Do(func() { close(ext.readyCh) })
299 }
300
301 m.mu.Lock()
302 m.ext[mf.Name] = ext
303 // Note: ext.commands and ext.tools may be empty here — they're
304 // populated by the read loop as register_* frames arrive after
305 // hello. Indexing happens in the read loop too. Discover()'s
306 // caller can WaitForReady() before relying on the registries.
307 m.mu.Unlock()
308 return nil
309}
310
311// LoadExplicit loads each path as an ad-hoc extension. Used for
312// `zot --ext <path>` so extension authors can iterate on a working
313// copy without having to `zot ext install` after every change.
314//
315// Loaded BEFORE Discover so explicit paths win on name conflicts
316// against installed extensions. Spawns happen in parallel like the
317// regular discovery path; errors are returned per path.
318// HasExtensionTheme reports whether an extension directory contains a theme
319// file in one of the supported extension-owned locations.
320func HasExtensionTheme(dir string) bool {
321 for _, file := range []string{"theme.json", filepath.Join("themes", "theme.json")} {
322 if _, err := os.Stat(filepath.Join(dir, file)); err == nil {
323 return true
324 }
325 }
326 return false
327}
328
329func (m *Manager) ThemeOptions() []tui.ThemeOption {
330 m.mu.RLock()
331 defer m.mu.RUnlock()
332 names := make([]string, 0, len(m.ext))
333 for name := range m.ext {
334 names = append(names, name)
335 }
336 sort.Strings(names)
337 var out []tui.ThemeOption
338 for _, name := range names {
339 ext := m.ext[name]
340 for _, file := range []string{"theme.json", "themes/theme.json"} {
341 path := filepath.Join(ext.Dir, file)
342 value := path
343 if opt, ok := tui.ThemeOptionFromFile(path, value, "extension "+ext.Manifest.Name); ok {
344 out = append(out, opt)
345 }
346 }
347 }
348 return out
349}
350
351func (m *Manager) LoadExplicit(ctx context.Context, paths []string) []error {
352 if len(paths) == 0 {
353 return nil
354 }
355
356 var wg sync.WaitGroup
357 errCh := make(chan error, len(paths))
358 absPaths := make([]string, 0, len(paths))
359 for _, p := range paths {
360 abs, err := filepath.Abs(p)
361 if err != nil {
362 errCh <- fmt.Errorf("%s: %w", p, err)
363 continue
364 }
365 absPaths = append(absPaths, abs)
366 wg.Add(1)
367 go func(extDir string) {
368 defer wg.Done()
369 if err := m.loadOne(ctx, extDir); err != nil {
370 errCh <- fmt.Errorf("%s: %w", extDir, err)
371 }
372 }(abs)
373 }
374 wg.Wait()
375 close(errCh)
376
377 m.mu.Lock()
378 m.explicitPaths = append(m.explicitPaths, absPaths...)
379 m.mu.Unlock()
380
381 var errs []error
382 for e := range errCh {
383 errs = append(errs, e)
384 }
385 return errs
386}
387
388// SetOnReload registers a callback fired after a successful Reload.
389// Hosts use it to rebuild the agent's tool registry with freshly-
390// registered extension tools.
391func (m *Manager) SetOnReload(fn func()) {
392 m.mu.Lock()
393 m.onReload = fn
394 m.mu.Unlock()
395}
396
397// ReloadStats summarises the outcome of Reload.
398type ReloadStats struct {
399 Stopped int // how many old processes were torn down
400 Loaded int // how many new processes reached spawn
401 Ready int // how many of those signalled ready in time
402 Errors []error // non-fatal per-extension errors
403}
404
405// Reload tears down every running extension, re-reads the manifests
406// from disk, respawns everyone (including the --ext paths remembered
407// from LoadExplicit), waits up to grace for ready signals, and
408// invokes the SetOnReload callback so the host can rebuild its tool
409// registry. The manager's internal maps are cleared before the
410// new load to ensure a clean slate.
411//
412// Safe to call concurrently with normal host operations: the lock is
413// released between stop and respawn so pending InvokeTool / Invoke
414// calls on the old processes get a clean error as their stdin
415// closes.
416func (m *Manager) Reload(ctx context.Context, grace time.Duration) ReloadStats {
417 stats := ReloadStats{}
418
419 // Snapshot and remember the explicit paths before we wipe state.
420 m.mu.Lock()
421 old := m.ext
422 explicit := append([]string(nil), m.explicitPaths...)
423 stats.Stopped = len(old)
424 m.ext = map[string]*Extension{}
425 m.commandIndex = map[string]commandRegistration{}
426 m.toolIndex = map[string]*Extension{}
427 m.explicitPaths = nil
428 callback := m.onReload
429 m.mu.Unlock()
430
431 // Graceful stop of the old set (reuses Stop's shutdown logic,
432 // but Stop re-reads m.ext which is now empty, so we replicate
433 // the small shutdown loop here on the snapshot).
434 oldExts := make([]*Extension, 0, len(old))
435 for _, ext := range old {
436 oldExts = append(oldExts, ext)
437 }
438 stopExtensions(oldExts, grace)
439
440 // Fresh load. Explicit paths first (they still win on conflict).
441 if errs := m.LoadExplicit(ctx, explicit); len(errs) > 0 {
442 stats.Errors = append(stats.Errors, errs...)
443 }
444 if errs := m.Discover(ctx); len(errs) > 0 {
445 stats.Errors = append(stats.Errors, errs...)
446 }
447
448 m.mu.RLock()
449 stats.Loaded = len(m.ext)
450 m.mu.RUnlock()
451
452 // Wait for ready frames. Use the same 3s grace zot uses at
453 // startup so the reload feels no slower than a cold boot.
454 readyDeadline := time.Now().Add(grace)
455 if time.Until(readyDeadline) < 3*time.Second {
456 readyDeadline = time.Now().Add(3 * time.Second)
457 }
458 m.WaitForReady(time.Until(readyDeadline))
459
460 m.mu.RLock()
461 for _, ext := range m.ext {
462 select {
463 case <-ext.readyCh:
464 stats.Ready++
465 default:
466 }
467 }
468 m.mu.RUnlock()
469
470 if callback != nil {
471 callback()
472 }
473
474 return stats
475}
476
477// WaitForReady blocks until every loaded extension has signalled
478// ReadyFromExt, or the grace period expires for the slowest one.
479//
480// Waits run in parallel: total time is max(per-extension wait), not
481// sum. Without this, a single slow extension (e.g. `npx tsx` cold)
482// would gate every other extension's wait too and zot startup would
483// scale linearly with the number of slow runtimes installed.
484//
485// Call after Discover and before relying on tool registrations.
486func (m *Manager) WaitForReady(grace time.Duration) {
487 m.mu.RLock()
488 exts := make([]*Extension, 0, len(m.ext))
489 for _, e := range m.ext {
490 exts = append(exts, e)
491 }
492 m.mu.RUnlock()
493
494 deadline := time.After(grace)
495 var wg sync.WaitGroup
496 for _, ext := range exts {
497 wg.Add(1)
498 go func(ext *Extension) {
499 defer wg.Done()
500 select {
501 case <-ext.readyCh:
502 case <-deadline:
503 ext.mu.Lock()
504 ext.readyTimedOut = true
505 ext.diagnostics = append(ext.diagnostics, "timed out waiting for ready frame")
506 ext.mu.Unlock()
507 fmt.Fprintf(ext.logFile, "[zot] timed out waiting for ready frame; proceeding\n")
508 ext.readyOnce.Do(func() { close(ext.readyCh) })
509 }
510 }(ext)
511 }
512 wg.Wait()
513}
514
515// spawn launches the subprocess, hooks up pipes, logs stderr, and
516// runs the synchronous portion of the hello handshake. Asynchronous
517// frames are processed in a goroutine started here.
518func (m *Manager) spawn(ctx context.Context, ext *Extension) error {
519 logsDir := filepath.Join(m.zotHome, "logs")
520 _ = os.MkdirAll(logsDir, 0o755)
521 logPath := filepath.Join(logsDir, "ext-"+ext.Manifest.Name+".log")
522 logFile, err := os.OpenFile(logPath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o644)
523 if err != nil {
524 return fmt.Errorf("open log: %w", err)
525 }
526 ext.LogPath = logPath
527 ext.logFile = logFile
528 fmt.Fprintf(logFile, "\n[zot] starting %s/%s at %s\n", ext.Manifest.Name, ext.Manifest.Version, time.Now().Format(time.RFC3339))
529
530 // Exec resolution rules:
531 // - absolute path: used as-is.
532 // - starts with "." (./ or ../): resolved relative to ext.Dir.
533 // - bare name (no path separator): looked up via $PATH so
534 // "node", "npx", "python3",
535 // "tsx" etc. work without
536 // forcing absolute paths.
537 // - other relative form (foo/bar): resolved relative to ext.Dir.
538 execPath := ext.Manifest.Exec
539 switch {
540 case filepath.IsAbs(execPath):
541 // keep
542 case strings.HasPrefix(execPath, "."+string(filepath.Separator)) ||
543 strings.HasPrefix(execPath, ".."+string(filepath.Separator)) ||
544 execPath == "." || execPath == "..":
545 execPath = filepath.Join(ext.Dir, execPath)
546 case strings.ContainsRune(execPath, filepath.Separator):
547 execPath = filepath.Join(ext.Dir, execPath)
548 default:
549 // bare name: leave as-is for exec.LookPath via exec.Command.
550 }
551 cmd := exec.CommandContext(ctx, execPath, ext.Manifest.Args...)
552 cmd.Dir = ext.Dir
553 cmd.Stderr = logFile
554 isolateExtensionProcess(cmd)
555
556 stdin, err := cmd.StdinPipe()
557 if err != nil {
558 return err
559 }
560 stdout, err := cmd.StdoutPipe()
561 if err != nil {
562 return err
563 }
564 if err := cmd.Start(); err != nil {
565 return fmt.Errorf("spawn: %w", err)
566 }
567 ext.cmd = cmd
568 ext.stdin = stdin
569 ext.stdout = stdout
570
571 // Hello handshake. Read the extension's HelloFromExt synchronously
572 // so we can fail fast on a broken extension; everything after is
573 // processed in the read goroutine.
574 scanner := bufio.NewScanner(stdout)
575 scanner.Buffer(make([]byte, 0, 64*1024), 4*1024*1024)
576 if !scanner.Scan() {
577 return fmt.Errorf("extension exited before hello: %w", scanner.Err())
578 }
579 var hello extproto.HelloFromExt
580 if err := json.Unmarshal(scanner.Bytes(), &hello); err != nil {
581 return fmt.Errorf("parse hello: %w", err)
582 }
583 if hello.Type != "hello" || hello.Name == "" {
584 return fmt.Errorf("first frame must be hello (got %q)", hello.Type)
585 }
586 // Trust the manifest's name; ignore mismatch from the hello.
587 ext.helloAck = true
588
589 ack, _ := extproto.Encode(extproto.HelloAckFromHost{
590 Type: "hello_ack",
591 ProtocolVersion: extproto.ProtocolVersion,
592 ZotVersion: m.zotVersion,
593 Provider: m.provider,
594 Model: m.model,
595 CWD: m.cwd,
596 ExtensionDir: ext.Dir,
597 DataDir: ext.Dir,
598 })
599 if _, err := stdin.Write(ack); err != nil {
600 return fmt.Errorf("send hello_ack: %w", err)
601 }
602
603 // Spin up the read loop now that the handshake is done.
604 go m.readLoop(ext, scanner)
605
606 // Compatibility shim: extensions built against the phase-1 SDK
607 // don't send a ready frame. Watch the read loop's frame arrival
608 // rate; if nothing's arrived for readyIdleWindow we treat the
609 // extension as ready so WaitForReady doesn't burn the full grace
610 // on every startup. Newer extensions still trigger the explicit
611 // path on their own ready frame.
612 go m.assumeReadyAfterIdle(ext)
613
614 return nil
615}
616
617// readyIdleWindow is how long the manager waits for a frame after
618// hello before assuming an extension that doesn't send `ready` is
619// nevertheless ready. 250ms is enough for any well-behaved native
620// binary to flush its register frames; slow runtimes (npx tsx) flush
621// even faster once they've started, so this rarely affects them.
622const readyIdleWindow = 250 * time.Millisecond
623
624func (m *Manager) assumeReadyAfterIdle(ext *Extension) {
625 ext.mu.Lock()
626 last := ext.lastFrameTime
627 ext.mu.Unlock()
628 for {
629 select {
630 case <-ext.readyCh:
631 return
632 case <-time.After(readyIdleWindow):
633 }
634 ext.mu.Lock()
635 current := ext.lastFrameTime
636 ext.mu.Unlock()
637 if current.Equal(last) {
638 // No new frame in the idle window. Treat as ready.
639 ext.readyOnce.Do(func() {
640 ext.mu.Lock()
641 ext.autoReady = true
642 ext.diagnostics = append(ext.diagnostics, "no ready frame; auto-ready after idle")
643 ext.mu.Unlock()
644 fmt.Fprintf(ext.logFile, "[zot] no ready frame; auto-readying after idle (legacy SDK?)\n")
645 close(ext.readyCh)
646 })
647 return
648 }
649 last = current
650 }
651}
652
653// readLoop processes every frame the extension sends after hello.
654// Returns when stdout closes.
655func (m *Manager) readLoop(ext *Extension, scanner *bufio.Scanner) {
656 defer func() {
657 // On close, drop every command + tool this extension owned so
658 // future invocations don't dangle. The subprocess is gone; we
659 // won't hear back about its commands or tool calls anymore.
660 m.mu.Lock()
661 for name, registration := range m.commandIndex {
662 if registration.ext == ext {
663 delete(m.commandIndex, name)
664 }
665 }
666 for name, owner := range m.toolIndex {
667 if owner == ext {
668 delete(m.toolIndex, name)
669 }
670 }
671 m.mu.Unlock()
672 ext.readyOnce.Do(func() { close(ext.readyCh) })
673 fmt.Fprintf(ext.logFile, "[zot] extension %s read loop exited at %s\n", ext.Manifest.Name, time.Now().Format(time.RFC3339))
674 }()
675
676 for scanner.Scan() {
677 line := scanner.Bytes()
678 ext.mu.Lock()
679 ext.lastFrameTime = time.Now()
680 ext.mu.Unlock()
681 var frame extproto.Frame
682 if err := json.Unmarshal(line, &frame); err != nil {
683 ext.recordDiagnostic(fmt.Sprintf("malformed json frame: %v", err))
684 fmt.Fprintf(ext.logFile, "[zot] malformed json from extension: %v\n", err)
685 continue
686 }
687 switch frame.Type {
688 case "register_command":
689 var rc extproto.RegisterCommandFromExt
690 if err := json.Unmarshal(line, &rc); err == nil {
691 // Both ext.commands and m.commandIndex are read by
692 // the public Commands() / HasCommand() helpers under
693 // m.mu, so the writes have to take the same lock to
694 // keep the race detector happy.
695 m.mu.Lock()
696 ext.commands = append(ext.commands, rc)
697 key := strings.ToLower(rc.Name)
698 _, shadowed := m.commandIndex[key]
699 if !shadowed {
700 m.commandIndex[key] = commandRegistration{ext: ext, name: rc.Name}
701 }
702 m.mu.Unlock()
703 if shadowed {
704 ext.recordDiagnostic(fmt.Sprintf("command /%s conflicts with another extension registering the same name", rc.Name))
705 }
706 }
707 case "register_tool":
708 var rt extproto.RegisterToolFromExt
709 if err := json.Unmarshal(line, &rt); err != nil {
710 fmt.Fprintf(ext.logFile, "[zot] bad register_tool frame: %v\n", err)
711 continue
712 }
713 // Validate the schema parses as JSON. If not, refuse to
714 // register — a broken schema confuses the model.
715 if len(rt.Schema) > 0 {
716 var tmp any
717 if err := json.Unmarshal(rt.Schema, &tmp); err != nil {
718 ext.recordDiagnostic(fmt.Sprintf("tool %q schema is not valid json: %v", rt.Name, err))
719 fmt.Fprintf(ext.logFile, "[zot] tool %q: schema is not valid json (%v); skipped\n", rt.Name, err)
720 continue
721 }
722 }
723 m.mu.Lock()
724 ext.tools = append(ext.tools, rt)
725 _, shadowed := m.toolIndex[rt.Name]
726 if !shadowed {
727 m.toolIndex[rt.Name] = ext
728 }
729 m.mu.Unlock()
730 if shadowed {
731 ext.recordDiagnostic(fmt.Sprintf("tool %s conflicts with another extension registering the same name", rt.Name))
732 }
733 case "ready":
734 ext.readyOnce.Do(func() { close(ext.readyCh) })
735 case "subscribe":
736 var sub extproto.SubscribeFromExt
737 if err := json.Unmarshal(line, &sub); err == nil {
738 ext.mu.Lock()
739 for _, ev := range sub.Events {
740 ext.eventSubs[ev] = struct{}{}
741 }
742 for _, ev := range sub.Intercept {
743 switch ev {
744 case "tool_call", "turn_start", "assistant_message":
745 ext.interceptSubs[ev] = struct{}{}
746 }
747 }
748 ext.mu.Unlock()
749 }
750 case "event_intercept_response":
751 var er extproto.EventInterceptResponseFromExt
752 if err := json.Unmarshal(line, &er); err == nil {
753 ext.mu.Lock()
754 ch, ok := ext.pendingIntercept[er.ID]
755 if ok {
756 delete(ext.pendingIntercept, er.ID)
757 }
758 ext.mu.Unlock()
759 if ok {
760 select {
761 case ch <- er:
762 default:
763 }
764 }
765 }
766 case "tool_result":
767 var tr extproto.ToolResultFromExt
768 if err := json.Unmarshal(line, &tr); err == nil {
769 ext.mu.Lock()
770 ch, ok := ext.pendingTool[tr.ID]
771 if ok {
772 delete(ext.pendingTool, tr.ID)
773 }
774 ext.mu.Unlock()
775 if ok {
776 select {
777 case ch <- tr:
778 default:
779 }
780 }
781 }
782 case "notify":
783 var n extproto.NotifyFromExt
784 if err := json.Unmarshal(line, &n); err == nil {
785 m.hooks.Notify(ext.Manifest.Name, n.Level, n.Message)
786 }
787 case "clear_notes":
788 m.hooks.ClearNotes(ext.Manifest.Name)
789 case "submit":
790 var s extproto.SubmitFromExt
791 if err := json.Unmarshal(line, &s); err == nil {
792 text := strings.TrimSpace(s.Text)
793 if text != "" {
794 m.hooks.Submit(text)
795 }
796 }
797 case "submit_slash":
798 // Spontaneous request to invoke a slash command in the
799 // TUI. Refused unless the payload looks like a slash
800 // command so a misbehaving extension can't sneak a model
801 // prompt through this path.
802 var s extproto.SubmitSlashFromExt
803 if err := json.Unmarshal(line, &s); err == nil {
804 text := strings.TrimSpace(s.Text)
805 if strings.HasPrefix(text, "/") {
806 m.hooks.SubmitSlash(text)
807 } else {
808 fmt.Fprintf(ext.logFile, "[zot] submit_slash refused (not a slash command): %q\n", s.Text)
809 }
810 }
811 case "command_response":
812 var cr extproto.CommandResponseFromExt
813 if err := json.Unmarshal(line, &cr); err == nil {
814 ext.mu.Lock()
815 ch, ok := ext.pending[cr.ID]
816 if ok {
817 delete(ext.pending, cr.ID)
818 }
819 ext.mu.Unlock()
820 if ok {
821 select {
822 case ch <- cr:
823 default:
824 }
825 }
826 }
827 case "open_panel":
828 var op extproto.OpenPanelFromExt
829 if err := json.Unmarshal(line, &op); err == nil {
830 m.hooks.OpenPanel(ext.Manifest.Name, op.Panel)
831 }
832 case "panel_render":
833 var pr extproto.PanelRenderFromExt
834 if err := json.Unmarshal(line, &pr); err == nil {
835 m.hooks.UpdatePanel(ext.Manifest.Name, pr.PanelID, pr.Title, pr.Lines, pr.Footer)
836 }
837 case "panel_close":
838 var pc extproto.PanelCloseFromExt
839 if err := json.Unmarshal(line, &pc); err == nil {
840 m.hooks.ClosePanel(ext.Manifest.Name, pc.PanelID)
841 }
842 case "shutdown_ack":
843 // Caller of Stop is waiting on the process exit, not this frame.
844 default:
845 ext.recordDiagnostic(fmt.Sprintf("unknown frame type %q", frame.Type))
846 fmt.Fprintf(ext.logFile, "[zot] unknown frame type %q\n", frame.Type)
847 }
848 }
849}
850
851func (ext *Extension) recordDiagnostic(msg string) {
852 ext.mu.Lock()
853 defer ext.mu.Unlock()
854 ext.diagnostics = append(ext.diagnostics, msg)
855}
856
857// RegisteredCommandDiagnostic describes one command an extension attempted
858// to register and whether it owns the active dispatch slot.
859type RegisteredCommandDiagnostic struct {
860 Name string
861 Description string
862 Active bool
863}
864
865// RegisteredToolDiagnostic describes one tool an extension attempted to
866// register and whether it owns the active dispatch slot.
867type RegisteredToolDiagnostic struct {
868 Name string
869 Description string
870 Active bool
871}
872
873// ExtensionDiagnostic is a stable snapshot for diagnostic commands.
874type ExtensionDiagnostic struct {
875 Name string
876 Version string
877 Dir string
878 LogPath string
879 ThemeOnly bool
880 Ready bool
881 AutoReady bool
882 ReadyTimedOut bool
883 Commands []RegisteredCommandDiagnostic
884 Tools []RegisteredToolDiagnostic
885 Messages []string
886}
887
888// Diagnostics returns a snapshot of loaded extension state for commands such
889// as `zot ext doctor`. It is read-only and does not change manager behavior.
890func (m *Manager) Diagnostics() []ExtensionDiagnostic {
891 m.mu.RLock()
892 defer m.mu.RUnlock()
893 names := make([]string, 0, len(m.ext))
894 for name := range m.ext {
895 names = append(names, name)
896 }
897 sort.Strings(names)
898 out := make([]ExtensionDiagnostic, 0, len(names))
899 for _, name := range names {
900 ext := m.ext[name]
901 ext.mu.Lock()
902 msgs := append([]string(nil), ext.diagnostics...)
903 autoReady := ext.autoReady
904 readyTimedOut := ext.readyTimedOut
905 ext.mu.Unlock()
906
907 diag := ExtensionDiagnostic{
908 Name: ext.Manifest.Name,
909 Version: ext.Manifest.Version,
910 Dir: ext.Dir,
911 LogPath: ext.LogPath,
912 ThemeOnly: ext.Manifest.Exec == "",
913 AutoReady: autoReady,
914 ReadyTimedOut: readyTimedOut,
915 Messages: msgs,
916 }
917 select {
918 case <-ext.readyCh:
919 diag.Ready = true
920 default:
921 }
922 for _, c := range ext.commands {
923 registration, ok := m.commandIndex[strings.ToLower(c.Name)]
924 diag.Commands = append(diag.Commands, RegisteredCommandDiagnostic{
925 Name: c.Name,
926 Description: c.Description,
927 Active: ok && registration.ext == ext && registration.name == c.Name,
928 })
929 }
930 for _, t := range ext.tools {
931 diag.Tools = append(diag.Tools, RegisteredToolDiagnostic{
932 Name: t.Name,
933 Description: t.Description,
934 Active: m.toolIndex[t.Name] == ext,
935 })
936 }
937 out = append(out, diag)
938 }
939 return out
940}
941
942// Commands returns a snapshot of every (extension, command) pair
943// currently registered. Used by the slash autocomplete + /help.
944func (m *Manager) Commands() []CommandInfo {
945 m.mu.RLock()
946 defer m.mu.RUnlock()
947 var out []CommandInfo
948 for _, ext := range m.ext {
949 for _, c := range ext.commands {
950 registration, ok := m.commandIndex[strings.ToLower(c.Name)]
951 if !ok || registration.ext != ext || registration.name != c.Name {
952 continue
953 }
954 out = append(out, CommandInfo{
955 Extension: ext.Manifest.Name,
956 Name: c.Name,
957 Description: c.Description,
958 })
959 }
960 }
961 return out
962}
963
964// CommandInfo is one extension-registered slash command, surfaced to
965// the rest of zot for display purposes.
966type CommandInfo struct {
967 Extension string
968 Name string
969 Description string
970}
971
972// ToolInfo is one extension-registered tool. Used by the agent's
973// build step to materialise core.Tool wrappers.
974type ToolInfo struct {
975 Extension string
976 Name string
977 Description string
978 Schema json.RawMessage
979 Deferred bool
980}
981
982// Tools returns a snapshot of every (extension, tool) pair currently
983// registered. Used at agent-build time to fold extension tools into
984// the runtime tool registry.
985func (m *Manager) Tools() []ToolInfo {
986 m.mu.RLock()
987 defer m.mu.RUnlock()
988 var out []ToolInfo
989 for _, ext := range m.ext {
990 for _, t := range ext.tools {
991 out = append(out, ToolInfo{
992 Extension: ext.Manifest.Name,
993 Name: t.Name,
994 Description: t.Description,
995 Schema: t.Schema,
996 Deferred: t.Deferred,
997 })
998 }
999 }
1000 return out
1001}
1002
1003// HasTool reports whether name is registered by any extension.
1004func (m *Manager) HasTool(name string) bool {
1005 m.mu.RLock()
1006 defer m.mu.RUnlock()
1007 _, ok := m.toolIndex[name]
1008 return ok
1009}
1010
1011// InvokeTool sends a tool_call to the owning extension and waits for
1012// the matching tool_result. Used by the core.Tool wrapper that the
1013// agent registers per extension-defined tool.
1014func (m *Manager) InvokeTool(ctx context.Context, name string, args json.RawMessage, timeout time.Duration) (extproto.ToolResultFromExt, error) {
1015 m.mu.RLock()
1016 ext, ok := m.toolIndex[name]
1017 m.mu.RUnlock()
1018 if !ok {
1019 return extproto.ToolResultFromExt{}, fmt.Errorf("no extension registered for tool %q", name)
1020 }
1021
1022 id := newCorrelationID()
1023 ch := make(chan extproto.ToolResultFromExt, 1)
1024 ext.mu.Lock()
1025 ext.pendingTool[id] = ch
1026 ext.mu.Unlock()
1027
1028 frame, _ := extproto.Encode(extproto.ToolCallFromHost{
1029 Type: "tool_call",
1030 ID: id,
1031 Name: name,
1032 Args: args,
1033 })
1034 if _, err := ext.stdin.Write(frame); err != nil {
1035 ext.mu.Lock()
1036 delete(ext.pendingTool, id)
1037 ext.mu.Unlock()
1038 return extproto.ToolResultFromExt{}, fmt.Errorf("write: %w", err)
1039 }
1040
1041 select {
1042 case resp := <-ch:
1043 return resp, nil
1044 case <-time.After(timeout):
1045 ext.mu.Lock()
1046 delete(ext.pendingTool, id)
1047 ext.mu.Unlock()
1048 return extproto.ToolResultFromExt{}, fmt.Errorf("timeout waiting for %s/%s", ext.Manifest.Name, name)
1049 case <-ctx.Done():
1050 ext.mu.Lock()
1051 delete(ext.pendingTool, id)
1052 ext.mu.Unlock()
1053 return extproto.ToolResultFromExt{}, ctx.Err()
1054 }
1055}
1056
1057// HasCommand reports whether name is registered by any extension.
1058func (m *Manager) HasCommand(name string) bool {
1059 m.mu.RLock()
1060 defer m.mu.RUnlock()
1061 _, ok := m.commandIndex[strings.ToLower(name)]
1062 return ok
1063}
1064
1065func (m *Manager) CommandOwner(name string) string {
1066 m.mu.RLock()
1067 defer m.mu.RUnlock()
1068 if registration, ok := m.commandIndex[strings.ToLower(name)]; ok && registration.ext != nil {
1069 return registration.ext.Manifest.Name
1070 }
1071 return ""
1072}
1073
1074// Invoke fires the named slash command's handler in the owning
1075// extension and waits up to timeout for the response. Returns the
1076// extension's CommandResponse so the caller can act on the action
1077// (prompt / insert / display).
1078func (m *Manager) Invoke(ctx context.Context, name, args string, timeout time.Duration) (extproto.CommandResponseFromExt, error) {
1079 m.mu.RLock()
1080 registration, ok := m.commandIndex[strings.ToLower(name)]
1081 m.mu.RUnlock()
1082 if !ok {
1083 return extproto.CommandResponseFromExt{}, fmt.Errorf("no extension registered for /%s", name)
1084 }
1085 ext := registration.ext
1086 name = registration.name
1087
1088 id := newCorrelationID()
1089 ch := make(chan extproto.CommandResponseFromExt, 1)
1090 ext.mu.Lock()
1091 ext.pending[id] = ch
1092 ext.mu.Unlock()
1093
1094 frame, _ := extproto.Encode(extproto.CommandInvokedFromHost{
1095 Type: "command_invoked",
1096 ID: id,
1097 Name: name,
1098 Args: args,
1099 })
1100 if _, err := ext.stdin.Write(frame); err != nil {
1101 ext.mu.Lock()
1102 delete(ext.pending, id)
1103 ext.mu.Unlock()
1104 return extproto.CommandResponseFromExt{}, fmt.Errorf("write: %w", err)
1105 }
1106
1107 select {
1108 case resp := <-ch:
1109 return resp, nil
1110 case <-time.After(timeout):
1111 ext.mu.Lock()
1112 delete(ext.pending, id)
1113 ext.mu.Unlock()
1114 return extproto.CommandResponseFromExt{}, fmt.Errorf("timeout waiting for %s/%s", ext.Manifest.Name, name)
1115 case <-ctx.Done():
1116 ext.mu.Lock()
1117 delete(ext.pending, id)
1118 ext.mu.Unlock()
1119 return extproto.CommandResponseFromExt{}, ctx.Err()
1120 }
1121}
1122
1123// Stop cleanly terminates every extension. Sends ShutdownFromHost,
1124// waits up to gracePeriod for each subprocess to exit, then SIGTERMs
1125// (and SIGKILLs after another second) the holdouts.
1126func (m *Manager) SendPanelKey(extName, panelID, key, text string) error {
1127 m.mu.RLock()
1128 ext, ok := m.ext[extName]
1129 m.mu.RUnlock()
1130 if !ok {
1131 return fmt.Errorf("no extension %q", extName)
1132 }
1133 frame, _ := extproto.Encode(extproto.PanelKeyFromHost{Type: "panel_key", PanelID: panelID, Key: key, Text: text})
1134 _, err := ext.stdin.Write(frame)
1135 return err
1136}
1137
1138func (m *Manager) SendPanelClose(extName, panelID string) error {
1139 m.mu.RLock()
1140 ext, ok := m.ext[extName]
1141 m.mu.RUnlock()
1142 if !ok {
1143 return fmt.Errorf("no extension %q", extName)
1144 }
1145 frame, _ := extproto.Encode(extproto.PanelCloseFromHost{Type: "panel_close", PanelID: panelID})
1146 _, err := ext.stdin.Write(frame)
1147 return err
1148}
1149
1150func (m *Manager) Stop(gracePeriod time.Duration) {
1151 m.mu.RLock()
1152 exts := make([]*Extension, 0, len(m.ext))
1153 for _, e := range m.ext {
1154 exts = append(exts, e)
1155 }
1156 m.mu.RUnlock()
1157 stopExtensions(exts, gracePeriod)
1158}
1159
1160func stopExtensions(exts []*Extension, gracePeriod time.Duration) {
1161 for _, ext := range exts {
1162 if ext.stdin == nil {
1163 continue
1164 }
1165 if frame, err := extproto.Encode(extproto.ShutdownFromHost{Type: "shutdown"}); err == nil {
1166 _, _ = ext.stdin.Write(frame)
1167 }
1168 _ = ext.stdin.Close()
1169 }
1170
1171 deadline := time.Now().Add(gracePeriod)
1172 for _, ext := range exts {
1173 if ext.cmd == nil {
1174 if ext.logFile != nil {
1175 _ = ext.logFile.Close()
1176 }
1177 continue
1178 }
1179 remaining := time.Until(deadline)
1180 if remaining <= 0 {
1181 remaining = 100 * time.Millisecond
1182 }
1183 done := make(chan struct{})
1184 go func() { _ = ext.cmd.Wait(); close(done) }()
1185 select {
1186 case <-done:
1187 case <-time.After(remaining):
1188 _ = ext.cmd.Process.Signal(syscall.SIGTERM)
1189 select {
1190 case <-done:
1191 case <-time.After(time.Second):
1192 _ = ext.cmd.Process.Kill()
1193 <-done
1194 }
1195 }
1196 if ext.logFile != nil {
1197 _ = ext.logFile.Close()
1198 }
1199 }
1200}
1201
1202// All returns every extension currently tracked, enabled or not.
1203// Used by `zot ext list`.
1204func (m *Manager) All() []*Extension {
1205 m.mu.RLock()
1206 defer m.mu.RUnlock()
1207 out := make([]*Extension, 0, len(m.ext))
1208 for _, e := range m.ext {
1209 out = append(out, e)
1210 }
1211 return out
1212}
1213
1214// newCorrelationID returns a short non-cryptographic id. We don't
1215// need uniqueness across processes, just within the lifetime of one
1216// extension's pending map.
1217func newCorrelationID() string {
1218 return strings.ReplaceAll(time.Now().Format("150405.000000"), ".", "")
1219}