okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
7b06ea51b
main
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}) { 15return async function runAgentTask ( task , signal ) { 16const model = choice (); 17if ( ! model ) return null ; 18const chatId = `task: ${ task . id } ` ; 19const cards = new Map (); 20let text = '' , failure = null ; 21const files = new Map (); 22const stop = () => void agent . stop ( chatId ). catch (() => {}); 23signal ?. addEventListener ( 'abort' , stop , { once :true }); 24try { 25await agent . runTurn ({ 26 chatId, message :task . prompt , workspacePath :workspace , choice :model , mode :task . mode ?? 'plan' , history :[], 27onFrame :frame => { 28if ( frame . type === 'delta' ) text += frame . text ?? '' ; 29else if ( frame . type === 'error' ) failure = frame . message ; 30else if ( frame . type === 'deliverable' && frame . abs && files . size < 30 ) files . set ( frame . abs , { name :frame . name ?? frame . path , path :frame . abs }); 31else if ( frame . type === 'agent-tool' ) { 32cards . set ( frame . tool . id , { ...( cards . get ( frame . tool . id ) ?? {}), ...frame . tool }); 33if ( 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 ); } 38signal ?. throwIfAborted (); 39if ( failure ) throw new Error ( failure ); 40return { ...taskReport ({ text, cards :[ ...cards . values ()] }), files :[ ...files . values ()] }; 41}; 42}