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