1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
import { test } from "node:test";
import { load } from "../../../atlibs/rust/src/harness.mjs";
import { allowlist, preset, rows, runAgent, serverLines } from "./agent.js";
const { module, run } = await load(new URL("modules/asyncai.wasm", import.meta.url));
const mod = { module, ...JSON.parse(await readFile(new URL("modules/asyncai.json", import.meta.url), "utf8")) };
test("presets and allowlist: co/core by the session's service auth, keys on the inference origin only, MCP origins bare", () => {
const mcp = [{ name: "d", url: "https://mcp.example/mcp" }, { name: "c", hook: "x.y.z" }];
assert.deepEqual(allowlist({ ...preset({ backend: "cocore" }), mcp }), {
"https://mcp.example": {},
"https://cocore.dev": { serviceAuth: { aud: "did:web:cocore.dev", lxm: "dev.cocore.inference.dispatch" } },
});
assert.deepEqual(allowlist({ ...preset({ backend: "anthropic" }), apiKey: "k", mcp }), {
"https://mcp.example": {},
"https://api.anthropic.com": { headers: { "x-api-key": "k", "anthropic-dangerous-direct-browser-access": "true" } },
});
assert.deepEqual(preset({ backend: "custom", endpoint: "https://api.deepseek.com/anthropic" }), { provider: "anthropic", endpoint: "https://api.deepseek.com/anthropic", auth: "key" });
assert.deepEqual(allowlist({ ...preset({ backend: "custom", endpoint: "https://api.deepseek.com/anthropic" }) }), { "https://api.deepseek.com": { headers: {} } });
assert.deepEqual(allowlist({ ...preset({ backend: "custom", endpoint: "https://llm.example/v1", format: "openai" }), apiKey: "t" }), { "https://llm.example": { headers: { authorization: "Bearer t" } } });
assert.throws(() => preset({ backend: "custom" }), /base URL/);
});
test("runAgent co/core: OpenAI chat completions with a token the host mints from the signed-in session", async () => {
const minted = [];
const session = { did: "did:plc:alice", fetchHandler: async (path) => (minted.push(path), Response.json({ token: "svc" })) };
const sent = [];
const ctx = {
wasm: { run: async ({ entry, input }) => run(entry, input) },
http: { fetch: async (url, init) => (sent.push([url, init.headers.authorization, JSON.parse(init.body)]), Response.json({ choices: [{ message: { role: "assistant", content: "hi" }, finish_reason: "stop" }], usage: { total_tokens: 3 } })) },
};
const earlier = [{ role: "user", content: "hello" }, { role: "assistant", content: "hey" }];
const ran = await runAgent(ctx, mod, { backend: "cocore", model: "stub", prompt: "again", messages: earlier, mcp: [] }, { session });
assert.equal(ran.error, undefined, ran.error);
assert.equal(ran.result.text, "hi");
assert.deepEqual(sent.map(([url, auth, body]) => [url, auth, body.model, body.messages.length]), [["https://cocore.dev/api/v1/chat/completions", "Bearer svc", "stub", 3]]);
assert.match(minted[0], /getServiceAuth\?aud=did%3Aweb%3Acocore.dev&lxm=dev.cocore.inference.dispatch/);
assert.deepEqual(rows(ran.result.messages).map((r) => r.text), ["hello", "hey", "again", "hi"]);
assert.throws(() => runAgent(ctx, mod, { backend: "cocore", model: "stub", prompt: "x" }), /sign in first/);
});
test("runAgent: progress per step, the transcript as rows, the key never in the module's input", async () => {
const seen = [];
const answers = [
{ content: [{ type: "thinking", thinking: "hm" }, { type: "text", text: "hello" }], stop_reason: "end_turn" },
];
const ctx = {
wasm: { run: async ({ entry, input }) => (seen.push(JSON.stringify(input)), run(entry, input)) },
http: { fetch: async (url, init) => (assert.equal(init.headers["x-api-key"], "SECRET"), Response.json(answers.shift())) },
};
const progress = [];
const ran = await runAgent(ctx, mod, { backend: "anthropic", prompt: "hi", apiKey: "SECRET", messages: [] }, { progress: (r) => progress.push(r) });
assert.equal(ran.result.text, "hello");
assert.deepEqual(rows(ran.result.messages), [
{ role: "user", kind: "text", text: "hi" },
{ role: "assistant", kind: "thinking", text: "hm" },
{ role: "assistant", kind: "text", text: "hello" },
]);
assert.ok(progress.length >= 2);
assert.deepEqual(progress.at(-1), rows(ran.result.messages), "the page follows the held transcript from the module's appends");
assert.ok(seen.every((input) => !input.includes("SECRET")));
});
test("runAgent: a compaction restarts the followed transcript at its summary, shown as a compaction row", async () => {
const answers = [
{ content: [{ type: "text", text: "<summary>said hi</summary>" }], stop_reason: "end_turn", usage: { input_tokens: 1 } },
{ content: [{ type: "text", text: "hello" }], stop_reason: "end_turn" },
];
const ctx = { wasm: { run: async ({ entry, input }) => run(entry, input) }, http: { fetch: async () => Response.json(answers.shift()) } };
const progress = [];
const earlier = [{ role: "user", content: "x".repeat(4000) }, { role: "assistant", content: [{ type: "text", text: "ok" }] }];
const ran = await runAgent(ctx, mod, { backend: "anthropic", prompt: "hi", messages: earlier, contextWindow: 1000 }, { progress: (r, info) => progress.push([r, info]) });
assert.equal(ran.result.compactions, 1);
assert.ok(progress.some(([, info]) => info.compacting));
const [last] = progress.at(-1);
assert.deepEqual(last, rows(ran.result.messages));
assert.equal(last[0].kind, "compaction");
assert.match(last[0].text, /said hi\n\nThe user's latest request: hi$/);
});
test("runAgent dynamicMcp: the prefixes' origins in the allowlist, the connected server in the lines", async () => {
const answers = [
{ content: [{ type: "tool_use", id: "c1", name: "mcp_connect", input: { name: "docs", server: { url: "https://mcp.example/a/mcp" } } }], stop_reason: "tool_use" },
{ content: [{ type: "text", text: "connected" }], stop_reason: "end_turn" },
];
const docs = { initialize: { protocolVersion: "2025-11-25" }, "tools/list": { tools: [{ name: "search", inputSchema: { type: "object" } }] } };
const ctx = {
wasm: { run: async ({ entry, input }) => run(entry, input) },
http: {
fetch: async (url, init) => {
if (!url.startsWith("https://mcp.example/")) return Response.json(answers.shift());
const m = JSON.parse(init.body);
return m.id === undefined ? new Response(null, { status: 202 }) : Response.json({ jsonrpc: "2.0", id: m.id, result: docs[m.method] });
},
},
};
assert.deepEqual(Object.keys(allowlist({ ...preset({ backend: "anthropic" }), dynamicMcp: ["https://mcp.example/a", "webrtc:///", "com.example.mcp.vm"] })), ["https://mcp.example", "webrtc://*", "https://api.anthropic.com"]);
const ran = await runAgent(ctx, mod, { backend: "anthropic", prompt: "connect", apiKey: "k", dynamicMcp: ["https://mcp.example/a"] });
assert.equal(ran.result.text, "connected");
assert.deepEqual(serverLines(ran.result.servers), ["docs · https://mcp.example/a/mcp · ready · 1 tools · connected by the agent"]);
});