-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathharness.ts
More file actions
65 lines (54 loc) · 2.24 KB
/
harness.ts
File metadata and controls
65 lines (54 loc) · 2.24 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
import * as path from 'node:path';
import * as fs from 'node:fs';
import type { Operation, Channel } from 'effection';
import { Session } from '@lloyal-labs/sdk';
import {
Ctx, agentPool, DefaultAgentPolicy,
} from '@lloyal-labs/lloyal-agents';
import type { Tool } from '@lloyal-labs/lloyal-agents';
import type { WorkflowEvent } from './tui';
import { reportTool } from '@lloyal-labs/rig';
function loadTask(name: string): { system: string; user: string } {
const raw = fs.readFileSync(path.resolve(__dirname, `tasks/${name}.md`), 'utf8').trim();
const sep = raw.indexOf('\n---\n');
if (sep === -1) return { system: raw, user: '' };
return { system: raw.slice(0, sep).trim(), user: raw.slice(sep + 5).trim() };
}
const RESEARCH = loadTask('research');
// ── Options ──────────────────────────────────────────────────────
export interface HarnessOpts {
session: Session;
tools: Tool[];
events: Channel<WorkflowEvent, void>;
maxTurns: number;
trace: boolean;
}
// ── Workflow ─────────────────────────────────────────────────────
export function* handleQuery(query: string, opts: HarnessOpts): Operation<void> {
yield* opts.events.send({ type: 'query', query });
const t = performance.now();
yield* opts.events.send({ type: 'research:start' });
const pool = yield* agentPool({
tasks: [{ content: query }],
tools: [...opts.tools, reportTool],
systemPrompt: RESEARCH.system,
terminalTool: 'report',
maxTurns: opts.maxTurns,
trace: opts.trace,
policy: new DefaultAgentPolicy({ budget: { context: { softLimit: 2048 } } }),
});
const timeMs = performance.now() - t;
yield* opts.events.send({ type: 'research:done', pool, timeMs });
const ctx = yield* Ctx.expect();
const p = ctx._storeKvPressure();
yield* opts.events.send({
type: 'answer',
text: pool.agents[0]?.result ?? '(no findings)',
tokenCount: pool.totalTokens,
toolCalls: pool.totalToolCalls,
timeMs,
ctxPct: Math.round(100 * p.cellsUsed / (p.nCtx || 1)),
ctxPos: p.cellsUsed,
ctxTotal: p.nCtx || 1,
});
}