@alexkroman1/aai 0.12.3 → 1.0.3

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 (135) hide show
  1. package/.turbo/turbo-build.log +20 -0
  2. package/CHANGELOG.md +176 -0
  3. package/dist/constants-VTFoymJ-.js +47 -0
  4. package/dist/host/_run-code.d.ts +1 -1
  5. package/dist/host/_runtime-conformance.d.ts +4 -5
  6. package/dist/host/builtin-tools.d.ts +11 -9
  7. package/dist/host/runtime-barrel.d.ts +15 -0
  8. package/dist/{direct-executor-DRRrZUp0.js → host/runtime-barrel.js} +453 -348
  9. package/dist/host/runtime-config.d.ts +42 -0
  10. package/dist/host/runtime.d.ts +119 -35
  11. package/dist/host/s2s.d.ts +14 -38
  12. package/dist/host/server.d.ts +16 -8
  13. package/dist/host/session-ctx.d.ts +55 -0
  14. package/dist/host/session.d.ts +20 -70
  15. package/dist/host/tool-executor.d.ts +20 -0
  16. package/dist/host/unstorage-kv.d.ts +1 -1
  17. package/dist/host/ws-handler.d.ts +4 -2
  18. package/dist/index.d.ts +9 -20
  19. package/dist/index.js +63 -2
  20. package/dist/{isolate → sdk}/_internal-types.d.ts +5 -9
  21. package/dist/{isolate → sdk}/constants.d.ts +6 -4
  22. package/dist/sdk/define.d.ts +66 -0
  23. package/dist/{isolate → sdk}/kv.d.ts +1 -49
  24. package/dist/sdk/manifest-barrel.d.ts +8 -0
  25. package/dist/sdk/manifest-barrel.js +52 -0
  26. package/dist/sdk/manifest.d.ts +50 -0
  27. package/dist/{isolate → sdk}/protocol.d.ts +59 -36
  28. package/dist/sdk/protocol.js +163 -0
  29. package/dist/{isolate → sdk}/system-prompt.d.ts +2 -2
  30. package/dist/sdk/types.d.ts +201 -0
  31. package/dist/sdk/ws-upgrade.d.ts +5 -0
  32. package/dist/{system-prompt-DYAYFW99.js → system-prompt-nik_iavo.js} +10 -10
  33. package/dist/types-Cfx_4QDK.js +39 -0
  34. package/dist/ws-upgrade-BeOQ7fXL.js +30 -0
  35. package/exports-no-dev-deps.test.ts +62 -0
  36. package/host/_mock-ws.ts +185 -0
  37. package/host/_run-code.ts +217 -0
  38. package/host/_runtime-conformance.ts +143 -0
  39. package/host/_test-utils.ts +276 -0
  40. package/host/builtin-tools.test.ts +774 -0
  41. package/host/builtin-tools.ts +255 -0
  42. package/host/cleanup.test.ts +422 -0
  43. package/host/fixture-replay.test.ts +463 -0
  44. package/host/fixtures/README.md +40 -0
  45. package/host/fixtures/greeting-session-sequence.json +40 -0
  46. package/host/fixtures/reply-audio-samples.json +42 -0
  47. package/host/fixtures/reply-lifecycle.json +21 -0
  48. package/host/fixtures/session-ready.json +48 -0
  49. package/host/fixtures/session-updated.json +45 -0
  50. package/host/fixtures/simple-question-sequence.json +73 -0
  51. package/host/fixtures/tool-call-sequence.json +114 -0
  52. package/host/fixtures/tool-calls.json +11 -0
  53. package/host/fixtures/tool-config-session-sequence.json +51 -0
  54. package/host/fixtures/user-speech-recognition.json +30 -0
  55. package/host/fixtures/web-search-sequence.json +122 -0
  56. package/host/integration.test.ts +222 -0
  57. package/host/runtime-barrel.ts +25 -0
  58. package/host/runtime-config.test.ts +71 -0
  59. package/host/runtime-config.ts +99 -0
  60. package/host/runtime.test.ts +641 -0
  61. package/host/runtime.ts +308 -0
  62. package/host/s2s-fixtures.test.ts +237 -0
  63. package/host/s2s.test.ts +562 -0
  64. package/host/s2s.ts +310 -0
  65. package/host/server-shutdown.test.ts +76 -0
  66. package/host/server.test.ts +116 -0
  67. package/host/server.ts +223 -0
  68. package/host/session-ctx.ts +107 -0
  69. package/host/session-fixture-replay.test.ts +136 -0
  70. package/host/session-prompt.test.ts +77 -0
  71. package/host/session.test.ts +590 -0
  72. package/host/session.ts +370 -0
  73. package/host/tool-executor.test.ts +124 -0
  74. package/host/tool-executor.ts +80 -0
  75. package/host/unstorage-kv.test.ts +99 -0
  76. package/host/unstorage-kv.ts +69 -0
  77. package/host/ws-handler.test.ts +739 -0
  78. package/host/ws-handler.ts +255 -0
  79. package/index.ts +16 -0
  80. package/package.json +24 -72
  81. package/sdk/_internal-types.test.ts +34 -0
  82. package/sdk/_internal-types.ts +115 -0
  83. package/sdk/compat-fixtures/README.md +26 -0
  84. package/sdk/compat-fixtures/v1.json +68 -0
  85. package/sdk/constants.ts +77 -0
  86. package/sdk/define.test.ts +57 -0
  87. package/sdk/define.ts +88 -0
  88. package/sdk/kv.ts +60 -0
  89. package/sdk/manifest-barrel.ts +12 -0
  90. package/sdk/manifest.test.ts +56 -0
  91. package/sdk/manifest.ts +89 -0
  92. package/sdk/protocol-compat.test.ts +187 -0
  93. package/sdk/protocol-snapshot.test.ts +199 -0
  94. package/sdk/protocol.test.ts +170 -0
  95. package/sdk/protocol.ts +223 -0
  96. package/sdk/schema-alignment.test.ts +191 -0
  97. package/sdk/system-prompt.test.ts +111 -0
  98. package/sdk/system-prompt.ts +74 -0
  99. package/sdk/tsconfig.json +12 -0
  100. package/sdk/types-inference.test.ts +122 -0
  101. package/sdk/types.test.ts +14 -0
  102. package/sdk/types.ts +226 -0
  103. package/sdk/utils.test.ts +52 -0
  104. package/sdk/utils.ts +20 -0
  105. package/sdk/ws-upgrade.test.ts +48 -0
  106. package/sdk/ws-upgrade.ts +13 -0
  107. package/tsconfig.build.json +14 -0
  108. package/tsconfig.json +10 -0
  109. package/tsdown.config.ts +26 -0
  110. package/vitest.config.ts +17 -0
  111. package/dist/host/_test-utils.d.ts +0 -73
  112. package/dist/host/direct-executor.d.ts +0 -130
  113. package/dist/host/index.d.ts +0 -19
  114. package/dist/host/index.js +0 -165
  115. package/dist/host/matchers.d.ts +0 -20
  116. package/dist/host/matchers.js +0 -41
  117. package/dist/host/server.js +0 -164
  118. package/dist/host/testing.d.ts +0 -294
  119. package/dist/host/testing.js +0 -2
  120. package/dist/host/vite-plugin.d.ts +0 -15
  121. package/dist/host/vite-plugin.js +0 -83
  122. package/dist/isolate/_kv-utils.d.ts +0 -10
  123. package/dist/isolate/_utils.js +0 -17
  124. package/dist/isolate/hooks.d.ts +0 -44
  125. package/dist/isolate/hooks.js +0 -58
  126. package/dist/isolate/index.d.ts +0 -18
  127. package/dist/isolate/index.js +0 -6
  128. package/dist/isolate/kv.js +0 -1
  129. package/dist/isolate/protocol.js +0 -2
  130. package/dist/isolate/types.d.ts +0 -418
  131. package/dist/isolate/types.js +0 -175
  132. package/dist/protocol-rcOrz7T3.js +0 -183
  133. package/dist/testing-BreLdpq-.js +0 -513
  134. package/dist/types.test-d.d.ts +0 -7
  135. /package/dist/{isolate/_utils.d.ts → sdk/utils.d.ts} +0 -0
@@ -22,18 +22,14 @@ export type ExecuteTool = (name: string, args: Readonly<Record<string, unknown>>
22
22
  */
23
23
  export declare const AgentConfigSchema: z.ZodObject<{
24
24
  name: z.ZodString;
25
- instructions: z.ZodString;
25
+ systemPrompt: z.ZodString;
26
26
  greeting: z.ZodString;
27
27
  sttPrompt: z.ZodOptional<z.ZodString>;
28
28
  maxSteps: z.ZodOptional<z.ZodNumber>;
29
- toolChoice: z.ZodOptional<z.ZodUnion<readonly [z.ZodEnum<{
29
+ toolChoice: z.ZodOptional<z.ZodEnum<{
30
30
  auto: "auto";
31
31
  required: "required";
32
- none: "none";
33
- }>, z.ZodObject<{
34
- type: z.ZodLiteral<"tool">;
35
- toolName: z.ZodString;
36
- }, z.core.$strip>]>>;
32
+ }>>;
37
33
  builtinTools: z.ZodOptional<z.ZodReadonly<z.ZodArray<z.ZodEnum<{
38
34
  web_search: "web_search";
39
35
  visit_webpage: "visit_webpage";
@@ -51,10 +47,10 @@ export type AgentConfig = z.infer<typeof AgentConfigSchema>;
51
47
  */
52
48
  export interface AgentConfigSource {
53
49
  name: string;
54
- instructions: string;
50
+ systemPrompt: string;
55
51
  greeting: string;
56
52
  sttPrompt?: string | undefined;
57
- maxSteps?: number | ((...args: never[]) => number) | undefined;
53
+ maxSteps?: number | undefined;
58
54
  toolChoice?: AgentConfig["toolChoice"] | undefined;
59
55
  builtinTools?: Readonly<AgentConfig["builtinTools"]> | undefined;
60
56
  idleTimeoutMs?: number | undefined;
@@ -8,8 +8,6 @@
8
8
  export declare const DEFAULT_STT_SAMPLE_RATE = 16000;
9
9
  /** Default sample rate for text-to-speech audio in Hz. */
10
10
  export declare const DEFAULT_TTS_SAMPLE_RATE = 24000;
11
- /** Default timeout for agent lifecycle hooks (onConnect, onTurn, etc). */
12
- export declare const HOOK_TIMEOUT_MS = 5000;
13
11
  /** Default timeout for tool execution in the worker. */
14
12
  export declare const TOOL_EXECUTION_TIMEOUT_MS = 30000;
15
13
  /** Timeout for session.start() (S2S connection setup). */
@@ -30,10 +28,14 @@ export declare const MAX_PAGE_CHARS = 10000;
30
28
  export declare const MAX_HTML_BYTES = 200000;
31
29
  /** Maximum value size for KV store entries (bytes). */
32
30
  export declare const MAX_VALUE_SIZE = 65536;
33
- /** Maximum glob pattern length to prevent ReDoS. */
34
- export declare const MAX_GLOB_PATTERN_LENGTH = 1024;
35
31
  /** Maximum conversation messages to retain (sliding window). */
36
32
  export declare const DEFAULT_MAX_HISTORY = 200;
33
+ /** Maximum WebSocket message payload size (bytes, 1 MiB). */
34
+ export declare const MAX_WS_PAYLOAD_BYTES: number;
35
+ /** Maximum messages buffered while session.start() is pending. */
36
+ export declare const MAX_MESSAGE_BUFFER_SIZE = 100;
37
+ /** WebSocket readyState value indicating the connection is open. */
38
+ export declare const WS_OPEN = 1;
37
39
  /**
38
40
  * Content-Security-Policy applied to agent UI pages (both self-hosted and
39
41
  * platform). Single source of truth — used by `secureHeaders` middleware
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Helper functions for defining agents and tools with full type inference.
3
+ */
4
+ import type { z } from "zod";
5
+ import { type AgentDef, type BuiltinTool, type ToolChoice, type ToolContext, type ToolDef } from "./types.ts";
6
+ /**
7
+ * Define a tool with typed parameters and execute function.
8
+ *
9
+ * Identity function for type inference — returns the input unchanged.
10
+ * Follows the Vercel AI SDK `tool()` pattern.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * import { tool } from "@alexkroman1/aai";
15
+ * import { z } from "zod";
16
+ *
17
+ * const greet = tool({
18
+ * description: "Greet someone by name",
19
+ * parameters: z.object({ name: z.string() }),
20
+ * execute: ({ name }) => `Hello, ${name}!`,
21
+ * });
22
+ * ```
23
+ *
24
+ * @public
25
+ */
26
+ export declare function tool<P extends z.ZodObject<z.ZodRawShape>>(def: {
27
+ description: string;
28
+ parameters?: P;
29
+ execute(args: z.infer<P>, ctx: ToolContext): Promise<unknown> | unknown;
30
+ }): ToolDef<P>;
31
+ /**
32
+ * Define an agent with tools, system prompt, and configuration.
33
+ *
34
+ * Applies sensible defaults for omitted fields. Export as the default
35
+ * export of your `agent.ts` file.
36
+ *
37
+ * @example
38
+ * ```ts
39
+ * import { agent, tool } from "@alexkroman1/aai";
40
+ * import { z } from "zod";
41
+ *
42
+ * const myTool = tool({
43
+ * description: "Echo a message",
44
+ * parameters: z.object({ message: z.string() }),
45
+ * execute: ({ message }) => message,
46
+ * });
47
+ *
48
+ * export default agent({
49
+ * name: "Echo Agent",
50
+ * tools: { echo: myTool },
51
+ * });
52
+ * ```
53
+ *
54
+ * @public
55
+ */
56
+ export declare function agent(def: {
57
+ name: string;
58
+ systemPrompt?: string;
59
+ greeting?: string;
60
+ tools?: Record<string, ToolDef>;
61
+ builtinTools?: BuiltinTool[];
62
+ maxSteps?: number;
63
+ toolChoice?: ToolChoice;
64
+ sttPrompt?: string;
65
+ idleTimeoutMs?: number;
66
+ }): AgentDef;
@@ -1,38 +1,10 @@
1
1
  /**
2
2
  * Key-value storage interface and shared utilities.
3
3
  */
4
- /**
5
- * A single key-value entry returned by {@link Kv.list}.
6
- *
7
- * @typeParam T - The type of the stored value. Defaults to `unknown`.
8
- *
9
- * @public
10
- */
11
- export type KvEntry<T = unknown> = {
12
- /** The key under which the value is stored. */
13
- key: string;
14
- /** The deserialized value. */
15
- value: T;
16
- };
17
- /**
18
- * Options for listing keys from the KV store.
19
- *
20
- * Used with {@link Kv.list} and {@link Kv.keys} to control filtering,
21
- * ordering, and pagination.
22
- *
23
- * @public
24
- */
25
- export type KvListOptions = {
26
- /** Maximum number of entries to return. */
27
- limit?: number;
28
- /** Return entries in reverse key order. */
29
- reverse?: boolean;
30
- };
31
4
  /**
32
5
  * Async key-value store interface used by agents.
33
6
  *
34
- * Agents access the KV store via `ToolContext.kv` or
35
- * `HookContext.kv`. Values are JSON-serialized and stored as
7
+ * Agents access the KV store via `ToolContext.kv`. Values are JSON-serialized and stored as
36
8
  * strings with an optional TTL.
37
9
  *
38
10
  * @example
@@ -78,26 +50,6 @@ export type Kv = {
78
50
  * @param keys - A single key or array of keys to delete. No-op for keys that do not exist.
79
51
  */
80
52
  delete(keys: string | string[]): Promise<void>;
81
- /**
82
- * List entries whose keys start with the given prefix.
83
- *
84
- * Results are sorted by key in ascending lexicographic order by default.
85
- *
86
- * @typeParam T - The expected type of the stored values.
87
- * @param prefix - Key prefix to filter by. Use `""` to list all entries.
88
- * @param options - Optional pagination and ordering settings.
89
- * @returns An array of matching {@link KvEntry} objects.
90
- */
91
- list<T = unknown>(prefix: string, options?: KvListOptions): Promise<KvEntry<T>[]>;
92
- /**
93
- * List all keys, optionally filtered by a prefix or glob-style pattern.
94
- *
95
- * @param pattern - Optional prefix string or glob pattern (e.g. `"user:*"`).
96
- * A pattern without wildcards (`*`) is treated as a prefix match.
97
- * If omitted, all keys are returned.
98
- * @returns An array of matching key strings.
99
- */
100
- keys(pattern?: string): Promise<string[]>;
101
53
  /**
102
54
  * Close the KV store, releasing any resources (intervals, database handles).
103
55
  *
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Manifest barrel — agent manifest parsing and tool schema conversion.
3
+ *
4
+ * Used by aai-cli (scanner, bundler) and aai-server (tests).
5
+ */
6
+ export * from "./_internal-types.ts";
7
+ export * from "./manifest.ts";
8
+ export * from "./system-prompt.ts";
@@ -0,0 +1,52 @@
1
+ import { r as DEFAULT_SYSTEM_PROMPT, t as BuiltinToolSchema } from "../types-Cfx_4QDK.js";
2
+ import { a as agentToolsToSchemas, i as ToolSchemaSchema, n as AgentConfigSchema, o as toAgentConfig, r as EMPTY_PARAMS, t as buildSystemPrompt } from "../system-prompt-nik_iavo.js";
3
+ import { z } from "zod";
4
+ //#region sdk/manifest.ts
5
+ /**
6
+ * Canonical manifest format for directory-based agents.
7
+ *
8
+ * Flows from build → host → sdk. Validated via Zod at the boundary,
9
+ * then used as a plain typed object throughout the runtime.
10
+ */
11
+ const ToolManifestSchema = z.object({
12
+ description: z.string(),
13
+ parameters: z.record(z.string(), z.unknown()).optional()
14
+ });
15
+ const ManifestSchema = z.object({
16
+ name: z.string().min(1),
17
+ systemPrompt: z.string().optional(),
18
+ greeting: z.string().optional(),
19
+ sttPrompt: z.string().optional(),
20
+ builtinTools: z.array(BuiltinToolSchema).optional(),
21
+ maxSteps: z.number().int().positive().optional(),
22
+ toolChoice: z.enum(["auto", "required"]).optional(),
23
+ idleTimeoutMs: z.number().int().positive().optional(),
24
+ theme: z.record(z.string(), z.string()).optional(),
25
+ tools: z.record(z.string(), ToolManifestSchema).optional()
26
+ });
27
+ /**
28
+ * Parse and normalize a raw agent manifest, applying defaults for all
29
+ * optional fields. Input is typically the JSON from a bundled agent.ts.
30
+ *
31
+ * Key defaults:
32
+ * - `maxSteps`: 5 — prevents runaway tool-call loops in a single reply
33
+ * - `toolChoice`: "auto" — LLM decides when to use tools vs respond directly
34
+ * - `builtinTools`: [] — no built-in tools unless explicitly opted in
35
+ */
36
+ function parseManifest(input) {
37
+ const parsed = ManifestSchema.parse(input);
38
+ return {
39
+ name: parsed.name,
40
+ systemPrompt: parsed.systemPrompt ?? DEFAULT_SYSTEM_PROMPT,
41
+ greeting: parsed.greeting ?? "Hey there. I'm a voice assistant. What can I help you with?",
42
+ sttPrompt: parsed.sttPrompt,
43
+ builtinTools: parsed.builtinTools ?? [],
44
+ maxSteps: parsed.maxSteps ?? 5,
45
+ toolChoice: parsed.toolChoice ?? "auto",
46
+ idleTimeoutMs: parsed.idleTimeoutMs,
47
+ theme: parsed.theme,
48
+ tools: parsed.tools ?? {}
49
+ };
50
+ }
51
+ //#endregion
52
+ export { AgentConfigSchema, EMPTY_PARAMS, ToolSchemaSchema, agentToolsToSchemas, buildSystemPrompt, parseManifest, toAgentConfig };
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Canonical manifest format for directory-based agents.
3
+ *
4
+ * Flows from build → host → sdk. Validated via Zod at the boundary,
5
+ * then used as a plain typed object throughout the runtime.
6
+ */
7
+ /**
8
+ * Tool definition as it appears in the serialized manifest JSON.
9
+ *
10
+ * This is the JSON-safe representation. Compare with `ToolDef` (in types.ts)
11
+ * which uses Zod schemas for parameters — `agentToolsToSchemas()` in
12
+ * `_internal-types.ts` converts ToolDef → ToolSchema (JSON Schema) for transport.
13
+ */
14
+ export type ToolManifest = {
15
+ description: string;
16
+ parameters?: Record<string, unknown> | undefined;
17
+ };
18
+ /** Normalized agent manifest — all optional fields resolved to defaults. */
19
+ export type Manifest = {
20
+ /** Agent display name (from `agent({ name: "..." })`). */
21
+ name: string;
22
+ /** System prompt sent to the LLM. Defaults to {@link DEFAULT_SYSTEM_PROMPT}. */
23
+ systemPrompt: string;
24
+ /** Initial greeting spoken to the user on connect. Defaults to {@link DEFAULT_GREETING}. */
25
+ greeting: string;
26
+ /** Optional prompt hint for the STT engine (improves transcription of domain terms). */
27
+ sttPrompt?: string | undefined;
28
+ /** Enabled built-in tools: `web_search`, `visit_webpage`, `fetch_json`, `run_code`. */
29
+ builtinTools: string[];
30
+ /** Max tool calls per LLM reply. Prevents runaway loops. Default: 5. */
31
+ maxSteps: number;
32
+ /** `"auto"` = LLM decides when to use tools; `"required"` = always call a tool. */
33
+ toolChoice: "auto" | "required";
34
+ /** Idle timeout in ms before auto-closing the session. `undefined` = use default (5 min). */
35
+ idleTimeoutMs?: number | undefined;
36
+ /** CSS custom properties for agent UI theming. */
37
+ theme?: Record<string, string> | undefined;
38
+ /** Custom tool definitions keyed by tool name. */
39
+ tools: Record<string, ToolManifest>;
40
+ };
41
+ /**
42
+ * Parse and normalize a raw agent manifest, applying defaults for all
43
+ * optional fields. Input is typically the JSON from a bundled agent.ts.
44
+ *
45
+ * Key defaults:
46
+ * - `maxSteps`: 5 — prevents runaway tool-call loops in a single reply
47
+ * - `toolChoice`: "auto" — LLM decides when to use tools vs respond directly
48
+ * - `builtinTools`: [] — no built-in tools unless explicitly opted in
49
+ */
50
+ export declare function parseManifest(input: unknown): Manifest;
@@ -10,6 +10,53 @@ import { z } from "zod";
10
10
  * All audio frames are 16-bit signed PCM, little-endian, mono.
11
11
  */
12
12
  export declare const AUDIO_FORMAT = "pcm16";
13
+ /**
14
+ * Minimal envelope schema for two-phase message parsing.
15
+ *
16
+ * When a strict schema (ServerMessageSchema / ClientMessageSchema) rejects a
17
+ * message, this schema determines whether the message is a valid but
18
+ * *unrecognised* type (safe to ignore during rolling upgrades) or genuinely
19
+ * malformed (should be warned about).
20
+ */
21
+ export declare const MessageEnvelopeSchema: z.ZodObject<{
22
+ type: z.ZodString;
23
+ }, z.core.$loose>;
24
+ /**
25
+ * Two-phase message parse: tries the strict schema first, then falls back to
26
+ * the envelope to distinguish unknown-but-valid types (safe to ignore during
27
+ * rolling upgrades) from genuinely malformed messages.
28
+ *
29
+ * Return value when `ok: false`:
30
+ * - `malformed: true` — message doesn't even have a `{ type: string }` shape;
31
+ * likely corrupt data, should warn
32
+ * - `malformed: false` — has a valid `type` field but the type is unrecognised;
33
+ * safe to ignore (e.g. new message type from a newer server version)
34
+ */
35
+ export declare function lenientParse<T>(schema: z.ZodType<T>, json: unknown): {
36
+ ok: true;
37
+ data: T;
38
+ } | {
39
+ ok: false;
40
+ malformed: boolean;
41
+ error: string;
42
+ };
43
+ /** Zod schema for the KV "get" operation. */
44
+ export declare const KvGetSchema: z.ZodObject<{
45
+ op: z.ZodLiteral<"get">;
46
+ key: z.ZodString;
47
+ }, z.core.$strip>;
48
+ /** Zod schema for the KV "set" operation. */
49
+ export declare const KvSetSchema: z.ZodObject<{
50
+ op: z.ZodLiteral<"set">;
51
+ key: z.ZodString;
52
+ value: z.ZodUnknown;
53
+ expireIn: z.ZodOptional<z.ZodNumber>;
54
+ }, z.core.$strip>;
55
+ /** Zod schema for the KV "del" operation. */
56
+ export declare const KvDelSchema: z.ZodObject<{
57
+ op: z.ZodLiteral<"del">;
58
+ key: z.ZodString;
59
+ }, z.core.$strip>;
13
60
  /** Zod schema for KV operation requests from the worker to the host. */
14
61
  export declare const KvRequestSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
15
62
  op: z.ZodLiteral<"get">;
@@ -17,19 +64,11 @@ export declare const KvRequestSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
17
64
  }, z.core.$strip>, z.ZodObject<{
18
65
  op: z.ZodLiteral<"set">;
19
66
  key: z.ZodString;
20
- value: z.ZodString;
67
+ value: z.ZodUnknown;
21
68
  expireIn: z.ZodOptional<z.ZodNumber>;
22
69
  }, z.core.$strip>, z.ZodObject<{
23
70
  op: z.ZodLiteral<"del">;
24
71
  key: z.ZodString;
25
- }, z.core.$strip>, z.ZodObject<{
26
- op: z.ZodLiteral<"list">;
27
- prefix: z.ZodString;
28
- limit: z.ZodOptional<z.ZodNumber>;
29
- reverse: z.ZodOptional<z.ZodBoolean>;
30
- }, z.core.$strip>, z.ZodObject<{
31
- op: z.ZodLiteral<"keys">;
32
- pattern: z.ZodOptional<z.ZodString>;
33
72
  }, z.core.$strip>], "op">;
34
73
  /** KV operation request — discriminated union on the `op` field. */
35
74
  export type KvRequest = z.infer<typeof KvRequestSchema>;
@@ -39,11 +78,11 @@ export type KvRequest = z.infer<typeof KvRequestSchema>;
39
78
  */
40
79
  export declare const SessionErrorCodeSchema: z.ZodEnum<{
41
80
  tool: "tool";
81
+ connection: "connection";
42
82
  stt: "stt";
43
83
  llm: "llm";
44
84
  tts: "tts";
45
85
  protocol: "protocol";
46
- connection: "connection";
47
86
  audio: "audio";
48
87
  internal: "internal";
49
88
  }>;
@@ -59,22 +98,14 @@ export declare const ClientEventSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
59
98
  }, z.core.$strip>, z.ZodObject<{
60
99
  type: z.ZodLiteral<"speech_stopped">;
61
100
  }, z.core.$strip>, z.ZodObject<{
62
- type: z.ZodLiteral<"transcript">;
101
+ type: z.ZodLiteral<"user_transcript">;
63
102
  text: z.ZodString;
64
- isFinal: z.ZodBoolean;
65
103
  turnOrder: z.ZodOptional<z.ZodNumber>;
66
104
  }, z.core.$strip>, z.ZodObject<{
67
- type: z.ZodLiteral<"turn">;
105
+ type: z.ZodLiteral<"agent_transcript">;
68
106
  text: z.ZodString;
69
- turnOrder: z.ZodOptional<z.ZodNumber>;
70
107
  }, z.core.$strip>, z.ZodObject<{
71
- type: z.ZodLiteral<"chat">;
72
- text: z.ZodString;
73
- }, z.core.$strip>, z.ZodObject<{
74
- type: z.ZodLiteral<"chat_delta">;
75
- text: z.ZodString;
76
- }, z.core.$strip>, z.ZodObject<{
77
- type: z.ZodLiteral<"tool_call_start">;
108
+ type: z.ZodLiteral<"tool_call">;
78
109
  toolCallId: z.ZodString;
79
110
  toolName: z.ZodString;
80
111
  args: z.ZodRecord<z.ZodString, z.ZodUnknown>;
@@ -83,7 +114,7 @@ export declare const ClientEventSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
83
114
  toolCallId: z.ZodString;
84
115
  result: z.ZodString;
85
116
  }, z.core.$strip>, z.ZodObject<{
86
- type: z.ZodLiteral<"tts_done">;
117
+ type: z.ZodLiteral<"reply_done">;
87
118
  }, z.core.$strip>, z.ZodObject<{
88
119
  type: z.ZodLiteral<"cancelled">;
89
120
  }, z.core.$strip>, z.ZodObject<{
@@ -94,11 +125,11 @@ export declare const ClientEventSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
94
125
  type: z.ZodLiteral<"error">;
95
126
  code: z.ZodEnum<{
96
127
  tool: "tool";
128
+ connection: "connection";
97
129
  stt: "stt";
98
130
  llm: "llm";
99
131
  tts: "tts";
100
132
  protocol: "protocol";
101
- connection: "connection";
102
133
  audio: "audio";
103
134
  internal: "internal";
104
135
  }>;
@@ -147,22 +178,14 @@ export declare const ServerMessageSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
147
178
  }, z.core.$strip>, z.ZodObject<{
148
179
  type: z.ZodLiteral<"speech_stopped">;
149
180
  }, z.core.$strip>, z.ZodObject<{
150
- type: z.ZodLiteral<"transcript">;
151
- text: z.ZodString;
152
- isFinal: z.ZodBoolean;
153
- turnOrder: z.ZodOptional<z.ZodNumber>;
154
- }, z.core.$strip>, z.ZodObject<{
155
- type: z.ZodLiteral<"turn">;
181
+ type: z.ZodLiteral<"user_transcript">;
156
182
  text: z.ZodString;
157
183
  turnOrder: z.ZodOptional<z.ZodNumber>;
158
184
  }, z.core.$strip>, z.ZodObject<{
159
- type: z.ZodLiteral<"chat">;
185
+ type: z.ZodLiteral<"agent_transcript">;
160
186
  text: z.ZodString;
161
187
  }, z.core.$strip>, z.ZodObject<{
162
- type: z.ZodLiteral<"chat_delta">;
163
- text: z.ZodString;
164
- }, z.core.$strip>, z.ZodObject<{
165
- type: z.ZodLiteral<"tool_call_start">;
188
+ type: z.ZodLiteral<"tool_call">;
166
189
  toolCallId: z.ZodString;
167
190
  toolName: z.ZodString;
168
191
  args: z.ZodRecord<z.ZodString, z.ZodUnknown>;
@@ -171,7 +194,7 @@ export declare const ServerMessageSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
171
194
  toolCallId: z.ZodString;
172
195
  result: z.ZodString;
173
196
  }, z.core.$strip>, z.ZodObject<{
174
- type: z.ZodLiteral<"tts_done">;
197
+ type: z.ZodLiteral<"reply_done">;
175
198
  }, z.core.$strip>, z.ZodObject<{
176
199
  type: z.ZodLiteral<"cancelled">;
177
200
  }, z.core.$strip>, z.ZodObject<{
@@ -182,11 +205,11 @@ export declare const ServerMessageSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
182
205
  type: z.ZodLiteral<"error">;
183
206
  code: z.ZodEnum<{
184
207
  tool: "tool";
208
+ connection: "connection";
185
209
  stt: "stt";
186
210
  llm: "llm";
187
211
  tts: "tts";
188
212
  protocol: "protocol";
189
- connection: "connection";
190
213
  audio: "audio";
191
214
  internal: "internal";
192
215
  }>;
@@ -0,0 +1,163 @@
1
+ import { f as MAX_TOOL_RESULT_CHARS } from "../constants-VTFoymJ-.js";
2
+ import { z } from "zod";
3
+ //#region sdk/protocol.ts
4
+ /**
5
+ * WebSocket wire-format types shared by server and client.
6
+ *
7
+ * Note: this module is for internal use only and should not be used directly.
8
+ */
9
+ /**
10
+ * Audio codec identifier used in the wire protocol.
11
+ *
12
+ * All audio frames are 16-bit signed PCM, little-endian, mono.
13
+ */
14
+ const AUDIO_FORMAT = "pcm16";
15
+ /**
16
+ * Minimal envelope schema for two-phase message parsing.
17
+ *
18
+ * When a strict schema (ServerMessageSchema / ClientMessageSchema) rejects a
19
+ * message, this schema determines whether the message is a valid but
20
+ * *unrecognised* type (safe to ignore during rolling upgrades) or genuinely
21
+ * malformed (should be warned about).
22
+ */
23
+ const MessageEnvelopeSchema = z.object({ type: z.string() }).passthrough();
24
+ /**
25
+ * Two-phase message parse: tries the strict schema first, then falls back to
26
+ * the envelope to distinguish unknown-but-valid types (safe to ignore during
27
+ * rolling upgrades) from genuinely malformed messages.
28
+ *
29
+ * Return value when `ok: false`:
30
+ * - `malformed: true` — message doesn't even have a `{ type: string }` shape;
31
+ * likely corrupt data, should warn
32
+ * - `malformed: false` — has a valid `type` field but the type is unrecognised;
33
+ * safe to ignore (e.g. new message type from a newer server version)
34
+ */
35
+ function lenientParse(schema, json) {
36
+ const result = schema.safeParse(json);
37
+ if (result.success) return {
38
+ ok: true,
39
+ data: result.data
40
+ };
41
+ return {
42
+ ok: false,
43
+ malformed: !MessageEnvelopeSchema.safeParse(json).success,
44
+ error: result.error.message
45
+ };
46
+ }
47
+ /** Zod schema for the KV "get" operation. */
48
+ const KvGetSchema = z.object({
49
+ op: z.literal("get"),
50
+ key: z.string().min(1)
51
+ });
52
+ /** Zod schema for the KV "set" operation. */
53
+ const KvSetSchema = z.object({
54
+ op: z.literal("set"),
55
+ key: z.string().min(1),
56
+ value: z.unknown(),
57
+ expireIn: z.number().int().positive().optional()
58
+ });
59
+ /** Zod schema for the KV "del" operation. */
60
+ const KvDelSchema = z.object({
61
+ op: z.literal("del"),
62
+ key: z.string().min(1)
63
+ });
64
+ /** Zod schema for KV operation requests from the worker to the host. */
65
+ const KvRequestSchema = z.discriminatedUnion("op", [
66
+ KvGetSchema,
67
+ KvSetSchema,
68
+ KvDelSchema
69
+ ]);
70
+ /**
71
+ * Zod schema for session error codes.
72
+ * @public
73
+ */
74
+ const SessionErrorCodeSchema = z.enum([
75
+ "stt",
76
+ "llm",
77
+ "tts",
78
+ "tool",
79
+ "protocol",
80
+ "connection",
81
+ "audio",
82
+ "internal"
83
+ ]);
84
+ /** Helper: simple event with only a type field. */
85
+ const ev = (t) => z.object({ type: z.literal(t) });
86
+ const turnOrder = z.number().int().nonnegative().optional();
87
+ /** Zod schema for {@link ClientEvent}. */
88
+ const ClientEventSchema = z.discriminatedUnion("type", [
89
+ ev("speech_started"),
90
+ ev("speech_stopped"),
91
+ z.object({
92
+ type: z.literal("user_transcript"),
93
+ text: z.string(),
94
+ turnOrder
95
+ }),
96
+ z.object({
97
+ type: z.literal("agent_transcript"),
98
+ text: z.string()
99
+ }),
100
+ z.object({
101
+ type: z.literal("tool_call"),
102
+ toolCallId: z.string(),
103
+ toolName: z.string(),
104
+ args: z.record(z.string(), z.unknown())
105
+ }),
106
+ z.object({
107
+ type: z.literal("tool_call_done"),
108
+ toolCallId: z.string(),
109
+ result: z.string().max(MAX_TOOL_RESULT_CHARS)
110
+ }),
111
+ ev("reply_done"),
112
+ ev("cancelled"),
113
+ ev("reset"),
114
+ ev("idle_timeout"),
115
+ z.object({
116
+ type: z.literal("error"),
117
+ code: SessionErrorCodeSchema,
118
+ message: z.string()
119
+ })
120
+ ]);
121
+ /** Zod schema for {@link ReadyConfig}. */
122
+ const ReadyConfigSchema = z.object({
123
+ audioFormat: z.enum(["pcm16"]),
124
+ sampleRate: z.number().int().positive(),
125
+ ttsSampleRate: z.number().int().positive()
126
+ });
127
+ /** Zod schema for server→client text messages. */
128
+ const ServerMessageSchema = z.discriminatedUnion("type", [
129
+ z.object({
130
+ type: z.literal("config"),
131
+ audioFormat: z.string(),
132
+ sampleRate: z.number(),
133
+ ttsSampleRate: z.number(),
134
+ sessionId: z.string().optional()
135
+ }),
136
+ ev("audio_done"),
137
+ ...ClientEventSchema.options
138
+ ]);
139
+ /** Zod schema for client→server text messages. */
140
+ const ClientMessageSchema = z.discriminatedUnion("type", [
141
+ ev("audio_ready"),
142
+ ev("cancel"),
143
+ ev("reset"),
144
+ z.object({
145
+ type: z.literal("history"),
146
+ messages: z.array(z.object({
147
+ role: z.enum(["user", "assistant"]),
148
+ content: z.string().max(1e5)
149
+ })).max(200)
150
+ })
151
+ ]);
152
+ /** Build the protocol-level session config from S2S sample rates. */
153
+ function buildReadyConfig(s2sConfig) {
154
+ return {
155
+ audioFormat: AUDIO_FORMAT,
156
+ sampleRate: s2sConfig.inputSampleRate,
157
+ ttsSampleRate: s2sConfig.outputSampleRate
158
+ };
159
+ }
160
+ /** Zod schema for {@link TurnConfig}. */
161
+ const TurnConfigSchema = z.object({ maxSteps: z.number().int().positive().optional() });
162
+ //#endregion
163
+ export { AUDIO_FORMAT, ClientEventSchema, ClientMessageSchema, KvDelSchema, KvGetSchema, KvRequestSchema, KvSetSchema, MessageEnvelopeSchema, ReadyConfigSchema, ServerMessageSchema, SessionErrorCodeSchema, TurnConfigSchema, buildReadyConfig, lenientParse };
@@ -5,10 +5,10 @@ import type { AgentConfig } from "./_internal-types.ts";
5
5
  /**
6
6
  * Build the system prompt sent to the LLM from the agent configuration.
7
7
  *
8
- * Assembles the default instructions, today's date, agent-specific instructions,
8
+ * Assembles the default system prompt, today's date, agent-specific instructions,
9
9
  * and optional sections for tool usage preamble and voice output rules.
10
10
  *
11
- * @param config - The serializable agent configuration (name, instructions, etc.).
11
+ * @param config - The serializable agent configuration (name, systemPrompt, etc.).
12
12
  * @param opts.hasTools - When `true`, appends a preamble instructing the LLM to
13
13
  * speak a brief phrase before each tool call to fill silence.
14
14
  * @param opts.voice - When `true`, appends strict voice-specific output rules