grix-connector 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/dist/adapter/acp/acp-adapter.js +10 -8
  2. package/dist/adapter/claude/claude-adapter.js +16 -15
  3. package/dist/adapter/claude/claude-bridge-server.js +1 -1
  4. package/dist/adapter/claude/mcp-http-launcher.js +2 -2
  5. package/dist/adapter/codewhale/codewhale-adapter.js +4 -3
  6. package/dist/adapter/codex/codex-bridge.js +6 -6
  7. package/dist/adapter/cursor/cursor-adapter.js +3 -2
  8. package/dist/adapter/opencode/opencode-adapter.js +7 -4
  9. package/dist/adapter/openhuman/openhuman-adapter.js +5 -2
  10. package/dist/adapter/pi/pi-adapter.js +8 -5
  11. package/dist/bridge/adapter-pool.js +1 -1
  12. package/dist/bridge/bridge.js +10 -6
  13. package/dist/bridge/deferred-events.js +1 -1
  14. package/dist/bridge/event-queue.js +1 -1
  15. package/dist/bridge/send-controller.js +4 -1
  16. package/dist/core/admin/admin-server.js +1 -0
  17. package/dist/core/admin/admin-token.js +1 -0
  18. package/dist/core/admin/index.js +1 -0
  19. package/dist/core/aibot/client.js +2 -1
  20. package/dist/core/aibot/connection-handle.js +1 -1
  21. package/dist/core/file-ops/handler.js +1 -1
  22. package/dist/core/mcp/event-tool-executor.js +1 -1
  23. package/dist/core/mcp/event-tool-port.js +0 -0
  24. package/dist/core/protocol/message-metadata.js +1 -1
  25. package/dist/core/protocol/message-reference.js +1 -1
  26. package/dist/core/protocol/payload-parser.js +6 -6
  27. package/dist/core/protocol/protocol-text.js +1 -1
  28. package/dist/core/provider-quota/kiro.js +1 -1
  29. package/dist/core/text-segmentation/safe-markdown-stream-segmenter.js +6 -6
  30. package/dist/core/upgrade/upgrade-checker.js +1 -1
  31. package/dist/grix.js +4 -3
  32. package/dist/log.js +2 -2
  33. package/dist/manager.js +2 -1
  34. package/dist/mcp/stream-http/gateway.js +1 -1
  35. package/dist/mcp/stream-http/security.js +1 -1
  36. package/dist/mcp/stream-http/tool-schemas.js +1 -1
  37. package/dist/protocol/acp-client.js +1 -1
  38. package/openclaw-plugin/index.js +15 -10
  39. package/package.json +4 -3
@@ -1,10 +1,13 @@
1
- import{EventEmitter as p}from"node:events";import{resolveCommandPath as f,spawnCommand as v,killProcessGroup as u}from"../../core/runtime/spawn.js";import{PiTransport as g}from"./pi-transport.js";import{log as a}from"../../core/log/index.js";import{scanSkills as S}from"../claude/skill-scanner.js";import{SessionBindingStore as I}from"../../core/persistence/session-binding-store.js";import{splitTextForAibotProtocol as x}from"../../core/protocol/index.js";class T extends p{adapterSessionId;constructor(t){super(),this.adapterSessionId=t}emitDone(t){this.emit("done",t)}emitError(t){if(this.listenerCount("error")===0){a.warn("pi-adapter",`Prompt handle error (no listeners): ${t.message}`);return}this.emit("error",t)}async cancel(){}}const b=12e4,k=500,E=2e3;class m extends p{type="pi";config;callbacks;process=null;transport=new g;alive=!1;stopped=!1;piSessionPath=null;activeEventId=null;activeSessionId=null;isStreaming=!1;streamSeq=0;clientMsgSeq=0;activeClientMsgId=null;thinkingSeq=0;textBuffer="";emittedTextByIndex=new Map;textFlushTimer=null;idleTimer=null;composingSessionId=null;composingInterval=null;doneGuardTimer=null;bindingStore=null;aibotSessionId="";sessionReadyPromise=null;constructor(t,e){super(),this.config=t,this.callbacks=e;const s=t.options??{};this.aibotSessionId=String(s.aibotSessionId??"").trim(),this.bindingStore=s.bindingStore instanceof I?s.bindingStore:null,this.bindingStore&&this.aibotSessionId&&(this.piSessionPath=this.bindingStore.getPiSessionPath(this.aibotSessionId)??null)}async start(){this.spawnPi(),await this.ensureSessionReady(),a.info("pi-adapter",`Ready (pid=${this.process?.pid})`)}async stop(){if(this.stopped=!0,this.alive=!1,this.stopComposing(),this.stopIdleTimer(),this.stopTextFlush(),this.cancelDoneGuard(),this.transport.close(),this.process){const t=this.process;try{u(t,"SIGTERM")}catch{}const e=setTimeout(()=>{try{u(t,"SIGKILL")}catch{}},5e3);t.on("exit",()=>clearTimeout(e)),this.process=null}}isAlive(){return this.alive}async createSession(t){this.sessionReadyPromise=null,await this.createNewSession();const e=this.piSessionPath||`pi-${Date.now()}`;return a.info("pi-adapter",`Session created: ${e} (path=${this.piSessionPath})`),e}async resumeSession(t,e){await this.switchSession()}async destroySession(t){this.piSessionPath=null,this.sessionReadyPromise=null,this.persistPiSessionPath(void 0)}sendPrompt(t){const e=new T(t.adapterSessionId),s=this.buildPromptTextFromRequest(t);return this.ensureSessionReady().then(()=>this.transport.send("prompt",{message:s})).then(i=>{i.success||e.emitDone({status:"failed",error:i.error})}).catch(i=>{e.emitError(i instanceof Error?i:new Error(String(i)))}),e}async cancel(t){try{await this.transport.send("abort")}catch{}}deliverInboundEvent(t){const{event_id:e,session_id:s,content:i}=t,n=this.buildPromptText(t);this.isStreaming?(this.activeEventId&&this.activeEventId!==e&&(a.info("pi-adapter",`steer: cancel ${this.activeEventId} -> ${e}`),this.flushTextBuffer(),this.callbacks.sendEventResult(this.activeEventId,"canceled","steered to new event")),this.activeEventId=e,this.activeSessionId=s,this.resetRunStreamState(),this.startComposing(s,e),this.resetIdleTimer(),this.transport.send("prompt",{message:n,streamingBehavior:"steer"}).catch(r=>{a.error("pi-adapter",`steer failed: ${r}`),this.callbacks.sendEventResult(e,"failed",String(r))})):(a.info("pi-adapter",`prompt: event=${e} session=${s}`),this.activeEventId=e,this.activeSessionId=s,this.isStreaming=!0,this.resetRunStreamState(),this.startComposing(s,e),this.resetIdleTimer(),this.transport.send("prompt",{message:n}).then(r=>{r.success||(a.error("pi-adapter",`prompt rejected: ${r.error}`),this.isStreaming=!1,this.stopComposing(),this.stopIdleTimer(),this.callbacks.sendEventResult(e,"failed",r.error),this.clearActive())}).catch(r=>{a.error("pi-adapter",`prompt error: ${r}`),this.isStreaming=!1,this.stopComposing(),this.stopIdleTimer(),this.callbacks.sendEventResult(e,"failed",String(r)),this.clearActive()}))}deliverStopEvent(t,e){if(this.activeEventId===t){a.info("pi-adapter",`stop: event=${t}, releasing busy immediately`),this.transport.send("abort").catch(()=>{}),this.flushTextBuffer();const s=this.nextStreamSeq(),i=this.activeClientMsgId??void 0;this.callbacks.sendStreamChunk(t,this.activeSessionId??"","",s,!0,i),this.isStreaming=!1,this.stopComposing(),this.stopIdleTimer(),this.callbacks.sendEventResult(t,"canceled","stopped by user"),this.clearActive()}}async handleLocalAction(t){const e=t.action_id;switch(t.action_type){case"set_model":{const{provider:s,modelId:i}=t.params??{};if(s&&i)try{return await this.transport.send("set_model",{provider:s,modelId:i}),this.callbacks.sendLocalActionResult(e,"ok",{outcome:"model_set",provider:s,modelId:i}),{handled:!0,kind:"set_model"}}catch(n){return this.callbacks.sendLocalActionResult(e,"failed",void 0,"set_model_error",n instanceof Error?n.message:String(n)),{handled:!0,kind:"set_model_error"}}return{handled:!1,kind:""}}case"get_context":try{const s=await this.transport.send("get_state");return this.callbacks.sendLocalActionResult(e,"ok",{state:s}),{handled:!0,kind:"get_context"}}catch(s){return this.callbacks.sendLocalActionResult(e,"failed",void 0,"get_context_error",s instanceof Error?s.message:String(s)),{handled:!1,kind:""}}case"pi_extension_ui_response":return this.transport.sendNoWait({type:"extension_ui_response",...t.params??{}}),this.callbacks.sendLocalActionResult(e,"ok"),{handled:!0,kind:"extension_ui_response"};default:return{handled:!1,kind:""}}}setPermissionHandler(t){}async ping(t){try{return await this.transport.send("get_state"),!0}catch{return!1}}getStatus(){return{alive:this.alive,busy:this.isStreaming,sessions:this.piSessionPath?1:0}}getActiveEventIds(){return this.activeEventId?[this.activeEventId]:[]}clearActiveEventForShutdown(){this.stopIdleTimer(),this.activeEventId=null}getMcpConfig(){return null}getSupportedCommands(){return[{name:"model",description:"List or set model",args:"[provider:model_id]"},{name:"interrupt",description:"Interrupt current run"},{name:"status",description:"Show session status"},{name:"skills",description:"List available skills"}]}async execCommand(t,e,s){try{if(!this.alive)return{status:"failed",message:"Pi process not running"};switch(t){case"model":{const i=e.trim();if(i){const c=i.indexOf(":");if(c<1)return{status:"failed",message:"Format: provider:model_id"};const o=i.slice(0,c),d=i.slice(c+1);if(!d)return{status:"failed",message:"Format: provider:model_id"};const l=await this.transport.send("set_model",{provider:o,modelId:d});return l?.success?{status:"ok",message:`Model set to ${o}:${d}`}:{status:"failed",message:`Failed to set model: ${l?.error??"unknown error"}`}}const r=(await this.transport.send("get_available_models"))?.data?.models;return r&&r.length>0?{status:"ok",message:`Available models:
2
- ${r.map(o=>` ${o.provider??"unknown"}:${o.id} (${o.name??o.id})`).join(`
3
- `)}`,data:r}:{status:"ok",message:"No models available",data:[]}}case"interrupt":return this.isStreaming?(await this.transport.send("abort"),this.activeEventId&&this.callbacks.sendEventResult(this.activeEventId,"canceled","interrupted"),this.clearActive(),{status:"ok",message:"Run interrupted"}):{status:"failed",message:"No active run to interrupt"};case"status":{const i=this.getStatus();return{status:"ok",message:`Alive: ${i.alive}, Busy: ${i.busy}, Session: ${this.piSessionPath??"none"}`,data:{alive:i.alive,busy:i.busy,sessions:i.sessions,sessionPath:this.piSessionPath}}}case"skills":{const i=S({mode:"pi"}),n=i.map(r=>`- ${r.name}${r.trigger?` (${r.trigger})`:""} [${r.source}]: ${r.description}`);return{status:"ok",message:n.length>0?n.join(`
4
- `):"No skills found",data:i}}default:return{status:"unsupported",message:`Unknown command: ${t}`}}}catch(i){return{status:"failed",message:i instanceof Error?i.message:String(i)}}}resolveCwd(){if(this.bindingStore&&this.aibotSessionId){const e=this.bindingStore.get(this.aibotSessionId);if(e?.cwd)return e.cwd}const t=(this.config.options??{}).cwd;return typeof t=="string"&&t?t:process.cwd()}spawnPi(){const t=this.config.command||"pi",e=f(t,typeof process.env.PATH=="string"?process.env.PATH:void 0),s=this.config.args??[],n=s.some(o=>o.startsWith("--mode"))?s:["--mode","rpc",...s],r={...process.env,...this.config.env},c=this.resolveCwd();a.info("pi-adapter",`Spawning: ${e} ${n.join(" ")}`);try{this.process=v(e,n,{env:r,cwd:c}).process}catch(o){throw a.error("pi-adapter",`PI spawn threw: ${o}`),this.alive=!1,o}this.process.on("error",o=>{a.error("pi-adapter",`Spawn error: ${o.message}`),this.alive=!1,this.transport.close(),this.activeEventId&&(this.callbacks.sendEventResult(this.activeEventId,"failed",`Spawn error: ${o.message}`),this.clearActive()),this.stopped||this.emit("exit",1)}),this.process.on("exit",o=>{a.info("pi-adapter",`PI process exited (code=${o})`),this.alive=!1,this.transport.close(),this.stopComposing(),this.stopIdleTimer(),this.stopTextFlush(),this.activeEventId&&this.callbacks.sendEventResult(this.activeEventId,"failed",`PI process exited (code=${o})`),this.isStreaming=!1,this.clearActive(),this.stopped||this.emit("exit",o??1)}),this.process.stderr?.on("data",o=>{const d=o.toString().trim();d&&a.info("pi-adapter",`[pi stderr] ${d}`)}),this.transport.on("event",o=>this.handlePiEvent(o)),this.transport.bind(this.process.stdin,this.process.stdout),this.alive=!0}handlePiEvent(t){if(this.stopped)return;switch(this.resetIdleTimer(),t.type){case"message_update":{const s=t.assistantMessageEvent;if(!s)break;const i=s.type;if(i==="text_delta"){const n=s.delta;if(n){const r=typeof s.contentIndex=="number"?s.contentIndex:0;this.rememberEmittedText(r,n),this.appendText(n)}}else if(i==="text_end"){const n=s.content,r=typeof s.contentIndex=="number"?s.contentIndex:0;n&&this.appendMissingText(r,n)}else if(i==="thinking_delta"){const n=s.delta;n&&this.activeEventId&&this.activeSessionId&&(this.thinkingSeq++,this.callbacks.sendThinking(this.activeEventId,this.activeSessionId,n))}else if(i==="done"||i==="error"){if(this.flushTextBuffer(),i==="error"&&this.activeEventId&&this.activeSessionId){const n=s.reason??"stream error";this.callbacks.sendRunError(this.activeEventId,this.activeSessionId,String(n),this.nextStreamSeq(),this.activeClientMsgId??void 0)}this.scheduleDoneGuard()}break}case"tool_execution_start":{if(this.cancelDoneGuard(),this.flushTextBuffer(),!this.activeEventId||!this.activeSessionId)break;const s=t;if(s.toolName){const i=typeof s.args=="object"&&s.args!==null?JSON.stringify(s.args):String(s.args??"");this.callbacks.sendToolUse(this.activeEventId,this.activeSessionId,s.toolName,i)}break}case"tool_execution_end":{if(!this.activeEventId||!this.activeSessionId)break;const s=t,i=_(s.result);i&&this.callbacks.sendToolResult(this.activeEventId,this.activeSessionId,s.toolName,i);break}case"agent_end":{if(a.info("pi-adapter",`agent_end event=${this.activeEventId} sessionId=${this.activeSessionId}`),this.cancelDoneGuard(),this.flushTextBuffer(),this.isStreaming=!1,this.stopComposing(),this.stopIdleTimer(),this.activeEventId){const s=this.activeEventId,i=this.activeSessionId??"",n=this.activeClientMsgId??void 0;this.clearActive(),this.finalizeEvent(s,i,n)}else this.clearActive();break}case"agent_start":{this.cancelDoneGuard(),this.activeEventId&&(a.info("pi-adapter",`agent_start event=${this.activeEventId}`),this.isStreaming=!0);break}default:break}}appendText(t){if(this.textBuffer+=t,this.textBuffer.length>=E){this.flushTextBuffer();return}this.scheduleTextFlush()}scheduleTextFlush(){this.textFlushTimer||(this.textFlushTimer=setTimeout(()=>{this.textFlushTimer=null,this.flushTextBuffer()},k))}flushTextBuffer(){this.stopTextFlush(),!(!this.textBuffer||!this.activeEventId||!this.activeSessionId)&&(this.sendTextChunks(this.textBuffer),this.textBuffer="")}sendTextChunks(t){if(!(!this.activeEventId||!this.activeSessionId))for(const e of x(t))this.callbacks.sendStreamChunk(this.activeEventId,this.activeSessionId,e,this.nextStreamSeq(),!1,this.activeClientMsgId??void 0)}stopTextFlush(){this.textFlushTimer&&(clearTimeout(this.textFlushTimer),this.textFlushTimer=null)}buildPromptText(t){let e=t.content||"";if(t.context_messages_json)try{const s=JSON.parse(t.context_messages_json);Array.isArray(s)&&s.length>0&&(e=s.map(n=>`[context] ${n.sender_id??"unknown"}: ${n.content}`).join(`
1
+ import{EventEmitter as p}from"node:events";import{stat as v}from"node:fs/promises";import{mkdirSync as g,writeFileSync as S,unlinkSync as I}from"node:fs";import{join as m,resolve as x}from"node:path";import{fileURLToPath as T}from"node:url";import{tmpdir as b}from"node:os";import{resolveCommandPath as E,spawnCommand as k,killProcessGroup as u}from"../../core/runtime/spawn.js";import{InternalApiServer as y}from"../../core/mcp/internal-api-server.js";import{PiTransport as _}from"./pi-transport.js";import{log as a}from"../../core/log/index.js";import{scanSkills as w}from"../claude/skill-scanner.js";import{SessionBindingStore as P}from"../../core/persistence/session-binding-store.js";import{splitTextForAibotProtocol as $}from"../../core/protocol/index.js";class A extends p{adapterSessionId;constructor(t){super(),this.adapterSessionId=t}emitDone(t){this.emit("done",t)}emitError(t){if(this.listenerCount("error")===0){a.warn("pi-adapter",`Prompt handle error (no listeners): ${t.message}`);return}this.emit("error",t)}async cancel(){}}const C=12e4,R=500,M=2e3;class f extends p{type="pi";config;callbacks;process=null;transport=new _;alive=!1;stopped=!1;internalApi=null;mcpConfigPath=null;piSessionPath=null;activeEventId=null;activeSessionId=null;isStreaming=!1;streamSeq=0;clientMsgSeq=0;activeClientMsgId=null;thinkingSeq=0;textBuffer="";emittedTextByIndex=new Map;textFlushTimer=null;idleTimer=null;composingSessionId=null;composingInterval=null;doneGuardTimer=null;bindingStore=null;aibotSessionId="";sessionReadyPromise=null;constructor(t,e){super(),this.config=t,this.callbacks=e;const s=t.options??{};this.aibotSessionId=String(s.aibotSessionId??"").trim(),this.bindingStore=s.bindingStore instanceof P?s.bindingStore:null,this.bindingStore&&this.aibotSessionId&&(this.piSessionPath=this.bindingStore.getPiSessionPath(this.aibotSessionId)??null)}async start(){await this.startInternalApi(),await this.spawnPi(),await this.ensureSessionReady(),a.info("pi-adapter",`Ready (pid=${this.process?.pid})`)}async stop(){if(this.stopped=!0,this.alive=!1,this.stopComposing(),this.stopIdleTimer(),this.stopTextFlush(),this.cancelDoneGuard(),this.transport.close(),this.internalApi&&(await this.internalApi.stop(),this.internalApi=null),this.mcpConfigPath){try{I(this.mcpConfigPath)}catch{}this.mcpConfigPath=null}if(this.process){const t=this.process;try{u(t,"SIGTERM")}catch{}const e=setTimeout(()=>{try{u(t,"SIGKILL")}catch{}},5e3);t.on("exit",()=>clearTimeout(e)),this.process=null}}isAlive(){return this.alive}async createSession(t){this.sessionReadyPromise=null,await this.createNewSession();const e=this.piSessionPath||`pi-${Date.now()}`;return a.info("pi-adapter",`Session created: ${e} (path=${this.piSessionPath})`),e}async resumeSession(t,e){await this.switchSession()}async destroySession(t){this.piSessionPath=null,this.sessionReadyPromise=null,this.persistPiSessionPath(void 0)}sendPrompt(t){const e=new A(t.adapterSessionId),s=this.buildPromptTextFromRequest(t);return this.ensureSessionReady().then(()=>this.transport.send("prompt",{message:s})).then(i=>{i.success||e.emitDone({status:"failed",error:i.error})}).catch(i=>{e.emitError(i instanceof Error?i:new Error(String(i)))}),e}async cancel(t){try{await this.transport.send("abort")}catch{}}deliverInboundEvent(t){const{event_id:e,session_id:s,content:i}=t,n=this.buildPromptText(t);this.isStreaming?(this.activeEventId&&this.activeEventId!==e&&(a.info("pi-adapter",`steer: cancel ${this.activeEventId} -> ${e}`),this.flushTextBuffer(),this.callbacks.sendEventResult(this.activeEventId,"canceled","steered to new event")),this.activeEventId=e,this.activeSessionId=s,this.resetRunStreamState(),this.startComposing(s,e),this.resetIdleTimer(),this.transport.send("prompt",{message:n,streamingBehavior:"steer"}).catch(o=>{a.error("pi-adapter",`steer failed: ${o}`),this.callbacks.sendEventResult(e,"failed",String(o))})):(a.info("pi-adapter",`prompt: event=${e} session=${s}`),this.activeEventId=e,this.activeSessionId=s,this.isStreaming=!0,this.resetRunStreamState(),this.startComposing(s,e),this.resetIdleTimer(),this.transport.send("prompt",{message:n}).then(o=>{o.success||(a.error("pi-adapter",`prompt rejected: ${o.error}`),this.isStreaming=!1,this.stopComposing(),this.stopIdleTimer(),this.callbacks.sendEventResult(e,"failed",o.error),this.clearActive())}).catch(o=>{a.error("pi-adapter",`prompt error: ${o}`),this.isStreaming=!1,this.stopComposing(),this.stopIdleTimer(),this.callbacks.sendEventResult(e,"failed",String(o)),this.clearActive()}))}deliverStopEvent(t,e){if(this.activeEventId===t){a.info("pi-adapter",`stop: event=${t}, releasing busy immediately`),this.transport.send("abort").catch(()=>{}),this.flushTextBuffer();const s=this.nextStreamSeq(),i=this.activeClientMsgId??void 0;this.callbacks.sendStreamChunk(t,this.activeSessionId??"",`
2
+ `,s,!0,i),this.isStreaming=!1,this.stopComposing(),this.stopIdleTimer(),this.callbacks.sendEventResult(t,"canceled","stopped by user"),this.clearActive()}}async handleLocalAction(t){const e=t.action_id;switch(t.action_type){case"set_model":{const{provider:s,modelId:i}=t.params??{};if(s&&i)try{return await this.transport.send("set_model",{provider:s,modelId:i}),this.callbacks.sendLocalActionResult(e,"ok",{outcome:"model_set",provider:s,modelId:i}),{handled:!0,kind:"set_model"}}catch(n){return this.callbacks.sendLocalActionResult(e,"failed",void 0,"set_model_error",n instanceof Error?n.message:String(n)),{handled:!0,kind:"set_model_error"}}return{handled:!1,kind:""}}case"get_context":try{const s=await this.transport.send("get_state");return this.callbacks.sendLocalActionResult(e,"ok",{state:s}),{handled:!0,kind:"get_context"}}catch(s){return this.callbacks.sendLocalActionResult(e,"failed",void 0,"get_context_error",s instanceof Error?s.message:String(s)),{handled:!1,kind:""}}case"pi_extension_ui_response":return this.transport.sendNoWait({type:"extension_ui_response",...t.params??{}}),this.callbacks.sendLocalActionResult(e,"ok"),{handled:!0,kind:"extension_ui_response"};default:return{handled:!1,kind:""}}}setPermissionHandler(t){}async ping(t){try{return await this.transport.send("get_state"),!0}catch{return!1}}getStatus(){return{alive:this.alive,busy:this.isStreaming,sessions:this.piSessionPath?1:0}}getActiveEventIds(){return this.activeEventId?[this.activeEventId]:[]}clearActiveEventForShutdown(){this.stopIdleTimer(),this.activeEventId=null}getMcpConfig(){if(!this.internalApi)return null;const t=x(T(import.meta.url),"../../../mcp/acp-mcp-server.js");return{name:"grix-connector-tools",command:process.execPath,args:[t,"--api-url",this.internalApi.url]}}getSupportedCommands(){return[{name:"model",description:"List or set model",args:"[provider:model_id]"},{name:"interrupt",description:"Interrupt current run"},{name:"status",description:"Show session status"},{name:"skills",description:"List available skills"}]}async execCommand(t,e,s){try{if(!this.alive)return{status:"failed",message:"Pi process not running"};switch(t){case"model":{const i=e.trim();if(i){const c=i.indexOf(":");if(c<1)return{status:"failed",message:"Format: provider:model_id"};const r=i.slice(0,c),d=i.slice(c+1);if(!d)return{status:"failed",message:"Format: provider:model_id"};const l=await this.transport.send("set_model",{provider:r,modelId:d});return l?.success?{status:"ok",message:`Model set to ${r}:${d}`}:{status:"failed",message:`Failed to set model: ${l?.error??"unknown error"}`}}const o=(await this.transport.send("get_available_models"))?.data?.models;return o&&o.length>0?{status:"ok",message:`Available models:
3
+ ${o.map(r=>` ${r.provider??"unknown"}:${r.id} (${r.name??r.id})`).join(`
4
+ `)}`,data:o}:{status:"ok",message:"No models available",data:[]}}case"interrupt":return this.isStreaming?(await this.transport.send("abort"),this.activeEventId&&this.callbacks.sendEventResult(this.activeEventId,"canceled","interrupted"),this.clearActive(),{status:"ok",message:"Run interrupted"}):{status:"failed",message:"No active run to interrupt"};case"status":{const i=this.getStatus();return{status:"ok",message:`Alive: ${i.alive}, Busy: ${i.busy}, Session: ${this.piSessionPath??"none"}`,data:{alive:i.alive,busy:i.busy,sessions:i.sessions,sessionPath:this.piSessionPath}}}case"skills":{const i=w({mode:"pi"}),n=i.map(o=>`- ${o.name}${o.trigger?` (${o.trigger})`:""} [${o.source}]: ${o.description}`);return{status:"ok",message:n.length>0?n.join(`
5
+ `):"No skills found",data:i}}default:return{status:"unsupported",message:`Unknown command: ${t}`}}}catch(i){return{status:"failed",message:i instanceof Error?i.message:String(i)}}}resolveCwd(){if(this.bindingStore&&this.aibotSessionId){const e=this.bindingStore.get(this.aibotSessionId);if(e?.cwd)return e.cwd}const t=(this.config.options??{}).cwd;return typeof t=="string"&&t?t:process.cwd()}async startInternalApi(){try{this.internalApi=new y,this.internalApi.setInvokeHandler(async(s,i)=>this.callbacks.agentInvoke(s,i)),await this.internalApi.start(0),a.info("pi-adapter",`Internal API started at ${this.internalApi.url}`);const t=this.getMcpConfig(),e=m(b(),"grix-pi-mcp");g(e,{recursive:!0}),this.mcpConfigPath=m(e,`mcp-${process.pid}-${Date.now()}.json`),S(this.mcpConfigPath,JSON.stringify({mcpServers:{[t.name]:{command:t.command,args:t.args}}}),"utf8"),a.info("pi-adapter",`MCP config written to ${this.mcpConfigPath}`)}catch(t){a.warn("pi-adapter",`Failed to start MCP tools (non-fatal): ${t instanceof Error?t.message:String(t)}`)}}async spawnPi(){const t=this.config.command||"pi",e=E(t,typeof process.env.PATH=="string"?process.env.PATH:void 0),s=this.config.args??[],n=s.some(r=>r.startsWith("--mode"))?[...s]:["--mode","rpc",...s];this.mcpConfigPath&&!n.some(r=>r==="--mcp-config")&&n.push("--mcp-config",this.mcpConfigPath);const o={...process.env,...this.config.env},c=this.resolveCwd();a.info("pi-adapter",`Spawning: ${e} ${n.join(" ")}`);try{if(!(await v(c)).isDirectory())throw new Error(`Bound path is not a directory: ${c}`)}catch(r){throw String(r?.code??"")==="ENOENT"?new Error(`Bound directory does not exist: ${c}. Please rebind with /grix open <valid-directory>.`):r}try{this.process=k(e,n,{env:o,cwd:c}).process}catch(r){throw a.error("pi-adapter",`PI spawn threw: ${r}`),this.alive=!1,r}this.process.on("error",r=>{a.error("pi-adapter",`Spawn error: ${r.message}`),this.alive=!1,this.transport.close(),this.activeEventId&&(this.callbacks.sendEventResult(this.activeEventId,"failed",`Spawn error: ${r.message}`),this.clearActive()),this.stopped||this.emit("exit",1)}),this.process.on("exit",r=>{a.info("pi-adapter",`PI process exited (code=${r})`),this.alive=!1,this.transport.close(),this.stopComposing(),this.stopIdleTimer(),this.stopTextFlush(),this.activeEventId&&this.callbacks.sendEventResult(this.activeEventId,"failed",`PI process exited (code=${r})`),this.isStreaming=!1,this.clearActive(),this.stopped||this.emit("exit",r??1)}),this.process.stderr?.on("data",r=>{const d=r.toString().trim();d&&a.info("pi-adapter",`[pi stderr] ${d}`)}),this.transport.on("event",r=>this.handlePiEvent(r)),this.transport.bind(this.process.stdin,this.process.stdout),this.alive=!0}handlePiEvent(t){if(this.stopped)return;switch(this.resetIdleTimer(),t.type){case"message_update":{const s=t.assistantMessageEvent;if(!s)break;const i=s.type;if(i==="text_delta"){const n=s.delta;if(n){const o=typeof s.contentIndex=="number"?s.contentIndex:0;this.rememberEmittedText(o,n),this.appendText(n)}}else if(i==="text_end"){const n=s.content,o=typeof s.contentIndex=="number"?s.contentIndex:0;n&&this.appendMissingText(o,n)}else if(i==="thinking_delta"){const n=s.delta;n&&this.activeEventId&&this.activeSessionId&&(this.thinkingSeq++,this.callbacks.sendThinking(this.activeEventId,this.activeSessionId,n))}else if(i==="done"||i==="error"){if(this.flushTextBuffer(),i==="error"&&this.activeEventId&&this.activeSessionId){const n=s.reason??"stream error";this.callbacks.sendRunError(this.activeEventId,this.activeSessionId,String(n),this.nextStreamSeq(),this.activeClientMsgId??void 0)}this.scheduleDoneGuard()}break}case"tool_execution_start":{if(this.cancelDoneGuard(),this.flushTextBuffer(),!this.activeEventId||!this.activeSessionId)break;const s=t;if(s.toolName){const i=typeof s.args=="object"&&s.args!==null?JSON.stringify(s.args):String(s.args??"");this.callbacks.sendToolUse(this.activeEventId,this.activeSessionId,s.toolName,i)}break}case"tool_execution_end":{if(!this.activeEventId||!this.activeSessionId)break;const s=t,i=F(s.result);i&&this.callbacks.sendToolResult(this.activeEventId,this.activeSessionId,s.toolName,i);break}case"agent_end":{if(a.info("pi-adapter",`agent_end event=${this.activeEventId} sessionId=${this.activeSessionId}`),this.cancelDoneGuard(),this.flushTextBuffer(),this.isStreaming=!1,this.stopComposing(),this.stopIdleTimer(),this.activeEventId){const s=this.activeEventId,i=this.activeSessionId??"",n=this.activeClientMsgId??void 0;this.clearActive(),this.finalizeEvent(s,i,n)}else this.clearActive();break}case"agent_start":{this.cancelDoneGuard(),this.activeEventId&&(a.info("pi-adapter",`agent_start event=${this.activeEventId}`),this.isStreaming=!0);break}default:break}}appendText(t){if(this.textBuffer+=t,this.textBuffer.length>=M){this.flushTextBuffer();return}this.scheduleTextFlush()}scheduleTextFlush(){this.textFlushTimer||(this.textFlushTimer=setTimeout(()=>{this.textFlushTimer=null,this.flushTextBuffer()},R))}flushTextBuffer(){this.stopTextFlush(),!(!this.textBuffer||!this.activeEventId||!this.activeSessionId)&&(this.sendTextChunks(this.textBuffer),this.textBuffer="")}sendTextChunks(t){if(!(!this.activeEventId||!this.activeSessionId))for(const e of $(t))this.callbacks.sendStreamChunk(this.activeEventId,this.activeSessionId,e,this.nextStreamSeq(),!1,this.activeClientMsgId??void 0)}stopTextFlush(){this.textFlushTimer&&(clearTimeout(this.textFlushTimer),this.textFlushTimer=null)}buildPromptText(t){let e=t.content||"";if(t.context_messages_json)try{const s=JSON.parse(t.context_messages_json);Array.isArray(s)&&s.length>0&&(e=s.map(n=>`[context] ${n.sender_id??"unknown"}: ${n.content}`).join(`
5
6
  `)+`
6
7
 
7
8
  `+e)}catch{}return e}buildPromptTextFromRequest(t){let e=t.text;return t.contextMessages&&t.contextMessages.length>0&&(e=t.contextMessages.map(i=>`[context] ${i.senderId}: ${i.content}`).join(`
8
9
  `)+`
9
10
 
10
- `+e),e}clearActive(){this.activeEventId=null,this.activeSessionId=null,this.activeClientMsgId=null,this.textBuffer="",this.emittedTextByIndex.clear()}resetRunStreamState(){this.streamSeq=0,this.thinkingSeq=0,this.textBuffer="",this.emittedTextByIndex.clear(),this.activeClientMsgId=`pi_${++this.clientMsgSeq}_${Date.now()}`}nextStreamSeq(){return this.streamSeq++,this.streamSeq}sendFinalStreamChunk(t,e){this.callbacks.sendStreamChunk(t,e,"",this.nextStreamSeq(),!0,this.activeClientMsgId??void 0)}finalizeEvent(t,e,s){const i=this.nextStreamSeq();this.callbacks.sendFinalStreamChunkReliable?this.callbacks.sendFinalStreamChunkReliable(t,e,i,s).then(()=>{this.callbacks.sendEventResult(t,"responded"),a.info("pi-adapter",`event completed (reliable) event=${t}`)}).catch(n=>{a.error("pi-adapter",`finalStreamChunk ACK failed event=${t}: ${n}`),this.callbacks.sendStreamChunk(t,e,"",i,!0,s),this.callbacks.sendEventResult(t,"responded"),a.info("pi-adapter",`event completed (fallback) event=${t}`)}):(this.callbacks.sendStreamChunk(t,e,"",i,!0,s),this.callbacks.sendEventResult(t,"responded"),a.info("pi-adapter",`event completed event=${t}`))}rememberEmittedText(t,e){this.emittedTextByIndex.set(t,(this.emittedTextByIndex.get(t)??"")+e)}appendMissingText(t,e){const s=this.emittedTextByIndex.get(t)??"";if(e!==s){if(e.startsWith(s)){const i=e.slice(s.length);i&&(this.rememberEmittedText(t,i),this.appendText(i));return}if(!s){this.rememberEmittedText(t,e),this.appendText(e);return}a.info("pi-adapter",`text_end content mismatch at index=${t}, keeping streamed deltas`)}}startComposing(t,e){this.stopComposing(),this.composingSessionId=t,this.callbacks.sendSessionActivitySet(t,"composing",!0,{ref_event_id:e,ttl_ms:12e4}),this.composingInterval=setInterval(()=>{this.composingSessionId&&this.callbacks.sendSessionActivitySet(this.composingSessionId,"composing",!0,{ttl_ms:12e4})},3e4)}stopComposing(){this.composingInterval&&(clearInterval(this.composingInterval),this.composingInterval=null),this.composingSessionId&&(this.callbacks.sendSessionActivitySet(this.composingSessionId,"composing",!1),this.composingSessionId=null)}static DONE_GUARD_MS=5e3;scheduleDoneGuard(){this.cancelDoneGuard(),!(!this.activeEventId||!this.activeSessionId)&&(this.doneGuardTimer=setTimeout(()=>{if(this.doneGuardTimer=null,!this.activeEventId||!this.isStreaming)return;a.info("pi-adapter",`done guard triggered \u2014 no agent_end received, ending event=${this.activeEventId}`),this.flushTextBuffer(),this.isStreaming=!1,this.stopComposing(),this.stopIdleTimer();const t=this.activeEventId,e=this.activeSessionId??"",s=this.activeClientMsgId??void 0;this.clearActive(),this.finalizeEvent(t,e,s)},m.DONE_GUARD_MS))}cancelDoneGuard(){this.doneGuardTimer&&(clearTimeout(this.doneGuardTimer),this.doneGuardTimer=null)}resetIdleTimer(){this.stopIdleTimer(),!(this.stopped||!this.isStreaming)&&(this.idleTimer=setTimeout(()=>{a.error("pi-adapter","Idle timeout \u2014 emitting exit for respawn"),this.flushTextBuffer(),this.isStreaming=!1,this.stopComposing(),this.activeEventId&&this.callbacks.sendEventResult(this.activeEventId,"failed","idle timeout"),this.clearActive(),this.emit("exit",-1)},b))}stopIdleTimer(){this.idleTimer&&(clearTimeout(this.idleTimer),this.idleTimer=null)}async ensureSessionReady(){return this.sessionReadyPromise?this.sessionReadyPromise:(this.sessionReadyPromise=this.restoreOrCreateSession().finally(()=>{this.sessionReadyPromise=null}),this.sessionReadyPromise)}async restoreOrCreateSession(){try{await this.switchSession();return}catch(t){this.piSessionPath&&a.error("pi-adapter",`switch_session failed, creating new session: ${t}`)}await this.createNewSession()}async switchSession(){if(!this.piSessionPath)throw new Error("no PI session path");const t=await this.transport.send("switch_session",{sessionPath:this.piSessionPath});if(!t.success)throw this.piSessionPath=null,this.persistPiSessionPath(void 0),new Error(`switch_session failed: ${t.error}`)}async createNewSession(){const t=await this.transport.send("new_session");if(!t.success)throw new Error(`new_session failed: ${t.error}`);let e=null;if(e=t.data?.sessionFile??null,!e)try{const i=await this.transport.send("get_state");i.success&&(e=i.data?.sessionFile??null)}catch{}this.piSessionPath=e,this.persistPiSessionPath(this.piSessionPath??void 0)}persistPiSessionPath(t){!this.bindingStore||!this.aibotSessionId||this.bindingStore.setPiSessionPath(this.aibotSessionId,t)}}function _(h){if(!h||typeof h!="object")return"";const t=h.content;if(!Array.isArray(t))return"";const e=[];for(const s of t)if(s&&typeof s=="object"){const i=s;i.type==="text"&&typeof i.text=="string"&&e.push(i.text)}return e.join("")}export{m as PiAdapter};
11
+ `+e),e}clearActive(){const t=this.activeEventId;this.activeEventId=null,this.activeSessionId=null,this.activeClientMsgId=null,this.textBuffer="",this.emittedTextByIndex.clear(),t&&this.emit("eventDone",t)}resetRunStreamState(){this.streamSeq=0,this.thinkingSeq=0,this.textBuffer="",this.emittedTextByIndex.clear(),this.activeClientMsgId=`pi_${++this.clientMsgSeq}_${Date.now()}`}nextStreamSeq(){return this.streamSeq++,this.streamSeq}sendFinalStreamChunk(t,e){this.callbacks.sendStreamChunk(t,e,"",this.nextStreamSeq(),!0,this.activeClientMsgId??void 0)}finalizeEvent(t,e,s){const i=this.nextStreamSeq();this.callbacks.sendFinalStreamChunkReliable?this.callbacks.sendFinalStreamChunkReliable(t,e,i,s).then(()=>{this.callbacks.sendEventResult(t,"responded"),a.info("pi-adapter",`event completed (reliable) event=${t}`)}).catch(n=>{a.error("pi-adapter",`finalStreamChunk ACK failed event=${t}: ${n}`),this.callbacks.sendStreamChunk(t,e,`
12
+ `,i,!0,s),this.callbacks.sendEventResult(t,"responded"),a.info("pi-adapter",`event completed (fallback) event=${t}`)}):(this.callbacks.sendStreamChunk(t,e,`
13
+ `,i,!0,s),this.callbacks.sendEventResult(t,"responded"),a.info("pi-adapter",`event completed event=${t}`))}rememberEmittedText(t,e){this.emittedTextByIndex.set(t,(this.emittedTextByIndex.get(t)??"")+e)}appendMissingText(t,e){const s=this.emittedTextByIndex.get(t)??"";if(e!==s){if(e.startsWith(s)){const i=e.slice(s.length);i&&(this.rememberEmittedText(t,i),this.appendText(i));return}if(!s){this.rememberEmittedText(t,e),this.appendText(e);return}a.info("pi-adapter",`text_end content mismatch at index=${t}, keeping streamed deltas`)}}startComposing(t,e){this.stopComposing(),this.composingSessionId=t,this.callbacks.sendSessionActivitySet(t,"composing",!0,{ref_event_id:e,ttl_ms:12e4}),this.composingInterval=setInterval(()=>{this.composingSessionId&&this.callbacks.sendSessionActivitySet(this.composingSessionId,"composing",!0,{ttl_ms:12e4})},3e4)}stopComposing(){this.composingInterval&&(clearInterval(this.composingInterval),this.composingInterval=null),this.composingSessionId&&(this.callbacks.sendSessionActivitySet(this.composingSessionId,"composing",!1),this.composingSessionId=null)}static DONE_GUARD_MS=5e3;scheduleDoneGuard(){this.cancelDoneGuard(),!(!this.activeEventId||!this.activeSessionId)&&(this.doneGuardTimer=setTimeout(()=>{if(this.doneGuardTimer=null,!this.activeEventId||!this.isStreaming)return;a.info("pi-adapter",`done guard triggered \u2014 no agent_end received, ending event=${this.activeEventId}`),this.flushTextBuffer(),this.isStreaming=!1,this.stopComposing(),this.stopIdleTimer();const t=this.activeEventId,e=this.activeSessionId??"",s=this.activeClientMsgId??void 0;this.clearActive(),this.finalizeEvent(t,e,s)},f.DONE_GUARD_MS))}cancelDoneGuard(){this.doneGuardTimer&&(clearTimeout(this.doneGuardTimer),this.doneGuardTimer=null)}resetIdleTimer(){this.stopIdleTimer(),!(this.stopped||!this.isStreaming)&&(this.idleTimer=setTimeout(()=>{a.error("pi-adapter","Idle timeout \u2014 emitting exit for respawn"),this.flushTextBuffer(),this.isStreaming=!1,this.stopComposing(),this.activeEventId&&this.callbacks.sendEventResult(this.activeEventId,"failed","idle timeout"),this.clearActive(),this.emit("exit",-1)},C))}stopIdleTimer(){this.idleTimer&&(clearTimeout(this.idleTimer),this.idleTimer=null)}async ensureSessionReady(){return this.sessionReadyPromise?this.sessionReadyPromise:(this.sessionReadyPromise=this.restoreOrCreateSession().finally(()=>{this.sessionReadyPromise=null}),this.sessionReadyPromise)}async restoreOrCreateSession(){try{await this.switchSession();return}catch(t){this.piSessionPath&&a.error("pi-adapter",`switch_session failed, creating new session: ${t}`)}await this.createNewSession()}async switchSession(){if(!this.piSessionPath)throw new Error("no PI session path");const t=await this.transport.send("switch_session",{sessionPath:this.piSessionPath});if(!t.success)throw this.piSessionPath=null,this.persistPiSessionPath(void 0),new Error(`switch_session failed: ${t.error}`)}async createNewSession(){const t=await this.transport.send("new_session");if(!t.success)throw new Error(`new_session failed: ${t.error}`);let e=null;if(e=t.data?.sessionFile??null,!e)try{const i=await this.transport.send("get_state");i.success&&(e=i.data?.sessionFile??null)}catch{}this.piSessionPath=e,this.persistPiSessionPath(this.piSessionPath??void 0)}persistPiSessionPath(t){!this.bindingStore||!this.aibotSessionId||this.bindingStore.setPiSessionPath(this.aibotSessionId,t)}}function F(h){if(!h||typeof h!="object")return"";const t=h.content;if(!Array.isArray(t))return"";const e=[];for(const s of t)if(s&&typeof s=="object"){const i=s;i.type==="text"&&typeof i.text=="string"&&e.push(i.text)}return e.join("")}export{f as PiAdapter};
@@ -1 +1 @@
1
- import{log as i}from"../core/log/index.js";import{RespawnManager as d}from"./respawn-manager.js";import{EventQueue as u}from"./event-queue.js";class p extends Error{constructor(t){super(`adapter pool full (maxSize=${t})`),this.name="PoolFullError"}}const f=6e4;class S{slots=new Map;config;factory;sendAck;onEventState=null;stopped=!1;sweepTimer=null;constructor(t,e,s){this.config=t,this.factory=e,this.sendAck=s}setEventStateHandler(t){this.onEventState=t}async deliverInboundEvent(t){const e=t.session_id,s=this.slots.get(e);if(s){if(s.startPromise&&await s.startPromise,s.state==="stopped"){s.lastActivityAt=Date.now(),this.sendAck(t.event_id,t.session_id),s.pendingEvents.push(t),i.info("adapter-pool",`Queued event ${t.event_id} for stopped slot ${e} (${s.pendingEvents.length} pending)`);return}s.lastActivityAt=Date.now(),this.sendAck(t.event_id,t.session_id),this.submitToSlotQueue(s,t);return}if(this.slots.size>=this.config.maxPoolSize&&!this.evictDeadSlot()&&!this.evictIdleSlot())throw new p(this.config.maxPoolSize);this.sendAck(t.event_id,t.session_id);const o=this.createSlot(e);try{await o.startPromise}catch(n){throw n}this.submitToSlotQueue(o,t)}submitToSlotQueue(t,e){if(!this.config.eventQueue){t.adapter.deliverInboundEvent(e);return}t.eventQueue.submit(e)==="rejected"&&i.info("adapter-pool",`Event ${e.event_id} rejected by EventQueue (queue full)`)}eventComplete(t,e){if(!this.config.eventQueue)return;const s=this.slots.get(e);s&&s.eventQueue.complete(t)}cancelEvent(t,e){if(!this.config.eventQueue)return!1;const s=this.slots.get(e);return s?s.eventQueue.cancel(t):!1}clearQueue(t){if(!this.config.eventQueue)return[];const e=this.slots.get(t);return e?e.eventQueue.clear(t):[]}getQueueSnapshot(t){if(!this.config.eventQueue)return null;const e=this.slots.get(t);return e?e.eventQueue.snapshot(t):null}deliverStopEvent(t,e){if(e){this.slots.get(e)?.adapter.deliverStopEvent(t,e);return}for(const s of this.slots.values())s.adapter.deliverStopEvent(t)}collectActiveEventIds(){const t=[];for(const e of this.slots.values()){const s=e.adapter.getActiveEventIds;typeof s=="function"&&t.push(...s.call(e.adapter))}return t}clearActiveEventsForShutdown(){for(const t of this.slots.values()){const e=t.adapter.clearActiveEventForShutdown;typeof e=="function"&&e.call(t.adapter)}}async deliverLocalAction(t,e){const s=String((t.params??{}).session_id??"");let o=s?this.slots.get(s):void 0;if(!o&&s&&e?.autoCreateSlot&&!this.stopped&&(this.slots.size>=this.config.maxPoolSize&&(this.evictDeadSlot()||this.evictIdleSlotSync()),this.slots.size<this.config.maxPoolSize)){i.info("adapter-pool",`Auto-creating slot for session ${s} (local_action trigger)`);const r=this.createSlot(s);try{await r.startPromise,o=r}catch(c){i.error("adapter-pool",`Failed to auto-create slot for ${s}: ${c}`)}}return o?.adapter?.handleLocalAction?(o.lastActivityAt=Date.now(),o.adapter.handleLocalAction(t)):(await Promise.allSettled([...this.slots.values()].map(r=>r.adapter.handleLocalAction?.(t)??Promise.resolve({handled:!1,kind:""})))).find(r=>r.status==="fulfilled"&&r.value.handled)?.value??{handled:!1,kind:""}}getOrCreateSlot(t){const e=this.slots.get(t);if(e)return e;if(!this.stopped)return this.createSlot(t)}getSlot(t){return this.slots.get(t)}getAllSlots(){return[...this.slots.values()]}async removeSlot(t){const e=this.slots.get(t);e&&(e.eventQueue.destroy(),e.respawn.stopAll(),e.adapter.removeAllListeners("exit"),await e.adapter.stop().catch(()=>{}),this.slots.delete(t))}async stop(){this.stopped=!0,this.stopIdleSweep();const t=[...this.slots.values()];this.slots.clear(),await Promise.allSettled(t.map(async e=>{e.eventQueue.destroy(),e.respawn.stopAll(),e.adapter.removeAllListeners("exit"),await e.adapter.stop().catch(()=>{})}))}startIdleSweep(){this.stopIdleSweep(),this.sweepTimer=setInterval(()=>this.sweepIdle(),f)}stopIdleSweep(){this.sweepTimer&&(clearInterval(this.sweepTimer),this.sweepTimer=null)}sweepIdle(){const t=Date.now();for(const[e,s]of this.slots){if(s.state==="stopped"&&s.respawn.exhausted){i.info("adapter-pool",`Removing dead slot for session ${e} (sweep)`),this.removeSlot(e).catch(()=>{});continue}if(s.state==="ready"){if(s.adapter.getStatus().busy){s.lastActivityAt=t;continue}t-s.lastActivityAt>this.config.idleTimeoutMs&&(i.info("adapter-pool",`Evicting idle slot for session ${e}`),this.removeSlot(e).catch(o=>{i.error("adapter-pool",`Failed to evict slot ${e}: ${o}`)}))}}}evictDeadSlot(){for(const[t,e]of this.slots)if(e.state==="stopped"&&e.respawn.exhausted)return i.info("adapter-pool",`Removing dead slot for session ${t} (exhausted respawn)`),this.removeSlot(t).catch(()=>{}),!0;return!1}evictIdleSlot(){let t=null;for(const e of this.slots.values())e.state==="ready"&&(e.adapter.getStatus().busy||(!t||e.lastActivityAt<t.lastActivityAt)&&(t=e));return t?(i.info("adapter-pool",`Evicting idle slot for session ${t.sessionId} (pool full)`),this.removeSlot(t.sessionId).catch(()=>{}),!0):!1}evictIdleSlotSync(){let t=null,e="";for(const[s,o]of this.slots)o.state==="ready"&&(o.adapter.getStatus().busy||(!t||o.lastActivityAt<t.lastActivityAt)&&(t=o,e=s));return!t||!e?!1:(i.info("adapter-pool",`Evicting idle slot for session ${e} (pool full, sync)`),this.slots.delete(e),t.respawn.stopAll(),t.adapter.removeAllListeners("exit"),t.adapter.stop().catch(()=>{}),!0)}getStatus(){let t=0,e=0;for(const s of this.slots.values())s.state==="ready"&&(t++,s.adapter.getStatus().busy&&e++);return{total:this.slots.size,ready:t,busy:e}}drainPendingEvents(t){const e=t.pendingEvents.splice(0);if(e.length!==0){i.info("adapter-pool",`Draining ${e.length} pending events for slot ${t.sessionId}`);for(const s of e)t.adapter.deliverInboundEvent(s)}}createSlot(t){const e=this.factory(t),s=new d,o=this.createEventQueue(t),n={sessionId:t,adapter:e,respawn:s,state:"starting",lastActivityAt:Date.now(),startPromise:null,pendingEvents:[],eventQueue:o};this.slots.set(t,n);const a=(async()=>{try{this.wireSlotEvents(n),await e.start(),n.state="ready"}catch(r){throw n.adapter.removeAllListeners("exit"),n.adapter.removeAllListeners("error"),this.slots.delete(t),r}finally{n.startPromise=null}})();return n.startPromise=a,s.startHealthCheck(this.slotRespawnCtx(n)),n}wireSlotEvents(t){t.adapter.on("exit",e=>{this.stopped||(i.error("adapter-pool",`Slot ${t.sessionId} adapter exited (code=${e})`),t.state="stopped",t.respawn.scheduleRespawn(this.slotRespawnCtx(t)))}),t.adapter.on?.("error",e=>{if(this.stopped)return;const s=e instanceof Error?e.message:String(e);i.error("adapter-pool",`Slot ${t.sessionId} adapter error: ${s}`)})}slotRespawnCtx(t){const e=this,s=t.sessionId;return{name:`slot-${s}`,get stopped(){return e.stopped||!e.slots.has(s)},get agentAlive(){return t.adapter.isAlive()},pingAgent:o=>t.adapter.ping(o),onRespawnNeeded:async()=>{t.adapter.removeAllListeners("exit"),await t.adapter.stop().catch(()=>{}),t.adapter=e.factory(s),t.state="starting",e.wireSlotEvents(t),await t.adapter.start(),t.state="ready",t.respawn.resetAttempts(),t.respawn.resetHealthFailures(),t.respawn.stopSlowRetry(),t.respawn.startHealthCheck(e.slotRespawnCtx(t)),e.drainPendingEvents(t)},onCleanupAgent:()=>{"cleanup"in t.adapter&&t.adapter.cleanup()},onAbortActiveRun:o=>{}}}createEventQueue(t){const e=this.config.eventQueue??{maxConcurrent:1,maxQueued:5,queueTimeoutMs:3e5,cancelableQueued:!0,cancelableRunning:!0},s={onDeliver:o=>{const n=this.slots.get(t);n&&n.adapter.deliverInboundEvent(o)},onStateChange:(o,n,a,r)=>{this.onEventState?.(o,n,a,r)},onCancelRunning:o=>{this.deliverStopEvent(o,t)},onRejected:(o,n)=>{this.onEventState?.(o.event_id,o.session_id,"failed",{reason:n})}};return new u(e,s)}}export{S as AdapterPool,p as PoolFullError};
1
+ import{log as i}from"../core/log/index.js";import{RespawnManager as c}from"./respawn-manager.js";import{EventQueue as d}from"./event-queue.js";class p extends Error{constructor(t){super(`adapter pool full (maxSize=${t})`),this.name="PoolFullError"}}const h=6e4;class S{slots=new Map;config;factory;sendAck;onEventState=null;onQueueComposing=null;stopped=!1;sweepTimer=null;constructor(t,e,s){this.config=t,this.factory=e,this.sendAck=s}setEventStateHandler(t){this.onEventState=t}setQueueComposingHandler(t){this.onQueueComposing=t}async deliverInboundEvent(t){const e=t.session_id,s=this.slots.get(e);if(s){if(s.startPromise&&await s.startPromise,s.state==="stopped"){s.lastActivityAt=Date.now(),this.sendAck(t.event_id,t.session_id),s.pendingEvents.push(t),i.info("adapter-pool",`Queued event ${t.event_id} for stopped slot ${e} (${s.pendingEvents.length} pending)`);return}s.lastActivityAt=Date.now(),this.sendAck(t.event_id,t.session_id),this.submitToSlotQueue(s,t);return}if(this.slots.size>=this.config.maxPoolSize&&!this.evictDeadSlot()&&!this.evictIdleSlot())throw new p(this.config.maxPoolSize);this.sendAck(t.event_id,t.session_id);const o=this.createSlot(e);try{await o.startPromise}catch(n){throw n}this.submitToSlotQueue(o,t)}submitToSlotQueue(t,e){if(!this.config.eventQueue){t.adapter.deliverInboundEvent(e);return}t.eventQueue.submit(e)==="rejected"&&i.info("adapter-pool",`Event ${e.event_id} rejected by EventQueue (queue full)`)}eventComplete(t,e){if(!this.config.eventQueue)return;const s=this.slots.get(e);s&&s.eventQueue.complete(t)}cancelEvent(t,e){if(!this.config.eventQueue)return!1;const s=this.slots.get(e);return s?s.eventQueue.cancel(t):!1}clearQueue(t){if(!this.config.eventQueue)return[];const e=this.slots.get(t);return e?e.eventQueue.clear(t):[]}getQueueSnapshot(t){if(!this.config.eventQueue)return null;const e=this.slots.get(t);return e?e.eventQueue.snapshot(t):null}deliverStopEvent(t,e){if(e){this.slots.get(e)?.adapter.deliverStopEvent(t,e);return}for(const s of this.slots.values())s.adapter.deliverStopEvent(t)}collectActiveEventIds(){const t=[];for(const e of this.slots.values()){const s=e.adapter.getActiveEventIds;typeof s=="function"&&t.push(...s.call(e.adapter))}return t}clearActiveEventsForShutdown(){for(const t of this.slots.values()){const e=t.adapter.clearActiveEventForShutdown;typeof e=="function"&&e.call(t.adapter)}}async deliverLocalAction(t,e){const s=String((t.params??{}).session_id??"");let o=s?this.slots.get(s):void 0;if(!o&&s&&e?.autoCreateSlot&&!this.stopped&&(this.slots.size>=this.config.maxPoolSize&&(this.evictDeadSlot()||this.evictIdleSlotSync()),this.slots.size<this.config.maxPoolSize)){i.info("adapter-pool",`Auto-creating slot for session ${s} (local_action trigger)`);const r=this.createSlot(s);try{await r.startPromise,o=r}catch(u){i.error("adapter-pool",`Failed to auto-create slot for ${s}: ${u}`)}}return o?.adapter?.handleLocalAction?(o.lastActivityAt=Date.now(),o.adapter.handleLocalAction(t)):(await Promise.allSettled([...this.slots.values()].map(r=>r.adapter.handleLocalAction?.(t)??Promise.resolve({handled:!1,kind:""})))).find(r=>r.status==="fulfilled"&&r.value.handled)?.value??{handled:!1,kind:""}}getOrCreateSlot(t){const e=this.slots.get(t);if(e)return e;if(!this.stopped)return this.createSlot(t)}getSlot(t){return this.slots.get(t)}getAllSlots(){return[...this.slots.values()]}async removeSlot(t){const e=this.slots.get(t);e&&(e.eventQueue.destroy(),e.respawn.stopAll(),e.adapter.removeAllListeners("exit"),await e.adapter.stop().catch(()=>{}),this.slots.delete(t))}async stop(){this.stopped=!0,this.stopIdleSweep();const t=[...this.slots.values()];this.slots.clear(),await Promise.allSettled(t.map(async e=>{e.eventQueue.destroy(),e.respawn.stopAll(),e.adapter.removeAllListeners("exit"),await e.adapter.stop().catch(()=>{})}))}startIdleSweep(){this.stopIdleSweep(),this.sweepTimer=setInterval(()=>this.sweepIdle(),h)}stopIdleSweep(){this.sweepTimer&&(clearInterval(this.sweepTimer),this.sweepTimer=null)}sweepIdle(){const t=Date.now();for(const[e,s]of this.slots){if(s.state==="stopped"&&s.respawn.exhausted){i.info("adapter-pool",`Removing dead slot for session ${e} (sweep)`),this.removeSlot(e).catch(()=>{});continue}if(s.state==="ready"){if(s.adapter.getStatus().busy){s.lastActivityAt=t;continue}t-s.lastActivityAt>this.config.idleTimeoutMs&&(i.info("adapter-pool",`Evicting idle slot for session ${e}`),this.removeSlot(e).catch(o=>{i.error("adapter-pool",`Failed to evict slot ${e}: ${o}`)}))}}}evictDeadSlot(){for(const[t,e]of this.slots)if(e.state==="stopped"&&e.respawn.exhausted)return i.info("adapter-pool",`Removing dead slot for session ${t} (exhausted respawn)`),this.removeSlot(t).catch(()=>{}),!0;return!1}evictIdleSlot(){let t=null;for(const e of this.slots.values())e.state==="ready"&&(e.adapter.getStatus().busy||(!t||e.lastActivityAt<t.lastActivityAt)&&(t=e));return t?(i.info("adapter-pool",`Evicting idle slot for session ${t.sessionId} (pool full)`),this.removeSlot(t.sessionId).catch(()=>{}),!0):!1}evictIdleSlotSync(){let t=null,e="";for(const[s,o]of this.slots)o.state==="ready"&&(o.adapter.getStatus().busy||(!t||o.lastActivityAt<t.lastActivityAt)&&(t=o,e=s));return!t||!e?!1:(i.info("adapter-pool",`Evicting idle slot for session ${e} (pool full, sync)`),this.slots.delete(e),t.respawn.stopAll(),t.adapter.removeAllListeners("exit"),t.adapter.stop().catch(()=>{}),!0)}getStatus(){let t=0,e=0;for(const s of this.slots.values())s.state==="ready"&&(t++,s.adapter.getStatus().busy&&e++);return{total:this.slots.size,ready:t,busy:e}}drainPendingEvents(t){const e=t.pendingEvents.splice(0);if(e.length!==0){i.info("adapter-pool",`Draining ${e.length} pending events for slot ${t.sessionId}`);for(const s of e)t.adapter.deliverInboundEvent(s)}}createSlot(t){const e=this.factory(t),s=new c,o=this.createEventQueue(t),n={sessionId:t,adapter:e,respawn:s,state:"starting",lastActivityAt:Date.now(),startPromise:null,pendingEvents:[],eventQueue:o};this.slots.set(t,n);const a=(async()=>{try{this.wireSlotEvents(n),await e.start(),n.state="ready"}catch(r){throw n.adapter.removeAllListeners("exit"),n.adapter.removeAllListeners("error"),this.slots.delete(t),r}finally{n.startPromise=null}})();return n.startPromise=a,s.startHealthCheck(this.slotRespawnCtx(n)),n}wireSlotEvents(t){t.adapter.on("exit",e=>{this.stopped||(i.error("adapter-pool",`Slot ${t.sessionId} adapter exited (code=${e})`),t.state="stopped",t.respawn.scheduleRespawn(this.slotRespawnCtx(t)))}),t.adapter.on?.("error",e=>{if(this.stopped)return;const s=e instanceof Error?e.message:String(e);i.error("adapter-pool",`Slot ${t.sessionId} adapter error: ${s}`)})}slotRespawnCtx(t){const e=this,s=t.sessionId;return{name:`slot-${s}`,get stopped(){return e.stopped||!e.slots.has(s)},get agentAlive(){return t.adapter.isAlive()},pingAgent:o=>t.adapter.ping(o),onRespawnNeeded:async()=>{t.adapter.removeAllListeners("exit"),await t.adapter.stop().catch(()=>{}),t.adapter=e.factory(s),t.state="starting",e.wireSlotEvents(t),await t.adapter.start(),t.state="ready",t.respawn.resetAttempts(),t.respawn.resetHealthFailures(),t.respawn.stopSlowRetry(),t.respawn.startHealthCheck(e.slotRespawnCtx(t)),e.drainPendingEvents(t)},onCleanupAgent:()=>{"cleanup"in t.adapter&&t.adapter.cleanup()},onAbortActiveRun:o=>{}}}createEventQueue(t){const e=this.config.eventQueue??{maxConcurrent:1,maxQueued:5,queueTimeoutMs:3e5,cancelableQueued:!0,cancelableRunning:!0},s={onDeliver:o=>{const n=this.slots.get(t);n&&n.adapter.deliverInboundEvent(o)},onStateChange:(o,n,a,r)=>{this.onEventState?.(o,n,a,r)},onCancelRunning:o=>{this.deliverStopEvent(o,t)},onRejected:(o,n)=>{this.onEventState?.(o.event_id,o.session_id,"failed",{reason:n})},onComposing:(o,n)=>{this.onQueueComposing?.(o,n)}};return new d(e,s)}}export{S as AdapterPool,p as PoolFullError};