@buildanagent/sdk 0.1.0-alpha.10 → 0.1.0-alpha.12
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 +45 -1
- package/dist/errors.d.ts +6 -3
- package/dist/errors.js +3 -0
- package/dist/errors.js.map +1 -1
- package/dist/testing/auto-responder.d.ts +35 -0
- package/dist/testing/auto-responder.js +142 -0
- package/dist/testing/auto-responder.js.map +1 -0
- package/dist/testing/index.d.ts +11 -4
- package/dist/testing/index.js +10 -3
- package/dist/testing/index.js.map +1 -1
- package/dist/testing/recording.d.ts +92 -0
- package/dist/testing/recording.js +130 -0
- package/dist/testing/recording.js.map +1 -0
- package/dist/testing/replay.d.ts +41 -0
- package/dist/testing/replay.js +134 -0
- package/dist/testing/replay.js.map +1 -0
- package/dist/testing/scripted-approvals.d.ts +32 -0
- package/dist/testing/scripted-approvals.js +31 -0
- package/dist/testing/scripted-approvals.js.map +1 -0
- package/dist/testing/scripted-mcp.d.ts +43 -0
- package/dist/testing/scripted-mcp.js +67 -0
- package/dist/testing/scripted-mcp.js.map +1 -0
- package/dist/testing/scripted-model.d.ts +11 -2
- package/dist/testing/scripted-model.js +33 -2
- package/dist/testing/scripted-model.js.map +1 -1
- package/dist/vendor/agent-safety/errors.d.ts +2 -1
- package/dist/vendor/agent-safety/errors.js.map +1 -1
- package/dist/vendor/agent-safety/test-signer.d.ts +13 -0
- package/dist/vendor/agent-safety/test-signer.js +18 -0
- package/dist/vendor/agent-safety/test-signer.js.map +1 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
The SDK runs your agent's loop over [`@buildanagent/gateway`](../gateway/README.md): it calls the model, runs the tools the model asks for, and stops when the work is done. Its testing export lets you test that agent offline against a scripted model, with no API key.
|
|
4
4
|
|
|
5
|
-
As of November 2026 the package holds the loop, tool definitions, hooks, signed pauses for approval and the testing export.
|
|
5
|
+
As of November 2026 the package holds the loop, tool definitions, hooks, signed pauses for approval and their resumption, and the testing export.
|
|
6
6
|
|
|
7
7
|
## Install the alpha with `pnpm add @buildanagent/sdk@alpha`
|
|
8
8
|
|
|
@@ -432,6 +432,7 @@ Hooks add two `SdkError` codes:
|
|
|
432
432
|
- **`hook_failed`**: an interceptor threw while the run fails closed. The run catches it and reports a `hook` error result, so it never reaches your code.
|
|
433
433
|
- **`run_stopped`**: the error is a `RunStoppedError` from `runOrThrow()`.
|
|
434
434
|
- **`unexpected_model_call`**: the error is an `UnexpectedModelCallError` from `ScriptedModel`.
|
|
435
|
+
- **`invalid_recording`, `recording_expired` and `recording_mismatch`**: the error is a `RecordingError` from `recordPairs()` or `replayPairs()`, for a malformed recording, an expired one, or one that differs from the replay.
|
|
435
436
|
|
|
436
437
|
## Test against a strict scripted model
|
|
437
438
|
|
|
@@ -458,6 +459,49 @@ const model = new ScriptedModel(
|
|
|
458
459
|
|
|
459
460
|
A step is either a fixed response or a function that builds one from the request. A response stops with `"tool_calls"` when it contains a tool call and with `"stop"` otherwise. Setting `error` ends the stream with an error instead. Set `usage` to the token counts the response reports; counts you leave out are zero. A response without `usage` acts like a provider that reported none: its usage is marked `unreported`, so `createModel()` prices it as unknown rather than free. Pass `usage: {}` to report zero tokens.
|
|
460
461
|
|
|
462
|
+
## `autoResponder()` drives an agent without a script, and never calls a host tool
|
|
463
|
+
|
|
464
|
+
`autoResponder()` returns a step that behaves like a cooperative model. On a request whose conversation has no tool call yet, it calls one offered tool: the one the user's text names, or else the first. It fills the tool's input with `synthesizeInput(schema)`, which picks `const`, the first `enum` value, `default`, the first `anyOf` branch, or a plain value of the schema's type, such as `"example"` or the `minimum`. On every later request it answers in text.
|
|
465
|
+
|
|
466
|
+
The step never calls a tool named in `DEFAULT_DENIED_TOOLS`, which lists the shell, file-editing and screen tools of the agent SDKs we verify, such as `bash`, `exec_command`, `apply_patch`, `write`, `edit`, `multi_edit`, `str_replace_based_edit_tool`, `computer` and `delete`. It matches the whole name, ignoring case, hyphens and underscores, because one of our verification runs let OpenAI's `exec_command` through a filter that checked only the tool's kind. When a denied tool is the only tool offered, the step answers in text instead. `denyTools` adds names or regular expressions to the list, and nothing removes the defaults. One step answers one call, so this model calls a tool and then answers:
|
|
467
|
+
|
|
468
|
+
```ts
|
|
469
|
+
const model = new ScriptedModel([autoResponder({ denyTools: [/^deploy/] }), autoResponder()]);
|
|
470
|
+
```
|
|
471
|
+
|
|
472
|
+
## Record a real model once, then replay it offline with `replayPairs()`
|
|
473
|
+
|
|
474
|
+
`recordPairs(model, { providerVersion, expiresAt })` wraps a model and records each call as a pair: the normalised request (model, context and options) and every event the call streamed. Pass `recorder.model` to `run()`, then write `recorder.pairs()` to a fixture as JSON. Each pair is stamped with the SDK version, the model's API, provider and id, the provider version you name, the recording time and the expiry.
|
|
475
|
+
|
|
476
|
+
A pair holds the gateway's transcript types, not wire bytes, so it survives changes to an adapter's wire format. It also holds no credential. The adapter's key and headers never reach a call, and from the call's options the recorder leaves out every property named like a credential header (`authorization`, `x-api-key`, `api-key`, `x-goog-api-key`, `proxy-authorization`, `cf-aig-authorization` or a cookie), `apiKey`, or a name ending in `token`, `secret` or `password`, at any depth and in any spelling. The conversation and the model's output are kept exactly, because the replay must reproduce them. `retry` events stay out, because a replay can't reproduce them.
|
|
477
|
+
|
|
478
|
+
`replayPairs(pairs)` returns a `ReplayModel`, a `ScriptedModel` that streams each pair's events exactly as they were recorded. Wrap it with `createModel()` under the recorded id, so the replayed calls are priced from the same catalog entry. Leave out the middleware the recorded model had: the recorder sat outside it, so the pairs already hold its effect.
|
|
479
|
+
|
|
480
|
+
```ts
|
|
481
|
+
const replay = replayPairs(JSON.parse(fixture), { expect: { providerVersion: "2026-10-21" } });
|
|
482
|
+
const model = createModel({ adapter: replay, id: replay.modelId });
|
|
483
|
+
```
|
|
484
|
+
|
|
485
|
+
Given the same clock and ids, the replayed run streams the same events as the recorded one. A replay refuses four kinds of fault, each with a `RecordingError`, an `SdkError`:
|
|
486
|
+
|
|
487
|
+
- **`recording_expired`:** the clock has reached the stamp's `expiresAt`. Record the fixture again.
|
|
488
|
+
- **`recording_mismatch` on the stamp:** the pairs disagree on their stamp, a field differs from `expect`, or the SDK that recorded them is on another release line (another minor version under 0.x).
|
|
489
|
+
- **`recording_mismatch` on a request:** a call sent a request other than the recorded one. The run ends `error`, and `replay.assertComplete()` throws the error, which names the first field that differs.
|
|
490
|
+
- **`invalid_recording`:** the pairs are empty or malformed.
|
|
491
|
+
|
|
492
|
+
## Scripted doubles stand in for the approver and an MCP server
|
|
493
|
+
|
|
494
|
+
`scriptedApprovals(answers)` answers the run's approval requests in order, through `blockingApprovals()`, so a test drives the real signing and checking path. Join it with a gate that returns `ask`. Each answer takes the id of the request it answers, so give a `requestId` only to test an answer that names the wrong request. `requests` holds every request the run raised, and `assertComplete()` throws when an answer went unused or a request came after the last answer, which the run denied:
|
|
495
|
+
|
|
496
|
+
```ts
|
|
497
|
+
const approvals = scriptedApprovals([{ decision: "allow" }, { decision: "deny", reason: "not today" }]);
|
|
498
|
+
run({ model, system, input, tools, interceptors: composeInterceptors(gate, approvals), signer: testSigner() });
|
|
499
|
+
```
|
|
500
|
+
|
|
501
|
+
`testSigner()` signs with a fixed, published key whose id is `TEST_KEY_ID`, so a pause signed in one test process verifies in another and can be checked into a fixture. Anyone can forge its signatures, so use it only in tests.
|
|
502
|
+
|
|
503
|
+
`scriptedMcpServer({ name, tools })` builds the tools an MCP server would lend an agent. Each tool is named `mcp__<server>__<tool>`, has `kind: "dynamic"` and `firstParty: false`, and returns its scripted results one call at a time. `calls` records every call, and `assertComplete()` throws when a result went unused or a call came after the last one.
|
|
504
|
+
|
|
461
505
|
## Tests give the same result every run
|
|
462
506
|
|
|
463
507
|
Nothing in the testing export reads the system clock or `Math.random()`. `fixedClock(start, stepMs)` returns a clock that advances by a fixed step, and `sequentialIds(prefix)` returns `prefix-1`, `prefix-2` and so on. Pass them to `run()` as `clock` and `idGenerator`. For random numbers you can repeat, import `seededRandom(seed)` from `@buildanagent/gateway/testing`. The same script with the same clock and ids streams the same events every time, timestamps and run id included.
|
package/dist/errors.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* `SdkError`: the SDK's error for programmer errors, such as a tool definition that breaks the name rule or a tool
|
|
3
3
|
* that returns something other than a tool result. A failure the model caused, such as invalid tool input, is never
|
|
4
|
-
* thrown: the tool pipeline returns it to the model as an error result.
|
|
4
|
+
* thrown: the tool pipeline returns it to the model as an error result. Three subclasses keep their own names:
|
|
5
5
|
* `RunStoppedError`, which `runOrThrow()` throws for a run that did not finish, and the testing export's
|
|
6
|
-
* `UnexpectedModelCallError`.
|
|
6
|
+
* `UnexpectedModelCallError` and `RecordingError`.
|
|
7
7
|
*/
|
|
8
8
|
import type { Context, UnfinishedResult } from "./result.js";
|
|
9
9
|
/** Why an `SdkError` was thrown. */
|
|
10
|
-
export type SdkErrorCode = "invalid_tool_definition" | "invalid_tool_result" | "invalid_run_options" | "invalid_model_stream" | "max_depth_exceeded" | "invalid_hook_result" | "hook_failed" | "run_stopped" | "undelivered_message" | "unexpected_model_call";
|
|
10
|
+
export type SdkErrorCode = "invalid_tool_definition" | "invalid_tool_result" | "invalid_run_options" | "invalid_model_stream" | "max_depth_exceeded" | "invalid_hook_result" | "hook_failed" | "run_stopped" | "undelivered_message" | "unexpected_model_call" | "invalid_recording" | "recording_expired" | "recording_mismatch";
|
|
11
11
|
/**
|
|
12
12
|
* Thrown for a mistake in how the SDK is called. `invalid_tool_definition`: a tool's name, description, schema,
|
|
13
13
|
* exposure, `executionMode` or `resourceKey` is unusable, or two tools share a name. `invalid_tool_result`: a tool's
|
|
@@ -24,6 +24,9 @@ export type SdkErrorCode = "invalid_tool_definition" | "invalid_tool_result" | "
|
|
|
24
24
|
* could apply the message.
|
|
25
25
|
* `unexpected_model_call`: a `ScriptedModel` was called more times than its script allows (an
|
|
26
26
|
* `UnexpectedModelCallError`).
|
|
27
|
+
* `invalid_recording`, `recording_expired` and `recording_mismatch`: the testing export's `replayPairs()` refused a
|
|
28
|
+
* recording that is malformed, past its expiry, or stamped or requested differently from the replay (a
|
|
29
|
+
* `RecordingError`).
|
|
27
30
|
*/
|
|
28
31
|
export declare class SdkError extends Error {
|
|
29
32
|
readonly name: string;
|
package/dist/errors.js
CHANGED
|
@@ -16,6 +16,9 @@ const SDK_ERROR = Symbol.for("buildanagent.sdk.SdkError");
|
|
|
16
16
|
* could apply the message.
|
|
17
17
|
* `unexpected_model_call`: a `ScriptedModel` was called more times than its script allows (an
|
|
18
18
|
* `UnexpectedModelCallError`).
|
|
19
|
+
* `invalid_recording`, `recording_expired` and `recording_mismatch`: the testing export's `replayPairs()` refused a
|
|
20
|
+
* recording that is malformed, past its expiry, or stamped or requested differently from the replay (a
|
|
21
|
+
* `RecordingError`).
|
|
19
22
|
*/
|
|
20
23
|
export class SdkError extends Error {
|
|
21
24
|
name = "SdkError";
|
package/dist/errors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAyBA,4FAA4F;AAC5F,MAAM,SAAS,GAAG,MAAM,CAAC,GAAG,CAAC,2BAA2B,CAAC,CAAC;AAE1D;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,OAAO,QAAS,SAAQ,KAAK;IACf,IAAI,GAAW,UAAU,CAAC;IACnC,IAAI,CAAe;IAE5B,YAAY,IAAkB,EAAE,OAAe,EAAE,OAAsB;QACrE,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACxB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,SAAS,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;IAC1D,CAAC;CACF;AAED,8EAA8E;AAC9E,MAAM,UAAU,UAAU,CAAC,KAAc;IACvC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAK,KAAiC,CAAC,SAAS,CAAC,KAAK,IAAI,CAAC;AAC/G,CAAC;AAED,MAAM,iBAAiB,GAAG,MAAM,CAAC,GAAG,CAAC,kCAAkC,CAAC,CAAC;AAEzE;;;;GAIG;AACH,MAAM,OAAO,eAAgB,SAAQ,QAAQ;IACzB,IAAI,GAAG,iBAAiB,CAAC;IAClC,UAAU,CAAiC;IAC3C,MAAM,CAAmB;IAClC,0DAA0D;IACjD,OAAO,CAAU;IAE1B,YAAY,MAAwB;QAClC,KAAK,CAAC,aAAa,EAAE,yBAAyB,MAAM,CAAC,UAAU,WAAW,MAAM,CAAC,KAAK,cAAc,CAAC,CAAC;QACtG,IAAI,CAAC,UAAU,GAAG,MAAM,CAAC,UAAU,CAAC;QACpC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC;QAC9B,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,iBAAiB,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;IAClE,CAAC;IAED,qHAAqH;IACrH,MAAM,CAAC,UAAU,CAAC,KAAc;QAC9B,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAK,KAAiC,CAAC,iBAAiB,CAAC,KAAK,IAAI,CAC9G,CAAC;IACJ,CAAC;CACF","sourcesContent":["/**\n * `SdkError`: the SDK's error for programmer errors, such as a tool definition that breaks the name rule or a tool\n * that returns something other than a tool result. A failure the model caused, such as invalid tool input, is never\n * thrown: the tool pipeline returns it to the model as an error result. Three subclasses keep their own names:\n * `RunStoppedError`, which `runOrThrow()` throws for a run that did not finish, and the testing export's\n * `UnexpectedModelCallError` and `RecordingError`.\n */\nimport type { Context, UnfinishedResult } from \"./result.js\";\n\n/** Why an `SdkError` was thrown. */\nexport type SdkErrorCode =\n | \"invalid_tool_definition\"\n | \"invalid_tool_result\"\n | \"invalid_run_options\"\n | \"invalid_model_stream\"\n | \"max_depth_exceeded\"\n | \"invalid_hook_result\"\n | \"hook_failed\"\n | \"run_stopped\"\n | \"undelivered_message\"\n | \"unexpected_model_call\"\n | \"invalid_recording\"\n | \"recording_expired\"\n | \"recording_mismatch\";\n\n// A registered symbol, so the guard still works when two copies of the package are bundled.\nconst SDK_ERROR = Symbol.for(\"buildanagent.sdk.SdkError\");\n\n/**\n * Thrown for a mistake in how the SDK is called. `invalid_tool_definition`: a tool's name, description, schema,\n * exposure, `executionMode` or `resourceKey` is unusable, or two tools share a name. `invalid_tool_result`: a tool's\n * `execute` returned a malformed result or write outcome. `invalid_run_options`: `run()` or `continueRun()` got an\n * option it can't use, such as a model that isn't from `createModel()`, or `continueRun()` got a context it can't\n * resume, such as one whose waiting call lost its pending request. `invalid_model_stream`: a model's stream ended\n * without a final message. `max_depth_exceeded`: a tool started a run nested deeper than `maxDepth` allows.\n * `invalid_hook_result`: an interceptor returned something its type does not allow, such as\n * a gate that returned no decision, or a replacement input that fails the tool's schema. `hook_failed`: an interceptor\n * threw while the run fails closed. The run catches it and ends with an `error` result whose `error.kind` is `hook`,\n * so it never reaches your code. `run_stopped`: `runOrThrow()` was given a run that did not finish (a\n * `RunStoppedError`).\n * `undelivered_message`: `steer()` or `followUp()` was called on a run that had ended, or the run ended before it\n * could apply the message.\n * `unexpected_model_call`: a `ScriptedModel` was called more times than its script allows (an\n * `UnexpectedModelCallError`).\n * `invalid_recording`, `recording_expired` and `recording_mismatch`: the testing export's `replayPairs()` refused a\n * recording that is malformed, past its expiry, or stamped or requested differently from the replay (a\n * `RecordingError`).\n */\nexport class SdkError extends Error {\n override readonly name: string = \"SdkError\";\n readonly code: SdkErrorCode;\n\n constructor(code: SdkErrorCode, message: string, options?: ErrorOptions) {\n super(message, options);\n this.code = code;\n Object.defineProperty(this, SDK_ERROR, { value: true });\n }\n}\n\n/** True for an `SdkError`, including one from another copy of the package. */\nexport function isSdkError(error: unknown): error is SdkError {\n return typeof error === \"object\" && error !== null && (error as Record<symbol, unknown>)[SDK_ERROR] === true;\n}\n\nconst RUN_STOPPED_ERROR = Symbol.for(\"buildanagent.sdk.RunStoppedError\");\n\n/**\n * Thrown by `runOrThrow()` for a run that ended any way other than `done`, such as one that reached `maxSteps`. It\n * carries the whole result, and the context to continue from: a paused run's context resumes with `continueRun()` and\n * its resolutions. It is an `SdkError` with code `run_stopped`.\n */\nexport class RunStoppedError extends SdkError {\n override readonly name = \"RunStoppedError\";\n readonly stopReason: UnfinishedResult[\"stopReason\"];\n readonly result: UnfinishedResult;\n /** The conversation so far, ready for `continueRun()`. */\n readonly context: Context;\n\n constructor(result: UnfinishedResult) {\n super(\"run_stopped\", `the run stopped with \"${result.stopReason}\" after ${result.steps} model calls`);\n this.stopReason = result.stopReason;\n this.result = result;\n this.context = result.context;\n Object.defineProperty(this, RUN_STOPPED_ERROR, { value: true });\n }\n\n /** True for a `RunStoppedError`, including one from another copy of the package. Use it in place of `instanceof`. */\n static isInstance(error: unknown): error is RunStoppedError {\n return (\n typeof error === \"object\" && error !== null && (error as Record<symbol, unknown>)[RUN_STOPPED_ERROR] === true\n );\n }\n}\n"]}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { ScriptStep } from "./scripted-model.js";
|
|
2
|
+
/**
|
|
3
|
+
* The tool names `autoResponder()` never calls, whatever options it gets: tools that run commands on the host,
|
|
4
|
+
* change its files or drive its screen, as the agent SDKs we verify name them. A name matches ignoring case, hyphens
|
|
5
|
+
* and underscores, so `Bash`, `MultiEdit` and `apply-patch` are all denied.
|
|
6
|
+
*/
|
|
7
|
+
export declare const DEFAULT_DENIED_TOOLS: readonly ["bash", "bash_output", "kill_shell", "exec", "exec_command", "write_stdin", "shell", "local_shell", "powershell", "terminal", "run_command", "computer", "write", "write_file", "create", "create_file", "edit", "edit_file", "multi_edit", "notebook_edit", "apply_patch", "str_replace_editor", "str_replace_based_edit_tool", "text_editor", "delete", "delete_file"];
|
|
8
|
+
/** What `autoResponder()` takes. */
|
|
9
|
+
export interface AutoResponderOptions {
|
|
10
|
+
/**
|
|
11
|
+
* More tools never to call, added to `DEFAULT_DENIED_TOOLS`: a string matches a tool's whole name, ignoring case,
|
|
12
|
+
* hyphens and underscores, and a regular expression is tested against the name (its `g` and `y` flags are ignored).
|
|
13
|
+
*/
|
|
14
|
+
readonly denyTools?: readonly (string | RegExp)[];
|
|
15
|
+
/** The final answer's text. Default `"Auto responder: done."`. */
|
|
16
|
+
readonly answer?: string;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Builds a value that fits a JSON Schema, the way a cooperative model fills in a tool's arguments: `const`, then the
|
|
20
|
+
* first `enum` value, then `default`, then the first `anyOf` or `oneOf` branch, then a value of the first non-null
|
|
21
|
+
* `type` (`"example"` for a string, the `minimum` or 1 for a number, `false`, a one-item array, or an object with
|
|
22
|
+
* every listed property). Nesting past six levels gives `null`. It reads no `$ref`, `pattern` or length bound.
|
|
23
|
+
*/
|
|
24
|
+
export declare function synthesizeInput(schema: Readonly<Record<string, unknown>>): unknown;
|
|
25
|
+
/**
|
|
26
|
+
* A responder step that acts like a cooperative model. On a request whose conversation has no tool call yet, it
|
|
27
|
+
* calls one offered tool that isn't denied (one whose name the user's text mentions, else the first), with input
|
|
28
|
+
* from `synthesizeInput()`; on any other request it answers in text. A denied tool is never called, even when it is
|
|
29
|
+
* the only tool offered: the step answers in text instead. `DEFAULT_DENIED_TOOLS` always applies, and `denyTools`
|
|
30
|
+
* adds to it.
|
|
31
|
+
*
|
|
32
|
+
* One step answers one call, so give `ScriptedModel` one step per call you expect, such as
|
|
33
|
+
* `new ScriptedModel([autoResponder(), autoResponder()])` for a call and its answer.
|
|
34
|
+
*/
|
|
35
|
+
export declare function autoResponder(options?: AutoResponderOptions): ScriptStep;
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The tool names `autoResponder()` never calls, whatever options it gets: tools that run commands on the host,
|
|
3
|
+
* change its files or drive its screen, as the agent SDKs we verify name them. A name matches ignoring case, hyphens
|
|
4
|
+
* and underscores, so `Bash`, `MultiEdit` and `apply-patch` are all denied.
|
|
5
|
+
*/
|
|
6
|
+
export const DEFAULT_DENIED_TOOLS = Object.freeze([
|
|
7
|
+
"bash",
|
|
8
|
+
"bash_output",
|
|
9
|
+
"kill_shell",
|
|
10
|
+
"exec",
|
|
11
|
+
"exec_command",
|
|
12
|
+
"write_stdin",
|
|
13
|
+
"shell",
|
|
14
|
+
"local_shell",
|
|
15
|
+
"powershell",
|
|
16
|
+
"terminal",
|
|
17
|
+
"run_command",
|
|
18
|
+
"computer",
|
|
19
|
+
"write",
|
|
20
|
+
"write_file",
|
|
21
|
+
"create",
|
|
22
|
+
"create_file",
|
|
23
|
+
"edit",
|
|
24
|
+
"edit_file",
|
|
25
|
+
"multi_edit",
|
|
26
|
+
"notebook_edit",
|
|
27
|
+
"apply_patch",
|
|
28
|
+
"str_replace_editor",
|
|
29
|
+
"str_replace_based_edit_tool",
|
|
30
|
+
"text_editor",
|
|
31
|
+
"delete",
|
|
32
|
+
"delete_file",
|
|
33
|
+
]);
|
|
34
|
+
const MAX_DEPTH = 6;
|
|
35
|
+
function firstType(schema) {
|
|
36
|
+
const { type } = schema;
|
|
37
|
+
return Array.isArray(type) ? type.find((item) => item !== "null") : type;
|
|
38
|
+
}
|
|
39
|
+
function synthesizeObject(schema, depth) {
|
|
40
|
+
const out = {};
|
|
41
|
+
const properties = schema.properties;
|
|
42
|
+
if (typeof properties !== "object" || properties === null)
|
|
43
|
+
return out;
|
|
44
|
+
for (const [key, value] of Object.entries(properties))
|
|
45
|
+
out[key] = synthesize(value, depth + 1);
|
|
46
|
+
return out;
|
|
47
|
+
}
|
|
48
|
+
function synthesizeTyped(schema, depth) {
|
|
49
|
+
switch (firstType(schema)) {
|
|
50
|
+
case "string":
|
|
51
|
+
return schema.format === "email" ? "user@example.com" : "example";
|
|
52
|
+
case "number":
|
|
53
|
+
case "integer":
|
|
54
|
+
return typeof schema.minimum === "number" ? schema.minimum : 1;
|
|
55
|
+
case "boolean":
|
|
56
|
+
return false;
|
|
57
|
+
case "null":
|
|
58
|
+
return null;
|
|
59
|
+
case "array":
|
|
60
|
+
return [synthesize(schema.items ?? {}, depth + 1)];
|
|
61
|
+
default:
|
|
62
|
+
return synthesizeObject(schema, depth);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
function synthesize(schema, depth) {
|
|
66
|
+
if (depth > MAX_DEPTH || typeof schema !== "object" || schema === null)
|
|
67
|
+
return null;
|
|
68
|
+
const s = schema;
|
|
69
|
+
if (s.const !== undefined)
|
|
70
|
+
return s.const;
|
|
71
|
+
if (Array.isArray(s.enum))
|
|
72
|
+
return s.enum[0];
|
|
73
|
+
if (s.default !== undefined)
|
|
74
|
+
return s.default;
|
|
75
|
+
const options = s.anyOf ?? s.oneOf;
|
|
76
|
+
if (Array.isArray(options))
|
|
77
|
+
return synthesize(options[0], depth + 1);
|
|
78
|
+
return synthesizeTyped(s, depth);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Builds a value that fits a JSON Schema, the way a cooperative model fills in a tool's arguments: `const`, then the
|
|
82
|
+
* first `enum` value, then `default`, then the first `anyOf` or `oneOf` branch, then a value of the first non-null
|
|
83
|
+
* `type` (`"example"` for a string, the `minimum` or 1 for a number, `false`, a one-item array, or an object with
|
|
84
|
+
* every listed property). Nesting past six levels gives `null`. It reads no `$ref`, `pattern` or length bound.
|
|
85
|
+
*/
|
|
86
|
+
export function synthesizeInput(schema) {
|
|
87
|
+
return synthesize(schema, 0);
|
|
88
|
+
}
|
|
89
|
+
const squash = (name) => name.toLowerCase().replace(/[-_]/g, "");
|
|
90
|
+
// A copy without the `g` and `y` flags: those make `test()` start from the last match's end, so the same rule tested
|
|
91
|
+
// against a second name could let it through.
|
|
92
|
+
function statelessRule(rule) {
|
|
93
|
+
return typeof rule === "string" ? squash(rule) : new RegExp(rule.source, rule.flags.replace(/[gy]/g, ""));
|
|
94
|
+
}
|
|
95
|
+
function isDenied(name, deny) {
|
|
96
|
+
const squashed = squash(name);
|
|
97
|
+
return deny.some((rule) => (typeof rule === "string" ? rule === squashed : rule.test(name)));
|
|
98
|
+
}
|
|
99
|
+
function toolCallsOf(messages) {
|
|
100
|
+
return messages.flatMap((message) => message.role === "assistant" ? message.content.filter((block) => block.type === "tool_call") : []);
|
|
101
|
+
}
|
|
102
|
+
function userText(messages) {
|
|
103
|
+
return messages
|
|
104
|
+
.flatMap((message) => (message.role === "user" ? message.content : []))
|
|
105
|
+
.map((block) => (block.type === "text" ? block.text : ""))
|
|
106
|
+
.join("\n");
|
|
107
|
+
}
|
|
108
|
+
const escaped = (text) => text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
109
|
+
/** True when `text` names `name` as a whole word, so `lookup` isn't found inside `lookup_v2`. */
|
|
110
|
+
function names(text, name) {
|
|
111
|
+
return new RegExp(`(?<![\\w-])${escaped(name)}(?![\\w-])`).test(text);
|
|
112
|
+
}
|
|
113
|
+
/** The tool to call: the one with the longest name the user's text names as a word, else the first allowed one. */
|
|
114
|
+
function pickTool(tools, messages) {
|
|
115
|
+
const text = userText(messages);
|
|
116
|
+
const named = tools.filter((tool) => names(text, tool.name)).sort((a, b) => b.name.length - a.name.length);
|
|
117
|
+
return named[0] ?? tools[0];
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* A responder step that acts like a cooperative model. On a request whose conversation has no tool call yet, it
|
|
121
|
+
* calls one offered tool that isn't denied (one whose name the user's text mentions, else the first), with input
|
|
122
|
+
* from `synthesizeInput()`; on any other request it answers in text. A denied tool is never called, even when it is
|
|
123
|
+
* the only tool offered: the step answers in text instead. `DEFAULT_DENIED_TOOLS` always applies, and `denyTools`
|
|
124
|
+
* adds to it.
|
|
125
|
+
*
|
|
126
|
+
* One step answers one call, so give `ScriptedModel` one step per call you expect, such as
|
|
127
|
+
* `new ScriptedModel([autoResponder(), autoResponder()])` for a call and its answer.
|
|
128
|
+
*/
|
|
129
|
+
export function autoResponder(options = {}) {
|
|
130
|
+
const deny = [...DEFAULT_DENIED_TOOLS, ...(options.denyTools ?? [])].map(statelessRule);
|
|
131
|
+
const answer = options.answer ?? "Auto responder: done.";
|
|
132
|
+
return (request) => {
|
|
133
|
+
const { messages, tools } = request.context;
|
|
134
|
+
const allowed = tools.filter((tool) => !isDenied(tool.name, deny));
|
|
135
|
+
const tool = toolCallsOf(messages).length === 0 ? pickTool(allowed, messages) : undefined;
|
|
136
|
+
if (tool === undefined)
|
|
137
|
+
return { content: [{ type: "text", text: answer }], usage: {} };
|
|
138
|
+
const input = synthesizeInput(tool.inputSchema);
|
|
139
|
+
return { content: [{ type: "tool_call", callId: "", name: tool.name, input }], usage: {} };
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
//# sourceMappingURL=auto-responder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auto-responder.js","sourceRoot":"","sources":["../../src/testing/auto-responder.ts"],"names":[],"mappings":"AASA;;;;GAIG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAG,MAAM,CAAC,MAAM,CAAC;IAChD,MAAM;IACN,aAAa;IACb,YAAY;IACZ,MAAM;IACN,cAAc;IACd,aAAa;IACb,OAAO;IACP,aAAa;IACb,YAAY;IACZ,UAAU;IACV,aAAa;IACb,UAAU;IACV,OAAO;IACP,YAAY;IACZ,QAAQ;IACR,aAAa;IACb,MAAM;IACN,WAAW;IACX,YAAY;IACZ,eAAe;IACf,aAAa;IACb,oBAAoB;IACpB,6BAA6B;IAC7B,aAAa;IACb,QAAQ;IACR,aAAa;CACL,CAAC,CAAC;AAaZ,MAAM,SAAS,GAAG,CAAC,CAAC;AAEpB,SAAS,SAAS,CAAC,MAA+B;IAChD,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,C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AK,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;QAC1F,IAAI,IAAI,KAAK,SAAS;YAAE,OAAO,EAAE,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC;QACxF,MAAM,KAAK,GAAG,eAAe,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;QAChD,OAAO,EAAE,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,WAAW,EAAE,MAAM,EAAE,EAAE,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC;IAC7F,CAAC,CAAC;AACJ,CAAC","sourcesContent":["/**\n * `autoResponder()`: a responder step that acts like a cooperative model without a script, so a test can drive any\n * agent offline (agent-SDK guide §8). It calls one tool with arguments built from the tool's JSON Schema, then answers\n * in text. It never calls a host tool: a deny list matched by name keeps shell, file-editing and screen tools out,\n * because a verification run once let OpenAI's `exec_command` through a filter that checked only the tool's kind.\n */\nimport type { ContentBlock, Message, WireTool } from \"@buildanagent/gateway\";\nimport type { AssistantScript, RecordedRequest, ScriptStep } from \"./scripted-model.js\";\n\n/**\n * The tool names `autoResponder()` never calls, whatever options it gets: tools that run commands on the host,\n * change its files or drive its screen, as the agent SDKs we verify name them. A name matches ignoring case, hyphens\n * and underscores, so `Bash`, `MultiEdit` and `apply-patch` are all denied.\n */\nexport const DEFAULT_DENIED_TOOLS = Object.freeze([\n \"bash\",\n \"bash_output\",\n \"kill_shell\",\n \"exec\",\n \"exec_command\",\n \"write_stdin\",\n \"shell\",\n \"local_shell\",\n \"powershell\",\n \"terminal\",\n \"run_command\",\n \"computer\",\n \"write\",\n \"write_file\",\n \"create\",\n \"create_file\",\n \"edit\",\n \"edit_file\",\n \"multi_edit\",\n \"notebook_edit\",\n \"apply_patch\",\n \"str_replace_editor\",\n \"str_replace_based_edit_tool\",\n \"text_editor\",\n \"delete\",\n \"delete_file\",\n] as const);\n\n/** What `autoResponder()` takes. */\nexport interface AutoResponderOptions {\n /**\n * More tools never to call, added to `DEFAULT_DENIED_TOOLS`: a string matches a tool's whole name, ignoring case,\n * hyphens and underscores, and a regular expression is tested against the name (its `g` and `y` flags are ignored).\n */\n readonly denyTools?: readonly (string | RegExp)[];\n /** The final answer's text. Default `\"Auto responder: done.\"`. */\n readonly answer?: string;\n}\n\nconst MAX_DEPTH = 6;\n\nfunction firstType(schema: Record<string, unknown>): unknown {\n const { type } = schema;\n return Array.isArray(type) ? type.find((item) => item !== \"null\") : type;\n}\n\nfunction synthesizeObject(schema: Record<string, unknown>, depth: number): Record<string, unknown> {\n const out: Record<string, unknown> = {};\n const properties = schema.properties;\n if (typeof properties !== \"object\" || properties === null) return out;\n for (const [key, value] of Object.entries(properties)) out[key] = synthesize(value, depth + 1);\n return out;\n}\n\nfunction synthesizeTyped(schema: Record<string, unknown>, depth: number): unknown {\n switch (firstType(schema)) {\n case \"string\":\n return schema.format === \"email\" ? \"user@example.com\" : \"example\";\n case \"number\":\n case \"integer\":\n return typeof schema.minimum === \"number\" ? schema.minimum : 1;\n case \"boolean\":\n return false;\n case \"null\":\n return null;\n case \"array\":\n return [synthesize(schema.items ?? {}, depth + 1)];\n default:\n return synthesizeObject(schema, depth);\n }\n}\n\nfunction synthesize(schema: unknown, depth: number): unknown {\n if (depth > MAX_DEPTH || typeof schema !== \"object\" || schema === null) return null;\n const s = schema as Record<string, unknown>;\n if (s.const !== undefined) return s.const;\n if (Array.isArray(s.enum)) return s.enum[0];\n if (s.default !== undefined) return s.default;\n const options = s.anyOf ?? s.oneOf;\n if (Array.isArray(options)) return synthesize(options[0], depth + 1);\n return synthesizeTyped(s, depth);\n}\n\n/**\n * Builds a value that fits a JSON Schema, the way a cooperative model fills in a tool's arguments: `const`, then the\n * first `enum` value, then `default`, then the first `anyOf` or `oneOf` branch, then a value of the first non-null\n * `type` (`\"example\"` for a string, the `minimum` or 1 for a number, `false`, a one-item array, or an object with\n * every listed property). Nesting past six levels gives `null`. It reads no `$ref`, `pattern` or length bound.\n */\nexport function synthesizeInput(schema: Readonly<Record<string, unknown>>): unknown {\n return synthesize(schema, 0);\n}\n\nconst squash = (name: string): string => name.toLowerCase().replace(/[-_]/g, \"\");\n\n// A copy without the `g` and `y` flags: those make `test()` start from the last match's end, so the same rule tested\n// against a second name could let it through.\nfunction statelessRule(rule: string | RegExp): string | RegExp {\n return typeof rule === \"string\" ? squash(rule) : new RegExp(rule.source, rule.flags.replace(/[gy]/g, \"\"));\n}\n\nfunction isDenied(name: string, deny: readonly (string | RegExp)[]): boolean {\n const squashed = squash(name);\n return deny.some((rule) => (typeof rule === \"string\" ? rule === squashed : rule.test(name)));\n}\n\nfunction toolCallsOf(messages: readonly Message[]): ContentBlock[] {\n return messages.flatMap((message) =>\n message.role === \"assistant\" ? message.content.filter((block) => block.type === \"tool_call\") : [],\n );\n}\n\nfunction userText(messages: readonly Message[]): string {\n return messages\n .flatMap((message) => (message.role === \"user\" ? message.content : []))\n .map((block) => (block.type === \"text\" ? block.text : \"\"))\n .join(\"\\n\");\n}\n\nconst escaped = (text: string): string => text.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\");\n\n/** True when `text` names `name` as a whole word, so `lookup` isn't found inside `lookup_v2`. */\nfunction names(text: string, name: string): boolean {\n return new RegExp(`(?<![\\\\w-])${escaped(name)}(?![\\\\w-])`).test(text);\n}\n\n/** The tool to call: the one with the longest name the user's text names as a word, else the first allowed one. */\nfunction pickTool(tools: readonly WireTool[], messages: readonly Message[]): WireTool | undefined {\n const text = userText(messages);\n const named = tools.filter((tool) => names(text, tool.name)).sort((a, b) => b.name.length - a.name.length);\n return named[0] ?? tools[0];\n}\n\n/**\n * A responder step that acts like a cooperative model. On a request whose conversation has no tool call yet, it\n * calls one offered tool that isn't denied (one whose name the user's text mentions, else the first), with input\n * from `synthesizeInput()`; on any other request it answers in text. A denied tool is never called, even when it is\n * the only tool offered: the step answers in text instead. `DEFAULT_DENIED_TOOLS` always applies, and `denyTools`\n * adds to it.\n *\n * One step answers one call, so give `ScriptedModel` one step per call you expect, such as\n * `new ScriptedModel([autoResponder(), autoResponder()])` for a call and its answer.\n */\nexport function autoResponder(options: AutoResponderOptions = {}): ScriptStep {\n const deny = [...DEFAULT_DENIED_TOOLS, ...(options.denyTools ?? [])].map(statelessRule);\n const answer = options.answer ?? \"Auto responder: done.\";\n return (request: RecordedRequest): AssistantScript => {\n const { messages, tools } = request.context;\n const allowed = tools.filter((tool) => !isDenied(tool.name, deny));\n const tool = toolCallsOf(messages).length === 0 ? pickTool(allowed, messages) : undefined;\n if (tool === undefined) return { content: [{ type: \"text\", text: answer }], usage: {} };\n const input = synthesizeInput(tool.inputSchema);\n return { content: [{ type: \"tool_call\", callId: \"\", name: tool.name, input }], usage: {} };\n };\n}\n"]}
|
package/dist/testing/index.d.ts
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* `@buildanagent/sdk/testing`:
|
|
3
|
-
*
|
|
4
|
-
* `
|
|
2
|
+
* `@buildanagent/sdk/testing`: everything a test needs to drive an agent offline. The strict `ScriptedModel` and
|
|
3
|
+
* `autoResponder()` stand in for the model, `scriptedApprovals()` and `scriptedMcpServer()` for the approval and MCP
|
|
4
|
+
* seams, `testSigner()` signs reproducible pauses, `recordPairs()` and `replayPairs()` record a real model once and
|
|
5
|
+
* replay it, and `fixedClock()` and `sequentialIds()` keep the run deterministic.
|
|
5
6
|
*/
|
|
7
|
+
export { TEST_KEY_ID, testSigner } from "../vendor/agent-safety/test-signer.js";
|
|
8
|
+
export { type AutoResponderOptions, autoResponder, DEFAULT_DENIED_TOOLS, synthesizeInput, } from "./auto-responder.js";
|
|
6
9
|
export { type Clock, fixedClock, sequentialIds } from "./determinism.js";
|
|
7
|
-
export { type
|
|
10
|
+
export { type RecordedPair, type Recorder, RecordingError, type RecordingErrorCode, type RecordingStamp, type RecordOptions, recordPairs, } from "./recording.js";
|
|
11
|
+
export { type ExpectedStamp, ReplayModel, type ReplayOptions, replayPairs } from "./replay.js";
|
|
12
|
+
export { type ScriptedAnswer, type ScriptedApprovals, scriptedApprovals } from "./scripted-approvals.js";
|
|
13
|
+
export { type ScriptedMcpCall, type ScriptedMcpServer, type ScriptedMcpServerOptions, type ScriptedMcpTool, type ScriptedToolStep, scriptedMcpServer, } from "./scripted-mcp.js";
|
|
14
|
+
export { type AssistantScript, type RecordedRequest, ScriptedModel, type ScriptedModelOptions, type ScriptStep, type StreamScript, UnexpectedModelCallError, } from "./scripted-model.js";
|
package/dist/testing/index.js
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* `@buildanagent/sdk/testing`:
|
|
3
|
-
*
|
|
4
|
-
* `
|
|
2
|
+
* `@buildanagent/sdk/testing`: everything a test needs to drive an agent offline. The strict `ScriptedModel` and
|
|
3
|
+
* `autoResponder()` stand in for the model, `scriptedApprovals()` and `scriptedMcpServer()` for the approval and MCP
|
|
4
|
+
* seams, `testSigner()` signs reproducible pauses, `recordPairs()` and `replayPairs()` record a real model once and
|
|
5
|
+
* replay it, and `fixedClock()` and `sequentialIds()` keep the run deterministic.
|
|
5
6
|
*/
|
|
7
|
+
export { TEST_KEY_ID, testSigner } from "../vendor/agent-safety/test-signer.js";
|
|
8
|
+
export { autoResponder, DEFAULT_DENIED_TOOLS, synthesizeInput, } from "./auto-responder.js";
|
|
6
9
|
export { fixedClock, sequentialIds } from "./determinism.js";
|
|
10
|
+
export { RecordingError, recordPairs, } from "./recording.js";
|
|
11
|
+
export { ReplayModel, replayPairs } from "./replay.js";
|
|
12
|
+
export { scriptedApprovals } from "./scripted-approvals.js";
|
|
13
|
+
export { scriptedMcpServer, } from "./scripted-mcp.js";
|
|
7
14
|
export { ScriptedModel, UnexpectedModelCallError, } from "./scripted-model.js";
|
|
8
15
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/testing/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/testing/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,uCAAuC,CAAC;AAChF,OAAO,EAEL,aAAa,EACb,oBAAoB,EACpB,eAAe,GAChB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAc,UAAU,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACzE,OAAO,EAGL,cAAc,EAId,WAAW,GACZ,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAsB,WAAW,EAAsB,WAAW,EAAE,MAAM,aAAa,CAAC;AAC/F,OAAO,EAA+C,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AACzG,OAAO,EAML,iBAAiB,GAClB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAGL,aAAa,EAIb,wBAAwB,GACzB,MAAM,qBAAqB,CAAC","sourcesContent":["/**\n * `@buildanagent/sdk/testing`: everything a test needs to drive an agent offline. The strict `ScriptedModel` and\n * `autoResponder()` stand in for the model, `scriptedApprovals()` and `scriptedMcpServer()` for the approval and MCP\n * seams, `testSigner()` signs reproducible pauses, `recordPairs()` and `replayPairs()` record a real model once and\n * replay it, and `fixedClock()` and `sequentialIds()` keep the run deterministic.\n */\n\nexport { TEST_KEY_ID, testSigner } from \"../vendor/agent-safety/test-signer.js\";\nexport {\n type AutoResponderOptions,\n autoResponder,\n DEFAULT_DENIED_TOOLS,\n synthesizeInput,\n} from \"./auto-responder.js\";\nexport { type Clock, fixedClock, sequentialIds } from \"./determinism.js\";\nexport {\n type RecordedPair,\n type Recorder,\n RecordingError,\n type RecordingErrorCode,\n type RecordingStamp,\n type RecordOptions,\n recordPairs,\n} from \"./recording.js\";\nexport { type ExpectedStamp, ReplayModel, type ReplayOptions, replayPairs } from \"./replay.js\";\nexport { type ScriptedAnswer, type ScriptedApprovals, scriptedApprovals } from \"./scripted-approvals.js\";\nexport {\n type ScriptedMcpCall,\n type ScriptedMcpServer,\n type ScriptedMcpServerOptions,\n type ScriptedMcpTool,\n type ScriptedToolStep,\n scriptedMcpServer,\n} from \"./scripted-mcp.js\";\nexport {\n type AssistantScript,\n type RecordedRequest,\n ScriptedModel,\n type ScriptedModelOptions,\n type ScriptStep,\n type StreamScript,\n UnexpectedModelCallError,\n} from \"./scripted-model.js\";\n"]}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `recordPairs()`: records each call a model makes as a pair of normalised request and response, stamped with the
|
|
3
|
+
* versions it was recorded against and an expiry (agent-SDK guide §8: record and replay at the normalised boundary).
|
|
4
|
+
* A pair holds the gateway's transcript types, never wire bytes, so it survives changes to an adapter's wire format,
|
|
5
|
+
* and it holds no credential: the adapter's key never reaches a call, and every property of the call's options named
|
|
6
|
+
* like a credential header or an API key is left out.
|
|
7
|
+
*/
|
|
8
|
+
import { type Model, type ModelCallOptions, type ModelCaps, type ModelContext } from "@buildanagent/gateway";
|
|
9
|
+
import { SdkError, type SdkErrorCode } from "../errors.js";
|
|
10
|
+
import type { Clock } from "./determinism.js";
|
|
11
|
+
import { type RecordedRequest, type StreamScript } from "./scripted-model.js";
|
|
12
|
+
/** What a recording was made against, and until when it may be replayed. Every pair of one recording shares it. */
|
|
13
|
+
export interface RecordingStamp {
|
|
14
|
+
/** The `@buildanagent/sdk` version that recorded it. */
|
|
15
|
+
readonly sdkVersion: string;
|
|
16
|
+
/** The model's wire protocol, such as `"anthropic-messages"`. */
|
|
17
|
+
readonly api: string;
|
|
18
|
+
/** The model's vendor, such as `"anthropic"`. */
|
|
19
|
+
readonly provider: string;
|
|
20
|
+
/** The provider's model id. */
|
|
21
|
+
readonly model: string;
|
|
22
|
+
/** The provider's API version, or whatever names the server build the recording was made against. */
|
|
23
|
+
readonly providerVersion: string;
|
|
24
|
+
/** The model's caps when it was recorded, which the replay model reports. */
|
|
25
|
+
readonly caps: ModelCaps;
|
|
26
|
+
/** When the recording was made, in ISO 8601. */
|
|
27
|
+
readonly recordedAt: string;
|
|
28
|
+
/** The time from which `replayPairs()` refuses the recording, in ISO 8601. */
|
|
29
|
+
readonly expiresAt: string;
|
|
30
|
+
}
|
|
31
|
+
/** One model call: the normalised request it sent and the normalised events it streamed. */
|
|
32
|
+
export interface RecordedPair {
|
|
33
|
+
readonly stamp: RecordingStamp;
|
|
34
|
+
readonly request: RecordedRequest;
|
|
35
|
+
/** The call's events, from `start` to its terminal, as the gateway streamed them, minus `retry` events. */
|
|
36
|
+
readonly response: StreamScript;
|
|
37
|
+
}
|
|
38
|
+
/** What `recordPairs()` takes besides the model. */
|
|
39
|
+
export interface RecordOptions {
|
|
40
|
+
/** The provider's API version, or another name for the server build you record against. Required. */
|
|
41
|
+
readonly providerVersion: string;
|
|
42
|
+
/** When the recording expires, in ISO 8601. Required, and later than `recordedAt`. */
|
|
43
|
+
readonly expiresAt: string;
|
|
44
|
+
/** Gives `recordedAt`. Defaults to the system time. */
|
|
45
|
+
readonly clock?: Clock;
|
|
46
|
+
}
|
|
47
|
+
/** A model that records its calls, and the pairs recorded so far. */
|
|
48
|
+
export interface Recorder {
|
|
49
|
+
/** Pass this model to `run()` in place of the one you recorded. */
|
|
50
|
+
readonly model: Model;
|
|
51
|
+
/** The pairs of every call that has reached its terminal, in the order the calls started. */
|
|
52
|
+
pairs(): RecordedPair[];
|
|
53
|
+
}
|
|
54
|
+
/** The codes a `RecordingError` carries. */
|
|
55
|
+
export type RecordingErrorCode = Extract<SdkErrorCode, "invalid_recording" | "recording_expired" | "recording_mismatch">;
|
|
56
|
+
/**
|
|
57
|
+
* Thrown by `recordPairs()` and `replayPairs()`. `invalid_recording`: the options or pairs are malformed, such as a
|
|
58
|
+
* missing stamp or an expiry that isn't a time. `recording_expired`: the replay time is at or past the stamp's
|
|
59
|
+
* `expiresAt`. `recording_mismatch`: the pairs disagree on their stamp, the stamp differs from what the replay
|
|
60
|
+
* expects, or a call sent a request other than the recorded one. It is an `SdkError`, so `isSdkError()` recognises it.
|
|
61
|
+
*/
|
|
62
|
+
export declare class RecordingError extends SdkError {
|
|
63
|
+
readonly name = "RecordingError";
|
|
64
|
+
readonly code: RecordingErrorCode;
|
|
65
|
+
constructor(code: RecordingErrorCode, message: string);
|
|
66
|
+
}
|
|
67
|
+
/** A JSON-safe copy: exactly what a fixture file written from it would hold. */
|
|
68
|
+
export declare function storable<T>(value: T): T;
|
|
69
|
+
/**
|
|
70
|
+
* The normalised form of one request, as a pair stores it and a replay compares it. Credentials are stripped from
|
|
71
|
+
* the options only, where a key or header can live. The context is the conversation, which a replay must reproduce
|
|
72
|
+
* exactly, so a tool input that happens to hold an `apiKey` property is kept as sent.
|
|
73
|
+
*/
|
|
74
|
+
export declare function normalisedRequest(model: Model["ref"], context: ModelContext, options: ModelCallOptions): RecordedRequest;
|
|
75
|
+
/**
|
|
76
|
+
* Wraps `model` so that each call it makes is recorded as a `RecordedPair`, and returns the wrapper with the pairs so
|
|
77
|
+
* far. Write `pairs()` to a fixture file as JSON, and replay it with `replayPairs()`. Each pair holds the request the
|
|
78
|
+
* call sent (model, context and options) and every event it streamed except `retry` events. The adapter's key and
|
|
79
|
+
* headers are never part of a call, and every property of the options named like a credential header or an API key,
|
|
80
|
+
* or ending in `token`, `secret` or `password`, is left out at any depth, so a fixture holds no key the adapter or
|
|
81
|
+
* the options carried. An option such as `cancellationToken` is therefore neither recorded nor compared. The
|
|
82
|
+
* conversation and the model's output are kept exactly as they were, because a replay must reproduce them. A call is recorded once it
|
|
83
|
+
* reaches its terminal event; one whose reader stopped earlier is not recorded.
|
|
84
|
+
*
|
|
85
|
+
* The pairs are taken where the caller meets `model`: each request as the caller sent it, before any middleware
|
|
86
|
+
* `model` was built with, and the events as the caller received them, after it. Replay them through `createModel()`
|
|
87
|
+
* without that middleware, or the replay compares a request the middleware changed with one it didn't.
|
|
88
|
+
*
|
|
89
|
+
* Throws a `RecordingError` with code `invalid_recording` for a blank `providerVersion`, or an `expiresAt` that isn't
|
|
90
|
+
* an ISO 8601 time later than the recording time.
|
|
91
|
+
*/
|
|
92
|
+
export declare function recordPairs(model: Model, options: RecordOptions): Recorder;
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `recordPairs()`: records each call a model makes as a pair of normalised request and response, stamped with the
|
|
3
|
+
* versions it was recorded against and an expiry (agent-SDK guide §8: record and replay at the normalised boundary).
|
|
4
|
+
* A pair holds the gateway's transcript types, never wire bytes, so it survives changes to an adapter's wire format,
|
|
5
|
+
* and it holds no credential: the adapter's key never reaches a call, and every property of the call's options named
|
|
6
|
+
* like a credential header or an API key is left out.
|
|
7
|
+
*/
|
|
8
|
+
import { complete, } from "@buildanagent/gateway";
|
|
9
|
+
import { stripCredentials } from "@buildanagent/gateway/testing";
|
|
10
|
+
import { SdkError } from "../errors.js";
|
|
11
|
+
import { SDK_VERSION } from "../version.js";
|
|
12
|
+
import { recordedOptions } from "./scripted-model.js";
|
|
13
|
+
/**
|
|
14
|
+
* Thrown by `recordPairs()` and `replayPairs()`. `invalid_recording`: the options or pairs are malformed, such as a
|
|
15
|
+
* missing stamp or an expiry that isn't a time. `recording_expired`: the replay time is at or past the stamp's
|
|
16
|
+
* `expiresAt`. `recording_mismatch`: the pairs disagree on their stamp, the stamp differs from what the replay
|
|
17
|
+
* expects, or a call sent a request other than the recorded one. It is an `SdkError`, so `isSdkError()` recognises it.
|
|
18
|
+
*/
|
|
19
|
+
export class RecordingError extends SdkError {
|
|
20
|
+
name = "RecordingError";
|
|
21
|
+
constructor(code, message) {
|
|
22
|
+
super(code, message);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const NORMALISED_EVENTS = new Set([
|
|
26
|
+
"start",
|
|
27
|
+
"text_start",
|
|
28
|
+
"text_delta",
|
|
29
|
+
"text_end",
|
|
30
|
+
"reasoning_start",
|
|
31
|
+
"reasoning_delta",
|
|
32
|
+
"reasoning_end",
|
|
33
|
+
"tool_input_start",
|
|
34
|
+
"tool_input_delta",
|
|
35
|
+
"tool_input_end",
|
|
36
|
+
"warning",
|
|
37
|
+
"done",
|
|
38
|
+
"error",
|
|
39
|
+
]);
|
|
40
|
+
/**
|
|
41
|
+
* True for an event a pair stores: the model spec's own events. `retry` events, which the gateway adds, and any
|
|
42
|
+
* provider event outside the spec stay out, because a replay can't reproduce them.
|
|
43
|
+
*/
|
|
44
|
+
function isNormalised(event) {
|
|
45
|
+
return NORMALISED_EVENTS.has(event.type);
|
|
46
|
+
}
|
|
47
|
+
/** A JSON-safe copy: exactly what a fixture file written from it would hold. */
|
|
48
|
+
export function storable(value) {
|
|
49
|
+
return JSON.parse(JSON.stringify(value));
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The normalised form of one request, as a pair stores it and a replay compares it. Credentials are stripped from
|
|
53
|
+
* the options only, where a key or header can live. The context is the conversation, which a replay must reproduce
|
|
54
|
+
* exactly, so a tool input that happens to hold an `apiKey` property is kept as sent.
|
|
55
|
+
*/
|
|
56
|
+
export function normalisedRequest(model, context, options) {
|
|
57
|
+
return storable({ model, context, options: stripCredentials(recordedOptions(options)) });
|
|
58
|
+
}
|
|
59
|
+
function timeOf(value, name) {
|
|
60
|
+
const time = typeof value === "string" ? Date.parse(value) : Number.NaN;
|
|
61
|
+
if (Number.isNaN(time))
|
|
62
|
+
throw new RecordingError("invalid_recording", `${name} must be an ISO 8601 time`);
|
|
63
|
+
return time;
|
|
64
|
+
}
|
|
65
|
+
function stampOf(model, options) {
|
|
66
|
+
if (typeof options?.providerVersion !== "string" || options.providerVersion.trim() === "") {
|
|
67
|
+
throw new RecordingError("invalid_recording", "recordPairs() needs a providerVersion");
|
|
68
|
+
}
|
|
69
|
+
const recordedAt = options.clock?.now() ?? new Date().toISOString();
|
|
70
|
+
if (timeOf(options.expiresAt, "expiresAt") <= timeOf(recordedAt, "recordedAt")) {
|
|
71
|
+
throw new RecordingError("invalid_recording", "recordPairs(): expiresAt must be later than the recording time");
|
|
72
|
+
}
|
|
73
|
+
const { api, provider, id } = model.ref;
|
|
74
|
+
const { providerVersion, expiresAt } = options;
|
|
75
|
+
return {
|
|
76
|
+
sdkVersion: SDK_VERSION,
|
|
77
|
+
api,
|
|
78
|
+
provider,
|
|
79
|
+
model: id,
|
|
80
|
+
providerVersion,
|
|
81
|
+
caps: model.caps,
|
|
82
|
+
recordedAt,
|
|
83
|
+
expiresAt,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Wraps `model` so that each call it makes is recorded as a `RecordedPair`, and returns the wrapper with the pairs so
|
|
88
|
+
* far. Write `pairs()` to a fixture file as JSON, and replay it with `replayPairs()`. Each pair holds the request the
|
|
89
|
+
* call sent (model, context and options) and every event it streamed except `retry` events. The adapter's key and
|
|
90
|
+
* headers are never part of a call, and every property of the options named like a credential header or an API key,
|
|
91
|
+
* or ending in `token`, `secret` or `password`, is left out at any depth, so a fixture holds no key the adapter or
|
|
92
|
+
* the options carried. An option such as `cancellationToken` is therefore neither recorded nor compared. The
|
|
93
|
+
* conversation and the model's output are kept exactly as they were, because a replay must reproduce them. A call is recorded once it
|
|
94
|
+
* reaches its terminal event; one whose reader stopped earlier is not recorded.
|
|
95
|
+
*
|
|
96
|
+
* The pairs are taken where the caller meets `model`: each request as the caller sent it, before any middleware
|
|
97
|
+
* `model` was built with, and the events as the caller received them, after it. Replay them through `createModel()`
|
|
98
|
+
* without that middleware, or the replay compares a request the middleware changed with one it didn't.
|
|
99
|
+
*
|
|
100
|
+
* Throws a `RecordingError` with code `invalid_recording` for a blank `providerVersion`, or an `expiresAt` that isn't
|
|
101
|
+
* an ISO 8601 time later than the recording time.
|
|
102
|
+
*/
|
|
103
|
+
export function recordPairs(model, options) {
|
|
104
|
+
const stamp = stampOf(model, options);
|
|
105
|
+
const slots = [];
|
|
106
|
+
async function* stream(context, callOptions = {}) {
|
|
107
|
+
const slot = slots.push(undefined) - 1;
|
|
108
|
+
const request = normalisedRequest(model.ref, context, callOptions);
|
|
109
|
+
const events = [];
|
|
110
|
+
for await (const event of model.stream(context, callOptions)) {
|
|
111
|
+
if (isNormalised(event))
|
|
112
|
+
events.push(event);
|
|
113
|
+
// Stored before the terminal is yielded: a reader such as `complete()` stops at the terminal, which ends this
|
|
114
|
+
// generator at the `yield`, so code after the loop would never run.
|
|
115
|
+
if (event.type === "done" || event.type === "error") {
|
|
116
|
+
slots[slot] = { stamp, request, response: { events: storable(events) } };
|
|
117
|
+
}
|
|
118
|
+
yield event;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
const recorded = {
|
|
122
|
+
ref: model.ref,
|
|
123
|
+
caps: model.caps,
|
|
124
|
+
stream,
|
|
125
|
+
complete: (context, callOptions) => complete(stream(context, callOptions)),
|
|
126
|
+
price: (usage) => model.price(usage),
|
|
127
|
+
};
|
|
128
|
+
return { model: recorded, pairs: () => slots.filter((pair) => pair !== undefined) };
|
|
129
|
+
}
|
|
130
|
+
//# sourceMappingURL=recording.js.map
|