@backendkit-labs/agent-testkit 0.0.0-stage → 0.1.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/README.md CHANGED
@@ -1,3 +1,126 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
1
+ # @backendkit-labs/agent-testkit
2
+
3
+ Test doubles for agents built on `@backendkit-labs/agent-core`. Install it as a **dev dependency**; it needs no API key and no network.
4
+
5
+ ```bash
6
+ npm i -D @backendkit-labs/agent-testkit
7
+ ```
8
+
9
+ In a project made with `create-bk-agent`, `npx create-bk-agent add testing` installs it together with jest and drops in an example test.
10
+
11
+ It does not depend on a test runner: assertions throw plain `Error`s, so it works with jest, vitest or `node:test`.
12
+
13
+ ## What is in it
14
+
15
+ | Piece | Use |
16
+ |---|---|
17
+ | `scriptedProvider(turns)` | an `LLMProvider` that answers from a script (text, tool calls, optional token metrics); `provider.calls` keeps what each call received |
18
+ | `mockTransport()` | a `Transport` that records events: `events`, `eventsOf('tool_result')`, `text()`, `clear()` |
19
+ | `startMockLlmServer({ script })` | an HTTP server that speaks the OpenAI chat-completions streaming protocol and misbehaves on demand |
20
+ | `RecordingProvider(inner, sink)` | wraps a real provider and writes every call (request, chunks, tool calls, metrics, errors, timings) as JSONL |
21
+ | `ReplayProvider(file, options)` | replays a recording with no network; a sidecar file or `overrides` injects faults the recording does not contain |
22
+ | `world.fileEquals / untouched / processGone` | assertions about the real system, not about what the agent claims |
23
+
24
+ ## How to test an agent
25
+
26
+ ### 1. Logic of your agent: a scripted provider
27
+
28
+ ```ts
29
+ import { createBaseEngine } from '@backendkit-labs/agent-core';
30
+ import { scriptedProvider, mockTransport } from '@backendkit-labs/agent-testkit';
31
+
32
+ const t = mockTransport();
33
+ const engine = createBaseEngine({
34
+ providers: { mock: scriptedProvider([
35
+ { tool_calls: [{ id: 'c1', name: 'read_file', args: { path: 'a.txt' } }] },
36
+ { content: 'The file says hello.' },
37
+ ]) },
38
+ defaultProvider: 'mock', agents: [myAgent], defaultAgent: myAgent.id,
39
+ tools: [readFileTool], transport: t.transport, workingDir: dir, noQA: true, iterationMode: 'auto',
40
+ });
41
+
42
+ await engine.run('what is in a.txt?');
43
+ expect(t.eventsOf('tool_result')[0].success).toBe(true);
44
+ expect(t.text()).toContain('hello');
45
+ ```
46
+
47
+ ### 2. Failures of a real provider: the mock server
48
+
49
+ Point the real provider at the server and script what goes wrong:
50
+
51
+ ```ts
52
+ import { OpenAICompatibleProvider } from '@backendkit-labs/agent-providers';
53
+ import { startMockLlmServer } from '@backendkit-labs/agent-testkit';
54
+
55
+ const server = await startMockLlmServer({ script: [
56
+ { kind: 'status', status: 429 }, // retried by the provider
57
+ { kind: 'text', content: 'fine now' },
58
+ ] });
59
+ const provider = new OpenAICompatibleProvider({
60
+ apiKey: 'test', baseUrl: server.url, model: 'mock',
61
+ retryBaseDelayMs: 1, // keep backoff out of your test time
62
+ });
63
+ // ... run the agent, then:
64
+ expect(server.requests).toHaveLength(2); // what the client really sent is captured
65
+ await server.close();
66
+ ```
67
+
68
+ Behaviours (one is consumed per request; when the script is used up the server answers `500 mock script exhausted`):
69
+
70
+ | `kind` | What happens |
71
+ |---|---|
72
+ | `text` | a streamed answer (`chunkSize`, `usage`, `delayMs`) |
73
+ | `tool_call` | an assistant turn calling `name` with `args` |
74
+ | `status` | an HTTP error before streaming (429, 500, 503…) |
75
+ | `reset` | the connection is dropped |
76
+ | `stream_error` | the stream starts, then an error event arrives inside it (what OpenAI/DeepSeek send when something fails after the 200); a provider must not retry this |
77
+ | `cut` | the stream starts, then the connection is reset. Plain `fetch` sees the failure on every platform; the OpenAI SDK's HTTP client was observed to hang on Linux (not abortable), so test mid-stream failures of a provider with `stream_error` and use `cut` only with a client you have checked |
78
+ | `corrupt` | a 200 stream whose first chunk is not JSON |
79
+ | `stall` | never answers (to test aborts and timeouts) |
80
+ | `overflow` | the exact 400 DeepSeek/OpenAI send when the prompt does not fit the context window |
81
+
82
+ Instead of a script, `random: { seed, weights }` picks a behaviour per request from a seeded generator: the same seed plays the same sequence.
83
+
84
+ ### 3. Record a real run once, replay it forever
85
+
86
+ ```ts
87
+ import { RecordingProvider, ReplayProvider } from '@backendkit-labs/agent-testkit';
88
+
89
+ // once, with the real provider (or the mock server):
90
+ const provider = new RecordingProvider(realProvider, 'fixtures/refactor.jsonl');
91
+
92
+ // in every test run, no network, no key:
93
+ const replay = new ReplayProvider('fixtures/refactor.jsonl', { verifyRequests: true });
94
+ ```
95
+
96
+ `verifyRequests` fails the replay when your code starts sending something different from what was recorded, so a changed prompt or history shows up as a clear message instead of a silently different answer.
97
+
98
+ Recordings contain the whole conversation: review them before committing, or pass `redact` to `RecordingProvider`.
99
+
100
+ **Injecting faults.** Put `fixtures/refactor.override.json` next to the recording (it is picked up automatically), or pass `overrides`. Keys are the 1-based number of the `chat()` invocation:
101
+
102
+ ```json
103
+ { "inject": {
104
+ "2": { "error": { "message": "Too Many Requests", "status": 429 } },
105
+ "4": { "cancel": true },
106
+ "5": { "hang": true }
107
+ } }
108
+ ```
109
+
110
+ An injected invocation does not use up a recorded call: the next invocation plays the next recorded one, which is what a retry, or the engine's recovery from a context overflow, would see live. `hang` waits until the call's `signal` is aborted.
111
+
112
+ ### 4. Check the world, not the agent's report
113
+
114
+ ```ts
115
+ import { world } from '@backendkit-labs/agent-testkit';
116
+
117
+ const guard = world.untouched([join(dir, '.env')]); // snapshot now
118
+ await engine.run('refactor src/');
119
+ guard.verify(); // throws if .env changed, appeared or vanished
120
+ world.fileEquals(join(dir, 'out.txt'), 'done');
121
+ await world.processGone(pid); // the background process really stopped
122
+ ```
123
+
124
+ ## Not here yet
125
+
126
+ An Anthropic-format mock server and a fake clock are planned (SPEC-0007). Jest's own fake timers cover deadlines and TTLs meanwhile.
@@ -0,0 +1,12 @@
1
+ export { scriptedProvider } from './scriptedProvider';
2
+ export type { Turn, ScriptedProviderOptions } from './scriptedProvider';
3
+ export { mockTransport } from './mockTransport';
4
+ export type { MockTransport } from './mockTransport';
5
+ export { world, fileEquals, untouched, processGone } from './world';
6
+ export { startMockLlmServer } from './mockLlmServer';
7
+ export type { MockBehavior, MockLlmServer, MockLlmServerOptions, CapturedRequest } from './mockLlmServer';
8
+ export { RecordingProvider, serializeError, deserializeError } from './recording';
9
+ export type { RecordedCall, RecordedEvent, RecordedError, RecordingSink, RecordingOptions } from './recording';
10
+ export { ReplayProvider, readRecording } from './replay';
11
+ export type { ReplayOptions, ReplayOverrides, ReplayFault } from './replay';
12
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,YAAY,EAAE,IAAI,EAAE,uBAAuB,EAAE,MAAM,oBAAoB,CAAC;AACxE,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,YAAY,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AACrD,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AACpE,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AACrD,YAAY,EAAE,YAAY,EAAE,aAAa,EAAE,oBAAoB,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAC1G,OAAO,EAAE,iBAAiB,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAClF,YAAY,EAAE,YAAY,EAAE,aAAa,EAAE,aAAa,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/G,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzD,YAAY,EAAE,aAAa,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.readRecording = exports.ReplayProvider = exports.deserializeError = exports.serializeError = exports.RecordingProvider = exports.startMockLlmServer = exports.processGone = exports.untouched = exports.fileEquals = exports.world = exports.mockTransport = exports.scriptedProvider = void 0;
4
+ var scriptedProvider_1 = require("./scriptedProvider");
5
+ Object.defineProperty(exports, "scriptedProvider", { enumerable: true, get: function () { return scriptedProvider_1.scriptedProvider; } });
6
+ var mockTransport_1 = require("./mockTransport");
7
+ Object.defineProperty(exports, "mockTransport", { enumerable: true, get: function () { return mockTransport_1.mockTransport; } });
8
+ var world_1 = require("./world");
9
+ Object.defineProperty(exports, "world", { enumerable: true, get: function () { return world_1.world; } });
10
+ Object.defineProperty(exports, "fileEquals", { enumerable: true, get: function () { return world_1.fileEquals; } });
11
+ Object.defineProperty(exports, "untouched", { enumerable: true, get: function () { return world_1.untouched; } });
12
+ Object.defineProperty(exports, "processGone", { enumerable: true, get: function () { return world_1.processGone; } });
13
+ var mockLlmServer_1 = require("./mockLlmServer");
14
+ Object.defineProperty(exports, "startMockLlmServer", { enumerable: true, get: function () { return mockLlmServer_1.startMockLlmServer; } });
15
+ var recording_1 = require("./recording");
16
+ Object.defineProperty(exports, "RecordingProvider", { enumerable: true, get: function () { return recording_1.RecordingProvider; } });
17
+ Object.defineProperty(exports, "serializeError", { enumerable: true, get: function () { return recording_1.serializeError; } });
18
+ Object.defineProperty(exports, "deserializeError", { enumerable: true, get: function () { return recording_1.deserializeError; } });
19
+ var replay_1 = require("./replay");
20
+ Object.defineProperty(exports, "ReplayProvider", { enumerable: true, get: function () { return replay_1.ReplayProvider; } });
21
+ Object.defineProperty(exports, "readRecording", { enumerable: true, get: function () { return replay_1.readRecording; } });
22
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,uDAAsD;AAA7C,oHAAA,gBAAgB,OAAA;AAEzB,iDAAgD;AAAvC,8GAAA,aAAa,OAAA;AAEtB,iCAAoE;AAA3D,8FAAA,KAAK,OAAA;AAAE,mGAAA,UAAU,OAAA;AAAE,kGAAA,SAAS,OAAA;AAAE,oGAAA,WAAW,OAAA;AAClD,iDAAqD;AAA5C,mHAAA,kBAAkB,OAAA;AAE3B,yCAAkF;AAAzE,8GAAA,iBAAiB,OAAA;AAAE,2GAAA,cAAc,OAAA;AAAE,6GAAA,gBAAgB,OAAA;AAE5D,mCAAyD;AAAhD,wGAAA,cAAc,OAAA;AAAE,uGAAA,aAAa,OAAA"}
@@ -0,0 +1,100 @@
1
+ import { type IncomingHttpHeaders } from 'node:http';
2
+ /** What the server does with one request. Behaviours are consumed in order, one per request. */
3
+ export type MockBehavior =
4
+ /** A normal answer, streamed as SSE in small chunks. */
5
+ {
6
+ kind: 'text';
7
+ content: string;
8
+ chunkSize?: number;
9
+ usage?: {
10
+ promptTokens: number;
11
+ completionTokens: number;
12
+ };
13
+ delayMs?: number;
14
+ }
15
+ /** An assistant turn that calls a tool (optionally with some text first). */
16
+ | {
17
+ kind: 'tool_call';
18
+ name: string;
19
+ args: Record<string, unknown>;
20
+ id?: string;
21
+ content?: string;
22
+ delayMs?: number;
23
+ }
24
+ /** An HTTP error before any streaming (429, 500, 503…). */
25
+ | {
26
+ kind: 'status';
27
+ status: number;
28
+ message?: string;
29
+ headers?: Record<string, string>;
30
+ delayMs?: number;
31
+ }
32
+ /** The connection is dropped before any response. */
33
+ | {
34
+ kind: 'reset';
35
+ delayMs?: number;
36
+ }
37
+ /** The stream starts, `afterChunks` content chunks arrive, then the connection is cut. */
38
+ | {
39
+ kind: 'cut';
40
+ content?: string;
41
+ afterChunks?: number;
42
+ delayMs?: number;
43
+ }
44
+ /** The stream starts, `afterChunks` content chunks arrive, then the server sends an error event inside the stream (as OpenAI and DeepSeek do when something fails after the 200). */
45
+ | {
46
+ kind: 'stream_error';
47
+ message?: string;
48
+ content?: string;
49
+ afterChunks?: number;
50
+ delayMs?: number;
51
+ }
52
+ /** A 200 SSE response whose first data line is not valid JSON. */
53
+ | {
54
+ kind: 'corrupt';
55
+ delayMs?: number;
56
+ }
57
+ /** The request is accepted and never answered (until the client aborts or the server closes). */
58
+ | {
59
+ kind: 'stall';
60
+ }
61
+ /** The 400 DeepSeek/OpenAI send when the prompt does not fit the context window. */
62
+ | {
63
+ kind: 'overflow';
64
+ limit?: number;
65
+ requested?: number;
66
+ delayMs?: number;
67
+ };
68
+ export interface CapturedRequest {
69
+ method: string;
70
+ url: string;
71
+ headers: IncomingHttpHeaders;
72
+ /** Parsed JSON body (undefined when it was not JSON). */
73
+ body: any;
74
+ }
75
+ export interface MockLlmServerOptions {
76
+ /** Behaviours to play, one per request. When used up the server answers 500 "mock script exhausted". */
77
+ script?: MockBehavior[];
78
+ /** Instead of a script: pick a behaviour per request from a seeded generator (reproducible runs). */
79
+ random?: {
80
+ seed: number;
81
+ weights?: Partial<Record<Exclude<MockBehavior['kind'], 'stall' | 'tool_call' | 'stream_error'>, number>>;
82
+ };
83
+ port?: number;
84
+ }
85
+ export interface MockLlmServer {
86
+ /** Base URL to give a provider (`http://127.0.0.1:<port>/v1`). */
87
+ readonly url: string;
88
+ /** Every request received, in order. */
89
+ readonly requests: CapturedRequest[];
90
+ /** Scripted behaviours not consumed yet. */
91
+ remaining(): number;
92
+ close(): Promise<void>;
93
+ }
94
+ /**
95
+ * An HTTP server that speaks the OpenAI chat-completions streaming protocol (what OpenAI, DeepSeek and
96
+ * most compatible providers use) and misbehaves on demand. Point a real provider at `server.url` to
97
+ * test retries, aborts, mid-stream failures and context overflow without a network or an API key.
98
+ */
99
+ export declare function startMockLlmServer(options?: MockLlmServerOptions): Promise<MockLlmServer>;
100
+ //# sourceMappingURL=mockLlmServer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mockLlmServer.d.ts","sourceRoot":"","sources":["../src/mockLlmServer.ts"],"names":[],"mappings":"A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AG,OAAO,CAAC,aAAa,CAAC,CA2CnG"}
@@ -0,0 +1,206 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.startMockLlmServer = startMockLlmServer;
4
+ const node_http_1 = require("node:http");
5
+ const DEFAULT_WEIGHTS = { text: 6, status: 2, reset: 1, cut: 1, corrupt: 1, overflow: 0 };
6
+ function mulberry32(seed) {
7
+ let a = seed >>> 0;
8
+ return () => {
9
+ a = (a + 0x6D2B79F5) >>> 0;
10
+ let t = a;
11
+ t = Math.imul(t ^ (t >>> 15), t | 1);
12
+ t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
13
+ return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
14
+ };
15
+ }
16
+ function randomBehavior(rand, weights) {
17
+ const entries = Object.entries(weights).filter(([, w]) => w > 0);
18
+ let pick = rand() * entries.reduce((n, [, w]) => n + w, 0);
19
+ let kind = entries[0]?.[0] ?? 'text';
20
+ for (const [k, w] of entries) {
21
+ if ((pick -= w) < 0) {
22
+ kind = k;
23
+ break;
24
+ }
25
+ }
26
+ switch (kind) {
27
+ case 'status': return { kind: 'status', status: [429, 500, 502, 503][Math.floor(rand() * 4)] };
28
+ case 'reset': return { kind: 'reset' };
29
+ case 'cut': return { kind: 'cut' };
30
+ case 'corrupt': return { kind: 'corrupt' };
31
+ case 'overflow': return { kind: 'overflow' };
32
+ default: return { kind: 'text', content: 'ok from the random mock' };
33
+ }
34
+ }
35
+ /**
36
+ * Drops the connection with a TCP reset. A plain `destroy()` ends it with a clean FIN on Linux, which
37
+ * some HTTP clients read as the end of the response; a reset is an error on every platform.
38
+ */
39
+ function dropConnection(req) {
40
+ const socket = req.socket;
41
+ if (typeof socket.resetAndDestroy === 'function')
42
+ socket.resetAndDestroy();
43
+ else
44
+ socket.destroy();
45
+ }
46
+ const sse = (res, data) => res.write(`data: ${typeof data === 'string' ? data : JSON.stringify(data)}\n\n`);
47
+ function chunk(delta, finish = null) {
48
+ return { id: 'chatcmpl-mock', object: 'chat.completion.chunk', created: 0, model: 'mock', choices: [{ index: 0, delta, finish_reason: finish }] };
49
+ }
50
+ function splitText(text, size) {
51
+ const parts = [];
52
+ for (let i = 0; i < text.length; i += size)
53
+ parts.push(text.slice(i, i + size));
54
+ return parts.length ? parts : [''];
55
+ }
56
+ function startStream(res) {
57
+ res.writeHead(200, { 'content-type': 'text/event-stream', 'cache-control': 'no-cache', connection: 'keep-alive' });
58
+ }
59
+ function sendError(res, status, message, headers = {}) {
60
+ res.writeHead(status, { 'content-type': 'application/json', ...headers });
61
+ res.end(JSON.stringify({ error: { message, type: status === 400 ? 'invalid_request_error' : 'api_error', param: null, code: status === 400 ? 'invalid_request_error' : null } }));
62
+ }
63
+ function wantsUsage(body) {
64
+ return !!body?.stream_options?.include_usage;
65
+ }
66
+ function play(b, req, res, body) {
67
+ const streaming = body?.stream === true;
68
+ switch (b.kind) {
69
+ case 'text': {
70
+ const usage = b.usage ?? { promptTokens: 10, completionTokens: Math.max(1, Math.ceil(b.content.length / 4)) };
71
+ if (!streaming) {
72
+ res.writeHead(200, { 'content-type': 'application/json' });
73
+ res.end(JSON.stringify({ id: 'chatcmpl-mock', object: 'chat.completion', created: 0, model: 'mock',
74
+ choices: [{ index: 0, message: { role: 'assistant', content: b.content }, finish_reason: 'stop' }],
75
+ usage: { prompt_tokens: usage.promptTokens, completion_tokens: usage.completionTokens, total_tokens: usage.promptTokens + usage.completionTokens } }));
76
+ return;
77
+ }
78
+ startStream(res);
79
+ sse(res, chunk({ role: 'assistant', content: '' }));
80
+ for (const part of splitText(b.content, b.chunkSize ?? 8))
81
+ sse(res, chunk({ content: part }));
82
+ sse(res, chunk({}, 'stop'));
83
+ if (wantsUsage(body)) {
84
+ sse(res, { id: 'chatcmpl-mock', object: 'chat.completion.chunk', created: 0, model: 'mock', choices: [],
85
+ usage: { prompt_tokens: usage.promptTokens, completion_tokens: usage.completionTokens, total_tokens: usage.promptTokens + usage.completionTokens } });
86
+ }
87
+ sse(res, '[DONE]');
88
+ res.end();
89
+ return;
90
+ }
91
+ case 'tool_call': {
92
+ const id = b.id ?? 'call_mock_1';
93
+ const args = JSON.stringify(b.args);
94
+ startStream(res);
95
+ sse(res, chunk({ role: 'assistant', content: b.content ?? null }));
96
+ sse(res, chunk({ tool_calls: [{ index: 0, id, type: 'function', function: { name: b.name, arguments: '' } }] }));
97
+ const mid = Math.ceil(args.length / 2);
98
+ sse(res, chunk({ tool_calls: [{ index: 0, function: { arguments: args.slice(0, mid) } }] }));
99
+ sse(res, chunk({ tool_calls: [{ index: 0, function: { arguments: args.slice(mid) } }] }));
100
+ sse(res, chunk({}, 'tool_calls'));
101
+ if (wantsUsage(body)) {
102
+ sse(res, { id: 'chatcmpl-mock', object: 'chat.completion.chunk', created: 0, model: 'mock', choices: [], usage: { prompt_tokens: 10, completion_tokens: 5, total_tokens: 15 } });
103
+ }
104
+ sse(res, '[DONE]');
105
+ res.end();
106
+ return;
107
+ }
108
+ case 'status':
109
+ sendError(res, b.status, b.message ?? `mock error ${b.status}`, b.headers);
110
+ return;
111
+ case 'reset':
112
+ dropConnection(req);
113
+ return;
114
+ case 'cut': {
115
+ startStream(res);
116
+ sse(res, chunk({ role: 'assistant', content: '' }));
117
+ const parts = splitText(b.content ?? 'partial answer that never finishes', 6).slice(0, b.afterChunks ?? 2);
118
+ for (const p of parts)
119
+ sse(res, chunk({ content: p }));
120
+ // Give the client a moment to read what was sent before the connection dies.
121
+ setTimeout(() => dropConnection(req), 20);
122
+ return;
123
+ }
124
+ case 'stream_error': {
125
+ startStream(res);
126
+ sse(res, chunk({ role: 'assistant', content: '' }));
127
+ for (const p of splitText(b.content ?? 'partial answer that is about to fail', 6).slice(0, b.afterChunks ?? 2))
128
+ sse(res, chunk({ content: p }));
129
+ sse(res, { error: { message: b.message ?? 'upstream failure after the stream started', type: 'server_error', code: null } });
130
+ res.end();
131
+ return;
132
+ }
133
+ case 'corrupt':
134
+ startStream(res);
135
+ sse(res, '{this is not json');
136
+ res.end();
137
+ return;
138
+ case 'stall':
139
+ return;
140
+ case 'overflow': {
141
+ const limit = b.limit ?? 1_048_576;
142
+ const requested = b.requested ?? limit + 1_352_000;
143
+ sendError(res, 400, `This model's maximum context length is ${limit} tokens. However, you requested ${requested} tokens (${requested - 16384} in the messages, 16384 in the completion). Please reduce the length of the messages or completion.`);
144
+ return;
145
+ }
146
+ }
147
+ }
148
+ /**
149
+ * An HTTP server that speaks the OpenAI chat-completions streaming protocol (what OpenAI, DeepSeek and
150
+ * most compatible providers use) and misbehaves on demand. Point a real provider at `server.url` to
151
+ * test retries, aborts, mid-stream failures and context overflow without a network or an API key.
152
+ */
153
+ async function startMockLlmServer(options = {}) {
154
+ const script = [...(options.script ?? [])];
155
+ const rand = options.random ? mulberry32(options.random.seed) : undefined;
156
+ const weights = { ...DEFAULT_WEIGHTS, ...(options.random?.weights ?? {}) };
157
+ const requests = [];
158
+ const sockets = new Set();
159
+ const server = (0, node_http_1.createServer)((req, res) => {
160
+ const parts = [];
161
+ req.on('data', d => parts.push(d));
162
+ req.on('end', () => {
163
+ let body;
164
+ try {
165
+ body = JSON.parse(Buffer.concat(parts).toString('utf8'));
166
+ }
167
+ catch {
168
+ body = undefined;
169
+ }
170
+ requests.push({ method: req.method ?? 'GET', url: req.url ?? '', headers: req.headers, body });
171
+ if (req.method !== 'POST' || !(req.url ?? '').split('?')[0].endsWith('/chat/completions')) {
172
+ sendError(res, 404, `mock server: no route for ${req.method} ${req.url}`);
173
+ return;
174
+ }
175
+ const behavior = rand ? randomBehavior(rand, weights) : script.shift();
176
+ if (!behavior) {
177
+ sendError(res, 500, 'mock script exhausted');
178
+ return;
179
+ }
180
+ const delay = 'delayMs' in behavior ? behavior.delayMs ?? 0 : 0;
181
+ if (delay > 0)
182
+ setTimeout(() => play(behavior, req, res, body), delay);
183
+ else
184
+ play(behavior, req, res, body);
185
+ });
186
+ });
187
+ server.on('connection', s => { sockets.add(s); s.on('close', () => sockets.delete(s)); });
188
+ await new Promise((resolve, reject) => {
189
+ server.once('error', reject);
190
+ server.listen(options.port ?? 0, '127.0.0.1', () => resolve());
191
+ });
192
+ const address = server.address();
193
+ if (!address || typeof address === 'string')
194
+ throw new Error('mock server: no address');
195
+ return {
196
+ url: `http://127.0.0.1:${address.port}/v1`,
197
+ requests,
198
+ remaining: () => script.length,
199
+ close: () => new Promise(resolve => {
200
+ for (const s of sockets)
201
+ s.destroy();
202
+ server.close(() => resolve());
203
+ }),
204
+ };
205
+ }
206
+ //# sourceMappingURL=mockLlmServer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mockLlmServer.js","sourceRoot":"","sources":["../src/mockLlmServer.ts"],"names":[],"mappings":";;AAsMA,gDA2CC;AAjPD,yCAA8G;AAkD9G,MAAM,eAAe,GAAG,EAAE,IAAI,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,EAAE,CAAC;AAE1F,SAAS,UAAU,CAAC,IAAY;IAC5B,IAAI,CAAC,GAAG,IAAI,KAAK,CAAC,CAAC;IACnB,OAAO,GAAG,EAAE;QACR,CAAC,GAAG,CAAC,CAAC,GAAG,UAAU,CAAC,KAAK,CAAC,CAAC;QAC3B,IAAI,CAAC,GAAG,CAAC,CAAC;QACV,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;QACrC,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC,CAAC;QAC1C,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,UAAU,CAAC;IACjD,CAAC,CAAC;AACN,CA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YAAY,EAAE,CAAC,CAAC,EAAE,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAE1F,MAAM,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACxC,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;QAC7B,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,IAAI,CAAC,EAAE,WAAW,EAAE,GAAG,EAAE,CAAC,OAAO,EAAE,CAAC,CAAC;IACnE,CAAC,CAAC,CAAC;IACH,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,EAAE,CAAC;IACjC,IAAI,CAAC,OAAO,IAAI,OAAO,OAAO,KAAK,QAAQ;QAAE,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;IAExF,OAAO;QACH,GAAG,EAAE,oBAAoB,OAAO,CAAC,IAAI,KAAK;QAC1C,QAAQ;QACR,SAAS,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,MAAM;QAC9B,KAAK,EAAE,GAAG,EAAE,CAAC,IAAI,OAAO,CAAO,OAAO,CAAC,EAAE;YACrC,KAAK,MAAM,CAAC,IAAI,OAAO;gBAAE,CAAC,CAAC,OAAO,EAAE,CAAC;YACrC,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,OAAO,EAAE,CAAC,CAAC;QAClC,CAAC,CAAC;KACL,CAAC;AACN,CAAC"}
@@ -0,0 +1,16 @@
1
+ import type { AgentEvent, Transport } from '@backendkit-labs/agent-core';
2
+ export interface MockTransport {
3
+ transport: Transport;
4
+ /** Every event emitted so far, in order. */
5
+ events: AgentEvent[];
6
+ /** The events of one type, narrowed. */
7
+ eventsOf<T extends AgentEvent['type']>(type: T): Array<Extract<AgentEvent, {
8
+ type: T;
9
+ }>>;
10
+ /** Concatenated `token` content: the text the user would have seen streaming. */
11
+ text(): string;
12
+ clear(): void;
13
+ }
14
+ /** A transport that records what the engine emits and offers small helpers to query it. */
15
+ export declare function mockTransport(): MockTransport;
16
+ //# sourceMappingURL=mockTransport.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mockTransport.d.ts","sourceRoot":"","sources":["../src/mockTransport.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAC;AAEzE,MAAM,WAAW,aAAa;IAC1B,SAAS,EAAE,SAAS,CAAC;IACrB,4CAA4C;IAC5C,MAAM,EAAE,UAAU,EAAE,CAAC;IACrB,wCAAwC;IACxC,QAAQ,CAAC,CAAC,SAAS,UAAU,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,CAAC,GAAG,KAAK,CAAC,OAAO,CAAC,UAAU,EAAE;QAAE,IAAI,EAAE,CAAC,CAAA;KAAE,CAAC,CAAC,CAAC;IACzF,iFAAiF;IACjF,IAAI,IAAI,MAAM,CAAC;IACf,KAAK,IAAI,IAAI,CAAC;CACjB;AAED,2FAA2F;AAC3F,wBAAgB,aAAa,IAAI,aAAa,CAa7C"}
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.mockTransport = mockTransport;
4
+ /** A transport that records what the engine emits and offers small helpers to query it. */
5
+ function mockTransport() {
6
+ const events = [];
7
+ return {
8
+ events,
9
+ transport: {
10
+ emit: (event) => { events.push(event); },
11
+ onInput: () => () => undefined,
12
+ close: () => undefined,
13
+ },
14
+ eventsOf: (type) => events.filter((e) => e.type === type),
15
+ text: () => events.map(e => (e.type === 'token' ? e.content : '')).join(''),
16
+ clear: () => { events.length = 0; },
17
+ };
18
+ }
19
+ //# sourceMappingURL=mockTransport.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mockTransport.js","sourceRoot":"","sources":["../src/mockTransport.ts"],"names":[],"mappings":";;AAcA,sCAaC;AAdD,2FAA2F;AAC3F,SAAgB,aAAa;IACzB,MAAM,MAAM,GAAiB,EAAE,CAAC;IAChC,OAAO;QACH,MAAM;QACN,SAAS,EAAE;YACP,IAAI,EAAE,CAAC,KAAK,EAAE,EAAE,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YACxC,OAAO,EAAE,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC,SAAS;YAC9B,KAAK,EAAE,GAAG,EAAE,CAAC,SAAS;SACzB;QACD,QAAQ,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,EAAmD,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,IAAI,CAAC;QAC1G,IAAI,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC;QAC3E,KAAK,EAAE,GAAG,EAAE,GAAG,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC;KACtC,CAAC;AACN,CAAC"}
@@ -0,0 +1,82 @@
1
+ import type { ChatOptions, LLMMessage, LLMProvider, LLMStreamCallbacks, ToolDefinition } from '@backendkit-labs/agent-core';
2
+ type LLMUsageDetails = NonNullable<Parameters<NonNullable<LLMStreamCallbacks['onMetrics']>>[2]>;
3
+ /** One thing the provider did during a call, `t` ms after the call started. */
4
+ export type RecordedEvent = {
5
+ t: number;
6
+ type: 'chunk';
7
+ delta: string;
8
+ } | {
9
+ t: number;
10
+ type: 'reasoning';
11
+ delta: string;
12
+ } | {
13
+ t: number;
14
+ type: 'tool_call';
15
+ name: string;
16
+ args: string;
17
+ id: string;
18
+ } | {
19
+ t: number;
20
+ type: 'metrics';
21
+ inputTokens: number;
22
+ outputTokens: number;
23
+ usage?: LLMUsageDetails;
24
+ } | {
25
+ t: number;
26
+ type: 'done';
27
+ message: LLMMessage;
28
+ } | {
29
+ t: number;
30
+ type: 'error';
31
+ error: RecordedError;
32
+ };
33
+ /** An error reduced to what the code that handles provider failures looks at. */
34
+ export interface RecordedError {
35
+ name: string;
36
+ message: string;
37
+ status?: number;
38
+ code?: string;
39
+ }
40
+ /** One `chat()` call: what was asked and everything that came back. */
41
+ export interface RecordedCall {
42
+ /** Format version of the file. */
43
+ v: 1;
44
+ /** 0-based position in the recording. */
45
+ index: number;
46
+ startedAt: number;
47
+ durationMs: number;
48
+ model?: string;
49
+ request: {
50
+ messages: LLMMessage[];
51
+ tools: string[];
52
+ options?: ChatOptions;
53
+ };
54
+ events: RecordedEvent[];
55
+ }
56
+ export type RecordingSink = string | ((call: RecordedCall) => void);
57
+ export interface RecordingOptions {
58
+ /** Edit or censor a call before it is written (recordings contain the whole conversation). */
59
+ redact?: (call: RecordedCall) => RecordedCall;
60
+ }
61
+ export declare function serializeError(err: unknown): RecordedError;
62
+ /** Rebuilds an `Error` carrying the same `name`, `status` and `code`, so retry and overflow checks behave as live. */
63
+ export declare function deserializeError(rec: RecordedError): Error;
64
+ /**
65
+ * Wraps a real provider and writes every call (request, streamed events, metrics, errors, timings) as
66
+ * one JSON line, to a file or to a function. The wrapped provider behaves exactly as before.
67
+ * The file contains the full conversation: treat it as sensitive and use `redact` when needed.
68
+ */
69
+ export declare class RecordingProvider implements LLMProvider {
70
+ private readonly inner;
71
+ private readonly sink;
72
+ private readonly options;
73
+ private index;
74
+ constructor(inner: LLMProvider, sink: RecordingSink, options?: RecordingOptions);
75
+ get capabilities(): {
76
+ structuredOutput: import("@backendkit-labs/agent-core").StructuredOutputSupport;
77
+ } | undefined;
78
+ get modelId(): string | undefined;
79
+ chat(messages: LLMMessage[], tools: ToolDefinition[], callbacks: LLMStreamCallbacks, options?: ChatOptions): Promise<void>;
80
+ }
81
+ export {};
82
+ //# sourceMappingURL=recording.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"recording.d.ts","sourceRoot":"","sources":["../src/recording.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,WAAW,EAAE,kBAAkB,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAE5H,KAAK,eAAe,GAAG,WAAW,CAAC,UAAU,CAAC,WAAW,CAAC,kBAAkB,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAEhG,+EAA+E;AAC/E,MAAM,MAAM,aAAa,GACnB;IAAE,CAAC,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAC3C;IAAE,CAAC,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,WAAW,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAC/C;IAAE,CAAC,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,WAAW,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,EAAE,EAAE,MAAM,CAAA;CAAE,GACxE;IAAE,CAAC,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,SAAS,CAAC;IAAC,WAAW,EAAE,MAAM,CAAC;IAAC,YAAY,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,eAAe,CAAA;CAAE,GAClG;IAAE,CAAC,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,UAAU,CAAA;CAAE,GAChD;IAAE,CAAC,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,aAAa,CAAA;CAAE,CAAC;AAEzD,iFAAiF;AACjF,MAAM,WAAW,aAAa;IAC1B,IA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Q,EAAE,UAAU,EAAE,EAAE,KAAK,EAAE,cAAc,EAAE,EAAE,SAAS,EAAE,kBAAkB,EAAE,OAAO,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;CAmCnI"}
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RecordingProvider = void 0;
4
+ exports.serializeError = serializeError;
5
+ exports.deserializeError = deserializeError;
6
+ const node_fs_1 = require("node:fs");
7
+ const node_path_1 = require("node:path");
8
+ function serializeError(err) {
9
+ const e = (err ?? {});
10
+ return {
11
+ name: typeof e.name === 'string' ? e.name : 'Error',
12
+ message: typeof e.message === 'string' ? e.message : String(err),
13
+ ...(typeof (e.status ?? e.statusCode) === 'number' ? { status: (e.status ?? e.statusCode) } : {}),
14
+ ...(typeof e.code === 'string' ? { code: e.code } : {}),
15
+ };
16
+ }
17
+ /** Rebuilds an `Error` carrying the same `name`, `status` and `code`, so retry and overflow checks behave as live. */
18
+ function deserializeError(rec) {
19
+ const err = new Error(rec.message);
20
+ err.name = rec.name;
21
+ if (rec.status !== undefined)
22
+ err.status = rec.status;
23
+ if (rec.code !== undefined)
24
+ err.code = rec.code;
25
+ return err;
26
+ }
27
+ /**
28
+ * Wraps a real provider and writes every call (request, streamed events, metrics, errors, timings) as
29
+ * one JSON line, to a file or to a function. The wrapped provider behaves exactly as before.
30
+ * The file contains the full conversation: treat it as sensitive and use `redact` when needed.
31
+ */
32
+ class RecordingProvider {
33
+ inner;
34
+ sink;
35
+ options;
36
+ index = 0;
37
+ constructor(inner, sink, options = {}) {
38
+ this.inner = inner;
39
+ this.sink = sink;
40
+ this.options = options;
41
+ if (typeof sink === 'string')
42
+ (0, node_fs_1.mkdirSync)((0, node_path_1.dirname)(sink), { recursive: true });
43
+ }
44
+ get capabilities() { return this.inner.capabilities; }
45
+ get modelId() { return this.inner.modelId; }
46
+ async chat(messages, tools, callbacks, options) {
47
+ const startedAt = Date.now();
48
+ const events = [];
49
+ const t = () => Date.now() - startedAt;
50
+ const recorded = {
51
+ ...callbacks,
52
+ onChunk: (delta) => { events.push({ t: t(), type: 'chunk', delta }); callbacks.onChunk?.(delta); },
53
+ onReasoning: (delta) => { events.push({ t: t(), type: 'reasoning', delta }); callbacks.onReasoning?.(delta); },
54
+ onToolCall: (name, args, id) => { events.push({ t: t(), type: 'tool_call', name, args, id }); callbacks.onToolCall?.(name, args, id); },
55
+ onMetrics: (inputTokens, outputTokens, usage) => {
56
+ events.push({ t: t(), type: 'metrics', inputTokens, outputTokens, ...(usage ? { usage } : {}) });
57
+ callbacks.onMetrics?.(inputTokens, outputTokens, usage);
58
+ },
59
+ onDone: (message) => { events.push({ t: t(), type: 'done', message }); callbacks.onDone(message); },
60
+ onError: (err) => { events.push({ t: t(), type: 'error', error: serializeError(err) }); callbacks.onError(err); },
61
+ };
62
+ const index = this.index++;
63
+ try {
64
+ await this.inner.chat(messages, tools, recorded, options);
65
+ }
66
+ catch (err) {
67
+ // A provider that throws instead of calling onError: keep the evidence, let the caller see it.
68
+ events.push({ t: t(), type: 'error', error: serializeError(err) });
69
+ throw err;
70
+ }
71
+ finally {
72
+ let call = {
73
+ v: 1, index, startedAt, durationMs: t(),
74
+ ...(callbacks.model ? { model: callbacks.model } : {}),
75
+ request: { messages, tools: tools.map(x => x.name), ...(options ? { options } : {}) },
76
+ events,
77
+ };
78
+ if (this.options.redact)
79
+ call = this.options.redact(call);
80
+ if (typeof this.sink === 'string')
81
+ (0, node_fs_1.appendFileSync)(this.sink, JSON.stringify(call) + '\n');
82
+ else
83
+ this.sink(call);
84
+ }
85
+ }
86
+ }
87
+ exports.RecordingProvider = RecordingProvider;
88
+ //# sourceMappingURL=recording.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"recording.js","sourceRoot":"","sources":["../src/recording.ts"],"names":[],"mappings":";;;AA2CA,wCAQC;AAGD,4CAMC;AA5DD,qCAAoD;AACpD,yCAAoC;AA0CpC,SAAgB,cAAc,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
+ import { type RecordedCall, type RecordedError } from './recording';
3
+ /**
4
+ * Faults to inject, keyed by the 1-based number of the `chat()` invocation. An injected invocation does
5
+ * NOT use up a recorded call: the next invocation plays the next recorded one, which is what a retry
6
+ * (or the engine's overflow recovery) would see live.
7
+ */
8
+ export interface ReplayOverrides {
9
+ inject?: Record<string, ReplayFault>;
10
+ }
11
+ export type ReplayFault =
12
+ /** Fail before the first chunk with this error (e.g. `{ status: 429 }` or the context-overflow 400). */
13
+ {
14
+ error: Partial<RecordedError> & {
15
+ message: string;
16
+ };
17
+ }
18
+ /** The call is cancelled: it ends with an `AbortError`. */
19
+ | {
20
+ cancel: true;
21
+ }
22
+ /** The call never answers until its `signal` is aborted (requires a signal). */
23
+ | {
24
+ hang: true;
25
+ };
26
+ export interface ReplayOptions {
27
+ /** Overrides as an object, or the path of a JSON file. Default: `<recording>.override.json` next to the file, when it exists. */
28
+ overrides?: ReplayOverrides | string;
29
+ /** Fail a call whose request differs from the recorded one. Default false. */
30
+ verifyRequests?: boolean;
31
+ /** `recorded` waits the original time between events. Default `instant`. */
32
+ timing?: 'instant' | 'recorded';
33
+ }
34
+ /** Reads a JSONL recording written by `RecordingProvider`. */
35
+ export declare function readRecording(path: string): RecordedCall[];
36
+ /**
37
+ * A provider that replays a recording: the same chunks, tool calls, metrics and errors, in order, with
38
+ * no network and no API key. Overrides inject failures the recording does not contain.
39
+ * `replay.requests` holds what the code under test sent on each invocation.
40
+ */
41
+ export declare class ReplayProvider implements LLMProvider {
42
+ private readonly options;
43
+ readonly requests: Array<{
44
+ messages: LLMMessage[];
45
+ tools: string[];
46
+ options?: ChatOptions;
47
+ }>;
48
+ private readonly calls;
49
+ private readonly overrides;
50
+ private cursor;
51
+ private invocation;
52
+ constructor(source: string | RecordedCall[], options?: ReplayOptions);
53
+ /** Recorded calls not played yet. */
54
+ remaining(): number;
55
+ chat(messages: LLMMessage[], tools: ToolDefinition[], callbacks: LLMStreamCallbacks, options?: ChatOptions): Promise<void>;
56
+ private playFault;
57
+ private play;
58
+ }
59
+ //# sourceMappingURL=replay.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"replay.d.ts","sourceRoot":"","sources":["../src/replay.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,WAAW,EAAE,kBAAkB,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAC5H,OAAO,EAAoB,KAAK,YAAY,EAAE,KAAK,aAAa,EAAsB,MAAM,aAAa,CAAC;AAE1G;;;;GAIG;AACH,MAAM,WAAW,eAAe;IAC5B,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;CACxC;AAED,MAAM,MAAM,WAAW;AACnB,wGAAwG;AACtG;IAAE,KAAK,EAAE,OAAO,CAAC,aAAa,CAAC,GAAG;QAAE,OAAO,EAAE,MAAM,CAAA;KAAE,CAAA;CAAE;AACzD,2DAA2D;GACzD;IAAE,MAAM,EAAE,IAAI,CAAA;CAAE;AAClB,gFAAgF;GAC9E;IAAE,IAAI,EAAE,IAAI,CAAA;CAAE,CAAC;AAErB,MAAM,WAAW,aAAa;IAC1B,iIAAiI;IACjI,SAAS,CAAC,EAAE,eAAe,GAAG,MAAM,CAAC;IACrC,8EAA8E;IAC9E,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,4EAA4E;IAC5E,MAAM,CAAC,EAAE,SAAS,GAAG,UAAU,CAAC;CACnC;AAQD,8DAA8D;AAC9D,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY,EAAE,CAM1D;AAUD;;;;GAIG;AACH,qBAAa,cAAe,YAAW,WAAW;IAOD,OAAO,CAAC,QAAQ,CAAC,OAAO;IANrE,QAAQ,CAAC,QAAQ,EAAE,KAAK,CAAC;QAAE,QAAQ,EAAE,UAAU,EAAE,CAAC;QAAC,KAAK,EAAE,MAAM,EAAE,CAAC;QAAC,OAAO,CAAC,EAAE,WAAW,CAAA;KAAE,CAAC,CAAM;IAClG,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAiB;IACvC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAkB;IAC5C,OAAO,CAAC,MAAM,CAAK;IACnB,OAAO,CAAC,UAAU,CAAK;gBAEX,MAAM,EAAE,MAAM,GAAG,YAAY,EAAE,EAAmB,OAAO,GAAE,aAAkB;IAUzF,qCAAqC;IACrC,SAAS,IAAI,MAAM;IAEb,IAAI,CAAC,QAAQ,EAAE,UAAU,EAAE,EAAE,KAAK,EAAE,cAAc,EAAE,EAAE,SAAS,EAAE,kBAAkB,EAAE,OAAO,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;YAoBlH,SAAS;YAgBT,IAAI;CAkBrB"}
package/dist/replay.js ADDED
@@ -0,0 +1,132 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ReplayProvider = void 0;
4
+ exports.readRecording = readRecording;
5
+ const node_fs_1 = require("node:fs");
6
+ const recording_1 = require("./recording");
7
+ function abortError(message) {
8
+ const err = new Error(message);
9
+ err.name = 'AbortError';
10
+ return err;
11
+ }
12
+ /** Reads a JSONL recording written by `RecordingProvider`. */
13
+ function readRecording(path) {
14
+ return (0, node_fs_1.readFileSync)(path, 'utf8').split(/\r?\n/).flatMap((line, i) => {
15
+ if (!line.trim())
16
+ return [];
17
+ try {
18
+ return [JSON.parse(line)];
19
+ }
20
+ catch {
21
+ throw new Error(`readRecording: ${path} line ${i + 1} is not valid JSON`);
22
+ }
23
+ });
24
+ }
25
+ function firstDifference(a, b) {
26
+ if (a.length !== b.length)
27
+ return `${a.length} messages sent, ${b.length} recorded`;
28
+ for (let i = 0; i < a.length; i++) {
29
+ if (JSON.stringify(a[i]) !== JSON.stringify(b[i]))
30
+ return `message ${i} (${a[i].role}) differs`;
31
+ }
32
+ return 'requests differ';
33
+ }
34
+ /**
35
+ * A provider that replays a recording: the same chunks, tool calls, metrics and errors, in order, with
36
+ * no network and no API key. Overrides inject failures the recording does not contain.
37
+ * `replay.requests` holds what the code under test sent on each invocation.
38
+ */
39
+ class ReplayProvider {
40
+ options;
41
+ requests = [];
42
+ calls;
43
+ overrides;
44
+ cursor = 0;
45
+ invocation = 0;
46
+ constructor(source, options = {}) {
47
+ this.options = options;
48
+ this.calls = typeof source === 'string' ? readRecording(source) : source;
49
+ let overrides = options.overrides;
50
+ if (overrides === undefined && typeof source === 'string') {
51
+ const sidecar = source.replace(/\.jsonl?$/, '') + '.override.json';
52
+ if ((0, node_fs_1.existsSync)(sidecar))
53
+ overrides = sidecar;
54
+ }
55
+ this.overrides = typeof overrides === 'string' ? JSON.parse((0, node_fs_1.readFileSync)(overrides, 'utf8')) : overrides ?? {};
56
+ }
57
+ /** Recorded calls not played yet. */
58
+ remaining() { return this.calls.length - this.cursor; }
59
+ async chat(messages, tools, callbacks, options) {
60
+ const n = ++this.invocation;
61
+ this.requests.push({ messages, tools: tools.map(t => t.name), ...(options ? { options } : {}) });
62
+ const fault = this.overrides.inject?.[String(n)];
63
+ if (fault)
64
+ return this.playFault(fault, callbacks);
65
+ const call = this.calls[this.cursor];
66
+ if (!call) {
67
+ callbacks.onError(new Error(`ReplayProvider: invocation ${n} has no recorded call (the recording has ${this.calls.length})`));
68
+ return;
69
+ }
70
+ if (this.options.verifyRequests && JSON.stringify(call.request.messages) !== JSON.stringify(messages)) {
71
+ callbacks.onError(new Error(`ReplayProvider: invocation ${n} does not match recorded call ${call.index}: ${firstDifference(messages, call.request.messages)}`));
72
+ return;
73
+ }
74
+ this.cursor++;
75
+ await this.play(call.events, callbacks);
76
+ }
77
+ async playFault(fault, callbacks) {
78
+ if ('error' in fault) {
79
+ callbacks.onError((0, recording_1.deserializeError)({ name: 'Error', ...fault.error }));
80
+ }
81
+ else if ('cancel' in fault) {
82
+ callbacks.onError(abortError('ReplayProvider: call cancelled by override'));
83
+ }
84
+ else {
85
+ const signal = callbacks.signal;
86
+ if (!signal)
87
+ throw new Error('ReplayProvider: a "hang" override needs the call to carry a signal');
88
+ await new Promise(resolve => {
89
+ if (signal.aborted)
90
+ resolve();
91
+ else
92
+ signal.addEventListener('abort', () => resolve(), { once: true });
93
+ });
94
+ callbacks.onError(abortError('ReplayProvider: hung call aborted'));
95
+ }
96
+ }
97
+ async play(events, callbacks) {
98
+ let elapsed = 0;
99
+ for (const e of events) {
100
+ if (callbacks.signal?.aborted) {
101
+ callbacks.onError(abortError('ReplayProvider: aborted during replay'));
102
+ return;
103
+ }
104
+ if (this.options.timing === 'recorded' && e.t > elapsed) {
105
+ await new Promise(r => setTimeout(r, e.t - elapsed));
106
+ elapsed = e.t;
107
+ }
108
+ switch (e.type) {
109
+ case 'chunk':
110
+ callbacks.onChunk?.(e.delta);
111
+ break;
112
+ case 'reasoning':
113
+ callbacks.onReasoning?.(e.delta);
114
+ break;
115
+ case 'tool_call':
116
+ callbacks.onToolCall?.(e.name, e.args, e.id);
117
+ break;
118
+ case 'metrics':
119
+ callbacks.onMetrics?.(e.inputTokens, e.outputTokens, e.usage);
120
+ break;
121
+ case 'done':
122
+ callbacks.onDone(e.message);
123
+ break;
124
+ case 'error':
125
+ callbacks.onError((0, recording_1.deserializeError)(e.error));
126
+ break;
127
+ }
128
+ }
129
+ }
130
+ }
131
+ exports.ReplayProvider = ReplayProvider;
132
+ //# sourceMappingURL=replay.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"replay.js","sourceRoot":"","sources":["../src/replay.ts"],"names":[],"mappings":";;;AAqCA,sCAMC;AA3CD,qCAAmD;AAEnD,2CAA0G;AA4B1G,SAAS,UAAU,CAAC,OAAe;IAC/B,MAAM,GAAG,GAAG,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC;IAC/B,GAAG,CAAC,IAAI,GAAG,YAAY,CAAC;IACxB,OAAO,GAAG,CAAC;AACf,CAAC;AAED,8DAA8D;AAC9D,SAAgB,aAAa,CAAC,IAAY;IACtC,OAAO,IAAA,sBAAY,EAAC,IAAI,EAAE,MAAM,CA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AAS,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC;oBAAC,MAAM;gBACtE,KAAK,SAAS;oBAAE,SAAS,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC,CAAC,YAAY,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC;oBAAC,MAAM;gBACrF,KAAK,MAAM;oBAAE,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC;oBAAC,MAAM;gBAChD,KAAK,OAAO;oBAAE,SAAS,CAAC,OAAO,CAAC,IAAA,4BAAgB,EAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;oBAAC,MAAM;YACtE,CAAC;QACL,CAAC;IACL,CAAC;CACJ;AA1ED,wCA0EC"}
@@ -0,0 +1,27 @@
1
+ import type { LLMProvider } from '@backendkit-labs/agent-core';
2
+ /** One answer of a scripted provider: plain text, or a set of tool calls. */
3
+ export interface Turn {
4
+ content?: string;
5
+ tool_calls?: Array<{
6
+ id: string;
7
+ name: string;
8
+ args: Record<string, unknown>;
9
+ }>;
10
+ /** Reported through `onMetrics` when set, as a real provider does with `include_usage`. */
11
+ metrics?: {
12
+ inputTokens: number;
13
+ outputTokens: number;
14
+ };
15
+ }
16
+ export interface ScriptedProviderOptions {
17
+ /** What to answer once the script is used up. Default: the text `[script exhausted]`. */
18
+ exhausted?: Turn;
19
+ }
20
+ /**
21
+ * A provider that follows a fixed script: each `chat()` call consumes the next turn.
22
+ * `provider.calls` records the messages every call received.
23
+ */
24
+ export declare function scriptedProvider(turns: Turn[], options?: ScriptedProviderOptions): LLMProvider & {
25
+ readonly calls: ReadonlyArray<Parameters<LLMProvider['chat']>[0]>;
26
+ };
27
+ //# sourceMappingURL=scriptedProvider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scriptedProvider.d.ts","sourceRoot":"","sources":["../src/scriptedProvider.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAE/D,6EAA6E;AAC7E,MAAM,WAAW,IAAI;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,UAAU,CAAC,EAAE,KAAK,CAAC;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;KAAE,CAAC,CAAC;IAChF,2FAA2F;IAC3F,OAAO,CAAC,EAAE;QAAE,WAAW,EAAE,MAAM,CAAC;QAAC,YAAY,EAAE,MAAM,CAAA;KAAE,CAAC;CAC3D;AAED,MAAM,WAAW,uBAAuB;IACpC,yFAAyF;IACzF,SAAS,CAAC,EAAE,IAAI,CAAC;CACpB;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE,OAAO,GAAE,uBAA4B,GAAG,WAAW,GAAG;IAAE,QAAQ,CAAC,KAAK,EAAE,aAAa,CAAC,UAAU,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;CAAE,CA2B1K"}
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.scriptedProvider = scriptedProvider;
4
+ /**
5
+ * A provider that follows a fixed script: each `chat()` call consumes the next turn.
6
+ * `provider.calls` records the messages every call received.
7
+ */
8
+ function scriptedProvider(turns, options = {}) {
9
+ let i = 0;
10
+ const calls = [];
11
+ return {
12
+ calls,
13
+ async chat(messages, _tools, callbacks) {
14
+ calls.push(messages);
15
+ const turn = turns[i++] ?? options.exhausted ?? { content: '[script exhausted]' };
16
+ if (turn.metrics)
17
+ callbacks.onMetrics?.(turn.metrics.inputTokens, turn.metrics.outputTokens);
18
+ if (turn.tool_calls?.length) {
19
+ for (const tc of turn.tool_calls)
20
+ callbacks.onToolCall?.(tc.name, JSON.stringify(tc.args), tc.id);
21
+ callbacks.onDone({
22
+ role: 'assistant',
23
+ content: null,
24
+ tool_calls: turn.tool_calls.map(tc => ({
25
+ id: tc.id,
26
+ type: 'function',
27
+ function: { name: tc.name, arguments: JSON.stringify(tc.args) },
28
+ })),
29
+ });
30
+ return;
31
+ }
32
+ const text = turn.content ?? '';
33
+ callbacks.onChunk?.(text);
34
+ callbacks.onDone({ role: 'assistant', content: text });
35
+ },
36
+ };
37
+ }
38
+ //# sourceMappingURL=scriptedProvider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scriptedProvider.js","sourceRoot":"","sources":["../src/scriptedProvider.ts"],"names":[],"mappings":";;AAmBA,4CA2BC;AA/BD;;;GAGG;AACH,SAAgB,gBAAgB,CAAC,KAAa,EAAE,UAAmC,EAAE;IACjF,IAAI,CAAC,GAAG,CAAC,CAAC;IACV,MAAM,KAAK,GAA8C,EAAE,CAAC;IAC5D,OAAO;QACH,KAAK;QACL,KAAK,CAAC,IAAI,CAAC,QAAQ,EAAE,MAAM,EAAE,SAAS;YAClC,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;YACrB,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,EAAE,CAAC,IAAI,OAAO,CAAC,SAAS,IAAI,EAAE,OAAO,EAAE,oBAAoB,EAAE,CAAC;YAClF,IAAI,IAAI,CAAC,OAAO;gBAAE,SAAS,CAAC,SAAS,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,WAAW,EAAE,IAAI,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC;YAC7F,IAAI,IAAI,CAAC,UAAU,EAAE,MAAM,EAAE,CAAC;gBAC1B,KAAK,MAAM,EAAE,IAAI,IAAI,CAAC,UAAU;oBAAE,SAAS,CAAC,UAAU,EAAE,CAAC,EAAE,CAAC,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,CAAC;gBAClG,SAAS,CAAC,MAAM,CAAC;oBACb,IAAI,EAAE,WAAW;oBACjB,OAAO,EAAE,IAAI;oBACb,UAAU,EAAE,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;wBACnC,EAAE,EAAE,EAAE,CAAC,EAAE;wBACT,IAAI,EAAE,UAAmB;wBACzB,QAAQ,EAAE,EAAE,IAAI,EAAE,EAAE,CAAC,IAAI,EAAE,SAAS,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE;qBAClE,CAAC,CAAC;iBACN,CAAC,CAAC;gBACH,OAAO;YACX,CAAC;YACD,MAAM,IAAI,GAAG,IAAI,CAAC,OAAO,IAAI,EAAE,CAAC;YAChC,SAAS,CAAC,OAAO,EAAE,CAAC,IAAI,CAAC,CAAC;YAC1B,SAAS,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,WAAW,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC;QAC3D,CAAC;KACJ,CAAC;AACN,CAAC"}
@@ -0,0 +1,19 @@
1
+ /** The file exists and its content is exactly `expected` (a string, or a predicate over the content). */
2
+ export declare function fileEquals(path: string, expected: string | ((content: string) => boolean)): void;
3
+ /**
4
+ * Snapshots `paths` now (content hash, or absence). Call `verify()` after the agent ran: it throws if
5
+ * any of them was created, changed or removed.
6
+ */
7
+ export declare function untouched(paths: string[]): {
8
+ verify(): void;
9
+ };
10
+ /** The process is gone (waits up to `timeoutMs` for it to exit). */
11
+ export declare function processGone(pid: number, opts?: {
12
+ timeoutMs?: number;
13
+ }): Promise<void>;
14
+ export declare const world: {
15
+ fileEquals: typeof fileEquals;
16
+ untouched: typeof untouched;
17
+ processGone: typeof processGone;
18
+ };
19
+ //# sourceMappingURL=world.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"world.d.ts","sourceRoot":"","sources":["../src/world.ts"],"names":[],"mappings":"AAeA,yGAAyG;AACzG,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,CAAC,CAAC,OAAO,EAAE,MAAM,KAAK,OAAO,CAAC,GAAG,IAAI,CAQhG;AAED;;;GAGG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG;IAAE,MAAM,IAAI,IAAI,CAAA;CAAE,CAQ7D;AAMD,oEAAoE;AACpE,wBAAsB,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,GAAE;IAAE,SAAS,CAAC,EAAE,MAAM,CAAA;CAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAM/F;AAED,eAAO,MAAM,KAAK;;;;CAAyC,CAAC"}
package/dist/world.js ADDED
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.world = void 0;
4
+ exports.fileEquals = fileEquals;
5
+ exports.untouched = untouched;
6
+ exports.processGone = processGone;
7
+ const node_fs_1 = require("node:fs");
8
+ const node_crypto_1 = require("node:crypto");
9
+ /**
10
+ * Assertions about the real system rather than what the agent says it did. They throw plain
11
+ * `Error`s, so they work under any test runner (jest, vitest, node:test).
12
+ */
13
+ function fingerprint(path) {
14
+ if (!(0, node_fs_1.existsSync)(path))
15
+ return 'absent';
16
+ const stat = (0, node_fs_1.statSync)(path);
17
+ if (stat.isDirectory())
18
+ return 'dir';
19
+ return 'file:' + (0, node_crypto_1.createHash)('sha256').update((0, node_fs_1.readFileSync)(path)).digest('hex');
20
+ }
21
+ /** The file exists and its content is exactly `expected` (a string, or a predicate over the content). */
22
+ function fileEquals(path, expected) {
23
+ if (!(0, node_fs_1.existsSync)(path))
24
+ throw new Error(`world.fileEquals: ${path} does not exist`);
25
+ const content = (0, node_fs_1.readFileSync)(path, 'utf8');
26
+ const ok = typeof expected === 'function' ? expected(content) : content === expected;
27
+ if (!ok) {
28
+ const shown = content.length > 200 ? content.slice(0, 200) + '…' : content;
29
+ throw new Error(`world.fileEquals: ${path} has unexpected content: ${JSON.stringify(shown)}`);
30
+ }
31
+ }
32
+ /**
33
+ * Snapshots `paths` now (content hash, or absence). Call `verify()` after the agent ran: it throws if
34
+ * any of them was created, changed or removed.
35
+ */
36
+ function untouched(paths) {
37
+ const before = new Map(paths.map(p => [p, fingerprint(p)]));
38
+ return {
39
+ verify() {
40
+ const changed = paths.filter(p => fingerprint(p) !== before.get(p));
41
+ if (changed.length)
42
+ throw new Error(`world.untouched: modified: ${changed.join(', ')}`);
43
+ },
44
+ };
45
+ }
46
+ function alive(pid) {
47
+ try {
48
+ process.kill(pid, 0);
49
+ return true;
50
+ }
51
+ catch (err) {
52
+ return err.code === 'EPERM';
53
+ }
54
+ }
55
+ /** The process is gone (waits up to `timeoutMs` for it to exit). */
56
+ async function processGone(pid, opts = {}) {
57
+ const end = Date.now() + (opts.timeoutMs ?? 5000);
58
+ while (alive(pid)) {
59
+ if (Date.now() > end)
60
+ throw new Error(`world.processGone: process ${pid} is still running`);
61
+ await new Promise(r => setTimeout(r, 25));
62
+ }
63
+ }
64
+ exports.world = { fileEquals, untouched, processGone };
65
+ //# sourceMappingURL=world.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"world.js","sourceRoot":"","sources":["../src/world.ts"],"names":[],"mappings":";;;AAgBA,gCAQC;AAMD,8BAQC;AAOD,kCAMC;AAnDD,qCAA6D;AAC7D,6CAAyC;AAEzC;;;GAGG;AAEH,SAAS,WAAW,CAAC,IAAY;IAC7B,IAAI,CAAC,IAAA,oBAAU,EAAC,IAAI,CAAC;QAAE,OAAO,QAAQ,CAAC;IACvC,MAAM,IAAI,GAAG,IAAA,kBAAQ,EAAC,IAAI,CAAC,CAAC;IAC5B,IAAI,IAAI,CAAC,WAAW,EAAE;QAAE,OAAO,KAAK,CAAC;IACrC,OAAO,OAAO,GAAG,IAAA,wBAAU,EAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,IAAA,sBAAY,EAAC,IAAI,C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AAC,IAAI,CAAC,EAAE,CAAC,CAAC;QAC5F,CAAC;KACJ,CAAC;AACN,CAAC;AAED,SAAS,KAAK,CAAC,GAAW;IACtB,IAAI,CAAC;QAAC,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;QAAC,OAAO,IAAI,CAAC;IAAC,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QAAC,OAAQ,GAA6B,CAAC,IAAI,KAAK,OAAO,CAAC;IAAC,CAAC;AACtH,CAAC;AAED,oEAAoE;AAC7D,KAAK,UAAU,WAAW,CAAC,GAAW,EAAE,OAA+B,EAAE;IAC5E,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,CAAC,IAAI,CAAC,SAAS,IAAI,IAAI,CAAC,CAAC;IAClD,OAAO,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC;QAChB,IAAI,IAAI,CAAC,GAAG,EAAE,GAAG,GAAG;YAAE,MAAM,IAAI,KAAK,CAAC,8BAA8B,GAAG,mBAAmB,CAAC,CAAC;QAC5F,MAAM,IAAI,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,UAAU,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;IAC9C,CAAC;AACL,CAAC;AAEY,QAAA,KAAK,GAAG,EAAE,UAAU,EAAE,SAAS,EAAE,WAAW,EAAE,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,47 @@
1
- {
2
- "name": "@backendkit-labs/agent-testkit",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
1
+ {
2
+ "name": "@backendkit-labs/agent-testkit",
3
+ "version": "0.1.1",
4
+ "description": "Test doubles for @backendkit-labs/agent-core agents: scripted providers, a mock OpenAI-compatible LLM server with injectable failures, a mock transport and assertions about the real world",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "default": "./dist/index.js"
11
+ }
12
+ },
13
+ "files": [
14
+ "dist/",
15
+ "README.md"
16
+ ],
17
+ "scripts": {
18
+ "build": "tsc",
19
+ "dev": "tsc --watch",
20
+ "typecheck": "tsc --noEmit",
21
+ "prepublishOnly": "npm run build",
22
+ "test": "jest"
23
+ },
24
+ "peerDependencies": {
25
+ "@backendkit-labs/agent-core": ">=0.29.0 <0.31.0"
26
+ },
27
+ "devDependencies": {
28
+ "@backendkit-labs/agent-providers": "^0.2.3",
29
+ "@types/jest": "^29.0.0",
30
+ "@types/node": "^20.0.0",
31
+ "jest": "^29.0.0",
32
+ "ts-jest": "^29.0.0",
33
+ "typescript": "^5.4.0"
34
+ },
35
+ "engines": {
36
+ "node": ">=20.0.0"
37
+ },
38
+ "repository": {
39
+ "type": "git",
40
+ "url": "git+https://github.com/BackendKit-labs/backendkit-agent-framework.git",
41
+ "directory": "packages/testkit"
42
+ },
43
+ "publishConfig": {
44
+ "access": "public"
45
+ },
46
+ "license": "MIT"
47
+ }