@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
@@ -0,0 +1,42 @@
1
+ /** Structured context attached to log messages. */
2
+ export type LogContext = Record<string, unknown>;
3
+ /**
4
+ * Structured logger interface. Used by tests to suppress output and by
5
+ * consumers to plug in custom logging backends.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * const myLogger: Logger = {
10
+ * info: (msg, ctx) => myBackend.log("info", msg, ctx),
11
+ * warn: (msg, ctx) => myBackend.log("warn", msg, ctx),
12
+ * error: (msg, ctx) => myBackend.log("error", msg, ctx),
13
+ * debug: (msg, ctx) => myBackend.log("debug", msg, ctx),
14
+ * };
15
+ * createServer({ agent, logger: myLogger });
16
+ * ```
17
+ */
18
+ export type Logger = {
19
+ info(msg: string, ctx?: LogContext): void;
20
+ warn(msg: string, ctx?: LogContext): void;
21
+ error(msg: string, ctx?: LogContext): void;
22
+ debug(msg: string, ctx?: LogContext): void;
23
+ };
24
+ /** Default console-backed logger. */
25
+ export declare const consoleLogger: Logger;
26
+ export declare const jsonLogger: Logger;
27
+ /**
28
+ * Speech-to-Speech (S2S) endpoint configuration.
29
+ *
30
+ * Controls which AssemblyAI real-time WebSocket endpoint to connect to and
31
+ * the audio sample rates for input (microphone → STT) and output (TTS → speaker).
32
+ */
33
+ export type S2SConfig = {
34
+ /** The WebSocket URL of the S2S real-time endpoint. */
35
+ wssUrl: string;
36
+ /** Sample rate in Hz for audio sent to STT (microphone capture). */
37
+ inputSampleRate: number;
38
+ /** Sample rate in Hz for TTS audio received from the server. */
39
+ outputSampleRate: number;
40
+ };
41
+ /** Default S2S endpoint configuration. */
42
+ export declare const DEFAULT_S2S_CONFIG: S2SConfig;
@@ -1,42 +1,126 @@
1
- /** Structured context attached to log messages. */
2
- export type LogContext = Record<string, unknown>;
3
1
  /**
4
- * Structured logger interface. Used by tests to suppress output and by
5
- * consumers to plug in custom logging backends.
6
- *
7
- * @example
8
- * ```ts
9
- * const myLogger: Logger = {
10
- * info: (msg, ctx) => myBackend.log("info", msg, ctx),
11
- * warn: (msg, ctx) => myBackend.log("warn", msg, ctx),
12
- * error: (msg, ctx) => myBackend.log("error", msg, ctx),
13
- * debug: (msg, ctx) => myBackend.log("debug", msg, ctx),
14
- * };
15
- * createServer({ agent, logger: myLogger });
16
- * ```
2
+ * Agent runtime the execution engine for voice agents.
3
+ *
4
+ * {@link createRuntime} builds the single execution engine used by both
5
+ * self-hosted servers and the platform sandbox. It wires up tool execution,
6
+ * lifecycle hooks, and session management.
7
+ */
8
+ import { type ToolSchema } from "../sdk/_internal-types.ts";
9
+ import type { Kv } from "../sdk/kv.ts";
10
+ import type { ClientSink } from "../sdk/protocol.ts";
11
+ import { type ReadyConfig } from "../sdk/protocol.ts";
12
+ import type { AgentDef } from "../sdk/types.ts";
13
+ import type { Logger, S2SConfig } from "./runtime-config.ts";
14
+ import type { CreateS2sWebSocket } from "./s2s.ts";
15
+ import { type Session } from "./session.ts";
16
+ import { type ExecuteTool } from "./tool-executor.ts";
17
+ import { type SessionWebSocket } from "./ws-handler.ts";
18
+ /** Per-session options passed to {@link AgentRuntime.startSession}. */
19
+ export type SessionStartOptions = {
20
+ skipGreeting?: boolean;
21
+ resumeFrom?: string;
22
+ logContext?: Record<string, string>;
23
+ onOpen?: () => void;
24
+ onClose?: () => void;
25
+ /** Called with session ID after session cleanup, for guest state cleanup. */
26
+ onSessionEnd?: (sessionId: string) => void;
27
+ };
28
+ /**
29
+ * Common interface for agent runtimes.
30
+ *
31
+ * Implemented by {@link createRuntime} and the platform sandbox.
32
+ */
33
+ export type AgentRuntime = {
34
+ startSession(ws: SessionWebSocket, opts?: SessionStartOptions): void;
35
+ shutdown(): Promise<void>;
36
+ readonly readyConfig: ReadyConfig;
37
+ };
38
+ /**
39
+ * Configuration for {@link createRuntime}.
40
+ *
41
+ * Configures the agent, environment, KV store, logging, and S2S connection.
42
+ *
43
+ * @public
17
44
  */
18
- export type Logger = {
19
- info(msg: string, ctx?: LogContext): void;
20
- warn(msg: string, ctx?: LogContext): void;
21
- error(msg: string, ctx?: LogContext): void;
22
- debug(msg: string, ctx?: LogContext): void;
45
+ export type RuntimeOptions = {
46
+ agent: AgentDef<any>;
47
+ env: Record<string, string>;
48
+ kv?: Kv | undefined;
49
+ /** Custom WebSocket factory for the S2S connection (useful for testing). */
50
+ createWebSocket?: CreateS2sWebSocket | undefined;
51
+ logger?: Logger | undefined;
52
+ s2sConfig?: S2SConfig | undefined;
53
+ /**
54
+ * Timeout in ms for `session.start()` (S2S connection setup).
55
+ * Defaults to 10 000 (10 s).
56
+ */
57
+ sessionStartTimeoutMs?: number | undefined;
58
+ /**
59
+ * Maximum time in milliseconds to wait for sessions to stop during
60
+ * {@link AgentRuntime.shutdown | shutdown()}. Defaults to `30_000` (30 s).
61
+ */
62
+ shutdownTimeoutMs?: number | undefined;
63
+ /**
64
+ * Override tool execution. When provided, `createRuntime` skips building
65
+ * in-process tool definitions and uses this function instead. Used by the
66
+ * platform sandbox to RPC tool calls to the isolate.
67
+ */
68
+ executeTool?: ExecuteTool | undefined;
69
+ /**
70
+ * Override tool schemas sent to the S2S API. Required when `executeTool`
71
+ * is provided (the host doesn't have the tool definitions to derive schemas).
72
+ */
73
+ toolSchemas?: ToolSchema[] | undefined;
74
+ /** System prompt guidance for builtin tools. Passed through in sandbox mode. */
75
+ toolGuidance?: string[] | undefined;
76
+ /**
77
+ * Pre-resolved builtin tool definitions. When provided alongside `executeTool`
78
+ * and `toolSchemas`, skips calling `resolveAllBuiltins` on the host.
79
+ */
80
+ builtinDefs?: Record<string, import("../sdk/types.ts").ToolDef> | undefined;
81
+ /**
82
+ * Override the fetch implementation used by built-in tools (web_search,
83
+ * visit_webpage, fetch_json). Defaults to `globalThis.fetch`.
84
+ *
85
+ * In platform mode, pass an SSRF-safe fetch to prevent requests to
86
+ * private/internal networks. In self-hosted mode, users may provide
87
+ * their own fetch wrapper.
88
+ */
89
+ fetch?: typeof globalThis.fetch | undefined;
23
90
  };
24
- /** Default console-backed logger. */
25
- export declare const consoleLogger: Logger;
26
- export declare const jsonLogger: Logger;
27
91
  /**
28
- * Speech-to-Speech (S2S) endpoint configuration.
92
+ * The agent runtime returned by {@link createRuntime}.
29
93
  *
30
- * Controls which AssemblyAI real-time WebSocket endpoint to connect to and
31
- * the audio sample rates for input (microphone STT) and output (TTS → speaker).
94
+ * Satisfies {@link AgentRuntime} for use by transport code, and also exposes
95
+ * lower-level helpers (`executeTool`, `hooks`, `toolSchemas`,
96
+ * `createSession`) for testing and advanced usage.
97
+ *
98
+ * @public
32
99
  */
33
- export type S2SConfig = {
34
- /** The WebSocket URL of the S2S real-time endpoint. */
35
- wssUrl: string;
36
- /** Sample rate in Hz for audio sent to STT (microphone capture). */
37
- inputSampleRate: number;
38
- /** Sample rate in Hz for TTS audio received from the server. */
39
- outputSampleRate: number;
100
+ export type Runtime = AgentRuntime & {
101
+ /** Execute a named tool with the given args, returning a JSON result string. */
102
+ executeTool: ExecuteTool;
103
+ /** Tool schemas registered with the S2S API (custom + built-in). */
104
+ toolSchemas: ToolSchema[];
105
+ /** Create a new voice session for a connected client (lower-level than startSession). */
106
+ createSession(opts: {
107
+ id: string;
108
+ agent: string;
109
+ client: ClientSink;
110
+ skipGreeting?: boolean;
111
+ resumeFrom?: string;
112
+ }): Session;
40
113
  };
41
- /** Default S2S endpoint configuration. */
42
- export declare const DEFAULT_S2S_CONFIG: S2SConfig;
114
+ /**
115
+ * Create an agent runtime — the execution engine for a voice agent.
116
+ *
117
+ * Merges built-in and custom tool definitions, builds tool schemas for the
118
+ * S2S API, and wires up lifecycle hooks.
119
+ *
120
+ * @param opts - Runtime configuration. See {@link RuntimeOptions}.
121
+ * @returns A {@link Runtime} with tool execution, hook invocation,
122
+ * schemas, and session management.
123
+ *
124
+ * @public
125
+ */
126
+ export declare function createRuntime(opts: RuntimeOptions): Runtime;
@@ -3,7 +3,8 @@
3
3
  */
4
4
  import type { JSONSchema7 } from "json-schema";
5
5
  import { type Unsubscribe } from "nanoevents";
6
- import type { Logger, S2SConfig } from "./runtime.ts";
6
+ import type { ClientEvent } from "../sdk/protocol.ts";
7
+ import type { Logger, S2SConfig } from "./runtime-config.ts";
7
8
  export type S2sWebSocket = {
8
9
  readonly readyState: number;
9
10
  send(data: string): void;
@@ -24,6 +25,14 @@ export type CreateS2sWebSocket = (url: string, opts: {
24
25
  headers: Record<string, string>;
25
26
  }) => S2sWebSocket;
26
27
  export declare const defaultCreateS2sWebSocket: CreateS2sWebSocket;
28
+ /**
29
+ * A ClientEvent extended with optional internal metadata for S2S-specific
30
+ * fields that don't appear on the wire protocol (e.g. `interrupted` on
31
+ * `agent_transcript`, which affects conversation history but not the client).
32
+ */
33
+ export type S2sEvent = ClientEvent & {
34
+ _interrupted?: boolean;
35
+ };
27
36
  export type S2sSessionConfig = {
28
37
  systemPrompt: string;
29
38
  tools: S2sToolSchema[];
@@ -35,53 +44,20 @@ export type S2sToolSchema = {
35
44
  description: string;
36
45
  parameters: JSONSchema7;
37
46
  };
38
- export type S2sToolCall = {
39
- callId: string;
40
- name: string;
41
- args: Record<string, unknown>;
42
- };
43
47
  export type S2sEvents = {
44
48
  ready: (detail: {
45
49
  sessionId: string;
46
50
  }) => void;
47
- sessionUpdated: (detail: Record<string, unknown>) => void;
48
- sessionExpired: (detail: {
49
- code: string;
50
- message: string;
51
- }) => void;
52
- speechStarted: () => void;
53
- speechStopped: () => void;
54
- userTranscriptDelta: (detail: {
55
- text: string;
56
- }) => void;
57
- userTranscript: (detail: {
58
- itemId: string;
59
- text: string;
60
- }) => void;
61
51
  replyStarted: (detail: {
62
52
  replyId: string;
63
53
  }) => void;
64
- agentTranscriptDelta: (detail: {
65
- text: string;
66
- }) => void;
67
- agentTranscript: (detail: {
68
- text: string;
69
- replyId: string;
70
- itemId: string;
71
- interrupted: boolean;
72
- }) => void;
73
- toolCall: (detail: S2sToolCall) => void;
74
- replyDone: (detail: {
75
- status?: string;
76
- }) => void;
54
+ sessionExpired: () => void;
55
+ event: (event: S2sEvent) => void;
77
56
  audio: (detail: {
78
57
  audio: Uint8Array;
79
58
  }) => void;
80
- error: (detail: {
81
- code: string;
82
- message: string;
83
- }) => void;
84
- close: () => void;
59
+ error: (err: Error) => void;
60
+ close: (code: number, reason: string) => void;
85
61
  };
86
62
  export type S2sHandle = {
87
63
  on<K extends keyof S2sEvents>(event: K, cb: S2sEvents[K]): Unsubscribe;
@@ -1,18 +1,21 @@
1
1
  /**
2
- * Self-hostable agent server.
2
+ * Agent HTTP+WebSocket server.
3
3
  *
4
4
  * {@link createServer} wraps a {@link Runtime} with an HTTP + WebSocket
5
5
  * server using only `node:http` and `ws` (no framework dependencies).
6
+ *
7
+ * **Internal module** — used by `aai-cli` dev server. Not a public API.
8
+ * Import via `aai/host`.
6
9
  */
7
- import type { Kv } from "../isolate/kv.ts";
8
- import type { Runtime } from "./direct-executor.ts";
9
- import type { Logger } from "./runtime.ts";
10
- export { createRuntime, type Runtime, type RuntimeOptions } from "./direct-executor.ts";
10
+ import type { Kv } from "../sdk/kv.ts";
11
+ import type { Runtime } from "./runtime.ts";
12
+ import type { Logger } from "./runtime-config.ts";
13
+ export { createRuntime, type Runtime, type RuntimeOptions } from "./runtime.ts";
11
14
  /**
12
15
  * Configuration for {@link createServer}.
13
- * @public
16
+ * @internal
14
17
  */
15
- export type ServerOptions = {
18
+ type ServerOptions = {
16
19
  runtime: Runtime;
17
20
  name?: string;
18
21
  kv?: Kv;
@@ -22,11 +25,16 @@ export type ServerOptions = {
22
25
  };
23
26
  /**
24
27
  * Handle returned by {@link createServer}.
25
- * @public
28
+ * @internal
26
29
  */
27
30
  export type AgentServer = {
28
31
  listen(port?: number): Promise<void>;
29
32
  close(): Promise<void>;
30
33
  port: number | undefined;
31
34
  };
35
+ /**
36
+ * Create an HTTP + WebSocket server for an agent.
37
+ *
38
+ * @internal Used by aai-cli dev server.
39
+ */
32
40
  export declare function createServer(options: ServerOptions): AgentServer;
@@ -0,0 +1,55 @@
1
+ /** Session context builder — extracted from session.ts. */
2
+ import type { AgentConfig, ExecuteTool } from "../sdk/_internal-types.ts";
3
+ import type { ClientSink } from "../sdk/protocol.ts";
4
+ import type { Message } from "../sdk/types.ts";
5
+ import type { Logger } from "./runtime-config.ts";
6
+ import type { S2sHandle } from "./s2s.ts";
7
+ type PendingTool = {
8
+ callId: string;
9
+ result: string;
10
+ };
11
+ /** Per-reply mutable state — reset on beginReply/cancelReply. */
12
+ export type ReplyState = {
13
+ pendingTools: PendingTool[];
14
+ toolCallCount: number;
15
+ currentReplyId: string | null;
16
+ };
17
+ /** Immutable dependencies injected at session creation. */
18
+ export type SessionDeps = {
19
+ readonly id: string;
20
+ readonly agent: string;
21
+ readonly client: ClientSink;
22
+ readonly agentConfig: AgentConfig;
23
+ readonly executeTool: ExecuteTool;
24
+ readonly log: Logger;
25
+ readonly maxHistory: number;
26
+ };
27
+ /**
28
+ * Session context threaded through event handlers.
29
+ *
30
+ * Split into three layers:
31
+ * - {@link SessionDeps} — immutable dependencies (set once)
32
+ * - {@link ReplyState} via `reply` — per-reply mutable state (reset on beginReply/cancelReply)
33
+ * - Remaining fields — connection, conversation, and lifecycle methods
34
+ */
35
+ export type S2sSessionCtx = SessionDeps & {
36
+ s2s: S2sHandle | null;
37
+ reply: ReplyState;
38
+ turnPromise: Promise<void> | null;
39
+ conversationMessages: Message[];
40
+ consumeToolCallStep(name: string, replyId: string | null): string | null;
41
+ pushMessages(...msgs: Message[]): void;
42
+ beginReply(replyId: string): void;
43
+ cancelReply(): void;
44
+ chainTurn(p: Promise<void>): void;
45
+ };
46
+ export declare function buildCtx(opts: {
47
+ id: string;
48
+ agent: string;
49
+ client: ClientSink;
50
+ agentConfig: AgentConfig;
51
+ executeTool: ExecuteTool;
52
+ log: Logger;
53
+ maxHistory?: number | undefined;
54
+ }): S2sSessionCtx;
55
+ export {};
@@ -1,78 +1,15 @@
1
1
  /** S2S session — relays audio between client and AssemblyAI S2S API. */
2
- import type { AgentConfig, ExecuteTool, ToolSchema } from "../isolate/_internal-types.ts";
3
- import type { AgentHookMap, AgentHooks } from "../isolate/hooks.ts";
4
- import type { ClientSink } from "../isolate/protocol.ts";
5
- import type { Message } from "../isolate/types.ts";
6
- import type { Logger, S2SConfig } from "./runtime.ts";
7
- import { type CreateS2sWebSocket, connectS2s, type S2sHandle } from "./s2s.ts";
8
- export type { S2sHandle } from "./s2s.ts";
9
- type PendingTool = {
10
- callId: string;
11
- result: string;
12
- };
13
- /** Per-reply mutable state — reset on beginReply/cancelReply. */
14
- export type ReplyState = {
15
- pendingTools: PendingTool[];
16
- toolCallCount: number;
17
- currentReplyId: string | null;
18
- };
19
- /** Immutable dependencies injected at session creation. */
20
- export type SessionDeps = {
21
- readonly id: string;
22
- readonly agent: string;
23
- readonly client: ClientSink;
24
- readonly agentConfig: AgentConfig;
25
- readonly executeTool: ExecuteTool;
26
- readonly hooks: AgentHooks | undefined;
27
- readonly log: Logger;
28
- readonly maxHistory: number;
29
- };
30
- /**
31
- * Session context threaded through event handlers.
32
- *
33
- * Split into three layers:
34
- * - {@link SessionDeps} — immutable dependencies (set once)
35
- * - {@link ReplyState} via `reply` — per-reply mutable state (reset on beginReply/cancelReply)
36
- * - Remaining fields — connection, conversation, and lifecycle methods
37
- */
38
- export type S2sSessionCtx = SessionDeps & {
39
- s2s: S2sHandle | null;
40
- reply: ReplyState;
41
- turnPromise: Promise<void> | null;
42
- conversationMessages: Message[];
43
- resolveTurnConfig(): Promise<{
44
- maxSteps?: number;
45
- } | null>;
46
- consumeToolCallStep(turnConfig: {
47
- maxSteps?: number;
48
- } | null, name: string, replyId: string | null): string | null;
49
- fireHook(name: keyof AgentHookMap, ...args: unknown[]): void;
50
- drainHooks(): Promise<void>;
51
- pushMessages(...msgs: Message[]): void;
52
- beginReply(replyId: string): void;
53
- cancelReply(): void;
54
- chainTurn(p: Promise<void>): void;
55
- };
56
- export declare function buildCtx(opts: {
57
- id: string;
58
- agent: string;
59
- client: ClientSink;
60
- agentConfig: AgentConfig;
61
- executeTool: ExecuteTool;
62
- hooks: AgentHooks | undefined;
63
- log: Logger;
64
- maxHistory?: number | undefined;
65
- }): S2sSessionCtx;
66
- export type { AgentHookMap, AgentHooks } from "../isolate/hooks.ts";
67
- export { callResolveTurnConfig, createAgentHooks } from "../isolate/hooks.ts";
68
- export { buildSystemPrompt } from "../isolate/system-prompt.ts";
2
+ import type { AgentConfig, ExecuteTool, ToolSchema } from "../sdk/_internal-types.ts";
3
+ import type { ClientSink } from "../sdk/protocol.ts";
4
+ import type { Logger, S2SConfig } from "./runtime-config.ts";
5
+ import { type CreateS2sWebSocket, connectS2s } from "./s2s.ts";
69
6
  /**
70
7
  * A voice session managing the Speech-to-Speech connection for one client.
71
8
  *
72
9
  * Created by {@link createS2sSession}. Each session owns a single S2S WebSocket
73
10
  * connection and relays audio between the browser client and AssemblyAI.
74
11
  *
75
- * @internal Exported for use by `ws-handler.ts`, `server.ts`, and `direct-executor.ts`.
12
+ * @internal Exported for use by `ws-handler.ts`, `server.ts`, and `runtime.ts`.
76
13
  */
77
14
  export type Session = {
78
15
  start(): Promise<void>;
@@ -87,22 +24,35 @@ export type Session = {
87
24
  }[]): void;
88
25
  waitForTurn(): Promise<void>;
89
26
  };
90
- /** Configuration options for creating a new session. */
27
+ /** Configuration options for creating a new S2S voice session. */
91
28
  export type S2sSessionOptions = {
29
+ /** Unique session identifier (used for KV scoping and logging). */
92
30
  id: string;
31
+ /** Agent slug — identifies which deployed agent this session belongs to. */
93
32
  agent: string;
33
+ /** Sink for pushing events and audio to the connected browser client. */
94
34
  client: ClientSink;
35
+ /** Serializable agent config (name, system prompt, greeting, maxSteps, etc.). */
95
36
  agentConfig: AgentConfig;
37
+ /** JSON Schema definitions for the agent's custom tools. */
96
38
  toolSchemas: readonly ToolSchema[];
39
+ /** Optional natural-language guidance appended to the system prompt for tool usage. */
97
40
  toolGuidance?: readonly string[];
41
+ /** AssemblyAI API key — stays host-side, never forwarded to the guest sandbox. */
98
42
  apiKey: string;
43
+ /** S2S connection config (sample rates, model selection). */
99
44
  s2sConfig: S2SConfig;
45
+ /** Function to invoke tools by name (wired to direct-executor or sandbox RPC). */
100
46
  executeTool: ExecuteTool;
47
+ /** Override WebSocket constructor for testing. */
101
48
  createWebSocket?: CreateS2sWebSocket;
49
+ /** Agent environment variables (secrets). Forwarded to tool context. */
102
50
  env?: Record<string, string | undefined>;
103
- hooks?: AgentHooks;
51
+ /** Skip the initial greeting audio on connect (used for session resume). */
104
52
  skipGreeting?: boolean;
53
+ /** Logger instance. Defaults to `consoleLogger`. */
105
54
  logger?: Logger;
55
+ /** Max conversation messages to retain. Defaults to DEFAULT_MAX_HISTORY (200). */
106
56
  maxHistory?: number;
107
57
  };
108
58
  /** @internal Not part of the public API. Exposed for testing only. */
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Tool execution — validates arguments and invokes tool handlers.
3
+ *
4
+ * {@link executeToolCall} is the single entry point used by both the
5
+ * direct (self-hosted) runtime and the platform sandbox sidecar.
6
+ */
7
+ import type { Kv } from "../sdk/kv.ts";
8
+ import type { Message, ToolDef } from "../sdk/types.ts";
9
+ import type { Logger } from "./runtime-config.ts";
10
+ export type { ExecuteTool } from "../sdk/_internal-types.ts";
11
+ export type ExecuteToolCallOptions = {
12
+ tool: ToolDef;
13
+ env: Readonly<Record<string, string>>;
14
+ state?: Record<string, unknown>;
15
+ sessionId?: string | undefined;
16
+ kv?: Kv | undefined;
17
+ messages?: readonly Message[] | undefined;
18
+ logger?: Logger | undefined;
19
+ };
20
+ export declare function executeToolCall(name: string, args: Readonly<Record<string, unknown>>, options: ExecuteToolCallOptions): Promise<string>;
@@ -4,7 +4,7 @@
4
4
  * Works with any unstorage driver (memory, fs, S3/R2, etc.).
5
5
  */
6
6
  import { type Storage } from "unstorage";
7
- import type { Kv } from "../isolate/kv.ts";
7
+ import type { Kv } from "../sdk/kv.ts";
8
8
  /**
9
9
  * Options for creating an unstorage-backed KV store.
10
10
  */
@@ -3,8 +3,8 @@
3
3
  *
4
4
  * Audio validation is handled at the host transport layer (see server.ts).
5
5
  */
6
- import type { ClientSink, ReadyConfig } from "../isolate/protocol.ts";
7
- import type { Logger } from "./runtime.ts";
6
+ import type { ClientSink, ReadyConfig } from "../sdk/protocol.ts";
7
+ import type { Logger } from "./runtime-config.ts";
8
8
  import type { Session } from "./session.ts";
9
9
  /**
10
10
  * Minimal WebSocket interface accepted by {@link wireSessionSocket}.
@@ -36,6 +36,8 @@ export type WsSessionOptions = {
36
36
  onOpen?: () => void;
37
37
  /** Callback invoked when the WebSocket connection closes. */
38
38
  onClose?: () => void;
39
+ /** Callback invoked with the session ID after session cleanup. */
40
+ onSessionEnd?: (sessionId: string) => void;
39
41
  /** Logger instance. Defaults to console. */
40
42
  logger?: Logger;
41
43
  /** Timeout in ms for session.start(). Defaults to 10 000 (10s). */
package/dist/index.d.ts CHANGED
@@ -1,23 +1,12 @@
1
1
  /**
2
- * AAI SDK build voice agents powered by STT, LLM, and TTS.
2
+ * aaishared fundamentals with no Node.js dependencies.
3
3
  *
4
- * @example
5
- * ```ts
6
- * import { defineAgent } from "aai";
7
- * import { z } from "zod";
8
- *
9
- * export default defineAgent({
10
- * name: "my-agent",
11
- * instructions: "You are a helpful voice assistant.",
12
- * tools: {
13
- * greet: {
14
- * description: "Greet the user by name",
15
- * parameters: z.object({ name: z.string() }),
16
- * execute: ({ name }) => `Hello, ${name}!`,
17
- * },
18
- * },
19
- * });
20
- * ```
4
+ * Types, KV interface, utils, and constants used across
5
+ * aai-cli, aai-server, and aai-ui.
21
6
  */
22
- export type { Kv, KvEntry, KvListOptions } from "./isolate/kv.ts";
23
- export { type AgentDef, type AgentOptions, type BuiltinTool, defineAgent, defineTool, defineToolFactory, type HookContext, type Message, type ToolChoice, type ToolContext, type ToolDef, type ToolResultMap, tool, } from "./isolate/types.ts";
7
+ export * from "./sdk/constants.ts";
8
+ export * from "./sdk/define.ts";
9
+ export * from "./sdk/kv.ts";
10
+ export * from "./sdk/types.ts";
11
+ export * from "./sdk/utils.ts";
12
+ export * from "./sdk/ws-upgrade.ts";
package/dist/index.js CHANGED
@@ -1,2 +1,63 @@
1
- import { defineAgent, defineTool, defineToolFactory } from "./isolate/types.js";
2
- export { defineAgent, defineTool, defineTool as tool, defineToolFactory };
1
+ import { _ as WS_OPEN, a as DEFAULT_SHUTDOWN_TIMEOUT_MS, c as FETCH_TIMEOUT_MS, d as MAX_PAGE_CHARS, f as MAX_TOOL_RESULT_CHARS, g as TOOL_EXECUTION_TIMEOUT_MS, h as RUN_CODE_TIMEOUT_MS, i as DEFAULT_SESSION_START_TIMEOUT_MS, l as MAX_HTML_BYTES, m as MAX_WS_PAYLOAD_BYTES, n as DEFAULT_IDLE_TIMEOUT_MS, o as DEFAULT_STT_SAMPLE_RATE, p as MAX_VALUE_SIZE, r as DEFAULT_MAX_HISTORY, s as DEFAULT_TTS_SAMPLE_RATE, t as AGENT_CSP, u as MAX_MESSAGE_BUFFER_SIZE } from "./constants-VTFoymJ-.js";
2
+ import { i as ToolChoiceSchema, n as DEFAULT_GREETING, r as DEFAULT_SYSTEM_PROMPT, t as BuiltinToolSchema } from "./types-Cfx_4QDK.js";
3
+ import { i as toolError, n as errorDetail, r as errorMessage, t as parseWsUpgradeParams } from "./ws-upgrade-BeOQ7fXL.js";
4
+ //#region sdk/define.ts
5
+ /**
6
+ * Define a tool with typed parameters and execute function.
7
+ *
8
+ * Identity function for type inference — returns the input unchanged.
9
+ * Follows the Vercel AI SDK `tool()` pattern.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * import { tool } from "@alexkroman1/aai";
14
+ * import { z } from "zod";
15
+ *
16
+ * const greet = tool({
17
+ * description: "Greet someone by name",
18
+ * parameters: z.object({ name: z.string() }),
19
+ * execute: ({ name }) => `Hello, ${name}!`,
20
+ * });
21
+ * ```
22
+ *
23
+ * @public
24
+ */
25
+ function tool(def) {
26
+ return def;
27
+ }
28
+ /**
29
+ * Define an agent with tools, system prompt, and configuration.
30
+ *
31
+ * Applies sensible defaults for omitted fields. Export as the default
32
+ * export of your `agent.ts` file.
33
+ *
34
+ * @example
35
+ * ```ts
36
+ * import { agent, tool } from "@alexkroman1/aai";
37
+ * import { z } from "zod";
38
+ *
39
+ * const myTool = tool({
40
+ * description: "Echo a message",
41
+ * parameters: z.object({ message: z.string() }),
42
+ * execute: ({ message }) => message,
43
+ * });
44
+ *
45
+ * export default agent({
46
+ * name: "Echo Agent",
47
+ * tools: { echo: myTool },
48
+ * });
49
+ * ```
50
+ *
51
+ * @public
52
+ */
53
+ function agent(def) {
54
+ return {
55
+ systemPrompt: DEFAULT_SYSTEM_PROMPT,
56
+ greeting: DEFAULT_GREETING,
57
+ maxSteps: 5,
58
+ tools: {},
59
+ ...def
60
+ };
61
+ }
62
+ //#endregion
63
+ export { AGENT_CSP, BuiltinToolSchema, DEFAULT_GREETING, DEFAULT_IDLE_TIMEOUT_MS, DEFAULT_MAX_HISTORY, DEFAULT_SESSION_START_TIMEOUT_MS, DEFAULT_SHUTDOWN_TIMEOUT_MS, DEFAULT_STT_SAMPLE_RATE, DEFAULT_SYSTEM_PROMPT, DEFAULT_TTS_SAMPLE_RATE, FETCH_TIMEOUT_MS, MAX_HTML_BYTES, MAX_MESSAGE_BUFFER_SIZE, MAX_PAGE_CHARS, MAX_TOOL_RESULT_CHARS, MAX_VALUE_SIZE, MAX_WS_PAYLOAD_BYTES, RUN_CODE_TIMEOUT_MS, TOOL_EXECUTION_TIMEOUT_MS, ToolChoiceSchema, WS_OPEN, agent, errorDetail, errorMessage, parseWsUpgradeParams, tool, toolError };