@alexkroman1/aai 0.9.3 → 0.10.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.
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,519 @@
1
+ import { t as createDirectExecutor } from "./direct-executor-Ca0wt5H0.js";
2
+ import { createSqliteKv } from "./sqlite-kv.js";
3
+ import { createSqliteVectorStore, createTestEmbedFn } from "./sqlite-vector.js";
4
+ import { mkdtempSync } from "node:fs";
5
+ import { tmpdir } from "node:os";
6
+ import { join } from "node:path";
7
+ //#region _mock-ws.ts
8
+ /**
9
+ * A mock WebSocket implementation for testing.
10
+ *
11
+ * Extends `EventTarget` to simulate WebSocket behavior without a real
12
+ * network connection. Records all sent messages in the {@link sent}
13
+ * array and provides helper methods to simulate incoming messages,
14
+ * connection events, and errors.
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * const ws = new MockWebSocket("wss://example.com");
19
+ * ws.send(JSON.stringify({ type: "ping" }));
20
+ * ws.simulateMessage(JSON.stringify({ type: "pong" }));
21
+ * assertEquals(ws.sentJson(), [{ type: "ping" }]);
22
+ * ```
23
+ */
24
+ var MockWebSocket = class MockWebSocket extends EventTarget {
25
+ static CONNECTING = 0;
26
+ static OPEN = 1;
27
+ static CLOSING = 2;
28
+ static CLOSED = 3;
29
+ readyState = MockWebSocket.CONNECTING;
30
+ binaryType = "arraybuffer";
31
+ /** All messages passed to {@link send}, in order. */
32
+ sent = [];
33
+ url;
34
+ /**
35
+ * Create a new MockWebSocket.
36
+ *
37
+ * Automatically transitions to `OPEN` state on the next microtask,
38
+ * dispatching an `"open"` event.
39
+ *
40
+ * @param url - The WebSocket URL.
41
+ * @param _protocols - Ignored; accepted for API compatibility.
42
+ */
43
+ constructor(url, _protocols) {
44
+ super();
45
+ this.url = typeof url === "string" ? url : url.toString();
46
+ queueMicrotask(() => {
47
+ if (this.readyState === MockWebSocket.CONNECTING) {
48
+ this.readyState = MockWebSocket.OPEN;
49
+ this.dispatchEvent(new Event("open"));
50
+ }
51
+ });
52
+ }
53
+ addEventListener(type, listener) {
54
+ super.addEventListener(type, listener);
55
+ }
56
+ /**
57
+ * Record a sent message without transmitting it.
58
+ *
59
+ * @param data - The message data to record.
60
+ */
61
+ send(data) {
62
+ this.sent.push(data);
63
+ }
64
+ /**
65
+ * Transition to `CLOSED` state and dispatch a `"close"` event.
66
+ *
67
+ * @param code - The close code (defaults to 1000).
68
+ * @param _reason - Ignored; accepted for API compatibility.
69
+ */
70
+ close(code, _reason) {
71
+ this.readyState = MockWebSocket.CLOSED;
72
+ this.dispatchEvent(Object.assign(new Event("close"), { code: code ?? 1e3 }));
73
+ }
74
+ /**
75
+ * Simulate receiving a message from the server.
76
+ *
77
+ * @param data - The message data (string or binary).
78
+ */
79
+ simulateMessage(data) {
80
+ this.dispatchEvent(new MessageEvent("message", { data }));
81
+ }
82
+ /** Transition to `OPEN` state and dispatch an `"open"` event. */
83
+ open() {
84
+ this.readyState = MockWebSocket.OPEN;
85
+ this.dispatchEvent(new Event("open"));
86
+ }
87
+ /**
88
+ * Shorthand for {@link simulateMessage}.
89
+ *
90
+ * @param data - The message data to dispatch.
91
+ */
92
+ msg(data) {
93
+ this.simulateMessage(data);
94
+ }
95
+ /**
96
+ * Simulate a connection close from the server.
97
+ *
98
+ * @param code - The close code (defaults to 1000).
99
+ */
100
+ disconnect(code = 1e3) {
101
+ this.dispatchEvent(Object.assign(new Event("close"), { code }));
102
+ }
103
+ /** Dispatch an `"error"` event on this socket. */
104
+ error() {
105
+ this.dispatchEvent(new Event("error"));
106
+ }
107
+ /**
108
+ * Return all sent string messages parsed as JSON objects.
109
+ *
110
+ * Binary messages are filtered out.
111
+ *
112
+ * @returns An array of parsed JSON objects from sent string messages.
113
+ */
114
+ sentJson() {
115
+ return this.sent.filter((d) => typeof d === "string").map((s) => JSON.parse(s));
116
+ }
117
+ };
118
+ const g = globalThis;
119
+ /**
120
+ * Replace `globalThis.WebSocket` with {@link MockWebSocket} for testing.
121
+ *
122
+ * Returns a handle that tracks all created mock sockets and can restore the
123
+ * original `WebSocket` constructor. Supports the `using` declaration via
124
+ * `Symbol.dispose` for automatic cleanup.
125
+ *
126
+ * @returns An object with `created` array, `lastWs` getter, `restore()`, and `[Symbol.dispose]()`.
127
+ *
128
+ * @example
129
+ * ```ts
130
+ * using mock = installMockWebSocket();
131
+ * const session = new Session("wss://example.com");
132
+ * const ws = mock.lastWs!;
133
+ * ws.simulateMessage(JSON.stringify({ type: "ready" }));
134
+ * // mock automatically restores WebSocket when disposed
135
+ * ```
136
+ */
137
+ function installMockWebSocket() {
138
+ const saved = globalThis.WebSocket;
139
+ const created = [];
140
+ g.WebSocket = class extends MockWebSocket {
141
+ constructor(url, protocols) {
142
+ super(url, protocols);
143
+ created.push(this);
144
+ }
145
+ };
146
+ return {
147
+ created,
148
+ get lastWs() {
149
+ return created.at(-1) ?? null;
150
+ },
151
+ restore() {
152
+ globalThis.WebSocket = saved;
153
+ },
154
+ [Symbol.dispose]() {
155
+ this.restore();
156
+ }
157
+ };
158
+ }
159
+ //#endregion
160
+ //#region testing.ts
161
+ /**
162
+ * Testing utilities for AAI agents.
163
+ *
164
+ * Provides a test harness for unit-testing agents without audio, network,
165
+ * or an LLM. Use {@link createTestHarness} to create a harness from a
166
+ * `defineAgent()` result, then drive tool calls and multi-turn conversations.
167
+ *
168
+ * @example
169
+ * ```ts
170
+ * import { describe, expect, test } from "vitest";
171
+ * import { createTestHarness } from "@alexkroman1/aai/testing";
172
+ * import agent from "./agent.ts";
173
+ *
174
+ * describe("my agent", () => {
175
+ * test("greet tool returns greeting", async () => {
176
+ * const t = createTestHarness(agent);
177
+ * const result = await t.executeTool("greet", { name: "Alice" });
178
+ * expect(result).toBe("Hello, Alice!");
179
+ * });
180
+ *
181
+ * test("multi-turn conversation", async () => {
182
+ * const t = createTestHarness(agent);
183
+ * const turn1 = await t.turn("Add a pizza", [
184
+ * { tool: "add_pizza", args: { size: "large", crust: "regular", toppings: ["pepperoni"], quantity: 1 } },
185
+ * ]);
186
+ * expect(turn1).toHaveCalledTool("add_pizza");
187
+ *
188
+ * const turn2 = await t.turn("View my order", [
189
+ * { tool: "view_order", args: {} },
190
+ * ]);
191
+ * expect(turn2).toHaveCalledTool("view_order");
192
+ * });
193
+ * });
194
+ * ```
195
+ *
196
+ * @packageDocumentation
197
+ */
198
+ /**
199
+ * Result of a simulated turn via {@link TestHarness.turn}.
200
+ *
201
+ * Contains all tool calls that were executed and provides assertion helpers
202
+ * for verifying agent behavior in tests.
203
+ *
204
+ * @example
205
+ * ```ts
206
+ * const result = await t.turn("search for flights", [
207
+ * { tool: "search_flights", args: { destination: "NYC" } },
208
+ * ]);
209
+ *
210
+ * // Check if a tool was called
211
+ * expect(result).toHaveCalledTool("search_flights");
212
+ *
213
+ * // Check tool was called with specific args
214
+ * expect(result).toHaveCalledTool("search_flights", { destination: "NYC" });
215
+ *
216
+ * // Access raw tool call data
217
+ * expect(result.toolCalls[0].result).toContain("JFK");
218
+ * ```
219
+ *
220
+ * @public
221
+ */
222
+ var TurnResult = class {
223
+ /** The user text that initiated this turn. */
224
+ text;
225
+ /** All tool calls executed during this turn, in order. */
226
+ toolCalls;
227
+ /** Convenience accessor: just the result strings from each tool call. */
228
+ toolResults;
229
+ /** @internal */
230
+ constructor(text, toolCalls) {
231
+ this.text = text;
232
+ this.toolCalls = toolCalls;
233
+ this.toolResults = toolCalls.map((tc) => tc.result);
234
+ }
235
+ /**
236
+ * Check whether a tool was called during this turn.
237
+ *
238
+ * When `args` is provided, checks that at least one call to the named tool
239
+ * contains all specified key-value pairs (partial match).
240
+ *
241
+ * @param toolName - The tool name to look for.
242
+ * @param args - Optional partial args to match against.
243
+ * @returns `true` if a matching tool call was found.
244
+ *
245
+ * @example
246
+ * ```ts
247
+ * result.toHaveCalledTool("add_pizza"); // any call
248
+ * result.toHaveCalledTool("add_pizza", { size: "large" }); // partial match
249
+ * ```
250
+ */
251
+ toHaveCalledTool(toolName, args) {
252
+ return this.toolCalls.some((tc) => {
253
+ if (tc.toolName !== toolName) return false;
254
+ if (!args) return true;
255
+ return Object.entries(args).every(([key, value]) => JSON.stringify(tc.args[key]) === JSON.stringify(value));
256
+ });
257
+ }
258
+ /**
259
+ * Get all calls to a specific tool during this turn.
260
+ *
261
+ * @param toolName - The tool name to filter by.
262
+ * @returns Array of matching tool calls (may be empty).
263
+ */
264
+ getToolCalls(toolName) {
265
+ return this.toolCalls.filter((tc) => tc.toolName === toolName);
266
+ }
267
+ /**
268
+ * Get the parsed JSON result of the first call to a specific tool.
269
+ *
270
+ * Throws if the tool was not called during this turn.
271
+ *
272
+ * @typeParam T - The expected shape of the parsed result.
273
+ * @param toolName - The tool name to look up.
274
+ * @returns The parsed result, cast to `T`.
275
+ *
276
+ * @example
277
+ * ```ts
278
+ * const order = turn.toolResult<{ pizzas: Pizza[]; total: string }>("view_order");
279
+ * expect(order.pizzas).toHaveLength(2);
280
+ * ```
281
+ */
282
+ toolResult(toolName) {
283
+ const call = this.toolCalls.find((tc) => tc.toolName === toolName);
284
+ if (!call) throw new Error(`Tool "${toolName}" was not called during this turn`);
285
+ return JSON.parse(call.result);
286
+ }
287
+ };
288
+ /**
289
+ * Test harness for unit-testing AAI agents without audio, network, or LLM.
290
+ *
291
+ * Created via {@link createTestHarness}. Maintains conversation state across
292
+ * turns, executes tools against the real agent code, and records all tool
293
+ * calls for assertions.
294
+ *
295
+ * @example
296
+ * ```ts
297
+ * import { createTestHarness } from "@alexkroman1/aai/testing";
298
+ * import agent from "./agent.ts";
299
+ *
300
+ * const t = createTestHarness(agent);
301
+ *
302
+ * // Execute a single tool
303
+ * const result = await t.executeTool("greet", { name: "Alice" });
304
+ *
305
+ * // Simulate a full turn with tool calls
306
+ * const turn = await t.turn("hello", [
307
+ * { tool: "greet", args: { name: "Alice" } },
308
+ * ]);
309
+ * expect(turn).toHaveCalledTool("greet");
310
+ * ```
311
+ *
312
+ * @public
313
+ */
314
+ var TestHarness = class {
315
+ /** @internal */
316
+ _executor;
317
+ /** @internal */
318
+ _sessionId;
319
+ _messages = [];
320
+ _onStepCalls = [];
321
+ _onTurnCalls = [];
322
+ _connected = false;
323
+ /** @internal */
324
+ constructor(executor, sessionId) {
325
+ this._executor = executor;
326
+ this._sessionId = sessionId;
327
+ }
328
+ /** Conversation messages accumulated across turns. */
329
+ get messages() {
330
+ return this._messages;
331
+ }
332
+ /** All `onStep` hook invocations recorded so far. */
333
+ get steps() {
334
+ return this._onStepCalls;
335
+ }
336
+ /** All `onTurn` hook invocations (the text argument) recorded so far. */
337
+ get turns() {
338
+ return this._onTurnCalls;
339
+ }
340
+ /**
341
+ * Fire the `onConnect` lifecycle hook.
342
+ *
343
+ * Called automatically on the first {@link turn} call if not called manually.
344
+ */
345
+ async connect() {
346
+ if (this._connected) return;
347
+ this._connected = true;
348
+ await this._executor.hookInvoker.onConnect(this._sessionId);
349
+ }
350
+ /**
351
+ * Fire the `onDisconnect` lifecycle hook and clean up session state.
352
+ */
353
+ async disconnect() {
354
+ if (!this._connected) return;
355
+ this._connected = false;
356
+ await this._executor.hookInvoker.onDisconnect(this._sessionId);
357
+ }
358
+ /**
359
+ * Execute a single tool by name with the given arguments.
360
+ *
361
+ * The tool runs with full agent context (env, state, kv, vector, messages).
362
+ * The call is **not** recorded in conversation history — use {@link turn}
363
+ * for that.
364
+ *
365
+ * @param toolName - The tool to execute.
366
+ * @param args - Arguments to pass to the tool.
367
+ * @returns The tool's string result.
368
+ *
369
+ * @example
370
+ * ```ts
371
+ * const result = await t.executeTool("get_weather", { city: "London" });
372
+ * const data = JSON.parse(result);
373
+ * expect(data.temp).toBeDefined();
374
+ * ```
375
+ */
376
+ async executeTool(toolName, args = {}) {
377
+ return this._executor.executeTool(toolName, args, this._sessionId, this._messages);
378
+ }
379
+ /**
380
+ * Simulate a user turn: add the user message, execute the given tool calls
381
+ * in sequence, and record everything.
382
+ *
383
+ * This is the primary method for testing agent behavior. It:
384
+ * 1. Fires `onConnect` if this is the first turn
385
+ * 2. Adds the user message to conversation history
386
+ * 3. Fires the `onTurn` hook
387
+ * 4. Executes each tool call in order, firing `onStep` for each
388
+ * 5. Returns a {@link TurnResult} with assertion helpers
389
+ *
390
+ * @param text - The user's spoken/typed input.
391
+ * @param toolCalls - Tool calls to execute (simulating what the LLM would invoke).
392
+ * @returns A {@link TurnResult} with recorded tool calls and assertion methods.
393
+ *
394
+ * @example
395
+ * ```ts
396
+ * const turn = await t.turn("Add pepperoni pizza", [
397
+ * { tool: "add_pizza", args: { size: "large", crust: "regular", toppings: ["pepperoni"], quantity: 1 } },
398
+ * ]);
399
+ * expect(turn).toHaveCalledTool("add_pizza", { size: "large" });
400
+ * expect(turn.toolCalls[0].result).toContain("$14.99");
401
+ * ```
402
+ */
403
+ async turn(text, toolCalls = []) {
404
+ await this.connect();
405
+ this._messages.push({
406
+ role: "user",
407
+ content: text
408
+ });
409
+ this._onTurnCalls.push(text);
410
+ await this._executor.hookInvoker.onTurn(this._sessionId, text);
411
+ const recorded = [];
412
+ for (let i = 0; i < toolCalls.length; i++) {
413
+ const tc = toolCalls[i];
414
+ const result = await this._executor.executeTool(tc.tool, tc.args, this._sessionId, this._messages);
415
+ recorded.push({
416
+ toolName: tc.tool,
417
+ args: tc.args,
418
+ result
419
+ });
420
+ this._messages.push({
421
+ role: "tool",
422
+ content: result
423
+ });
424
+ const step = {
425
+ stepNumber: i + 1,
426
+ toolCalls: [{
427
+ toolName: tc.tool,
428
+ args: tc.args
429
+ }],
430
+ text: ""
431
+ };
432
+ this._onStepCalls.push(step);
433
+ await this._executor.hookInvoker.onStep(this._sessionId, step);
434
+ }
435
+ return new TurnResult(text, recorded);
436
+ }
437
+ /**
438
+ * Add a user message to conversation history without executing tools.
439
+ *
440
+ * Useful for setting up conversation context before a turn.
441
+ */
442
+ addUserMessage(text) {
443
+ this._messages.push({
444
+ role: "user",
445
+ content: text
446
+ });
447
+ }
448
+ /**
449
+ * Add an assistant message to conversation history.
450
+ *
451
+ * Useful for simulating prior assistant responses in multi-turn tests.
452
+ */
453
+ addAssistantMessage(text) {
454
+ this._messages.push({
455
+ role: "assistant",
456
+ content: text
457
+ });
458
+ }
459
+ /**
460
+ * Reset conversation state: clears messages, step/turn history.
461
+ *
462
+ * Does **not** reset KV or vector store — create a new harness for that.
463
+ */
464
+ reset() {
465
+ this._messages = [];
466
+ this._onStepCalls = [];
467
+ this._onTurnCalls = [];
468
+ }
469
+ };
470
+ /**
471
+ * Create a SQLite-vec backed vector store with deterministic test embeddings.
472
+ * Uses a temp directory that is unique per call for test isolation.
473
+ */
474
+ function createTestVectorStore() {
475
+ return createSqliteVectorStore({
476
+ path: join(mkdtempSync(join(tmpdir(), "aai-test-vec-")), "vectors.db"),
477
+ embedFn: createTestEmbedFn()
478
+ });
479
+ }
480
+ /**
481
+ * Create a test harness for unit-testing an agent.
482
+ *
483
+ * The harness wraps the agent's tool definitions and lifecycle hooks,
484
+ * providing a simple API for executing tools and simulating multi-turn
485
+ * conversations — all without audio, network, or an LLM.
486
+ *
487
+ * @param agent - The agent definition returned by `defineAgent()`.
488
+ * @param options - Optional environment, KV, and vector store overrides.
489
+ * @returns A {@link TestHarness} instance.
490
+ *
491
+ * @example
492
+ * ```ts
493
+ * import { createTestHarness } from "@alexkroman1/aai/testing";
494
+ * import agent from "./agent.ts";
495
+ *
496
+ * const t = createTestHarness(agent);
497
+ * const result = await t.executeTool("my_tool", { key: "value" });
498
+ * ```
499
+ *
500
+ * @example With environment variables
501
+ * ```ts
502
+ * const t = createTestHarness(agent, {
503
+ * env: { API_KEY: "test-key" },
504
+ * });
505
+ * ```
506
+ *
507
+ * @public
508
+ */
509
+ function createTestHarness(agent, options = {}) {
510
+ const { env = {}, kv = createSqliteKv({ path: ":memory:" }), vector = createTestVectorStore() } = options;
511
+ return new TestHarness(createDirectExecutor({
512
+ agent,
513
+ env,
514
+ kv,
515
+ vector
516
+ }), `test-${Date.now()}`);
517
+ }
518
+ //#endregion
519
+ export { installMockWebSocket as a, MockWebSocket as i, TurnResult as n, createTestHarness as r, TestHarness as t };