@capybara-hq/testing 0.1.0-rc.43 → 0.1.0-rc.45
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/fake-channel.d.ts +11 -0
- package/dist/fake-channel.js +21 -2
- package/dist/fixtures.d.ts +2 -1
- package/dist/fixtures.js +2 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/model-conformance.d.ts +11 -1
- package/dist/model-conformance.js +44 -1
- package/fixtures/extensions/fake-im/src/index.ts +1 -0
- package/fixtures/extensions/fake-watch/.capybara-plugin/plugin.json +7 -0
- package/fixtures/extensions/fake-watch/package.json +11 -0
- package/fixtures/extensions/fake-watch/src/index.ts +130 -0
- package/package.json +5 -5
package/dist/fake-channel.d.ts
CHANGED
|
@@ -157,6 +157,17 @@ export interface FakeChannelServer {
|
|
|
157
157
|
count?: number;
|
|
158
158
|
onlyCards?: boolean;
|
|
159
159
|
}): void;
|
|
160
|
+
/**
|
|
161
|
+
* Hold the response of the next `count` successful `/send` (or `/update`) requests until
|
|
162
|
+
* `release()` (a gate, not a time window): recorded on arrival like {@link delayNext}, answered
|
|
163
|
+
* only once released. `stop()` releases every hold.
|
|
164
|
+
*/
|
|
165
|
+
holdNext(path: "/send" | "/update" | "/send-file", opts?: {
|
|
166
|
+
count?: number;
|
|
167
|
+
onlyCards?: boolean;
|
|
168
|
+
}): {
|
|
169
|
+
release(): void;
|
|
170
|
+
};
|
|
160
171
|
/** 让 /connect 返回该状态码(null 恢复正常)。 */
|
|
161
172
|
failConnect(status: number | null): void;
|
|
162
173
|
/** 该渠道连接当前在线的长连接。 */
|
package/dist/fake-channel.js
CHANGED
|
@@ -32,6 +32,8 @@ export async function startFakeChannelServer(opts = {}) {
|
|
|
32
32
|
const fileByKey = new Map();
|
|
33
33
|
const failures = [];
|
|
34
34
|
const delays = [];
|
|
35
|
+
const holds = [];
|
|
36
|
+
const pause = (d) => d.until ?? new Promise((r) => setTimeout(r, d.delayMs));
|
|
35
37
|
const live = new Map();
|
|
36
38
|
const peaks = new Map();
|
|
37
39
|
let failStatus = null;
|
|
@@ -109,7 +111,7 @@ export async function startFakeChannelServer(opts = {}) {
|
|
|
109
111
|
const fd = delays.find((x) => x.path === path && x.left > 0);
|
|
110
112
|
if (fd) {
|
|
111
113
|
fd.left--;
|
|
112
|
-
await
|
|
114
|
+
await pause(fd);
|
|
113
115
|
}
|
|
114
116
|
json(res, 200, { messageId: fid });
|
|
115
117
|
return;
|
|
@@ -132,7 +134,7 @@ export async function startFakeChannelServer(opts = {}) {
|
|
|
132
134
|
const d = delays.find((x) => x.path === path && x.left > 0 && (!x.onlyCards || hasCards));
|
|
133
135
|
if (d) {
|
|
134
136
|
d.left--;
|
|
135
|
-
await
|
|
137
|
+
await pause(d);
|
|
136
138
|
}
|
|
137
139
|
json(res, 200, { messageId });
|
|
138
140
|
};
|
|
@@ -282,6 +284,21 @@ export async function startFakeChannelServer(opts = {}) {
|
|
|
282
284
|
delayNext(path, delayMs, opts = {}) {
|
|
283
285
|
delays.push({ path, delayMs, left: opts.count ?? 1, onlyCards: opts.onlyCards ?? false });
|
|
284
286
|
},
|
|
287
|
+
holdNext(path, opts = {}) {
|
|
288
|
+
let release;
|
|
289
|
+
const until = new Promise((r) => {
|
|
290
|
+
release = r;
|
|
291
|
+
});
|
|
292
|
+
holds.push(release);
|
|
293
|
+
delays.push({
|
|
294
|
+
path,
|
|
295
|
+
delayMs: 0,
|
|
296
|
+
left: opts.count ?? 1,
|
|
297
|
+
onlyCards: opts.onlyCards ?? false,
|
|
298
|
+
until,
|
|
299
|
+
});
|
|
300
|
+
return { release };
|
|
301
|
+
},
|
|
285
302
|
failConnect(status) {
|
|
286
303
|
failStatus = status;
|
|
287
304
|
},
|
|
@@ -314,6 +331,8 @@ export async function startFakeChannelServer(opts = {}) {
|
|
|
314
331
|
return (updated.filter((x) => x.messageId === s.messageId).at(-1)?.message.text ?? s.message.text);
|
|
315
332
|
},
|
|
316
333
|
async stop() {
|
|
334
|
+
for (const release of holds)
|
|
335
|
+
release();
|
|
317
336
|
for (const res of live.values())
|
|
318
337
|
res.destroy();
|
|
319
338
|
live.clear();
|
package/dist/fixtures.d.ts
CHANGED
|
@@ -21,11 +21,12 @@
|
|
|
21
21
|
* | `fake-web` / `fake-web-2` | `web-search@1` and `web-fetch@1`, scripted by a prefix of the query or of the fetched URL's path (`error:<kind>`, `throw:`, `hang:`, `redirect:<url>`, `junk:`, `empty:`, `big:<n>`; see `makeFakeWeb`); results name the provider; deployment config: a required, org-overridable secret `token` the platform may leave empty, `configured()` iff it is set and not `unconfigured` (throws for `throw:configured`); calls recorded on `globalThis.__fakeWeb` (`fakeWebProbe`) |
|
|
22
22
|
* | `fake-context` | `context@1` scripted by its deployment config (`text`, default "fake context"; `mode` `ok` / `null` / `throw` / `hang`); calls recorded on `globalThis.__fakeContext` (`fakeContextProbe`); tool `note` (`sources` web and channel_dm) |
|
|
23
23
|
* | `fake-client` | client extension (`permissions.client`, no extension point): components `hud` (`conversation.properties.section`, server view with `data`), `brand` (`shell.brand`, renders `builtin` in `data-testid="fake-client-brand"`), `empty` (`conversation.empty`), `frame` (`conversation.composer.frame`) and `fx` (`shell.overlay`); `client/styles/fake.css` (outline-color on `button.primary` and the composer's buttons, `--cx-fake-client-run` while `run=running`); an `events` listener storing the last event type; theme `fake`; `suggestedAppearance` = theme `fake` + `shell.brand` -> `brand` |
|
|
24
|
+
* | `fake-watch` | tool `wait` (input `{ key, deadlineMs? }`): a watched input wait whose watcher reads kv `fake-watch:<key>` (`go` settles, `fail` throws, `hang` never answers, else pending), counts its polls in kv `fake-watch:<key>:polls` and writes the identity it got to kv `fake-watch:<key>:ctx`; the resumed call returns `{ outcome, answer, signal }` as JSON; tool `nowatch` asks for a watched wait without a watcher |
|
|
24
25
|
* | `fake-client-broken` | client code that throws: component `boom` (render), a `run.started` events listener, and a `conversation.empty` replacement (render) |
|
|
25
26
|
* | `fake-push` | `push-provider@1` scripted by the token prefix (`dead:`, `auth:`, `rate:`, `flaky:<n>:`, `throw:`, `net:`, `hang:`; else delivered); deployment config `apps` (default `["com.example.app"]`), optional secret `key`; every send is a row of `plugin_fake_push.sends` and an entry of `fakePushProbe().sends`; `makeFakePush()` is the same provider without a kernel |
|
|
26
27
|
* | `fake-config-form` | display name 表单样例 / Form sample; no extension point: an openai-compatible-shaped deployment config (endpoints with a preset menu, an API key per endpoint in the secret record `apiKeys`, model rows, advanced fields, localized custom issues) carrying the config form hints of the extension config forms spec §6, for the web form e2e |
|
|
27
28
|
*/
|
|
28
|
-
export declare const FAKE_EXTENSIONS: readonly ["fake-connector", "fake-openapi", "fake-object-store", "fake-tools-ui", "fake-storage-user", "fake-lifecycle", "fake-embed", "fake-bootstrap-a", "fake-bootstrap-b", "fake-dep-provider", "fake-dep-requirer", "fake-im", "fake-web", "fake-web-2", "fake-context", "fake-config-form", "fake-push", "fake-client", "fake-client-broken"];
|
|
29
|
+
export declare const FAKE_EXTENSIONS: readonly ["fake-connector", "fake-openapi", "fake-object-store", "fake-tools-ui", "fake-storage-user", "fake-lifecycle", "fake-embed", "fake-bootstrap-a", "fake-bootstrap-b", "fake-dep-provider", "fake-dep-requirer", "fake-im", "fake-web", "fake-web-2", "fake-context", "fake-config-form", "fake-push", "fake-client", "fake-client-broken", "fake-watch"];
|
|
29
30
|
/**
|
|
30
31
|
* Fake plugins without an extension (declarative: they live in `fixtures/plugins/<name>`, same
|
|
31
32
|
* pack layout).
|
package/dist/fixtures.js
CHANGED
|
@@ -24,6 +24,7 @@ import { fileURLToPath } from "node:url";
|
|
|
24
24
|
* | `fake-web` / `fake-web-2` | `web-search@1` and `web-fetch@1`, scripted by a prefix of the query or of the fetched URL's path (`error:<kind>`, `throw:`, `hang:`, `redirect:<url>`, `junk:`, `empty:`, `big:<n>`; see `makeFakeWeb`); results name the provider; deployment config: a required, org-overridable secret `token` the platform may leave empty, `configured()` iff it is set and not `unconfigured` (throws for `throw:configured`); calls recorded on `globalThis.__fakeWeb` (`fakeWebProbe`) |
|
|
25
25
|
* | `fake-context` | `context@1` scripted by its deployment config (`text`, default "fake context"; `mode` `ok` / `null` / `throw` / `hang`); calls recorded on `globalThis.__fakeContext` (`fakeContextProbe`); tool `note` (`sources` web and channel_dm) |
|
|
26
26
|
* | `fake-client` | client extension (`permissions.client`, no extension point): components `hud` (`conversation.properties.section`, server view with `data`), `brand` (`shell.brand`, renders `builtin` in `data-testid="fake-client-brand"`), `empty` (`conversation.empty`), `frame` (`conversation.composer.frame`) and `fx` (`shell.overlay`); `client/styles/fake.css` (outline-color on `button.primary` and the composer's buttons, `--cx-fake-client-run` while `run=running`); an `events` listener storing the last event type; theme `fake`; `suggestedAppearance` = theme `fake` + `shell.brand` -> `brand` |
|
|
27
|
+
* | `fake-watch` | tool `wait` (input `{ key, deadlineMs? }`): a watched input wait whose watcher reads kv `fake-watch:<key>` (`go` settles, `fail` throws, `hang` never answers, else pending), counts its polls in kv `fake-watch:<key>:polls` and writes the identity it got to kv `fake-watch:<key>:ctx`; the resumed call returns `{ outcome, answer, signal }` as JSON; tool `nowatch` asks for a watched wait without a watcher |
|
|
27
28
|
* | `fake-client-broken` | client code that throws: component `boom` (render), a `run.started` events listener, and a `conversation.empty` replacement (render) |
|
|
28
29
|
* | `fake-push` | `push-provider@1` scripted by the token prefix (`dead:`, `auth:`, `rate:`, `flaky:<n>:`, `throw:`, `net:`, `hang:`; else delivered); deployment config `apps` (default `["com.example.app"]`), optional secret `key`; every send is a row of `plugin_fake_push.sends` and an entry of `fakePushProbe().sends`; `makeFakePush()` is the same provider without a kernel |
|
|
29
30
|
* | `fake-config-form` | display name 表单样例 / Form sample; no extension point: an openai-compatible-shaped deployment config (endpoints with a preset menu, an API key per endpoint in the secret record `apiKeys`, model rows, advanced fields, localized custom issues) carrying the config form hints of the extension config forms spec §6, for the web form e2e |
|
|
@@ -48,6 +49,7 @@ export const FAKE_EXTENSIONS = [
|
|
|
48
49
|
"fake-push",
|
|
49
50
|
"fake-client",
|
|
50
51
|
"fake-client-broken",
|
|
52
|
+
"fake-watch",
|
|
51
53
|
];
|
|
52
54
|
/**
|
|
53
55
|
* Fake plugins without an extension (declarative: they live in `fixtures/plugins/<name>`, same
|
package/dist/index.d.ts
CHANGED
|
@@ -25,7 +25,7 @@ export { FAKE_EXTENSIONS, type FakeExtensionName, fakeExtensionDir, fakeExtensio
|
|
|
25
25
|
export { cardRecorded, cookieRequest, createAgent, createConversation, createLocalAccount, createOrg, enableModelProviders, eventsOf, joinOrg, type KernelApp, type KernelRequest, type KernelRequestOptions, type KernelResponse, login, pidOwningLocalPort, postMessage, sleep, startTestKernel, TEST_PASSWORD, type TestKernel, testKernelConfig, truncateTables, waitFor, waitForEvent, waitForTerminal, } from "./kernel-helpers.js";
|
|
26
26
|
export { type LoginSourceSeed, seedLoginSources } from "./login-sources.js";
|
|
27
27
|
export { FAKE_PNG_BASE64, isPngBase64, pdfBase64, pdfSampleWord, redPngBase64, solidPngBase64, } from "./media-samples.js";
|
|
28
|
-
export { type ConformanceTarget, checkDeclaredParameters, describeModelProviderConformance, MEDIA_SAMPLES, thinkingReplayParams, } from "./model-conformance.js";
|
|
28
|
+
export { type ConformanceTarget, checkDeclaredParameters, checkToolStream, describeModelProviderConformance, MEDIA_SAMPLES, thinkingReplayParams, } from "./model-conformance.js";
|
|
29
29
|
export { describePushProviderConformance, type PushConformanceTarget, type PushOutcome, type PushVendorRequest, } from "./push-conformance.js";
|
|
30
30
|
export { type NativeFixtureEntry, runSandboxNativeConformance, type SandboxNativeFixture, } from "./sandbox-native-conformance.js";
|
|
31
31
|
export { runSandboxPermissionConformance, type SandboxPermissionConformancePart, type SandboxPermissionEndpoint, type SandboxPermissionFixture, type SandboxPermissionGrant, type SandboxPermissionOpenNetwork, type SandboxPermissionTarget, } from "./sandbox-permission-conformance.js";
|
package/dist/index.js
CHANGED
|
@@ -25,7 +25,7 @@ export { FAKE_EXTENSIONS, fakeExtensionDir, fakeExtensionEntry, packFixture, } f
|
|
|
25
25
|
export { cardRecorded, cookieRequest, createAgent, createConversation, createLocalAccount, createOrg, enableModelProviders, eventsOf, joinOrg, login, pidOwningLocalPort, postMessage, sleep, startTestKernel, TEST_PASSWORD, testKernelConfig, truncateTables, waitFor, waitForEvent, waitForTerminal, } from "./kernel-helpers.js";
|
|
26
26
|
export { seedLoginSources } from "./login-sources.js";
|
|
27
27
|
export { FAKE_PNG_BASE64, isPngBase64, pdfBase64, pdfSampleWord, redPngBase64, solidPngBase64, } from "./media-samples.js";
|
|
28
|
-
export { checkDeclaredParameters, describeModelProviderConformance, MEDIA_SAMPLES, thinkingReplayParams, } from "./model-conformance.js";
|
|
28
|
+
export { checkDeclaredParameters, checkToolStream, describeModelProviderConformance, MEDIA_SAMPLES, thinkingReplayParams, } from "./model-conformance.js";
|
|
29
29
|
export { describePushProviderConformance, } from "./push-conformance.js";
|
|
30
30
|
export { runSandboxNativeConformance, } from "./sandbox-native-conformance.js";
|
|
31
31
|
export { runSandboxPermissionConformance, } from "./sandbox-permission-conformance.js";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type ModelInfo, type ModelParameter, type ModelProvider, type ParamValue } from "@capybara-hq/plugin-sdk";
|
|
1
|
+
import { type ModelInfo, type ModelParameter, type ModelProvider, type ModelStreamEvent, type ParamValue } from "@capybara-hq/plugin-sdk";
|
|
2
2
|
interface TargetCommon {
|
|
3
3
|
model: string;
|
|
4
4
|
expect: {
|
|
@@ -26,6 +26,16 @@ export type ConformanceTarget = TargetCommon & {
|
|
|
26
26
|
plugin: string;
|
|
27
27
|
provider: ModelProvider;
|
|
28
28
|
};
|
|
29
|
+
/**
|
|
30
|
+
* Checks a stream's tool events against its final message: every delta follows its call's start,
|
|
31
|
+
* each call starts once, and the deltas of each started call join to JSON whose parsed value
|
|
32
|
+
* equals the final call's input (structurally: key order does not matter). A started call with
|
|
33
|
+
* no final call of its id is allowed. Returns the problems (empty when fine); a stream with no
|
|
34
|
+
* tool events passes.
|
|
35
|
+
*/
|
|
36
|
+
export declare function checkToolStream(events: ModelStreamEvent[], done: Extract<ModelStreamEvent, {
|
|
37
|
+
type: "message_complete";
|
|
38
|
+
}>): string[];
|
|
29
39
|
/**
|
|
30
40
|
* `@2` 参数声明检查:每个模型的 `parameters` 须通过 `assertValidDeclarations`,
|
|
31
41
|
* 每个声明的默认值须通过 `validateParams`。返回问题列表(形如 `<模型 id>: <原因>`),无问题为空。
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isDeepStrictEqual } from "node:util";
|
|
1
2
|
import { assertValidDeclarations, EFFORT_VALUES, isModelError, isValidImageGenerationInfo, sanitizeParams, validateEmbedResult, validateImageGenerateResult, validateParams, } from "@capybara-hq/plugin-sdk";
|
|
2
3
|
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
|
3
4
|
import { pdfBase64, redPngBase64 } from "./media-samples.js";
|
|
@@ -5,6 +6,47 @@ function streamOf(t, req, signal) {
|
|
|
5
6
|
const { params, ...rest } = req;
|
|
6
7
|
return t.provider.stream({ ...rest, params: params ?? {} }, { signal });
|
|
7
8
|
}
|
|
9
|
+
/**
|
|
10
|
+
* Checks a stream's tool events against its final message: every delta follows its call's start,
|
|
11
|
+
* each call starts once, and the deltas of each started call join to JSON whose parsed value
|
|
12
|
+
* equals the final call's input (structurally: key order does not matter). A started call with
|
|
13
|
+
* no final call of its id is allowed. Returns the problems (empty when fine); a stream with no
|
|
14
|
+
* tool events passes.
|
|
15
|
+
*/
|
|
16
|
+
export function checkToolStream(events, done) {
|
|
17
|
+
const problems = [];
|
|
18
|
+
const streamed = new Map();
|
|
19
|
+
for (const e of events) {
|
|
20
|
+
if (e.type === "tool_call_start") {
|
|
21
|
+
// a repeated start keeps the input streamed so far
|
|
22
|
+
if (streamed.has(e.id))
|
|
23
|
+
problems.push(`repeated tool_call_start for id ${e.id}`);
|
|
24
|
+
else
|
|
25
|
+
streamed.set(e.id, "");
|
|
26
|
+
}
|
|
27
|
+
else if (e.type === "tool_call_delta") {
|
|
28
|
+
const soFar = streamed.get(e.id);
|
|
29
|
+
if (soFar === undefined)
|
|
30
|
+
problems.push(`tool_call_delta for unknown id ${e.id}`);
|
|
31
|
+
else
|
|
32
|
+
streamed.set(e.id, soFar + e.input);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
for (const [id, json] of streamed) {
|
|
36
|
+
const call = done.content.find((b) => b.type === "tool_call" && b.id === id);
|
|
37
|
+
// a started call without a final call of that id is allowed (spec §3)
|
|
38
|
+
if (call?.type !== "tool_call")
|
|
39
|
+
continue;
|
|
40
|
+
let same = false;
|
|
41
|
+
try {
|
|
42
|
+
same = isDeepStrictEqual(JSON.parse(json), call.input);
|
|
43
|
+
}
|
|
44
|
+
catch { }
|
|
45
|
+
if (!same)
|
|
46
|
+
problems.push(`streamed input of ${id} does not parse to the final input`);
|
|
47
|
+
}
|
|
48
|
+
return problems;
|
|
49
|
+
}
|
|
8
50
|
/**
|
|
9
51
|
* `@2` 参数声明检查:每个模型的 `parameters` 须通过 `assertValidDeclarations`,
|
|
10
52
|
* 每个声明的默认值须通过 `validateParams`。返回问题列表(形如 `<模型 id>: <原因>`),无问题为空。
|
|
@@ -120,8 +162,9 @@ export function describeModelProviderConformance(name, setup) {
|
|
|
120
162
|
{ role: "user", content: [{ type: "text", text: "What is the weather in Paris?" }] },
|
|
121
163
|
],
|
|
122
164
|
});
|
|
123
|
-
const { done } = await collect(t, first);
|
|
165
|
+
const { events, done } = await collect(t, first);
|
|
124
166
|
expect(done.stopReason).toBe("tool_use");
|
|
167
|
+
expect(checkToolStream(events, done)).toEqual([]);
|
|
125
168
|
const call = done.content.find((b) => b.type === "tool_call");
|
|
126
169
|
expect(call).toBeDefined();
|
|
127
170
|
if (call?.type !== "tool_call")
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@capybara-hq/test-plugin-fake-watch",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": true,
|
|
5
|
+
"description": "Test fake: an extension tool with a watched input wait, scripted through ctx.kv",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"dependencies": {
|
|
9
|
+
"@capybara-hq/plugin-sdk": "^0.1.0-rc.0"
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defineExtension,
|
|
3
|
+
type ExtensionContext,
|
|
4
|
+
type InputRequired,
|
|
5
|
+
type ToolSpec,
|
|
6
|
+
type ToolWatchContext,
|
|
7
|
+
} from "@capybara-hq/plugin-sdk";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Test fake of a watched input wait (browser long waits spec §5): no official plugin needed.
|
|
11
|
+
* - tool `wait` (model-visible `plugin__fake-watch__wait`, default policy allow), input
|
|
12
|
+
* `{ key, deadlineMs? }`: the first call returns a watched `InputRequired` (one end button,
|
|
13
|
+
* state `{ key }`, `watch.deadlineMs` default 60 000); the resumed call returns, as JSON text,
|
|
14
|
+
* `{ outcome, answer, signal }` (`signal`: the kv value below when resumed).
|
|
15
|
+
* - its watcher reads kv `fake-watch:<key>`: `go` settles, `fail` throws, `hang` never answers
|
|
16
|
+
* (the kernel's timeout ends it), anything else is pending. Each call increments kv
|
|
17
|
+
* `fake-watch:<key>:polls` and writes kv `fake-watch:<key>:ctx` (JSON of the call identity the
|
|
18
|
+
* watcher got and the state).
|
|
19
|
+
* - tool `nowatch`: returns a watched `InputRequired` without a registered watcher.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
const WAIT: ToolSpec = {
|
|
23
|
+
name: "wait",
|
|
24
|
+
description: "Wait until the test sets a signal. Test fixture only.",
|
|
25
|
+
inputSchema: {
|
|
26
|
+
type: "object",
|
|
27
|
+
properties: {
|
|
28
|
+
key: { type: "string", minLength: 1, description: "The signal's key." },
|
|
29
|
+
deadlineMs: { type: "number", description: "The wait's deadline in milliseconds." },
|
|
30
|
+
},
|
|
31
|
+
required: ["key"],
|
|
32
|
+
additionalProperties: false,
|
|
33
|
+
},
|
|
34
|
+
defaultPolicy: "allow",
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const NOWATCH: ToolSpec = {
|
|
38
|
+
name: "nowatch",
|
|
39
|
+
description: "Ask for a watched wait without a watcher. Test fixture only.",
|
|
40
|
+
inputSchema: { type: "object", properties: {}, additionalProperties: false },
|
|
41
|
+
defaultPolicy: "allow",
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const TTL = { ttlSeconds: 3600 };
|
|
45
|
+
|
|
46
|
+
const waitForm = (key: string, deadlineMs: number): InputRequired => ({
|
|
47
|
+
kind: "input_required",
|
|
48
|
+
form: {
|
|
49
|
+
title: { zh: `等待信号 ${key}`, en: `Waiting for signal ${key}` },
|
|
50
|
+
fields: [
|
|
51
|
+
{
|
|
52
|
+
key: "end",
|
|
53
|
+
label: { zh: "结束等待", en: "End the wait" },
|
|
54
|
+
schema: { type: "string", enum: ["end"] },
|
|
55
|
+
required: true,
|
|
56
|
+
choices: [{ value: "end", label: { zh: "结束等待", en: "End wait" } }],
|
|
57
|
+
},
|
|
58
|
+
],
|
|
59
|
+
},
|
|
60
|
+
state: { key },
|
|
61
|
+
watch: { deadlineMs },
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
async function watch(ctx: ExtensionContext, w: ToolWatchContext): Promise<"settle" | "pending"> {
|
|
65
|
+
const key = (w.state as { key?: string } | null)?.key ?? "";
|
|
66
|
+
await ctx.kv.incr(`fake-watch:${key}:polls`, TTL);
|
|
67
|
+
await ctx.kv.set(
|
|
68
|
+
`fake-watch:${key}:ctx`,
|
|
69
|
+
JSON.stringify({
|
|
70
|
+
orgId: w.orgId,
|
|
71
|
+
accountId: w.accountId,
|
|
72
|
+
ownerAccountId: w.ownerAccountId,
|
|
73
|
+
conversationId: w.conversationId,
|
|
74
|
+
rootConversationId: w.rootConversationId,
|
|
75
|
+
callId: w.callId,
|
|
76
|
+
idempotencyKey: w.idempotencyKey,
|
|
77
|
+
role: w.role,
|
|
78
|
+
source: w.source,
|
|
79
|
+
state: w.state,
|
|
80
|
+
}),
|
|
81
|
+
TTL,
|
|
82
|
+
);
|
|
83
|
+
const signal = await ctx.kv.get(`fake-watch:${key}`);
|
|
84
|
+
if (signal === "go") return "settle";
|
|
85
|
+
if (signal === "fail") throw new Error("scripted watcher failure");
|
|
86
|
+
if (signal === "hang") return new Promise<never>(() => {});
|
|
87
|
+
return "pending";
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export default defineExtension({
|
|
91
|
+
name: "fake-watch",
|
|
92
|
+
version: "0.1.0",
|
|
93
|
+
sdk: "^0.1.0-rc.0",
|
|
94
|
+
provides: [],
|
|
95
|
+
permissions: { tools: ["wait", "nowatch"] },
|
|
96
|
+
setup(ctx) {
|
|
97
|
+
ctx.tools.register(
|
|
98
|
+
WAIT,
|
|
99
|
+
async (input, call) => {
|
|
100
|
+
const { key, deadlineMs } = input as { key: string; deadlineMs?: number };
|
|
101
|
+
if (call.resume === undefined) return waitForm(key, deadlineMs ?? 60_000);
|
|
102
|
+
const signal = await ctx.kv.get(`fake-watch:${key}`);
|
|
103
|
+
await ctx.kv.set(
|
|
104
|
+
`fake-watch:${key}:handler`,
|
|
105
|
+
JSON.stringify({
|
|
106
|
+
callId: call.callId,
|
|
107
|
+
idempotencyKey: call.idempotencyKey,
|
|
108
|
+
conversationId: call.conversationId,
|
|
109
|
+
rootConversationId: call.rootConversationId,
|
|
110
|
+
}),
|
|
111
|
+
TTL,
|
|
112
|
+
);
|
|
113
|
+
return {
|
|
114
|
+
content: [
|
|
115
|
+
{
|
|
116
|
+
type: "text",
|
|
117
|
+
text: JSON.stringify({
|
|
118
|
+
outcome: call.resume.outcome,
|
|
119
|
+
answer: call.resume.answer,
|
|
120
|
+
signal,
|
|
121
|
+
}),
|
|
122
|
+
},
|
|
123
|
+
],
|
|
124
|
+
};
|
|
125
|
+
},
|
|
126
|
+
{ watch: (w) => watch(ctx, w) },
|
|
127
|
+
);
|
|
128
|
+
ctx.tools.register(NOWATCH, async () => waitForm("none", 60_000));
|
|
129
|
+
},
|
|
130
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@capybara-hq/testing",
|
|
3
|
-
"version": "0.1.0-rc.
|
|
3
|
+
"version": "0.1.0-rc.45",
|
|
4
4
|
"description": "Test helpers for Capybara: test containers, dual-replica harness and fake third-party servers",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -47,17 +47,17 @@
|
|
|
47
47
|
"tsx": "^4.23.15",
|
|
48
48
|
"ws": "^8.22.0",
|
|
49
49
|
"zod": "^4.6.7",
|
|
50
|
-
"@capybara-hq/kernel": "^0.1.0-rc.
|
|
50
|
+
"@capybara-hq/kernel": "^0.1.0-rc.100"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@types/pg": "^8.23.1",
|
|
54
54
|
"@types/ws": "^8.18.2",
|
|
55
|
-
"@capybara-hq/plugin-
|
|
56
|
-
"@capybara-hq/plugin-
|
|
55
|
+
"@capybara-hq/plugin-client": "^0.1.0-rc.9",
|
|
56
|
+
"@capybara-hq/plugin-sdk": "^0.1.0-rc.83"
|
|
57
57
|
},
|
|
58
58
|
"peerDependencies": {
|
|
59
59
|
"vitest": "^5.0.5",
|
|
60
|
-
"@capybara-hq/plugin-sdk": "^0.1.0-rc.
|
|
60
|
+
"@capybara-hq/plugin-sdk": "^0.1.0-rc.83",
|
|
61
61
|
"@capybara-hq/plugin-tools": "^0.1.0-rc.25"
|
|
62
62
|
},
|
|
63
63
|
"peerDependenciesMeta": {
|