@alexkroman1/aai 0.9.3 → 0.10.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 (57) hide show
  1. package/dist/_internal-types.d.ts +49 -22
  2. package/dist/_internal-types.js +43 -1
  3. package/dist/_mock-ws.d.ts +1 -2
  4. package/dist/_run-code.d.ts +31 -0
  5. package/dist/_session-ctx.d.ts +73 -0
  6. package/dist/_session-otel.d.ts +43 -0
  7. package/dist/_session-persist.d.ts +30 -0
  8. package/dist/_ssrf.d.ts +30 -0
  9. package/dist/_ssrf.js +123 -0
  10. package/dist/_utils.d.ts +25 -0
  11. package/dist/_utils.js +54 -1
  12. package/dist/builtin-tools.d.ts +5 -34
  13. package/dist/direct-executor-Ca0wt5H0.js +572 -0
  14. package/dist/direct-executor.d.ts +34 -5
  15. package/dist/index.d.ts +2 -1
  16. package/dist/index.js +2 -2
  17. package/dist/kv.d.ts +30 -38
  18. package/dist/kv.js +19 -86
  19. package/dist/matchers.d.ts +20 -0
  20. package/dist/matchers.js +41 -0
  21. package/dist/memory-tools.d.ts +39 -0
  22. package/dist/middleware-core.d.ts +47 -0
  23. package/dist/middleware-core.js +107 -0
  24. package/dist/middleware.d.ts +37 -0
  25. package/dist/protocol.d.ts +44 -24
  26. package/dist/protocol.js +34 -14
  27. package/dist/runtime.d.ts +26 -2
  28. package/dist/runtime.js +44 -7
  29. package/dist/s2s.d.ts +19 -29
  30. package/dist/s2s.js +117 -87
  31. package/dist/server.d.ts +31 -3
  32. package/dist/server.js +102 -28
  33. package/dist/session-BkN9u0ni.js +683 -0
  34. package/dist/session.d.ts +55 -28
  35. package/dist/session.js +2 -312
  36. package/dist/sqlite-kv.d.ts +34 -0
  37. package/dist/sqlite-kv.js +133 -0
  38. package/dist/sqlite-vector.d.ts +58 -0
  39. package/dist/sqlite-vector.js +149 -0
  40. package/dist/system-prompt.d.ts +21 -0
  41. package/dist/telemetry.d.ts +49 -0
  42. package/dist/telemetry.js +95 -0
  43. package/dist/testing-MRl3SXsI.js +519 -0
  44. package/dist/testing.d.ts +299 -0
  45. package/dist/testing.js +2 -0
  46. package/dist/types.d.ts +324 -39
  47. package/dist/types.js +62 -9
  48. package/dist/vector.d.ts +18 -22
  49. package/dist/vector.js +41 -48
  50. package/dist/worker-entry.d.ts +11 -3
  51. package/dist/worker-entry.js +19 -8
  52. package/dist/ws-handler.d.ts +7 -3
  53. package/dist/ws-handler.js +64 -12
  54. package/package.json +55 -8
  55. package/dist/_mock-ws.js +0 -158
  56. package/dist/builtin-tools.js +0 -270
  57. package/dist/direct-executor.js +0 -125
@@ -0,0 +1,572 @@
1
+ import { defineTool } from "./types.js";
2
+ import { EMPTY_PARAMS, agentToolsToSchemas, toAgentConfig } from "./_internal-types.js";
3
+ import { ssrfSafeFetch } from "./_ssrf.js";
4
+ import { createSessionStateMap, errorMessage, isReadOnlyFsOp, toolError } from "./_utils.js";
5
+ import { runAfterToolCallMiddleware, runAfterTurnMiddleware, runBeforeTurnMiddleware, runInputFilters, runOutputFilters, runToolCallInterceptors } from "./middleware-core.js";
6
+ import { DEFAULT_S2S_CONFIG, consoleLogger } from "./runtime.js";
7
+ import { n as createS2sSession } from "./session-BkN9u0ni.js";
8
+ import { createSqliteKv } from "./sqlite-kv.js";
9
+ import { createSqliteVectorStore } from "./sqlite-vector.js";
10
+ import { executeToolCall } from "./worker-entry.js";
11
+ import { z } from "zod";
12
+ import { mkdirSync } from "node:fs";
13
+ //#region _run-code.ts
14
+ /**
15
+ * run_code built-in tool — executes user JavaScript in a fresh secure-exec
16
+ * V8 isolate with no network, filesystem writes, or env access.
17
+ */
18
+ const runCodeParams = z.object({ code: z.string().describe("JavaScript code to execute. Use console.log() for output.") });
19
+ /** Timeout for sandboxed code execution. */
20
+ const RUN_CODE_TIMEOUT = 5e3;
21
+ /** Memory limit for run_code isolates (MB). */
22
+ const RUN_CODE_MEMORY_MB = 32;
23
+ /**
24
+ * Execute JavaScript code inside a fresh secure-exec V8 isolate.
25
+ *
26
+ * Each invocation spins up a disposable isolate with:
27
+ * - No filesystem writes
28
+ * - No network access
29
+ * - No child process spawning
30
+ * - No environment variable access
31
+ * - 32 MB memory limit
32
+ * - 5 second execution timeout
33
+ *
34
+ * The isolate is disposed immediately after execution, so no state
35
+ * leaks between invocations or across sessions.
36
+ */
37
+ function createRunCode() {
38
+ return {
39
+ description: "Execute JavaScript code in a secure sandbox and return the output. Use this for calculations, data transformations, string manipulation, or any task that benefits from running code. Output is captured from console.log(). No network or filesystem access.",
40
+ parameters: runCodeParams,
41
+ async execute(args) {
42
+ return executeInIsolate(args.code);
43
+ }
44
+ };
45
+ }
46
+ /** Lazily import secure-exec to avoid top-level side effects. */
47
+ let _secureExecPromise;
48
+ function getSecureExec() {
49
+ _secureExecPromise ??= import("secure-exec");
50
+ return _secureExecPromise;
51
+ }
52
+ const RUN_CODE_HARNESS = `
53
+ import { readFileSync } from "node:fs";
54
+
55
+ const __output = [];
56
+ const __capture = (...args) => __output.push(args.map(String).join(" "));
57
+ const __console = {
58
+ log: __capture, info: __capture, warn: __capture,
59
+ error: __capture, debug: __capture,
60
+ };
61
+ try {
62
+ const __userCode = readFileSync("/app/user-code.js", "utf8");
63
+ const __AsyncFn = Object.getPrototypeOf(async function(){}).constructor;
64
+ const __fn = new __AsyncFn("console", __userCode);
65
+ await __fn(__console);
66
+ const result = __output.join("\\n").trim();
67
+ process.stdout.write(JSON.stringify({ ok: true, result: result || "Code ran successfully (no output)" }));
68
+ } catch (err) {
69
+ process.stdout.write(JSON.stringify({ ok: false, error: String(err?.message ?? err) }));
70
+ }
71
+ `;
72
+ const IsolateOutputSchema = z.object({
73
+ ok: z.boolean(),
74
+ result: z.string().optional(),
75
+ error: z.string().optional()
76
+ });
77
+ /** Parse stdout from the run_code harness into a result or error. */
78
+ function parseIsolateOutput(stdout, stderr) {
79
+ if (!stdout) {
80
+ if (stderr) return { error: stderr.trim() };
81
+ return { error: "Code execution timed out" };
82
+ }
83
+ try {
84
+ const parsed = IsolateOutputSchema.parse(JSON.parse(stdout));
85
+ if (parsed.ok) return parsed.result ?? "Code ran successfully (no output)";
86
+ return { error: parsed.error ?? "Unknown error" };
87
+ } catch {
88
+ return stdout.trim() || "Code ran successfully (no output)";
89
+ }
90
+ }
91
+ /**
92
+ * Exported for testing — execute user code in a fresh secure-exec V8 isolate.
93
+ */
94
+ async function executeInIsolate(code) {
95
+ const { createInMemoryFileSystem, createNodeDriver, createNodeRuntimeDriverFactory, NodeRuntime } = await getSecureExec();
96
+ const fs = createInMemoryFileSystem();
97
+ await fs.writeFile("/app/harness.js", RUN_CODE_HARNESS);
98
+ await fs.writeFile("/app/user-code.js", code);
99
+ const stdoutChunks = [];
100
+ const stderrChunks = [];
101
+ let resolveOutput = null;
102
+ const outputReady = new Promise((r) => {
103
+ resolveOutput = r;
104
+ });
105
+ const runtime = new NodeRuntime({
106
+ systemDriver: createNodeDriver({
107
+ filesystem: fs,
108
+ permissions: {
109
+ fs: (req) => isReadOnlyFsOp(req.op) ? { allow: true } : {
110
+ allow: false,
111
+ reason: "Filesystem is read-only"
112
+ },
113
+ network: () => ({
114
+ allow: false,
115
+ reason: "Network access is disabled in run_code"
116
+ }),
117
+ childProcess: () => ({
118
+ allow: false,
119
+ reason: "Subprocess spawning is disabled"
120
+ }),
121
+ env: () => ({
122
+ allow: false,
123
+ reason: "Env access is disabled in run_code"
124
+ })
125
+ }
126
+ }),
127
+ runtimeDriverFactory: createNodeRuntimeDriverFactory(),
128
+ memoryLimit: RUN_CODE_MEMORY_MB,
129
+ onStdio(event) {
130
+ if (event.channel === "stdout") stdoutChunks.push(event.message);
131
+ if (event.channel === "stderr") stderrChunks.push(event.message);
132
+ resolveOutput?.();
133
+ }
134
+ });
135
+ const execPromise = runtime.exec("import \"/app/harness.js\";", { cwd: "/app" });
136
+ try {
137
+ await Promise.race([outputReady, new Promise((r) => setTimeout(r, RUN_CODE_TIMEOUT))]);
138
+ await Promise.race([execPromise.catch(() => {}), new Promise((r) => setTimeout(r, 200))]);
139
+ return parseIsolateOutput(stdoutChunks.join(""), stderrChunks.join(""));
140
+ } catch (err) {
141
+ return { error: errorMessage(err) };
142
+ } finally {
143
+ runtime.dispose();
144
+ }
145
+ }
146
+ //#endregion
147
+ //#region memory-tools.ts
148
+ /**
149
+ * KV-backed memory tools for agent persistent state.
150
+ */
151
+ /**
152
+ * Returns a standard set of KV-backed memory tools: `save_memory`,
153
+ * `recall_memory`, `list_memories`, and `forget_memory`.
154
+ *
155
+ * Spread the result into your agent's `tools` record.
156
+ *
157
+ * @example
158
+ * ```ts
159
+ * import { defineAgent, memoryTools } from "aai";
160
+ *
161
+ * export default defineAgent({
162
+ * name: "My Agent",
163
+ * tools: { ...memoryTools() },
164
+ * });
165
+ * ```
166
+ *
167
+ * @returns A record with four tool definitions: `save_memory`, `recall_memory`,
168
+ * `list_memories`, and `forget_memory`.
169
+ * @public
170
+ */
171
+ function memoryTools() {
172
+ return {
173
+ save_memory: defineTool({
174
+ description: "Save a piece of information to persistent memory. Use a descriptive key like 'user:name' or 'project:status'.",
175
+ parameters: z.object({
176
+ key: z.string().describe("A descriptive key for this memory (e.g. 'user:name', 'preference:color')"),
177
+ value: z.string().describe("The information to remember")
178
+ }),
179
+ execute: async ({ key, value }, ctx) => {
180
+ await ctx.kv.set(key, value);
181
+ return { saved: key };
182
+ }
183
+ }),
184
+ recall_memory: defineTool({
185
+ description: "Retrieve a previously saved memory by its key.",
186
+ parameters: z.object({ key: z.string().describe("The key to look up") }),
187
+ execute: async ({ key }, ctx) => {
188
+ const value = await ctx.kv.get(key);
189
+ if (value === null) return {
190
+ found: false,
191
+ key
192
+ };
193
+ return {
194
+ found: true,
195
+ key,
196
+ value
197
+ };
198
+ }
199
+ }),
200
+ list_memories: defineTool({
201
+ description: "List all saved memory keys, optionally filtered by a prefix (e.g. 'user:').",
202
+ parameters: z.object({ prefix: z.string().describe("Prefix to filter keys (e.g. 'user:'). Use empty string for all.").optional() }),
203
+ execute: async ({ prefix }, ctx) => {
204
+ const entries = await ctx.kv.list(prefix ?? "");
205
+ return {
206
+ count: entries.length,
207
+ keys: entries.map((e) => e.key)
208
+ };
209
+ }
210
+ }),
211
+ forget_memory: defineTool({
212
+ description: "Delete a previously saved memory by its key.",
213
+ parameters: z.object({ key: z.string().describe("The key to delete") }),
214
+ execute: async ({ key }, ctx) => {
215
+ await ctx.kv.delete(key);
216
+ return { deleted: key };
217
+ }
218
+ })
219
+ };
220
+ }
221
+ //#endregion
222
+ //#region builtin-tools.ts
223
+ /**
224
+ * Built-in tool definitions for the AAI agent SDK.
225
+ *
226
+ * In self-hosted mode, these run in-process alongside custom tools.
227
+ * In platform mode, they run on the host process outside the sandbox.
228
+ * Network requests go through the host's fetch proxy (with SSRF protection).
229
+ */
230
+ /** Per-fetch timeout for network tools — tighter than the overall tool timeout. */
231
+ const FETCH_TIMEOUT_MS = 15e3;
232
+ const fetchSignal = () => AbortSignal.timeout(FETCH_TIMEOUT_MS);
233
+ /** Lazily import html-to-text to avoid loading 824KB dep tree at startup. */
234
+ let _htmlToTextPromise;
235
+ function getHtmlToText() {
236
+ _htmlToTextPromise ??= import("html-to-text");
237
+ return _htmlToTextPromise;
238
+ }
239
+ async function htmlToText(html) {
240
+ const { convert } = await getHtmlToText();
241
+ return convert(html, { wordwrap: false });
242
+ }
243
+ const webSearchParams = z.object({
244
+ query: z.string().describe("The search query"),
245
+ max_results: z.number().describe("Maximum number of results to return (default 5)").optional()
246
+ });
247
+ const BRAVE_SEARCH_URL = "https://api.search.brave.com/res/v1/web/search";
248
+ const BraveSearchResponseSchema = z.object({ web: z.object({ results: z.array(z.object({
249
+ title: z.string(),
250
+ url: z.string(),
251
+ description: z.string()
252
+ })) }).optional() });
253
+ function createWebSearch(fetchFn = globalThis.fetch) {
254
+ return {
255
+ description: "Search the web for current information, facts, news, or answers to questions. Returns a list of results with title, URL, and description. Use this when the user asks about something you don't know, need up-to-date information, or want to verify facts.",
256
+ parameters: webSearchParams,
257
+ async execute(args, ctx) {
258
+ const { query, max_results: maxResults = 5 } = args;
259
+ const apiKey = ctx.env.BRAVE_API_KEY ?? "";
260
+ if (!apiKey) return { error: "BRAVE_API_KEY is not set — web search unavailable" };
261
+ const resp = await fetchFn(`${BRAVE_SEARCH_URL}?${new URLSearchParams({
262
+ q: query,
263
+ count: String(maxResults),
264
+ text_decorations: "false"
265
+ })}`, {
266
+ headers: { "X-Subscription-Token": apiKey },
267
+ signal: fetchSignal()
268
+ });
269
+ if (!resp.ok) return { error: `Search request failed: ${resp.status} ${resp.statusText}` };
270
+ const raw = await resp.json();
271
+ const data = BraveSearchResponseSchema.safeParse(raw);
272
+ if (!data.success) return { error: "Unexpected search response format" };
273
+ return (data.data.web?.results ?? []).slice(0, maxResults).map((r) => ({
274
+ title: r.title,
275
+ url: r.url,
276
+ description: r.description
277
+ }));
278
+ }
279
+ };
280
+ }
281
+ const MAX_PAGE_CHARS = 1e4;
282
+ const MAX_HTML_BYTES = 2e5;
283
+ const visitWebpageParams = z.object({ url: z.string().describe("The full URL to fetch (e.g., 'https://example.com/page')") });
284
+ function createVisitWebpage(fetchFn = globalThis.fetch) {
285
+ return {
286
+ description: "Fetch a webpage and return its content as clean text. Use this to read the full content of a URL found via web_search, or any link the user shares. Good for reading articles, documentation, blog posts, or product pages.",
287
+ parameters: visitWebpageParams,
288
+ async execute(args, _ctx) {
289
+ const { url } = args;
290
+ const resp = await ssrfSafeFetch(url, {
291
+ headers: {
292
+ "User-Agent": "Mozilla/5.0 (compatible; VoiceAgent/1.0; +https://github.com/AssemblyAI/aai)",
293
+ Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
294
+ },
295
+ signal: fetchSignal()
296
+ }, fetchFn);
297
+ if (!resp.ok) return {
298
+ error: `Failed to fetch: ${resp.status} ${resp.statusText}`,
299
+ url
300
+ };
301
+ const htmlContent = await resp.text();
302
+ const text = await htmlToText(htmlContent.length > MAX_HTML_BYTES ? htmlContent.slice(0, MAX_HTML_BYTES) : htmlContent);
303
+ const truncated = text.length > MAX_PAGE_CHARS;
304
+ return {
305
+ url,
306
+ content: truncated ? text.slice(0, MAX_PAGE_CHARS) : text,
307
+ ...truncated ? {
308
+ truncated: true,
309
+ totalChars: text.length
310
+ } : {}
311
+ };
312
+ }
313
+ };
314
+ }
315
+ const fetchJsonParams = z.object({
316
+ url: z.string().describe("The URL to fetch JSON from"),
317
+ headers: z.record(z.string(), z.string()).describe("Optional HTTP headers to include in the request (only safe headers like Accept, Content-Type are allowed)").optional()
318
+ });
319
+ /** Headers the LLM must never control — could exfiltrate credentials or manipulate routing. */
320
+ const BLOCKED_FETCH_HEADERS = new Set([
321
+ "authorization",
322
+ "cookie",
323
+ "set-cookie",
324
+ "host",
325
+ "proxy-authorization",
326
+ "x-forwarded-for",
327
+ "x-forwarded-host",
328
+ "x-forwarded-proto",
329
+ "x-real-ip",
330
+ "cf-connecting-ip",
331
+ "fly-client-ip"
332
+ ]);
333
+ function sanitizeHeaders(raw) {
334
+ if (!raw) return;
335
+ const safe = {};
336
+ for (const [key, value] of Object.entries(raw)) if (!BLOCKED_FETCH_HEADERS.has(key.toLowerCase())) safe[key] = value;
337
+ return Object.keys(safe).length > 0 ? safe : void 0;
338
+ }
339
+ function createFetchJson(fetchFn = globalThis.fetch) {
340
+ return {
341
+ description: "Call a REST API endpoint via HTTP GET and return the JSON response. Use this to fetch structured data from APIs — for example, weather data, stock prices, exchange rates, or any public JSON API. Supports custom headers for authenticated APIs.",
342
+ parameters: fetchJsonParams,
343
+ async execute(args, _ctx) {
344
+ const { url, headers } = args;
345
+ const safeHeaders = sanitizeHeaders(headers);
346
+ const resp = await ssrfSafeFetch(url, {
347
+ ...safeHeaders && { headers: safeHeaders },
348
+ signal: fetchSignal()
349
+ }, fetchFn);
350
+ if (!resp.ok) return {
351
+ error: `HTTP ${resp.status} ${resp.statusText}`,
352
+ url
353
+ };
354
+ try {
355
+ return await resp.json();
356
+ } catch {
357
+ return {
358
+ error: "Response was not valid JSON",
359
+ url
360
+ };
361
+ }
362
+ }
363
+ };
364
+ }
365
+ const vectorSearchParams = z.object({
366
+ query: z.string().describe("Short keyword query to search the knowledge base. Use specific topic terms, not full sentences. Do NOT include the company or product name since all documents are from the same source. For example, if the user asks \"how much does Acme cost\", search for \"pricing plans rates\"."),
367
+ topK: z.number().describe("Maximum results to return (default: 5)").optional()
368
+ });
369
+ function createVectorSearch(vectorSearchFn) {
370
+ return {
371
+ description: "Search the agent's knowledge base for relevant information. Use this when the user asks a question that might be answered by previously ingested documents or data. Returns the most relevant matches ranked by similarity.",
372
+ parameters: vectorSearchParams,
373
+ async execute(args) {
374
+ const { query, topK = 5 } = args;
375
+ return vectorSearchFn(query, topK);
376
+ }
377
+ };
378
+ }
379
+ /** Resolve a builtin name to an array of [toolName, ToolDef] pairs. */
380
+ function resolveBuiltin(name, opts) {
381
+ switch (name) {
382
+ case "web_search": return [["web_search", createWebSearch(opts?.fetch)]];
383
+ case "visit_webpage": return [["visit_webpage", createVisitWebpage(opts?.fetch)]];
384
+ case "fetch_json": return [["fetch_json", createFetchJson(opts?.fetch)]];
385
+ case "run_code": return [["run_code", createRunCode()]];
386
+ case "vector_search":
387
+ if (!opts?.vectorSearch) return [];
388
+ return [["vector_search", createVectorSearch(opts.vectorSearch)]];
389
+ case "memory": return Object.entries(memoryTools());
390
+ default: return [];
391
+ }
392
+ }
393
+ /**
394
+ * Create built-in tool definitions for the given tool names.
395
+ * For runtime use — vector_search requires opts.vectorSearch to be included.
396
+ */
397
+ function getBuiltinToolDefs(names, opts) {
398
+ const defs = {};
399
+ for (const name of names) for (const [k, v] of resolveBuiltin(name, opts)) defs[k] = v;
400
+ return defs;
401
+ }
402
+ /** Returns JSON tool schemas for the specified builtin tools. */
403
+ function getBuiltinToolSchemas(names) {
404
+ return names.flatMap((name) => resolveBuiltin(name, { vectorSearch: async () => "" }).map(([toolName, def]) => ({
405
+ name: toolName,
406
+ description: def.description,
407
+ parameters: z.toJSONSchema(def.parameters ?? EMPTY_PARAMS)
408
+ })));
409
+ }
410
+ //#endregion
411
+ //#region direct-executor.ts
412
+ /**
413
+ * Direct tool execution for self-hosted mode.
414
+ *
415
+ * In self-hosted mode, agent code is trusted (you're running your own code).
416
+ * Tools execute directly in-process — no sandbox, no RPC.
417
+ */
418
+ /** Create a SQLite-backed KV store in `.aai/local.db`. */
419
+ function createLocalKv() {
420
+ mkdirSync(".aai", { recursive: true });
421
+ return createSqliteKv();
422
+ }
423
+ /** Create a SQLite-vec backed vector store in `.aai/vectors.db`. */
424
+ function createLocalVectorStore() {
425
+ mkdirSync(".aai", { recursive: true });
426
+ return createSqliteVectorStore({ path: ".aai/vectors.db" });
427
+ }
428
+ /**
429
+ * Create a direct (in-process) tool executor and hook invoker for an agent.
430
+ *
431
+ * Merges built-in and custom tool definitions, builds tool schemas for the
432
+ * S2S API, and wires up middleware and lifecycle hooks.
433
+ *
434
+ * @param opts - Executor configuration. See {@link DirectExecutorOptions}.
435
+ * @returns A {@link DirectExecutor} with tool execution, hook invocation,
436
+ * schemas, and session creation.
437
+ */
438
+ function createDirectExecutor(opts) {
439
+ const { agent, env, kv = createLocalKv(), vector = createLocalVectorStore(), vectorSearch, createWebSocket, logger = consoleLogger, s2sConfig = DEFAULT_S2S_CONFIG } = opts;
440
+ const agentConfig = toAgentConfig(agent);
441
+ const allTools = {
442
+ ...getBuiltinToolDefs(agent.builtinTools ?? [], vectorSearch ? { vectorSearch } : void 0),
443
+ ...agent.tools
444
+ };
445
+ const customSchemas = agentToolsToSchemas(agent.tools ?? {});
446
+ const builtinSchemas = getBuiltinToolSchemas(agent.builtinTools ?? []);
447
+ const toolSchemas = [...customSchemas, ...builtinSchemas];
448
+ const sessionState = createSessionStateMap(agent.state);
449
+ const frozenEnv = Object.freeze({ ...env });
450
+ /** SSRF-safe fetch for tool/hook contexts in self-hosted mode. */
451
+ const safeFetch = (input, init) => {
452
+ const req = new Request(input, init);
453
+ return ssrfSafeFetch(req.url, {
454
+ ...init,
455
+ method: req.method
456
+ }, globalThis.fetch);
457
+ };
458
+ function makeHookContext(sessionId) {
459
+ return {
460
+ env: frozenEnv,
461
+ state: sessionState.get(sessionId),
462
+ sessionId,
463
+ get kv() {
464
+ return kv;
465
+ },
466
+ get vector() {
467
+ return vector;
468
+ },
469
+ fetch: safeFetch
470
+ };
471
+ }
472
+ const executeTool = async (name, args, sessionId, messages, onUpdate) => {
473
+ const tool = allTools[name];
474
+ if (!tool) return toolError(`Unknown tool: ${name}`);
475
+ return executeToolCall(name, args, {
476
+ tool,
477
+ env: frozenEnv,
478
+ state: sessionState.get(sessionId ?? ""),
479
+ sessionId: sessionId ?? "",
480
+ kv,
481
+ vector,
482
+ messages,
483
+ logger,
484
+ onUpdate,
485
+ fetch: safeFetch
486
+ });
487
+ };
488
+ const middleware = agent.middleware ?? [];
489
+ const hookInvoker = {
490
+ async onConnect(sessionId) {
491
+ await agent.onConnect?.(makeHookContext(sessionId));
492
+ },
493
+ async onDisconnect(sessionId) {
494
+ await agent.onDisconnect?.(makeHookContext(sessionId));
495
+ sessionState.delete(sessionId);
496
+ },
497
+ async onTurn(sessionId, text) {
498
+ await agent.onTurn?.(text, makeHookContext(sessionId));
499
+ },
500
+ async onError(sessionId, error) {
501
+ await agent.onError?.(new Error(error.message), makeHookContext(sessionId));
502
+ },
503
+ async onStep(sessionId, step) {
504
+ await agent.onStep?.(step, makeHookContext(sessionId));
505
+ },
506
+ async resolveTurnConfig(sessionId) {
507
+ const ctx = makeHookContext(sessionId);
508
+ const maxSteps = typeof agent.maxSteps === "function" ? await agent.maxSteps(ctx) ?? void 0 : void 0;
509
+ if (maxSteps === void 0) return null;
510
+ return { maxSteps };
511
+ },
512
+ async filterInput(sessionId, text) {
513
+ if (middleware.length === 0) return text;
514
+ return runInputFilters(middleware, text, makeHookContext(sessionId));
515
+ },
516
+ async beforeTurn(sessionId, text) {
517
+ if (middleware.length === 0) return;
518
+ return (await runBeforeTurnMiddleware(middleware, text, makeHookContext(sessionId)))?.reason;
519
+ },
520
+ async afterTurn(sessionId, text) {
521
+ if (middleware.length === 0) return;
522
+ await runAfterTurnMiddleware(middleware, text, makeHookContext(sessionId));
523
+ },
524
+ async interceptToolCall(sessionId, toolName, args) {
525
+ if (middleware.length === 0) return;
526
+ return runToolCallInterceptors(middleware, toolName, args, makeHookContext(sessionId));
527
+ },
528
+ async afterToolCall(sessionId, toolName, args, result) {
529
+ if (middleware.length === 0) return;
530
+ await runAfterToolCallMiddleware(middleware, toolName, args, result, makeHookContext(sessionId));
531
+ },
532
+ async filterOutput(sessionId, text) {
533
+ if (middleware.length === 0) return text;
534
+ return runOutputFilters(middleware, text, makeHookContext(sessionId));
535
+ }
536
+ };
537
+ function createSession(sessionOpts) {
538
+ const apiKey = frozenEnv.ASSEMBLYAI_API_KEY ?? "";
539
+ const persistenceOpts = agent.persistence ? {
540
+ persistence: {
541
+ kv,
542
+ ttl: agent.persistence.ttl,
543
+ getState: () => sessionState.get(sessionOpts.id),
544
+ setState: (state) => sessionState.set(sessionOpts.id, state)
545
+ },
546
+ ...sessionOpts.resumeFrom ? { resumeFrom: sessionOpts.resumeFrom } : {}
547
+ } : {};
548
+ return createS2sSession({
549
+ id: sessionOpts.id,
550
+ agent: sessionOpts.agent,
551
+ client: sessionOpts.client,
552
+ agentConfig,
553
+ toolSchemas,
554
+ apiKey,
555
+ s2sConfig,
556
+ executeTool,
557
+ ...createWebSocket ? { createWebSocket } : {},
558
+ hookInvoker,
559
+ skipGreeting: sessionOpts.skipGreeting ?? false,
560
+ logger,
561
+ ...persistenceOpts
562
+ });
563
+ }
564
+ return {
565
+ executeTool,
566
+ hookInvoker,
567
+ toolSchemas,
568
+ createSession
569
+ };
570
+ }
571
+ //#endregion
572
+ export { createDirectExecutor as t };
@@ -4,7 +4,7 @@
4
4
  * In self-hosted mode, agent code is trusted (you're running your own code).
5
5
  * Tools execute directly in-process — no sandbox, no RPC.
6
6
  */
7
- import { type AgentConfig, type ToolSchema } from "./_internal-types.ts";
7
+ import { type ToolSchema } from "./_internal-types.ts";
8
8
  import type { Kv } from "./kv.ts";
9
9
  import type { ClientSink } from "./protocol.ts";
10
10
  import type { Logger, S2SConfig } from "./runtime.ts";
@@ -13,28 +13,57 @@ import { type HookInvoker, type Session } from "./session.ts";
13
13
  import type { AgentDef } from "./types.ts";
14
14
  import type { VectorStore } from "./vector.ts";
15
15
  import type { ExecuteTool } from "./worker-entry.ts";
16
+ /**
17
+ * Configuration for creating a direct (in-process) tool executor for self-hosted mode.
18
+ *
19
+ * In self-hosted mode, agent tools run directly in the Node process — no sandbox or
20
+ * RPC layer. This type configures the agent, environment, stores, and logging.
21
+ */
16
22
  export type DirectExecutorOptions = {
17
- agent: AgentDef;
23
+ agent: AgentDef<any>;
18
24
  env: Record<string, string>;
19
25
  kv?: Kv | undefined;
20
26
  vector?: VectorStore | undefined;
27
+ /** Vector search callback. Accepts a query and topK, returns a JSON string.
28
+ * Used as an RPC proxy in platform mode; in self-hosted mode the default
29
+ * uses the local `vector` store directly. */
21
30
  vectorSearch?: ((query: string, topK: number) => Promise<string>) | undefined;
31
+ /** Custom WebSocket factory for the S2S connection (useful for testing). */
22
32
  createWebSocket?: CreateS2sWebSocket | undefined;
23
33
  logger?: Logger | undefined;
24
34
  s2sConfig?: S2SConfig | undefined;
25
35
  };
36
+ /**
37
+ * The direct (in-process) executor returned by {@link createDirectExecutor}.
38
+ *
39
+ * Provides tool execution, hook invocation, tool schemas for the S2S API,
40
+ * and a factory for creating voice sessions.
41
+ */
26
42
  export type DirectExecutor = {
43
+ /** Execute a named tool with the given args, returning a JSON result string. */
27
44
  executeTool: ExecuteTool;
45
+ /** Hook invoker wired to the agent's lifecycle hooks and middleware. */
28
46
  hookInvoker: HookInvoker;
47
+ /** Tool schemas registered with the S2S API (custom + built-in). */
29
48
  toolSchemas: ToolSchema[];
49
+ /** Create a new voice session for a connected client. */
30
50
  createSession(opts: {
31
51
  id: string;
32
52
  agent: string;
33
53
  client: ClientSink;
34
54
  skipGreeting?: boolean;
55
+ /** Old session ID to resume from (loads persisted state from KV). */
56
+ resumeFrom?: string;
35
57
  }): Session;
36
58
  };
37
- /** Build a serializable AgentConfig from an AgentDef. */
38
- export declare function buildAgentConfig(agent: AgentDef): AgentConfig;
39
- /** Create a direct (in-process) tool executor and hook invoker for an agent. */
59
+ /**
60
+ * Create a direct (in-process) tool executor and hook invoker for an agent.
61
+ *
62
+ * Merges built-in and custom tool definitions, builds tool schemas for the
63
+ * S2S API, and wires up middleware and lifecycle hooks.
64
+ *
65
+ * @param opts - Executor configuration. See {@link DirectExecutorOptions}.
66
+ * @returns A {@link DirectExecutor} with tool execution, hook invocation,
67
+ * schemas, and session creation.
68
+ */
40
69
  export declare function createDirectExecutor(opts: DirectExecutorOptions): DirectExecutor;
package/dist/index.d.ts CHANGED
@@ -19,5 +19,6 @@
19
19
  * });
20
20
  * ```
21
21
  */
22
- export { type AgentOptions, type BeforeStepResult, type BuiltinTool, defineAgent, type HookContext, type Message, type StepInfo, type ToolContext, type ToolDef, tool, } from "./types.ts";
22
+ export type { Kv, KvEntry, KvListOptions } from "./kv.ts";
23
+ export { type AgentDef, type AgentOptions, type BuiltinTool, createToolFactory, defineAgent, defineTool, type HookContext, type Message, type Middleware, type MiddlewareBlockResult, type StepInfo, type ToolCallInterceptResult, type ToolChoice, type ToolContext, type ToolDef, type ToolResultMap, tool, } from "./types.ts";
23
24
  export type { VectorEntry, VectorStore } from "./vector.ts";
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- import { defineAgent, tool } from "./types.js";
2
- export { defineAgent, tool };
1
+ import { createToolFactory, defineAgent, defineTool } from "./types.js";
2
+ export { createToolFactory, defineAgent, defineTool, defineTool as tool };