module.exports=[65454,a=>{"use strict";var b=a.i(22486),c=a.i(48836),d=a.i(12714);a.i(2157);var e=a.i(60526),f=a.i(50227),g=a.i(66680);function h(a,b){return(0,f.join)(process.env.NOTFAIR_CMO_DATA_DIR??(0,f.join)((0,e.homedir)(),".notfair-cmo"),"agents",a,"shadow",`${b}.jsonl`)}async function i(a,b){await (0,d.mkdir)((0,f.dirname)(h(a,b)),{recursive:!0})}async function j(a,b){await i(a,b);let c=h(a,b),e="";async function f(a){await (0,d.appendFile)(c,`${JSON.stringify(a)}
`,"utf8")}async function j(){let a=e.trim();e="",a&&await f({type:"message",id:`shadow-${(0,g.randomUUID)()}`,timestamp:new Date().toISOString(),message:{role:"assistant",content:[{type:"text",text:a}]}})}return{appendDelta(a){e+=a},replaceAssistant(a){e=a},flushAssistant:j,async toolStart(a,b,c){await j(),await f({type:"message",id:`shadow-${(0,g.randomUUID)()}`,timestamp:new Date().toISOString(),message:{role:"assistant",content:[{type:"toolCall",id:a,name:b,...c?{arguments:{__label:c}}:{}}]}})},async toolResult(a,b,c,d){await f({type:"message",id:`shadow-${(0,g.randomUUID)()}`,timestamp:new Date().toISOString(),message:{role:"toolResult",toolCallId:a,toolName:b,isError:!c,content:d?[{type:"text",text:d}]:void 0}})},async close(){await j()}}}async function k(a,b){switch(b.kind){case"delta":a.appendDelta(b.text);return;case"final":a.replaceAssistant(b.text),await a.flushAssistant();return;case"tool":"start"===b.phase?await a.toolStart(b.toolCallId,b.name,b.label):"result"===b.phase&&await a.toolResult(b.toolCallId,b.name,!0,b.label);return;case"lifecycle":case"error":return}}var l=a.i(19078),m=a.i(26341);async function n(a){let d=a;if(!d.thread_id){let b=(0,l.setTaskThreadIfMissing)(a.id,(0,m.generateTaskThreadId)());if(b&&(d=b),!d.thread_id)throw Error(`Failed to assign thread_id for task ${a.id}`)}let e=(0,b.buildPendingSessionKey)(d.agent_id,d.thread_id),f=(0,m.buildTaskKickoffMessage)(d),g=await j(d.agent_id,d.thread_id);console.log(`[run-task] kickoff start task=${d.id} agent=${d.agent_id} thread=${d.thread_id}`);let h=[2e3,4e3,8e3],i=null,n=!1;for(let a=0;a<=h.length;a+=1)try{for await(let a of(0,c.streamChatViaGateway)({sessionKey:e,sessionId:d.thread_id,message:f})){try{await k(g,a)}catch(a){console.error("[run-task] shadow write failed:",a)}if("error"===a.kind)throw Error(a.message)}n=!0;break}catch(c){if(i=c,!(c instanceof Error?c.message:String(c)).includes("no longer exists in configuration")||a>=h.length)break;let b=h[a];console.warn(`[run-task] gateway snapshot stale for ${d.agent_id}; retrying in ${b}ms (attempt ${a+1}/${h.length})`),await new Promise(a=>setTimeout(a,b))}if(n){await g.close(),console.log(`[run-task] kickoff stream done task=${d.id}`);return}await g.close().catch(()=>void 0);let o=i instanceof Error?i.message:String(i);console.error(`[run-task] kickoff failed task=${d.id} agent=${d.agent_id}: ${o}`),(0,l.updateTask)(d.id,{status:"failed",error_message:o})}a.s(["runTaskKickoffServerSide",0,n,"startTaskIfProposed",0,function(a){let b=(0,l.claimProposedTask)(a.id);return b?(n(b).catch(a=>{console.error("[start-task] kickoff failed:",a)}),b):a}],65454)}];

//# sourceMappingURL=src_server_orchestration_run-task_ts_0z.4ea3._.js.map