@theokit/sdk 2.19.0 → 2.21.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.
Files changed (89) hide show
  1. package/CHANGELOG.md +123 -0
  2. package/dist/a2a/index.cjs +611 -274
  3. package/dist/a2a/index.cjs.map +1 -1
  4. package/dist/a2a/index.js +612 -275
  5. package/dist/a2a/index.js.map +1 -1
  6. package/dist/a2a/types.d.cts +7 -0
  7. package/dist/a2a/types.d.ts +7 -0
  8. package/dist/agent-builder.d.ts +2 -1
  9. package/dist/agent-generate.d.ts +12 -0
  10. package/dist/client/index.cjs.map +1 -1
  11. package/dist/client/index.d.cts +5 -0
  12. package/dist/client/index.d.ts +5 -0
  13. package/dist/client/index.js.map +1 -1
  14. package/dist/client/theokit-client.d.cts +6 -0
  15. package/dist/client/theokit-client.d.ts +6 -0
  16. package/dist/client/types.d.cts +2 -0
  17. package/dist/client/types.d.ts +2 -0
  18. package/dist/create-skill.d.ts +29 -0
  19. package/dist/{cron-D_wK1S-0.d.cts → cron-YrmsszEN.d.cts} +110 -3
  20. package/dist/{cron-Cep07kTz.d.ts → cron-dpvtRoro.d.ts} +110 -3
  21. package/dist/cron.cjs +660 -370
  22. package/dist/cron.cjs.map +1 -1
  23. package/dist/cron.d.cts +3 -2
  24. package/dist/cron.d.ts +3 -2
  25. package/dist/cron.js +661 -371
  26. package/dist/cron.js.map +1 -1
  27. package/dist/{errors-5lj1EWgs.d.ts → errors-C4vZPqXf.d.ts} +2 -2
  28. package/dist/{errors-CE-lMBi2.d.cts → errors-DrcpYVfZ.d.cts} +2 -2
  29. package/dist/errors.d.cts +3 -2
  30. package/dist/eval.cjs +660 -370
  31. package/dist/eval.cjs.map +1 -1
  32. package/dist/eval.js +660 -370
  33. package/dist/eval.js.map +1 -1
  34. package/dist/generate-object.d.ts +19 -4
  35. package/dist/index.cjs +867 -371
  36. package/dist/index.cjs.map +1 -1
  37. package/dist/index.d.cts +274 -16
  38. package/dist/index.d.ts +274 -16
  39. package/dist/index.js +863 -373
  40. package/dist/index.js.map +1 -1
  41. package/dist/internal/agent-loop/loop-types.d.ts +2 -0
  42. package/dist/internal/agent-loop/tool-result-guard.d.ts +1 -0
  43. package/dist/internal/llm/tool-result-content.d.ts +40 -0
  44. package/dist/internal/llm/types.d.ts +6 -1
  45. package/dist/internal/persistence/conversation-storage-fs.d.cts +3 -1
  46. package/dist/internal/persistence/conversation-storage-fs.d.ts +3 -1
  47. package/dist/internal/persistence/conversation-storage-memory.d.cts +3 -1
  48. package/dist/internal/persistence/conversation-storage-memory.d.ts +3 -1
  49. package/dist/internal/persistence/session-meta.d.cts +13 -0
  50. package/dist/internal/persistence/session-meta.d.ts +13 -0
  51. package/dist/internal/runtime/model-selection.d.ts +14 -0
  52. package/dist/internal/structured-output-helpers.d.ts +1 -1
  53. package/dist/permission-engine.d.ts +32 -1
  54. package/dist/{run-BgfBWX-z.d.cts → run-BMo8yRwK.d.cts} +262 -19
  55. package/dist/{run-BgfBWX-z.d.ts → run-BMo8yRwK.d.ts} +262 -19
  56. package/dist/stream-object.d.ts +2 -1
  57. package/dist/tool-error.d.ts +30 -0
  58. package/dist/types/agent-prims.d.ts +10 -8
  59. package/dist/types/agent.d.ts +22 -1
  60. package/dist/types/content-blocks.d.ts +47 -0
  61. package/dist/types/conversation-storage.d.ts +38 -0
  62. package/dist/types/index.d.ts +1 -0
  63. package/dist/types/messages.d.ts +2 -9
  64. package/dist/types/run-events.d.ts +84 -0
  65. package/dist/types/run.d.ts +115 -1
  66. package/dist/types/session.d.ts +99 -0
  67. package/dist/types/workflow.d.ts +7 -0
  68. package/dist/workflow.cjs +7 -2
  69. package/dist/workflow.cjs.map +1 -1
  70. package/dist/workflow.d.cts +2 -0
  71. package/dist/workflow.d.ts +2 -0
  72. package/dist/workflow.js +7 -2
  73. package/dist/workflow.js.map +1 -1
  74. package/package.json +3 -23
  75. package/dist/internal/observability/context.d.cts +0 -23
  76. package/dist/internal/observability/context.d.ts +0 -23
  77. package/dist/internal/observability/index.cjs +0 -38
  78. package/dist/internal/observability/index.cjs.map +0 -1
  79. package/dist/internal/observability/index.d.cts +0 -8
  80. package/dist/internal/observability/index.d.ts +0 -8
  81. package/dist/internal/observability/index.js +0 -33
  82. package/dist/internal/observability/index.js.map +0 -1
  83. package/dist/internal/plugins/index.cjs +0 -363
  84. package/dist/internal/plugins/index.cjs.map +0 -1
  85. package/dist/internal/plugins/index.js +0 -357
  86. package/dist/internal/plugins/index.js.map +0 -1
  87. package/dist/internal/workflow/index.d.ts +0 -12
  88. package/dist/server/adapter/index.d.cts +0 -8
  89. package/dist/server/adapter/index.d.ts +0 -8
@@ -0,0 +1,84 @@
1
+ /**
2
+ * SE2 — typed runtime EVENT stream, ADDITIVE to the `SDKMessage` content stream.
3
+ *
4
+ * `Run.stream()` yields `SDKMessage`s (the conversation content). `RunEvent`s are
5
+ * out-of-band runtime-OBSERVABILITY signals — the model's content is unaffected —
6
+ * delivered opt-in via `SendOptions.onRunEvent`. Discriminate on `type`. Mirrors
7
+ * the Anthropic `SDKMessage`-union approach (rate-limit, permission-denied, task
8
+ * lifecycle, compaction boundary).
9
+ *
10
+ * The union is the forward-compatible CONTRACT (discriminate exhaustively). As of
11
+ * SE2 the runtime EMITS `tool_progress` and `permission_denied` end-to-end (from
12
+ * the agent-loop tool-dispatch seam). `rate_limit`, `task_*`, and `compact_boundary`
13
+ * are part of the contract; their emission is wired incrementally as the sink is
14
+ * threaded into the LLM-client retry / task / session-compaction subsystems (they
15
+ * live below the loop). A consumer switching on `type` is future-proof either way.
16
+ *
17
+ * @public
18
+ */
19
+ export type RunEvent = RunToolProgressEvent | RunRateLimitEvent | RunPermissionDeniedEvent | RunTaskStartedEvent | RunTaskUpdatedEvent | RunTaskCompletedEvent | RunCompactBoundaryEvent;
20
+ /** A tool call is being dispatched (before its result). */
21
+ export interface RunToolProgressEvent {
22
+ readonly type: "tool_progress";
23
+ readonly toolName: string;
24
+ readonly toolCallId: string;
25
+ }
26
+ /** The provider returned a rate-limit (HTTP 429); the loop will back off + retry. */
27
+ export interface RunRateLimitEvent {
28
+ readonly type: "rate_limit";
29
+ /** Retry attempt number (1-based) about to be delayed. */
30
+ readonly attempt: number;
31
+ /** Delay in ms before the retry, when the provider/policy supplied one. */
32
+ readonly retryAfterMs?: number;
33
+ }
34
+ /**
35
+ * A tool call was DENIED before dispatch — by the permission gate/plugin (SE1),
36
+ * an operator file-hook `preToolUse`, or the fork tool-whitelist. `source`
37
+ * discriminates which. `toolCallId` joins the event to the tool-call log.
38
+ */
39
+ export interface RunPermissionDeniedEvent {
40
+ readonly type: "permission_denied";
41
+ readonly toolName: string;
42
+ readonly toolCallId: string;
43
+ /** Which layer blocked the call. */
44
+ readonly source: "plugin" | "file_hook" | "fork_whitelist";
45
+ /** The rejection message surfaced to the model. */
46
+ readonly message: string;
47
+ }
48
+ /** A background task/subagent started. */
49
+ export interface RunTaskStartedEvent {
50
+ readonly type: "task_started";
51
+ readonly taskId: string;
52
+ readonly description?: string;
53
+ }
54
+ /** A background task/subagent changed state. */
55
+ export interface RunTaskUpdatedEvent {
56
+ readonly type: "task_updated";
57
+ readonly taskId: string;
58
+ readonly status: string;
59
+ }
60
+ /** A background task/subagent finished. */
61
+ export interface RunTaskCompletedEvent {
62
+ readonly type: "task_completed";
63
+ readonly taskId: string;
64
+ readonly status: "completed" | "failed" | "stopped";
65
+ }
66
+ /** The conversation crossed a compaction boundary (history was summarized). */
67
+ export interface RunCompactBoundaryEvent {
68
+ readonly type: "compact_boundary";
69
+ readonly trigger: "manual" | "auto";
70
+ /** Token count before compaction, when known. */
71
+ readonly preTokens?: number;
72
+ }
73
+ /**
74
+ * SE2 — the opt-in sink for {@link RunEvent}s. Supplied via `SendOptions.onRunEvent`.
75
+ * Synchronous + best-effort: a throwing sink must never break the run (the emitter
76
+ * try-catches it), so keep it fast (push to a queue, don't await).
77
+ */
78
+ export type RunEventSink = (event: RunEvent) => void;
79
+ /**
80
+ * SE2 — emit a {@link RunEvent} to an optional sink, swallowing any sink error so
81
+ * observability can never break the run (fail-safe, mirrors the EventBus EC-2
82
+ * contract). No-op when the sink is absent.
83
+ */
84
+ export declare function emitRunEvent(sink: RunEventSink | undefined, event: RunEvent): void;
@@ -32,6 +32,49 @@ export interface RunGitInfo {
32
32
  prUrl?: string;
33
33
  }>;
34
34
  }
35
+ /**
36
+ * SE3 — provenance of the turn that produced a run: WHO triggered it. Stamped in
37
+ * the multi-agent path (Squad / a2a / handoff / background-delegation) and
38
+ * forwarded onto {@link RunResult.origin} — so consumers can attribute/route
39
+ * turns by their trigger. Metadata-only; discriminate on `kind`. Mirrors the
40
+ * Anthropic Agent SDK's `origin` shape.
41
+ *
42
+ * Encoding note (distinct from absence): an ABSENT origin (`undefined`) means the
43
+ * provenance was NOT stamped — the default for a plain `agent.send()`. The explicit
44
+ * `{ kind: "human" }` is a positive marker a HOST stamps to say "this turn is
45
+ * definitely from a human" (e.g. to distinguish a real user message from an
46
+ * un-attributed one in an audit UI). The two are not interchangeable: `undefined`
47
+ * = unknown/unstamped; `{ kind: "human" }` = explicitly human. Consumers writing an
48
+ * exhaustive `switch (origin?.kind)` therefore handle `undefined` (unstamped) and
49
+ * `"human"` (explicit) as separate, meaningful cases.
50
+ *
51
+ * @public
52
+ */
53
+ export type MessageOrigin =
54
+ /** Explicitly a human-triggered turn — a positive marker a host stamps (NOT the
55
+ * same as an absent/unstamped origin, which is `undefined`). */
56
+ {
57
+ readonly kind: "human";
58
+ }
59
+ /** Another agent (a Squad peer or an a2a sender) triggered this turn. */
60
+ | {
61
+ readonly kind: "peer";
62
+ readonly from: string;
63
+ }
64
+ /** A background task's completion re-entered the agent as a follow-up turn. */
65
+ | {
66
+ readonly kind: "task-notification";
67
+ }
68
+ /** A delegating/handoff coordinator triggered this turn. `from` is the coordinator's
69
+ * id when known, omitted for an anonymous coordinator. */
70
+ | {
71
+ readonly kind: "coordinator";
72
+ readonly from?: string;
73
+ }
74
+ /** The loop's continuation driver re-sent to continue truncated work. */
75
+ | {
76
+ readonly kind: "auto-continuation";
77
+ };
35
78
  /**
36
79
  * Terminal result of a {@link Run}.
37
80
  *
@@ -44,6 +87,15 @@ export interface RunResult {
44
87
  model?: ModelSelection;
45
88
  durationMs?: number;
46
89
  git?: RunGitInfo;
90
+ /**
91
+ * SE3 — provenance forwarded from {@link SendOptions.origin}: who triggered
92
+ * this turn (human / peer / task-notification / coordinator / auto-continuation).
93
+ * `undefined` means the provenance was not stamped (a plain `agent.send()`).
94
+ * Metadata-only — never affects routing.
95
+ *
96
+ * @public
97
+ */
98
+ origin?: MessageOrigin;
47
99
  /**
48
100
  * Structured error detail, populated when `status === "error"`. Surfaces
49
101
  * the diagnostic that emit-error-event pushes into the stream so callers
@@ -218,7 +270,27 @@ export interface SDKUserMessage {
218
270
  * @public
219
271
  */
220
272
  export interface SendOptions {
221
- model?: ModelSelection;
273
+ /**
274
+ * Per-send model override. SE8 — accepts a bare-string id shorthand
275
+ * (`"openai/gpt-4o-mini"`, normalized to `{ id }`) OR a {@link ModelSelection}
276
+ * object (use the object form to pass `params`).
277
+ */
278
+ model?: string | ModelSelection;
279
+ /**
280
+ * SE3 — provenance of this turn (who triggered it). Stamped by the multi-agent
281
+ * path (Squad peer, a2a sender, coordinator/handoff, background task-notification)
282
+ * and forwarded onto {@link RunResult.origin}. Metadata-only — the value never
283
+ * changes routing or dispatch. Omit to leave the turn un-attributed; a host may
284
+ * pass `{ kind: "human" }` to positively mark a human turn.
285
+ */
286
+ origin?: MessageOrigin;
287
+ /**
288
+ * SE2 — opt-in typed runtime-EVENT sink. Receives out-of-band `RunEvent`s
289
+ * (permission_denied, tool_progress, rate_limit, task_*, compact_boundary) for
290
+ * observability, ADDITIVE to the `SDKMessage` content stream. Best-effort: a
291
+ * throwing sink never breaks the run. Discriminate on `event.type`.
292
+ */
293
+ onRunEvent?: import("./run-events.js").RunEventSink;
222
294
  /**
223
295
  * Doom-loop guard config. The loop stops (with `terminal: "no_progress"`, `RunResult.stoppedByDoomLoop`)
224
296
  * when the model repeats IDENTICAL tool calls to the hard threshold. On by default with generous
@@ -356,3 +428,45 @@ export interface Run {
356
428
  /** Subscribe to status changes. Returns an unsubscribe function. */
357
429
  onDidChangeStatus(listener: (status: RunStatus) => void): () => void;
358
430
  }
431
+ /**
432
+ * SE9 — options for the integrated structured-output method `agent.generate`: the
433
+ * {@link SendOptions} that drive the tool loop (phase 1) plus the required `output`
434
+ * Zod schema and structuring knobs (phase 2). Co-located with `SendOptions` /
435
+ * `RunResult` (which they extend/use) so the public `SDKAgent` interface does not
436
+ * import the runtime `agent-generate` module (breaks the type cycle).
437
+ *
438
+ * @public
439
+ */
440
+ export interface GenerateOptions<T extends import("zod").ZodType> extends SendOptions {
441
+ /** Zod schema the final answer is coerced into (the structuring contract). */
442
+ output: T;
443
+ /** Retry budget on the structuring phase's parse-failures (reused from generateObject). Default 1. */
444
+ maxRetries?: number;
445
+ /**
446
+ * What the STRUCTURING phase (phase 2) does when the model's output still fails
447
+ * Zod validation after retries. Default `"throw"`. `"return-partial"` / `"return-raw"`
448
+ * salvage the object; the salvaged/raw value is in {@link GenerateRunResult.raw}
449
+ * (the pre-parse structuring input) — NOT the phase-1 text answer, which is in
450
+ * `result.result`.
451
+ */
452
+ errorStrategy?: "throw" | "return-partial" | "return-raw";
453
+ }
454
+ /**
455
+ * SE9 — result of `agent.generate`: the validated typed object plus the underlying
456
+ * tool-loop {@link RunResult} (status / usage / model) and the raw pre-parse input.
457
+ *
458
+ * @public
459
+ */
460
+ export interface GenerateRunResult<O> {
461
+ /** The validated object — inferred type from the `output` schema. */
462
+ object: O;
463
+ /** The underlying tool-loop run (phase 1). */
464
+ result: RunResult;
465
+ /** Raw model input to the synthetic `output` tool, before the Zod parse. */
466
+ raw: unknown;
467
+ /** Combined token usage of the structuring phase. */
468
+ usage: {
469
+ inputTokens: number;
470
+ outputTokens: number;
471
+ };
472
+ }
@@ -0,0 +1,99 @@
1
+ /**
2
+ * SE4 — public types for the session-management surface (`createSessionManager`).
3
+ *
4
+ * A "session" is one conversation in a {@link ConversationStorageAdapter}. These
5
+ * DTOs are deliberately LIGHT — derived display metadata, not raw store rows — so
6
+ * a host (TheoKit) can build a session list/UI without reaching into storage
7
+ * internals.
8
+ *
9
+ * @public
10
+ */
11
+ import type { StoredMessage } from "./conversation-storage.js";
12
+ /**
13
+ * Light, display-oriented metadata for one session, derived from its transcript
14
+ * plus any host-set title/tag. All fields except `id`/`messageCount` are optional
15
+ * because a fresh or empty session may lack them.
16
+ *
17
+ * @public
18
+ */
19
+ export interface SessionSummary {
20
+ /** The conversation id (verbatim — includes any scope prefix like `user__`). */
21
+ readonly id: string;
22
+ /** Number of stored messages in the transcript. */
23
+ readonly messageCount: number;
24
+ /** Content of the first `user` message, when present — the opening prompt. */
25
+ readonly firstPrompt?: string;
26
+ /** Largest message timestamp (`StoredMessage.at`) seen, when any is stamped. */
27
+ readonly lastModified?: number;
28
+ /** Host-set title (via `renameSession`), when set. */
29
+ readonly title?: string;
30
+ /** Host-set tag (via `tagSession`), when set. */
31
+ readonly tag?: string;
32
+ /**
33
+ * A short display preview: the `title` when set, otherwise the `firstPrompt`
34
+ * (truncated). Undefined only when the session has neither a title nor a first
35
+ * user prompt.
36
+ */
37
+ readonly summary?: string;
38
+ }
39
+ /**
40
+ * Windowing for {@link SessionManager.listSessions}. `offset` counts from the
41
+ * first listed session; `limit` bounds the returned count. Omitting both lists
42
+ * all sessions (bounded only by the adapter's own `listConversationIds` limit).
43
+ *
44
+ * @public
45
+ */
46
+ export interface SessionListOptions {
47
+ readonly offset?: number;
48
+ readonly limit?: number;
49
+ }
50
+ /**
51
+ * Typed capability result — the graceful-degradation signal for operations an
52
+ * adapter cannot perform (listing, or writing session metadata). Prefer this over
53
+ * throwing on every call: a host inspects `supported` once and hides the
54
+ * corresponding UI affordance. Mirrors the SDK's existing degradation precedent
55
+ * (`ConversationStorageAdapter.listConversationIds` returning `undefined`,
56
+ * `Run.unsupportedReason`).
57
+ *
58
+ * @public
59
+ */
60
+ export type SessionCapabilityResult<T> = {
61
+ readonly supported: true;
62
+ readonly value: T;
63
+ } | {
64
+ readonly supported: false;
65
+ readonly reason: string;
66
+ };
67
+ /**
68
+ * The session-management surface returned by `createSessionManager`. Bound to a
69
+ * single {@link ConversationStorageAdapter} instance (the same one a host passed
70
+ * to `Agent.create({ conversationStorage })`).
71
+ *
72
+ * @public
73
+ */
74
+ export interface SessionManager {
75
+ /**
76
+ * List sessions with light derived metadata. Returns an unsupported result
77
+ * when the adapter cannot enumerate conversations (`listConversationIds`
78
+ * absent or returning `undefined`).
79
+ */
80
+ listSessions(opts?: SessionListOptions): Promise<SessionCapabilityResult<readonly SessionSummary[]>>;
81
+ /**
82
+ * Read a session's stored transcript (passthrough to the adapter's mandatory
83
+ * `getMessages`). Always supported. `{ offset, limit }` windows the result.
84
+ */
85
+ getSessionMessages(id: string, opts?: {
86
+ offset?: number;
87
+ limit?: number;
88
+ }): Promise<readonly StoredMessage[]>;
89
+ /**
90
+ * Set a session's title. Unsupported when the adapter cannot write session
91
+ * metadata (`setSessionMeta` absent).
92
+ */
93
+ renameSession(id: string, title: string): Promise<SessionCapabilityResult<void>>;
94
+ /**
95
+ * Set (`tag`) or clear (`null`) a session's tag. Unsupported when the adapter
96
+ * cannot write session metadata (`setSessionMeta` absent).
97
+ */
98
+ tagSession(id: string, tag: string | null): Promise<SessionCapabilityResult<void>>;
99
+ }
@@ -10,6 +10,7 @@
10
10
  */
11
11
  import type { ZodType } from "zod";
12
12
  import type { SDKAgent } from "./agent.js";
13
+ import type { MessageOrigin } from "./run.js";
13
14
  export type Step = FnStep | AgentStep | ParallelStep | BranchStep | ForeachStep | DowhileStep | SleepStep | SuspendStep;
14
15
  /** A pure function step. */
15
16
  export interface FnStep {
@@ -29,6 +30,12 @@ export interface AgentStep {
29
30
  readonly agent: SDKAgent;
30
31
  readonly promptTemplate: string | ((input: unknown) => string);
31
32
  readonly retry?: RetryPolicy;
33
+ /**
34
+ * SE3 — provenance stamped onto this step's `agent.send()` (forwarded to
35
+ * `RunResult.origin`). Squad sets `{ kind: "peer", from: "agent-<i-1>" }` on
36
+ * every step after the first so a peer-driven turn is attributable.
37
+ */
38
+ readonly origin?: MessageOrigin;
32
39
  }
33
40
  /** N concurrent branches, each its own mini-step-list. */
34
41
  export interface ParallelStep {
package/dist/workflow.cjs CHANGED
@@ -681,7 +681,10 @@ async function runAgentStep(step, input, ctx) {
681
681
  };
682
682
  }
683
683
  const exec = async () => {
684
- const run = await step.agent.send(prompt);
684
+ const run = await step.agent.send(
685
+ prompt,
686
+ step.origin !== void 0 ? { origin: step.origin } : void 0
687
+ );
685
688
  const result = await run.wait();
686
689
  if (result.status === "finished") {
687
690
  return result.result ?? "";
@@ -2462,7 +2465,9 @@ function agentStep(id, agent, promptTemplate, opts) {
2462
2465
  id,
2463
2466
  agent,
2464
2467
  promptTemplate,
2465
- ...opts?.retry !== void 0 ? { retry: opts.retry } : {}
2468
+ ...opts?.retry !== void 0 ? { retry: opts.retry } : {},
2469
+ // SE3 — carry the turn's provenance down to `agent.send()` (metadata-only).
2470
+ ...opts?.origin !== void 0 ? { origin: opts.origin } : {}
2466
2471
  };
2467
2472
  }
2468
2473
  function validateStepId(id) {