@alexkroman1/aai 0.12.3 → 1.0.2

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 +174 -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
@@ -1,175 +0,0 @@
1
- import { z } from "zod";
2
- //#region isolate/types.ts
3
- /**
4
- * Core type definitions for the AAI agent SDK.
5
- */
6
- /**
7
- * Identity helper that preserves the Zod schema generic for type inference.
8
- *
9
- * When tools are defined inline in `defineAgent({ tools: { ... } })`, the
10
- * generic `P` gets widened to the base `ZodObject` type, so `args` in
11
- * `execute` loses its specific shape. Wrapping a tool definition in
12
- * `defineTool()` lets TypeScript infer `P` from `parameters` and type
13
- * `args` correctly.
14
- *
15
- * @example
16
- * ```ts
17
- * import { defineAgent, defineTool } from "aai";
18
- * import { z } from "zod";
19
- *
20
- * export default defineAgent({
21
- * name: "my-agent",
22
- * tools: {
23
- * greet: defineTool({
24
- * description: "Greet the user",
25
- * parameters: z.object({ name: z.string() }),
26
- * execute: ({ name }) => `Hello, ${name}!`, // name is string
27
- * }),
28
- * },
29
- * });
30
- * ```
31
- *
32
- * @public
33
- */
34
- function defineTool(def) {
35
- return def;
36
- }
37
- /**
38
- * Create a typed `defineTool` helper with the session state type baked in.
39
- *
40
- * When tools need access to typed session state, you'd normally have to write
41
- * verbose generics on every `defineTool` call. `defineToolFactory` eliminates
42
- * that boilerplate by returning a `defineTool` variant that already knows `S`.
43
- *
44
- * @example
45
- * ```ts
46
- * import { defineToolFactory, defineAgent } from "aai";
47
- * import { z } from "zod";
48
- *
49
- * interface PortfolioState { holdings: Map<string, number> }
50
- *
51
- * const tool = defineToolFactory<PortfolioState>();
52
- *
53
- * export default defineAgent<PortfolioState>({
54
- * name: "portfolio",
55
- * state: () => ({ holdings: new Map() }),
56
- * tools: {
57
- * buy: tool({
58
- * description: "Buy shares",
59
- * parameters: z.object({ symbol: z.string(), qty: z.number() }),
60
- * execute: (args, ctx) => {
61
- * // args.symbol is string, ctx.state is PortfolioState
62
- * ctx.state.holdings.set(args.symbol, args.qty);
63
- * },
64
- * }),
65
- * },
66
- * });
67
- * ```
68
- *
69
- * @public
70
- */
71
- function defineToolFactory() {
72
- return (def) => def;
73
- }
74
- /**
75
- * Default system prompt used when `instructions` is not provided.
76
- *
77
- * Optimized for voice-first interactions: short sentences, no visual
78
- * formatting, confident tone, and concise answers.
79
- */
80
- const DEFAULT_INSTRUCTIONS = `\
81
- You are AAI, a helpful AI assistant.
82
-
83
- Voice-First Rules:
84
- - Optimize for natural speech. Avoid jargon unless central to the answer. \
85
- Use short, punchy sentences.
86
- - Never mention "search results," "sources," or "the provided text." \
87
- Speak as if the knowledge is your own.
88
- - No visual formatting. Do not say "bullet point," "bold," or "bracketed one." \
89
- If you need to list items, say "First," "Next," and "Finally."
90
- - Start with the most important information. No introductory filler.
91
- - Be concise. Keep answers to 1-3 sentences. For complex topics, provide a high-level summary.
92
- - Be confident. Avoid hedging phrases like "It seems that" or "I believe."
93
- - If you don't have enough information, say so directly rather than guessing.
94
- - Never use exclamation points. Keep your tone calm and conversational.`;
95
- /** Default greeting spoken when a session starts. */
96
- const DEFAULT_GREETING = "Hey there. I'm a voice assistant. What can I help you with?";
97
- /** @internal Zod schema for {@link BuiltinTool}. Exported for reuse in internal schemas. */
98
- const BuiltinToolSchema = z.enum([
99
- "web_search",
100
- "visit_webpage",
101
- "fetch_json",
102
- "run_code"
103
- ]);
104
- /** @internal Zod schema for {@link ToolChoice}. Exported for reuse in internal schemas. */
105
- const ToolChoiceSchema = z.union([z.enum([
106
- "auto",
107
- "required",
108
- "none"
109
- ]), z.object({
110
- type: z.literal("tool"),
111
- toolName: z.string().min(1)
112
- })]);
113
- const ToolDefSchema = z.object({
114
- description: z.string().min(1, "Tool description must be non-empty"),
115
- parameters: z.custom((val) => val === void 0 || val instanceof z.ZodType, "Expected a Zod schema").optional(),
116
- execute: z.function()
117
- });
118
- const AgentOptionsSchema = z.object({
119
- name: z.string().min(1, "Agent name must be non-empty"),
120
- instructions: z.string().optional(),
121
- greeting: z.string().optional(),
122
- sttPrompt: z.string().optional(),
123
- maxSteps: z.union([z.number().int().positive(), z.function()]).optional(),
124
- toolChoice: ToolChoiceSchema.optional(),
125
- builtinTools: z.array(BuiltinToolSchema).optional(),
126
- tools: z.record(z.string(), ToolDefSchema).optional(),
127
- state: z.function().optional(),
128
- onConnect: z.function().optional(),
129
- onDisconnect: z.function().optional(),
130
- onError: z.function().optional(),
131
- onTurn: z.function().optional(),
132
- idleTimeoutMs: z.number().nonnegative().optional()
133
- });
134
- /**
135
- * Create an agent definition from the given options, applying sensible defaults.
136
- *
137
- * This is the main entry point for defining a voice agent. The returned
138
- * `AgentDef` is consumed by the AAI server at deploy time.
139
- *
140
- * @param options - Configuration for the agent including name, instructions,
141
- * tools, hooks, and other settings.
142
- * @returns A fully resolved agent definition with all defaults applied.
143
- *
144
- * @public
145
- *
146
- * @example Basic agent with a custom tool
147
- * ```ts
148
- * import { defineAgent } from "aai";
149
- * import { z } from "zod";
150
- *
151
- * export default defineAgent({
152
- * name: "greeter",
153
- * instructions: "You greet people warmly.",
154
- * tools: {
155
- * greet: {
156
- * description: "Greet a user by name",
157
- * parameters: z.object({ name: z.string() }),
158
- * execute: ({ name }) => `Hello, ${name}!`,
159
- * },
160
- * },
161
- * });
162
- * ```
163
- */
164
- function defineAgent(options) {
165
- AgentOptionsSchema.parse(options);
166
- return {
167
- ...options,
168
- instructions: options.instructions ?? DEFAULT_INSTRUCTIONS,
169
- greeting: options.greeting ?? "Hey there. I'm a voice assistant. What can I help you with?",
170
- maxSteps: options.maxSteps ?? 5,
171
- tools: options.tools ?? {}
172
- };
173
- }
174
- //#endregion
175
- export { BuiltinToolSchema, DEFAULT_GREETING, DEFAULT_INSTRUCTIONS, ToolChoiceSchema, defineAgent, defineTool, defineTool as tool, defineToolFactory };
@@ -1,183 +0,0 @@
1
- import { z } from "zod";
2
- //#region isolate/constants.ts
3
- /**
4
- * Centralised numeric constants — timeouts, size limits, sample rates.
5
- *
6
- * Every magic number that controls a timeout, buffer size, or threshold
7
- * lives here so the values are discoverable in one place.
8
- */
9
- /** Default sample rate for speech-to-text audio in Hz (AssemblyAI). */
10
- const DEFAULT_STT_SAMPLE_RATE = 16e3;
11
- /** Default sample rate for text-to-speech audio in Hz. */
12
- const DEFAULT_TTS_SAMPLE_RATE = 24e3;
13
- /** Default timeout for agent lifecycle hooks (onConnect, onTurn, etc). */
14
- const HOOK_TIMEOUT_MS = 5e3;
15
- /** Default timeout for tool execution in the worker. */
16
- const TOOL_EXECUTION_TIMEOUT_MS = 3e4;
17
- /** Timeout for session.start() (S2S connection setup). */
18
- const DEFAULT_SESSION_START_TIMEOUT_MS = 1e4;
19
- /** S2S session idle timeout before auto-close. */
20
- const DEFAULT_IDLE_TIMEOUT_MS = 3e5;
21
- /** Per-fetch timeout for network tools (web_search, visit_webpage, fetch_json). */
22
- const FETCH_TIMEOUT_MS = 15e3;
23
- /** Timeout for sandboxed run_code execution. */
24
- const RUN_CODE_TIMEOUT_MS = 5e3;
25
- /** Maximum time to wait for sessions to stop during graceful shutdown. */
26
- const DEFAULT_SHUTDOWN_TIMEOUT_MS = 3e4;
27
- /** Maximum length for tool result strings sent to clients. */
28
- const MAX_TOOL_RESULT_CHARS = 4e3;
29
- /** Maximum chars for webpage text after HTML-to-text conversion. */
30
- const MAX_PAGE_CHARS = 1e4;
31
- /** Maximum bytes to fetch from an HTML page before conversion. */
32
- const MAX_HTML_BYTES = 2e5;
33
- /** Maximum value size for KV store entries (bytes). */
34
- const MAX_VALUE_SIZE = 65536;
35
- /** Maximum glob pattern length to prevent ReDoS. */
36
- const MAX_GLOB_PATTERN_LENGTH = 1024;
37
- /** Maximum conversation messages to retain (sliding window). */
38
- const DEFAULT_MAX_HISTORY = 200;
39
- /**
40
- * Content-Security-Policy applied to agent UI pages (both self-hosted and
41
- * platform). Single source of truth — used by `secureHeaders` middleware
42
- * and per-response CSP headers.
43
- */
44
- const AGENT_CSP = "default-src 'self'; script-src 'self' 'unsafe-eval' blob:; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; connect-src 'self' wss: ws:; img-src 'self' data:; font-src 'self' https://fonts.gstatic.com; object-src 'none'; base-uri 'self'";
45
- //#endregion
46
- //#region isolate/protocol.ts
47
- /**
48
- * WebSocket wire-format types shared by server and client.
49
- *
50
- * Note: this module is for internal use only and should not be used directly.
51
- */
52
- /**
53
- * Audio codec identifier used in the wire protocol.
54
- *
55
- * All audio frames are 16-bit signed PCM, little-endian, mono.
56
- */
57
- const AUDIO_FORMAT = "pcm16";
58
- /** Zod schema for KV operation requests from the worker to the host. */
59
- const KvRequestSchema = z.discriminatedUnion("op", [
60
- z.object({
61
- op: z.literal("get"),
62
- key: z.string().min(1)
63
- }),
64
- z.object({
65
- op: z.literal("set"),
66
- key: z.string().min(1),
67
- value: z.string(),
68
- expireIn: z.number().int().positive().optional()
69
- }),
70
- z.object({
71
- op: z.literal("del"),
72
- key: z.string().min(1)
73
- }),
74
- z.object({
75
- op: z.literal("list"),
76
- prefix: z.string(),
77
- limit: z.number().int().positive().optional(),
78
- reverse: z.boolean().optional()
79
- }),
80
- z.object({
81
- op: z.literal("keys"),
82
- pattern: z.string().optional()
83
- })
84
- ]);
85
- /**
86
- * Zod schema for session error codes.
87
- * @public
88
- */
89
- const SessionErrorCodeSchema = z.enum([
90
- "stt",
91
- "llm",
92
- "tts",
93
- "tool",
94
- "protocol",
95
- "connection",
96
- "audio",
97
- "internal"
98
- ]);
99
- /** Helper: simple event with only a type field. */
100
- const ev = (t) => z.object({ type: z.literal(t) });
101
- /** Helper: event with type + text. */
102
- const textEv = (t) => z.object({
103
- type: z.literal(t),
104
- text: z.string()
105
- });
106
- const turnOrder = z.number().int().nonnegative().optional();
107
- /** Zod schema for {@link ClientEvent}. */
108
- const ClientEventSchema = z.discriminatedUnion("type", [
109
- ev("speech_started"),
110
- ev("speech_stopped"),
111
- z.object({
112
- type: z.literal("transcript"),
113
- text: z.string(),
114
- isFinal: z.boolean(),
115
- turnOrder
116
- }),
117
- textEv("turn").extend({ turnOrder }),
118
- textEv("chat"),
119
- textEv("chat_delta"),
120
- z.object({
121
- type: z.literal("tool_call_start"),
122
- toolCallId: z.string(),
123
- toolName: z.string(),
124
- args: z.record(z.string(), z.unknown())
125
- }),
126
- z.object({
127
- type: z.literal("tool_call_done"),
128
- toolCallId: z.string(),
129
- result: z.string().max(MAX_TOOL_RESULT_CHARS)
130
- }),
131
- ev("tts_done"),
132
- ev("cancelled"),
133
- ev("reset"),
134
- ev("idle_timeout"),
135
- z.object({
136
- type: z.literal("error"),
137
- code: SessionErrorCodeSchema,
138
- message: z.string()
139
- })
140
- ]);
141
- /** Zod schema for {@link ReadyConfig}. */
142
- const ReadyConfigSchema = z.object({
143
- audioFormat: z.enum(["pcm16"]),
144
- sampleRate: z.number().int().positive(),
145
- ttsSampleRate: z.number().int().positive()
146
- });
147
- /** Zod schema for server→client text messages. */
148
- const ServerMessageSchema = z.discriminatedUnion("type", [
149
- z.object({
150
- type: z.literal("config"),
151
- audioFormat: z.string(),
152
- sampleRate: z.number(),
153
- ttsSampleRate: z.number(),
154
- sessionId: z.string().optional()
155
- }),
156
- ev("audio_done"),
157
- ...ClientEventSchema.options
158
- ]);
159
- /** Zod schema for client→server text messages. */
160
- const ClientMessageSchema = z.discriminatedUnion("type", [
161
- ev("audio_ready"),
162
- ev("cancel"),
163
- ev("reset"),
164
- z.object({
165
- type: z.literal("history"),
166
- messages: z.array(z.object({
167
- role: z.enum(["user", "assistant"]),
168
- content: z.string().max(1e5)
169
- })).max(200)
170
- })
171
- ]);
172
- /** Build the protocol-level session config from S2S sample rates. */
173
- function buildReadyConfig(s2sConfig) {
174
- return {
175
- audioFormat: AUDIO_FORMAT,
176
- sampleRate: s2sConfig.inputSampleRate,
177
- ttsSampleRate: s2sConfig.outputSampleRate
178
- };
179
- }
180
- /** Zod schema for {@link TurnConfig}. */
181
- const TurnConfigSchema = z.object({ maxSteps: z.number().int().positive().optional() });
182
- //#endregion
183
- export { MAX_VALUE_SIZE as C, MAX_TOOL_RESULT_CHARS as S, TOOL_EXECUTION_TIMEOUT_MS as T, FETCH_TIMEOUT_MS as _, ReadyConfigSchema as a, MAX_HTML_BYTES as b, TurnConfigSchema as c, DEFAULT_IDLE_TIMEOUT_MS as d, DEFAULT_MAX_HISTORY as f, DEFAULT_TTS_SAMPLE_RATE as g, DEFAULT_STT_SAMPLE_RATE as h, KvRequestSchema as i, buildReadyConfig as l, DEFAULT_SHUTDOWN_TIMEOUT_MS as m, ClientEventSchema as n, ServerMessageSchema as o, DEFAULT_SESSION_START_TIMEOUT_MS as p, ClientMessageSchema as r, SessionErrorCodeSchema as s, AUDIO_FORMAT as t, AGENT_CSP as u, HOOK_TIMEOUT_MS as v, RUN_CODE_TIMEOUT_MS as w, MAX_PAGE_CHARS as x, MAX_GLOB_PATTERN_LENGTH as y };