@theokit/sdk 2.20.0 → 2.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +148 -0
- package/dist/a2a/index.cjs +673 -317
- package/dist/a2a/index.cjs.map +1 -1
- package/dist/a2a/index.d.cts +2 -1
- package/dist/a2a/index.d.ts +2 -1
- package/dist/a2a/index.js +674 -318
- package/dist/a2a/index.js.map +1 -1
- package/dist/a2a/subagent.d.cts +70 -1
- package/dist/a2a/subagent.d.ts +70 -1
- package/dist/a2a/types.d.cts +7 -0
- package/dist/a2a/types.d.ts +7 -0
- package/dist/agent-builder.d.ts +2 -1
- package/dist/agent-generate.d.ts +12 -0
- package/dist/client/index.cjs.map +1 -1
- package/dist/client/index.d.cts +5 -0
- package/dist/client/index.d.ts +5 -0
- package/dist/client/index.js.map +1 -1
- package/dist/client/theokit-client.d.cts +6 -0
- package/dist/client/theokit-client.d.ts +6 -0
- package/dist/client/types.d.cts +2 -0
- package/dist/client/types.d.ts +2 -0
- package/dist/{cron-B8fqui49.d.cts → cron-7CruUd_0.d.ts} +53 -3
- package/dist/{cron-D2yLOrk2.d.ts → cron-Bpt_1khA.d.cts} +53 -3
- package/dist/cron.cjs +654 -389
- package/dist/cron.cjs.map +1 -1
- package/dist/cron.d.cts +3 -2
- package/dist/cron.d.ts +3 -2
- package/dist/cron.js +655 -390
- package/dist/cron.js.map +1 -1
- package/dist/{errors-5lj1EWgs.d.ts → errors-BuwwkrAk.d.ts} +2 -2
- package/dist/{errors-CE-lMBi2.d.cts → errors-CkCaIqVP.d.cts} +2 -2
- package/dist/errors.d.cts +3 -2
- package/dist/eval.cjs +654 -389
- package/dist/eval.cjs.map +1 -1
- package/dist/eval.js +654 -389
- package/dist/eval.js.map +1 -1
- package/dist/generate-object.d.ts +4 -3
- package/dist/index.cjs +800 -392
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +259 -17
- package/dist/index.d.ts +259 -17
- package/dist/index.js +798 -394
- package/dist/index.js.map +1 -1
- package/dist/internal/agent-loop/loop-types.d.ts +2 -0
- package/dist/internal/agent-loop/tool-result-guard.d.ts +1 -0
- package/dist/internal/llm/tool-result-content.d.ts +40 -0
- package/dist/internal/llm/types.d.ts +6 -1
- package/dist/internal/persistence/conversation-storage-fs.d.cts +3 -1
- package/dist/internal/persistence/conversation-storage-fs.d.ts +3 -1
- package/dist/internal/persistence/conversation-storage-memory.d.cts +3 -1
- package/dist/internal/persistence/conversation-storage-memory.d.ts +3 -1
- package/dist/internal/persistence/session-meta.d.cts +13 -0
- package/dist/internal/persistence/session-meta.d.ts +13 -0
- package/dist/internal/runtime/model-selection.d.ts +14 -0
- package/dist/internal/structured-output-helpers.d.ts +1 -1
- package/dist/permission-engine.d.ts +32 -1
- package/dist/{run-BgfBWX-z.d.cts → run-CrIulPF7.d.cts} +282 -19
- package/dist/{run-BgfBWX-z.d.ts → run-CrIulPF7.d.ts} +282 -19
- package/dist/stream-object.d.ts +2 -1
- package/dist/tool-error.d.ts +30 -0
- package/dist/types/agent-prims.d.ts +30 -8
- package/dist/types/agent.d.ts +13 -2
- package/dist/types/content-blocks.d.ts +47 -0
- package/dist/types/conversation-storage.d.ts +38 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/messages.d.ts +2 -9
- package/dist/types/run-events.d.ts +84 -0
- package/dist/types/run.d.ts +115 -1
- package/dist/types/session.d.ts +99 -0
- package/dist/types/workflow.d.ts +7 -0
- package/dist/workflow.cjs +7 -2
- package/dist/workflow.cjs.map +1 -1
- package/dist/workflow.d.cts +2 -0
- package/dist/workflow.d.ts +2 -0
- package/dist/workflow.js +7 -2
- package/dist/workflow.js.map +1 -1
- package/package.json +14 -34
- package/dist/internal/observability/context.d.cts +0 -23
- package/dist/internal/observability/context.d.ts +0 -23
- package/dist/internal/observability/index.cjs +0 -38
- package/dist/internal/observability/index.cjs.map +0 -1
- package/dist/internal/observability/index.d.cts +0 -8
- package/dist/internal/observability/index.d.ts +0 -8
- package/dist/internal/observability/index.js +0 -33
- package/dist/internal/observability/index.js.map +0 -1
- package/dist/internal/plugins/index.cjs +0 -363
- package/dist/internal/plugins/index.cjs.map +0 -1
- package/dist/internal/plugins/index.js +0 -357
- package/dist/internal/plugins/index.js.map +0 -1
- package/dist/internal/workflow/index.d.ts +0 -12
- package/dist/server/adapter/index.d.cts +0 -8
- package/dist/server/adapter/index.d.ts +0 -8
|
@@ -1,3 +1,53 @@
|
|
|
1
|
+
import * as zod from 'zod';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Leaf module for content-block types shared by `messages.ts` (assistant/user
|
|
5
|
+
* content) and `agent-prims.ts` (`CustomTool` handler results). Kept
|
|
6
|
+
* import-free so both can depend on it WITHOUT the `agent-prims ↔ messages`
|
|
7
|
+
* cycle (#7).
|
|
8
|
+
*
|
|
9
|
+
* @public
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* Plain text content block emitted by the assistant or user, or returned by a
|
|
13
|
+
* tool.
|
|
14
|
+
*
|
|
15
|
+
* @public
|
|
16
|
+
*/
|
|
17
|
+
interface TextBlock {
|
|
18
|
+
type: "text";
|
|
19
|
+
text: string;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* SE7 — a base64-encoded image block a tool can hand back as (part of) its
|
|
23
|
+
* result or its `ToolError`. `media_type` is a MIME type (e.g. `"image/png"`);
|
|
24
|
+
* `data` is the base64 payload without a data-URL prefix.
|
|
25
|
+
*
|
|
26
|
+
* Note: when a tool builds this from model- or user-influenced input, treat
|
|
27
|
+
* `media_type` as UNTRUSTED — validate/allow-list it before rendering it in a
|
|
28
|
+
* log or UI (it could carry newlines / control chars). The SDK only forwards it
|
|
29
|
+
* (JSON-serialized onto the wire) and never executes or path-joins it.
|
|
30
|
+
*
|
|
31
|
+
* @public
|
|
32
|
+
*/
|
|
33
|
+
interface ImageBlock {
|
|
34
|
+
type: "image";
|
|
35
|
+
source: {
|
|
36
|
+
type: "base64";
|
|
37
|
+
media_type: string;
|
|
38
|
+
data: string;
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* SE7 — structured content a tool result may carry: text and/or images. A tool
|
|
43
|
+
* `handler` may return this (success) and a `ToolError` may carry it (failure).
|
|
44
|
+
* Block-capable provider wires forward it natively; string-only provider wires
|
|
45
|
+
* flatten text and fail fast on an image.
|
|
46
|
+
*
|
|
47
|
+
* @public
|
|
48
|
+
*/
|
|
49
|
+
type ToolResultContentBlock = TextBlock | ImageBlock;
|
|
50
|
+
|
|
1
51
|
/**
|
|
2
52
|
* #57 — content-level defense for tool results before they reach the LLM.
|
|
3
53
|
*
|
|
@@ -11,6 +61,7 @@
|
|
|
11
61
|
*
|
|
12
62
|
* @internal
|
|
13
63
|
*/
|
|
64
|
+
|
|
14
65
|
interface ToolResultGuardOptions {
|
|
15
66
|
/** Wrap tool-result content in explicit data boundaries (spotlighting). */
|
|
16
67
|
delimit?: boolean;
|
|
@@ -18,6 +69,91 @@ interface ToolResultGuardOptions {
|
|
|
18
69
|
redactPii?: boolean;
|
|
19
70
|
}
|
|
20
71
|
|
|
72
|
+
/**
|
|
73
|
+
* SE2 — typed runtime EVENT stream, ADDITIVE to the `SDKMessage` content stream.
|
|
74
|
+
*
|
|
75
|
+
* `Run.stream()` yields `SDKMessage`s (the conversation content). `RunEvent`s are
|
|
76
|
+
* out-of-band runtime-OBSERVABILITY signals — the model's content is unaffected —
|
|
77
|
+
* delivered opt-in via `SendOptions.onRunEvent`. Discriminate on `type`. Mirrors
|
|
78
|
+
* the Anthropic `SDKMessage`-union approach (rate-limit, permission-denied, task
|
|
79
|
+
* lifecycle, compaction boundary).
|
|
80
|
+
*
|
|
81
|
+
* The union is the forward-compatible CONTRACT (discriminate exhaustively). As of
|
|
82
|
+
* SE2 the runtime EMITS `tool_progress` and `permission_denied` end-to-end (from
|
|
83
|
+
* the agent-loop tool-dispatch seam). `rate_limit`, `task_*`, and `compact_boundary`
|
|
84
|
+
* are part of the contract; their emission is wired incrementally as the sink is
|
|
85
|
+
* threaded into the LLM-client retry / task / session-compaction subsystems (they
|
|
86
|
+
* live below the loop). A consumer switching on `type` is future-proof either way.
|
|
87
|
+
*
|
|
88
|
+
* @public
|
|
89
|
+
*/
|
|
90
|
+
type RunEvent = RunToolProgressEvent | RunRateLimitEvent | RunPermissionDeniedEvent | RunTaskStartedEvent | RunTaskUpdatedEvent | RunTaskCompletedEvent | RunCompactBoundaryEvent;
|
|
91
|
+
/** A tool call is being dispatched (before its result). */
|
|
92
|
+
interface RunToolProgressEvent {
|
|
93
|
+
readonly type: "tool_progress";
|
|
94
|
+
readonly toolName: string;
|
|
95
|
+
readonly toolCallId: string;
|
|
96
|
+
}
|
|
97
|
+
/** The provider returned a rate-limit (HTTP 429); the loop will back off + retry. */
|
|
98
|
+
interface RunRateLimitEvent {
|
|
99
|
+
readonly type: "rate_limit";
|
|
100
|
+
/** Retry attempt number (1-based) about to be delayed. */
|
|
101
|
+
readonly attempt: number;
|
|
102
|
+
/** Delay in ms before the retry, when the provider/policy supplied one. */
|
|
103
|
+
readonly retryAfterMs?: number;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* A tool call was DENIED before dispatch — by the permission gate/plugin (SE1),
|
|
107
|
+
* an operator file-hook `preToolUse`, or the fork tool-whitelist. `source`
|
|
108
|
+
* discriminates which. `toolCallId` joins the event to the tool-call log.
|
|
109
|
+
*/
|
|
110
|
+
interface RunPermissionDeniedEvent {
|
|
111
|
+
readonly type: "permission_denied";
|
|
112
|
+
readonly toolName: string;
|
|
113
|
+
readonly toolCallId: string;
|
|
114
|
+
/** Which layer blocked the call. */
|
|
115
|
+
readonly source: "plugin" | "file_hook" | "fork_whitelist";
|
|
116
|
+
/** The rejection message surfaced to the model. */
|
|
117
|
+
readonly message: string;
|
|
118
|
+
}
|
|
119
|
+
/** A background task/subagent started. */
|
|
120
|
+
interface RunTaskStartedEvent {
|
|
121
|
+
readonly type: "task_started";
|
|
122
|
+
readonly taskId: string;
|
|
123
|
+
readonly description?: string;
|
|
124
|
+
}
|
|
125
|
+
/** A background task/subagent changed state. */
|
|
126
|
+
interface RunTaskUpdatedEvent {
|
|
127
|
+
readonly type: "task_updated";
|
|
128
|
+
readonly taskId: string;
|
|
129
|
+
readonly status: string;
|
|
130
|
+
}
|
|
131
|
+
/** A background task/subagent finished. */
|
|
132
|
+
interface RunTaskCompletedEvent {
|
|
133
|
+
readonly type: "task_completed";
|
|
134
|
+
readonly taskId: string;
|
|
135
|
+
readonly status: "completed" | "failed" | "stopped";
|
|
136
|
+
}
|
|
137
|
+
/** The conversation crossed a compaction boundary (history was summarized). */
|
|
138
|
+
interface RunCompactBoundaryEvent {
|
|
139
|
+
readonly type: "compact_boundary";
|
|
140
|
+
readonly trigger: "manual" | "auto";
|
|
141
|
+
/** Token count before compaction, when known. */
|
|
142
|
+
readonly preTokens?: number;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* SE2 — the opt-in sink for {@link RunEvent}s. Supplied via `SendOptions.onRunEvent`.
|
|
146
|
+
* Synchronous + best-effort: a throwing sink must never break the run (the emitter
|
|
147
|
+
* try-catches it), so keep it fast (push to a queue, don't await).
|
|
148
|
+
*/
|
|
149
|
+
type RunEventSink = (event: RunEvent) => void;
|
|
150
|
+
/**
|
|
151
|
+
* SE2 — emit a {@link RunEvent} to an optional sink, swallowing any sink error so
|
|
152
|
+
* observability can never break the run (fail-safe, mirrors the EventBus EC-2
|
|
153
|
+
* contract). No-op when the sink is absent.
|
|
154
|
+
*/
|
|
155
|
+
declare function emitRunEvent(sink: RunEventSink | undefined, event: RunEvent): void;
|
|
156
|
+
|
|
21
157
|
/**
|
|
22
158
|
* Type-leaf — primitives shared between `agent.ts`, `run.ts`, and
|
|
23
159
|
* `messages.ts`. Extracted to break LOW type-only cycles #5 and #7
|
|
@@ -50,6 +186,23 @@ interface ModelSelection {
|
|
|
50
186
|
id: string;
|
|
51
187
|
params?: ModelParameterValue[];
|
|
52
188
|
}
|
|
189
|
+
/**
|
|
190
|
+
* SE12 — a read-only, text-only projection of one turn of the run's conversation,
|
|
191
|
+
* exposed to a tool handler via `ctx.messages`. Content is flattened to text; tool
|
|
192
|
+
* calls / results and non-text parts are dropped, and turns that project to empty
|
|
193
|
+
* text are omitted. Consumed by `defineSubAgent`'s `messageFilter` to forward
|
|
194
|
+
* (a subset of) the supervisor transcript to a subagent.
|
|
195
|
+
*
|
|
196
|
+
* `role` includes `"system"` for type-completeness with the wire message shape,
|
|
197
|
+
* but the system prompt travels on a separate request field — a `"system"` entry
|
|
198
|
+
* does NOT appear in the current projection.
|
|
199
|
+
*
|
|
200
|
+
* @public
|
|
201
|
+
*/
|
|
202
|
+
interface ToolContextMessage {
|
|
203
|
+
role: "system" | "user" | "assistant";
|
|
204
|
+
content: string;
|
|
205
|
+
}
|
|
53
206
|
/**
|
|
54
207
|
* Local function tool declared per-agent via {@link AgentOptions.tools}. The
|
|
55
208
|
* handler runs in-process; no MCP server is involved. The SDK serializes
|
|
@@ -73,18 +226,23 @@ interface CustomTool {
|
|
|
73
226
|
inputSchema: Record<string, unknown>;
|
|
74
227
|
/**
|
|
75
228
|
* Local handler invoked when the model emits `tool_use` for this tool.
|
|
76
|
-
* Returns a string
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
* `
|
|
229
|
+
* Returns a string OR structured content blocks (SE7 — text + image, e.g. a
|
|
230
|
+
* screenshot) that become the `tool_result.content` surfaced back to the
|
|
231
|
+
* model. Throws → SDK converts to `tool_result` with `isError: true` and the
|
|
232
|
+
* error `message` as content; throw a `ToolError` to carry a clean message or
|
|
233
|
+
* multimodal error content. #65 — an optional 2nd `ToolContext` argument
|
|
234
|
+
* carries the run's `AbortSignal`; single-argument handlers are unaffected. M7
|
|
235
|
+
* — the same `ctx` also carries an optional user `context` (provided once via
|
|
236
|
+
* `SendOptions.context`), so shared config like a `projectRoot` is read by
|
|
237
|
+
* every tool instead of baked into each factory. SE12 — `ctx.messages` is a
|
|
238
|
+
* read-only, text-only projection of the current turn's transcript (see
|
|
239
|
+
* {@link ToolContextMessage}); `defineSubAgent`'s `messageFilter` consumes it.
|
|
83
240
|
*/
|
|
84
241
|
handler: (input: Record<string, unknown>, ctx?: {
|
|
85
242
|
signal?: AbortSignal;
|
|
86
243
|
context?: unknown;
|
|
87
|
-
|
|
244
|
+
messages?: readonly ToolContextMessage[];
|
|
245
|
+
}) => string | ToolResultContentBlock[] | Promise<string | ToolResultContentBlock[]>;
|
|
88
246
|
}
|
|
89
247
|
|
|
90
248
|
/**
|
|
@@ -441,15 +599,6 @@ type McpHttpServerConfig = {
|
|
|
441
599
|
*/
|
|
442
600
|
type McpServerConfig = McpStdioServerConfig | McpHttpServerConfig;
|
|
443
601
|
|
|
444
|
-
/**
|
|
445
|
-
* Plain text content block emitted by the assistant or user.
|
|
446
|
-
*
|
|
447
|
-
* @public
|
|
448
|
-
*/
|
|
449
|
-
interface TextBlock {
|
|
450
|
-
type: "text";
|
|
451
|
-
text: string;
|
|
452
|
-
}
|
|
453
602
|
/**
|
|
454
603
|
* Tool invocation block emitted by the assistant.
|
|
455
604
|
*
|
|
@@ -685,6 +834,49 @@ interface RunGitInfo {
|
|
|
685
834
|
prUrl?: string;
|
|
686
835
|
}>;
|
|
687
836
|
}
|
|
837
|
+
/**
|
|
838
|
+
* SE3 — provenance of the turn that produced a run: WHO triggered it. Stamped in
|
|
839
|
+
* the multi-agent path (Squad / a2a / handoff / background-delegation) and
|
|
840
|
+
* forwarded onto {@link RunResult.origin} — so consumers can attribute/route
|
|
841
|
+
* turns by their trigger. Metadata-only; discriminate on `kind`. Mirrors the
|
|
842
|
+
* Anthropic Agent SDK's `origin` shape.
|
|
843
|
+
*
|
|
844
|
+
* Encoding note (distinct from absence): an ABSENT origin (`undefined`) means the
|
|
845
|
+
* provenance was NOT stamped — the default for a plain `agent.send()`. The explicit
|
|
846
|
+
* `{ kind: "human" }` is a positive marker a HOST stamps to say "this turn is
|
|
847
|
+
* definitely from a human" (e.g. to distinguish a real user message from an
|
|
848
|
+
* un-attributed one in an audit UI). The two are not interchangeable: `undefined`
|
|
849
|
+
* = unknown/unstamped; `{ kind: "human" }` = explicitly human. Consumers writing an
|
|
850
|
+
* exhaustive `switch (origin?.kind)` therefore handle `undefined` (unstamped) and
|
|
851
|
+
* `"human"` (explicit) as separate, meaningful cases.
|
|
852
|
+
*
|
|
853
|
+
* @public
|
|
854
|
+
*/
|
|
855
|
+
type MessageOrigin =
|
|
856
|
+
/** Explicitly a human-triggered turn — a positive marker a host stamps (NOT the
|
|
857
|
+
* same as an absent/unstamped origin, which is `undefined`). */
|
|
858
|
+
{
|
|
859
|
+
readonly kind: "human";
|
|
860
|
+
}
|
|
861
|
+
/** Another agent (a Squad peer or an a2a sender) triggered this turn. */
|
|
862
|
+
| {
|
|
863
|
+
readonly kind: "peer";
|
|
864
|
+
readonly from: string;
|
|
865
|
+
}
|
|
866
|
+
/** A background task's completion re-entered the agent as a follow-up turn. */
|
|
867
|
+
| {
|
|
868
|
+
readonly kind: "task-notification";
|
|
869
|
+
}
|
|
870
|
+
/** A delegating/handoff coordinator triggered this turn. `from` is the coordinator's
|
|
871
|
+
* id when known, omitted for an anonymous coordinator. */
|
|
872
|
+
| {
|
|
873
|
+
readonly kind: "coordinator";
|
|
874
|
+
readonly from?: string;
|
|
875
|
+
}
|
|
876
|
+
/** The loop's continuation driver re-sent to continue truncated work. */
|
|
877
|
+
| {
|
|
878
|
+
readonly kind: "auto-continuation";
|
|
879
|
+
};
|
|
688
880
|
/**
|
|
689
881
|
* Terminal result of a {@link Run}.
|
|
690
882
|
*
|
|
@@ -697,6 +889,15 @@ interface RunResult {
|
|
|
697
889
|
model?: ModelSelection;
|
|
698
890
|
durationMs?: number;
|
|
699
891
|
git?: RunGitInfo;
|
|
892
|
+
/**
|
|
893
|
+
* SE3 — provenance forwarded from {@link SendOptions.origin}: who triggered
|
|
894
|
+
* this turn (human / peer / task-notification / coordinator / auto-continuation).
|
|
895
|
+
* `undefined` means the provenance was not stamped (a plain `agent.send()`).
|
|
896
|
+
* Metadata-only — never affects routing.
|
|
897
|
+
*
|
|
898
|
+
* @public
|
|
899
|
+
*/
|
|
900
|
+
origin?: MessageOrigin;
|
|
700
901
|
/**
|
|
701
902
|
* Structured error detail, populated when `status === "error"`. Surfaces
|
|
702
903
|
* the diagnostic that emit-error-event pushes into the stream so callers
|
|
@@ -871,7 +1072,27 @@ interface SDKUserMessage {
|
|
|
871
1072
|
* @public
|
|
872
1073
|
*/
|
|
873
1074
|
interface SendOptions {
|
|
874
|
-
|
|
1075
|
+
/**
|
|
1076
|
+
* Per-send model override. SE8 — accepts a bare-string id shorthand
|
|
1077
|
+
* (`"openai/gpt-4o-mini"`, normalized to `{ id }`) OR a {@link ModelSelection}
|
|
1078
|
+
* object (use the object form to pass `params`).
|
|
1079
|
+
*/
|
|
1080
|
+
model?: string | ModelSelection;
|
|
1081
|
+
/**
|
|
1082
|
+
* SE3 — provenance of this turn (who triggered it). Stamped by the multi-agent
|
|
1083
|
+
* path (Squad peer, a2a sender, coordinator/handoff, background task-notification)
|
|
1084
|
+
* and forwarded onto {@link RunResult.origin}. Metadata-only — the value never
|
|
1085
|
+
* changes routing or dispatch. Omit to leave the turn un-attributed; a host may
|
|
1086
|
+
* pass `{ kind: "human" }` to positively mark a human turn.
|
|
1087
|
+
*/
|
|
1088
|
+
origin?: MessageOrigin;
|
|
1089
|
+
/**
|
|
1090
|
+
* SE2 — opt-in typed runtime-EVENT sink. Receives out-of-band `RunEvent`s
|
|
1091
|
+
* (permission_denied, tool_progress, rate_limit, task_*, compact_boundary) for
|
|
1092
|
+
* observability, ADDITIVE to the `SDKMessage` content stream. Best-effort: a
|
|
1093
|
+
* throwing sink never breaks the run. Discriminate on `event.type`.
|
|
1094
|
+
*/
|
|
1095
|
+
onRunEvent?: RunEventSink;
|
|
875
1096
|
/**
|
|
876
1097
|
* Doom-loop guard config. The loop stops (with `terminal: "no_progress"`, `RunResult.stoppedByDoomLoop`)
|
|
877
1098
|
* when the model repeats IDENTICAL tool calls to the hard threshold. On by default with generous
|
|
@@ -1009,5 +1230,47 @@ interface Run {
|
|
|
1009
1230
|
/** Subscribe to status changes. Returns an unsubscribe function. */
|
|
1010
1231
|
onDidChangeStatus(listener: (status: RunStatus) => void): () => void;
|
|
1011
1232
|
}
|
|
1233
|
+
/**
|
|
1234
|
+
* SE9 — options for the integrated structured-output method `agent.generate`: the
|
|
1235
|
+
* {@link SendOptions} that drive the tool loop (phase 1) plus the required `output`
|
|
1236
|
+
* Zod schema and structuring knobs (phase 2). Co-located with `SendOptions` /
|
|
1237
|
+
* `RunResult` (which they extend/use) so the public `SDKAgent` interface does not
|
|
1238
|
+
* import the runtime `agent-generate` module (breaks the type cycle).
|
|
1239
|
+
*
|
|
1240
|
+
* @public
|
|
1241
|
+
*/
|
|
1242
|
+
interface GenerateOptions<T extends zod.ZodType> extends SendOptions {
|
|
1243
|
+
/** Zod schema the final answer is coerced into (the structuring contract). */
|
|
1244
|
+
output: T;
|
|
1245
|
+
/** Retry budget on the structuring phase's parse-failures (reused from generateObject). Default 1. */
|
|
1246
|
+
maxRetries?: number;
|
|
1247
|
+
/**
|
|
1248
|
+
* What the STRUCTURING phase (phase 2) does when the model's output still fails
|
|
1249
|
+
* Zod validation after retries. Default `"throw"`. `"return-partial"` / `"return-raw"`
|
|
1250
|
+
* salvage the object; the salvaged/raw value is in {@link GenerateRunResult.raw}
|
|
1251
|
+
* (the pre-parse structuring input) — NOT the phase-1 text answer, which is in
|
|
1252
|
+
* `result.result`.
|
|
1253
|
+
*/
|
|
1254
|
+
errorStrategy?: "throw" | "return-partial" | "return-raw";
|
|
1255
|
+
}
|
|
1256
|
+
/**
|
|
1257
|
+
* SE9 — result of `agent.generate`: the validated typed object plus the underlying
|
|
1258
|
+
* tool-loop {@link RunResult} (status / usage / model) and the raw pre-parse input.
|
|
1259
|
+
*
|
|
1260
|
+
* @public
|
|
1261
|
+
*/
|
|
1262
|
+
interface GenerateRunResult<O> {
|
|
1263
|
+
/** The validated object — inferred type from the `output` schema. */
|
|
1264
|
+
object: O;
|
|
1265
|
+
/** The underlying tool-loop run (phase 1). */
|
|
1266
|
+
result: RunResult;
|
|
1267
|
+
/** Raw model input to the synthetic `output` tool, before the Zod parse. */
|
|
1268
|
+
raw: unknown;
|
|
1269
|
+
/** Combined token usage of the structuring phase. */
|
|
1270
|
+
usage: {
|
|
1271
|
+
inputTokens: number;
|
|
1272
|
+
outputTokens: number;
|
|
1273
|
+
};
|
|
1274
|
+
}
|
|
1012
1275
|
|
|
1013
|
-
export type
|
|
1276
|
+
export { type SendOptions as $, type AgentConversationTurn as A, type RunTaskStartedEvent as B, type CustomTool as C, type DoomLoopThresholds as D, type RunTaskUpdatedEvent as E, type RunToCompletionOptions as F, type GenerateOptions as G, type RunToCompletionResult as H, type ImageBlock as I, type RunToolProgressEvent as J, type SDKAssistantMessage as K, type SDKImage as L, type ModelSelection as M, type SDKImageDimension as N, type SDKObjectDelta as O, type PartialToolCallUpdate as P, type SDKRequestMessage as Q, type RunResult as R, type SDKMessage as S, type ToolResultContentBlock as T, type SDKStatusMessage as U, type SDKSystemMessage as V, type SDKTaskMessage as W, type SDKThinkingMessage as X, type SDKToolUseMessage as Y, type SDKUserMessage as Z, type SDKUserMessageEvent as _, type McpServerConfig as a, type ShellCommand as a0, type ShellConversationTurn as a1, type ShellOutput as a2, type ShellOutputDeltaUpdate as a3, type StepCompletedUpdate as a4, type StepStartedUpdate as a5, type StreamToCompletionResult as a6, type SummaryCompletedUpdate as a7, type SummaryStartedUpdate as a8, type SummaryUpdate as a9, type TextBlock as aa, type TextDeltaUpdate as ab, type ThinkingCompletedUpdate as ac, type ThinkingDeltaUpdate as ad, type ThinkingMessage as ae, type TokenDeltaUpdate as af, type TokenUsage as ag, type ToolCall as ah, type ToolCallCompletedUpdate as ai, type ToolCallStartedUpdate as aj, type ToolContextMessage as ak, type ToolResult as al, type ToolResultGuardOptions as am, type ToolUseBlock as an, type TurnEndedUpdate as ao, type UserMessage as ap, type UserMessageAppendedUpdate as aq, emitRunEvent as ar, type Run as b, type MessageOrigin as c, type AssistantMessage as d, type ConversationStep as e, type ConversationTurn as f, type CostBreakdown as g, type CostSource as h, type CostStatus as i, type GenerateRunResult as j, type InteractionUpdate as k, type McpAuthConfig as l, type McpHttpServerConfig as m, type McpOAuthConfig as n, type McpStdioServerConfig as o, type ModelParameterValue as p, type RunCompactBoundaryEvent as q, type RunErrorDetail as r, type RunEvent as s, type RunEventSink as t, type RunGitInfo as u, type RunOperation as v, type RunPermissionDeniedEvent as w, type RunRateLimitEvent as x, type RunStatus as y, type RunTaskCompletedEvent as z };
|
package/dist/stream-object.d.ts
CHANGED
|
@@ -12,7 +12,8 @@ export interface StreamObjectOptions<T extends ZodType> {
|
|
|
12
12
|
schema: T;
|
|
13
13
|
prompt: string;
|
|
14
14
|
systemPrompt?: string;
|
|
15
|
-
|
|
15
|
+
/** SE8 — accepts a bare-string id shorthand or a {@link ModelSelection}. */
|
|
16
|
+
model: string | ModelSelection;
|
|
16
17
|
apiKey?: string;
|
|
17
18
|
local: LocalOptions;
|
|
18
19
|
maxRetries?: number;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SE7 — `ToolError`: thrown FROM a tool `handler` to report a failure back to
|
|
3
|
+
* the model with structured content (text and/or an image), not just a string.
|
|
4
|
+
* Kept in its own module (not `errors.ts`) so the error taxonomy file stays
|
|
5
|
+
* under the G8 LoC budget and this tool-shaped error lives next to the tool
|
|
6
|
+
* surface it serves.
|
|
7
|
+
*
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
10
|
+
import { type ErrorMetadata, TheokitAgentError } from "./errors.js";
|
|
11
|
+
import type { ToolResultContentBlock } from "./types/content-blocks.js";
|
|
12
|
+
/**
|
|
13
|
+
* Thrown from a tool `handler` to surface a failure to the model. The SDK turns
|
|
14
|
+
* it into a `tool_result` with `isError: true` carrying the content. A plain
|
|
15
|
+
* `Error` thrown from a handler still works (its message becomes text);
|
|
16
|
+
* `ToolError` is the opt-in for a clean message or a multimodal error (e.g. an
|
|
17
|
+
* error screenshot).
|
|
18
|
+
*
|
|
19
|
+
* @public
|
|
20
|
+
*/
|
|
21
|
+
export declare class ToolError extends TheokitAgentError {
|
|
22
|
+
readonly name: string;
|
|
23
|
+
/** The error content surfaced to the model: a string, or text/image blocks. */
|
|
24
|
+
readonly content: string | ToolResultContentBlock[];
|
|
25
|
+
constructor(content: string | ToolResultContentBlock[], options?: {
|
|
26
|
+
code?: string;
|
|
27
|
+
cause?: unknown;
|
|
28
|
+
metadata?: ErrorMetadata;
|
|
29
|
+
});
|
|
30
|
+
}
|
|
@@ -30,6 +30,23 @@ export interface ModelSelection {
|
|
|
30
30
|
id: string;
|
|
31
31
|
params?: ModelParameterValue[];
|
|
32
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* SE12 — a read-only, text-only projection of one turn of the run's conversation,
|
|
35
|
+
* exposed to a tool handler via `ctx.messages`. Content is flattened to text; tool
|
|
36
|
+
* calls / results and non-text parts are dropped, and turns that project to empty
|
|
37
|
+
* text are omitted. Consumed by `defineSubAgent`'s `messageFilter` to forward
|
|
38
|
+
* (a subset of) the supervisor transcript to a subagent.
|
|
39
|
+
*
|
|
40
|
+
* `role` includes `"system"` for type-completeness with the wire message shape,
|
|
41
|
+
* but the system prompt travels on a separate request field — a `"system"` entry
|
|
42
|
+
* does NOT appear in the current projection.
|
|
43
|
+
*
|
|
44
|
+
* @public
|
|
45
|
+
*/
|
|
46
|
+
export interface ToolContextMessage {
|
|
47
|
+
role: "system" | "user" | "assistant";
|
|
48
|
+
content: string;
|
|
49
|
+
}
|
|
33
50
|
/**
|
|
34
51
|
* Local function tool declared per-agent via {@link AgentOptions.tools}. The
|
|
35
52
|
* handler runs in-process; no MCP server is involved. The SDK serializes
|
|
@@ -53,16 +70,21 @@ export interface CustomTool {
|
|
|
53
70
|
inputSchema: Record<string, unknown>;
|
|
54
71
|
/**
|
|
55
72
|
* Local handler invoked when the model emits `tool_use` for this tool.
|
|
56
|
-
* Returns a string
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
* `
|
|
73
|
+
* Returns a string OR structured content blocks (SE7 — text + image, e.g. a
|
|
74
|
+
* screenshot) that become the `tool_result.content` surfaced back to the
|
|
75
|
+
* model. Throws → SDK converts to `tool_result` with `isError: true` and the
|
|
76
|
+
* error `message` as content; throw a `ToolError` to carry a clean message or
|
|
77
|
+
* multimodal error content. #65 — an optional 2nd `ToolContext` argument
|
|
78
|
+
* carries the run's `AbortSignal`; single-argument handlers are unaffected. M7
|
|
79
|
+
* — the same `ctx` also carries an optional user `context` (provided once via
|
|
80
|
+
* `SendOptions.context`), so shared config like a `projectRoot` is read by
|
|
81
|
+
* every tool instead of baked into each factory. SE12 — `ctx.messages` is a
|
|
82
|
+
* read-only, text-only projection of the current turn's transcript (see
|
|
83
|
+
* {@link ToolContextMessage}); `defineSubAgent`'s `messageFilter` consumes it.
|
|
63
84
|
*/
|
|
64
85
|
handler: (input: Record<string, unknown>, ctx?: {
|
|
65
86
|
signal?: AbortSignal;
|
|
66
87
|
context?: unknown;
|
|
67
|
-
|
|
88
|
+
messages?: readonly ToolContextMessage[];
|
|
89
|
+
}) => string | import("./content-blocks.js").ToolResultContentBlock[] | Promise<string | import("./content-blocks.js").ToolResultContentBlock[]>;
|
|
68
90
|
}
|
package/dist/types/agent.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import type { ContextSettings, SDKContextManager } from "./context.js";
|
|
|
2
2
|
import type { McpServerConfig } from "./mcp.js";
|
|
3
3
|
import type { PluginsSettings, ProviderRoutingSettings, SDKProvidersManager } from "./providers.js";
|
|
4
4
|
import type { Run, SDKUserMessage, SendOptions } from "./run.js";
|
|
5
|
-
export type { CustomTool, ModelParameterValue, ModelSelection } from "./agent-prims.js";
|
|
5
|
+
export type { CustomTool, ModelParameterValue, ModelSelection, ToolContextMessage, } from "./agent-prims.js";
|
|
6
6
|
import type { Plugin } from "../internal/plugins/types.js";
|
|
7
7
|
import type { CustomTool, ModelSelection } from "./agent-prims.js";
|
|
8
8
|
/**
|
|
@@ -284,7 +284,12 @@ export interface TelemetrySettings {
|
|
|
284
284
|
* @public
|
|
285
285
|
*/
|
|
286
286
|
export interface AgentOptions {
|
|
287
|
-
|
|
287
|
+
/**
|
|
288
|
+
* The model to run. SE8 — accepts a bare-string id shorthand
|
|
289
|
+
* (`"openai/gpt-4o-mini"`, normalized to `{ id }`) OR a {@link ModelSelection}
|
|
290
|
+
* object (use the object form to pass `params`).
|
|
291
|
+
*/
|
|
292
|
+
model?: string | ModelSelection;
|
|
288
293
|
/** Falls back to `THEOKIT_API_KEY`. */
|
|
289
294
|
apiKey?: string;
|
|
290
295
|
name?: string;
|
|
@@ -576,6 +581,12 @@ export interface SDKAgent {
|
|
|
576
581
|
*/
|
|
577
582
|
readonly plugins?: SDKAgentPlugins;
|
|
578
583
|
send(message: string | SDKUserMessage, options?: SendOptions): Promise<Run>;
|
|
584
|
+
/**
|
|
585
|
+
* SE9 — integrated structured output. Runs the normal tool loop (the tools run
|
|
586
|
+
* first) then coerces the final answer into the `output` Zod schema, returning a
|
|
587
|
+
* validated, inferred-typed object. Sugar over `Agent.generateObject` (ADR D33).
|
|
588
|
+
*/
|
|
589
|
+
generate<T extends import("zod").ZodType>(message: string | SDKUserMessage, options: import("./run.js").GenerateOptions<T>): Promise<import("./run.js").GenerateRunResult<import("zod").z.infer<T>>>;
|
|
579
590
|
/** Fire-and-forget disposal. */
|
|
580
591
|
close(): void;
|
|
581
592
|
/** Re-read filesystem config (context, hooks, project MCP, subagents) without disposing. */
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Leaf module for content-block types shared by `messages.ts` (assistant/user
|
|
3
|
+
* content) and `agent-prims.ts` (`CustomTool` handler results). Kept
|
|
4
|
+
* import-free so both can depend on it WITHOUT the `agent-prims ↔ messages`
|
|
5
|
+
* cycle (#7).
|
|
6
|
+
*
|
|
7
|
+
* @public
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Plain text content block emitted by the assistant or user, or returned by a
|
|
11
|
+
* tool.
|
|
12
|
+
*
|
|
13
|
+
* @public
|
|
14
|
+
*/
|
|
15
|
+
export interface TextBlock {
|
|
16
|
+
type: "text";
|
|
17
|
+
text: string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* SE7 — a base64-encoded image block a tool can hand back as (part of) its
|
|
21
|
+
* result or its `ToolError`. `media_type` is a MIME type (e.g. `"image/png"`);
|
|
22
|
+
* `data` is the base64 payload without a data-URL prefix.
|
|
23
|
+
*
|
|
24
|
+
* Note: when a tool builds this from model- or user-influenced input, treat
|
|
25
|
+
* `media_type` as UNTRUSTED — validate/allow-list it before rendering it in a
|
|
26
|
+
* log or UI (it could carry newlines / control chars). The SDK only forwards it
|
|
27
|
+
* (JSON-serialized onto the wire) and never executes or path-joins it.
|
|
28
|
+
*
|
|
29
|
+
* @public
|
|
30
|
+
*/
|
|
31
|
+
export interface ImageBlock {
|
|
32
|
+
type: "image";
|
|
33
|
+
source: {
|
|
34
|
+
type: "base64";
|
|
35
|
+
media_type: string;
|
|
36
|
+
data: string;
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* SE7 — structured content a tool result may carry: text and/or images. A tool
|
|
41
|
+
* `handler` may return this (success) and a `ToolError` may carry it (failure).
|
|
42
|
+
* Block-capable provider wires forward it natively; string-only provider wires
|
|
43
|
+
* flatten text and fail fast on an image.
|
|
44
|
+
*
|
|
45
|
+
* @public
|
|
46
|
+
*/
|
|
47
|
+
export type ToolResultContentBlock = TextBlock | ImageBlock;
|
|
@@ -31,6 +31,31 @@ export interface StoredMessage {
|
|
|
31
31
|
/** Optional epoch-ms timestamp. Implementations MAY default to `Date.now()` on append. */
|
|
32
32
|
at?: number;
|
|
33
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* SE4 — light, session-level metadata stored ALONGSIDE the transcript (title +
|
|
36
|
+
* tag). Distinct from {@link StoredMessage} (which is per-turn content): this is
|
|
37
|
+
* per-conversation display metadata a host sets to build a session UI.
|
|
38
|
+
*
|
|
39
|
+
* @public
|
|
40
|
+
*/
|
|
41
|
+
export interface SessionMeta {
|
|
42
|
+
/** Human-facing title set via `renameSession`. Absent = untitled. */
|
|
43
|
+
readonly title?: string;
|
|
44
|
+
/** Single free-form tag set via `tagSession`. Absent = untagged. */
|
|
45
|
+
readonly tag?: string;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* SE4 — a partial update to {@link SessionMeta}. An omitted field is left
|
|
49
|
+
* unchanged; `tag: null` explicitly CLEARS the tag (distinct from omitting it).
|
|
50
|
+
*
|
|
51
|
+
* @public
|
|
52
|
+
*/
|
|
53
|
+
export interface SessionMetaPatch {
|
|
54
|
+
/** Set the title; `null` explicitly CLEARS it (distinct from omitting it). */
|
|
55
|
+
readonly title?: string | null;
|
|
56
|
+
/** Set the tag; `null` explicitly CLEARS it (distinct from omitting it). */
|
|
57
|
+
readonly tag?: string | null;
|
|
58
|
+
}
|
|
34
59
|
/**
|
|
35
60
|
* Pluggable conversation persistence contract.
|
|
36
61
|
*
|
|
@@ -102,6 +127,19 @@ export interface ConversationStorageAdapter {
|
|
|
102
127
|
* trims old turns past a soft cap; other backends typically no-op.
|
|
103
128
|
*/
|
|
104
129
|
compact?(conversationId: string, maxTurns: number): Promise<void>;
|
|
130
|
+
/**
|
|
131
|
+
* SE4 — optional: read the session-level metadata (title/tag) for a
|
|
132
|
+
* conversation. MUST return `undefined` when none was set (NOT throw).
|
|
133
|
+
* Adapters that cannot store side metadata MAY omit this — the session
|
|
134
|
+
* manager then reports rename/tag as unsupported rather than throwing.
|
|
135
|
+
*/
|
|
136
|
+
getSessionMeta?(conversationId: string): Promise<SessionMeta | undefined>;
|
|
137
|
+
/**
|
|
138
|
+
* SE4 — optional: apply a {@link SessionMetaPatch} to a conversation's
|
|
139
|
+
* session metadata (set title, set/clear tag). MUST create the metadata
|
|
140
|
+
* lazily. Adapters that cannot store side metadata MAY omit this.
|
|
141
|
+
*/
|
|
142
|
+
setSessionMeta?(conversationId: string, patch: SessionMetaPatch): Promise<void>;
|
|
105
143
|
/**
|
|
106
144
|
* Optional: dispose underlying handles (close DB pool, etc.).
|
|
107
145
|
* MUST be safe to call multiple times.
|
package/dist/types/index.d.ts
CHANGED
|
@@ -11,6 +11,7 @@ export type * from "./memory-adapter.js";
|
|
|
11
11
|
export type * from "./messages.js";
|
|
12
12
|
export type * from "./providers.js";
|
|
13
13
|
export type * from "./run.js";
|
|
14
|
+
export type * from "./session.js";
|
|
14
15
|
export type * from "./task.js";
|
|
15
16
|
export type * from "./theokit.js";
|
|
16
17
|
export type * from "./trajectory.js";
|
package/dist/types/messages.d.ts
CHANGED
|
@@ -1,13 +1,6 @@
|
|
|
1
1
|
import type { ModelSelection } from "./agent-prims.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
*
|
|
5
|
-
* @public
|
|
6
|
-
*/
|
|
7
|
-
export interface TextBlock {
|
|
8
|
-
type: "text";
|
|
9
|
-
text: string;
|
|
10
|
-
}
|
|
2
|
+
import type { TextBlock } from "./content-blocks.js";
|
|
3
|
+
export type { ImageBlock, TextBlock, ToolResultContentBlock } from "./content-blocks.js";
|
|
11
4
|
/**
|
|
12
5
|
* Tool invocation block emitted by the assistant.
|
|
13
6
|
*
|