@alexkroman1/aai 0.11.1 → 0.12.1
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.
- package/dist/{direct-executor-DyY-Y4ZE.js → direct-executor-ZUU0Ke4j.js} +92 -59
- package/dist/host/builtin-tools.d.ts +0 -1
- package/dist/host/index.js +4 -6
- package/dist/host/matchers.js +1 -1
- package/dist/host/server.js +14 -8
- package/dist/host/testing.js +1 -1
- package/dist/host/vite-plugin.js +1 -0
- package/dist/isolate/_internal-types.d.ts +0 -1
- package/dist/isolate/_kv-utils.d.ts +10 -0
- package/dist/isolate/index.d.ts +0 -1
- package/dist/isolate/index.js +3 -5
- package/dist/isolate/kv.d.ts +0 -10
- package/dist/isolate/kv.js +1 -33
- package/dist/isolate/protocol.js +1 -139
- package/dist/isolate/types.d.ts +1 -2
- package/dist/isolate/types.js +1 -2
- package/dist/protocol-rcOrz7T3.js +183 -0
- package/dist/{system-prompt-u19j6xfA.js → system-prompt-CVJSQJiA.js} +2 -77
- package/dist/{testing-CNNg2-n-.js → testing-Bb2B5Uob.js} +1 -1
- package/package.json +1 -1
- package/dist/constants-CpLN9WGY.js +0 -45
- package/dist/isolate/memory-tools.d.ts +0 -39
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { a as agentToolsToSchemas, o as toAgentConfig, r as EMPTY_PARAMS, t as buildSystemPrompt } from "./system-prompt-CVJSQJiA.js";
|
|
2
2
|
import { errorDetail, errorMessage, toolError } from "./isolate/_utils.js";
|
|
3
|
-
import {
|
|
3
|
+
import { C as MAX_VALUE_SIZE, S as MAX_TOOL_RESULT_CHARS, T as TOOL_EXECUTION_TIMEOUT_MS, _ as FETCH_TIMEOUT_MS, b as MAX_HTML_BYTES, g as DEFAULT_TTS_SAMPLE_RATE, h as DEFAULT_STT_SAMPLE_RATE, l as buildReadyConfig, m as DEFAULT_SHUTDOWN_TIMEOUT_MS, r as ClientMessageSchema, v as HOOK_TIMEOUT_MS, w as RUN_CODE_TIMEOUT_MS, x as MAX_PAGE_CHARS, y as MAX_GLOB_PATTERN_LENGTH } from "./protocol-rcOrz7T3.js";
|
|
4
4
|
import { callResolveTurnConfig, createAgentHooks } from "./isolate/hooks.js";
|
|
5
|
-
import { matchGlob, sortAndPaginate } from "./isolate/kv.js";
|
|
6
|
-
import { ClientMessageSchema, buildReadyConfig } from "./isolate/protocol.js";
|
|
7
5
|
import { z } from "zod";
|
|
8
6
|
import pTimeout from "p-timeout";
|
|
9
7
|
import { createStorage, prefixStorage } from "unstorage";
|
|
@@ -220,7 +218,6 @@ function resolveBuiltin(name, opts) {
|
|
|
220
218
|
case "visit_webpage": return [["visit_webpage", createVisitWebpage(opts?.fetch)]];
|
|
221
219
|
case "fetch_json": return [["fetch_json", createFetchJson(opts?.fetch)]];
|
|
222
220
|
case "run_code": return [["run_code", createRunCode()]];
|
|
223
|
-
case "memory": return Object.entries(memoryTools());
|
|
224
221
|
default: return [];
|
|
225
222
|
}
|
|
226
223
|
}
|
|
@@ -290,60 +287,63 @@ const uint8ToBase64 = (bytes) => Buffer.from(bytes).toString("base64");
|
|
|
290
287
|
const base64ToUint8 = (base64) => new Uint8Array(Buffer.from(base64, "base64"));
|
|
291
288
|
const WS_OPEN = 1;
|
|
292
289
|
const defaultCreateS2sWebSocket = (url, opts) => new WsWebSocket(url, { headers: opts.headers });
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
type: "
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
290
|
+
const S2sMessageSchema = z.discriminatedUnion("type", [
|
|
291
|
+
z.object({
|
|
292
|
+
type: z.literal("session.ready"),
|
|
293
|
+
session_id: z.string()
|
|
294
|
+
}).passthrough(),
|
|
295
|
+
z.object({ type: z.literal("session.updated") }).passthrough(),
|
|
296
|
+
z.object({ type: z.literal("input.speech.started") }),
|
|
297
|
+
z.object({ type: z.literal("input.speech.stopped") }),
|
|
298
|
+
z.object({
|
|
299
|
+
type: z.literal("transcript.user.delta"),
|
|
300
|
+
text: z.string()
|
|
301
|
+
}),
|
|
302
|
+
z.object({
|
|
303
|
+
type: z.literal("transcript.user"),
|
|
304
|
+
item_id: z.string(),
|
|
305
|
+
text: z.string()
|
|
306
|
+
}),
|
|
307
|
+
z.object({
|
|
308
|
+
type: z.literal("reply.started"),
|
|
309
|
+
reply_id: z.string()
|
|
310
|
+
}),
|
|
311
|
+
z.object({
|
|
312
|
+
type: z.literal("transcript.agent.delta"),
|
|
313
|
+
delta: z.string()
|
|
314
|
+
}).passthrough(),
|
|
315
|
+
z.object({
|
|
316
|
+
type: z.literal("transcript.agent"),
|
|
317
|
+
text: z.string(),
|
|
318
|
+
reply_id: z.string().optional().default(""),
|
|
319
|
+
item_id: z.string().optional().default(""),
|
|
320
|
+
interrupted: z.boolean().optional().default(false)
|
|
321
|
+
}),
|
|
322
|
+
z.object({ type: z.literal("reply.content_part.started") }).passthrough(),
|
|
323
|
+
z.object({ type: z.literal("reply.content_part.done") }).passthrough(),
|
|
324
|
+
z.object({
|
|
325
|
+
type: z.literal("tool.call"),
|
|
326
|
+
call_id: z.string(),
|
|
327
|
+
name: z.string(),
|
|
328
|
+
args: z.record(z.string(), z.unknown()).optional().default({})
|
|
329
|
+
}),
|
|
330
|
+
z.object({
|
|
331
|
+
type: z.literal("reply.done"),
|
|
332
|
+
status: z.string().optional()
|
|
333
|
+
}),
|
|
334
|
+
z.object({
|
|
335
|
+
type: z.literal("session.error"),
|
|
336
|
+
code: z.string(),
|
|
337
|
+
message: z.string()
|
|
338
|
+
}),
|
|
339
|
+
z.object({
|
|
340
|
+
type: z.literal("error"),
|
|
341
|
+
message: z.string()
|
|
342
|
+
})
|
|
342
343
|
]);
|
|
343
344
|
function parseS2sMessage(obj) {
|
|
344
|
-
const
|
|
345
|
-
|
|
346
|
-
return MESSAGE_VALIDATORS.get(type)?.(obj);
|
|
345
|
+
const result = S2sMessageSchema.safeParse(obj);
|
|
346
|
+
return result.success ? result.data : void 0;
|
|
347
347
|
}
|
|
348
348
|
function dispatchS2sMessage(emitter, msg) {
|
|
349
349
|
switch (msg.type) {
|
|
@@ -927,6 +927,35 @@ function createS2sSession(opts) {
|
|
|
927
927
|
};
|
|
928
928
|
}
|
|
929
929
|
//#endregion
|
|
930
|
+
//#region isolate/_kv-utils.ts
|
|
931
|
+
/** Internal KV helpers shared by kv.ts and unstorage-kv.ts. */
|
|
932
|
+
/** Sort entries by key and apply reverse/limit options. Mutates the array. */
|
|
933
|
+
function sortAndPaginate(entries, options) {
|
|
934
|
+
entries.sort((a, b) => a.key.localeCompare(b.key));
|
|
935
|
+
if (options?.reverse) entries.reverse();
|
|
936
|
+
if (options?.limit && options.limit > 0) entries.length = Math.min(entries.length, options.limit);
|
|
937
|
+
return entries;
|
|
938
|
+
}
|
|
939
|
+
/** Simple glob matcher — supports `*` as a wildcard for any characters. */
|
|
940
|
+
function matchGlob(key, pattern) {
|
|
941
|
+
if (pattern.length > 1024) throw new Error(`Glob pattern exceeds maximum length of ${MAX_GLOB_PATTERN_LENGTH}`);
|
|
942
|
+
const parts = pattern.split("*");
|
|
943
|
+
if (parts.length === 1) return key === pattern;
|
|
944
|
+
const first = parts[0];
|
|
945
|
+
if (!key.startsWith(first)) return false;
|
|
946
|
+
const last = parts.at(-1);
|
|
947
|
+
if (key.length < first.length + last.length) return false;
|
|
948
|
+
if (!key.endsWith(last)) return false;
|
|
949
|
+
let pos = first.length;
|
|
950
|
+
const end = key.length - last.length;
|
|
951
|
+
for (const part of parts.slice(1, -1)) {
|
|
952
|
+
const idx = key.indexOf(part, pos);
|
|
953
|
+
if (idx === -1 || idx > end) return false;
|
|
954
|
+
pos = idx + part.length;
|
|
955
|
+
}
|
|
956
|
+
return pos <= end;
|
|
957
|
+
}
|
|
958
|
+
//#endregion
|
|
930
959
|
//#region host/unstorage-kv.ts
|
|
931
960
|
/**
|
|
932
961
|
* Key-value store backed by unstorage.
|
|
@@ -1348,8 +1377,12 @@ function createRuntime(opts) {
|
|
|
1348
1377
|
for (const r of results) if (r.status === "rejected") logger.warn(`Session stop failed during shutdown: ${r.reason}`);
|
|
1349
1378
|
return "done";
|
|
1350
1379
|
});
|
|
1351
|
-
|
|
1352
|
-
|
|
1380
|
+
let outcome;
|
|
1381
|
+
try {
|
|
1382
|
+
outcome = await Promise.race([graceful, timeout]);
|
|
1383
|
+
} finally {
|
|
1384
|
+
if (timer) clearTimeout(timer);
|
|
1385
|
+
}
|
|
1353
1386
|
if (outcome === "timeout") logger.warn(`Shutdown timeout (${shutdownTimeoutMs}ms) exceeded — force-closing ${sessions.size} remaining session(s)`);
|
|
1354
1387
|
sessions.clear();
|
|
1355
1388
|
}
|
|
@@ -8,7 +8,6 @@
|
|
|
8
8
|
import { z } from "zod";
|
|
9
9
|
import { type ToolSchema } from "../isolate/_internal-types.ts";
|
|
10
10
|
import type { ToolDef } from "../isolate/types.ts";
|
|
11
|
-
export { memoryTools } from "../isolate/memory-tools.ts";
|
|
12
11
|
export { executeInIsolate } from "./_run-code.ts";
|
|
13
12
|
/** Options for creating built-in tool definitions. */
|
|
14
13
|
export type BuiltinToolOptions = {
|
package/dist/host/index.js
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import { BuiltinToolSchema, DEFAULT_GREETING, DEFAULT_INSTRUCTIONS, ToolChoiceSchema, defineAgent, defineTool, defineToolFactory } from "../isolate/types.js";
|
|
2
|
-
import { a as
|
|
2
|
+
import { a as agentToolsToSchemas, i as ToolSchemaSchema, n as AgentConfigSchema, o as toAgentConfig, r as EMPTY_PARAMS, t as buildSystemPrompt } from "../system-prompt-CVJSQJiA.js";
|
|
3
3
|
import { errorDetail, errorMessage, toolError } from "../isolate/_utils.js";
|
|
4
|
-
import { a as
|
|
4
|
+
import { C as MAX_VALUE_SIZE, S as MAX_TOOL_RESULT_CHARS, T as TOOL_EXECUTION_TIMEOUT_MS, _ as FETCH_TIMEOUT_MS, a as ReadyConfigSchema, b as MAX_HTML_BYTES, c as TurnConfigSchema, d as DEFAULT_IDLE_TIMEOUT_MS, f as DEFAULT_MAX_HISTORY, g as DEFAULT_TTS_SAMPLE_RATE, h as DEFAULT_STT_SAMPLE_RATE, i as KvRequestSchema, l as buildReadyConfig, m as DEFAULT_SHUTDOWN_TIMEOUT_MS, n as ClientEventSchema, o as ServerMessageSchema, p as DEFAULT_SESSION_START_TIMEOUT_MS, r as ClientMessageSchema, s as SessionErrorCodeSchema, t as AUDIO_FORMAT, u as AGENT_CSP, v as HOOK_TIMEOUT_MS, w as RUN_CODE_TIMEOUT_MS, x as MAX_PAGE_CHARS, y as MAX_GLOB_PATTERN_LENGTH } from "../protocol-rcOrz7T3.js";
|
|
5
5
|
import { callResolveTurnConfig, createAgentHooks } from "../isolate/hooks.js";
|
|
6
|
-
import { matchGlob, sortAndPaginate } from "../isolate/kv.js";
|
|
7
|
-
import { AUDIO_FORMAT, ClientEventSchema, ClientMessageSchema, KvRequestSchema, ReadyConfigSchema, ServerMessageSchema, SessionErrorCodeSchema, TurnConfigSchema, buildReadyConfig } from "../isolate/protocol.js";
|
|
8
6
|
import "../isolate/index.js";
|
|
9
|
-
import { a as _internals, c as connectS2s, d as consoleLogger, f as jsonLogger, i as createUnstorageKv, l as defaultCreateS2sWebSocket, n as executeToolCall, o as buildCtx, r as wireSessionSocket, s as createS2sSession, t as createRuntime, u as DEFAULT_S2S_CONFIG } from "../direct-executor-
|
|
7
|
+
import { a as _internals, c as connectS2s, d as consoleLogger, f as jsonLogger, i as createUnstorageKv, l as defaultCreateS2sWebSocket, n as executeToolCall, o as buildCtx, r as wireSessionSocket, s as createS2sSession, t as createRuntime, u as DEFAULT_S2S_CONFIG } from "../direct-executor-ZUU0Ke4j.js";
|
|
10
8
|
import { z } from "zod";
|
|
11
9
|
import { describe, expect, test } from "vitest";
|
|
12
10
|
//#region host/_runtime-conformance.ts
|
|
@@ -164,4 +162,4 @@ function testRuntime(label, getContext) {
|
|
|
164
162
|
});
|
|
165
163
|
}
|
|
166
164
|
//#endregion
|
|
167
|
-
export { AGENT_CSP, AUDIO_FORMAT, AgentConfigSchema, BuiltinToolSchema, CONFORMANCE_AGENT, ClientEventSchema, ClientMessageSchema, DEFAULT_GREETING, DEFAULT_IDLE_TIMEOUT_MS, DEFAULT_INSTRUCTIONS, DEFAULT_MAX_HISTORY, DEFAULT_S2S_CONFIG, DEFAULT_SESSION_START_TIMEOUT_MS, DEFAULT_SHUTDOWN_TIMEOUT_MS, DEFAULT_STT_SAMPLE_RATE, DEFAULT_TTS_SAMPLE_RATE, EMPTY_PARAMS, FETCH_TIMEOUT_MS, HOOK_TIMEOUT_MS, KvRequestSchema, MAX_GLOB_PATTERN_LENGTH, MAX_HTML_BYTES, MAX_PAGE_CHARS, MAX_TOOL_RESULT_CHARS, MAX_VALUE_SIZE, RUN_CODE_TIMEOUT_MS, ReadyConfigSchema, ServerMessageSchema, SessionErrorCodeSchema, TOOL_EXECUTION_TIMEOUT_MS, ToolChoiceSchema, ToolSchemaSchema, TurnConfigSchema, _internals, agentToolsToSchemas, buildCtx, buildReadyConfig, buildSystemPrompt, callResolveTurnConfig, connectS2s, consoleLogger, createAgentHooks, createRuntime, createS2sSession, createUnstorageKv, defaultCreateS2sWebSocket, defineAgent, defineTool, defineTool as tool, defineToolFactory, errorDetail, errorMessage, executeToolCall, jsonLogger,
|
|
165
|
+
export { AGENT_CSP, AUDIO_FORMAT, AgentConfigSchema, BuiltinToolSchema, CONFORMANCE_AGENT, ClientEventSchema, ClientMessageSchema, DEFAULT_GREETING, DEFAULT_IDLE_TIMEOUT_MS, DEFAULT_INSTRUCTIONS, DEFAULT_MAX_HISTORY, DEFAULT_S2S_CONFIG, DEFAULT_SESSION_START_TIMEOUT_MS, DEFAULT_SHUTDOWN_TIMEOUT_MS, DEFAULT_STT_SAMPLE_RATE, DEFAULT_TTS_SAMPLE_RATE, EMPTY_PARAMS, FETCH_TIMEOUT_MS, HOOK_TIMEOUT_MS, KvRequestSchema, MAX_GLOB_PATTERN_LENGTH, MAX_HTML_BYTES, MAX_PAGE_CHARS, MAX_TOOL_RESULT_CHARS, MAX_VALUE_SIZE, RUN_CODE_TIMEOUT_MS, ReadyConfigSchema, ServerMessageSchema, SessionErrorCodeSchema, TOOL_EXECUTION_TIMEOUT_MS, ToolChoiceSchema, ToolSchemaSchema, TurnConfigSchema, _internals, agentToolsToSchemas, buildCtx, buildReadyConfig, buildSystemPrompt, callResolveTurnConfig, connectS2s, consoleLogger, createAgentHooks, createRuntime, createS2sSession, createUnstorageKv, defaultCreateS2sWebSocket, defineAgent, defineTool, defineTool as tool, defineToolFactory, errorDetail, errorMessage, executeToolCall, jsonLogger, testRuntime, toAgentConfig, toolError, wireSessionSocket };
|
package/dist/host/matchers.js
CHANGED
package/dist/host/server.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { d as consoleLogger, t as createRuntime } from "../direct-executor-
|
|
1
|
+
import { u as AGENT_CSP } from "../protocol-rcOrz7T3.js";
|
|
2
|
+
import { d as consoleLogger, t as createRuntime } from "../direct-executor-ZUU0Ke4j.js";
|
|
3
3
|
import { WebSocketServer } from "ws";
|
|
4
4
|
import fs from "node:fs";
|
|
5
5
|
import http from "node:http";
|
|
@@ -145,12 +145,18 @@ function createServer(options) {
|
|
|
145
145
|
});
|
|
146
146
|
},
|
|
147
147
|
async close() {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
148
|
+
try {
|
|
149
|
+
await runtime.shutdown();
|
|
150
|
+
} finally {
|
|
151
|
+
try {
|
|
152
|
+
wss.close();
|
|
153
|
+
} finally {
|
|
154
|
+
if (listenPort !== void 0) await new Promise((resolve, reject) => {
|
|
155
|
+
httpServer.close((err) => err ? reject(err) : resolve());
|
|
156
|
+
});
|
|
157
|
+
listenPort = void 0;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
154
160
|
}
|
|
155
161
|
};
|
|
156
162
|
}
|
package/dist/host/testing.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as makeStubSession, i as flush, n as TurnResult, o as MockWebSocket, r as createTestHarness, s as installMockWebSocket, t as TestHarness } from "../testing-
|
|
1
|
+
import { a as makeStubSession, i as flush, n as TurnResult, o as MockWebSocket, r as createTestHarness, s as installMockWebSocket, t as TestHarness } from "../testing-Bb2B5Uob.js";
|
|
2
2
|
export { MockWebSocket, TestHarness, TurnResult, createTestHarness, flush, installMockWebSocket, makeStubSession };
|
package/dist/host/vite-plugin.js
CHANGED
|
@@ -66,6 +66,7 @@ function aai(options) {
|
|
|
66
66
|
}),
|
|
67
67
|
name: agentDef.name
|
|
68
68
|
});
|
|
69
|
+
if (backendPort == null) throw new Error("backendPort was not resolved during config phase");
|
|
69
70
|
await agentServer.listen(backendPort);
|
|
70
71
|
server = agentServer;
|
|
71
72
|
viteServer.config.logger.info(`Agent backend on port ${backendPort}`);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** Internal KV helpers shared by kv.ts and unstorage-kv.ts. */
|
|
2
|
+
/** Sort entries by key and apply reverse/limit options. Mutates the array. */
|
|
3
|
+
export declare function sortAndPaginate<T extends {
|
|
4
|
+
key: string;
|
|
5
|
+
}>(entries: T[], options?: {
|
|
6
|
+
limit?: number;
|
|
7
|
+
reverse?: boolean;
|
|
8
|
+
}): T[];
|
|
9
|
+
/** Simple glob matcher — supports `*` as a wildcard for any characters. */
|
|
10
|
+
export declare function matchGlob(key: string, pattern: string): boolean;
|
package/dist/isolate/index.d.ts
CHANGED
|
@@ -13,7 +13,6 @@ export * from "./_utils.ts";
|
|
|
13
13
|
export * from "./constants.ts";
|
|
14
14
|
export * from "./hooks.ts";
|
|
15
15
|
export * from "./kv.ts";
|
|
16
|
-
export * from "./memory-tools.ts";
|
|
17
16
|
export * from "./protocol.ts";
|
|
18
17
|
export * from "./system-prompt.ts";
|
|
19
18
|
export * from "./types.ts";
|
package/dist/isolate/index.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { BuiltinToolSchema, DEFAULT_GREETING, DEFAULT_INSTRUCTIONS, ToolChoiceSchema, defineAgent, defineTool, defineToolFactory } from "./types.js";
|
|
2
|
-
import { a as
|
|
2
|
+
import { a as agentToolsToSchemas, i as ToolSchemaSchema, n as AgentConfigSchema, o as toAgentConfig, r as EMPTY_PARAMS, t as buildSystemPrompt } from "../system-prompt-CVJSQJiA.js";
|
|
3
3
|
import { errorDetail, errorMessage, toolError } from "./_utils.js";
|
|
4
|
-
import { a as
|
|
4
|
+
import { C as MAX_VALUE_SIZE, S as MAX_TOOL_RESULT_CHARS, T as TOOL_EXECUTION_TIMEOUT_MS, _ as FETCH_TIMEOUT_MS, a as ReadyConfigSchema, b as MAX_HTML_BYTES, c as TurnConfigSchema, d as DEFAULT_IDLE_TIMEOUT_MS, f as DEFAULT_MAX_HISTORY, g as DEFAULT_TTS_SAMPLE_RATE, h as DEFAULT_STT_SAMPLE_RATE, i as KvRequestSchema, l as buildReadyConfig, m as DEFAULT_SHUTDOWN_TIMEOUT_MS, n as ClientEventSchema, o as ServerMessageSchema, p as DEFAULT_SESSION_START_TIMEOUT_MS, r as ClientMessageSchema, s as SessionErrorCodeSchema, t as AUDIO_FORMAT, u as AGENT_CSP, v as HOOK_TIMEOUT_MS, w as RUN_CODE_TIMEOUT_MS, x as MAX_PAGE_CHARS, y as MAX_GLOB_PATTERN_LENGTH } from "../protocol-rcOrz7T3.js";
|
|
5
5
|
import { callResolveTurnConfig, createAgentHooks } from "./hooks.js";
|
|
6
|
-
|
|
7
|
-
import { AUDIO_FORMAT, ClientEventSchema, ClientMessageSchema, KvRequestSchema, ReadyConfigSchema, ServerMessageSchema, SessionErrorCodeSchema, TurnConfigSchema, buildReadyConfig } from "./protocol.js";
|
|
8
|
-
export { AGENT_CSP, AUDIO_FORMAT, AgentConfigSchema, BuiltinToolSchema, ClientEventSchema, ClientMessageSchema, DEFAULT_GREETING, DEFAULT_IDLE_TIMEOUT_MS, DEFAULT_INSTRUCTIONS, DEFAULT_MAX_HISTORY, DEFAULT_SESSION_START_TIMEOUT_MS, DEFAULT_SHUTDOWN_TIMEOUT_MS, DEFAULT_STT_SAMPLE_RATE, DEFAULT_TTS_SAMPLE_RATE, EMPTY_PARAMS, FETCH_TIMEOUT_MS, HOOK_TIMEOUT_MS, KvRequestSchema, MAX_GLOB_PATTERN_LENGTH, MAX_HTML_BYTES, MAX_PAGE_CHARS, MAX_TOOL_RESULT_CHARS, MAX_VALUE_SIZE, RUN_CODE_TIMEOUT_MS, ReadyConfigSchema, ServerMessageSchema, SessionErrorCodeSchema, TOOL_EXECUTION_TIMEOUT_MS, ToolChoiceSchema, ToolSchemaSchema, TurnConfigSchema, agentToolsToSchemas, buildReadyConfig, buildSystemPrompt, callResolveTurnConfig, createAgentHooks, defineAgent, defineTool, defineTool as tool, defineToolFactory, errorDetail, errorMessage, matchGlob, memoryTools, sortAndPaginate, toAgentConfig, toolError };
|
|
6
|
+
export { AGENT_CSP, AUDIO_FORMAT, AgentConfigSchema, BuiltinToolSchema, ClientEventSchema, ClientMessageSchema, DEFAULT_GREETING, DEFAULT_IDLE_TIMEOUT_MS, DEFAULT_INSTRUCTIONS, DEFAULT_MAX_HISTORY, DEFAULT_SESSION_START_TIMEOUT_MS, DEFAULT_SHUTDOWN_TIMEOUT_MS, DEFAULT_STT_SAMPLE_RATE, DEFAULT_TTS_SAMPLE_RATE, EMPTY_PARAMS, FETCH_TIMEOUT_MS, HOOK_TIMEOUT_MS, KvRequestSchema, MAX_GLOB_PATTERN_LENGTH, MAX_HTML_BYTES, MAX_PAGE_CHARS, MAX_TOOL_RESULT_CHARS, MAX_VALUE_SIZE, RUN_CODE_TIMEOUT_MS, ReadyConfigSchema, ServerMessageSchema, SessionErrorCodeSchema, TOOL_EXECUTION_TIMEOUT_MS, ToolChoiceSchema, ToolSchemaSchema, TurnConfigSchema, agentToolsToSchemas, buildReadyConfig, buildSystemPrompt, callResolveTurnConfig, createAgentHooks, defineAgent, defineTool, defineTool as tool, defineToolFactory, errorDetail, errorMessage, toAgentConfig, toolError };
|
package/dist/isolate/kv.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Key-value storage interface and shared utilities.
|
|
3
3
|
*/
|
|
4
|
-
export { MAX_VALUE_SIZE } from "./constants.ts";
|
|
5
4
|
/**
|
|
6
5
|
* A single key-value entry returned by {@link Kv.list}.
|
|
7
6
|
*
|
|
@@ -107,12 +106,3 @@ export type Kv = {
|
|
|
107
106
|
*/
|
|
108
107
|
close?(): void;
|
|
109
108
|
};
|
|
110
|
-
/** Sort entries by key and apply reverse/limit options. Mutates the array. */
|
|
111
|
-
export declare function sortAndPaginate<T extends {
|
|
112
|
-
key: string;
|
|
113
|
-
}>(entries: T[], options?: {
|
|
114
|
-
limit?: number;
|
|
115
|
-
reverse?: boolean;
|
|
116
|
-
}): T[];
|
|
117
|
-
/** Simple glob matcher — supports `*` as a wildcard for any characters. */
|
|
118
|
-
export declare function matchGlob(key: string, pattern: string): boolean;
|
package/dist/isolate/kv.js
CHANGED
|
@@ -1,33 +1 @@
|
|
|
1
|
-
|
|
2
|
-
//#region isolate/kv.ts
|
|
3
|
-
/**
|
|
4
|
-
* Key-value storage interface and shared utilities.
|
|
5
|
-
*/
|
|
6
|
-
/** Sort entries by key and apply reverse/limit options. Mutates the array. */
|
|
7
|
-
function sortAndPaginate(entries, options) {
|
|
8
|
-
entries.sort((a, b) => a.key.localeCompare(b.key));
|
|
9
|
-
if (options?.reverse) entries.reverse();
|
|
10
|
-
if (options?.limit && options.limit > 0) entries.length = Math.min(entries.length, options.limit);
|
|
11
|
-
return entries;
|
|
12
|
-
}
|
|
13
|
-
/** Simple glob matcher — supports `*` as a wildcard for any characters. */
|
|
14
|
-
function matchGlob(key, pattern) {
|
|
15
|
-
if (pattern.length > 1024) throw new Error(`Glob pattern exceeds maximum length of ${MAX_GLOB_PATTERN_LENGTH}`);
|
|
16
|
-
const parts = pattern.split("*");
|
|
17
|
-
if (parts.length === 1) return key === pattern;
|
|
18
|
-
const first = parts[0];
|
|
19
|
-
if (!key.startsWith(first)) return false;
|
|
20
|
-
const last = parts.at(-1);
|
|
21
|
-
if (key.length < first.length + last.length) return false;
|
|
22
|
-
if (!key.endsWith(last)) return false;
|
|
23
|
-
let pos = first.length;
|
|
24
|
-
const end = key.length - last.length;
|
|
25
|
-
for (const part of parts.slice(1, -1)) {
|
|
26
|
-
const idx = key.indexOf(part, pos);
|
|
27
|
-
if (idx === -1 || idx > end) return false;
|
|
28
|
-
pos = idx + part.length;
|
|
29
|
-
}
|
|
30
|
-
return pos <= end;
|
|
31
|
-
}
|
|
32
|
-
//#endregion
|
|
33
|
-
export { MAX_VALUE_SIZE, matchGlob, sortAndPaginate };
|
|
1
|
+
export {};
|
package/dist/isolate/protocol.js
CHANGED
|
@@ -1,140 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { z } from "zod";
|
|
3
|
-
//#region isolate/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
|
-
/** Zod schema for KV operation requests from the worker to the host. */
|
|
16
|
-
const KvRequestSchema = z.discriminatedUnion("op", [
|
|
17
|
-
z.object({
|
|
18
|
-
op: z.literal("get"),
|
|
19
|
-
key: z.string().min(1)
|
|
20
|
-
}),
|
|
21
|
-
z.object({
|
|
22
|
-
op: z.literal("set"),
|
|
23
|
-
key: z.string().min(1),
|
|
24
|
-
value: z.string(),
|
|
25
|
-
expireIn: z.number().int().positive().optional()
|
|
26
|
-
}),
|
|
27
|
-
z.object({
|
|
28
|
-
op: z.literal("del"),
|
|
29
|
-
key: z.string().min(1)
|
|
30
|
-
}),
|
|
31
|
-
z.object({
|
|
32
|
-
op: z.literal("list"),
|
|
33
|
-
prefix: z.string(),
|
|
34
|
-
limit: z.number().int().positive().optional(),
|
|
35
|
-
reverse: z.boolean().optional()
|
|
36
|
-
}),
|
|
37
|
-
z.object({
|
|
38
|
-
op: z.literal("keys"),
|
|
39
|
-
pattern: z.string().optional()
|
|
40
|
-
})
|
|
41
|
-
]);
|
|
42
|
-
/**
|
|
43
|
-
* Zod schema for session error codes.
|
|
44
|
-
* @public
|
|
45
|
-
*/
|
|
46
|
-
const SessionErrorCodeSchema = z.enum([
|
|
47
|
-
"stt",
|
|
48
|
-
"llm",
|
|
49
|
-
"tts",
|
|
50
|
-
"tool",
|
|
51
|
-
"protocol",
|
|
52
|
-
"connection",
|
|
53
|
-
"audio",
|
|
54
|
-
"internal"
|
|
55
|
-
]);
|
|
56
|
-
/** Helper: simple event with only a type field. */
|
|
57
|
-
const ev = (t) => z.object({ type: z.literal(t) });
|
|
58
|
-
/** Helper: event with type + text. */
|
|
59
|
-
const textEv = (t) => z.object({
|
|
60
|
-
type: z.literal(t),
|
|
61
|
-
text: z.string()
|
|
62
|
-
});
|
|
63
|
-
const turnOrder = z.number().int().nonnegative().optional();
|
|
64
|
-
/** Zod schema for {@link ClientEvent}. */
|
|
65
|
-
const ClientEventSchema = z.discriminatedUnion("type", [
|
|
66
|
-
ev("speech_started"),
|
|
67
|
-
ev("speech_stopped"),
|
|
68
|
-
z.object({
|
|
69
|
-
type: z.literal("transcript"),
|
|
70
|
-
text: z.string(),
|
|
71
|
-
isFinal: z.boolean(),
|
|
72
|
-
turnOrder
|
|
73
|
-
}),
|
|
74
|
-
textEv("turn").extend({ turnOrder }),
|
|
75
|
-
textEv("chat"),
|
|
76
|
-
textEv("chat_delta"),
|
|
77
|
-
z.object({
|
|
78
|
-
type: z.literal("tool_call_start"),
|
|
79
|
-
toolCallId: z.string(),
|
|
80
|
-
toolName: z.string(),
|
|
81
|
-
args: z.record(z.string(), z.unknown())
|
|
82
|
-
}),
|
|
83
|
-
z.object({
|
|
84
|
-
type: z.literal("tool_call_done"),
|
|
85
|
-
toolCallId: z.string(),
|
|
86
|
-
result: z.string().max(MAX_TOOL_RESULT_CHARS)
|
|
87
|
-
}),
|
|
88
|
-
ev("tts_done"),
|
|
89
|
-
ev("cancelled"),
|
|
90
|
-
ev("reset"),
|
|
91
|
-
ev("idle_timeout"),
|
|
92
|
-
z.object({
|
|
93
|
-
type: z.literal("error"),
|
|
94
|
-
code: SessionErrorCodeSchema,
|
|
95
|
-
message: z.string()
|
|
96
|
-
})
|
|
97
|
-
]);
|
|
98
|
-
/** Zod schema for {@link ReadyConfig}. */
|
|
99
|
-
const ReadyConfigSchema = z.object({
|
|
100
|
-
audioFormat: z.enum(["pcm16"]),
|
|
101
|
-
sampleRate: z.number().int().positive(),
|
|
102
|
-
ttsSampleRate: z.number().int().positive()
|
|
103
|
-
});
|
|
104
|
-
/** Zod schema for server→client text messages. */
|
|
105
|
-
const ServerMessageSchema = z.discriminatedUnion("type", [
|
|
106
|
-
z.object({
|
|
107
|
-
type: z.literal("config"),
|
|
108
|
-
audioFormat: z.string(),
|
|
109
|
-
sampleRate: z.number(),
|
|
110
|
-
ttsSampleRate: z.number(),
|
|
111
|
-
sessionId: z.string().optional()
|
|
112
|
-
}),
|
|
113
|
-
ev("audio_done"),
|
|
114
|
-
...ClientEventSchema.options
|
|
115
|
-
]);
|
|
116
|
-
/** Zod schema for client→server text messages. */
|
|
117
|
-
const ClientMessageSchema = z.discriminatedUnion("type", [
|
|
118
|
-
ev("audio_ready"),
|
|
119
|
-
ev("cancel"),
|
|
120
|
-
ev("reset"),
|
|
121
|
-
z.object({
|
|
122
|
-
type: z.literal("history"),
|
|
123
|
-
messages: z.array(z.object({
|
|
124
|
-
role: z.enum(["user", "assistant"]),
|
|
125
|
-
content: z.string().max(1e5)
|
|
126
|
-
})).max(200)
|
|
127
|
-
})
|
|
128
|
-
]);
|
|
129
|
-
/** Build the protocol-level session config from S2S sample rates. */
|
|
130
|
-
function buildReadyConfig(s2sConfig) {
|
|
131
|
-
return {
|
|
132
|
-
audioFormat: AUDIO_FORMAT,
|
|
133
|
-
sampleRate: s2sConfig.inputSampleRate,
|
|
134
|
-
ttsSampleRate: s2sConfig.outputSampleRate
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
/** Zod schema for {@link TurnConfig}. */
|
|
138
|
-
const TurnConfigSchema = z.object({ maxSteps: z.number().int().positive().optional() });
|
|
139
|
-
//#endregion
|
|
1
|
+
import { a as ReadyConfigSchema, c as TurnConfigSchema, i as KvRequestSchema, l as buildReadyConfig, n as ClientEventSchema, o as ServerMessageSchema, r as ClientMessageSchema, s as SessionErrorCodeSchema, t as AUDIO_FORMAT } from "../protocol-rcOrz7T3.js";
|
|
140
2
|
export { AUDIO_FORMAT, ClientEventSchema, ClientMessageSchema, KvRequestSchema, ReadyConfigSchema, ServerMessageSchema, SessionErrorCodeSchema, TurnConfigSchema, buildReadyConfig };
|
package/dist/isolate/types.d.ts
CHANGED
|
@@ -17,7 +17,7 @@ import type { Kv } from "./kv.ts";
|
|
|
17
17
|
*
|
|
18
18
|
* @public
|
|
19
19
|
*/
|
|
20
|
-
export type BuiltinTool = "web_search" | "visit_webpage" | "fetch_json" | "run_code"
|
|
20
|
+
export type BuiltinTool = "web_search" | "visit_webpage" | "fetch_json" | "run_code";
|
|
21
21
|
/**
|
|
22
22
|
* How the LLM should select tools during a turn.
|
|
23
23
|
*
|
|
@@ -375,7 +375,6 @@ export declare const BuiltinToolSchema: z.ZodEnum<{
|
|
|
375
375
|
visit_webpage: "visit_webpage";
|
|
376
376
|
fetch_json: "fetch_json";
|
|
377
377
|
run_code: "run_code";
|
|
378
|
-
memory: "memory";
|
|
379
378
|
}>;
|
|
380
379
|
/** @internal Zod schema for {@link ToolChoice}. Exported for reuse in internal schemas. */
|
|
381
380
|
export declare const ToolChoiceSchema: z.ZodUnion<readonly [z.ZodEnum<{
|
package/dist/isolate/types.js
CHANGED
|
@@ -99,8 +99,7 @@ const BuiltinToolSchema = z.enum([
|
|
|
99
99
|
"web_search",
|
|
100
100
|
"visit_webpage",
|
|
101
101
|
"fetch_json",
|
|
102
|
-
"run_code"
|
|
103
|
-
"memory"
|
|
102
|
+
"run_code"
|
|
104
103
|
]);
|
|
105
104
|
/** @internal Zod schema for {@link ToolChoice}. Exported for reuse in internal schemas. */
|
|
106
105
|
const ToolChoiceSchema = z.union([z.enum([
|
|
@@ -0,0 +1,183 @@
|
|
|
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 };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BuiltinToolSchema, DEFAULT_INSTRUCTIONS, ToolChoiceSchema
|
|
1
|
+
import { BuiltinToolSchema, DEFAULT_INSTRUCTIONS, ToolChoiceSchema } from "./isolate/types.js";
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
//#region isolate/_internal-types.ts
|
|
4
4
|
/**
|
|
@@ -58,81 +58,6 @@ function agentToolsToSchemas(tools) {
|
|
|
58
58
|
}));
|
|
59
59
|
}
|
|
60
60
|
//#endregion
|
|
61
|
-
//#region isolate/memory-tools.ts
|
|
62
|
-
/**
|
|
63
|
-
* KV-backed memory tools for agent persistent state.
|
|
64
|
-
*/
|
|
65
|
-
/**
|
|
66
|
-
* Returns a standard set of KV-backed memory tools: `save_memory`,
|
|
67
|
-
* `recall_memory`, `list_memories`, and `forget_memory`.
|
|
68
|
-
*
|
|
69
|
-
* Spread the result into your agent's `tools` record.
|
|
70
|
-
*
|
|
71
|
-
* @example
|
|
72
|
-
* ```ts
|
|
73
|
-
* import { defineAgent, memoryTools } from "aai";
|
|
74
|
-
*
|
|
75
|
-
* export default defineAgent({
|
|
76
|
-
* name: "My Agent",
|
|
77
|
-
* tools: { ...memoryTools() },
|
|
78
|
-
* });
|
|
79
|
-
* ```
|
|
80
|
-
*
|
|
81
|
-
* @returns A record with four tool definitions: `save_memory`, `recall_memory`,
|
|
82
|
-
* `list_memories`, and `forget_memory`.
|
|
83
|
-
* @public
|
|
84
|
-
*/
|
|
85
|
-
function memoryTools() {
|
|
86
|
-
return {
|
|
87
|
-
save_memory: defineTool({
|
|
88
|
-
description: "Save a piece of information to persistent memory. Use a descriptive key like 'user:name' or 'project:status'.",
|
|
89
|
-
parameters: z.object({
|
|
90
|
-
key: z.string().describe("A descriptive key for this memory (e.g. 'user:name', 'preference:color')"),
|
|
91
|
-
value: z.string().describe("The information to remember")
|
|
92
|
-
}),
|
|
93
|
-
execute: async ({ key, value }, ctx) => {
|
|
94
|
-
await ctx.kv.set(key, value);
|
|
95
|
-
return { saved: key };
|
|
96
|
-
}
|
|
97
|
-
}),
|
|
98
|
-
recall_memory: defineTool({
|
|
99
|
-
description: "Retrieve a previously saved memory by its key.",
|
|
100
|
-
parameters: z.object({ key: z.string().describe("The key to look up") }),
|
|
101
|
-
execute: async ({ key }, ctx) => {
|
|
102
|
-
const value = await ctx.kv.get(key);
|
|
103
|
-
if (value === null) return {
|
|
104
|
-
found: false,
|
|
105
|
-
key
|
|
106
|
-
};
|
|
107
|
-
return {
|
|
108
|
-
found: true,
|
|
109
|
-
key,
|
|
110
|
-
value
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
}),
|
|
114
|
-
list_memories: defineTool({
|
|
115
|
-
description: "List all saved memory keys, optionally filtered by a prefix (e.g. 'user:').",
|
|
116
|
-
parameters: z.object({ prefix: z.string().describe("Prefix to filter keys (e.g. 'user:'). Use empty string for all.").optional() }),
|
|
117
|
-
execute: async ({ prefix }, ctx) => {
|
|
118
|
-
const entries = await ctx.kv.list(prefix ?? "");
|
|
119
|
-
return {
|
|
120
|
-
count: entries.length,
|
|
121
|
-
keys: entries.map((e) => e.key)
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
}),
|
|
125
|
-
forget_memory: defineTool({
|
|
126
|
-
description: "Delete a previously saved memory by its key.",
|
|
127
|
-
parameters: z.object({ key: z.string().describe("The key to delete") }),
|
|
128
|
-
execute: async ({ key }, ctx) => {
|
|
129
|
-
await ctx.kv.delete(key);
|
|
130
|
-
return { deleted: key };
|
|
131
|
-
}
|
|
132
|
-
})
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
//#endregion
|
|
136
61
|
//#region isolate/system-prompt.ts
|
|
137
62
|
function getFormattedDate() {
|
|
138
63
|
return (/* @__PURE__ */ new Date()).toLocaleDateString("en-US", {
|
|
@@ -163,4 +88,4 @@ function buildSystemPrompt(config, opts) {
|
|
|
163
88
|
return DEFAULT_INSTRUCTIONS + `\n\nToday's date is ${getFormattedDate()}.` + agentInstructions + toolPreamble + (opts.voice ? VOICE_RULES : "");
|
|
164
89
|
}
|
|
165
90
|
//#endregion
|
|
166
|
-
export {
|
|
91
|
+
export { agentToolsToSchemas as a, ToolSchemaSchema as i, AgentConfigSchema as n, toAgentConfig as o, EMPTY_PARAMS as r, buildSystemPrompt as t };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import "./isolate/types.js";
|
|
2
|
-
import { i as createUnstorageKv, t as createRuntime } from "./direct-executor-
|
|
2
|
+
import { i as createUnstorageKv, t as createRuntime } from "./direct-executor-ZUU0Ke4j.js";
|
|
3
3
|
import { vi } from "vitest";
|
|
4
4
|
import { createStorage } from "unstorage";
|
|
5
5
|
import "nanoevents";
|
package/package.json
CHANGED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
//#region isolate/constants.ts
|
|
2
|
-
/**
|
|
3
|
-
* Centralised numeric constants — timeouts, size limits, sample rates.
|
|
4
|
-
*
|
|
5
|
-
* Every magic number that controls a timeout, buffer size, or threshold
|
|
6
|
-
* lives here so the values are discoverable in one place.
|
|
7
|
-
*/
|
|
8
|
-
/** Default sample rate for speech-to-text audio in Hz (AssemblyAI). */
|
|
9
|
-
const DEFAULT_STT_SAMPLE_RATE = 16e3;
|
|
10
|
-
/** Default sample rate for text-to-speech audio in Hz. */
|
|
11
|
-
const DEFAULT_TTS_SAMPLE_RATE = 24e3;
|
|
12
|
-
/** Default timeout for agent lifecycle hooks (onConnect, onTurn, etc). */
|
|
13
|
-
const HOOK_TIMEOUT_MS = 5e3;
|
|
14
|
-
/** Default timeout for tool execution in the worker. */
|
|
15
|
-
const TOOL_EXECUTION_TIMEOUT_MS = 3e4;
|
|
16
|
-
/** Timeout for session.start() (S2S connection setup). */
|
|
17
|
-
const DEFAULT_SESSION_START_TIMEOUT_MS = 1e4;
|
|
18
|
-
/** S2S session idle timeout before auto-close. */
|
|
19
|
-
const DEFAULT_IDLE_TIMEOUT_MS = 3e5;
|
|
20
|
-
/** Per-fetch timeout for network tools (web_search, visit_webpage, fetch_json). */
|
|
21
|
-
const FETCH_TIMEOUT_MS = 15e3;
|
|
22
|
-
/** Timeout for sandboxed run_code execution. */
|
|
23
|
-
const RUN_CODE_TIMEOUT_MS = 5e3;
|
|
24
|
-
/** Maximum time to wait for sessions to stop during graceful shutdown. */
|
|
25
|
-
const DEFAULT_SHUTDOWN_TIMEOUT_MS = 3e4;
|
|
26
|
-
/** Maximum length for tool result strings sent to clients. */
|
|
27
|
-
const MAX_TOOL_RESULT_CHARS = 4e3;
|
|
28
|
-
/** Maximum chars for webpage text after HTML-to-text conversion. */
|
|
29
|
-
const MAX_PAGE_CHARS = 1e4;
|
|
30
|
-
/** Maximum bytes to fetch from an HTML page before conversion. */
|
|
31
|
-
const MAX_HTML_BYTES = 2e5;
|
|
32
|
-
/** Maximum value size for KV store entries (bytes). */
|
|
33
|
-
const MAX_VALUE_SIZE = 65536;
|
|
34
|
-
/** Maximum glob pattern length to prevent ReDoS. */
|
|
35
|
-
const MAX_GLOB_PATTERN_LENGTH = 1024;
|
|
36
|
-
/** Maximum conversation messages to retain (sliding window). */
|
|
37
|
-
const DEFAULT_MAX_HISTORY = 200;
|
|
38
|
-
/**
|
|
39
|
-
* Content-Security-Policy applied to agent UI pages (both self-hosted and
|
|
40
|
-
* platform). Single source of truth — used by `secureHeaders` middleware
|
|
41
|
-
* and per-response CSP headers.
|
|
42
|
-
*/
|
|
43
|
-
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'";
|
|
44
|
-
//#endregion
|
|
45
|
-
export { DEFAULT_SHUTDOWN_TIMEOUT_MS as a, FETCH_TIMEOUT_MS as c, MAX_HTML_BYTES as d, MAX_PAGE_CHARS as f, TOOL_EXECUTION_TIMEOUT_MS as g, RUN_CODE_TIMEOUT_MS as h, DEFAULT_SESSION_START_TIMEOUT_MS as i, HOOK_TIMEOUT_MS as l, MAX_VALUE_SIZE as m, DEFAULT_IDLE_TIMEOUT_MS as n, DEFAULT_STT_SAMPLE_RATE as o, MAX_TOOL_RESULT_CHARS as p, DEFAULT_MAX_HISTORY as r, DEFAULT_TTS_SAMPLE_RATE as s, AGENT_CSP as t, MAX_GLOB_PATTERN_LENGTH as u };
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* KV-backed memory tools for agent persistent state.
|
|
3
|
-
*/
|
|
4
|
-
import { z } from "zod";
|
|
5
|
-
/**
|
|
6
|
-
* Returns a standard set of KV-backed memory tools: `save_memory`,
|
|
7
|
-
* `recall_memory`, `list_memories`, and `forget_memory`.
|
|
8
|
-
*
|
|
9
|
-
* Spread the result into your agent's `tools` record.
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* ```ts
|
|
13
|
-
* import { defineAgent, memoryTools } from "aai";
|
|
14
|
-
*
|
|
15
|
-
* export default defineAgent({
|
|
16
|
-
* name: "My Agent",
|
|
17
|
-
* tools: { ...memoryTools() },
|
|
18
|
-
* });
|
|
19
|
-
* ```
|
|
20
|
-
*
|
|
21
|
-
* @returns A record with four tool definitions: `save_memory`, `recall_memory`,
|
|
22
|
-
* `list_memories`, and `forget_memory`.
|
|
23
|
-
* @public
|
|
24
|
-
*/
|
|
25
|
-
export declare function memoryTools(): {
|
|
26
|
-
save_memory: import("./types.ts").ToolDef<z.ZodObject<{
|
|
27
|
-
key: z.ZodString;
|
|
28
|
-
value: z.ZodString;
|
|
29
|
-
}, z.core.$strip>, Record<string, unknown>>;
|
|
30
|
-
recall_memory: import("./types.ts").ToolDef<z.ZodObject<{
|
|
31
|
-
key: z.ZodString;
|
|
32
|
-
}, z.core.$strip>, Record<string, unknown>>;
|
|
33
|
-
list_memories: import("./types.ts").ToolDef<z.ZodObject<{
|
|
34
|
-
prefix: z.ZodOptional<z.ZodString>;
|
|
35
|
-
}, z.core.$strip>, Record<string, unknown>>;
|
|
36
|
-
forget_memory: import("./types.ts").ToolDef<z.ZodObject<{
|
|
37
|
-
key: z.ZodString;
|
|
38
|
-
}, z.core.$strip>, Record<string, unknown>>;
|
|
39
|
-
};
|