-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathagent.ts
More file actions
67 lines (57 loc) · 2.46 KB
/
Copy pathagent.ts
File metadata and controls
67 lines (57 loc) · 2.46 KB
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
import { chidori, run, type BranchOutcome } from "chidori:agent";
/**
* Branching example (docs/branching-execution.md).
*
* The agent does shared "expensive" prefix work once, then calls
* `chidori.branch` to fork into two strategy modules from that anchored state.
* Each branch runs its OWN source file (editable independently under
* `strategies/`), receives the prefix's result as explicit `input`, and
* returns an outcome. The agent compares the outcomes and picks one — the fork
* is a controlled experiment: the shared prefix is identical, so the only
* variable is each branch's code.
*
* Durability: the whole fan-out is ONE recorded `branch` call. Replaying this
* run (`chidori resume examples/branching/agent.ts <run-id> --dir examples/branching`)
* returns the outcomes from the call log without re-running either branch.
*
* `summarizeBrief` is a local helper so the example runs offline with no LLM
* provider; swap it (and the strategies) for `chidori.prompt(...)` calls to
* see real model spans nested under each branch subtree in the trace.
*/
type Brief = { topic?: string };
run(async (input: Brief) => {
const topic = input.topic ?? "incident postmortem";
// Shared prefix: paid once, handed to every branch as state.
await chidori.log(`researching: ${topic}`);
const research = summarizeBrief(topic);
const outcomes = await chidori.branch([
{
label: "outline-first",
source: "examples/branching/strategies/outline_first.ts",
input: { topic, research },
},
{
label: "draft-direct",
source: "examples/branching/strategies/draft_direct.ts",
input: { topic, research },
},
]);
for (const outcome of outcomes) {
await chidori.log(
`branch ${outcome.label}: ${outcome.status}` +
(outcome.status === "failed" ? ` (${outcome.error})` : ""),
);
}
// Compare and pick: here, the longest completed draft wins.
const completed = outcomes.filter((o) => o.status === "completed");
const best = completed.reduce((a, b) => (score(a) >= score(b) ? a : b));
await chidori.log(`picked: ${best.label}`);
return { picked: best.label, draft: best.output, outcomes };
});
function score(outcome: BranchOutcome): number {
const draft = (outcome.output as { draft?: string } | undefined)?.draft ?? "";
return draft.length;
}
function summarizeBrief(topic: string): string {
return `key facts about ${topic}: timeline reconstructed; root cause identified; two follow-ups proposed`;
}