@alexkroman1/aai 0.10.2 → 0.10.4
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/_internal-types.d.ts +8 -1
- package/dist/_runtime-conformance.d.ts +64 -0
- package/dist/_test-utils.d.ts +70 -0
- package/dist/_utils.d.ts +1 -8
- package/dist/_utils.js +1 -13
- package/dist/builtin-tools.d.ts +1 -5
- package/dist/constants-BbAOvKl_.js +47 -0
- package/dist/constants.d.ts +44 -0
- package/dist/direct-executor-BfHrDdPL.js +1589 -0
- package/dist/direct-executor.d.ts +90 -31
- package/dist/hooks.d.ts +44 -0
- package/dist/hooks.js +58 -0
- package/dist/index.d.ts +1 -2
- package/dist/index.js +2 -2
- package/dist/internal.d.ts +19 -0
- package/dist/internal.js +209 -0
- package/dist/kv.d.ts +1 -1
- package/dist/kv.js +5 -4
- package/dist/matchers.js +1 -1
- package/dist/protocol.d.ts +3 -29
- package/dist/protocol.js +2 -24
- package/dist/server.d.ts +25 -38
- package/dist/server.js +114 -138
- package/dist/session.d.ts +65 -44
- package/dist/{testing-MRl3SXsI.js → testing-BonJtfHJ.js} +26 -46
- package/dist/testing.d.ts +9 -14
- package/dist/testing.js +2 -2
- package/dist/types.d.ts +24 -226
- package/dist/types.js +6 -22
- package/dist/types.test-d.d.ts +7 -0
- package/dist/unstorage-kv.d.ts +33 -0
- package/dist/vite-plugin.d.ts +15 -0
- package/dist/vite-plugin.js +82 -0
- package/dist/ws-handler.d.ts +1 -2
- package/package.json +29 -84
- package/dist/_internal-types.js +0 -61
- package/dist/_session-ctx.d.ts +0 -73
- package/dist/_session-otel.d.ts +0 -43
- package/dist/_session-persist.d.ts +0 -30
- package/dist/_ssrf.d.ts +0 -30
- package/dist/_ssrf.js +0 -123
- package/dist/direct-executor-Ca0wt5H0.js +0 -572
- package/dist/middleware-core.d.ts +0 -47
- package/dist/middleware-core.js +0 -107
- package/dist/middleware.d.ts +0 -37
- package/dist/runtime.js +0 -53
- package/dist/s2s.js +0 -272
- package/dist/session-BkN9u0ni.js +0 -683
- package/dist/session.js +0 -2
- package/dist/sqlite-kv.d.ts +0 -34
- package/dist/sqlite-kv.js +0 -133
- package/dist/sqlite-vector.d.ts +0 -58
- package/dist/sqlite-vector.js +0 -149
- package/dist/telemetry.d.ts +0 -49
- package/dist/telemetry.js +0 -95
- package/dist/vector.d.ts +0 -85
- package/dist/vector.js +0 -49
- package/dist/worker-entry.d.ts +0 -47
- package/dist/worker-entry.js +0 -70
- package/dist/ws-handler.js +0 -207
|
@@ -1,69 +1,128 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Agent runtime — the execution engine for voice agents.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
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.
|
|
6
7
|
*/
|
|
7
|
-
import { type ToolSchema } from "./_internal-types.ts";
|
|
8
|
+
import { type ExecuteTool, type ToolSchema } from "./_internal-types.ts";
|
|
9
|
+
import { type AgentHooks } from "./hooks.ts";
|
|
8
10
|
import type { Kv } from "./kv.ts";
|
|
9
11
|
import type { ClientSink } from "./protocol.ts";
|
|
12
|
+
import { type ReadyConfig } from "./protocol.ts";
|
|
10
13
|
import type { Logger, S2SConfig } from "./runtime.ts";
|
|
11
14
|
import type { CreateS2sWebSocket } from "./s2s.ts";
|
|
12
|
-
import { type
|
|
13
|
-
import type { AgentDef } from "./types.ts";
|
|
14
|
-
import type
|
|
15
|
-
|
|
15
|
+
import { type Session } from "./session.ts";
|
|
16
|
+
import type { AgentDef, Message, ToolDef } from "./types.ts";
|
|
17
|
+
import { type SessionWebSocket } from "./ws-handler.ts";
|
|
18
|
+
export type { ExecuteTool } from "./_internal-types.ts";
|
|
19
|
+
export type ExecuteToolCallOptions = {
|
|
20
|
+
tool: ToolDef;
|
|
21
|
+
env: Readonly<Record<string, string>>;
|
|
22
|
+
state?: Record<string, unknown>;
|
|
23
|
+
sessionId?: string | undefined;
|
|
24
|
+
kv?: Kv | undefined;
|
|
25
|
+
messages?: readonly Message[] | undefined;
|
|
26
|
+
logger?: Logger | undefined;
|
|
27
|
+
fetch?: typeof globalThis.fetch | undefined;
|
|
28
|
+
};
|
|
29
|
+
export declare function executeToolCall(name: string, args: Readonly<Record<string, unknown>>, options: ExecuteToolCallOptions): Promise<string>;
|
|
30
|
+
/** Per-session options passed to {@link AgentRuntime.startSession}. */
|
|
31
|
+
export type SessionStartOptions = {
|
|
32
|
+
skipGreeting?: boolean;
|
|
33
|
+
resumeFrom?: string;
|
|
34
|
+
logContext?: Record<string, string>;
|
|
35
|
+
onOpen?: () => void;
|
|
36
|
+
onClose?: () => void;
|
|
37
|
+
};
|
|
16
38
|
/**
|
|
17
|
-
*
|
|
39
|
+
* Common interface for agent runtimes.
|
|
18
40
|
*
|
|
19
|
-
*
|
|
20
|
-
* RPC layer. This type configures the agent, environment, stores, and logging.
|
|
41
|
+
* Implemented by {@link createRuntime} and the platform sandbox.
|
|
21
42
|
*/
|
|
22
|
-
export type
|
|
43
|
+
export type AgentRuntime = {
|
|
44
|
+
startSession(ws: SessionWebSocket, opts?: SessionStartOptions): void;
|
|
45
|
+
shutdown(): Promise<void>;
|
|
46
|
+
readonly readyConfig: ReadyConfig;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Configuration for {@link createRuntime}.
|
|
50
|
+
*
|
|
51
|
+
* Configures the agent, environment, KV store, logging, and S2S connection.
|
|
52
|
+
*
|
|
53
|
+
* @public
|
|
54
|
+
*/
|
|
55
|
+
export type RuntimeOptions = {
|
|
23
56
|
agent: AgentDef<any>;
|
|
24
57
|
env: Record<string, string>;
|
|
25
58
|
kv?: Kv | undefined;
|
|
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. */
|
|
30
|
-
vectorSearch?: ((query: string, topK: number) => Promise<string>) | undefined;
|
|
31
59
|
/** Custom WebSocket factory for the S2S connection (useful for testing). */
|
|
32
60
|
createWebSocket?: CreateS2sWebSocket | undefined;
|
|
33
61
|
logger?: Logger | undefined;
|
|
34
62
|
s2sConfig?: S2SConfig | undefined;
|
|
63
|
+
/**
|
|
64
|
+
* Timeout in ms for `session.start()` (S2S connection setup).
|
|
65
|
+
* Defaults to 10 000 (10 s).
|
|
66
|
+
*/
|
|
67
|
+
sessionStartTimeoutMs?: number | undefined;
|
|
68
|
+
/**
|
|
69
|
+
* Maximum time in milliseconds to wait for sessions to stop during
|
|
70
|
+
* {@link AgentRuntime.shutdown | shutdown()}. Defaults to `30_000` (30 s).
|
|
71
|
+
*/
|
|
72
|
+
shutdownTimeoutMs?: number | undefined;
|
|
73
|
+
/**
|
|
74
|
+
* Override tool execution. When provided, `createRuntime` skips building
|
|
75
|
+
* in-process tool definitions and uses this function instead. Used by the
|
|
76
|
+
* platform sandbox to RPC tool calls to the isolate.
|
|
77
|
+
*/
|
|
78
|
+
executeTool?: ExecuteTool | undefined;
|
|
79
|
+
/**
|
|
80
|
+
* Override lifecycle hooks. When provided, `createRuntime` skips building
|
|
81
|
+
* in-process hooks and uses these instead. Used by the platform sandbox
|
|
82
|
+
* to RPC hook calls to the isolate.
|
|
83
|
+
*/
|
|
84
|
+
hooks?: AgentHooks | undefined;
|
|
85
|
+
/**
|
|
86
|
+
* Override tool schemas sent to the S2S API. Required when `executeTool`
|
|
87
|
+
* is provided (the host doesn't have the tool definitions to derive schemas).
|
|
88
|
+
*/
|
|
89
|
+
toolSchemas?: ToolSchema[] | undefined;
|
|
35
90
|
};
|
|
36
91
|
/**
|
|
37
|
-
* The
|
|
92
|
+
* The agent runtime returned by {@link createRuntime}.
|
|
38
93
|
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
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
|
|
41
99
|
*/
|
|
42
|
-
export type
|
|
100
|
+
export type Runtime = AgentRuntime & {
|
|
43
101
|
/** Execute a named tool with the given args, returning a JSON result string. */
|
|
44
102
|
executeTool: ExecuteTool;
|
|
45
|
-
/**
|
|
46
|
-
|
|
103
|
+
/** Hookable instance wired to the agent's lifecycle hooks. */
|
|
104
|
+
hooks: AgentHooks;
|
|
47
105
|
/** Tool schemas registered with the S2S API (custom + built-in). */
|
|
48
106
|
toolSchemas: ToolSchema[];
|
|
49
|
-
/** Create a new voice session for a connected client. */
|
|
107
|
+
/** Create a new voice session for a connected client (lower-level than startSession). */
|
|
50
108
|
createSession(opts: {
|
|
51
109
|
id: string;
|
|
52
110
|
agent: string;
|
|
53
111
|
client: ClientSink;
|
|
54
112
|
skipGreeting?: boolean;
|
|
55
|
-
/** Old session ID to resume from (loads persisted state from KV). */
|
|
56
113
|
resumeFrom?: string;
|
|
57
114
|
}): Session;
|
|
58
115
|
};
|
|
59
116
|
/**
|
|
60
|
-
* Create
|
|
117
|
+
* Create an agent runtime — the execution engine for a voice agent.
|
|
61
118
|
*
|
|
62
119
|
* Merges built-in and custom tool definitions, builds tool schemas for the
|
|
63
|
-
* S2S API, and wires up
|
|
120
|
+
* S2S API, and wires up lifecycle hooks.
|
|
121
|
+
*
|
|
122
|
+
* @param opts - Runtime configuration. See {@link RuntimeOptions}.
|
|
123
|
+
* @returns A {@link Runtime} with tool execution, hook invocation,
|
|
124
|
+
* schemas, and session management.
|
|
64
125
|
*
|
|
65
|
-
* @
|
|
66
|
-
* @returns A {@link DirectExecutor} with tool execution, hook invocation,
|
|
67
|
-
* schemas, and session creation.
|
|
126
|
+
* @public
|
|
68
127
|
*/
|
|
69
|
-
export declare function
|
|
128
|
+
export declare function createRuntime(opts: RuntimeOptions): Runtime;
|
package/dist/hooks.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hookable-based lifecycle hook system.
|
|
3
|
+
*
|
|
4
|
+
* Provides a unified hook registry built on {@link https://github.com/unjs/hookable | hookable}.
|
|
5
|
+
* Lifecycle hooks (connect, disconnect, turn, error, resolveTurnConfig) are
|
|
6
|
+
* registered on a single `Hookable<AgentHookMap>` instance.
|
|
7
|
+
*/
|
|
8
|
+
import { type Hookable } from "hookable";
|
|
9
|
+
import type { AgentDef, HookContext } from "./types.ts";
|
|
10
|
+
/**
|
|
11
|
+
* Map of all agent hook names to their function signatures.
|
|
12
|
+
*
|
|
13
|
+
* All hooks are typed as void-returning for hookable compatibility.
|
|
14
|
+
* Value-returning hooks (resolveTurnConfig) are invoked via
|
|
15
|
+
* {@link callHookWith} with a custom caller that extracts the return value.
|
|
16
|
+
*/
|
|
17
|
+
export interface AgentHookMap {
|
|
18
|
+
connect: (sessionId: string, timeoutMs?: number) => void | Promise<void>;
|
|
19
|
+
disconnect: (sessionId: string, timeoutMs?: number) => void | Promise<void>;
|
|
20
|
+
turn: (sessionId: string, text: string, timeoutMs?: number) => void | Promise<void>;
|
|
21
|
+
error: (sessionId: string, error: {
|
|
22
|
+
message: string;
|
|
23
|
+
}, timeoutMs?: number) => void | Promise<void>;
|
|
24
|
+
resolveTurnConfig: (sessionId: string, timeoutMs?: number) => void | Promise<void>;
|
|
25
|
+
}
|
|
26
|
+
/** A hookable instance typed to the agent hook map. */
|
|
27
|
+
export type AgentHooks = Hookable<AgentHookMap>;
|
|
28
|
+
/**
|
|
29
|
+
* Call the resolveTurnConfig hook.
|
|
30
|
+
* Returns null when no handler is registered.
|
|
31
|
+
*/
|
|
32
|
+
export declare function callResolveTurnConfig(hooks: AgentHooks | undefined, sessionId: string, timeoutMs?: number): Promise<{
|
|
33
|
+
maxSteps?: number;
|
|
34
|
+
} | null>;
|
|
35
|
+
/**
|
|
36
|
+
* Create an {@link AgentHooks} instance from an agent definition.
|
|
37
|
+
*
|
|
38
|
+
* Registers lifecycle hooks from the agent's `onConnect`, `onDisconnect`,
|
|
39
|
+
* `onTurn`, `onError` callbacks.
|
|
40
|
+
*/
|
|
41
|
+
export declare function createAgentHooks(opts: {
|
|
42
|
+
agent: AgentDef<any>;
|
|
43
|
+
makeCtx: (sessionId: string) => HookContext;
|
|
44
|
+
}): AgentHooks;
|
package/dist/hooks.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { createHooks } from "hookable";
|
|
2
|
+
//#region hooks.ts
|
|
3
|
+
/**
|
|
4
|
+
* Hookable-based lifecycle hook system.
|
|
5
|
+
*
|
|
6
|
+
* Provides a unified hook registry built on {@link https://github.com/unjs/hookable | hookable}.
|
|
7
|
+
* Lifecycle hooks (connect, disconnect, turn, error, resolveTurnConfig) are
|
|
8
|
+
* registered on a single `Hookable<AgentHookMap>` instance.
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Caller that invokes the first registered handler and returns its result.
|
|
12
|
+
* Returns `fallback` when no handlers are registered.
|
|
13
|
+
*/
|
|
14
|
+
function firstResultCaller(fallback) {
|
|
15
|
+
return async (fns, args) => {
|
|
16
|
+
if (fns.length === 0) return fallback;
|
|
17
|
+
return await fns[0](...args) ?? fallback;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Call the resolveTurnConfig hook.
|
|
22
|
+
* Returns null when no handler is registered.
|
|
23
|
+
*/
|
|
24
|
+
async function callResolveTurnConfig(hooks, sessionId, timeoutMs) {
|
|
25
|
+
if (!hooks) return null;
|
|
26
|
+
return hooks.callHookWith(firstResultCaller(null), "resolveTurnConfig", [sessionId, timeoutMs]);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Create an {@link AgentHooks} instance from an agent definition.
|
|
30
|
+
*
|
|
31
|
+
* Registers lifecycle hooks from the agent's `onConnect`, `onDisconnect`,
|
|
32
|
+
* `onTurn`, `onError` callbacks.
|
|
33
|
+
*/
|
|
34
|
+
function createAgentHooks(opts) {
|
|
35
|
+
const { agent, makeCtx } = opts;
|
|
36
|
+
const hooks = createHooks();
|
|
37
|
+
hooks.hook("connect", async (sessionId) => {
|
|
38
|
+
await agent.onConnect?.(makeCtx(sessionId));
|
|
39
|
+
});
|
|
40
|
+
hooks.hook("disconnect", async (sessionId) => {
|
|
41
|
+
await agent.onDisconnect?.(makeCtx(sessionId));
|
|
42
|
+
});
|
|
43
|
+
hooks.hook("turn", async (sessionId, text) => {
|
|
44
|
+
await agent.onTurn?.(text, makeCtx(sessionId));
|
|
45
|
+
});
|
|
46
|
+
hooks.hook("error", async (sessionId, error) => {
|
|
47
|
+
await agent.onError?.(new Error(error.message), makeCtx(sessionId));
|
|
48
|
+
});
|
|
49
|
+
hooks.hook("resolveTurnConfig", (async (sessionId) => {
|
|
50
|
+
const ctx = makeCtx(sessionId);
|
|
51
|
+
const maxSteps = typeof agent.maxSteps === "function" ? await agent.maxSteps(ctx) ?? void 0 : void 0;
|
|
52
|
+
if (maxSteps === void 0) return null;
|
|
53
|
+
return { maxSteps };
|
|
54
|
+
}));
|
|
55
|
+
return hooks;
|
|
56
|
+
}
|
|
57
|
+
//#endregion
|
|
58
|
+
export { callResolveTurnConfig, createAgentHooks };
|
package/dist/index.d.ts
CHANGED
|
@@ -20,5 +20,4 @@
|
|
|
20
20
|
* ```
|
|
21
21
|
*/
|
|
22
22
|
export type { Kv, KvEntry, KvListOptions } from "./kv.ts";
|
|
23
|
-
export { type AgentDef, type AgentOptions, type BuiltinTool,
|
|
24
|
-
export type { VectorEntry, VectorStore } from "./vector.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 "./types.ts";
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export {
|
|
1
|
+
import { defineAgent, defineTool, defineToolFactory } from "./types.js";
|
|
2
|
+
export { defineAgent, defineTool, defineTool as tool, defineToolFactory };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal barrel — re-exports all SDK internals for use by the platform
|
|
3
|
+
* server (`aai-server`) and CLI. **Not a public API.**
|
|
4
|
+
*
|
|
5
|
+
* Consumer packages should import from the top-level `@alexkroman1/aai`
|
|
6
|
+
* entry, `./server`, `./types`, `./kv`, `./protocol`, or `./testing`.
|
|
7
|
+
*/
|
|
8
|
+
export * from "./_internal-types.ts";
|
|
9
|
+
export * from "./_runtime-conformance.ts";
|
|
10
|
+
export * from "./_utils.ts";
|
|
11
|
+
export * from "./constants.ts";
|
|
12
|
+
export * from "./direct-executor.ts";
|
|
13
|
+
export * from "./hooks.ts";
|
|
14
|
+
export * from "./protocol.ts";
|
|
15
|
+
export * from "./runtime.ts";
|
|
16
|
+
export * from "./s2s.ts";
|
|
17
|
+
export * from "./session.ts";
|
|
18
|
+
export * from "./unstorage-kv.ts";
|
|
19
|
+
export * from "./ws-handler.ts";
|
package/dist/internal.js
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { defineTool } from "./types.js";
|
|
2
|
+
import { _ as TOOL_EXECUTION_TIMEOUT_MS, a as DEFAULT_SHUTDOWN_TIMEOUT_MS, c as FETCH_TIMEOUT_MS, d as MAX_HTML_BYTES, f as MAX_PAGE_CHARS, g as RUN_CODE_TIMEOUT_MS, h as RUN_CODE_MEMORY_MB, i as DEFAULT_SESSION_START_TIMEOUT_MS, l as HOOK_TIMEOUT_MS, m as MAX_VALUE_SIZE, n as DEFAULT_IDLE_TIMEOUT_MS, o as DEFAULT_STT_SAMPLE_RATE, p as MAX_TOOL_RESULT_CHARS, r as DEFAULT_MAX_HISTORY, s as DEFAULT_TTS_SAMPLE_RATE, t as AGENT_CSP, u as MAX_GLOB_PATTERN_LENGTH } from "./constants-BbAOvKl_.js";
|
|
3
|
+
import { _ as consoleLogger, a as _internals, c as buildSystemPrompt, d as AgentConfigSchema, f as EMPTY_PARAMS, g as DEFAULT_S2S_CONFIG, h as toAgentConfig, i as createUnstorageKv, l as connectS2s, m as agentToolsToSchemas, n as executeToolCall, o as buildCtx, p as ToolSchemaSchema, r as wireSessionSocket, s as createS2sSession, t as createRuntime, u as defaultCreateS2sWebSocket, v as jsonLogger } from "./direct-executor-BfHrDdPL.js";
|
|
4
|
+
import { createSessionStateMap, errorDetail, errorMessage, isReadOnlyFsOp, toolError } from "./_utils.js";
|
|
5
|
+
import { callResolveTurnConfig, createAgentHooks } from "./hooks.js";
|
|
6
|
+
import { AUDIO_FORMAT, ClientEventSchema, ClientMessageSchema, KvRequestSchema, ReadyConfigSchema, ServerMessageSchema, SessionErrorCodeSchema, TurnConfigSchema, buildReadyConfig } from "./protocol.js";
|
|
7
|
+
import { z } from "zod";
|
|
8
|
+
import { describe, expect, test } from "vitest";
|
|
9
|
+
//#region _runtime-conformance.ts
|
|
10
|
+
/**
|
|
11
|
+
* Shared runtime conformance tests.
|
|
12
|
+
*
|
|
13
|
+
* Both the self-hosted direct executor and the platform sandbox must satisfy
|
|
14
|
+
* the same behavioral contract. This module defines that contract as a
|
|
15
|
+
* reusable test suite that can be wired to either runtime.
|
|
16
|
+
*
|
|
17
|
+
* Inspired by Nitro's `testNitro()` pattern: one test fixture, many runtimes.
|
|
18
|
+
*
|
|
19
|
+
* @example Direct executor (unit test)
|
|
20
|
+
* ```ts
|
|
21
|
+
* import { testRuntime } from "./_runtime-conformance.ts";
|
|
22
|
+
*
|
|
23
|
+
* testRuntime("direct", () => {
|
|
24
|
+
* const exec = createRuntime({ agent: CONFORMANCE_AGENT, env: { MY_VAR: "test-value" } });
|
|
25
|
+
* return { executeTool: exec.executeTool, hooks: exec.hooks };
|
|
26
|
+
* });
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @example Sandbox (integration test)
|
|
30
|
+
* ```ts
|
|
31
|
+
* import { testRuntime, CONFORMANCE_AGENT_BUNDLE } from "@alexkroman1/aai/runtime-conformance";
|
|
32
|
+
*
|
|
33
|
+
* testRuntime("sandbox", async () => {
|
|
34
|
+
* // ... start isolate with CONFORMANCE_AGENT_BUNDLE
|
|
35
|
+
* return { executeTool: buildExecuteTool(...), hooks: buildHookInvoker(...) };
|
|
36
|
+
* });
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
/**
|
|
40
|
+
* Agent definition used by the conformance suite (direct executor path).
|
|
41
|
+
*
|
|
42
|
+
* Must be kept in sync with {@link CONFORMANCE_AGENT_BUNDLE}.
|
|
43
|
+
*/
|
|
44
|
+
const CONFORMANCE_AGENT = {
|
|
45
|
+
name: "conformance-test",
|
|
46
|
+
instructions: "Conformance test agent.",
|
|
47
|
+
greeting: "Hello!",
|
|
48
|
+
maxSteps: 5,
|
|
49
|
+
state: () => ({
|
|
50
|
+
count: 0,
|
|
51
|
+
lastTurn: ""
|
|
52
|
+
}),
|
|
53
|
+
tools: {
|
|
54
|
+
echo: defineTool({
|
|
55
|
+
description: "Echo input",
|
|
56
|
+
parameters: z.object({ text: z.string() }),
|
|
57
|
+
execute: ({ text }) => `echo:${text}`
|
|
58
|
+
}),
|
|
59
|
+
get_env: {
|
|
60
|
+
description: "Get MY_VAR from env",
|
|
61
|
+
execute: (_args, ctx) => ctx.env.MY_VAR ?? "missing"
|
|
62
|
+
},
|
|
63
|
+
get_state: {
|
|
64
|
+
description: "Get session state",
|
|
65
|
+
execute: (_args, ctx) => JSON.stringify(ctx.state)
|
|
66
|
+
},
|
|
67
|
+
echo_messages: {
|
|
68
|
+
description: "Return messages as JSON",
|
|
69
|
+
execute: (_args, ctx) => JSON.stringify(ctx.messages)
|
|
70
|
+
},
|
|
71
|
+
kv_roundtrip: defineTool({
|
|
72
|
+
description: "KV set then get",
|
|
73
|
+
parameters: z.object({ value: z.string() }),
|
|
74
|
+
execute: async ({ value }, ctx) => {
|
|
75
|
+
await ctx.kv.set("test-key", value);
|
|
76
|
+
const result = await ctx.kv.get("test-key");
|
|
77
|
+
return `stored:${JSON.stringify(result)}`;
|
|
78
|
+
}
|
|
79
|
+
})
|
|
80
|
+
},
|
|
81
|
+
onConnect: (ctx) => {
|
|
82
|
+
ctx.state.count = 1;
|
|
83
|
+
},
|
|
84
|
+
onTurn: (text, ctx) => {
|
|
85
|
+
ctx.state.lastTurn = text;
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* JavaScript bundle equivalent of {@link CONFORMANCE_AGENT} for the sandbox
|
|
90
|
+
* isolate path. Must be kept in sync with the AgentDef above.
|
|
91
|
+
*/
|
|
92
|
+
const CONFORMANCE_AGENT_BUNDLE = `
|
|
93
|
+
export default {
|
|
94
|
+
name: "conformance-test",
|
|
95
|
+
instructions: "Conformance test agent.",
|
|
96
|
+
greeting: "Hello!",
|
|
97
|
+
maxSteps: 5,
|
|
98
|
+
state: () => ({ count: 0, lastTurn: "" }),
|
|
99
|
+
tools: {
|
|
100
|
+
echo: {
|
|
101
|
+
description: "Echo input",
|
|
102
|
+
execute(args) { return "echo:" + args.text; },
|
|
103
|
+
},
|
|
104
|
+
get_env: {
|
|
105
|
+
description: "Get MY_VAR from env",
|
|
106
|
+
execute(_args, ctx) { return ctx.env.MY_VAR ?? "missing"; },
|
|
107
|
+
},
|
|
108
|
+
get_state: {
|
|
109
|
+
description: "Get session state",
|
|
110
|
+
execute(_args, ctx) { return JSON.stringify(ctx.state); },
|
|
111
|
+
},
|
|
112
|
+
echo_messages: {
|
|
113
|
+
description: "Return messages as JSON",
|
|
114
|
+
execute(_args, ctx) { return JSON.stringify(ctx.messages); },
|
|
115
|
+
},
|
|
116
|
+
kv_roundtrip: {
|
|
117
|
+
description: "KV set then get",
|
|
118
|
+
async execute(args, ctx) {
|
|
119
|
+
await ctx.kv.set("test-key", args.value);
|
|
120
|
+
const result = await ctx.kv.get("test-key");
|
|
121
|
+
return "stored:" + JSON.stringify(result);
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
onConnect: (ctx) => { ctx.state.count = 1; },
|
|
126
|
+
onTurn: (text, ctx) => { ctx.state.lastTurn = text; },
|
|
127
|
+
};
|
|
128
|
+
`;
|
|
129
|
+
/**
|
|
130
|
+
* Run the runtime conformance test suite against a given runtime context.
|
|
131
|
+
*
|
|
132
|
+
* The `getContext` callback is invoked once per test to retrieve the
|
|
133
|
+
* current {@link RuntimeTestContext}. This allows the caller to set up
|
|
134
|
+
* the runtime in a `beforeAll` and return it lazily.
|
|
135
|
+
*
|
|
136
|
+
* All tests assume the runtime was created with {@link CONFORMANCE_AGENT}
|
|
137
|
+
* (or its bundle equivalent) and `env: { MY_VAR: "test-value" }`.
|
|
138
|
+
*/
|
|
139
|
+
function testRuntime(label, getContext) {
|
|
140
|
+
describe(`runtime conformance: ${label}`, () => {
|
|
141
|
+
test("executes tool and returns result", async () => {
|
|
142
|
+
const { executeTool } = getContext();
|
|
143
|
+
expect(await executeTool("echo", { text: "hello" }, "s1", [])).toBe("echo:hello");
|
|
144
|
+
});
|
|
145
|
+
test("tool receives env variables", async () => {
|
|
146
|
+
const { executeTool } = getContext();
|
|
147
|
+
expect(await executeTool("get_env", {}, "s1", [])).toBe("test-value");
|
|
148
|
+
});
|
|
149
|
+
test("tool receives conversation messages", async () => {
|
|
150
|
+
const { executeTool } = getContext();
|
|
151
|
+
const msgs = [{
|
|
152
|
+
role: "user",
|
|
153
|
+
content: "hi"
|
|
154
|
+
}, {
|
|
155
|
+
role: "assistant",
|
|
156
|
+
content: "hello"
|
|
157
|
+
}];
|
|
158
|
+
const result = await executeTool("echo_messages", {}, "s1", msgs);
|
|
159
|
+
expect(JSON.parse(result)).toEqual(msgs);
|
|
160
|
+
});
|
|
161
|
+
test("KV round-trip through tool context", async () => {
|
|
162
|
+
const { executeTool } = getContext();
|
|
163
|
+
expect(await executeTool("kv_roundtrip", { value: "abc" }, "s1", [])).toBe("stored:\"abc\"");
|
|
164
|
+
});
|
|
165
|
+
test("session state is initialized from factory", async () => {
|
|
166
|
+
const { executeTool } = getContext();
|
|
167
|
+
const result = await executeTool("get_state", {}, "state-init", []);
|
|
168
|
+
const state = JSON.parse(result);
|
|
169
|
+
expect(state).toHaveProperty("count", 0);
|
|
170
|
+
expect(state).toHaveProperty("lastTurn", "");
|
|
171
|
+
});
|
|
172
|
+
test("onConnect hook updates session state", async () => {
|
|
173
|
+
const { executeTool, hooks } = getContext();
|
|
174
|
+
const sid = "state-connect";
|
|
175
|
+
await hooks.callHook("connect", sid);
|
|
176
|
+
const result = await executeTool("get_state", {}, sid, []);
|
|
177
|
+
expect(JSON.parse(result).count).toBe(1);
|
|
178
|
+
});
|
|
179
|
+
test("onTurn hook updates session state", async () => {
|
|
180
|
+
const { executeTool, hooks } = getContext();
|
|
181
|
+
const sid = "state-turn";
|
|
182
|
+
await hooks.callHook("turn", sid, "user said something");
|
|
183
|
+
const result = await executeTool("get_state", {}, sid, []);
|
|
184
|
+
expect(JSON.parse(result).lastTurn).toBe("user said something");
|
|
185
|
+
});
|
|
186
|
+
test("onConnect resolves without error", async () => {
|
|
187
|
+
const { hooks } = getContext();
|
|
188
|
+
await expect(hooks.callHook("connect", "hook-1")).resolves.toBeUndefined();
|
|
189
|
+
});
|
|
190
|
+
test("onDisconnect resolves without error", async () => {
|
|
191
|
+
const { hooks } = getContext();
|
|
192
|
+
await expect(hooks.callHook("disconnect", "hook-2")).resolves.toBeUndefined();
|
|
193
|
+
});
|
|
194
|
+
test("onTurn resolves without error", async () => {
|
|
195
|
+
const { hooks } = getContext();
|
|
196
|
+
await expect(hooks.callHook("turn", "hook-3", "test")).resolves.toBeUndefined();
|
|
197
|
+
});
|
|
198
|
+
test("onError resolves without error", async () => {
|
|
199
|
+
const { hooks } = getContext();
|
|
200
|
+
await expect(hooks.callHook("error", "hook-4", { message: "boom" })).resolves.toBeUndefined();
|
|
201
|
+
});
|
|
202
|
+
test("resolveTurnConfig returns null for static maxSteps", async () => {
|
|
203
|
+
const { hooks } = getContext();
|
|
204
|
+
expect(await callResolveTurnConfig(hooks, "hook-5")).toBeNull();
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
//#endregion
|
|
209
|
+
export { AGENT_CSP, AUDIO_FORMAT, AgentConfigSchema, CONFORMANCE_AGENT, CONFORMANCE_AGENT_BUNDLE, ClientEventSchema, ClientMessageSchema, DEFAULT_IDLE_TIMEOUT_MS, 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_MEMORY_MB, RUN_CODE_TIMEOUT_MS, ReadyConfigSchema, ServerMessageSchema, SessionErrorCodeSchema, TOOL_EXECUTION_TIMEOUT_MS, ToolSchemaSchema, TurnConfigSchema, _internals, agentToolsToSchemas, buildCtx, buildReadyConfig, buildSystemPrompt, callResolveTurnConfig, connectS2s, consoleLogger, createAgentHooks, createRuntime, createS2sSession, createSessionStateMap, createUnstorageKv, defaultCreateS2sWebSocket, errorDetail, errorMessage, executeToolCall, isReadOnlyFsOp, jsonLogger, testRuntime, toAgentConfig, toolError, wireSessionSocket };
|
package/dist/kv.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Key-value storage interface and shared utilities.
|
|
3
3
|
*/
|
|
4
|
+
export { MAX_VALUE_SIZE } from "./constants.ts";
|
|
4
5
|
/**
|
|
5
6
|
* A single key-value entry returned by {@link Kv.list}.
|
|
6
7
|
*
|
|
@@ -106,7 +107,6 @@ export type Kv = {
|
|
|
106
107
|
*/
|
|
107
108
|
close?(): void;
|
|
108
109
|
};
|
|
109
|
-
export declare const MAX_VALUE_SIZE = 65536;
|
|
110
110
|
/** Sort entries by key and apply reverse/limit options. Mutates the array. */
|
|
111
111
|
export declare function sortAndPaginate<T extends {
|
|
112
112
|
key: string;
|
package/dist/kv.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
import { m as MAX_VALUE_SIZE, u as MAX_GLOB_PATTERN_LENGTH } from "./constants-BbAOvKl_.js";
|
|
1
2
|
//#region kv.ts
|
|
2
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Key-value storage interface and shared utilities.
|
|
5
|
+
*/
|
|
3
6
|
/** Sort entries by key and apply reverse/limit options. Mutates the array. */
|
|
4
7
|
function sortAndPaginate(entries, options) {
|
|
5
8
|
entries.sort((a, b) => a.key.localeCompare(b.key));
|
|
@@ -7,11 +10,9 @@ function sortAndPaginate(entries, options) {
|
|
|
7
10
|
if (options?.limit && options.limit > 0) entries.length = Math.min(entries.length, options.limit);
|
|
8
11
|
return entries;
|
|
9
12
|
}
|
|
10
|
-
/** Maximum allowed glob pattern length to prevent ReDoS. */
|
|
11
|
-
const MAX_GLOB_PATTERN_LENGTH = 1024;
|
|
12
13
|
/** Simple glob matcher — supports `*` as a wildcard for any characters. */
|
|
13
14
|
function matchGlob(key, pattern) {
|
|
14
|
-
if (pattern.length >
|
|
15
|
+
if (pattern.length > 1024) throw new Error(`Glob pattern exceeds maximum length of ${MAX_GLOB_PATTERN_LENGTH}`);
|
|
15
16
|
const parts = pattern.split("*");
|
|
16
17
|
if (parts.length === 1) return key === pattern;
|
|
17
18
|
const first = parts[0];
|
package/dist/matchers.js
CHANGED
package/dist/protocol.d.ts
CHANGED
|
@@ -4,18 +4,6 @@
|
|
|
4
4
|
* Note: this module is for internal use only and should not be used directly.
|
|
5
5
|
*/
|
|
6
6
|
import { z } from "zod";
|
|
7
|
-
/**
|
|
8
|
-
* Default sample rate for speech-to-text audio in Hz.
|
|
9
|
-
*
|
|
10
|
-
* This is the sample rate expected by the STT provider (AssemblyAI).
|
|
11
|
-
*/
|
|
12
|
-
export declare const DEFAULT_STT_SAMPLE_RATE = 16000;
|
|
13
|
-
/**
|
|
14
|
-
* Default sample rate for text-to-speech audio in Hz.
|
|
15
|
-
*
|
|
16
|
-
* This is the sample rate produced by the TTS provider.
|
|
17
|
-
*/
|
|
18
|
-
export declare const DEFAULT_TTS_SAMPLE_RATE = 24000;
|
|
19
7
|
/**
|
|
20
8
|
* Audio codec identifier used in the wire protocol.
|
|
21
9
|
*
|
|
@@ -45,23 +33,17 @@ export declare const KvRequestSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
|
45
33
|
}, z.core.$strip>], "op">;
|
|
46
34
|
/** KV operation request — discriminated union on the `op` field. */
|
|
47
35
|
export type KvRequest = z.infer<typeof KvRequestSchema>;
|
|
48
|
-
/** Default timeout for agent lifecycle hooks (onConnect, onTurn, etc). */
|
|
49
|
-
export declare const HOOK_TIMEOUT_MS = 5000;
|
|
50
|
-
/** Default timeout for tool execution in the worker. */
|
|
51
|
-
export declare const TOOL_EXECUTION_TIMEOUT_MS = 30000;
|
|
52
|
-
/** Maximum length for tool result strings sent to clients. */
|
|
53
|
-
export declare const MAX_TOOL_RESULT_CHARS = 4000;
|
|
54
36
|
/**
|
|
55
37
|
* Zod schema for session error codes.
|
|
56
38
|
* @public
|
|
57
39
|
*/
|
|
58
40
|
export declare const SessionErrorCodeSchema: z.ZodEnum<{
|
|
59
41
|
tool: "tool";
|
|
42
|
+
connection: "connection";
|
|
60
43
|
stt: "stt";
|
|
61
44
|
llm: "llm";
|
|
62
45
|
tts: "tts";
|
|
63
46
|
protocol: "protocol";
|
|
64
|
-
connection: "connection";
|
|
65
47
|
audio: "audio";
|
|
66
48
|
internal: "internal";
|
|
67
49
|
}>;
|
|
@@ -96,10 +78,6 @@ export declare const ClientEventSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
|
96
78
|
toolCallId: z.ZodString;
|
|
97
79
|
toolName: z.ZodString;
|
|
98
80
|
args: z.ZodRecord<z.ZodString, z.ZodUnknown>;
|
|
99
|
-
}, z.core.$strip>, z.ZodObject<{
|
|
100
|
-
type: z.ZodLiteral<"tool_call_update">;
|
|
101
|
-
toolCallId: z.ZodString;
|
|
102
|
-
data: z.ZodString;
|
|
103
81
|
}, z.core.$strip>, z.ZodObject<{
|
|
104
82
|
type: z.ZodLiteral<"tool_call_done">;
|
|
105
83
|
toolCallId: z.ZodString;
|
|
@@ -116,11 +94,11 @@ export declare const ClientEventSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
|
116
94
|
type: z.ZodLiteral<"error">;
|
|
117
95
|
code: z.ZodEnum<{
|
|
118
96
|
tool: "tool";
|
|
97
|
+
connection: "connection";
|
|
119
98
|
stt: "stt";
|
|
120
99
|
llm: "llm";
|
|
121
100
|
tts: "tts";
|
|
122
101
|
protocol: "protocol";
|
|
123
|
-
connection: "connection";
|
|
124
102
|
audio: "audio";
|
|
125
103
|
internal: "internal";
|
|
126
104
|
}>;
|
|
@@ -188,10 +166,6 @@ export declare const ServerMessageSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
|
188
166
|
toolCallId: z.ZodString;
|
|
189
167
|
toolName: z.ZodString;
|
|
190
168
|
args: z.ZodRecord<z.ZodString, z.ZodUnknown>;
|
|
191
|
-
}, z.core.$strip>, z.ZodObject<{
|
|
192
|
-
type: z.ZodLiteral<"tool_call_update">;
|
|
193
|
-
toolCallId: z.ZodString;
|
|
194
|
-
data: z.ZodString;
|
|
195
169
|
}, z.core.$strip>, z.ZodObject<{
|
|
196
170
|
type: z.ZodLiteral<"tool_call_done">;
|
|
197
171
|
toolCallId: z.ZodString;
|
|
@@ -208,11 +182,11 @@ export declare const ServerMessageSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
|
208
182
|
type: z.ZodLiteral<"error">;
|
|
209
183
|
code: z.ZodEnum<{
|
|
210
184
|
tool: "tool";
|
|
185
|
+
connection: "connection";
|
|
211
186
|
stt: "stt";
|
|
212
187
|
llm: "llm";
|
|
213
188
|
tts: "tts";
|
|
214
189
|
protocol: "protocol";
|
|
215
|
-
connection: "connection";
|
|
216
190
|
audio: "audio";
|
|
217
191
|
internal: "internal";
|
|
218
192
|
}>;
|