kslock-daemon 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +26 -23
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1,31 +1,34 @@
1
1
  #!/usr/bin/env node
2
- import be from"node:os";import{existsSync as Bn,readFileSync as ct}from"node:fs";import{join as V}from"node:path";function lt(t,e=process.env){let n=e.KSLOCK_SERVER_URL||e.DAEMON_SERVER_URL||"",r=e.KSLOCK_MACHINE_KEY||e.DAEMON_MACHINE_KEY||"";for(let s=0;s<t.length;s+=1){let o=t[s];if(o!=="start"){if(o==="--server-url"&&t[s+1]){n=t[++s];continue}o==="--machine-key"&&t[s+1]&&(r=t[++s])}}return!n||!r?null:{serverUrl:n.replace(/\/$/,""),machineKey:r,daemonVersion:ke(e),hostname:be.hostname(),os:be.platform(),arch:be.arch(),reconnectBaseMs:1e3}}function dt(t=process.env,e=process.cwd()){let n=Un(t.KSLOCK_ENV??t.NODE_ENV),r=zn(e),s=[n?V(r,`.env.${n}`):null,V(r,".env")];for(let o of s)o&&Bn(o)&&qn(o,t)}function ke(t=process.env){let e=t.KSLOCK_DAEMON_VERSION?.trim();return e||(Jn()??"0.0.0-dev")}function Jn(){try{let t=JSON.parse(ct(new URL("../package.json",import.meta.url),"utf8"));return typeof t.version=="string"&&t.version.trim()?t.version.trim():null}catch{return null}}function zn(t){return t.endsWith(V("apps","daemon"))?V(t,"..",".."):t}function qn(t,e){for(let n of ct(t,"utf8").split(/\r?\n/)){let r=n.trim();if(!r||r.startsWith("#"))continue;let s=r.indexOf("=");if(s===-1)continue;let o=r.slice(0,s).trim();if(e[o]!==void 0)continue;let i=r.slice(s+1).trim();i.startsWith('"')&&i.endsWith('"')&&(i=i.slice(1,-1)),i.startsWith("'")&&i.endsWith("'")&&(i=i.slice(1,-1)),e[o]=i}}function Un(t){let e=t?.trim().toLowerCase();if(e)return e==="development"||e==="local"?"dev":e==="production"?"prod":e}var Ce=["Usage: kslock-daemon [start] --server-url <url> --machine-key <key>"," kslock-daemon check-messages --agent-id <agent-id> [--channel-id <channel-id>] [--json]","","Environment:"," KSLOCK_SERVER_URL API server URL, for example https://api.kslock.ai"," KSLOCK_MACHINE_KEY Machine key created from the web console"," KSLOCK_DAEMON_VERSION Override reported daemon version, optional"].join(`
3
- `);import On from"ws";var H=class{constructor(e){this.sendFrame=e}delivered(e){this.sendFrame({type:"agent:delivery_ack",deliveryId:e.deliveryId,agentId:e.agentId,status:"delivered"})}completed(e){this.sendFrame({type:"agent:delivery_ack",deliveryId:e.deliveryId,agentId:e.agentId,status:"completed"})}failed(e,n){this.sendFrame({type:"agent:delivery_ack",deliveryId:e.deliveryId,agentId:e.agentId,status:"failed",error:n})}status(e,n,r){this.sendFrame({type:"agent:status",agentId:e.agentId,status:n,...r!==void 0?{sessionId:r}:{}})}runtimeLog(e,n,r){this.sendFrame({type:"runtime:log",agentId:e.agentId,deliveryId:e.deliveryId,stream:n,content:r})}message(e,n){this.sendFrame({type:"agent:message",agentId:e.agentId,channelId:e.channelId,threadId:e.threadId,replyToMessageId:e.messageId,deliveryId:e.deliveryId,content:n})}messageDelta(e,n,r){!n&&!r||this.sendFrame({type:"agent:message_delta",agentId:e.agentId,channelId:e.channelId,threadId:e.threadId,replyToMessageId:e.messageId,deliveryId:e.deliveryId,contentDelta:n,...r!==void 0?{contentSnapshot:r}:{}})}taskInProgress(e){e.taskId&&this.sendFrame({type:"task:status",taskId:e.taskId,agentId:e.agentId,status:"in_progress",message:`${e.runtimeType} agent started working on the task.`})}taskInReview(e){e.taskId&&this.sendFrame({type:"task:status",taskId:e.taskId,agentId:e.agentId,status:"in_review",message:`${e.runtimeType} agent completed the task and requested review.`})}};function g(t){return`${t.agentId}:${t.channelId}`}function Ae(t){return encodeURIComponent(t)}var Q=class{constructor(e){this.onCancelledBeforeStart=e}deliveryQueues=new Map;activeDeliveries=new Map;activeScopes=new Map;cancelledDeliveries=new Map;async enqueue(e,n,r={}){if(this.canRunAsBusyDelivery(e,r))try{await r.onBusyDelivery?.(e)}catch{}let s=g(e),i=(this.deliveryQueues.get(s)??Promise.resolve()).catch(()=>{}).then(()=>this.run(e,n));this.deliveryQueues.set(s,i),await i.finally(()=>{this.deliveryQueues.get(s)===i&&this.deliveryQueues.delete(s)})}cancel(e,n="cancelled_by_user"){this.cancelledDeliveries.set(e,n),this.activeDeliveries.get(e)?.abort(n)}canRunAsBusyDelivery(e,n){if(!n.allowBusyDelivery)return!1;let r=this.activeScopes.get(g(e));return!!(r&&r.runtimeType===e.runtimeType&&r.deliveryIds.size>0)}async run(e,n){let r=this.cancelledDeliveries.get(e.deliveryId);if(r){this.cancelledDeliveries.delete(e.deliveryId),this.onCancelledBeforeStart(e,r);return}let s=new AbortController;this.activeDeliveries.set(e.deliveryId,s),this.markAgentActive(e);try{await n(e,s.signal)}finally{this.activeDeliveries.delete(e.deliveryId),this.cancelledDeliveries.delete(e.deliveryId),this.markAgentInactive(e)}}markAgentActive(e){let n=g(e),r=this.activeScopes.get(n)??{runtimeType:e.runtimeType,deliveryIds:new Set};r.runtimeType=e.runtimeType,r.deliveryIds.add(e.deliveryId),this.activeScopes.set(n,r)}markAgentInactive(e){let n=g(e),r=this.activeScopes.get(n);r&&(r.deliveryIds.delete(e.deliveryId),r.deliveryIds.size===0&&this.activeScopes.delete(n))}};import{mkdir as Vn,readdir as Hn,readFile as Qn,rename as Xn,writeFile as Yn}from"node:fs/promises";import{join as Te}from"node:path";import{homedir as Gn}from"node:os";import{join as X}from"node:path";function Wn(){return X(Gn(),".agent-collab")}function Ee(t){return X(Wn(),"agents",Ae(t))}function R(t){return X(Ee(t.agentId),"channels",Ae(t.channelId))}function mt(t){return X(R(t),"logs")}var Zn="inbox.json",er="inbox.md",Y=new Map;function gt(t){return Te(R(t),Zn)}function De(t){return Te(R(t),er)}async function ft(t){return vt(t,async()=>{let e=await ee(t);return e.messages.some(n=>n.deliveryId===t.deliveryId)||e.messages.push(nr(t)),ht(e)})}async function Z(t,e){return vt(t,async()=>{let n=await ee(t),r=n.messages.filter(s=>s.deliveryId!==e);return r.length===n.messages.length?n:ht({...n,updatedAt:new Date().toISOString(),messages:r})})}async function yt(t){return t.channelId?ee({agentId:t.agentId,channelId:t.channelId}):tr(t.agentId)}function Me(t){return t.messages.length===0?["# kslock inbox","","No pending messages.",""].join(`
4
- `):["# kslock inbox","",`Scope: ${t.runtimeScopeKey??`${t.agentId}/all-channels`}`,"",`Pending messages: ${t.messages.length}`,`Updated at: ${t.updatedAt}`,"",...t.messages.flatMap((e,n)=>[`## ${n+1}. delivery ${e.deliveryId}`,"",`- messageId: ${e.messageId}`,`- channelId: ${e.channelId}`,`- threadId: ${e.threadId??"none"}`,`- taskId: ${e.taskId??"none"}`,`- createdAt: ${e.createdAt}`,"",e.content,"",...rr(e)])].join(`
5
- `)}async function ee(t){try{let e=await Qn(gt(t),"utf8"),n=JSON.parse(e);return{agentId:t.agentId,channelId:t.channelId,runtimeScopeKey:g(t),updatedAt:typeof n.updatedAt=="string"?n.updatedAt:new Date().toISOString(),messages:Array.isArray(n.messages)?n.messages.filter(sr):[]}}catch{return{agentId:t.agentId,channelId:t.channelId,runtimeScopeKey:g(t),updatedAt:new Date().toISOString(),messages:[]}}}async function tr(t){let e=Te(Ee(t),"channels"),n=[],r=new Date().toISOString();try{let s=await Hn(e,{withFileTypes:!0});for(let o of s){if(!o.isDirectory())continue;let i=decodeURIComponent(o.name),a=await ee({agentId:t,channelId:i});n.push(...a.messages),a.updatedAt>r&&(r=a.updatedAt)}}catch{}return{agentId:t,channelId:null,runtimeScopeKey:null,updatedAt:r,messages:n.sort((s,o)=>s.createdAt.localeCompare(o.createdAt))}}async function ht(t){let e={...t,updatedAt:new Date().toISOString()};if(!t.channelId)throw new Error("cannot write aggregate agent inbox");let n={agentId:t.agentId,channelId:t.channelId},r=R(n);return await Vn(r,{recursive:!0}),await pt(gt(n),`${JSON.stringify(e,null,2)}
6
- `),await pt(De(n),Me(e)),e}async function pt(t,e){let n=`${t}.${process.pid}.${Date.now()}.tmp`;await Yn(n,e,"utf8"),await Xn(n,t)}async function vt(t,e){let n=g(t),r=Y.get(n)??Promise.resolve(),s=()=>{},o=r.catch(()=>{}).then(()=>new Promise(i=>{s=i}));Y.set(n,o),await r.catch(()=>{});try{return await e()}finally{s(),Y.get(n)===o&&Y.delete(n)}}function nr(t){return{deliveryId:t.deliveryId,messageId:t.messageId,serverId:t.serverId,channelId:t.channelId,threadId:t.threadId,taskId:t.taskId,content:t.content,attachments:t.attachments.map(e=>({id:e.id,filename:e.filename,contentType:e.contentType,size:e.size})),createdAt:new Date().toISOString()}}function rr(t){return t.attachments.length===0?[]:["Attachments:",...t.attachments.map(e=>`- ${e.filename} (${e.contentType}, ${e.size} bytes)`),""]}function sr(t){if(!t||typeof t!="object")return!1;let e=t;return typeof e.deliveryId=="string"&&typeof e.messageId=="string"&&typeof e.serverId=="string"&&typeof e.channelId=="string"&&(typeof e.threadId=="string"||e.threadId===null)&&(typeof e.taskId=="string"||e.taskId===null)&&typeof e.content=="string"&&Array.isArray(e.attachments)&&typeof e.createdAt=="string"}import{appendFile as or,mkdir as ir,rename as ar,stat as ur}from"node:fs/promises";import{join as cr}from"node:path";var lr="session.log",dr=1e6,mr=5;async function C(t,e,n){let r=mt(t);await ir(r,{recursive:!0});let s=cr(r,lr);await pr(s);let o=JSON.stringify({time:new Date().toISOString(),agentId:t.agentId,channelId:t.channelId,stream:e,content:n});await or(s,`${o}
7
- `,"utf8")}async function pr(t){try{if((await ur(t)).size<dr)return}catch{return}for(let e=mr-1;e>=1;e-=1)await wt(`${t}.${e}`,`${t}.${e+1}`);await wt(t,`${t}.1`)}async function wt(t,e){try{await ar(t,e)}catch{}}function xt(t){let e=t.pendingCount===1?"message":"messages";return[`[System notification: You have ${t.pendingCount} pending inbox ${e}.]`,"","Do not lose your current task context. At the next safe breakpoint, inspect the inbox before context-sensitive side effects.",`Run: ${t.checkMessagesCommand}`,`Fallback file: ${t.inboxFile}`,"","Pending inbox deliveries remain queued and will be delivered as their own turns after the current turn finishes."].join(`
8
- `)}function M(t){let e=["kslock-daemon check-messages",`--agent-id ${It(t.agentId)}`,`--channel-id ${It(t.channelId)}`].join(" "),n=De(t);return{KSLOCK_RUNTIME_SCOPE_KEY:g(t),KSLOCK_INBOX_FILE:n,KSLOCK_CHECK_MESSAGES_COMMAND:e}}function St(t){let e=M(t);return["Runtime inbox:",`- Current runtime scope: ${e.KSLOCK_RUNTIME_SCOPE_KEY}`,"- While you are busy, kslock may notify you about pending inbox messages.",`- Inspect them with: ${e.KSLOCK_CHECK_MESSAGES_COMMAND}`,`- If the command is unavailable, read: ${e.KSLOCK_INBOX_FILE}`,"- Pending inbox deliveries are queued and will arrive as their own turns; keep replies scoped to the current delivered message unless the user explicitly asks otherwise."].join(`
9
- `)}function It(t){return/^[A-Za-z0-9_./:@-]+$/.test(t)?t:`'${t.replace(/'/g,"'\\''")}'`}var te=class{constructor(e){this.publisher=e}create(e,n,r){return{cwd:e.workspacePath??void 0,abortSignal:n,env:{...process.env,AGENT_ID:e.agentId,SERVER_ID:e.serverId,CHANNEL_ID:e.channelId,RUNTIME_SCOPE_KEY:g(e),WORKSPACE_ID:e.workspaceId??"",AGENT_TOKEN:e.agentToken,AGENT_PROXY_URL:"",AGENT_RUNTIME:e.runtimeType,...M(e)},onLog:(s,o)=>{r?.(),this.publisher.runtimeLog(e,s,o),C(e,s,o)},onPartial:(s,o)=>{r?.(),this.publisher.messageDelta(e,s,o)}}}};function Rt(t){let e=gr(t.env??process.env),n=new AbortController,r=Date.now(),s=!1,o=null,i=()=>{n.abort(t.parentSignal?.reason??"runtime_cancelled")};t.parentSignal?.aborted?i():t.parentSignal?.addEventListener("abort",i,{once:!0});let a=()=>{e<=0||n.signal.aborted||(o&&clearTimeout(o),o=setTimeout(()=>{let u=Date.now()-r;if(u<e){a();return}s=!0;let c=`runtime stalled: no output for ${Math.round(u/1e3)}s on delivery ${t.frame.deliveryId}`;t.onStall(c),n.abort("runtime_stalled")},e))};return a(),{get signal(){return n.signal},get stalled(){return s},progress(){r=Date.now(),a()},dispose(){o&&(clearTimeout(o),o=null),t.parentSignal?.removeEventListener("abort",i)}}}function gr(t){let e=t.KSLOCK_DELIVERY_STALL_MS;if(!e)return 9e5;let n=Number(e);return!Number.isFinite(n)||n<0?9e5:n}var ne=class{constructor(e,n){this.runtimes=e;this.publisher=n;this.contextFactory=new te(n)}contextFactory;activeDeliveriesByAgent=new Map;async run(e,n){await Z(e,e.deliveryId),this.publisher.delivered(e),this.publisher.taskInProgress(e),this.startAgentWork(e);try{this.publisher.runtimeLog(e,"system",`using ${e.executionPolicy.preset} execution policy for @${e.agentHandle}`);let r=this.runtimes.get(e.runtimeType),s=Rt({frame:e,parentSignal:n,onStall:i=>{this.publisher.runtimeLog(e,"stderr",i),C(e,"stderr",i)}}),o;try{o=await r.handleDelivery(e,this.contextFactory.create(e,s.signal,()=>s.progress()))}catch(i){throw s.stalled?new Error("runtime_stalled"):i}finally{s.dispose()}e.attachments.length>0&&await C(e,"system",`delivery has attachments: ${e.attachments.map(i=>i.filename).join(", ")}`),o.sessionId&&this.activeAgentCount(e.agentId)<=1&&this.publisher.status(e,"working",o.sessionId),!o.suppressMessage&&o.content.trim()&&this.publisher.message(e,o.content),this.publisher.taskInReview(e),this.publisher.completed(e),this.finishAgentWork(e,"idle",o.sessionId??null)}catch(r){let s=r instanceof Error?r.message:`${e.runtimeType} runtime failed`;C(e,"stderr",s),this.publisher.failed(e,s),this.finishAgentWork(e,"error")}}startAgentWork(e){let n=this.activeAgentCount(e.agentId);this.activeDeliveriesByAgent.set(e.agentId,n+1),n===0&&this.publisher.status(e,"working")}finishAgentWork(e,n,r){let s=this.activeAgentCount(e.agentId),o=Math.max(0,s-1);if(o===0){this.activeDeliveriesByAgent.delete(e.agentId),this.publisher.status(e,n,r);return}this.activeDeliveriesByAgent.set(e.agentId,o)}activeAgentCount(e){return this.activeDeliveriesByAgent.get(e)??0}};import{randomUUID as Ht}from"node:crypto";import{mkdir as fr,readFile as yr,writeFile as hr}from"node:fs/promises";import{join as bt}from"node:path";function h(t){return R(t)}async function I(t){try{let e=await yr(bt(h(t),"session.json"),"utf8");return{...JSON.parse(e),agentId:t.agentId,channelId:t.channelId,runtimeScopeKey:g(t)}}catch{return null}}async function x(t){let e=h(t);await fr(e,{recursive:!0}),await hr(bt(e,"session.json"),`${JSON.stringify(t,null,2)}
10
- `,"utf8")}import{spawn as _e}from"node:child_process";import{constants as vr}from"node:fs";import{access as wr,readdir as Ir}from"node:fs/promises";import{homedir as Pe}from"node:os";import{delimiter as xr,join as b}from"node:path";var Sr=/^[a-zA-Z0-9._-]+$/,Fe=new Map;async function S(t,e){let n=Rr(t,e),r=`${n.command}:${n.envVars.join(",")}:${n.extraCandidates.join(",")}`,s=[...br(n),...n.extraCandidates,...Er(n.command)],o=Fe.get(r);if(o&&(s.length===0||s.includes(o))&&await kt(o))return o;o&&Fe.delete(r);let i=[...s,await Cr(n.command),...await Ar(n.command),n.command].filter(Boolean);for(let a of Mr(i))if(await kt(a))return Fe.set(r,a),a;return null}function Le(t,e=[]){return new Promise(n=>{let r="",s="",o=!1,i=c=>{o||(o=!0,n(c))},a=_e(t,[...e,"--version"],{stdio:["ignore","pipe","pipe"]}),u=setTimeout(()=>{a.kill("SIGTERM"),i(null)},3e3);u.unref?.(),a.stdout.on("data",c=>{r+=c.toString()}),a.stderr.on("data",c=>{s+=c.toString()}),a.on("error",()=>{clearTimeout(u),i(null)}),a.on("exit",()=>{clearTimeout(u),i((r||s).trim().split(/\r?\n/)[0]||null)})})}function Rr(t,e){return typeof t!="string"?{command:t.command,envVars:t.envVars??[],extraCandidates:t.extraCandidates??[]}:{command:t,envVars:e?[e]:[],extraCandidates:[]}}function br(t){return[...t.envVars,...kr(t.command)].map(e=>process.env[e]?.trim()).filter(Boolean)}function kr(t){let e=t.toUpperCase().replace(/[^A-Z0-9]+/g,"_");return[`${e}_BIN`,`${e}_PATH`,`KSLOCK_${e}_BIN`,`KSLOCK_RUNTIME_${e}_BIN`]}async function Cr(t){if(!Sr.test(t))return null;let e=process.env.SHELL||"/bin/zsh";return new Promise(n=>{let r="",s=_e(e,["-lc",`command -v ${t}`],{stdio:["ignore","pipe","ignore"]});s.stdout.on("data",o=>{r+=o.toString()}),s.on("error",()=>n(null)),s.on("exit",o=>{let i=r.trim().split(/\r?\n/)[0];n(o===0&&i?i:null)})})}async function Ar(t){return[...await Dr(t),b(Pe(),".local","bin",t),b(Pe(),"bin",t),b("/opt/homebrew/bin",t),b("/usr/local/bin",t)]}function Er(t){return Tr(process.env.KSLOCK_RUNTIME_PATHS||process.env.KSLOCK_RUNTIME_PATH).map(e=>b(e,t))}function Tr(t){return(t??"").split(xr).map(e=>e.trim()).filter(Boolean)}async function Dr(t){let e=b(Pe(),".nvm","versions","node");try{return(await Ir(e)).sort((r,s)=>s.localeCompare(r)).map(r=>b(e,r,"bin",t))}catch{return[]}}async function kt(t){if(t.includes("/"))try{return await wr(t,vr.X_OK),!0}catch{return!1}return new Promise(e=>{let n=!1,r=i=>{n||(n=!0,e(i))},s=_e(t,["--version"],{stdio:"ignore"}),o=setTimeout(()=>{s.kill("SIGTERM"),r(!1)},3e3);o.unref?.(),s.on("error",()=>{clearTimeout(o),r(!1)}),s.on("exit",()=>{clearTimeout(o),r(!0)})})}function Mr(t){return[...new Set(t)]}import{Buffer as Ct}from"node:buffer";import{mkdir as Fr,writeFile as Pr}from"node:fs/promises";import{tmpdir as At}from"node:os";import{join as re}from"node:path";var je=16e3;async function v(t,e={}){return`${t.attachments.length===0?t.content:await _r(t,e)}
2
+ import Ce from"node:os";import{existsSync as Vn,readFileSync as dt}from"node:fs";import{join as X}from"node:path";function pt(t,e=process.env){let n=e.KSLOCK_SERVER_URL||e.DAEMON_SERVER_URL||"",r=e.KSLOCK_MACHINE_KEY||e.DAEMON_MACHINE_KEY||"";for(let s=0;s<t.length;s+=1){let o=t[s];if(o!=="start"){if(o==="--server-url"&&t[s+1]){n=t[++s];continue}o==="--machine-key"&&t[s+1]&&(r=t[++s])}}return!n||!r?null:{serverUrl:n.replace(/\/$/,""),machineKey:r,daemonVersion:Ae(e),hostname:Ce.hostname(),os:Ce.platform(),arch:Ce.arch(),reconnectBaseMs:1e3}}function mt(t=process.env,e=process.cwd()){let n=Yn(t.KSLOCK_ENV??t.NODE_ENV),r=Qn(e),s=[X(r,".env.local"),n?X(r,`.env.${n}`):null];for(let o of s)o&&Vn(o)&&Xn(o,t)}function Ae(t=process.env){let e=t.KSLOCK_DAEMON_VERSION?.trim();return e||(Hn()??"0.0.0-dev")}function Hn(){try{let t=JSON.parse(dt(new URL("../package.json",import.meta.url),"utf8"));return typeof t.version=="string"&&t.version.trim()?t.version.trim():null}catch{return null}}function Qn(t){return t.endsWith(X("apps","daemon"))?X(t,"..",".."):t}function Xn(t,e){for(let n of dt(t,"utf8").split(/\r?\n/)){let r=n.trim();if(!r||r.startsWith("#"))continue;let s=r.indexOf("=");if(s===-1)continue;let o=r.slice(0,s).trim();if(e[o]!==void 0)continue;let i=r.slice(s+1).trim();i.startsWith('"')&&i.endsWith('"')&&(i=i.slice(1,-1)),i.startsWith("'")&&i.endsWith("'")&&(i=i.slice(1,-1)),e[o]=i}}function Yn(t){let e=t?.trim().toLowerCase();if(e)return e==="development"||e==="local"?"dev":e==="production"?"prod":e}var Ee=["Usage: kslock-daemon [start] --server-url <url> --machine-key <key>"," kslock-daemon check-messages --agent-id <agent-id> [--channel-id <channel-id>] [--json]","","Environment:"," KSLOCK_SERVER_URL API server URL, for example https://api.kslock.ai"," KSLOCK_MACHINE_KEY Machine key created from the web console"," KSLOCK_DAEMON_VERSION Override reported daemon version, optional"].join(`
3
+ `);import zn from"ws";var Y=class{constructor(e){this.sendFrame=e}delivered(e){this.sendFrame({type:"agent:delivery_ack",deliveryId:e.deliveryId,agentId:e.agentId,status:"delivered"})}completed(e){this.sendFrame({type:"agent:delivery_ack",deliveryId:e.deliveryId,agentId:e.agentId,status:"completed"})}failed(e,n){this.sendFrame({type:"agent:delivery_ack",deliveryId:e.deliveryId,agentId:e.agentId,status:"failed",error:n})}status(e,n,r){this.sendFrame({type:"agent:status",agentId:e.agentId,status:n,...r!==void 0?{sessionId:r}:{}})}runtimeLog(e,n,r){this.sendFrame({type:"runtime:log",agentId:e.agentId,deliveryId:e.deliveryId,stream:n,content:r})}message(e,n){this.sendFrame({type:"agent:message",agentId:e.agentId,channelId:e.channelId,threadId:e.threadId,replyToMessageId:e.messageId,deliveryId:e.deliveryId,content:n})}messageDelta(e,n,r){!n&&!r||this.sendFrame({type:"agent:message_delta",agentId:e.agentId,channelId:e.channelId,threadId:e.threadId,replyToMessageId:e.messageId,deliveryId:e.deliveryId,contentDelta:n,...r!==void 0?{contentSnapshot:r}:{}})}taskInProgress(e){e.taskId&&this.sendFrame({type:"task:status",taskId:e.taskId,agentId:e.agentId,status:"in_progress",message:`${e.runtimeType} agent started working on the task.`})}taskInReview(e){e.taskId&&this.sendFrame({type:"task:status",taskId:e.taskId,agentId:e.agentId,status:"in_review",message:`${e.runtimeType} agent completed the task and requested review.`})}};function R(t){return t.taskId?{type:"task",id:t.taskId}:t.threadId?{type:"thread",id:t.threadId}:t.requesterUserId?{type:"user",id:t.requesterUserId}:{type:"channel",id:t.channelId}}function g(t){let e=R(t);return`${t.agentId}:${t.channelId}:${e.type}:${e.id}`}function F(t){return encodeURIComponent(t)}var Z=class{constructor(e){this.onCancelledBeforeStart=e}deliveryQueues=new Map;activeDeliveries=new Map;activeScopes=new Map;cancelledDeliveries=new Map;async enqueue(e,n,r={}){if(this.canRunAsBusyDelivery(e,r))try{await r.onBusyDelivery?.(e)}catch{}let s=g(e),i=(this.deliveryQueues.get(s)??Promise.resolve()).catch(()=>{}).then(()=>this.run(e,n));this.deliveryQueues.set(s,i),await i.finally(()=>{this.deliveryQueues.get(s)===i&&this.deliveryQueues.delete(s)})}cancel(e,n="cancelled_by_user"){this.cancelledDeliveries.set(e,n),this.activeDeliveries.get(e)?.abort(n)}canRunAsBusyDelivery(e,n){if(!n.allowBusyDelivery)return!1;let r=this.activeScopes.get(g(e));return!!(r&&r.runtimeType===e.runtimeType&&r.deliveryIds.size>0)}async run(e,n){let r=this.cancelledDeliveries.get(e.deliveryId);if(r){this.cancelledDeliveries.delete(e.deliveryId),this.onCancelledBeforeStart(e,r);return}let s=new AbortController;this.activeDeliveries.set(e.deliveryId,s),this.markAgentActive(e);try{await n(e,s.signal)}finally{this.activeDeliveries.delete(e.deliveryId),this.cancelledDeliveries.delete(e.deliveryId),this.markAgentInactive(e)}}markAgentActive(e){let n=g(e),r=this.activeScopes.get(n)??{runtimeType:e.runtimeType,deliveryIds:new Set};r.runtimeType=e.runtimeType,r.deliveryIds.add(e.deliveryId),this.activeScopes.set(n,r)}markAgentInactive(e){let n=g(e),r=this.activeScopes.get(n);r&&(r.deliveryIds.delete(e.deliveryId),r.deliveryIds.size===0&&this.activeScopes.delete(n))}};import{mkdir as tr,readdir as yt,readFile as ht,rename as nr,writeFile as rr}from"node:fs/promises";import{join as te}from"node:path";import{homedir as Zn}from"node:os";import{join as P}from"node:path";function er(){return P(Zn(),".agent-collab")}function Te(t){return P(er(),"agents",F(t))}function De(t,e){return P(Te(t),"channels",F(e))}function b(t){let e=R(t);return P(De(t.agentId,t.channelId),"scopes",F(e.type),F(e.id))}function gt(t){return P(b(t),"logs")}var vt="inbox.json",sr="inbox.md",ee=new Map;function It(t){return te(b(t),vt)}function Me(t){return te(b(t),sr)}async function wt(t){return bt(t,async()=>{let e=await Pe(t);return e.messages.some(n=>n.deliveryId===t.deliveryId)||e.messages.push(ur(t)),Rt(t,e)})}async function ne(t,e){return bt(t,async()=>{let n=await Pe(t),r=n.messages.filter(s=>s.deliveryId!==e);return r.length===n.messages.length?n:Rt(t,{...n,updatedAt:new Date().toISOString(),messages:r})})}async function St(t){return t.channelId?lr(t)?Pe(t):xt(t.agentId,t.channelId):or(t.agentId)}function Fe(t){return t.messages.length===0?["# kslock inbox","","No pending messages.",""].join(`
4
+ `):["# kslock inbox","",`Scope: ${t.runtimeScopeKey??`${t.agentId}/all-channels`}`,"",`Pending messages: ${t.messages.length}`,`Updated at: ${t.updatedAt}`,"",...t.messages.flatMap((e,n)=>[`## ${n+1}. delivery ${e.deliveryId}`,"",`- messageId: ${e.messageId}`,`- channelId: ${e.channelId}`,`- threadId: ${e.threadId??"none"}`,`- taskId: ${e.taskId??"none"}`,`- createdAt: ${e.createdAt}`,"",e.content,"",...cr(e)])].join(`
5
+ `)}async function Pe(t){try{let e=await ht(It(t),"utf8"),n=JSON.parse(e);return{agentId:t.agentId,channelId:t.channelId,runtimeScopeKey:g(t),updatedAt:typeof n.updatedAt=="string"?n.updatedAt:new Date().toISOString(),messages:Array.isArray(n.messages)?n.messages.filter(kt):[]}}catch{return{agentId:t.agentId,channelId:t.channelId,runtimeScopeKey:g(t),updatedAt:new Date().toISOString(),messages:[]}}}async function or(t){let e=te(Te(t),"channels"),n=[],r=new Date().toISOString();try{let s=await yt(e,{withFileTypes:!0});for(let o of s){if(!o.isDirectory())continue;let i=decodeURIComponent(o.name),a=await xt(t,i);n.push(...a.messages),a.updatedAt>r&&(r=a.updatedAt)}}catch{}return{agentId:t,channelId:null,runtimeScopeKey:null,updatedAt:r,messages:n.sort((s,o)=>s.createdAt.localeCompare(o.createdAt))}}async function xt(t,e){let n=[],r=new Date().toISOString();for(let s of await ir(De(t,e)))n.push(...s.messages),s.updatedAt>r&&(r=s.updatedAt);return{agentId:t,channelId:e,runtimeScopeKey:null,updatedAt:r,messages:n.sort((s,o)=>s.createdAt.localeCompare(o.createdAt))}}async function ir(t){let e=[];async function n(r){let s;try{s=await yt(r,{withFileTypes:!0})}catch{return}for(let o of s){let i=te(r,o.name);if(o.isDirectory()){await n(i);continue}if(o.isFile()&&o.name===vt){let a=await ar(i);a&&e.push(a)}}}return await n(t),e}async function ar(t){try{let e=JSON.parse(await ht(t,"utf8"));return{agentId:typeof e.agentId=="string"?e.agentId:"",channelId:typeof e.channelId=="string"?e.channelId:null,runtimeScopeKey:typeof e.runtimeScopeKey=="string"?e.runtimeScopeKey:null,updatedAt:typeof e.updatedAt=="string"?e.updatedAt:new Date().toISOString(),messages:Array.isArray(e.messages)?e.messages.filter(kt):[]}}catch{return null}}async function Rt(t,e){let n={...e,updatedAt:new Date().toISOString()};if(!e.channelId)throw new Error("cannot write aggregate agent inbox");let r=b(t);return await tr(r,{recursive:!0}),await ft(It(t),`${JSON.stringify(n,null,2)}
6
+ `),await ft(Me(t),Fe(n)),n}async function ft(t,e){let n=`${t}.${process.pid}.${Date.now()}.tmp`;await rr(n,e,"utf8"),await nr(n,t)}async function bt(t,e){let n=g(t),r=ee.get(n)??Promise.resolve(),s=()=>{},o=r.catch(()=>{}).then(()=>new Promise(i=>{s=i}));ee.set(n,o),await r.catch(()=>{});try{return await e()}finally{s(),ee.get(n)===o&&ee.delete(n)}}function ur(t){return{deliveryId:t.deliveryId,messageId:t.messageId,serverId:t.serverId,channelId:t.channelId,threadId:t.threadId,taskId:t.taskId,content:t.content,attachments:t.attachments.map(e=>({id:e.id,filename:e.filename,contentType:e.contentType,size:e.size})),createdAt:new Date().toISOString()}}function lr(t){return!!("taskId"in t&&t.taskId||"threadId"in t&&t.threadId||"requesterUserId"in t&&t.requesterUserId)}function cr(t){return t.attachments.length===0?[]:["Attachments:",...t.attachments.map(e=>`- ${e.filename} (${e.contentType}, ${e.size} bytes)`),""]}function kt(t){if(!t||typeof t!="object")return!1;let e=t;return typeof e.deliveryId=="string"&&typeof e.messageId=="string"&&typeof e.serverId=="string"&&typeof e.channelId=="string"&&(typeof e.threadId=="string"||e.threadId===null)&&(typeof e.taskId=="string"||e.taskId===null)&&typeof e.content=="string"&&Array.isArray(e.attachments)&&typeof e.createdAt=="string"}import{appendFile as dr,mkdir as pr,rename as mr,stat as gr}from"node:fs/promises";import{join as fr}from"node:path";var yr="session.log",hr=1e6,vr=5;async function A(t,e,n){let r=gt(t);await pr(r,{recursive:!0});let s=fr(r,yr);await Ir(s);let o=JSON.stringify({time:new Date().toISOString(),agentId:t.agentId,channelId:t.channelId,stream:e,content:n});await dr(s,`${o}
7
+ `,"utf8")}async function Ir(t){try{if((await gr(t)).size<hr)return}catch{return}for(let e=vr-1;e>=1;e-=1)await Ct(`${t}.${e}`,`${t}.${e+1}`);await Ct(t,`${t}.1`)}async function Ct(t,e){try{await mr(t,e)}catch{}}function Et(t){let e=t.pendingCount===1?"message":"messages";return[`[System notification: You have ${t.pendingCount} pending inbox ${e}.]`,"","Do not lose your current task context. At the next safe breakpoint, inspect the inbox before context-sensitive side effects.",`Run: ${t.checkMessagesCommand}`,`Fallback file: ${t.inboxFile}`,"","Pending inbox deliveries remain queued and will be delivered as their own turns after the current turn finishes."].join(`
8
+ `)}function _(t){let e=["kslock-daemon check-messages",`--agent-id ${At(t.agentId)}`,`--channel-id ${At(t.channelId)}`].join(" "),n=Me(t);return{KSLOCK_RUNTIME_SCOPE_KEY:g(t),KSLOCK_INBOX_FILE:n,KSLOCK_CHECK_MESSAGES_COMMAND:e}}function Tt(t){let e=_(t);return["Runtime inbox:",`- Current runtime scope: ${e.KSLOCK_RUNTIME_SCOPE_KEY}`,"- While you are busy, kslock may notify you about pending inbox messages.",`- Inspect them with: ${e.KSLOCK_CHECK_MESSAGES_COMMAND}`,`- If the command is unavailable, read: ${e.KSLOCK_INBOX_FILE}`,"- Pending inbox deliveries are queued and will arrive as their own turns; keep replies scoped to the current delivered message unless the user explicitly asks otherwise."].join(`
9
+ `)}function At(t){return/^[A-Za-z0-9_./:@-]+$/.test(t)?t:`'${t.replace(/'/g,"'\\''")}'`}var re=class{constructor(e){this.publisher=e}create(e,n,r){let s=R(e);return{cwd:e.workspacePath??void 0,abortSignal:n,env:{...process.env,AGENT_ID:e.agentId,SERVER_ID:e.serverId,CHANNEL_ID:e.channelId,REQUESTER_USER_ID:e.requesterUserId??"",RUNTIME_SCOPE_KEY:g(e),RUNTIME_SCOPE_TYPE:s.type,RUNTIME_SCOPE_ID:s.id,WORKSPACE_ID:e.workspaceId??"",AGENT_TOKEN:e.agentToken,AGENT_PROXY_URL:"",AGENT_RUNTIME:e.runtimeType,..._(e)},onLog:(o,i)=>{r?.(),this.publisher.runtimeLog(e,o,i),A(e,o,i)},onPartial:(o,i)=>{r?.(),this.publisher.messageDelta(e,o,i)}}}};function Dt(t){let e=wr(t.env??process.env),n=new AbortController,r=Date.now(),s=!1,o=null,i=()=>{n.abort(t.parentSignal?.reason??"runtime_cancelled")};t.parentSignal?.aborted?i():t.parentSignal?.addEventListener("abort",i,{once:!0});let a=()=>{e<=0||n.signal.aborted||(o&&clearTimeout(o),o=setTimeout(()=>{let u=Date.now()-r;if(u<e){a();return}s=!0;let l=`runtime stalled: no output for ${Math.round(u/1e3)}s on delivery ${t.frame.deliveryId}`;t.onStall(l),n.abort("runtime_stalled")},e))};return a(),{get signal(){return n.signal},get stalled(){return s},progress(){r=Date.now(),a()},dispose(){o&&(clearTimeout(o),o=null),t.parentSignal?.removeEventListener("abort",i)}}}function wr(t){let e=t.KSLOCK_DELIVERY_STALL_MS;if(!e)return 9e5;let n=Number(e);return!Number.isFinite(n)||n<0?9e5:n}var se=class{constructor(e,n){this.runtimes=e;this.publisher=n;this.contextFactory=new re(n)}contextFactory;activeDeliveriesByAgent=new Map;async run(e,n){await ne(e,e.deliveryId),this.publisher.delivered(e),this.publisher.taskInProgress(e),this.startAgentWork(e);try{this.publisher.runtimeLog(e,"system",`using ${e.executionPolicy.preset} execution policy for @${e.agentHandle}`);let r=this.runtimes.get(e.runtimeType),s=Dt({frame:e,parentSignal:n,onStall:i=>{this.publisher.runtimeLog(e,"stderr",i),A(e,"stderr",i)}}),o;try{o=await r.handleDelivery(e,this.contextFactory.create(e,s.signal,()=>s.progress()))}catch(i){throw s.stalled?new Error("runtime_stalled"):i}finally{s.dispose()}e.attachments.length>0&&await A(e,"system",`delivery has attachments: ${e.attachments.map(i=>i.filename).join(", ")}`),o.sessionId&&this.activeAgentCount(e.agentId)<=1&&this.publisher.status(e,"working",o.sessionId),!o.suppressMessage&&o.content.trim()&&this.publisher.message(e,o.content),this.publisher.taskInReview(e),this.publisher.completed(e),this.finishAgentWork(e,"idle",o.sessionId??null)}catch(r){let s=r instanceof Error?r.message:`${e.runtimeType} runtime failed`;A(e,"stderr",s),this.publisher.failed(e,s),this.finishAgentWork(e,"error")}}startAgentWork(e){let n=this.activeAgentCount(e.agentId);this.activeDeliveriesByAgent.set(e.agentId,n+1),n===0&&this.publisher.status(e,"working")}finishAgentWork(e,n,r){let s=this.activeAgentCount(e.agentId),o=Math.max(0,s-1);if(o===0){this.activeDeliveriesByAgent.delete(e.agentId),this.publisher.status(e,n,r);return}this.activeDeliveriesByAgent.set(e.agentId,o)}activeAgentCount(e){return this.activeDeliveriesByAgent.get(e)??0}};import{randomUUID as nn}from"node:crypto";import{mkdir as Sr,readFile as xr,writeFile as Rr}from"node:fs/promises";import{join as Mt}from"node:path";function h(t){return b(t)}async function w(t){try{let e=await xr(Mt(h(t),"session.json"),"utf8");return{...JSON.parse(e),agentId:t.agentId,channelId:t.channelId,runtimeScopeKey:g(t)}}catch{return null}}async function S(t){let e=h(t);await Sr(e,{recursive:!0}),await Rr(Mt(e,"session.json"),`${JSON.stringify(t,null,2)}
10
+ `,"utf8")}import{spawn as je}from"node:child_process";import{constants as br}from"node:fs";import{access as kr,readdir as Cr}from"node:fs/promises";import{homedir as Le}from"node:os";import{delimiter as Ar,join as k}from"node:path";var Er=/^[a-zA-Z0-9._-]+$/,_e=new Map;async function x(t,e){let n=Tr(t,e),r=`${n.command}:${n.envVars.join(",")}:${n.extraCandidates.join(",")}`,s=[...Dr(n),...n.extraCandidates,..._r(n.command)],o=_e.get(r);if(o&&(s.length===0||s.includes(o))&&await Ft(o))return o;o&&_e.delete(r);let i=[...s,await Fr(n.command),...await Pr(n.command),n.command].filter(Boolean);for(let a of Or(i))if(await Ft(a))return _e.set(r,a),a;return null}function Oe(t,e=[]){return new Promise(n=>{let r="",s="",o=!1,i=l=>{o||(o=!0,n(l))},a=je(t,[...e,"--version"],{stdio:["ignore","pipe","pipe"]}),u=setTimeout(()=>{a.kill("SIGTERM"),i(null)},3e3);u.unref?.(),a.stdout.on("data",l=>{r+=l.toString()}),a.stderr.on("data",l=>{s+=l.toString()}),a.on("error",()=>{clearTimeout(u),i(null)}),a.on("exit",()=>{clearTimeout(u),i((r||s).trim().split(/\r?\n/)[0]||null)})})}function Tr(t,e){return typeof t!="string"?{command:t.command,envVars:t.envVars??[],extraCandidates:t.extraCandidates??[]}:{command:t,envVars:e?[e]:[],extraCandidates:[]}}function Dr(t){return[...t.envVars,...Mr(t.command)].map(e=>process.env[e]?.trim()).filter(Boolean)}function Mr(t){let e=t.toUpperCase().replace(/[^A-Z0-9]+/g,"_");return[`${e}_BIN`,`${e}_PATH`,`KSLOCK_${e}_BIN`,`KSLOCK_RUNTIME_${e}_BIN`]}async function Fr(t){if(!Er.test(t))return null;let e=process.env.SHELL||"/bin/zsh";return new Promise(n=>{let r="",s=je(e,["-lc",`command -v ${t}`],{stdio:["ignore","pipe","ignore"]});s.stdout.on("data",o=>{r+=o.toString()}),s.on("error",()=>n(null)),s.on("exit",o=>{let i=r.trim().split(/\r?\n/)[0];n(o===0&&i?i:null)})})}async function Pr(t){return[...await jr(t),k(Le(),".local","bin",t),k(Le(),"bin",t),k("/opt/homebrew/bin",t),k("/usr/local/bin",t)]}function _r(t){return Lr(process.env.KSLOCK_RUNTIME_PATHS||process.env.KSLOCK_RUNTIME_PATH).map(e=>k(e,t))}function Lr(t){return(t??"").split(Ar).map(e=>e.trim()).filter(Boolean)}async function jr(t){let e=k(Le(),".nvm","versions","node");try{return(await Cr(e)).sort((r,s)=>s.localeCompare(r)).map(r=>k(e,r,"bin",t))}catch{return[]}}async function Ft(t){if(t.includes("/"))try{return await kr(t,br.X_OK),!0}catch{return!1}return new Promise(e=>{let n=!1,r=i=>{n||(n=!0,e(i))},s=je(t,["--version"],{stdio:"ignore"}),o=setTimeout(()=>{s.kill("SIGTERM"),r(!1)},3e3);o.unref?.(),s.on("error",()=>{clearTimeout(o),r(!1)}),s.on("exit",()=>{clearTimeout(o),r(!0)})})}function Or(t){return[...new Set(t)]}import{Buffer as Pt}from"node:buffer";import{mkdir as $r,writeFile as Nr}from"node:fs/promises";import{tmpdir as _t}from"node:os";import{join as oe}from"node:path";var $e=16e3;async function v(t,e={}){let n=t.attachments.length===0?t.content:await Kr(t,e);return`${Jr(t)}
11
11
 
12
- ${St(t)}`}async function _r(t,e){let r=(await Promise.all(t.attachments.map(s=>Lr(t,s,e)))).map(jr).join(`
12
+ ${n}
13
+
14
+ ${Tt(t)}`}async function Kr(t,e){let r=(await Promise.all(t.attachments.map(s=>Br(t,s,e)))).map(qr).join(`
13
15
  `);return`${t.content}
14
16
 
15
17
  Attachments:
16
- ${r}`}async function Lr(t,e,n){let r=e.downloadUrl??e.previewUrl,s=$r(r);if(!s)return{filename:e.filename,contentType:e.contentType,size:e.size,localPath:null,textPreview:null,sourceUrl:r&&!r.startsWith("data:")?r:null,warning:r?"attachment content is not embedded in the daemon frame":null};let o=null,i=null;try{o=await Or(t,e.id,e.filename,s.buffer,n.workspacePath)}catch(a){i=a instanceof Error?`failed to write local attachment file: ${a.message}`:"failed to write local attachment file"}return{filename:e.filename,contentType:e.contentType||s.contentType,size:e.size,localPath:o,textPreview:Nr(s.contentType||e.contentType,e.filename)?Kr(s.buffer.toString("utf8")):null,sourceUrl:null,warning:i}}function jr(t){let e=[`- ${t.filename} (${t.contentType}, ${t.size} bytes)`];return t.localPath&&e.push(` Local path: ${t.localPath}`),t.sourceUrl&&e.push(` Source URL: ${t.sourceUrl}`),t.textPreview?(e.push(" Text preview:"),e.push(Br(t.textPreview))):t.contentType.startsWith("image/")&&t.localPath&&e.push(" Image attachment saved as a local file. Inspect the local path when image/file tools are available."),t.warning&&e.push(` Warning: ${t.warning}`),e.join(`
17
- `)}async function Or(t,e,n,r,s){let o=`${Et(e,"attachment")}-${Et(n,"file")}`;if(s)try{return await Oe(re(s,".kslock","attachments",t.deliveryId),o,r)}catch{return Oe(re(At(),"kslock-daemon","attachments",t.deliveryId),o,r)}return Oe(re(At(),"kslock-daemon","attachments",t.deliveryId),o,r)}async function Oe(t,e,n){await Fr(t,{recursive:!0});let r=re(t,e);return await Pr(r,n),r}function $r(t){if(!t?.startsWith("data:"))return null;let e=t.indexOf(",");if(e<0)return null;let n=t.slice(5,e),r=t.slice(e+1),s=n.split(";").filter(Boolean),o=s[0]||"application/octet-stream",i=s.includes("base64");try{return{buffer:i?Ct.from(r.replace(/\s/g,""),"base64"):Ct.from(decodeURIComponent(r)),contentType:o}}catch{return null}}function Nr(t,e){let n=t.toLowerCase();return n.startsWith("text/")||["application/json","application/ld+json","application/xml","application/javascript","application/x-javascript","application/yaml","application/x-yaml"].includes(n)?!0:/\.(csv|env|html?|js|json|log|md|markdown|sql|styl|svg|ts|tsx|txt|vue|xml|ya?ml)$/i.test(e)}function Kr(t){let e=t.replace(/\r\n/g,`
18
- `).replace(/\u0000/g,"");return e.length<=je?e:`${e.slice(0,je)}
19
- ...[truncated ${e.length-je} chars]`}function Br(t){return t.split(`
18
+ ${r}`}async function Br(t,e,n){let r=e.downloadUrl??e.previewUrl,s=Ur(r);if(!s)return{filename:e.filename,contentType:e.contentType,size:e.size,localPath:null,textPreview:null,sourceUrl:r&&!r.startsWith("data:")?r:null,warning:r?"attachment content is not embedded in the daemon frame":null};let o=null,i=null;try{o=await zr(t,e.id,e.filename,s.buffer,n.workspacePath)}catch(a){i=a instanceof Error?`failed to write local attachment file: ${a.message}`:"failed to write local attachment file"}return{filename:e.filename,contentType:e.contentType||s.contentType,size:e.size,localPath:o,textPreview:Gr(s.contentType||e.contentType,e.filename)?Wr(s.buffer.toString("utf8")):null,sourceUrl:null,warning:i}}function qr(t){let e=[`- ${t.filename} (${t.contentType}, ${t.size} bytes)`];return t.localPath&&e.push(` Local path: ${t.localPath}`),t.sourceUrl&&e.push(` Source URL: ${t.sourceUrl}`),t.textPreview?(e.push(" Text preview:"),e.push(Vr(t.textPreview))):t.contentType.startsWith("image/")&&t.localPath&&e.push(" Image attachment saved as a local file. Inspect the local path when image/file tools are available."),t.warning&&e.push(` Warning: ${t.warning}`),e.join(`
19
+ `)}function Jr(t){let e=R(t);return["kslock context:",`- Requester: ${t.requesterUserId?`${t.requesterDisplayName||t.requesterUserId} (${t.requesterUserId})`:"system or unknown"}`,`- Channel: ${t.channelId}`,`- Thread: ${t.threadId??"none"}`,`- Task: ${t.taskId??"none"}`,`- Runtime session scope: ${e.type}:${e.id}`,`- Runtime scope key: ${g(t)}`].join(`
20
+ `)}async function zr(t,e,n,r,s){let o=`${Lt(e,"attachment")}-${Lt(n,"file")}`;if(s)try{return await Ne(oe(s,".kslock","attachments",t.deliveryId),o,r)}catch{return Ne(oe(_t(),"kslock-daemon","attachments",t.deliveryId),o,r)}return Ne(oe(_t(),"kslock-daemon","attachments",t.deliveryId),o,r)}async function Ne(t,e,n){await $r(t,{recursive:!0});let r=oe(t,e);return await Nr(r,n),r}function Ur(t){if(!t?.startsWith("data:"))return null;let e=t.indexOf(",");if(e<0)return null;let n=t.slice(5,e),r=t.slice(e+1),s=n.split(";").filter(Boolean),o=s[0]||"application/octet-stream",i=s.includes("base64");try{return{buffer:i?Pt.from(r.replace(/\s/g,""),"base64"):Pt.from(decodeURIComponent(r)),contentType:o}}catch{return null}}function Gr(t,e){let n=t.toLowerCase();return n.startsWith("text/")||["application/json","application/ld+json","application/xml","application/javascript","application/x-javascript","application/yaml","application/x-yaml"].includes(n)?!0:/\.(csv|env|html?|js|json|log|md|markdown|sql|styl|svg|ts|tsx|txt|vue|xml|ya?ml)$/i.test(e)}function Wr(t){let e=t.replace(/\r\n/g,`
21
+ `).replace(/\u0000/g,"");return e.length<=$e?e:`${e.slice(0,$e)}
22
+ ...[truncated ${e.length-$e} chars]`}function Vr(t){return t.split(`
20
23
  `).map(e=>` ${e}`).join(`
21
- `)}function Et(t,e){return t.trim().replace(/[<>:"/\\|?*\x00-\x1F]/g,"_").replace(/^\.+$/,"").slice(0,160)||e}import{spawn as Jr}from"node:child_process";var zr=5e3,Tt=8*1024*1024;async function Mt(t){let e=await $e(t,["debug","models"]);return e.code!==0?[]:qr(e.stdout)}async function Ft(t){let e=await $e(t,["models"]);return e.code!==0?[]:Pt(e.stdout)}async function se(t){let e=await $e(t,["models"]);return e.code!==0?[]:Pt(e.stdout)}function qr(t){let e=t.indexOf("{");if(e<0)return[];try{let n=JSON.parse(t.slice(e));return Array.isArray(n.models)?_t(n.models.flatMap(r=>{if(!r||typeof r!="object")return[];let s=r;return s.visibility&&s.visibility!=="list"?[]:typeof s.slug!="string"||!s.slug.trim()?[]:[{id:s.slug.trim(),label:typeof s.display_name=="string"&&s.display_name.trim()?s.display_name.trim():s.slug.trim()}]})):[]}catch{return[]}}function Pt(t){return _t(t.split(/\r?\n/).map(e=>e.trim()).flatMap(e=>{if(!e||/^[\s#=-]+$/.test(e))return[];let r=e.match(/[a-z0-9][a-z0-9_.-]+\/[a-z0-9][a-z0-9_.:-]+/i)?.[0]??e.match(/^[a-z0-9][a-z0-9_.:-]+/i)?.[0]??"";return!r||["id","model","models","provider"].includes(r.toLowerCase())?[]:[{id:r,label:r}]}))}function $e(t,e){return new Promise(n=>{let r="",s="",o=!1,i=null,a=Jr(t,e,{stdio:["ignore","pipe","pipe"]}),u=c=>{o||(o=!0,i&&clearTimeout(i),n(c))};i=setTimeout(()=>{a.kill("SIGTERM"),u({stdout:r,stderr:s,code:null})},zr),i.unref?.(),a.stdout.on("data",c=>{r=Dt(r,c.toString())}),a.stderr.on("data",c=>{s=Dt(s,c.toString())}),a.on("error",()=>u({stdout:r,stderr:s,code:null})),a.on("exit",c=>u({stdout:r,stderr:s,code:c}))})}function Dt(t,e){return t.length>=Tt?t:(t+e).slice(0,Tt)}function _t(t){let e=new Map;for(let n of t)n.id&&e.set(n.id,n);return[...e.values()]}function w(t,e){return t.runtimeModel?.trim()||e}import{spawn as Ur}from"node:child_process";function oe(t){try{return JSON.parse(t)}catch{return null}}var A=class{constructor(e){this.spec=e}states=new Map;runTurn(e){return new Promise((n,r)=>{let s={frame:e.frame,context:e.context,prompt:e.prompt,resolve:n,reject:r,output:"",snapshot:"",injected:!1,abort:()=>{}};if(s.abort=()=>{this.removeTurn(e.frame,s),r(new Error("runtime_cancelled"))},e.context.abortSignal?.aborted){s.abort();return}e.context.abortSignal?.addEventListener("abort",s.abort,{once:!0});try{let o=this.ensureState(e);if(o.current){s.injected=!0,o.injected.push(s),s.context.onLog("system",`${this.spec.displayName} busy stdin delivery`),o.child.stdin.write(this.encodeUserMessage(s.prompt,o.sessionId)+`
24
+ `)}function Lt(t,e){return t.trim().replace(/[<>:"/\\|?*\x00-\x1F]/g,"_").replace(/^\.+$/,"").slice(0,160)||e}import{spawn as Hr}from"node:child_process";var Qr=5e3,jt=8*1024*1024;async function $t(t){let e=await Ke(t,["debug","models"]);return e.code!==0?[]:Xr(e.stdout)}async function Nt(t){let e=await Ke(t,["models"]);return e.code!==0?[]:Kt(e.stdout)}async function ie(t){let e=await Ke(t,["models"]);return e.code!==0?[]:Kt(e.stdout)}function Xr(t){let e=t.indexOf("{");if(e<0)return[];try{let n=JSON.parse(t.slice(e));return Array.isArray(n.models)?Bt(n.models.flatMap(r=>{if(!r||typeof r!="object")return[];let s=r;return s.visibility&&s.visibility!=="list"?[]:typeof s.slug!="string"||!s.slug.trim()?[]:[{id:s.slug.trim(),label:typeof s.display_name=="string"&&s.display_name.trim()?s.display_name.trim():s.slug.trim()}]})):[]}catch{return[]}}function Kt(t){return Bt(t.split(/\r?\n/).map(e=>e.trim()).flatMap(e=>{if(!e||/^[\s#=-]+$/.test(e))return[];let r=e.match(/[a-z0-9][a-z0-9_.-]+\/[a-z0-9][a-z0-9_.:-]+/i)?.[0]??e.match(/^[a-z0-9][a-z0-9_.:-]+/i)?.[0]??"";return!r||["id","model","models","provider"].includes(r.toLowerCase())?[]:[{id:r,label:r}]}))}function Ke(t,e){return new Promise(n=>{let r="",s="",o=!1,i=null,a=Hr(t,e,{stdio:["ignore","pipe","pipe"]}),u=l=>{o||(o=!0,i&&clearTimeout(i),n(l))};i=setTimeout(()=>{a.kill("SIGTERM"),u({stdout:r,stderr:s,code:null})},Qr),i.unref?.(),a.stdout.on("data",l=>{r=Ot(r,l.toString())}),a.stderr.on("data",l=>{s=Ot(s,l.toString())}),a.on("error",()=>u({stdout:r,stderr:s,code:null})),a.on("exit",l=>u({stdout:r,stderr:s,code:l}))})}function Ot(t,e){return t.length>=jt?t:(t+e).slice(0,jt)}function Bt(t){let e=new Map;for(let n of t)n.id&&e.set(n.id,n);return[...e.values()]}function I(t,e){return t.runtimeModel?.trim()||e}import{spawn as Yr}from"node:child_process";function ae(t){try{return JSON.parse(t)}catch{return null}}var E=class{constructor(e){this.spec=e}states=new Map;runTurn(e){return new Promise((n,r)=>{let s={frame:e.frame,context:e.context,prompt:e.prompt,resolve:n,reject:r,output:"",snapshot:"",injected:!1,abort:()=>{}};if(s.abort=()=>{this.removeTurn(e.frame,s),r(new Error("runtime_cancelled"))},e.context.abortSignal?.aborted){s.abort();return}e.context.abortSignal?.addEventListener("abort",s.abort,{once:!0});try{let o=this.ensureState(e);if(o.current){s.injected=!0,o.injected.push(s),s.context.onLog("system",`${this.spec.displayName} busy stdin delivery`),o.child.stdin.write(this.encodeUserMessage(s.prompt,o.sessionId)+`
22
25
  `);return}o.queue.push(s),this.pump(o)}catch(o){r(o instanceof Error?o:new Error(String(o)))}})}dispose(e){let n=g(e),r=this.states.get(n);r&&(r.closed=!0,this.clearIdleTimer(r),r.child.kill("SIGTERM"),this.states.delete(n))}notify(e,n){let r=this.states.get(g(e));return!r||r.closed||!r.current?!1:(r.child.stdin.write(this.encodeUserMessage(n,r.sessionId)+`
23
- `),!0)}ensureState(e){let n=e.context.cwd||process.cwd(),r=g(e.frame),s=this.states.get(r);if(s&&!s.closed&&s.command===e.command&&s.cwd===n)return this.clearIdleTimer(s),s;s&&this.dispose(e.frame);let o=w(e.frame,this.spec.resolveModel(e.context.env));e.context.onLog("system",o?`${this.spec.displayName} using model override ${o}`:`${this.spec.displayName} using configured model`);let i=Ur(e.command,this.spec.buildArgs({frame:e.frame,sessionId:e.sessionId,resume:e.resume,model:o}),{cwd:n,env:e.context.env,stdio:["pipe","pipe","pipe"]}),a={agentId:e.frame.agentId,scopeKey:r,command:e.command,cwd:n,env:e.context.env,child:i,stdoutBuffer:"",stderrBuffer:"",sessionId:e.sessionId,current:null,injected:[],queue:[],idleTimer:null,closed:!1};return this.states.set(r,a),this.bindProcess(a),a}bindProcess(e){e.child.stdout.on("data",n=>{e.stdoutBuffer=Lt(e.stdoutBuffer+n.toString(),r=>{this.handleLine(e,r)})}),e.child.stderr.on("data",n=>{e.stderrBuffer=Lt(e.stderrBuffer+n.toString(),r=>{e.current?.context.onLog("stderr",r)})}),e.child.on("error",n=>{this.rejectState(e,n instanceof Error?n:new Error(String(n)))}),e.child.on("close",n=>{e.closed=!0,this.states.delete(e.scopeKey),(e.current||e.queue.length>0)&&this.rejectState(e,new Error(`${this.spec.displayName} process exited with code ${n??"unknown"}`))})}handleLine(e,n){let r=oe(n);if(!r){e.current?.context.onLog("stdout",n);return}let s=this.spec.extractSessionId(r);s&&(e.sessionId=s);let o=this.spec.extractEventName(r);o&&e.current?.context.onLog("system",`[${this.spec.type}:event] ${o}`);let i=this.spec.normalizeError(this.spec.summarizeError(r));if(i){this.finishCurrent(e,new Error(i));return}let a=this.spec.extractText(r);if(a&&e.current){let u=e.current;if(jt(r))u.output.trim()||(u.output=a);else{let c=a.startsWith(u.snapshot)?a:`${u.snapshot}${a}`,l=c.slice(u.snapshot.length);u.snapshot=c,u.output=c,l&&u.context.onPartial?.(l,c)}}jt(r)&&this.finishCurrent(e)}pump(e){if(e.closed||e.current)return;let n=e.queue.shift();if(!n){this.scheduleIdleDispose(e);return}this.clearIdleTimer(e),e.current=n,n.context.onLog("system",`${this.spec.displayName} session ${e.sessionId}`),e.child.stdin.write(this.encodeUserMessage(n.prompt,e.sessionId)+`
24
- `)}finishCurrent(e,n){let r=e.current;if(!r)return;let s=e.injected.splice(0);if(e.current=null,n){this.rejectTurn(r,n);for(let o of s)this.rejectTurn(o,n);for(let o of e.queue.splice(0))this.rejectTurn(o,n);e.closed=!0,e.child.kill("SIGTERM"),this.states.delete(e.scopeKey)}else{this.resolveTurn(r,e.sessionId);for(let o of s)this.resolveTurn(o,e.sessionId);this.pump(e)}}resolveTurn(e,n){this.cleanupTurn(e);let r=e.output.trim();e.resolve({content:r,sessionId:n,suppressMessage:e.injected&&!r})}rejectTurn(e,n){this.cleanupTurn(e),e.reject(n)}cleanupTurn(e){e.context.abortSignal?.removeEventListener("abort",e.abort)}removeTurn(e,n){let r=g(e),s=this.states.get(r);s&&(s.queue=s.queue.filter(o=>o!==n),s.injected=s.injected.filter(o=>o!==n),s.current===n&&(s.current=null,this.clearIdleTimer(s),s.child.kill("SIGTERM"),this.states.delete(r)))}rejectState(e,n){let r=e.current;e.current=null,this.clearIdleTimer(e),r&&this.rejectTurn(r,n);for(let s of e.injected.splice(0))this.rejectTurn(s,n);for(let s of e.queue.splice(0))this.rejectTurn(s,n);this.states.delete(e.scopeKey)}scheduleIdleDispose(e){if(e.closed||e.current||e.queue.length>0||e.idleTimer)return;let n=Gr();n<=0||(e.idleTimer=setTimeout(()=>{e.closed||e.current||e.queue.length>0||(e.closed=!0,e.child.kill("SIGTERM"),this.states.delete(e.scopeKey))},n),e.idleTimer.unref?.())}clearIdleTimer(e){e.idleTimer&&(clearTimeout(e.idleTimer),e.idleTimer=null)}encodeUserMessage(e,n){return JSON.stringify({type:"user",message:{role:"user",content:[{type:"text",text:e}]},session_id:n})}};function Lt(t,e){let n=t.split(/\r?\n/),r=n.pop()??"";for(let s of n)s.trim()&&e(s);return r}function jt(t){return!!(t&&typeof t=="object"&&t.type==="result")}function Gr(){let t=process.env.KSLOCK_RUNTIME_IDLE_TTL_MS;if(!t)return 1800*1e3;let e=Number(t);return!Number.isFinite(e)||e<0?1800*1e3:e}function Ne(t){return t.preset==="read_only"?["--permission-mode","dontAsk","--tools","Read,Grep,Glob,LS"]:t.preset==="full_access"?["--dangerously-skip-permissions"]:["--permission-mode","acceptEdits","--allowedTools",["Read","Grep","Glob","LS","Edit","MultiEdit","Write","Bash(git status *)","Bash(git diff *)","Bash(git log *)","Bash(git show *)","Bash(pnpm test *)","Bash(pnpm run test *)","Bash(pnpm typecheck *)","Bash(pnpm run typecheck *)","Bash(pnpm lint *)","Bash(pnpm run lint *)","Bash(pnpm build *)","Bash(pnpm run build *)","Bash(npm test *)","Bash(npm run test *)","Bash(npm run typecheck *)","Bash(npm run lint *)","Bash(npm run build *)","Bash(yarn test *)","Bash(yarn typecheck *)","Bash(yarn lint *)","Bash(yarn build *)","Bash(tsc *)","Bash(vitest *)"].join(","),"--disallowedTools",["Bash(rm *)","Bash(sudo *)","Bash(chmod *)","Bash(chown *)","Bash(curl *)","Bash(wget *)"].join(",")]}function Ke(t){return["-p","--output-format","stream-json","--verbose","--include-partial-messages",...t.resume?["--resume",t.sessionId]:["--session-id",t.sessionId],...Ot(t.model),...Ne(t.frame.executionPolicy),t.message]}function Be(t){return["--output-format","stream-json","--input-format","stream-json","--verbose","--include-partial-messages",...t.resume?["--resume",t.sessionId]:["--session-id",t.sessionId],...Ot(t.model),...Ne(t.frame.executionPolicy)]}function ie(t){return t.CLAUDE_MODEL?.trim()||t.ANTHROPIC_MODEL?.trim()||null}function Ot(t){return t?["--model",t]:[]}var ae="Claude \u8FD4\u56DE\u9519\u8BEF\uFF0C\u672A\u63D0\u4F9B\u8BE6\u7EC6\u539F\u56E0";function ze(t){if(!t||typeof t!="object")return null;let e=t;for(let n of["type","subtype","event","event_name"])if(typeof e[n]=="string")return e[n];return null}function ue(t){if(!t||typeof t!="object")return null;let e=t;for(let n of["session_id","sessionId"])if(typeof e[n]=="string"&&Wr(e[n]))return e[n];for(let n of Object.values(e)){let r=ue(n);if(r)return r}return null}function P(t){if(!t||typeof t!="object")return null;let e=t;if(typeof e.result=="string"&&e.result.trim())return e.result;if(typeof e.content=="string"&&e.content.trim())return e.content;let n=e.message;if(n&&typeof n=="object"){let r=$t(n.content);if(r)return r}return $t(e.content)}function _(t){if(!t)return null;let e=Vr(t),r=(e?F(e):t)?.trim();return!r||Je(r)?null:r}function E(t){if(!t||typeof t!="object")return null;let e=t,n=typeof e.type=="string"?e.type:"",r=typeof e.subtype=="string"?e.subtype:"";return n==="error"||r==="error"||r==="failure"||e.is_error===!0||n==="result"&&r&&r!=="success"?F(t)??ae:null}function L(t,e){return!t||t===ae&&e!==ae?e:t}function $t(t){if(typeof t=="string"&&t.trim())return t;if(!Array.isArray(t))return null;let e=t.map(n=>{if(!n||typeof n!="object")return"";let r=n;return typeof r.text=="string"?r.text:""}).filter(Boolean).join("");return e.trim()?e:null}function F(t){if(typeof t=="string"&&t&&!Je(t))return t;if(!t||typeof t!="object")return null;let e=t;for(let r of["message","detail","reason","error"]){let s=e[r];if(typeof s=="string"&&s&&!Je(s))return s}let n=F(e.error);if(n)return n;for(let r of Object.values(e)){let s=F(r);if(s)return s}return null}function Wr(t){return/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(t)}function Je(t){let e=t.trim().toLowerCase();return e==="error"||e==="failure"}function Vr(t){try{return JSON.parse(t)}catch{return null}}import{spawn as Hr}from"node:child_process";function qt(t){return new Promise((e,n)=>{let r=[],s=[],o="",i=[],a="",u="",c="",l=Hr(t.command,t.args,{cwd:t.cwd||process.cwd(),env:t.env,stdio:["ignore","pipe","pipe"]});if(t.signal?.aborted){l.kill("SIGTERM"),n(new Error("runtime_cancelled"));return}t.signal?.addEventListener("abort",()=>{l.kill("SIGTERM"),n(new Error("runtime_cancelled"))},{once:!0}),l.stdout.on("data",d=>{u=Nt(u+d.toString(),p=>{let m=Kt(p);if(m){let f=t.onJson(m)??E(m);f&&(a=L(a,f));let y=P(m);y&&(Bt(m)?s.push(y):o=Jt(y,o,t.onPartial));return}r.push(p),t.onLog("stdout",p)})}),l.stderr.on("data",d=>{c=Nt(c+d.toString(),p=>{i.push(p),t.onLog("stderr",p)})}),l.on("error",n),l.on("exit",d=>{if(u.trim()){let m=Kt(u.trim());if(m){let f=t.onJson(m)??E(m);f&&(a=L(a,f));let y=P(m);y&&(Bt(m)?s.push(y):o=Jt(y,o,t.onPartial))}else r.push(u.trim()),t.onLog("stdout",u.trim())}if(c.trim()&&(i.push(c.trim()),t.onLog("stderr",c.trim())),t.onLog("system",`${t.command} exited with code ${d??"unknown"}`),d===0){e({output:Qr(s,o,r)});return}let p=_(zt(i))??zt(i);n(new Error(a||p||`${t.command} exited with code ${d}`))})})}function Nt(t,e){let n=t.split(/\r?\n/),r=n.pop()??"";for(let s of n)s.trim()&&e(s);return r}function Kt(t){try{return JSON.parse(t)}catch{return null}}function Bt(t){return!!(t&&typeof t=="object"&&t.type==="result")}function Jt(t,e,n){let r=t.startsWith(e)?t:`${e}${t}`,s=r.slice(e.length);return s&&n?.(s,r),r}function Qr(t,e,n){let r=t.at(-1)?.trim();if(r)return r;let s=e.trim();return s||n.join(`
25
- `).trim()}function zt(t){return t.map(e=>e.trim()).filter(Boolean).at(-1)??""}import{mkdir as Xr,readFile as Yr,writeFile as Ut}from"node:fs/promises";import{join as Zr}from"node:path";var es="claude-last-message.txt";function ts(t){return Zr(h(t),es)}async function Gt(t){await Xr(h(t),{recursive:!0});let e=ts(t);return await Ut(e,"","utf8"),e}async function Wt(t,e){await Ut(t,e,"utf8")}async function Vt(t){try{return await Yr(t,"utf8")}catch{return""}}var j=class{type="claude";busyDeliveryMode="gated";sessions=new A({type:"claude",displayName:"claude",buildArgs:Be,resolveModel:ie,extractSessionId:ue,extractText:P,extractEventName:ze,summarizeError:E,normalizeError:_,preferFailure:L});capabilities={supportsSession:!0,supportsStreaming:!0,supportsExecutionPolicy:!0};async detect(){return!!await this.resolveCommand()}notifyInbox(e,n){return this.sessions.notify(e,n)}async listModels(){let e=await this.resolveCommand();return e?se(e):[]}async handleDelivery(e,n){let r=await this.resolveCommand();if(!r)throw new Error("claude runtime is not available");let s=await I(e),o=s?.runtimeType===this.type?s.runtimeSessionId:null,i=o??Ht(),a=o?`resuming ${o}`:`starting new session ${i}`;n.onLog("system",`claude ${a} in ${n.cwd??process.cwd()}`);let u;try{u=await this.runClaudeStream(r,e,n,i,!!o)}catch(c){if(Qt(c))n.onLog("stderr",`claude stream-json session failed; falling back to print mode. ${c instanceof Error?c.message:""}`),u=await this.runClaude(r,e,n,i,!!o);else if(o){let l=Ht();n.onLog("stderr",`claude resume ${o} failed; starting a new session ${l}. ${c instanceof Error?c.message:""}`),u=await this.runClaudeStream(r,e,n,l,!1).catch(d=>{if(!Qt(d))throw d;return n.onLog("stderr",`claude stream retry failed; falling back to print mode. ${d instanceof Error?d.message:""}`),this.runClaude(r,e,n,l,!1)})}else throw c}return await x({agentId:e.agentId,channelId:e.channelId,runtimeScopeKey:g(e),runtimeType:this.type,runtimeSessionId:u.sessionId,workspacePath:n.cwd??null,updatedAt:new Date().toISOString()}),n.onLog("system",`claude session ${u.sessionId}`),u.suppressMessage?{content:u.output.trim(),sessionId:u.sessionId,suppressMessage:!0}:{content:u.output.trim()||"claude runtime completed without output",sessionId:u.sessionId}}async runClaudeStream(e,n,r,s,o){r.onLog("system",o?`claude stream resume ${s}`:`claude stream session-id ${s}`);let i=await this.sessions.runTurn({command:e,frame:n,context:r,prompt:await v(n,{workspacePath:r.cwd}),sessionId:s,resume:o});return{output:i.content,sessionId:i.sessionId??s,suppressMessage:i.suppressMessage}}async runClaude(e,n,r,s,o){let i=await v(n,{workspacePath:r.cwd}),a=w(n,ie(r.env)),u=await Gt(n);r.onLog("system",a?`claude using model override ${a}`:"claude using configured model"),r.onLog("system",o?`claude resume ${s}`:`claude session-id ${s}`);let c=new Set,l=await qt({command:e,args:Ke({frame:n,message:i,sessionId:s,resume:o,model:a}),cwd:r.cwd,env:r.env,signal:r.abortSignal,onPartial:r.onPartial,onJson:d=>{let p=ue(d);p&&c.add(p);let m=ze(d);m&&r.onLog("system",`[claude:event] ${m}`);let f=_(E(d));return f?(r.onLog("stderr",`[claude:error] ${f}`),f):null},onLog:r.onLog});return await Wt(u,l.output),{output:l.output||await Vt(u),sessionId:[...c].at(-1)??s}}async resolveCommand(){return S({command:"claude",envVars:["CLAUDE_BIN"]})}};function Qt(t){if(process.env.KSLOCK_CLAUDE_DISABLE_PRINT_FALLBACK==="1")return!1;let e=t instanceof Error?t.message:String(t);return/process exited|unknown option|unrecognized|input-format|stream-json|stdin|not supported/i.test(e)}import{mkdir as ps,writeFile as gs}from"node:fs/promises";import{join as fs}from"node:path";function k(t){return t.preset==="read_only"?"read-only":t.preset==="full_access"?"danger-full-access":"workspace-write"}function qe(t){let e=["-a","never","-s",k(t.frame.executionPolicy),"exec"];return t.sessionId?[...e,"resume","--json","--skip-git-repo-check",...Xt(t.model),"--output-last-message",t.outputFile,t.sessionId,t.prompt]:[...e,"--json","--skip-git-repo-check",...Xt(t.model),"--output-last-message",t.outputFile,t.prompt]}function ce(t){return t.CODEX_MODEL?.trim()||null}function Xt(t){return t?["--model",t]:[]}var Ue="Codex \u8FD4\u56DE\u9519\u8BEF\uFF0C\u672A\u63D0\u4F9B\u8BE6\u7EC6\u539F\u56E0",ns=/The ['‘"]?([^'’"]+)['’"]? model requires a newer version of Codex\. Please upgrade to the latest app or CLI and try again\./i;function de(t){if(!t||typeof t!="object")return null;let e=t;for(let n of["type","event","event_name","method"])if(typeof e[n]=="string")return e[n];return null}function Ge(t){if(!t||typeof t!="object")return null;let e=t;for(let n of["session_id","sessionId","thread_id","threadId","conversation_id","conversationId"])if(typeof e[n]=="string"&&e[n])return e[n];for(let n of Object.values(e)){let r=Ge(n);if(r)return r}return null}function $(t){if(!t)return null;let e=ss(t),r=(e?O(e):t)?.trim();return!r||le(r)?null:rs(r)}function me(t){if(!t||typeof t!="object")return null;let e=de(t);return e!=="error"&&e!=="turn.failed"?null:Ue}function T(t){if(!t||typeof t!="object")return null;let e=t,n=de(t);if(n!=="error"&&n!=="turn.failed")return null;for(let s of["message","detail","reason"])if(typeof e[s]=="string"&&e[s]&&!le(e[s]))return e[s];let r=O(e.error);if(r)return r;for(let[s,o]of Object.entries(e)){if(["type","event","event_name","method","status","error"].includes(s))continue;let i=O(o);if(i)return i}return null}function We(t,e){return!t||t===Ue&&e!==Ue?e:t}function O(t){if(typeof t=="string"&&t&&!le(t))return t;if(!t||typeof t!="object")return null;let e=t;for(let r of["message","detail","reason"])if(typeof e[r]=="string"&&e[r]&&!le(e[r]))return e[r];let n=O(e.error);if(n)return n;for(let r of Object.values(e)){let s=O(r);if(s)return s}return null}function le(t){let e=t.trim().toLowerCase();return e==="error"||e==="turn.failed"}function rs(t){let e=t.match(ns);return e?`Codex CLI \u7248\u672C\u8FC7\u4F4E\uFF0C\u6682\u4E0D\u652F\u6301\u6A21\u578B ${e[1]??"\u5F53\u524D\u6A21\u578B"}\u3002\u8BF7\u5347\u7EA7 Codex CLI \u540E\u91CD\u542F daemon\uFF0C\u6216\u5C06 CODEX_MODEL \u8C03\u6574\u4E3A\u5F53\u524D CLI \u652F\u6301\u7684\u6A21\u578B\u540E\u91CD\u8BD5\u3002`:t}function ss(t){try{return JSON.parse(t)}catch{return null}}import{spawn as os}from"node:child_process";var pe=class{states=new Map;async runTurn(e){return new Promise((n,r)=>{let s={frame:e.frame,context:e.context,prompt:e.prompt,resolve:n,reject:r,output:"",streamedAgentMessageIds:new Set,streamedReasoningIds:new Set,injected:!1,steerSent:!1,abort:()=>{}};if(s.abort=()=>{this.removeTurn(e.frame,s),r(new Error("runtime_cancelled"))},e.context.abortSignal?.aborted){s.abort();return}e.context.abortSignal?.addEventListener("abort",s.abort,{once:!0});try{let o=this.ensureState(e);if(o.current||o.queue.length>0){s.injected=!0,o.injected.push(s),s.context.onLog("system","codex steering active turn"),this.flushInjectedSteers(o);return}o.queue.push(s),this.pump(o)}catch(o){r(o instanceof Error?o:new Error(String(o)))}})}dispose(e){let n=g(e),r=this.states.get(n);r&&(r.closed=!0,this.clearIdleTimer(r),r.child.kill("SIGTERM"),this.states.delete(n))}notify(e,n){let r=this.states.get(g(e));return!r||r.closed||!r.current&&r.queue.length===0?!1:(r.pendingNotifications.push(n),this.flushPendingNotifications(r),!0)}ensureState(e){let n=g(e.frame),r=e.context.cwd||process.cwd(),s=this.states.get(n);if(s&&!s.closed&&s.command===e.command&&s.cwd===r)return this.clearIdleTimer(s),s;s&&this.dispose(e.frame);let o=os(e.command,["app-server","--listen","stdio://"],{cwd:r,env:e.context.env,stdio:["pipe","pipe","pipe"]}),i={agentId:e.frame.agentId,scopeKey:n,command:e.command,cwd:r,env:e.context.env,child:o,stdoutBuffer:"",stderrBuffer:"",requestId:0,initializeRequestId:null,pendingThreadRequest:this.buildThreadRequest(e.frame,e.sessionId,e.model,r),threadId:null,activeTurnId:null,sessionAnnounced:!1,current:null,injected:[],pendingNotifications:[],queue:[],idleTimer:null,closed:!1};return this.states.set(n,i),this.bindProcess(n,i),i.initializeRequestId=this.sendRequest(i,"initialize",{clientInfo:{name:"kslock-daemon",version:"1.0.0"},capabilities:{experimentalApi:!0}}),i}buildThreadRequest(e,n,r,s){let o={cwd:s,approvalPolicy:"never",sandbox:k(e.executionPolicy),developerInstructions:[`You are @${e.agentHandle} in kslock channel ${e.channelId}.`,"Reply with the final answer for the delivered message."].join(`
26
- `)};return r&&(o.model=r),n?{method:"thread/resume",params:{threadId:n,...o}}:{method:"thread/start",params:o}}bindProcess(e,n){n.child.stdout.on("data",r=>{n.stdoutBuffer=Yt(n.stdoutBuffer+r.toString(),s=>{for(let o of this.parseLine(n,s))this.handleEvent(n,o)})}),n.child.stderr.on("data",r=>{n.stderrBuffer=Yt(n.stderrBuffer+r.toString(),s=>{n.current?.context.onLog("stderr",s)})}),n.child.on("error",r=>{this.rejectState(e,n,r instanceof Error?r:new Error(String(r)))}),n.child.on("close",r=>{n.closed=!0,this.states.delete(e);let s=new Error(`codex app-server exited with code ${r??"unknown"}`);(n.current||n.queue.length>0)&&this.rejectState(e,n,s)})}parseLine(e,n){let r=oe(n);if(!r||typeof r!="object")return[];let s=r,o=[];if(s.result&&typeof s.result=="object"){if(s.id===e.initializeRequestId)return e.initializeRequestId=null,this.sendNotification(e,"initialized",{}),e.pendingThreadRequest&&(this.sendRequest(e,e.pendingThreadRequest.method,e.pendingThreadRequest.params),e.pendingThreadRequest=null),o;let i=s.result,a=i.thread;if(a&&typeof a=="object"&&typeof a.id=="string")return this.handleThreadReady(e,String(a.id),o),o;let u=i.turn;if(u&&typeof u=="object"&&typeof u.id=="string")return e.activeTurnId=String(u.id),this.flushInjectedSteers(e),this.flushPendingNotifications(e),o;if(typeof i.turnId=="string")return e.activeTurnId=i.turnId,this.flushInjectedSteers(e),this.flushPendingNotifications(e),o}if(s.error){let i=$(T(s.error))??T(s.error);return o.push({kind:"error",message:i||"Codex app-server request failed"}),o}switch(s.method){case"thread/started":{let i=s.params,a=i&&typeof i=="object"?i.thread:null,u=a&&typeof a=="object"?a.id:null;typeof u=="string"&&this.handleThreadReady(e,u,o);break}case"turn/started":{let i=s.params,a=i&&typeof i=="object"?i.turn:null,u=a&&typeof a=="object"?a.id:null;typeof u=="string"&&(e.activeTurnId=u,this.flushInjectedSteers(e),this.flushPendingNotifications(e)),o.push({kind:"thinking",text:""});break}case"item/agentMessage/delta":{let i=s.params,a=i?.delta,u=i?.itemId;typeof u=="string"&&e.current?.streamedAgentMessageIds.add(u),typeof a=="string"&&a.length>0&&o.push({kind:"text",text:a});break}case"item/reasoning/summaryTextDelta":case"item/reasoning/textDelta":{let i=s.params,a=i?.delta,u=i?.itemId;typeof u=="string"&&e.current?.streamedReasoningIds.add(u),typeof a=="string"&&a.length>0&&o.push({kind:"thinking",text:a});break}case"item/started":case"item/completed":o.push(...this.parseItemEvent(e,s));break;case"turn/completed":{let a=s.params?.turn,u=a?.error;if(a?.status==="failed"&&u&&typeof u=="object"){let c=u.message;o.push({kind:"error",message:typeof c=="string"?c:"Codex turn failed"})}e.activeTurnId=null,o.push({kind:"turn_end",sessionId:e.threadId});break}case"error":{let i=$(T(s.params))??me(s);o.push({kind:"error",message:i||"Unknown Codex app-server error"});break}}return o}parseItemEvent(e,n){let r=[],o=n.params?.item;if(!o||typeof o.type!="string")return r;let i=n.method==="item/started",a=n.method==="item/completed";switch(o.type){case"reasoning":if(a&&typeof o.id=="string"&&!e.current?.streamedReasoningIds.has(o.id)){let u=is(o);u&&r.push({kind:"thinking",text:u})}break;case"agentMessage":a&&typeof o.id=="string"&&!e.current?.streamedAgentMessageIds.has(o.id)&&typeof o.text=="string"&&o.text&&r.push({kind:"text",text:o.text});break;case"commandExecution":i&&typeof o.command=="string"&&r.push({kind:"tool_call",name:"shell",input:{command:o.command}}),a&&r.push({kind:"tool_output",name:"shell"});break;case"contextCompaction":i&&r.push({kind:"compaction_started"}),a&&r.push({kind:"compaction_finished"});break;case"mcpToolCall":i&&r.push({kind:"tool_call",name:typeof o.tool=="string"?o.tool:"mcp_tool",input:o.arguments}),a&&r.push({kind:"tool_output",name:typeof o.tool=="string"?o.tool:"mcp_tool"});break}return r}handleEvent(e,n){let r=e.current;if(!(!r&&n.kind!=="session"))switch(n.kind){case"session":e.threadId=n.sessionId,r?.context.onLog("system",`codex session ${n.sessionId}`);break;case"thinking":n.text&&r?.context.onLog("system",`[codex:thinking] ${n.text}`);break;case"text":this.appendText(e,n.text);break;case"tool_call":r?.context.onLog("system",`[codex:tool] ${n.name}`);break;case"tool_output":r?.context.onLog("system",`[codex:tool_result] ${n.name}`);break;case"compaction_started":r?.context.onLog("system","codex compacting context");break;case"compaction_finished":r?.context.onLog("system","codex context compaction finished");break;case"error":this.finishCurrent(e,new Error(n.message));break;case"turn_end":n.sessionId&&(e.threadId=n.sessionId),this.finishCurrent(e);break}}handleThreadReady(e,n,r){e.threadId=n,e.sessionAnnounced||(r.push({kind:"session",sessionId:n}),e.sessionAnnounced=!0),this.pump(e)}pump(e){if(e.current||e.closed||!e.threadId)return;let n=e.queue.shift();if(!n){this.scheduleIdleDispose(e);return}this.clearIdleTimer(e),e.current=n,n.context.onLog("system",e.activeTurnId?"codex steering active turn":"codex starting turn"),this.sendRequest(e,"turn/start",{threadId:e.threadId,input:[{type:"text",text:n.prompt}]})}finishCurrent(e,n){let r=e.current;if(!r)return;let s=e.injected.splice(0);if(e.current=null,n){this.rejectTurn(r,n);for(let o of s)this.rejectTurn(o,n);for(let o of e.queue.splice(0))this.rejectTurn(o,n);e.closed=!0,this.clearIdleTimer(e),e.child.kill("SIGTERM"),this.states.delete(g(r.frame))}else{this.resolveTurn(r,e.threadId);for(let o of s)this.resolveTurn(o,e.threadId);this.pump(e)}}appendText(e,n){let r=e.current;r&&(r.output+=n,r.context.onPartial?.(n,r.output))}flushInjectedSteers(e){if(!(!e.threadId||!e.activeTurnId))for(let n of e.injected)n.steerSent||(n.steerSent=!0,this.sendRequest(e,"turn/steer",{threadId:e.threadId,expectedTurnId:e.activeTurnId,input:[{type:"text",text:n.prompt}]}))}flushPendingNotifications(e){if(!(!e.threadId||!e.activeTurnId||e.pendingNotifications.length===0))for(let n of e.pendingNotifications.splice(0))this.sendRequest(e,"turn/steer",{threadId:e.threadId,expectedTurnId:e.activeTurnId,input:[{type:"text",text:n}]})}resolveTurn(e,n){this.cleanupTurn(e);let r=e.output.trim();e.resolve({content:r,sessionId:n,suppressMessage:e.injected&&!r})}rejectTurn(e,n){this.cleanupTurn(e),e.reject(n)}cleanupTurn(e){e.context.abortSignal?.removeEventListener("abort",e.abort),e.streamedAgentMessageIds.clear(),e.streamedReasoningIds.clear()}removeTurn(e,n){let r=g(e),s=this.states.get(r);s&&(s.queue=s.queue.filter(o=>o!==n),s.injected=s.injected.filter(o=>o!==n),s.current===n&&(s.current=null,this.clearIdleTimer(s),s.child.kill("SIGTERM"),this.states.delete(r)))}rejectState(e,n,r){let s=n.current;n.current=null,this.clearIdleTimer(n),s&&this.rejectTurn(s,r);for(let o of n.injected.splice(0))this.rejectTurn(o,r);for(let o of n.queue.splice(0))this.rejectTurn(o,r);this.states.delete(e)}scheduleIdleDispose(e){if(e.closed||e.current||e.queue.length>0||e.idleTimer)return;let n=as();n<=0||(e.idleTimer=setTimeout(()=>{e.closed||e.current||e.queue.length>0||(e.closed=!0,e.child.kill("SIGTERM"),this.states.delete(e.scopeKey))},n),e.idleTimer.unref?.())}clearIdleTimer(e){e.idleTimer&&(clearTimeout(e.idleTimer),e.idleTimer=null)}sendRequest(e,n,r){return e.requestId+=1,e.child.stdin.write(JSON.stringify({jsonrpc:"2.0",id:e.requestId,method:n,params:r})+`
26
+ `),!0)}ensureState(e){let n=e.context.cwd||process.cwd(),r=g(e.frame),s=this.states.get(r);if(s&&!s.closed&&s.command===e.command&&s.cwd===n)return this.clearIdleTimer(s),s;s&&this.dispose(e.frame);let o=I(e.frame,this.spec.resolveModel(e.context.env));e.context.onLog("system",o?`${this.spec.displayName} using model override ${o}`:`${this.spec.displayName} using configured model`);let i=Yr(e.command,this.spec.buildArgs({frame:e.frame,sessionId:e.sessionId,resume:e.resume,model:o}),{cwd:n,env:e.context.env,stdio:["pipe","pipe","pipe"]}),a={agentId:e.frame.agentId,scopeKey:r,command:e.command,cwd:n,env:e.context.env,child:i,stdoutBuffer:"",stderrBuffer:"",sessionId:e.sessionId,current:null,injected:[],queue:[],idleTimer:null,closed:!1};return this.states.set(r,a),this.bindProcess(a),a}bindProcess(e){e.child.stdout.on("data",n=>{e.stdoutBuffer=qt(e.stdoutBuffer+n.toString(),r=>{this.handleLine(e,r)})}),e.child.stderr.on("data",n=>{e.stderrBuffer=qt(e.stderrBuffer+n.toString(),r=>{e.current?.context.onLog("stderr",r)})}),e.child.on("error",n=>{this.rejectState(e,n instanceof Error?n:new Error(String(n)))}),e.child.on("close",n=>{e.closed=!0,this.states.delete(e.scopeKey),(e.current||e.queue.length>0)&&this.rejectState(e,new Error(`${this.spec.displayName} process exited with code ${n??"unknown"}`))})}handleLine(e,n){let r=ae(n);if(!r){e.current?.context.onLog("stdout",n);return}let s=this.spec.extractSessionId(r);s&&(e.sessionId=s);let o=this.spec.extractEventName(r);o&&e.current?.context.onLog("system",`[${this.spec.type}:event] ${o}`);let i=this.spec.normalizeError(this.spec.summarizeError(r));if(i){this.finishCurrent(e,new Error(i));return}let a=this.spec.extractText(r);if(a&&e.current){let u=e.current;if(Jt(r))u.output.trim()||(u.output=a);else{let l=a.startsWith(u.snapshot)?a:`${u.snapshot}${a}`,c=l.slice(u.snapshot.length);u.snapshot=l,u.output=l,c&&u.context.onPartial?.(c,l)}}Jt(r)&&this.finishCurrent(e)}pump(e){if(e.closed||e.current)return;let n=e.queue.shift();if(!n){this.scheduleIdleDispose(e);return}this.clearIdleTimer(e),e.current=n,n.context.onLog("system",`${this.spec.displayName} session ${e.sessionId}`),e.child.stdin.write(this.encodeUserMessage(n.prompt,e.sessionId)+`
27
+ `)}finishCurrent(e,n){let r=e.current;if(!r)return;let s=e.injected.splice(0);if(e.current=null,n){this.rejectTurn(r,n);for(let o of s)this.rejectTurn(o,n);for(let o of e.queue.splice(0))this.rejectTurn(o,n);e.closed=!0,e.child.kill("SIGTERM"),this.states.delete(e.scopeKey)}else{this.resolveTurn(r,e.sessionId);for(let o of s)this.resolveTurn(o,e.sessionId);this.pump(e)}}resolveTurn(e,n){this.cleanupTurn(e);let r=e.output.trim();e.resolve({content:r,sessionId:n,suppressMessage:e.injected&&!r})}rejectTurn(e,n){this.cleanupTurn(e),e.reject(n)}cleanupTurn(e){e.context.abortSignal?.removeEventListener("abort",e.abort)}removeTurn(e,n){let r=g(e),s=this.states.get(r);s&&(s.queue=s.queue.filter(o=>o!==n),s.injected=s.injected.filter(o=>o!==n),s.current===n&&(s.current=null,this.clearIdleTimer(s),s.child.kill("SIGTERM"),this.states.delete(r)))}rejectState(e,n){let r=e.current;e.current=null,this.clearIdleTimer(e),r&&this.rejectTurn(r,n);for(let s of e.injected.splice(0))this.rejectTurn(s,n);for(let s of e.queue.splice(0))this.rejectTurn(s,n);this.states.delete(e.scopeKey)}scheduleIdleDispose(e){if(e.closed||e.current||e.queue.length>0||e.idleTimer)return;let n=Zr();n<=0||(e.idleTimer=setTimeout(()=>{e.closed||e.current||e.queue.length>0||(e.closed=!0,e.child.kill("SIGTERM"),this.states.delete(e.scopeKey))},n),e.idleTimer.unref?.())}clearIdleTimer(e){e.idleTimer&&(clearTimeout(e.idleTimer),e.idleTimer=null)}encodeUserMessage(e,n){return JSON.stringify({type:"user",message:{role:"user",content:[{type:"text",text:e}]},session_id:n})}};function qt(t,e){let n=t.split(/\r?\n/),r=n.pop()??"";for(let s of n)s.trim()&&e(s);return r}function Jt(t){return!!(t&&typeof t=="object"&&t.type==="result")}function Zr(){let t=process.env.KSLOCK_RUNTIME_IDLE_TTL_MS;if(!t)return 1800*1e3;let e=Number(t);return!Number.isFinite(e)||e<0?1800*1e3:e}function Be(t){return t.preset==="read_only"?["--permission-mode","dontAsk","--tools","Read,Grep,Glob,LS"]:t.preset==="full_access"?["--dangerously-skip-permissions"]:["--permission-mode","acceptEdits","--allowedTools",["Read","Grep","Glob","LS","Edit","MultiEdit","Write","Bash(git status *)","Bash(git diff *)","Bash(git log *)","Bash(git show *)","Bash(pnpm test *)","Bash(pnpm run test *)","Bash(pnpm typecheck *)","Bash(pnpm run typecheck *)","Bash(pnpm lint *)","Bash(pnpm run lint *)","Bash(pnpm build *)","Bash(pnpm run build *)","Bash(npm test *)","Bash(npm run test *)","Bash(npm run typecheck *)","Bash(npm run lint *)","Bash(npm run build *)","Bash(yarn test *)","Bash(yarn typecheck *)","Bash(yarn lint *)","Bash(yarn build *)","Bash(tsc *)","Bash(vitest *)"].join(","),"--disallowedTools",["Bash(rm *)","Bash(sudo *)","Bash(chmod *)","Bash(chown *)","Bash(curl *)","Bash(wget *)"].join(",")]}function qe(t){return["-p","--output-format","stream-json","--verbose","--include-partial-messages",...t.resume?["--resume",t.sessionId]:["--session-id",t.sessionId],...zt(t.model),...Be(t.frame.executionPolicy),t.message]}function Je(t){return["--output-format","stream-json","--input-format","stream-json","--verbose","--include-partial-messages",...t.resume?["--resume",t.sessionId]:["--session-id",t.sessionId],...zt(t.model),...Be(t.frame.executionPolicy)]}function ue(t){return t.CLAUDE_MODEL?.trim()||t.ANTHROPIC_MODEL?.trim()||null}function zt(t){return t?["--model",t]:[]}var le="Claude \u8FD4\u56DE\u9519\u8BEF\uFF0C\u672A\u63D0\u4F9B\u8BE6\u7EC6\u539F\u56E0";function Ue(t){if(!t||typeof t!="object")return null;let e=t;for(let n of["type","subtype","event","event_name"])if(typeof e[n]=="string")return e[n];return null}function ce(t){if(!t||typeof t!="object")return null;let e=t;for(let n of["session_id","sessionId"])if(typeof e[n]=="string"&&es(e[n]))return e[n];for(let n of Object.values(e)){let r=ce(n);if(r)return r}return null}function j(t){if(!t||typeof t!="object")return null;let e=t;if(typeof e.result=="string"&&e.result.trim())return e.result;if(typeof e.content=="string"&&e.content.trim())return e.content;let n=e.message;if(n&&typeof n=="object"){let r=Ut(n.content);if(r)return r}return Ut(e.content)}function O(t){if(!t)return null;let e=ts(t),r=(e?L(e):t)?.trim();return!r||ze(r)?null:r}function T(t){if(!t||typeof t!="object")return null;let e=t,n=typeof e.type=="string"?e.type:"",r=typeof e.subtype=="string"?e.subtype:"";return n==="error"||r==="error"||r==="failure"||e.is_error===!0||n==="result"&&r&&r!=="success"?L(t)??le:null}function $(t,e){return!t||t===le&&e!==le?e:t}function Ut(t){if(typeof t=="string"&&t.trim())return t;if(!Array.isArray(t))return null;let e=t.map(n=>{if(!n||typeof n!="object")return"";let r=n;return typeof r.text=="string"?r.text:""}).filter(Boolean).join("");return e.trim()?e:null}function L(t){if(typeof t=="string"&&t&&!ze(t))return t;if(!t||typeof t!="object")return null;let e=t;for(let r of["message","detail","reason","error"]){let s=e[r];if(typeof s=="string"&&s&&!ze(s))return s}let n=L(e.error);if(n)return n;for(let r of Object.values(e)){let s=L(r);if(s)return s}return null}function es(t){return/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(t)}function ze(t){let e=t.trim().toLowerCase();return e==="error"||e==="failure"}function ts(t){try{return JSON.parse(t)}catch{return null}}import{spawn as ns}from"node:child_process";function Xt(t){return new Promise((e,n)=>{let r=[],s=[],o="",i=[],a="",u="",l="",c=ns(t.command,t.args,{cwd:t.cwd||process.cwd(),env:t.env,stdio:["ignore","pipe","pipe"]});if(t.signal?.aborted){c.kill("SIGTERM"),n(new Error("runtime_cancelled"));return}t.signal?.addEventListener("abort",()=>{c.kill("SIGTERM"),n(new Error("runtime_cancelled"))},{once:!0}),c.stdout.on("data",d=>{u=Gt(u+d.toString(),m=>{let p=Wt(m);if(p){let f=t.onJson(p)??T(p);f&&(a=$(a,f));let y=j(p);y&&(Vt(p)?s.push(y):o=Ht(y,o,t.onPartial));return}r.push(m),t.onLog("stdout",m)})}),c.stderr.on("data",d=>{l=Gt(l+d.toString(),m=>{i.push(m),t.onLog("stderr",m)})}),c.on("error",n),c.on("exit",d=>{if(u.trim()){let p=Wt(u.trim());if(p){let f=t.onJson(p)??T(p);f&&(a=$(a,f));let y=j(p);y&&(Vt(p)?s.push(y):o=Ht(y,o,t.onPartial))}else r.push(u.trim()),t.onLog("stdout",u.trim())}if(l.trim()&&(i.push(l.trim()),t.onLog("stderr",l.trim())),t.onLog("system",`${t.command} exited with code ${d??"unknown"}`),d===0){e({output:rs(s,o,r)});return}let m=O(Qt(i))??Qt(i);n(new Error(a||m||`${t.command} exited with code ${d}`))})})}function Gt(t,e){let n=t.split(/\r?\n/),r=n.pop()??"";for(let s of n)s.trim()&&e(s);return r}function Wt(t){try{return JSON.parse(t)}catch{return null}}function Vt(t){return!!(t&&typeof t=="object"&&t.type==="result")}function Ht(t,e,n){let r=t.startsWith(e)?t:`${e}${t}`,s=r.slice(e.length);return s&&n?.(s,r),r}function rs(t,e,n){let r=t.at(-1)?.trim();if(r)return r;let s=e.trim();return s||n.join(`
28
+ `).trim()}function Qt(t){return t.map(e=>e.trim()).filter(Boolean).at(-1)??""}import{mkdir as ss,readFile as os,writeFile as Yt}from"node:fs/promises";import{join as is}from"node:path";var as="claude-last-message.txt";function us(t){return is(h(t),as)}async function Zt(t){await ss(h(t),{recursive:!0});let e=us(t);return await Yt(e,"","utf8"),e}async function en(t,e){await Yt(t,e,"utf8")}async function tn(t){try{return await os(t,"utf8")}catch{return""}}var N=class{type="claude";busyDeliveryMode="gated";sessions=new E({type:"claude",displayName:"claude",buildArgs:Je,resolveModel:ue,extractSessionId:ce,extractText:j,extractEventName:Ue,summarizeError:T,normalizeError:O,preferFailure:$});capabilities={supportsSession:!0,supportsStreaming:!0,supportsExecutionPolicy:!0};async detect(){return!!await this.resolveCommand()}notifyInbox(e,n){return this.sessions.notify(e,n)}async listModels(){let e=await this.resolveCommand();return e?ie(e):[]}async handleDelivery(e,n){let r=await this.resolveCommand();if(!r)throw new Error("claude runtime is not available");let s=await w(e),o=s?.runtimeType===this.type?s.runtimeSessionId:null,i=o??nn(),a=o?`resuming ${o}`:`starting new session ${i}`;n.onLog("system",`claude ${a} in ${n.cwd??process.cwd()}`);let u;try{u=await this.runClaudeStream(r,e,n,i,!!o)}catch(l){if(rn(l))n.onLog("stderr",`claude stream-json session failed; falling back to print mode. ${l instanceof Error?l.message:""}`),u=await this.runClaude(r,e,n,i,!!o);else if(o){let c=nn();n.onLog("stderr",`claude resume ${o} failed; starting a new session ${c}. ${l instanceof Error?l.message:""}`),u=await this.runClaudeStream(r,e,n,c,!1).catch(d=>{if(!rn(d))throw d;return n.onLog("stderr",`claude stream retry failed; falling back to print mode. ${d instanceof Error?d.message:""}`),this.runClaude(r,e,n,c,!1)})}else throw l}return await S({agentId:e.agentId,channelId:e.channelId,runtimeScopeKey:g(e),runtimeType:this.type,runtimeSessionId:u.sessionId,workspacePath:n.cwd??null,updatedAt:new Date().toISOString()}),n.onLog("system",`claude session ${u.sessionId}`),u.suppressMessage?{content:u.output.trim(),sessionId:u.sessionId,suppressMessage:!0}:{content:u.output.trim()||"claude runtime completed without output",sessionId:u.sessionId}}async runClaudeStream(e,n,r,s,o){r.onLog("system",o?`claude stream resume ${s}`:`claude stream session-id ${s}`);let i=await this.sessions.runTurn({command:e,frame:n,context:r,prompt:await v(n,{workspacePath:r.cwd}),sessionId:s,resume:o});return{output:i.content,sessionId:i.sessionId??s,suppressMessage:i.suppressMessage}}async runClaude(e,n,r,s,o){let i=await v(n,{workspacePath:r.cwd}),a=I(n,ue(r.env)),u=await Zt(n);r.onLog("system",a?`claude using model override ${a}`:"claude using configured model"),r.onLog("system",o?`claude resume ${s}`:`claude session-id ${s}`);let l=new Set,c=await Xt({command:e,args:qe({frame:n,message:i,sessionId:s,resume:o,model:a}),cwd:r.cwd,env:r.env,signal:r.abortSignal,onPartial:r.onPartial,onJson:d=>{let m=ce(d);m&&l.add(m);let p=Ue(d);p&&r.onLog("system",`[claude:event] ${p}`);let f=O(T(d));return f?(r.onLog("stderr",`[claude:error] ${f}`),f):null},onLog:r.onLog});return await en(u,c.output),{output:c.output||await tn(u),sessionId:[...l].at(-1)??s}}async resolveCommand(){return x({command:"claude",envVars:["CLAUDE_BIN"]})}};function rn(t){if(process.env.KSLOCK_CLAUDE_DISABLE_PRINT_FALLBACK==="1")return!1;let e=t instanceof Error?t.message:String(t);return/process exited|unknown option|unrecognized|input-format|stream-json|stdin|not supported/i.test(e)}import{mkdir as ws,writeFile as Ss}from"node:fs/promises";import{join as xs}from"node:path";function C(t){return t.preset==="read_only"?"read-only":t.preset==="full_access"?"danger-full-access":"workspace-write"}function Ge(t){let e=["-a","never","-s",C(t.frame.executionPolicy),"exec"];return t.sessionId?[...e,"resume","--json","--skip-git-repo-check",...sn(t.model),"--output-last-message",t.outputFile,t.sessionId,t.prompt]:[...e,"--json","--skip-git-repo-check",...sn(t.model),"--output-last-message",t.outputFile,t.prompt]}function de(t){return t.CODEX_MODEL?.trim()||null}function sn(t){return t?["--model",t]:[]}var We="Codex \u8FD4\u56DE\u9519\u8BEF\uFF0C\u672A\u63D0\u4F9B\u8BE6\u7EC6\u539F\u56E0",ls=/The ['‘"]?([^'’"]+)['’"]? model requires a newer version of Codex\. Please upgrade to the latest app or CLI and try again\./i;function me(t){if(!t||typeof t!="object")return null;let e=t;for(let n of["type","event","event_name","method"])if(typeof e[n]=="string")return e[n];return null}function Ve(t){if(!t||typeof t!="object")return null;let e=t;for(let n of["session_id","sessionId","thread_id","threadId","conversation_id","conversationId"])if(typeof e[n]=="string"&&e[n])return e[n];for(let n of Object.values(e)){let r=Ve(n);if(r)return r}return null}function B(t){if(!t)return null;let e=ds(t),r=(e?K(e):t)?.trim();return!r||pe(r)?null:cs(r)}function ge(t){if(!t||typeof t!="object")return null;let e=me(t);return e!=="error"&&e!=="turn.failed"?null:We}function D(t){if(!t||typeof t!="object")return null;let e=t,n=me(t);if(n!=="error"&&n!=="turn.failed")return null;for(let s of["message","detail","reason"])if(typeof e[s]=="string"&&e[s]&&!pe(e[s]))return e[s];let r=K(e.error);if(r)return r;for(let[s,o]of Object.entries(e)){if(["type","event","event_name","method","status","error"].includes(s))continue;let i=K(o);if(i)return i}return null}function He(t,e){return!t||t===We&&e!==We?e:t}function K(t){if(typeof t=="string"&&t&&!pe(t))return t;if(!t||typeof t!="object")return null;let e=t;for(let r of["message","detail","reason"])if(typeof e[r]=="string"&&e[r]&&!pe(e[r]))return e[r];let n=K(e.error);if(n)return n;for(let r of Object.values(e)){let s=K(r);if(s)return s}return null}function pe(t){let e=t.trim().toLowerCase();return e==="error"||e==="turn.failed"}function cs(t){let e=t.match(ls);return e?`Codex CLI \u7248\u672C\u8FC7\u4F4E\uFF0C\u6682\u4E0D\u652F\u6301\u6A21\u578B ${e[1]??"\u5F53\u524D\u6A21\u578B"}\u3002\u8BF7\u5347\u7EA7 Codex CLI \u540E\u91CD\u542F daemon\uFF0C\u6216\u5C06 CODEX_MODEL \u8C03\u6574\u4E3A\u5F53\u524D CLI \u652F\u6301\u7684\u6A21\u578B\u540E\u91CD\u8BD5\u3002`:t}function ds(t){try{return JSON.parse(t)}catch{return null}}import{spawn as ps}from"node:child_process";var fe=class{states=new Map;async runTurn(e){return new Promise((n,r)=>{let s={frame:e.frame,context:e.context,prompt:e.prompt,resolve:n,reject:r,output:"",streamedAgentMessageIds:new Set,streamedReasoningIds:new Set,injected:!1,steerSent:!1,abort:()=>{}};if(s.abort=()=>{this.removeTurn(e.frame,s),r(new Error("runtime_cancelled"))},e.context.abortSignal?.aborted){s.abort();return}e.context.abortSignal?.addEventListener("abort",s.abort,{once:!0});try{let o=this.ensureState(e);if(o.current||o.queue.length>0){s.injected=!0,o.injected.push(s),s.context.onLog("system","codex steering active turn"),this.flushInjectedSteers(o);return}o.queue.push(s),this.pump(o)}catch(o){r(o instanceof Error?o:new Error(String(o)))}})}dispose(e){let n=g(e),r=this.states.get(n);r&&(r.closed=!0,this.clearIdleTimer(r),r.child.kill("SIGTERM"),this.states.delete(n))}notify(e,n){let r=this.states.get(g(e));return!r||r.closed||!r.current&&r.queue.length===0?!1:(r.pendingNotifications.push(n),this.flushPendingNotifications(r),!0)}ensureState(e){let n=g(e.frame),r=e.context.cwd||process.cwd(),s=this.states.get(n);if(s&&!s.closed&&s.command===e.command&&s.cwd===r)return this.clearIdleTimer(s),s;s&&this.dispose(e.frame);let o=ps(e.command,["app-server","--listen","stdio://"],{cwd:r,env:e.context.env,stdio:["pipe","pipe","pipe"]}),i={agentId:e.frame.agentId,scopeKey:n,command:e.command,cwd:r,env:e.context.env,child:o,stdoutBuffer:"",stderrBuffer:"",requestId:0,initializeRequestId:null,pendingThreadRequest:this.buildThreadRequest(e.frame,e.sessionId,e.model,r),threadId:null,activeTurnId:null,sessionAnnounced:!1,current:null,injected:[],pendingNotifications:[],queue:[],idleTimer:null,closed:!1};return this.states.set(n,i),this.bindProcess(n,i),i.initializeRequestId=this.sendRequest(i,"initialize",{clientInfo:{name:"kslock-daemon",version:"1.0.0"},capabilities:{experimentalApi:!0}}),i}buildThreadRequest(e,n,r,s){let o={cwd:s,approvalPolicy:"never",sandbox:C(e.executionPolicy),developerInstructions:[`You are @${e.agentHandle} in kslock channel ${e.channelId}.`,"Reply with the final answer for the delivered message."].join(`
29
+ `)};return r&&(o.model=r),n?{method:"thread/resume",params:{threadId:n,...o}}:{method:"thread/start",params:o}}bindProcess(e,n){n.child.stdout.on("data",r=>{n.stdoutBuffer=on(n.stdoutBuffer+r.toString(),s=>{for(let o of this.parseLine(n,s))this.handleEvent(n,o)})}),n.child.stderr.on("data",r=>{n.stderrBuffer=on(n.stderrBuffer+r.toString(),s=>{n.current?.context.onLog("stderr",s)})}),n.child.on("error",r=>{this.rejectState(e,n,r instanceof Error?r:new Error(String(r)))}),n.child.on("close",r=>{n.closed=!0,this.states.delete(e);let s=new Error(`codex app-server exited with code ${r??"unknown"}`);(n.current||n.queue.length>0)&&this.rejectState(e,n,s)})}parseLine(e,n){let r=ae(n);if(!r||typeof r!="object")return[];let s=r,o=[];if(s.result&&typeof s.result=="object"){if(s.id===e.initializeRequestId)return e.initializeRequestId=null,this.sendNotification(e,"initialized",{}),e.pendingThreadRequest&&(this.sendRequest(e,e.pendingThreadRequest.method,e.pendingThreadRequest.params),e.pendingThreadRequest=null),o;let i=s.result,a=i.thread;if(a&&typeof a=="object"&&typeof a.id=="string")return this.handleThreadReady(e,String(a.id),o),o;let u=i.turn;if(u&&typeof u=="object"&&typeof u.id=="string")return e.activeTurnId=String(u.id),this.flushInjectedSteers(e),this.flushPendingNotifications(e),o;if(typeof i.turnId=="string")return e.activeTurnId=i.turnId,this.flushInjectedSteers(e),this.flushPendingNotifications(e),o}if(s.error){let i=B(D(s.error))??D(s.error);return o.push({kind:"error",message:i||"Codex app-server request failed"}),o}switch(s.method){case"thread/started":{let i=s.params,a=i&&typeof i=="object"?i.thread:null,u=a&&typeof a=="object"?a.id:null;typeof u=="string"&&this.handleThreadReady(e,u,o);break}case"turn/started":{let i=s.params,a=i&&typeof i=="object"?i.turn:null,u=a&&typeof a=="object"?a.id:null;typeof u=="string"&&(e.activeTurnId=u,this.flushInjectedSteers(e),this.flushPendingNotifications(e)),o.push({kind:"thinking",text:""});break}case"item/agentMessage/delta":{let i=s.params,a=i?.delta,u=i?.itemId;typeof u=="string"&&e.current?.streamedAgentMessageIds.add(u),typeof a=="string"&&a.length>0&&o.push({kind:"text",text:a});break}case"item/reasoning/summaryTextDelta":case"item/reasoning/textDelta":{let i=s.params,a=i?.delta,u=i?.itemId;typeof u=="string"&&e.current?.streamedReasoningIds.add(u),typeof a=="string"&&a.length>0&&o.push({kind:"thinking",text:a});break}case"item/started":case"item/completed":o.push(...this.parseItemEvent(e,s));break;case"turn/completed":{let a=s.params?.turn,u=a?.error;if(a?.status==="failed"&&u&&typeof u=="object"){let l=u.message;o.push({kind:"error",message:typeof l=="string"?l:"Codex turn failed"})}e.activeTurnId=null,o.push({kind:"turn_end",sessionId:e.threadId});break}case"error":{let i=B(D(s.params))??ge(s);o.push({kind:"error",message:i||"Unknown Codex app-server error"});break}}return o}parseItemEvent(e,n){let r=[],o=n.params?.item;if(!o||typeof o.type!="string")return r;let i=n.method==="item/started",a=n.method==="item/completed";switch(o.type){case"reasoning":if(a&&typeof o.id=="string"&&!e.current?.streamedReasoningIds.has(o.id)){let u=ms(o);u&&r.push({kind:"thinking",text:u})}break;case"agentMessage":a&&typeof o.id=="string"&&!e.current?.streamedAgentMessageIds.has(o.id)&&typeof o.text=="string"&&o.text&&r.push({kind:"text",text:o.text});break;case"commandExecution":i&&typeof o.command=="string"&&r.push({kind:"tool_call",name:"shell",input:{command:o.command}}),a&&r.push({kind:"tool_output",name:"shell"});break;case"contextCompaction":i&&r.push({kind:"compaction_started"}),a&&r.push({kind:"compaction_finished"});break;case"mcpToolCall":i&&r.push({kind:"tool_call",name:typeof o.tool=="string"?o.tool:"mcp_tool",input:o.arguments}),a&&r.push({kind:"tool_output",name:typeof o.tool=="string"?o.tool:"mcp_tool"});break}return r}handleEvent(e,n){let r=e.current;if(!(!r&&n.kind!=="session"))switch(n.kind){case"session":e.threadId=n.sessionId,r?.context.onLog("system",`codex session ${n.sessionId}`);break;case"thinking":n.text&&r?.context.onLog("system",`[codex:thinking] ${n.text}`);break;case"text":this.appendText(e,n.text);break;case"tool_call":r?.context.onLog("system",`[codex:tool] ${n.name}`);break;case"tool_output":r?.context.onLog("system",`[codex:tool_result] ${n.name}`);break;case"compaction_started":r?.context.onLog("system","codex compacting context");break;case"compaction_finished":r?.context.onLog("system","codex context compaction finished");break;case"error":this.finishCurrent(e,new Error(n.message));break;case"turn_end":n.sessionId&&(e.threadId=n.sessionId),this.finishCurrent(e);break}}handleThreadReady(e,n,r){e.threadId=n,e.sessionAnnounced||(r.push({kind:"session",sessionId:n}),e.sessionAnnounced=!0),this.pump(e)}pump(e){if(e.current||e.closed||!e.threadId)return;let n=e.queue.shift();if(!n){this.scheduleIdleDispose(e);return}this.clearIdleTimer(e),e.current=n,n.context.onLog("system",e.activeTurnId?"codex steering active turn":"codex starting turn"),this.sendRequest(e,"turn/start",{threadId:e.threadId,input:[{type:"text",text:n.prompt}]})}finishCurrent(e,n){let r=e.current;if(!r)return;let s=e.injected.splice(0);if(e.current=null,n){this.rejectTurn(r,n);for(let o of s)this.rejectTurn(o,n);for(let o of e.queue.splice(0))this.rejectTurn(o,n);e.closed=!0,this.clearIdleTimer(e),e.child.kill("SIGTERM"),this.states.delete(g(r.frame))}else{this.resolveTurn(r,e.threadId);for(let o of s)this.resolveTurn(o,e.threadId);this.pump(e)}}appendText(e,n){let r=e.current;r&&(r.output+=n,r.context.onPartial?.(n,r.output))}flushInjectedSteers(e){if(!(!e.threadId||!e.activeTurnId))for(let n of e.injected)n.steerSent||(n.steerSent=!0,this.sendRequest(e,"turn/steer",{threadId:e.threadId,expectedTurnId:e.activeTurnId,input:[{type:"text",text:n.prompt}]}))}flushPendingNotifications(e){if(!(!e.threadId||!e.activeTurnId||e.pendingNotifications.length===0))for(let n of e.pendingNotifications.splice(0))this.sendRequest(e,"turn/steer",{threadId:e.threadId,expectedTurnId:e.activeTurnId,input:[{type:"text",text:n}]})}resolveTurn(e,n){this.cleanupTurn(e);let r=e.output.trim();e.resolve({content:r,sessionId:n,suppressMessage:e.injected&&!r})}rejectTurn(e,n){this.cleanupTurn(e),e.reject(n)}cleanupTurn(e){e.context.abortSignal?.removeEventListener("abort",e.abort),e.streamedAgentMessageIds.clear(),e.streamedReasoningIds.clear()}removeTurn(e,n){let r=g(e),s=this.states.get(r);s&&(s.queue=s.queue.filter(o=>o!==n),s.injected=s.injected.filter(o=>o!==n),s.current===n&&(s.current=null,this.clearIdleTimer(s),s.child.kill("SIGTERM"),this.states.delete(r)))}rejectState(e,n,r){let s=n.current;n.current=null,this.clearIdleTimer(n),s&&this.rejectTurn(s,r);for(let o of n.injected.splice(0))this.rejectTurn(o,r);for(let o of n.queue.splice(0))this.rejectTurn(o,r);this.states.delete(e)}scheduleIdleDispose(e){if(e.closed||e.current||e.queue.length>0||e.idleTimer)return;let n=gs();n<=0||(e.idleTimer=setTimeout(()=>{e.closed||e.current||e.queue.length>0||(e.closed=!0,e.child.kill("SIGTERM"),this.states.delete(e.scopeKey))},n),e.idleTimer.unref?.())}clearIdleTimer(e){e.idleTimer&&(clearTimeout(e.idleTimer),e.idleTimer=null)}sendRequest(e,n,r){return e.requestId+=1,e.child.stdin.write(JSON.stringify({jsonrpc:"2.0",id:e.requestId,method:n,params:r})+`
27
30
  `),e.requestId}sendNotification(e,n,r){e.child.stdin.write(JSON.stringify({jsonrpc:"2.0",method:n,params:r})+`
28
- `)}};function Yt(t,e){let n=t.split(/\r?\n/),r=n.pop()??"";for(let s of n)s.trim()&&e(s);return r}function is(t){let e=Array.isArray(t.summary)?t.summary.filter(r=>typeof r=="string"):[],n=Array.isArray(t.content)?t.content.filter(r=>typeof r=="string"):[];return[...e,...n].join(`
29
- `).trim()}function as(){let t=process.env.KSLOCK_RUNTIME_IDLE_TTL_MS;if(!t)return 1800*1e3;let e=Number(t);return!Number.isFinite(e)||e<0?1800*1e3:e}import{spawn as us}from"node:child_process";var cs="Reading additional input from stdin...";function tn(t){return new Promise((e,n)=>{let r=[],s="",o="",i="",a=us(t.command,t.args,{cwd:t.cwd||process.cwd(),env:t.env,stdio:["ignore","pipe","pipe"]});if(t.signal?.aborted){a.kill("SIGTERM"),n(new Error("runtime_cancelled"));return}t.signal?.addEventListener("abort",()=>{a.kill("SIGTERM"),n(new Error("runtime_cancelled"))},{once:!0}),a.stdout.on("data",u=>{o=Zt(o+u.toString(),c=>{let l=en(c);if(l){let d=t.onJson(l);d&&(s=We(s,d));return}t.onLog("stdout",c)})}),a.stderr.on("data",u=>{i=Zt(i+u.toString(),c=>{Ve(c)||(r.push(c),t.onLog("stderr",c))})}),a.on("error",n),a.on("exit",u=>{if(o.trim()){let c=en(o.trim());if(c){let l=t.onJson(c);l&&(s=We(s,l))}else t.onLog("stdout",o.trim())}if(i.trim()&&!Ve(i.trim())&&(r.push(i.trim()),t.onLog("stderr",i.trim())),t.onLog("system",`${t.command} exited with code ${u??"unknown"}`),u===0){e();return}n(new Error(s||ls(r)||`${t.command} exited with code ${u}`))})})}function Zt(t,e){let n=t.split(/\r?\n/),r=n.pop()??"";for(let s of n)s.trim()&&e(s);return r}function en(t){try{return JSON.parse(t)}catch{return null}}function Ve(t){return t.trim()===cs}function ls(t){return t.map(e=>e.trim()).filter(e=>e&&!Ve(e)).at(-1)??""}import{readFile as ds}from"node:fs/promises";async function nn(t){try{return await ds(t,"utf8")}catch{return""}}function rn(t){if(!t||typeof t!="object")return null;let e=t,n=ms(t)??"";for(let s of["delta","text_delta","content_delta"])if(typeof e[s]=="string"&&e[s])return e[s];if(n.includes("delta")||n.includes("stream")){for(let s of["text","content","message"])if(typeof e[s]=="string"&&e[s])return e[s]}return sn(e.delta??e.message??e.item)}function sn(t){if(typeof t=="string"&&t)return t;if(!t||typeof t!="object")return null;let e=t;for(let n of["delta","text_delta","content_delta","text"])if(typeof e[n]=="string"&&e[n])return e[n];for(let n of Object.values(e)){let r=sn(n);if(r)return r}return null}function ms(t){if(!t||typeof t!="object")return null;let e=t;for(let n of["type","event","event_name","method"])if(typeof e[n]=="string")return e[n];return null}var N=class{type="codex";appServers=new pe;busyDeliveryMode="direct";capabilities={supportsSession:!0,supportsStreaming:!0,supportsExecutionPolicy:!0};async detect(){return!!await this.resolveCommand()}notifyInbox(e,n){return this.appServers.notify(e,n)}async listModels(){let e=await this.resolveCommand();return e?Mt(e):[]}async handleDelivery(e,n){let r=await this.resolveCommand();if(!r)throw new Error("codex runtime is not available");let s=await I(e),o=s?.runtimeType===this.type?s.runtimeSessionId:null,i=o?`resuming ${o}`:"starting new session";n.onLog("system",`codex ${i} in ${n.cwd??process.cwd()}`);let a;try{a=await this.runCodexAppServer(r,e,n,o)}catch(c){if(on(c))n.onLog("stderr",`codex app-server failed; falling back to codex exec. ${c instanceof Error?c.message:""}`),a=await this.runCodex(r,e,n,o);else if(o)n.onLog("stderr",`codex resume ${o} failed; starting a new session. ${c instanceof Error?c.message:""}`),a=await this.runCodexAppServer(r,e,n,null).catch(l=>{if(!on(l))throw l;return n.onLog("stderr",`codex app-server retry failed; falling back to codex exec. ${l instanceof Error?l.message:""}`),this.runCodex(r,e,n,null)});else throw c}let u=a.sessionId??o??null;return await x({agentId:e.agentId,channelId:e.channelId,runtimeScopeKey:g(e),runtimeType:this.type,runtimeSessionId:u,workspacePath:n.cwd??null,updatedAt:new Date().toISOString()}),u?n.onLog("system",`codex session ${u}`):n.onLog("system","codex completed without exposing a resumable session id"),a.suppressMessage?{content:a.output.trim(),sessionId:u,suppressMessage:!0}:{content:a.output.trim()||"codex runtime completed without output",sessionId:u}}async runCodexAppServer(e,n,r,s){let o=await v(n,{workspacePath:r.cwd}),i=w(n,ce(r.env));r.onLog("system",i?`codex app-server using model override ${i}`:"codex app-server using configured model"),r.onLog("system",`codex app-server sandbox ${k(n.executionPolicy)}`);let a=await this.appServers.runTurn({command:e,frame:n,context:r,prompt:o,sessionId:s,model:i});return{output:a.content,sessionId:a.sessionId??null,suppressMessage:a.suppressMessage}}async runCodex(e,n,r,s){let o=h(n);await ps(o,{recursive:!0});let i=fs(o,"last-message.txt");await gs(i,"","utf8");let a=await v(n,{workspacePath:r.cwd}),u=w(n,ce(r.env));r.onLog("system",u?`codex using model override ${u}`:"codex using configured model"),r.onLog("system",`codex sandbox ${k(n.executionPolicy)}`);let c=new Set;return await tn({command:e,args:qe({frame:n,prompt:a,outputFile:i,model:u,sessionId:s}),cwd:r.cwd,env:r.env,signal:r.abortSignal,onJson:l=>{let d=rn(l);d&&r.onPartial?.(d);let p=Ge(l);p&&c.add(p);let m=de(l);m&&r.onLog("system",`[codex:event] ${m}`);let f=$(T(l))??me(l);return f?(r.onLog("stderr",`[codex:error] ${f}`),f):null},onLog:r.onLog}),{output:await nn(i),sessionId:[...c].at(-1)??null}}async resolveCommand(){return S({command:"codex",envVars:["CODEX_BIN","CODEX_CLI_PATH"]})}};function on(t){if(process.env.KSLOCK_CODEX_DISABLE_EXEC_FALLBACK==="1")return!1;let e=t instanceof Error?t.message:String(t);return/app-server exited|unrecognized|unknown command|invalid subcommand|not supported/i.test(e)}import{randomUUID as wn}from"node:crypto";function He(t){return t.preset==="read_only"?["--permission-mode","dontAsk","--tools","Read,Grep,Glob,LS"]:t.preset==="full_access"?["--dangerously-skip-permissions"]:["--permission-mode","acceptEdits","--allowedTools",["Read","Grep","Glob","LS","Edit","MultiEdit","Write","Bash(git status *)","Bash(git diff *)","Bash(git log *)","Bash(git show *)","Bash(pnpm test *)","Bash(pnpm run test *)","Bash(pnpm typecheck *)","Bash(pnpm run typecheck *)","Bash(pnpm lint *)","Bash(pnpm run lint *)","Bash(pnpm build *)","Bash(pnpm run build *)","Bash(npm test *)","Bash(npm run test *)","Bash(npm run typecheck *)","Bash(npm run lint *)","Bash(npm run build *)","Bash(yarn test *)","Bash(yarn typecheck *)","Bash(yarn lint *)","Bash(yarn build *)","Bash(tsc *)","Bash(vitest *)"].join(","),"--disallowedTools",["Bash(rm *)","Bash(sudo *)","Bash(chmod *)","Bash(chown *)","Bash(curl *)","Bash(wget *)"].join(",")]}function Qe(t){return["-p","--output-format","stream-json","--verbose","--include-partial-messages",...t.resume?["--resume",t.sessionId]:["--session-id",t.sessionId],...an(t.model),...He(t.frame.executionPolicy)]}function Xe(t){return["--output-format","stream-json","--input-format","stream-json","--verbose","--include-partial-messages",...t.resume?["--resume",t.sessionId]:["--session-id",t.sessionId],...an(t.model),...He(t.frame.executionPolicy)]}function ge(t){return t.KSCC_MODEL?.trim()||t.ANTHROPIC_MODEL?.trim()||null}function an(t){return t?["--model",t]:[]}var fe="kscc \u8FD4\u56DE\u9519\u8BEF\uFF0C\u672A\u63D0\u4F9B\u8BE6\u7EC6\u539F\u56E0";function Ze(t){if(!t||typeof t!="object")return null;let e=t;for(let n of["type","subtype","event","event_name"])if(typeof e[n]=="string")return e[n];return null}function ye(t){if(!t||typeof t!="object")return null;let e=t;for(let n of["session_id","sessionId"])if(typeof e[n]=="string"&&ys(e[n]))return e[n];for(let n of Object.values(e)){let r=ye(n);if(r)return r}return null}function B(t){if(!t||typeof t!="object")return null;let e=t;if(typeof e.result=="string"&&e.result.trim())return e.result;if(typeof e.content=="string"&&e.content.trim())return e.content;let n=e.message;if(n&&typeof n=="object"){let r=un(n.content);if(r)return r}return un(e.content)}function J(t){if(!t)return null;let e=hs(t),r=(e?K(e):t)?.trim();return!r||Ye(r)?null:r}function D(t){if(!t||typeof t!="object")return null;let e=t,n=typeof e.type=="string"?e.type:"",r=typeof e.subtype=="string"?e.subtype:"";return n==="error"||r==="error"||r==="failure"||e.is_error===!0||n==="result"&&r&&r!=="success"?K(t)??fe:null}function z(t,e){return!t||t===fe&&e!==fe?e:t}function un(t){if(typeof t=="string"&&t.trim())return t;if(!Array.isArray(t))return null;let e=t.map(n=>{if(!n||typeof n!="object")return"";let r=n;return typeof r.text=="string"?r.text:""}).filter(Boolean).join("");return e.trim()?e:null}function K(t){if(typeof t=="string"&&t&&!Ye(t))return t;if(!t||typeof t!="object")return null;let e=t;for(let r of["message","detail","reason","error"]){let s=e[r];if(typeof s=="string"&&s&&!Ye(s))return s}let n=K(e.error);if(n)return n;for(let r of Object.values(e)){let s=K(r);if(s)return s}return null}function ys(t){return/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(t)}function Ye(t){let e=t.trim().toLowerCase();return e==="error"||e==="failure"}function hs(t){try{return JSON.parse(t)}catch{return null}}import{spawn as vs}from"node:child_process";function gn(t){return new Promise((e,n)=>{let r=[],s=[],o="",i=[],a="",u="",c="",l=vs(t.command,t.args,{cwd:t.cwd||process.cwd(),env:t.env,stdio:["pipe","pipe","pipe"]});if(l.stdin.on("error",()=>{}),t.signal?.aborted){l.kill("SIGTERM"),n(new Error("runtime_cancelled"));return}t.signal?.addEventListener("abort",()=>{l.kill("SIGTERM"),n(new Error("runtime_cancelled"))},{once:!0}),l.stdin.end(t.stdin??""),l.stdout.on("data",d=>{u=cn(u+d.toString(),p=>{let m=ln(p);if(m){let f=t.onJson(m)??D(m);f&&(a=z(a,f));let y=B(m);y&&(dn(m)?s.push(y):o=mn(y,o,t.onPartial));return}r.push(p),t.onLog("stdout",p)})}),l.stderr.on("data",d=>{c=cn(c+d.toString(),p=>{i.push(p),t.onLog("stderr",p)})}),l.on("error",n),l.on("exit",d=>{if(u.trim()){let m=ln(u.trim());if(m){let f=t.onJson(m)??D(m);f&&(a=z(a,f));let y=B(m);y&&(dn(m)?s.push(y):o=mn(y,o,t.onPartial))}else r.push(u.trim()),t.onLog("stdout",u.trim())}if(c.trim()&&(i.push(c.trim()),t.onLog("stderr",c.trim())),t.onLog("system",`${t.command} exited with code ${d??"unknown"}`),d===0){e({output:ws(s,o,r)});return}let p=J(pn(i))??pn(i);n(new Error(a||p||`${t.command} exited with code ${d}`))})})}function cn(t,e){let n=t.split(/\r?\n/),r=n.pop()??"";for(let s of n)s.trim()&&e(s);return r}function ln(t){try{return JSON.parse(t)}catch{return null}}function dn(t){return!!(t&&typeof t=="object"&&t.type==="result")}function mn(t,e,n){let r=t.startsWith(e)?t:`${e}${t}`,s=r.slice(e.length);return s&&n?.(s,r),r}function ws(t,e,n){let r=t.at(-1)?.trim();if(r)return r;let s=e.trim();return s||n.join(`
30
- `).trim()}function pn(t){return t.map(e=>e.trim()).filter(Boolean).at(-1)??""}import{mkdir as Is,readFile as xs,writeFile as fn}from"node:fs/promises";import{join as Ss}from"node:path";var Rs="kscc-last-message.txt";function bs(t){return Ss(h(t),Rs)}async function yn(t){await Is(h(t),{recursive:!0});let e=bs(t);return await fn(e,"","utf8"),e}async function hn(t,e){await fn(t,e,"utf8")}async function vn(t){try{return await xs(t,"utf8")}catch{return""}}var q=class{type="kscc";busyDeliveryMode="gated";sessions=new A({type:"kscc",displayName:"kscc",buildArgs:Xe,resolveModel:ge,extractSessionId:ye,extractText:B,extractEventName:Ze,summarizeError:D,normalizeError:J,preferFailure:z});capabilities={supportsSession:!0,supportsStreaming:!0,supportsExecutionPolicy:!0};async detect(){return!!await this.resolveCommand()}notifyInbox(e,n){return this.sessions.notify(e,n)}async listModels(){let e=await this.resolveCommand();return e?se(e):[]}async handleDelivery(e,n){let r=await this.resolveCommand();if(!r)throw new Error("kscc runtime is not available");let s=await I(e),o=s?.runtimeType===this.type?s.runtimeSessionId:null,i=o??wn(),a=o?`resuming ${o}`:`starting new session ${i}`;n.onLog("system",`kscc ${a} in ${n.cwd??process.cwd()}`);let u;try{u=await this.runKsccStream(r,e,n,i,!!o)}catch(c){if(In(c))n.onLog("stderr",`kscc stream-json session failed; falling back to print mode. ${c instanceof Error?c.message:""}`),u=await this.runKscc(r,e,n,i,!!o);else if(o){let l=wn();n.onLog("stderr",`kscc resume ${o} failed; starting a new session ${l}. ${c instanceof Error?c.message:""}`),u=await this.runKsccStream(r,e,n,l,!1).catch(d=>{if(!In(d))throw d;return n.onLog("stderr",`kscc stream retry failed; falling back to print mode. ${d instanceof Error?d.message:""}`),this.runKscc(r,e,n,l,!1)})}else throw c}return await x({agentId:e.agentId,channelId:e.channelId,runtimeScopeKey:g(e),runtimeType:this.type,runtimeSessionId:u.sessionId,workspacePath:n.cwd??null,updatedAt:new Date().toISOString()}),n.onLog("system",`kscc session ${u.sessionId}`),u.suppressMessage?{content:u.output.trim(),sessionId:u.sessionId,suppressMessage:!0}:{content:u.output.trim()||"kscc runtime completed without output",sessionId:u.sessionId}}async runKsccStream(e,n,r,s,o){r.onLog("system",o?`kscc stream resume ${s}`:`kscc stream session-id ${s}`);let i=await this.sessions.runTurn({command:e,frame:n,context:r,prompt:await v(n,{workspacePath:r.cwd}),sessionId:s,resume:o});return{output:i.content,sessionId:i.sessionId??s,suppressMessage:i.suppressMessage}}async runKscc(e,n,r,s,o){let i=await v(n,{workspacePath:r.cwd}),a=w(n,ge(r.env)),u=await yn(n);r.onLog("system",a?`kscc using model override ${a}`:"kscc using configured model"),r.onLog("system",o?`kscc resume ${s}`:`kscc session-id ${s}`);let c=new Set,l=await gn({command:e,args:Qe({frame:n,sessionId:s,resume:o,model:a}),stdin:i,cwd:r.cwd,env:r.env,signal:r.abortSignal,onPartial:r.onPartial,onJson:d=>{let p=ye(d);p&&c.add(p);let m=Ze(d);m&&r.onLog("system",`[kscc:event] ${m}`);let f=J(D(d));return f?(r.onLog("stderr",`[kscc:error] ${f}`),f):null},onLog:r.onLog});return await hn(u,l.output),{output:l.output||await vn(u),sessionId:[...c].at(-1)??s}}async resolveCommand(){return S({command:"kscc",envVars:["KSCC_BIN"]})}};function In(t){if(process.env.KSLOCK_KSCC_DISABLE_PRINT_FALLBACK==="1")return!1;let e=t instanceof Error?t.message:String(t);return/process exited|unknown option|unrecognized|input-format|stream-json|stdin|not supported/i.test(e)}function xn(t){return t.preset==="full_access"?"allow":t.preset==="read_only"?{"*":"deny",read:{"*":"allow","*.env":"deny","*.env.*":"deny","*.env.example":"allow"},glob:"allow",grep:"allow",list:"allow",lsp:"allow",todoread:"allow",edit:"deny",bash:"deny",task:"deny",skill:"deny",todowrite:"deny",webfetch:"deny",websearch:"deny",codesearch:"deny",external_directory:"deny",doom_loop:"deny"}:{"*":"deny",read:{"*":"allow","*.env":"deny","*.env.*":"deny","*.env.example":"allow"},glob:"allow",grep:"allow",list:"allow",lsp:"allow",todoread:"allow",todowrite:"allow",edit:"allow",skill:"allow",bash:{"*":"deny","git status *":"allow","git diff *":"allow","git log *":"allow","git show *":"allow","pnpm test *":"allow","pnpm run test *":"allow","pnpm typecheck *":"allow","pnpm run typecheck *":"allow","pnpm lint *":"allow","pnpm run lint *":"allow","pnpm build *":"allow","pnpm run build *":"allow","npm test *":"allow","npm run test *":"allow","npm run typecheck *":"allow","npm run lint *":"allow","npm run build *":"allow","yarn test *":"allow","yarn typecheck *":"allow","yarn lint *":"allow","yarn build *":"allow","tsc *":"allow","vitest *":"allow","rm *":"deny","sudo *":"deny","chmod *":"deny","chown *":"deny","curl *":"deny","wget *":"deny"},task:"deny",webfetch:"deny",websearch:"deny",codesearch:"deny",external_directory:"deny",doom_loop:"deny"}}var et="1.14.30";function tt(t){return["run","--format","json","--pure",...Cs(t.workspacePath),...ks(t.sessionId),...As(t.model),...Es(t.frame.executionPolicy),"--",t.message]}function nt(t,e){return{...t,OPENCODE_PERMISSION:JSON.stringify(xn(e))}}function rt(t){return t.OPENCODE_MODEL?.trim()||null}function he(t){return!t||Rn(t)?null:`OpenCode CLI ${t} \u6682\u4E0D\u652F\u6301\uFF0C\u5F53\u524D\u8FD0\u884C\u65F6\u8981\u6C42 opencode >= ${et}\u3002\u8BF7\u5347\u7EA7 opencode \u540E\u91CD\u542F daemon\u3002`}function Rn(t){if(!t)return!0;let e=Sn(t),n=Sn(et);if(!e||!n)return!0;for(let r=0;r<3;r+=1){if(e[r]>n[r])return!0;if(e[r]<n[r])return!1}return!0}function Sn(t){let e=t.match(/(\d+)\.(\d+)\.(\d+)/);return e?[Number(e[1]),Number(e[2]),Number(e[3])]:null}function ks(t){return t?["--session",t]:[]}function Cs(t){return t?["--dir",t]:[]}function As(t){return t?["--model",t]:[]}function Es(t){return t.preset==="full_access"?["--dangerously-skip-permissions"]:[]}var ve="opencode \u8FD4\u56DE\u9519\u8BEF\uFF0C\u672A\u63D0\u4F9B\u8BE6\u7EC6\u539F\u56E0";function kn(t){if(!t||typeof t!="object")return null;let e=t;for(let n of["type","subtype","event","event_name","eventName"])if(typeof e[n]=="string")return e[n];return null}function ot(t){if(!t||typeof t!="object")return null;let e=t;for(let r of["sessionID","sessionId","session_id"])if(typeof e[r]=="string"&&e[r].trim())return e[r];let n=e.session;if(n&&typeof n=="object"){let r=n.id;if(typeof r=="string"&&r.trim())return r}for(let r of Object.values(e)){let s=ot(r);if(s)return s}return null}function it(t){if(!t||typeof t!="object")return null;let e=t;if(typeof e.result=="string"&&e.result.trim())return e.result;if(typeof e.output=="string"&&e.output.trim())return e.output;let n=bn(e.message);if(n)return n;let r=bn(e.part);if(r)return r;if(Ts(e)){if(typeof e.content=="string"&&e.content.trim())return e.content;if(typeof e.text=="string"&&e.text.trim())return e.text;let s=we(e.content);if(s)return s;let o=we(e.parts);if(o)return o}return null}function Ie(t){if(!t)return null;let e=Ds(t),r=(e?U(e):t)?.trim();return!r||st(r)?null:r}function G(t){if(!t||typeof t!="object")return null;let e=t,n=typeof e.type=="string"?e.type.toLowerCase():"",r=typeof e.subtype=="string"?e.subtype.toLowerCase():"";return n.includes("error")||r==="error"||r==="failure"||e.is_error===!0||n==="result"&&r&&r!=="success"?U(t)??ve:null}function at(t,e){return!t||t===ve&&e!==ve?e:t}function bn(t){if(!t||typeof t!="object")return null;let e=t,n=typeof e.role=="string"?e.role.toLowerCase():"",r=typeof e.type=="string"?e.type.toLowerCase():"";return n&&n!=="assistant"?null:typeof e.text=="string"&&e.text.trim()?e.text:typeof e.content=="string"&&e.content.trim()?e.content:r&&r!=="text"&&!r.includes("assistant")&&!r.includes("message")&&!r.includes("part")?null:we(e.content)??we(e.parts)}function we(t){if(typeof t=="string"&&t.trim())return t;if(!Array.isArray(t))return null;let e=t.map(n=>{if(!n||typeof n!="object")return"";let r=n;return typeof r.text=="string"?r.text:typeof r.content=="string"?r.content:""}).filter(Boolean).join("");return e.trim()?e:null}function Ts(t){let e=typeof t.role=="string"?t.role.toLowerCase():"";if(e&&e!=="assistant")return!1;let n=typeof t.type=="string"?t.type.toLowerCase():"";return n?n==="result"||n==="text"||n.includes("assistant")||n.includes("message")||n.includes("part"):e==="assistant"}function U(t){if(typeof t=="string"&&t&&!st(t))return t;if(!t||typeof t!="object")return null;let e=t;for(let r of["message","detail","reason","error"]){let s=e[r];if(typeof s=="string"&&s&&!st(s))return s}let n=U(e.error);if(n)return n;for(let r of Object.values(e)){let s=U(r);if(s)return s}return null}function st(t){let e=t.trim().toLowerCase();return e==="error"||e==="failure"}function Ds(t){try{return JSON.parse(t)}catch{return null}}import{spawn as Ms}from"node:child_process";function Mn(t){return new Promise((e,n)=>{let r=[],s=[],o="",i=[],a="",u="",c="",l=Ms(t.command,t.args,{cwd:t.cwd||process.cwd(),env:t.env,stdio:["ignore","pipe","pipe"]});if(t.signal?.aborted){l.kill("SIGTERM"),n(new Error("runtime_cancelled"));return}t.signal?.addEventListener("abort",()=>{l.kill("SIGTERM"),n(new Error("runtime_cancelled"))},{once:!0}),l.stdout.on("data",d=>{u=Cn(u+d.toString(),p=>{let m=An(p);if(m){let f=t.onJson(m)??G(m);f&&(a=at(a,f));let y=it(m);y&&(En(m)?s.push(y):o=Tn(y,o,t.onPartial));return}r.push(p),t.onLog("stdout",p)})}),l.stderr.on("data",d=>{c=Cn(c+d.toString(),p=>{i.push(p),t.onLog("stderr",p)})}),l.on("error",n),l.on("exit",d=>{if(u.trim()){let m=An(u.trim());if(m){let f=t.onJson(m)??G(m);f&&(a=at(a,f));let y=it(m);y&&(En(m)?s.push(y):o=Tn(y,o,t.onPartial))}else r.push(u.trim()),t.onLog("stdout",u.trim())}if(c.trim()&&(i.push(c.trim()),t.onLog("stderr",c.trim())),t.onLog("system",`${t.command} exited with code ${d??"unknown"}`),d===0){e({output:Fs(s,o,r)});return}let p=Ie(Dn(i))??Dn(i);n(new Error(a||p||`${t.command} exited with code ${d}`))})})}function Cn(t,e){let n=t.split(/\r?\n/),r=n.pop()??"";for(let s of n)s.trim()&&e(s);return r}function An(t){try{return JSON.parse(t)}catch{return null}}function En(t){if(!t||typeof t!="object")return!1;let e=t,n=typeof e.type=="string"?e.type.toLowerCase():"";return n==="result"||n.includes("result")||n.includes("complete")}function Tn(t,e,n){let r=t.startsWith(e)?t:`${e}${t}`,s=r.slice(e.length);return s&&n?.(s,r),r}function Fs(t,e,n){let r=t.at(-1)?.trim();if(r)return r;let s=e.trim();return s||n.join(`
31
- `).trim()}function Dn(t){return t.map(e=>e.trim()).filter(Boolean).at(-1)??""}import{mkdir as Ps,readFile as _s,writeFile as Fn}from"node:fs/promises";import{join as Ls}from"node:path";var js="opencode-last-message.txt";function Os(t){return Ls(h(t),js)}async function Pn(t){await Ps(h(t),{recursive:!0});let e=Os(t);return await Fn(e,"","utf8"),e}async function _n(t,e){await Fn(t,e,"utf8")}async function Ln(t){try{return await _s(t,"utf8")}catch{return""}}var W=class{type="opencode";busyDeliveryMode="queue";capabilities={supportsSession:!0,supportsStreaming:!0,supportsExecutionPolicy:!0};async detect(){let e=await this.resolveCommand();return e?!he(await Le(e)):!1}async listModels(){let e=await this.resolveCommand();return e?Ft(e):[]}async handleDelivery(e,n){let r=await this.resolveCommand();if(!r)throw new Error("opencode runtime is not available");let s=he(await Le(r));if(s)throw new Error(s);let o=await I(e),i=o?.runtimeType===this.type?o.runtimeSessionId:null,a=i?`resuming ${i}`:"starting new session";n.onLog("system",`opencode ${a} in ${n.cwd??process.cwd()}`);let u;try{u=await this.runOpenCode(r,e,n,i)}catch(c){if(!i)throw c;n.onLog("stderr",`opencode resume ${i} failed; starting a new session. ${c instanceof Error?c.message:""}`),u=await this.runOpenCode(r,e,n,null)}return await x({agentId:e.agentId,channelId:e.channelId,runtimeScopeKey:g(e),runtimeType:this.type,runtimeSessionId:u.sessionId,workspacePath:n.cwd??null,updatedAt:new Date().toISOString()}),u.sessionId&&n.onLog("system",`opencode session ${u.sessionId}`),{content:u.output.trim()||"opencode runtime completed without output",sessionId:u.sessionId}}async runOpenCode(e,n,r,s){let o=await v(n,{workspacePath:r.cwd}),i=w(n,rt(r.env)),a=await Pn(n),u=nt(r.env,n.executionPolicy);r.onLog("system",i?`opencode using model override ${i}`:"opencode using configured model"),r.onLog("system",s?`opencode session ${s}`:"opencode new session");let c=new Set,l=await Mn({command:e,args:tt({frame:n,message:o,sessionId:s,model:i,workspacePath:r.cwd}),cwd:r.cwd,env:u,signal:r.abortSignal,onPartial:r.onPartial,onJson:d=>{let p=ot(d);p&&c.add(p);let m=kn(d);m&&r.onLog("system",`[opencode:event] ${m}`);let f=Ie(G(d));return f?(r.onLog("stderr",`[opencode:error] ${f}`),f):null},onLog:r.onLog});return await _n(a,l.output),{output:l.output||await Ln(a),sessionId:[...c].at(-1)??s}}async resolveCommand(){return S({command:"opencode",envVars:["OPENCODE_BIN"]})}};var ut=class{runtimes=new Map;constructor(e){for(let n of e)this.runtimes.set(n.type,n)}get(e){let n=this.runtimes.get(e);if(!n)throw new Error(`runtime ${e} is not registered`);return n}detectable(){return[...this.runtimes.values()]}async detectCapabilities(){return Promise.all(this.detectable().map(async e=>{try{let n=e.detect?await e.detect():!0,r=n&&e.listModels?await e.listModels().catch(()=>[]):[];return{type:e.type,available:n,...e.capabilities,...r.length>0?{models:r}:{}}}catch{return{type:e.type,available:!1,...e.capabilities}}}))}};function jn(){return new ut([new N,new j,new q,new W])}var xe=class{runtimes=jn();publisher;runner;queue;constructor(e){this.publisher=new H(e),this.runner=new ne(this.runtimes,this.publisher),this.queue=new Q((n,r)=>{Z(n,n.deliveryId),this.publisher.failed(n,r),this.publisher.status(n,"error")})}async deliver(e){await this.queue.enqueue(e,(n,r)=>this.runner.run(n,r),{allowBusyDelivery:!0,onBusyDelivery:n=>this.handleBusyDelivery(n)})}cancelDelivery(e,n="cancelled_by_user"){this.queue.cancel(e,n)}handlePermissionResponse(e){}async detectAvailableRuntimes(){return(await this.detectRuntimeCapabilities()).filter(n=>n.available).map(n=>n.type)}async detectRuntimeCapabilities(){return(await this.runtimes.detectCapabilities()).map(n=>({...n,available:n.available}))}async handleBusyDelivery(e){let n=this.runtimes.get(e.runtimeType),r=await ft(e),s=M(e),o=xt({pendingCount:r.messages.length,checkMessagesCommand:s.KSLOCK_CHECK_MESSAGES_COMMAND,inboxFile:s.KSLOCK_INBOX_FILE});this.publisher.delivered(e),this.publisher.runtimeLog(e,"system",`queued in agent inbox (${r.messages.length} pending)`),await n.notifyInbox?.(e,o)||this.publisher.runtimeLog(e,"system","agent inbox notification deferred until the queued delivery starts")}};var Se=class{frames=[];enqueue(e){return $s(e)?(this.frames.push(e),this.frames.length>200&&this.frames.splice(0,this.frames.length-200),!0):!1}drain(){return this.frames.splice(0)}get size(){return this.frames.length}};function $s(t){return t.type==="agent:delivery_ack"||t.type==="agent:message"||t.type==="agent:status"||t.type==="task:status"}var Re=class{constructor(e){this.config=e}ws=null;reconnectAttempts=0;reconnectTimer=null;stopped=!1;outbox=new Se;agentManager=new xe(e=>this.send(e));connect(){this.stopped=!1;let e=this.buildWebSocketUrl();this.ws=new On(e),this.ws.on("open",()=>{this.handleOpen()}),this.ws.on("message",n=>{let r=Ns(n.toString());r&&this.handleFrame(r)}),this.ws.on("close",(n,r)=>{console.log(`[Daemon] Disconnected${Ks(n,r)}`),this.ws=null,this.scheduleReconnect()}),this.ws.on("error",n=>{console.error(`[Daemon] WebSocket error: ${n.message}`)})}close(){this.stopped=!0,this.reconnectTimer&&(clearTimeout(this.reconnectTimer),this.reconnectTimer=null),this.ws?.close()}async handleFrame(e){if(e.type==="ping"){this.send({type:"pong",requestId:e.requestId});return}if(e.type==="error"){console.error(`[Daemon] Server error ${e.code}: ${e.message}`),(e.code==="invalid_machine_key"||e.code==="machine_key_revoked")&&(this.stopped=!0);return}if(e.type==="agent:deliver"){await this.agentManager.deliver(e);return}if(e.type==="agent:cancel"){this.agentManager.cancelDelivery(e.deliveryId,e.reason);return}e.type==="permission:response"&&this.agentManager.handlePermissionResponse(e)}async handleOpen(){this.reconnectAttempts=0,console.log(`[Daemon] Connected to ${this.config.serverUrl}`);let e=await this.agentManager.detectRuntimeCapabilities();this.send({type:"machine:ready",daemonVersion:this.config.daemonVersion,hostname:this.config.hostname,os:this.config.os,arch:this.config.arch,availableRuntimes:e.filter(n=>n.available).map(n=>n.type),runtimeCapabilities:e}),this.flushOutbox()}send(e){this.sendNow(e)||this.stopped||this.outbox.enqueue(e)&&console.warn(`[Daemon] Queued ${e.type}; websocket is not open`)}sendNow(e){if(!this.ws||this.ws.readyState!==On.OPEN)return!1;try{return this.ws.send(JSON.stringify(e)),!0}catch{return!1}}flushOutbox(){if(this.outbox.size===0)return;let e=this.outbox.drain();console.log(`[Daemon] Flushing ${e.length} queued frame(s)`);for(let n of e)this.sendNow(n)||this.outbox.enqueue(n)}buildWebSocketUrl(){return`${this.config.serverUrl.replace(/^http/,"ws")}/daemon/connect?key=${encodeURIComponent(this.config.machineKey)}`}scheduleReconnect(){if(this.stopped||this.reconnectTimer)return;let e=Math.min(this.config.reconnectBaseMs*2**this.reconnectAttempts,3e4);this.reconnectAttempts+=1,console.log(`[Daemon] Reconnecting in ${e}ms`),this.reconnectTimer=setTimeout(()=>{this.reconnectTimer=null,this.stopped||this.connect()},e)}};function Ns(t){try{return JSON.parse(t)}catch{return null}}function Ks(t,e){let n=e.toString();return!t&&!n?"":` (code ${t}${n?`, reason: ${n}`:""})`}function Nn(t=process.argv.slice(2)){if(t[0]==="check-messages"||t[0]==="check_messages")return Bs(t.slice(1)),null;if(t.includes("--help")||t.includes("-h"))return console.log(Ce),null;if(t.includes("--version")||t.includes("-v"))return console.log(ke()),null;dt();let e=lt(t);if(!e)return console.error(Ce),process.exitCode=1,null;let n=new Re(e);n.connect();let r=()=>{n.close(),process.exit(0)};return process.once("SIGINT",r),process.once("SIGTERM",r),n}async function Bs(t){let e=$n(t,"--agent-id")??process.env.AGENT_ID,n=$n(t,"--channel-id")??process.env.CHANNEL_ID;if(!e){console.error("Usage: kslock-daemon check-messages --agent-id <agent-id> [--channel-id <channel-id>]"),process.exitCode=1;return}let r=await yt({agentId:e,channelId:n});if(t.includes("--json")){console.log(JSON.stringify(r,null,2));return}console.log(Me(r))}function $n(t,e){let n=t.indexOf(e);return n<0?null:t[n+1]??null}Nn();
31
+ `)}};function on(t,e){let n=t.split(/\r?\n/),r=n.pop()??"";for(let s of n)s.trim()&&e(s);return r}function ms(t){let e=Array.isArray(t.summary)?t.summary.filter(r=>typeof r=="string"):[],n=Array.isArray(t.content)?t.content.filter(r=>typeof r=="string"):[];return[...e,...n].join(`
32
+ `).trim()}function gs(){let t=process.env.KSLOCK_RUNTIME_IDLE_TTL_MS;if(!t)return 1800*1e3;let e=Number(t);return!Number.isFinite(e)||e<0?1800*1e3:e}import{spawn as fs}from"node:child_process";var ys="Reading additional input from stdin...";function ln(t){return new Promise((e,n)=>{let r=[],s="",o="",i="",a=fs(t.command,t.args,{cwd:t.cwd||process.cwd(),env:t.env,stdio:["ignore","pipe","pipe"]});if(t.signal?.aborted){a.kill("SIGTERM"),n(new Error("runtime_cancelled"));return}t.signal?.addEventListener("abort",()=>{a.kill("SIGTERM"),n(new Error("runtime_cancelled"))},{once:!0}),a.stdout.on("data",u=>{o=an(o+u.toString(),l=>{let c=un(l);if(c){let d=t.onJson(c);d&&(s=He(s,d));return}t.onLog("stdout",l)})}),a.stderr.on("data",u=>{i=an(i+u.toString(),l=>{Qe(l)||(r.push(l),t.onLog("stderr",l))})}),a.on("error",n),a.on("exit",u=>{if(o.trim()){let l=un(o.trim());if(l){let c=t.onJson(l);c&&(s=He(s,c))}else t.onLog("stdout",o.trim())}if(i.trim()&&!Qe(i.trim())&&(r.push(i.trim()),t.onLog("stderr",i.trim())),t.onLog("system",`${t.command} exited with code ${u??"unknown"}`),u===0){e();return}n(new Error(s||hs(r)||`${t.command} exited with code ${u}`))})})}function an(t,e){let n=t.split(/\r?\n/),r=n.pop()??"";for(let s of n)s.trim()&&e(s);return r}function un(t){try{return JSON.parse(t)}catch{return null}}function Qe(t){return t.trim()===ys}function hs(t){return t.map(e=>e.trim()).filter(e=>e&&!Qe(e)).at(-1)??""}import{readFile as vs}from"node:fs/promises";async function cn(t){try{return await vs(t,"utf8")}catch{return""}}function dn(t){if(!t||typeof t!="object")return null;let e=t,n=Is(t)??"";for(let s of["delta","text_delta","content_delta"])if(typeof e[s]=="string"&&e[s])return e[s];if(n.includes("delta")||n.includes("stream")){for(let s of["text","content","message"])if(typeof e[s]=="string"&&e[s])return e[s]}return pn(e.delta??e.message??e.item)}function pn(t){if(typeof t=="string"&&t)return t;if(!t||typeof t!="object")return null;let e=t;for(let n of["delta","text_delta","content_delta","text"])if(typeof e[n]=="string"&&e[n])return e[n];for(let n of Object.values(e)){let r=pn(n);if(r)return r}return null}function Is(t){if(!t||typeof t!="object")return null;let e=t;for(let n of["type","event","event_name","method"])if(typeof e[n]=="string")return e[n];return null}var q=class{type="codex";appServers=new fe;busyDeliveryMode="direct";capabilities={supportsSession:!0,supportsStreaming:!0,supportsExecutionPolicy:!0};async detect(){return!!await this.resolveCommand()}notifyInbox(e,n){return this.appServers.notify(e,n)}async listModels(){let e=await this.resolveCommand();return e?$t(e):[]}async handleDelivery(e,n){let r=await this.resolveCommand();if(!r)throw new Error("codex runtime is not available");let s=await w(e),o=s?.runtimeType===this.type?s.runtimeSessionId:null,i=o?`resuming ${o}`:"starting new session";n.onLog("system",`codex ${i} in ${n.cwd??process.cwd()}`);let a;try{a=await this.runCodexAppServer(r,e,n,o)}catch(l){if(mn(l))n.onLog("stderr",`codex app-server failed; falling back to codex exec. ${l instanceof Error?l.message:""}`),a=await this.runCodex(r,e,n,o);else if(o)n.onLog("stderr",`codex resume ${o} failed; starting a new session. ${l instanceof Error?l.message:""}`),a=await this.runCodexAppServer(r,e,n,null).catch(c=>{if(!mn(c))throw c;return n.onLog("stderr",`codex app-server retry failed; falling back to codex exec. ${c instanceof Error?c.message:""}`),this.runCodex(r,e,n,null)});else throw l}let u=a.sessionId??o??null;return await S({agentId:e.agentId,channelId:e.channelId,runtimeScopeKey:g(e),runtimeType:this.type,runtimeSessionId:u,workspacePath:n.cwd??null,updatedAt:new Date().toISOString()}),u?n.onLog("system",`codex session ${u}`):n.onLog("system","codex completed without exposing a resumable session id"),a.suppressMessage?{content:a.output.trim(),sessionId:u,suppressMessage:!0}:{content:a.output.trim()||"codex runtime completed without output",sessionId:u}}async runCodexAppServer(e,n,r,s){let o=await v(n,{workspacePath:r.cwd}),i=I(n,de(r.env));r.onLog("system",i?`codex app-server using model override ${i}`:"codex app-server using configured model"),r.onLog("system",`codex app-server sandbox ${C(n.executionPolicy)}`);let a=await this.appServers.runTurn({command:e,frame:n,context:r,prompt:o,sessionId:s,model:i});return{output:a.content,sessionId:a.sessionId??null,suppressMessage:a.suppressMessage}}async runCodex(e,n,r,s){let o=h(n);await ws(o,{recursive:!0});let i=xs(o,"last-message.txt");await Ss(i,"","utf8");let a=await v(n,{workspacePath:r.cwd}),u=I(n,de(r.env));r.onLog("system",u?`codex using model override ${u}`:"codex using configured model"),r.onLog("system",`codex sandbox ${C(n.executionPolicy)}`);let l=new Set;return await ln({command:e,args:Ge({frame:n,prompt:a,outputFile:i,model:u,sessionId:s}),cwd:r.cwd,env:r.env,signal:r.abortSignal,onJson:c=>{let d=dn(c);d&&r.onPartial?.(d);let m=Ve(c);m&&l.add(m);let p=me(c);p&&r.onLog("system",`[codex:event] ${p}`);let f=B(D(c))??ge(c);return f?(r.onLog("stderr",`[codex:error] ${f}`),f):null},onLog:r.onLog}),{output:await cn(i),sessionId:[...l].at(-1)??null}}async resolveCommand(){return x({command:"codex",envVars:["CODEX_BIN","CODEX_CLI_PATH"]})}};function mn(t){if(process.env.KSLOCK_CODEX_DISABLE_EXEC_FALLBACK==="1")return!1;let e=t instanceof Error?t.message:String(t);return/app-server exited|unrecognized|unknown command|invalid subcommand|not supported/i.test(e)}import{randomUUID as Cn}from"node:crypto";function Xe(t){return t.preset==="read_only"?["--permission-mode","dontAsk","--tools","Read,Grep,Glob,LS"]:t.preset==="full_access"?["--dangerously-skip-permissions"]:["--permission-mode","acceptEdits","--allowedTools",["Read","Grep","Glob","LS","Edit","MultiEdit","Write","Bash(git status *)","Bash(git diff *)","Bash(git log *)","Bash(git show *)","Bash(pnpm test *)","Bash(pnpm run test *)","Bash(pnpm typecheck *)","Bash(pnpm run typecheck *)","Bash(pnpm lint *)","Bash(pnpm run lint *)","Bash(pnpm build *)","Bash(pnpm run build *)","Bash(npm test *)","Bash(npm run test *)","Bash(npm run typecheck *)","Bash(npm run lint *)","Bash(npm run build *)","Bash(yarn test *)","Bash(yarn typecheck *)","Bash(yarn lint *)","Bash(yarn build *)","Bash(tsc *)","Bash(vitest *)"].join(","),"--disallowedTools",["Bash(rm *)","Bash(sudo *)","Bash(chmod *)","Bash(chown *)","Bash(curl *)","Bash(wget *)"].join(",")]}function Ye(t){return["-p","--output-format","stream-json","--verbose","--include-partial-messages",...t.resume?["--resume",t.sessionId]:["--session-id",t.sessionId],...gn(t.model),...Xe(t.frame.executionPolicy)]}function Ze(t){return["--output-format","stream-json","--input-format","stream-json","--verbose","--include-partial-messages",...t.resume?["--resume",t.sessionId]:["--session-id",t.sessionId],...gn(t.model),...Xe(t.frame.executionPolicy)]}function ye(t){return t.KSCC_MODEL?.trim()||t.ANTHROPIC_MODEL?.trim()||null}function gn(t){return t?["--model",t]:[]}var he="kscc \u8FD4\u56DE\u9519\u8BEF\uFF0C\u672A\u63D0\u4F9B\u8BE6\u7EC6\u539F\u56E0";function tt(t){if(!t||typeof t!="object")return null;let e=t;for(let n of["type","subtype","event","event_name"])if(typeof e[n]=="string")return e[n];return null}function ve(t){if(!t||typeof t!="object")return null;let e=t;for(let n of["session_id","sessionId"])if(typeof e[n]=="string"&&Rs(e[n]))return e[n];for(let n of Object.values(e)){let r=ve(n);if(r)return r}return null}function z(t){if(!t||typeof t!="object")return null;let e=t;if(typeof e.result=="string"&&e.result.trim())return e.result;if(typeof e.content=="string"&&e.content.trim())return e.content;let n=e.message;if(n&&typeof n=="object"){let r=fn(n.content);if(r)return r}return fn(e.content)}function U(t){if(!t)return null;let e=bs(t),r=(e?J(e):t)?.trim();return!r||et(r)?null:r}function M(t){if(!t||typeof t!="object")return null;let e=t,n=typeof e.type=="string"?e.type:"",r=typeof e.subtype=="string"?e.subtype:"";return n==="error"||r==="error"||r==="failure"||e.is_error===!0||n==="result"&&r&&r!=="success"?J(t)??he:null}function G(t,e){return!t||t===he&&e!==he?e:t}function fn(t){if(typeof t=="string"&&t.trim())return t;if(!Array.isArray(t))return null;let e=t.map(n=>{if(!n||typeof n!="object")return"";let r=n;return typeof r.text=="string"?r.text:""}).filter(Boolean).join("");return e.trim()?e:null}function J(t){if(typeof t=="string"&&t&&!et(t))return t;if(!t||typeof t!="object")return null;let e=t;for(let r of["message","detail","reason","error"]){let s=e[r];if(typeof s=="string"&&s&&!et(s))return s}let n=J(e.error);if(n)return n;for(let r of Object.values(e)){let s=J(r);if(s)return s}return null}function Rs(t){return/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(t)}function et(t){let e=t.trim().toLowerCase();return e==="error"||e==="failure"}function bs(t){try{return JSON.parse(t)}catch{return null}}import{spawn as ks}from"node:child_process";function Sn(t){return new Promise((e,n)=>{let r=[],s=[],o="",i=[],a="",u="",l="",c=ks(t.command,t.args,{cwd:t.cwd||process.cwd(),env:t.env,stdio:["pipe","pipe","pipe"]});if(c.stdin.on("error",()=>{}),t.signal?.aborted){c.kill("SIGTERM"),n(new Error("runtime_cancelled"));return}t.signal?.addEventListener("abort",()=>{c.kill("SIGTERM"),n(new Error("runtime_cancelled"))},{once:!0}),c.stdin.end(t.stdin??""),c.stdout.on("data",d=>{u=yn(u+d.toString(),m=>{let p=hn(m);if(p){let f=t.onJson(p)??M(p);f&&(a=G(a,f));let y=z(p);y&&(vn(p)?s.push(y):o=In(y,o,t.onPartial));return}r.push(m),t.onLog("stdout",m)})}),c.stderr.on("data",d=>{l=yn(l+d.toString(),m=>{i.push(m),t.onLog("stderr",m)})}),c.on("error",n),c.on("exit",d=>{if(u.trim()){let p=hn(u.trim());if(p){let f=t.onJson(p)??M(p);f&&(a=G(a,f));let y=z(p);y&&(vn(p)?s.push(y):o=In(y,o,t.onPartial))}else r.push(u.trim()),t.onLog("stdout",u.trim())}if(l.trim()&&(i.push(l.trim()),t.onLog("stderr",l.trim())),t.onLog("system",`${t.command} exited with code ${d??"unknown"}`),d===0){e({output:Cs(s,o,r)});return}let m=U(wn(i))??wn(i);n(new Error(a||m||`${t.command} exited with code ${d}`))})})}function yn(t,e){let n=t.split(/\r?\n/),r=n.pop()??"";for(let s of n)s.trim()&&e(s);return r}function hn(t){try{return JSON.parse(t)}catch{return null}}function vn(t){return!!(t&&typeof t=="object"&&t.type==="result")}function In(t,e,n){let r=t.startsWith(e)?t:`${e}${t}`,s=r.slice(e.length);return s&&n?.(s,r),r}function Cs(t,e,n){let r=t.at(-1)?.trim();if(r)return r;let s=e.trim();return s||n.join(`
33
+ `).trim()}function wn(t){return t.map(e=>e.trim()).filter(Boolean).at(-1)??""}import{mkdir as As,readFile as Es,writeFile as xn}from"node:fs/promises";import{join as Ts}from"node:path";var Ds="kscc-last-message.txt";function Ms(t){return Ts(h(t),Ds)}async function Rn(t){await As(h(t),{recursive:!0});let e=Ms(t);return await xn(e,"","utf8"),e}async function bn(t,e){await xn(t,e,"utf8")}async function kn(t){try{return await Es(t,"utf8")}catch{return""}}var W=class{type="kscc";busyDeliveryMode="gated";sessions=new E({type:"kscc",displayName:"kscc",buildArgs:Ze,resolveModel:ye,extractSessionId:ve,extractText:z,extractEventName:tt,summarizeError:M,normalizeError:U,preferFailure:G});capabilities={supportsSession:!0,supportsStreaming:!0,supportsExecutionPolicy:!0};async detect(){return!!await this.resolveCommand()}notifyInbox(e,n){return this.sessions.notify(e,n)}async listModels(){let e=await this.resolveCommand();return e?ie(e):[]}async handleDelivery(e,n){let r=await this.resolveCommand();if(!r)throw new Error("kscc runtime is not available");let s=await w(e),o=s?.runtimeType===this.type?s.runtimeSessionId:null,i=o??Cn(),a=o?`resuming ${o}`:`starting new session ${i}`;n.onLog("system",`kscc ${a} in ${n.cwd??process.cwd()}`);let u;try{u=await this.runKsccStream(r,e,n,i,!!o)}catch(l){if(An(l))n.onLog("stderr",`kscc stream-json session failed; falling back to print mode. ${l instanceof Error?l.message:""}`),u=await this.runKscc(r,e,n,i,!!o);else if(o){let c=Cn();n.onLog("stderr",`kscc resume ${o} failed; starting a new session ${c}. ${l instanceof Error?l.message:""}`),u=await this.runKsccStream(r,e,n,c,!1).catch(d=>{if(!An(d))throw d;return n.onLog("stderr",`kscc stream retry failed; falling back to print mode. ${d instanceof Error?d.message:""}`),this.runKscc(r,e,n,c,!1)})}else throw l}return await S({agentId:e.agentId,channelId:e.channelId,runtimeScopeKey:g(e),runtimeType:this.type,runtimeSessionId:u.sessionId,workspacePath:n.cwd??null,updatedAt:new Date().toISOString()}),n.onLog("system",`kscc session ${u.sessionId}`),u.suppressMessage?{content:u.output.trim(),sessionId:u.sessionId,suppressMessage:!0}:{content:u.output.trim()||"kscc runtime completed without output",sessionId:u.sessionId}}async runKsccStream(e,n,r,s,o){r.onLog("system",o?`kscc stream resume ${s}`:`kscc stream session-id ${s}`);let i=await this.sessions.runTurn({command:e,frame:n,context:r,prompt:await v(n,{workspacePath:r.cwd}),sessionId:s,resume:o});return{output:i.content,sessionId:i.sessionId??s,suppressMessage:i.suppressMessage}}async runKscc(e,n,r,s,o){let i=await v(n,{workspacePath:r.cwd}),a=I(n,ye(r.env)),u=await Rn(n);r.onLog("system",a?`kscc using model override ${a}`:"kscc using configured model"),r.onLog("system",o?`kscc resume ${s}`:`kscc session-id ${s}`);let l=new Set,c=await Sn({command:e,args:Ye({frame:n,sessionId:s,resume:o,model:a}),stdin:i,cwd:r.cwd,env:r.env,signal:r.abortSignal,onPartial:r.onPartial,onJson:d=>{let m=ve(d);m&&l.add(m);let p=tt(d);p&&r.onLog("system",`[kscc:event] ${p}`);let f=U(M(d));return f?(r.onLog("stderr",`[kscc:error] ${f}`),f):null},onLog:r.onLog});return await bn(u,c.output),{output:c.output||await kn(u),sessionId:[...l].at(-1)??s}}async resolveCommand(){return x({command:"kscc",envVars:["KSCC_BIN"]})}};function An(t){if(process.env.KSLOCK_KSCC_DISABLE_PRINT_FALLBACK==="1")return!1;let e=t instanceof Error?t.message:String(t);return/process exited|unknown option|unrecognized|input-format|stream-json|stdin|not supported/i.test(e)}function En(t){return t.preset==="full_access"?"allow":t.preset==="read_only"?{"*":"deny",read:{"*":"allow","*.env":"deny","*.env.*":"deny","*.env.example":"allow"},glob:"allow",grep:"allow",list:"allow",lsp:"allow",todoread:"allow",edit:"deny",bash:"deny",task:"deny",skill:"deny",todowrite:"deny",webfetch:"deny",websearch:"deny",codesearch:"deny",external_directory:"deny",doom_loop:"deny"}:{"*":"deny",read:{"*":"allow","*.env":"deny","*.env.*":"deny","*.env.example":"allow"},glob:"allow",grep:"allow",list:"allow",lsp:"allow",todoread:"allow",todowrite:"allow",edit:"allow",skill:"allow",bash:{"*":"deny","git status *":"allow","git diff *":"allow","git log *":"allow","git show *":"allow","pnpm test *":"allow","pnpm run test *":"allow","pnpm typecheck *":"allow","pnpm run typecheck *":"allow","pnpm lint *":"allow","pnpm run lint *":"allow","pnpm build *":"allow","pnpm run build *":"allow","npm test *":"allow","npm run test *":"allow","npm run typecheck *":"allow","npm run lint *":"allow","npm run build *":"allow","yarn test *":"allow","yarn typecheck *":"allow","yarn lint *":"allow","yarn build *":"allow","tsc *":"allow","vitest *":"allow","rm *":"deny","sudo *":"deny","chmod *":"deny","chown *":"deny","curl *":"deny","wget *":"deny"},task:"deny",webfetch:"deny",websearch:"deny",codesearch:"deny",external_directory:"deny",doom_loop:"deny"}}var nt="1.14.30";function rt(t){return["run","--format","json","--pure",...Ps(t.workspacePath),...Fs(t.sessionId),..._s(t.model),...Ls(t.frame.executionPolicy),"--",t.message]}function st(t,e){return{...t,OPENCODE_PERMISSION:JSON.stringify(En(e))}}function ot(t){return t.OPENCODE_MODEL?.trim()||null}function Ie(t){return!t||Dn(t)?null:`OpenCode CLI ${t} \u6682\u4E0D\u652F\u6301\uFF0C\u5F53\u524D\u8FD0\u884C\u65F6\u8981\u6C42 opencode >= ${nt}\u3002\u8BF7\u5347\u7EA7 opencode \u540E\u91CD\u542F daemon\u3002`}function Dn(t){if(!t)return!0;let e=Tn(t),n=Tn(nt);if(!e||!n)return!0;for(let r=0;r<3;r+=1){if(e[r]>n[r])return!0;if(e[r]<n[r])return!1}return!0}function Tn(t){let e=t.match(/(\d+)\.(\d+)\.(\d+)/);return e?[Number(e[1]),Number(e[2]),Number(e[3])]:null}function Fs(t){return t?["--session",t]:[]}function Ps(t){return t?["--dir",t]:[]}function _s(t){return t?["--model",t]:[]}function Ls(t){return t.preset==="full_access"?["--dangerously-skip-permissions"]:[]}var we="opencode \u8FD4\u56DE\u9519\u8BEF\uFF0C\u672A\u63D0\u4F9B\u8BE6\u7EC6\u539F\u56E0";function Fn(t){if(!t||typeof t!="object")return null;let e=t;for(let n of["type","subtype","event","event_name","eventName"])if(typeof e[n]=="string")return e[n];return null}function at(t){if(!t||typeof t!="object")return null;let e=t;for(let r of["sessionID","sessionId","session_id"])if(typeof e[r]=="string"&&e[r].trim())return e[r];let n=e.session;if(n&&typeof n=="object"){let r=n.id;if(typeof r=="string"&&r.trim())return r}for(let r of Object.values(e)){let s=at(r);if(s)return s}return null}function ut(t){if(!t||typeof t!="object")return null;let e=t;if(typeof e.result=="string"&&e.result.trim())return e.result;if(typeof e.output=="string"&&e.output.trim())return e.output;let n=Mn(e.message);if(n)return n;let r=Mn(e.part);if(r)return r;if(js(e)){if(typeof e.content=="string"&&e.content.trim())return e.content;if(typeof e.text=="string"&&e.text.trim())return e.text;let s=Se(e.content);if(s)return s;let o=Se(e.parts);if(o)return o}return null}function xe(t){if(!t)return null;let e=Os(t),r=(e?V(e):t)?.trim();return!r||it(r)?null:r}function H(t){if(!t||typeof t!="object")return null;let e=t,n=typeof e.type=="string"?e.type.toLowerCase():"",r=typeof e.subtype=="string"?e.subtype.toLowerCase():"";return n.includes("error")||r==="error"||r==="failure"||e.is_error===!0||n==="result"&&r&&r!=="success"?V(t)??we:null}function lt(t,e){return!t||t===we&&e!==we?e:t}function Mn(t){if(!t||typeof t!="object")return null;let e=t,n=typeof e.role=="string"?e.role.toLowerCase():"",r=typeof e.type=="string"?e.type.toLowerCase():"";return n&&n!=="assistant"?null:typeof e.text=="string"&&e.text.trim()?e.text:typeof e.content=="string"&&e.content.trim()?e.content:r&&r!=="text"&&!r.includes("assistant")&&!r.includes("message")&&!r.includes("part")?null:Se(e.content)??Se(e.parts)}function Se(t){if(typeof t=="string"&&t.trim())return t;if(!Array.isArray(t))return null;let e=t.map(n=>{if(!n||typeof n!="object")return"";let r=n;return typeof r.text=="string"?r.text:typeof r.content=="string"?r.content:""}).filter(Boolean).join("");return e.trim()?e:null}function js(t){let e=typeof t.role=="string"?t.role.toLowerCase():"";if(e&&e!=="assistant")return!1;let n=typeof t.type=="string"?t.type.toLowerCase():"";return n?n==="result"||n==="text"||n.includes("assistant")||n.includes("message")||n.includes("part"):e==="assistant"}function V(t){if(typeof t=="string"&&t&&!it(t))return t;if(!t||typeof t!="object")return null;let e=t;for(let r of["message","detail","reason","error"]){let s=e[r];if(typeof s=="string"&&s&&!it(s))return s}let n=V(e.error);if(n)return n;for(let r of Object.values(e)){let s=V(r);if(s)return s}return null}function it(t){let e=t.trim().toLowerCase();return e==="error"||e==="failure"}function Os(t){try{return JSON.parse(t)}catch{return null}}import{spawn as $s}from"node:child_process";function $n(t){return new Promise((e,n)=>{let r=[],s=[],o="",i=[],a="",u="",l="",c=$s(t.command,t.args,{cwd:t.cwd||process.cwd(),env:t.env,stdio:["ignore","pipe","pipe"]});if(t.signal?.aborted){c.kill("SIGTERM"),n(new Error("runtime_cancelled"));return}t.signal?.addEventListener("abort",()=>{c.kill("SIGTERM"),n(new Error("runtime_cancelled"))},{once:!0}),c.stdout.on("data",d=>{u=Pn(u+d.toString(),m=>{let p=_n(m);if(p){let f=t.onJson(p)??H(p);f&&(a=lt(a,f));let y=ut(p);y&&(Ln(p)?s.push(y):o=jn(y,o,t.onPartial));return}r.push(m),t.onLog("stdout",m)})}),c.stderr.on("data",d=>{l=Pn(l+d.toString(),m=>{i.push(m),t.onLog("stderr",m)})}),c.on("error",n),c.on("exit",d=>{if(u.trim()){let p=_n(u.trim());if(p){let f=t.onJson(p)??H(p);f&&(a=lt(a,f));let y=ut(p);y&&(Ln(p)?s.push(y):o=jn(y,o,t.onPartial))}else r.push(u.trim()),t.onLog("stdout",u.trim())}if(l.trim()&&(i.push(l.trim()),t.onLog("stderr",l.trim())),t.onLog("system",`${t.command} exited with code ${d??"unknown"}`),d===0){e({output:Ns(s,o,r)});return}let m=xe(On(i))??On(i);n(new Error(a||m||`${t.command} exited with code ${d}`))})})}function Pn(t,e){let n=t.split(/\r?\n/),r=n.pop()??"";for(let s of n)s.trim()&&e(s);return r}function _n(t){try{return JSON.parse(t)}catch{return null}}function Ln(t){if(!t||typeof t!="object")return!1;let e=t,n=typeof e.type=="string"?e.type.toLowerCase():"";return n==="result"||n.includes("result")||n.includes("complete")}function jn(t,e,n){let r=t.startsWith(e)?t:`${e}${t}`,s=r.slice(e.length);return s&&n?.(s,r),r}function Ns(t,e,n){let r=t.at(-1)?.trim();if(r)return r;let s=e.trim();return s||n.join(`
34
+ `).trim()}function On(t){return t.map(e=>e.trim()).filter(Boolean).at(-1)??""}import{mkdir as Ks,readFile as Bs,writeFile as Nn}from"node:fs/promises";import{join as qs}from"node:path";var Js="opencode-last-message.txt";function zs(t){return qs(h(t),Js)}async function Kn(t){await Ks(h(t),{recursive:!0});let e=zs(t);return await Nn(e,"","utf8"),e}async function Bn(t,e){await Nn(t,e,"utf8")}async function qn(t){try{return await Bs(t,"utf8")}catch{return""}}var Q=class{type="opencode";busyDeliveryMode="queue";capabilities={supportsSession:!0,supportsStreaming:!0,supportsExecutionPolicy:!0};async detect(){let e=await this.resolveCommand();return e?!Ie(await Oe(e)):!1}async listModels(){let e=await this.resolveCommand();return e?Nt(e):[]}async handleDelivery(e,n){let r=await this.resolveCommand();if(!r)throw new Error("opencode runtime is not available");let s=Ie(await Oe(r));if(s)throw new Error(s);let o=await w(e),i=o?.runtimeType===this.type?o.runtimeSessionId:null,a=i?`resuming ${i}`:"starting new session";n.onLog("system",`opencode ${a} in ${n.cwd??process.cwd()}`);let u;try{u=await this.runOpenCode(r,e,n,i)}catch(l){if(!i)throw l;n.onLog("stderr",`opencode resume ${i} failed; starting a new session. ${l instanceof Error?l.message:""}`),u=await this.runOpenCode(r,e,n,null)}return await S({agentId:e.agentId,channelId:e.channelId,runtimeScopeKey:g(e),runtimeType:this.type,runtimeSessionId:u.sessionId,workspacePath:n.cwd??null,updatedAt:new Date().toISOString()}),u.sessionId&&n.onLog("system",`opencode session ${u.sessionId}`),{content:u.output.trim()||"opencode runtime completed without output",sessionId:u.sessionId}}async runOpenCode(e,n,r,s){let o=await v(n,{workspacePath:r.cwd}),i=I(n,ot(r.env)),a=await Kn(n),u=st(r.env,n.executionPolicy);r.onLog("system",i?`opencode using model override ${i}`:"opencode using configured model"),r.onLog("system",s?`opencode session ${s}`:"opencode new session");let l=new Set,c=await $n({command:e,args:rt({frame:n,message:o,sessionId:s,model:i,workspacePath:r.cwd}),cwd:r.cwd,env:u,signal:r.abortSignal,onPartial:r.onPartial,onJson:d=>{let m=at(d);m&&l.add(m);let p=Fn(d);p&&r.onLog("system",`[opencode:event] ${p}`);let f=xe(H(d));return f?(r.onLog("stderr",`[opencode:error] ${f}`),f):null},onLog:r.onLog});return await Bn(a,c.output),{output:c.output||await qn(a),sessionId:[...l].at(-1)??s}}async resolveCommand(){return x({command:"opencode",envVars:["OPENCODE_BIN"]})}};var ct=class{runtimes=new Map;constructor(e){for(let n of e)this.runtimes.set(n.type,n)}get(e){let n=this.runtimes.get(e);if(!n)throw new Error(`runtime ${e} is not registered`);return n}detectable(){return[...this.runtimes.values()]}async detectCapabilities(){return Promise.all(this.detectable().map(async e=>{try{let n=e.detect?await e.detect():!0,r=n&&e.listModels?await e.listModels().catch(()=>[]):[];return{type:e.type,available:n,...e.capabilities,...r.length>0?{models:r}:{}}}catch{return{type:e.type,available:!1,...e.capabilities}}}))}};function Jn(){return new ct([new q,new N,new W,new Q])}var Re=class{runtimes=Jn();publisher;runner;queue;constructor(e){this.publisher=new Y(e),this.runner=new se(this.runtimes,this.publisher),this.queue=new Z((n,r)=>{ne(n,n.deliveryId),this.publisher.failed(n,r),this.publisher.status(n,"error")})}async deliver(e){await this.queue.enqueue(e,(n,r)=>this.runner.run(n,r),{allowBusyDelivery:!0,onBusyDelivery:n=>this.handleBusyDelivery(n)})}cancelDelivery(e,n="cancelled_by_user"){this.queue.cancel(e,n)}handlePermissionResponse(e){}async detectAvailableRuntimes(){return(await this.detectRuntimeCapabilities()).filter(n=>n.available).map(n=>n.type)}async detectRuntimeCapabilities(){return(await this.runtimes.detectCapabilities()).map(n=>({...n,available:n.available}))}async handleBusyDelivery(e){let n=this.runtimes.get(e.runtimeType),r=await wt(e),s=_(e),o=Et({pendingCount:r.messages.length,checkMessagesCommand:s.KSLOCK_CHECK_MESSAGES_COMMAND,inboxFile:s.KSLOCK_INBOX_FILE});this.publisher.delivered(e),this.publisher.runtimeLog(e,"system",`queued in agent inbox (${r.messages.length} pending)`),await n.notifyInbox?.(e,o)||this.publisher.runtimeLog(e,"system","agent inbox notification deferred until the queued delivery starts")}};var be=class{frames=[];enqueue(e){return Us(e)?(this.frames.push(e),this.frames.length>200&&this.frames.splice(0,this.frames.length-200),!0):!1}drain(){return this.frames.splice(0)}get size(){return this.frames.length}};function Us(t){return t.type==="agent:delivery_ack"||t.type==="agent:message"||t.type==="agent:status"||t.type==="task:status"}var ke=class{constructor(e){this.config=e}ws=null;reconnectAttempts=0;reconnectTimer=null;stopped=!1;outbox=new be;agentManager=new Re(e=>this.send(e));connect(){this.stopped=!1;let e=this.buildWebSocketUrl();this.ws=new zn(e),this.ws.on("open",()=>{this.handleOpen()}),this.ws.on("message",n=>{let r=Gs(n.toString());r&&this.handleFrame(r)}),this.ws.on("close",(n,r)=>{console.log(`[Daemon] Disconnected${Ws(n,r)}`),this.ws=null,this.scheduleReconnect()}),this.ws.on("error",n=>{console.error(`[Daemon] WebSocket error: ${n.message}`)})}close(){this.stopped=!0,this.reconnectTimer&&(clearTimeout(this.reconnectTimer),this.reconnectTimer=null),this.ws?.close()}async handleFrame(e){if(e.type==="ping"){this.send({type:"pong",requestId:e.requestId});return}if(e.type==="error"){console.error(`[Daemon] Server error ${e.code}: ${e.message}`),(e.code==="invalid_machine_key"||e.code==="machine_key_revoked")&&(this.stopped=!0);return}if(e.type==="agent:deliver"){await this.agentManager.deliver(e);return}if(e.type==="agent:cancel"){this.agentManager.cancelDelivery(e.deliveryId,e.reason);return}e.type==="permission:response"&&this.agentManager.handlePermissionResponse(e)}async handleOpen(){this.reconnectAttempts=0,console.log(`[Daemon] Connected to ${this.config.serverUrl}`);let e=await this.agentManager.detectRuntimeCapabilities();this.send({type:"machine:ready",daemonVersion:this.config.daemonVersion,hostname:this.config.hostname,os:this.config.os,arch:this.config.arch,availableRuntimes:e.filter(n=>n.available).map(n=>n.type),runtimeCapabilities:e}),this.flushOutbox()}send(e){this.sendNow(e)||this.stopped||this.outbox.enqueue(e)&&console.warn(`[Daemon] Queued ${e.type}; websocket is not open`)}sendNow(e){if(!this.ws||this.ws.readyState!==zn.OPEN)return!1;try{return this.ws.send(JSON.stringify(e)),!0}catch{return!1}}flushOutbox(){if(this.outbox.size===0)return;let e=this.outbox.drain();console.log(`[Daemon] Flushing ${e.length} queued frame(s)`);for(let n of e)this.sendNow(n)||this.outbox.enqueue(n)}buildWebSocketUrl(){return`${this.config.serverUrl.replace(/^http/,"ws")}/daemon/connect?key=${encodeURIComponent(this.config.machineKey)}`}scheduleReconnect(){if(this.stopped||this.reconnectTimer)return;let e=Math.min(this.config.reconnectBaseMs*2**this.reconnectAttempts,3e4);this.reconnectAttempts+=1,console.log(`[Daemon] Reconnecting in ${e}ms`),this.reconnectTimer=setTimeout(()=>{this.reconnectTimer=null,this.stopped||this.connect()},e)}};function Gs(t){try{return JSON.parse(t)}catch{return null}}function Ws(t,e){let n=e.toString();return!t&&!n?"":` (code ${t}${n?`, reason: ${n}`:""})`}function Gn(t=process.argv.slice(2)){if(t[0]==="check-messages"||t[0]==="check_messages")return Vs(t.slice(1)),null;if(t.includes("--help")||t.includes("-h"))return console.log(Ee),null;if(t.includes("--version")||t.includes("-v"))return console.log(Ae()),null;mt();let e=pt(t);if(!e)return console.error(Ee),process.exitCode=1,null;let n=new ke(e);n.connect();let r=()=>{n.close(),process.exit(0)};return process.once("SIGINT",r),process.once("SIGTERM",r),n}async function Vs(t){let e=Un(t,"--agent-id")??process.env.AGENT_ID,n=Un(t,"--channel-id")??process.env.CHANNEL_ID;if(!e){console.error("Usage: kslock-daemon check-messages --agent-id <agent-id> [--channel-id <channel-id>]"),process.exitCode=1;return}let r=await St({agentId:e,channelId:n});if(t.includes("--json")){console.log(JSON.stringify(r,null,2));return}console.log(Fe(r))}function Un(t,e){let n=t.indexOf(e);return n<0?null:t[n+1]??null}Gn();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kslock-daemon",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "Local machine daemon for kslock agent collaboration.",
5
5
  "license": "UNLICENSED",
6
6
  "type": "module",