okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-split-fb3 into f-split-int (locale keys re-added after the card-list strings)7b06ea51b

main
2.3 KiB42 linesraw
1/**
2 * A scheduled task as the agent: a Harness session of its own, under the mode the task was given, tools included.
3 *
4 * Nobody is at the screen when it runs, so a request for approval is answered "no" at once and reported in the result: nothing is
5 * changed without a person saying yes. (Plan changes nothing; Manual therefore stops at the first change and says what it was; Bypass
6 * is full access and asks nothing.) Every run starts a fresh session, so yesterday's run does not fill today's context.
7 */
8import { taskReport } from '../task-report.mjs';
9
10/**
11 * @param {{agent: object, choice: () => ({instanceId:string}|{connectionId:string, modelId:string}|null), workspace: string}} deps `choice` names the model the task runs on
12 * @returns {(task: object, signal?: AbortSignal) => Promise<{text:string, attention:string|null, demo:boolean}|null>} null = no local model is ready (the caller falls back)
13 */
14export function createAgentTaskRunner({ agent, choice, workspace }) {
15  return async function runAgentTask(task, signal) {
16    const model = choice();
17    if (!model) return null;
18    const chatId = `task:${task.id}`;
19    const cards = new Map();
20    let text = '', failure = null;
21    const files = new Map();
22    const stop = () => void agent.stop(chatId).catch(() => {});
23    signal?.addEventListener('abort', stop, { once: true });
24    try {
25      await agent.runTurn({
26        chatId, message: task.prompt, workspacePath: workspace, choice: model, mode: task.mode ?? 'plan', history: [],
27        onFrame: frame => {
28          if (frame.type === 'delta') text += frame.text ?? '';
29          else if (frame.type === 'error') failure = frame.message;
30          else if (frame.type === 'deliverable' && frame.abs && files.size < 30) files.set(frame.abs, { name: frame.name ?? frame.path, path: frame.abs });
31          else if (frame.type === 'agent-tool') {
32            cards.set(frame.tool.id, { ...(cards.get(frame.tool.id) ?? {}), ...frame.tool });
33            if (frame.tool.state === 'proposed') void agent.approve({ chatId, callId: frame.tool.id, decision: 'deny' }).catch(() => {});
34          }
35        },
36      });
37    } finally { signal?.removeEventListener('abort', stop); agent.forget(chatId); }
38    signal?.throwIfAborted();
39    if (failure) throw new Error(failure);
40    return { ...taskReport({ text, cards: [...cards.values()] }), files: [...files.values()] };
41  };
42}