grix-connector 3.5.0 → 3.5.2

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.
@@ -1,11 +1,11 @@
1
- import{execFileSync as R}from"node:child_process";import{createInterface as B}from"node:readline";import{EventEmitter as P}from"node:events";import{stat as $}from"node:fs/promises";import{join as S,resolve as J}from"node:path";import{fileURLToPath as X}from"node:url";import{homedir as G}from"node:os";import{syncDefaultSkillsToDir as V}from"../../default-skills/index.js";import{resolveCommandPath as Y,spawnCommand as K,killProcessGroup as L,hasChildProcesses as Z}from"../../core/runtime/spawn.js";import{InternalApiServer as Q}from"../../core/mcp/internal-api-server.js";import{log as r}from"../../core/log/index.js";import{resolveClientVersion as ee}from"../../core/util/client-version.js";import{isUserVisibleAgentMessagePhase as te}from"../../core/util/codex-output-policy.js";import{SessionBindingStore as ie}from"../../core/persistence/session-binding-store.js";import{ensureCodexProjectTrusted as se,isCodexCommand as ne,readCodexProviderSettings as C}from"./codex-trust.js";import{checkCodexSessionActivity as oe}from"./session-activity.js";import{findRolloutFile as re}from"./rollout-locator.js";import{checkCodexLock as N}from"./lock-monitor.js";import{resolveCliPath as ae,getCliVersion as de}from"../../core/util/cli-probe.js";import{scanSkills as ce}from"../claude/skill-scanner.js";const le=60*1e3,he=600*1e3,ue=5*1e3,O=2,pe=20,fe=8e3,D=80,me=120*1e3,ge=[{id:"gpt-5.3-codex",displayName:"GPT-5.3 Codex",defaultReasoningEffort:null,supportedReasoningEfforts:[],defaultServiceTier:null,serviceTiers:[],isDefault:!0}],j=[{id:"default",displayName:"Default"},{id:"plan",displayName:"Plan"}];function F(a,e){const t=String(a??"").trim()||"codex",s=Y(t,e);if(s!==t)return s;const i=process.platform==="win32"?[]:["/opt/homebrew/bin/codex","/usr/local/bin/codex","/usr/bin/codex"],n=process.platform!=="win32"?[]:[S(process.env.LOCALAPPDATA??"","npm","codex.cmd"),S(process.env.APPDATA??"","npm","codex.cmd"),S(process.env.LOCALAPPDATA??"","Programs","codex","codex.exe")],o=[...i,...n];for(const d of o)try{return require("node:fs").accessSync(d,require("node:fs").constants.X_OK),d}catch{continue}return r.warn("codex-adapter",`resolveCodexCommandPath: failed to resolve "${t}". envPath=${e?"provided":"missing (using process.env.PATH)"}. fallbacks tried: [${o.join(", ")}]`),t}class M extends P{type="codex";config;callbacks;process=null;alive=!1;stopped=!1;internalApi=null;bridgeStatus="starting";pendingRequests=new Map;requestId=0;threadId=null;initialized=!1;cwd;approvalPolicy;sandboxMode;model;collaborationMode;reasoningEffort;serviceTier;idleTimer=null;lastProgressAt=0;lastIdleCheckAt=0;idleNoProgressCount=0;inFlightToolOps=0;pendingApprovals=new Map;needsHistoryInjection=!1;lastInjectedSessionId=null;identityInjectedThreadId=null;bindingStore=null;aibotSessionId="";threadResumePending=!1;autoTrustProject="auto";codexHome;codexModelOptions=[];codexModelOptionsCachedAt=0;static MODEL_CACHE_TTL_MS=3600*1e3;rateLimitSnapshot=null;currentThreadTokenUsage=null;currentModelContextWindow=null;constructor(e,t){super(),this.config=e,this.callbacks=t;const s=e.options??{};if(this.approvalPolicy=s.approvalPolicy??"never",this.sandboxMode=typeof s.sandboxMode=="string"&&s.sandboxMode.trim()&&s.sandboxMode.trim()!=="default"?s.sandboxMode.trim():void 0,this.aibotSessionId=String(s.aibotSessionId??"").trim(),this.bindingStore=s.bindingStore instanceof ie?s.bindingStore:null,this.model=s.model,this.collaborationMode=s.collaborationMode,this.reasoningEffort=s.reasoningEffort,this.serviceTier=s.serviceTier,this.bindingStore&&this.aibotSessionId&&(this.model=this.model??this.bindingStore.getCodexModelId(this.aibotSessionId),this.collaborationMode=this.collaborationMode??this.bindingStore.getCodexModeId(this.aibotSessionId),this.reasoningEffort=this.reasoningEffort??this.bindingStore.getCodexReasoningEffort(this.aibotSessionId),this.serviceTier=this.serviceTier??this.bindingStore.getCodexServiceTier(this.aibotSessionId),this.sandboxMode=this.sandboxMode??this.bindingStore.getCodexSandboxMode(this.aibotSessionId)),this.autoTrustProject=typeof s.autoTrustProject=="boolean"?s.autoTrustProject:"auto",this.codexHome=typeof s.codexHome=="string"&&s.codexHome.trim()?s.codexHome.trim():void 0,this.cwd=this.resolveCwd(),this.bindingStore&&this.aibotSessionId){const i=this.bindingStore.getCodexThreadId(this.aibotSessionId);i&&(this.threadId=i,this.threadResumePending=!0)}}resolveCwd(){if(this.bindingStore&&this.aibotSessionId){const e=this.bindingStore.get(this.aibotSessionId);if(e?.cwd)return e.cwd}return process.cwd()}async start(){await this.ensureProjectTrusted(),await this.startInternalApiAndRegisterMcp(),await this.spawnCodex(),await this.initializeHandshake(),await this.notifyBindingReadyWithContext(),r.info("codex-adapter",`Ready (pid=${this.process?.pid})`)}async ensureProjectTrusted(){if(this.shouldAutoTrustProject())try{const e=await se(this.cwd,{codexHome:this.codexHome});e.changed&&r.info("codex-adapter",`Trusted Codex project ${this.cwd} in ${e.configPath}`)}catch(e){const t=e instanceof Error?e.message:String(e);throw r.error("codex-adapter",`Failed to trust Codex project ${this.cwd}: ${t}`),e}}async startInternalApiAndRegisterMcp(){try{this.internalApi=new Q,this.internalApi.setInvokeHandler(async(d,h,f,m)=>this.callbacks.agentInvoke(d,h,m)),await this.internalApi.start(0),r.info("codex-adapter",`Internal API started at ${this.internalApi.url}`);const e=this.getMcpConfig(),t={...process.env,...this.config.env},s=typeof t.PATH=="string"?t.PATH:void 0,i=F(this.config.command||"codex",s);try{R(i,["mcp","remove",e.name],{env:t,timeout:1e4,stdio:"ignore"})}catch{}R(i,["mcp","add",e.name,"--",e.command,...e.args],{env:t,timeout:1e4,stdio:"ignore"}),r.info("codex-adapter",`Registered MCP server: ${e.name}`);const n=S(G(),".codex","skills"),o=V(n);o.length>0&&r.info("codex-adapter",`Synced connector skills to ${n}: [${o.join(", ")}]`)}catch(e){r.warn("codex-adapter",`Failed to register MCP tools (non-fatal): ${e instanceof Error?e.message:String(e)}`)}}shouldAutoTrustProject(){return this.autoTrustProject===!1?!1:this.autoTrustProject===!0?!0:ne(this.config.command)}notifyBindingReady(){!this.aibotSessionId||!this.cwd||this.callbacks.sendUpdateBindingCard(this.aibotSessionId,"ready",this.cwd)}async notifyBindingReadyWithContext(){if(!(!this.aibotSessionId||!this.cwd))try{this.callbacks.sendUpdateBindingCard(this.aibotSessionId,"ready",this.cwd,await this.buildToolbarContextResult("binding_ready"))}catch(e){r.warn("codex-adapter",`Failed to attach toolbar context to binding update: ${e instanceof Error?e.message:String(e)}`),this.notifyBindingReady()}}externalProviderCheckedAt=0;externalProviderCached=!1;static EXTERNAL_PROVIDER_CACHE_TTL_MS=60*1e3;isExternalProvider(){const e=Date.now();if(e-this.externalProviderCheckedAt<M.EXTERNAL_PROVIDER_CACHE_TTL_MS)return this.externalProviderCached;const t=C(this.codexHome);return this.externalProviderCached=!!(t.baseUrl&&q(t.baseUrl)),this.externalProviderCheckedAt=e,this.externalProviderCached}effectiveServiceTier(){if(!this.serviceTier||this.isExternalProvider())return;const e=this.getModelOption(this.currentModelId());if(!(e&&!e.serviceTiers.some(t=>t.id===this.serviceTier)))return this.serviceTier}getEffortMeta(){if(this.isExternalProvider())return{};const e=this.currentModelId(),t=this.getModelOption(e),s={},i=t?.supportedReasoningEfforts??[];i.length>0&&(s.available_efforts=i,s.reasoning_effort=this.reasoningEffort??t?.defaultReasoningEffort??null);const n=t?.serviceTiers??[];return n.length>0&&(s.available_service_tiers=n,s.service_tier=this.effectiveServiceTier()??t?.defaultServiceTier??null),s}async stop(){this.stopped=!0,this.alive=!1,this.stopComposing(),this.clearIdleTimer(),this.rejectAllPending("adapter stopped"),this.pendingAutoCompact=!1,this.compacting=!1,this.compactingTimer&&(clearTimeout(this.compactingTimer),this.compactingTimer=null);for(const t of this.compactionDeferred.splice(0))this.callbacks.sendEventResult(t.event_id,"failed","adapter stopped");for(const t of this.steerDeferred.splice(0))this.callbacks.sendEventResult(t.event_id,"failed","adapter stopped");this.internalApi&&(await this.internalApi.stop(),this.internalApi=null);const e=this.process;if(this.process=null,e?.pid&&typeof e.once=="function")L(e,"SIGTERM"),await Promise.race([new Promise(s=>{e.once("exit",()=>s(!0))}),new Promise(s=>{setTimeout(()=>s(!1),5e3)})])||L(e,"SIGKILL");else if(e)try{e.kill("SIGTERM")}catch{}}isAlive(){return this.alive}async createSession(e){if(this.initialized||await this.initializeHandshake(),this.threadId||await this.startNewThread(),!this.threadId)throw new Error("Failed to create session: thread ID is missing");return await this.notifyBindingReadyWithContext(),this.threadId}async resumeSession(e,t){await this.ensureThreadResumed()}async destroySession(e){this.threadId=null,this.threadResumePending=!1,this.persistThreadId(void 0)}sendPrompt(e){const t=new E(e.adapterSessionId);return this.runTurn(e,t).catch(s=>{t.emitError(s instanceof Error?s:new Error(String(s)))}),t}async cancel(e){if(this.threadId)try{await this.sendRequest("turn/interrupt",{threadId:this.threadId,turnId:this.currentTurnId??""},5e3)}catch{}}setPermissionHandler(e){this.permissionHandler=e}permissionHandler=null;async ping(e){if(!this.alive||!this.process)return!1;if(!this.process.pid){const t=!!(this.process.stdin&&!this.process.stdin.destroyed),s=!!(this.process.stdout&&!this.process.stdout.destroyed);return t&&s}if(!this.process.stdin||this.process.stdin.destroyed)return!!(this.process.stdout&&!this.process.stdout.destroyed);try{const t=++this.requestId,s={jsonrpc:"2.0",id:t,method:"ping",params:{}};return await new Promise(i=>{const n=setTimeout(()=>{this.pendingRequests.delete(String(t)),i(!1)},e);this.pendingRequests.set(String(t),{resolve:()=>i(!0),reject:()=>i(!1),timer:n}),this.process.stdin.write(`${JSON.stringify(s)}
2
- `)})}catch{return!1}}getStatus(){return{alive:this.alive,busy:this.currentTurnId!==null||this.compacting,sessions:this.threadId?1:0}}getActiveEventIds(){return this.activeEventId?[this.activeEventId]:[]}clearActiveEventForShutdown(){this.clearIdleTimer(),this.activeEventId=null}setModel(e){this.model=e,this.persistCodexContext(),r.info("codex-adapter",`Model set to: ${e}`)}setMode(e){const t=T(e);if(!t){r.info("codex-adapter",`Ignoring unsupported mode: ${e}`);return}this.collaborationMode=t,this.persistCodexContext(),r.info("codex-adapter",`Mode set to: ${t}`)}setReasoningEffort(e){this.reasoningEffort=e,this.persistCodexContext(),r.info("codex-adapter",`Reasoning effort set to: ${e}`)}setServiceTier(e){const t=e.trim();this.serviceTier=t.toLowerCase()==="default"?void 0:t,this.persistCodexContext(),r.info("codex-adapter",`Service tier set to: ${this.serviceTier??"default"}`)}setSandboxMode(e){this.sandboxMode=e,this.persistCodexContext(),r.info("codex-adapter",`Sandbox mode set to: ${e} (applied on next restart)`)}async threadCompact(){if(!this.threadId)throw new Error("No active thread");return this.sendRequest("thread/compact/start",{threadId:this.threadId},12e4)}async threadRollback(e){if(!this.threadId)throw new Error("No active thread");return this.sendRequest("thread/rollback",{threadId:this.threadId,numTurns:e},1e4)}getThreadId(){return this.threadId}getMcpConfig(){if(!this.internalApi)return null;const e=J(X(import.meta.url),"../../../mcp/acp-mcp-server.js");return{name:"grix-connector-tools",command:process.execPath,args:[e,"--api-url",this.internalApi.url]}}async hasBackgroundWork(){const e=this.process?.pid;return e?Z(e,[e]):!1}onAgentProfileChanged(){this.identityInjectedThreadId!==null&&(r.info("codex-adapter",`agent profile changed; clearing identity-injected thread record (was ${this.identityInjectedThreadId})`),this.identityInjectedThreadId=null)}async probe(e){const t=this.config.command||"codex",s=await ae(t),i=s!==null;let n=null,o;if(i){const w=await de(t);n=w.version,w.error&&(o=w.error)}else o={code:"cli_not_found",message:`command not found: ${t}`};const h=C().baseUrl??null,f=this.currentModelId()||null,m=this.getStatus(),u=e?.conversation?{attempted:!0,ok:!1,latency_ms:null,error:{code:"unsupported",message:"codex does not support minimal prompt probe"}}:{attempted:!1,ok:!1,latency_ms:null},p=await this.probeSessionRecord(),g=N({codexHome:this.codexHome});return{cli:{command:t,installed:i,path:s,version:n,...o?{error:o}:{}},conversation:u,config:{model:f,base_url:h,source:{model:f?"runtime":"unknown",base_url:h?"file":"unknown"}},process:{started:!!this.process,alive:m.alive,busy:m.busy},session:p,lock:{present:g.present,locked:g.locked,stale:g.stale,path:g.path}}}async probeSessionRecord(){const e=this.threadId?re(this.threadId,this.codexHome):null;if(!e)return{recordPath:null,lastActivityMs:null,freshMs:null};try{const t=await $(e);return{recordPath:e,lastActivityMs:t.mtimeMs,freshMs:Date.now()-t.mtimeMs}}catch{return{recordPath:e,lastActivityMs:null,freshMs:null}}}getSupportedCommands(){return[{name:"compact",description:"Compact thread to reduce context size"},{name:"model",description:"List or set model",args:"[model_id]"},{name:"mode",description:"List or set collaboration mode",args:"[default|plan]"},{name:"rollback",description:"Roll back thread turns",args:"[num_turns]"},{name:"interrupt",description:"Interrupt current turn"},{name:"rate_limits",description:"Show current rate limits"},{name:"status",description:"Show thread and session status"},{name:"skills",description:"List available skills"}]}async execCommand(e,t,s){try{switch(e){case"compact":{if(!this.threadId)return{status:"failed",message:"No active thread"};if(this.compacting)return{status:"failed",message:"Compaction already in progress"};if(this.currentTurnId&&(await this.cancel(this.threadId).catch(()=>{}),this.activeEventId&&this.callbacks.sendEventResult(this.activeEventId,"canceled","compacting"),this.clearActive()),await this.ensureThreadResumed(),this.currentTurnId)return{status:"failed",message:"agent busy"};if(this.needsHistoryInjection)return{status:"failed",message:"The session has expired and there is no history to compact. Please send a new message and try again."};this.compacting=!0;const i=new Promise(n=>{this.compactionDoneResolver=n});try{return await this.threadCompact(),this.armCompactionFallback(),await i,{status:"ok",message:"Thread compacted"}}catch(n){this.finishCompaction("compact-error");const o=n instanceof Error?n.message:String(n);if(/thread.*(not found|not exist|unknown|invalid)/i.test(o)||/not found.*thread/i.test(o))return this.threadId=null,this.persistThreadId(void 0),{status:"failed",message:"The session has expired and there is no history to compact. Please send a new message and try again."};throw n}}case"model":{const i=t.trim();return i?this.getModelOptions().some(o=>o.id===i)?(this.setModel(i),{status:"ok",message:`Model set to ${i}`}):{status:"failed",message:`Unknown model: ${i}`}:{status:"ok",message:`Current: ${this.currentModelId()}`,data:{models:this.getModelOptions()}}}case"mode":{const i=t.trim();if(i){const n=T(i);return n?(this.setMode(n),{status:"ok",message:`Mode set to ${n}`}):{status:"failed",message:`Unknown mode: ${i}. Supported: default, plan`}}return{status:"ok",message:`Current: ${this.collaborationMode??"default"}`,data:{modes:j}}}case"rollback":{if(!this.threadId)return{status:"failed",message:"No active thread"};const i=Math.max(1,parseInt(t.trim(),10)||1);return this.currentTurnId&&(await this.cancel(this.threadId).catch(()=>{}),this.activeEventId&&this.callbacks.sendEventResult(this.activeEventId,"canceled","rolled back"),this.clearActive()),await this.threadRollback(i),{status:"ok",message:`Rolled back ${i} turn(s)`}}case"interrupt":return this.threadId?(await this.cancel(this.threadId),this.activeEventId&&this.callbacks.sendEventResult(this.activeEventId,"canceled","interrupted"),this.clearActive(),{status:"ok",message:"Turn interrupted"}):{status:"failed",message:"No active thread"};case"rate_limits":return{status:"ok",message:this.rateLimitSnapshot?`Primary: ${this.rateLimitSnapshot.primary.usedPercent.toFixed(1)}%, Secondary: ${this.rateLimitSnapshot.secondary.usedPercent.toFixed(1)}%`:"Rate limits not available",data:{rateLimits:this.rateLimitSnapshot}};case"status":return{status:"ok",message:`Thread: ${this.threadId??"none"}, Model: ${this.currentModelId()}, Mode: ${this.collaborationMode??"default"}`,data:{threadId:this.threadId,model:this.currentModelId(),mode:this.collaborationMode??"default",cwd:this.cwd,alive:this.alive}};case"skills":{const i=ce({mode:"codex",projectDir:this.cwd}),n=i.map(o=>`- ${o.name}${o.trigger?` (${o.trigger})`:""}: ${o.description}`);return{status:"ok",message:n.length>0?n.join(`
3
- `):"No skills found",data:i}}default:return{status:"unsupported",message:`Unknown command: ${e}`}}}catch(i){return{status:"failed",message:i instanceof Error?i.message:String(i)}}}async handleLocalAction(e){const t=e.action_type??"",s=e.params??{};switch(r.info("codex-adapter",`handleLocalAction action_type=${t} action_id=${e.action_id} session_id=${s.session_id??""}`),t){case"set_model":{const i=x(c(s.model_id),c(s.modelId),c(s.value));return i&&this.setModel(i),this.callbacks.sendLocalActionResult(e.action_id,"ok",await this.buildToolbarContextResult("model_set")),{handled:!0,kind:"set_model"}}case"set_mode":{const i=s.mode_id;return i&&this.setMode(i),this.callbacks.sendLocalActionResult(e.action_id,"ok",await this.buildToolbarContextResult("mode_set")),{handled:!0,kind:"set_mode"}}case"set_reasoning_effort":{const i=x(c(s.reasoning_effort),c(s.reasoning_eff),c(s.effort));return i&&this.setReasoningEffort(i),this.callbacks.sendLocalActionResult(e.action_id,"ok",await this.buildToolbarContextResult("effort_set",!1)),{handled:!0,kind:"set_reasoning_effort"}}case"set_service_tier":{const i=x(c(s.service_tier),c(s.serviceTier),c(s.value));return i&&this.setServiceTier(i),this.callbacks.sendLocalActionResult(e.action_id,"ok",await this.buildToolbarContextResult("service_tier_set",!1)),{handled:!0,kind:"set_service_tier"}}case"set_sandbox_mode":{const i=x(c(s.sandbox_mode),c(s.sandboxMode),c(s.value));return i&&this.setSandboxMode(i),this.callbacks.sendLocalActionResult(e.action_id,"ok",await this.buildToolbarContextResult("sandbox_mode_set",!1)),{handled:!0,kind:"set_sandbox_mode"}}case"thread_compact":{r.info("codex-adapter",`thread_compact start action_id=${e.action_id} threadId=${this.threadId}`),this.currentTurnId&&(await this.cancel(this.threadId).catch(()=>{}),this.activeEventId&&this.callbacks.sendEventResult(this.activeEventId,"canceled","compacting"),this.clearActive());try{if(await this.ensureThreadResumed(),this.needsHistoryInjection)return r.info("codex-adapter",`thread_compact aborted: thread expired, new thread created action_id=${e.action_id}`),this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,"thread_expired","The session has expired and there is no history to compact. Please send a new message and try again."),{handled:!0,kind:"thread_compact"};this.compacting=!0;const i=new Promise(n=>{this.compactionDoneResolver=n});await this.threadCompact(),this.armCompactionFallback(),await i,r.info("codex-adapter",`thread_compact done action_id=${e.action_id}`),this.callbacks.sendLocalActionResult(e.action_id,"ok")}catch(i){this.finishCompaction("compact-error");const n=i instanceof Error?i.message:String(i);/thread.*(not found|not exist|unknown|invalid)/i.test(n)||/not found.*thread/i.test(n)?(r.warn("codex-adapter",`thread_compact thread expired, resetting threadId action_id=${e.action_id} err=${n}`),this.threadId=null,this.persistThreadId(void 0),this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,"thread_expired","The session has expired and there is no history to compact. Please send a new message and try again.")):(r.info("codex-adapter",`thread_compact failed action_id=${e.action_id} err=${n}`),this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,void 0,n))}return{handled:!0,kind:"thread_compact"}}case"get_context":return this.callbacks.sendLocalActionResult(e.action_id,"ok",{threadId:this.threadId,model:this.model,mode:this.collaborationMode,approvalPolicy:this.approvalPolicy,cwd:this.cwd,...await this.buildToolbarContextResult("context")}),{handled:!0,kind:"get_context"};case"exec_approve":case"file_approve":{const i=this.resolvePendingApproval(e);if(!i)return this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,"unknown_or_expired_approval_id","That approval request is no longer pending."),{handled:!0,kind:t};const n=s.decision||"allow-once",o=ve[n]??"accept";return this.sendApprovalDecision(i.requestId,o),this.pendingApprovals.delete(i.approvalId),this.callbacks.sendLocalActionResult(e.action_id,"ok",{approval_id:i.approvalId,approval_command_id:i.approvalCommandId,decision:n}),this.resumeAfterApproval(e,s,i),{handled:!0,kind:t}}case"exec_reject":case"file_reject":{const i=this.resolvePendingApproval(e);return i?(this.sendApprovalDecision(i.requestId,"deny"),this.pendingApprovals.delete(i.approvalId),this.callbacks.sendLocalActionResult(e.action_id,"ok",{approval_id:i.approvalId,approval_command_id:i.approvalCommandId,decision:"deny"}),this.resumeAfterApproval(e,s,i),{handled:!0,kind:t}):(this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,"unknown_or_expired_approval_id","That approval request is no longer pending."),{handled:!0,kind:t})}case"permission_approve":{const i=this.resolvePendingApproval(e);return!i||i.kind!=="permission"?(this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,"unknown_or_expired_approval_id","That permission approval request is no longer pending."),{handled:!0,kind:t}):(this.sendApprovalDecision(i.requestId,"accept"),this.pendingApprovals.delete(i.approvalId),this.callbacks.sendLocalActionResult(e.action_id,"ok",{approval_id:i.approvalId,decision:"approve"}),this.resumeAfterApproval(e,s,i),{handled:!0,kind:t})}case"permission_reject":{const i=this.resolvePendingApproval(e);return!i||i.kind!=="permission"?(this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,"unknown_or_expired_approval_id","That permission approval request is no longer pending."),{handled:!0,kind:t}):(this.sendApprovalDecision(i.requestId,"deny"),this.pendingApprovals.delete(i.approvalId),this.callbacks.sendLocalActionResult(e.action_id,"ok",{approval_id:i.approvalId,decision:"deny"}),this.resumeAfterApproval(e,s,i),{handled:!0,kind:t})}case"turn_interrupt":{try{await this.cancel(this.threadId??""),this.callbacks.sendLocalActionResult(e.action_id,"ok")}catch(i){this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,void 0,i instanceof Error?i.message:String(i))}return{handled:!0,kind:"turn_interrupt"}}case"thread_rollback":{const i=Number(s.numTurns??s.num_turns??1);if(!this.threadId)return this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,"no_thread","No active thread"),{handled:!0,kind:"thread_rollback"};this.currentTurnId&&(await this.cancel(this.threadId).catch(()=>{}),this.activeEventId&&this.callbacks.sendEventResult(this.activeEventId,"canceled","rolled back"),this.clearActive());try{await this.threadRollback(i),this.callbacks.sendLocalActionResult(e.action_id,"ok",{numTurns:i})}catch(n){this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,void 0,n instanceof Error?n.message:String(n))}return{handled:!0,kind:"thread_rollback"}}case"get_rate_limits":{const i=this.rateLimitSnapshot,n=this.buildContextWindowSnapshot(),o=Date.now(),d=this.currentThreadTokenUsage;return i?r.info("codex-adapter",`[rate-limits] responding: primary=${i.primary.usedPercent.toFixed(1)}% window=${i.primary.windowMinutes}min resetsAt=${i.primary.resetsAt} secondary=${i.secondary.usedPercent.toFixed(1)}% window=${i.secondary.windowMinutes}min resetsAt=${i.secondary.resetsAt}`):r.info("codex-adapter","[rate-limits] responding: no rateLimitSnapshot available"),r.debug("codex-adapter",`[cp-diagnose] get_rate_limits: threadId=${this.threadId??"-"} contextWindow=${JSON.stringify(n)} tokenUsage=${JSON.stringify(d)}`),this.callbacks.sendLocalActionResult(e.action_id,"ok",{adapterType:"codex",available:i!==null,cached:!1,sampledAt:o,rateLimits:i,contextWindow:n,tokenUsage:d}),{handled:!0,kind:"get_rate_limits"}}default:return{handled:!1,kind:""}}}async buildToolbarContextResult(e,t=!0){t&&await this.refreshCodexModelOptions();const s=T(this.collaborationMode)??"default",i=this.currentModelId(),n=this.getModelOption(i),o=this.getModelOptions(),d=j,h=this.reasoningEffort??n?.defaultReasoningEffort??null,f=n?.supportedReasoningEfforts??[],m=this.effectiveServiceTier()??n?.defaultServiceTier??null,u=n?.serviceTiers??[],p=this.isExternalProvider(),g=p?[]:f,w=p?null:h,l=p?[]:u,I=p?null:m,v={outcome:e,session_context:{modelId:i,modeId:s,reasoningEffort:w,serviceTier:I,approvalPolicy:this.approvalPolicy,sandboxMode:this.sandboxMode??null},model_id:i,mode_id:s,currentModelId:i,currentModeId:s,available_models:o,available_modes:d,available_efforts:g,availableModels:o,availableModes:d,reasoning_effort:w,available_service_tiers:l,service_tier:I,sandbox_mode:this.sandboxMode??null,models:o.map(_=>({modelId:_.id,name:_.displayName})),modes:d.map(_=>({id:_.id,name:_.displayName}))};return this.rateLimitSnapshot&&(v.rate_limit_primary_percent=this.rateLimitSnapshot.primary.usedPercent,v.rate_limit_secondary_percent=this.rateLimitSnapshot.secondary.usedPercent,v.rate_limit_primary_window_min=this.rateLimitSnapshot.primary.windowMinutes,v.rate_limit_secondary_window_min=this.rateLimitSnapshot.secondary.windowMinutes),v}currentModelId(){const e=String(this.model??"").trim();if(e)return e;const t=this.getModelOptions();return t.find(s=>s.isDefault)?.id??t[0]?.id??""}getModelOption(e){const t=e.trim().toLowerCase();return this.getModelOptions().find(s=>s.id.trim().toLowerCase()===t)}getModelOptions(){const e=this.config.options??{},t=e.available_models??e.availableModels??e.models,s=_e(t),i=this.codexModelOptions,n=[],o=String(this.model??"").trim(),d=o&&!i.some(u=>u.id.trim().toLowerCase()===o.toLowerCase()),h=i.length>0?i:d?[]:ge;if(o){const u=[...i,...s].find(p=>p.id.trim().toLowerCase()===o.toLowerCase());n.push({id:o,displayName:u?.displayName??y(o),defaultReasoningEffort:u?.defaultReasoningEffort??null,supportedReasoningEfforts:u?.supportedReasoningEfforts??[],defaultServiceTier:u?.defaultServiceTier??null,serviceTiers:u?.serviceTiers??[],isDefault:u?.isDefault??!1})}n.push(...s),n.push(...h);const f=[],m=new Set;for(const u of n){const p=u.id.trim(),g=p.toLowerCase();!p||m.has(g)||(m.add(g),f.push({id:p,displayName:u.displayName.trim()||p,defaultReasoningEffort:u.defaultReasoningEffort,supportedReasoningEfforts:[...u.supportedReasoningEfforts],defaultServiceTier:u.defaultServiceTier,serviceTiers:[...u.serviceTiers],isDefault:u.isDefault}))}return f}async refreshCodexModelOptions(){if(!this.initialized||!this.alive||!this.process?.stdin||Date.now()-this.codexModelOptionsCachedAt<M.MODEL_CACHE_TTL_MS)return;const e=C(this.codexHome);if(e.baseUrl&&q(e.baseUrl)){try{const t=await Te(e.baseUrl,e.apiKey);if(t.length>0){this.codexModelOptions=H(t),this.codexModelOptionsCachedAt=Date.now(),r.info("codex-adapter",`Loaded ${t.length} models from external provider ${e.baseUrl}`);return}}catch(t){r.warn("codex-adapter",`External provider /v1/models failed (${t instanceof Error?t.message:String(t)}), falling back`)}this.codexModelOptions=[],this.codexModelOptionsCachedAt=Date.now();return}try{const t=[];let s;do{const i=await this.sendRequest("model/list",{cursor:s??null,includeHidden:!1,limit:100},15e3),n=Se(i);t.push(...n.models),s=n.nextCursor}while(s);t.length>0&&(this.codexModelOptions=H(t),this.codexModelOptionsCachedAt=Date.now())}catch(t){r.warn("codex-adapter",`Failed to load Codex model list: ${t instanceof Error?t.message:String(t)}`)}}currentTurnId=null;compacting=!1;pendingAutoCompact=!1;compactionDeferred=[];steerDeferred=[];compactingTimer=null;compactionDoneResolver=null;activeEventId=null;activeSessionId=null;visibleAgentMessageIds=new Set;hiddenAgentMessageIds=new Set;agentMessagePhases=new Map;codexSequence=0;turnError=null;deliverInboundEvent(e){if(this.compacting){r.info("codex-adapter",`Event ${e.event_id} deferred: compaction in progress`),this.compactionDeferred.push(e);return}const t=e.content;if(this.currentTurnId&&t.trim()){r.info("codex-adapter",`Event ${e.event_id}: steering active turn ${this.currentTurnId}`),this.steerTurn(t).catch(s=>{const i=s instanceof Error?s.message:String(s);if(r.info("codex-adapter",`Steer failed, falling through: ${i}`),!this.currentTurnId){this.startNewTurn(e,t);return}if(ke(i)){r.info("codex-adapter","Unsteerable turn detected, clearing active state and starting new turn"),this.clearActive(),this.startNewTurn(e,t);return}r.info("codex-adapter",`Steer failed but turn still active; deferring event ${e.event_id} for replay after turn ends`),this.steerDeferred.push(e)});return}if(this.currentTurnId){r.info("codex-adapter",`Event ${e.event_id} rejected: busy and empty content`),this.callbacks.sendEventResult(e.event_id,"failed","agent busy");return}this.startNewTurn(e,t)}startNewTurn(e,t){this.activeEventId=e.event_id,this.activeSessionId=e.session_id,this.visibleAgentMessageIds.clear(),this.hiddenAgentMessageIds.clear(),this.agentMessagePhases.clear(),this.codexSequence=0,this.startComposing();const s=this.threadId??"",i=new E(s),n={adapterSessionId:s,text:t,contextMessages:e.context_messages_json?JSON.parse(e.context_messages_json).map(o=>({senderId:o.sender_id??"unknown",content:o.content})):void 0};this.runTurn(n,i,e.event_id,e.session_id).catch(o=>{r.error("codex-adapter",`Turn failed: ${o}`),this.callbacks.sendEventResult(e.event_id,"failed",o instanceof Error?o.message:String(o)),this.clearActive()}),this.resetIdleTimer(e.event_id)}deliverStopEvent(e,t){this.activeEventId===e&&(this.cancel(this.threadId??"").catch(()=>{}),this.callbacks.sendEventResult(e,"canceled","stopped by user"),this.clearActive())}async spawnCodex(){const e={...process.env,...this.config.env},t=typeof e.PATH=="string"?e.PATH:void 0;t||r.warn("codex-adapter",`spawnCodex: env.PATH is missing! process.env.PATH=${process.env.PATH?"set":"missing"}, config.env=${JSON.stringify(this.config.env)}`);const s=F(this.config.command||"codex",t),i=xe(this.config.args);this.sandboxMode&&i.push("-c",`sandbox_mode="${this.sandboxMode}"`),r.info("codex-adapter",`Spawning: ${s} ${i.join(" ")}`);try{if(!(await $(this.cwd)).isDirectory())throw new Error(`Bound path is not a directory: ${this.cwd}`)}catch(o){throw String(o?.code??"")==="ENOENT"?new Error(`Bound directory does not exist: ${this.cwd}. Please rebind with /grix open <valid-directory>.`):o}const n=N({codexHome:this.codexHome});if(n.locked&&!n.stale)throw new Error(`Codex session appears locked by another process: ${n.path}`);n.stale&&r.warn("codex-adapter",`Ignoring stale Codex lock: ${n.path}`);try{this.process=K(s,i,{env:e,cwd:this.cwd}).process}catch(o){throw r.error("codex-adapter",`Codex spawn threw: ${o}`),this.process=null,this.alive=!1,this.bridgeStatus="closed",o}this.process.on("error",o=>{r.error("codex-adapter",`Codex process spawn error: ${o}`),this.process=null,this.alive=!1,this.bridgeStatus="closed",this.clearIdleTimer(),this.rejectAllPending("spawn failed: "+(o instanceof Error?o.message:String(o))),this.stopComposing(),this.activeEventId&&(this.callbacks.sendEventResult(this.activeEventId,"failed","Codex process spawn failed"),this.activeEventId=null),this.emit("exit",1)}),this.process.on("exit",o=>{r.info("codex-adapter",`Codex process exited (code=${o})`),this.alive=!1,this.bridgeStatus="closed",this.clearIdleTimer(),this.rejectAllPending("process exited"),this.stopComposing(),this.emit("exit",o)}),this.process.stderr?.on("data",o=>{const d=o.toString().trim();d&&r.info("codex-adapter",`[codex stderr] ${d}`)}),this.bindStdout(),this.alive=!0}bindStdout(){if(!this.process?.stdout)return;this.process.stdout.setEncoding("utf8"),B({input:this.process.stdout}).on("line",t=>{if(!t.trim())return;let s;try{s=JSON.parse(t)}catch{r.error("codex-adapter",`Invalid JSON from Codex: ${t.slice(0,200)}`);return}if(this.bridgeStatus==="starting"&&(this.bridgeStatus="ready"),s.id!=null){const i=String(s.id),n=this.pendingRequests.get(i);if(n){clearTimeout(n.timer),this.pendingRequests.delete(i),n.resolve(s);return}}this.handleNotification(s)})}handleNotification(e){const t=e.method,s=e.params??{};switch(this.captureContextWindowFromPayload(t,s),this.activeEventId&&this.resetIdleTimer(this.activeEventId),this.activeEventId&&t&&t.startsWith("item/")&&!t.endsWith("/requestApproval")&&this.startComposing(),t){case"item/started":{const i=s.item,n=i?.id,o=i?.type,d=i?.phase;this.isLongRunningToolItemType(o)&&(this.inFlightToolOps+=1),o==="agentMessage"&&n&&(typeof d=="string"&&d.trim()?this.agentMessagePhases.set(n,d.trim().toLowerCase()):this.agentMessagePhases.delete(n),te(d)?(this.visibleAgentMessageIds.add(n),this.hiddenAgentMessageIds.delete(n)):(this.hiddenAgentMessageIds.add(n),this.visibleAgentMessageIds.delete(n)));break}case"item/agentMessage/delta":{const i=s.delta,n=s.itemId;i&&this.activeEventId&&(!n||!this.hiddenAgentMessageIds.has(n))&&!this.shouldSuppressAgentMessageDeltaDuringToolRun(n)&&this.emitCodexEvent("item/agentMessage/delta",e);break}case"item/completed":{const i=s.item,n=i?.id,o=i?.type;this.isLongRunningToolItemType(o)&&(this.inFlightToolOps=Math.max(0,this.inFlightToolOps-1)),this.activeEventId&&o!=="agentMessage"&&this.emitCodexEvent("item/completed",e),o==="agentMessage"&&n&&(this.visibleAgentMessageIds.delete(n),this.hiddenAgentMessageIds.delete(n),this.agentMessagePhases.delete(n));break}case"turn/started":{const n=s.turn?.id;n&&(this.currentTurnId=n,this.turnError=null,r.info("codex-adapter",`Turn started: ${n}`),this.startComposing());break}case"item/fileChange/requestApproval":{this.handleApprovalRequest(e,"file");break}case"item/commandExecution/requestApproval":{this.handleApprovalRequest(e,"exec");break}case"item/permissions/requestApproval":{this.activeEventId&&this.emitCodexEvent("item/permissions/requestApproval",e),this.handleApprovalRequest(e,"permission");break}case"turn/completed":{this.handleTurnCompleted(s);break}case"error":{const i=s.message??JSON.stringify(s);r.error("codex-adapter",`Codex error: ${i}`),i&&(this.turnError=i),this.activeEventId&&this.callbacks.sendRunError(this.activeEventId,this.activeSessionId??"",i);break}case"account/rateLimits/updated":{this.rateLimitSnapshot=this.parseRateLimitSnapshot(s),this.rateLimitSnapshot&&(r.info("codex-adapter",`Rate limits updated: primary=${this.rateLimitSnapshot.primary.usedPercent.toFixed(1)}% secondary=${this.rateLimitSnapshot.secondary.usedPercent.toFixed(1)}%`),this.callbacks.onRateLimitsUpdated?.(this.rateLimitSnapshot));break}case"thread/tokenUsage/updated":{const i=s.threadId,n=s.tokenUsage,o=n?.last;(!i||!n||!o)&&r.info("codex-adapter",`[cp-diagnose] token_usage payload_incomplete: method=thread/tokenUsage/updated hasThreadId=${String(!!i)} hasTokenUsage=${String(!!n)} hasLast=${String(!!o)} params=${JSON.stringify(s)}`),i&&n&&o&&(this.currentThreadTokenUsage={inputTokens:o.inputTokens??0,outputTokens:o.outputTokens??0,cacheReadInputTokens:o.cachedInputTokens??0,cacheCreationInputTokens:0},r.info("codex-adapter",`[cp-diagnose] token_usage raw thread=${i}: ${JSON.stringify(n)}`),r.info("codex-adapter",`[cp-diagnose] token_usage parsed thread=${i}: ${JSON.stringify(this.currentThreadTokenUsage)}`),this.callbacks.onTokenUsageUpdated?.(this.currentThreadTokenUsage),this.publishContextWindowSnapshot());break}default:break}}captureContextWindowFromPayload(e,t){const s=["model_context_window","modelContextWindow","context_window_size","contextWindowSize"],i=this.findNumericField(t,s);if(i==null||i<=0){const n=this.probeContextWindowFields(t,s);n.foundAnyCandidateField&&r.info("codex-adapter",`[cp-diagnose] context_window missing_or_invalid: method=${e??"-"} matchedFields=${n.matchedFields.join(",")||"-"} raw=${n.rawValues.join(",")||"-"} extracted=${String(i)}`);return}this.currentModelContextWindow!==i&&(this.currentModelContextWindow=i,r.info("codex-adapter",`[cp-diagnose] context_window updated: method=${e??"-"} size=${i}`),this.publishContextWindowSnapshot())}probeContextWindowFields(e,t){const s=[],i=[],n=o=>{if(!o||typeof o!="object")return;if(Array.isArray(o)){for(const h of o)n(h);return}const d=o;for(const h of t)Object.prototype.hasOwnProperty.call(d,h)&&(s.push(h),i.push(String(d[h])));for(const h of Object.values(d))n(h)};return n(e),{foundAnyCandidateField:s.length>0,matchedFields:s,rawValues:i}}findNumericField(e,t){if(!e||typeof e!="object")return null;const s=e;for(const i of t){const n=s[i];if(typeof n=="number"&&Number.isFinite(n))return n;if(typeof n=="string"){const o=Number(n);if(Number.isFinite(o))return o}}for(const i of Object.values(s)){if(Array.isArray(i)){for(const o of i){const d=this.findNumericField(o,t);if(d!=null)return d}continue}const n=this.findNumericField(i,t);if(n!=null)return n}return null}buildContextWindowSnapshot(){const e=this.currentModelContextWindow;if(!e||e<=0)return r.debug("codex-adapter",`[cp-diagnose] context_window snapshot skipped: invalid size=${String(e)}`),null;const t=this.currentThreadTokenUsage,s=t?t.inputTokens+t.outputTokens:null,i=s==null?null:Math.max(0,e-s),n=s==null?null:Math.min(100,s/e*100),o=s==null?null:Math.max(0,100-(n??0));return r.info("codex-adapter",`[cp-diagnose] context_window computed: threadId=${this.threadId??"-"} size=${e} usedTokens=${String(s)} usedPercentage=${String(n)}`),{sizeTokens:e,usedTokens:s,remainingTokens:i,usedPercentage:n,remainingPercentage:o,source:"codex:model_context_window"}}publishContextWindowSnapshot(){const e=this.buildContextWindowSnapshot();this.callbacks.onContextWindowUpdated?.(e),this.maybeScheduleAutoCompact(e)}maybeScheduleAutoCompact(e){const t=e?.usedPercentage;typeof t!="number"||t<D||this.pendingAutoCompact||this.compacting||(this.pendingAutoCompact=!0,r.info("codex-adapter",`[auto-compact] context_window usedPercentage=${t.toFixed(1)}% >= ${D}%, scheduling compact`),this.tryRunPendingAutoCompact())}tryRunPendingAutoCompact(){this.pendingAutoCompact&&(this.stopped||!this.alive||this.currentTurnId||this.compacting||(this.pendingAutoCompact=!1,this.execCommand("compact","",this.activeSessionId??"").then(e=>{r.info("codex-adapter",`[auto-compact] compact done status=${e.status} msg=${e.message??""}`),e.status==="failed"&&/agent busy/i.test(e.message??"")&&(this.pendingAutoCompact=!0)}).catch(e=>{r.warn("codex-adapter",`[auto-compact] compact error: ${e instanceof Error?e.message:e}`)})))}parseRateLimitSnapshot(e){try{const s=e.rateLimitsByLimitId?.codex??e.rateLimits??e,i=s.primary,n=s.secondary,o=s.credits,d=l=>{if(typeof l=="number"&&Number.isFinite(l))return l;if(typeof l=="string"){const I=Number(l);if(Number.isFinite(I))return I}return 0},h=l=>typeof l=="boolean"?l:typeof l=="string"?l.toLowerCase()==="true":!1,f=l=>{if(l==null)return null;if(typeof l=="string"){const I=l.trim();if(!I)return null;const v=Number(I);return Number.isFinite(v)&&v>1e9?new Date(v*1e3).toISOString():I}if(typeof l=="number"&&Number.isFinite(l)&&l>0){const I=l>1e12?l/1e3:l;return new Date(I*1e3).toISOString()}return null},m=l=>({usedPercent:d(l.used_percent??l.usedPercent),windowMinutes:d(l.window_minutes??l.windowDurationMins??l.windowMinutes),resetsAt:f(l.resets_at??l.resetsAt)}),u=()=>({usedPercent:0,windowMinutes:0,resetsAt:null}),p=()=>{const l=s.windows;if(!Array.isArray(l))return null;const I=l.map(b=>b&&typeof b=="object"?m(b):u()).filter(b=>b.windowMinutes>0);if(I.length===0)return null;const v=[...I].sort((b,W)=>b.windowMinutes-W.windowMinutes),_=v.find(b=>Math.abs(b.windowMinutes-300)<=30)??v[0],z=v.find(b=>Math.abs(b.windowMinutes-10080)<=120)??v[v.length-1];return{primary:_,secondary:z}},w=(i||n?{primary:i?m(i):u(),secondary:n?m(n):u()}:null)??p();return w?{primary:w.primary,secondary:w.secondary,credits:o?{hasCredits:h(o.has_credits??o.hasCredits),unlimited:h(o.unlimited),balance:o.balance==null?null:d(o.balance)}:{hasCredits:!1,unlimited:!1,balance:null}}:null}catch{return null}}handleApprovalRequest(e,t){const s=e.id;if(s==null)return;const i=e.method,n=`${s}`.trim(),o=`codex_${U(this.activeSessionId??"")}_${U(n)}`;if(this.approvalPolicy==="never"){this.sendApprovalDecision(s,"accept");return}this.activeEventId&&this.emitCodexEvent(i,e),this.pendingApprovals.set(o,{approvalId:o,approvalCommandId:n,sourceEventId:this.activeEventId??"",requestId:s,kind:t}),r.info("codex-adapter",`Pending approval stored: ${o} (kind=${t})`),this.clearIdleTimer(),this.stopComposing()}resolvePendingApproval(e){const t=e.params??{},s=x(t.approval_id,t.approvalId);if(s)return this.pendingApprovals.get(s)??null;const i=x(t.approval_command_id,t.approvalCommandId);if(i){for(const[,n]of this.pendingApprovals)if(n.approvalCommandId===i)return n}return null}sendApprovalDecision(e,t){if(e==null||!this.process?.stdin)return;const s={jsonrpc:"2.0",id:e,result:{decision:t}};this.process.stdin.write(`${JSON.stringify(s)}
4
- `)}armCompactionFallback(){this.compacting&&(this.compactingTimer&&clearTimeout(this.compactingTimer),this.compactingTimer=setTimeout(()=>{r.warn("codex-adapter","Compaction fallback fired: turn/completed not received within timeout, treating as stuck and releasing deferred events"),this.currentTurnId=null,this.finishCompaction("fallback-timeout")},me),this.compactingTimer.unref?.())}finishCompaction(e){if(!this.compacting)return;this.compacting=!1,this.compactingTimer&&(clearTimeout(this.compactingTimer),this.compactingTimer=null);const t=this.compactionDeferred.splice(0);r.info("codex-adapter",`Compaction finished (${e}); replaying ${t.length} deferred events`);for(const i of t)this.deliverInboundEvent(i);const s=this.compactionDoneResolver;this.compactionDoneResolver=null,s?.()}handleTurnCompleted(e){if(this.currentTurnId=null,this.stopComposing(),this.activeEventId){const t=this.activeEventId,s={event_id:t,session_id:this.activeSessionId??"",thread_id:this.threadId??void 0,codex_event_type:"codex",codex_method:"turn/completed",codex_sequence:++this.codexSequence,codex_payload:e,codex_at:new Date().toISOString()},i=this.turnError;this.turnError=null;const n=!!i;this.callbacks.sendCodexEventReliable?(this.clearActive({runPendingAutoCompact:!1}),this.callbacks.sendCodexEventReliable(s).then(()=>{this.callbacks.sendEventResult(t,n?"failed":"responded",n?i??void 0:void 0),this.tryRunPendingAutoCompact()}).catch(o=>{r.error("codex-adapter",`sendCodexEventReliable failed event=${t}: ${o}`),this.callbacks.sendCodexEvent(s),this.callbacks.sendEventResult(t,n?"failed":"responded",n?i??void 0:void 0),this.tryRunPendingAutoCompact()})):(this.callbacks.sendCodexEvent(s),this.callbacks.sendEventResult(t,n?"failed":"responded",n?i??void 0:void 0),this.clearActive())}this.compacting&&this.finishCompaction("turn-completed")}emitCodexEvent(e,t){this.activeEventId&&this.callbacks.sendCodexEvent({event_id:this.activeEventId,session_id:this.activeSessionId??"",thread_id:this.threadId??void 0,codex_event_type:"codex",codex_method:e,codex_sequence:++this.codexSequence,codex_payload:t,codex_at:new Date().toISOString()})}startComposing(){}stopComposing(){}async sendRequest(e,t,s=3e4){return new Promise((i,n)=>{if(!this.process?.stdin){n(new Error("Codex process not available"));return}const o=++this.requestId,d={jsonrpc:"2.0",id:o,method:e,params:t??{}},h=setTimeout(()=>{this.pendingRequests.delete(String(o)),n(new Error(`Request timeout: ${e}`))},s);this.pendingRequests.set(String(o),{resolve:m=>{m.error?n(new Error(`JSON-RPC error: ${m.error.message}`)):i(m.result)},reject:n,timer:h});const f=JSON.stringify(d);this.process.stdin.write(`${f}
1
+ import{execFileSync as L}from"node:child_process";import{createInterface as J}from"node:readline";import{EventEmitter as N}from"node:events";import{stat as O}from"node:fs/promises";import{join as C,resolve as X}from"node:path";import{fileURLToPath as G}from"node:url";import{homedir as V}from"node:os";import{syncDefaultSkillsToDir as Y}from"../../default-skills/index.js";import{resolveCommandPath as K,spawnCommand as Z,killProcessGroup as D,hasChildProcesses as Q}from"../../core/runtime/spawn.js";import{InternalApiServer as ee}from"../../core/mcp/internal-api-server.js";import{log as r}from"../../core/log/index.js";import{resolveClientVersion as te}from"../../core/util/client-version.js";import{isUserVisibleAgentMessagePhase as ie}from"../../core/util/codex-output-policy.js";import{SessionBindingStore as se}from"../../core/persistence/session-binding-store.js";import{ensureCodexProjectTrusted as ne,isCodexCommand as oe,readCodexProviderSettings as k}from"./codex-trust.js";import{checkCodexSessionActivity as re}from"./session-activity.js";import{findRolloutFile as ae}from"./rollout-locator.js";import{checkCodexLock as j}from"./lock-monitor.js";import{resolveCliPath as de,getCliVersion as ce}from"../../core/util/cli-probe.js";import{scanSkills as le}from"../claude/skill-scanner.js";const he=60*1e3,ue=600*1e3,pe=5*1e3,F=2,fe=20,me=8e3,q=80,ge=120*1e3,ve=[{id:"gpt-5.3-codex",displayName:"GPT-5.3 Codex",defaultReasoningEffort:null,supportedReasoningEfforts:[],defaultServiceTier:null,serviceTiers:[],isDefault:!0}],H=[{id:"default",displayName:"Default"},{id:"plan",displayName:"Plan"}];function U(a,e){const t=String(a??"").trim()||"codex",s=K(t,e);if(s!==t)return s;const i=process.platform==="win32"?[]:["/opt/homebrew/bin/codex","/usr/local/bin/codex","/usr/bin/codex"],n=process.platform!=="win32"?[]:[C(process.env.LOCALAPPDATA??"","npm","codex.cmd"),C(process.env.APPDATA??"","npm","codex.cmd"),C(process.env.LOCALAPPDATA??"","Programs","codex","codex.exe")],o=[...i,...n];for(const d of o)try{return require("node:fs").accessSync(d,require("node:fs").constants.X_OK),d}catch{continue}return r.warn("codex-adapter",`resolveCodexCommandPath: failed to resolve "${t}". envPath=${e?"provided":"missing (using process.env.PATH)"}. fallbacks tried: [${o.join(", ")}]`),t}class M extends N{type="codex";config;callbacks;process=null;alive=!1;stopped=!1;internalApi=null;bridgeStatus="starting";pendingRequests=new Map;requestId=0;threadId=null;initialized=!1;cwd;approvalPolicy;sandboxMode;model;collaborationMode;reasoningEffort;serviceTier;idleTimer=null;lastProgressAt=0;lastIdleCheckAt=0;idleNoProgressCount=0;inFlightToolOps=0;pendingApprovals=new Map;needsHistoryInjection=!1;lastInjectedSessionId=null;identityInjectedThreadId=null;bindingStore=null;aibotSessionId="";threadResumePending=!1;autoTrustProject="auto";codexHome;codexModelOptions=[];codexModelOptionsCachedAt=0;static MODEL_CACHE_TTL_MS=3600*1e3;rateLimitSnapshot=null;currentThreadTokenUsage=null;currentModelContextWindow=null;constructor(e,t){super(),this.config=e,this.callbacks=t;const s=e.options??{};if(this.approvalPolicy=s.approvalPolicy??"never",this.sandboxMode=typeof s.sandboxMode=="string"&&s.sandboxMode.trim()&&s.sandboxMode.trim()!=="default"?s.sandboxMode.trim():void 0,this.aibotSessionId=String(s.aibotSessionId??"").trim(),this.bindingStore=s.bindingStore instanceof se?s.bindingStore:null,this.model=s.model,this.collaborationMode=s.collaborationMode,this.reasoningEffort=s.reasoningEffort,this.serviceTier=s.serviceTier,this.bindingStore&&this.aibotSessionId&&(this.model=this.model??this.bindingStore.getCodexModelId(this.aibotSessionId),this.collaborationMode=this.collaborationMode??this.bindingStore.getCodexModeId(this.aibotSessionId),this.reasoningEffort=this.reasoningEffort??this.bindingStore.getCodexReasoningEffort(this.aibotSessionId),this.serviceTier=this.serviceTier??this.bindingStore.getCodexServiceTier(this.aibotSessionId),this.sandboxMode=this.sandboxMode??this.bindingStore.getCodexSandboxMode(this.aibotSessionId)),this.autoTrustProject=typeof s.autoTrustProject=="boolean"?s.autoTrustProject:"auto",this.codexHome=typeof s.codexHome=="string"&&s.codexHome.trim()?s.codexHome.trim():void 0,this.cwd=this.resolveCwd(),this.bindingStore&&this.aibotSessionId){const i=this.bindingStore.getCodexThreadId(this.aibotSessionId);i&&(this.threadId=i,this.threadResumePending=!0)}}resolveCwd(){if(this.bindingStore&&this.aibotSessionId){const e=this.bindingStore.get(this.aibotSessionId);if(e?.cwd)return e.cwd}return process.cwd()}async start(){await this.ensureProjectTrusted(),await this.startInternalApiAndRegisterMcp(),await this.spawnCodex(),await this.initializeHandshake(),await this.notifyBindingReadyWithContext(),r.info("codex-adapter",`Ready (pid=${this.process?.pid})`)}async ensureProjectTrusted(){if(this.shouldAutoTrustProject())try{const e=await ne(this.cwd,{codexHome:this.codexHome});e.changed&&r.info("codex-adapter",`Trusted Codex project ${this.cwd} in ${e.configPath}`)}catch(e){const t=e instanceof Error?e.message:String(e);throw r.error("codex-adapter",`Failed to trust Codex project ${this.cwd}: ${t}`),e}}async startInternalApiAndRegisterMcp(){try{this.internalApi=new ee,this.internalApi.setInvokeHandler(async(d,u,m,g)=>this.callbacks.agentInvoke(d,u,g)),await this.internalApi.start(0),r.info("codex-adapter",`Internal API started at ${this.internalApi.url}`);const e=this.getMcpConfig(),t={...process.env,...this.config.env},s=typeof t.PATH=="string"?t.PATH:void 0,i=U(this.config.command||"codex",s);try{L(i,["mcp","remove",e.name],{env:t,timeout:1e4,stdio:"ignore"})}catch{}L(i,["mcp","add",e.name,"--",e.command,...e.args],{env:t,timeout:1e4,stdio:"ignore"}),r.info("codex-adapter",`Registered MCP server: ${e.name}`);const n=C(V(),".codex","skills"),o=Y(n);o.length>0&&r.info("codex-adapter",`Synced connector skills to ${n}: [${o.join(", ")}]`)}catch(e){r.warn("codex-adapter",`Failed to register MCP tools (non-fatal): ${e instanceof Error?e.message:String(e)}`)}}shouldAutoTrustProject(){return this.autoTrustProject===!1?!1:this.autoTrustProject===!0?!0:oe(this.config.command)}notifyBindingReady(){!this.aibotSessionId||!this.cwd||this.callbacks.sendUpdateBindingCard(this.aibotSessionId,"ready",this.cwd)}async notifyBindingReadyWithContext(){if(!(!this.aibotSessionId||!this.cwd))try{this.callbacks.sendUpdateBindingCard(this.aibotSessionId,"ready",this.cwd,await this.buildToolbarContextResult("binding_ready"))}catch(e){r.warn("codex-adapter",`Failed to attach toolbar context to binding update: ${e instanceof Error?e.message:String(e)}`),this.notifyBindingReady()}}externalProviderCheckedAt=0;externalProviderCached=!1;static EXTERNAL_PROVIDER_CACHE_TTL_MS=60*1e3;isExternalProvider(){const e=Date.now();if(e-this.externalProviderCheckedAt<M.EXTERNAL_PROVIDER_CACHE_TTL_MS)return this.externalProviderCached;const t=k(this.codexHome);return this.externalProviderCached=!!(t.baseUrl&&z(t.baseUrl)),this.externalProviderCheckedAt=e,this.externalProviderCached}effectiveServiceTier(){if(!this.serviceTier||this.isExternalProvider())return;const e=this.getModelOption(this.currentModelId());if(!(e&&!e.serviceTiers.some(t=>t.id===this.serviceTier)))return this.serviceTier}getEffortMeta(){if(this.isExternalProvider())return{};const e=this.currentModelId(),t=this.getModelOption(e),s={},i=t?.supportedReasoningEfforts??[];i.length>0&&(s.available_efforts=i,s.reasoning_effort=this.reasoningEffort??t?.defaultReasoningEffort??null);const n=t?.serviceTiers??[];return n.length>0&&(s.available_service_tiers=n,s.service_tier=this.effectiveServiceTier()??t?.defaultServiceTier??null),s}async stop(){this.stopped=!0,this.alive=!1,this.stopComposing(),this.clearIdleTimer(),this.rejectAllPending("adapter stopped"),this.pendingAutoCompact=!1,this.compacting=!1,this.compactingTimer&&(clearTimeout(this.compactingTimer),this.compactingTimer=null);for(const t of this.compactionDeferred.splice(0))this.callbacks.sendEventResult(t.event_id,"failed","adapter stopped");for(const t of this.steerDeferred.splice(0))this.callbacks.sendEventResult(t.event_id,"failed","adapter stopped");this.internalApi&&(await this.internalApi.stop(),this.internalApi=null);const e=this.process;if(this.process=null,e?.pid&&typeof e.once=="function")D(e,"SIGTERM"),await Promise.race([new Promise(s=>{e.once("exit",()=>s(!0))}),new Promise(s=>{setTimeout(()=>s(!1),5e3)})])||D(e,"SIGKILL");else if(e)try{e.kill("SIGTERM")}catch{}}isAlive(){return this.alive}async createSession(e){if(this.initialized||await this.initializeHandshake(),this.threadId||await this.startNewThread(),!this.threadId)throw new Error("Failed to create session: thread ID is missing");return await this.notifyBindingReadyWithContext(),this.threadId}async resumeSession(e,t){await this.ensureThreadResumed()}async destroySession(e){this.threadId=null,this.threadResumePending=!1,this.persistThreadId(void 0)}sendPrompt(e){const t=new R(e.adapterSessionId);return this.runTurn(e,t).catch(s=>{t.emitError(s instanceof Error?s:new Error(String(s)))}),t}async cancel(e){if(this.threadId)try{await this.sendRequest("turn/interrupt",{threadId:this.threadId,turnId:this.currentTurnId??""},5e3)}catch{}}setPermissionHandler(e){this.permissionHandler=e}permissionHandler=null;async ping(e){if(!this.alive||!this.process)return!1;if(!this.process.pid){const t=!!(this.process.stdin&&!this.process.stdin.destroyed),s=!!(this.process.stdout&&!this.process.stdout.destroyed);return t&&s}if(!this.process.stdin||this.process.stdin.destroyed)return!!(this.process.stdout&&!this.process.stdout.destroyed);try{const t=++this.requestId,s={jsonrpc:"2.0",id:t,method:"ping",params:{}};return await new Promise(i=>{const n=setTimeout(()=>{this.pendingRequests.delete(String(t)),i(!1)},e);this.pendingRequests.set(String(t),{resolve:()=>i(!0),reject:()=>i(!1),timer:n}),this.process.stdin.write(`${JSON.stringify(s)}
2
+ `)})}catch{return!1}}getStatus(){return{alive:this.alive,busy:this.currentTurnId!==null||this.compacting,sessions:this.threadId?1:0}}getActiveEventIds(){return this.activeEventId?[this.activeEventId]:[]}clearActiveEventForShutdown(){this.clearIdleTimer(),this.activeEventId=null}setModel(e){this.model=e,this.persistCodexContext(),r.info("codex-adapter",`Model set to: ${e}`)}setMode(e){const t=T(e);if(!t){r.info("codex-adapter",`Ignoring unsupported mode: ${e}`);return}this.collaborationMode=t,this.persistCodexContext(),r.info("codex-adapter",`Mode set to: ${t}`)}setReasoningEffort(e){this.reasoningEffort=e,this.persistCodexContext(),r.info("codex-adapter",`Reasoning effort set to: ${e}`)}setServiceTier(e){const t=e.trim();this.serviceTier=t.toLowerCase()==="default"?void 0:t,this.persistCodexContext(),r.info("codex-adapter",`Service tier set to: ${this.serviceTier??"default"}`)}setSandboxMode(e){this.sandboxMode=e,this.persistCodexContext(),r.info("codex-adapter",`Sandbox mode set to: ${e} (applied on next restart)`)}async threadCompact(){if(!this.threadId)throw new Error("No active thread");return this.sendRequest("thread/compact/start",{threadId:this.threadId},12e4)}async threadRollback(e){if(!this.threadId)throw new Error("No active thread");return this.sendRequest("thread/rollback",{threadId:this.threadId,numTurns:e},1e4)}getThreadId(){return this.threadId}getMcpConfig(){if(!this.internalApi)return null;const e=X(G(import.meta.url),"../../../mcp/acp-mcp-server.js");return{name:"grix-connector-tools",command:process.execPath,args:[e,"--api-url",this.internalApi.url]}}async hasBackgroundWork(){const e=this.process?.pid;return e?Q(e,[e]):!1}onAgentProfileChanged(){this.identityInjectedThreadId!==null&&(r.info("codex-adapter",`agent profile changed; clearing identity-injected thread record (was ${this.identityInjectedThreadId})`),this.identityInjectedThreadId=null)}async probe(e){const t=this.config.command||"codex",s=await de(t),i=s!==null;let n=null,o;if(i){const I=await ce(t);n=I.version,I.error&&(o=I.error)}else o={code:"cli_not_found",message:`command not found: ${t}`};const u=k().baseUrl??null,m=this.currentModelId()||null,g=this.getStatus(),p=e?.conversation?{attempted:!0,ok:!1,latency_ms:null,error:{code:"unsupported",message:"codex does not support minimal prompt probe"}}:{attempted:!1,ok:!1,latency_ms:null},h=await this.probeSessionRecord(),v=j({codexHome:this.codexHome});return{cli:{command:t,installed:i,path:s,version:n,...o?{error:o}:{}},conversation:p,config:{model:m,base_url:u,source:{model:m?"runtime":"unknown",base_url:u?"file":"unknown"}},process:{started:!!this.process,alive:g.alive,busy:g.busy},session:h,lock:{present:v.present,locked:v.locked,stale:v.stale,path:v.path}}}async probeSessionRecord(){const e=this.threadId?ae(this.threadId,this.codexHome):null;if(!e)return{recordPath:null,lastActivityMs:null,freshMs:null};try{const t=await O(e);return{recordPath:e,lastActivityMs:t.mtimeMs,freshMs:Date.now()-t.mtimeMs}}catch{return{recordPath:e,lastActivityMs:null,freshMs:null}}}getSupportedCommands(){return[{name:"compact",description:"Compact thread to reduce context size"},{name:"model",description:"List or set model",args:"[model_id]"},{name:"mode",description:"List or set collaboration mode",args:"[default|plan]"},{name:"rollback",description:"Roll back thread turns",args:"[num_turns]"},{name:"interrupt",description:"Interrupt current turn"},{name:"rate_limits",description:"Show current rate limits"},{name:"status",description:"Show thread and session status"},{name:"skills",description:"List available skills"}]}async execCommand(e,t,s){try{switch(e){case"compact":{if(!this.threadId)return{status:"failed",message:"No active thread"};if(this.compacting)return{status:"failed",message:"Compaction already in progress"};if(this.currentTurnId&&(await this.cancel(this.threadId).catch(()=>{}),this.activeEventId&&this.callbacks.sendEventResult(this.activeEventId,"canceled","compacting"),this.clearActive()),await this.ensureThreadResumed(),this.currentTurnId)return{status:"failed",message:"agent busy"};if(this.needsHistoryInjection)return{status:"failed",message:"The session has expired and there is no history to compact. Please send a new message and try again."};this.compacting=!0;const i=new Promise(n=>{this.compactionDoneResolver=n});try{return await this.threadCompact(),this.armCompactionFallback(),await i,{status:"ok",message:"Thread compacted"}}catch(n){this.finishCompaction("compact-error");const o=n instanceof Error?n.message:String(n);if(/thread.*(not found|not exist|unknown|invalid)/i.test(o)||/not found.*thread/i.test(o))return this.threadId=null,this.persistThreadId(void 0),{status:"failed",message:"The session has expired and there is no history to compact. Please send a new message and try again."};throw n}}case"model":{const i=t.trim();return i?this.getModelOptions().some(o=>o.id===i)?(this.setModel(i),{status:"ok",message:`Model set to ${i}`}):{status:"failed",message:`Unknown model: ${i}`}:{status:"ok",message:`Current: ${this.currentModelId()}`,data:{models:this.getModelOptions()}}}case"mode":{const i=t.trim();if(i){const n=T(i);return n?(this.setMode(n),{status:"ok",message:`Mode set to ${n}`}):{status:"failed",message:`Unknown mode: ${i}. Supported: default, plan`}}return{status:"ok",message:`Current: ${this.collaborationMode??"default"}`,data:{modes:H}}}case"rollback":{if(!this.threadId)return{status:"failed",message:"No active thread"};const i=Math.max(1,parseInt(t.trim(),10)||1);return this.currentTurnId&&(await this.cancel(this.threadId).catch(()=>{}),this.activeEventId&&this.callbacks.sendEventResult(this.activeEventId,"canceled","rolled back"),this.clearActive()),await this.threadRollback(i),{status:"ok",message:`Rolled back ${i} turn(s)`}}case"interrupt":return this.threadId?(await this.cancel(this.threadId),this.activeEventId&&this.callbacks.sendEventResult(this.activeEventId,"canceled","interrupted"),this.clearActive(),{status:"ok",message:"Turn interrupted"}):{status:"failed",message:"No active thread"};case"rate_limits":return{status:"ok",message:this.rateLimitSnapshot?`Primary: ${this.rateLimitSnapshot.primary.usedPercent.toFixed(1)}%, Secondary: ${this.rateLimitSnapshot.secondary.usedPercent.toFixed(1)}%`:"Rate limits not available",data:{rateLimits:this.rateLimitSnapshot}};case"status":return{status:"ok",message:`Thread: ${this.threadId??"none"}, Model: ${this.currentModelId()}, Mode: ${this.collaborationMode??"default"}`,data:{threadId:this.threadId,model:this.currentModelId(),mode:this.collaborationMode??"default",cwd:this.cwd,alive:this.alive}};case"skills":{const i=le({mode:"codex",projectDir:this.cwd}),n=i.map(o=>`- ${o.name}${o.trigger?` (${o.trigger})`:""}: ${o.description}`);return{status:"ok",message:n.length>0?n.join(`
3
+ `):"No skills found",data:i}}default:return{status:"unsupported",message:`Unknown command: ${e}`}}}catch(i){return{status:"failed",message:i instanceof Error?i.message:String(i)}}}async handleLocalAction(e){const t=e.action_type??"",s=e.params??{};switch(r.info("codex-adapter",`handleLocalAction action_type=${t} action_id=${e.action_id} session_id=${s.session_id??""}`),t){case"set_model":{const i=x(l(s.model_id),l(s.modelId),l(s.value));return i&&this.setModel(i),this.callbacks.sendLocalActionResult(e.action_id,"ok",await this.buildToolbarContextResult("model_set")),{handled:!0,kind:"set_model"}}case"set_mode":{const i=s.mode_id;return i&&this.setMode(i),this.callbacks.sendLocalActionResult(e.action_id,"ok",await this.buildToolbarContextResult("mode_set")),{handled:!0,kind:"set_mode"}}case"set_reasoning_effort":{const i=x(l(s.reasoning_effort),l(s.reasoning_eff),l(s.effort));return i&&this.setReasoningEffort(i),this.callbacks.sendLocalActionResult(e.action_id,"ok",await this.buildToolbarContextResult("effort_set",!1)),{handled:!0,kind:"set_reasoning_effort"}}case"set_service_tier":{const i=x(l(s.service_tier),l(s.serviceTier),l(s.value));return i&&this.setServiceTier(i),this.callbacks.sendLocalActionResult(e.action_id,"ok",await this.buildToolbarContextResult("service_tier_set",!1)),{handled:!0,kind:"set_service_tier"}}case"set_sandbox_mode":{const i=x(l(s.sandbox_mode),l(s.sandboxMode),l(s.value));return i&&this.setSandboxMode(i),this.callbacks.sendLocalActionResult(e.action_id,"ok",await this.buildToolbarContextResult("sandbox_mode_set",!1)),{handled:!0,kind:"set_sandbox_mode"}}case"thread_compact":{r.info("codex-adapter",`thread_compact start action_id=${e.action_id} threadId=${this.threadId}`),this.currentTurnId&&(await this.cancel(this.threadId).catch(()=>{}),this.activeEventId&&this.callbacks.sendEventResult(this.activeEventId,"canceled","compacting"),this.clearActive());try{if(await this.ensureThreadResumed(),this.needsHistoryInjection)return r.info("codex-adapter",`thread_compact aborted: thread expired, new thread created action_id=${e.action_id}`),this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,"thread_expired","The session has expired and there is no history to compact. Please send a new message and try again."),{handled:!0,kind:"thread_compact"};this.compacting=!0;const i=new Promise(n=>{this.compactionDoneResolver=n});await this.threadCompact(),this.armCompactionFallback(),await i,r.info("codex-adapter",`thread_compact done action_id=${e.action_id}`),this.callbacks.sendLocalActionResult(e.action_id,"ok")}catch(i){this.finishCompaction("compact-error");const n=i instanceof Error?i.message:String(i);/thread.*(not found|not exist|unknown|invalid)/i.test(n)||/not found.*thread/i.test(n)?(r.warn("codex-adapter",`thread_compact thread expired, resetting threadId action_id=${e.action_id} err=${n}`),this.threadId=null,this.persistThreadId(void 0),this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,"thread_expired","The session has expired and there is no history to compact. Please send a new message and try again.")):(r.info("codex-adapter",`thread_compact failed action_id=${e.action_id} err=${n}`),this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,void 0,n))}return{handled:!0,kind:"thread_compact"}}case"get_context":return this.callbacks.sendLocalActionResult(e.action_id,"ok",{threadId:this.threadId,model:this.model,mode:this.collaborationMode,approvalPolicy:this.approvalPolicy,cwd:this.cwd,...await this.buildToolbarContextResult("context")}),{handled:!0,kind:"get_context"};case"exec_approve":case"file_approve":{const i=this.resolvePendingApproval(e);if(!i)return this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,"unknown_or_expired_approval_id","That approval request is no longer pending."),{handled:!0,kind:t};const n=s.decision||"allow-once",o=xe[n]??"accept";return this.sendApprovalDecision(i.requestId,o),this.pendingApprovals.delete(i.approvalId),this.callbacks.sendLocalActionResult(e.action_id,"ok",{approval_id:i.approvalId,approval_command_id:i.approvalCommandId,decision:n}),this.resumeAfterApproval(e,s,i),{handled:!0,kind:t}}case"exec_reject":case"file_reject":{const i=this.resolvePendingApproval(e);return i?(this.sendApprovalDecision(i.requestId,"deny"),this.pendingApprovals.delete(i.approvalId),this.callbacks.sendLocalActionResult(e.action_id,"ok",{approval_id:i.approvalId,approval_command_id:i.approvalCommandId,decision:"deny"}),this.resumeAfterApproval(e,s,i),{handled:!0,kind:t}):(this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,"unknown_or_expired_approval_id","That approval request is no longer pending."),{handled:!0,kind:t})}case"permission_approve":{const i=this.resolvePendingApproval(e);return!i||i.kind!=="permission"?(this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,"unknown_or_expired_approval_id","That permission approval request is no longer pending."),{handled:!0,kind:t}):(this.sendApprovalDecision(i.requestId,"accept"),this.pendingApprovals.delete(i.approvalId),this.callbacks.sendLocalActionResult(e.action_id,"ok",{approval_id:i.approvalId,decision:"approve"}),this.resumeAfterApproval(e,s,i),{handled:!0,kind:t})}case"permission_reject":{const i=this.resolvePendingApproval(e);return!i||i.kind!=="permission"?(this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,"unknown_or_expired_approval_id","That permission approval request is no longer pending."),{handled:!0,kind:t}):(this.sendApprovalDecision(i.requestId,"deny"),this.pendingApprovals.delete(i.approvalId),this.callbacks.sendLocalActionResult(e.action_id,"ok",{approval_id:i.approvalId,decision:"deny"}),this.resumeAfterApproval(e,s,i),{handled:!0,kind:t})}case"turn_interrupt":{try{await this.cancel(this.threadId??""),this.callbacks.sendLocalActionResult(e.action_id,"ok")}catch(i){this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,void 0,i instanceof Error?i.message:String(i))}return{handled:!0,kind:"turn_interrupt"}}case"thread_rollback":{const i=Number(s.numTurns??s.num_turns??1);if(!this.threadId)return this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,"no_thread","No active thread"),{handled:!0,kind:"thread_rollback"};this.currentTurnId&&(await this.cancel(this.threadId).catch(()=>{}),this.activeEventId&&this.callbacks.sendEventResult(this.activeEventId,"canceled","rolled back"),this.clearActive());try{await this.threadRollback(i),this.callbacks.sendLocalActionResult(e.action_id,"ok",{numTurns:i})}catch(n){this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,void 0,n instanceof Error?n.message:String(n))}return{handled:!0,kind:"thread_rollback"}}case"get_rate_limits":{const i=this.rateLimitSnapshot,n=this.buildContextWindowSnapshot(),o=Date.now(),d=this.currentThreadTokenUsage;return i?r.info("codex-adapter",`[rate-limits] responding: primary=${i.primary.usedPercent.toFixed(1)}% window=${i.primary.windowMinutes}min resetsAt=${i.primary.resetsAt} secondary=${i.secondary.usedPercent.toFixed(1)}% window=${i.secondary.windowMinutes}min resetsAt=${i.secondary.resetsAt}`):r.info("codex-adapter","[rate-limits] responding: no rateLimitSnapshot available"),r.debug("codex-adapter",`[cp-diagnose] get_rate_limits: threadId=${this.threadId??"-"} contextWindow=${JSON.stringify(n)} tokenUsage=${JSON.stringify(d)}`),this.callbacks.sendLocalActionResult(e.action_id,"ok",{adapterType:"codex",available:i!==null,cached:!1,sampledAt:o,rateLimits:i,contextWindow:n,tokenUsage:d}),{handled:!0,kind:"get_rate_limits"}}default:return{handled:!1,kind:""}}}async buildToolbarContextResult(e,t=!0){t&&await this.refreshCodexModelOptions();const s=T(this.collaborationMode)??"default",i=this.currentModelId(),n=this.getModelOption(i),o=this.getModelOptions(),d=H,u=this.reasoningEffort??n?.defaultReasoningEffort??null,m=n?.supportedReasoningEfforts??[],g=this.effectiveServiceTier()??n?.defaultServiceTier??null,p=n?.serviceTiers??[],h=this.isExternalProvider(),v=h?[]:m,I=h?null:u,_=h?[]:p,w=h?null:g,c={outcome:e,session_context:{modelId:i,modeId:s,reasoningEffort:I,serviceTier:w,approvalPolicy:this.approvalPolicy,sandboxMode:this.sandboxMode??null},model_id:i,mode_id:s,currentModelId:i,currentModeId:s,available_models:o,available_modes:d,available_efforts:v,availableModels:o,availableModes:d,reasoning_effort:I,available_service_tiers:_,service_tier:w,sandbox_mode:this.sandboxMode??null,models:o.map(f=>({modelId:f.id,name:f.displayName})),modes:d.map(f=>({id:f.id,name:f.displayName}))};return this.rateLimitSnapshot&&(c.rate_limit_primary_percent=this.rateLimitSnapshot.primary.usedPercent,c.rate_limit_secondary_percent=this.rateLimitSnapshot.secondary.usedPercent,c.rate_limit_primary_window_min=this.rateLimitSnapshot.primary.windowMinutes,c.rate_limit_secondary_window_min=this.rateLimitSnapshot.secondary.windowMinutes),c}currentModelId(){const e=String(this.model??"").trim();if(e)return e;const t=this.getModelOptions();return t.find(s=>s.isDefault)?.id??t[0]?.id??""}getModelOption(e){const t=e.trim().toLowerCase();return this.getModelOptions().find(s=>s.id.trim().toLowerCase()===t)}getModelOptions(){const e=this.config.options??{},t=e.available_models??e.availableModels??e.models,s=Te(t),i=this.codexModelOptions,n=[],o=String(this.model??"").trim(),d=o&&!i.some(p=>p.id.trim().toLowerCase()===o.toLowerCase()),u=i.length>0?i:d?[]:ve;if(o){const p=[...i,...s].find(h=>h.id.trim().toLowerCase()===o.toLowerCase());n.push({id:o,displayName:p?.displayName??y(o),defaultReasoningEffort:p?.defaultReasoningEffort??null,supportedReasoningEfforts:p?.supportedReasoningEfforts??[],defaultServiceTier:p?.defaultServiceTier??null,serviceTiers:p?.serviceTiers??[],isDefault:p?.isDefault??!1})}n.push(...s),n.push(...u);const m=[],g=new Set;for(const p of n){const h=p.id.trim(),v=h.toLowerCase();!h||g.has(v)||(g.add(v),m.push({id:h,displayName:p.displayName.trim()||h,defaultReasoningEffort:p.defaultReasoningEffort,supportedReasoningEfforts:[...p.supportedReasoningEfforts],defaultServiceTier:p.defaultServiceTier,serviceTiers:[...p.serviceTiers],isDefault:p.isDefault}))}return m}async refreshCodexModelOptions(){if(!this.initialized||!this.alive||!this.process?.stdin||Date.now()-this.codexModelOptionsCachedAt<M.MODEL_CACHE_TTL_MS)return;const e=k(this.codexHome);if(e.baseUrl&&z(e.baseUrl)){try{const t=await ye(e.baseUrl,e.apiKey);if(t.length>0){this.codexModelOptions=W(t),this.codexModelOptionsCachedAt=Date.now(),r.info("codex-adapter",`Loaded ${t.length} models from external provider ${e.baseUrl}`);return}}catch(t){r.warn("codex-adapter",`External provider /v1/models failed (${t instanceof Error?t.message:String(t)}), falling back`)}this.codexModelOptions=[],this.codexModelOptionsCachedAt=Date.now();return}try{const t=[];let s;do{const i=await this.sendRequest("model/list",{cursor:s??null,includeHidden:!1,limit:100},15e3),n=Ce(i);t.push(...n.models),s=n.nextCursor}while(s);t.length>0&&(this.codexModelOptions=W(t),this.codexModelOptionsCachedAt=Date.now())}catch(t){r.warn("codex-adapter",`Failed to load Codex model list: ${t instanceof Error?t.message:String(t)}`)}}currentTurnId=null;compacting=!1;pendingAutoCompact=!1;compactionDeferred=[];steerDeferred=[];compactingTimer=null;compactionDoneResolver=null;activeEventId=null;activeSessionId=null;visibleAgentMessageIds=new Set;hiddenAgentMessageIds=new Set;agentMessagePhases=new Map;codexSequence=0;turnError=null;deliverInboundEvent(e){if(this.compacting){r.info("codex-adapter",`Event ${e.event_id} deferred: compaction in progress`),this.compactionDeferred.push(e);return}const t=e.content;if(this.currentTurnId&&t.trim()){r.info("codex-adapter",`Event ${e.event_id}: steering active turn ${this.currentTurnId}`),this.steerTurn(t).catch(s=>{const i=s instanceof Error?s.message:String(s);if(r.info("codex-adapter",`Steer failed, falling through: ${i}`),!this.currentTurnId){this.startNewTurn(e,t);return}if(Ae(i)){r.info("codex-adapter","Unsteerable turn detected, clearing active state and starting new turn"),this.clearActive(),this.startNewTurn(e,t);return}r.info("codex-adapter",`Steer failed but turn still active; deferring event ${e.event_id} for replay after turn ends`),this.steerDeferred.push(e)});return}if(this.currentTurnId){r.info("codex-adapter",`Event ${e.event_id} rejected: busy and empty content`),this.callbacks.sendEventResult(e.event_id,"failed","agent busy");return}this.startNewTurn(e,t)}startNewTurn(e,t){this.activeEventId=e.event_id,this.activeSessionId=e.session_id,this.visibleAgentMessageIds.clear(),this.hiddenAgentMessageIds.clear(),this.agentMessagePhases.clear(),this.codexSequence=0,this.startComposing();const s=this.threadId??"",i=new R(s),n={adapterSessionId:s,text:t,contextMessages:e.context_messages_json?JSON.parse(e.context_messages_json).map(o=>({senderId:o.sender_id??"unknown",content:o.content})):void 0};this.runTurn(n,i,e.event_id,e.session_id).catch(o=>{r.error("codex-adapter",`Turn failed: ${o}`),this.callbacks.sendEventResult(e.event_id,"failed",o instanceof Error?o.message:String(o)),this.clearActive()}),this.resetIdleTimer(e.event_id)}deliverStopEvent(e,t){this.activeEventId===e&&(this.cancel(this.threadId??"").catch(()=>{}),this.callbacks.sendEventResult(e,"canceled","stopped by user"),this.clearActive())}async spawnCodex(){const e={...process.env,...this.config.env},t=typeof e.PATH=="string"?e.PATH:void 0;t||r.warn("codex-adapter",`spawnCodex: env.PATH is missing! process.env.PATH=${process.env.PATH?"set":"missing"}, config.env=${JSON.stringify(this.config.env)}`);const s=U(this.config.command||"codex",t),i=Ie(this.config.args);this.sandboxMode&&i.push("-c",`sandbox_mode="${this.sandboxMode}"`),r.info("codex-adapter",`Spawning: ${s} ${i.join(" ")}`);try{if(!(await O(this.cwd)).isDirectory())throw new Error(`Bound path is not a directory: ${this.cwd}`)}catch(o){throw String(o?.code??"")==="ENOENT"?new Error(`Bound directory does not exist: ${this.cwd}. Please rebind with /grix open <valid-directory>.`):o}const n=j({codexHome:this.codexHome});if(n.locked&&!n.stale)throw new Error(`Codex session appears locked by another process: ${n.path}`);n.stale&&r.warn("codex-adapter",`Ignoring stale Codex lock: ${n.path}`);try{this.process=Z(s,i,{env:e,cwd:this.cwd}).process}catch(o){throw r.error("codex-adapter",`Codex spawn threw: ${o}`),this.process=null,this.alive=!1,this.bridgeStatus="closed",o}this.process.on("error",o=>{r.error("codex-adapter",`Codex process spawn error: ${o}`),this.process=null,this.alive=!1,this.bridgeStatus="closed",this.clearIdleTimer(),this.rejectAllPending("spawn failed: "+(o instanceof Error?o.message:String(o))),this.stopComposing(),this.activeEventId&&(this.callbacks.sendEventResult(this.activeEventId,"failed","Codex process spawn failed"),this.activeEventId=null),this.emit("exit",1)}),this.process.on("exit",o=>{r.info("codex-adapter",`Codex process exited (code=${o})`),this.alive=!1,this.bridgeStatus="closed",this.clearIdleTimer(),this.rejectAllPending("process exited"),this.stopComposing(),this.emit("exit",o)}),this.process.stderr?.on("data",o=>{const d=o.toString().trim();d&&r.info("codex-adapter",`[codex stderr] ${d}`)}),this.bindStdout(),this.alive=!0}bindStdout(){if(!this.process?.stdout)return;this.process.stdout.setEncoding("utf8"),J({input:this.process.stdout}).on("line",t=>{if(!t.trim())return;let s;try{s=JSON.parse(t)}catch{r.error("codex-adapter",`Invalid JSON from Codex: ${t.slice(0,200)}`);return}if(this.bridgeStatus==="starting"&&(this.bridgeStatus="ready"),s.id!=null){const i=String(s.id),n=this.pendingRequests.get(i);if(n){clearTimeout(n.timer),this.pendingRequests.delete(i),n.resolve(s);return}}this.handleNotification(s)})}handleNotification(e){const t=e.method,s=e.params??{};switch(this.captureContextWindowFromPayload(t,s),this.activeEventId&&this.resetIdleTimer(this.activeEventId),this.activeEventId&&t&&t.startsWith("item/")&&!t.endsWith("/requestApproval")&&this.startComposing(),t){case"item/started":{const i=s.item,n=i?.id,o=i?.type,d=i?.phase;this.isLongRunningToolItemType(o)&&(this.inFlightToolOps+=1),o==="agentMessage"&&n&&(typeof d=="string"&&d.trim()?this.agentMessagePhases.set(n,d.trim().toLowerCase()):this.agentMessagePhases.delete(n),ie(d)?(this.visibleAgentMessageIds.add(n),this.hiddenAgentMessageIds.delete(n)):(this.hiddenAgentMessageIds.add(n),this.visibleAgentMessageIds.delete(n)));break}case"item/agentMessage/delta":{const i=s.delta,n=s.itemId;i&&this.activeEventId&&(!n||!this.hiddenAgentMessageIds.has(n))&&!this.shouldSuppressAgentMessageDeltaDuringToolRun(n)&&this.emitCodexEvent("item/agentMessage/delta",e);break}case"item/completed":{const i=s.item,n=i?.id,o=i?.type;this.isLongRunningToolItemType(o)&&(this.inFlightToolOps=Math.max(0,this.inFlightToolOps-1)),this.activeEventId&&o!=="agentMessage"&&this.emitCodexEvent("item/completed",e),o==="agentMessage"&&n&&(this.visibleAgentMessageIds.delete(n),this.hiddenAgentMessageIds.delete(n),this.agentMessagePhases.delete(n));break}case"turn/started":{const n=s.turn?.id;n&&(this.currentTurnId=n,this.turnError=null,r.info("codex-adapter",`Turn started: ${n}`),this.startComposing());break}case"item/fileChange/requestApproval":{this.handleApprovalRequest(e,"file");break}case"item/commandExecution/requestApproval":{this.handleApprovalRequest(e,"exec");break}case"item/permissions/requestApproval":{this.activeEventId&&this.emitCodexEvent("item/permissions/requestApproval",e),this.handleApprovalRequest(e,"permission");break}case"turn/completed":{this.handleTurnCompleted(s);break}case"error":{const i=s.message??JSON.stringify(s);r.error("codex-adapter",`Codex error: ${i}`),i&&(this.turnError=i),this.activeEventId&&this.callbacks.sendRunError(this.activeEventId,this.activeSessionId??"",i);break}case"account/rateLimits/updated":{this.rateLimitSnapshot=this.parseRateLimitSnapshot(s),this.rateLimitSnapshot&&(r.info("codex-adapter",`Rate limits updated: primary=${this.rateLimitSnapshot.primary.usedPercent.toFixed(1)}% secondary=${this.rateLimitSnapshot.secondary.usedPercent.toFixed(1)}%`),this.callbacks.onRateLimitsUpdated?.(this.rateLimitSnapshot));break}case"thread/tokenUsage/updated":{const i=s.threadId,n=s.tokenUsage,o=n?.last;(!i||!n||!o)&&r.info("codex-adapter",`[cp-diagnose] token_usage payload_incomplete: method=thread/tokenUsage/updated hasThreadId=${String(!!i)} hasTokenUsage=${String(!!n)} hasLast=${String(!!o)} params=${JSON.stringify(s)}`),i&&n&&o&&(this.currentThreadTokenUsage={inputTokens:o.inputTokens??0,outputTokens:o.outputTokens??0,cacheReadInputTokens:o.cachedInputTokens??0,cacheCreationInputTokens:0},r.info("codex-adapter",`[cp-diagnose] token_usage raw thread=${i}: ${JSON.stringify(n)}`),r.info("codex-adapter",`[cp-diagnose] token_usage parsed thread=${i}: ${JSON.stringify(this.currentThreadTokenUsage)}`),this.callbacks.onTokenUsageUpdated?.(this.currentThreadTokenUsage),this.publishContextWindowSnapshot());break}default:break}}captureContextWindowFromPayload(e,t){const s=["model_context_window","modelContextWindow","context_window_size","contextWindowSize"],i=this.findNumericField(t,s);if(i==null||i<=0){const n=this.probeContextWindowFields(t,s);n.foundAnyCandidateField&&r.info("codex-adapter",`[cp-diagnose] context_window missing_or_invalid: method=${e??"-"} matchedFields=${n.matchedFields.join(",")||"-"} raw=${n.rawValues.join(",")||"-"} extracted=${String(i)}`);return}this.currentModelContextWindow!==i&&(this.currentModelContextWindow=i,r.info("codex-adapter",`[cp-diagnose] context_window updated: method=${e??"-"} size=${i}`),this.publishContextWindowSnapshot())}probeContextWindowFields(e,t){const s=[],i=[],n=o=>{if(!o||typeof o!="object")return;if(Array.isArray(o)){for(const u of o)n(u);return}const d=o;for(const u of t)Object.prototype.hasOwnProperty.call(d,u)&&(s.push(u),i.push(String(d[u])));for(const u of Object.values(d))n(u)};return n(e),{foundAnyCandidateField:s.length>0,matchedFields:s,rawValues:i}}findNumericField(e,t){if(!e||typeof e!="object")return null;const s=e;for(const i of t){const n=s[i];if(typeof n=="number"&&Number.isFinite(n))return n;if(typeof n=="string"){const o=Number(n);if(Number.isFinite(o))return o}}for(const i of Object.values(s)){if(Array.isArray(i)){for(const o of i){const d=this.findNumericField(o,t);if(d!=null)return d}continue}const n=this.findNumericField(i,t);if(n!=null)return n}return null}buildContextWindowSnapshot(){const e=this.currentModelContextWindow;if(!e||e<=0)return r.debug("codex-adapter",`[cp-diagnose] context_window snapshot skipped: invalid size=${String(e)}`),null;const t=this.currentThreadTokenUsage,s=t?t.inputTokens+t.outputTokens:null,i=s==null?null:Math.max(0,e-s),n=s==null?null:Math.min(100,s/e*100),o=s==null?null:Math.max(0,100-(n??0));return r.info("codex-adapter",`[cp-diagnose] context_window computed: threadId=${this.threadId??"-"} size=${e} usedTokens=${String(s)} usedPercentage=${String(n)}`),{sizeTokens:e,usedTokens:s,remainingTokens:i,usedPercentage:n,remainingPercentage:o,source:"codex:model_context_window"}}publishContextWindowSnapshot(){const e=this.buildContextWindowSnapshot();this.callbacks.onContextWindowUpdated?.(e),this.maybeScheduleAutoCompact(e)}maybeScheduleAutoCompact(e){const t=e?.usedPercentage;typeof t!="number"||t<q||this.pendingAutoCompact||this.compacting||(this.pendingAutoCompact=!0,r.info("codex-adapter",`[auto-compact] context_window usedPercentage=${t.toFixed(1)}% >= ${q}%, scheduling compact`),this.tryRunPendingAutoCompact())}tryRunPendingAutoCompact(){this.pendingAutoCompact&&(this.stopped||!this.alive||this.currentTurnId||this.compacting||(this.pendingAutoCompact=!1,this.execCommand("compact","",this.activeSessionId??"").then(e=>{r.info("codex-adapter",`[auto-compact] compact done status=${e.status} msg=${e.message??""}`),e.status==="failed"&&/agent busy/i.test(e.message??"")&&(this.pendingAutoCompact=!0)}).catch(e=>{r.warn("codex-adapter",`[auto-compact] compact error: ${e instanceof Error?e.message:e}`)})))}parseRateLimitSnapshot(e){try{const s=e.rateLimitsByLimitId?.codex??e.rateLimits??e,i=s.primary,n=s.secondary,o=s.credits,d=c=>{if(typeof c=="number"&&Number.isFinite(c))return c;if(typeof c=="string"){const f=Number(c);if(Number.isFinite(f))return f}return 0},u=c=>typeof c=="boolean"?c:typeof c=="string"?c.toLowerCase()==="true":!1,m=c=>{if(c==null)return null;if(typeof c=="string"){const f=c.trim();if(!f)return null;const b=Number(f);return Number.isFinite(b)&&b>1e9?new Date(b*1e3).toISOString():f}if(typeof c=="number"&&Number.isFinite(c)&&c>0){const f=c>1e12?c/1e3:c;return new Date(f*1e3).toISOString()}return null},g=c=>{const f=c.resets_in_seconds??c.resetsInSeconds,b=typeof f=="number"||typeof f=="string"?Number(f):NaN;return!Number.isFinite(b)||b<=0?null:new Date(Date.now()+b*1e3).toISOString()},p=c=>({usedPercent:d(c.used_percent??c.usedPercent),windowMinutes:d(c.window_minutes??c.windowDurationMins??c.windowMinutes),resetsAt:m(c.resets_at??c.resetsAt)??g(c)}),h=()=>({usedPercent:0,windowMinutes:0,resetsAt:null}),v=()=>{const c=[];if(i&&c.push(i),n&&c.push(n),Array.isArray(s.windows))for(const f of s.windows)f&&typeof f=="object"&&c.push(f);return c.map(p).filter(f=>f.windowMinutes>0)},I=(c,f,b)=>{const P=c.filter(S=>b(S.windowMinutes));return P.length===0?null:P.reduce((S,$)=>Math.abs($.windowMinutes-f)<Math.abs(S.windowMinutes-f)?$:S)},_=v(),w=_.length>0?{primary:I(_,300,c=>c<1440)??h(),secondary:I(_,10080,c=>c>=1440)??h()}:i||n?{primary:i?p(i):h(),secondary:n?p(n):h()}:null;return w?{primary:w.primary,secondary:w.secondary,credits:o?{hasCredits:u(o.has_credits??o.hasCredits),unlimited:u(o.unlimited),balance:o.balance==null?null:d(o.balance)}:{hasCredits:!1,unlimited:!1,balance:null}}:null}catch{return null}}handleApprovalRequest(e,t){const s=e.id;if(s==null)return;const i=e.method,n=`${s}`.trim(),o=`codex_${B(this.activeSessionId??"")}_${B(n)}`;if(this.approvalPolicy==="never"){this.sendApprovalDecision(s,"accept");return}this.activeEventId&&this.emitCodexEvent(i,e),this.pendingApprovals.set(o,{approvalId:o,approvalCommandId:n,sourceEventId:this.activeEventId??"",requestId:s,kind:t}),r.info("codex-adapter",`Pending approval stored: ${o} (kind=${t})`),this.clearIdleTimer(),this.stopComposing()}resolvePendingApproval(e){const t=e.params??{},s=x(t.approval_id,t.approvalId);if(s)return this.pendingApprovals.get(s)??null;const i=x(t.approval_command_id,t.approvalCommandId);if(i){for(const[,n]of this.pendingApprovals)if(n.approvalCommandId===i)return n}return null}sendApprovalDecision(e,t){if(e==null||!this.process?.stdin)return;const s={jsonrpc:"2.0",id:e,result:{decision:t}};this.process.stdin.write(`${JSON.stringify(s)}
4
+ `)}armCompactionFallback(){this.compacting&&(this.compactingTimer&&clearTimeout(this.compactingTimer),this.compactingTimer=setTimeout(()=>{r.warn("codex-adapter","Compaction fallback fired: turn/completed not received within timeout, treating as stuck and releasing deferred events"),this.currentTurnId=null,this.finishCompaction("fallback-timeout")},ge),this.compactingTimer.unref?.())}finishCompaction(e){if(!this.compacting)return;this.compacting=!1,this.compactingTimer&&(clearTimeout(this.compactingTimer),this.compactingTimer=null);const t=this.compactionDeferred.splice(0);r.info("codex-adapter",`Compaction finished (${e}); replaying ${t.length} deferred events`);for(const i of t)this.deliverInboundEvent(i);const s=this.compactionDoneResolver;this.compactionDoneResolver=null,s?.()}handleTurnCompleted(e){if(this.currentTurnId=null,this.stopComposing(),this.activeEventId){const t=this.activeEventId,s={event_id:t,session_id:this.activeSessionId??"",thread_id:this.threadId??void 0,codex_event_type:"codex",codex_method:"turn/completed",codex_sequence:++this.codexSequence,codex_payload:e,codex_at:new Date().toISOString()},i=this.turnError;this.turnError=null;const n=!!i;this.callbacks.sendCodexEventReliable?(this.clearActive({runPendingAutoCompact:!1}),this.callbacks.sendCodexEventReliable(s).then(()=>{this.callbacks.sendEventResult(t,n?"failed":"responded",n?i??void 0:void 0),this.tryRunPendingAutoCompact()}).catch(o=>{r.error("codex-adapter",`sendCodexEventReliable failed event=${t}: ${o}`),this.callbacks.sendCodexEvent(s),this.callbacks.sendEventResult(t,n?"failed":"responded",n?i??void 0:void 0),this.tryRunPendingAutoCompact()})):(this.callbacks.sendCodexEvent(s),this.callbacks.sendEventResult(t,n?"failed":"responded",n?i??void 0:void 0),this.clearActive())}this.compacting&&this.finishCompaction("turn-completed")}emitCodexEvent(e,t){this.activeEventId&&this.callbacks.sendCodexEvent({event_id:this.activeEventId,session_id:this.activeSessionId??"",thread_id:this.threadId??void 0,codex_event_type:"codex",codex_method:e,codex_sequence:++this.codexSequence,codex_payload:t,codex_at:new Date().toISOString()})}startComposing(){}stopComposing(){}async sendRequest(e,t,s=3e4){return new Promise((i,n)=>{if(!this.process?.stdin){n(new Error("Codex process not available"));return}const o=++this.requestId,d={jsonrpc:"2.0",id:o,method:e,params:t??{}},u=setTimeout(()=>{this.pendingRequests.delete(String(o)),n(new Error(`Request timeout: ${e}`))},s);this.pendingRequests.set(String(o),{resolve:g=>{g.error?n(new Error(`JSON-RPC error: ${g.error.message}`)):i(g.result)},reject:n,timer:u});const m=JSON.stringify(d);this.process.stdin.write(`${m}
5
5
  `)})}sendNotification(e,t){if(!this.process?.stdin)return;const s={jsonrpc:"2.0",method:e,params:t??{}};this.process.stdin.write(`${JSON.stringify(s)}
6
- `)}async initializeHandshake(){if(this.initialized)return;const e=ee(),t=await this.sendRequest("initialize",{clientInfo:{name:"grix-connector",title:"Grix Connector",version:e},capabilities:{experimentalApi:!0}},15e3);r.info("codex-adapter",`Initialized: ${JSON.stringify(t)}`),this.sendNotification("initialized"),this.initialized=!0,await this.refreshCodexModelOptions(),this.fetchRateLimits()}fetchRateLimits(){Promise.resolve(this.sendRequest("account/rateLimits/read",void 0,1e4)).then(e=>{const t=this.parseRateLimitSnapshot(e);t&&(this.rateLimitSnapshot=t,r.info("codex-adapter",`Initial rate limits: primary=${t.primary.usedPercent.toFixed(1)}% secondary=${t.secondary.usedPercent.toFixed(1)}% credits=${t.credits.balance??"N/A"}`),this.callbacks.onRateLimitsUpdated?.(t))}).catch(e=>{r.warn("codex-adapter",`Failed to fetch rate limits: ${e instanceof Error?e.message:e}`)})}async runTurn(e,t,s,i){this.threadId||await this.createSession({cwd:this.cwd}),await this.ensureThreadResumed();const n=[],o=this.callbacks.getAgentProfile?.(),d=o?.agentName?.trim()??"",h=o?.introduction?.trim()??"";if((d||h)&&this.identityInjectedThreadId!==this.threadId){const p=["[system-identity]"];d&&p.push(`Your name is "${d}".`),h&&p.push(`Your self-introduction: ${h}`),p.push("Treat this as an out-of-band instruction; do not echo or repeat it in your replies."),p.push("[/system-identity]"),n.push({type:"text",text:p.join(`
7
- `),text_elements:[]}),this.identityInjectedThreadId=this.threadId,r.info("codex-adapter",`Injected agent identity for thread=${this.threadId} name="${d}"`)}if(this.needsHistoryInjection&&i){this.needsHistoryInjection=!1;const p=await this.loadHistoryForInjection(i);for(const g of p)n.push(g);p.length>0&&r.info("codex-adapter",`Injected ${p.length} history items`)}if(e.contextMessages&&e.contextMessages.length>0){const p=e.contextMessages.map(g=>`[${g.senderId??"unknown"}]: ${g.content}`).join(`
6
+ `)}async initializeHandshake(){if(this.initialized)return;const e=te(),t=await this.sendRequest("initialize",{clientInfo:{name:"grix-connector",title:"Grix Connector",version:e},capabilities:{experimentalApi:!0}},15e3);r.info("codex-adapter",`Initialized: ${JSON.stringify(t)}`),this.sendNotification("initialized"),this.initialized=!0,await this.refreshCodexModelOptions(),this.fetchRateLimits()}fetchRateLimits(){Promise.resolve(this.sendRequest("account/rateLimits/read",void 0,1e4)).then(e=>{const t=this.parseRateLimitSnapshot(e);t&&(this.rateLimitSnapshot=t,r.info("codex-adapter",`Initial rate limits: primary=${t.primary.usedPercent.toFixed(1)}% secondary=${t.secondary.usedPercent.toFixed(1)}% credits=${t.credits.balance??"N/A"}`),this.callbacks.onRateLimitsUpdated?.(t))}).catch(e=>{r.warn("codex-adapter",`Failed to fetch rate limits: ${e instanceof Error?e.message:e}`)})}async runTurn(e,t,s,i){this.threadId||await this.createSession({cwd:this.cwd}),await this.ensureThreadResumed();const n=[],o=this.callbacks.getAgentProfile?.(),d=o?.agentName?.trim()??"",u=o?.introduction?.trim()??"";if((d||u)&&this.identityInjectedThreadId!==this.threadId){const h=["[system-identity]"];d&&h.push(`Your name is "${d}".`),u&&h.push(`Your self-introduction: ${u}`),h.push("Treat this as an out-of-band instruction; do not echo or repeat it in your replies."),h.push("[/system-identity]"),n.push({type:"text",text:h.join(`
7
+ `),text_elements:[]}),this.identityInjectedThreadId=this.threadId,r.info("codex-adapter",`Injected agent identity for thread=${this.threadId} name="${d}"`)}if(this.needsHistoryInjection&&i){this.needsHistoryInjection=!1;const h=await this.loadHistoryForInjection(i);for(const v of h)n.push(v);h.length>0&&r.info("codex-adapter",`Injected ${h.length} history items`)}if(e.contextMessages&&e.contextMessages.length>0){const h=e.contextMessages.map(v=>`[${v.senderId??"unknown"}]: ${v.content}`).join(`
8
8
  `);n.push({type:"text",text:`Conversation context:
9
- ${p}
9
+ ${h}
10
10
 
11
- Latest user message:`,text_elements:[]})}n.push({type:"text",text:e.text,text_elements:[]});const f={threadId:this.threadId,approvalPolicy:this.approvalPolicy,input:n,model:this.model},m=we(this.collaborationMode,this.model);m&&(f.collaborationMode=m),this.reasoningEffort&&(f.effort=this.reasoningEffort);const u=this.effectiveServiceTier();u&&(f.serviceTier=u),await this.sendRequest("turn/start",f),t.emitDone({status:"completed"})}async ensureThreadResumed(){if(!(!this.threadId||!this.threadResumePending))try{const e=await this.sendRequest("thread/resume",{threadId:this.threadId,approvalPolicy:this.approvalPolicy,model:this.model,excludeTurns:!0,persistExtendedHistory:!1,...this.reasoningEffort?{effort:this.reasoningEffort}:{},...this.effectiveServiceTier()?{serviceTier:this.effectiveServiceTier()}:{}});this.updateModelFromThreadResult(e),this.threadResumePending=!1}catch{this.threadId=null,this.needsHistoryInjection=!0,await this.startNewThread(),this.threadResumePending=!1}}async startNewThread(){try{const e=await this.sendRequest("thread/start",{approvalPolicy:this.approvalPolicy,model:this.model,cwd:this.cwd,experimentalRawEvents:!1,persistExtendedHistory:!1,...this.reasoningEffort?{effort:this.reasoningEffort}:{},...this.effectiveServiceTier()?{serviceTier:this.effectiveServiceTier()}:{}}),t=Ie(e);if(!t)throw new Error("Codex thread id is missing in thread/start result");this.threadId=t,this.updateModelFromThreadResult(e),this.threadResumePending=!1,this.persistThreadId(t)}catch{throw new Error("Failed to start Codex thread")}}updateModelFromThreadResult(e){if(this.model)return;const t=be(e);t&&(this.model=t,this.persistCodexContext())}persistThreadId(e){!this.bindingStore||!this.aibotSessionId||this.bindingStore.setCodexThreadId(this.aibotSessionId,e)}persistCodexContext(){!this.bindingStore||!this.aibotSessionId||this.bindingStore.setCodexContext(this.aibotSessionId,{modelId:this.model,modeId:T(this.collaborationMode)??void 0,reasoningEffort:this.reasoningEffort,serviceTier:this.serviceTier,sandboxMode:this.sandboxMode})}async loadHistoryForInjection(e){const t=this.callbacks.getConversationLog();if(!t)return[];try{const s=await t.readHistory(e,pe),i=[];for(const n of s){const o=(n.content??"").slice(0,fe);n.direction==="inbound"?i.push({type:"text",text:o,text_elements:[]}):n.direction==="outbound"&&i.push({type:"assistant",text:o,text_elements:[]})}return i}catch(s){return r.error("codex-adapter",`Failed to load history: ${s}`),[]}}async steerTurn(e){if(!this.threadId||!this.currentTurnId)throw new Error("No active turn to steer");await this.sendRequest("turn/steer",{threadId:this.threadId,expectedTurnId:this.currentTurnId,input:[{type:"text",text:e,text_elements:[]}]})}resumeAfterApproval(e,t,s){if(!this.threadId)return;const i=e.event_id?.trim()||`local_action_${e.action_id.trim()}`,n=x(t.session_id,t.sessionId)??this.activeSessionId??"";if(!n)return;const o=this.threadId,d=new E(o),h={adapterSessionId:o,text:""};this.activeEventId=i,this.activeSessionId=n,this.visibleAgentMessageIds.clear(),this.hiddenAgentMessageIds.clear(),this.codexSequence=0,this.runTurn(h,d,i,n).catch(f=>{r.error("codex-adapter",`Post-approval turn failed: ${f}`),this.callbacks.sendEventResult(i,"failed",f instanceof Error?f.message:String(f)),this.clearActive()}),this.resetIdleTimer(i)}clearActive(e){const t=this.activeEventId;this.stopComposing(),this.activeEventId=null,this.activeSessionId=null,this.currentTurnId=null,this.visibleAgentMessageIds.clear(),this.hiddenAgentMessageIds.clear(),this.agentMessagePhases.clear(),this.inFlightToolOps=0,this.clearIdleTimer(),this.pendingApprovals.clear(),t&&this.emit("eventDone",t),this.replaySteerDeferred(),e?.runPendingAutoCompact!==!1&&this.tryRunPendingAutoCompact()}replaySteerDeferred(){if(this.steerDeferred.length===0)return;const e=this.steerDeferred.splice(0);r.info("codex-adapter",`Replaying ${e.length} steer-deferred event(s) after turn ended`);for(const t of e)this.deliverInboundEvent(t)}resetIdleTimer(e){const t=Date.now();this.lastProgressAt=t,this.lastIdleCheckAt=t,this.idleNoProgressCount=0,this.armIdleTimer(e)}armIdleTimer(e){this.clearIdleTimer();const t=this.inFlightToolOps>0?he:le;this.idleTimer=setTimeout(()=>{this.onIdleTimeout(e,t)},t)}async onIdleTimeout(e,t){if(this.activeEventId!==e)return;let s=!1;try{s=await this.ping(ue)}catch{s=!1}if(this.activeEventId!==e)return;if(!s){r.error("codex-adapter",`Agent idle ${t/1e3}s and ping failed, declaring stuck: ${e}`),this.declareStuck(e,`agent idle ${t/1e3}s, ping failed`);return}const i=this.lastIdleCheckAt,n=this.lastProgressAt>i,o=n?!1:await this.checkSessionActivitySince(i);if(this.activeEventId===e){if(this.lastIdleCheckAt=Date.now(),n||o){o&&r.info("codex-adapter",`Idle timer: no stream output but rollout still being written, continuing: ${e}`),this.idleNoProgressCount=0,this.armIdleTimer(e);return}this.idleNoProgressCount++,this.idleNoProgressCount>=O?(r.error("codex-adapter",`Agent alive but no progress for ${this.idleNoProgressCount} idle cycles, declaring stuck: ${e}`),this.declareStuck(e,`agent alive but no progress for ${this.idleNoProgressCount} idle cycles`)):(r.warn("codex-adapter",`Idle timer fired, ping ok, no progress (${this.idleNoProgressCount}/${O}): ${e}`),this.armIdleTimer(e))}}declareStuck(e,t){this.callbacks.sendEventResult(e,"failed",t),this.clearActive(),this.emit("stuck")}async checkSessionActivitySince(e){if(!this.threadId)return!1;try{return await oe(this.threadId,e,this.codexHome)}catch{return!1}}isLongRunningToolItemType(e){if(!e)return!1;const t=e.toLowerCase();return t==="commandexecution"||t==="command_execution"||t==="item/commandexecution"||t==="filechange"||t==="file_change"||t==="item/filechange"}shouldSuppressAgentMessageDeltaDuringToolRun(e){return this.inFlightToolOps<=0?!1:(r.info("codex-adapter",`Suppressing agentMessage delta during tool run item_id=${e??"unknown"}`),!0)}clearIdleTimer(){this.idleTimer&&(clearTimeout(this.idleTimer),this.idleTimer=null)}rejectAllPending(e){for(const[,t]of this.pendingRequests)clearTimeout(t.timer),t.reject(new Error(`Request canceled: ${e}`));this.pendingRequests.clear(),this.pendingApprovals.clear()}}const ve={allow:"accept","allow-once":"accept","allow-always":"acceptForSession",deny:"deny"};function xe(a){return a&&a.length>0?[...a]:["app-server"]}function c(a){if(typeof a!="string")return;const e=a.trim();return e||void 0}function Ie(a){if(!a||typeof a!="object")return null;const e=a,t=e.thread&&typeof e.thread=="object"?e.thread:void 0;return x(c(e.threadId),c(e.threadID),c(t?.id))??null}function be(a){return!a||typeof a!="object"?null:c(a.model)??null}function T(a){const e=a?.trim().toLowerCase();return e==="default"||e==="plan"?e:null}function we(a,e){const t=T(a),s=c(e);if(!(!t||!s))return{mode:t,settings:{model:s}}}function _e(a){if(!Array.isArray(a))return[];const e=[];for(const t of a){if(typeof t=="string"){const o=t.trim();o&&e.push(Ce(o,y(o)));continue}if(!t||typeof t!="object")continue;const s=t,i=x(c(s.id),c(s.model_id),c(s.modelId),c(s.value));if(!i)continue;const n=x(c(s.displayName),c(s.display_name),c(s.name),c(s.label))??y(i);e.push({id:i,displayName:n,defaultReasoningEffort:c(s.defaultReasoningEffort)??null,supportedReasoningEfforts:k(s.supportedReasoningEfforts),defaultServiceTier:c(s.defaultServiceTier)??null,serviceTiers:A(s.serviceTiers),isDefault:s.isDefault===!0})}return e}function q(a){const e=a.toLowerCase().replace(/\/+$/,"");return!(e==="https://api.openai.com"||e==="https://api.openai.com/v1"||e.endsWith(".openai.com"))}async function Te(a,e){const s=`${a.replace(/\/+$/,"")}/models`,i={Accept:"application/json"};e&&(i.Authorization=`Bearer ${e}`);const n=new AbortController,o=setTimeout(()=>n.abort(),1e4);try{const d=await fetch(s,{headers:i,signal:n.signal});if(!d.ok)return[];const h=await d.json();return ye(h)}finally{clearTimeout(o)}}function ye(a){if(!a||typeof a!="object")return[];const e=a,t=Array.isArray(e.data)?e.data:[],s=[];for(const i of t){if(!i||typeof i!="object")continue;const n=i,o=x(c(n.id),c(n.model));if(!o)continue;const d=x(c(n.displayName),c(n.display_name),c(n.name))??y(o);s.push({id:o,displayName:d,defaultReasoningEffort:c(n.defaultReasoningEffort)??null,supportedReasoningEfforts:k(n.supportedReasoningEfforts),defaultServiceTier:c(n.defaultServiceTier)??null,serviceTiers:A(n.serviceTiers),isDefault:n.isDefault===!0})}return s}function Se(a){if(!a||typeof a!="object")return{models:[],nextCursor:null};const e=a,t=Array.isArray(e.data)?e.data:[],s=[];for(const i of t){if(!i||typeof i!="object")continue;const n=i;if(n.hidden===!0)continue;const o=x(c(n.id),c(n.model));if(!o)continue;const d=x(c(n.displayName),c(n.display_name),c(n.name))??y(o);s.push({id:o,displayName:d,defaultReasoningEffort:c(n.defaultReasoningEffort)??null,supportedReasoningEfforts:k(n.supportedReasoningEfforts),defaultServiceTier:c(n.defaultServiceTier)??null,serviceTiers:A(n.serviceTiers),isDefault:n.isDefault===!0})}return{models:s,nextCursor:c(e.nextCursor)??null}}function H(a){const e=[],t=new Set;for(const s of a){const i=s.id.trim(),n=i.toLowerCase();!i||t.has(n)||(t.add(n),e.push({id:i,displayName:s.displayName.trim()||i,defaultReasoningEffort:s.defaultReasoningEffort,supportedReasoningEfforts:[...s.supportedReasoningEfforts],defaultServiceTier:s.defaultServiceTier,serviceTiers:[...s.serviceTiers],isDefault:s.isDefault}))}return e}function Ce(a,e){return{id:a,displayName:e,defaultReasoningEffort:null,supportedReasoningEfforts:[],defaultServiceTier:null,serviceTiers:[],isDefault:!1}}function k(a){if(!Array.isArray(a))return[];const e=[];for(const t of a){if(typeof t=="string"){const i=t.trim();i&&e.push(i);continue}if(!t||typeof t!="object")continue;const s=c(t.reasoningEffort);s&&e.push(s)}return e}function A(a){if(!Array.isArray(a))return[];const e=[];for(const t of a){if(typeof t=="string"){const n=t.trim();n&&e.push({id:n,displayName:n,description:null});continue}if(!t||typeof t!="object")continue;const s=t,i=c(s.id);i&&e.push({id:i,displayName:x(c(s.name),c(s.displayName),c(s.display_name))??i,description:c(s.description)??null})}return e}function y(a){return a.split(/[-_\s]+/).filter(Boolean).map(e=>/^(gpt|codex|o\d+)$/i.test(e)?e.toUpperCase():e.charAt(0).toUpperCase()+e.slice(1)).join(" ")}function x(...a){for(const e of a)if(e!=null){if(typeof e=="string"&&!e.trim())continue;return e}}function U(a){return a.trim().replace(/[^a-zA-Z0-9._:-]+/g,"_")}class E extends P{adapterSessionId;constructor(e){super(),this.adapterSessionId=e}emitDone(e){this.emit("done",e)}emitError(e){if(this.listenerCount("error")===0){r.warn("codex-adapter",`Prompt handle error (no listeners): ${e.message}`);return}this.emit("error",e)}async cancel(){}}function ke(a){return["cannot steer a compact turn","turn not found","turn already completed","no active turn"].some(t=>a.includes(t))}export{M as CodexAdapter};
11
+ Latest user message:`,text_elements:[]})}n.push({type:"text",text:e.text,text_elements:[]});const m={threadId:this.threadId,approvalPolicy:this.approvalPolicy,input:n,model:this.model},g=_e(this.collaborationMode,this.model);g&&(m.collaborationMode=g),this.reasoningEffort&&(m.effort=this.reasoningEffort);const p=this.effectiveServiceTier();p&&(m.serviceTier=p),await this.sendRequest("turn/start",m),t.emitDone({status:"completed"})}async ensureThreadResumed(){if(!(!this.threadId||!this.threadResumePending))try{const e=await this.sendRequest("thread/resume",{threadId:this.threadId,approvalPolicy:this.approvalPolicy,model:this.model,excludeTurns:!0,persistExtendedHistory:!1,...this.reasoningEffort?{effort:this.reasoningEffort}:{},...this.effectiveServiceTier()?{serviceTier:this.effectiveServiceTier()}:{}});this.updateModelFromThreadResult(e),this.threadResumePending=!1}catch{this.threadId=null,this.needsHistoryInjection=!0,await this.startNewThread(),this.threadResumePending=!1}}async startNewThread(){try{const e=await this.sendRequest("thread/start",{approvalPolicy:this.approvalPolicy,model:this.model,cwd:this.cwd,experimentalRawEvents:!1,persistExtendedHistory:!1,...this.reasoningEffort?{effort:this.reasoningEffort}:{},...this.effectiveServiceTier()?{serviceTier:this.effectiveServiceTier()}:{}}),t=be(e);if(!t)throw new Error("Codex thread id is missing in thread/start result");this.threadId=t,this.updateModelFromThreadResult(e),this.threadResumePending=!1,this.persistThreadId(t)}catch{throw new Error("Failed to start Codex thread")}}updateModelFromThreadResult(e){if(this.model)return;const t=we(e);t&&(this.model=t,this.persistCodexContext())}persistThreadId(e){!this.bindingStore||!this.aibotSessionId||this.bindingStore.setCodexThreadId(this.aibotSessionId,e)}persistCodexContext(){!this.bindingStore||!this.aibotSessionId||this.bindingStore.setCodexContext(this.aibotSessionId,{modelId:this.model,modeId:T(this.collaborationMode)??void 0,reasoningEffort:this.reasoningEffort,serviceTier:this.serviceTier,sandboxMode:this.sandboxMode})}async loadHistoryForInjection(e){const t=this.callbacks.getConversationLog();if(!t)return[];try{const s=await t.readHistory(e,fe),i=[];for(const n of s){const o=(n.content??"").slice(0,me);n.direction==="inbound"?i.push({type:"text",text:o,text_elements:[]}):n.direction==="outbound"&&i.push({type:"assistant",text:o,text_elements:[]})}return i}catch(s){return r.error("codex-adapter",`Failed to load history: ${s}`),[]}}async steerTurn(e){if(!this.threadId||!this.currentTurnId)throw new Error("No active turn to steer");await this.sendRequest("turn/steer",{threadId:this.threadId,expectedTurnId:this.currentTurnId,input:[{type:"text",text:e,text_elements:[]}]})}resumeAfterApproval(e,t,s){if(!this.threadId)return;const i=e.event_id?.trim()||`local_action_${e.action_id.trim()}`,n=x(t.session_id,t.sessionId)??this.activeSessionId??"";if(!n)return;const o=this.threadId,d=new R(o),u={adapterSessionId:o,text:""};this.activeEventId=i,this.activeSessionId=n,this.visibleAgentMessageIds.clear(),this.hiddenAgentMessageIds.clear(),this.codexSequence=0,this.runTurn(u,d,i,n).catch(m=>{r.error("codex-adapter",`Post-approval turn failed: ${m}`),this.callbacks.sendEventResult(i,"failed",m instanceof Error?m.message:String(m)),this.clearActive()}),this.resetIdleTimer(i)}clearActive(e){const t=this.activeEventId;this.stopComposing(),this.activeEventId=null,this.activeSessionId=null,this.currentTurnId=null,this.visibleAgentMessageIds.clear(),this.hiddenAgentMessageIds.clear(),this.agentMessagePhases.clear(),this.inFlightToolOps=0,this.clearIdleTimer(),this.pendingApprovals.clear(),t&&this.emit("eventDone",t),this.replaySteerDeferred(),e?.runPendingAutoCompact!==!1&&this.tryRunPendingAutoCompact()}replaySteerDeferred(){if(this.steerDeferred.length===0)return;const e=this.steerDeferred.splice(0);r.info("codex-adapter",`Replaying ${e.length} steer-deferred event(s) after turn ended`);for(const t of e)this.deliverInboundEvent(t)}resetIdleTimer(e){const t=Date.now();this.lastProgressAt=t,this.lastIdleCheckAt=t,this.idleNoProgressCount=0,this.armIdleTimer(e)}armIdleTimer(e){this.clearIdleTimer();const t=this.inFlightToolOps>0?ue:he;this.idleTimer=setTimeout(()=>{this.onIdleTimeout(e,t)},t)}async onIdleTimeout(e,t){if(this.activeEventId!==e)return;let s=!1;try{s=await this.ping(pe)}catch{s=!1}if(this.activeEventId!==e)return;if(!s){r.error("codex-adapter",`Agent idle ${t/1e3}s and ping failed, declaring stuck: ${e}`),this.declareStuck(e,`agent idle ${t/1e3}s, ping failed`);return}const i=this.lastIdleCheckAt,n=this.lastProgressAt>i,o=n?!1:await this.checkSessionActivitySince(i);if(this.activeEventId===e){if(this.lastIdleCheckAt=Date.now(),n||o){o&&r.info("codex-adapter",`Idle timer: no stream output but rollout still being written, continuing: ${e}`),this.idleNoProgressCount=0,this.armIdleTimer(e);return}this.idleNoProgressCount++,this.idleNoProgressCount>=F?(r.error("codex-adapter",`Agent alive but no progress for ${this.idleNoProgressCount} idle cycles, declaring stuck: ${e}`),this.declareStuck(e,`agent alive but no progress for ${this.idleNoProgressCount} idle cycles`)):(r.warn("codex-adapter",`Idle timer fired, ping ok, no progress (${this.idleNoProgressCount}/${F}): ${e}`),this.armIdleTimer(e))}}declareStuck(e,t){this.callbacks.sendEventResult(e,"failed",t),this.clearActive(),this.emit("stuck")}async checkSessionActivitySince(e){if(!this.threadId)return!1;try{return await re(this.threadId,e,this.codexHome)}catch{return!1}}isLongRunningToolItemType(e){if(!e)return!1;const t=e.toLowerCase();return t==="commandexecution"||t==="command_execution"||t==="item/commandexecution"||t==="filechange"||t==="file_change"||t==="item/filechange"}shouldSuppressAgentMessageDeltaDuringToolRun(e){return this.inFlightToolOps<=0?!1:(r.info("codex-adapter",`Suppressing agentMessage delta during tool run item_id=${e??"unknown"}`),!0)}clearIdleTimer(){this.idleTimer&&(clearTimeout(this.idleTimer),this.idleTimer=null)}rejectAllPending(e){for(const[,t]of this.pendingRequests)clearTimeout(t.timer),t.reject(new Error(`Request canceled: ${e}`));this.pendingRequests.clear(),this.pendingApprovals.clear()}}const xe={allow:"accept","allow-once":"accept","allow-always":"acceptForSession",deny:"deny"};function Ie(a){return a&&a.length>0?[...a]:["app-server"]}function l(a){if(typeof a!="string")return;const e=a.trim();return e||void 0}function be(a){if(!a||typeof a!="object")return null;const e=a,t=e.thread&&typeof e.thread=="object"?e.thread:void 0;return x(l(e.threadId),l(e.threadID),l(t?.id))??null}function we(a){return!a||typeof a!="object"?null:l(a.model)??null}function T(a){const e=a?.trim().toLowerCase();return e==="default"||e==="plan"?e:null}function _e(a,e){const t=T(a),s=l(e);if(!(!t||!s))return{mode:t,settings:{model:s}}}function Te(a){if(!Array.isArray(a))return[];const e=[];for(const t of a){if(typeof t=="string"){const o=t.trim();o&&e.push(ke(o,y(o)));continue}if(!t||typeof t!="object")continue;const s=t,i=x(l(s.id),l(s.model_id),l(s.modelId),l(s.value));if(!i)continue;const n=x(l(s.displayName),l(s.display_name),l(s.name),l(s.label))??y(i);e.push({id:i,displayName:n,defaultReasoningEffort:l(s.defaultReasoningEffort)??null,supportedReasoningEfforts:A(s.supportedReasoningEfforts),defaultServiceTier:l(s.defaultServiceTier)??null,serviceTiers:E(s.serviceTiers),isDefault:s.isDefault===!0})}return e}function z(a){const e=a.toLowerCase().replace(/\/+$/,"");return!(e==="https://api.openai.com"||e==="https://api.openai.com/v1"||e.endsWith(".openai.com"))}async function ye(a,e){const s=`${a.replace(/\/+$/,"")}/models`,i={Accept:"application/json"};e&&(i.Authorization=`Bearer ${e}`);const n=new AbortController,o=setTimeout(()=>n.abort(),1e4);try{const d=await fetch(s,{headers:i,signal:n.signal});if(!d.ok)return[];const u=await d.json();return Se(u)}finally{clearTimeout(o)}}function Se(a){if(!a||typeof a!="object")return[];const e=a,t=Array.isArray(e.data)?e.data:[],s=[];for(const i of t){if(!i||typeof i!="object")continue;const n=i,o=x(l(n.id),l(n.model));if(!o)continue;const d=x(l(n.displayName),l(n.display_name),l(n.name))??y(o);s.push({id:o,displayName:d,defaultReasoningEffort:l(n.defaultReasoningEffort)??null,supportedReasoningEfforts:A(n.supportedReasoningEfforts),defaultServiceTier:l(n.defaultServiceTier)??null,serviceTiers:E(n.serviceTiers),isDefault:n.isDefault===!0})}return s}function Ce(a){if(!a||typeof a!="object")return{models:[],nextCursor:null};const e=a,t=Array.isArray(e.data)?e.data:[],s=[];for(const i of t){if(!i||typeof i!="object")continue;const n=i;if(n.hidden===!0)continue;const o=x(l(n.id),l(n.model));if(!o)continue;const d=x(l(n.displayName),l(n.display_name),l(n.name))??y(o);s.push({id:o,displayName:d,defaultReasoningEffort:l(n.defaultReasoningEffort)??null,supportedReasoningEfforts:A(n.supportedReasoningEfforts),defaultServiceTier:l(n.defaultServiceTier)??null,serviceTiers:E(n.serviceTiers),isDefault:n.isDefault===!0})}return{models:s,nextCursor:l(e.nextCursor)??null}}function W(a){const e=[],t=new Set;for(const s of a){const i=s.id.trim(),n=i.toLowerCase();!i||t.has(n)||(t.add(n),e.push({id:i,displayName:s.displayName.trim()||i,defaultReasoningEffort:s.defaultReasoningEffort,supportedReasoningEfforts:[...s.supportedReasoningEfforts],defaultServiceTier:s.defaultServiceTier,serviceTiers:[...s.serviceTiers],isDefault:s.isDefault}))}return e}function ke(a,e){return{id:a,displayName:e,defaultReasoningEffort:null,supportedReasoningEfforts:[],defaultServiceTier:null,serviceTiers:[],isDefault:!1}}function A(a){if(!Array.isArray(a))return[];const e=[];for(const t of a){if(typeof t=="string"){const i=t.trim();i&&e.push(i);continue}if(!t||typeof t!="object")continue;const s=l(t.reasoningEffort);s&&e.push(s)}return e}function E(a){if(!Array.isArray(a))return[];const e=[];for(const t of a){if(typeof t=="string"){const n=t.trim();n&&e.push({id:n,displayName:n,description:null});continue}if(!t||typeof t!="object")continue;const s=t,i=l(s.id);i&&e.push({id:i,displayName:x(l(s.name),l(s.displayName),l(s.display_name))??i,description:l(s.description)??null})}return e}function y(a){return a.split(/[-_\s]+/).filter(Boolean).map(e=>/^(gpt|codex|o\d+)$/i.test(e)?e.toUpperCase():e.charAt(0).toUpperCase()+e.slice(1)).join(" ")}function x(...a){for(const e of a)if(e!=null){if(typeof e=="string"&&!e.trim())continue;return e}}function B(a){return a.trim().replace(/[^a-zA-Z0-9._:-]+/g,"_")}class R extends N{adapterSessionId;constructor(e){super(),this.adapterSessionId=e}emitDone(e){this.emit("done",e)}emitError(e){if(this.listenerCount("error")===0){r.warn("codex-adapter",`Prompt handle error (no listeners): ${e.message}`);return}this.emit("error",e)}async cancel(){}}function Ae(a){return["cannot steer a compact turn","turn not found","turn already completed","no active turn"].some(t=>a.includes(t))}export{M as CodexAdapter};
@@ -1,6 +1,6 @@
1
- import T from"node:path";import{realpath as j,stat as H}from"node:fs/promises";import{tmpdir as V}from"node:os";import{randomUUID as M}from"node:crypto";import{ConnectionManager as Y}from"../core/aibot/index.js";import{ClaudeAdapter as y}from"../adapter/claude/index.js";import{CodexAdapter as J}from"../adapter/codex/index.js";import{PiAdapter as X}from"../adapter/pi/index.js";import{AcpAdapter as A}from"../adapter/acp/index.js";import{OpenHumanAdapter as Z}from"../adapter/openhuman/index.js";import{CursorAdapter as I}from"../adapter/cursor/index.js";import{CodeWhaleAdapter as U}from"../adapter/codewhale/index.js";import{OpenCodeAdapter as ee}from"../adapter/opencode/index.js";import{AgyAdapter as O}from"../adapter/agy/index.js";import{getCachedAgyModels as te}from"../adapter/agy/model-list.js";import{getCachedAgyQuotaInfo as ie}from"../adapter/agy/quota.js";import{LOCAL_ACTION_ERROR_CODES as x,LOCAL_ACTION_TYPES as b,SESSION_CONTROL_ERROR_CODES as g,SESSION_CONTROL_VERBS as p,SESSION_MODE_IDS as C}from"../adapter/claude/protocol-contract.js";import{parseClaudeSessionUsage as ne}from"../adapter/claude/usage-parser.js";import{resolveSkillScanMode as se}from"../adapter/claude/skill-scanner.js";import{buildReportedSkills as oe}from"../default-skills/index.js";import{fetchAvailableModels as D,getCachedModels as Q,readSettingsEnv as re}from"../adapter/claude/model-list.js";import{parseAcpSessionUsage as ae}from"../adapter/acp/usage-parser.js";import{applyProxyEnv as de,getProxyManager as ce,relayHostForClientType as le}from"../core/proxy/index.js";import{buildProviderEnv as ue}from"../core/config/provider-env.js";import{parseCodexSessionUsage as he}from"../adapter/codex/usage-parser.js";import{readCodexProviderSettings as ge}from"../adapter/codex/codex-trust.js";import{scanCodexSessions as me}from"../adapter/codex/session-scanner.js";import{scanClaudeSessions as pe}from"../adapter/claude/session-scanner.js";import{scanAcpSessions as fe}from"../adapter/acp/session-scanner.js";import{parsePiSessionUsage as _e}from"../adapter/pi/usage-parser.js";import{SessionScanCache as L,resolveCodexLeafDirs as ve,resolveClaudeLeafDirs as Se,resolveAcpLeafDirs as be}from"./session-scan-cache.js";import{log as u,ConversationLog as we,AgentApiPacketLog as Ce,BridgeEventLog as Ae}from"../core/log/index.js";import{RevokeHandler as ke}from"./revoke-handler.js";import{AdapterPool as Re,PoolFullError as Ee}from"./adapter-pool.js";import{parseSessionControlCommand as $e,handleSessionControlCommand as q,handleSessionControlLocalAction as ye}from"./session-controller.js";import{handleAcpSetModel as W,handleAcpSetMode as N,resolveAcpInitialDefaults as xe}from"./acp-toolbar-persist.js";import{SessionBindingStore as Te}from"../core/persistence/session-binding-store.js";import{handleFileListAction as Le,handleCreateFolderAction as Pe,serveLocalFile as He,realHomeDir as K}from"../core/files/index.js";import{getMachineName as Me}from"../core/util/index.js";import{uploadReplyFileToAgentMedia as Ie}from"../core/protocol/agent-api-media.js";import{ActiveEventStore as De}from"../core/persistence/active-event-store.js";import{DEFAULT_CONNECTOR_RUNTIME_CONFIG as Qe,applyConnectorRuntimeConfigPatch as Be,extractConnectorRuntimeConfigPatch as Fe}from"./runtime-config.js";import{SendController as Ue}from"./send-controller.js";import{queryProviderQuota as z}from"../core/provider-quota/index.js";import{queryKiroQuota as B}from"../core/provider-quota/kiro.js";import{buildToolUseCard as E,buildToolResultCard as $,buildLocalGrixCardLink as Oe}from"./tool-card-utils.js";import{DeferredEventManager as qe}from"./deferred-events.js";import{buildAgentProbeResult as We,PROBE_CACHE_TTL_STATIC_MS as Ne,PROBE_CACHE_TTL_FULL_MS as Ke}from"./probe-helper.js";const ze=600*1e3,Ge=60*1e3,G=30*1e3,je=10*1e3,P=new Set(["claude","acp","agy","cursor","codex"]),Ve=new Set(["claude","codex","cursor","codewhale","opencode","pi","openhuman","agy","acp"]),F=3;class Kt{config;name;aibotHandle;aibotConfig;pool;stopped=!1;revokeHandler=new ke;sessionBindings=new Map;deferredMgr;sendCtrl=new Ue(Qe);bindingStore;globalConfigStore;upgradeTrigger=null;agentDeletedHandler=null;shareSetHandler=null;providerConfigHandler=null;agentProfile={agentName:"",introduction:""};activeEventStore;cachedRateLimits=null;cachedRateLimitsSampledAtMs=null;cachedCodexContextWindow=null;cachedCodexTokenUsage=null;cachedCodexUsageSampledAtMs=null;cachedAcpContextWindow=null;cachedAcpContextWindowSampledAtMs=null;cachedClaudeRateLimitState=null;cachedProviderQuota=null;cachedProviderQuotaSampledAtMs=null;claudeWorkerStatus=new Map;lastReportedSkillsHash="";conversationLog=null;packetLog=null;kiroQuotaTimer=null;eventSessionIndex=new Map;inflightEvents=new Map;restartCount=new Map;selfDrivenSessions=new Set;selfDrivenLabels=new Map;probeCache=new Map;sessionScanCache;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=re();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=ge();!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 z(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(n=>`${n.name}=${n.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}}startKiroQuotaTimer(){this.kiroQuotaTimer||(this.kiroQuotaTimer=setInterval(()=>{if(this.stopped){this.stopKiroQuotaTimer();return}this.refreshAndPushKiroQuota().catch(()=>{})},G),u.info(this.name,`[kiro-quota-timer] started (interval=${G}ms)`))}stopKiroQuotaTimer(){this.kiroQuotaTimer&&(clearInterval(this.kiroQuotaTimer),this.kiroQuotaTimer=null,u.info(this.name,"[kiro-quota-timer] stopped"))}async refreshAndPushKiroQuota(){try{const e=await B();this.cachedProviderQuota=e,this.cachedProviderQuotaSampledAtMs=Date.now(),e.success&&this.pushKiroQuotaToBindings(e)}catch{}}pushKiroQuotaToBindings(e){const o=this.providerQuotaToRateLimits(e);for(const[t,n]of this.sessionBindings.entries()){if(!n)continue;const i={provider_quota:e};o&&(i.rate_limits=o);const s=this.cachedAcpContextWindow;s&&("usedPercentage"in s?i.context_window={usedPercentage:s.usedPercentage,remainingPercentage:100-s.usedPercentage}:i.context_window=s),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:"ready",cwd:n,meta:i})}}getFreshClaudeRateLimitState(){const e=this.cachedClaudeRateLimitState;return e&&this.isRateLimitsCacheFresh(e.sampledAt)?e:null}getFreshCodexGlobalRateLimitCache(){const e=this.cachedRateLimits,o=this.cachedCodexContextWindow,t=this.cachedCodexTokenUsage;return{sampledAt:Math.max(this.cachedRateLimitsSampledAtMs??0,this.cachedCodexUsageSampledAtMs??0)||null,rateLimits:e,contextWindow:o,tokenUsage:t,hasData:!!(e||o||t)}}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",clientType:this.config.aibot.clientType,pool:e}}hasPendingWork(){return this.pool?.hasPendingWork()??!1}relayEnvStale=!1;relayEnvSettledHandler;async recycleAdaptersForRelayChange(){if(!this.pool)return this.relayEnvStale=!1,!0;if(this.hasPendingWork())return this.relayEnvStale=!0,u.info(this.name,"relay changed but agent is busy; adapters keep the old env until the next message (marked stale)"),!1;const e=[...this.pool.getAllSlots()];let o=0,t=!1;for(const n of e){if(this.hasPendingWork())return this.relayEnvStale=!0,u.info(this.name,"new work arrived while recycling; remaining adapters stay stale until the next message"),!1;let i=!1;await this.pool.removeSlot(n.sessionId).catch(s=>{t=!0,i=!0,u.warn(this.name,`failed to recycle adapter slot ${n.sessionId} after relay change: ${s}`)}),i||(o+=1)}return o>0&&u.info(this.name,`recycled ${o} adapter slot(s) so the new Grix relay env takes effect`),t?(this.relayEnvStale=!0,!1):(this.relayEnvStale=!1,this.relayEnvSettledHandler?.(),!0)}setRelayEnvSettledHandler(e){this.relayEnvSettledHandler=e}hasStaleRelayEnv(){return this.relayEnvStale}markRelayEnvStale(){this.relayEnvStale=!0}async probe(e={}){const o=e.conversation?"full":"static",t=e.conversation?Ke:Ne;if(!e.fresh){const a=this.probeCache.get(o);if(a&&Date.now()-a.sampledAt<t)return{...a.result,cached:!0}}const n=this.config.adapterType??"acp",i=this.createAdapter(n,"__probe__"),s=e.conversation&&n==="acp"?()=>this.runAcpConversationProbe(i,e.timeoutMs??1e4):void 0;let r;try{r=await We({adapter:i,agentName:this.name,clientType:this.config.aibot.clientType,adapterType:n,providerBaseUrl:this.config.providerBaseUrl??null,opts:e,launchConversationProbe:s})}finally{i.stop().catch(()=>{})}return this.probeCache.set(o,{result:r,sampledAt:r.probed_at}),r}async runAcpConversationProbe(e,o){const t=Date.now(),n="__probe__",i=`probe-${Date.now()}-${Math.random().toString(36).slice(2,8)}`,s=this.config.agent.cwd||V(),r=()=>Date.now()-t;try{await e.start()}catch(h){return{attempted:!0,ok:!1,latency_ms:r(),error:{code:"conversation_failed",message:`start failed: ${h instanceof Error?h.message:String(h)}`}}}if(!e.isAlive())return{attempted:!0,ok:!1,latency_ms:r(),error:{code:"process_not_started",message:"agent process not alive"}};if(e instanceof A)try{await e.bindSession(n,s)}catch{}let a=null;const d=new Promise(h=>{a=m=>{m===i&&h()},e.on("eventDone",a)});e.deliverInboundEvent({event_id:i,session_id:n,content:"ping",msg_id:i}),e.deliverStopEvent(i,n);let c=null;const l=await Promise.race([d.then(()=>!1),new Promise(h=>{c=setTimeout(()=>h(!0),o)})]);return c&&clearTimeout(c),a&&e.removeListener("eventDone",a),l?{attempted:!0,ok:!1,latency_ms:r(),error:{code:"conversation_timeout",message:`no eventDone within ${o}ms`}}:{attempted:!0,ok:!0,latency_ms:r()}}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 we(e.logDir):null,this.packetLog=e.logDir?new Ce(e.logDir):null,this.bindingStore=new Te(e.bindingsPath),this.bindingStore.load(),this.globalConfigStore=o??null,this.deferredMgr=new qe(this.name),this.activeEventStore=e.activeEventStorePath?new De(e.activeEventStorePath):null,t==="codex"?this.sessionScanCache=new L(me,ve):t==="claude"?this.sessionScanCache=new L(pe,Se):t==="agy"?this.sessionScanCache=new L(()=>[],()=>[]):this.sessionScanCache=new L(fe,be)}async start(){(this.config.adapterType??"acp")==="claude"&&(await D().catch(()=>{}),this.maybeQueryProviderQuota().catch(()=>{})),this.config.aibot.clientType==="kiro"&&(this.maybeQueryProviderQuota().catch(()=>{}),this.startKiroQuotaTimer()),(this.config.adapterType??"acp")==="codex"&&this.maybeQueryProviderQuota().catch(()=>{}),await this.connectAibot(),this.sendCtrl.bind(this.aibotHandle);const e=this.config.adapterType??"acp";this.pool=new Re({maxPoolSize:this.config.poolMaxSize??20,idleTimeoutMs:this.config.poolIdleTimeoutMs??18e5,eventQueue:this.config.eventQueue},o=>{const t=this.createAdapter(e,o);return t instanceof A&&t.on("acpSessionReady",n=>{this.bindingStore.setAcpSessionId(o,n),this.sessionScanCache.invalidate()}),t},(o,t)=>{this.aibotHandle.sendEventAck({event_id:o,session_id:t,received_at:Date.now()})}),this.pool.setEventStateHandler((o,t,n,i)=>{u.info(this.name,`[queue-debug] send event_state session=${t} event=${o} state=${n} queue_pos=${i?.queue_position??""} queue_total=${i?.queue_total??""}`),this.aibotHandle.sendEventState({event_id:o,session_id:t,state:n,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),(n==="canceled"||n==="failed")&&this.aibotHandle.sendEventResult({event_id:o,status:n==="canceled"?"canceled":"failed",msg:i?.reason,updated_at:Date.now()})}),this.pool.setQueueComposingHandler((o,t,n)=>{this.aibotHandle.sendSessionActivitySet({session_id:o,kind:"composing",active:t,...t?{ttl_ms:3e4,ref_event_id:n}:{}})}),this.pool.setInternalErrorHandler(o=>{this.handleSessionInternalError(o).catch(t=>{u.error(this.name,`[recovery] handleSessionInternalError failed event=${o.eventId} session=${o.sessionId}: ${t instanceof Error?t.message:String(t)}`)})}),this.pool.setEventStartedHandler((o,t)=>{if(this.reportSessionSkills(t),this.config.adapterType!=="claude")return;this.claudeWorkerStatus.set(t,"busy");const n=this.bindingStore.get(t);n?.cwd&&this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:"busy",cwd:n.cwd,meta:this.buildClaudeToolbarMeta(t)})}),this.pool.setEventDoneHandler((o,t)=>{const n=this.config.adapterType??"acp";if(n==="claude"){this.claudeWorkerStatus.set(t,"ready");const i=this.bindingStore.get(t);i?.cwd&&this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:"ready",cwd:i.cwd,meta:this.buildClaudeToolbarMeta(t)})}else if(n==="agy"){const i=this.bindingStore.get(t);if(i?.cwd){const s=this.buildAgyToolbarMeta(t),r=s?.available_models??[];u.info(this.name,`[agy-toolbar-diag] eventDone push binding card session=${t} model_id=${String(s?.model_id??"")} available_models=${r.length} meta_keys=${s?Object.keys(s).join(","):"<none>"}`),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:"ready",cwd:i.cwd,meta:s})}else u.info(this.name,`[agy-toolbar-diag] eventDone skip binding card: no binding cwd session=${t}`)}}),this.pool.setSessionActivityHandler((o,t,n)=>{const i=this.selfDrivenSessions.has(o),s=this.selfDrivenLabels.get(o);t?(this.selfDrivenSessions.add(o),n&&this.selfDrivenLabels.set(o,n),this.aibotHandle.sendSessionActivitySet({session_id:o,kind:"composing",active:!0,ttl_ms:9e4})):(this.selfDrivenSessions.delete(o),this.selfDrivenLabels.delete(o),i&&this.aibotHandle.sendSessionActivitySet({session_id:o,kind:"composing",active:!1})),(i!==t||t&&n!==void 0&&n!==s)&&this.pushQueueSnapshotForSession(o)}),this.pool.startIdleSweep(),u.info(this.name,`Ready (adapter: ${e}, poolMax: ${this.config.poolMaxSize??20})`)}async stop(){this.stopped=!0,this.pool?.stopIdleSweep(),this.stopKiroQuotaTimer();const e=this.pool?.collectActiveEventIds()??[];e.length>0&&this.activeEventStore&&await this.activeEventStore.save(e);for(const n of e)u.info(this.name,`Canceling active event on shutdown: ${n}`),this.sendEventResultWithCleanup(n,"canceled","process shutting down");e.length>0&&await new Promise(n=>setTimeout(n,100)),this.pool?.clearActiveEventsForShutdown();const o=this.deferredMgr.getAllDeferredEvents();for(const n of o)u.info(this.name,`Failing deferred event on shutdown: ${n.event_id}`),this.sendEventResultWithCleanup(n.event_id,"failed","process shutting down");const t=this.pool?.drainAllQueuedEvents()??[];for(const n of t)u.info(this.name,`Failing queued event on shutdown: ${n.event_id}`),this.sendEventResultWithCleanup(n.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(),this.inflightEvents.clear(),this.restartCount.clear()}resolveSpawnEnv(){const e=ue(this.config.agent.clientType,this.config.agent.provider),o=this.config.agent.env,t=Object.keys(e).length>0?{...e,...o}:o;return de(t,this.name)}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);case"agy":return this.createAgyAdapter(o);default:return this.createAcpAdapter(o)}}createCursorAdapter(e){const o={...this.config.adapterOptions??{}};o.bindingStore=this.bindingStore,o.aibotSessionId=e;const t=this.bindingStore.get(e),n=t?.modelId??this.globalConfigStore?.get(this.name)?.modelId;n&&(o.model=n),t?.modeId&&(o.mode=t.modeId);const i={sendStreamChunk:(s,r,a,d,c)=>{this.sendStreamChunkByRuntimeConfig(s,r,a,d,c)},sendEventResult:(s,r,a)=>{this.sendEventResultWithCleanup(s,r,a)},sendEventAck:(s,r)=>{this.aibotHandle.sendEventAck({event_id:s,session_id:r,received_at:Date.now()})},sendRawEventEnvelope:(s,r,a)=>{this.aibotHandle.sendMsg({event_id:s,session_id:r,msg_type:1,content:"[cursor] raw_event",extra:{channel_data:{cursor:{raw_event:a}},agent_api_origin:!0}})},agentInvoke:async(s,r,a)=>this.platformInvoke(s,r,a),sendLocalActionResult:(s,r,a,d,c)=>{this.aibotHandle.sendLocalActionResult({action_id:s,status:r,...a!==void 0?{result:a}:{},...d?{error_code:d}:{},...c?{error_msg:c}:{}})}};return new I({command:this.config.agent.command,args:this.config.agent.args,env:this.resolveSpawnEnv(),options:o},i)}createClaudeAdapter(e){const o={sendReply:(i,s,r,a,d)=>{this.sendReplyByRuntimeConfig(i,s,r,a,d)},sendStreamChunk:(i,s,r,a,d,c,l)=>{this.sendStreamChunkByRuntimeConfig(i,s,r,a,d,c,l)},sendMedia:(i,s,r,a,d,c,l)=>{this.aibotHandle.sendMedia({event_id:i,session_id:s,content:r,msg_type:2,quoted_message_id:d||void 0,client_msg_id:c||void 0,extra:l?{media_caption:a,...l}:{media_caption:a}})},sendEventResult:(i,s,r,a)=>{this.sendEventResultWithCleanup(i,s,r,a)},sendEventAck:(i,s)=>{this.aibotHandle.sendEventAck({event_id:i,session_id:s,received_at:Date.now()})},agentInvoke:async(i,s,r)=>this.platformInvoke(i,s,r),sendLocalActionResult:(i,s,r,a,d)=>{this.aibotHandle.sendLocalActionResult({action_id:i,status:s,...r!==void 0?{result:r}:{},...a?{error_code:a}:{},...d?{error_msg:d}:{}})},sendToolUse:(i,s,r,a)=>{this.sendToolExecutionCard(i,s,E(r,a))},sendToolResult:(i,s,r,a)=>{this.sendToolExecutionCard(i,s,$(r,a))},getWsUrl:()=>this.config.aibot.url,getAgentId:()=>this.config.aibot.agentId,getAgentProfile:()=>this.agentProfile,getApiKey:()=>this.config.aibot.apiKey,getActiveEventCount:()=>0,getPendingPermissionCount:()=>0,getPendingElicitationCount:()=>0,sendAgentQuestionCard:(i,s,r)=>{const a=r.questions.map(c=>c.header).join(", "),d=Oe(`[Agent Question] ${r.request_id}`,"agent_question",r);this.aibotHandle.sendText({event_id:i,session_id:s,content:d,msg_type:1,extra:{card_type:"agent_question",summary_text:a}})},sendPermissionCard:i=>{this.aibotHandle.sendMsg({event_id:i.eventId,session_id:i.sessionId,client_msg_id:`perm_${M()}`,msg_type:1,content:i.toolTitle?`Permission required: ${i.toolTitle}`:"Permission request",extra:{channel_data:{execApproval:{approvalId:i.approvalId,approvalSlug:i.toolName},grix:{execApproval:{approval_command_id:i.approvalId,command:i.toolTitle||i.toolName,host:"claude"}}},agent_api_origin:!0}})},sendDirectMessage:i=>{this.aibotHandle.sendMsg({session_id:i.sessionId,msg_type:1,content:i.content,...i.clientMsgId?{client_msg_id:i.clientMsgId}:{},...i.quotedMessageId?{quoted_message_id:i.quotedMessageId}:{}})},onStatusLineUpdated:i=>{(i.rateLimits?.fiveHour||i.rateLimits?.sevenDay)&&(this.cachedClaudeRateLimitState=i);const s=this.bindingStore.get(e);s?.cwd&&this.aibotHandle.sendUpdateBindingCard({session_id:e,worker_status:this.claudeWorkerStatus.get(e)??"ready",cwd:s.cwd,meta:this.buildClaudeToolbarMeta(e)})},sendMcpFrame:i=>{this.aibotHandle.sendMcpFrame(e,i)}},t=this.config.adapterOptions??{},n={...t,sessionRuntimeResolver:()=>{const i=this.bindingStore.get(e);return{cwd:i?.cwd,modeId:i?.modeId??C.fullAuto,modelId:i?.modelId??this.globalConfigStore?.get(this.name)?.modelId,pluginDir:t.pluginDir,claudeSessionId:i?.claudeSessionId,onSessionIdAssigned:s=>{this.bindingStore.setClaudeSessionId(e,s),this.sessionScanCache.invalidate()}}}};return new y({command:this.config.agent.command,args:this.config.agent.args,env:this.resolveSpawnEnv(),options:n},o)}createCodexAdapter(e){let o=null;const t={sendEventResult:(s,r,a)=>{this.sendEventResultWithCleanup(s,r,a)},sendEventAck:(s,r)=>this.aibotHandle.sendEventAck({event_id:s,session_id:r,received_at:Date.now()}),sendCodexEvent:s=>{this.shouldDropCodexDisplayEvent(s.event_id,s.codex_method)||(this.aibotHandle.sendCodexEvent(s),this.logCodexEventToConversation(s))},sendCodexEventReliable:async s=>{if(!this.shouldDropCodexDisplayEvent(s.event_id,s.codex_method)){try{await this.aibotHandle.sendCodexEventReliable(s)}catch(r){u.warn("bridge",`[codex] sendCodexEventReliable ACK failed event=${s.event_id}: ${r}`)}u.info("bridge",`[codex] sendCodexEventReliable done event=${s.event_id} method=${s.codex_method}`),this.logCodexEventToConversation(s)}},sendRunError:(s,r,a)=>{this.sendStreamChunkByRuntimeConfig(s,r,`
1
+ import T from"node:path";import{realpath as j,stat as H}from"node:fs/promises";import{tmpdir as V}from"node:os";import{randomUUID as M}from"node:crypto";import{ConnectionManager as Y}from"../core/aibot/index.js";import{ClaudeAdapter as y}from"../adapter/claude/index.js";import{CodexAdapter as J}from"../adapter/codex/index.js";import{PiAdapter as X}from"../adapter/pi/index.js";import{AcpAdapter as A}from"../adapter/acp/index.js";import{OpenHumanAdapter as Z}from"../adapter/openhuman/index.js";import{CursorAdapter as I}from"../adapter/cursor/index.js";import{CodeWhaleAdapter as U}from"../adapter/codewhale/index.js";import{OpenCodeAdapter as ee}from"../adapter/opencode/index.js";import{AgyAdapter as O}from"../adapter/agy/index.js";import{getCachedAgyModels as te}from"../adapter/agy/model-list.js";import{getCachedAgyQuotaInfo as ie}from"../adapter/agy/quota.js";import{LOCAL_ACTION_ERROR_CODES as x,LOCAL_ACTION_TYPES as b,SESSION_CONTROL_ERROR_CODES as g,SESSION_CONTROL_VERBS as p,SESSION_MODE_IDS as C}from"../adapter/claude/protocol-contract.js";import{parseClaudeSessionUsage as ne}from"../adapter/claude/usage-parser.js";import{resolveSkillScanMode as se}from"../adapter/claude/skill-scanner.js";import{buildReportedSkills as oe}from"../default-skills/index.js";import{fetchAvailableModels as D,getCachedModels as Q,readSettingsEnv as re}from"../adapter/claude/model-list.js";import{parseAcpSessionUsage as ae}from"../adapter/acp/usage-parser.js";import{applyProxyEnv as de,getProxyManager as ce,relayHostForClientType as le}from"../core/proxy/index.js";import{buildProviderEnv as ue}from"../core/config/provider-env.js";import{parseCodexSessionUsage as he}from"../adapter/codex/usage-parser.js";import{readCodexProviderSettings as ge}from"../adapter/codex/codex-trust.js";import{scanCodexSessions as me}from"../adapter/codex/session-scanner.js";import{scanClaudeSessions as pe}from"../adapter/claude/session-scanner.js";import{scanAcpSessions as fe}from"../adapter/acp/session-scanner.js";import{parsePiSessionUsage as _e}from"../adapter/pi/usage-parser.js";import{SessionScanCache as L,resolveCodexLeafDirs as ve,resolveClaudeLeafDirs as Se,resolveAcpLeafDirs as be}from"./session-scan-cache.js";import{log as u,ConversationLog as we,AgentApiPacketLog as Ce,BridgeEventLog as Ae}from"../core/log/index.js";import{RevokeHandler as ke}from"./revoke-handler.js";import{AdapterPool as Re,PoolFullError as Ee}from"./adapter-pool.js";import{parseSessionControlCommand as $e,handleSessionControlCommand as q,handleSessionControlLocalAction as ye}from"./session-controller.js";import{handleAcpSetModel as W,handleAcpSetMode as N,resolveAcpInitialDefaults as xe}from"./acp-toolbar-persist.js";import{SessionBindingStore as Te}from"../core/persistence/session-binding-store.js";import{handleFileListAction as Le,handleCreateFolderAction as Pe,serveLocalFile as He,realHomeDir as K}from"../core/files/index.js";import{getMachineName as Me}from"../core/util/index.js";import{uploadReplyFileToAgentMedia as Ie}from"../core/protocol/agent-api-media.js";import{ActiveEventStore as De}from"../core/persistence/active-event-store.js";import{DEFAULT_CONNECTOR_RUNTIME_CONFIG as Qe,applyConnectorRuntimeConfigPatch as Be,extractConnectorRuntimeConfigPatch as Fe}from"./runtime-config.js";import{SendController as Ue}from"./send-controller.js";import{queryProviderQuota as z}from"../core/provider-quota/index.js";import{queryKiroQuota as B}from"../core/provider-quota/kiro.js";import{buildToolUseCard as E,buildToolResultCard as $,buildLocalGrixCardLink as Oe}from"./tool-card-utils.js";import{DeferredEventManager as qe}from"./deferred-events.js";import{buildAgentProbeResult as We,PROBE_CACHE_TTL_STATIC_MS as Ne,PROBE_CACHE_TTL_FULL_MS as Ke}from"./probe-helper.js";const ze=600*1e3,Ge=60*1e3,G=30*1e3,je=10*1e3,P=new Set(["claude","acp","agy","cursor","codex"]),Ve=new Set(["claude","codex","cursor","codewhale","opencode","pi","openhuman","agy","acp"]),F=3;class Kt{config;name;aibotHandle;aibotConfig;pool;stopped=!1;revokeHandler=new ke;sessionBindings=new Map;deferredMgr;sendCtrl=new Ue(Qe);bindingStore;globalConfigStore;upgradeTrigger=null;agentDeletedHandler=null;shareSetHandler=null;providerConfigHandler=null;agentProfile={agentName:"",introduction:""};activeEventStore;cachedRateLimits=null;cachedRateLimitsSampledAtMs=null;cachedCodexContextWindow=null;cachedCodexTokenUsage=null;cachedCodexUsageSampledAtMs=null;cachedAcpContextWindow=null;cachedAcpContextWindowSampledAtMs=null;cachedClaudeRateLimitState=null;cachedProviderQuota=null;cachedProviderQuotaSampledAtMs=null;claudeWorkerStatus=new Map;lastReportedSkillsHash="";conversationLog=null;packetLog=null;kiroQuotaTimer=null;eventSessionIndex=new Map;inflightEvents=new Map;restartCount=new Map;selfDrivenSessions=new Set;selfDrivenLabels=new Map;probeCache=new Map;sessionScanCache;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=re();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=ge();!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 z(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(n=>`${n.name}=${n.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}}startKiroQuotaTimer(){this.kiroQuotaTimer||(this.kiroQuotaTimer=setInterval(()=>{if(this.stopped){this.stopKiroQuotaTimer();return}this.refreshAndPushKiroQuota().catch(()=>{})},G),u.info(this.name,`[kiro-quota-timer] started (interval=${G}ms)`))}stopKiroQuotaTimer(){this.kiroQuotaTimer&&(clearInterval(this.kiroQuotaTimer),this.kiroQuotaTimer=null,u.info(this.name,"[kiro-quota-timer] stopped"))}async refreshAndPushKiroQuota(){try{const e=await B();this.cachedProviderQuota=e,this.cachedProviderQuotaSampledAtMs=Date.now(),e.success&&this.pushKiroQuotaToBindings(e)}catch{}}pushKiroQuotaToBindings(e){const o=this.providerQuotaToRateLimits(e);for(const[t,n]of this.sessionBindings.entries()){if(!n)continue;const i={provider_quota:e};o&&(i.rate_limits=o);const s=this.cachedAcpContextWindow;s&&("usedPercentage"in s?i.context_window={usedPercentage:s.usedPercentage,remainingPercentage:100-s.usedPercentage}:i.context_window=s),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:"ready",cwd:n,meta:i})}}getFreshClaudeRateLimitState(){const e=this.cachedClaudeRateLimitState;return e&&this.isRateLimitsCacheFresh(e.sampledAt)?e:null}getFreshCodexGlobalRateLimitCache(){const e=this.cachedRateLimits,o=this.cachedCodexContextWindow,t=this.cachedCodexTokenUsage;return{sampledAt:Math.max(this.cachedRateLimitsSampledAtMs??0,this.cachedCodexUsageSampledAtMs??0)||null,rateLimits:e,contextWindow:o,tokenUsage:t,hasData:!!(e||o||t)}}getStatus(){const e=this.pool?.getStatus()??{total:0,ready:0,busy:0};return{name:this.name,agentId:this.config.aibot.agentId,alive:!this.stopped,busy:e.busy>0,exhausted:this.pool?[...this.pool.getAllSlots()].some(o=>o.respawn.exhausted):!1,adapterType:this.config.adapterType??"acp",clientType:this.config.aibot.clientType,pool:e}}hasPendingWork(){return this.pool?.hasPendingWork()??!1}relayEnvStale=!1;relayEnvSettledHandler;async recycleAdaptersForRelayChange(){if(!this.pool)return this.relayEnvStale=!1,!0;if(this.hasPendingWork())return this.relayEnvStale=!0,u.info(this.name,"relay changed but agent is busy; adapters keep the old env until the next message (marked stale)"),!1;const e=[...this.pool.getAllSlots()];let o=0,t=!1;for(const n of e){if(this.hasPendingWork())return this.relayEnvStale=!0,u.info(this.name,"new work arrived while recycling; remaining adapters stay stale until the next message"),!1;let i=!1;await this.pool.removeSlot(n.sessionId).catch(s=>{t=!0,i=!0,u.warn(this.name,`failed to recycle adapter slot ${n.sessionId} after relay change: ${s}`)}),i||(o+=1)}return o>0&&u.info(this.name,`recycled ${o} adapter slot(s) so the new Grix relay env takes effect`),t?(this.relayEnvStale=!0,!1):(this.relayEnvStale=!1,this.relayEnvSettledHandler?.(),!0)}setRelayEnvSettledHandler(e){this.relayEnvSettledHandler=e}hasStaleRelayEnv(){return this.relayEnvStale}markRelayEnvStale(){this.relayEnvStale=!0}async probe(e={}){const o=e.conversation?"full":"static",t=e.conversation?Ke:Ne;if(!e.fresh){const a=this.probeCache.get(o);if(a&&Date.now()-a.sampledAt<t)return{...a.result,cached:!0}}const n=this.config.adapterType??"acp",i=this.createAdapter(n,"__probe__"),s=e.conversation&&n==="acp"?()=>this.runAcpConversationProbe(i,e.timeoutMs??1e4):void 0;let r;try{r=await We({adapter:i,agentName:this.name,clientType:this.config.aibot.clientType,adapterType:n,providerBaseUrl:this.config.providerBaseUrl??null,opts:e,launchConversationProbe:s})}finally{i.stop().catch(()=>{})}return this.probeCache.set(o,{result:r,sampledAt:r.probed_at}),r}async runAcpConversationProbe(e,o){const t=Date.now(),n="__probe__",i=`probe-${Date.now()}-${Math.random().toString(36).slice(2,8)}`,s=this.config.agent.cwd||V(),r=()=>Date.now()-t;try{await e.start()}catch(h){return{attempted:!0,ok:!1,latency_ms:r(),error:{code:"conversation_failed",message:`start failed: ${h instanceof Error?h.message:String(h)}`}}}if(!e.isAlive())return{attempted:!0,ok:!1,latency_ms:r(),error:{code:"process_not_started",message:"agent process not alive"}};if(e instanceof A)try{await e.bindSession(n,s)}catch{}let a=null;const d=new Promise(h=>{a=m=>{m===i&&h()},e.on("eventDone",a)});e.deliverInboundEvent({event_id:i,session_id:n,content:"ping",msg_id:i}),e.deliverStopEvent(i,n);let c=null;const l=await Promise.race([d.then(()=>!1),new Promise(h=>{c=setTimeout(()=>h(!0),o)})]);return c&&clearTimeout(c),a&&e.removeListener("eventDone",a),l?{attempted:!0,ok:!1,latency_ms:r(),error:{code:"conversation_timeout",message:`no eventDone within ${o}ms`}}:{attempted:!0,ok:!0,latency_ms:r()}}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 we(e.logDir):null,this.packetLog=e.logDir?new Ce(e.logDir):null,this.bindingStore=new Te(e.bindingsPath),this.bindingStore.load(),this.globalConfigStore=o??null,this.deferredMgr=new qe(this.name),this.activeEventStore=e.activeEventStorePath?new De(e.activeEventStorePath):null,t==="codex"?this.sessionScanCache=new L(me,ve):t==="claude"?this.sessionScanCache=new L(pe,Se):t==="agy"?this.sessionScanCache=new L(()=>[],()=>[]):this.sessionScanCache=new L(fe,be)}async start(){(this.config.adapterType??"acp")==="claude"&&(await D().catch(()=>{}),this.maybeQueryProviderQuota().catch(()=>{})),this.config.aibot.clientType==="kiro"&&(this.maybeQueryProviderQuota().catch(()=>{}),this.startKiroQuotaTimer()),(this.config.adapterType??"acp")==="codex"&&this.maybeQueryProviderQuota().catch(()=>{}),await this.connectAibot(),this.sendCtrl.bind(this.aibotHandle);const e=this.config.adapterType??"acp";this.pool=new Re({maxPoolSize:this.config.poolMaxSize??20,idleTimeoutMs:this.config.poolIdleTimeoutMs??18e5,eventQueue:this.config.eventQueue},o=>{const t=this.createAdapter(e,o);return t instanceof A&&t.on("acpSessionReady",n=>{this.bindingStore.setAcpSessionId(o,n),this.sessionScanCache.invalidate()}),t},(o,t)=>{this.aibotHandle.sendEventAck({event_id:o,session_id:t,received_at:Date.now()})}),this.pool.setEventStateHandler((o,t,n,i)=>{u.info(this.name,`[queue-debug] send event_state session=${t} event=${o} state=${n} queue_pos=${i?.queue_position??""} queue_total=${i?.queue_total??""}`),this.aibotHandle.sendEventState({event_id:o,session_id:t,state:n,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),(n==="canceled"||n==="failed")&&this.aibotHandle.sendEventResult({event_id:o,status:n==="canceled"?"canceled":"failed",msg:i?.reason,updated_at:Date.now()})}),this.pool.setQueueComposingHandler((o,t,n)=>{this.aibotHandle.sendSessionActivitySet({session_id:o,kind:"composing",active:t,...t?{ttl_ms:3e4,ref_event_id:n}:{}})}),this.pool.setInternalErrorHandler(o=>{this.handleSessionInternalError(o).catch(t=>{u.error(this.name,`[recovery] handleSessionInternalError failed event=${o.eventId} session=${o.sessionId}: ${t instanceof Error?t.message:String(t)}`)})}),this.pool.setEventStartedHandler((o,t)=>{if(this.reportSessionSkills(t),this.config.adapterType!=="claude")return;this.claudeWorkerStatus.set(t,"busy");const n=this.bindingStore.get(t);n?.cwd&&this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:"busy",cwd:n.cwd,meta:this.buildClaudeToolbarMeta(t)})}),this.pool.setEventDoneHandler((o,t)=>{const n=this.config.adapterType??"acp";if(n==="claude"){this.claudeWorkerStatus.set(t,"ready");const i=this.bindingStore.get(t);i?.cwd&&this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:"ready",cwd:i.cwd,meta:this.buildClaudeToolbarMeta(t)})}else if(n==="agy"){const i=this.bindingStore.get(t);if(i?.cwd){const s=this.buildAgyToolbarMeta(t),r=s?.available_models??[];u.info(this.name,`[agy-toolbar-diag] eventDone push binding card session=${t} model_id=${String(s?.model_id??"")} available_models=${r.length} meta_keys=${s?Object.keys(s).join(","):"<none>"}`),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:"ready",cwd:i.cwd,meta:s})}else u.info(this.name,`[agy-toolbar-diag] eventDone skip binding card: no binding cwd session=${t}`)}}),this.pool.setSessionActivityHandler((o,t,n)=>{const i=this.selfDrivenSessions.has(o),s=this.selfDrivenLabels.get(o);t?(this.selfDrivenSessions.add(o),n&&this.selfDrivenLabels.set(o,n),this.aibotHandle.sendSessionActivitySet({session_id:o,kind:"composing",active:!0,ttl_ms:9e4})):(this.selfDrivenSessions.delete(o),this.selfDrivenLabels.delete(o),i&&this.aibotHandle.sendSessionActivitySet({session_id:o,kind:"composing",active:!1})),(i!==t||t&&n!==void 0&&n!==s)&&this.pushQueueSnapshotForSession(o)}),this.pool.startIdleSweep(),u.info(this.name,`Ready (adapter: ${e}, poolMax: ${this.config.poolMaxSize??20})`)}async stop(){this.stopped=!0,this.pool?.stopIdleSweep(),this.stopKiroQuotaTimer();const e=this.pool?.collectActiveEventIds()??[];e.length>0&&this.activeEventStore&&await this.activeEventStore.save(e);for(const n of e)u.info(this.name,`Canceling active event on shutdown: ${n}`),this.sendEventResultWithCleanup(n,"canceled","process shutting down");e.length>0&&await new Promise(n=>setTimeout(n,100)),this.pool?.clearActiveEventsForShutdown();const o=this.deferredMgr.getAllDeferredEvents();for(const n of o)u.info(this.name,`Failing deferred event on shutdown: ${n.event_id}`),this.sendEventResultWithCleanup(n.event_id,"failed","process shutting down");const t=this.pool?.drainAllQueuedEvents()??[];for(const n of t)u.info(this.name,`Failing queued event on shutdown: ${n.event_id}`),this.sendEventResultWithCleanup(n.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(),this.inflightEvents.clear(),this.restartCount.clear()}resolveSpawnEnv(){const e=ue(this.config.agent.clientType,this.config.agent.provider),o=this.config.agent.env,t=Object.keys(e).length>0?{...e,...o}:o;return de(t,this.name)}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);case"agy":return this.createAgyAdapter(o);default:return this.createAcpAdapter(o)}}createCursorAdapter(e){const o={...this.config.adapterOptions??{}};o.bindingStore=this.bindingStore,o.aibotSessionId=e;const t=this.bindingStore.get(e),n=t?.modelId??this.globalConfigStore?.get(this.name)?.modelId;n&&(o.model=n),t?.modeId&&(o.mode=t.modeId);const i={sendStreamChunk:(s,r,a,d,c)=>{this.sendStreamChunkByRuntimeConfig(s,r,a,d,c)},sendEventResult:(s,r,a)=>{this.sendEventResultWithCleanup(s,r,a)},sendEventAck:(s,r)=>{this.aibotHandle.sendEventAck({event_id:s,session_id:r,received_at:Date.now()})},sendRawEventEnvelope:(s,r,a)=>{this.aibotHandle.sendMsg({event_id:s,session_id:r,msg_type:1,content:"[cursor] raw_event",extra:{channel_data:{cursor:{raw_event:a}},agent_api_origin:!0}})},agentInvoke:async(s,r,a)=>this.platformInvoke(s,r,a),sendLocalActionResult:(s,r,a,d,c)=>{this.aibotHandle.sendLocalActionResult({action_id:s,status:r,...a!==void 0?{result:a}:{},...d?{error_code:d}:{},...c?{error_msg:c}:{}})}};return new I({command:this.config.agent.command,args:this.config.agent.args,env:this.resolveSpawnEnv(),options:o},i)}createClaudeAdapter(e){const o={sendReply:(i,s,r,a,d)=>{this.sendReplyByRuntimeConfig(i,s,r,a,d)},sendStreamChunk:(i,s,r,a,d,c,l)=>{this.sendStreamChunkByRuntimeConfig(i,s,r,a,d,c,l)},sendMedia:(i,s,r,a,d,c,l)=>{this.aibotHandle.sendMedia({event_id:i,session_id:s,content:r,msg_type:2,quoted_message_id:d||void 0,client_msg_id:c||void 0,extra:l?{media_caption:a,...l}:{media_caption:a}})},sendEventResult:(i,s,r,a)=>{this.sendEventResultWithCleanup(i,s,r,a)},sendEventAck:(i,s)=>{this.aibotHandle.sendEventAck({event_id:i,session_id:s,received_at:Date.now()})},agentInvoke:async(i,s,r)=>this.platformInvoke(i,s,r),sendLocalActionResult:(i,s,r,a,d)=>{this.aibotHandle.sendLocalActionResult({action_id:i,status:s,...r!==void 0?{result:r}:{},...a?{error_code:a}:{},...d?{error_msg:d}:{}})},sendToolUse:(i,s,r,a)=>{this.sendToolExecutionCard(i,s,E(r,a))},sendToolResult:(i,s,r,a)=>{this.sendToolExecutionCard(i,s,$(r,a))},getWsUrl:()=>this.config.aibot.url,getAgentId:()=>this.config.aibot.agentId,getAgentProfile:()=>this.agentProfile,getApiKey:()=>this.config.aibot.apiKey,getActiveEventCount:()=>0,getPendingPermissionCount:()=>0,getPendingElicitationCount:()=>0,sendAgentQuestionCard:(i,s,r)=>{const a=r.questions.map(c=>c.header).join(", "),d=Oe(`[Agent Question] ${r.request_id}`,"agent_question",r);this.aibotHandle.sendText({event_id:i,session_id:s,content:d,msg_type:1,extra:{card_type:"agent_question",summary_text:a}})},sendPermissionCard:i=>{this.aibotHandle.sendMsg({event_id:i.eventId,session_id:i.sessionId,client_msg_id:`perm_${M()}`,msg_type:1,content:i.toolTitle?`Permission required: ${i.toolTitle}`:"Permission request",extra:{channel_data:{execApproval:{approvalId:i.approvalId,approvalSlug:i.toolName},grix:{execApproval:{approval_command_id:i.approvalId,command:i.toolTitle||i.toolName,host:"claude"}}},agent_api_origin:!0}})},sendDirectMessage:i=>{this.aibotHandle.sendMsg({session_id:i.sessionId,msg_type:1,content:i.content,...i.clientMsgId?{client_msg_id:i.clientMsgId}:{},...i.quotedMessageId?{quoted_message_id:i.quotedMessageId}:{}})},onStatusLineUpdated:i=>{(i.rateLimits?.fiveHour||i.rateLimits?.sevenDay)&&(this.cachedClaudeRateLimitState=i);const s=this.bindingStore.get(e);s?.cwd&&this.aibotHandle.sendUpdateBindingCard({session_id:e,worker_status:this.claudeWorkerStatus.get(e)??"ready",cwd:s.cwd,meta:this.buildClaudeToolbarMeta(e)})},sendMcpFrame:i=>{this.aibotHandle.sendMcpFrame(e,i)}},t=this.config.adapterOptions??{},n={...t,sessionRuntimeResolver:()=>{const i=this.bindingStore.get(e);return{cwd:i?.cwd,modeId:i?.modeId??C.fullAuto,modelId:i?.modelId??this.globalConfigStore?.get(this.name)?.modelId,pluginDir:t.pluginDir,claudeSessionId:i?.claudeSessionId,onSessionIdAssigned:s=>{this.bindingStore.setClaudeSessionId(e,s),this.sessionScanCache.invalidate()}}}};return new y({command:this.config.agent.command,args:this.config.agent.args,env:this.resolveSpawnEnv(),options:n},o)}createCodexAdapter(e){let o=null;const t={sendEventResult:(s,r,a)=>{this.sendEventResultWithCleanup(s,r,a)},sendEventAck:(s,r)=>this.aibotHandle.sendEventAck({event_id:s,session_id:r,received_at:Date.now()}),sendCodexEvent:s=>{this.shouldDropCodexDisplayEvent(s.event_id,s.codex_method)||(this.aibotHandle.sendCodexEvent(s),this.logCodexEventToConversation(s))},sendCodexEventReliable:async s=>{if(!this.shouldDropCodexDisplayEvent(s.event_id,s.codex_method)){try{await this.aibotHandle.sendCodexEventReliable(s)}catch(r){u.warn("bridge",`[codex] sendCodexEventReliable ACK failed event=${s.event_id}: ${r}`)}u.info("bridge",`[codex] sendCodexEventReliable done event=${s.event_id} method=${s.codex_method}`),this.logCodexEventToConversation(s)}},sendRunError:(s,r,a)=>{this.sendStreamChunkByRuntimeConfig(s,r,`
2
2
 
3
- Error: ${a}`,1,!1)},sendUpdateBindingCard:(s,r,a,d)=>{const c={...d??{}};if(!c.rate_limits&&this.cachedProviderQuota?.success){this.isRateLimitsCacheFresh(this.cachedProviderQuotaSampledAtMs)||this.maybeQueryProviderQuota().catch(()=>{});const l=this.providerQuotaToCodexRateLimits(this.cachedProviderQuota);l&&(c.rate_limits=l.rateLimits,c.rate_limit_primary_percent=l.primaryPercent,c.rate_limit_secondary_percent=l.secondaryPercent,c.rate_limit_primary_window_min=l.primaryWindowMin,c.rate_limit_secondary_window_min=l.secondaryWindowMin)}!c.provider_quota&&this.cachedProviderQuota?.success&&(c.provider_quota=this.cachedProviderQuota),this.aibotHandle.sendUpdateBindingCard({session_id:s,worker_status:r,cwd:a,...Object.keys(c).length>0?{meta:c}:{}})},agentInvoke:async(s,r,a)=>this.platformInvoke(s,r,a),sendLocalActionResult:(s,r,a,d,c)=>this.aibotHandle.sendLocalActionResult({action_id:s,status:r,...a!==void 0?{result:a}:{},...d?{error_code:d}:{},...c?{error_msg:c}:{}}),sendSessionActivitySet:(s,r,a,d)=>{this.aibotHandle.sendSessionActivitySet({session_id:s,kind:r,active:a,...d??{}})},getConversationLog:()=>this.conversationLog,getAgentProfile:()=>this.agentProfile,onRateLimitsUpdated:s=>{this.cachedRateLimits=s,this.cachedRateLimitsSampledAtMs=Date.now(),this.isRateLimitsCacheFresh(this.cachedProviderQuotaSampledAtMs)||this.maybeQueryProviderQuota().catch(()=>{});const r=this.bindingStore.get(e);if(r?.cwd){const a=this.cachedRateLimitsSampledAtMs,d={rate_limits:{primary:s.primary,secondary:s.secondary,sampledAt:a},rate_limit_primary_percent:s.primary.usedPercent,rate_limit_secondary_percent:s.secondary.usedPercent,rate_limit_primary_window_min:s.primary.windowMinutes,rate_limit_secondary_window_min:s.secondary.windowMinutes,...o?.getEffortMeta()};this.cachedProviderQuota?.success&&(d.provider_quota=this.cachedProviderQuota),this.aibotHandle.sendUpdateBindingCard({session_id:e,worker_status:"ready",cwd:r.cwd,meta:d})}},onContextWindowUpdated:s=>{if(!s)return;this.cachedCodexContextWindow=s,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:s,...o?.getEffortMeta()}})},onTokenUsageUpdated:s=>{s&&(this.cachedCodexTokenUsage=s,this.cachedCodexUsageSampledAtMs=Date.now())}},n=this.config.adapterOptions??{},i=this.globalConfigStore?.get(this.name);return o=new J({command:this.config.agent.command,args:this.config.agent.args,env:this.resolveSpawnEnv(),options:{...n,model:this.bindingStore.getCodexModelId(e)??n.model,collaborationMode:this.bindingStore.getCodexModeId(e)??n.collaborationMode,reasoningEffort:i?.codexReasoningEffort??n.reasoningEffort,serviceTier:this.bindingStore.getCodexServiceTier(e)??n.serviceTier,sandboxMode:i?.codexSandboxMode??n.sandboxMode,aibotSessionId:e,bindingStore:this.bindingStore}},t),o}createCodeWhaleAdapter(e){const o={sendEventResult:(n,i,s)=>{this.sendEventResultWithCleanup(n,i,s)},sendEventAck:(n,i)=>this.aibotHandle.sendEventAck({event_id:n,session_id:i,received_at:Date.now()}),sendStreamChunk:(n,i,s,r,a,d)=>{this.sendStreamChunkByRuntimeConfig(n,i,s,r,a,d)},sendUpdateBindingCard:(n,i,s,r)=>this.aibotHandle.sendUpdateBindingCard({session_id:n,worker_status:i,cwd:s,...r?{meta:r}:{}}),sendLocalActionResult:(n,i,s,r,a)=>this.aibotHandle.sendLocalActionResult({action_id:n,status:i,...s!==void 0?{result:s}:{},...r?{error_code:r}:{},...a?{error_msg:a}:{}}),sendSessionActivitySet:(n,i,s,r)=>{this.aibotHandle.sendSessionActivitySet({session_id:n,kind:i,active:s,...r??{}})},sendToolUse:(n,i,s,r)=>{this.sendToolExecutionCard(n,i,E(s,r))},sendToolResult:(n,i,s,r)=>{this.sendToolExecutionCard(n,i,$(s,r))},agentInvoke:async(n,i,s)=>this.platformInvoke(n,i,s),getConversationLog:()=>this.conversationLog,getAgentProfile:()=>this.agentProfile},t=this.config.adapterOptions??{};return new U({command:this.config.agent.command,args:this.config.agent.args,env:this.resolveSpawnEnv(),options:{...t,aibotSessionId:e,bindingStore:this.bindingStore}},o)}createPiAdapter(e){const o={sendEventResult:(n,i,s)=>{this.sendEventResultWithCleanup(n,i,s),u.info("bridge",`[pi] sendEventResult event=${n} status=${i}`)},sendEventAck:(n,i)=>this.aibotHandle.sendEventAck({event_id:n,session_id:i,received_at:Date.now()}),sendUpdateBindingCard:(n,i,s,r)=>this.aibotHandle.sendUpdateBindingCard({session_id:n,worker_status:i,cwd:s,...r?{meta:r}:{}}),agentInvoke:async(n,i,s)=>this.platformInvoke(n,i,s),sendLocalActionResult:(n,i,s,r,a)=>{this.aibotHandle.sendLocalActionResult({action_id:n,status:i,...s!==void 0?{result:s}:{},...r?{error_code:r}:{},...a?{error_msg:a}:{}})},sendSessionActivitySet:(n,i,s,r)=>{this.aibotHandle.sendSessionActivitySet({session_id:n,kind:i,active:s,...r??{}})},sendToolUse:(n,i,s,r)=>{this.sendToolExecutionCard(n,i,E(s,r))},sendToolResult:(n,i,s,r)=>{this.sendToolExecutionCard(n,i,$(s,r))},sendStreamChunk:(n,i,s,r,a,d)=>{this.sendStreamChunkByRuntimeConfig(n,i,s,r,a,d),a&&u.info("bridge",`[pi] sendFinalStreamChunk event=${n} seq=${r}`)},sendFinalStreamChunkReliable:async(n,i,s,r)=>{if(this.finalizeThinking(n,i),this.resolveEventRuntimeConfig(n).responseDelivery==="single_message"&&n){this.bufferStreamChunk(n,i,"",!0);return}try{await this.aibotHandle.sendStreamChunkRequest({event_id:n,session_id:i,delta_content:"",chunk_seq:s,is_finish:!0,...r?{client_msg_id:r}:{}})}catch(d){u.warn("bridge",`[pi] sendFinalStreamChunkReliable ACK failed event=${n}: ${d}`)}u.info("bridge",`[pi] sendFinalStreamChunkReliable done event=${n} seq=${s}`)},sendThinking:(n,i,s)=>{this.sendThinkingByRuntimeConfig(n,i,s)},sendRunError:(n,i,s,r,a)=>{this.sendStreamChunkByRuntimeConfig(n,i,`
3
+ Error: ${a}`,1,!1)},sendUpdateBindingCard:(s,r,a,d)=>{const c={...d??{}};if(!c.rate_limits&&this.cachedProviderQuota?.success){this.isRateLimitsCacheFresh(this.cachedProviderQuotaSampledAtMs)||this.maybeQueryProviderQuota().catch(()=>{});const l=this.providerQuotaToCodexRateLimits(this.cachedProviderQuota);l&&(c.rate_limits=l.rateLimits,c.rate_limit_primary_percent=l.primaryPercent,c.rate_limit_secondary_percent=l.secondaryPercent,c.rate_limit_primary_window_min=l.primaryWindowMin,c.rate_limit_secondary_window_min=l.secondaryWindowMin)}!c.provider_quota&&this.cachedProviderQuota?.success&&(c.provider_quota=this.cachedProviderQuota),this.aibotHandle.sendUpdateBindingCard({session_id:s,worker_status:r,cwd:a,...Object.keys(c).length>0?{meta:c}:{}})},agentInvoke:async(s,r,a)=>this.platformInvoke(s,r,a),sendLocalActionResult:(s,r,a,d,c)=>this.aibotHandle.sendLocalActionResult({action_id:s,status:r,...a!==void 0?{result:a}:{},...d?{error_code:d}:{},...c?{error_msg:c}:{}}),sendSessionActivitySet:(s,r,a,d)=>{this.aibotHandle.sendSessionActivitySet({session_id:s,kind:r,active:a,...d??{}})},getConversationLog:()=>this.conversationLog,getAgentProfile:()=>this.agentProfile,onRateLimitsUpdated:s=>{this.cachedRateLimits=s,this.cachedRateLimitsSampledAtMs=Date.now(),this.isRateLimitsCacheFresh(this.cachedProviderQuotaSampledAtMs)||this.maybeQueryProviderQuota().catch(()=>{});const r=this.bindingStore.get(e);if(r?.cwd){const a=this.cachedRateLimitsSampledAtMs,d={rate_limits:{...s.primary.windowMinutes>0?{primary:s.primary}:{},...s.secondary.windowMinutes>0?{secondary:s.secondary}:{},sampledAt:a},rate_limit_primary_percent:s.primary.usedPercent,rate_limit_secondary_percent:s.secondary.usedPercent,rate_limit_primary_window_min:s.primary.windowMinutes,rate_limit_secondary_window_min:s.secondary.windowMinutes,...o?.getEffortMeta()};this.cachedProviderQuota?.success&&(d.provider_quota=this.cachedProviderQuota),this.aibotHandle.sendUpdateBindingCard({session_id:e,worker_status:"ready",cwd:r.cwd,meta:d})}},onContextWindowUpdated:s=>{if(!s)return;this.cachedCodexContextWindow=s,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:s,...o?.getEffortMeta()}})},onTokenUsageUpdated:s=>{s&&(this.cachedCodexTokenUsage=s,this.cachedCodexUsageSampledAtMs=Date.now())}},n=this.config.adapterOptions??{},i=this.globalConfigStore?.get(this.name);return o=new J({command:this.config.agent.command,args:this.config.agent.args,env:this.resolveSpawnEnv(),options:{...n,model:this.bindingStore.getCodexModelId(e)??n.model,collaborationMode:this.bindingStore.getCodexModeId(e)??n.collaborationMode,reasoningEffort:i?.codexReasoningEffort??n.reasoningEffort,serviceTier:this.bindingStore.getCodexServiceTier(e)??n.serviceTier,sandboxMode:i?.codexSandboxMode??n.sandboxMode,aibotSessionId:e,bindingStore:this.bindingStore}},t),o}createCodeWhaleAdapter(e){const o={sendEventResult:(n,i,s)=>{this.sendEventResultWithCleanup(n,i,s)},sendEventAck:(n,i)=>this.aibotHandle.sendEventAck({event_id:n,session_id:i,received_at:Date.now()}),sendStreamChunk:(n,i,s,r,a,d)=>{this.sendStreamChunkByRuntimeConfig(n,i,s,r,a,d)},sendUpdateBindingCard:(n,i,s,r)=>this.aibotHandle.sendUpdateBindingCard({session_id:n,worker_status:i,cwd:s,...r?{meta:r}:{}}),sendLocalActionResult:(n,i,s,r,a)=>this.aibotHandle.sendLocalActionResult({action_id:n,status:i,...s!==void 0?{result:s}:{},...r?{error_code:r}:{},...a?{error_msg:a}:{}}),sendSessionActivitySet:(n,i,s,r)=>{this.aibotHandle.sendSessionActivitySet({session_id:n,kind:i,active:s,...r??{}})},sendToolUse:(n,i,s,r)=>{this.sendToolExecutionCard(n,i,E(s,r))},sendToolResult:(n,i,s,r)=>{this.sendToolExecutionCard(n,i,$(s,r))},agentInvoke:async(n,i,s)=>this.platformInvoke(n,i,s),getConversationLog:()=>this.conversationLog,getAgentProfile:()=>this.agentProfile},t=this.config.adapterOptions??{};return new U({command:this.config.agent.command,args:this.config.agent.args,env:this.resolveSpawnEnv(),options:{...t,aibotSessionId:e,bindingStore:this.bindingStore}},o)}createPiAdapter(e){const o={sendEventResult:(n,i,s)=>{this.sendEventResultWithCleanup(n,i,s),u.info("bridge",`[pi] sendEventResult event=${n} status=${i}`)},sendEventAck:(n,i)=>this.aibotHandle.sendEventAck({event_id:n,session_id:i,received_at:Date.now()}),sendUpdateBindingCard:(n,i,s,r)=>this.aibotHandle.sendUpdateBindingCard({session_id:n,worker_status:i,cwd:s,...r?{meta:r}:{}}),agentInvoke:async(n,i,s)=>this.platformInvoke(n,i,s),sendLocalActionResult:(n,i,s,r,a)=>{this.aibotHandle.sendLocalActionResult({action_id:n,status:i,...s!==void 0?{result:s}:{},...r?{error_code:r}:{},...a?{error_msg:a}:{}})},sendSessionActivitySet:(n,i,s,r)=>{this.aibotHandle.sendSessionActivitySet({session_id:n,kind:i,active:s,...r??{}})},sendToolUse:(n,i,s,r)=>{this.sendToolExecutionCard(n,i,E(s,r))},sendToolResult:(n,i,s,r)=>{this.sendToolExecutionCard(n,i,$(s,r))},sendStreamChunk:(n,i,s,r,a,d)=>{this.sendStreamChunkByRuntimeConfig(n,i,s,r,a,d),a&&u.info("bridge",`[pi] sendFinalStreamChunk event=${n} seq=${r}`)},sendFinalStreamChunkReliable:async(n,i,s,r)=>{if(this.finalizeThinking(n,i),this.resolveEventRuntimeConfig(n).responseDelivery==="single_message"&&n){this.bufferStreamChunk(n,i,"",!0);return}try{await this.aibotHandle.sendStreamChunkRequest({event_id:n,session_id:i,delta_content:"",chunk_seq:s,is_finish:!0,...r?{client_msg_id:r}:{}})}catch(d){u.warn("bridge",`[pi] sendFinalStreamChunkReliable ACK failed event=${n}: ${d}`)}u.info("bridge",`[pi] sendFinalStreamChunkReliable done event=${n} seq=${s}`)},sendThinking:(n,i,s)=>{this.sendThinkingByRuntimeConfig(n,i,s)},sendRunError:(n,i,s,r,a)=>{this.sendStreamChunkByRuntimeConfig(n,i,`
4
4
 
5
5
  Error: ${s}`,r,!1,a)},getAgentProfile:()=>this.agentProfile},t=this.config.adapterOptions??{};return new X({command:this.config.agent.command,args:this.config.agent.args,env:this.resolveSpawnEnv(),options:{...t,aibotSessionId:e,bindingStore:this.bindingStore}},o)}createOpenHumanAdapter(e){const o={sendStreamChunk:(n,i,s,r,a,d)=>{this.finalizeThinking(n,i),this.sendStreamChunkByRuntimeConfig(n,i,s,r,a,d)},sendFinalStreamChunkReliable:async(n,i,s,r)=>{if(this.finalizeThinking(n,i),this.resolveEventRuntimeConfig(n).responseDelivery==="single_message"&&n){this.bufferStreamChunk(n,i,"",!0);return}try{await this.aibotHandle.sendStreamChunkRequest({event_id:n,session_id:i,delta_content:"",chunk_seq:s,is_finish:!0,...r?{client_msg_id:r}:{}})}catch(d){u.warn("bridge",`[openhuman] sendFinalStreamChunkReliable ACK failed event=${n}: ${d}`)}},sendEventResult:(n,i,s)=>{this.sendEventResultWithCleanup(n,i,s),u.info("bridge",`[openhuman] sendEventResult event=${n} status=${i}`)},sendEventAck:(n,i)=>this.aibotHandle.sendEventAck({event_id:n,session_id:i,received_at:Date.now()}),sendToolUse:(n,i,s,r)=>{this.sendToolExecutionCard(n,i,E(s,r))},sendToolResult:(n,i,s,r)=>{this.sendToolExecutionCard(n,i,$(s,r))},sendThinking:(n,i,s)=>{this.sendThinkingByRuntimeConfig(n,i,s)},sendRunError:(n,i,s)=>{this.sendStreamChunkByRuntimeConfig(n,i,`
6
6
 
@@ -1 +1 @@
1
- function a(e){const t=new Set([`http://127.0.0.1:${e.serverPort}`,`http://localhost:${e.serverPort}`,...e.allowedOrigins]),o=new Set([`127.0.0.1:${e.serverPort}`,`localhost:${e.serverPort}`,...e.allowedHosts]);return{validateRequest(s){const r=i(s,t);if(!r.ok)return r;const n=l(s,o);return n.ok?{ok:!0}:n}}}function i(e,t){const o=e.headers.origin;return o?t.has(o)?{ok:!0}:{ok:!1,statusCode:403,message:`Origin not allowed: ${o}`}:{ok:!0}}function l(e,t){const o=e.headers.host;return o?t.has(o)?{ok:!0}:{ok:!1,statusCode:403,message:`Host not allowed: ${o}`}:{ok:!1,statusCode:403,message:"Missing Host header"}}export{a as createSecurityPolicy};
1
+ function a(t){const o=new Set([`http://127.0.0.1:${t.serverPort}`,`http://localhost:${t.serverPort}`,...t.allowedOrigins]),e=new Set([`127.0.0.1:${t.serverPort}`,`localhost:${t.serverPort}`,...t.allowedHosts]);return{validateRequest(s){const r=i(s,o);if(!r.ok)return r;const n=l(s,e);return n.ok?{ok:!0}:n}}}function i(t,o){const e=t.headers.origin;return e?o.has(e)?{ok:!0}:{ok:!1,statusCode:403,message:`Origin not allowed: ${e}`}:{ok:!0}}function l(t,o){const e=t.headers.host;return e?o.has(e)?{ok:!0}:{ok:!1,statusCode:403,message:`Host not allowed: ${e}`}:{ok:!1,statusCode:403,message:"Missing Host header"}}export{a as createSecurityPolicy};
@@ -1,4 +1,4 @@
1
- import{existsSync as E,readFileSync as O}from"node:fs";import{mkdir as P,readdir as v,readFile as g,rm as h,stat as U,writeFile as w}from"node:fs/promises";import l from"node:os";import u from"node:path";import{isProcessRunning as C,runCommand as d,spawnDetached as F,isWindowsElevated as H,killProcessesByCommandLine as _}from"./process-control.js";import{getServicePrefix as A,parseConfigDirFromPlistXML as G,parseConfigDirFromSystemdUnit as V,resolveBareDaemonMarkerPath as y,resolveLinuxUserUnitPath as X,resolveMacOSLaunchAgentPath as Q}from"./service-paths.js";function p(t){return String(t??"").replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;")}function T(t){return`'${String(t??"").replace(/'/g,"'\\''")}'`}function j(t){return[t.nodePath,t.cliPath,...t.configDir?["--config-dir",t.configDir]:[]]}function z(t){const e=j(t);return`<?xml version="1.0" encoding="UTF-8"?>
1
+ import{existsSync as N,readFileSync as O}from"node:fs";import{mkdir as P,readdir as C,readFile as g,rm as h,stat as H,writeFile as w}from"node:fs/promises";import l from"node:os";import u from"node:path";import{isProcessRunning as v,runCommand as d,spawnDetached as R,isWindowsElevated as G,killProcessesByCommandLine as _}from"./process-control.js";import{getServicePrefix as A,parseConfigDirFromPlistXML as V,parseConfigDirFromSystemdUnit as X,resolveBareDaemonMarkerPath as y,resolveLinuxUserUnitPath as Q,resolveMacOSLaunchAgentPath as z}from"./service-paths.js";function p(t){return String(t??"").replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;")}function T(t){return`'${String(t??"").replace(/'/g,"'\\''")}'`}function j(t){return[t.nodePath,t.cliPath,...t.configDir?["--config-dir",t.configDir]:[]]}function J(t){const e=j(t);return`<?xml version="1.0" encoding="UTF-8"?>
2
2
  <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3
3
  <plist version="1.0">
4
4
  <dict>
@@ -26,7 +26,7 @@ ${t.environmentPath?` <key>EnvironmentVariables</key>
26
26
  <string>${p(t.stderrPath)}</string>
27
27
  </dict>
28
28
  </plist>
29
- `}function J(t){const e=j(t).map(r=>T(r)).join(" ");return`[Unit]
29
+ `}function K(t){const e=j(t).map(r=>T(r)).join(" ");return`[Unit]
30
30
  Description=grix-connector daemon (${t.serviceID})
31
31
  After=network.target
32
32
 
@@ -42,7 +42,7 @@ StandardError=append:${t.stderrPath}
42
42
  [Install]
43
43
  WantedBy=default.target
44
44
  `}function k(t,e){const r=t.replace(/[^a-zA-Z0-9._-]/g,"_");return u.join(e,".grix",`${r}-wrapper.vbs`)}function I(t,e){const r=t.replace(/[^a-zA-Z0-9._-]/g,"_"),n=u.join(e,"AppData","Roaming","Microsoft","Windows","Start Menu","Programs","Startup");return u.join(n,`${r}.lnk`)}function S(t){return String(t??"").replace(/"/g,'""')}function Z(t,e){const r=u.join(u.dirname(t),u.basename(e).replace(/\.lnk$/,"-create.vbs")),n=["Option Explicit","Dim shell, shortcut",'Set shell = CreateObject("WScript.Shell")',`Set shortcut = shell.CreateShortcut("${S(e)}")`,'shortcut.TargetPath = "wscript.exe"',`shortcut.Arguments = "//B //NoLogo \\"${S(t)}\\""`,"shortcut.WindowStyle = 7","shortcut.Save",""].join(`\r
45
- `);return{path:r,content:n}}async function q(t){const{wrapperPath:e,shortcutPath:r,runCommand:n}=t,a=Z(e,r);for(let i=1;i<=3;i+=1){try{await w(a.path,a.content,"utf8"),await n("wscript.exe",[a.path,"//B","//NoLogo"])}catch{}finally{await h(a.path,{force:!0}).catch(()=>{})}if(E(r))return!0}return console.warn(`Warning: failed to create Windows logon-autostart shortcut at ${r}. The daemon will NOT start automatically after the next reboot/logon. Check Controlled Folder Access / antivirus blocking writes to the Startup folder, or create the shortcut manually.`),!1}async function K(t){try{const e=await t("whoami",["/user","/fo","csv","/nh"],{allowFailure:!0}),r=String(e?.stdout??"").match(/"(S-1-[0-9-]+)"/);if(r)return r[1]}catch{}return null}function Y(t){const e=`//B //NoLogo "${t.wrapperPath}"`;return`<?xml version="1.0" encoding="UTF-16"?>
45
+ `);return{path:r,content:n}}async function q(t){const{wrapperPath:e,shortcutPath:r,runCommand:n}=t,a=Z(e,r);for(let i=1;i<=3;i+=1){try{await w(a.path,a.content,"utf8"),await n("wscript.exe",[a.path,"//B","//NoLogo"])}catch{}finally{await h(a.path,{force:!0}).catch(()=>{})}if(N(r))return!0}return console.warn(`Warning: failed to create Windows logon-autostart shortcut at ${r}. The daemon will NOT start automatically after the next reboot/logon. Check Controlled Folder Access / antivirus blocking writes to the Startup folder, or create the shortcut manually.`),!1}async function Y(t){try{const e=await t("whoami",["/user","/fo","csv","/nh"],{allowFailure:!0}),r=String(e?.stdout??"").match(/"(S-1-[0-9-]+)"/);if(r)return r[1]}catch{}return null}function tt(t){const e=`//B //NoLogo "${t.wrapperPath}"`;return`<?xml version="1.0" encoding="UTF-16"?>
46
46
  <Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
47
47
  <RegistrationInfo>
48
48
  <Description>grix-connector daemon autostart</Description>
@@ -85,12 +85,12 @@ WantedBy=default.target
85
85
  </Exec>
86
86
  </Actions>
87
87
  </Task>
88
- `}async function tt(t){const{serviceID:e,wrapperPath:r,runCommand:n}=t,a=await K(n)??l.userInfo().username,i=u.join(u.dirname(r),`${e}-task.xml`);try{const o=Y({userId:a,wrapperPath:r});return await w(i,"\uFEFF"+o,"utf16le"),await n("schtasks",["/Create","/TN",e,"/XML",i,"/F"]),!0}catch{return!1}finally{await h(i,{force:!0}).catch(()=>{})}}function et(t){const e=[`"${S(t.nodePath)}"`,`"${S(t.cliPath)}"`,...t.configDir?["--config-dir",`"${S(t.configDir)}"`]:[]].join(" ");return["Option Explicit","Dim shell, command, delayMs, rapidCount",`command = "${S(e)}"`,'Set shell = CreateObject("WScript.Shell")',"delayMs = 5000","rapidCount = 0","Do"," shell.Run command, 0, True"," rapidCount = rapidCount + 1"," If rapidCount >= 10 Then"," WScript.Sleep 300000"," rapidCount = 0"," Else"," WScript.Sleep delayMs"," End If","Loop",""].join(`\r
89
- `)}function b(t){return`gui/${t??0}`}function N(t){const e=String(t?.stdout??"").trim();return String(t?.stderr??"").trim()||e||`exit=${Number(t?.exitCode??-1)}`}function R(t,e){if(Number(t?.exitCode??0)!==0)throw new Error(`${e}: ${N(t)}`)}function rt(){return{platform:"darwin",kind:"launchd",async install({serviceID:t,nodePath:e,cliPath:r,configDir:n,stdoutPath:a,stderrPath:i,environmentPath:s="",homeDir:o=l.homedir()}){const c=Q(t,o);return await P(u.dirname(c),{recursive:!0}),await w(c,z({serviceID:t,nodePath:e,cliPath:r,configDir:n,stdoutPath:a,stderrPath:i,environmentPath:s}),{encoding:"utf8",mode:384}),{definitionPath:c}},async start({serviceID:t,definitionPath:e,runCommand:r=d,uid:n=process.getuid?.()??0}){const a=b(n);let i=await r("launchctl",["bootstrap",a,e],{allowFailure:!0});Number(i?.exitCode??0)!==0&&(await r("launchctl",["bootout",`${a}/${t}`],{allowFailure:!0}),i=await r("launchctl",["bootstrap",a,e],{allowFailure:!0}),R(i,`launchctl bootstrap ${a}`));const s=await r("launchctl",["kickstart","-k",`${a}/${t}`],{allowFailure:!0});if(Number(s?.exitCode??0)!==0){const o=Number(i?.exitCode??0)===0?"":`, bootstrap=${N(i)}`;throw new Error(`launchctl start failed for ${a}/${t}: ${N(s)}${o}`)}},async stop({serviceID:t,runCommand:e=d,uid:r=process.getuid?.()??0}){const n=b(r);await e("launchctl",["bootout",`${n}/${t}`],{allowFailure:!0})},async restart({serviceID:t,definitionPath:e,runCommand:r=d,uid:n=process.getuid?.()??0}){const a=b(n);await r("launchctl",["bootout",`${a}/${t}`],{allowFailure:!0});for(let o=0;o<20&&(await r("launchctl",["print",`${a}/${t}`],{allowFailure:!0})).exitCode===0;o+=1)await new Promise(m=>setTimeout(m,250));try{await U(e)}catch{throw new Error(`launchd plist missing: ${e}`)}const i=await r("launchctl",["bootstrap",a,e],{allowFailure:!0});R(i,`launchctl bootstrap ${a}`);const s=await r("launchctl",["kickstart","-k",`${a}/${t}`],{allowFailure:!0});R(s,`launchctl kickstart ${a}/${t}`)},async uninstall({serviceID:t,definitionPath:e,runCommand:r=d,uid:n=process.getuid?.()??0}){const a=b(n);await r("launchctl",["bootout",`${a}/${t}`],{allowFailure:!0}),await h(e,{force:!0})},async discoverServices({homeDir:t=l.homedir()}={}){const e=u.join(t,"Library","LaunchAgents"),r=await v(e).catch(()=>[]),n=A("darwin"),a=[];for(const i of r){if(!i.startsWith(n)||!i.endsWith(".plist"))continue;const s=i.slice(0,-6),o=u.join(e,i);let c=null;try{const m=await g(o,"utf8");c=G(m)}catch{}a.push({serviceID:s,definitionPath:o,configDir:c})}return a},async isServiceLoaded({serviceID:t,runCommand:e=d,uid:r=process.getuid?.()??0}){const n=b(r),a=await e("launchctl",["print",`${n}/${t}`],{allowFailure:!0});return Number(a?.exitCode??1)===0}}}function nt(){return{platform:"win32",kind:"task-scheduler",async install({serviceID:t,nodePath:e,cliPath:r,configDir:n,runCommand:a=d,homeDir:i=l.homedir()}){const s=k(t,i);await P(u.dirname(s),{recursive:!0}),await w(s,et({nodePath:e,cliPath:r,configDir:n}),"utf8");let o=!1;H()&&(o=await tt({serviceID:t,wrapperPath:s,runCommand:a}));const c=I(t,i);return o?await h(c,{force:!0}).catch(()=>{}):await q({wrapperPath:s,shortcutPath:c,runCommand:a}),{definitionPath:o?`task:${t}`:`startup:${t}`}},async start({serviceID:t,definitionPath:e,runCommand:r=d,homeDir:n=l.homedir()}){const a=k(t,n);if(e.startsWith("task:"))try{await r("schtasks",["/Run","/TN",t]);return}catch{}F("wscript.exe",["//B","//NoLogo",a])},async stop({serviceID:t,runCommand:e=d,homeDir:r=l.homedir()}){await e("schtasks",["/End","/TN",t],{allowFailure:!0});const n=`${t}-wrapper.vbs`;await _(n,{platform:"win32"})},async restart({serviceID:t,definitionPath:e,runCommand:r=d,homeDir:n=l.homedir()}){await r("schtasks",["/End","/TN",t],{allowFailure:!0});const a=`${t}-wrapper.vbs`;await _(a,{platform:"win32"});const i=k(t,n);if(e?.startsWith("task:"))try{await r("schtasks",["/Run","/TN",t]);return}catch{}F("wscript.exe",["//B","//NoLogo",i])},async uninstall({serviceID:t,runCommand:e=d,homeDir:r=l.homedir()}){await e("schtasks",["/Delete","/TN",t,"/F"],{allowFailure:!0});const n=k(t,r);await h(n,{force:!0});const a=I(t,r);await h(a,{force:!0})},async discoverServices({homeDir:t=l.homedir(),runCommand:e=d}={}){const r=A("win32"),n=await e("schtasks",["/Query","/FO","CSV","/NH"],{allowFailure:!0}),a=[];if(Number(n?.exitCode??-1)===0){const o=String(n.stdout??"").split(/\r?\n/);for(const c of o){const m=c.match(/^"([^"]+)"/);if(!m)continue;const f=m[1];if(!f.startsWith(r))continue;let W=null;try{const x=k(f,t),D=(await g(x,"utf8")).match(/--config-dir\s+""([^""]+)""/);D&&(W=D[1])}catch{}a.push({serviceID:f,definitionPath:`task:${f}`,configDir:W})}}const i=u.join(t,"AppData","Roaming","Microsoft","Windows","Start Menu","Programs","Startup"),s=await v(i).catch(()=>[]);for(const o of s){if(!o.startsWith(r)||!o.endsWith(".lnk"))continue;const c=o.slice(0,-4);if(a.some(f=>f.serviceID===c))continue;let m=null;try{const f=k(c,t),x=(await g(f,"utf8")).match(/--config-dir\s+""([^""]+)""/);x&&(m=x[1])}catch{}a.push({serviceID:c,definitionPath:`startup:${c}`,configDir:m})}return a},async isServiceLoaded({serviceID:t,runCommand:e=d}){const r=await e("schtasks",["/Query","/TN",t,"/NH"],{allowFailure:!0});return Number(r?.exitCode??1)===0}}}function at(){return{platform:"linux",kind:"systemd-user",async install({serviceID:t,nodePath:e,cliPath:r,configDir:n,stdoutPath:a,stderrPath:i,homeDir:s=l.homedir(),runCommand:o=d}){const c=X(t,s);return await P(u.dirname(c),{recursive:!0}),await w(c,J({serviceID:t,nodePath:e,cliPath:r,configDir:n,stdoutPath:a,stderrPath:i}),{encoding:"utf8",mode:384}),await o("systemctl",["--user","daemon-reload"]),await o("systemctl",["--user","enable",`${t}.service`]),{definitionPath:c}},async start({serviceID:t,runCommand:e=d}){await e("systemctl",["--user","start",`${t}.service`])},async stop({serviceID:t,runCommand:e=d}){await e("systemctl",["--user","stop",`${t}.service`],{allowFailure:!0})},async restart({serviceID:t,runCommand:e=d}){await e("systemctl",["--user","restart",`${t}.service`])},async uninstall({serviceID:t,definitionPath:e,runCommand:r=d}){await r("systemctl",["--user","stop",`${t}.service`],{allowFailure:!0}),await r("systemctl",["--user","disable",`${t}.service`],{allowFailure:!0}),await h(e,{force:!0}),await r("systemctl",["--user","daemon-reload"])},async discoverServices({homeDir:t=l.homedir()}={}){const e=u.join(t,".config","systemd","user"),r=await v(e).catch(()=>[]),n=A("linux"),a=[];for(const i of r){if(!i.startsWith(n)||!i.endsWith(".service"))continue;const s=i.slice(0,-8),o=u.join(e,i);let c=null;try{const m=await g(o,"utf8");c=V(m)}catch{}a.push({serviceID:s,definitionPath:o,configDir:c})}return a},async isServiceLoaded({serviceID:t,runCommand:e=d}){const r=await e("systemctl",["--user","is-active",`${t}.service`],{allowFailure:!0});return String(r?.stdout??"").trim()==="active"}}}function $(t){try{const e=O(t,"utf8"),r=JSON.parse(e);if(r&&typeof r.service_id=="string"&&typeof r.node_path=="string"&&typeof r.cli_path=="string"&&typeof r.root_dir=="string")return r}catch{}return null}function it(t){return u.join(t,"daemon.lock.json")}function L(t){const e=it(t);try{const r=O(e,"utf8"),n=JSON.parse(r);if(typeof n?.pid=="number")return n.pid}catch{}return 0}function B(t){return u.join(t,".profile")}function ot(t){return t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function st(t){const e=[T(t.nodePath),T(t.cliPath)];t.configDir&&e.push("--config-dir",T(t.configDir));const r=e.join(" ");return`${r} status >/dev/null 2>&1 || (nohup ${r} start >/dev/null 2>&1 &)`}function ct(t,e){return`# >>> grix-connector autostart [${t}] >>>
88
+ `}async function et(t){const{serviceID:e,wrapperPath:r,runCommand:n}=t,a=await Y(n)??l.userInfo().username,i=u.join(u.dirname(r),`${e}-task.xml`);try{const o=tt({userId:a,wrapperPath:r});return await w(i,"\uFEFF"+o,"utf16le"),await n("schtasks",["/Create","/TN",e,"/XML",i,"/F"]),!0}catch{return!1}finally{await h(i,{force:!0}).catch(()=>{})}}function rt(t){const e=[`"${S(t.nodePath)}"`,`"${S(t.cliPath)}"`,...t.configDir?["--config-dir",`"${S(t.configDir)}"`]:[]].join(" ");return["Option Explicit","Dim shell, command, delayMs, rapidCount",`command = "${S(e)}"`,'Set shell = CreateObject("WScript.Shell")',"delayMs = 5000","rapidCount = 0","Do"," shell.Run command, 0, True"," rapidCount = rapidCount + 1"," If rapidCount >= 10 Then"," WScript.Sleep 300000"," rapidCount = 0"," Else"," WScript.Sleep delayMs"," End If","Loop",""].join(`\r
89
+ `)}function b(t){return`gui/${t??0}`}function L(t){const e=String(t?.stdout??"").trim();return String(t?.stderr??"").trim()||e||`exit=${Number(t?.exitCode??-1)}`}function F(t,e){if(Number(t?.exitCode??0)!==0)throw new Error(`${e}: ${L(t)}`)}function B(t){return Number(t?.exitCode??0)===0?!1:L(t).includes("Could not find service")}function nt(){return{platform:"darwin",kind:"launchd",async install({serviceID:t,nodePath:e,cliPath:r,configDir:n,stdoutPath:a,stderrPath:i,environmentPath:s="",homeDir:o=l.homedir()}){const c=z(t,o);return await P(u.dirname(c),{recursive:!0}),await w(c,J({serviceID:t,nodePath:e,cliPath:r,configDir:n,stdoutPath:a,stderrPath:i,environmentPath:s}),{encoding:"utf8",mode:384}),{definitionPath:c}},async start({serviceID:t,definitionPath:e,runCommand:r=d,uid:n=process.getuid?.()??0}){const a=b(n);let i=await r("launchctl",["bootstrap",a,e],{allowFailure:!0});Number(i?.exitCode??0)!==0&&(await r("launchctl",["bootout",`${a}/${t}`],{allowFailure:!0}),i=await r("launchctl",["bootstrap",a,e],{allowFailure:!0}),F(i,`launchctl bootstrap ${a}`));const s=await r("launchctl",["kickstart","-k",`${a}/${t}`],{allowFailure:!0});if(Number(s?.exitCode??0)!==0&&!B(s)){const o=Number(i?.exitCode??0)===0?"":`, bootstrap=${L(i)}`;throw new Error(`launchctl start failed for ${a}/${t}: ${L(s)}${o}`)}},async stop({serviceID:t,runCommand:e=d,uid:r=process.getuid?.()??0}){const n=b(r);await e("launchctl",["bootout",`${n}/${t}`],{allowFailure:!0})},async restart({serviceID:t,definitionPath:e,runCommand:r=d,uid:n=process.getuid?.()??0}){const a=b(n);await r("launchctl",["bootout",`${a}/${t}`],{allowFailure:!0});for(let o=0;o<20&&(await r("launchctl",["print",`${a}/${t}`],{allowFailure:!0})).exitCode===0;o+=1)await new Promise(m=>setTimeout(m,250));try{await H(e)}catch{throw new Error(`launchd plist missing: ${e}`)}const i=await r("launchctl",["bootstrap",a,e],{allowFailure:!0});F(i,`launchctl bootstrap ${a}`);const s=await r("launchctl",["kickstart","-k",`${a}/${t}`],{allowFailure:!0});B(s)||F(s,`launchctl kickstart ${a}/${t}`)},async uninstall({serviceID:t,definitionPath:e,runCommand:r=d,uid:n=process.getuid?.()??0}){const a=b(n);await r("launchctl",["bootout",`${a}/${t}`],{allowFailure:!0}),await h(e,{force:!0})},async discoverServices({homeDir:t=l.homedir()}={}){const e=u.join(t,"Library","LaunchAgents"),r=await C(e).catch(()=>[]),n=A("darwin"),a=[];for(const i of r){if(!i.startsWith(n)||!i.endsWith(".plist"))continue;const s=i.slice(0,-6),o=u.join(e,i);let c=null;try{const m=await g(o,"utf8");c=V(m)}catch{}a.push({serviceID:s,definitionPath:o,configDir:c})}return a},async isServiceLoaded({serviceID:t,runCommand:e=d,uid:r=process.getuid?.()??0}){const n=b(r),a=await e("launchctl",["print",`${n}/${t}`],{allowFailure:!0});return Number(a?.exitCode??1)===0}}}function at(){return{platform:"win32",kind:"task-scheduler",async install({serviceID:t,nodePath:e,cliPath:r,configDir:n,runCommand:a=d,homeDir:i=l.homedir()}){const s=k(t,i);await P(u.dirname(s),{recursive:!0}),await w(s,rt({nodePath:e,cliPath:r,configDir:n}),"utf8");let o=!1;G()&&(o=await et({serviceID:t,wrapperPath:s,runCommand:a}));const c=I(t,i);return o?await h(c,{force:!0}).catch(()=>{}):await q({wrapperPath:s,shortcutPath:c,runCommand:a}),{definitionPath:o?`task:${t}`:`startup:${t}`}},async start({serviceID:t,definitionPath:e,runCommand:r=d,homeDir:n=l.homedir()}){const a=k(t,n);if(e.startsWith("task:"))try{await r("schtasks",["/Run","/TN",t]);return}catch{}R("wscript.exe",["//B","//NoLogo",a])},async stop({serviceID:t,runCommand:e=d,homeDir:r=l.homedir()}){await e("schtasks",["/End","/TN",t],{allowFailure:!0});const n=`${t}-wrapper.vbs`;await _(n,{platform:"win32"})},async restart({serviceID:t,definitionPath:e,runCommand:r=d,homeDir:n=l.homedir()}){await r("schtasks",["/End","/TN",t],{allowFailure:!0});const a=`${t}-wrapper.vbs`;await _(a,{platform:"win32"});const i=k(t,n);if(e?.startsWith("task:"))try{await r("schtasks",["/Run","/TN",t]);return}catch{}R("wscript.exe",["//B","//NoLogo",i])},async uninstall({serviceID:t,runCommand:e=d,homeDir:r=l.homedir()}){await e("schtasks",["/Delete","/TN",t,"/F"],{allowFailure:!0});const n=k(t,r);await h(n,{force:!0});const a=I(t,r);await h(a,{force:!0})},async discoverServices({homeDir:t=l.homedir(),runCommand:e=d}={}){const r=A("win32"),n=await e("schtasks",["/Query","/FO","CSV","/NH"],{allowFailure:!0}),a=[];if(Number(n?.exitCode??-1)===0){const o=String(n.stdout??"").split(/\r?\n/);for(const c of o){const m=c.match(/^"([^"]+)"/);if(!m)continue;const f=m[1];if(!f.startsWith(r))continue;let E=null;try{const x=k(f,t),D=(await g(x,"utf8")).match(/--config-dir\s+""([^""]+)""/);D&&(E=D[1])}catch{}a.push({serviceID:f,definitionPath:`task:${f}`,configDir:E})}}const i=u.join(t,"AppData","Roaming","Microsoft","Windows","Start Menu","Programs","Startup"),s=await C(i).catch(()=>[]);for(const o of s){if(!o.startsWith(r)||!o.endsWith(".lnk"))continue;const c=o.slice(0,-4);if(a.some(f=>f.serviceID===c))continue;let m=null;try{const f=k(c,t),x=(await g(f,"utf8")).match(/--config-dir\s+""([^""]+)""/);x&&(m=x[1])}catch{}a.push({serviceID:c,definitionPath:`startup:${c}`,configDir:m})}return a},async isServiceLoaded({serviceID:t,runCommand:e=d}){const r=await e("schtasks",["/Query","/TN",t,"/NH"],{allowFailure:!0});return Number(r?.exitCode??1)===0}}}function it(){return{platform:"linux",kind:"systemd-user",async install({serviceID:t,nodePath:e,cliPath:r,configDir:n,stdoutPath:a,stderrPath:i,homeDir:s=l.homedir(),runCommand:o=d}){const c=Q(t,s);return await P(u.dirname(c),{recursive:!0}),await w(c,K({serviceID:t,nodePath:e,cliPath:r,configDir:n,stdoutPath:a,stderrPath:i}),{encoding:"utf8",mode:384}),await o("systemctl",["--user","daemon-reload"]),await o("systemctl",["--user","enable",`${t}.service`]),{definitionPath:c}},async start({serviceID:t,runCommand:e=d}){await e("systemctl",["--user","start",`${t}.service`])},async stop({serviceID:t,runCommand:e=d}){await e("systemctl",["--user","stop",`${t}.service`],{allowFailure:!0})},async restart({serviceID:t,runCommand:e=d}){await e("systemctl",["--user","restart",`${t}.service`])},async uninstall({serviceID:t,definitionPath:e,runCommand:r=d}){await r("systemctl",["--user","stop",`${t}.service`],{allowFailure:!0}),await r("systemctl",["--user","disable",`${t}.service`],{allowFailure:!0}),await h(e,{force:!0}),await r("systemctl",["--user","daemon-reload"])},async discoverServices({homeDir:t=l.homedir()}={}){const e=u.join(t,".config","systemd","user"),r=await C(e).catch(()=>[]),n=A("linux"),a=[];for(const i of r){if(!i.startsWith(n)||!i.endsWith(".service"))continue;const s=i.slice(0,-8),o=u.join(e,i);let c=null;try{const m=await g(o,"utf8");c=X(m)}catch{}a.push({serviceID:s,definitionPath:o,configDir:c})}return a},async isServiceLoaded({serviceID:t,runCommand:e=d}){const r=await e("systemctl",["--user","is-active",`${t}.service`],{allowFailure:!0});return String(r?.stdout??"").trim()==="active"}}}function $(t){try{const e=O(t,"utf8"),r=JSON.parse(e);if(r&&typeof r.service_id=="string"&&typeof r.node_path=="string"&&typeof r.cli_path=="string"&&typeof r.root_dir=="string")return r}catch{}return null}function ot(t){return u.join(t,"daemon.lock.json")}function W(t){const e=ot(t);try{const r=O(e,"utf8"),n=JSON.parse(r);if(typeof n?.pid=="number")return n.pid}catch{}return 0}function M(t){return u.join(t,".profile")}function st(t){return t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function ct(t){const e=[T(t.nodePath),T(t.cliPath)];t.configDir&&e.push("--config-dir",T(t.configDir));const r=e.join(" ");return`${r} status >/dev/null 2>&1 || (nohup ${r} start >/dev/null 2>&1 &)`}function ut(t,e){return`# >>> grix-connector autostart [${t}] >>>
90
90
  ${e}
91
91
  # <<< grix-connector autostart [${t}] <<<
92
- `}function M(t){const e=ot(t);return new RegExp(`(?:^|\\n)# >>> grix-connector autostart \\[${e}\\] >>>\\n[\\s\\S]*?\\n# <<< grix-connector autostart \\[${e}\\] <<<\\n?`,"m")}async function ut(t,e,r){let n="";try{n=await g(t,"utf8")}catch{}const a=ct(e,r),i=M(e);let s;if(i.test(n))s=n.replace(i,`
92
+ `}function U(t){const e=st(t);return new RegExp(`(?:^|\\n)# >>> grix-connector autostart \\[${e}\\] >>>\\n[\\s\\S]*?\\n# <<< grix-connector autostart \\[${e}\\] <<<\\n?`,"m")}async function lt(t,e,r){let n="";try{n=await g(t,"utf8")}catch{}const a=ut(e,r),i=U(e);let s;if(i.test(n))s=n.replace(i,`
93
93
  ${a}`).replace(/^\n+/,"");else{const o=n.length===0||n.endsWith(`
94
94
  `)?"":`
95
- `;s=`${n}${o}${a}`}s!==n&&(await P(u.dirname(t),{recursive:!0}),await w(t,s,{encoding:"utf8"}))}async function lt(t,e){let r="";try{r=await g(t,"utf8")}catch{return}const n=M(e);if(!n.test(r))return;const a=r.replace(n,"").replace(/^\n+/,"");await w(t,a,{encoding:"utf8"})}function dt(){return{platform:"linux",kind:"bare-daemon",async install({serviceID:t,nodePath:e,cliPath:r,configDir:n,stdoutPath:a,stderrPath:i,homeDir:s=l.homedir()}){const o=y(t,s);await P(u.dirname(o),{recursive:!0});const c=u.resolve(u.dirname(u.dirname(a||i||o))),m={schema_version:1,service_id:t,node_path:e,cli_path:r,config_dir:n??"",root_dir:c,stdout_path:a,stderr_path:i,installed_at:Date.now()};await w(o,`${JSON.stringify(m,null,2)}
96
- `,{encoding:"utf8",mode:384});const f=st({nodePath:e,cliPath:r,configDir:n??""});return await ut(B(s),t,f),{definitionPath:o}},async start({serviceID:t,homeDir:e=l.homedir()}){const r=y(t,e),n=$(r);if(!n)throw new Error(`bare-daemon marker missing: ${r}`);const a=[n.cli_path];n.config_dir&&a.push("--config-dir",n.config_dir),F(n.node_path,a)},async stop({serviceID:t,homeDir:e=l.homedir()}){const r=y(t,e),n=$(r);if(!n)return;const a=L(n.root_dir);if(a>0&&C(a))try{process.kill(a,"SIGTERM")}catch{}},async restart(t){await this.stop(t);const e=Date.now()+5e3,r=y(t.serviceID,t.homeDir??l.homedir()),n=$(r);for(;n&&Date.now()<e;){const a=L(n.root_dir);if(a<=0||!C(a))break;await new Promise(i=>setTimeout(i,100))}await this.start(t)},async uninstall({serviceID:t,homeDir:e=l.homedir()}){const r=y(t,e),n=$(r);if(n){const a=L(n.root_dir);if(a>0&&C(a))try{process.kill(a,"SIGTERM")}catch{}}await h(r,{force:!0}),await lt(B(e),t)},async discoverServices({homeDir:t=l.homedir()}={}){const e=u.join(t,".grix","service"),r=await v(e).catch(()=>[]),n=A("linux"),a=[];for(const i of r){if(!i.startsWith(n)||!i.endsWith(".bare.json"))continue;const s=i.slice(0,-10),o=u.join(e,i),c=$(o);a.push({serviceID:s,definitionPath:o,configDir:c?.config_dir||null})}return a},async isServiceLoaded({serviceID:t,homeDir:e=l.homedir()}){const r=y(t,e),n=$(r);if(!n)return!1;const a=L(n.root_dir);return a>0&&C(a)}}}function mt(t=process.getuid?.()??0){if(process.env.GRIX_FORCE_BARE_DAEMON==="1")return!1;const e=process.env.XDG_RUNTIME_DIR;return!!(e&&E(u.join(e,"bus"))||E(`/run/user/${t}/bus`))}function St(t=process.platform,e={}){return t==="darwin"?rt():t==="win32"?nt():e.systemdUserAvailable??mt()?at():dt()}export{St as getPlatformServiceAdapter,mt as isSystemdUserBusAvailable,lt as removeProfileAutostart,ut as upsertProfileAutostart};
95
+ `;s=`${n}${o}${a}`}s!==n&&(await P(u.dirname(t),{recursive:!0}),await w(t,s,{encoding:"utf8"}))}async function dt(t,e){let r="";try{r=await g(t,"utf8")}catch{return}const n=U(e);if(!n.test(r))return;const a=r.replace(n,"").replace(/^\n+/,"");await w(t,a,{encoding:"utf8"})}function mt(){return{platform:"linux",kind:"bare-daemon",async install({serviceID:t,nodePath:e,cliPath:r,configDir:n,stdoutPath:a,stderrPath:i,homeDir:s=l.homedir()}){const o=y(t,s);await P(u.dirname(o),{recursive:!0});const c=u.resolve(u.dirname(u.dirname(a||i||o))),m={schema_version:1,service_id:t,node_path:e,cli_path:r,config_dir:n??"",root_dir:c,stdout_path:a,stderr_path:i,installed_at:Date.now()};await w(o,`${JSON.stringify(m,null,2)}
96
+ `,{encoding:"utf8",mode:384});const f=ct({nodePath:e,cliPath:r,configDir:n??""});return await lt(M(s),t,f),{definitionPath:o}},async start({serviceID:t,homeDir:e=l.homedir()}){const r=y(t,e),n=$(r);if(!n)throw new Error(`bare-daemon marker missing: ${r}`);const a=[n.cli_path];n.config_dir&&a.push("--config-dir",n.config_dir),R(n.node_path,a)},async stop({serviceID:t,homeDir:e=l.homedir()}){const r=y(t,e),n=$(r);if(!n)return;const a=W(n.root_dir);if(a>0&&v(a))try{process.kill(a,"SIGTERM")}catch{}},async restart(t){await this.stop(t);const e=Date.now()+5e3,r=y(t.serviceID,t.homeDir??l.homedir()),n=$(r);for(;n&&Date.now()<e;){const a=W(n.root_dir);if(a<=0||!v(a))break;await new Promise(i=>setTimeout(i,100))}await this.start(t)},async uninstall({serviceID:t,homeDir:e=l.homedir()}){const r=y(t,e),n=$(r);if(n){const a=W(n.root_dir);if(a>0&&v(a))try{process.kill(a,"SIGTERM")}catch{}}await h(r,{force:!0}),await dt(M(e),t)},async discoverServices({homeDir:t=l.homedir()}={}){const e=u.join(t,".grix","service"),r=await C(e).catch(()=>[]),n=A("linux"),a=[];for(const i of r){if(!i.startsWith(n)||!i.endsWith(".bare.json"))continue;const s=i.slice(0,-10),o=u.join(e,i),c=$(o);a.push({serviceID:s,definitionPath:o,configDir:c?.config_dir||null})}return a},async isServiceLoaded({serviceID:t,homeDir:e=l.homedir()}){const r=y(t,e),n=$(r);if(!n)return!1;const a=W(n.root_dir);return a>0&&v(a)}}}function ft(t=process.getuid?.()??0){if(process.env.GRIX_FORCE_BARE_DAEMON==="1")return!1;const e=process.env.XDG_RUNTIME_DIR;return!!(e&&N(u.join(e,"bus"))||N(`/run/user/${t}/bus`))}function $t(t=process.platform,e={}){return t==="darwin"?nt():t==="win32"?at():e.systemdUserAvailable??ft()?it():mt()}export{$t as getPlatformServiceAdapter,ft as isSystemdUserBusAvailable,dt as removeProfileAutostart,lt as upsertProfileAutostart};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "grix-connector",
3
- "version": "3.5.0",
3
+ "version": "3.5.2",
4
4
  "description": "Connect local AI coding agents (Claude, Codex, Gemini, Qwen, DeepSeek, Cursor, OpenCode, Pi, OpenHuman, Reasonix) to the Grix scheduling platform. Also serves as an OpenClaw plugin for Grix channel transport.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",