1// Package sdk is the public Go SDK for embedding the zot agent
2// runtime in third-party programs. It is the only stable, importable
3// surface the project exposes; everything under internal/ is subject
4// to change without notice.
5//
6// Lifecycle:
7//
8// rt, err := sdk.New(sdk.Config{Provider: "anthropic", Model: "claude-sonnet-4-5"})
9// if err != nil { ... }
10// defer rt.Close()
11//
12// events, err := rt.Prompt(ctx, "fix the failing test", nil)
13// if err != nil { ... }
14// for ev := range events {
15// fmt.Printf("%s: %+v\n", ev.Type(), ev)
16// }
17//
18// Concurrency model: one prompt at a time per Runtime. Spawn one
19// Runtime per project / cwd. The Cancel call interrupts the active
20// prompt; subsequent prompts work normally.
21//
22// For a non-Go consumer, run `zot rpc` and speak the same JSON
23// schema over stdin/stdout. See docs/rpc.md.
24package sdk
25
26import (
27 "context"
28 "errors"
29 "fmt"
30 "sync"
31
32 "github.com/patriceckhart/zot/packages/agent"
33 "github.com/patriceckhart/zot/packages/core"
34 "github.com/patriceckhart/zot/packages/provider"
35)
36
37// Config configures a Runtime. All fields are optional; sensible
38// defaults are read from $ZOT_HOME/config.json, env vars, and the
39// resolver chain (the same one the cli uses).
40type Config struct {
41 // Provider is "anthropic" or "openai". Empty = use the user's
42 // default from config.json or env.
43 Provider string
44
45 // Model is the model id. Empty = use the provider's default.
46 Model string
47
48 // CWD is the working directory the agent operates in. Tools are
49 // confined to this directory when Lock is true. Empty = current
50 // process cwd.
51 CWD string
52
53 // SystemPrompt overrides the built-in system prompt when non-empty or when
54 // SystemPromptSet is true.
55 SystemPrompt string
56
57 // SystemPromptSet allows an empty SystemPrompt to intentionally omit the
58 // built-in identity. It is unnecessary for non-empty prompts.
59 SystemPromptSet bool
60
61 // AppendSystemPrompt is appended to the built-in (or overridden)
62 // system prompt. Useful for project-specific instructions.
63 AppendSystemPrompt []string
64
65 // Reasoning sets the reasoning effort for models that support it
66 // ("minimum", "low", "medium", "high", "xhigh", "max"). Empty disables it.
67 Reasoning string
68
69 // Temperature sets the sampling temperature. Nil = provider default.
70 Temperature *float32
71
72 // MaxSteps caps the agent loop iterations per Prompt call.
73 // 0 uses the default (50).
74 MaxSteps int
75
76 // APIKey overrides the credential lookup chain.
77 APIKey string
78
79 // BaseURL overrides the provider base url (for tests / proxies).
80 BaseURL string
81
82 // Tools is the list of tools to enable. Nil/empty = all built-ins
83 // (read, write, edit, bash, glob). Pass an empty-but-non-nil slice
84 // (e.g. []string{}) plus NoTools=true to disable everything.
85 Tools []string
86
87 // NoTools disables every tool. Useful for chat-only embeddings.
88 NoTools bool
89
90 // Lock confines tools to CWD. Same effect as the /jail command.
91 Lock bool
92}
93
94// Runtime is one zot agent session. Safe for use from one goroutine
95// at a time per Runtime; create separate Runtimes for parallel work.
96type Runtime struct {
97 mu sync.Mutex
98 agent *core.Agent
99 provider string
100 model string
101 cwd string
102
103 // activeCancel is set while a Prompt is streaming.
104 activeCancel context.CancelFunc
105
106 // closed signals that Close has been called.
107 closed bool
108}
109
110// New constructs a Runtime from cfg. Returns an error if no
111// credential is available for the requested provider.
112func New(cfg Config) (*Runtime, error) {
113 args := argsFromConfig(cfg)
114 if args.MaxSteps == 0 {
115 args.MaxSteps = 50
116 }
117 r, err := agent.Resolve(args, true)
118 if err != nil {
119 return nil, err
120 }
121 if cfg.Lock && r.Sandbox != nil {
122 r.Sandbox.Lock()
123 }
124 ag := r.NewAgent()
125 return &Runtime{
126 agent: ag,
127 provider: r.Provider,
128 model: r.Model,
129 cwd: r.CWD,
130 }, nil
131}
132
133func argsFromConfig(cfg Config) agent.Args {
134 return agent.Args{
135 Mode: agent.ModeJSON, // headless
136 Provider: cfg.Provider,
137 Model: cfg.Model,
138 CWD: cfg.CWD,
139 APIKey: cfg.APIKey,
140 BaseURL: cfg.BaseURL,
141 SystemPrompt: cfg.SystemPrompt,
142 SystemPromptSet: cfg.SystemPromptSet || cfg.SystemPrompt != "",
143 AppendSystemPrompt: cfg.AppendSystemPrompt,
144 Reasoning: cfg.Reasoning,
145 Temperature: cfg.Temperature,
146 MaxSteps: cfg.MaxSteps,
147 Tools: cfg.Tools,
148 NoTools: cfg.NoTools,
149 NoSess: true, // SDK callers manage persistence themselves
150 }
151}
152
153// Provider returns the active provider id.
154func (r *Runtime) Provider() string { r.mu.Lock(); defer r.mu.Unlock(); return r.provider }
155
156// Model returns the active model id.
157func (r *Runtime) Model() string { r.mu.Lock(); defer r.mu.Unlock(); return r.model }
158
159// CWD returns the working directory the agent operates in.
160func (r *Runtime) CWD() string { r.mu.Lock(); defer r.mu.Unlock(); return r.cwd }
161
162// Messages returns a copy of the current transcript.
163func (r *Runtime) Messages() []Message {
164 if r.agent == nil {
165 return nil
166 }
167 src := r.agent.Messages()
168 out := make([]Message, len(src))
169 for i, m := range src {
170 out[i] = Message{
171 Role: string(m.Role),
172 Content: convertContent(m.Content),
173 Time: m.Time.Format("2006-01-02T15:04:05Z"),
174 }
175 }
176 return out
177}
178
179// SetMessages replaces the transcript. Use to seed history from a
180// session file or to clear with nil.
181func (r *Runtime) SetMessages(msgs []Message) {
182 if r.agent == nil {
183 return
184 }
185 out := make([]provider.Message, 0, len(msgs))
186 for _, m := range msgs {
187 out = append(out, provider.Message{
188 Role: provider.Role(m.Role),
189 Content: rebuildContent(m.Content),
190 })
191 }
192 r.agent.SetMessages(out)
193}
194
195// Cost returns the cumulative usage and dollar cost for this Runtime.
196func (r *Runtime) Cost() Usage {
197 if r.agent == nil {
198 return Usage{}
199 }
200 u := r.agent.Cost()
201 return Usage{
202 Input: u.InputTokens,
203 Output: u.OutputTokens,
204 Reasoning: sdkReasoningTokens(u),
205 CacheRead: u.CacheReadTokens,
206 CacheWrite: u.CacheWriteTokens,
207 CostUSD: u.CostUSD,
208 }
209}
210
211// Prompt sends a user message and runs the agent loop. Returns a
212// channel that emits one Event per agent action, closed when the
213// turn finishes (cleanly or with error). Only one Prompt may be
214// active at a time per Runtime; concurrent calls return ErrBusy.
215func (r *Runtime) Prompt(ctx context.Context, text string, images []Image) (<-chan Event, error) {
216 r.mu.Lock()
217 if r.closed {
218 r.mu.Unlock()
219 return nil, ErrClosed
220 }
221 if r.activeCancel != nil {
222 r.mu.Unlock()
223 return nil, ErrBusy
224 }
225 if r.agent == nil {
226 r.mu.Unlock()
227 return nil, fmt.Errorf("sdk: no agent (login first via /login or set credentials)")
228 }
229 subCtx, cancel := context.WithCancel(ctx)
230 r.activeCancel = cancel
231 r.mu.Unlock()
232
233 imgBlocks := make([]provider.ImageBlock, 0, len(images))
234 for _, img := range images {
235 imgBlocks = append(imgBlocks, provider.ImageBlock{MimeType: img.MimeType, Data: img.Data})
236 }
237
238 out := make(chan Event, 16)
239 go func() {
240 defer close(out)
241 defer func() {
242 r.mu.Lock()
243 r.activeCancel = nil
244 r.mu.Unlock()
245 }()
246 err := r.agent.Prompt(subCtx, text, imgBlocks, func(ev core.AgentEvent) {
247 out <- toEvent(ev)
248 })
249 if err != nil && !errors.Is(err, context.Canceled) {
250 out <- Event{Type: "error", Error: err.Error()}
251 }
252 }()
253 return out, nil
254}
255
256// Cancel interrupts the active Prompt, if any. Safe to call when no
257// prompt is in flight (no-op).
258func (r *Runtime) Cancel() {
259 r.mu.Lock()
260 cancel := r.activeCancel
261 r.mu.Unlock()
262 if cancel != nil {
263 cancel()
264 }
265}
266
267// Compact summarises the current transcript into a single synthetic
268// user message, freeing up context. Blocks until done.
269func (r *Runtime) Compact(ctx context.Context, customInstructions string) (CompactResult, error) {
270 if r.agent == nil {
271 return CompactResult{}, fmt.Errorf("sdk: no agent")
272 }
273 r.mu.Lock()
274 if r.activeCancel != nil {
275 r.mu.Unlock()
276 return CompactResult{}, ErrBusy
277 }
278 subCtx, cancel := context.WithCancel(ctx)
279 r.activeCancel = cancel
280 r.mu.Unlock()
281 defer func() {
282 r.mu.Lock()
283 r.activeCancel = nil
284 r.mu.Unlock()
285 }()
286
287 summary, err := r.agent.Compact(subCtx, 4, nil)
288 if err != nil {
289 return CompactResult{}, err
290 }
291 return CompactResult{Summary: summary, Messages: r.Messages()}, nil
292}
293
294// SetModel switches the active model. Same provider only; for cross-
295// provider switches, create a new Runtime.
296func (r *Runtime) SetModel(model string) error {
297 r.mu.Lock()
298 defer r.mu.Unlock()
299 if r.agent == nil {
300 return fmt.Errorf("sdk: no agent")
301 }
302 if _, err := provider.FindModel(r.provider, model); err != nil {
303 return err
304 }
305 r.agent.Model = model
306 r.model = model
307 return nil
308}
309
310// SetReasoning changes the reasoning level used by subsequent prompts.
311func (r *Runtime) SetReasoning(level string) error {
312 r.mu.Lock()
313 defer r.mu.Unlock()
314 if r.agent == nil {
315 return fmt.Errorf("sdk: no agent")
316 }
317 reasoning := provider.NormalizeReasoning(level)
318 switch reasoning {
319 case "", "minimum", "low", "medium", "high", "xhigh", "max":
320 r.agent.Reasoning = reasoning
321 return nil
322 default:
323 return fmt.Errorf("sdk: reasoning must be off|minimum|low|medium|high|xhigh|max")
324 }
325}
326
327// State returns a snapshot of the runtime's current state. Useful for
328// driving UIs.
329func (r *Runtime) State() State {
330 r.mu.Lock()
331 defer r.mu.Unlock()
332 return State{
333 Provider: r.provider,
334 Model: r.model,
335 Reasoning: r.agent.Reasoning,
336 CWD: r.cwd,
337 Busy: r.activeCancel != nil,
338 MessageCount: len(r.Messages()),
339 }
340}
341
342// Close releases resources held by the Runtime. Cancels any active
343// prompt. Subsequent calls return ErrClosed.
344func (r *Runtime) Close() error {
345 r.mu.Lock()
346 if r.closed {
347 r.mu.Unlock()
348 return nil
349 }
350 r.closed = true
351 cancel := r.activeCancel
352 r.mu.Unlock()
353 if cancel != nil {
354 cancel()
355 }
356 return nil
357}
358
359// ListModels returns every model known to the runtime for the
360// current provider (catalog + live discovery if cached).
361func (r *Runtime) ListModels() []ModelInfo {
362 models := provider.ModelsForProvider(r.Provider())
363 out := make([]ModelInfo, 0, len(models))
364 for _, m := range models {
365 out = append(out, ModelInfo{
366 ID: m.ID,
367 Provider: m.Provider,
368 ContextWindow: m.ContextWindow,
369 MaxOutput: m.MaxOutput,
370 Reasoning: m.Reasoning,
371 })
372 }
373 return out
374}
375
376// ---- public errors ----
377
378// ErrBusy is returned when a Prompt or Compact is started while
379// another is in flight on the same Runtime.
380var ErrBusy = errors.New("sdk: runtime is busy")
381
382// ErrClosed is returned by methods on a Runtime after Close.
383var ErrClosed = errors.New("sdk: runtime is closed")