@useavalon/caelence-agent 0.2.5-canary.3 → 0.3.0
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 +52 -0
- package/package.json +3 -3
- package/src/browser/browser-use.test.ts +131 -0
- package/src/browser/browser-use.ts +145 -0
- package/src/config.ts +16 -0
- package/src/core/approval.ts +18 -2
- package/src/core/run-turn.ts +77 -0
- package/src/core/stdio-mcp.test.ts +120 -0
- package/src/core/stdio-mcp.ts +64 -4
- package/src/decisions/approval.ts +47 -0
- package/src/decisions/client.ts +134 -0
- package/src/decisions/config.ts +83 -0
- package/src/decisions/decisions.test.ts +365 -0
- package/src/decisions/integration.test.ts +473 -0
- package/src/decisions/memory.ts +37 -0
- package/src/decisions/shortcuts.ts +28 -0
- package/src/index.ts +22 -0
- package/src/runtime.test.ts +167 -0
- package/src/runtime.ts +80 -5
|
@@ -0,0 +1,473 @@
|
|
|
1
|
+
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
|
|
2
|
+
import { mkdtemp, rm } from "node:fs/promises";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { DEFAULT_CONFIG } from "../config.ts";
|
|
6
|
+
import { createExecApprovalGate } from "../core/approval.ts";
|
|
7
|
+
import type { AgentEvent, MainModelProvider } from "../core/events.ts";
|
|
8
|
+
import { textResult, UNKNOWN_TOOL_ANNOTATIONS } from "../core/mcp.ts";
|
|
9
|
+
import { type RunTurnDeps, runTurn } from "../core/run-turn.ts";
|
|
10
|
+
import { createMemorySessionStore } from "../core/session.ts";
|
|
11
|
+
import type { ConstructAgent } from "../core/strands.ts";
|
|
12
|
+
import { noopObservability } from "../observability/noop.ts";
|
|
13
|
+
import { createHarness } from "../runtime.ts";
|
|
14
|
+
import { DEFAULT_JEV_MODEL, type FetchImpl } from "./client.ts";
|
|
15
|
+
import type { ResolvedDecisions } from "./config.ts";
|
|
16
|
+
import type { ShortcutPlanner } from "./shortcuts.ts";
|
|
17
|
+
|
|
18
|
+
const fakeProvider: MainModelProvider = {
|
|
19
|
+
kind: "fake",
|
|
20
|
+
modelId: "fake",
|
|
21
|
+
async *stream() {
|
|
22
|
+
yield { type: "done" };
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const resolved: ResolvedDecisions = {
|
|
27
|
+
model: DEFAULT_JEV_MODEL,
|
|
28
|
+
minConfidence: 0.6,
|
|
29
|
+
approvalThreshold: 0.9,
|
|
30
|
+
shortcuts: true,
|
|
31
|
+
approvals: true,
|
|
32
|
+
memory: true,
|
|
33
|
+
apiKey: "sk-test",
|
|
34
|
+
baseUrl: "https://openrouter.ai/api/v1",
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
function openTool(opened: string[], fail = false) {
|
|
38
|
+
return {
|
|
39
|
+
name: "open_url",
|
|
40
|
+
description: "open",
|
|
41
|
+
inputSchema: { type: "object", properties: {} },
|
|
42
|
+
annotations: UNKNOWN_TOOL_ANNOTATIONS,
|
|
43
|
+
async handler(input: Record<string, unknown>) {
|
|
44
|
+
opened.push(String(input.url));
|
|
45
|
+
return fail ? textResult("could not open", true) : textResult("ok");
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function agentSpy() {
|
|
51
|
+
const seen = { ran: 0, system: "" };
|
|
52
|
+
const constructAgent: ConstructAgent = (input) => {
|
|
53
|
+
seen.system = input.systemPrompt;
|
|
54
|
+
return {
|
|
55
|
+
async run(_message, emit) {
|
|
56
|
+
seen.ran += 1;
|
|
57
|
+
emit({ kind: "text_delta", text: "from the agent" });
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
return { seen, constructAgent };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function deps(
|
|
65
|
+
cwd: string,
|
|
66
|
+
tool: ReturnType<typeof openTool>,
|
|
67
|
+
spy: ReturnType<typeof agentSpy>,
|
|
68
|
+
extra: Partial<RunTurnDeps>,
|
|
69
|
+
): RunTurnDeps {
|
|
70
|
+
return {
|
|
71
|
+
cwd,
|
|
72
|
+
name: "test",
|
|
73
|
+
systemPrompt: "base",
|
|
74
|
+
tools: [tool],
|
|
75
|
+
approval: createExecApprovalGate({ policy: "auto" }),
|
|
76
|
+
store: createMemorySessionStore(),
|
|
77
|
+
provider: fakeProvider,
|
|
78
|
+
constructAgent: spy.constructAgent,
|
|
79
|
+
observability: noopObservability,
|
|
80
|
+
decisions: resolved,
|
|
81
|
+
...extra,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async function collect(run: (emit: (e: AgentEvent) => void) => Promise<unknown>) {
|
|
86
|
+
const events: AgentEvent[] = [];
|
|
87
|
+
await run((e) => events.push(e));
|
|
88
|
+
return events;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
describe("shortcut seam", () => {
|
|
92
|
+
const plan: ShortcutPlanner = async ({ message }) =>
|
|
93
|
+
message.startsWith("open ")
|
|
94
|
+
? { toolName: "open_url", input: { url: message.slice(5) }, reply: "Opened it." }
|
|
95
|
+
: undefined;
|
|
96
|
+
|
|
97
|
+
test("answers with one tool call and never calls the agent", async () => {
|
|
98
|
+
const cwd = await mkdtemp(join(tmpdir(), "shortcut-"));
|
|
99
|
+
try {
|
|
100
|
+
const opened: string[] = [];
|
|
101
|
+
const spy = agentSpy();
|
|
102
|
+
const d = deps(cwd, openTool(opened), spy, { shortcut: plan });
|
|
103
|
+
const events = await collect((emit) => runTurn(d, { message: "open https://a.dev" }, emit));
|
|
104
|
+
expect(opened).toEqual(["https://a.dev"]);
|
|
105
|
+
expect(spy.seen.ran).toBe(0);
|
|
106
|
+
const kinds = events.map((e) => e.kind);
|
|
107
|
+
expect(kinds).toContain("tool_call_start");
|
|
108
|
+
expect(kinds).toContain("tool_call_end");
|
|
109
|
+
expect(events.find((e) => e.kind === "text_delta")).toMatchObject({ text: "Opened it." });
|
|
110
|
+
expect(kinds.at(-1)).toBe("completion");
|
|
111
|
+
} finally {
|
|
112
|
+
await rm(cwd, { recursive: true, force: true });
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
test("saves the reply into the session", async () => {
|
|
117
|
+
const cwd = await mkdtemp(join(tmpdir(), "shortcut-"));
|
|
118
|
+
try {
|
|
119
|
+
const spy = agentSpy();
|
|
120
|
+
const d = deps(cwd, openTool([]), spy, { shortcut: plan });
|
|
121
|
+
const session = await runTurn(d, { message: "open https://a.dev" }, () => undefined);
|
|
122
|
+
const roles = session?.messages.map((m) => (m.kind === "turn" ? m.role : m.kind));
|
|
123
|
+
expect(roles?.filter((r) => r !== "system")).toEqual(["user", "tool", "assistant"]);
|
|
124
|
+
} finally {
|
|
125
|
+
await rm(cwd, { recursive: true, force: true });
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test("a storage failure after a shortcut ends the turn with an error, not a rejection", async () => {
|
|
130
|
+
const cwd = await mkdtemp(join(tmpdir(), "shortcut-"));
|
|
131
|
+
try {
|
|
132
|
+
const spy = agentSpy();
|
|
133
|
+
const real = createMemorySessionStore();
|
|
134
|
+
let appends = 0;
|
|
135
|
+
const store: typeof real = {
|
|
136
|
+
...real,
|
|
137
|
+
async append(session, message) {
|
|
138
|
+
// Let the user turn and tool row save; fail on the final assistant reply.
|
|
139
|
+
if (message.kind === "turn" && message.role === "assistant") {
|
|
140
|
+
appends += 1;
|
|
141
|
+
throw new Error("disk full");
|
|
142
|
+
}
|
|
143
|
+
return real.append(session, message);
|
|
144
|
+
},
|
|
145
|
+
};
|
|
146
|
+
const d = deps(cwd, openTool([]), spy, { shortcut: plan, store });
|
|
147
|
+
const events: AgentEvent[] = [];
|
|
148
|
+
const session = await runTurn(d, { message: "open https://a.dev" }, (e) => events.push(e));
|
|
149
|
+
expect(appends).toBe(1);
|
|
150
|
+
expect(session).toBeDefined();
|
|
151
|
+
expect(spy.seen.ran).toBe(0);
|
|
152
|
+
const last = events.at(-1);
|
|
153
|
+
expect(last).toMatchObject({ kind: "error", message: "disk full" });
|
|
154
|
+
expect(events.some((e) => e.kind === "completion")).toBe(false);
|
|
155
|
+
} finally {
|
|
156
|
+
await rm(cwd, { recursive: true, force: true });
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
test("falls through to the agent when the planner passes", async () => {
|
|
161
|
+
const cwd = await mkdtemp(join(tmpdir(), "shortcut-"));
|
|
162
|
+
try {
|
|
163
|
+
const opened: string[] = [];
|
|
164
|
+
const spy = agentSpy();
|
|
165
|
+
const d = deps(cwd, openTool(opened), spy, { shortcut: plan });
|
|
166
|
+
await collect((emit) => runTurn(d, { message: "explain closures" }, emit));
|
|
167
|
+
expect(spy.seen.ran).toBe(1);
|
|
168
|
+
expect(opened).toEqual([]);
|
|
169
|
+
} finally {
|
|
170
|
+
await rm(cwd, { recursive: true, force: true });
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
test("falls through when the tool fails, throws, or is missing", async () => {
|
|
175
|
+
const cwd = await mkdtemp(join(tmpdir(), "shortcut-"));
|
|
176
|
+
try {
|
|
177
|
+
const cases: Array<[string, ShortcutPlanner, boolean]> = [
|
|
178
|
+
["tool error", plan, true],
|
|
179
|
+
[
|
|
180
|
+
"planner throws",
|
|
181
|
+
async () => {
|
|
182
|
+
throw new Error("boom");
|
|
183
|
+
},
|
|
184
|
+
false,
|
|
185
|
+
],
|
|
186
|
+
["unknown tool", async () => ({ toolName: "nope", input: {}, reply: "x" }), false],
|
|
187
|
+
];
|
|
188
|
+
for (const [, planner, failTool] of cases) {
|
|
189
|
+
const spy = agentSpy();
|
|
190
|
+
const d = deps(cwd, openTool([], failTool), spy, { shortcut: planner });
|
|
191
|
+
await collect((emit) => runTurn(d, { message: "open https://a.dev" }, emit));
|
|
192
|
+
expect(spy.seen.ran).toBe(1);
|
|
193
|
+
}
|
|
194
|
+
} finally {
|
|
195
|
+
await rm(cwd, { recursive: true, force: true });
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
test("continueWithAgent runs the tool, then the agent with the note", async () => {
|
|
200
|
+
const cwd = await mkdtemp(join(tmpdir(), "shortcut-"));
|
|
201
|
+
try {
|
|
202
|
+
const opened: string[] = [];
|
|
203
|
+
const spy = agentSpy();
|
|
204
|
+
const d = deps(cwd, openTool(opened), spy, {
|
|
205
|
+
shortcut: async () => ({
|
|
206
|
+
toolName: "open_url",
|
|
207
|
+
input: { url: "https://a.dev" },
|
|
208
|
+
reply: "",
|
|
209
|
+
continueWithAgent: true,
|
|
210
|
+
agentNote: "The site is already open.",
|
|
211
|
+
}),
|
|
212
|
+
});
|
|
213
|
+
await collect((emit) => runTurn(d, { message: "open a.dev and read it" }, emit));
|
|
214
|
+
expect(opened).toEqual(["https://a.dev"]);
|
|
215
|
+
expect(spy.seen.ran).toBe(1);
|
|
216
|
+
expect(spy.seen.system).toContain("The site is already open.");
|
|
217
|
+
} finally {
|
|
218
|
+
await rm(cwd, { recursive: true, force: true });
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
test("is skipped when decisions are off, shortcuts are off, or the turn is an edit", async () => {
|
|
223
|
+
const cwd = await mkdtemp(join(tmpdir(), "shortcut-"));
|
|
224
|
+
try {
|
|
225
|
+
for (const extra of [
|
|
226
|
+
{ decisions: undefined },
|
|
227
|
+
{ decisions: { ...resolved, shortcuts: false } },
|
|
228
|
+
] as Array<Partial<RunTurnDeps>>) {
|
|
229
|
+
const opened: string[] = [];
|
|
230
|
+
const spy = agentSpy();
|
|
231
|
+
const d = deps(cwd, openTool(opened), spy, { shortcut: plan, ...extra });
|
|
232
|
+
await collect((emit) => runTurn(d, { message: "open https://a.dev" }, emit));
|
|
233
|
+
expect(opened).toEqual([]);
|
|
234
|
+
expect(spy.seen.ran).toBe(1);
|
|
235
|
+
}
|
|
236
|
+
} finally {
|
|
237
|
+
await rm(cwd, { recursive: true, force: true });
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
test("the shortcut tool call still goes through approval", async () => {
|
|
242
|
+
const cwd = await mkdtemp(join(tmpdir(), "shortcut-"));
|
|
243
|
+
try {
|
|
244
|
+
let asked = 0;
|
|
245
|
+
const exec = {
|
|
246
|
+
...openTool([]),
|
|
247
|
+
name: "exec",
|
|
248
|
+
async handler() {
|
|
249
|
+
return textResult("ran");
|
|
250
|
+
},
|
|
251
|
+
};
|
|
252
|
+
const spy = agentSpy();
|
|
253
|
+
const d = deps(cwd, exec, spy, {
|
|
254
|
+
approval: createExecApprovalGate({
|
|
255
|
+
policy: "prompt",
|
|
256
|
+
ask: async () => {
|
|
257
|
+
asked += 1;
|
|
258
|
+
return false;
|
|
259
|
+
},
|
|
260
|
+
}),
|
|
261
|
+
shortcut: async () => ({ toolName: "exec", input: { command: "ls" }, reply: "done" }),
|
|
262
|
+
});
|
|
263
|
+
await collect((emit) => runTurn(d, { message: "list files" }, emit));
|
|
264
|
+
expect(asked).toBe(1);
|
|
265
|
+
expect(spy.seen.ran).toBe(1);
|
|
266
|
+
} finally {
|
|
267
|
+
await rm(cwd, { recursive: true, force: true });
|
|
268
|
+
}
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
describe("runtime wiring", () => {
|
|
273
|
+
let home = "";
|
|
274
|
+
const prevHome = process.env.HARNESS_HOME;
|
|
275
|
+
beforeAll(async () => {
|
|
276
|
+
home = await mkdtemp(join(tmpdir(), "rt-dec-home-"));
|
|
277
|
+
process.env.HARNESS_HOME = home;
|
|
278
|
+
});
|
|
279
|
+
afterAll(async () => {
|
|
280
|
+
if (prevHome === undefined) delete process.env.HARNESS_HOME;
|
|
281
|
+
else process.env.HARNESS_HOME = prevHome;
|
|
282
|
+
await rm(home, { recursive: true, force: true });
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
const noulFetch = (value: number) => {
|
|
286
|
+
let calls = 0;
|
|
287
|
+
const impl: FetchImpl = async (_url, init) => {
|
|
288
|
+
calls += 1;
|
|
289
|
+
const body = JSON.parse(String(init?.body)) as { questions: Record<string, unknown> };
|
|
290
|
+
const key = Object.keys(body.questions)[0] ?? "";
|
|
291
|
+
return new Response(JSON.stringify({ answers: { [key]: { noul: value } } }));
|
|
292
|
+
};
|
|
293
|
+
return { impl, count: () => calls };
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
test("an injected test provider keeps decisions off, so no network", async () => {
|
|
297
|
+
const cwd = await mkdtemp(join(tmpdir(), "rt-dec-"));
|
|
298
|
+
try {
|
|
299
|
+
const { impl, count } = noulFetch(0.99);
|
|
300
|
+
let approvalRan = 0;
|
|
301
|
+
const harness = await createHarness({
|
|
302
|
+
cwd,
|
|
303
|
+
config: DEFAULT_CONFIG,
|
|
304
|
+
provider: fakeProvider,
|
|
305
|
+
decisionsFetch: impl,
|
|
306
|
+
constructAgent: (input) => ({
|
|
307
|
+
async run() {
|
|
308
|
+
const exec = input.tools.find((t) => t.name === "exec");
|
|
309
|
+
await exec?.invoke({ command: "echo hi" });
|
|
310
|
+
approvalRan += 1;
|
|
311
|
+
},
|
|
312
|
+
}),
|
|
313
|
+
observability: noopObservability,
|
|
314
|
+
});
|
|
315
|
+
process.env.OPENROUTER_API_KEY = "sk-test";
|
|
316
|
+
await harness.runTurn("say hi", () => undefined, { approvalAsk: async () => false });
|
|
317
|
+
expect(approvalRan).toBe(1);
|
|
318
|
+
expect(count()).toBe(0);
|
|
319
|
+
harness.close();
|
|
320
|
+
} finally {
|
|
321
|
+
delete process.env.OPENROUTER_API_KEY;
|
|
322
|
+
await rm(cwd, { recursive: true, force: true });
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
test("with decisions on, a clearly safe exec skips the prompt", async () => {
|
|
327
|
+
const cwd = await mkdtemp(join(tmpdir(), "rt-dec-"));
|
|
328
|
+
try {
|
|
329
|
+
const { impl, count } = noulFetch(0.97);
|
|
330
|
+
let asked = 0;
|
|
331
|
+
const events: AgentEvent[] = [];
|
|
332
|
+
const harness = await createHarness({
|
|
333
|
+
cwd,
|
|
334
|
+
config: DEFAULT_CONFIG,
|
|
335
|
+
provider: fakeProvider,
|
|
336
|
+
decisions: true,
|
|
337
|
+
decisionsFetch: impl,
|
|
338
|
+
constructAgent: (input) => ({
|
|
339
|
+
async run() {
|
|
340
|
+
await input.tools.find((t) => t.name === "exec")?.invoke({ command: "echo hi" });
|
|
341
|
+
},
|
|
342
|
+
}),
|
|
343
|
+
observability: noopObservability,
|
|
344
|
+
});
|
|
345
|
+
harness.setApiKey("sk-test");
|
|
346
|
+
await harness.runTurn("run echo hi", (e) => events.push(e), {
|
|
347
|
+
approvalAsk: async () => {
|
|
348
|
+
asked += 1;
|
|
349
|
+
return false;
|
|
350
|
+
},
|
|
351
|
+
});
|
|
352
|
+
expect(count()).toBe(1);
|
|
353
|
+
expect(asked).toBe(0);
|
|
354
|
+
expect(events.some((e) => e.kind === "approval_request")).toBe(false);
|
|
355
|
+
harness.close();
|
|
356
|
+
} finally {
|
|
357
|
+
delete process.env.OPENROUTER_API_KEY;
|
|
358
|
+
await rm(cwd, { recursive: true, force: true });
|
|
359
|
+
}
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
test("with decisions on, an unsure score still asks the user", async () => {
|
|
363
|
+
const cwd = await mkdtemp(join(tmpdir(), "rt-dec-"));
|
|
364
|
+
try {
|
|
365
|
+
const { impl } = noulFetch(0.4);
|
|
366
|
+
let asked = 0;
|
|
367
|
+
const harness = await createHarness({
|
|
368
|
+
cwd,
|
|
369
|
+
config: DEFAULT_CONFIG,
|
|
370
|
+
provider: fakeProvider,
|
|
371
|
+
decisions: true,
|
|
372
|
+
decisionsFetch: impl,
|
|
373
|
+
constructAgent: (input) => ({
|
|
374
|
+
async run() {
|
|
375
|
+
await input.tools.find((t) => t.name === "exec")?.invoke({ command: "echo hi" });
|
|
376
|
+
},
|
|
377
|
+
}),
|
|
378
|
+
observability: noopObservability,
|
|
379
|
+
});
|
|
380
|
+
harness.setApiKey("sk-test");
|
|
381
|
+
await harness.runTurn("run echo hi", () => undefined, {
|
|
382
|
+
approvalAsk: async () => {
|
|
383
|
+
asked += 1;
|
|
384
|
+
return false;
|
|
385
|
+
},
|
|
386
|
+
});
|
|
387
|
+
expect(asked).toBe(1);
|
|
388
|
+
harness.close();
|
|
389
|
+
} finally {
|
|
390
|
+
delete process.env.OPENROUTER_API_KEY;
|
|
391
|
+
await rm(cwd, { recursive: true, force: true });
|
|
392
|
+
}
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
test("config can turn decisions off even with a key", async () => {
|
|
396
|
+
const cwd = await mkdtemp(join(tmpdir(), "rt-dec-"));
|
|
397
|
+
try {
|
|
398
|
+
const { impl, count } = noulFetch(0.99);
|
|
399
|
+
let asked = 0;
|
|
400
|
+
const harness = await createHarness({
|
|
401
|
+
cwd,
|
|
402
|
+
config: { ...DEFAULT_CONFIG, decisions: { enabled: false } },
|
|
403
|
+
provider: fakeProvider,
|
|
404
|
+
decisions: true,
|
|
405
|
+
decisionsFetch: impl,
|
|
406
|
+
constructAgent: (input) => ({
|
|
407
|
+
async run() {
|
|
408
|
+
await input.tools.find((t) => t.name === "exec")?.invoke({ command: "echo hi" });
|
|
409
|
+
},
|
|
410
|
+
}),
|
|
411
|
+
observability: noopObservability,
|
|
412
|
+
});
|
|
413
|
+
harness.setApiKey("sk-test");
|
|
414
|
+
await harness.runTurn("run echo hi", () => undefined, {
|
|
415
|
+
approvalAsk: async () => {
|
|
416
|
+
asked += 1;
|
|
417
|
+
return false;
|
|
418
|
+
},
|
|
419
|
+
});
|
|
420
|
+
expect(count()).toBe(0);
|
|
421
|
+
expect(asked).toBe(1);
|
|
422
|
+
harness.close();
|
|
423
|
+
} finally {
|
|
424
|
+
delete process.env.OPENROUTER_API_KEY;
|
|
425
|
+
await rm(cwd, { recursive: true, force: true });
|
|
426
|
+
}
|
|
427
|
+
});
|
|
428
|
+
|
|
429
|
+
test("memory: a turn with nothing durable skips the extraction call", async () => {
|
|
430
|
+
const { writeMemoryPrefs } = await import("../memory/prefs.ts");
|
|
431
|
+
writeMemoryPrefs({ enabled: true });
|
|
432
|
+
for (const [score, expectedStreams] of [
|
|
433
|
+
[0.01, 0],
|
|
434
|
+
[0.8, 1],
|
|
435
|
+
] as const) {
|
|
436
|
+
const cwd = await mkdtemp(join(tmpdir(), "rt-dec-"));
|
|
437
|
+
try {
|
|
438
|
+
const { impl } = noulFetch(score);
|
|
439
|
+
let extractCalls = 0;
|
|
440
|
+
const provider: MainModelProvider = {
|
|
441
|
+
kind: "fake",
|
|
442
|
+
modelId: "fake",
|
|
443
|
+
async *stream() {
|
|
444
|
+
extractCalls += 1;
|
|
445
|
+
yield { type: "text_delta", text: "[]" };
|
|
446
|
+
yield { type: "done" };
|
|
447
|
+
},
|
|
448
|
+
};
|
|
449
|
+
const harness = await createHarness({
|
|
450
|
+
cwd,
|
|
451
|
+
config: { ...DEFAULT_CONFIG, decisions: { approvals: false } },
|
|
452
|
+
provider,
|
|
453
|
+
decisions: true,
|
|
454
|
+
decisionsFetch: impl,
|
|
455
|
+
constructAgent: () => ({
|
|
456
|
+
async run(_m, emit) {
|
|
457
|
+
emit({ kind: "text_delta", text: "ok" });
|
|
458
|
+
},
|
|
459
|
+
}),
|
|
460
|
+
observability: noopObservability,
|
|
461
|
+
});
|
|
462
|
+
harness.setApiKey("sk-test");
|
|
463
|
+
await harness.runTurn("I always prefer bun over npm", () => undefined);
|
|
464
|
+
await Bun.sleep(80);
|
|
465
|
+
expect(extractCalls).toBe(expectedStreams);
|
|
466
|
+
harness.close();
|
|
467
|
+
} finally {
|
|
468
|
+
await rm(cwd, { recursive: true, force: true });
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
delete process.env.OPENROUTER_API_KEY;
|
|
472
|
+
});
|
|
473
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { clipState, readNoulAnswer } from "./client.ts";
|
|
2
|
+
import { decide, type ResolvedDecisions } from "./config.ts";
|
|
3
|
+
|
|
4
|
+
/** Skip the extraction call only when Jev is quite sure there is nothing worth keeping. */
|
|
5
|
+
const SKIP_BELOW = 0.1;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Memory extraction costs a full chat call after every turn. One cheap yes/no question tells
|
|
9
|
+
* most turns apart from the few that hold a preference or project decision. Unsure, or any
|
|
10
|
+
* failure, means extract as before, so a bad answer can only cost a call, never a memory.
|
|
11
|
+
*/
|
|
12
|
+
export async function turnHasDurableFact(
|
|
13
|
+
decisions: ResolvedDecisions,
|
|
14
|
+
source: string,
|
|
15
|
+
signal?: AbortSignal,
|
|
16
|
+
): Promise<boolean> {
|
|
17
|
+
if (!decisions.memory) return true;
|
|
18
|
+
const json = await decide(
|
|
19
|
+
decisions,
|
|
20
|
+
{ turn: clipState(source, 3000) },
|
|
21
|
+
{
|
|
22
|
+
durable: {
|
|
23
|
+
type: "noul",
|
|
24
|
+
instructions:
|
|
25
|
+
"Does this exchange state a lasting user preference, tool or stack choice, project decision, or convention worth remembering in later chats?",
|
|
26
|
+
criteria: {
|
|
27
|
+
true: "The user states how they like things done, picks a tool or approach, or records a decision that will still matter later.",
|
|
28
|
+
false:
|
|
29
|
+
"A one-off task, a question, a code change, an error, or small talk with nothing that outlasts this chat.",
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
signal,
|
|
34
|
+
);
|
|
35
|
+
const durable = readNoulAnswer(json, "durable");
|
|
36
|
+
return durable === null || durable >= SKIP_BELOW;
|
|
37
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { ResolvedDecisions } from "./config.ts";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A host can answer a message with one direct tool call and skip the chat model entirely
|
|
5
|
+
* ("open example.com"). The harness owns the mechanism: it checks approval, runs the tool,
|
|
6
|
+
* records the turn, and falls back to the agent on any failure. The host owns the judgment
|
|
7
|
+
* of which messages qualify, usually with a Jev choice question via {@link decide}.
|
|
8
|
+
*/
|
|
9
|
+
export interface ShortcutPlan {
|
|
10
|
+
toolName: string;
|
|
11
|
+
input: Record<string, unknown>;
|
|
12
|
+
/** Final answer shown when the tool succeeds. */
|
|
13
|
+
reply: string;
|
|
14
|
+
/** Run the tool, then let the agent carry on instead of ending the turn. */
|
|
15
|
+
continueWithAgent?: boolean;
|
|
16
|
+
/** Added to the system prompt when continueWithAgent is set. */
|
|
17
|
+
agentNote?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface ShortcutContext {
|
|
21
|
+
message: string;
|
|
22
|
+
/** Tools available this turn, after mode filtering. */
|
|
23
|
+
toolNames: ReadonlySet<string>;
|
|
24
|
+
decisions: ResolvedDecisions;
|
|
25
|
+
signal?: AbortSignal;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export type ShortcutPlanner = (ctx: ShortcutContext) => Promise<ShortcutPlan | undefined>;
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
export type { BrowserConfig } from "./browser/browser-use.ts";
|
|
1
2
|
export {
|
|
2
3
|
CAELENCE_THEME,
|
|
3
4
|
PLAIN_THEME,
|
|
@@ -26,6 +27,7 @@ export {
|
|
|
26
27
|
export type { AgentEvent, AgentEventEmitter, MainModelProvider } from "./core/events.ts";
|
|
27
28
|
export { isTerminalAgentEvent } from "./core/events.ts";
|
|
28
29
|
export { createHookRunner, type HookConfig, type HookRunner } from "./core/hooks.ts";
|
|
30
|
+
export { newId } from "./core/ids.ts";
|
|
29
31
|
export {
|
|
30
32
|
type InstructionLayers,
|
|
31
33
|
loadInstructionLayers,
|
|
@@ -42,6 +44,7 @@ export {
|
|
|
42
44
|
READ_ONLY_ANNOTATIONS,
|
|
43
45
|
resolveToolAnnotations,
|
|
44
46
|
textResult,
|
|
47
|
+
toolResultText,
|
|
45
48
|
UNKNOWN_TOOL_ANNOTATIONS,
|
|
46
49
|
} from "./core/mcp.ts";
|
|
47
50
|
export {
|
|
@@ -68,6 +71,25 @@ export {
|
|
|
68
71
|
titleFromUserMessage,
|
|
69
72
|
} from "./core/session.ts";
|
|
70
73
|
export { addUsage, formatUsage, parseTokenUsage, type TokenUsage } from "./core/usage.ts";
|
|
74
|
+
export { type ApprovalVerdict, classifyToolApproval } from "./decisions/approval.ts";
|
|
75
|
+
export {
|
|
76
|
+
type ChoiceAnswer,
|
|
77
|
+
clipState,
|
|
78
|
+
DEFAULT_JEV_MODEL,
|
|
79
|
+
type DecisionsResponse,
|
|
80
|
+
decisionsUrl,
|
|
81
|
+
readChoiceAnswer,
|
|
82
|
+
readNoulAnswer,
|
|
83
|
+
submitDecisions,
|
|
84
|
+
} from "./decisions/client.ts";
|
|
85
|
+
export {
|
|
86
|
+
decide,
|
|
87
|
+
type HarnessDecisionsConfig,
|
|
88
|
+
type ResolvedDecisions,
|
|
89
|
+
resolveDecisions,
|
|
90
|
+
} from "./decisions/config.ts";
|
|
91
|
+
export { turnHasDurableFact } from "./decisions/memory.ts";
|
|
92
|
+
export type { ShortcutContext, ShortcutPlan, ShortcutPlanner } from "./decisions/shortcuts.ts";
|
|
71
93
|
export {
|
|
72
94
|
type ChatFn,
|
|
73
95
|
createOpenRouterChat,
|