grix-connector 1.0.2 → 1.0.4

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 (39) hide show
  1. package/dist/adapter/acp/acp-adapter.js +10 -8
  2. package/dist/adapter/claude/claude-adapter.js +16 -15
  3. package/dist/adapter/claude/claude-bridge-server.js +1 -1
  4. package/dist/adapter/claude/mcp-http-launcher.js +2 -2
  5. package/dist/adapter/codewhale/codewhale-adapter.js +4 -3
  6. package/dist/adapter/codex/codex-bridge.js +6 -6
  7. package/dist/adapter/cursor/cursor-adapter.js +3 -2
  8. package/dist/adapter/opencode/opencode-adapter.js +7 -4
  9. package/dist/adapter/openhuman/openhuman-adapter.js +5 -2
  10. package/dist/adapter/pi/pi-adapter.js +8 -5
  11. package/dist/bridge/adapter-pool.js +1 -1
  12. package/dist/bridge/bridge.js +10 -6
  13. package/dist/bridge/deferred-events.js +1 -1
  14. package/dist/bridge/event-queue.js +1 -1
  15. package/dist/bridge/send-controller.js +4 -1
  16. package/dist/core/admin/admin-server.js +1 -0
  17. package/dist/core/admin/admin-token.js +1 -0
  18. package/dist/core/admin/index.js +1 -0
  19. package/dist/core/aibot/client.js +2 -1
  20. package/dist/core/aibot/connection-handle.js +1 -1
  21. package/dist/core/file-ops/handler.js +1 -1
  22. package/dist/core/mcp/event-tool-executor.js +1 -1
  23. package/dist/core/mcp/event-tool-port.js +0 -0
  24. package/dist/core/protocol/message-metadata.js +1 -1
  25. package/dist/core/protocol/message-reference.js +1 -1
  26. package/dist/core/protocol/payload-parser.js +6 -6
  27. package/dist/core/protocol/protocol-text.js +1 -1
  28. package/dist/core/provider-quota/kiro.js +1 -1
  29. package/dist/core/text-segmentation/safe-markdown-stream-segmenter.js +6 -6
  30. package/dist/core/upgrade/upgrade-checker.js +1 -1
  31. package/dist/grix.js +4 -3
  32. package/dist/log.js +2 -2
  33. package/dist/manager.js +2 -1
  34. package/dist/mcp/stream-http/gateway.js +1 -1
  35. package/dist/mcp/stream-http/security.js +1 -1
  36. package/dist/mcp/stream-http/tool-schemas.js +1 -1
  37. package/dist/protocol/acp-client.js +1 -1
  38. package/openclaw-plugin/index.js +15 -10
  39. package/package.json +4 -3
@@ -1,10 +1,14 @@
1
- import x from"node:path";import{realpath as U,stat as T}from"node:fs/promises";import{ConnectionManager as Q}from"../core/aibot/index.js";import{ClaudeAdapter as E}from"../adapter/claude/index.js";import{CodexAdapter as F}from"../adapter/codex/index.js";import{PiAdapter as q}from"../adapter/pi/index.js";import{AcpAdapter as b}from"../adapter/acp/index.js";import{OpenHumanAdapter as O}from"../adapter/openhuman/index.js";import{CursorAdapter as $}from"../adapter/cursor/index.js";import{CodeWhaleAdapter as D}from"../adapter/codewhale/index.js";import{OpenCodeAdapter as W}from"../adapter/opencode/index.js";import{LOCAL_ACTION_ERROR_CODES as A,LOCAL_ACTION_TYPES as p,SESSION_CONTROL_ERROR_CODES as h,SESSION_CONTROL_VERBS as m,SESSION_MODE_IDS as v}from"../adapter/claude/protocol-contract.js";import{parseClaudeSessionUsage as N}from"../adapter/claude/usage-parser.js";import{fetchAvailableModels as H,getCachedModels as M,readSettingsEnv as K}from"../adapter/claude/model-list.js";import{parseAcpSessionUsage as z}from"../adapter/acp/usage-parser.js";import{parseCodexSessionUsage as G}from"../adapter/codex/usage-parser.js";import{readCodexProviderSettings as j}from"../adapter/codex/codex-trust.js";import{parsePiSessionUsage as V}from"../adapter/pi/usage-parser.js";import{log as u,ConversationLog as J}from"../core/log/index.js";import{RevokeHandler as Y}from"./revoke-handler.js";import{AdapterPool as X,PoolFullError as Z}from"./adapter-pool.js";import{parseSessionControlCommand as ee,handleSessionControlCommand as P,handleSessionControlLocalAction as L}from"./session-controller.js";import{SessionBindingStore as te}from"../core/persistence/session-binding-store.js";import{handleFileListAction as ie,handleCreateFolderAction as ne,realHomeDir as I}from"../core/files/index.js";import{AllowlistGate as se}from"../core/access/allowlist-gate.js";import{ActiveEventStore as oe}from"../core/persistence/active-event-store.js";import{DEFAULT_CONNECTOR_RUNTIME_CONFIG as re,applyConnectorRuntimeConfigPatch as de,extractConnectorRuntimeConfigPatch as ae}from"./runtime-config.js";import{SendController as ce}from"./send-controller.js";import{queryProviderQuota as y}from"../core/provider-quota/index.js";import{queryKiroQuota as B}from"../core/provider-quota/kiro.js";import{buildToolUseCard as w,buildToolResultCard as C,buildLocalGrixCardLink as le}from"./tool-card-utils.js";import{DeferredEventManager as ue}from"./deferred-events.js";const he=600*1e3,ge=60*1e3;class Ge{config;name;aibotHandle;aibotConfig;pool;stopped=!1;revokeHandler=new Y;sessionBindings=new Map;deferredMgr;sendCtrl=new ce(re);bindingStore;globalConfigStore;upgradeTrigger=null;allowlistGate;activeEventStore;cachedRateLimits=null;cachedRateLimitsSampledAtMs=null;cachedCodexContextWindow=null;cachedCodexTokenUsage=null;cachedCodexUsageSampledAtMs=null;cachedClaudeRateLimitState=null;cachedProviderQuota=null;cachedProviderQuotaSampledAtMs=null;conversationLog=null;eventSessionIndex=new Map;isRateLimitsCacheFresh(e){if(!Number.isFinite(e))return!1;const o=Number(e);return o>0&&Date.now()-o<=ge}async maybeQueryProviderQuota(){if(this.config.aibot.clientType==="kiro"){if(this.isRateLimitsCacheFresh(this.cachedProviderQuotaSampledAtMs)&&this.cachedProviderQuota)return this.cachedProviderQuota;try{const t=await B();return this.cachedProviderQuota=t,this.cachedProviderQuotaSampledAtMs=Date.now(),u.info(this.name,`[provider-quota] kiro queried: success=${t.success}`+(t.balance?` balance=${t.balance.remaining} ${t.balance.unit}`:"")+(t.planName?` plan=${t.planName}`:"")+(t.error?` error=${t.error}`:"")),t}catch(t){return u.warn(this.name,`[provider-quota] kiro query failed: ${t instanceof Error?t.message:String(t)}`),null}}let e=this.config.providerBaseUrl,o=this.config.providerApiKey;if((!e||!o)&&(this.config.adapterType??"acp")==="claude"){const t=K();e||(e=(t.ANTHROPIC_BASE_URL??"").trim()||void 0),o||(o=(t.ANTHROPIC_API_KEY??"").trim()||(t.ANTHROPIC_AUTH_TOKEN??"").trim()||void 0)}if((!e||!o)&&(this.config.adapterType??"acp")==="codex"){const t=j();!e&&t.baseUrl&&(e=t.baseUrl),!o&&t.apiKey&&(o=t.apiKey)}if(!e||!o)return null;if(this.isRateLimitsCacheFresh(this.cachedProviderQuotaSampledAtMs)&&this.cachedProviderQuota)return this.cachedProviderQuota;try{const t=await y(e,o);return this.cachedProviderQuota=t,this.cachedProviderQuotaSampledAtMs=Date.now(),u.info(this.name,`[provider-quota] queried: provider=${t.provider} success=${t.success}`+(t.tiers.length>0?` tiers=${t.tiers.map(s=>`${s.name}=${s.usedPercent}%`).join(",")}`:"")+(t.balance?` balance=${t.balance.remaining} ${t.balance.unit}`:"")+(t.error?` error=${t.error}`:"")),t}catch(t){return u.warn(this.name,`[provider-quota] query failed: ${t instanceof Error?t.message:String(t)}`),null}}getFreshClaudeRateLimitState(){const e=this.cachedClaudeRateLimitState;return e&&this.isRateLimitsCacheFresh(e.sampledAt)?e:null}getFreshCodexGlobalRateLimitCache(){const e=this.isRateLimitsCacheFresh(this.cachedRateLimitsSampledAtMs),o=this.isRateLimitsCacheFresh(this.cachedCodexUsageSampledAtMs),t=e?this.cachedRateLimits:null,s=o?this.cachedCodexContextWindow:null,i=o?this.cachedCodexTokenUsage:null;return{sampledAt:Math.max(e?this.cachedRateLimitsSampledAtMs??0:0,o?this.cachedCodexUsageSampledAtMs??0:0)||null,rateLimits:t,contextWindow:s,tokenUsage:i,hasData:!!(t||s||i)}}getStatus(){const e=this.pool?.getStatus()??{total:0,ready:0,busy:0};return{name:this.name,alive:!this.stopped,busy:e.busy>0,exhausted:this.pool?[...this.pool.getAllSlots()].some(o=>o.respawn.exhausted):!1,adapterType:this.config.adapterType??"acp",pool:e}}constructor(e,o){this.config=e,this.name=e.name;const t=e.adapterType??"acp";this.aibotConfig={...e.aibot,...t==="claude"?{localActions:e.aibot.localActions??["session_control","claude_interaction_reply","get_session_usage","get_rate_limits","set_model","set_mode","thread_compact","get_agent_global_config"]}:{}},e.eventQueue&&(this.aibotConfig.concurrency={max_concurrent:e.eventQueue.maxConcurrent,max_queued:e.eventQueue.maxQueued,queue_timeout_ms:e.eventQueue.queueTimeoutMs,cancelable_queued:e.eventQueue.cancelableQueued,cancelable_running:e.eventQueue.cancelableRunning}),this.conversationLog=e.logDir?new J(e.logDir):null,this.bindingStore=new te(e.bindingsPath),this.bindingStore.load(),this.globalConfigStore=o??null,this.deferredMgr=new ue(this.name),this.allowlistGate=e.allowlistPath?new se(e.allowlistPath):null,this.activeEventStore=e.activeEventStorePath?new oe(e.activeEventStorePath):null}async start(){(this.config.adapterType??"acp")==="claude"&&(await H().catch(()=>{}),this.maybeQueryProviderQuota().catch(()=>{})),this.config.aibot.clientType==="kiro"&&this.maybeQueryProviderQuota().catch(()=>{}),await this.connectAibot(),this.sendCtrl.bind(this.aibotHandle);const e=this.config.adapterType??"acp";this.pool=new X({maxPoolSize:this.config.poolMaxSize??20,idleTimeoutMs:this.config.poolIdleTimeoutMs??3e5,eventQueue:this.config.eventQueue},o=>{const t=this.createAdapter(e,o);return t instanceof b&&t.on("acpSessionReady",s=>{this.bindingStore.setAcpSessionId(o,s)}),t},(o,t)=>{this.aibotHandle.sendEventAck({event_id:o,session_id:t,received_at:Date.now()})}),this.pool.setEventStateHandler((o,t,s,i)=>{this.aibotHandle.sendEventState({event_id:o,session_id:t,state:s,content_preview:i?.content_preview,queue_position:i?.queue_position,queue_total:i?.queue_total,actions:i?.actions,reason:i?.reason,updated_at:Date.now()}),this.pushQueueSnapshotForSession(t),(s==="canceled"||s==="failed")&&this.aibotHandle.sendEventResult({event_id:o,status:s==="canceled"?"canceled":"failed",msg:i?.reason,updated_at:Date.now()})}),this.pool.startIdleSweep(),u.info(this.name,`Ready (adapter: ${e}, poolMax: ${this.config.poolMaxSize??20})`)}async stop(){this.stopped=!0,this.pool?.stopIdleSweep();const e=this.pool?.collectActiveEventIds()??[];e.length>0&&this.activeEventStore&&await this.activeEventStore.save(e);for(const t of e)u.info(this.name,`Canceling active event on shutdown: ${t}`),this.sendEventResultWithCleanup(t,"canceled","process shutting down");e.length>0&&await new Promise(t=>setTimeout(t,100)),this.pool?.clearActiveEventsForShutdown();const o=this.deferredMgr.getAllDeferredEvents();for(const t of o)u.info(this.name,`Failing deferred event on shutdown: ${t.event_id}`),this.sendEventResultWithCleanup(t.event_id,"failed","process shutting down");this.deferredMgr.clearAll(),await this.pool?.stop(),this.aibotHandle?.disconnect(),e.length>0&&this.activeEventStore&&await this.activeEventStore.save([]),this.eventSessionIndex.clear()}createAdapter(e,o){switch(e){case"claude":return this.createClaudeAdapter(o);case"codex":return this.createCodexAdapter(o);case"pi":return this.createPiAdapter(o);case"openhuman":return this.createOpenHumanAdapter(o);case"codewhale":return this.createCodeWhaleAdapter(o);case"cursor":return this.createCursorAdapter(o);case"opencode":return this.createOpenCodeAdapter(o);default:return this.createAcpAdapter(o)}}createCursorAdapter(e){const o={...this.config.adapterOptions??{}},t=this.globalConfigStore?.get(this.name);!o.model&&t?.modelId&&(o.model=t.modelId),!o.mode&&t?.modeId&&(o.mode=t.modeId);const s={sendStreamChunk:(i,n,r,d,a)=>{this.sendStreamChunkByRuntimeConfig(i,n,r,d,a)},sendEventResult:(i,n,r)=>{this.sendEventResultWithCleanup(i,n,r)},sendEventAck:(i,n)=>{this.aibotHandle.sendEventAck({event_id:i,session_id:n,received_at:Date.now()})},sendSessionComposing:(i,n)=>{this.aibotHandle.sendSessionActivitySet({session_id:i,kind:"composing",active:n})},sendRawEventEnvelope:(i,n,r)=>{this.aibotHandle.sendMsg({event_id:i,session_id:n,msg_type:1,content:"[cursor] raw_event",extra:{channel_data:{cursor:{raw_event:r}},agent_api_origin:!0}})},agentInvoke:async(i,n)=>this.aibotHandle.agentInvoke(i,n),sendLocalActionResult:(i,n,r,d,a)=>{this.aibotHandle.sendLocalActionResult({action_id:i,status:n,...r!==void 0?{result:r}:{},...d?{error_code:d}:{},...a?{error_msg:a}:{}})}};return new $({command:this.config.agent.command,args:this.config.agent.args,env:this.config.agent.env,options:o},s)}createClaudeAdapter(e){const o={sendReply:(n,r,d,a,c)=>{this.sendReplyByRuntimeConfig(n,r,d,a,c)},sendStreamChunk:(n,r,d,a,c,l,g)=>{this.sendStreamChunkByRuntimeConfig(n,r,d,a,c,l,g)},sendMedia:(n,r,d,a,c,l,g)=>{this.aibotHandle.sendMedia({event_id:n,session_id:r,content:d,msg_type:2,quoted_message_id:c||void 0,client_msg_id:l||void 0,extra:g?{media_caption:a,...g}:{media_caption:a}})},sendEventResult:(n,r,d,a)=>{this.sendEventResultWithCleanup(n,r,d,a)},sendEventAck:(n,r)=>{this.aibotHandle.sendEventAck({event_id:n,session_id:r,received_at:Date.now()})},agentInvoke:async(n,r,d)=>this.aibotHandle.agentInvoke(n,r,d),sendLocalActionResult:(n,r,d,a,c)=>{this.aibotHandle.sendLocalActionResult({action_id:n,status:r,...d!==void 0?{result:d}:{},...a?{error_code:a}:{},...c?{error_msg:c}:{}})},sendSessionComposing:(n,r,d,a)=>{this.aibotHandle.sendSessionActivitySet({session_id:n,kind:"composing",active:r,ttl_ms:r?3e4:void 0,ref_event_id:r?a:void 0,...d?{activity:d}:{}})},sendToolUse:(n,r,d,a)=>{this.sendToolExecutionCard(n,r,w(d,a))},sendToolResult:(n,r,d,a)=>{this.sendToolExecutionCard(n,r,C(d,a))},getWsUrl:()=>this.config.aibot.url,getAgentId:()=>this.config.aibot.agentId,getApiKey:()=>this.config.aibot.apiKey,getActiveEventCount:()=>0,getPendingPermissionCount:()=>0,getPendingElicitationCount:()=>0,sendAgentQuestionCard:(n,r,d)=>{const a=d.questions.map(l=>l.header).join(", "),c=le(`[Agent Question] ${d.request_id}`,"agent_question",d);this.aibotHandle.sendText({event_id:n,session_id:r,content:c,msg_type:1,extra:{card_type:"agent_question",summary_text:a}})},sendPermissionCard:n=>{this.aibotHandle.sendMsg({event_id:n.eventId,session_id:n.sessionId,msg_type:1,content:n.toolTitle?`Permission required: ${n.toolTitle}`:"Permission request",extra:{channel_data:{execApproval:{approvalId:n.approvalId,approvalSlug:n.toolName},grix:{execApproval:{approval_command_id:n.approvalId,command:n.toolTitle||n.toolName,host:"claude"}}},agent_api_origin:!0}})},getHandle:()=>this.aibotHandle,onStatusLineUpdated:n=>{(n.rateLimits?.fiveHour||n.rateLimits?.sevenDay)&&(this.cachedClaudeRateLimitState=n);const r=this.bindingStore.get(e);r?.cwd&&this.aibotHandle.sendUpdateBindingCard({session_id:e,worker_status:this.getClaudeWorkerStatus(e),cwd:r.cwd,meta:this.buildClaudeToolbarMeta(e)})}},t=this.config.adapterOptions??{},s=this.globalConfigStore?.get(this.name),i={...t,sessionRuntimeResolver:()=>{const n=this.bindingStore.get(e);return{cwd:n?.cwd,modeId:n?.modeId??s?.modeId??v.fullAuto,modelId:n?.modelId??s?.modelId,pluginDir:t.pluginDir,claudeSessionId:n?.claudeSessionId,onSessionIdAssigned:r=>{this.bindingStore.setClaudeSessionId(e,r)}}}};return new E({command:this.config.agent.command,args:this.config.agent.args,env:this.config.agent.env,options:i},o)}createCodexAdapter(e){let o=null;const t={sendEventResult:(n,r,d)=>{this.sendEventResultWithCleanup(n,r,d)},sendEventAck:(n,r)=>this.aibotHandle.sendEventAck({event_id:n,session_id:r,received_at:Date.now()}),sendCodexEvent:n=>{this.shouldDropCodexDisplayEvent(n.event_id,n.codex_method)||(this.aibotHandle.sendCodexEvent(n),this.logCodexEventToConversation(n))},sendCodexEventReliable:async n=>{if(!this.shouldDropCodexDisplayEvent(n.event_id,n.codex_method)){try{await this.aibotHandle.sendCodexEventReliable(n)}catch(r){u.warn("bridge",`[codex] sendCodexEventReliable ACK failed event=${n.event_id}: ${r}`)}u.info("bridge",`[codex] sendCodexEventReliable done event=${n.event_id} method=${n.codex_method}`),this.logCodexEventToConversation(n)}},sendUpdateBindingCard:(n,r,d,a)=>this.aibotHandle.sendUpdateBindingCard({session_id:n,worker_status:r,cwd:d,...a?{meta:a}:{}}),agentInvoke:async(n,r)=>this.aibotHandle.agentInvoke(n,r),sendLocalActionResult:(n,r,d,a,c)=>this.aibotHandle.sendLocalActionResult({action_id:n,status:r,...d!==void 0?{result:d}:{},...a?{error_code:a}:{},...c?{error_msg:c}:{}}),sendSessionActivitySet:(n,r,d,a)=>{this.aibotHandle.sendSessionActivitySet({session_id:n,kind:r,active:d,...a??{}})},getConversationLog:()=>this.conversationLog,onRateLimitsUpdated:n=>{this.cachedRateLimits=n,this.cachedRateLimitsSampledAtMs=Date.now();const r=this.bindingStore.get(e);if(r?.cwd){const d=this.cachedRateLimitsSampledAtMs;this.aibotHandle.sendUpdateBindingCard({session_id:e,worker_status:"ready",cwd:r.cwd,meta:{rate_limits:{primary:n.primary,secondary:n.secondary,sampledAt:d},rate_limit_primary_percent:n.primary.usedPercent,rate_limit_secondary_percent:n.secondary.usedPercent,rate_limit_primary_window_min:n.primary.windowMinutes,rate_limit_secondary_window_min:n.secondary.windowMinutes,...o?.getEffortMeta()}})}},onContextWindowUpdated:n=>{if(!n)return;this.cachedCodexContextWindow=n,this.cachedCodexUsageSampledAtMs=Date.now();const r=this.bindingStore.get(e);r?.cwd&&this.aibotHandle.sendUpdateBindingCard({session_id:e,worker_status:"ready",cwd:r.cwd,meta:{context_window:n,...o?.getEffortMeta()}})},onTokenUsageUpdated:n=>{n&&(this.cachedCodexTokenUsage=n,this.cachedCodexUsageSampledAtMs=Date.now())}},s=this.config.adapterOptions??{},i=this.globalConfigStore?.get(this.name);return o=new F({command:this.config.agent.command,args:this.config.agent.args,env:this.config.agent.env,options:{...s,model:i?.codexModelId??s.model,collaborationMode:i?.codexModeId??s.collaborationMode,reasoningEffort:i?.codexReasoningEffort??s.reasoningEffort,sandboxMode:i?.codexSandboxMode??s.sandboxMode,aibotSessionId:e,bindingStore:this.bindingStore}},t),o}createCodeWhaleAdapter(e){const o={sendEventResult:(s,i,n)=>{this.sendEventResultWithCleanup(s,i,n)},sendEventAck:(s,i)=>this.aibotHandle.sendEventAck({event_id:s,session_id:i,received_at:Date.now()}),sendStreamChunk:(s,i,n,r,d,a)=>{this.sendStreamChunkByRuntimeConfig(s,i,n,r,d,a)},sendUpdateBindingCard:(s,i,n,r)=>this.aibotHandle.sendUpdateBindingCard({session_id:s,worker_status:i,cwd:n,...r?{meta:r}:{}}),sendLocalActionResult:(s,i,n,r,d)=>this.aibotHandle.sendLocalActionResult({action_id:s,status:i,...n!==void 0?{result:n}:{},...r?{error_code:r}:{},...d?{error_msg:d}:{}}),sendSessionActivitySet:(s,i,n,r)=>{this.aibotHandle.sendSessionActivitySet({session_id:s,kind:i,active:n,...r??{}})},sendToolUse:(s,i,n,r)=>{this.sendToolExecutionCard(s,i,w(n,r))},sendToolResult:(s,i,n,r)=>{this.sendToolExecutionCard(s,i,C(n,r))},getConversationLog:()=>this.conversationLog},t=this.config.adapterOptions??{};return new D({command:this.config.agent.command,args:this.config.agent.args,env:this.config.agent.env,options:{...t,aibotSessionId:e,bindingStore:this.bindingStore}},o)}createPiAdapter(e){const o={sendEventResult:(s,i,n)=>{this.sendEventResultWithCleanup(s,i,n),u.info("bridge",`[pi] sendEventResult event=${s} status=${i}`)},sendEventAck:(s,i)=>this.aibotHandle.sendEventAck({event_id:s,session_id:i,received_at:Date.now()}),sendUpdateBindingCard:(s,i,n)=>this.aibotHandle.sendUpdateBindingCard({session_id:s,worker_status:i,cwd:n}),agentInvoke:async(s,i)=>this.aibotHandle.agentInvoke(s,i),sendLocalActionResult:(s,i,n,r,d)=>{this.aibotHandle.sendLocalActionResult({action_id:s,status:i,...n!==void 0?{result:n}:{},...r?{error_code:r}:{},...d?{error_msg:d}:{}})},sendSessionActivitySet:(s,i,n,r)=>{this.aibotHandle.sendSessionActivitySet({session_id:s,kind:i,active:n,...r??{}})},sendToolUse:(s,i,n,r)=>{this.sendToolExecutionCard(s,i,w(n,r))},sendToolResult:(s,i,n,r)=>{this.sendToolExecutionCard(s,i,C(n,r))},sendStreamChunk:(s,i,n,r,d,a)=>{this.sendStreamChunkByRuntimeConfig(s,i,n,r,d,a),d&&u.info("bridge",`[pi] sendFinalStreamChunk event=${s} seq=${r}`)},sendFinalStreamChunkReliable:async(s,i,n,r)=>{if(this.finalizeThinking(s,i),this.resolveEventRuntimeConfig(s).responseDelivery==="single_message"&&s){this.bufferStreamChunk(s,i,"",!0);return}try{await this.aibotHandle.sendStreamChunkRequest({event_id:s,session_id:i,delta_content:"",chunk_seq:n,is_finish:!0,...r?{client_msg_id:r}:{}})}catch(a){u.warn("bridge",`[pi] sendFinalStreamChunkReliable ACK failed event=${s}: ${a}`)}u.info("bridge",`[pi] sendFinalStreamChunkReliable done event=${s} seq=${n}`)},sendThinking:(s,i,n)=>{this.sendThinkingByRuntimeConfig(s,i,n)},sendRunError:(s,i,n,r,d)=>{this.sendStreamChunkByRuntimeConfig(s,i,`
1
+ import x from"node:path";import{realpath as U,stat as $}from"node:fs/promises";import{ConnectionManager as Q}from"../core/aibot/index.js";import{ClaudeAdapter as E}from"../adapter/claude/index.js";import{CodexAdapter as q}from"../adapter/codex/index.js";import{PiAdapter as F}from"../adapter/pi/index.js";import{AcpAdapter as b}from"../adapter/acp/index.js";import{OpenHumanAdapter as O}from"../adapter/openhuman/index.js";import{CursorAdapter as H}from"../adapter/cursor/index.js";import{CodeWhaleAdapter as D}from"../adapter/codewhale/index.js";import{OpenCodeAdapter as W}from"../adapter/opencode/index.js";import{LOCAL_ACTION_ERROR_CODES as A,LOCAL_ACTION_TYPES as p,SESSION_CONTROL_ERROR_CODES as h,SESSION_CONTROL_VERBS as m,SESSION_MODE_IDS as v}from"../adapter/claude/protocol-contract.js";import{parseClaudeSessionUsage as N}from"../adapter/claude/usage-parser.js";import{fetchAvailableModels as M,getCachedModels as L,readSettingsEnv as K}from"../adapter/claude/model-list.js";import{parseAcpSessionUsage as z}from"../adapter/acp/usage-parser.js";import{parseCodexSessionUsage as G}from"../adapter/codex/usage-parser.js";import{readCodexProviderSettings as j}from"../adapter/codex/codex-trust.js";import{parsePiSessionUsage as V}from"../adapter/pi/usage-parser.js";import{log as l,ConversationLog as J}from"../core/log/index.js";import{RevokeHandler as Y}from"./revoke-handler.js";import{AdapterPool as X,PoolFullError as Z}from"./adapter-pool.js";import{parseSessionControlCommand as ee,handleSessionControlCommand as P,handleSessionControlLocalAction as T}from"./session-controller.js";import{SessionBindingStore as te}from"../core/persistence/session-binding-store.js";import{handleFileListAction as ie,handleCreateFolderAction as ne,realHomeDir as I}from"../core/files/index.js";import{AllowlistGate as se}from"../core/access/allowlist-gate.js";import{ActiveEventStore as oe}from"../core/persistence/active-event-store.js";import{DEFAULT_CONNECTOR_RUNTIME_CONFIG as re,applyConnectorRuntimeConfigPatch as de,extractConnectorRuntimeConfigPatch as ae}from"./runtime-config.js";import{SendController as ce}from"./send-controller.js";import{queryProviderQuota as B}from"../core/provider-quota/index.js";import{queryKiroQuota as y}from"../core/provider-quota/kiro.js";import{buildToolUseCard as w,buildToolResultCard as C,buildLocalGrixCardLink as le}from"./tool-card-utils.js";import{DeferredEventManager as ue}from"./deferred-events.js";const he=600*1e3,ge=60*1e3;class Ge{config;name;aibotHandle;aibotConfig;pool;stopped=!1;revokeHandler=new Y;sessionBindings=new Map;deferredMgr;sendCtrl=new ce(re);bindingStore;globalConfigStore;upgradeTrigger=null;allowlistGate;activeEventStore;cachedRateLimits=null;cachedRateLimitsSampledAtMs=null;cachedCodexContextWindow=null;cachedCodexTokenUsage=null;cachedCodexUsageSampledAtMs=null;cachedClaudeRateLimitState=null;cachedProviderQuota=null;cachedProviderQuotaSampledAtMs=null;conversationLog=null;eventSessionIndex=new Map;isRateLimitsCacheFresh(e){if(!Number.isFinite(e))return!1;const o=Number(e);return o>0&&Date.now()-o<=ge}async maybeQueryProviderQuota(){if(this.config.aibot.clientType==="kiro"){if(this.isRateLimitsCacheFresh(this.cachedProviderQuotaSampledAtMs)&&this.cachedProviderQuota)return this.cachedProviderQuota;try{const t=await y();return this.cachedProviderQuota=t,this.cachedProviderQuotaSampledAtMs=Date.now(),l.info(this.name,`[provider-quota] kiro queried: success=${t.success}`+(t.balance?` balance=${t.balance.remaining} ${t.balance.unit}`:"")+(t.planName?` plan=${t.planName}`:"")+(t.error?` error=${t.error}`:"")),t}catch(t){return l.warn(this.name,`[provider-quota] kiro query failed: ${t instanceof Error?t.message:String(t)}`),null}}let e=this.config.providerBaseUrl,o=this.config.providerApiKey;if((!e||!o)&&(this.config.adapterType??"acp")==="claude"){const t=K();e||(e=(t.ANTHROPIC_BASE_URL??"").trim()||void 0),o||(o=(t.ANTHROPIC_API_KEY??"").trim()||(t.ANTHROPIC_AUTH_TOKEN??"").trim()||void 0)}if((!e||!o)&&(this.config.adapterType??"acp")==="codex"){const t=j();!e&&t.baseUrl&&(e=t.baseUrl),!o&&t.apiKey&&(o=t.apiKey)}if(!e||!o)return null;if(this.isRateLimitsCacheFresh(this.cachedProviderQuotaSampledAtMs)&&this.cachedProviderQuota)return this.cachedProviderQuota;try{const t=await B(e,o);return this.cachedProviderQuota=t,this.cachedProviderQuotaSampledAtMs=Date.now(),l.info(this.name,`[provider-quota] queried: provider=${t.provider} success=${t.success}`+(t.tiers.length>0?` tiers=${t.tiers.map(s=>`${s.name}=${s.usedPercent}%`).join(",")}`:"")+(t.balance?` balance=${t.balance.remaining} ${t.balance.unit}`:"")+(t.error?` error=${t.error}`:"")),t}catch(t){return l.warn(this.name,`[provider-quota] query failed: ${t instanceof Error?t.message:String(t)}`),null}}getFreshClaudeRateLimitState(){const e=this.cachedClaudeRateLimitState;return e&&this.isRateLimitsCacheFresh(e.sampledAt)?e:null}getFreshCodexGlobalRateLimitCache(){const e=this.isRateLimitsCacheFresh(this.cachedRateLimitsSampledAtMs),o=this.isRateLimitsCacheFresh(this.cachedCodexUsageSampledAtMs),t=e?this.cachedRateLimits:null,s=o?this.cachedCodexContextWindow:null,i=o?this.cachedCodexTokenUsage:null;return{sampledAt:Math.max(e?this.cachedRateLimitsSampledAtMs??0:0,o?this.cachedCodexUsageSampledAtMs??0:0)||null,rateLimits:t,contextWindow:s,tokenUsage:i,hasData:!!(t||s||i)}}getStatus(){const e=this.pool?.getStatus()??{total:0,ready:0,busy:0};return{name:this.name,alive:!this.stopped,busy:e.busy>0,exhausted:this.pool?[...this.pool.getAllSlots()].some(o=>o.respawn.exhausted):!1,adapterType:this.config.adapterType??"acp",pool:e}}constructor(e,o){this.config=e,this.name=e.name;const t=e.adapterType??"acp";this.aibotConfig={...e.aibot,...t==="claude"?{localActions:e.aibot.localActions??["session_control","claude_interaction_reply","get_session_usage","get_rate_limits","set_model","set_mode","thread_compact","get_agent_global_config"]}:{}},e.eventQueue&&(this.aibotConfig.concurrency={max_concurrent:e.eventQueue.maxConcurrent,max_queued:e.eventQueue.maxQueued,queue_timeout_ms:e.eventQueue.queueTimeoutMs,cancelable_queued:e.eventQueue.cancelableQueued,cancelable_running:e.eventQueue.cancelableRunning}),this.conversationLog=e.logDir?new J(e.logDir):null,this.bindingStore=new te(e.bindingsPath),this.bindingStore.load(),this.globalConfigStore=o??null,this.deferredMgr=new ue(this.name),this.allowlistGate=e.allowlistPath?new se(e.allowlistPath):null,this.activeEventStore=e.activeEventStorePath?new oe(e.activeEventStorePath):null}async start(){(this.config.adapterType??"acp")==="claude"&&(await M().catch(()=>{}),this.maybeQueryProviderQuota().catch(()=>{})),this.config.aibot.clientType==="kiro"&&this.maybeQueryProviderQuota().catch(()=>{}),await this.connectAibot(),this.sendCtrl.bind(this.aibotHandle);const e=this.config.adapterType??"acp";this.pool=new X({maxPoolSize:this.config.poolMaxSize??20,idleTimeoutMs:this.config.poolIdleTimeoutMs??3e5,eventQueue:this.config.eventQueue},o=>{const t=this.createAdapter(e,o);return t instanceof b&&t.on("acpSessionReady",s=>{this.bindingStore.setAcpSessionId(o,s)}),t},(o,t)=>{this.aibotHandle.sendEventAck({event_id:o,session_id:t,received_at:Date.now()})}),this.pool.setEventStateHandler((o,t,s,i)=>{this.aibotHandle.sendEventState({event_id:o,session_id:t,state:s,content_preview:i?.content_preview,queue_position:i?.queue_position,queue_total:i?.queue_total,actions:i?.actions,reason:i?.reason,updated_at:Date.now()}),this.pushQueueSnapshotForSession(t),(s==="canceled"||s==="failed")&&this.aibotHandle.sendEventResult({event_id:o,status:s==="canceled"?"canceled":"failed",msg:i?.reason,updated_at:Date.now()})}),this.pool.setQueueComposingHandler((o,t)=>{this.aibotHandle.sendSessionActivitySet({session_id:o,kind:"composing",active:t,...t?{ttl_ms:3e4}:{}})}),this.pool.startIdleSweep(),l.info(this.name,`Ready (adapter: ${e}, poolMax: ${this.config.poolMaxSize??20})`)}async stop(){this.stopped=!0,this.pool?.stopIdleSweep();const e=this.pool?.collectActiveEventIds()??[];e.length>0&&this.activeEventStore&&await this.activeEventStore.save(e);for(const t of e)l.info(this.name,`Canceling active event on shutdown: ${t}`),this.sendEventResultWithCleanup(t,"canceled","process shutting down");e.length>0&&await new Promise(t=>setTimeout(t,100)),this.pool?.clearActiveEventsForShutdown();const o=this.deferredMgr.getAllDeferredEvents();for(const t of o)l.info(this.name,`Failing deferred event on shutdown: ${t.event_id}`),this.sendEventResultWithCleanup(t.event_id,"failed","process shutting down");this.deferredMgr.clearAll(),await this.pool?.stop(),this.aibotHandle?.disconnect(),e.length>0&&this.activeEventStore&&await this.activeEventStore.save([]),this.eventSessionIndex.clear()}createAdapter(e,o){switch(e){case"claude":return this.createClaudeAdapter(o);case"codex":return this.createCodexAdapter(o);case"pi":return this.createPiAdapter(o);case"openhuman":return this.createOpenHumanAdapter(o);case"codewhale":return this.createCodeWhaleAdapter(o);case"cursor":return this.createCursorAdapter(o);case"opencode":return this.createOpenCodeAdapter(o);default:return this.createAcpAdapter(o)}}createCursorAdapter(e){const o={...this.config.adapterOptions??{}},t=this.globalConfigStore?.get(this.name);!o.model&&t?.modelId&&(o.model=t.modelId),!o.mode&&t?.modeId&&(o.mode=t.modeId);const s={sendStreamChunk:(i,n,r,d,a)=>{this.sendStreamChunkByRuntimeConfig(i,n,r,d,a)},sendEventResult:(i,n,r)=>{this.sendEventResultWithCleanup(i,n,r)},sendEventAck:(i,n)=>{this.aibotHandle.sendEventAck({event_id:i,session_id:n,received_at:Date.now()})},sendRawEventEnvelope:(i,n,r)=>{this.aibotHandle.sendMsg({event_id:i,session_id:n,msg_type:1,content:"[cursor] raw_event",extra:{channel_data:{cursor:{raw_event:r}},agent_api_origin:!0}})},agentInvoke:async(i,n)=>this.aibotHandle.agentInvoke(i,n),sendLocalActionResult:(i,n,r,d,a)=>{this.aibotHandle.sendLocalActionResult({action_id:i,status:n,...r!==void 0?{result:r}:{},...d?{error_code:d}:{},...a?{error_msg:a}:{}})}};return new H({command:this.config.agent.command,args:this.config.agent.args,env:this.config.agent.env,options:o},s)}createClaudeAdapter(e){const o={sendReply:(n,r,d,a,c)=>{this.sendReplyByRuntimeConfig(n,r,d,a,c)},sendStreamChunk:(n,r,d,a,c,u,g)=>{this.sendStreamChunkByRuntimeConfig(n,r,d,a,c,u,g)},sendMedia:(n,r,d,a,c,u,g)=>{this.aibotHandle.sendMedia({event_id:n,session_id:r,content:d,msg_type:2,quoted_message_id:c||void 0,client_msg_id:u||void 0,extra:g?{media_caption:a,...g}:{media_caption:a}})},sendEventResult:(n,r,d,a)=>{this.sendEventResultWithCleanup(n,r,d,a)},sendEventAck:(n,r)=>{this.aibotHandle.sendEventAck({event_id:n,session_id:r,received_at:Date.now()})},agentInvoke:async(n,r,d)=>this.aibotHandle.agentInvoke(n,r,d),sendLocalActionResult:(n,r,d,a,c)=>{this.aibotHandle.sendLocalActionResult({action_id:n,status:r,...d!==void 0?{result:d}:{},...a?{error_code:a}:{},...c?{error_msg:c}:{}})},sendToolUse:(n,r,d,a)=>{this.sendToolExecutionCard(n,r,w(d,a))},sendToolResult:(n,r,d,a)=>{this.sendToolExecutionCard(n,r,C(d,a))},getWsUrl:()=>this.config.aibot.url,getAgentId:()=>this.config.aibot.agentId,getApiKey:()=>this.config.aibot.apiKey,getActiveEventCount:()=>0,getPendingPermissionCount:()=>0,getPendingElicitationCount:()=>0,sendAgentQuestionCard:(n,r,d)=>{const a=d.questions.map(u=>u.header).join(", "),c=le(`[Agent Question] ${d.request_id}`,"agent_question",d);this.aibotHandle.sendText({event_id:n,session_id:r,content:c,msg_type:1,extra:{card_type:"agent_question",summary_text:a}})},sendPermissionCard:n=>{this.aibotHandle.sendMsg({event_id:n.eventId,session_id:n.sessionId,msg_type:1,content:n.toolTitle?`Permission required: ${n.toolTitle}`:"Permission request",extra:{channel_data:{execApproval:{approvalId:n.approvalId,approvalSlug:n.toolName},grix:{execApproval:{approval_command_id:n.approvalId,command:n.toolTitle||n.toolName,host:"claude"}}},agent_api_origin:!0}})},sendDirectMessage:n=>{this.aibotHandle.sendMsg({session_id:n.sessionId,msg_type:1,content:n.content,...n.clientMsgId?{client_msg_id:n.clientMsgId}:{},...n.quotedMessageId?{quoted_message_id:n.quotedMessageId}:{}})},onStatusLineUpdated:n=>{(n.rateLimits?.fiveHour||n.rateLimits?.sevenDay)&&(this.cachedClaudeRateLimitState=n);const r=this.bindingStore.get(e);r?.cwd&&this.aibotHandle.sendUpdateBindingCard({session_id:e,worker_status:this.getClaudeWorkerStatus(e),cwd:r.cwd,meta:this.buildClaudeToolbarMeta(e)})}},t=this.config.adapterOptions??{},s=this.globalConfigStore?.get(this.name),i={...t,sessionRuntimeResolver:()=>{const n=this.bindingStore.get(e);return{cwd:n?.cwd,modeId:n?.modeId??s?.modeId??v.fullAuto,modelId:n?.modelId??s?.modelId,pluginDir:t.pluginDir,claudeSessionId:n?.claudeSessionId,onSessionIdAssigned:r=>{this.bindingStore.setClaudeSessionId(e,r)}}}};return new E({command:this.config.agent.command,args:this.config.agent.args,env:this.config.agent.env,options:i},o)}createCodexAdapter(e){let o=null;const t={sendEventResult:(n,r,d)=>{this.sendEventResultWithCleanup(n,r,d)},sendEventAck:(n,r)=>this.aibotHandle.sendEventAck({event_id:n,session_id:r,received_at:Date.now()}),sendCodexEvent:n=>{this.shouldDropCodexDisplayEvent(n.event_id,n.codex_method)||(this.aibotHandle.sendCodexEvent(n),this.logCodexEventToConversation(n))},sendCodexEventReliable:async n=>{if(!this.shouldDropCodexDisplayEvent(n.event_id,n.codex_method)){try{await this.aibotHandle.sendCodexEventReliable(n)}catch(r){l.warn("bridge",`[codex] sendCodexEventReliable ACK failed event=${n.event_id}: ${r}`)}l.info("bridge",`[codex] sendCodexEventReliable done event=${n.event_id} method=${n.codex_method}`),this.logCodexEventToConversation(n)}},sendUpdateBindingCard:(n,r,d,a)=>this.aibotHandle.sendUpdateBindingCard({session_id:n,worker_status:r,cwd:d,...a?{meta:a}:{}}),agentInvoke:async(n,r)=>this.aibotHandle.agentInvoke(n,r),sendLocalActionResult:(n,r,d,a,c)=>this.aibotHandle.sendLocalActionResult({action_id:n,status:r,...d!==void 0?{result:d}:{},...a?{error_code:a}:{},...c?{error_msg:c}:{}}),sendSessionActivitySet:(n,r,d,a)=>{this.aibotHandle.sendSessionActivitySet({session_id:n,kind:r,active:d,...a??{}})},getConversationLog:()=>this.conversationLog,onRateLimitsUpdated:n=>{this.cachedRateLimits=n,this.cachedRateLimitsSampledAtMs=Date.now();const r=this.bindingStore.get(e);if(r?.cwd){const d=this.cachedRateLimitsSampledAtMs;this.aibotHandle.sendUpdateBindingCard({session_id:e,worker_status:"ready",cwd:r.cwd,meta:{rate_limits:{primary:n.primary,secondary:n.secondary,sampledAt:d},rate_limit_primary_percent:n.primary.usedPercent,rate_limit_secondary_percent:n.secondary.usedPercent,rate_limit_primary_window_min:n.primary.windowMinutes,rate_limit_secondary_window_min:n.secondary.windowMinutes,...o?.getEffortMeta()}})}},onContextWindowUpdated:n=>{if(!n)return;this.cachedCodexContextWindow=n,this.cachedCodexUsageSampledAtMs=Date.now();const r=this.bindingStore.get(e);r?.cwd&&this.aibotHandle.sendUpdateBindingCard({session_id:e,worker_status:"ready",cwd:r.cwd,meta:{context_window:n,...o?.getEffortMeta()}})},onTokenUsageUpdated:n=>{n&&(this.cachedCodexTokenUsage=n,this.cachedCodexUsageSampledAtMs=Date.now())}},s=this.config.adapterOptions??{},i=this.globalConfigStore?.get(this.name);return o=new q({command:this.config.agent.command,args:this.config.agent.args,env:this.config.agent.env,options:{...s,model:i?.codexModelId??s.model,collaborationMode:i?.codexModeId??s.collaborationMode,reasoningEffort:i?.codexReasoningEffort??s.reasoningEffort,sandboxMode:i?.codexSandboxMode??s.sandboxMode,aibotSessionId:e,bindingStore:this.bindingStore}},t),o}createCodeWhaleAdapter(e){const o={sendEventResult:(s,i,n)=>{this.sendEventResultWithCleanup(s,i,n)},sendEventAck:(s,i)=>this.aibotHandle.sendEventAck({event_id:s,session_id:i,received_at:Date.now()}),sendStreamChunk:(s,i,n,r,d,a)=>{this.sendStreamChunkByRuntimeConfig(s,i,n,r,d,a)},sendUpdateBindingCard:(s,i,n,r)=>this.aibotHandle.sendUpdateBindingCard({session_id:s,worker_status:i,cwd:n,...r?{meta:r}:{}}),sendLocalActionResult:(s,i,n,r,d)=>this.aibotHandle.sendLocalActionResult({action_id:s,status:i,...n!==void 0?{result:n}:{},...r?{error_code:r}:{},...d?{error_msg:d}:{}}),sendSessionActivitySet:(s,i,n,r)=>{this.aibotHandle.sendSessionActivitySet({session_id:s,kind:i,active:n,...r??{}})},sendToolUse:(s,i,n,r)=>{this.sendToolExecutionCard(s,i,w(n,r))},sendToolResult:(s,i,n,r)=>{this.sendToolExecutionCard(s,i,C(n,r))},agentInvoke:async(s,i)=>this.aibotHandle.agentInvoke(s,i),getConversationLog:()=>this.conversationLog},t=this.config.adapterOptions??{};return new D({command:this.config.agent.command,args:this.config.agent.args,env:this.config.agent.env,options:{...t,aibotSessionId:e,bindingStore:this.bindingStore}},o)}createPiAdapter(e){const o={sendEventResult:(s,i,n)=>{this.sendEventResultWithCleanup(s,i,n),l.info("bridge",`[pi] sendEventResult event=${s} status=${i}`)},sendEventAck:(s,i)=>this.aibotHandle.sendEventAck({event_id:s,session_id:i,received_at:Date.now()}),sendUpdateBindingCard:(s,i,n)=>this.aibotHandle.sendUpdateBindingCard({session_id:s,worker_status:i,cwd:n}),agentInvoke:async(s,i)=>this.aibotHandle.agentInvoke(s,i),sendLocalActionResult:(s,i,n,r,d)=>{this.aibotHandle.sendLocalActionResult({action_id:s,status:i,...n!==void 0?{result:n}:{},...r?{error_code:r}:{},...d?{error_msg:d}:{}})},sendSessionActivitySet:(s,i,n,r)=>{this.aibotHandle.sendSessionActivitySet({session_id:s,kind:i,active:n,...r??{}})},sendToolUse:(s,i,n,r)=>{this.sendToolExecutionCard(s,i,w(n,r))},sendToolResult:(s,i,n,r)=>{this.sendToolExecutionCard(s,i,C(n,r))},sendStreamChunk:(s,i,n,r,d,a)=>{this.sendStreamChunkByRuntimeConfig(s,i,n,r,d,a),d&&l.info("bridge",`[pi] sendFinalStreamChunk event=${s} seq=${r}`)},sendFinalStreamChunkReliable:async(s,i,n,r)=>{if(this.finalizeThinking(s,i),this.resolveEventRuntimeConfig(s).responseDelivery==="single_message"&&s){this.bufferStreamChunk(s,i,"",!0);return}try{await this.aibotHandle.sendStreamChunkRequest({event_id:s,session_id:i,delta_content:`
2
+ `,chunk_seq:n,is_finish:!0,...r?{client_msg_id:r}:{}})}catch(a){l.warn("bridge",`[pi] sendFinalStreamChunkReliable ACK failed event=${s}: ${a}`)}l.info("bridge",`[pi] sendFinalStreamChunkReliable done event=${s} seq=${n}`)},sendThinking:(s,i,n)=>{this.sendThinkingByRuntimeConfig(s,i,n)},sendRunError:(s,i,n,r,d)=>{this.sendStreamChunkByRuntimeConfig(s,i,`
2
3
 
3
- Error: ${n}`,r,!1,d)}},t=this.config.adapterOptions??{};return new q({command:this.config.agent.command,args:this.config.agent.args,env:this.config.agent.env,options:{...t,aibotSessionId:e,bindingStore:this.bindingStore}},o)}createOpenHumanAdapter(e){const o={sendStreamChunk:(s,i,n,r,d,a)=>{this.sendStreamChunkByRuntimeConfig(s,i,n,r,d,a)},sendFinalStreamChunkReliable:async(s,i,n,r)=>{if(this.finalizeThinking(s,i),this.resolveEventRuntimeConfig(s).responseDelivery==="single_message"&&s){this.bufferStreamChunk(s,i,"",!0);return}try{await this.aibotHandle.sendStreamChunkRequest({event_id:s,session_id:i,delta_content:"",chunk_seq:n,is_finish:!0,...r?{client_msg_id:r}:{}})}catch(a){u.warn("bridge",`[openhuman] sendFinalStreamChunkReliable ACK failed event=${s}: ${a}`)}},sendEventResult:(s,i,n)=>{this.sendEventResultWithCleanup(s,i,n),u.info("bridge",`[openhuman] sendEventResult event=${s} status=${i}`)},sendEventAck:(s,i)=>this.aibotHandle.sendEventAck({event_id:s,session_id:i,received_at:Date.now()}),sendToolUse:(s,i,n,r)=>{this.sendToolExecutionCard(s,i,w(n,r))},sendToolResult:(s,i,n,r)=>{this.sendToolExecutionCard(s,i,C(n,r))},sendThinking:(s,i,n)=>{this.sendThinkingByRuntimeConfig(s,i,n)},sendRunError:(s,i,n)=>{this.sendStreamChunkByRuntimeConfig(s,i,`
4
+ Error: ${n}`,r,!1,d)}},t=this.config.adapterOptions??{};return new F({command:this.config.agent.command,args:this.config.agent.args,env:this.config.agent.env,options:{...t,aibotSessionId:e,bindingStore:this.bindingStore}},o)}createOpenHumanAdapter(e){const o={sendStreamChunk:(s,i,n,r,d,a)=>{this.sendStreamChunkByRuntimeConfig(s,i,n,r,d,a)},sendFinalStreamChunkReliable:async(s,i,n,r)=>{if(this.finalizeThinking(s,i),this.resolveEventRuntimeConfig(s).responseDelivery==="single_message"&&s){this.bufferStreamChunk(s,i,"",!0);return}try{await this.aibotHandle.sendStreamChunkRequest({event_id:s,session_id:i,delta_content:`
5
+ `,chunk_seq:n,is_finish:!0,...r?{client_msg_id:r}:{}})}catch(a){l.warn("bridge",`[openhuman] sendFinalStreamChunkReliable ACK failed event=${s}: ${a}`)}},sendEventResult:(s,i,n)=>{this.sendEventResultWithCleanup(s,i,n),l.info("bridge",`[openhuman] sendEventResult event=${s} status=${i}`)},sendEventAck:(s,i)=>this.aibotHandle.sendEventAck({event_id:s,session_id:i,received_at:Date.now()}),sendToolUse:(s,i,n,r)=>{this.sendToolExecutionCard(s,i,w(n,r))},sendToolResult:(s,i,n,r)=>{this.sendToolExecutionCard(s,i,C(n,r))},sendThinking:(s,i,n)=>{this.sendThinkingByRuntimeConfig(s,i,n)},sendRunError:(s,i,n)=>{this.sendStreamChunkByRuntimeConfig(s,i,`
4
6
 
5
- Error: ${n}`,0,!1)},sendSessionComposing:(s,i)=>{this.aibotHandle.sendSessionActivitySet({session_id:s,kind:"composing",active:i,ttl_ms:i?3e4:void 0})},sendUpdateBindingCard:(s,i,n)=>this.aibotHandle.sendUpdateBindingCard({session_id:s,worker_status:i,cwd:n}),agentInvoke:async(s,i)=>this.aibotHandle.agentInvoke(s,i),sendLocalActionResult:(s,i,n,r,d)=>{this.aibotHandle.sendLocalActionResult({action_id:s,status:i,...n!==void 0?{result:n}:{},...r?{error_code:r}:{},...d?{error_msg:d}:{}})}},t=this.config.adapterOptions??{};return new O({command:this.config.agent.command,args:this.config.agent.args,env:this.config.agent.env,options:t},o,{port:t.port,host:t.host,workspaceDir:t.workspace_dir,sessionToken:t.session_token,enableSessionBinding:!0,aibotSessionId:e})}createOpenCodeAdapter(e){const o={sendStreamChunk:(s,i,n,r,d,a)=>{this.sendStreamChunkByRuntimeConfig(s,i,n,r,d,a)},sendFinalStreamChunkReliable:async(s,i,n,r)=>{if(this.finalizeThinking(s,i),this.resolveEventRuntimeConfig(s).responseDelivery==="single_message"&&s){this.bufferStreamChunk(s,i,"",!0);return}try{await this.aibotHandle.sendStreamChunkRequest({event_id:s,session_id:i,delta_content:"",chunk_seq:n,is_finish:!0,...r?{client_msg_id:r}:{}})}catch(a){u.warn("bridge",`[opencode] sendFinalStreamChunkReliable ACK failed event=${s}: ${a}`)}},sendEventResult:(s,i,n)=>{this.sendEventResultWithCleanup(s,i,n),u.info("bridge",`[opencode] sendEventResult event=${s} status=${i}`)},sendEventAck:(s,i)=>this.aibotHandle.sendEventAck({event_id:s,session_id:i,received_at:Date.now()}),sendToolUse:(s,i,n,r)=>{this.sendToolExecutionCard(s,i,w(n,r))},sendToolResult:(s,i,n,r)=>{this.sendToolExecutionCard(s,i,C(n,r))},sendThinking:(s,i,n)=>{this.sendThinkingByRuntimeConfig(s,i,n)},sendRunError:(s,i,n)=>{this.sendStreamChunkByRuntimeConfig(s,i,`
7
+ Error: ${n}`,0,!1)},sendUpdateBindingCard:(s,i,n)=>this.aibotHandle.sendUpdateBindingCard({session_id:s,worker_status:i,cwd:n}),agentInvoke:async(s,i)=>this.aibotHandle.agentInvoke(s,i),sendLocalActionResult:(s,i,n,r,d)=>{this.aibotHandle.sendLocalActionResult({action_id:s,status:i,...n!==void 0?{result:n}:{},...r?{error_code:r}:{},...d?{error_msg:d}:{}})}},t=this.config.adapterOptions??{};return new O({command:this.config.agent.command,args:this.config.agent.args,env:this.config.agent.env,options:t},o,{port:t.port,host:t.host,workspaceDir:t.workspace_dir,sessionToken:t.session_token,enableSessionBinding:!0,aibotSessionId:e})}createOpenCodeAdapter(e){const o={sendStreamChunk:(s,i,n,r,d,a)=>{this.sendStreamChunkByRuntimeConfig(s,i,n,r,d,a)},sendFinalStreamChunkReliable:async(s,i,n,r)=>{if(this.finalizeThinking(s,i),this.resolveEventRuntimeConfig(s).responseDelivery==="single_message"&&s){this.bufferStreamChunk(s,i,"",!0);return}try{await this.aibotHandle.sendStreamChunkRequest({event_id:s,session_id:i,delta_content:`
8
+ `,chunk_seq:n,is_finish:!0,...r?{client_msg_id:r}:{}})}catch(a){l.warn("bridge",`[opencode] sendFinalStreamChunkReliable ACK failed event=${s}: ${a}`)}},sendEventResult:(s,i,n)=>{this.sendEventResultWithCleanup(s,i,n),l.info("bridge",`[opencode] sendEventResult event=${s} status=${i}`)},sendEventAck:(s,i)=>this.aibotHandle.sendEventAck({event_id:s,session_id:i,received_at:Date.now()}),sendToolUse:(s,i,n,r)=>{this.sendToolExecutionCard(s,i,w(n,r))},sendToolResult:(s,i,n,r)=>{this.sendToolExecutionCard(s,i,C(n,r))},sendThinking:(s,i,n)=>{this.sendThinkingByRuntimeConfig(s,i,n)},sendRunError:(s,i,n)=>{this.sendStreamChunkByRuntimeConfig(s,i,`
6
9
 
7
- Error: ${n}`,0,!1)},sendSessionComposing:(s,i)=>{this.aibotHandle.sendSessionActivitySet({session_id:s,kind:"composing",active:i,ttl_ms:i?3e4:void 0})},sendUpdateBindingCard:(s,i,n)=>this.aibotHandle.sendUpdateBindingCard({session_id:s,worker_status:i,cwd:n}),agentInvoke:async(s,i)=>this.aibotHandle.agentInvoke(s,i),sendLocalActionResult:(s,i,n,r,d)=>{this.aibotHandle.sendLocalActionResult({action_id:s,status:i,...n!==void 0?{result:n}:{},...r?{error_code:r}:{},...d?{error_msg:d}:{}})}},t=this.config.adapterOptions??{};return new W({command:this.config.agent.command,args:this.config.agent.args,env:this.config.agent.env,options:t},o,{port:t.port,hostname:t.hostname,model:t.model,agent:t.agent,permissionPolicy:t.permission_policy,enableSessionBinding:!0,aibotSessionId:e})}createAcpAdapter(e){const o=this.isAcpRawTransportEnabled(),t={sendStreamChunk:(i,n,r,d,a,c)=>{this.sendStreamChunkByRuntimeConfig(i,n,r,d,a,c)},sendFinalStreamChunkReliable:async(i,n,r,d)=>{if(this.finalizeThinking(i,n),this.resolveEventRuntimeConfig(i).responseDelivery==="single_message"&&i){this.bufferStreamChunk(i,n,"",!0);return}try{await this.aibotHandle.sendStreamChunkRequest({event_id:i,session_id:n,delta_content:"",chunk_seq:r,is_finish:!0,...d?{client_msg_id:d}:{}})}catch(c){u.warn("bridge",`[acp] sendFinalStreamChunkReliable ACK failed event=${i}: ${c}`)}u.info("bridge",`[acp] sendFinalStreamChunkReliable done event=${i} seq=${r}`)},sendEventResult:(i,n,r)=>{this.sendEventResultWithCleanup(i,n,r)},sendEventAck:(i,n)=>{this.aibotHandle.sendEventAck({event_id:i,session_id:n,received_at:Date.now()})},agentInvoke:async(i,n)=>this.aibotHandle.agentInvoke(i,n),sendLocalActionResult:(i,n,r,d,a)=>{this.aibotHandle.sendLocalActionResult({action_id:i,status:n,...r!==void 0?{result:r}:{},...d?{error_code:d}:{},...a?{error_msg:a}:{}})},sendSessionComposing:(i,n)=>{this.aibotHandle.sendSessionActivitySet({session_id:i,kind:"composing",active:n,...n?{ttl_ms:3e4}:{}})},sendRawEventEnvelope:(i,n,r)=>{this.sendAcpRawEventEnvelope(i,n,r)},sendToolUse:(i,n,r,d)=>{if(o){this.sendAcpRawEventEnvelope(i,n,{type:"tool_use",payload:{tool_name:r,tool_input:d??""}});return}this.sendToolExecutionCard(i,n,w(r,d))},sendToolResult:(i,n,r,d)=>{this.sendToolExecutionCard(i,n,C(r,d))},sendThinking:(i,n,r)=>{this.sendThinkingByRuntimeConfig(i,n,r)},sendRunError:(i,n,r)=>{this.sendStreamChunkByRuntimeConfig(i,n,`
10
+ Error: ${n}`,0,!1)},sendUpdateBindingCard:(s,i,n)=>this.aibotHandle.sendUpdateBindingCard({session_id:s,worker_status:i,cwd:n}),agentInvoke:async(s,i)=>this.aibotHandle.agentInvoke(s,i),sendLocalActionResult:(s,i,n,r,d)=>{this.aibotHandle.sendLocalActionResult({action_id:s,status:i,...n!==void 0?{result:n}:{},...r?{error_code:r}:{},...d?{error_msg:d}:{}})}},t=this.config.adapterOptions??{};return new W({command:this.config.agent.command,args:this.config.agent.args,env:this.config.agent.env,options:t},o,{port:t.port,hostname:t.hostname,model:t.model,agent:t.agent,permissionPolicy:t.permission_policy,enableSessionBinding:!0,aibotSessionId:e})}createAcpAdapter(e){const o=this.isAcpRawTransportEnabled(),t={sendStreamChunk:(i,n,r,d,a,c)=>{this.sendStreamChunkByRuntimeConfig(i,n,r,d,a,c)},sendFinalStreamChunkReliable:async(i,n,r,d)=>{if(this.finalizeThinking(i,n),this.resolveEventRuntimeConfig(i).responseDelivery==="single_message"&&i){this.bufferStreamChunk(i,n,"",!0);return}try{await this.aibotHandle.sendStreamChunkRequest({event_id:i,session_id:n,delta_content:`
11
+ `,chunk_seq:r,is_finish:!0,...d?{client_msg_id:d}:{}})}catch(c){l.warn("bridge",`[acp] sendFinalStreamChunkReliable ACK failed event=${i}: ${c}`)}l.info("bridge",`[acp] sendFinalStreamChunkReliable done event=${i} seq=${r}`)},sendEventResult:(i,n,r)=>{this.sendEventResultWithCleanup(i,n,r)},sendEventAck:(i,n)=>{this.aibotHandle.sendEventAck({event_id:i,session_id:n,received_at:Date.now()})},agentInvoke:async(i,n)=>this.aibotHandle.agentInvoke(i,n),sendLocalActionResult:(i,n,r,d,a)=>{this.aibotHandle.sendLocalActionResult({action_id:i,status:n,...r!==void 0?{result:r}:{},...d?{error_code:d}:{},...a?{error_msg:a}:{}})},sendRawEventEnvelope:(i,n,r)=>{this.sendAcpRawEventEnvelope(i,n,r)},sendToolUse:(i,n,r,d)=>{if(o){this.sendAcpRawEventEnvelope(i,n,{type:"tool_use",payload:{tool_name:r,tool_input:d??""}});return}this.sendToolExecutionCard(i,n,w(r,d))},sendToolResult:(i,n,r,d)=>{this.sendToolExecutionCard(i,n,C(r,d))},sendThinking:(i,n,r)=>{this.sendThinkingByRuntimeConfig(i,n,r)},sendRunError:(i,n,r)=>{this.sendStreamChunkByRuntimeConfig(i,n,`
8
12
 
9
- Error: ${r}`,1,!1)},sendPermissionCard:i=>{if(o){this.sendAcpRawEventEnvelope(i.eventId,i.sessionId,{type:"permission_request",payload:{tool_call_id:i.toolCallId,tool_name:i.toolName,tool_title:i.toolTitle,options:i.options}});return}this.aibotHandle.sendMsg({event_id:i.eventId,session_id:i.sessionId,msg_type:1,content:i.toolTitle?`Permission required: ${i.toolTitle}`:"Permission request",extra:{channel_data:{execApproval:{approvalId:i.toolCallId,approvalSlug:i.toolName},grix:{execApproval:{approval_command_id:i.toolCallId,command:i.toolTitle||i.toolName,host:"acp"}}},agent_api_origin:!0}})},sendAuthNotification:(i,n)=>{i&&this.aibotHandle.sendMsg({session_id:i,msg_type:1,content:n,extra:{biz_card:{version:1,type:"agent_error",payload:{error:{name:"AuthRequired",message:n}}}}})},sendUpdateBindingCard:(i,n,r,d)=>{const a={...d??{}};if(!a.rate_limits&&this.cachedProviderQuota?.success){this.isRateLimitsCacheFresh(this.cachedProviderQuotaSampledAtMs)||this.maybeQueryProviderQuota().catch(()=>{});const c=this.providerQuotaToRateLimits(this.cachedProviderQuota);c&&(a.rate_limits=c)}!a.provider_quota&&this.cachedProviderQuota?.success&&(a.provider_quota=this.cachedProviderQuota),this.aibotHandle.sendUpdateBindingCard({session_id:i,worker_status:n,cwd:r,...Object.keys(a).length>0?{meta:a}:{}})}},s=this.globalConfigStore?.get(this.name);return new b({command:this.config.agent.command,args:this.config.agent.args,env:this.config.agent.env},t,{acpAuthMethod:this.config.acpAuthMethod,acpInitialMode:this.config.acpInitialMode??s?.acpInitialMode,acpMcpTools:this.config.acpMcpTools,rawTransport:o,eventResultsPath:this.config.eventResultsPath,approvalMode:this.config.approvalMode,bindingStore:this.config.enableSessionBinding?this.bindingStore:void 0,aibotSessionId:e,autoInjectArgs:this.config.autoInjectArgs})}async connectAibot(){const e=new Q;if(this.aibotHandle=await e.connect(this.aibotConfig,{aborted:()=>this.stopped,label:this.name}),this.aibotHandle.onEvent(o=>{this.handleAibotEvent(o).catch(t=>{u.error(this.name,`handleAibotEvent failed: ${t}`),this.aibotHandle.sendEventAck({event_id:o.event_id,session_id:o.session_id,received_at:Date.now()}),this.aibotHandle.sendEventResult({event_id:o.event_id,status:"failed",msg:t instanceof Error?t.message:String(t),updated_at:Date.now()})})}),this.aibotHandle.onStop(o=>{try{this.handleAibotStop(o)}catch(t){u.error(this.name,`handleAibotStop failed: ${t}`)}}),this.aibotHandle.onRevoke(o=>{try{this.handleAibotRevoke(o)}catch(t){u.error(this.name,`handleAibotRevoke failed: ${t}`)}}),this.aibotHandle.onLocalAction(o=>{this.handleAibotLocalAction(o).catch(t=>{u.error(this.name,`handleAibotLocalAction failed: ${t}`)})}),this.aibotHandle.onEventCancel(o=>{u.info(this.name,`recv event_cancel event_id=${o.event_id} session_id=${o.session_id}`),this.handleEventCancel(o).catch(t=>{u.error(this.name,`handleEventCancel failed: ${t}`)})}),this.aibotHandle.onQueueClear(o=>{const t=this.pool.clearQueue(o.session_id);this.aibotHandle.sendQueueClearResult({session_id:o.session_id,canceled_event_ids:t}),this.pushQueueSnapshotForSession(o.session_id)}),u.info(this.name,"Connected to aibot"),this.activeEventStore){const o=await this.activeEventStore.drain();if(o.length>0){u.warn(this.name,`Recovering ${o.length} stale event(s) from previous run`);for(const t of o)u.info(this.name,`Failing stale event on startup: ${t}`),this.aibotHandle.sendEventResult({event_id:t,status:"failed",msg:"process restarted, event lost",updated_at:Date.now()})}}this.pushQueueSnapshots()}pushQueueSnapshots(){if(!(!this.config.eventQueue||!this.pool))for(const e of this.pool.getAllSlots())this.pushQueueSnapshotForSession(e.sessionId)}pushQueueSnapshotForSession(e){if(!this.config.eventQueue||!this.pool)return;const o=this.pool.getQueueSnapshot(e);o&&this.aibotHandle.sendQueueSnapshot({session_id:e,running:o.running,running_items:o.running_items.map(t=>({event_id:t.event_id,...t.content_preview?{content_preview:t.content_preview}:{},...t.title?{title:t.title}:{},...t.summary?{summary:t.summary}:{}})),queued:o.queued.map(t=>({event_id:t.event_id,position:t.position,...t.content_preview?{content_preview:t.content_preview}:{},...t.title?{title:t.title}:{},...t.summary?{summary:t.summary}:{},actions:[{type:"cancel"}]}))})}sendReplyByRuntimeConfig(e,o,t,s,i){this.indexEventSession(e,o),this.sendCtrl.sendReply(e,o,t,s,i),t&&this.conversationLog?.logOutbound?.(o,e,"reply",t)}sendStreamChunkByRuntimeConfig(e,o,t,s,i,n,r){this.indexEventSession(e,o),this.sendCtrl.sendStreamChunk(e,o,t,s,i,n,r),(t||i)&&this.conversationLog?.logOutbound?.(o,e,i?"stream_chunk_finish":"stream_chunk",t)}sendEventResultWithCleanup(e,o,t,s){this.sendCtrl.sendEventResult(e,o,t,s);const i=this.eventSessionIndex.get(e);i&&(this.pool.eventComplete(e,i),this.pushQueueSnapshotForSession(i),this.conversationLog?.logResult?.(i,e,o,t),this.eventSessionIndex.delete(e)),o==="responded"&&(this.cachedProviderQuotaSampledAtMs=null,this.maybeQueryProviderQuota().catch(()=>{}))}sendThinkingByRuntimeConfig(e,o,t){this.sendCtrl.sendThinking(e,o,t)}bufferStreamChunk(e,o,t,s,i){this.sendCtrl.bufferOnly(e,o,t,s,i)}flushBufferedStreamText(e){}resolveEventRuntimeConfig(e){return this.sendCtrl.resolveEventRuntimeConfig(e)}captureEventRuntimeConfig(e){this.sendCtrl.captureEventRuntimeConfig(e),this.indexEventSession(e.event_id,e.session_id)}indexEventSession(e,o){!e||!o||this.eventSessionIndex.set(e,o)}shouldDropToolDisplayEvent(e){return this.sendCtrl.shouldDropToolDisplayEvent(e)}shouldDropThinkingDisplayEvent(e){return this.sendCtrl.shouldDropThinkingDisplayEvent(e)}shouldDropCodexDisplayEvent(e,o){return this.sendCtrl.shouldDropCodexDisplayEvent(e,o)}logCodexEventToConversation(e){if(!this.conversationLog||e.codex_method!=="item/agentMessage/delta")return;const t=e.codex_payload?.params?.delta;if(!t)return;const s=this.eventSessionIndex.get(e.event_id)??e.session_id;this.conversationLog.append(s,{ts:Date.now(),dir:"outbound",event_id:e.event_id,kind:"codex_delta",text_len:t.length,content:t})}isAcpRawTransportEnabled(){return(this.config.adapterOptions??{}).raw_transport===!0}shouldDropAcpRawDisplayEvent(e,o){return this.sendCtrl.shouldDropAcpRawDisplayEvent(e,o)}sendAcpRawEventEnvelope(e,o,t){this.shouldDropAcpRawDisplayEvent(e,t.type)||this.aibotHandle.sendMsg({event_id:e,session_id:o,msg_type:1,content:this.buildAcpRawEventFallbackText(t),extra:{channel_data:{acp:{raw_event:t}},agent_api_origin:!0}})}buildAcpRawEventFallbackText(e){const o=String(e.type??"").trim();if(!o)return"[acp] event";switch(o){case"permission_request":return`Permission required: ${String(e.payload?.tool_title??e.payload?.tool_name??"permission request")}`;case"tool_use":return`[tool] ${String(e.payload?.tool_name??"tool")}`;case"tool_result":return"[tool result]";case"thinking":return"[thinking]";case"error":return`[error] ${String(e.payload?.message??"agent error")}`;case"result":return"[result]";default:return`[acp] ${o}`}}sendToolExecutionCard(e,o,t,s){this.sendCtrl.sendToolExecutionCard(e,o,t,s)}async handleAibotEvent(e){if(this.stopped){this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendEventResult({event_id:e.event_id,status:"failed",msg:"agent shutting down",updated_at:Date.now()});return}this.logInboundConversation(e);const o=this.config.adapterType??"acp";if(this.allowlistGate&&e.sender_id&&!await this.allowlistGate.checkAccess(e.sender_id)){this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendEventResult({event_id:e.event_id,status:"responded",code:"access_denied",msg:`sender ${e.sender_id} is not authorized`,updated_at:Date.now()});return}const t=ae(e.extra);if(t.error){this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendEventResult({event_id:e.event_id,status:"failed",code:"connector_config_invalid",msg:t.error,updated_at:Date.now()});return}this.sendCtrl.setGlobalRuntimeConfig(de(this.sendCtrl.getGlobalRuntimeConfig(),t.patch));const s=ee(e);if(s){if(s.verb===m.exec){await this.handleSessionControlCommand(s,e);return}if(o==="claude"){await this.handleSessionControlCommand(s,e);return}if(o==="codex"&&s.verb===m.open){await this.handleCodexSessionControlOpen(s,e);return}if(o==="pi"&&s.verb===m.open){await this.handlePiSessionControlOpen(s,e);return}if(o==="pi"&&s.verb===m.restart){await this.handlePiSessionControlRestart(e);return}if((o==="openhuman"||o==="opencode")&&s.verb===m.open){await this.handleOpenHumanSessionControlOpen(s,e);return}if(o==="codewhale"&&s.verb===m.open){await this.handleCodeWhaleSessionControlOpen(s,e);return}if(this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),s.verb===m.open){const n=s.args.trim();if(!n){this.aibotHandle.sendEventResult({event_id:e.event_id,status:"failed",code:h.cwdRequired,msg:"cwd is required",updated_at:Date.now()});return}try{const r=x.resolve(n);if(!(await T(r)).isDirectory()){this.aibotHandle.sendEventResult({event_id:e.event_id,status:"failed",code:h.invalidCwd,msg:`Path is not a directory: ${r}`,updated_at:Date.now()});return}}catch(r){const d=String(r?.code??""),a=d==="ENOENT"?`Directory does not exist: ${x.resolve(n)}`:d==="EACCES"||d==="EPERM"?"Directory is not accessible":`Invalid path: ${n}`;this.aibotHandle.sendEventResult({event_id:e.event_id,status:"failed",code:h.invalidCwd,msg:a,updated_at:Date.now()});return}}if(await this.handleSessionControlForPool(s,e),o==="acp"&&s.verb===m.stop){const r=this.bindingStore.get(e.session_id)?.cwd??"";await this.pool.removeSlot(e.session_id).catch(()=>{}),this.aibotHandle.sendEventResult({event_id:e.event_id,status:"responded",msg:`Session worker stopped for ${r}`,updated_at:Date.now()});return}P(s,e,this.sessionControlCtx(e.session_id),{...this.sessionControlSenders(),sendEventAck:()=>{}}),s.verb===m.open&&(o==="pi"?await this.deferredMgr.replayDeferredPiEvents(e.session_id,this.deferredCallbacks()):o==="openhuman"?await this.deferredMgr.replayDeferredOpenHumanEvents(e.session_id,this.deferredCallbacks()):o==="opencode"?await this.deferredMgr.replayDeferredOpenCodeEvents(e.session_id,this.deferredCallbacks()):await this.deferredMgr.replayDeferredAcpEvents(e.session_id,this.deferredCallbacks()));return}if(e.mirror_mode==="record_only"){this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendEventResult({event_id:e.event_id,status:"responded",updated_at:Date.now()});return}if(o==="claude"){if(this.isStaleClaudeEvent(e)){this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendEventResult({event_id:e.event_id,status:"failed",code:"event_stale",msg:"event is stale and will not be processed",updated_at:Date.now()});return}if(!this.bindingStore.get(e.session_id)?.cwd){this.deferredMgr.deferClaudeEvent(String(e.session_id??"").trim(),this.buildInboundEvent(e));const r=this.resolveBindingChannelKey(o);this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendMsg({event_id:e.event_id,session_id:e.session_id,msg_type:1,content:"Session binding missing.",extra:{channel_data:{[r]:{sessionBinding:{status:"missing",reason:"binding_missing",error_code:h.bindingMissing}}}},quoted_message_id:e.msg_id});return}}if(o==="codex"&&!this.bindingStore.get(e.session_id)?.cwd){this.deferredMgr.deferCodexEvent(String(e.session_id??"").trim(),this.buildInboundEvent(e));const r=this.resolveBindingChannelKey(o);this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendMsg({event_id:e.event_id,session_id:e.session_id,msg_type:1,content:"Session binding missing.",extra:{channel_data:{[r]:{sessionBinding:{status:"missing",reason:"binding_missing",error_code:h.bindingMissing}}}},quoted_message_id:e.msg_id});return}if(o==="cursor"&&!this.bindingStore.get(e.session_id)?.cwd){this.deferredMgr.deferCursorEvent(String(e.session_id??"").trim(),this.buildInboundEvent(e));const r=this.resolveBindingChannelKey(o);this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendMsg({event_id:e.event_id,session_id:e.session_id,msg_type:1,content:"Session binding missing.",extra:{channel_data:{[r]:{sessionBinding:{status:"missing",reason:"binding_missing",error_code:h.bindingMissing}}}},quoted_message_id:e.msg_id});return}if(o==="codewhale"&&!this.bindingStore.get(e.session_id)?.cwd){this.deferredMgr.deferCodeWhaleEvent(String(e.session_id??"").trim(),this.buildInboundEvent(e));const r=this.resolveBindingChannelKey(o);this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendMsg({event_id:e.event_id,session_id:e.session_id,msg_type:1,content:"Session binding missing.",extra:{channel_data:{[r]:{sessionBinding:{status:"missing",reason:"binding_missing",error_code:h.bindingMissing}}}},quoted_message_id:e.msg_id});return}if(o==="opencode"&&!this.bindingStore.get(e.session_id)?.cwd){this.deferredMgr.deferOpenCodeEvent(String(e.session_id??"").trim(),this.buildInboundEvent(e));const r=this.resolveBindingChannelKey(o);this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendMsg({event_id:e.event_id,session_id:e.session_id,msg_type:1,content:"Session binding missing.",extra:{channel_data:{[r]:{sessionBinding:{status:"missing",reason:"binding_missing",error_code:h.bindingMissing}}}},quoted_message_id:e.msg_id});return}if(o==="pi"&&!this.bindingStore.get(e.session_id)?.cwd){this.deferredMgr.deferPiEvent(String(e.session_id??"").trim(),this.buildInboundEvent(e));const r=this.resolveBindingChannelKey(o);this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendMsg({event_id:e.event_id,session_id:e.session_id,msg_type:1,content:"Session binding missing.",extra:{channel_data:{[r]:{sessionBinding:{status:"missing",reason:"binding_missing",error_code:h.bindingMissing}}}},quoted_message_id:e.msg_id});return}if(o==="openhuman"&&!this.bindingStore.get(e.session_id)?.cwd){this.deferredMgr.deferOpenHumanEvent(String(e.session_id??"").trim(),this.buildInboundEvent(e));const r=this.resolveBindingChannelKey(o);this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendMsg({event_id:e.event_id,session_id:e.session_id,msg_type:1,content:"Session binding missing.",extra:{channel_data:{[r]:{sessionBinding:{status:"missing",reason:"binding_missing",error_code:h.bindingMissing}}}},quoted_message_id:e.msg_id});return}if(o==="acp"&&!this.bindingStore.get(e.session_id)?.cwd){this.deferredMgr.deferAcpEvent(String(e.session_id??"").trim(),this.buildInboundEvent(e));const r=this.resolveBindingChannelKey(o);this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendMsg({event_id:e.event_id,session_id:e.session_id,msg_type:1,content:"Session binding missing.",extra:{channel_data:{[r]:{sessionBinding:{status:"missing",reason:"binding_missing"}}}},quoted_message_id:e.msg_id});return}const i=this.buildInboundEvent(e);try{this.captureEventRuntimeConfig(i),await this.pool.deliverInboundEvent(i)}catch(n){if(this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),n instanceof Z)this.aibotHandle.sendEventResult({event_id:e.event_id,status:"failed",msg:n.message,updated_at:Date.now()});else throw n}}async handleCodexSessionControlOpen(e,o){const t=o.session_id,s=e.args.trim(),i=()=>this.aibotHandle.sendEventAck({event_id:o.event_id,session_id:t,received_at:Date.now()}),n=(r,d)=>this.aibotHandle.sendEventResult({event_id:o.event_id,status:r,...d,updated_at:Date.now()});if(!s){i(),n("failed",{msg:"Usage: /grix open <working-directory>",code:h.cwdRequired});return}try{const r=await this.resolveCwdForBinding(s),d=this.bindingStore.get(t);if(d?.cwd){if(await this.resolveCwdForBinding(d.cwd)!==r){i(),n("failed",{msg:`Session already bound to ${d.cwd}. Rebinding is not allowed.`,code:h.rebindForbidden});return}i(),n("responded",{msg:`Session already bound to ${d.cwd}`});return}this.bindingStore.set(t,r),this.sessionBindings.set(t,r),this.deferredMgr.sendCodexDeferredReplayComposing(t,this.deferredCallbacks()),await this.bindSessionForPool(t,r),await this.deferredMgr.replayDeferredCodexEvents(t,this.deferredCallbacks(),{announceComposing:!1}),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:"ready",cwd:r}),i(),n("responded",{msg:`Session bound to ${r}`})}catch(r){i(),n("failed",{code:h.invalidCwd,msg:r instanceof Error?r.message:String(r)})}}async handleSessionControlForPool(e,o){e.verb===m.open&&await this.bindSessionForPool(o.session_id,e.args.trim())}async handleSessionControlLocalActionForPool(e){if(String(e.action_type??"")!==p.sessionControl)return;const o=e.params??{};if(String(o.verb??"").trim().toLowerCase()!==m.open)return;const s=String(o.session_id??"").trim(),i=String(o.cwd??"").trim();!s||!i||await this.bindSessionForPool(s,i)}async handleCodexSessionControlLocalActionOpen(e){const o=e.params??{},t=String(o.session_id??"").trim(),s=String(o.cwd??"").trim(),i=(n,r,d,a)=>{this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:n,...r!==void 0?{result:r}:{},...d?{error_code:d}:{},...a?{error_msg:a}:{}})};if(!t||!s){i("failed",void 0,h.cwdRequired,"session cwd is required");return}try{const n=await this.resolveCwdForBinding(s),r=this.bindingStore.get(t);if(r?.cwd){if(await this.resolveCwdForBinding(r.cwd)!==n){i("failed",void 0,h.rebindForbidden,`Session already bound to ${r.cwd}`);return}i("ok",{outcome:"opened",cwd:r.cwd});return}this.bindingStore.set(t,n),this.sessionBindings.set(t,n),this.deferredMgr.sendCodexDeferredReplayComposing(t,this.deferredCallbacks()),await this.bindSessionForPool(t,n),await this.deferredMgr.replayDeferredCodexEvents(t,this.deferredCallbacks(),{announceComposing:!1}),i("ok",{outcome:"opened",cwd:n})}catch(n){i("failed",void 0,h.invalidCwd,n instanceof Error?n.message:String(n))}}async handleCursorSessionControlLocalActionOpen(e){const o=e.params??{},t=String(o.session_id??"").trim(),s=String(o.cwd??"").trim(),i=(n,r,d,a)=>{this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:n,...r!==void 0?{result:r}:{},...d?{error_code:d}:{},...a?{error_msg:a}:{}})};if(!t||!s){i("failed",void 0,h.cwdRequired,"session cwd is required");return}try{const n=await this.resolveCwdForBinding(s),r=this.bindingStore.get(t);if(r?.cwd){if(await this.resolveCwdForBinding(r.cwd)!==n){i("failed",void 0,h.rebindForbidden,`Session already bound to ${r.cwd}`);return}i("ok",{outcome:"opened",cwd:r.cwd});return}this.bindingStore.set(t,n),this.sessionBindings.set(t,n),this.deferredMgr.sendCursorDeferredReplayComposing(t,this.deferredCallbacks()),await this.bindSessionForPool(t,n),await this.deferredMgr.replayDeferredCursorEvents(t,this.deferredCallbacks(),{announceComposing:!1}),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:"ready",cwd:n}),i("ok",{outcome:"opened",cwd:n})}catch(n){i("failed",void 0,h.invalidCwd,n instanceof Error?n.message:String(n))}}async handlePiSessionControlOpen(e,o){const t=o.session_id,s=e.args.trim(),i=()=>this.aibotHandle.sendEventAck({event_id:o.event_id,session_id:t,received_at:Date.now()}),n=(r,d)=>this.aibotHandle.sendEventResult({event_id:o.event_id,status:r,...d,updated_at:Date.now()});if(!s){i(),n("failed",{msg:"Usage: /grix open <working-directory>",code:h.cwdRequired});return}try{const r=await this.resolveCwdForBinding(s),d=this.bindingStore.get(t);if(d?.cwd){if(await this.resolveCwdForBinding(d.cwd)!==r){i(),n("failed",{msg:`Session already bound to ${d.cwd}. Rebinding is not allowed.`,code:h.rebindForbidden});return}i(),n("responded",{msg:`Session already bound to ${d.cwd}`}),this.aibotHandle.sendMsg({event_id:o.event_id,session_id:t,msg_type:1,content:`\u2705 Session already bound to \`${d.cwd}\``,quoted_message_id:o.msg_id});return}this.bindingStore.set(t,r),this.sessionBindings.set(t,r),await this.deferredMgr.replayDeferredPiEvents(t,this.deferredCallbacks()),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:"ready",cwd:r}),i(),n("responded",{msg:`Session bound to ${r}`}),this.aibotHandle.sendMsg({event_id:o.event_id,session_id:t,msg_type:1,content:`\u2705 Working directory bound: \`${r}\``,quoted_message_id:o.msg_id})}catch(r){i(),n("failed",{code:h.invalidCwd,msg:r instanceof Error?r.message:String(r)})}}async handlePiSessionControlRestart(e){const o=e.session_id,t=()=>this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:o,received_at:Date.now()}),s=(r,d)=>this.aibotHandle.sendEventResult({event_id:e.event_id,status:r,...d,updated_at:Date.now()}),n=this.bindingStore.get(o)?.cwd??"";if(!n){t(),s("failed",{msg:"session binding was not found",code:h.bindingMissing});return}t(),await this.pool.removeSlot(o).catch(()=>{}),this.aibotHandle.sendUpdateBindingCard({session_id:o,worker_status:"ready",cwd:n}),s("responded",{msg:`Session worker restarted for ${n}`})}async handlePiSessionControlRestartLocalAction(e){const o=e.params??{},t=String(o.session_id??"").trim(),i=(t?this.bindingStore.get(t):void 0)?.cwd??"",n=(r,d,a,c)=>{this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:r,...d!==void 0?{result:d}:{},...a?{error_code:a}:{},...c?{error_msg:c}:{}})};if(!t){n("failed",void 0,"session_id_required","session_id is required for restart");return}if(!i){n("failed",void 0,h.bindingMissing,"Session binding missing. Open a workspace first.");return}await this.pool.removeSlot(t).catch(()=>{}),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:"ready",cwd:i}),n("ok",{outcome:"restarted",binding:{aibotSessionId:t,cwd:i,workerStatus:"ready"}})}async bindSessionForPool(e,o){const t=String(o??"").trim();if(!e||!t)return;const s=this.pool.getOrCreateSlot(e);if(!s||(s.startPromise&&await s.startPromise,!s.adapter))return;const i=s.adapter instanceof b?s.adapter:null;i?.hasSessionBinding&&(i.announceDeferredComposing(e),await i.bindSession(e,t),i.replayDeferredEvents(e))}deferredCallbacks(){return{captureEventRuntimeConfig:e=>this.captureEventRuntimeConfig(e),deliverInboundEvent:e=>this.pool.deliverInboundEvent(e),sendEventResult:(e,o,t)=>this.sendEventResultWithCleanup(e,o,t),sendSessionComposing:(e,o,t)=>{this.aibotHandle.sendSessionActivitySet({session_id:e,kind:"composing",active:o,...t?.ttlMs?{ttl_ms:t.ttlMs}:{},...t?.activity?{activity:t.activity}:{},...t?.refEventId?{ref_event_id:t.refEventId}:{},...t?.refMsgId?{ref_msg_id:t.refMsgId}:{}})}}}async handleOpenHumanSessionControlOpen(e,o){const t=()=>this.aibotHandle.sendEventAck({event_id:o.event_id,session_id:o.session_id,received_at:Date.now()});try{await this.handleSessionControlForPool(e,o),P(e,o,this.sessionControlCtx(o.session_id),this.sessionControlSenders()),this.deferredMgr.hasDeferred("openhuman",o.session_id)&&await this.deferredMgr.replayDeferredOpenHumanEvents(o.session_id,this.deferredCallbacks()),this.deferredMgr.hasDeferred("opencode",o.session_id)&&await this.deferredMgr.replayDeferredOpenCodeEvents(o.session_id,this.deferredCallbacks())}catch(s){t(),this.aibotHandle.sendEventResult({event_id:o.event_id,status:"failed",msg:s instanceof Error?s.message:String(s),updated_at:Date.now()})}}async handleCodeWhaleSessionControlOpen(e,o){const t=o.session_id,s=e.args.trim(),i=()=>this.aibotHandle.sendEventAck({event_id:o.event_id,session_id:t,received_at:Date.now()}),n=(r,d)=>this.aibotHandle.sendEventResult({event_id:o.event_id,status:r,...d,updated_at:Date.now()});if(!s){i(),n("failed",{msg:"Usage: /grix open <working-directory>",code:h.cwdRequired});return}try{const r=await this.resolveCwdForBinding(s),d=this.bindingStore.get(t);if(d?.cwd){if(await this.resolveCwdForBinding(d.cwd)!==r){i(),n("failed",{msg:`Session already bound to ${d.cwd}. Rebinding is not allowed.`,code:h.rebindForbidden});return}i(),n("responded",{msg:`Session already bound to ${d.cwd}`});return}this.bindingStore.set(t,r),this.sessionBindings.set(t,r),await this.bindSessionForPool(t,r),await this.deferredMgr.replayDeferredCodeWhaleEvents(t,this.deferredCallbacks(),{announceComposing:!1}),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:"ready",cwd:r}),i(),n("responded",{msg:`Session bound to ${r}`})}catch(r){i(),n("failed",{code:h.invalidCwd,msg:r instanceof Error?r.message:String(r)})}}async handleCodeWhaleSessionControlLocalActionOpen(e){const o=e.params??{},t=String(o.session_id??"").trim(),s=String(o.cwd??"").trim(),i=(n,r,d,a)=>{this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:n,...r!==void 0?{result:r}:{},...d?{error_code:d}:{},...a?{error_msg:a}:{}})};if(!t||!s){i("failed",void 0,h.cwdRequired,"session cwd is required");return}try{const n=await this.resolveCwdForBinding(s),r=this.bindingStore.get(t);if(r?.cwd){if(await this.resolveCwdForBinding(r.cwd)!==n){i("failed",void 0,h.rebindForbidden,`Session already bound to ${r.cwd}`);return}i("ok",{outcome:"opened",cwd:r.cwd});return}this.bindingStore.set(t,n),this.sessionBindings.set(t,n),await this.bindSessionForPool(t,n),await this.deferredMgr.replayDeferredCodeWhaleEvents(t,this.deferredCallbacks(),{announceComposing:!1}),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:"ready",cwd:n}),i("ok",{outcome:"opened",cwd:n})}catch(n){i("failed",void 0,h.invalidCwd,n instanceof Error?n.message:String(n))}}normalizeClaudeModeId(e){return String(e??"").trim().toLowerCase()===v.approval?v.approval:v.fullAuto}buildClaudeToolbarMeta(e){const o=this.bindingStore.get(e);if(!o)return;const t=M(),s=t.length>0?t[0].id:"",i=o.modelId||this.globalConfigStore?.get(this.name)?.modelId||s,n=this.normalizeClaudeModeId(o.modeId),r={model_id:i,mode_id:n,currentModelId:i,currentModeId:n,available_models:t.map(l=>({id:l.id,displayName:l.displayName}))},d=this.pool.getSlot(e),a=d?.adapter instanceof E?d.adapter.getSessionState():null;(a?.rateLimits?.fiveHour||a?.rateLimits?.sevenDay)&&(this.cachedClaudeRateLimitState=a);const c=a??this.getFreshClaudeRateLimitState();if(c){const l=c.rateLimits;l&&(l.fiveHour||l.sevenDay)&&(r.rate_limits={...l.fiveHour?{fiveHour:l.fiveHour}:{},...l.sevenDay?{sevenDay:l.sevenDay}:{},sampledAt:c.sampledAt||Date.now()});const g=c.contextWindow;g.usedPercentage!=null&&(r.context_window={usedPercentage:g.usedPercentage,remainingPercentage:g.remainingPercentage})}if(!r.rate_limits&&this.cachedProviderQuota?.success){this.isRateLimitsCacheFresh(this.cachedProviderQuotaSampledAtMs)||this.maybeQueryProviderQuota().catch(()=>{});const l=this.providerQuotaToRateLimits(this.cachedProviderQuota);l&&(r.rate_limits=l),u.info(this.name,`[toolbar-meta] provider quota fallback: hasCached=${!!this.cachedProviderQuota} fromProvider=${JSON.stringify(l)}`)}return r.rate_limits&&u.info(this.name,`[toolbar-meta] rate_limits included: ${JSON.stringify(r.rate_limits)}`),r}providerQuotaToRateLimits(e){const o=i=>{if(!i)return 0;const n=Date.parse(i);return Number.isFinite(n)?Math.floor(n/1e3):0},t=e.tiers.find(i=>i.name==="five_hour"),s=e.tiers.find(i=>i.name==="weekly_limit");if(t||s)return{...t?{fiveHour:{usedPercentage:t.usedPercent,resetsAt:o(t.resetsAt)}}:{},...s?{sevenDay:{usedPercentage:s.usedPercent,resetsAt:o(s.resetsAt)}}:{},sampledAt:this.cachedProviderQuotaSampledAtMs??Date.now()};if(e.balance){const i=e.balance;return{credit:{remaining:i.remaining,total:i.total,used:i.used,unit:i.unit},planName:e.planName,sampledAt:this.cachedProviderQuotaSampledAtMs??Date.now()}}return null}async resolveCwdForBinding(e){const o=String(e??"").trim();if(process.platform!=="win32"&&(/^[a-zA-Z]:[\\/]/.test(o)||/^\\\\/.test(o))){const i=new Error(`Specified path is not valid on this host: ${o}`);throw i.cwdErrorCode=h.invalidCwd,i}const t=x.resolve(o);let s;try{s=await T(t)}catch(i){const n=String(i?.code??"");if(n==="ENOENT"){const r=new Error(`Specified path does not exist: ${t}`);throw r.cwdErrorCode=h.invalidCwd,r}if(n==="EACCES"||n==="EPERM"){const r=new Error("Specified path is not accessible.");throw r.cwdErrorCode=h.invalidCwd,r}throw i}if(!s.isDirectory()){const i=new Error("Specified path is not a directory.");throw i.cwdErrorCode=h.invalidCwd,i}try{return await U(t)}catch{return t}}getClaudeWorkerStatus(e){const o=this.pool.getSlot(e);return o?o.state==="starting"?"starting":o.state==="stopped"?"stopped":o.adapter.getStatus().busy?"busy":"ready":"stopped"}async ensureClaudeSlotStarted(e){const o=this.pool.getOrCreateSlot(e);if(!o)throw new Error("Failed to allocate Claude session slot");if(o.startPromise){u.info(this.name,`ensureClaudeSlotStarted: awaiting startPromise for session=${e}`);const t=6e4;await Promise.race([o.startPromise,new Promise((s,i)=>setTimeout(()=>i(new Error(`ensureClaudeSlotStarted timeout (${t}ms) session=${e}`)),t))]),u.info(this.name,`ensureClaudeSlotStarted: startPromise resolved for session=${e}`)}}async handleSessionControlCommand(e,o){const t=o.session_id,s=(i,n,r)=>{this.aibotHandle.sendEventResult({event_id:o.event_id,status:i,...n?{msg:n}:{},...r?{code:r}:{},updated_at:Date.now()})};this.aibotHandle.sendEventAck({event_id:o.event_id,session_id:t,received_at:Date.now()});try{switch(e.verb){case m.open:{await H().catch(()=>{});const i=e.args.trim();if(!i){s("failed","Usage: /grix open <working-directory>",h.cwdRequired);return}let n="";try{n=await this.resolveCwdForBinding(i)}catch(d){s("failed",d instanceof Error?d.message:String(d),h.invalidCwd);return}const r=this.bindingStore.get(t);if(r?.cwd){const d=await this.resolveCwdForBinding(r.cwd);if(d!==n){s("failed","session binding cannot be changed to another working directory",h.rebindForbidden);return}this.bindingStore.ensureModeId(t,this.globalConfigStore?.get(this.name)?.modeId??v.fullAuto),this.sessionBindings.set(t,d),await this.ensureClaudeSlotStarted(t),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:this.getClaudeWorkerStatus(t),cwd:d,meta:this.buildClaudeToolbarMeta(t)}),await this.deferredMgr.replayDeferredClaudeEvents(t,this.deferredCallbacks()),s("responded",`Working directory already bound: ${d}`);return}this.bindingStore.set(t,n,{modeId:this.globalConfigStore?.get(this.name)?.modeId??v.fullAuto}),this.sessionBindings.set(t,n),await this.ensureClaudeSlotStarted(t),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:this.getClaudeWorkerStatus(t),cwd:n,meta:this.buildClaudeToolbarMeta(t)}),await this.deferredMgr.replayDeferredClaudeEvents(t,this.deferredCallbacks()),s("responded",`Session bound to ${n}`);return}case m.where:{const i=this.bindingStore.get(t);if(!i?.cwd){s("failed","session binding was not found",h.bindingMissing);return}s("responded",`Working directory: ${i.cwd}`);return}case m.status:{const i=this.bindingStore.get(t);if(!i?.cwd){s("failed","session binding was not found",h.bindingMissing);return}const n=this.normalizeClaudeModeId(i.modeId),r=this.getClaudeWorkerStatus(t);s("responded",`Status: worker=${r} mode=${n} cwd=${i.cwd}`);return}case m.stop:{const i=this.bindingStore.get(t);if(!i?.cwd){s("failed","session binding was not found",h.bindingMissing);return}await this.pool.removeSlot(t),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:"stopped",cwd:i.cwd}),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:"stopped",cwd:i.cwd,meta:this.buildClaudeToolbarMeta(t)}),s("responded",`Session worker stopped for ${i.cwd}`);return}case m.restart:{const i=this.bindingStore.get(t);if(!i?.cwd){s("failed","session binding was not found",h.bindingMissing);return}await this.pool.removeSlot(t),await this.ensureClaudeSlotStarted(t),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:this.getClaudeWorkerStatus(t),cwd:i.cwd,meta:this.buildClaudeToolbarMeta(t)}),s("responded",`Session worker restarted for ${i.cwd}`);return}case m.setMode:{const i=e.args.trim();if(!i){s("failed","Usage: /grix set_mode <mode-id>",A.modeInvalid);return}const n=this.normalizeClaudeModeId(i);if(n!==i.toLowerCase()){s("failed","set mode_id is invalid",A.modeInvalid);return}const r=this.bindingStore.get(t);if(!r?.cwd){s("failed","session binding was not found",h.bindingMissing);return}if(this.normalizeClaudeModeId(r.modeId)===n){s("responded",`Mode unchanged: ${n}`);return}this.bindingStore.setModeId(t,n),this.globalConfigStore?.set(this.name,{modeId:n}),await this.pool.removeSlot(t),await this.ensureClaudeSlotStarted(t),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:this.getClaudeWorkerStatus(t),cwd:r.cwd,meta:this.buildClaudeToolbarMeta(t)}),s("responded",`Mode set to ${n}`);return}case m.setModel:{const i=e.args.trim();if(!i){s("failed","Usage: /grix set_model <model-id>");return}const n=this.bindingStore.get(t);if(!n?.cwd){s("failed","session binding was not found",h.bindingMissing);return}if((n.modelId??"")===i){s("responded",`Model unchanged: ${i}`);return}this.bindingStore.setModelId(t,i),this.globalConfigStore?.set(this.name,{modelId:i}),await this.pool.removeSlot(t),await this.ensureClaudeSlotStarted(t),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:this.getClaudeWorkerStatus(t),cwd:n.cwd,meta:this.buildClaudeToolbarMeta(t)}),s("responded",`Model set to ${i}`);return}case m.listOptions:{const i=M(),n=this.bindingStore.get(t),r=i.map(a=>a.id).join(", "),d=`${v.fullAuto}, ${v.approval}`;s("responded",`Modes (current: ${this.normalizeClaudeModeId(n?.modeId)}): ${d}
10
- Models (current: ${n?.modelId??"default"}): ${r}`);return}case m.exec:{const[i,...n]=e.args.trim().split(/\s+/);if(!i){s("failed","Usage: /grix exec <command> [args]",h.verbInvalid);return}const d=this.pool.getSlot(t)?.adapter;if(!d?.execCommand){s("failed","Agent does not support command execution",h.verbInvalid);return}const a=d.getSupportedCommands?.()??[];if(!a.some(c=>c.name===i)){s("failed",`Unknown command: ${i}. Supported: ${a.map(c=>c.name).join(", ")}`,h.verbInvalid);return}try{const c=await d.execCommand(i,n.join(" "),t);s(c.status==="ok"?"responded":"failed",c.message??`${i} ${c.status}`,c.status==="ok"?void 0:h.runtimeError)}catch(c){s("failed",`exec error: ${c instanceof Error?c.message:c}`,h.runtimeError)}return}default:s("failed",`Unsupported command for Claude: /grix ${e.verb}`,h.verbInvalid)}}catch(i){s("failed",i instanceof Error?i.message:String(i),h.runtimeError)}}async handleSessionControlLocalAction(e){const o=String(e.action_type??"").trim();if(o!==p.sessionControl&&o!==p.setMode&&o!=="set_mode"&&o!==p.setModel&&o!=="set_model")return!1;const t=e.params??{},s=String(t.session_id??"").trim(),i=o===p.setMode?m.setMode:o===p.setModel?m.setModel:String(t.verb??"").trim().toLowerCase();if(u.info(this.name,`handleSessionControlLocalAction verb=${i} action_id=${e.action_id} session_id=${s}`),!s)return this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:A.localActionRouteMissing,error_msg:"local action session_id is required"}),!0;const n=d=>{this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:d})},r=(d,a)=>{u.warn(this.name,`session_control local_action failed action_id=${e.action_id} session_id=${s} verb=${i} code=${d} msg=${a}`),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:d,error_msg:a})};try{switch(i){case m.open:{await H().catch(()=>{});const d=String(t.cwd??"").trim();if(!d)return r(h.cwdRequired,"session control cwd is required"),!0;u.info(this.name,`handleSessionControlLocalAction open cwd=${d} session_id=${s}`);const a=await this.resolveCwdForBinding(d),c=this.bindingStore.get(s);if(c?.cwd){const l=await this.resolveCwdForBinding(c.cwd);return l!==a?(r(h.rebindForbidden,"session binding cannot be changed to another working directory"),!0):(this.bindingStore.ensureModeId(s,this.globalConfigStore?.get(this.name)?.modeId??v.fullAuto),this.sessionBindings.set(s,l),await this.ensureClaudeSlotStarted(s),this.aibotHandle.sendUpdateBindingCard({session_id:s,worker_status:this.getClaudeWorkerStatus(s),cwd:l,meta:this.buildClaudeToolbarMeta(s)}),await this.deferredMgr.replayDeferredClaudeEvents(s,this.deferredCallbacks()),n({outcome:"already_bound",binding:{cwd:l,mode_id:this.normalizeClaudeModeId(this.bindingStore.get(s)?.modeId)}}),!0)}return this.bindingStore.set(s,a,{modeId:this.globalConfigStore?.get(this.name)?.modeId??v.fullAuto}),this.sessionBindings.set(s,a),await this.ensureClaudeSlotStarted(s),this.aibotHandle.sendUpdateBindingCard({session_id:s,worker_status:this.getClaudeWorkerStatus(s),cwd:a,meta:this.buildClaudeToolbarMeta(s)}),await this.deferredMgr.replayDeferredClaudeEvents(s,this.deferredCallbacks()),n({outcome:"opened",binding:{cwd:a,mode_id:this.normalizeClaudeModeId(this.bindingStore.get(s)?.modeId)}}),!0}case m.status:case m.where:{const d=this.bindingStore.get(s);return d?.cwd?(n({outcome:i,binding:{cwd:d.cwd,mode_id:this.normalizeClaudeModeId(d.modeId),worker_status:this.getClaudeWorkerStatus(s)}}),!0):(r(h.bindingMissing,"session binding was not found"),!0)}case m.stop:{const d=this.bindingStore.get(s);return d?.cwd?(await this.pool.removeSlot(s),n({outcome:"stopped",binding:{cwd:d.cwd,mode_id:this.normalizeClaudeModeId(d.modeId),worker_status:"stopped"}}),!0):(r(h.bindingMissing,"session binding was not found"),!0)}case m.restart:{const d=this.bindingStore.get(s);return d?.cwd?(await this.pool.removeSlot(s),await this.ensureClaudeSlotStarted(s),this.aibotHandle.sendUpdateBindingCard({session_id:s,worker_status:this.getClaudeWorkerStatus(s),cwd:d.cwd,meta:this.buildClaudeToolbarMeta(s)}),n({outcome:"restarted",binding:{cwd:d.cwd,mode_id:this.normalizeClaudeModeId(d.modeId)}}),!0):(r(h.bindingMissing,"session binding was not found"),!0)}case m.setMode:{const d=String(t.mode_id??t.modeId??"").trim();if(!d)return r(A.modeInvalid,"set mode_id is invalid"),!0;const a=this.normalizeClaudeModeId(d);if(a!==d.toLowerCase())return r(A.modeInvalid,"set mode_id is invalid"),!0;const c=this.bindingStore.get(s);return c?.cwd?(this.normalizeClaudeModeId(c.modeId)!==a&&(this.bindingStore.setModeId(s,a),this.globalConfigStore?.set(this.name,{modeId:a}),await this.pool.removeSlot(s),await this.ensureClaudeSlotStarted(s),this.aibotHandle.sendUpdateBindingCard({session_id:s,worker_status:this.getClaudeWorkerStatus(s),cwd:c.cwd,meta:this.buildClaudeToolbarMeta(s)})),n({outcome:"mode_set",mode_id:a,binding:{cwd:c.cwd,mode_id:a}}),!0):(r(h.bindingMissing,"session binding was not found"),!0)}case m.setModel:{const d=String(t.model_id??t.modelId??"").trim();if(!d)return r("set_model_invalid","model_id is required"),!0;const a=this.bindingStore.get(s);return a?.cwd?((a.modelId??"")!==d&&(this.bindingStore.setModelId(s,d),this.globalConfigStore?.set(this.name,{modelId:d}),await this.pool.removeSlot(s),await this.ensureClaudeSlotStarted(s),this.aibotHandle.sendUpdateBindingCard({session_id:s,worker_status:this.getClaudeWorkerStatus(s),cwd:a.cwd,meta:this.buildClaudeToolbarMeta(s)})),n({outcome:"model_set",model_id:d,binding:{cwd:a.cwd,model_id:d}}),!0):(r(h.bindingMissing,"session binding was not found"),!0)}case m.listOptions:{const d=M(),a=this.pool.getSlot(s);return n({modes:[v.fullAuto,v.approval],currentModeId:this.normalizeClaudeModeId(this.bindingStore.get(s)?.modeId),models:d.map(c=>({modelId:c.id,name:c.displayName})),currentModelId:this.bindingStore.get(s)?.modelId??"",available_models:d.map(c=>({id:c.id,displayName:c.displayName})),agent_commands:a?.adapter?.getSupportedCommands?.()??[]}),!0}case m.exec:{const[d,...a]=String(t.args??"").trim().split(/\s+/);if(!d)return r(h.verbInvalid,"Usage: exec <command> [args]"),!0;await this.ensureClaudeSlotStarted(s);const l=this.pool.getSlot(s)?.adapter;if(!l?.execCommand)return r(h.verbInvalid,"Agent does not support command execution"),!0;const g=l.getSupportedCommands?.()??[];if(!g.some(_=>_.name===d))return r(h.verbInvalid,`Unknown command: ${d}. Supported: ${g.map(_=>_.name).join(", ")}`),!0;try{const _=await l.execCommand(d,a.join(" "),s);_.status==="ok"?n({outcome:"exec",command:d,message:_.message,data:_.data}):r(h.runtimeError,_.message??`${d} failed`)}catch(_){r(h.runtimeError,`exec error: ${_ instanceof Error?_.message:_}`)}return!0}default:return r(h.verbInvalid,`session control verb ${i} is not supported`),!0}}catch(d){const a=d instanceof Error&&d.cwdErrorCode?d.cwdErrorCode:h.runtimeError;return u.error(this.name,`handleSessionControlLocalAction error verb=${i} session_id=${s}: ${d instanceof Error?d.message:d}`),r(a,d instanceof Error?d.message:String(d)),!0}}async handleEventCancel(e){const{event_id:o,session_id:t}=e;if(u.info(this.name,`handleEventCancel start event_id=${o} session_id=${t}`),this.pool.cancelEvent(o,t)){if(!(this.pool.getSlot(t)?.adapter?.getActiveEventIds().includes(o)??!1)){this.aibotHandle.sendEventCancelResult({event_id:o,accepted:!0,final_state:"canceled"}),this.pushQueueSnapshotForSession(t);return}await this.waitForEventDone(o,t,15e3),this.aibotHandle.sendEventCancelResult({event_id:o,accepted:!0,final_state:"canceled"}),this.pushQueueSnapshotForSession(t);return}this.aibotHandle.sendEventCancelResult({event_id:o,accepted:!1,reason:"event not found or not cancelable"})}waitForEventDone(e,o,t){return new Promise(s=>{const i=this.pool.getSlot(o);if(!i?.adapter){s();return}const n=setTimeout(()=>{i.adapter.removeListener("eventDone",r),s()},t),r=d=>{d===e&&(clearTimeout(n),i.adapter.removeListener("eventDone",r),s())};i.adapter.on("eventDone",r)})}handleAibotStop(e){this.aibotHandle.sendEventStopAck({stop_id:e.stop_id,event_id:e.event_id,accepted:!0,updated_at:Date.now()});const o=this.pool.getSlot(e.session_id),t=o?.adapter?.getStatus().busy??!1;if(o?.adapter&&t){const s=i=>{i===e.event_id&&(o.adapter.removeListener("eventDone",s),this.aibotHandle.sendEventStopResult({stop_id:e.stop_id,event_id:e.event_id,status:"stopped",updated_at:Date.now()}))};o.adapter.on("eventDone",s),this.pool.deliverStopEvent(e.event_id,e.session_id)}else this.pool.deliverStopEvent(e.event_id,e.session_id),this.aibotHandle.sendEventStopResult({stop_id:e.stop_id,event_id:e.event_id,status:"stopped",updated_at:Date.now()})}handleAibotRevoke(e){if(!e.event_id||!this.revokeHandler.checkAndTrack(e.event_id))return;const o=e.event_id,t=e.session_id;if(t&&this.pool.cancelEvent(o,t)){(this.pool.getSlot(t)?.adapter?.getActiveEventIds().includes(o)??!1)||this.sendEventResultWithCleanup(o,"canceled","revoked");return}if(this.deferredMgr.removeEvent(o)){this.aibotHandle.sendEventResult({event_id:o,status:"canceled",msg:"revoked",updated_at:Date.now()});return}this.pool.deliverStopEvent(o,t||void 0)}async handleAibotLocalAction(e){const o=e.action_type??"",t=String((e.params??{}).session_id??""),s=String((e.params??{}).verb??"").trim().toLowerCase();u.debug(this.name,`local_action received action_type=${o} verb=${s||"-"} action_id=${e.action_id} session_id=${t}`);const i=(this.config.adapterType??"acp")==="claude";if(o===p.sessionControl&&s===m.exec&&await this.handleSessionControlLocalAction(e)||i&&await this.handleSessionControlLocalAction(e))return;if(o===p.sessionControl){const a=this.config.adapterType??"acp",c=a==="codex",l=a==="pi",g=String((e.params??{}).verb??"").trim().toLowerCase();if(c&&g===m.open){await this.handleCodexSessionControlLocalActionOpen(e);return}if(a==="cursor"&&g===m.open){await this.handleCursorSessionControlLocalActionOpen(e);return}if(c&&g==="restart"){const R=this.bindingStore.get(t)?.cwd??"";await this.pool.removeSlot(t).catch(()=>{}),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{outcome:"restarted",binding:{aibotSessionId:t,cwd:R,workerStatus:"ready"}}});return}if(l&&g===m.open){L(e,this.sessionControlCtx(t),this.sessionControlSenders()),await this.deferredMgr.replayDeferredPiEvents(t,this.deferredCallbacks());return}if(l&&g===m.restart){await this.handlePiSessionControlRestartLocalAction(e);return}const _=a==="openhuman",f=a==="opencode";if((_||f)&&g===m.open){L(e,this.sessionControlCtx(t),this.sessionControlSenders()),_&&await this.deferredMgr.replayDeferredOpenHumanEvents(t,this.deferredCallbacks()),f&&await this.deferredMgr.replayDeferredOpenCodeEvents(t,this.deferredCallbacks());return}if(a==="codewhale"&&g===m.open){await this.handleCodeWhaleSessionControlLocalActionOpen(e);return}if(a==="acp"&&g===m.stop){const R=this.bindingStore.get(t)?.cwd??"";await this.pool.removeSlot(t).catch(()=>{}),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{outcome:"stopped",binding:{aibotSessionId:t,cwd:R,workerStatus:"stopped"}}});return}try{await this.handleSessionControlLocalActionForPool(e)}catch(S){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:h.runtimeError,error_msg:S instanceof Error?S.message:String(S)});return}L(e,this.sessionControlCtx(t),this.sessionControlSenders()),g===m.open&&(a==="pi"?await this.deferredMgr.replayDeferredPiEvents(t,this.deferredCallbacks()):a==="openhuman"?await this.deferredMgr.replayDeferredOpenHumanEvents(t,this.deferredCallbacks()):a==="opencode"?await this.deferredMgr.replayDeferredOpenCodeEvents(t,this.deferredCallbacks()):await this.deferredMgr.replayDeferredAcpEvents(t,this.deferredCallbacks()));return}if(o==="file_list"){const a=t?this.bindingStore.get(t)?.cwd:void 0,c=e.params??{},l=String(c.parent_id??"").trim(),g=Array.isArray(c.allowed_extensions)?c.allowed_extensions.filter(f=>typeof f=="string").map(f=>f.trim()).filter(f=>f.length>0):[],_=await ie({parent_id:l||null,session_id:t,show_hidden:!!c.show_hidden,allowed_extensions:g},{resolveCwd:()=>a??this.config.agent.cwd??process.cwd(),fallbackDir:I()});this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:_.status,..._.result?{result:_.result}:{},..._.error_code?{error_code:_.error_code}:{},..._.error_msg?{error_msg:_.error_msg}:{}});return}if(o==="create_folder"){const a=t?this.bindingStore.get(t)?.cwd:void 0,c=String((e.params??{}).parent_id??"").trim(),l=String((e.params??{}).name??"").trim(),g=await ne({parent_id:c||null,name:l,session_id:t},{resolveCwd:()=>a??this.config.agent.cwd??process.cwd(),fallbackDir:I()});this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:g.status,...g.result?{result:g.result}:{},...g.error_code?{error_code:g.error_code}:{},...g.error_msg?{error_msg:g.error_msg}:{}});return}if(o===p.getSessionUsage){await this.handleGetSessionUsage(e,t);return}if(o===p.getRateLimits){await this.handleGetRateLimits(e,t);return}if(i&&o===p.threadCompact){await this.handleClaudeThreadCompact(e,t);return}if(o==="connector_rollback"){await this.handleConnectorRollback(e);return}if(o==="connector_upgrade_push"){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{accepted:!0}}),this.upgradeTrigger?.();return}if(o===p.getAgentGlobalConfig){const a=this.globalConfigStore?.get(this.name);this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{agentName:this.name,...a?{config:a}:{config:null}}});return}const n=this.config.adapterType??"acp",r=(n==="codex"||n==="cursor"||n==="pi"||n==="openhuman"||n==="opencode")&&!!t&&!!this.bindingStore.get(t)?.cwd,d=await this.pool.deliverLocalAction(e,{autoCreateSlot:r});if(d.handled){if(d.kind==="set_mode"){const a=String((e.params??{}).mode_id??"");a&&(n==="codex"?this.globalConfigStore?.set(this.name,{codexModeId:a}):n==="cursor"||n==="claude"?this.globalConfigStore?.set(this.name,{modeId:a}):this.globalConfigStore?.set(this.name,{acpInitialMode:a}))}else if(d.kind==="set_model"){const a=String((e.params??{}).model_id??"");a&&(n==="codex"?this.globalConfigStore?.set(this.name,{codexModelId:a}):this.globalConfigStore?.set(this.name,{modelId:a}))}else if(d.kind==="set_reasoning_effort"){const a=String((e.params??{}).reasoning_effort??(e.params??{}).reasoning_eff??(e.params??{}).effort??"");a&&this.globalConfigStore?.set(this.name,{codexReasoningEffort:a})}else if(d.kind==="set_sandbox_mode"){const a=String((e.params??{}).sandbox_mode??(e.params??{}).sandboxMode??"");if(a){const c=a==="default"?void 0:a;this.globalConfigStore?.set(this.name,{codexSandboxMode:c})}}return}if((n==="codex"||n==="cursor"||n==="pi"||n==="openhuman"||n==="opencode")&&t&&!this.bindingStore.get(t)?.cwd){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:h.bindingMissing,error_msg:"Session binding missing. Open a workspace first."});return}if(o==="set_mode"||o==="set_model"){const a=this.sessionControlCtx(t),c=this.sessionControlSenders(),l=this.pool.getSlot(t)?.adapter;if(o==="set_mode"){const g=String((e.params??{}).mode_id??"");if(!g||!a.isAcpAlive){u.warn(this.name,`set_mode rejected: modeId=${g} acpAlive=${a.isAcpAlive}`),c.sendLocalActionResult(e.action_id,"failed",void 0,"set_mode_invalid","mode_id is required and ACP session must be active");return}const _=await a.setMode(g);u.info(this.name,`set_mode result: modeId=${g} ok=${_}`),_&&this.globalConfigStore?.set(this.name,{acpInitialMode:g});const f=l instanceof b?l.buildToolbarContext(_?"mode_set":"mode_set_failed"):null,k=_?f??{outcome:"mode_set",modeId:g}:f;c.sendLocalActionResult(e.action_id,_?"ok":"failed",k,_?void 0:"set_mode_failed",_?void 0:`failed to set mode ${g}`)}else{const g=String((e.params??{}).model_id??"");if(!g||!a.isAcpAlive){u.warn(this.name,`set_model rejected: modelId=${g} acpAlive=${a.isAcpAlive}`),c.sendLocalActionResult(e.action_id,"failed",void 0,"set_model_invalid","model_id is required and ACP session must be active");return}const _=await a.setModel(g);u.info(this.name,`set_model result: modelId=${g} ok=${_}`),_&&this.globalConfigStore?.set(this.name,{modelId:g});const f=l instanceof b?l.buildToolbarContext(_?"model_set":"model_set_failed"):null,k=_?f??{outcome:"model_set",modelId:g}:f;c.sendLocalActionResult(e.action_id,_?"ok":"failed",k,_?void 0:"set_model_failed",_?void 0:`failed to set model ${g}`)}return}this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"unsupported_local_action",error_msg:`action type ${o} is not supported`})}async handleGetSessionUsage(e,o){if(!o){u.warn(this.name,`[usage] get_session_usage rejected: no session_id in params, action_id=${e.action_id}`),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"session_id_required",error_msg:"session_id is required for get_session_usage"});return}const t=this.config.adapterType??"acp",s=this.bindingStore.get(o),i=s?.cwd??this.config.agent.cwd??process.cwd();u.info(this.name,`[usage] get_session_usage action_id=${e.action_id} session_id=${o} adapterType=${t} hasBinding=${!!s} cwd=${i}`);let n=null,r,d;switch(t){case"claude":{if(r=s?.claudeSessionId,!r){u.warn(this.name,`[usage] no claude binding for session_id=${o}, action_id=${e.action_id}`),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"no_binding",error_msg:"No Claude session binding found"});return}u.info(this.name,`[usage] parsing claude usage: claudeSessionId=${r} cwd=${i}`),n=await N(r,i),d="claude";break}case"acp":default:{if(r=s?.acpSessionId,!r){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"no_binding",error_msg:"No ACP session binding found"});return}n=await z(r,i),d=t,(!d||d==="acp")&&(d="acp");break}case"codex":{if(r=s?.codexThreadId,!r){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"no_binding",error_msg:"No Codex thread binding found"});return}n=await G(r),d="codex";break}case"pi":{if(r=s?.piSessionPath,!r){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"no_binding",error_msg:"No Pi session path binding found"});return}n=await V(r),d="pi";break}case"cursor":{const c=this.pool.getSlot(o)?.adapter;if(!(c instanceof $)){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"no_binding",error_msg:"No Cursor session found"});return}const l=c.getUsageSnapshot(o);if(!l){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"usage_not_found",error_msg:"No usage data found for this session"});return}this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{sessionId:o,adapterType:"cursor",models:[{modelId:this.bindingStore.get(o)?.modelId??this.globalConfigStore?.get(this.name)?.modelId??"auto",turns:l.turns,input:l.total.input,output:l.total.output,cacheRead:l.total.cacheRead,cacheWrite:l.total.cacheWrite}],total:{input:l.total.input,output:l.total.output,cacheRead:l.total.cacheRead,cacheWrite:l.total.cacheWrite},turns:l.turns,sampledAt:l.sampledAt}});return}case"codewhale":{const c=this.pool.getSlot(o)?.adapter;if(!(c instanceof D)){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"no_binding",error_msg:"No CodeWhale session found"});return}const l=c.getUsageSnapshot();if(!l){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"usage_not_found",error_msg:"No usage data found for this session"});return}this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{sessionId:o,adapterType:"codewhale",models:[{modelId:this.bindingStore.get(o)?.modelId??"codewhale",turns:l.turns,input:l.total.input,output:l.total.output}],total:{input:l.total.input,output:l.total.output},turns:l.turns,sampledAt:l.sampledAt}});return}}if(!n){u.info(this.name,`[usage] no usage data found: session_id=${o} adapterSessionId=${r} adapterType=${d}`),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"usage_not_found",error_msg:"No usage data found for this session"});return}u.info(this.name,`[usage] result ok: session_id=${o} adapterSessionId=${r} turns=${n.turns} models=${n.models.length}`),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{sessionId:r,adapterType:d,models:n.models,total:n.total,turns:n.turns,sampledAt:new Date().toISOString()}})}async handleClaudeThreadCompact(e,o){if(!o){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"session_id_required",error_msg:"session_id is required for thread_compact"});return}if(!this.bindingStore.get(o)?.cwd){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:h.bindingMissing,error_msg:"session binding was not found"});return}try{await this.ensureClaudeSlotStarted(o);const i=this.pool.getSlot(o)?.adapter;if(!i?.execCommand){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:h.runtimeError,error_msg:"Agent does not support command execution"});return}u.info(this.name,`thread_compact session_id=${o} action_id=${e.action_id}`);const n=await i.execCommand("compact","",o);n.status==="ok"?this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{outcome:"compacted",message:n.message,data:n.data}}):this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:h.runtimeError,error_msg:n.message??"compact failed"})}catch(s){u.warn(this.name,`thread_compact error session_id=${o}: ${s instanceof Error?s.message:s}`),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:h.runtimeError,error_msg:s instanceof Error?s.message:String(s)})}}async handleGetRateLimits(e,o){const t=this.config.adapterType??"acp";if(this.config.aibot.clientType==="kiro"){if(this.isRateLimitsCacheFresh(this.cachedProviderQuotaSampledAtMs)&&this.cachedProviderQuota){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{adapterType:"acp",available:!0,cached:!0,sampledAt:this.cachedProviderQuotaSampledAtMs,rateLimits:null,contextWindow:null,tokenUsage:null,providerQuota:this.cachedProviderQuota}});return}try{const i=await B();this.cachedProviderQuota=i,this.cachedProviderQuotaSampledAtMs=Date.now(),u.info(this.name,`[rate-limits] kiro quota queried: success=${i.success}`+(i.balance?` balance=${i.balance.remaining} ${i.balance.unit}`:"")+(i.error?` error=${i.error}`:"")),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{adapterType:"acp",available:!0,cached:!1,sampledAt:this.cachedProviderQuotaSampledAtMs,rateLimits:null,contextWindow:null,tokenUsage:null,providerQuota:i}})}catch(i){u.warn(this.name,`[rate-limits] kiro quota query failed: ${i instanceof Error?i.message:String(i)}`),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{adapterType:"acp",available:!1,cached:!1,sampledAt:null,rateLimits:null,contextWindow:null,tokenUsage:null}})}return}switch(t){case"codex":{const s=this.maybeQueryProviderQuota(),i=this.getFreshCodexGlobalRateLimitCache();if(i.hasData){i.rateLimits?u.info(this.name,`[rate-limits] codex cached: primary=${i.rateLimits.primary.usedPercent.toFixed(1)}% resetsAt=${i.rateLimits.primary.resetsAt} secondary=${i.rateLimits.secondary.usedPercent.toFixed(1)}% resetsAt=${i.rateLimits.secondary.resetsAt}`):u.info(this.name,`[rate-limits] codex cached context/token only: hasContext=${!!i.contextWindow} hasToken=${!!i.tokenUsage}`);const c=await s;this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{adapterType:"codex",available:i.hasData||!!c,cached:!0,sampledAt:i.sampledAt,rateLimits:i.rateLimits,contextWindow:i.contextWindow,tokenUsage:i.tokenUsage,providerQuota:c}});return}const n=this.pool.getAllSlots().find(c=>c.state==="ready"&&c.adapter);if(n&&(u.info(this.name,`[rate-limits] codex reuse existing slot: session=${n.sessionId}`),(await n.adapter.handleLocalAction?.(e))?.handled))return;const r=this.resolveRateLimitWakeSessionId(o,t);if(r){const c=await this.wakeRateLimitSlot(r,t);if(c?.adapter&&(await c.adapter.handleLocalAction?.(e))?.handled)return}const d=await s,a=!!d;u.info(this.name,`[rate-limits] codex no native data, providerQuota=${d?d.provider:"none"} available=${a}`),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{adapterType:"codex",available:a,cached:!1,sampledAt:null,rateLimits:null,contextWindow:null,tokenUsage:null,providerQuota:d}});return}case"claude":{const s=this.maybeQueryProviderQuota(),i=this.getFreshClaudeRateLimitState();if(i){const c=i.rateLimits,l=await s;u.info(this.name,`[rate-limits] claude global cached: sampledAt=${i.sampledAt} hasRateLimits=${!!c}`+(l?` providerQuota=${l.provider}`:"")),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{adapterType:"claude",available:!0,cached:!0,sampledAt:i.sampledAt,rateLimits:i.rateLimits??null,contextWindow:i.contextWindow,tokenUsage:null,providerQuota:l}});return}let n=this.pool.getAllSlots().find(c=>c.state==="ready"&&c.adapter instanceof E)??null;if(!n){const c=this.resolveRateLimitWakeSessionId(o,t);c&&(n=await this.wakeRateLimitSlot(c,t))}u.info(this.name,`[rate-limits] handleGetRateLimits: session_id=${o} adapterType=claude hasSlot=${!!n} hasAdapter=${!!n?.adapter}`);const r=n?.adapter,d=r instanceof E?r.getSessionState():null,a=await s;if(d){(d.rateLimits?.fiveHour||d.rateLimits?.sevenDay||d.contextWindow?.usedPercentage!=null)&&(this.cachedClaudeRateLimitState=d);const c=this.getFreshClaudeRateLimitState(),l=c?.rateLimits&&!d.rateLimits?{...d,rateLimits:c.rateLimits}:d,g=l.rateLimits;u.info(this.name,`[rate-limits] claude global state: sampledAt=${l.sampledAt} hasRateLimits=${!!g}`+(g?` fiveHour=${g.fiveHour?.usedPercentage??"n/a"}% resetsAt=${g.fiveHour?.resetsAt??"n/a"} sevenDay=${g.sevenDay?.usedPercentage??"n/a"}% resetsAt=${g.sevenDay?.resetsAt??"n/a"}`:"")+(c?.rateLimits&&!d.rateLimits?" source=live+cached-fallback":" source=live")+(a?` providerQuota=${a.provider}`:"")),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{adapterType:"claude",available:!0,cached:!1,sampledAt:l.sampledAt,rateLimits:l.rateLimits??null,contextWindow:l.contextWindow,tokenUsage:null,providerQuota:a}})}else u.info(this.name,`[rate-limits] claude no global state: hasAdapter=${!!r} adapterType=${r?.constructor?.name??"n/a"}`+(a?` providerQuota=${a.provider}`:"")),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{adapterType:"claude",available:!!a,cached:!1,sampledAt:null,rateLimits:null,contextWindow:null,tokenUsage:null,providerQuota:a}});return}case"cursor":{const i=(o?this.pool.getSlot(o):null)?.adapter,n=i instanceof $?i.getRateLimitsSnapshot():{adapterType:"cursor",available:!1,cached:!1,sampledAt:null,rateLimits:null,contextWindow:null,tokenUsage:null};this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:n});return}default:{const s=this.config.providerBaseUrl,i=this.config.providerApiKey;if(!s||!i){u.info(this.name,`[rate-limits] no provider config for adapterType=${t}`),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{adapterType:t,available:!1,cached:!1,sampledAt:null,rateLimits:null,contextWindow:null,tokenUsage:null}});return}if(this.isRateLimitsCacheFresh(this.cachedProviderQuotaSampledAtMs)&&this.cachedProviderQuota){u.info(this.name,`[rate-limits] provider quota cached: provider=${this.cachedProviderQuota.provider} success=${this.cachedProviderQuota.success}`),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{adapterType:t,available:!0,cached:!0,sampledAt:this.cachedProviderQuotaSampledAtMs,rateLimits:null,contextWindow:null,tokenUsage:null,providerQuota:this.cachedProviderQuota}});return}try{const r=await y(s,i);this.cachedProviderQuota=r,this.cachedProviderQuotaSampledAtMs=Date.now(),u.info(this.name,`[rate-limits] provider quota queried: provider=${r.provider} success=${r.success}`+(r.tiers.length>0?` tiers=${r.tiers.map(d=>`${d.name}=${d.usedPercent}%`).join(",")}`:"")+(r.balance?` balance=${r.balance.remaining} ${r.balance.unit}`:"")+(r.error?` error=${r.error}`:"")),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{adapterType:t,available:!0,cached:!1,sampledAt:this.cachedProviderQuotaSampledAtMs,rateLimits:null,contextWindow:null,tokenUsage:null,providerQuota:r}})}catch(r){u.warn(this.name,`[rate-limits] provider quota query failed: ${r instanceof Error?r.message:String(r)}`),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{adapterType:t,available:!1,cached:!1,sampledAt:null,rateLimits:null,contextWindow:null,tokenUsage:null}})}return}}}resolveRateLimitWakeSessionId(e,o){const t=String(e??"").trim(),s=this.bindingStore.getMostRecentlyUpdatedSessionId({requireCwd:!0});return s?t&&t===s?t:o==="codex"||o==="claude"?(t&&t!==s&&u.info(this.name,`[rate-limits] ${o} remap wake session: requested=${t} use_recent=${s}`),s):t&&t!==s?(u.info(this.name,`[rate-limits] skip wake slot: session=${t} adapterType=${o} reason=not_recent_session recent=${s}`),null):t||null:(u.info(this.name,`[rate-limits] skip wake slot: adapterType=${o} reason=no_recent_binding`),null)}async wakeRateLimitSlot(e,o){const t=String(e??"").trim();if(!t)return null;if(!this.bindingStore.get(t)?.cwd)return u.info(this.name,`[rate-limits] skip wake slot: session=${t} adapterType=${o} reason=binding_cwd_missing`),null;try{const i=this.pool.getOrCreateSlot(t);if(!i)return u.info(this.name,`[rate-limits] skip wake slot: session=${t} adapterType=${o} reason=slot_unavailable`),null;if(i.startPromise){const n=o==="claude"?6e4:2e4;await Promise.race([i.startPromise,new Promise((r,d)=>setTimeout(()=>d(new Error(`wake rate-limits slot timeout (${n}ms)`)),n))])}return u.info(this.name,`[rate-limits] wake slot success: session=${t} adapterType=${o}`),this.pool.getSlot(t)??i}catch(i){return u.warn(this.name,`[rate-limits] wake slot failed: session=${t} adapterType=${o} err=${i instanceof Error?i.message:String(i)}`),null}}sessionControlCtx(e){const o=this.pool.getSlot(e),t=o?.adapter instanceof b?o.adapter:null;return{getCwd:()=>this.bindingStore.get(e)?.cwd??this.config.agent.cwd??process.cwd(),getSessionBindings:()=>{if(t)return t.getSessionBindings();const s=this.sessionBindings;if(e&&!s.has(e)){const i=this.bindingStore.get(e);i?.cwd&&s.set(e,i.cwd)}return s},getStatus:()=>this.getStatus(),isAcpAlive:!!t?.isAlive(),getAcpSessionOptions:()=>t?.acpSessionOptions??null,setMode:s=>t?t.setMode(s):Promise.resolve(!1),setModel:s=>t?t.setModel(s):Promise.resolve(!1),getPendingApproval:s=>{const i=t?.pendingApprovalEntries.get(s);return i?{requestId:i}:void 0},deletePendingApproval:s=>t?.pendingApprovalEntries.delete(s)??!1,respondPermission:(s,i)=>(t&&t.respondToPermission(s,i),Promise.resolve()),onSessionBound:(s,i)=>{this.bindingStore.set(s,i)},onSessionUnbound:s=>{const n=this.bindingStore.get(s)?.cwd??"";this.bindingStore.delete(s),this.sessionBindings.delete(s),this.deferredMgr.clearSession(s),n&&this.aibotHandle.sendUpdateBindingCard({session_id:s,worker_status:"stopped",cwd:n})},cancelActiveRun:()=>o?.adapter?.cancel("")??Promise.resolve(),onModeSet:s=>{(this.config.adapterType??"acp")==="codex"?this.globalConfigStore?.set(this.name,{codexModeId:s}):this.globalConfigStore?.set(this.name,{acpInitialMode:s})},onModelSet:s=>{(this.config.adapterType??"acp")==="codex"?this.globalConfigStore?.set(this.name,{codexModelId:s}):this.globalConfigStore?.set(this.name,{modelId:s})}}}sessionControlSenders(){return{sendEventAck:(e,o)=>this.aibotHandle.sendEventAck({event_id:e,session_id:o,received_at:Date.now()}),sendEventResult:(e,o,t)=>this.aibotHandle.sendEventResult({event_id:e,status:o,...t?.msg?{msg:t.msg}:{},...t?.code?{code:t.code}:{},updated_at:Date.now()}),sendLocalActionResult:(e,o,t,s,i)=>this.aibotHandle.sendLocalActionResult({action_id:e,status:o,...t?{result:t}:{},...s?{error_code:s}:{},...i?{error_msg:i}:{}})}}resolveBindingChannelKey(e){switch(e){case"claude":return"grix-claude";case"codex":return"codex";case"cursor":return"cursor";case"pi":return"pi";case"openhuman":return"openhuman";case"codewhale":return"codewhale";case"opencode":return"opencode";default:return e}}finalizeThinking(e,o){this.sendCtrl.finalizeThinking(e,o)}logInboundConversation(e){const o=String(e.session_id??"").trim();o&&this.conversationLog?.logInbound(o,{event_id:e.event_id,msg_id:e.msg_id,sender_id:e.sender_id,msg_type:e.msg_type,content:e.content??""})}buildInboundEvent(e){return{event_id:e.event_id,session_id:e.session_id,thread_id:e.thread_id,sender_id:e.sender_id,msg_id:e.msg_id,msg_type:e.msg_type,content:e.content??"",quoted_message_id:e.quoted_message_id,context_messages_json:e.context_messages?JSON.stringify(e.context_messages):void 0,extra_json:e.extra?JSON.stringify(e.extra):void 0,connector_runtime_config:{response_delivery:this.sendCtrl.getGlobalRuntimeConfig().responseDelivery,tool_events:this.sendCtrl.getGlobalRuntimeConfig().toolEvents,thinking_events:this.sendCtrl.getGlobalRuntimeConfig().thinkingEvents},session_type:e.session_type,created_at:e.created_at}}isStaleClaudeEvent(e){const o=Number(e.created_at);return!Number.isFinite(o)||o<=0?!1:Date.now()-o>he}async handleConnectorRollback(e){const o=String((e.params??{}).target_version??"").trim(),t=String((e.params??{}).reason??"server_initiated");if(!o){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"MISSING_TARGET_VERSION",error_msg:"target_version is required for connector_rollback"});return}u.info(this.name,`connector_rollback: target=${o} reason=${t}`);try{const{npmInstall:s,writePending:i,removePending:n,upgradeLog:r}=await import("../core/upgrade/npm-upgrader.js"),{resolveClientVersion:d}=await import("../core/util/client-version.js"),a=d();r(`server rollback: ${a} -> ${o} reason=${t}`),i(a,o),await s("grix-connector",o),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{rolled_back_to:o}}),process.kill(process.pid,"SIGTERM")}catch(s){try{const{removePending:n}=await import("../core/upgrade/npm-upgrader.js");n()}catch{}const i=s instanceof Error?s.message:String(s);u.error(this.name,`connector_rollback failed: ${i}`),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"ROLLBACK_FAILED",error_msg:i})}}setUpgradeTrigger(e){this.upgradeTrigger=e}}export{Ge as AgentInstance};
13
+ Error: ${r}`,1,!1)},sendPermissionCard:i=>{if(o){this.sendAcpRawEventEnvelope(i.eventId,i.sessionId,{type:"permission_request",payload:{tool_call_id:i.toolCallId,tool_name:i.toolName,tool_title:i.toolTitle,options:i.options}});return}this.aibotHandle.sendMsg({event_id:i.eventId,session_id:i.sessionId,msg_type:1,content:i.toolTitle?`Permission required: ${i.toolTitle}`:"Permission request",extra:{channel_data:{execApproval:{approvalId:i.toolCallId,approvalSlug:i.toolName},grix:{execApproval:{approval_command_id:i.toolCallId,command:i.toolTitle||i.toolName,host:"acp"}}},agent_api_origin:!0}})},sendAuthNotification:(i,n)=>{i&&this.aibotHandle.sendMsg({session_id:i,msg_type:1,content:n,extra:{biz_card:{version:1,type:"agent_error",payload:{error:{name:"AuthRequired",message:n}}}}})},sendUpdateBindingCard:(i,n,r,d)=>{const a={...d??{}};if(!a.rate_limits&&this.cachedProviderQuota?.success){this.isRateLimitsCacheFresh(this.cachedProviderQuotaSampledAtMs)||this.maybeQueryProviderQuota().catch(()=>{});const c=this.providerQuotaToRateLimits(this.cachedProviderQuota);c&&(a.rate_limits=c)}!a.provider_quota&&this.cachedProviderQuota?.success&&(a.provider_quota=this.cachedProviderQuota),this.aibotHandle.sendUpdateBindingCard({session_id:i,worker_status:n,cwd:r,...Object.keys(a).length>0?{meta:a}:{}})}},s=this.globalConfigStore?.get(this.name);return new b({command:this.config.agent.command,args:this.config.agent.args,env:this.config.agent.env},t,{acpAuthMethod:this.config.acpAuthMethod,acpInitialMode:this.config.acpInitialMode??s?.acpInitialMode,acpMcpTools:this.config.acpMcpTools,rawTransport:o,eventResultsPath:this.config.eventResultsPath,approvalMode:this.config.approvalMode,bindingStore:this.config.enableSessionBinding?this.bindingStore:void 0,aibotSessionId:e,autoInjectArgs:this.config.autoInjectArgs})}async connectAibot(){const e=new Q;if(this.aibotHandle=await e.connect(this.aibotConfig,{aborted:()=>this.stopped,label:this.name}),this.aibotHandle.onEvent(o=>{this.handleAibotEvent(o).catch(t=>{l.error(this.name,`handleAibotEvent failed: ${t}`),this.aibotHandle.sendEventAck({event_id:o.event_id,session_id:o.session_id,received_at:Date.now()});const s=t instanceof Error?t.message:String(t);if(/CWD must be|Bound directory does not exist|Bound path is not a directory/i.test(s)&&o.session_id){this.bindingStore.delete(o.session_id),this.sessionBindings.delete(o.session_id);const n=this.pool.getSlot(o.session_id);n?.adapter instanceof b&&n.adapter.getSessionBindings().delete(o.session_id);const r=this.config.adapterType??"acp",d=this.resolveBindingChannelKey(r);this.aibotHandle.sendMsg({event_id:o.event_id,session_id:o.session_id,msg_type:1,content:s,extra:{channel_data:{[d]:{sessionBinding:{status:"missing",reason:"binding_stale",error_code:h.invalidCwd}}}},quoted_message_id:o.msg_id});return}this.aibotHandle.sendEventResult({event_id:o.event_id,status:"failed",msg:s,updated_at:Date.now()})})}),this.aibotHandle.onStop(o=>{try{this.handleAibotStop(o)}catch(t){l.error(this.name,`handleAibotStop failed: ${t}`)}}),this.aibotHandle.onRevoke(o=>{try{this.handleAibotRevoke(o)}catch(t){l.error(this.name,`handleAibotRevoke failed: ${t}`)}}),this.aibotHandle.onLocalAction(o=>{this.handleAibotLocalAction(o).catch(t=>{l.error(this.name,`handleAibotLocalAction failed: ${t}`)})}),this.aibotHandle.onEventCancel(o=>{l.info(this.name,`recv event_cancel event_id=${o.event_id} session_id=${o.session_id}`),this.handleEventCancel(o).catch(t=>{l.error(this.name,`handleEventCancel failed: ${t}`)})}),this.aibotHandle.onQueueClear(o=>{const t=this.pool.clearQueue(o.session_id);this.aibotHandle.sendQueueClearResult({session_id:o.session_id,canceled_event_ids:t}),this.pushQueueSnapshotForSession(o.session_id)}),l.info(this.name,"Connected to aibot"),this.activeEventStore){const o=await this.activeEventStore.drain();if(o.length>0){l.warn(this.name,`Recovering ${o.length} stale event(s) from previous run`);for(const t of o)l.info(this.name,`Failing stale event on startup: ${t}`),this.aibotHandle.sendEventResult({event_id:t,status:"failed",msg:"process restarted, event lost",updated_at:Date.now()})}}this.pushQueueSnapshots()}pushQueueSnapshots(){if(!(!this.config.eventQueue||!this.pool))for(const e of this.pool.getAllSlots())this.pushQueueSnapshotForSession(e.sessionId)}pushQueueSnapshotForSession(e){if(!this.config.eventQueue||!this.pool)return;const o=this.pool.getQueueSnapshot(e);o&&this.aibotHandle.sendQueueSnapshot({session_id:e,running:o.running,running_items:o.running_items.map(t=>({event_id:t.event_id,...t.content_preview?{content_preview:t.content_preview}:{},...t.title?{title:t.title}:{},...t.summary?{summary:t.summary}:{}})),queued:o.queued.map(t=>({event_id:t.event_id,position:t.position,...t.content_preview?{content_preview:t.content_preview}:{},...t.title?{title:t.title}:{},...t.summary?{summary:t.summary}:{},actions:[{type:"cancel"}]}))})}sendReplyByRuntimeConfig(e,o,t,s,i){this.indexEventSession(e,o),this.sendCtrl.sendReply(e,o,t,s,i),t&&this.conversationLog?.logOutbound?.(o,e,"reply",t)}sendStreamChunkByRuntimeConfig(e,o,t,s,i,n,r){this.indexEventSession(e,o),this.sendCtrl.sendStreamChunk(e,o,t,s,i,n,r),(t||i)&&this.conversationLog?.logOutbound?.(o,e,i?"stream_chunk_finish":"stream_chunk",t)}sendEventResultWithCleanup(e,o,t,s){this.sendCtrl.sendEventResult(e,o,t,s);const i=this.eventSessionIndex.get(e);i&&(this.pool.eventComplete(e,i),this.pushQueueSnapshotForSession(i),this.conversationLog?.logResult?.(i,e,o,t),this.eventSessionIndex.delete(e)),o==="responded"&&(this.cachedProviderQuotaSampledAtMs=null,this.maybeQueryProviderQuota().catch(()=>{}))}sendThinkingByRuntimeConfig(e,o,t){this.sendCtrl.sendThinking(e,o,t)}bufferStreamChunk(e,o,t,s,i){this.sendCtrl.bufferOnly(e,o,t,s,i)}flushBufferedStreamText(e){}resolveEventRuntimeConfig(e){return this.sendCtrl.resolveEventRuntimeConfig(e)}captureEventRuntimeConfig(e){this.sendCtrl.captureEventRuntimeConfig(e),this.indexEventSession(e.event_id,e.session_id)}indexEventSession(e,o){!e||!o||this.eventSessionIndex.set(e,o)}shouldDropToolDisplayEvent(e){return this.sendCtrl.shouldDropToolDisplayEvent(e)}shouldDropThinkingDisplayEvent(e){return this.sendCtrl.shouldDropThinkingDisplayEvent(e)}shouldDropCodexDisplayEvent(e,o){return this.sendCtrl.shouldDropCodexDisplayEvent(e,o)}logCodexEventToConversation(e){if(!this.conversationLog||e.codex_method!=="item/agentMessage/delta")return;const t=e.codex_payload?.params?.delta;if(!t)return;const s=this.eventSessionIndex.get(e.event_id)??e.session_id;this.conversationLog.append(s,{ts:Date.now(),dir:"outbound",event_id:e.event_id,kind:"codex_delta",text_len:t.length,content:t})}isAcpRawTransportEnabled(){return(this.config.adapterOptions??{}).raw_transport===!0}shouldDropAcpRawDisplayEvent(e,o){return this.sendCtrl.shouldDropAcpRawDisplayEvent(e,o)}sendAcpRawEventEnvelope(e,o,t){this.shouldDropAcpRawDisplayEvent(e,t.type)||this.aibotHandle.sendMsg({event_id:e,session_id:o,msg_type:1,content:this.buildAcpRawEventFallbackText(t),extra:{channel_data:{acp:{raw_event:t}},agent_api_origin:!0}})}buildAcpRawEventFallbackText(e){const o=String(e.type??"").trim();if(!o)return"[acp] event";switch(o){case"permission_request":return`Permission required: ${String(e.payload?.tool_title??e.payload?.tool_name??"permission request")}`;case"tool_use":return`[tool] ${String(e.payload?.tool_name??"tool")}`;case"tool_result":return"[tool result]";case"thinking":return"[thinking]";case"error":return`[error] ${String(e.payload?.message??"agent error")}`;case"result":return"[result]";default:return`[acp] ${o}`}}sendToolExecutionCard(e,o,t,s){this.sendCtrl.sendToolExecutionCard(e,o,t,s)}async handleAibotEvent(e){if(this.stopped){this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendEventResult({event_id:e.event_id,status:"failed",msg:"agent shutting down",updated_at:Date.now()});return}this.logInboundConversation(e);const o=this.config.adapterType??"acp";if(this.allowlistGate&&e.sender_id&&!await this.allowlistGate.checkAccess(e.sender_id)){this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendEventResult({event_id:e.event_id,status:"responded",code:"access_denied",msg:`sender ${e.sender_id} is not authorized`,updated_at:Date.now()});return}const t=ae(e.extra);if(t.error){this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendEventResult({event_id:e.event_id,status:"failed",code:"connector_config_invalid",msg:t.error,updated_at:Date.now()});return}this.sendCtrl.setGlobalRuntimeConfig(de(this.sendCtrl.getGlobalRuntimeConfig(),t.patch));const s=ee(e);if(s){if(s.verb===m.exec){await this.handleSessionControlCommand(s,e);return}if(o==="claude"){await this.handleSessionControlCommand(s,e);return}if(o==="codex"&&s.verb===m.open){await this.handleCodexSessionControlOpen(s,e);return}if(o==="pi"&&s.verb===m.open){await this.handlePiSessionControlOpen(s,e);return}if(o==="pi"&&s.verb===m.restart){await this.handlePiSessionControlRestart(e);return}if((o==="openhuman"||o==="opencode")&&s.verb===m.open){await this.handleOpenHumanSessionControlOpen(s,e);return}if(o==="codewhale"&&s.verb===m.open){await this.handleCodeWhaleSessionControlOpen(s,e);return}if(this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),s.verb===m.open){const n=s.args.trim();if(!n){this.aibotHandle.sendEventResult({event_id:e.event_id,status:"failed",code:h.cwdRequired,msg:"cwd is required",updated_at:Date.now()});return}try{const r=x.resolve(n);if(!(await $(r)).isDirectory()){this.aibotHandle.sendEventResult({event_id:e.event_id,status:"failed",code:h.invalidCwd,msg:`Path is not a directory: ${r}`,updated_at:Date.now()});return}}catch(r){const d=String(r?.code??""),a=d==="ENOENT"?`Directory does not exist: ${x.resolve(n)}`:d==="EACCES"||d==="EPERM"?"Directory is not accessible":`Invalid path: ${n}`;this.aibotHandle.sendEventResult({event_id:e.event_id,status:"failed",code:h.invalidCwd,msg:a,updated_at:Date.now()});return}}if(s.verb===m.open){const n=this.bindingStore.get(e.session_id);if(n?.cwd)try{await $(n.cwd)}catch{l.info("bridge",`Stale binding detected for session ${e.session_id}: ${n.cwd} no longer exists, clearing`),this.bindingStore.delete(e.session_id),this.sessionBindings.delete(e.session_id);const r=this.pool.getSlot(e.session_id);r?.adapter instanceof b&&r.adapter.getSessionBindings().delete(e.session_id)}}if(await this.handleSessionControlForPool(s,e),o==="acp"&&s.verb===m.stop){const r=this.bindingStore.get(e.session_id)?.cwd??"";await this.pool.removeSlot(e.session_id).catch(()=>{}),this.aibotHandle.sendEventResult({event_id:e.event_id,status:"responded",msg:`Session worker stopped for ${r}`,updated_at:Date.now()});return}P(s,e,this.sessionControlCtx(e.session_id),{...this.sessionControlSenders(),sendEventAck:()=>{}}),s.verb===m.open&&(o==="pi"?await this.deferredMgr.replayDeferredPiEvents(e.session_id,this.deferredCallbacks()):o==="openhuman"?await this.deferredMgr.replayDeferredOpenHumanEvents(e.session_id,this.deferredCallbacks()):o==="opencode"?await this.deferredMgr.replayDeferredOpenCodeEvents(e.session_id,this.deferredCallbacks()):await this.deferredMgr.replayDeferredAcpEvents(e.session_id,this.deferredCallbacks()));return}if(e.mirror_mode==="record_only"){this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendEventResult({event_id:e.event_id,status:"responded",updated_at:Date.now()});return}if(o==="claude"){if(this.isStaleClaudeEvent(e)){this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendEventResult({event_id:e.event_id,status:"failed",code:"event_stale",msg:"event is stale and will not be processed",updated_at:Date.now()});return}if(!this.bindingStore.get(e.session_id)?.cwd){this.deferredMgr.deferClaudeEvent(String(e.session_id??"").trim(),this.buildInboundEvent(e));const r=this.resolveBindingChannelKey(o);this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendMsg({event_id:e.event_id,session_id:e.session_id,msg_type:1,content:"Session binding missing.",extra:{channel_data:{[r]:{sessionBinding:{status:"missing",reason:"binding_missing",error_code:h.bindingMissing}}}},quoted_message_id:e.msg_id});return}}if(o==="codex"&&!this.bindingStore.get(e.session_id)?.cwd){this.deferredMgr.deferCodexEvent(String(e.session_id??"").trim(),this.buildInboundEvent(e));const r=this.resolveBindingChannelKey(o);this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendMsg({event_id:e.event_id,session_id:e.session_id,msg_type:1,content:"Session binding missing.",extra:{channel_data:{[r]:{sessionBinding:{status:"missing",reason:"binding_missing",error_code:h.bindingMissing}}}},quoted_message_id:e.msg_id});return}if(o==="cursor"&&!this.bindingStore.get(e.session_id)?.cwd){this.deferredMgr.deferCursorEvent(String(e.session_id??"").trim(),this.buildInboundEvent(e));const r=this.resolveBindingChannelKey(o);this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendMsg({event_id:e.event_id,session_id:e.session_id,msg_type:1,content:"Session binding missing.",extra:{channel_data:{[r]:{sessionBinding:{status:"missing",reason:"binding_missing",error_code:h.bindingMissing}}}},quoted_message_id:e.msg_id});return}if(o==="codewhale"&&!this.bindingStore.get(e.session_id)?.cwd){this.deferredMgr.deferCodeWhaleEvent(String(e.session_id??"").trim(),this.buildInboundEvent(e));const r=this.resolveBindingChannelKey(o);this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendMsg({event_id:e.event_id,session_id:e.session_id,msg_type:1,content:"Session binding missing.",extra:{channel_data:{[r]:{sessionBinding:{status:"missing",reason:"binding_missing",error_code:h.bindingMissing}}}},quoted_message_id:e.msg_id});return}if(o==="opencode"&&!this.bindingStore.get(e.session_id)?.cwd){this.deferredMgr.deferOpenCodeEvent(String(e.session_id??"").trim(),this.buildInboundEvent(e));const r=this.resolveBindingChannelKey(o);this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendMsg({event_id:e.event_id,session_id:e.session_id,msg_type:1,content:"Session binding missing.",extra:{channel_data:{[r]:{sessionBinding:{status:"missing",reason:"binding_missing",error_code:h.bindingMissing}}}},quoted_message_id:e.msg_id});return}if(o==="pi"&&!this.bindingStore.get(e.session_id)?.cwd){this.deferredMgr.deferPiEvent(String(e.session_id??"").trim(),this.buildInboundEvent(e));const r=this.resolveBindingChannelKey(o);this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendMsg({event_id:e.event_id,session_id:e.session_id,msg_type:1,content:"Session binding missing.",extra:{channel_data:{[r]:{sessionBinding:{status:"missing",reason:"binding_missing",error_code:h.bindingMissing}}}},quoted_message_id:e.msg_id});return}if(o==="openhuman"&&!this.bindingStore.get(e.session_id)?.cwd){this.deferredMgr.deferOpenHumanEvent(String(e.session_id??"").trim(),this.buildInboundEvent(e));const r=this.resolveBindingChannelKey(o);this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendMsg({event_id:e.event_id,session_id:e.session_id,msg_type:1,content:"Session binding missing.",extra:{channel_data:{[r]:{sessionBinding:{status:"missing",reason:"binding_missing",error_code:h.bindingMissing}}}},quoted_message_id:e.msg_id});return}if(o==="acp"&&!this.bindingStore.get(e.session_id)?.cwd){l.info(this.name,`[acp] binding missing session_id=${e.session_id} event_id=${e.event_id}`),this.deferredMgr.deferAcpEvent(String(e.session_id??"").trim(),this.buildInboundEvent(e));const r=this.resolveBindingChannelKey(o);this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendMsg({event_id:e.event_id,session_id:e.session_id,msg_type:1,content:"Session binding missing.",extra:{channel_data:{[r]:{sessionBinding:{status:"missing",reason:"binding_missing",error_code:h.bindingMissing}}}},quoted_message_id:e.msg_id});return}const i=this.buildInboundEvent(e);try{this.captureEventRuntimeConfig(i),await this.pool.deliverInboundEvent(i)}catch(n){if(this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),n instanceof Z)this.aibotHandle.sendEventResult({event_id:e.event_id,status:"failed",msg:n.message,updated_at:Date.now()});else throw n}}async handleCodexSessionControlOpen(e,o){const t=o.session_id,s=e.args.trim(),i=()=>this.aibotHandle.sendEventAck({event_id:o.event_id,session_id:t,received_at:Date.now()}),n=(r,d)=>this.aibotHandle.sendEventResult({event_id:o.event_id,status:r,...d,updated_at:Date.now()});if(!s){i(),n("failed",{msg:"Usage: /grix open <working-directory>",code:h.cwdRequired});return}try{const r=await this.resolveCwdForBinding(s),d=this.bindingStore.get(t);if(d?.cwd){let a=!1;try{if(await this.resolveCwdForBinding(d.cwd)===r){i(),n("responded",{msg:`Session already bound to ${d.cwd}`});return}}catch{a=!0,l.info("bridge",`Stale codex binding for session ${t}: ${d.cwd} no longer exists, allowing rebind`)}if(!a){i(),n("failed",{msg:`Session already bound to ${d.cwd}. Rebinding is not allowed.`,code:h.rebindForbidden});return}}this.bindingStore.set(t,r),this.sessionBindings.set(t,r),this.deferredMgr.sendCodexDeferredReplayComposing(t,this.deferredCallbacks()),await this.bindSessionForPool(t,r),await this.deferredMgr.replayDeferredCodexEvents(t,this.deferredCallbacks(),{announceComposing:!1}),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:"ready",cwd:r}),i(),n("responded",{msg:`Session bound to ${r}`})}catch(r){i(),n("failed",{code:h.invalidCwd,msg:r instanceof Error?r.message:String(r)})}}async handleSessionControlForPool(e,o){e.verb===m.open&&await this.bindSessionForPool(o.session_id,e.args.trim())}async handleSessionControlLocalActionForPool(e){if(String(e.action_type??"")!==p.sessionControl)return;const o=e.params??{};if(String(o.verb??"").trim().toLowerCase()!==m.open)return;const s=String(o.session_id??"").trim(),i=String(o.cwd??"").trim();!s||!i||await this.bindSessionForPool(s,i)}async handleCodexSessionControlLocalActionOpen(e){const o=e.params??{},t=String(o.session_id??"").trim(),s=String(o.cwd??"").trim(),i=(n,r,d,a)=>{this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:n,...r!==void 0?{result:r}:{},...d?{error_code:d}:{},...a?{error_msg:a}:{}})};if(!t||!s){i("failed",void 0,h.cwdRequired,"session cwd is required");return}try{const n=await this.resolveCwdForBinding(s),r=this.bindingStore.get(t);if(r?.cwd){let d=!1;try{if(await this.resolveCwdForBinding(r.cwd)===n){i("ok",{outcome:"opened",cwd:r.cwd});return}}catch{d=!0,l.info("bridge",`Stale codex binding for session ${t}: ${r.cwd} no longer exists, allowing rebind`)}if(!d){i("failed",void 0,h.rebindForbidden,`Session already bound to ${r.cwd}`);return}}this.bindingStore.set(t,n),this.sessionBindings.set(t,n),this.deferredMgr.sendCodexDeferredReplayComposing(t,this.deferredCallbacks()),await this.bindSessionForPool(t,n),await this.deferredMgr.replayDeferredCodexEvents(t,this.deferredCallbacks(),{announceComposing:!1}),i("ok",{outcome:"opened",cwd:n})}catch(n){i("failed",void 0,h.invalidCwd,n instanceof Error?n.message:String(n))}}async handleCursorSessionControlLocalActionOpen(e){const o=e.params??{},t=String(o.session_id??"").trim(),s=String(o.cwd??"").trim(),i=(n,r,d,a)=>{this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:n,...r!==void 0?{result:r}:{},...d?{error_code:d}:{},...a?{error_msg:a}:{}})};if(!t||!s){i("failed",void 0,h.cwdRequired,"session cwd is required");return}try{const n=await this.resolveCwdForBinding(s),r=this.bindingStore.get(t);if(r?.cwd){let d=!1;try{if(await this.resolveCwdForBinding(r.cwd)===n){i("ok",{outcome:"opened",cwd:r.cwd});return}}catch{d=!0,l.info("bridge",`Stale cursor binding for session ${t}: ${r.cwd} no longer exists, allowing rebind`)}if(!d){i("failed",void 0,h.rebindForbidden,`Session already bound to ${r.cwd}`);return}}this.bindingStore.set(t,n),this.sessionBindings.set(t,n),this.deferredMgr.sendCursorDeferredReplayComposing(t,this.deferredCallbacks()),await this.bindSessionForPool(t,n),await this.deferredMgr.replayDeferredCursorEvents(t,this.deferredCallbacks(),{announceComposing:!1}),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:"ready",cwd:n}),i("ok",{outcome:"opened",cwd:n})}catch(n){i("failed",void 0,h.invalidCwd,n instanceof Error?n.message:String(n))}}async handlePiSessionControlOpen(e,o){const t=o.session_id,s=e.args.trim(),i=()=>this.aibotHandle.sendEventAck({event_id:o.event_id,session_id:t,received_at:Date.now()}),n=(r,d)=>this.aibotHandle.sendEventResult({event_id:o.event_id,status:r,...d,updated_at:Date.now()});if(!s){i(),n("failed",{msg:"Usage: /grix open <working-directory>",code:h.cwdRequired});return}try{const r=await this.resolveCwdForBinding(s),d=this.bindingStore.get(t);if(d?.cwd){let a=!1;try{if(await this.resolveCwdForBinding(d.cwd)===r){i(),n("responded",{msg:`Session already bound to ${d.cwd}`}),this.aibotHandle.sendMsg({event_id:o.event_id,session_id:t,msg_type:1,content:`\u2705 Session already bound to \`${d.cwd}\``,quoted_message_id:o.msg_id});return}}catch{a=!0,l.info("bridge",`Stale pi binding for session ${t}: ${d.cwd} no longer exists, allowing rebind`)}if(!a){i(),n("failed",{msg:`Session already bound to ${d.cwd}. Rebinding is not allowed.`,code:h.rebindForbidden});return}}this.bindingStore.set(t,r),this.sessionBindings.set(t,r),await this.deferredMgr.replayDeferredPiEvents(t,this.deferredCallbacks()),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:"ready",cwd:r}),i(),n("responded",{msg:`Session bound to ${r}`}),this.aibotHandle.sendMsg({event_id:o.event_id,session_id:t,msg_type:1,content:`\u2705 Working directory bound: \`${r}\``,quoted_message_id:o.msg_id})}catch(r){i(),n("failed",{code:h.invalidCwd,msg:r instanceof Error?r.message:String(r)})}}async handlePiSessionControlRestart(e){const o=e.session_id,t=()=>this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:o,received_at:Date.now()}),s=(r,d)=>this.aibotHandle.sendEventResult({event_id:e.event_id,status:r,...d,updated_at:Date.now()}),n=this.bindingStore.get(o)?.cwd??"";if(!n){t(),s("failed",{msg:"session binding was not found",code:h.bindingMissing});return}t(),await this.pool.removeSlot(o).catch(()=>{}),this.aibotHandle.sendUpdateBindingCard({session_id:o,worker_status:"ready",cwd:n}),s("responded",{msg:`Session worker restarted for ${n}`})}async handlePiSessionControlRestartLocalAction(e){const o=e.params??{},t=String(o.session_id??"").trim(),i=(t?this.bindingStore.get(t):void 0)?.cwd??"",n=(r,d,a,c)=>{this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:r,...d!==void 0?{result:d}:{},...a?{error_code:a}:{},...c?{error_msg:c}:{}})};if(!t){n("failed",void 0,"session_id_required","session_id is required for restart");return}if(!i){n("failed",void 0,h.bindingMissing,"Session binding missing. Open a workspace first.");return}await this.pool.removeSlot(t).catch(()=>{}),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:"ready",cwd:i}),n("ok",{outcome:"restarted",binding:{aibotSessionId:t,cwd:i,workerStatus:"ready"}})}async bindSessionForPool(e,o){const t=String(o??"").trim();if(!e||!t)return;const s=this.pool.getOrCreateSlot(e);if(!s||(s.startPromise&&await s.startPromise,!s.adapter))return;const i=s.adapter instanceof b?s.adapter:null;i?.hasSessionBinding&&(i.announceDeferredComposing(e),await i.bindSession(e,t),i.replayDeferredEvents(e))}deferredCallbacks(){return{captureEventRuntimeConfig:e=>this.captureEventRuntimeConfig(e),deliverInboundEvent:e=>this.pool.deliverInboundEvent(e),sendEventResult:(e,o,t)=>this.sendEventResultWithCleanup(e,o,t)}}async handleOpenHumanSessionControlOpen(e,o){const t=()=>this.aibotHandle.sendEventAck({event_id:o.event_id,session_id:o.session_id,received_at:Date.now()});try{await this.handleSessionControlForPool(e,o),P(e,o,this.sessionControlCtx(o.session_id),this.sessionControlSenders()),this.deferredMgr.hasDeferred("openhuman",o.session_id)&&await this.deferredMgr.replayDeferredOpenHumanEvents(o.session_id,this.deferredCallbacks()),this.deferredMgr.hasDeferred("opencode",o.session_id)&&await this.deferredMgr.replayDeferredOpenCodeEvents(o.session_id,this.deferredCallbacks())}catch(s){t(),this.aibotHandle.sendEventResult({event_id:o.event_id,status:"failed",msg:s instanceof Error?s.message:String(s),updated_at:Date.now()})}}async handleCodeWhaleSessionControlOpen(e,o){const t=o.session_id,s=e.args.trim(),i=()=>this.aibotHandle.sendEventAck({event_id:o.event_id,session_id:t,received_at:Date.now()}),n=(r,d)=>this.aibotHandle.sendEventResult({event_id:o.event_id,status:r,...d,updated_at:Date.now()});if(!s){i(),n("failed",{msg:"Usage: /grix open <working-directory>",code:h.cwdRequired});return}try{const r=await this.resolveCwdForBinding(s),d=this.bindingStore.get(t);if(d?.cwd){if(await this.resolveCwdForBinding(d.cwd)!==r){i(),n("failed",{msg:`Session already bound to ${d.cwd}. Rebinding is not allowed.`,code:h.rebindForbidden});return}i(),n("responded",{msg:`Session already bound to ${d.cwd}`});return}this.bindingStore.set(t,r),this.sessionBindings.set(t,r),await this.bindSessionForPool(t,r),await this.deferredMgr.replayDeferredCodeWhaleEvents(t,this.deferredCallbacks(),{announceComposing:!1}),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:"ready",cwd:r}),i(),n("responded",{msg:`Session bound to ${r}`})}catch(r){i(),n("failed",{code:h.invalidCwd,msg:r instanceof Error?r.message:String(r)})}}async handleCodeWhaleSessionControlLocalActionOpen(e){const o=e.params??{},t=String(o.session_id??"").trim(),s=String(o.cwd??"").trim(),i=(n,r,d,a)=>{this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:n,...r!==void 0?{result:r}:{},...d?{error_code:d}:{},...a?{error_msg:a}:{}})};if(!t||!s){i("failed",void 0,h.cwdRequired,"session cwd is required");return}try{const n=await this.resolveCwdForBinding(s),r=this.bindingStore.get(t);if(r?.cwd){if(await this.resolveCwdForBinding(r.cwd)!==n){i("failed",void 0,h.rebindForbidden,`Session already bound to ${r.cwd}`);return}i("ok",{outcome:"opened",cwd:r.cwd});return}this.bindingStore.set(t,n),this.sessionBindings.set(t,n),await this.bindSessionForPool(t,n),await this.deferredMgr.replayDeferredCodeWhaleEvents(t,this.deferredCallbacks(),{announceComposing:!1}),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:"ready",cwd:n}),i("ok",{outcome:"opened",cwd:n})}catch(n){i("failed",void 0,h.invalidCwd,n instanceof Error?n.message:String(n))}}normalizeClaudeModeId(e){return String(e??"").trim().toLowerCase()===v.approval?v.approval:v.fullAuto}buildClaudeToolbarMeta(e){const o=this.bindingStore.get(e);if(!o)return;const t=L(),s=t.length>0?t[0].id:"",i=o.modelId||this.globalConfigStore?.get(this.name)?.modelId||s,n=this.normalizeClaudeModeId(o.modeId),r={model_id:i,mode_id:n,currentModelId:i,currentModeId:n,available_models:t.map(u=>({id:u.id,displayName:u.displayName}))},d=this.pool.getSlot(e),a=d?.adapter instanceof E?d.adapter.getSessionState():null;(a?.rateLimits?.fiveHour||a?.rateLimits?.sevenDay)&&(this.cachedClaudeRateLimitState=a);const c=a??this.getFreshClaudeRateLimitState();if(c){const u=c.rateLimits;u&&(u.fiveHour||u.sevenDay)&&(r.rate_limits={...u.fiveHour?{fiveHour:u.fiveHour}:{},...u.sevenDay?{sevenDay:u.sevenDay}:{},sampledAt:c.sampledAt||Date.now()});const g=c.contextWindow;g.usedPercentage!=null&&(r.context_window={usedPercentage:g.usedPercentage,remainingPercentage:g.remainingPercentage})}if(!r.rate_limits&&this.cachedProviderQuota?.success){this.isRateLimitsCacheFresh(this.cachedProviderQuotaSampledAtMs)||this.maybeQueryProviderQuota().catch(()=>{});const u=this.providerQuotaToRateLimits(this.cachedProviderQuota);u&&(r.rate_limits=u),l.info(this.name,`[toolbar-meta] provider quota fallback: hasCached=${!!this.cachedProviderQuota} fromProvider=${JSON.stringify(u)}`)}return r.rate_limits&&l.info(this.name,`[toolbar-meta] rate_limits included: ${JSON.stringify(r.rate_limits)}`),r}providerQuotaToRateLimits(e){const o=i=>{if(!i)return 0;const n=Date.parse(i);return Number.isFinite(n)?Math.floor(n/1e3):0},t=e.tiers.find(i=>i.name==="five_hour"),s=e.tiers.find(i=>i.name==="weekly_limit");if(t||s)return{...t?{fiveHour:{usedPercentage:t.usedPercent,resetsAt:o(t.resetsAt)}}:{},...s?{sevenDay:{usedPercentage:s.usedPercent,resetsAt:o(s.resetsAt)}}:{},sampledAt:this.cachedProviderQuotaSampledAtMs??Date.now()};if(e.balance){const i=e.balance;return{credit:{remaining:i.remaining,total:i.total,used:i.used,unit:i.unit,resetsAt:i.resetsAt?o(i.resetsAt):0},planName:e.planName,sampledAt:this.cachedProviderQuotaSampledAtMs??Date.now()}}return null}async resolveCwdForBinding(e){const o=String(e??"").trim();if(process.platform!=="win32"&&(/^[a-zA-Z]:[\\/]/.test(o)||/^\\\\/.test(o))){const i=new Error(`Specified path is not valid on this host: ${o}`);throw i.cwdErrorCode=h.invalidCwd,i}const t=x.resolve(o);let s;try{s=await $(t)}catch(i){const n=String(i?.code??"");if(n==="ENOENT"){const r=new Error(`Specified path does not exist: ${t}`);throw r.cwdErrorCode=h.invalidCwd,r}if(n==="EACCES"||n==="EPERM"){const r=new Error("Specified path is not accessible.");throw r.cwdErrorCode=h.invalidCwd,r}throw i}if(!s.isDirectory()){const i=new Error("Specified path is not a directory.");throw i.cwdErrorCode=h.invalidCwd,i}try{return await U(t)}catch{return t}}getClaudeWorkerStatus(e){const o=this.pool.getSlot(e);return o?o.state==="starting"?"starting":o.state==="stopped"?"stopped":o.adapter.getStatus().busy?"busy":"ready":"stopped"}async ensureClaudeSlotStarted(e){const o=this.pool.getOrCreateSlot(e);if(!o)throw new Error("Failed to allocate Claude session slot");if(o.startPromise){l.info(this.name,`ensureClaudeSlotStarted: awaiting startPromise for session=${e}`);const t=6e4;await Promise.race([o.startPromise,new Promise((s,i)=>setTimeout(()=>i(new Error(`ensureClaudeSlotStarted timeout (${t}ms) session=${e}`)),t))]),l.info(this.name,`ensureClaudeSlotStarted: startPromise resolved for session=${e}`)}}async handleSessionControlCommand(e,o){const t=o.session_id,s=(i,n,r)=>{this.aibotHandle.sendEventResult({event_id:o.event_id,status:i,...n?{msg:n}:{},...r?{code:r}:{},updated_at:Date.now()})};this.aibotHandle.sendEventAck({event_id:o.event_id,session_id:t,received_at:Date.now()});try{switch(e.verb){case m.open:{await M().catch(()=>{});const i=e.args.trim();if(!i){s("failed","Usage: /grix open <working-directory>",h.cwdRequired);return}let n="";try{n=await this.resolveCwdForBinding(i)}catch(d){s("failed",d instanceof Error?d.message:String(d),h.invalidCwd);return}const r=this.bindingStore.get(t);if(r?.cwd){const d=await this.resolveCwdForBinding(r.cwd);if(d!==n){s("failed","session binding cannot be changed to another working directory",h.rebindForbidden);return}this.bindingStore.ensureModeId(t,this.globalConfigStore?.get(this.name)?.modeId??v.fullAuto),this.sessionBindings.set(t,d),await this.ensureClaudeSlotStarted(t),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:this.getClaudeWorkerStatus(t),cwd:d,meta:this.buildClaudeToolbarMeta(t)}),await this.deferredMgr.replayDeferredClaudeEvents(t,this.deferredCallbacks()),s("responded",`Working directory already bound: ${d}`);return}this.bindingStore.set(t,n,{modeId:this.globalConfigStore?.get(this.name)?.modeId??v.fullAuto}),this.sessionBindings.set(t,n),await this.ensureClaudeSlotStarted(t),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:this.getClaudeWorkerStatus(t),cwd:n,meta:this.buildClaudeToolbarMeta(t)}),await this.deferredMgr.replayDeferredClaudeEvents(t,this.deferredCallbacks()),s("responded",`Session bound to ${n}`);return}case m.where:{const i=this.bindingStore.get(t);if(!i?.cwd){s("failed","session binding was not found",h.bindingMissing);return}s("responded",`Working directory: ${i.cwd}`);return}case m.status:{const i=this.bindingStore.get(t);if(!i?.cwd){s("failed","session binding was not found",h.bindingMissing);return}const n=this.normalizeClaudeModeId(i.modeId),r=this.getClaudeWorkerStatus(t);s("responded",`Status: worker=${r} mode=${n} cwd=${i.cwd}`);return}case m.stop:{const i=this.bindingStore.get(t);if(!i?.cwd){s("failed","session binding was not found",h.bindingMissing);return}await this.pool.removeSlot(t),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:"stopped",cwd:i.cwd}),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:"stopped",cwd:i.cwd,meta:this.buildClaudeToolbarMeta(t)}),s("responded",`Session worker stopped for ${i.cwd}`);return}case m.restart:{const i=this.bindingStore.get(t);if(!i?.cwd){s("failed","session binding was not found",h.bindingMissing);return}await this.pool.removeSlot(t),await this.ensureClaudeSlotStarted(t),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:this.getClaudeWorkerStatus(t),cwd:i.cwd,meta:this.buildClaudeToolbarMeta(t)}),s("responded",`Session worker restarted for ${i.cwd}`);return}case m.setMode:{const i=e.args.trim();if(!i){s("failed","Usage: /grix set_mode <mode-id>",A.modeInvalid);return}const n=this.normalizeClaudeModeId(i);if(n!==i.toLowerCase()){s("failed","set mode_id is invalid",A.modeInvalid);return}const r=this.bindingStore.get(t);if(!r?.cwd){s("failed","session binding was not found",h.bindingMissing);return}if(this.normalizeClaudeModeId(r.modeId)===n){s("responded",`Mode unchanged: ${n}`);return}this.bindingStore.setModeId(t,n),this.globalConfigStore?.set(this.name,{modeId:n}),await this.pool.removeSlot(t),await this.ensureClaudeSlotStarted(t),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:this.getClaudeWorkerStatus(t),cwd:r.cwd,meta:this.buildClaudeToolbarMeta(t)}),s("responded",`Mode set to ${n}`);return}case m.setModel:{const i=e.args.trim();if(!i){s("failed","Usage: /grix set_model <model-id>");return}const n=this.bindingStore.get(t);if(!n?.cwd){s("failed","session binding was not found",h.bindingMissing);return}if((n.modelId??"")===i){s("responded",`Model unchanged: ${i}`);return}this.bindingStore.setModelId(t,i),this.globalConfigStore?.set(this.name,{modelId:i}),await this.pool.removeSlot(t),await this.ensureClaudeSlotStarted(t),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:this.getClaudeWorkerStatus(t),cwd:n.cwd,meta:this.buildClaudeToolbarMeta(t)}),s("responded",`Model set to ${i}`);return}case m.listOptions:{const i=L(),n=this.bindingStore.get(t),r=i.map(a=>a.id).join(", "),d=`${v.fullAuto}, ${v.approval}`;s("responded",`Modes (current: ${this.normalizeClaudeModeId(n?.modeId)}): ${d}
14
+ Models (current: ${n?.modelId??"default"}): ${r}`);return}case m.exec:{const[i,...n]=e.args.trim().split(/\s+/);if(!i){s("failed","Usage: /grix exec <command> [args]",h.verbInvalid);return}const d=this.pool.getSlot(t)?.adapter;if(!d?.execCommand){s("failed","Agent does not support command execution",h.verbInvalid);return}const a=d.getSupportedCommands?.()??[];if(!a.some(c=>c.name===i)){s("failed",`Unknown command: ${i}. Supported: ${a.map(c=>c.name).join(", ")}`,h.verbInvalid);return}try{const c=await d.execCommand(i,n.join(" "),t);s(c.status==="ok"?"responded":"failed",c.message??`${i} ${c.status}`,c.status==="ok"?void 0:h.runtimeError)}catch(c){s("failed",`exec error: ${c instanceof Error?c.message:c}`,h.runtimeError)}return}default:s("failed",`Unsupported command for Claude: /grix ${e.verb}`,h.verbInvalid)}}catch(i){s("failed",i instanceof Error?i.message:String(i),h.runtimeError)}}async handleSessionControlLocalAction(e){const o=String(e.action_type??"").trim();if(o!==p.sessionControl&&o!==p.setMode&&o!=="set_mode"&&o!==p.setModel&&o!=="set_model")return!1;const t=e.params??{},s=String(t.session_id??"").trim(),i=o===p.setMode?m.setMode:o===p.setModel?m.setModel:String(t.verb??"").trim().toLowerCase();if(l.info(this.name,`handleSessionControlLocalAction verb=${i} action_id=${e.action_id} session_id=${s}`),!s)return this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:A.localActionRouteMissing,error_msg:"local action session_id is required"}),!0;const n=d=>{this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:d})},r=(d,a)=>{l.warn(this.name,`session_control local_action failed action_id=${e.action_id} session_id=${s} verb=${i} code=${d} msg=${a}`),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:d,error_msg:a})};try{switch(i){case m.open:{await M().catch(()=>{});const d=String(t.cwd??"").trim();if(!d)return r(h.cwdRequired,"session control cwd is required"),!0;l.info(this.name,`handleSessionControlLocalAction open cwd=${d} session_id=${s}`);const a=await this.resolveCwdForBinding(d),c=this.bindingStore.get(s);if(c?.cwd){const u=await this.resolveCwdForBinding(c.cwd);return u!==a?(r(h.rebindForbidden,"session binding cannot be changed to another working directory"),!0):(this.bindingStore.ensureModeId(s,this.globalConfigStore?.get(this.name)?.modeId??v.fullAuto),this.sessionBindings.set(s,u),await this.ensureClaudeSlotStarted(s),this.aibotHandle.sendUpdateBindingCard({session_id:s,worker_status:this.getClaudeWorkerStatus(s),cwd:u,meta:this.buildClaudeToolbarMeta(s)}),await this.deferredMgr.replayDeferredClaudeEvents(s,this.deferredCallbacks()),n({outcome:"already_bound",binding:{cwd:u,mode_id:this.normalizeClaudeModeId(this.bindingStore.get(s)?.modeId)}}),!0)}return this.bindingStore.set(s,a,{modeId:this.globalConfigStore?.get(this.name)?.modeId??v.fullAuto}),this.sessionBindings.set(s,a),await this.ensureClaudeSlotStarted(s),this.aibotHandle.sendUpdateBindingCard({session_id:s,worker_status:this.getClaudeWorkerStatus(s),cwd:a,meta:this.buildClaudeToolbarMeta(s)}),await this.deferredMgr.replayDeferredClaudeEvents(s,this.deferredCallbacks()),n({outcome:"opened",binding:{cwd:a,mode_id:this.normalizeClaudeModeId(this.bindingStore.get(s)?.modeId)}}),!0}case m.status:case m.where:{const d=this.bindingStore.get(s);return d?.cwd?(n({outcome:i,binding:{cwd:d.cwd,mode_id:this.normalizeClaudeModeId(d.modeId),worker_status:this.getClaudeWorkerStatus(s)}}),!0):(r(h.bindingMissing,"session binding was not found"),!0)}case m.stop:{const d=this.bindingStore.get(s);return d?.cwd?(await this.pool.removeSlot(s),n({outcome:"stopped",binding:{cwd:d.cwd,mode_id:this.normalizeClaudeModeId(d.modeId),worker_status:"stopped"}}),!0):(r(h.bindingMissing,"session binding was not found"),!0)}case m.restart:{const d=this.bindingStore.get(s);return d?.cwd?(await this.pool.removeSlot(s),await this.ensureClaudeSlotStarted(s),this.aibotHandle.sendUpdateBindingCard({session_id:s,worker_status:this.getClaudeWorkerStatus(s),cwd:d.cwd,meta:this.buildClaudeToolbarMeta(s)}),n({outcome:"restarted",binding:{cwd:d.cwd,mode_id:this.normalizeClaudeModeId(d.modeId)}}),!0):(r(h.bindingMissing,"session binding was not found"),!0)}case m.setMode:{const d=String(t.mode_id??t.modeId??"").trim();if(!d)return r(A.modeInvalid,"set mode_id is invalid"),!0;const a=this.normalizeClaudeModeId(d);if(a!==d.toLowerCase())return r(A.modeInvalid,"set mode_id is invalid"),!0;const c=this.bindingStore.get(s);return c?.cwd?(this.normalizeClaudeModeId(c.modeId)!==a&&(this.bindingStore.setModeId(s,a),this.globalConfigStore?.set(this.name,{modeId:a}),await this.pool.removeSlot(s),await this.ensureClaudeSlotStarted(s),this.aibotHandle.sendUpdateBindingCard({session_id:s,worker_status:this.getClaudeWorkerStatus(s),cwd:c.cwd,meta:this.buildClaudeToolbarMeta(s)})),n({outcome:"mode_set",mode_id:a,binding:{cwd:c.cwd,mode_id:a}}),!0):(r(h.bindingMissing,"session binding was not found"),!0)}case m.setModel:{const d=String(t.model_id??t.modelId??"").trim();if(!d)return r("set_model_invalid","model_id is required"),!0;const a=this.bindingStore.get(s);return a?.cwd?((a.modelId??"")!==d&&(this.bindingStore.setModelId(s,d),this.globalConfigStore?.set(this.name,{modelId:d}),await this.pool.removeSlot(s),await this.ensureClaudeSlotStarted(s),this.aibotHandle.sendUpdateBindingCard({session_id:s,worker_status:this.getClaudeWorkerStatus(s),cwd:a.cwd,meta:this.buildClaudeToolbarMeta(s)})),n({outcome:"model_set",model_id:d,binding:{cwd:a.cwd,model_id:d}}),!0):(r(h.bindingMissing,"session binding was not found"),!0)}case m.listOptions:{const d=L(),a=this.pool.getSlot(s);return n({modes:[v.fullAuto,v.approval],currentModeId:this.normalizeClaudeModeId(this.bindingStore.get(s)?.modeId),models:d.map(c=>({modelId:c.id,name:c.displayName})),currentModelId:this.bindingStore.get(s)?.modelId??"",available_models:d.map(c=>({id:c.id,displayName:c.displayName})),agent_commands:a?.adapter?.getSupportedCommands?.()??[]}),!0}case m.exec:{const[d,...a]=String(t.args??"").trim().split(/\s+/);if(!d)return r(h.verbInvalid,"Usage: exec <command> [args]"),!0;await this.ensureClaudeSlotStarted(s);const u=this.pool.getSlot(s)?.adapter;if(!u?.execCommand)return r(h.verbInvalid,"Agent does not support command execution"),!0;const g=u.getSupportedCommands?.()??[];if(!g.some(_=>_.name===d))return r(h.verbInvalid,`Unknown command: ${d}. Supported: ${g.map(_=>_.name).join(", ")}`),!0;try{const _=await u.execCommand(d,a.join(" "),s);_.status==="ok"?n({outcome:"exec",command:d,message:_.message,data:_.data}):r(h.runtimeError,_.message??`${d} failed`)}catch(_){r(h.runtimeError,`exec error: ${_ instanceof Error?_.message:_}`)}return!0}default:return r(h.verbInvalid,`session control verb ${i} is not supported`),!0}}catch(d){const a=d instanceof Error&&d.cwdErrorCode?d.cwdErrorCode:h.runtimeError;return l.error(this.name,`handleSessionControlLocalAction error verb=${i} session_id=${s}: ${d instanceof Error?d.message:d}`),r(a,d instanceof Error?d.message:String(d)),!0}}async handleEventCancel(e){const{event_id:o,session_id:t}=e;if(l.info(this.name,`handleEventCancel start event_id=${o} session_id=${t}`),this.pool.cancelEvent(o,t)){if(!(this.pool.getSlot(t)?.adapter?.getActiveEventIds().includes(o)??!1)){this.sendEventResultWithCleanup(o,"canceled","canceled"),this.aibotHandle.sendEventCancelResult({event_id:o,accepted:!0,final_state:"canceled"});return}await this.waitForEventDone(o,t,15e3),this.aibotHandle.sendEventCancelResult({event_id:o,accepted:!0,final_state:"canceled"}),this.pushQueueSnapshotForSession(t);return}this.aibotHandle.sendEventCancelResult({event_id:o,accepted:!1,reason:"event not found or not cancelable"})}waitForEventDone(e,o,t){return new Promise(s=>{const i=this.pool.getSlot(o);if(!i?.adapter){s();return}const n=setTimeout(()=>{i.adapter.removeListener("eventDone",r),s()},t),r=d=>{d===e&&(clearTimeout(n),i.adapter.removeListener("eventDone",r),s())};i.adapter.on("eventDone",r)})}handleAibotStop(e){this.aibotHandle.sendEventStopAck({stop_id:e.stop_id,event_id:e.event_id,accepted:!0,updated_at:Date.now()});const o=this.pool.getSlot(e.session_id),t=o?.adapter?.getStatus().busy??!1;if(o?.adapter&&t){const s=i=>{i===e.event_id&&(o.adapter.removeListener("eventDone",s),this.aibotHandle.sendEventStopResult({stop_id:e.stop_id,event_id:e.event_id,status:"stopped",updated_at:Date.now()}))};o.adapter.on("eventDone",s),this.pool.deliverStopEvent(e.event_id,e.session_id)}else this.pool.deliverStopEvent(e.event_id,e.session_id),this.aibotHandle.sendEventStopResult({stop_id:e.stop_id,event_id:e.event_id,status:"stopped",updated_at:Date.now()})}handleAibotRevoke(e){if(!e.event_id||!this.revokeHandler.checkAndTrack(e.event_id))return;const o=e.event_id,t=e.session_id;if(t&&this.pool.cancelEvent(o,t)){(this.pool.getSlot(t)?.adapter?.getActiveEventIds().includes(o)??!1)||this.sendEventResultWithCleanup(o,"canceled","revoked");return}if(this.deferredMgr.removeEvent(o)){this.aibotHandle.sendEventResult({event_id:o,status:"canceled",msg:"revoked",updated_at:Date.now()});return}this.pool.deliverStopEvent(o,t||void 0)}async handleAibotLocalAction(e){const o=e.action_type??"",t=String((e.params??{}).session_id??""),s=String((e.params??{}).verb??"").trim().toLowerCase();l.debug(this.name,`local_action received action_type=${o} verb=${s||"-"} action_id=${e.action_id} session_id=${t}`);const i=(this.config.adapterType??"acp")==="claude";if(o===p.sessionControl&&s===m.exec&&await this.handleSessionControlLocalAction(e)||i&&await this.handleSessionControlLocalAction(e))return;if(o===p.sessionControl){const a=this.config.adapterType??"acp",c=a==="codex",u=a==="pi",g=String((e.params??{}).verb??"").trim().toLowerCase();if(c&&g===m.open){await this.handleCodexSessionControlLocalActionOpen(e);return}if(a==="cursor"&&g===m.open){await this.handleCursorSessionControlLocalActionOpen(e);return}if(c&&g==="restart"){const R=this.bindingStore.get(t)?.cwd??"";await this.pool.removeSlot(t).catch(()=>{}),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{outcome:"restarted",binding:{aibotSessionId:t,cwd:R,workerStatus:"ready"}}});return}if(u&&g===m.open){T(e,this.sessionControlCtx(t),this.sessionControlSenders()),await this.deferredMgr.replayDeferredPiEvents(t,this.deferredCallbacks());return}if(u&&g===m.restart){await this.handlePiSessionControlRestartLocalAction(e);return}const _=a==="openhuman",f=a==="opencode";if((_||f)&&g===m.open){T(e,this.sessionControlCtx(t),this.sessionControlSenders()),_&&await this.deferredMgr.replayDeferredOpenHumanEvents(t,this.deferredCallbacks()),f&&await this.deferredMgr.replayDeferredOpenCodeEvents(t,this.deferredCallbacks());return}if(a==="codewhale"&&g===m.open){await this.handleCodeWhaleSessionControlLocalActionOpen(e);return}if(a==="acp"&&g===m.stop){const R=this.bindingStore.get(t)?.cwd??"";await this.pool.removeSlot(t).catch(()=>{}),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{outcome:"stopped",binding:{aibotSessionId:t,cwd:R,workerStatus:"stopped"}}});return}try{await this.handleSessionControlLocalActionForPool(e)}catch(S){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:h.runtimeError,error_msg:S instanceof Error?S.message:String(S)});return}T(e,this.sessionControlCtx(t),this.sessionControlSenders()),g===m.open&&(a==="pi"?await this.deferredMgr.replayDeferredPiEvents(t,this.deferredCallbacks()):a==="openhuman"?await this.deferredMgr.replayDeferredOpenHumanEvents(t,this.deferredCallbacks()):a==="opencode"?await this.deferredMgr.replayDeferredOpenCodeEvents(t,this.deferredCallbacks()):await this.deferredMgr.replayDeferredAcpEvents(t,this.deferredCallbacks()));return}if(o==="file_list"){const a=t?this.bindingStore.get(t)?.cwd:void 0,c=e.params??{},u=String(c.parent_id??"").trim(),g=Array.isArray(c.allowed_extensions)?c.allowed_extensions.filter(f=>typeof f=="string").map(f=>f.trim()).filter(f=>f.length>0):[],_=await ie({parent_id:u||null,session_id:t,show_hidden:!!c.show_hidden,allowed_extensions:g},{resolveCwd:()=>a??this.config.agent.cwd??process.cwd(),fallbackDir:I()});this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:_.status,..._.result?{result:_.result}:{},..._.error_code?{error_code:_.error_code}:{},..._.error_msg?{error_msg:_.error_msg}:{}});return}if(o==="create_folder"){const a=t?this.bindingStore.get(t)?.cwd:void 0,c=String((e.params??{}).parent_id??"").trim(),u=String((e.params??{}).name??"").trim(),g=await ne({parent_id:c||null,name:u,session_id:t},{resolveCwd:()=>a??this.config.agent.cwd??process.cwd(),fallbackDir:I()});this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:g.status,...g.result?{result:g.result}:{},...g.error_code?{error_code:g.error_code}:{},...g.error_msg?{error_msg:g.error_msg}:{}});return}if(o===p.getSessionUsage){await this.handleGetSessionUsage(e,t);return}if(o===p.getRateLimits){await this.handleGetRateLimits(e,t);return}if(i&&o===p.threadCompact){await this.handleClaudeThreadCompact(e,t);return}if(o==="connector_rollback"){await this.handleConnectorRollback(e);return}if(o==="connector_upgrade_push"){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{accepted:!0}}),this.upgradeTrigger?.();return}if(o===p.getAgentGlobalConfig){const a=this.globalConfigStore?.get(this.name);this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{agentName:this.name,...a?{config:a}:{config:null}}});return}const n=this.config.adapterType??"acp",r=(n==="codex"||n==="cursor"||n==="pi"||n==="openhuman"||n==="opencode")&&!!t&&!!this.bindingStore.get(t)?.cwd,d=await this.pool.deliverLocalAction(e,{autoCreateSlot:r});if(d.handled){if(d.kind==="set_mode"){const a=String((e.params??{}).mode_id??"");a&&(n==="codex"?this.globalConfigStore?.set(this.name,{codexModeId:a}):n==="cursor"||n==="claude"?this.globalConfigStore?.set(this.name,{modeId:a}):this.globalConfigStore?.set(this.name,{acpInitialMode:a}))}else if(d.kind==="set_model"){const a=String((e.params??{}).model_id??"");a&&(n==="codex"?this.globalConfigStore?.set(this.name,{codexModelId:a}):this.globalConfigStore?.set(this.name,{modelId:a}))}else if(d.kind==="set_reasoning_effort"){const a=String((e.params??{}).reasoning_effort??(e.params??{}).reasoning_eff??(e.params??{}).effort??"");a&&this.globalConfigStore?.set(this.name,{codexReasoningEffort:a})}else if(d.kind==="set_sandbox_mode"){const a=String((e.params??{}).sandbox_mode??(e.params??{}).sandboxMode??"");if(a){const c=a==="default"?void 0:a;this.globalConfigStore?.set(this.name,{codexSandboxMode:c})}}return}if((n==="codex"||n==="cursor"||n==="pi"||n==="openhuman"||n==="opencode")&&t&&!this.bindingStore.get(t)?.cwd){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:h.bindingMissing,error_msg:"Session binding missing. Open a workspace first."});return}if(o==="set_mode"||o==="set_model"){const a=this.sessionControlCtx(t),c=this.sessionControlSenders(),u=this.pool.getSlot(t)?.adapter;if(o==="set_mode"){const g=String((e.params??{}).mode_id??"");if(!g||!a.isAcpAlive){l.warn(this.name,`set_mode rejected: modeId=${g} acpAlive=${a.isAcpAlive}`),c.sendLocalActionResult(e.action_id,"failed",void 0,"set_mode_invalid","mode_id is required and ACP session must be active");return}const _=await a.setMode(g);l.info(this.name,`set_mode result: modeId=${g} ok=${_}`),_&&this.globalConfigStore?.set(this.name,{acpInitialMode:g});const f=u instanceof b?u.buildToolbarContext(_?"mode_set":"mode_set_failed"):null,k=_?f??{outcome:"mode_set",modeId:g}:f;c.sendLocalActionResult(e.action_id,_?"ok":"failed",k,_?void 0:"set_mode_failed",_?void 0:`failed to set mode ${g}`)}else{const g=String((e.params??{}).model_id??"");if(!g||!a.isAcpAlive){l.warn(this.name,`set_model rejected: modelId=${g} acpAlive=${a.isAcpAlive}`),c.sendLocalActionResult(e.action_id,"failed",void 0,"set_model_invalid","model_id is required and ACP session must be active");return}const _=await a.setModel(g);l.info(this.name,`set_model result: modelId=${g} ok=${_}`),_&&this.globalConfigStore?.set(this.name,{modelId:g});const f=u instanceof b?u.buildToolbarContext(_?"model_set":"model_set_failed"):null,k=_?f??{outcome:"model_set",modelId:g}:f;c.sendLocalActionResult(e.action_id,_?"ok":"failed",k,_?void 0:"set_model_failed",_?void 0:`failed to set model ${g}`)}return}this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"unsupported_local_action",error_msg:`action type ${o} is not supported`})}async handleGetSessionUsage(e,o){if(!o){l.warn(this.name,`[usage] get_session_usage rejected: no session_id in params, action_id=${e.action_id}`),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"session_id_required",error_msg:"session_id is required for get_session_usage"});return}const t=this.config.adapterType??"acp",s=this.bindingStore.get(o),i=s?.cwd??this.config.agent.cwd??process.cwd();l.info(this.name,`[usage] get_session_usage action_id=${e.action_id} session_id=${o} adapterType=${t} hasBinding=${!!s} cwd=${i}`);let n=null,r,d;switch(t){case"claude":{if(r=s?.claudeSessionId,!r){l.warn(this.name,`[usage] no claude binding for session_id=${o}, action_id=${e.action_id}`),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"no_binding",error_msg:"No Claude session binding found"});return}l.info(this.name,`[usage] parsing claude usage: claudeSessionId=${r} cwd=${i}`),n=await N(r,i),d="claude";break}case"acp":default:{if(r=s?.acpSessionId,!r){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"no_binding",error_msg:"No ACP session binding found"});return}n=await z(r,i),d=t,(!d||d==="acp")&&(d="acp");break}case"codex":{if(r=s?.codexThreadId,!r){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"no_binding",error_msg:"No Codex thread binding found"});return}n=await G(r),d="codex";break}case"pi":{if(r=s?.piSessionPath,!r){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"no_binding",error_msg:"No Pi session path binding found"});return}n=await V(r),d="pi";break}case"cursor":{const c=this.pool.getSlot(o)?.adapter;if(!(c instanceof H)){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"no_binding",error_msg:"No Cursor session found"});return}const u=c.getUsageSnapshot(o);if(!u){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"usage_not_found",error_msg:"No usage data found for this session"});return}this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{sessionId:o,adapterType:"cursor",models:[{modelId:this.bindingStore.get(o)?.modelId??this.globalConfigStore?.get(this.name)?.modelId??"auto",turns:u.turns,input:u.total.input,output:u.total.output,cacheRead:u.total.cacheRead,cacheWrite:u.total.cacheWrite}],total:{input:u.total.input,output:u.total.output,cacheRead:u.total.cacheRead,cacheWrite:u.total.cacheWrite},turns:u.turns,sampledAt:u.sampledAt}});return}case"codewhale":{const c=this.pool.getSlot(o)?.adapter;if(!(c instanceof D)){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"no_binding",error_msg:"No CodeWhale session found"});return}const u=c.getUsageSnapshot();if(!u){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"usage_not_found",error_msg:"No usage data found for this session"});return}this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{sessionId:o,adapterType:"codewhale",models:[{modelId:this.bindingStore.get(o)?.modelId??"codewhale",turns:u.turns,input:u.total.input,output:u.total.output}],total:{input:u.total.input,output:u.total.output},turns:u.turns,sampledAt:u.sampledAt}});return}}if(!n){l.info(this.name,`[usage] no usage data found: session_id=${o} adapterSessionId=${r} adapterType=${d}`),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"usage_not_found",error_msg:"No usage data found for this session"});return}l.info(this.name,`[usage] result ok: session_id=${o} adapterSessionId=${r} turns=${n.turns} models=${n.models.length}`),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{sessionId:r,adapterType:d,models:n.models,total:n.total,turns:n.turns,sampledAt:new Date().toISOString()}})}async handleClaudeThreadCompact(e,o){if(!o){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"session_id_required",error_msg:"session_id is required for thread_compact"});return}if(!this.bindingStore.get(o)?.cwd){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:h.bindingMissing,error_msg:"session binding was not found"});return}try{await this.ensureClaudeSlotStarted(o);const i=this.pool.getSlot(o)?.adapter;if(!i?.execCommand){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:h.runtimeError,error_msg:"Agent does not support command execution"});return}l.info(this.name,`thread_compact session_id=${o} action_id=${e.action_id}`);const n=await i.execCommand("compact","",o);n.status==="ok"?this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{outcome:"compacted",message:n.message,data:n.data}}):this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:h.runtimeError,error_msg:n.message??"compact failed"})}catch(s){l.warn(this.name,`thread_compact error session_id=${o}: ${s instanceof Error?s.message:s}`),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:h.runtimeError,error_msg:s instanceof Error?s.message:String(s)})}}async handleGetRateLimits(e,o){const t=this.config.adapterType??"acp";if(this.config.aibot.clientType==="kiro"){if(this.isRateLimitsCacheFresh(this.cachedProviderQuotaSampledAtMs)&&this.cachedProviderQuota){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{adapterType:"acp",available:!0,cached:!0,sampledAt:this.cachedProviderQuotaSampledAtMs,rateLimits:null,contextWindow:null,tokenUsage:null,providerQuota:this.cachedProviderQuota}});return}try{const i=await y();this.cachedProviderQuota=i,this.cachedProviderQuotaSampledAtMs=Date.now(),l.info(this.name,`[rate-limits] kiro quota queried: success=${i.success}`+(i.balance?` balance=${i.balance.remaining} ${i.balance.unit}`:"")+(i.error?` error=${i.error}`:"")),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{adapterType:"acp",available:!0,cached:!1,sampledAt:this.cachedProviderQuotaSampledAtMs,rateLimits:null,contextWindow:null,tokenUsage:null,providerQuota:i}})}catch(i){l.warn(this.name,`[rate-limits] kiro quota query failed: ${i instanceof Error?i.message:String(i)}`),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{adapterType:"acp",available:!1,cached:!1,sampledAt:null,rateLimits:null,contextWindow:null,tokenUsage:null}})}return}switch(t){case"codex":{const s=this.maybeQueryProviderQuota(),i=this.getFreshCodexGlobalRateLimitCache();if(i.hasData){i.rateLimits?l.info(this.name,`[rate-limits] codex cached: primary=${i.rateLimits.primary.usedPercent.toFixed(1)}% resetsAt=${i.rateLimits.primary.resetsAt} secondary=${i.rateLimits.secondary.usedPercent.toFixed(1)}% resetsAt=${i.rateLimits.secondary.resetsAt}`):l.info(this.name,`[rate-limits] codex cached context/token only: hasContext=${!!i.contextWindow} hasToken=${!!i.tokenUsage}`);const c=await s;this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{adapterType:"codex",available:i.hasData||!!c,cached:!0,sampledAt:i.sampledAt,rateLimits:i.rateLimits,contextWindow:i.contextWindow,tokenUsage:i.tokenUsage,providerQuota:c}});return}const n=this.pool.getAllSlots().find(c=>c.state==="ready"&&c.adapter);if(n&&(l.info(this.name,`[rate-limits] codex reuse existing slot: session=${n.sessionId}`),(await n.adapter.handleLocalAction?.(e))?.handled))return;const r=this.resolveRateLimitWakeSessionId(o,t);if(r){const c=await this.wakeRateLimitSlot(r,t);if(c?.adapter&&(await c.adapter.handleLocalAction?.(e))?.handled)return}const d=await s,a=!!d;l.info(this.name,`[rate-limits] codex no native data, providerQuota=${d?d.provider:"none"} available=${a}`),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{adapterType:"codex",available:a,cached:!1,sampledAt:null,rateLimits:null,contextWindow:null,tokenUsage:null,providerQuota:d}});return}case"claude":{const s=this.maybeQueryProviderQuota(),i=this.getFreshClaudeRateLimitState();if(i){const c=i.rateLimits,u=await s;l.info(this.name,`[rate-limits] claude global cached: sampledAt=${i.sampledAt} hasRateLimits=${!!c}`+(u?` providerQuota=${u.provider}`:"")),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{adapterType:"claude",available:!0,cached:!0,sampledAt:i.sampledAt,rateLimits:i.rateLimits??null,contextWindow:i.contextWindow,tokenUsage:null,providerQuota:u}});return}let n=this.pool.getAllSlots().find(c=>c.state==="ready"&&c.adapter instanceof E)??null;if(!n){const c=this.resolveRateLimitWakeSessionId(o,t);c&&(n=await this.wakeRateLimitSlot(c,t))}l.info(this.name,`[rate-limits] handleGetRateLimits: session_id=${o} adapterType=claude hasSlot=${!!n} hasAdapter=${!!n?.adapter}`);const r=n?.adapter,d=r instanceof E?r.getSessionState():null,a=await s;if(d){(d.rateLimits?.fiveHour||d.rateLimits?.sevenDay||d.contextWindow?.usedPercentage!=null)&&(this.cachedClaudeRateLimitState=d);const c=this.getFreshClaudeRateLimitState(),u=c?.rateLimits&&!d.rateLimits?{...d,rateLimits:c.rateLimits}:d,g=u.rateLimits;l.info(this.name,`[rate-limits] claude global state: sampledAt=${u.sampledAt} hasRateLimits=${!!g}`+(g?` fiveHour=${g.fiveHour?.usedPercentage??"n/a"}% resetsAt=${g.fiveHour?.resetsAt??"n/a"} sevenDay=${g.sevenDay?.usedPercentage??"n/a"}% resetsAt=${g.sevenDay?.resetsAt??"n/a"}`:"")+(c?.rateLimits&&!d.rateLimits?" source=live+cached-fallback":" source=live")+(a?` providerQuota=${a.provider}`:"")),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{adapterType:"claude",available:!0,cached:!1,sampledAt:u.sampledAt,rateLimits:u.rateLimits??null,contextWindow:u.contextWindow,tokenUsage:null,providerQuota:a}})}else l.info(this.name,`[rate-limits] claude no global state: hasAdapter=${!!r} adapterType=${r?.constructor?.name??"n/a"}`+(a?` providerQuota=${a.provider}`:"")),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{adapterType:"claude",available:!!a,cached:!1,sampledAt:null,rateLimits:null,contextWindow:null,tokenUsage:null,providerQuota:a}});return}case"cursor":{const i=(o?this.pool.getSlot(o):null)?.adapter,n=i instanceof H?i.getRateLimitsSnapshot():{adapterType:"cursor",available:!1,cached:!1,sampledAt:null,rateLimits:null,contextWindow:null,tokenUsage:null};this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:n});return}default:{const s=this.config.providerBaseUrl,i=this.config.providerApiKey;if(!s||!i){l.info(this.name,`[rate-limits] no provider config for adapterType=${t}`),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{adapterType:t,available:!1,cached:!1,sampledAt:null,rateLimits:null,contextWindow:null,tokenUsage:null}});return}if(this.isRateLimitsCacheFresh(this.cachedProviderQuotaSampledAtMs)&&this.cachedProviderQuota){l.info(this.name,`[rate-limits] provider quota cached: provider=${this.cachedProviderQuota.provider} success=${this.cachedProviderQuota.success}`),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{adapterType:t,available:!0,cached:!0,sampledAt:this.cachedProviderQuotaSampledAtMs,rateLimits:null,contextWindow:null,tokenUsage:null,providerQuota:this.cachedProviderQuota}});return}try{const r=await B(s,i);this.cachedProviderQuota=r,this.cachedProviderQuotaSampledAtMs=Date.now(),l.info(this.name,`[rate-limits] provider quota queried: provider=${r.provider} success=${r.success}`+(r.tiers.length>0?` tiers=${r.tiers.map(d=>`${d.name}=${d.usedPercent}%`).join(",")}`:"")+(r.balance?` balance=${r.balance.remaining} ${r.balance.unit}`:"")+(r.error?` error=${r.error}`:"")),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{adapterType:t,available:!0,cached:!1,sampledAt:this.cachedProviderQuotaSampledAtMs,rateLimits:null,contextWindow:null,tokenUsage:null,providerQuota:r}})}catch(r){l.warn(this.name,`[rate-limits] provider quota query failed: ${r instanceof Error?r.message:String(r)}`),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{adapterType:t,available:!1,cached:!1,sampledAt:null,rateLimits:null,contextWindow:null,tokenUsage:null}})}return}}}resolveRateLimitWakeSessionId(e,o){const t=String(e??"").trim(),s=this.bindingStore.getMostRecentlyUpdatedSessionId({requireCwd:!0});return s?t&&t===s?t:o==="codex"||o==="claude"?(t&&t!==s&&l.info(this.name,`[rate-limits] ${o} remap wake session: requested=${t} use_recent=${s}`),s):t&&t!==s?(l.info(this.name,`[rate-limits] skip wake slot: session=${t} adapterType=${o} reason=not_recent_session recent=${s}`),null):t||null:(l.info(this.name,`[rate-limits] skip wake slot: adapterType=${o} reason=no_recent_binding`),null)}async wakeRateLimitSlot(e,o){const t=String(e??"").trim();if(!t)return null;if(!this.bindingStore.get(t)?.cwd)return l.info(this.name,`[rate-limits] skip wake slot: session=${t} adapterType=${o} reason=binding_cwd_missing`),null;try{const i=this.pool.getOrCreateSlot(t);if(!i)return l.info(this.name,`[rate-limits] skip wake slot: session=${t} adapterType=${o} reason=slot_unavailable`),null;if(i.startPromise){const n=o==="claude"?6e4:2e4;await Promise.race([i.startPromise,new Promise((r,d)=>setTimeout(()=>d(new Error(`wake rate-limits slot timeout (${n}ms)`)),n))])}return l.info(this.name,`[rate-limits] wake slot success: session=${t} adapterType=${o}`),this.pool.getSlot(t)??i}catch(i){return l.warn(this.name,`[rate-limits] wake slot failed: session=${t} adapterType=${o} err=${i instanceof Error?i.message:String(i)}`),null}}sessionControlCtx(e){const o=this.pool.getSlot(e),t=o?.adapter instanceof b?o.adapter:null;return{getCwd:()=>this.bindingStore.get(e)?.cwd??this.config.agent.cwd??process.cwd(),getSessionBindings:()=>{if(t)return t.getSessionBindings();const s=this.sessionBindings;if(e&&!s.has(e)){const i=this.bindingStore.get(e);i?.cwd&&s.set(e,i.cwd)}return s},getStatus:()=>this.getStatus(),isAcpAlive:!!t?.isAlive(),getAcpSessionOptions:()=>t?.acpSessionOptions??null,setMode:s=>t?t.setMode(s):Promise.resolve(!1),setModel:s=>t?t.setModel(s):Promise.resolve(!1),getPendingApproval:s=>{const i=t?.pendingApprovalEntries.get(s);return i?{requestId:i}:void 0},deletePendingApproval:s=>t?.pendingApprovalEntries.delete(s)??!1,respondPermission:(s,i)=>(t&&t.respondToPermission(s,i),Promise.resolve()),onSessionBound:(s,i)=>{this.bindingStore.set(s,i)},onSessionUnbound:s=>{const n=this.bindingStore.get(s)?.cwd??"";this.bindingStore.delete(s),this.sessionBindings.delete(s),this.deferredMgr.clearSession(s),n&&this.aibotHandle.sendUpdateBindingCard({session_id:s,worker_status:"stopped",cwd:n})},cancelActiveRun:()=>o?.adapter?.cancel("")??Promise.resolve(),onModeSet:s=>{(this.config.adapterType??"acp")==="codex"?this.globalConfigStore?.set(this.name,{codexModeId:s}):this.globalConfigStore?.set(this.name,{acpInitialMode:s})},onModelSet:s=>{(this.config.adapterType??"acp")==="codex"?this.globalConfigStore?.set(this.name,{codexModelId:s}):this.globalConfigStore?.set(this.name,{modelId:s})}}}sessionControlSenders(){return{sendEventAck:(e,o)=>this.aibotHandle.sendEventAck({event_id:e,session_id:o,received_at:Date.now()}),sendEventResult:(e,o,t)=>this.aibotHandle.sendEventResult({event_id:e,status:o,...t?.msg?{msg:t.msg}:{},...t?.code?{code:t.code}:{},updated_at:Date.now()}),sendLocalActionResult:(e,o,t,s,i)=>this.aibotHandle.sendLocalActionResult({action_id:e,status:o,...t?{result:t}:{},...s?{error_code:s}:{},...i?{error_msg:i}:{}})}}resolveBindingChannelKey(e){switch(e){case"claude":return"grix-claude";case"codex":return"codex";case"cursor":return"cursor";case"pi":return"pi";case"openhuman":return"openhuman";case"codewhale":return"codewhale";case"opencode":return"opencode";default:return e}}finalizeThinking(e,o){this.sendCtrl.finalizeThinking(e,o)}logInboundConversation(e){const o=String(e.session_id??"").trim();o&&this.conversationLog?.logInbound(o,{event_id:e.event_id,msg_id:e.msg_id,sender_id:e.sender_id,msg_type:e.msg_type,content:e.content??""})}buildInboundEvent(e){return{event_id:e.event_id,session_id:e.session_id,thread_id:e.thread_id,sender_id:e.sender_id,msg_id:e.msg_id,msg_type:e.msg_type,content:e.content??"",quoted_message_id:e.quoted_message_id,context_messages_json:e.context_messages?JSON.stringify(e.context_messages):void 0,extra_json:e.extra?JSON.stringify(e.extra):void 0,connector_runtime_config:{response_delivery:this.sendCtrl.getGlobalRuntimeConfig().responseDelivery,tool_events:this.sendCtrl.getGlobalRuntimeConfig().toolEvents,thinking_events:this.sendCtrl.getGlobalRuntimeConfig().thinkingEvents},session_type:e.session_type,created_at:e.created_at}}isStaleClaudeEvent(e){const o=Number(e.created_at);return!Number.isFinite(o)||o<=0?!1:Date.now()-o>he}async handleConnectorRollback(e){const o=String((e.params??{}).target_version??"").trim(),t=String((e.params??{}).reason??"server_initiated");if(!o){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"MISSING_TARGET_VERSION",error_msg:"target_version is required for connector_rollback"});return}l.info(this.name,`connector_rollback: target=${o} reason=${t}`);try{const{npmInstall:s,writePending:i,removePending:n,upgradeLog:r}=await import("../core/upgrade/npm-upgrader.js"),{resolveClientVersion:d}=await import("../core/util/client-version.js"),a=d();r(`server rollback: ${a} -> ${o} reason=${t}`),i(a,o),await s("grix-connector",o),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{rolled_back_to:o}}),process.kill(process.pid,"SIGTERM")}catch(s){try{const{removePending:n}=await import("../core/upgrade/npm-upgrader.js");n()}catch{}const i=s instanceof Error?s.message:String(s);l.error(this.name,`connector_rollback failed: ${i}`),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"ROLLBACK_FAILED",error_msg:i})}}setUpgradeTrigger(e){this.upgradeTrigger=e}}export{Ge as AgentInstance};
@@ -1 +1 @@
1
- import{log as a}from"../core/log/index.js";const u=2,v=3e3;class h{name;deferredClaudeEvents=new Map;deferredClaudeReplayRetries=new Map;deferredCodexEvents=new Map;deferredCursorEvents=new Map;deferredAcpEvents=new Map;deferredPiEvents=new Map;deferredOpenHumanEvents=new Map;deferredCodeWhaleEvents=new Map;deferredOpenCodeEvents=new Map;constructor(e){this.name=e}deferClaudeEvent(e,t){if(!e)return;const r=this.deferredClaudeEvents.get(e)??[];r.some(n=>n.event_id===t.event_id)||(a.info(this.name,`deferClaudeEvent session_id=${e} event_id=${t.event_id} queue_len=${r.length}`),r.push(t),this.deferredClaudeEvents.set(e,r))}deferCodexEvent(e,t){if(!e)return;const r=this.deferredCodexEvents.get(e)??[];r.some(n=>n.event_id===t.event_id)||(r.push(t),this.deferredCodexEvents.set(e,r))}deferCursorEvent(e,t){if(!e)return;const r=this.deferredCursorEvents.get(e)??[];r.some(n=>n.event_id===t.event_id)||(r.push(t),this.deferredCursorEvents.set(e,r))}deferAcpEvent(e,t){if(!e)return;const r=this.deferredAcpEvents.get(e)??[];r.some(n=>n.event_id===t.event_id)||(r.push(t),this.deferredAcpEvents.set(e,r))}deferPiEvent(e,t){if(!e)return;const r=this.deferredPiEvents.get(e)??[];r.some(n=>n.event_id===t.event_id)||(r.push(t),this.deferredPiEvents.set(e,r))}deferOpenHumanEvent(e,t){if(!e)return;const r=this.deferredOpenHumanEvents.get(e)??[];r.some(n=>n.event_id===t.event_id)||(r.push(t),this.deferredOpenHumanEvents.set(e,r))}deferCodeWhaleEvent(e,t){if(!e)return;const r=this.deferredCodeWhaleEvents.get(e)??[];r.some(n=>n.event_id===t.event_id)||(r.push(t),this.deferredCodeWhaleEvents.set(e,r))}deferOpenCodeEvent(e,t){if(!e)return;const r=this.deferredOpenCodeEvents.get(e)??[];r.some(n=>n.event_id===t.event_id)||(r.push(t),this.deferredOpenCodeEvents.set(e,r))}async replayDeferredClaudeEvents(e,t){const r=this.deferredClaudeEvents.get(e);if(!(!r||r.length===0)){this.deferredClaudeEvents.delete(e);for(const n of r)try{t.captureEventRuntimeConfig(n),await t.deliverInboundEvent(n),this.deferredClaudeReplayRetries.delete(n.event_id)}catch(d){const s=d instanceof Error?d.message:String(d),f=this.deferredClaudeReplayRetries.get(n.event_id)??0;if(this.isClaudeDeferredReplayRetriable(s)&&f<u){this.deferredClaudeReplayRetries.set(n.event_id,f+1);const o=this.deferredClaudeEvents.get(e)??[];o.some(i=>i.event_id===n.event_id)||(o.push(n),this.deferredClaudeEvents.set(e,o)),a.warn(this.name,`replayDeferredClaudeEvents retry ${f+1}/${u} session_id=${e} event_id=${n.event_id} reason=${s}`),setTimeout(()=>{this.replayDeferredClaudeEvents(e,t).catch(i=>{a.warn(this.name,`replayDeferredClaudeEvents async retry failed session_id=${e}: ${i instanceof Error?i.message:String(i)}`)})},v).unref();continue}this.deferredClaudeReplayRetries.delete(n.event_id),t.sendEventResult(n.event_id,"failed",s)}}}async replayDeferredCodexEvents(e,t,r={}){const n=this.deferredCodexEvents.get(e);if(!(!n||n.length===0)){this.deferredCodexEvents.delete(e);for(const d of n){r.announceComposing!==!1&&t.sendSessionComposing(d.session_id,!0,{ttlMs:12e4,refEventId:d.event_id,refMsgId:d.msg_id});try{t.captureEventRuntimeConfig(d),await t.deliverInboundEvent(d)}catch(s){t.sendSessionComposing(d.session_id,!1),t.sendEventResult(d.event_id,"failed",s instanceof Error?s.message:String(s))}}}}async replayDeferredCursorEvents(e,t,r={}){const n=this.deferredCursorEvents.get(e);if(!(!n||n.length===0)){this.deferredCursorEvents.delete(e);for(const d of n){r.announceComposing!==!1&&t.sendSessionComposing(d.session_id,!0,{ttlMs:12e4,refEventId:d.event_id,refMsgId:d.msg_id});try{t.captureEventRuntimeConfig(d),await t.deliverInboundEvent(d)}catch(s){t.sendSessionComposing(d.session_id,!1),t.sendEventResult(d.event_id,"failed",s instanceof Error?s.message:String(s))}}}}async replayDeferredPiEvents(e,t){const r=this.deferredPiEvents.get(e);if(!(!r||r.length===0)){this.deferredPiEvents.delete(e),t.sendSessionComposing(e,!0,{ttlMs:3e4});for(const n of r)try{t.captureEventRuntimeConfig(n),await t.deliverInboundEvent(n)}catch(d){t.sendSessionComposing(n.session_id,!1),t.sendEventResult(n.event_id,"failed",d instanceof Error?d.message:String(d))}}}async replayDeferredOpenHumanEvents(e,t){const r=this.deferredOpenHumanEvents.get(e);if(!(!r||r.length===0)){this.deferredOpenHumanEvents.delete(e),t.sendSessionComposing(e,!0,{ttlMs:3e4});for(const n of r)try{t.captureEventRuntimeConfig(n),await t.deliverInboundEvent(n)}catch(d){t.sendSessionComposing(n.session_id,!1),t.sendEventResult(n.event_id,"failed",d instanceof Error?d.message:String(d))}}}async replayDeferredOpenCodeEvents(e,t){const r=this.deferredOpenCodeEvents.get(e);if(!(!r||r.length===0)){this.deferredOpenCodeEvents.delete(e),t.sendSessionComposing(e,!0,{ttlMs:3e4});for(const n of r)try{t.captureEventRuntimeConfig(n),await t.deliverInboundEvent(n)}catch(d){t.sendSessionComposing(n.session_id,!1),t.sendEventResult(n.event_id,"failed",d instanceof Error?d.message:String(d))}}}async replayDeferredAcpEvents(e,t){const r=this.deferredAcpEvents.get(e);if(!(!r||r.length===0)){this.deferredAcpEvents.delete(e),t.sendSessionComposing(e,!0,{ttlMs:3e4});for(const n of r)try{t.captureEventRuntimeConfig(n),await t.deliverInboundEvent(n)}catch(d){t.sendSessionComposing(n.session_id,!1),t.sendEventResult(n.event_id,"failed",d instanceof Error?d.message:String(d))}}}async replayDeferredCodeWhaleEvents(e,t,r={}){const n=this.deferredCodeWhaleEvents.get(e);if(!(!n||n.length===0)){this.deferredCodeWhaleEvents.delete(e),r.announceComposing!==!1&&t.sendSessionComposing(e,!0,{ttlMs:3e4});for(const d of n)try{t.captureEventRuntimeConfig(d),await t.deliverInboundEvent(d)}catch(s){t.sendSessionComposing(d.session_id,!1),t.sendEventResult(d.event_id,"failed",s instanceof Error?s.message:String(s))}}}sendCodexDeferredReplayComposing(e,t){const r=this.deferredCodexEvents.get(e);!r||r.length===0||t.sendSessionComposing(e,!0,{ttlMs:12e4,activity:"preparing"})}sendCursorDeferredReplayComposing(e,t){const r=this.deferredCursorEvents.get(e);!r||r.length===0||t.sendSessionComposing(e,!0,{ttlMs:12e4,activity:"preparing"})}hasDeferred(e,t){return this.getMap(e).has(t)}getAllDeferredEvents(){return[...this.deferredClaudeEvents.values(),...this.deferredCodexEvents.values(),...this.deferredCursorEvents.values(),...this.deferredPiEvents.values(),...this.deferredAcpEvents.values(),...this.deferredOpenHumanEvents.values(),...this.deferredCodeWhaleEvents.values(),...this.deferredOpenCodeEvents.values()].flat()}clearAll(){this.deferredClaudeEvents.clear(),this.deferredClaudeReplayRetries.clear(),this.deferredCodexEvents.clear(),this.deferredCursorEvents.clear(),this.deferredPiEvents.clear(),this.deferredOpenHumanEvents.clear(),this.deferredCodeWhaleEvents.clear(),this.deferredOpenCodeEvents.clear(),this.deferredAcpEvents.clear()}removeEvent(e){for(const t of[this.deferredClaudeEvents,this.deferredCodexEvents,this.deferredCursorEvents,this.deferredAcpEvents,this.deferredPiEvents,this.deferredOpenHumanEvents,this.deferredCodeWhaleEvents,this.deferredOpenCodeEvents])for(const[r,n]of t.entries()){const d=n.findIndex(s=>s.event_id===e);if(d>=0)return n.splice(d,1),n.length===0&&t.delete(r),this.deferredClaudeReplayRetries.delete(e),a.info(this.name,`removeEvent event_id=${e} from deferred queue`),!0}return!1}clearSession(e){const t=this.deferredClaudeEvents.get(e)??[];for(const r of t)this.deferredClaudeReplayRetries.delete(r.event_id);this.deferredClaudeEvents.delete(e),this.deferredCodexEvents.delete(e),this.deferredCursorEvents.delete(e),this.deferredAcpEvents.delete(e),this.deferredPiEvents.delete(e),this.deferredOpenHumanEvents.delete(e),this.deferredCodeWhaleEvents.delete(e),this.deferredOpenCodeEvents.delete(e)}isClaudeDeferredReplayRetriable(e){const t=e.toLowerCase();return t.includes("claude channel listener not ready")||t.includes("notify port")||t.includes("mcp stdio server not available")||t.includes("mcp server startup failed")}getMap(e){switch(e){case"claude":return this.deferredClaudeEvents;case"codex":return this.deferredCodexEvents;case"cursor":return this.deferredCursorEvents;case"acp":return this.deferredAcpEvents;case"pi":return this.deferredPiEvents;case"openhuman":return this.deferredOpenHumanEvents;case"codewhale":return this.deferredCodeWhaleEvents;case"opencode":return this.deferredOpenCodeEvents}}}export{h as DeferredEventManager};
1
+ import{log as a}from"../core/log/index.js";const v=2,l=3e3;class h{name;deferredClaudeEvents=new Map;deferredClaudeReplayRetries=new Map;deferredCodexEvents=new Map;deferredCursorEvents=new Map;deferredAcpEvents=new Map;deferredPiEvents=new Map;deferredOpenHumanEvents=new Map;deferredCodeWhaleEvents=new Map;deferredOpenCodeEvents=new Map;constructor(e){this.name=e}deferClaudeEvent(e,t){if(!e)return;const r=this.deferredClaudeEvents.get(e)??[];r.some(n=>n.event_id===t.event_id)||(a.info(this.name,`deferClaudeEvent session_id=${e} event_id=${t.event_id} queue_len=${r.length}`),r.push(t),this.deferredClaudeEvents.set(e,r))}deferCodexEvent(e,t){if(!e)return;const r=this.deferredCodexEvents.get(e)??[];r.some(n=>n.event_id===t.event_id)||(r.push(t),this.deferredCodexEvents.set(e,r))}deferCursorEvent(e,t){if(!e)return;const r=this.deferredCursorEvents.get(e)??[];r.some(n=>n.event_id===t.event_id)||(r.push(t),this.deferredCursorEvents.set(e,r))}deferAcpEvent(e,t){if(!e)return;const r=this.deferredAcpEvents.get(e)??[];r.some(n=>n.event_id===t.event_id)||(r.push(t),this.deferredAcpEvents.set(e,r))}deferPiEvent(e,t){if(!e)return;const r=this.deferredPiEvents.get(e)??[];r.some(n=>n.event_id===t.event_id)||(r.push(t),this.deferredPiEvents.set(e,r))}deferOpenHumanEvent(e,t){if(!e)return;const r=this.deferredOpenHumanEvents.get(e)??[];r.some(n=>n.event_id===t.event_id)||(r.push(t),this.deferredOpenHumanEvents.set(e,r))}deferCodeWhaleEvent(e,t){if(!e)return;const r=this.deferredCodeWhaleEvents.get(e)??[];r.some(n=>n.event_id===t.event_id)||(r.push(t),this.deferredCodeWhaleEvents.set(e,r))}deferOpenCodeEvent(e,t){if(!e)return;const r=this.deferredOpenCodeEvents.get(e)??[];r.some(n=>n.event_id===t.event_id)||(r.push(t),this.deferredOpenCodeEvents.set(e,r))}async replayDeferredClaudeEvents(e,t){const r=this.deferredClaudeEvents.get(e);if(!(!r||r.length===0)){this.deferredClaudeEvents.delete(e);for(const n of r)try{t.captureEventRuntimeConfig(n),await t.deliverInboundEvent(n),this.deferredClaudeReplayRetries.delete(n.event_id)}catch(d){const s=d instanceof Error?d.message:String(d),f=this.deferredClaudeReplayRetries.get(n.event_id)??0;if(this.isClaudeDeferredReplayRetriable(s)&&f<v){this.deferredClaudeReplayRetries.set(n.event_id,f+1);const u=this.deferredClaudeEvents.get(e)??[];u.some(i=>i.event_id===n.event_id)||(u.push(n),this.deferredClaudeEvents.set(e,u)),a.warn(this.name,`replayDeferredClaudeEvents retry ${f+1}/${v} session_id=${e} event_id=${n.event_id} reason=${s}`),setTimeout(()=>{this.replayDeferredClaudeEvents(e,t).catch(i=>{a.warn(this.name,`replayDeferredClaudeEvents async retry failed session_id=${e}: ${i instanceof Error?i.message:String(i)}`)})},l).unref();continue}this.deferredClaudeReplayRetries.delete(n.event_id),t.sendEventResult(n.event_id,"failed",s)}}}async replayDeferredCodexEvents(e,t,r={}){const n=this.deferredCodexEvents.get(e);if(!(!n||n.length===0)){this.deferredCodexEvents.delete(e);for(const d of n)try{t.captureEventRuntimeConfig(d),await t.deliverInboundEvent(d)}catch(s){t.sendEventResult(d.event_id,"failed",s instanceof Error?s.message:String(s))}}}async replayDeferredCursorEvents(e,t,r={}){const n=this.deferredCursorEvents.get(e);if(!(!n||n.length===0)){this.deferredCursorEvents.delete(e);for(const d of n)try{t.captureEventRuntimeConfig(d),await t.deliverInboundEvent(d)}catch(s){t.sendEventResult(d.event_id,"failed",s instanceof Error?s.message:String(s))}}}async replayDeferredPiEvents(e,t){const r=this.deferredPiEvents.get(e);if(!(!r||r.length===0)){this.deferredPiEvents.delete(e);for(const n of r)try{t.captureEventRuntimeConfig(n),await t.deliverInboundEvent(n)}catch(d){t.sendEventResult(n.event_id,"failed",d instanceof Error?d.message:String(d))}}}async replayDeferredOpenHumanEvents(e,t){const r=this.deferredOpenHumanEvents.get(e);if(!(!r||r.length===0)){this.deferredOpenHumanEvents.delete(e);for(const n of r)try{t.captureEventRuntimeConfig(n),await t.deliverInboundEvent(n)}catch(d){t.sendEventResult(n.event_id,"failed",d instanceof Error?d.message:String(d))}}}async replayDeferredOpenCodeEvents(e,t){const r=this.deferredOpenCodeEvents.get(e);if(!(!r||r.length===0)){this.deferredOpenCodeEvents.delete(e);for(const n of r)try{t.captureEventRuntimeConfig(n),await t.deliverInboundEvent(n)}catch(d){t.sendEventResult(n.event_id,"failed",d instanceof Error?d.message:String(d))}}}async replayDeferredAcpEvents(e,t){const r=this.deferredAcpEvents.get(e);if(!(!r||r.length===0)){this.deferredAcpEvents.delete(e);for(const n of r)try{t.captureEventRuntimeConfig(n),await t.deliverInboundEvent(n)}catch(d){t.sendEventResult(n.event_id,"failed",d instanceof Error?d.message:String(d))}}}async replayDeferredCodeWhaleEvents(e,t,r={}){const n=this.deferredCodeWhaleEvents.get(e);if(!(!n||n.length===0)){this.deferredCodeWhaleEvents.delete(e);for(const d of n)try{t.captureEventRuntimeConfig(d),await t.deliverInboundEvent(d)}catch(s){t.sendEventResult(d.event_id,"failed",s instanceof Error?s.message:String(s))}}}sendCodexDeferredReplayComposing(e,t){const r=this.deferredCodexEvents.get(e);!r||r.length}sendCursorDeferredReplayComposing(e,t){const r=this.deferredCursorEvents.get(e);!r||r.length}hasDeferred(e,t){return this.getMap(e).has(t)}getAllDeferredEvents(){return[...this.deferredClaudeEvents.values(),...this.deferredCodexEvents.values(),...this.deferredCursorEvents.values(),...this.deferredPiEvents.values(),...this.deferredAcpEvents.values(),...this.deferredOpenHumanEvents.values(),...this.deferredCodeWhaleEvents.values(),...this.deferredOpenCodeEvents.values()].flat()}clearAll(){this.deferredClaudeEvents.clear(),this.deferredClaudeReplayRetries.clear(),this.deferredCodexEvents.clear(),this.deferredCursorEvents.clear(),this.deferredPiEvents.clear(),this.deferredOpenHumanEvents.clear(),this.deferredCodeWhaleEvents.clear(),this.deferredOpenCodeEvents.clear(),this.deferredAcpEvents.clear()}removeEvent(e){for(const t of[this.deferredClaudeEvents,this.deferredCodexEvents,this.deferredCursorEvents,this.deferredAcpEvents,this.deferredPiEvents,this.deferredOpenHumanEvents,this.deferredCodeWhaleEvents,this.deferredOpenCodeEvents])for(const[r,n]of t.entries()){const d=n.findIndex(s=>s.event_id===e);if(d>=0)return n.splice(d,1),n.length===0&&t.delete(r),this.deferredClaudeReplayRetries.delete(e),a.info(this.name,`removeEvent event_id=${e} from deferred queue`),!0}return!1}clearSession(e){const t=this.deferredClaudeEvents.get(e)??[];for(const r of t)this.deferredClaudeReplayRetries.delete(r.event_id);this.deferredClaudeEvents.delete(e),this.deferredCodexEvents.delete(e),this.deferredCursorEvents.delete(e),this.deferredAcpEvents.delete(e),this.deferredPiEvents.delete(e),this.deferredOpenHumanEvents.delete(e),this.deferredCodeWhaleEvents.delete(e),this.deferredOpenCodeEvents.delete(e)}isClaudeDeferredReplayRetriable(e){const t=e.toLowerCase();return t.includes("claude channel listener not ready")||t.includes("notify port")||t.includes("mcp stdio server not available")||t.includes("mcp server startup failed")}getMap(e){switch(e){case"claude":return this.deferredClaudeEvents;case"codex":return this.deferredCodexEvents;case"cursor":return this.deferredCursorEvents;case"acp":return this.deferredAcpEvents;case"pi":return this.deferredPiEvents;case"openhuman":return this.deferredOpenHumanEvents;case"codewhale":return this.deferredCodeWhaleEvents;case"opencode":return this.deferredOpenCodeEvents}}}export{h as DeferredEventManager};
@@ -1 +1 @@
1
- class o{config;callbacks;running=new Map;queued=[];timers=new Map;constructor(e,t){this.config=e,this.callbacks=t}submit(e){return this.running.has(e.event_id)||this.queued.some(t=>t.event_id===e.event_id)?"accepted":this.running.size<this.config.maxConcurrent?(this.startRunning(e),"accepted"):this.config.maxQueued<=0||this.queued.length>=this.config.maxQueued?(this.callbacks.onRejected(e,"queue full"),"rejected"):(this.enqueue(e),"accepted")}cancel(e){const t=this.queued.findIndex(i=>i.event_id===e);if(t>=0){if(!this.config.cancelableQueued)return!1;const[i]=this.queued.splice(t,1);return this.clearTimer(e),this.callbacks.onStateChange(e,i.session_id,"canceled",{reason:"canceled by user"}),this.broadcastQueuePositions(),this.drainNext(),!0}return this.running.has(e)&&this.config.cancelableRunning?(this.callbacks.onCancelRunning(e),!0):!1}complete(e){this.running.delete(e),this.clearTimer(e),this.drainNext()}clear(e){const t=[],i=[];for(const s of this.queued)s.session_id===e?(this.clearTimer(s.event_id),this.callbacks.onStateChange(s.event_id,e,"canceled",{reason:"queue cleared"}),t.push(s.event_id)):i.push(s);return this.queued=i,t.length>0&&this.broadcastQueuePositions(),t}snapshot(e){const t=[...this.running.values()].filter(n=>n.session_id===e),i=t.map(n=>n.event_id),s=t.map(n=>({event_id:n.event_id,content_preview:this.buildQueueItemTitle(n.content),title:this.buildQueueItemTitle(n.content),summary:this.buildQueueItemTitle(n.content)})),u=this.queued.flatMap((n,c)=>n.session_id===e?[{event_id:n.event_id,position:c+1,content_preview:this.buildQueueItemTitle(n.content),title:this.buildQueueItemTitle(n.content),summary:this.buildQueueItemTitle(n.content)}]:[]);return{running:i,running_items:s,queued:u}}hasCapacity(){return this.running.size<this.config.maxConcurrent}get runningCount(){return this.running.size}get queuedCount(){return this.queued.length}destroy(){for(const e of this.timers.values())clearTimeout(e);this.timers.clear(),this.queued=[],this.running.clear()}enqueue(e){this.queued.push(e);const t=this.queued.length;if(this.callbacks.onStateChange(e.event_id,e.session_id,"queued",{queue_position:t,queue_total:t,actions:this.config.cancelableQueued?[{type:"cancel"}]:[],content_preview:this.buildQueueItemTitle(e.content)}),this.config.queueTimeoutMs>0){const i=setTimeout(()=>{this.timeoutEvent(e.event_id)},this.config.queueTimeoutMs);i.unref(),this.timers.set(e.event_id,i)}}timeoutEvent(e){const t=this.queued.findIndex(s=>s.event_id===e);if(t<0)return;const[i]=this.queued.splice(t,1);this.timers.delete(e),this.callbacks.onStateChange(e,i.session_id,"failed",{reason:"queue timeout"}),this.broadcastQueuePositions(),this.drainNext()}startRunning(e){this.running.set(e.event_id,e),this.callbacks.onStateChange(e.event_id,e.session_id,"running",{actions:this.config.cancelableRunning?[{type:"stop"}]:[],content_preview:this.buildQueueItemTitle(e.content)}),this.callbacks.onDeliver(e)}drainNext(){for(;this.running.size<this.config.maxConcurrent&&this.queued.length>0;){const e=this.queued.shift();this.clearTimer(e.event_id),this.startRunning(e)}this.queued.length>0&&this.broadcastQueuePositions()}broadcastQueuePositions(){const e=this.queued.length;for(let t=0;t<e;t++){const i=this.queued[t];this.callbacks.onStateChange(i.event_id,i.session_id,"queued",{queue_position:t+1,queue_total:e,actions:this.config.cancelableQueued?[{type:"cancel"}]:[],content_preview:this.buildQueueItemTitle(i.content)})}}clearTimer(e){const t=this.timers.get(e);t&&(clearTimeout(t),this.timers.delete(e))}buildQueueItemTitle(e){const t=String(e??"").replace(/\s+/g," ").trim();return t?t.length>64?`${t.slice(0,64)}...`:t:"Message"}}export{o as EventQueue};
1
+ const r=25e3;class a{config;callbacks;running=new Map;queued=[];timers=new Map;composingTimers=new Map;constructor(e,t){this.config=e,this.callbacks=t}submit(e){return this.running.has(e.event_id)||this.queued.some(t=>t.event_id===e.event_id)?"accepted":this.running.size<this.config.maxConcurrent?(this.startRunning(e),"accepted"):this.config.maxQueued<=0||this.queued.length>=this.config.maxQueued?(this.callbacks.onRejected(e,"queue full"),"rejected"):(this.enqueue(e),"accepted")}cancel(e){const t=this.queued.findIndex(i=>i.event_id===e);if(t>=0){if(!this.config.cancelableQueued)return!1;const[i]=this.queued.splice(t,1);return this.clearTimer(e),this.callbacks.onStateChange(e,i.session_id,"canceled",{reason:"canceled by user"}),this.broadcastQueuePositions(),this.drainNext(),this.checkStopComposing(i.session_id),!0}return this.running.has(e)&&this.config.cancelableRunning?(this.callbacks.onCancelRunning(e),!0):!1}complete(e){const i=this.running.get(e)?.session_id;this.running.delete(e),this.clearTimer(e),this.drainNext(),i&&this.checkStopComposing(i)}clear(e){const t=[],i=[];for(const n of this.queued)n.session_id===e?(this.clearTimer(n.event_id),this.callbacks.onStateChange(n.event_id,e,"canceled",{reason:"queue cleared"}),t.push(n.event_id)):i.push(n);return this.queued=i,t.length>0&&this.broadcastQueuePositions(),this.checkStopComposing(e),t}snapshot(e){const t=[...this.running.values()].filter(s=>s.session_id===e),i=t.map(s=>s.event_id),n=t.map(s=>({event_id:s.event_id,content_preview:this.buildQueueItemTitle(s.content),title:this.buildQueueItemTitle(s.content),summary:this.buildQueueItemTitle(s.content)})),u=this.queued.flatMap((s,o)=>s.session_id===e?[{event_id:s.event_id,position:o+1,content_preview:this.buildQueueItemTitle(s.content),title:this.buildQueueItemTitle(s.content),summary:this.buildQueueItemTitle(s.content)}]:[]);return{running:i,running_items:n,queued:u}}hasCapacity(){return this.running.size<this.config.maxConcurrent}get runningCount(){return this.running.size}get queuedCount(){return this.queued.length}destroy(){for(const e of this.timers.values())clearTimeout(e);this.timers.clear();for(const e of this.composingTimers.values())clearInterval(e);this.composingTimers.clear(),this.queued=[],this.running.clear()}enqueue(e){this.queued.push(e);const t=this.queued.length;if(this.callbacks.onStateChange(e.event_id,e.session_id,"queued",{queue_position:t,queue_total:t,actions:this.config.cancelableQueued?[{type:"cancel"}]:[],content_preview:this.buildQueueItemTitle(e.content)}),this.config.queueTimeoutMs>0){const i=setTimeout(()=>{this.timeoutEvent(e.event_id)},this.config.queueTimeoutMs);i.unref(),this.timers.set(e.event_id,i)}this.ensureComposing(e.session_id)}timeoutEvent(e){const t=this.queued.findIndex(n=>n.event_id===e);if(t<0)return;const[i]=this.queued.splice(t,1);this.timers.delete(e),this.callbacks.onStateChange(e,i.session_id,"failed",{reason:"queue timeout"}),this.broadcastQueuePositions(),this.drainNext(),this.checkStopComposing(i.session_id)}startRunning(e){this.running.set(e.event_id,e),this.callbacks.onStateChange(e.event_id,e.session_id,"running",{actions:this.config.cancelableRunning?[{type:"stop"}]:[],content_preview:this.buildQueueItemTitle(e.content)}),this.ensureComposing(e.session_id),this.callbacks.onDeliver(e)}drainNext(){for(;this.running.size<this.config.maxConcurrent&&this.queued.length>0;){const e=this.queued.shift();this.clearTimer(e.event_id),this.startRunning(e)}this.queued.length>0&&this.broadcastQueuePositions()}broadcastQueuePositions(){const e=this.queued.length;for(let t=0;t<e;t++){const i=this.queued[t];this.callbacks.onStateChange(i.event_id,i.session_id,"queued",{queue_position:t+1,queue_total:e,actions:this.config.cancelableQueued?[{type:"cancel"}]:[],content_preview:this.buildQueueItemTitle(i.content)})}}clearTimer(e){const t=this.timers.get(e);t&&(clearTimeout(t),this.timers.delete(e))}buildQueueItemTitle(e){const t=String(e??"").replace(/\s+/g," ").trim();return t?t.length>64?`${t.slice(0,64)}...`:t:"Message"}ensureComposing(e){if(!this.callbacks.onComposing||this.composingTimers.has(e))return;this.callbacks.onComposing(e,!0);const t=setInterval(()=>{this.sessionHasEvents(e)?this.callbacks.onComposing(e,!0):this.stopComposing(e)},25e3);t.unref(),this.composingTimers.set(e,t)}checkStopComposing(e){this.sessionHasEvents(e)||this.stopComposing(e)}stopComposing(e){const t=this.composingTimers.get(e);t&&(clearInterval(t),this.composingTimers.delete(e)),this.callbacks.onComposing?.(e,!1)}sessionHasEvents(e){for(const t of this.running.values())if(t.session_id===e)return!0;return this.queued.some(t=>t.session_id===e)}}export{a as EventQueue};
@@ -1 +1,4 @@
1
- import{log as a}from"../core/log/index.js";import{splitTextForAibotProtocol as m}from"../core/protocol/protocol-text.js";import{isThinkingEventDropped as g,isToolEventDropped as d,normalizeRuntimeConfigSnapshot as p}from"./runtime-config.js";class E{handle;connectorRuntimeConfig;eventRuntimeConfigs=new Map;thinkingSeqMap=new Map;toolCardSeq=0;bufferedEventTexts=new Map;constructor(e){this.connectorRuntimeConfig={...e}}bind(e){this.handle=e}setGlobalRuntimeConfig(e){this.connectorRuntimeConfig=e}getGlobalRuntimeConfig(){return this.connectorRuntimeConfig}captureEventRuntimeConfig(e){const t=p(e.connector_runtime_config)??this.connectorRuntimeConfig;this.eventRuntimeConfigs.set(e.event_id,{responseDelivery:t.responseDelivery,toolEvents:t.toolEvents,thinkingEvents:t.thinkingEvents})}resolveEventRuntimeConfig(e){if(e){const t=this.eventRuntimeConfigs.get(e);if(t)return t}return this.connectorRuntimeConfig}shouldDropToolDisplayEvent(e){return d(this.resolveEventRuntimeConfig(e))}shouldDropThinkingDisplayEvent(e){return g(this.resolveEventRuntimeConfig(e))}shouldDropCodexDisplayEvent(e,t){return this.shouldDropToolDisplayEvent(e)?t==="item/completed":!1}shouldDropAcpRawDisplayEvent(e,t){return!!(this.shouldDropToolDisplayEvent(e)&&(t==="tool_use"||t==="tool_result")||this.shouldDropThinkingDisplayEvent(e)&&t==="thinking")}sendReply(e,t,n,i,s,o){if(!o?.skipToolFilter&&this.shouldDropToolDisplayEvent(e)&&_(n,s))return;if(this.resolveEventRuntimeConfig(e).responseDelivery==="stream"&&!s){const r=m(n),f=e?`reply_stream_${e}`:`reply_stream_${Date.now()}`;if(r.length===0){this.sendStreamChunk(e,t,"",1,!0,f,i);return}for(let u=0;u<r.length;u++)this.sendStreamChunk(e,t,r[u],u+1,!1,f,i);this.sendStreamChunk(e,t,"",r.length+1,!0,f,i);return}this.handle.sendMsg({event_id:e,session_id:t,msg_type:1,content:n,quoted_message_id:i||void 0,...s?{extra:s}:{}})}sendStreamChunk(e,t,n,i,s,o,l){if(this.finalizeThinking(e,t),this.resolveEventRuntimeConfig(e).responseDelivery==="single_message"&&e){this.bufferStreamChunk(e,t,n,s,l);return}this.handle.sendStreamChunk({event_id:e,session_id:t,delta_content:n,chunk_seq:i,is_finish:s,...o?{client_msg_id:o}:{},...l?{quoted_message_id:l}:{}})}sendEventResult(e,t,n,i){this.finalizeThinking(e,""),this.flushBufferedStreamText(e),this.handle.sendEventResult({event_id:e,status:t,...n?{msg:n}:{},...i?{code:i}:{},updated_at:Date.now()}),this.eventRuntimeConfigs.delete(e)}sendThinking(e,t,n){if(this.shouldDropThinkingDisplayEvent(e))return;const i=(this.thinkingSeqMap.get(e)??0)+1;this.thinkingSeqMap.set(e,i),this.handle.sendStreamChunk({event_id:e,session_id:t,delta_content:n,chunk_seq:i,is_finish:!1,client_msg_id:`${e}_thinking`})}sendToolExecutionCard(e,t,n,i){if(this.shouldDropToolDisplayEvent(e)){a.info("send-ctrl",`[tool-card] DROP event=${e} session=${t} summary=${n.summaryText}`);return}const o={summary_text:n.summaryText};n.detailText&&(o.detail_text=n.detailText),a.info("send-ctrl",`[tool-card] SEND event=${e} session=${t} summary=${n.summaryText}`),this.handle.sendMsg({event_id:e,session_id:t,client_msg_id:i??`tool_${++this.toolCardSeq}_${Date.now()}`,msg_type:1,content:"",extra:{channel_data:{grix:{toolExecution:o}},agent_api_origin:!0}})}finalizeThinking(e,t){const n=this.thinkingSeqMap.get(e);n!==void 0&&(this.thinkingSeqMap.delete(e),t&&this.handle.sendStreamChunk({event_id:e,session_id:t,delta_content:"",chunk_seq:n+1,is_finish:!0,client_msg_id:`${e}_thinking`}))}bufferOnly(e,t,n,i,s){this.bufferStreamChunk(e,t,n,i,s)}bufferStreamChunk(e,t,n,i,s){const o=this.bufferedEventTexts.get(e);o?(n&&(o.text+=n),!o.quotedMessageId&&s&&(o.quotedMessageId=s)):this.bufferedEventTexts.set(e,{sessionId:t,text:n??"",quotedMessageId:s||void 0}),i&&this.flushBufferedStreamText(e)}flushBufferedStreamText(e){const t=this.bufferedEventTexts.get(e);t&&(this.bufferedEventTexts.delete(e),t.text&&this.handle.sendMsg({event_id:e,session_id:t.sessionId,msg_type:1,content:t.text,...t.quotedMessageId?{quoted_message_id:t.quotedMessageId}:{}}))}}function _(h,e){return e&&typeof e=="object"&&e.channel_data?.grix?.toolExecution?!0:/^\[tool\]/.test(h)||/^\[tool result\]/.test(h)}export{E as SendController};
1
+ import{log as m}from"../core/log/index.js";import{splitTextForAibotProtocol as p,AIBOT_PROTOCOL_MAX_RUNES as k}from"../core/protocol/protocol-text.js";import{isThinkingEventDropped as C,isToolEventDropped as E,normalizeRuntimeConfigSnapshot as y}from"./runtime-config.js";class q{handle;connectorRuntimeConfig;eventRuntimeConfigs=new Map;thinkingSeqMap=new Map;toolCardSeq=0;bufferedEventTexts=new Map;chunkSeqMap=new Map;constructor(e){this.connectorRuntimeConfig={...e}}bind(e){this.handle=e}setGlobalRuntimeConfig(e){this.connectorRuntimeConfig=e}getGlobalRuntimeConfig(){return this.connectorRuntimeConfig}captureEventRuntimeConfig(e){const t=y(e.connector_runtime_config)??this.connectorRuntimeConfig;this.eventRuntimeConfigs.set(e.event_id,{responseDelivery:t.responseDelivery,toolEvents:t.toolEvents,thinkingEvents:t.thinkingEvents})}resolveEventRuntimeConfig(e){if(e){const t=this.eventRuntimeConfigs.get(e);if(t)return t}return this.connectorRuntimeConfig}shouldDropToolDisplayEvent(e){return E(this.resolveEventRuntimeConfig(e))}shouldDropThinkingDisplayEvent(e){return C(this.resolveEventRuntimeConfig(e))}shouldDropCodexDisplayEvent(e,t){return this.shouldDropToolDisplayEvent(e)?t==="item/completed":!1}shouldDropAcpRawDisplayEvent(e,t){return!!(this.shouldDropToolDisplayEvent(e)&&(t==="tool_use"||t==="tool_result")||this.shouldDropThinkingDisplayEvent(e)&&t==="thinking")}sendReply(e,t,n,s,i,o){if(!o?.skipToolFilter&&this.shouldDropToolDisplayEvent(e)&&D(n,i))return;if(this.resolveEventRuntimeConfig(e).responseDelivery==="stream"&&!i){const l=p(n),u=e?`reply_stream_${e}`:`reply_stream_${Date.now()}`;if(l.length===0){this.handle.sendMsg({event_id:e,session_id:t,msg_type:1,content:n||" ",quoted_message_id:s||void 0});return}for(let r=0;r<l.length;r++){const a=r===l.length-1;this.sendStreamChunk(e,t,l[r],r+1,a,u,s)}return}this.handle.sendMsg({event_id:e,session_id:t,msg_type:1,content:n,quoted_message_id:s||void 0,...i?{extra:i}:{}})}sendStreamChunk(e,t,n,s,i,o,f){if(this.finalizeThinking(e,t),this.resolveEventRuntimeConfig(e).responseDelivery==="single_message"&&e){this.bufferStreamChunk(e,t,n,i,f);return}const u=n||(i?`
2
+ `:"");if(!u&&!i)return;!n&&i&&m.warn("send-ctrl",`stream_chunk delta_content empty at finish, patched to newline event=${e} session=${t} seq=${s}`);const r=p(u,k);r.length===0&&r.push(u||`
3
+ `);const a=`${e}:${o??"default"}`;for(let h=0;h<r.length;h++){const _=h===r.length-1,c=this.chunkSeqMap.get(a)??0,d=h===0&&s>c?s:c+1;this.chunkSeqMap.set(a,d),this.handle.sendStreamChunk({event_id:e,session_id:t,delta_content:r[h],chunk_seq:d,is_finish:_&&i,...o?{client_msg_id:o}:{},...f?{quoted_message_id:f}:{}})}}sendEventResult(e,t,n,s){this.finalizeThinking(e,""),this.flushBufferedStreamText(e),this.handle.sendEventResult({event_id:e,status:t,...n?{msg:n}:{},...s?{code:s}:{},updated_at:Date.now()}),this.eventRuntimeConfigs.delete(e);for(const i of this.chunkSeqMap.keys())i.startsWith(`${e}:`)&&this.chunkSeqMap.delete(i)}sendThinking(e,t,n){if(this.shouldDropThinkingDisplayEvent(e))return;const s=(this.thinkingSeqMap.get(e)??0)+1;this.thinkingSeqMap.set(e,s),this.handle.sendStreamChunk({event_id:e,session_id:t,delta_content:n,chunk_seq:s,is_finish:!1,client_msg_id:`${e}_thinking`})}sendToolExecutionCard(e,t,n,s){if(this.shouldDropToolDisplayEvent(e)){m.info("send-ctrl",`[tool-card] DROP event=${e} session=${t} summary=${n.summaryText}`);return}const o={summary_text:n.summaryText};n.detailText&&(o.detail_text=n.detailText),m.info("send-ctrl",`[tool-card] SEND event=${e} session=${t} summary=${n.summaryText}`),this.handle.sendMsg({event_id:e,session_id:t,client_msg_id:s??`tool_${++this.toolCardSeq}_${Date.now()}`,msg_type:1,content:"",extra:{channel_data:{grix:{toolExecution:o}},agent_api_origin:!0}})}finalizeThinking(e,t){const n=this.thinkingSeqMap.get(e);n!==void 0&&(this.thinkingSeqMap.delete(e),t&&this.handle.sendStreamChunk({event_id:e,session_id:t,delta_content:`
4
+ `,chunk_seq:n+1,is_finish:!0,client_msg_id:`${e}_thinking`}))}bufferOnly(e,t,n,s,i){this.bufferStreamChunk(e,t,n,s,i)}bufferStreamChunk(e,t,n,s,i){const o=this.bufferedEventTexts.get(e);o?(n&&(o.text+=n),!o.quotedMessageId&&i&&(o.quotedMessageId=i)):this.bufferedEventTexts.set(e,{sessionId:t,text:n??"",quotedMessageId:i||void 0}),s&&this.flushBufferedStreamText(e)}flushBufferedStreamText(e){const t=this.bufferedEventTexts.get(e);t&&(this.bufferedEventTexts.delete(e),t.text&&this.handle.sendMsg({event_id:e,session_id:t.sessionId,msg_type:1,content:t.text,...t.quotedMessageId?{quoted_message_id:t.quotedMessageId}:{}}))}}function D(g,e){return e&&typeof e=="object"&&e.channel_data?.grix?.toolExecution?!0:/^\[tool\]/.test(g)||/^\[tool result\]/.test(g)}export{q as SendController};
@@ -0,0 +1 @@
1
+ import{createServer as o}from"node:http";import{log as i}from"../log/logger.js";class c{server=null;token;handler=null;upgradeHandler=null;constructor(e){this.token=e}setAgentHandler(e){this.handler=e}setUpgradeHandler(e){this.upgradeHandler=e}async start(e){return new Promise((r,t)=>{this.server=o((n,a)=>this.handleRequest(n,a)),this.server.listen(e,"127.0.0.1",()=>{i.info("admin",`Listening on 127.0.0.1:${e}`),r()}),this.server.on("error",t)})}async stop(){if(this.server)return new Promise(e=>{this.server.close(()=>e())})}handleRequest(e,r){const t=e.headers.authorization;if(!t||t!==`Bearer ${this.token}`){this.json(r,401,{error:"unauthorized"});return}const n=e.url??"",a=e.method??"";if(n==="/api/agents"&&a==="GET")this.handleList(r);else if(n==="/api/agents"&&a==="POST")this.readBody(e).then(s=>this.handleAdd(r,s)).catch(s=>this.error(r,s));else if(a==="DELETE"&&n.startsWith("/api/agents/")){const s=decodeURIComponent(n.slice(12));this.handleRemove(r,s)}else if(a==="POST"&&n.match(/^\/api\/agents\/[^/]+\/restart$/)){const s=decodeURIComponent(n.slice(12,n.lastIndexOf("/restart")));this.handleRestart(r,s)}else n==="/api/upgrade"&&a==="GET"?this.handleCheckUpgrade(r):n==="/api/upgrade"&&a==="POST"?this.handleTriggerUpgrade(r):this.json(r,404,{error:"not_found"})}handleList(e){try{const r=this.handler?.list()??[];this.json(e,200,r)}catch(r){this.error(e,r)}}async handleAdd(e,r){try{const t=await this.handler.add(r);this.json(e,201,t??{ok:!0})}catch(t){this.error(e,t)}}handleRemove(e,r){this.handler.remove(r).then(()=>{e.writeHead(204),e.end()}).catch(t=>this.error(e,t))}handleRestart(e,r){this.handler.restart(r).then(()=>{this.json(e,200,{ok:!0})}).catch(t=>this.error(e,t))}handleCheckUpgrade(e){if(!this.upgradeHandler){this.json(e,501,{error:"upgrade not configured"});return}this.upgradeHandler.check().then(r=>{this.json(e,200,r)}).catch(r=>this.error(e,r))}handleTriggerUpgrade(e){if(!this.upgradeHandler){this.json(e,501,{error:"upgrade not configured"});return}this.upgradeHandler.trigger(),this.json(e,200,{ok:!0,message:"upgrade check triggered"})}error(e,r){const t=r;t.code==="NOT_FOUND"?this.json(e,404,{error:t.message??"not found"}):(i.error("admin",`Handler error: ${t.message??r}`),this.json(e,500,{error:t.message??"internal error"}))}json(e,r,t){const n=JSON.stringify(t);e.writeHead(r,{"Content-Type":"application/json"}),e.end(n)}readBody(e){return new Promise((r,t)=>{let n="";e.on("data",a=>{n+=a}),e.on("end",()=>{try{r(JSON.parse(n))}catch{t(new Error("invalid JSON body"))}}),e.on("error",t)})}}export{c as AdminServer};
@@ -0,0 +1 @@
1
+ import{randomBytes as t}from"node:crypto";import{readFileSync as n,writeFileSync as o,chmodSync as i}from"node:fs";function f(){return t(32).toString("hex")}function m(r,e){o(r,e,"utf-8"),process.platform!=="win32"&&i(r,384)}function p(r){try{return n(r,"utf-8").trim()}catch{return null}}export{f as generateToken,p as readTokenFile,m as writeTokenFile};