v0.2.57 be0642f

sdk.go RawHistoryFolder

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	// ("low", "medium", "high"). Empty = no reasoning.
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// State returns a snapshot of the runtime's current state. Useful for
300// driving UIs.
301func (r *Runtime) State() State {
302	r.mu.Lock()
303	defer r.mu.Unlock()
304	return State{
305		Provider:     r.provider,
306		Model:        r.model,
307		CWD:          r.cwd,
308		Busy:         r.activeCancel != nil,
309		MessageCount: len(r.Messages()),
310	}
311}
312
313// Close releases resources held by the Runtime. Cancels any active
314// prompt. Subsequent calls return ErrClosed.
315func (r *Runtime) Close() error {
316	r.mu.Lock()
317	if r.closed {
318		r.mu.Unlock()
319		return nil
320	}
321	r.closed = true
322	cancel := r.activeCancel
323	r.mu.Unlock()
324	if cancel != nil {
325		cancel()
326	}
327	return nil
328}
329
330// ListModels returns every model known to the runtime for the
331// current provider (catalog + live discovery if cached).
332func (r *Runtime) ListModels() []ModelInfo {
333	models := provider.ModelsForProvider(r.Provider())
334	out := make([]ModelInfo, 0, len(models))
335	for _, m := range models {
336		out = append(out, ModelInfo{
337			ID:            m.ID,
338			Provider:      m.Provider,
339			ContextWindow: m.ContextWindow,
340			MaxOutput:     m.MaxOutput,
341			Reasoning:     m.Reasoning,
342		})
343	}
344	return out
345}
346
347// ---- public errors ----
348
349// ErrBusy is returned when a Prompt or Compact is started while
350// another is in flight on the same Runtime.
351var ErrBusy = errors.New("sdk: runtime is busy")
352
353// ErrClosed is returned by methods on a Runtime after Close.
354var ErrClosed = errors.New("sdk: runtime is closed")

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 477

↑↓ moveenter openesc close