grix-connector 1.0.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.
- package/README.md +149 -0
- package/dist/adapter/acp/acp-adapter.js +13 -0
- package/dist/adapter/acp/index.js +1 -0
- package/dist/adapter/acp/usage-parser.js +1 -0
- package/dist/adapter/claude/activity-status-manager.js +1 -0
- package/dist/adapter/claude/channel-notification.js +1 -0
- package/dist/adapter/claude/claude-adapter.js +15 -0
- package/dist/adapter/claude/claude-bridge-server.js +1 -0
- package/dist/adapter/claude/claude-tools.js +1 -0
- package/dist/adapter/claude/claude-worker-client.js +1 -0
- package/dist/adapter/claude/index.js +1 -0
- package/dist/adapter/claude/interaction-protocol.js +1 -0
- package/dist/adapter/claude/mcp-http-launcher.js +2 -0
- package/dist/adapter/claude/model-list.js +1 -0
- package/dist/adapter/claude/protocol-contract.js +1 -0
- package/dist/adapter/claude/result-timeout.js +1 -0
- package/dist/adapter/claude/skill-scanner.js +2 -0
- package/dist/adapter/claude/usage-parser.js +3 -0
- package/dist/adapter/codewhale/codewhale-adapter.js +6 -0
- package/dist/adapter/codewhale/index.js +1 -0
- package/dist/adapter/codex/codex-bridge.js +10 -0
- package/dist/adapter/codex/codex-trust.js +8 -0
- package/dist/adapter/codex/index.js +1 -0
- package/dist/adapter/codex/usage-parser.js +1 -0
- package/dist/adapter/cursor/cursor-adapter.js +8 -0
- package/dist/adapter/cursor/index.js +1 -0
- package/dist/adapter/deepseek/deepseek-adapter.js +6 -0
- package/dist/adapter/deepseek/index.js +1 -0
- package/dist/adapter/index.js +1 -0
- package/dist/adapter/opencode/index.js +1 -0
- package/dist/adapter/opencode/opencode-adapter.js +8 -0
- package/dist/adapter/opencode/opencode-transport.js +5 -0
- package/dist/adapter/opencode/opencode-types.js +0 -0
- package/dist/adapter/openhuman/index.js +1 -0
- package/dist/adapter/openhuman/openhuman-adapter.js +7 -0
- package/dist/adapter/openhuman/openhuman-transport.js +1 -0
- package/dist/adapter/openhuman/openhuman-types.js +0 -0
- package/dist/adapter/pi/index.js +1 -0
- package/dist/adapter/pi/pi-adapter.js +10 -0
- package/dist/adapter/pi/pi-transport.js +4 -0
- package/dist/adapter/pi/pi-types.js +0 -0
- package/dist/adapter/pi/usage-parser.js +1 -0
- package/dist/adapter/qwen/index.js +1 -0
- package/dist/adapter/qwen/qwen-adapter.js +4 -0
- package/dist/adapter/types.js +1 -0
- package/dist/agent/index.js +1 -0
- package/dist/agent/process.js +2 -0
- package/dist/aibot/client.js +1 -0
- package/dist/aibot/index.js +1 -0
- package/dist/aibot/types.js +0 -0
- package/dist/bridge/adapter-pool.js +1 -0
- package/dist/bridge/bridge.js +10 -0
- package/dist/bridge/deferred-events.js +1 -0
- package/dist/bridge/event-queue.js +1 -0
- package/dist/bridge/index.js +1 -0
- package/dist/bridge/respawn-manager.js +1 -0
- package/dist/bridge/revoke-handler.js +1 -0
- package/dist/bridge/runtime-config.js +1 -0
- package/dist/bridge/send-controller.js +1 -0
- package/dist/bridge/session-controller.js +9 -0
- package/dist/bridge/tool-card-utils.js +1 -0
- package/dist/core/access/allowlist-gate.js +1 -0
- package/dist/core/access/allowlist-store.js +1 -0
- package/dist/core/access/index.js +1 -0
- package/dist/core/aibot/client.js +1 -0
- package/dist/core/aibot/connection-handle.js +1 -0
- package/dist/core/aibot/connection-manager.js +1 -0
- package/dist/core/aibot/event-lifecycle-types.js +0 -0
- package/dist/core/aibot/index.js +1 -0
- package/dist/core/aibot/types.js +0 -0
- package/dist/core/config/index.js +1 -0
- package/dist/core/config/paths.js +1 -0
- package/dist/core/context/channel-context-resolution.js +1 -0
- package/dist/core/context/channel-context-store.js +1 -0
- package/dist/core/context/index.js +1 -0
- package/dist/core/context/transcript-channel-context.js +1 -0
- package/dist/core/file-ops/handler.js +1 -0
- package/dist/core/file-ops/list-files.js +1 -0
- package/dist/core/file-ops/types.js +0 -0
- package/dist/core/files/create-folder.js +1 -0
- package/dist/core/files/index.js +1 -0
- package/dist/core/files/list-files.js +1 -0
- package/dist/core/files/list-handler.js +1 -0
- package/dist/core/files/types.js +0 -0
- package/dist/core/files/utils.js +1 -0
- package/dist/core/hooks/hook-signal-store.js +2 -0
- package/dist/core/hooks/index.js +1 -0
- package/dist/core/log/bridge-event-log.js +2 -0
- package/dist/core/log/conversation-log.js +3 -0
- package/dist/core/log/index.js +1 -0
- package/dist/core/log/logger.js +6 -0
- package/dist/core/log/packet-log.js +2 -0
- package/dist/core/log/rotation.js +2 -0
- package/dist/core/mcp/event-tool-executor.js +1 -0
- package/dist/core/mcp/index.js +1 -0
- package/dist/core/mcp/internal-api-server.js +1 -0
- package/dist/core/mcp/tool-schemas.js +1 -0
- package/dist/core/mcp/tools.js +1 -0
- package/dist/core/persistence/active-event-store.js +1 -0
- package/dist/core/persistence/agent-global-config-store.js +1 -0
- package/dist/core/persistence/elicitation-store.js +1 -0
- package/dist/core/persistence/event-results-store.js +1 -0
- package/dist/core/persistence/permission-store.js +1 -0
- package/dist/core/persistence/question-store.js +1 -0
- package/dist/core/persistence/session-binding-store.js +1 -0
- package/dist/core/protocol/agent-api-media.js +1 -0
- package/dist/core/protocol/attachment-file.js +1 -0
- package/dist/core/protocol/index.js +1 -0
- package/dist/core/protocol/interaction-parser.js +1 -0
- package/dist/core/protocol/message-metadata.js +2 -0
- package/dist/core/protocol/message-reference.js +2 -0
- package/dist/core/protocol/payload-parser.js +11 -0
- package/dist/core/protocol/protocol-descriptor.js +1 -0
- package/dist/core/protocol/protocol-text.js +1 -0
- package/dist/core/provider-quota/index.js +1 -0
- package/dist/core/provider-quota/kiro.js +1 -0
- package/dist/core/provider-quota/providers.js +1 -0
- package/dist/core/provider-quota/types.js +0 -0
- package/dist/core/runtime/health.js +1 -0
- package/dist/core/runtime/index.js +1 -0
- package/dist/core/runtime/pidfile.js +2 -0
- package/dist/core/runtime/spawn.js +1 -0
- package/dist/core/text-segmentation/index.js +1 -0
- package/dist/core/text-segmentation/safe-markdown-stream-segmenter.js +6 -0
- package/dist/core/transport/index.js +1 -0
- package/dist/core/transport/json-rpc.js +3 -0
- package/dist/core/upgrade/npm-upgrader.js +2 -0
- package/dist/core/upgrade/upgrade-checker.js +1 -0
- package/dist/core/util/client-version.js +1 -0
- package/dist/core/util/codex-output-policy.js +1 -0
- package/dist/core/util/event-buffer.js +1 -0
- package/dist/core/util/index.js +1 -0
- package/dist/core/util/json-file.js +2 -0
- package/dist/core/util/normalize-string.js +1 -0
- package/dist/core/util/quoted-message-stream.js +3 -0
- package/dist/grix.js +28 -0
- package/dist/index.js +1 -0
- package/dist/log.js +3 -0
- package/dist/main.js +31 -0
- package/dist/manager.js +1 -0
- package/dist/mcp/acp-mcp-server.js +5 -0
- package/dist/mcp/stdio/server.js +10 -0
- package/dist/mcp/stream-http/config.js +1 -0
- package/dist/mcp/stream-http/connection-binding.js +1 -0
- package/dist/mcp/stream-http/event-tool-executor.js +1 -0
- package/dist/mcp/stream-http/gateway.js +1 -0
- package/dist/mcp/stream-http/index.js +1 -0
- package/dist/mcp/stream-http/security.js +1 -0
- package/dist/mcp/stream-http/session-manager.js +1 -0
- package/dist/mcp/stream-http/tool-executor.js +1 -0
- package/dist/mcp/stream-http/tool-registry.js +1 -0
- package/dist/mcp/stream-http/tool-schemas.js +1 -0
- package/dist/protocol/acp-client.js +1 -0
- package/dist/protocol/event-mapper.js +5 -0
- package/dist/protocol/index.js +1 -0
- package/dist/runtime/daemon-lock.js +2 -0
- package/dist/runtime/service-state.js +2 -0
- package/dist/scripts/approve-plan-hook.js +2 -0
- package/dist/scripts/elicitation-hook.js +6 -0
- package/dist/scripts/lib/read-stdin.js +1 -0
- package/dist/scripts/lifecycle-hook.js +2 -0
- package/dist/scripts/notification-hook.js +4 -0
- package/dist/scripts/permission-hook.js +5 -0
- package/dist/scripts/status-line-forwarder.js +2 -0
- package/dist/scripts/user-prompt-submit-hook.js +2 -0
- package/dist/service/platform-adapter.js +45 -0
- package/dist/service/process-control.js +1 -0
- package/dist/service/service-install-store.js +1 -0
- package/dist/service/service-manager.js +1 -0
- package/dist/service/service-paths.js +1 -0
- package/dist/session/index.js +1 -0
- package/dist/session/manager.js +1 -0
- package/dist/transport/index.js +1 -0
- package/dist/transport/json-rpc.js +3 -0
- package/dist/types/events.js +1 -0
- package/dist/types/index.js +1 -0
- package/dist/types/protocol.js +0 -0
- package/dist/types/session-state.js +0 -0
- package/dist/types/usage.js +0 -0
- package/openclaw-plugin/index.js +11271 -0
- package/openclaw-plugin/skills/grix-admin/SKILL.md +202 -0
- package/openclaw-plugin/skills/grix-admin/references/api-contract.md +210 -0
- package/openclaw-plugin/skills/grix-egg/SKILL.md +81 -0
- package/openclaw-plugin/skills/grix-egg/references/api-contract.md +40 -0
- package/openclaw-plugin/skills/grix-group/SKILL.md +164 -0
- package/openclaw-plugin/skills/grix-group/references/api-contract.md +97 -0
- package/openclaw-plugin/skills/grix-query/SKILL.md +247 -0
- package/openclaw-plugin/skills/grix-register/SKILL.md +86 -0
- package/openclaw-plugin/skills/grix-register/references/api-contract.md +76 -0
- package/openclaw-plugin/skills/grix-register/references/grix-concepts.md +26 -0
- package/openclaw-plugin/skills/grix-register/references/handoff-contract.md +24 -0
- package/openclaw-plugin/skills/grix-register/references/openclaw-setup.md +6 -0
- package/openclaw-plugin/skills/grix-register/references/user-replies.md +25 -0
- package/openclaw-plugin/skills/grix-register/scripts/grix_auth.ts +599 -0
- package/openclaw-plugin/skills/grix-update/SKILL.md +310 -0
- package/openclaw-plugin/skills/grix-update/references/cron-setup.md +56 -0
- package/openclaw-plugin/skills/grix-update/references/update-contract.md +149 -0
- package/openclaw-plugin/skills/message-send/SKILL.md +197 -0
- package/openclaw-plugin/skills/message-unsend/SKILL.md +186 -0
- package/openclaw-plugin/skills/message-unsend/flowchart.mermaid +27 -0
- package/openclaw-plugin/skills/openclaw-memory-setup/SKILL.md +282 -0
- package/openclaw-plugin/skills/openclaw-memory-setup/references/case-study-macpro.md +52 -0
- package/openclaw-plugin/skills/openclaw-memory-setup/references/host-readiness.md +147 -0
- package/openclaw-plugin/skills/openclaw-memory-setup/scripts/bench_ollama_embeddings.ts +326 -0
- package/openclaw-plugin/skills/openclaw-memory-setup/scripts/set_openclaw_memory_model.ts +385 -0
- package/openclaw-plugin/skills/openclaw-memory-setup/scripts/survey_host_readiness.ts +294 -0
- package/openclaw.plugin.json +24 -0
- package/package.json +114 -0
- package/scripts/install-guardian.mjs +30 -0
- package/scripts/install-guardian.sh +30 -0
- package/scripts/upgrade-guardian.sh +98 -0
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import x from"node:path";import{realpath as U,stat as T}from"node:fs/promises";import{ConnectionManager as Q}from"../core/aibot/index.js";import{ClaudeAdapter as E}from"../adapter/claude/index.js";import{CodexAdapter as F}from"../adapter/codex/index.js";import{PiAdapter as q}from"../adapter/pi/index.js";import{AcpAdapter as b}from"../adapter/acp/index.js";import{OpenHumanAdapter as O}from"../adapter/openhuman/index.js";import{CursorAdapter as $}from"../adapter/cursor/index.js";import{CodeWhaleAdapter as D}from"../adapter/codewhale/index.js";import{OpenCodeAdapter as W}from"../adapter/opencode/index.js";import{LOCAL_ACTION_ERROR_CODES as A,LOCAL_ACTION_TYPES as p,SESSION_CONTROL_ERROR_CODES as h,SESSION_CONTROL_VERBS as m,SESSION_MODE_IDS as v}from"../adapter/claude/protocol-contract.js";import{parseClaudeSessionUsage as N}from"../adapter/claude/usage-parser.js";import{fetchAvailableModels as H,getCachedModels as M,readSettingsEnv as K}from"../adapter/claude/model-list.js";import{parseAcpSessionUsage as z}from"../adapter/acp/usage-parser.js";import{parseCodexSessionUsage as G}from"../adapter/codex/usage-parser.js";import{readCodexProviderSettings as j}from"../adapter/codex/codex-trust.js";import{parsePiSessionUsage as V}from"../adapter/pi/usage-parser.js";import{log as u,ConversationLog as J}from"../core/log/index.js";import{RevokeHandler as Y}from"./revoke-handler.js";import{AdapterPool as X,PoolFullError as Z}from"./adapter-pool.js";import{parseSessionControlCommand as ee,handleSessionControlCommand as P,handleSessionControlLocalAction as L}from"./session-controller.js";import{SessionBindingStore as te}from"../core/persistence/session-binding-store.js";import{handleFileListAction as ie,handleCreateFolderAction as ne,realHomeDir as I}from"../core/files/index.js";import{AllowlistGate as se}from"../core/access/allowlist-gate.js";import{ActiveEventStore as oe}from"../core/persistence/active-event-store.js";import{DEFAULT_CONNECTOR_RUNTIME_CONFIG as re,applyConnectorRuntimeConfigPatch as de,extractConnectorRuntimeConfigPatch as ae}from"./runtime-config.js";import{SendController as ce}from"./send-controller.js";import{queryProviderQuota as y}from"../core/provider-quota/index.js";import{queryKiroQuota as B}from"../core/provider-quota/kiro.js";import{buildToolUseCard as w,buildToolResultCard as C,buildLocalGrixCardLink as le}from"./tool-card-utils.js";import{DeferredEventManager as ue}from"./deferred-events.js";const he=600*1e3,ge=60*1e3;class Ge{config;name;aibotHandle;aibotConfig;pool;stopped=!1;revokeHandler=new Y;sessionBindings=new Map;deferredMgr;sendCtrl=new ce(re);bindingStore;globalConfigStore;upgradeTrigger=null;allowlistGate;activeEventStore;cachedRateLimits=null;cachedRateLimitsSampledAtMs=null;cachedCodexContextWindow=null;cachedCodexTokenUsage=null;cachedCodexUsageSampledAtMs=null;cachedClaudeRateLimitState=null;cachedProviderQuota=null;cachedProviderQuotaSampledAtMs=null;conversationLog=null;eventSessionIndex=new Map;isRateLimitsCacheFresh(e){if(!Number.isFinite(e))return!1;const o=Number(e);return o>0&&Date.now()-o<=ge}async maybeQueryProviderQuota(){if(this.config.aibot.clientType==="kiro"){if(this.isRateLimitsCacheFresh(this.cachedProviderQuotaSampledAtMs)&&this.cachedProviderQuota)return this.cachedProviderQuota;try{const t=await B();return this.cachedProviderQuota=t,this.cachedProviderQuotaSampledAtMs=Date.now(),u.info(this.name,`[provider-quota] kiro queried: success=${t.success}`+(t.balance?` balance=${t.balance.remaining} ${t.balance.unit}`:"")+(t.planName?` plan=${t.planName}`:"")+(t.error?` error=${t.error}`:"")),t}catch(t){return u.warn(this.name,`[provider-quota] kiro query failed: ${t instanceof Error?t.message:String(t)}`),null}}let e=this.config.providerBaseUrl,o=this.config.providerApiKey;if((!e||!o)&&(this.config.adapterType??"acp")==="claude"){const t=K();e||(e=(t.ANTHROPIC_BASE_URL??"").trim()||void 0),o||(o=(t.ANTHROPIC_API_KEY??"").trim()||(t.ANTHROPIC_AUTH_TOKEN??"").trim()||void 0)}if((!e||!o)&&(this.config.adapterType??"acp")==="codex"){const t=j();!e&&t.baseUrl&&(e=t.baseUrl),!o&&t.apiKey&&(o=t.apiKey)}if(!e||!o)return null;if(this.isRateLimitsCacheFresh(this.cachedProviderQuotaSampledAtMs)&&this.cachedProviderQuota)return this.cachedProviderQuota;try{const t=await y(e,o);return this.cachedProviderQuota=t,this.cachedProviderQuotaSampledAtMs=Date.now(),u.info(this.name,`[provider-quota] queried: provider=${t.provider} success=${t.success}`+(t.tiers.length>0?` tiers=${t.tiers.map(s=>`${s.name}=${s.usedPercent}%`).join(",")}`:"")+(t.balance?` balance=${t.balance.remaining} ${t.balance.unit}`:"")+(t.error?` error=${t.error}`:"")),t}catch(t){return u.warn(this.name,`[provider-quota] query failed: ${t instanceof Error?t.message:String(t)}`),null}}getFreshClaudeRateLimitState(){const e=this.cachedClaudeRateLimitState;return e&&this.isRateLimitsCacheFresh(e.sampledAt)?e:null}getFreshCodexGlobalRateLimitCache(){const e=this.isRateLimitsCacheFresh(this.cachedRateLimitsSampledAtMs),o=this.isRateLimitsCacheFresh(this.cachedCodexUsageSampledAtMs),t=e?this.cachedRateLimits:null,s=o?this.cachedCodexContextWindow:null,i=o?this.cachedCodexTokenUsage:null;return{sampledAt:Math.max(e?this.cachedRateLimitsSampledAtMs??0:0,o?this.cachedCodexUsageSampledAtMs??0:0)||null,rateLimits:t,contextWindow:s,tokenUsage:i,hasData:!!(t||s||i)}}getStatus(){const e=this.pool?.getStatus()??{total:0,ready:0,busy:0};return{name:this.name,alive:!this.stopped,busy:e.busy>0,exhausted:this.pool?[...this.pool.getAllSlots()].some(o=>o.respawn.exhausted):!1,adapterType:this.config.adapterType??"acp",pool:e}}constructor(e,o){this.config=e,this.name=e.name;const t=e.adapterType??"acp";this.aibotConfig={...e.aibot,...t==="claude"?{localActions:e.aibot.localActions??["session_control","claude_interaction_reply","get_session_usage","get_rate_limits","set_model","set_mode","thread_compact","get_agent_global_config"]}:{}},e.eventQueue&&(this.aibotConfig.concurrency={max_concurrent:e.eventQueue.maxConcurrent,max_queued:e.eventQueue.maxQueued,queue_timeout_ms:e.eventQueue.queueTimeoutMs,cancelable_queued:e.eventQueue.cancelableQueued,cancelable_running:e.eventQueue.cancelableRunning}),this.conversationLog=e.logDir?new J(e.logDir):null,this.bindingStore=new te(e.bindingsPath),this.bindingStore.load(),this.globalConfigStore=o??null,this.deferredMgr=new ue(this.name),this.allowlistGate=e.allowlistPath?new se(e.allowlistPath):null,this.activeEventStore=e.activeEventStorePath?new oe(e.activeEventStorePath):null}async start(){(this.config.adapterType??"acp")==="claude"&&(await H().catch(()=>{}),this.maybeQueryProviderQuota().catch(()=>{})),this.config.aibot.clientType==="kiro"&&this.maybeQueryProviderQuota().catch(()=>{}),await this.connectAibot(),this.sendCtrl.bind(this.aibotHandle);const e=this.config.adapterType??"acp";this.pool=new X({maxPoolSize:this.config.poolMaxSize??20,idleTimeoutMs:this.config.poolIdleTimeoutMs??3e5,eventQueue:this.config.eventQueue},o=>{const t=this.createAdapter(e,o);return t instanceof b&&t.on("acpSessionReady",s=>{this.bindingStore.setAcpSessionId(o,s)}),t},(o,t)=>{this.aibotHandle.sendEventAck({event_id:o,session_id:t,received_at:Date.now()})}),this.pool.setEventStateHandler((o,t,s,i)=>{this.aibotHandle.sendEventState({event_id:o,session_id:t,state:s,content_preview:i?.content_preview,queue_position:i?.queue_position,queue_total:i?.queue_total,actions:i?.actions,reason:i?.reason,updated_at:Date.now()}),this.pushQueueSnapshotForSession(t),(s==="canceled"||s==="failed")&&this.aibotHandle.sendEventResult({event_id:o,status:s==="canceled"?"canceled":"failed",msg:i?.reason,updated_at:Date.now()})}),this.pool.startIdleSweep(),u.info(this.name,`Ready (adapter: ${e}, poolMax: ${this.config.poolMaxSize??20})`)}async stop(){this.stopped=!0,this.pool?.stopIdleSweep();const e=this.pool?.collectActiveEventIds()??[];e.length>0&&this.activeEventStore&&await this.activeEventStore.save(e);for(const t of e)u.info(this.name,`Canceling active event on shutdown: ${t}`),this.sendEventResultWithCleanup(t,"canceled","process shutting down");e.length>0&&await new Promise(t=>setTimeout(t,100)),this.pool?.clearActiveEventsForShutdown();const o=this.deferredMgr.getAllDeferredEvents();for(const t of o)u.info(this.name,`Failing deferred event on shutdown: ${t.event_id}`),this.sendEventResultWithCleanup(t.event_id,"failed","process shutting down");this.deferredMgr.clearAll(),await this.pool?.stop(),this.aibotHandle?.disconnect(),e.length>0&&this.activeEventStore&&await this.activeEventStore.save([]),this.eventSessionIndex.clear()}createAdapter(e,o){switch(e){case"claude":return this.createClaudeAdapter(o);case"codex":return this.createCodexAdapter(o);case"pi":return this.createPiAdapter(o);case"openhuman":return this.createOpenHumanAdapter(o);case"codewhale":return this.createCodeWhaleAdapter(o);case"cursor":return this.createCursorAdapter(o);case"opencode":return this.createOpenCodeAdapter(o);default:return this.createAcpAdapter(o)}}createCursorAdapter(e){const o={...this.config.adapterOptions??{}},t=this.globalConfigStore?.get(this.name);!o.model&&t?.modelId&&(o.model=t.modelId),!o.mode&&t?.modeId&&(o.mode=t.modeId);const s={sendStreamChunk:(i,n,r,d,a)=>{this.sendStreamChunkByRuntimeConfig(i,n,r,d,a)},sendEventResult:(i,n,r)=>{this.sendEventResultWithCleanup(i,n,r)},sendEventAck:(i,n)=>{this.aibotHandle.sendEventAck({event_id:i,session_id:n,received_at:Date.now()})},sendSessionComposing:(i,n)=>{this.aibotHandle.sendSessionActivitySet({session_id:i,kind:"composing",active:n})},sendRawEventEnvelope:(i,n,r)=>{this.aibotHandle.sendMsg({event_id:i,session_id:n,msg_type:1,content:"[cursor] raw_event",extra:{channel_data:{cursor:{raw_event:r}},agent_api_origin:!0}})},agentInvoke:async(i,n)=>this.aibotHandle.agentInvoke(i,n),sendLocalActionResult:(i,n,r,d,a)=>{this.aibotHandle.sendLocalActionResult({action_id:i,status:n,...r!==void 0?{result:r}:{},...d?{error_code:d}:{},...a?{error_msg:a}:{}})}};return new $({command:this.config.agent.command,args:this.config.agent.args,env:this.config.agent.env,options:o},s)}createClaudeAdapter(e){const o={sendReply:(n,r,d,a,c)=>{this.sendReplyByRuntimeConfig(n,r,d,a,c)},sendStreamChunk:(n,r,d,a,c,l,g)=>{this.sendStreamChunkByRuntimeConfig(n,r,d,a,c,l,g)},sendMedia:(n,r,d,a,c,l,g)=>{this.aibotHandle.sendMedia({event_id:n,session_id:r,content:d,msg_type:2,quoted_message_id:c||void 0,client_msg_id:l||void 0,extra:g?{media_caption:a,...g}:{media_caption:a}})},sendEventResult:(n,r,d,a)=>{this.sendEventResultWithCleanup(n,r,d,a)},sendEventAck:(n,r)=>{this.aibotHandle.sendEventAck({event_id:n,session_id:r,received_at:Date.now()})},agentInvoke:async(n,r,d)=>this.aibotHandle.agentInvoke(n,r,d),sendLocalActionResult:(n,r,d,a,c)=>{this.aibotHandle.sendLocalActionResult({action_id:n,status:r,...d!==void 0?{result:d}:{},...a?{error_code:a}:{},...c?{error_msg:c}:{}})},sendSessionComposing:(n,r,d,a)=>{this.aibotHandle.sendSessionActivitySet({session_id:n,kind:"composing",active:r,ttl_ms:r?3e4:void 0,ref_event_id:r?a:void 0,...d?{activity:d}:{}})},sendToolUse:(n,r,d,a)=>{this.sendToolExecutionCard(n,r,w(d,a))},sendToolResult:(n,r,d,a)=>{this.sendToolExecutionCard(n,r,C(d,a))},getWsUrl:()=>this.config.aibot.url,getAgentId:()=>this.config.aibot.agentId,getApiKey:()=>this.config.aibot.apiKey,getActiveEventCount:()=>0,getPendingPermissionCount:()=>0,getPendingElicitationCount:()=>0,sendAgentQuestionCard:(n,r,d)=>{const a=d.questions.map(l=>l.header).join(", "),c=le(`[Agent Question] ${d.request_id}`,"agent_question",d);this.aibotHandle.sendText({event_id:n,session_id:r,content:c,msg_type:1,extra:{card_type:"agent_question",summary_text:a}})},sendPermissionCard:n=>{this.aibotHandle.sendMsg({event_id:n.eventId,session_id:n.sessionId,msg_type:1,content:n.toolTitle?`Permission required: ${n.toolTitle}`:"Permission request",extra:{channel_data:{execApproval:{approvalId:n.approvalId,approvalSlug:n.toolName},grix:{execApproval:{approval_command_id:n.approvalId,command:n.toolTitle||n.toolName,host:"claude"}}},agent_api_origin:!0}})},getHandle:()=>this.aibotHandle,onStatusLineUpdated:n=>{(n.rateLimits?.fiveHour||n.rateLimits?.sevenDay)&&(this.cachedClaudeRateLimitState=n);const r=this.bindingStore.get(e);r?.cwd&&this.aibotHandle.sendUpdateBindingCard({session_id:e,worker_status:this.getClaudeWorkerStatus(e),cwd:r.cwd,meta:this.buildClaudeToolbarMeta(e)})}},t=this.config.adapterOptions??{},s=this.globalConfigStore?.get(this.name),i={...t,sessionRuntimeResolver:()=>{const n=this.bindingStore.get(e);return{cwd:n?.cwd,modeId:n?.modeId??s?.modeId??v.fullAuto,modelId:n?.modelId??s?.modelId,pluginDir:t.pluginDir,claudeSessionId:n?.claudeSessionId,onSessionIdAssigned:r=>{this.bindingStore.setClaudeSessionId(e,r)}}}};return new E({command:this.config.agent.command,args:this.config.agent.args,env:this.config.agent.env,options:i},o)}createCodexAdapter(e){let o=null;const t={sendEventResult:(n,r,d)=>{this.sendEventResultWithCleanup(n,r,d)},sendEventAck:(n,r)=>this.aibotHandle.sendEventAck({event_id:n,session_id:r,received_at:Date.now()}),sendCodexEvent:n=>{this.shouldDropCodexDisplayEvent(n.event_id,n.codex_method)||(this.aibotHandle.sendCodexEvent(n),this.logCodexEventToConversation(n))},sendCodexEventReliable:async n=>{if(!this.shouldDropCodexDisplayEvent(n.event_id,n.codex_method)){try{await this.aibotHandle.sendCodexEventReliable(n)}catch(r){u.warn("bridge",`[codex] sendCodexEventReliable ACK failed event=${n.event_id}: ${r}`)}u.info("bridge",`[codex] sendCodexEventReliable done event=${n.event_id} method=${n.codex_method}`),this.logCodexEventToConversation(n)}},sendUpdateBindingCard:(n,r,d,a)=>this.aibotHandle.sendUpdateBindingCard({session_id:n,worker_status:r,cwd:d,...a?{meta:a}:{}}),agentInvoke:async(n,r)=>this.aibotHandle.agentInvoke(n,r),sendLocalActionResult:(n,r,d,a,c)=>this.aibotHandle.sendLocalActionResult({action_id:n,status:r,...d!==void 0?{result:d}:{},...a?{error_code:a}:{},...c?{error_msg:c}:{}}),sendSessionActivitySet:(n,r,d,a)=>{this.aibotHandle.sendSessionActivitySet({session_id:n,kind:r,active:d,...a??{}})},getConversationLog:()=>this.conversationLog,onRateLimitsUpdated:n=>{this.cachedRateLimits=n,this.cachedRateLimitsSampledAtMs=Date.now();const r=this.bindingStore.get(e);if(r?.cwd){const d=this.cachedRateLimitsSampledAtMs;this.aibotHandle.sendUpdateBindingCard({session_id:e,worker_status:"ready",cwd:r.cwd,meta:{rate_limits:{primary:n.primary,secondary:n.secondary,sampledAt:d},rate_limit_primary_percent:n.primary.usedPercent,rate_limit_secondary_percent:n.secondary.usedPercent,rate_limit_primary_window_min:n.primary.windowMinutes,rate_limit_secondary_window_min:n.secondary.windowMinutes,...o?.getEffortMeta()}})}},onContextWindowUpdated:n=>{if(!n)return;this.cachedCodexContextWindow=n,this.cachedCodexUsageSampledAtMs=Date.now();const r=this.bindingStore.get(e);r?.cwd&&this.aibotHandle.sendUpdateBindingCard({session_id:e,worker_status:"ready",cwd:r.cwd,meta:{context_window:n,...o?.getEffortMeta()}})},onTokenUsageUpdated:n=>{n&&(this.cachedCodexTokenUsage=n,this.cachedCodexUsageSampledAtMs=Date.now())}},s=this.config.adapterOptions??{},i=this.globalConfigStore?.get(this.name);return o=new F({command:this.config.agent.command,args:this.config.agent.args,env:this.config.agent.env,options:{...s,model:i?.codexModelId??s.model,collaborationMode:i?.codexModeId??s.collaborationMode,reasoningEffort:i?.codexReasoningEffort??s.reasoningEffort,sandboxMode:i?.codexSandboxMode??s.sandboxMode,aibotSessionId:e,bindingStore:this.bindingStore}},t),o}createCodeWhaleAdapter(e){const o={sendEventResult:(s,i,n)=>{this.sendEventResultWithCleanup(s,i,n)},sendEventAck:(s,i)=>this.aibotHandle.sendEventAck({event_id:s,session_id:i,received_at:Date.now()}),sendStreamChunk:(s,i,n,r,d,a)=>{this.sendStreamChunkByRuntimeConfig(s,i,n,r,d,a)},sendUpdateBindingCard:(s,i,n,r)=>this.aibotHandle.sendUpdateBindingCard({session_id:s,worker_status:i,cwd:n,...r?{meta:r}:{}}),sendLocalActionResult:(s,i,n,r,d)=>this.aibotHandle.sendLocalActionResult({action_id:s,status:i,...n!==void 0?{result:n}:{},...r?{error_code:r}:{},...d?{error_msg:d}:{}}),sendSessionActivitySet:(s,i,n,r)=>{this.aibotHandle.sendSessionActivitySet({session_id:s,kind:i,active:n,...r??{}})},sendToolUse:(s,i,n,r)=>{this.sendToolExecutionCard(s,i,w(n,r))},sendToolResult:(s,i,n,r)=>{this.sendToolExecutionCard(s,i,C(n,r))},getConversationLog:()=>this.conversationLog},t=this.config.adapterOptions??{};return new D({command:this.config.agent.command,args:this.config.agent.args,env:this.config.agent.env,options:{...t,aibotSessionId:e,bindingStore:this.bindingStore}},o)}createPiAdapter(e){const o={sendEventResult:(s,i,n)=>{this.sendEventResultWithCleanup(s,i,n),u.info("bridge",`[pi] sendEventResult event=${s} status=${i}`)},sendEventAck:(s,i)=>this.aibotHandle.sendEventAck({event_id:s,session_id:i,received_at:Date.now()}),sendUpdateBindingCard:(s,i,n)=>this.aibotHandle.sendUpdateBindingCard({session_id:s,worker_status:i,cwd:n}),agentInvoke:async(s,i)=>this.aibotHandle.agentInvoke(s,i),sendLocalActionResult:(s,i,n,r,d)=>{this.aibotHandle.sendLocalActionResult({action_id:s,status:i,...n!==void 0?{result:n}:{},...r?{error_code:r}:{},...d?{error_msg:d}:{}})},sendSessionActivitySet:(s,i,n,r)=>{this.aibotHandle.sendSessionActivitySet({session_id:s,kind:i,active:n,...r??{}})},sendToolUse:(s,i,n,r)=>{this.sendToolExecutionCard(s,i,w(n,r))},sendToolResult:(s,i,n,r)=>{this.sendToolExecutionCard(s,i,C(n,r))},sendStreamChunk:(s,i,n,r,d,a)=>{this.sendStreamChunkByRuntimeConfig(s,i,n,r,d,a),d&&u.info("bridge",`[pi] sendFinalStreamChunk event=${s} seq=${r}`)},sendFinalStreamChunkReliable:async(s,i,n,r)=>{if(this.finalizeThinking(s,i),this.resolveEventRuntimeConfig(s).responseDelivery==="single_message"&&s){this.bufferStreamChunk(s,i,"",!0);return}try{await this.aibotHandle.sendStreamChunkRequest({event_id:s,session_id:i,delta_content:"",chunk_seq:n,is_finish:!0,...r?{client_msg_id:r}:{}})}catch(a){u.warn("bridge",`[pi] sendFinalStreamChunkReliable ACK failed event=${s}: ${a}`)}u.info("bridge",`[pi] sendFinalStreamChunkReliable done event=${s} seq=${n}`)},sendThinking:(s,i,n)=>{this.sendThinkingByRuntimeConfig(s,i,n)},sendRunError:(s,i,n,r,d)=>{this.sendStreamChunkByRuntimeConfig(s,i,`
|
|
2
|
+
|
|
3
|
+
Error: ${n}`,r,!1,d)}},t=this.config.adapterOptions??{};return new q({command:this.config.agent.command,args:this.config.agent.args,env:this.config.agent.env,options:{...t,aibotSessionId:e,bindingStore:this.bindingStore}},o)}createOpenHumanAdapter(e){const o={sendStreamChunk:(s,i,n,r,d,a)=>{this.sendStreamChunkByRuntimeConfig(s,i,n,r,d,a)},sendFinalStreamChunkReliable:async(s,i,n,r)=>{if(this.finalizeThinking(s,i),this.resolveEventRuntimeConfig(s).responseDelivery==="single_message"&&s){this.bufferStreamChunk(s,i,"",!0);return}try{await this.aibotHandle.sendStreamChunkRequest({event_id:s,session_id:i,delta_content:"",chunk_seq:n,is_finish:!0,...r?{client_msg_id:r}:{}})}catch(a){u.warn("bridge",`[openhuman] sendFinalStreamChunkReliable ACK failed event=${s}: ${a}`)}},sendEventResult:(s,i,n)=>{this.sendEventResultWithCleanup(s,i,n),u.info("bridge",`[openhuman] sendEventResult event=${s} status=${i}`)},sendEventAck:(s,i)=>this.aibotHandle.sendEventAck({event_id:s,session_id:i,received_at:Date.now()}),sendToolUse:(s,i,n,r)=>{this.sendToolExecutionCard(s,i,w(n,r))},sendToolResult:(s,i,n,r)=>{this.sendToolExecutionCard(s,i,C(n,r))},sendThinking:(s,i,n)=>{this.sendThinkingByRuntimeConfig(s,i,n)},sendRunError:(s,i,n)=>{this.sendStreamChunkByRuntimeConfig(s,i,`
|
|
4
|
+
|
|
5
|
+
Error: ${n}`,0,!1)},sendSessionComposing:(s,i)=>{this.aibotHandle.sendSessionActivitySet({session_id:s,kind:"composing",active:i,ttl_ms:i?3e4:void 0})},sendUpdateBindingCard:(s,i,n)=>this.aibotHandle.sendUpdateBindingCard({session_id:s,worker_status:i,cwd:n}),agentInvoke:async(s,i)=>this.aibotHandle.agentInvoke(s,i),sendLocalActionResult:(s,i,n,r,d)=>{this.aibotHandle.sendLocalActionResult({action_id:s,status:i,...n!==void 0?{result:n}:{},...r?{error_code:r}:{},...d?{error_msg:d}:{}})}},t=this.config.adapterOptions??{};return new O({command:this.config.agent.command,args:this.config.agent.args,env:this.config.agent.env,options:t},o,{port:t.port,host:t.host,workspaceDir:t.workspace_dir,sessionToken:t.session_token,enableSessionBinding:!0,aibotSessionId:e})}createOpenCodeAdapter(e){const o={sendStreamChunk:(s,i,n,r,d,a)=>{this.sendStreamChunkByRuntimeConfig(s,i,n,r,d,a)},sendFinalStreamChunkReliable:async(s,i,n,r)=>{if(this.finalizeThinking(s,i),this.resolveEventRuntimeConfig(s).responseDelivery==="single_message"&&s){this.bufferStreamChunk(s,i,"",!0);return}try{await this.aibotHandle.sendStreamChunkRequest({event_id:s,session_id:i,delta_content:"",chunk_seq:n,is_finish:!0,...r?{client_msg_id:r}:{}})}catch(a){u.warn("bridge",`[opencode] sendFinalStreamChunkReliable ACK failed event=${s}: ${a}`)}},sendEventResult:(s,i,n)=>{this.sendEventResultWithCleanup(s,i,n),u.info("bridge",`[opencode] sendEventResult event=${s} status=${i}`)},sendEventAck:(s,i)=>this.aibotHandle.sendEventAck({event_id:s,session_id:i,received_at:Date.now()}),sendToolUse:(s,i,n,r)=>{this.sendToolExecutionCard(s,i,w(n,r))},sendToolResult:(s,i,n,r)=>{this.sendToolExecutionCard(s,i,C(n,r))},sendThinking:(s,i,n)=>{this.sendThinkingByRuntimeConfig(s,i,n)},sendRunError:(s,i,n)=>{this.sendStreamChunkByRuntimeConfig(s,i,`
|
|
6
|
+
|
|
7
|
+
Error: ${n}`,0,!1)},sendSessionComposing:(s,i)=>{this.aibotHandle.sendSessionActivitySet({session_id:s,kind:"composing",active:i,ttl_ms:i?3e4:void 0})},sendUpdateBindingCard:(s,i,n)=>this.aibotHandle.sendUpdateBindingCard({session_id:s,worker_status:i,cwd:n}),agentInvoke:async(s,i)=>this.aibotHandle.agentInvoke(s,i),sendLocalActionResult:(s,i,n,r,d)=>{this.aibotHandle.sendLocalActionResult({action_id:s,status:i,...n!==void 0?{result:n}:{},...r?{error_code:r}:{},...d?{error_msg:d}:{}})}},t=this.config.adapterOptions??{};return new W({command:this.config.agent.command,args:this.config.agent.args,env:this.config.agent.env,options:t},o,{port:t.port,hostname:t.hostname,model:t.model,agent:t.agent,permissionPolicy:t.permission_policy,enableSessionBinding:!0,aibotSessionId:e})}createAcpAdapter(e){const o=this.isAcpRawTransportEnabled(),t={sendStreamChunk:(i,n,r,d,a,c)=>{this.sendStreamChunkByRuntimeConfig(i,n,r,d,a,c)},sendFinalStreamChunkReliable:async(i,n,r,d)=>{if(this.finalizeThinking(i,n),this.resolveEventRuntimeConfig(i).responseDelivery==="single_message"&&i){this.bufferStreamChunk(i,n,"",!0);return}try{await this.aibotHandle.sendStreamChunkRequest({event_id:i,session_id:n,delta_content:"",chunk_seq:r,is_finish:!0,...d?{client_msg_id:d}:{}})}catch(c){u.warn("bridge",`[acp] sendFinalStreamChunkReliable ACK failed event=${i}: ${c}`)}u.info("bridge",`[acp] sendFinalStreamChunkReliable done event=${i} seq=${r}`)},sendEventResult:(i,n,r)=>{this.sendEventResultWithCleanup(i,n,r)},sendEventAck:(i,n)=>{this.aibotHandle.sendEventAck({event_id:i,session_id:n,received_at:Date.now()})},agentInvoke:async(i,n)=>this.aibotHandle.agentInvoke(i,n),sendLocalActionResult:(i,n,r,d,a)=>{this.aibotHandle.sendLocalActionResult({action_id:i,status:n,...r!==void 0?{result:r}:{},...d?{error_code:d}:{},...a?{error_msg:a}:{}})},sendSessionComposing:(i,n)=>{this.aibotHandle.sendSessionActivitySet({session_id:i,kind:"composing",active:n,...n?{ttl_ms:3e4}:{}})},sendRawEventEnvelope:(i,n,r)=>{this.sendAcpRawEventEnvelope(i,n,r)},sendToolUse:(i,n,r,d)=>{if(o){this.sendAcpRawEventEnvelope(i,n,{type:"tool_use",payload:{tool_name:r,tool_input:d??""}});return}this.sendToolExecutionCard(i,n,w(r,d))},sendToolResult:(i,n,r,d)=>{this.sendToolExecutionCard(i,n,C(r,d))},sendThinking:(i,n,r)=>{this.sendThinkingByRuntimeConfig(i,n,r)},sendRunError:(i,n,r)=>{this.sendStreamChunkByRuntimeConfig(i,n,`
|
|
8
|
+
|
|
9
|
+
Error: ${r}`,1,!1)},sendPermissionCard:i=>{if(o){this.sendAcpRawEventEnvelope(i.eventId,i.sessionId,{type:"permission_request",payload:{tool_call_id:i.toolCallId,tool_name:i.toolName,tool_title:i.toolTitle,options:i.options}});return}this.aibotHandle.sendMsg({event_id:i.eventId,session_id:i.sessionId,msg_type:1,content:i.toolTitle?`Permission required: ${i.toolTitle}`:"Permission request",extra:{channel_data:{execApproval:{approvalId:i.toolCallId,approvalSlug:i.toolName},grix:{execApproval:{approval_command_id:i.toolCallId,command:i.toolTitle||i.toolName,host:"acp"}}},agent_api_origin:!0}})},sendAuthNotification:(i,n)=>{i&&this.aibotHandle.sendMsg({session_id:i,msg_type:1,content:n,extra:{biz_card:{version:1,type:"agent_error",payload:{error:{name:"AuthRequired",message:n}}}}})},sendUpdateBindingCard:(i,n,r,d)=>{const a={...d??{}};if(!a.rate_limits&&this.cachedProviderQuota?.success){this.isRateLimitsCacheFresh(this.cachedProviderQuotaSampledAtMs)||this.maybeQueryProviderQuota().catch(()=>{});const c=this.providerQuotaToRateLimits(this.cachedProviderQuota);c&&(a.rate_limits=c)}!a.provider_quota&&this.cachedProviderQuota?.success&&(a.provider_quota=this.cachedProviderQuota),this.aibotHandle.sendUpdateBindingCard({session_id:i,worker_status:n,cwd:r,...Object.keys(a).length>0?{meta:a}:{}})}},s=this.globalConfigStore?.get(this.name);return new b({command:this.config.agent.command,args:this.config.agent.args,env:this.config.agent.env},t,{acpAuthMethod:this.config.acpAuthMethod,acpInitialMode:this.config.acpInitialMode??s?.acpInitialMode,acpMcpTools:this.config.acpMcpTools,rawTransport:o,eventResultsPath:this.config.eventResultsPath,approvalMode:this.config.approvalMode,bindingStore:this.config.enableSessionBinding?this.bindingStore:void 0,aibotSessionId:e,autoInjectArgs:this.config.autoInjectArgs})}async connectAibot(){const e=new Q;if(this.aibotHandle=await e.connect(this.aibotConfig,{aborted:()=>this.stopped,label:this.name}),this.aibotHandle.onEvent(o=>{this.handleAibotEvent(o).catch(t=>{u.error(this.name,`handleAibotEvent failed: ${t}`),this.aibotHandle.sendEventAck({event_id:o.event_id,session_id:o.session_id,received_at:Date.now()}),this.aibotHandle.sendEventResult({event_id:o.event_id,status:"failed",msg:t instanceof Error?t.message:String(t),updated_at:Date.now()})})}),this.aibotHandle.onStop(o=>{try{this.handleAibotStop(o)}catch(t){u.error(this.name,`handleAibotStop failed: ${t}`)}}),this.aibotHandle.onRevoke(o=>{try{this.handleAibotRevoke(o)}catch(t){u.error(this.name,`handleAibotRevoke failed: ${t}`)}}),this.aibotHandle.onLocalAction(o=>{this.handleAibotLocalAction(o).catch(t=>{u.error(this.name,`handleAibotLocalAction failed: ${t}`)})}),this.aibotHandle.onEventCancel(o=>{u.info(this.name,`recv event_cancel event_id=${o.event_id} session_id=${o.session_id}`),this.handleEventCancel(o).catch(t=>{u.error(this.name,`handleEventCancel failed: ${t}`)})}),this.aibotHandle.onQueueClear(o=>{const t=this.pool.clearQueue(o.session_id);this.aibotHandle.sendQueueClearResult({session_id:o.session_id,canceled_event_ids:t}),this.pushQueueSnapshotForSession(o.session_id)}),u.info(this.name,"Connected to aibot"),this.activeEventStore){const o=await this.activeEventStore.drain();if(o.length>0){u.warn(this.name,`Recovering ${o.length} stale event(s) from previous run`);for(const t of o)u.info(this.name,`Failing stale event on startup: ${t}`),this.aibotHandle.sendEventResult({event_id:t,status:"failed",msg:"process restarted, event lost",updated_at:Date.now()})}}this.pushQueueSnapshots()}pushQueueSnapshots(){if(!(!this.config.eventQueue||!this.pool))for(const e of this.pool.getAllSlots())this.pushQueueSnapshotForSession(e.sessionId)}pushQueueSnapshotForSession(e){if(!this.config.eventQueue||!this.pool)return;const o=this.pool.getQueueSnapshot(e);o&&this.aibotHandle.sendQueueSnapshot({session_id:e,running:o.running,running_items:o.running_items.map(t=>({event_id:t.event_id,...t.content_preview?{content_preview:t.content_preview}:{},...t.title?{title:t.title}:{},...t.summary?{summary:t.summary}:{}})),queued:o.queued.map(t=>({event_id:t.event_id,position:t.position,...t.content_preview?{content_preview:t.content_preview}:{},...t.title?{title:t.title}:{},...t.summary?{summary:t.summary}:{},actions:[{type:"cancel"}]}))})}sendReplyByRuntimeConfig(e,o,t,s,i){this.indexEventSession(e,o),this.sendCtrl.sendReply(e,o,t,s,i),t&&this.conversationLog?.logOutbound?.(o,e,"reply",t)}sendStreamChunkByRuntimeConfig(e,o,t,s,i,n,r){this.indexEventSession(e,o),this.sendCtrl.sendStreamChunk(e,o,t,s,i,n,r),(t||i)&&this.conversationLog?.logOutbound?.(o,e,i?"stream_chunk_finish":"stream_chunk",t)}sendEventResultWithCleanup(e,o,t,s){this.sendCtrl.sendEventResult(e,o,t,s);const i=this.eventSessionIndex.get(e);i&&(this.pool.eventComplete(e,i),this.pushQueueSnapshotForSession(i),this.conversationLog?.logResult?.(i,e,o,t),this.eventSessionIndex.delete(e)),o==="responded"&&(this.cachedProviderQuotaSampledAtMs=null,this.maybeQueryProviderQuota().catch(()=>{}))}sendThinkingByRuntimeConfig(e,o,t){this.sendCtrl.sendThinking(e,o,t)}bufferStreamChunk(e,o,t,s,i){this.sendCtrl.bufferOnly(e,o,t,s,i)}flushBufferedStreamText(e){}resolveEventRuntimeConfig(e){return this.sendCtrl.resolveEventRuntimeConfig(e)}captureEventRuntimeConfig(e){this.sendCtrl.captureEventRuntimeConfig(e),this.indexEventSession(e.event_id,e.session_id)}indexEventSession(e,o){!e||!o||this.eventSessionIndex.set(e,o)}shouldDropToolDisplayEvent(e){return this.sendCtrl.shouldDropToolDisplayEvent(e)}shouldDropThinkingDisplayEvent(e){return this.sendCtrl.shouldDropThinkingDisplayEvent(e)}shouldDropCodexDisplayEvent(e,o){return this.sendCtrl.shouldDropCodexDisplayEvent(e,o)}logCodexEventToConversation(e){if(!this.conversationLog||e.codex_method!=="item/agentMessage/delta")return;const t=e.codex_payload?.params?.delta;if(!t)return;const s=this.eventSessionIndex.get(e.event_id)??e.session_id;this.conversationLog.append(s,{ts:Date.now(),dir:"outbound",event_id:e.event_id,kind:"codex_delta",text_len:t.length,content:t})}isAcpRawTransportEnabled(){return(this.config.adapterOptions??{}).raw_transport===!0}shouldDropAcpRawDisplayEvent(e,o){return this.sendCtrl.shouldDropAcpRawDisplayEvent(e,o)}sendAcpRawEventEnvelope(e,o,t){this.shouldDropAcpRawDisplayEvent(e,t.type)||this.aibotHandle.sendMsg({event_id:e,session_id:o,msg_type:1,content:this.buildAcpRawEventFallbackText(t),extra:{channel_data:{acp:{raw_event:t}},agent_api_origin:!0}})}buildAcpRawEventFallbackText(e){const o=String(e.type??"").trim();if(!o)return"[acp] event";switch(o){case"permission_request":return`Permission required: ${String(e.payload?.tool_title??e.payload?.tool_name??"permission request")}`;case"tool_use":return`[tool] ${String(e.payload?.tool_name??"tool")}`;case"tool_result":return"[tool result]";case"thinking":return"[thinking]";case"error":return`[error] ${String(e.payload?.message??"agent error")}`;case"result":return"[result]";default:return`[acp] ${o}`}}sendToolExecutionCard(e,o,t,s){this.sendCtrl.sendToolExecutionCard(e,o,t,s)}async handleAibotEvent(e){if(this.stopped){this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendEventResult({event_id:e.event_id,status:"failed",msg:"agent shutting down",updated_at:Date.now()});return}this.logInboundConversation(e);const o=this.config.adapterType??"acp";if(this.allowlistGate&&e.sender_id&&!await this.allowlistGate.checkAccess(e.sender_id)){this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendEventResult({event_id:e.event_id,status:"responded",code:"access_denied",msg:`sender ${e.sender_id} is not authorized`,updated_at:Date.now()});return}const t=ae(e.extra);if(t.error){this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendEventResult({event_id:e.event_id,status:"failed",code:"connector_config_invalid",msg:t.error,updated_at:Date.now()});return}this.sendCtrl.setGlobalRuntimeConfig(de(this.sendCtrl.getGlobalRuntimeConfig(),t.patch));const s=ee(e);if(s){if(s.verb===m.exec){await this.handleSessionControlCommand(s,e);return}if(o==="claude"){await this.handleSessionControlCommand(s,e);return}if(o==="codex"&&s.verb===m.open){await this.handleCodexSessionControlOpen(s,e);return}if(o==="pi"&&s.verb===m.open){await this.handlePiSessionControlOpen(s,e);return}if(o==="pi"&&s.verb===m.restart){await this.handlePiSessionControlRestart(e);return}if((o==="openhuman"||o==="opencode")&&s.verb===m.open){await this.handleOpenHumanSessionControlOpen(s,e);return}if(o==="codewhale"&&s.verb===m.open){await this.handleCodeWhaleSessionControlOpen(s,e);return}if(this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),s.verb===m.open){const n=s.args.trim();if(!n){this.aibotHandle.sendEventResult({event_id:e.event_id,status:"failed",code:h.cwdRequired,msg:"cwd is required",updated_at:Date.now()});return}try{const r=x.resolve(n);if(!(await T(r)).isDirectory()){this.aibotHandle.sendEventResult({event_id:e.event_id,status:"failed",code:h.invalidCwd,msg:`Path is not a directory: ${r}`,updated_at:Date.now()});return}}catch(r){const d=String(r?.code??""),a=d==="ENOENT"?`Directory does not exist: ${x.resolve(n)}`:d==="EACCES"||d==="EPERM"?"Directory is not accessible":`Invalid path: ${n}`;this.aibotHandle.sendEventResult({event_id:e.event_id,status:"failed",code:h.invalidCwd,msg:a,updated_at:Date.now()});return}}if(await this.handleSessionControlForPool(s,e),o==="acp"&&s.verb===m.stop){const r=this.bindingStore.get(e.session_id)?.cwd??"";await this.pool.removeSlot(e.session_id).catch(()=>{}),this.aibotHandle.sendEventResult({event_id:e.event_id,status:"responded",msg:`Session worker stopped for ${r}`,updated_at:Date.now()});return}P(s,e,this.sessionControlCtx(e.session_id),{...this.sessionControlSenders(),sendEventAck:()=>{}}),s.verb===m.open&&(o==="pi"?await this.deferredMgr.replayDeferredPiEvents(e.session_id,this.deferredCallbacks()):o==="openhuman"?await this.deferredMgr.replayDeferredOpenHumanEvents(e.session_id,this.deferredCallbacks()):o==="opencode"?await this.deferredMgr.replayDeferredOpenCodeEvents(e.session_id,this.deferredCallbacks()):await this.deferredMgr.replayDeferredAcpEvents(e.session_id,this.deferredCallbacks()));return}if(e.mirror_mode==="record_only"){this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendEventResult({event_id:e.event_id,status:"responded",updated_at:Date.now()});return}if(o==="claude"){if(this.isStaleClaudeEvent(e)){this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendEventResult({event_id:e.event_id,status:"failed",code:"event_stale",msg:"event is stale and will not be processed",updated_at:Date.now()});return}if(!this.bindingStore.get(e.session_id)?.cwd){this.deferredMgr.deferClaudeEvent(String(e.session_id??"").trim(),this.buildInboundEvent(e));const r=this.resolveBindingChannelKey(o);this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendMsg({event_id:e.event_id,session_id:e.session_id,msg_type:1,content:"Session binding missing.",extra:{channel_data:{[r]:{sessionBinding:{status:"missing",reason:"binding_missing",error_code:h.bindingMissing}}}},quoted_message_id:e.msg_id});return}}if(o==="codex"&&!this.bindingStore.get(e.session_id)?.cwd){this.deferredMgr.deferCodexEvent(String(e.session_id??"").trim(),this.buildInboundEvent(e));const r=this.resolveBindingChannelKey(o);this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendMsg({event_id:e.event_id,session_id:e.session_id,msg_type:1,content:"Session binding missing.",extra:{channel_data:{[r]:{sessionBinding:{status:"missing",reason:"binding_missing",error_code:h.bindingMissing}}}},quoted_message_id:e.msg_id});return}if(o==="cursor"&&!this.bindingStore.get(e.session_id)?.cwd){this.deferredMgr.deferCursorEvent(String(e.session_id??"").trim(),this.buildInboundEvent(e));const r=this.resolveBindingChannelKey(o);this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendMsg({event_id:e.event_id,session_id:e.session_id,msg_type:1,content:"Session binding missing.",extra:{channel_data:{[r]:{sessionBinding:{status:"missing",reason:"binding_missing",error_code:h.bindingMissing}}}},quoted_message_id:e.msg_id});return}if(o==="codewhale"&&!this.bindingStore.get(e.session_id)?.cwd){this.deferredMgr.deferCodeWhaleEvent(String(e.session_id??"").trim(),this.buildInboundEvent(e));const r=this.resolveBindingChannelKey(o);this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendMsg({event_id:e.event_id,session_id:e.session_id,msg_type:1,content:"Session binding missing.",extra:{channel_data:{[r]:{sessionBinding:{status:"missing",reason:"binding_missing",error_code:h.bindingMissing}}}},quoted_message_id:e.msg_id});return}if(o==="opencode"&&!this.bindingStore.get(e.session_id)?.cwd){this.deferredMgr.deferOpenCodeEvent(String(e.session_id??"").trim(),this.buildInboundEvent(e));const r=this.resolveBindingChannelKey(o);this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendMsg({event_id:e.event_id,session_id:e.session_id,msg_type:1,content:"Session binding missing.",extra:{channel_data:{[r]:{sessionBinding:{status:"missing",reason:"binding_missing",error_code:h.bindingMissing}}}},quoted_message_id:e.msg_id});return}if(o==="pi"&&!this.bindingStore.get(e.session_id)?.cwd){this.deferredMgr.deferPiEvent(String(e.session_id??"").trim(),this.buildInboundEvent(e));const r=this.resolveBindingChannelKey(o);this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendMsg({event_id:e.event_id,session_id:e.session_id,msg_type:1,content:"Session binding missing.",extra:{channel_data:{[r]:{sessionBinding:{status:"missing",reason:"binding_missing",error_code:h.bindingMissing}}}},quoted_message_id:e.msg_id});return}if(o==="openhuman"&&!this.bindingStore.get(e.session_id)?.cwd){this.deferredMgr.deferOpenHumanEvent(String(e.session_id??"").trim(),this.buildInboundEvent(e));const r=this.resolveBindingChannelKey(o);this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendMsg({event_id:e.event_id,session_id:e.session_id,msg_type:1,content:"Session binding missing.",extra:{channel_data:{[r]:{sessionBinding:{status:"missing",reason:"binding_missing",error_code:h.bindingMissing}}}},quoted_message_id:e.msg_id});return}if(o==="acp"&&!this.bindingStore.get(e.session_id)?.cwd){this.deferredMgr.deferAcpEvent(String(e.session_id??"").trim(),this.buildInboundEvent(e));const r=this.resolveBindingChannelKey(o);this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),this.aibotHandle.sendMsg({event_id:e.event_id,session_id:e.session_id,msg_type:1,content:"Session binding missing.",extra:{channel_data:{[r]:{sessionBinding:{status:"missing",reason:"binding_missing"}}}},quoted_message_id:e.msg_id});return}const i=this.buildInboundEvent(e);try{this.captureEventRuntimeConfig(i),await this.pool.deliverInboundEvent(i)}catch(n){if(this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:e.session_id,received_at:Date.now()}),n instanceof Z)this.aibotHandle.sendEventResult({event_id:e.event_id,status:"failed",msg:n.message,updated_at:Date.now()});else throw n}}async handleCodexSessionControlOpen(e,o){const t=o.session_id,s=e.args.trim(),i=()=>this.aibotHandle.sendEventAck({event_id:o.event_id,session_id:t,received_at:Date.now()}),n=(r,d)=>this.aibotHandle.sendEventResult({event_id:o.event_id,status:r,...d,updated_at:Date.now()});if(!s){i(),n("failed",{msg:"Usage: /grix open <working-directory>",code:h.cwdRequired});return}try{const r=await this.resolveCwdForBinding(s),d=this.bindingStore.get(t);if(d?.cwd){if(await this.resolveCwdForBinding(d.cwd)!==r){i(),n("failed",{msg:`Session already bound to ${d.cwd}. Rebinding is not allowed.`,code:h.rebindForbidden});return}i(),n("responded",{msg:`Session already bound to ${d.cwd}`});return}this.bindingStore.set(t,r),this.sessionBindings.set(t,r),this.deferredMgr.sendCodexDeferredReplayComposing(t,this.deferredCallbacks()),await this.bindSessionForPool(t,r),await this.deferredMgr.replayDeferredCodexEvents(t,this.deferredCallbacks(),{announceComposing:!1}),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:"ready",cwd:r}),i(),n("responded",{msg:`Session bound to ${r}`})}catch(r){i(),n("failed",{code:h.invalidCwd,msg:r instanceof Error?r.message:String(r)})}}async handleSessionControlForPool(e,o){e.verb===m.open&&await this.bindSessionForPool(o.session_id,e.args.trim())}async handleSessionControlLocalActionForPool(e){if(String(e.action_type??"")!==p.sessionControl)return;const o=e.params??{};if(String(o.verb??"").trim().toLowerCase()!==m.open)return;const s=String(o.session_id??"").trim(),i=String(o.cwd??"").trim();!s||!i||await this.bindSessionForPool(s,i)}async handleCodexSessionControlLocalActionOpen(e){const o=e.params??{},t=String(o.session_id??"").trim(),s=String(o.cwd??"").trim(),i=(n,r,d,a)=>{this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:n,...r!==void 0?{result:r}:{},...d?{error_code:d}:{},...a?{error_msg:a}:{}})};if(!t||!s){i("failed",void 0,h.cwdRequired,"session cwd is required");return}try{const n=await this.resolveCwdForBinding(s),r=this.bindingStore.get(t);if(r?.cwd){if(await this.resolveCwdForBinding(r.cwd)!==n){i("failed",void 0,h.rebindForbidden,`Session already bound to ${r.cwd}`);return}i("ok",{outcome:"opened",cwd:r.cwd});return}this.bindingStore.set(t,n),this.sessionBindings.set(t,n),this.deferredMgr.sendCodexDeferredReplayComposing(t,this.deferredCallbacks()),await this.bindSessionForPool(t,n),await this.deferredMgr.replayDeferredCodexEvents(t,this.deferredCallbacks(),{announceComposing:!1}),i("ok",{outcome:"opened",cwd:n})}catch(n){i("failed",void 0,h.invalidCwd,n instanceof Error?n.message:String(n))}}async handleCursorSessionControlLocalActionOpen(e){const o=e.params??{},t=String(o.session_id??"").trim(),s=String(o.cwd??"").trim(),i=(n,r,d,a)=>{this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:n,...r!==void 0?{result:r}:{},...d?{error_code:d}:{},...a?{error_msg:a}:{}})};if(!t||!s){i("failed",void 0,h.cwdRequired,"session cwd is required");return}try{const n=await this.resolveCwdForBinding(s),r=this.bindingStore.get(t);if(r?.cwd){if(await this.resolveCwdForBinding(r.cwd)!==n){i("failed",void 0,h.rebindForbidden,`Session already bound to ${r.cwd}`);return}i("ok",{outcome:"opened",cwd:r.cwd});return}this.bindingStore.set(t,n),this.sessionBindings.set(t,n),this.deferredMgr.sendCursorDeferredReplayComposing(t,this.deferredCallbacks()),await this.bindSessionForPool(t,n),await this.deferredMgr.replayDeferredCursorEvents(t,this.deferredCallbacks(),{announceComposing:!1}),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:"ready",cwd:n}),i("ok",{outcome:"opened",cwd:n})}catch(n){i("failed",void 0,h.invalidCwd,n instanceof Error?n.message:String(n))}}async handlePiSessionControlOpen(e,o){const t=o.session_id,s=e.args.trim(),i=()=>this.aibotHandle.sendEventAck({event_id:o.event_id,session_id:t,received_at:Date.now()}),n=(r,d)=>this.aibotHandle.sendEventResult({event_id:o.event_id,status:r,...d,updated_at:Date.now()});if(!s){i(),n("failed",{msg:"Usage: /grix open <working-directory>",code:h.cwdRequired});return}try{const r=await this.resolveCwdForBinding(s),d=this.bindingStore.get(t);if(d?.cwd){if(await this.resolveCwdForBinding(d.cwd)!==r){i(),n("failed",{msg:`Session already bound to ${d.cwd}. Rebinding is not allowed.`,code:h.rebindForbidden});return}i(),n("responded",{msg:`Session already bound to ${d.cwd}`}),this.aibotHandle.sendMsg({event_id:o.event_id,session_id:t,msg_type:1,content:`\u2705 Session already bound to \`${d.cwd}\``,quoted_message_id:o.msg_id});return}this.bindingStore.set(t,r),this.sessionBindings.set(t,r),await this.deferredMgr.replayDeferredPiEvents(t,this.deferredCallbacks()),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:"ready",cwd:r}),i(),n("responded",{msg:`Session bound to ${r}`}),this.aibotHandle.sendMsg({event_id:o.event_id,session_id:t,msg_type:1,content:`\u2705 Working directory bound: \`${r}\``,quoted_message_id:o.msg_id})}catch(r){i(),n("failed",{code:h.invalidCwd,msg:r instanceof Error?r.message:String(r)})}}async handlePiSessionControlRestart(e){const o=e.session_id,t=()=>this.aibotHandle.sendEventAck({event_id:e.event_id,session_id:o,received_at:Date.now()}),s=(r,d)=>this.aibotHandle.sendEventResult({event_id:e.event_id,status:r,...d,updated_at:Date.now()}),n=this.bindingStore.get(o)?.cwd??"";if(!n){t(),s("failed",{msg:"session binding was not found",code:h.bindingMissing});return}t(),await this.pool.removeSlot(o).catch(()=>{}),this.aibotHandle.sendUpdateBindingCard({session_id:o,worker_status:"ready",cwd:n}),s("responded",{msg:`Session worker restarted for ${n}`})}async handlePiSessionControlRestartLocalAction(e){const o=e.params??{},t=String(o.session_id??"").trim(),i=(t?this.bindingStore.get(t):void 0)?.cwd??"",n=(r,d,a,c)=>{this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:r,...d!==void 0?{result:d}:{},...a?{error_code:a}:{},...c?{error_msg:c}:{}})};if(!t){n("failed",void 0,"session_id_required","session_id is required for restart");return}if(!i){n("failed",void 0,h.bindingMissing,"Session binding missing. Open a workspace first.");return}await this.pool.removeSlot(t).catch(()=>{}),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:"ready",cwd:i}),n("ok",{outcome:"restarted",binding:{aibotSessionId:t,cwd:i,workerStatus:"ready"}})}async bindSessionForPool(e,o){const t=String(o??"").trim();if(!e||!t)return;const s=this.pool.getOrCreateSlot(e);if(!s||(s.startPromise&&await s.startPromise,!s.adapter))return;const i=s.adapter instanceof b?s.adapter:null;i?.hasSessionBinding&&(i.announceDeferredComposing(e),await i.bindSession(e,t),i.replayDeferredEvents(e))}deferredCallbacks(){return{captureEventRuntimeConfig:e=>this.captureEventRuntimeConfig(e),deliverInboundEvent:e=>this.pool.deliverInboundEvent(e),sendEventResult:(e,o,t)=>this.sendEventResultWithCleanup(e,o,t),sendSessionComposing:(e,o,t)=>{this.aibotHandle.sendSessionActivitySet({session_id:e,kind:"composing",active:o,...t?.ttlMs?{ttl_ms:t.ttlMs}:{},...t?.activity?{activity:t.activity}:{},...t?.refEventId?{ref_event_id:t.refEventId}:{},...t?.refMsgId?{ref_msg_id:t.refMsgId}:{}})}}}async handleOpenHumanSessionControlOpen(e,o){const t=()=>this.aibotHandle.sendEventAck({event_id:o.event_id,session_id:o.session_id,received_at:Date.now()});try{await this.handleSessionControlForPool(e,o),P(e,o,this.sessionControlCtx(o.session_id),this.sessionControlSenders()),this.deferredMgr.hasDeferred("openhuman",o.session_id)&&await this.deferredMgr.replayDeferredOpenHumanEvents(o.session_id,this.deferredCallbacks()),this.deferredMgr.hasDeferred("opencode",o.session_id)&&await this.deferredMgr.replayDeferredOpenCodeEvents(o.session_id,this.deferredCallbacks())}catch(s){t(),this.aibotHandle.sendEventResult({event_id:o.event_id,status:"failed",msg:s instanceof Error?s.message:String(s),updated_at:Date.now()})}}async handleCodeWhaleSessionControlOpen(e,o){const t=o.session_id,s=e.args.trim(),i=()=>this.aibotHandle.sendEventAck({event_id:o.event_id,session_id:t,received_at:Date.now()}),n=(r,d)=>this.aibotHandle.sendEventResult({event_id:o.event_id,status:r,...d,updated_at:Date.now()});if(!s){i(),n("failed",{msg:"Usage: /grix open <working-directory>",code:h.cwdRequired});return}try{const r=await this.resolveCwdForBinding(s),d=this.bindingStore.get(t);if(d?.cwd){if(await this.resolveCwdForBinding(d.cwd)!==r){i(),n("failed",{msg:`Session already bound to ${d.cwd}. Rebinding is not allowed.`,code:h.rebindForbidden});return}i(),n("responded",{msg:`Session already bound to ${d.cwd}`});return}this.bindingStore.set(t,r),this.sessionBindings.set(t,r),await this.bindSessionForPool(t,r),await this.deferredMgr.replayDeferredCodeWhaleEvents(t,this.deferredCallbacks(),{announceComposing:!1}),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:"ready",cwd:r}),i(),n("responded",{msg:`Session bound to ${r}`})}catch(r){i(),n("failed",{code:h.invalidCwd,msg:r instanceof Error?r.message:String(r)})}}async handleCodeWhaleSessionControlLocalActionOpen(e){const o=e.params??{},t=String(o.session_id??"").trim(),s=String(o.cwd??"").trim(),i=(n,r,d,a)=>{this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:n,...r!==void 0?{result:r}:{},...d?{error_code:d}:{},...a?{error_msg:a}:{}})};if(!t||!s){i("failed",void 0,h.cwdRequired,"session cwd is required");return}try{const n=await this.resolveCwdForBinding(s),r=this.bindingStore.get(t);if(r?.cwd){if(await this.resolveCwdForBinding(r.cwd)!==n){i("failed",void 0,h.rebindForbidden,`Session already bound to ${r.cwd}`);return}i("ok",{outcome:"opened",cwd:r.cwd});return}this.bindingStore.set(t,n),this.sessionBindings.set(t,n),await this.bindSessionForPool(t,n),await this.deferredMgr.replayDeferredCodeWhaleEvents(t,this.deferredCallbacks(),{announceComposing:!1}),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:"ready",cwd:n}),i("ok",{outcome:"opened",cwd:n})}catch(n){i("failed",void 0,h.invalidCwd,n instanceof Error?n.message:String(n))}}normalizeClaudeModeId(e){return String(e??"").trim().toLowerCase()===v.approval?v.approval:v.fullAuto}buildClaudeToolbarMeta(e){const o=this.bindingStore.get(e);if(!o)return;const t=M(),s=t.length>0?t[0].id:"",i=o.modelId||this.globalConfigStore?.get(this.name)?.modelId||s,n=this.normalizeClaudeModeId(o.modeId),r={model_id:i,mode_id:n,currentModelId:i,currentModeId:n,available_models:t.map(l=>({id:l.id,displayName:l.displayName}))},d=this.pool.getSlot(e),a=d?.adapter instanceof E?d.adapter.getSessionState():null;(a?.rateLimits?.fiveHour||a?.rateLimits?.sevenDay)&&(this.cachedClaudeRateLimitState=a);const c=a??this.getFreshClaudeRateLimitState();if(c){const l=c.rateLimits;l&&(l.fiveHour||l.sevenDay)&&(r.rate_limits={...l.fiveHour?{fiveHour:l.fiveHour}:{},...l.sevenDay?{sevenDay:l.sevenDay}:{},sampledAt:c.sampledAt||Date.now()});const g=c.contextWindow;g.usedPercentage!=null&&(r.context_window={usedPercentage:g.usedPercentage,remainingPercentage:g.remainingPercentage})}if(!r.rate_limits&&this.cachedProviderQuota?.success){this.isRateLimitsCacheFresh(this.cachedProviderQuotaSampledAtMs)||this.maybeQueryProviderQuota().catch(()=>{});const l=this.providerQuotaToRateLimits(this.cachedProviderQuota);l&&(r.rate_limits=l),u.info(this.name,`[toolbar-meta] provider quota fallback: hasCached=${!!this.cachedProviderQuota} fromProvider=${JSON.stringify(l)}`)}return r.rate_limits&&u.info(this.name,`[toolbar-meta] rate_limits included: ${JSON.stringify(r.rate_limits)}`),r}providerQuotaToRateLimits(e){const o=i=>{if(!i)return 0;const n=Date.parse(i);return Number.isFinite(n)?Math.floor(n/1e3):0},t=e.tiers.find(i=>i.name==="five_hour"),s=e.tiers.find(i=>i.name==="weekly_limit");if(t||s)return{...t?{fiveHour:{usedPercentage:t.usedPercent,resetsAt:o(t.resetsAt)}}:{},...s?{sevenDay:{usedPercentage:s.usedPercent,resetsAt:o(s.resetsAt)}}:{},sampledAt:this.cachedProviderQuotaSampledAtMs??Date.now()};if(e.balance){const i=e.balance;return{credit:{remaining:i.remaining,total:i.total,used:i.used,unit:i.unit},planName:e.planName,sampledAt:this.cachedProviderQuotaSampledAtMs??Date.now()}}return null}async resolveCwdForBinding(e){const o=String(e??"").trim();if(process.platform!=="win32"&&(/^[a-zA-Z]:[\\/]/.test(o)||/^\\\\/.test(o))){const i=new Error(`Specified path is not valid on this host: ${o}`);throw i.cwdErrorCode=h.invalidCwd,i}const t=x.resolve(o);let s;try{s=await T(t)}catch(i){const n=String(i?.code??"");if(n==="ENOENT"){const r=new Error(`Specified path does not exist: ${t}`);throw r.cwdErrorCode=h.invalidCwd,r}if(n==="EACCES"||n==="EPERM"){const r=new Error("Specified path is not accessible.");throw r.cwdErrorCode=h.invalidCwd,r}throw i}if(!s.isDirectory()){const i=new Error("Specified path is not a directory.");throw i.cwdErrorCode=h.invalidCwd,i}try{return await U(t)}catch{return t}}getClaudeWorkerStatus(e){const o=this.pool.getSlot(e);return o?o.state==="starting"?"starting":o.state==="stopped"?"stopped":o.adapter.getStatus().busy?"busy":"ready":"stopped"}async ensureClaudeSlotStarted(e){const o=this.pool.getOrCreateSlot(e);if(!o)throw new Error("Failed to allocate Claude session slot");if(o.startPromise){u.info(this.name,`ensureClaudeSlotStarted: awaiting startPromise for session=${e}`);const t=6e4;await Promise.race([o.startPromise,new Promise((s,i)=>setTimeout(()=>i(new Error(`ensureClaudeSlotStarted timeout (${t}ms) session=${e}`)),t))]),u.info(this.name,`ensureClaudeSlotStarted: startPromise resolved for session=${e}`)}}async handleSessionControlCommand(e,o){const t=o.session_id,s=(i,n,r)=>{this.aibotHandle.sendEventResult({event_id:o.event_id,status:i,...n?{msg:n}:{},...r?{code:r}:{},updated_at:Date.now()})};this.aibotHandle.sendEventAck({event_id:o.event_id,session_id:t,received_at:Date.now()});try{switch(e.verb){case m.open:{await H().catch(()=>{});const i=e.args.trim();if(!i){s("failed","Usage: /grix open <working-directory>",h.cwdRequired);return}let n="";try{n=await this.resolveCwdForBinding(i)}catch(d){s("failed",d instanceof Error?d.message:String(d),h.invalidCwd);return}const r=this.bindingStore.get(t);if(r?.cwd){const d=await this.resolveCwdForBinding(r.cwd);if(d!==n){s("failed","session binding cannot be changed to another working directory",h.rebindForbidden);return}this.bindingStore.ensureModeId(t,this.globalConfigStore?.get(this.name)?.modeId??v.fullAuto),this.sessionBindings.set(t,d),await this.ensureClaudeSlotStarted(t),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:this.getClaudeWorkerStatus(t),cwd:d,meta:this.buildClaudeToolbarMeta(t)}),await this.deferredMgr.replayDeferredClaudeEvents(t,this.deferredCallbacks()),s("responded",`Working directory already bound: ${d}`);return}this.bindingStore.set(t,n,{modeId:this.globalConfigStore?.get(this.name)?.modeId??v.fullAuto}),this.sessionBindings.set(t,n),await this.ensureClaudeSlotStarted(t),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:this.getClaudeWorkerStatus(t),cwd:n,meta:this.buildClaudeToolbarMeta(t)}),await this.deferredMgr.replayDeferredClaudeEvents(t,this.deferredCallbacks()),s("responded",`Session bound to ${n}`);return}case m.where:{const i=this.bindingStore.get(t);if(!i?.cwd){s("failed","session binding was not found",h.bindingMissing);return}s("responded",`Working directory: ${i.cwd}`);return}case m.status:{const i=this.bindingStore.get(t);if(!i?.cwd){s("failed","session binding was not found",h.bindingMissing);return}const n=this.normalizeClaudeModeId(i.modeId),r=this.getClaudeWorkerStatus(t);s("responded",`Status: worker=${r} mode=${n} cwd=${i.cwd}`);return}case m.stop:{const i=this.bindingStore.get(t);if(!i?.cwd){s("failed","session binding was not found",h.bindingMissing);return}await this.pool.removeSlot(t),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:"stopped",cwd:i.cwd}),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:"stopped",cwd:i.cwd,meta:this.buildClaudeToolbarMeta(t)}),s("responded",`Session worker stopped for ${i.cwd}`);return}case m.restart:{const i=this.bindingStore.get(t);if(!i?.cwd){s("failed","session binding was not found",h.bindingMissing);return}await this.pool.removeSlot(t),await this.ensureClaudeSlotStarted(t),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:this.getClaudeWorkerStatus(t),cwd:i.cwd,meta:this.buildClaudeToolbarMeta(t)}),s("responded",`Session worker restarted for ${i.cwd}`);return}case m.setMode:{const i=e.args.trim();if(!i){s("failed","Usage: /grix set_mode <mode-id>",A.modeInvalid);return}const n=this.normalizeClaudeModeId(i);if(n!==i.toLowerCase()){s("failed","set mode_id is invalid",A.modeInvalid);return}const r=this.bindingStore.get(t);if(!r?.cwd){s("failed","session binding was not found",h.bindingMissing);return}if(this.normalizeClaudeModeId(r.modeId)===n){s("responded",`Mode unchanged: ${n}`);return}this.bindingStore.setModeId(t,n),this.globalConfigStore?.set(this.name,{modeId:n}),await this.pool.removeSlot(t),await this.ensureClaudeSlotStarted(t),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:this.getClaudeWorkerStatus(t),cwd:r.cwd,meta:this.buildClaudeToolbarMeta(t)}),s("responded",`Mode set to ${n}`);return}case m.setModel:{const i=e.args.trim();if(!i){s("failed","Usage: /grix set_model <model-id>");return}const n=this.bindingStore.get(t);if(!n?.cwd){s("failed","session binding was not found",h.bindingMissing);return}if((n.modelId??"")===i){s("responded",`Model unchanged: ${i}`);return}this.bindingStore.setModelId(t,i),this.globalConfigStore?.set(this.name,{modelId:i}),await this.pool.removeSlot(t),await this.ensureClaudeSlotStarted(t),this.aibotHandle.sendUpdateBindingCard({session_id:t,worker_status:this.getClaudeWorkerStatus(t),cwd:n.cwd,meta:this.buildClaudeToolbarMeta(t)}),s("responded",`Model set to ${i}`);return}case m.listOptions:{const i=M(),n=this.bindingStore.get(t),r=i.map(a=>a.id).join(", "),d=`${v.fullAuto}, ${v.approval}`;s("responded",`Modes (current: ${this.normalizeClaudeModeId(n?.modeId)}): ${d}
|
|
10
|
+
Models (current: ${n?.modelId??"default"}): ${r}`);return}case m.exec:{const[i,...n]=e.args.trim().split(/\s+/);if(!i){s("failed","Usage: /grix exec <command> [args]",h.verbInvalid);return}const d=this.pool.getSlot(t)?.adapter;if(!d?.execCommand){s("failed","Agent does not support command execution",h.verbInvalid);return}const a=d.getSupportedCommands?.()??[];if(!a.some(c=>c.name===i)){s("failed",`Unknown command: ${i}. Supported: ${a.map(c=>c.name).join(", ")}`,h.verbInvalid);return}try{const c=await d.execCommand(i,n.join(" "),t);s(c.status==="ok"?"responded":"failed",c.message??`${i} ${c.status}`,c.status==="ok"?void 0:h.runtimeError)}catch(c){s("failed",`exec error: ${c instanceof Error?c.message:c}`,h.runtimeError)}return}default:s("failed",`Unsupported command for Claude: /grix ${e.verb}`,h.verbInvalid)}}catch(i){s("failed",i instanceof Error?i.message:String(i),h.runtimeError)}}async handleSessionControlLocalAction(e){const o=String(e.action_type??"").trim();if(o!==p.sessionControl&&o!==p.setMode&&o!=="set_mode"&&o!==p.setModel&&o!=="set_model")return!1;const t=e.params??{},s=String(t.session_id??"").trim(),i=o===p.setMode?m.setMode:o===p.setModel?m.setModel:String(t.verb??"").trim().toLowerCase();if(u.info(this.name,`handleSessionControlLocalAction verb=${i} action_id=${e.action_id} session_id=${s}`),!s)return this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:A.localActionRouteMissing,error_msg:"local action session_id is required"}),!0;const n=d=>{this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:d})},r=(d,a)=>{u.warn(this.name,`session_control local_action failed action_id=${e.action_id} session_id=${s} verb=${i} code=${d} msg=${a}`),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:d,error_msg:a})};try{switch(i){case m.open:{await H().catch(()=>{});const d=String(t.cwd??"").trim();if(!d)return r(h.cwdRequired,"session control cwd is required"),!0;u.info(this.name,`handleSessionControlLocalAction open cwd=${d} session_id=${s}`);const a=await this.resolveCwdForBinding(d),c=this.bindingStore.get(s);if(c?.cwd){const l=await this.resolveCwdForBinding(c.cwd);return l!==a?(r(h.rebindForbidden,"session binding cannot be changed to another working directory"),!0):(this.bindingStore.ensureModeId(s,this.globalConfigStore?.get(this.name)?.modeId??v.fullAuto),this.sessionBindings.set(s,l),await this.ensureClaudeSlotStarted(s),this.aibotHandle.sendUpdateBindingCard({session_id:s,worker_status:this.getClaudeWorkerStatus(s),cwd:l,meta:this.buildClaudeToolbarMeta(s)}),await this.deferredMgr.replayDeferredClaudeEvents(s,this.deferredCallbacks()),n({outcome:"already_bound",binding:{cwd:l,mode_id:this.normalizeClaudeModeId(this.bindingStore.get(s)?.modeId)}}),!0)}return this.bindingStore.set(s,a,{modeId:this.globalConfigStore?.get(this.name)?.modeId??v.fullAuto}),this.sessionBindings.set(s,a),await this.ensureClaudeSlotStarted(s),this.aibotHandle.sendUpdateBindingCard({session_id:s,worker_status:this.getClaudeWorkerStatus(s),cwd:a,meta:this.buildClaudeToolbarMeta(s)}),await this.deferredMgr.replayDeferredClaudeEvents(s,this.deferredCallbacks()),n({outcome:"opened",binding:{cwd:a,mode_id:this.normalizeClaudeModeId(this.bindingStore.get(s)?.modeId)}}),!0}case m.status:case m.where:{const d=this.bindingStore.get(s);return d?.cwd?(n({outcome:i,binding:{cwd:d.cwd,mode_id:this.normalizeClaudeModeId(d.modeId),worker_status:this.getClaudeWorkerStatus(s)}}),!0):(r(h.bindingMissing,"session binding was not found"),!0)}case m.stop:{const d=this.bindingStore.get(s);return d?.cwd?(await this.pool.removeSlot(s),n({outcome:"stopped",binding:{cwd:d.cwd,mode_id:this.normalizeClaudeModeId(d.modeId),worker_status:"stopped"}}),!0):(r(h.bindingMissing,"session binding was not found"),!0)}case m.restart:{const d=this.bindingStore.get(s);return d?.cwd?(await this.pool.removeSlot(s),await this.ensureClaudeSlotStarted(s),this.aibotHandle.sendUpdateBindingCard({session_id:s,worker_status:this.getClaudeWorkerStatus(s),cwd:d.cwd,meta:this.buildClaudeToolbarMeta(s)}),n({outcome:"restarted",binding:{cwd:d.cwd,mode_id:this.normalizeClaudeModeId(d.modeId)}}),!0):(r(h.bindingMissing,"session binding was not found"),!0)}case m.setMode:{const d=String(t.mode_id??t.modeId??"").trim();if(!d)return r(A.modeInvalid,"set mode_id is invalid"),!0;const a=this.normalizeClaudeModeId(d);if(a!==d.toLowerCase())return r(A.modeInvalid,"set mode_id is invalid"),!0;const c=this.bindingStore.get(s);return c?.cwd?(this.normalizeClaudeModeId(c.modeId)!==a&&(this.bindingStore.setModeId(s,a),this.globalConfigStore?.set(this.name,{modeId:a}),await this.pool.removeSlot(s),await this.ensureClaudeSlotStarted(s),this.aibotHandle.sendUpdateBindingCard({session_id:s,worker_status:this.getClaudeWorkerStatus(s),cwd:c.cwd,meta:this.buildClaudeToolbarMeta(s)})),n({outcome:"mode_set",mode_id:a,binding:{cwd:c.cwd,mode_id:a}}),!0):(r(h.bindingMissing,"session binding was not found"),!0)}case m.setModel:{const d=String(t.model_id??t.modelId??"").trim();if(!d)return r("set_model_invalid","model_id is required"),!0;const a=this.bindingStore.get(s);return a?.cwd?((a.modelId??"")!==d&&(this.bindingStore.setModelId(s,d),this.globalConfigStore?.set(this.name,{modelId:d}),await this.pool.removeSlot(s),await this.ensureClaudeSlotStarted(s),this.aibotHandle.sendUpdateBindingCard({session_id:s,worker_status:this.getClaudeWorkerStatus(s),cwd:a.cwd,meta:this.buildClaudeToolbarMeta(s)})),n({outcome:"model_set",model_id:d,binding:{cwd:a.cwd,model_id:d}}),!0):(r(h.bindingMissing,"session binding was not found"),!0)}case m.listOptions:{const d=M(),a=this.pool.getSlot(s);return n({modes:[v.fullAuto,v.approval],currentModeId:this.normalizeClaudeModeId(this.bindingStore.get(s)?.modeId),models:d.map(c=>({modelId:c.id,name:c.displayName})),currentModelId:this.bindingStore.get(s)?.modelId??"",available_models:d.map(c=>({id:c.id,displayName:c.displayName})),agent_commands:a?.adapter?.getSupportedCommands?.()??[]}),!0}case m.exec:{const[d,...a]=String(t.args??"").trim().split(/\s+/);if(!d)return r(h.verbInvalid,"Usage: exec <command> [args]"),!0;await this.ensureClaudeSlotStarted(s);const l=this.pool.getSlot(s)?.adapter;if(!l?.execCommand)return r(h.verbInvalid,"Agent does not support command execution"),!0;const g=l.getSupportedCommands?.()??[];if(!g.some(_=>_.name===d))return r(h.verbInvalid,`Unknown command: ${d}. Supported: ${g.map(_=>_.name).join(", ")}`),!0;try{const _=await l.execCommand(d,a.join(" "),s);_.status==="ok"?n({outcome:"exec",command:d,message:_.message,data:_.data}):r(h.runtimeError,_.message??`${d} failed`)}catch(_){r(h.runtimeError,`exec error: ${_ instanceof Error?_.message:_}`)}return!0}default:return r(h.verbInvalid,`session control verb ${i} is not supported`),!0}}catch(d){const a=d instanceof Error&&d.cwdErrorCode?d.cwdErrorCode:h.runtimeError;return u.error(this.name,`handleSessionControlLocalAction error verb=${i} session_id=${s}: ${d instanceof Error?d.message:d}`),r(a,d instanceof Error?d.message:String(d)),!0}}async handleEventCancel(e){const{event_id:o,session_id:t}=e;if(u.info(this.name,`handleEventCancel start event_id=${o} session_id=${t}`),this.pool.cancelEvent(o,t)){if(!(this.pool.getSlot(t)?.adapter?.getActiveEventIds().includes(o)??!1)){this.aibotHandle.sendEventCancelResult({event_id:o,accepted:!0,final_state:"canceled"}),this.pushQueueSnapshotForSession(t);return}await this.waitForEventDone(o,t,15e3),this.aibotHandle.sendEventCancelResult({event_id:o,accepted:!0,final_state:"canceled"}),this.pushQueueSnapshotForSession(t);return}this.aibotHandle.sendEventCancelResult({event_id:o,accepted:!1,reason:"event not found or not cancelable"})}waitForEventDone(e,o,t){return new Promise(s=>{const i=this.pool.getSlot(o);if(!i?.adapter){s();return}const n=setTimeout(()=>{i.adapter.removeListener("eventDone",r),s()},t),r=d=>{d===e&&(clearTimeout(n),i.adapter.removeListener("eventDone",r),s())};i.adapter.on("eventDone",r)})}handleAibotStop(e){this.aibotHandle.sendEventStopAck({stop_id:e.stop_id,event_id:e.event_id,accepted:!0,updated_at:Date.now()});const o=this.pool.getSlot(e.session_id),t=o?.adapter?.getStatus().busy??!1;if(o?.adapter&&t){const s=i=>{i===e.event_id&&(o.adapter.removeListener("eventDone",s),this.aibotHandle.sendEventStopResult({stop_id:e.stop_id,event_id:e.event_id,status:"stopped",updated_at:Date.now()}))};o.adapter.on("eventDone",s),this.pool.deliverStopEvent(e.event_id,e.session_id)}else this.pool.deliverStopEvent(e.event_id,e.session_id),this.aibotHandle.sendEventStopResult({stop_id:e.stop_id,event_id:e.event_id,status:"stopped",updated_at:Date.now()})}handleAibotRevoke(e){if(!e.event_id||!this.revokeHandler.checkAndTrack(e.event_id))return;const o=e.event_id,t=e.session_id;if(t&&this.pool.cancelEvent(o,t)){(this.pool.getSlot(t)?.adapter?.getActiveEventIds().includes(o)??!1)||this.sendEventResultWithCleanup(o,"canceled","revoked");return}if(this.deferredMgr.removeEvent(o)){this.aibotHandle.sendEventResult({event_id:o,status:"canceled",msg:"revoked",updated_at:Date.now()});return}this.pool.deliverStopEvent(o,t||void 0)}async handleAibotLocalAction(e){const o=e.action_type??"",t=String((e.params??{}).session_id??""),s=String((e.params??{}).verb??"").trim().toLowerCase();u.debug(this.name,`local_action received action_type=${o} verb=${s||"-"} action_id=${e.action_id} session_id=${t}`);const i=(this.config.adapterType??"acp")==="claude";if(o===p.sessionControl&&s===m.exec&&await this.handleSessionControlLocalAction(e)||i&&await this.handleSessionControlLocalAction(e))return;if(o===p.sessionControl){const a=this.config.adapterType??"acp",c=a==="codex",l=a==="pi",g=String((e.params??{}).verb??"").trim().toLowerCase();if(c&&g===m.open){await this.handleCodexSessionControlLocalActionOpen(e);return}if(a==="cursor"&&g===m.open){await this.handleCursorSessionControlLocalActionOpen(e);return}if(c&&g==="restart"){const R=this.bindingStore.get(t)?.cwd??"";await this.pool.removeSlot(t).catch(()=>{}),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{outcome:"restarted",binding:{aibotSessionId:t,cwd:R,workerStatus:"ready"}}});return}if(l&&g===m.open){L(e,this.sessionControlCtx(t),this.sessionControlSenders()),await this.deferredMgr.replayDeferredPiEvents(t,this.deferredCallbacks());return}if(l&&g===m.restart){await this.handlePiSessionControlRestartLocalAction(e);return}const _=a==="openhuman",f=a==="opencode";if((_||f)&&g===m.open){L(e,this.sessionControlCtx(t),this.sessionControlSenders()),_&&await this.deferredMgr.replayDeferredOpenHumanEvents(t,this.deferredCallbacks()),f&&await this.deferredMgr.replayDeferredOpenCodeEvents(t,this.deferredCallbacks());return}if(a==="codewhale"&&g===m.open){await this.handleCodeWhaleSessionControlLocalActionOpen(e);return}if(a==="acp"&&g===m.stop){const R=this.bindingStore.get(t)?.cwd??"";await this.pool.removeSlot(t).catch(()=>{}),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{outcome:"stopped",binding:{aibotSessionId:t,cwd:R,workerStatus:"stopped"}}});return}try{await this.handleSessionControlLocalActionForPool(e)}catch(S){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:h.runtimeError,error_msg:S instanceof Error?S.message:String(S)});return}L(e,this.sessionControlCtx(t),this.sessionControlSenders()),g===m.open&&(a==="pi"?await this.deferredMgr.replayDeferredPiEvents(t,this.deferredCallbacks()):a==="openhuman"?await this.deferredMgr.replayDeferredOpenHumanEvents(t,this.deferredCallbacks()):a==="opencode"?await this.deferredMgr.replayDeferredOpenCodeEvents(t,this.deferredCallbacks()):await this.deferredMgr.replayDeferredAcpEvents(t,this.deferredCallbacks()));return}if(o==="file_list"){const a=t?this.bindingStore.get(t)?.cwd:void 0,c=e.params??{},l=String(c.parent_id??"").trim(),g=Array.isArray(c.allowed_extensions)?c.allowed_extensions.filter(f=>typeof f=="string").map(f=>f.trim()).filter(f=>f.length>0):[],_=await ie({parent_id:l||null,session_id:t,show_hidden:!!c.show_hidden,allowed_extensions:g},{resolveCwd:()=>a??this.config.agent.cwd??process.cwd(),fallbackDir:I()});this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:_.status,..._.result?{result:_.result}:{},..._.error_code?{error_code:_.error_code}:{},..._.error_msg?{error_msg:_.error_msg}:{}});return}if(o==="create_folder"){const a=t?this.bindingStore.get(t)?.cwd:void 0,c=String((e.params??{}).parent_id??"").trim(),l=String((e.params??{}).name??"").trim(),g=await ne({parent_id:c||null,name:l,session_id:t},{resolveCwd:()=>a??this.config.agent.cwd??process.cwd(),fallbackDir:I()});this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:g.status,...g.result?{result:g.result}:{},...g.error_code?{error_code:g.error_code}:{},...g.error_msg?{error_msg:g.error_msg}:{}});return}if(o===p.getSessionUsage){await this.handleGetSessionUsage(e,t);return}if(o===p.getRateLimits){await this.handleGetRateLimits(e,t);return}if(i&&o===p.threadCompact){await this.handleClaudeThreadCompact(e,t);return}if(o==="connector_rollback"){await this.handleConnectorRollback(e);return}if(o==="connector_upgrade_push"){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{accepted:!0}}),this.upgradeTrigger?.();return}if(o===p.getAgentGlobalConfig){const a=this.globalConfigStore?.get(this.name);this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{agentName:this.name,...a?{config:a}:{config:null}}});return}const n=this.config.adapterType??"acp",r=(n==="codex"||n==="cursor"||n==="pi"||n==="openhuman"||n==="opencode")&&!!t&&!!this.bindingStore.get(t)?.cwd,d=await this.pool.deliverLocalAction(e,{autoCreateSlot:r});if(d.handled){if(d.kind==="set_mode"){const a=String((e.params??{}).mode_id??"");a&&(n==="codex"?this.globalConfigStore?.set(this.name,{codexModeId:a}):n==="cursor"||n==="claude"?this.globalConfigStore?.set(this.name,{modeId:a}):this.globalConfigStore?.set(this.name,{acpInitialMode:a}))}else if(d.kind==="set_model"){const a=String((e.params??{}).model_id??"");a&&(n==="codex"?this.globalConfigStore?.set(this.name,{codexModelId:a}):this.globalConfigStore?.set(this.name,{modelId:a}))}else if(d.kind==="set_reasoning_effort"){const a=String((e.params??{}).reasoning_effort??(e.params??{}).reasoning_eff??(e.params??{}).effort??"");a&&this.globalConfigStore?.set(this.name,{codexReasoningEffort:a})}else if(d.kind==="set_sandbox_mode"){const a=String((e.params??{}).sandbox_mode??(e.params??{}).sandboxMode??"");if(a){const c=a==="default"?void 0:a;this.globalConfigStore?.set(this.name,{codexSandboxMode:c})}}return}if((n==="codex"||n==="cursor"||n==="pi"||n==="openhuman"||n==="opencode")&&t&&!this.bindingStore.get(t)?.cwd){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:h.bindingMissing,error_msg:"Session binding missing. Open a workspace first."});return}if(o==="set_mode"||o==="set_model"){const a=this.sessionControlCtx(t),c=this.sessionControlSenders(),l=this.pool.getSlot(t)?.adapter;if(o==="set_mode"){const g=String((e.params??{}).mode_id??"");if(!g||!a.isAcpAlive){u.warn(this.name,`set_mode rejected: modeId=${g} acpAlive=${a.isAcpAlive}`),c.sendLocalActionResult(e.action_id,"failed",void 0,"set_mode_invalid","mode_id is required and ACP session must be active");return}const _=await a.setMode(g);u.info(this.name,`set_mode result: modeId=${g} ok=${_}`),_&&this.globalConfigStore?.set(this.name,{acpInitialMode:g});const f=l instanceof b?l.buildToolbarContext(_?"mode_set":"mode_set_failed"):null,k=_?f??{outcome:"mode_set",modeId:g}:f;c.sendLocalActionResult(e.action_id,_?"ok":"failed",k,_?void 0:"set_mode_failed",_?void 0:`failed to set mode ${g}`)}else{const g=String((e.params??{}).model_id??"");if(!g||!a.isAcpAlive){u.warn(this.name,`set_model rejected: modelId=${g} acpAlive=${a.isAcpAlive}`),c.sendLocalActionResult(e.action_id,"failed",void 0,"set_model_invalid","model_id is required and ACP session must be active");return}const _=await a.setModel(g);u.info(this.name,`set_model result: modelId=${g} ok=${_}`),_&&this.globalConfigStore?.set(this.name,{modelId:g});const f=l instanceof b?l.buildToolbarContext(_?"model_set":"model_set_failed"):null,k=_?f??{outcome:"model_set",modelId:g}:f;c.sendLocalActionResult(e.action_id,_?"ok":"failed",k,_?void 0:"set_model_failed",_?void 0:`failed to set model ${g}`)}return}this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"unsupported_local_action",error_msg:`action type ${o} is not supported`})}async handleGetSessionUsage(e,o){if(!o){u.warn(this.name,`[usage] get_session_usage rejected: no session_id in params, action_id=${e.action_id}`),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"session_id_required",error_msg:"session_id is required for get_session_usage"});return}const t=this.config.adapterType??"acp",s=this.bindingStore.get(o),i=s?.cwd??this.config.agent.cwd??process.cwd();u.info(this.name,`[usage] get_session_usage action_id=${e.action_id} session_id=${o} adapterType=${t} hasBinding=${!!s} cwd=${i}`);let n=null,r,d;switch(t){case"claude":{if(r=s?.claudeSessionId,!r){u.warn(this.name,`[usage] no claude binding for session_id=${o}, action_id=${e.action_id}`),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"no_binding",error_msg:"No Claude session binding found"});return}u.info(this.name,`[usage] parsing claude usage: claudeSessionId=${r} cwd=${i}`),n=await N(r,i),d="claude";break}case"acp":default:{if(r=s?.acpSessionId,!r){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"no_binding",error_msg:"No ACP session binding found"});return}n=await z(r,i),d=t,(!d||d==="acp")&&(d="acp");break}case"codex":{if(r=s?.codexThreadId,!r){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"no_binding",error_msg:"No Codex thread binding found"});return}n=await G(r),d="codex";break}case"pi":{if(r=s?.piSessionPath,!r){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"no_binding",error_msg:"No Pi session path binding found"});return}n=await V(r),d="pi";break}case"cursor":{const c=this.pool.getSlot(o)?.adapter;if(!(c instanceof $)){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"no_binding",error_msg:"No Cursor session found"});return}const l=c.getUsageSnapshot(o);if(!l){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"usage_not_found",error_msg:"No usage data found for this session"});return}this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{sessionId:o,adapterType:"cursor",models:[{modelId:this.bindingStore.get(o)?.modelId??this.globalConfigStore?.get(this.name)?.modelId??"auto",turns:l.turns,input:l.total.input,output:l.total.output,cacheRead:l.total.cacheRead,cacheWrite:l.total.cacheWrite}],total:{input:l.total.input,output:l.total.output,cacheRead:l.total.cacheRead,cacheWrite:l.total.cacheWrite},turns:l.turns,sampledAt:l.sampledAt}});return}case"codewhale":{const c=this.pool.getSlot(o)?.adapter;if(!(c instanceof D)){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"no_binding",error_msg:"No CodeWhale session found"});return}const l=c.getUsageSnapshot();if(!l){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"usage_not_found",error_msg:"No usage data found for this session"});return}this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{sessionId:o,adapterType:"codewhale",models:[{modelId:this.bindingStore.get(o)?.modelId??"codewhale",turns:l.turns,input:l.total.input,output:l.total.output}],total:{input:l.total.input,output:l.total.output},turns:l.turns,sampledAt:l.sampledAt}});return}}if(!n){u.info(this.name,`[usage] no usage data found: session_id=${o} adapterSessionId=${r} adapterType=${d}`),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"usage_not_found",error_msg:"No usage data found for this session"});return}u.info(this.name,`[usage] result ok: session_id=${o} adapterSessionId=${r} turns=${n.turns} models=${n.models.length}`),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{sessionId:r,adapterType:d,models:n.models,total:n.total,turns:n.turns,sampledAt:new Date().toISOString()}})}async handleClaudeThreadCompact(e,o){if(!o){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"session_id_required",error_msg:"session_id is required for thread_compact"});return}if(!this.bindingStore.get(o)?.cwd){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:h.bindingMissing,error_msg:"session binding was not found"});return}try{await this.ensureClaudeSlotStarted(o);const i=this.pool.getSlot(o)?.adapter;if(!i?.execCommand){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:h.runtimeError,error_msg:"Agent does not support command execution"});return}u.info(this.name,`thread_compact session_id=${o} action_id=${e.action_id}`);const n=await i.execCommand("compact","",o);n.status==="ok"?this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{outcome:"compacted",message:n.message,data:n.data}}):this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:h.runtimeError,error_msg:n.message??"compact failed"})}catch(s){u.warn(this.name,`thread_compact error session_id=${o}: ${s instanceof Error?s.message:s}`),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:h.runtimeError,error_msg:s instanceof Error?s.message:String(s)})}}async handleGetRateLimits(e,o){const t=this.config.adapterType??"acp";if(this.config.aibot.clientType==="kiro"){if(this.isRateLimitsCacheFresh(this.cachedProviderQuotaSampledAtMs)&&this.cachedProviderQuota){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{adapterType:"acp",available:!0,cached:!0,sampledAt:this.cachedProviderQuotaSampledAtMs,rateLimits:null,contextWindow:null,tokenUsage:null,providerQuota:this.cachedProviderQuota}});return}try{const i=await B();this.cachedProviderQuota=i,this.cachedProviderQuotaSampledAtMs=Date.now(),u.info(this.name,`[rate-limits] kiro quota queried: success=${i.success}`+(i.balance?` balance=${i.balance.remaining} ${i.balance.unit}`:"")+(i.error?` error=${i.error}`:"")),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{adapterType:"acp",available:!0,cached:!1,sampledAt:this.cachedProviderQuotaSampledAtMs,rateLimits:null,contextWindow:null,tokenUsage:null,providerQuota:i}})}catch(i){u.warn(this.name,`[rate-limits] kiro quota query failed: ${i instanceof Error?i.message:String(i)}`),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{adapterType:"acp",available:!1,cached:!1,sampledAt:null,rateLimits:null,contextWindow:null,tokenUsage:null}})}return}switch(t){case"codex":{const s=this.maybeQueryProviderQuota(),i=this.getFreshCodexGlobalRateLimitCache();if(i.hasData){i.rateLimits?u.info(this.name,`[rate-limits] codex cached: primary=${i.rateLimits.primary.usedPercent.toFixed(1)}% resetsAt=${i.rateLimits.primary.resetsAt} secondary=${i.rateLimits.secondary.usedPercent.toFixed(1)}% resetsAt=${i.rateLimits.secondary.resetsAt}`):u.info(this.name,`[rate-limits] codex cached context/token only: hasContext=${!!i.contextWindow} hasToken=${!!i.tokenUsage}`);const c=await s;this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{adapterType:"codex",available:i.hasData||!!c,cached:!0,sampledAt:i.sampledAt,rateLimits:i.rateLimits,contextWindow:i.contextWindow,tokenUsage:i.tokenUsage,providerQuota:c}});return}const n=this.pool.getAllSlots().find(c=>c.state==="ready"&&c.adapter);if(n&&(u.info(this.name,`[rate-limits] codex reuse existing slot: session=${n.sessionId}`),(await n.adapter.handleLocalAction?.(e))?.handled))return;const r=this.resolveRateLimitWakeSessionId(o,t);if(r){const c=await this.wakeRateLimitSlot(r,t);if(c?.adapter&&(await c.adapter.handleLocalAction?.(e))?.handled)return}const d=await s,a=!!d;u.info(this.name,`[rate-limits] codex no native data, providerQuota=${d?d.provider:"none"} available=${a}`),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{adapterType:"codex",available:a,cached:!1,sampledAt:null,rateLimits:null,contextWindow:null,tokenUsage:null,providerQuota:d}});return}case"claude":{const s=this.maybeQueryProviderQuota(),i=this.getFreshClaudeRateLimitState();if(i){const c=i.rateLimits,l=await s;u.info(this.name,`[rate-limits] claude global cached: sampledAt=${i.sampledAt} hasRateLimits=${!!c}`+(l?` providerQuota=${l.provider}`:"")),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{adapterType:"claude",available:!0,cached:!0,sampledAt:i.sampledAt,rateLimits:i.rateLimits??null,contextWindow:i.contextWindow,tokenUsage:null,providerQuota:l}});return}let n=this.pool.getAllSlots().find(c=>c.state==="ready"&&c.adapter instanceof E)??null;if(!n){const c=this.resolveRateLimitWakeSessionId(o,t);c&&(n=await this.wakeRateLimitSlot(c,t))}u.info(this.name,`[rate-limits] handleGetRateLimits: session_id=${o} adapterType=claude hasSlot=${!!n} hasAdapter=${!!n?.adapter}`);const r=n?.adapter,d=r instanceof E?r.getSessionState():null,a=await s;if(d){(d.rateLimits?.fiveHour||d.rateLimits?.sevenDay||d.contextWindow?.usedPercentage!=null)&&(this.cachedClaudeRateLimitState=d);const c=this.getFreshClaudeRateLimitState(),l=c?.rateLimits&&!d.rateLimits?{...d,rateLimits:c.rateLimits}:d,g=l.rateLimits;u.info(this.name,`[rate-limits] claude global state: sampledAt=${l.sampledAt} hasRateLimits=${!!g}`+(g?` fiveHour=${g.fiveHour?.usedPercentage??"n/a"}% resetsAt=${g.fiveHour?.resetsAt??"n/a"} sevenDay=${g.sevenDay?.usedPercentage??"n/a"}% resetsAt=${g.sevenDay?.resetsAt??"n/a"}`:"")+(c?.rateLimits&&!d.rateLimits?" source=live+cached-fallback":" source=live")+(a?` providerQuota=${a.provider}`:"")),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{adapterType:"claude",available:!0,cached:!1,sampledAt:l.sampledAt,rateLimits:l.rateLimits??null,contextWindow:l.contextWindow,tokenUsage:null,providerQuota:a}})}else u.info(this.name,`[rate-limits] claude no global state: hasAdapter=${!!r} adapterType=${r?.constructor?.name??"n/a"}`+(a?` providerQuota=${a.provider}`:"")),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{adapterType:"claude",available:!!a,cached:!1,sampledAt:null,rateLimits:null,contextWindow:null,tokenUsage:null,providerQuota:a}});return}case"cursor":{const i=(o?this.pool.getSlot(o):null)?.adapter,n=i instanceof $?i.getRateLimitsSnapshot():{adapterType:"cursor",available:!1,cached:!1,sampledAt:null,rateLimits:null,contextWindow:null,tokenUsage:null};this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:n});return}default:{const s=this.config.providerBaseUrl,i=this.config.providerApiKey;if(!s||!i){u.info(this.name,`[rate-limits] no provider config for adapterType=${t}`),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{adapterType:t,available:!1,cached:!1,sampledAt:null,rateLimits:null,contextWindow:null,tokenUsage:null}});return}if(this.isRateLimitsCacheFresh(this.cachedProviderQuotaSampledAtMs)&&this.cachedProviderQuota){u.info(this.name,`[rate-limits] provider quota cached: provider=${this.cachedProviderQuota.provider} success=${this.cachedProviderQuota.success}`),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{adapterType:t,available:!0,cached:!0,sampledAt:this.cachedProviderQuotaSampledAtMs,rateLimits:null,contextWindow:null,tokenUsage:null,providerQuota:this.cachedProviderQuota}});return}try{const r=await y(s,i);this.cachedProviderQuota=r,this.cachedProviderQuotaSampledAtMs=Date.now(),u.info(this.name,`[rate-limits] provider quota queried: provider=${r.provider} success=${r.success}`+(r.tiers.length>0?` tiers=${r.tiers.map(d=>`${d.name}=${d.usedPercent}%`).join(",")}`:"")+(r.balance?` balance=${r.balance.remaining} ${r.balance.unit}`:"")+(r.error?` error=${r.error}`:"")),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{adapterType:t,available:!0,cached:!1,sampledAt:this.cachedProviderQuotaSampledAtMs,rateLimits:null,contextWindow:null,tokenUsage:null,providerQuota:r}})}catch(r){u.warn(this.name,`[rate-limits] provider quota query failed: ${r instanceof Error?r.message:String(r)}`),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{adapterType:t,available:!1,cached:!1,sampledAt:null,rateLimits:null,contextWindow:null,tokenUsage:null}})}return}}}resolveRateLimitWakeSessionId(e,o){const t=String(e??"").trim(),s=this.bindingStore.getMostRecentlyUpdatedSessionId({requireCwd:!0});return s?t&&t===s?t:o==="codex"||o==="claude"?(t&&t!==s&&u.info(this.name,`[rate-limits] ${o} remap wake session: requested=${t} use_recent=${s}`),s):t&&t!==s?(u.info(this.name,`[rate-limits] skip wake slot: session=${t} adapterType=${o} reason=not_recent_session recent=${s}`),null):t||null:(u.info(this.name,`[rate-limits] skip wake slot: adapterType=${o} reason=no_recent_binding`),null)}async wakeRateLimitSlot(e,o){const t=String(e??"").trim();if(!t)return null;if(!this.bindingStore.get(t)?.cwd)return u.info(this.name,`[rate-limits] skip wake slot: session=${t} adapterType=${o} reason=binding_cwd_missing`),null;try{const i=this.pool.getOrCreateSlot(t);if(!i)return u.info(this.name,`[rate-limits] skip wake slot: session=${t} adapterType=${o} reason=slot_unavailable`),null;if(i.startPromise){const n=o==="claude"?6e4:2e4;await Promise.race([i.startPromise,new Promise((r,d)=>setTimeout(()=>d(new Error(`wake rate-limits slot timeout (${n}ms)`)),n))])}return u.info(this.name,`[rate-limits] wake slot success: session=${t} adapterType=${o}`),this.pool.getSlot(t)??i}catch(i){return u.warn(this.name,`[rate-limits] wake slot failed: session=${t} adapterType=${o} err=${i instanceof Error?i.message:String(i)}`),null}}sessionControlCtx(e){const o=this.pool.getSlot(e),t=o?.adapter instanceof b?o.adapter:null;return{getCwd:()=>this.bindingStore.get(e)?.cwd??this.config.agent.cwd??process.cwd(),getSessionBindings:()=>{if(t)return t.getSessionBindings();const s=this.sessionBindings;if(e&&!s.has(e)){const i=this.bindingStore.get(e);i?.cwd&&s.set(e,i.cwd)}return s},getStatus:()=>this.getStatus(),isAcpAlive:!!t?.isAlive(),getAcpSessionOptions:()=>t?.acpSessionOptions??null,setMode:s=>t?t.setMode(s):Promise.resolve(!1),setModel:s=>t?t.setModel(s):Promise.resolve(!1),getPendingApproval:s=>{const i=t?.pendingApprovalEntries.get(s);return i?{requestId:i}:void 0},deletePendingApproval:s=>t?.pendingApprovalEntries.delete(s)??!1,respondPermission:(s,i)=>(t&&t.respondToPermission(s,i),Promise.resolve()),onSessionBound:(s,i)=>{this.bindingStore.set(s,i)},onSessionUnbound:s=>{const n=this.bindingStore.get(s)?.cwd??"";this.bindingStore.delete(s),this.sessionBindings.delete(s),this.deferredMgr.clearSession(s),n&&this.aibotHandle.sendUpdateBindingCard({session_id:s,worker_status:"stopped",cwd:n})},cancelActiveRun:()=>o?.adapter?.cancel("")??Promise.resolve(),onModeSet:s=>{(this.config.adapterType??"acp")==="codex"?this.globalConfigStore?.set(this.name,{codexModeId:s}):this.globalConfigStore?.set(this.name,{acpInitialMode:s})},onModelSet:s=>{(this.config.adapterType??"acp")==="codex"?this.globalConfigStore?.set(this.name,{codexModelId:s}):this.globalConfigStore?.set(this.name,{modelId:s})}}}sessionControlSenders(){return{sendEventAck:(e,o)=>this.aibotHandle.sendEventAck({event_id:e,session_id:o,received_at:Date.now()}),sendEventResult:(e,o,t)=>this.aibotHandle.sendEventResult({event_id:e,status:o,...t?.msg?{msg:t.msg}:{},...t?.code?{code:t.code}:{},updated_at:Date.now()}),sendLocalActionResult:(e,o,t,s,i)=>this.aibotHandle.sendLocalActionResult({action_id:e,status:o,...t?{result:t}:{},...s?{error_code:s}:{},...i?{error_msg:i}:{}})}}resolveBindingChannelKey(e){switch(e){case"claude":return"grix-claude";case"codex":return"codex";case"cursor":return"cursor";case"pi":return"pi";case"openhuman":return"openhuman";case"codewhale":return"codewhale";case"opencode":return"opencode";default:return e}}finalizeThinking(e,o){this.sendCtrl.finalizeThinking(e,o)}logInboundConversation(e){const o=String(e.session_id??"").trim();o&&this.conversationLog?.logInbound(o,{event_id:e.event_id,msg_id:e.msg_id,sender_id:e.sender_id,msg_type:e.msg_type,content:e.content??""})}buildInboundEvent(e){return{event_id:e.event_id,session_id:e.session_id,thread_id:e.thread_id,sender_id:e.sender_id,msg_id:e.msg_id,msg_type:e.msg_type,content:e.content??"",quoted_message_id:e.quoted_message_id,context_messages_json:e.context_messages?JSON.stringify(e.context_messages):void 0,extra_json:e.extra?JSON.stringify(e.extra):void 0,connector_runtime_config:{response_delivery:this.sendCtrl.getGlobalRuntimeConfig().responseDelivery,tool_events:this.sendCtrl.getGlobalRuntimeConfig().toolEvents,thinking_events:this.sendCtrl.getGlobalRuntimeConfig().thinkingEvents},session_type:e.session_type,created_at:e.created_at}}isStaleClaudeEvent(e){const o=Number(e.created_at);return!Number.isFinite(o)||o<=0?!1:Date.now()-o>he}async handleConnectorRollback(e){const o=String((e.params??{}).target_version??"").trim(),t=String((e.params??{}).reason??"server_initiated");if(!o){this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"MISSING_TARGET_VERSION",error_msg:"target_version is required for connector_rollback"});return}u.info(this.name,`connector_rollback: target=${o} reason=${t}`);try{const{npmInstall:s,writePending:i,removePending:n,upgradeLog:r}=await import("../core/upgrade/npm-upgrader.js"),{resolveClientVersion:d}=await import("../core/util/client-version.js"),a=d();r(`server rollback: ${a} -> ${o} reason=${t}`),i(a,o),await s("grix-connector",o),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"ok",result:{rolled_back_to:o}}),process.kill(process.pid,"SIGTERM")}catch(s){try{const{removePending:n}=await import("../core/upgrade/npm-upgrader.js");n()}catch{}const i=s instanceof Error?s.message:String(s);u.error(this.name,`connector_rollback failed: ${i}`),this.aibotHandle.sendLocalActionResult({action_id:e.action_id,status:"failed",error_code:"ROLLBACK_FAILED",error_msg:i})}}setUpgradeTrigger(e){this.upgradeTrigger=e}}export{Ge as AgentInstance};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{log as a}from"../core/log/index.js";const u=2,v=3e3;class h{name;deferredClaudeEvents=new Map;deferredClaudeReplayRetries=new Map;deferredCodexEvents=new Map;deferredCursorEvents=new Map;deferredAcpEvents=new Map;deferredPiEvents=new Map;deferredOpenHumanEvents=new Map;deferredCodeWhaleEvents=new Map;deferredOpenCodeEvents=new Map;constructor(e){this.name=e}deferClaudeEvent(e,t){if(!e)return;const r=this.deferredClaudeEvents.get(e)??[];r.some(n=>n.event_id===t.event_id)||(a.info(this.name,`deferClaudeEvent session_id=${e} event_id=${t.event_id} queue_len=${r.length}`),r.push(t),this.deferredClaudeEvents.set(e,r))}deferCodexEvent(e,t){if(!e)return;const r=this.deferredCodexEvents.get(e)??[];r.some(n=>n.event_id===t.event_id)||(r.push(t),this.deferredCodexEvents.set(e,r))}deferCursorEvent(e,t){if(!e)return;const r=this.deferredCursorEvents.get(e)??[];r.some(n=>n.event_id===t.event_id)||(r.push(t),this.deferredCursorEvents.set(e,r))}deferAcpEvent(e,t){if(!e)return;const r=this.deferredAcpEvents.get(e)??[];r.some(n=>n.event_id===t.event_id)||(r.push(t),this.deferredAcpEvents.set(e,r))}deferPiEvent(e,t){if(!e)return;const r=this.deferredPiEvents.get(e)??[];r.some(n=>n.event_id===t.event_id)||(r.push(t),this.deferredPiEvents.set(e,r))}deferOpenHumanEvent(e,t){if(!e)return;const r=this.deferredOpenHumanEvents.get(e)??[];r.some(n=>n.event_id===t.event_id)||(r.push(t),this.deferredOpenHumanEvents.set(e,r))}deferCodeWhaleEvent(e,t){if(!e)return;const r=this.deferredCodeWhaleEvents.get(e)??[];r.some(n=>n.event_id===t.event_id)||(r.push(t),this.deferredCodeWhaleEvents.set(e,r))}deferOpenCodeEvent(e,t){if(!e)return;const r=this.deferredOpenCodeEvents.get(e)??[];r.some(n=>n.event_id===t.event_id)||(r.push(t),this.deferredOpenCodeEvents.set(e,r))}async replayDeferredClaudeEvents(e,t){const r=this.deferredClaudeEvents.get(e);if(!(!r||r.length===0)){this.deferredClaudeEvents.delete(e);for(const n of r)try{t.captureEventRuntimeConfig(n),await t.deliverInboundEvent(n),this.deferredClaudeReplayRetries.delete(n.event_id)}catch(d){const s=d instanceof Error?d.message:String(d),f=this.deferredClaudeReplayRetries.get(n.event_id)??0;if(this.isClaudeDeferredReplayRetriable(s)&&f<u){this.deferredClaudeReplayRetries.set(n.event_id,f+1);const o=this.deferredClaudeEvents.get(e)??[];o.some(i=>i.event_id===n.event_id)||(o.push(n),this.deferredClaudeEvents.set(e,o)),a.warn(this.name,`replayDeferredClaudeEvents retry ${f+1}/${u} session_id=${e} event_id=${n.event_id} reason=${s}`),setTimeout(()=>{this.replayDeferredClaudeEvents(e,t).catch(i=>{a.warn(this.name,`replayDeferredClaudeEvents async retry failed session_id=${e}: ${i instanceof Error?i.message:String(i)}`)})},v).unref();continue}this.deferredClaudeReplayRetries.delete(n.event_id),t.sendEventResult(n.event_id,"failed",s)}}}async replayDeferredCodexEvents(e,t,r={}){const n=this.deferredCodexEvents.get(e);if(!(!n||n.length===0)){this.deferredCodexEvents.delete(e);for(const d of n){r.announceComposing!==!1&&t.sendSessionComposing(d.session_id,!0,{ttlMs:12e4,refEventId:d.event_id,refMsgId:d.msg_id});try{t.captureEventRuntimeConfig(d),await t.deliverInboundEvent(d)}catch(s){t.sendSessionComposing(d.session_id,!1),t.sendEventResult(d.event_id,"failed",s instanceof Error?s.message:String(s))}}}}async replayDeferredCursorEvents(e,t,r={}){const n=this.deferredCursorEvents.get(e);if(!(!n||n.length===0)){this.deferredCursorEvents.delete(e);for(const d of n){r.announceComposing!==!1&&t.sendSessionComposing(d.session_id,!0,{ttlMs:12e4,refEventId:d.event_id,refMsgId:d.msg_id});try{t.captureEventRuntimeConfig(d),await t.deliverInboundEvent(d)}catch(s){t.sendSessionComposing(d.session_id,!1),t.sendEventResult(d.event_id,"failed",s instanceof Error?s.message:String(s))}}}}async replayDeferredPiEvents(e,t){const r=this.deferredPiEvents.get(e);if(!(!r||r.length===0)){this.deferredPiEvents.delete(e),t.sendSessionComposing(e,!0,{ttlMs:3e4});for(const n of r)try{t.captureEventRuntimeConfig(n),await t.deliverInboundEvent(n)}catch(d){t.sendSessionComposing(n.session_id,!1),t.sendEventResult(n.event_id,"failed",d instanceof Error?d.message:String(d))}}}async replayDeferredOpenHumanEvents(e,t){const r=this.deferredOpenHumanEvents.get(e);if(!(!r||r.length===0)){this.deferredOpenHumanEvents.delete(e),t.sendSessionComposing(e,!0,{ttlMs:3e4});for(const n of r)try{t.captureEventRuntimeConfig(n),await t.deliverInboundEvent(n)}catch(d){t.sendSessionComposing(n.session_id,!1),t.sendEventResult(n.event_id,"failed",d instanceof Error?d.message:String(d))}}}async replayDeferredOpenCodeEvents(e,t){const r=this.deferredOpenCodeEvents.get(e);if(!(!r||r.length===0)){this.deferredOpenCodeEvents.delete(e),t.sendSessionComposing(e,!0,{ttlMs:3e4});for(const n of r)try{t.captureEventRuntimeConfig(n),await t.deliverInboundEvent(n)}catch(d){t.sendSessionComposing(n.session_id,!1),t.sendEventResult(n.event_id,"failed",d instanceof Error?d.message:String(d))}}}async replayDeferredAcpEvents(e,t){const r=this.deferredAcpEvents.get(e);if(!(!r||r.length===0)){this.deferredAcpEvents.delete(e),t.sendSessionComposing(e,!0,{ttlMs:3e4});for(const n of r)try{t.captureEventRuntimeConfig(n),await t.deliverInboundEvent(n)}catch(d){t.sendSessionComposing(n.session_id,!1),t.sendEventResult(n.event_id,"failed",d instanceof Error?d.message:String(d))}}}async replayDeferredCodeWhaleEvents(e,t,r={}){const n=this.deferredCodeWhaleEvents.get(e);if(!(!n||n.length===0)){this.deferredCodeWhaleEvents.delete(e),r.announceComposing!==!1&&t.sendSessionComposing(e,!0,{ttlMs:3e4});for(const d of n)try{t.captureEventRuntimeConfig(d),await t.deliverInboundEvent(d)}catch(s){t.sendSessionComposing(d.session_id,!1),t.sendEventResult(d.event_id,"failed",s instanceof Error?s.message:String(s))}}}sendCodexDeferredReplayComposing(e,t){const r=this.deferredCodexEvents.get(e);!r||r.length===0||t.sendSessionComposing(e,!0,{ttlMs:12e4,activity:"preparing"})}sendCursorDeferredReplayComposing(e,t){const r=this.deferredCursorEvents.get(e);!r||r.length===0||t.sendSessionComposing(e,!0,{ttlMs:12e4,activity:"preparing"})}hasDeferred(e,t){return this.getMap(e).has(t)}getAllDeferredEvents(){return[...this.deferredClaudeEvents.values(),...this.deferredCodexEvents.values(),...this.deferredCursorEvents.values(),...this.deferredPiEvents.values(),...this.deferredAcpEvents.values(),...this.deferredOpenHumanEvents.values(),...this.deferredCodeWhaleEvents.values(),...this.deferredOpenCodeEvents.values()].flat()}clearAll(){this.deferredClaudeEvents.clear(),this.deferredClaudeReplayRetries.clear(),this.deferredCodexEvents.clear(),this.deferredCursorEvents.clear(),this.deferredPiEvents.clear(),this.deferredOpenHumanEvents.clear(),this.deferredCodeWhaleEvents.clear(),this.deferredOpenCodeEvents.clear(),this.deferredAcpEvents.clear()}removeEvent(e){for(const t of[this.deferredClaudeEvents,this.deferredCodexEvents,this.deferredCursorEvents,this.deferredAcpEvents,this.deferredPiEvents,this.deferredOpenHumanEvents,this.deferredCodeWhaleEvents,this.deferredOpenCodeEvents])for(const[r,n]of t.entries()){const d=n.findIndex(s=>s.event_id===e);if(d>=0)return n.splice(d,1),n.length===0&&t.delete(r),this.deferredClaudeReplayRetries.delete(e),a.info(this.name,`removeEvent event_id=${e} from deferred queue`),!0}return!1}clearSession(e){const t=this.deferredClaudeEvents.get(e)??[];for(const r of t)this.deferredClaudeReplayRetries.delete(r.event_id);this.deferredClaudeEvents.delete(e),this.deferredCodexEvents.delete(e),this.deferredCursorEvents.delete(e),this.deferredAcpEvents.delete(e),this.deferredPiEvents.delete(e),this.deferredOpenHumanEvents.delete(e),this.deferredCodeWhaleEvents.delete(e),this.deferredOpenCodeEvents.delete(e)}isClaudeDeferredReplayRetriable(e){const t=e.toLowerCase();return t.includes("claude channel listener not ready")||t.includes("notify port")||t.includes("mcp stdio server not available")||t.includes("mcp server startup failed")}getMap(e){switch(e){case"claude":return this.deferredClaudeEvents;case"codex":return this.deferredCodexEvents;case"cursor":return this.deferredCursorEvents;case"acp":return this.deferredAcpEvents;case"pi":return this.deferredPiEvents;case"openhuman":return this.deferredOpenHumanEvents;case"codewhale":return this.deferredCodeWhaleEvents;case"opencode":return this.deferredOpenCodeEvents}}}export{h as DeferredEventManager};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
class o{config;callbacks;running=new Map;queued=[];timers=new Map;constructor(e,t){this.config=e,this.callbacks=t}submit(e){return this.running.has(e.event_id)||this.queued.some(t=>t.event_id===e.event_id)?"accepted":this.running.size<this.config.maxConcurrent?(this.startRunning(e),"accepted"):this.config.maxQueued<=0||this.queued.length>=this.config.maxQueued?(this.callbacks.onRejected(e,"queue full"),"rejected"):(this.enqueue(e),"accepted")}cancel(e){const t=this.queued.findIndex(i=>i.event_id===e);if(t>=0){if(!this.config.cancelableQueued)return!1;const[i]=this.queued.splice(t,1);return this.clearTimer(e),this.callbacks.onStateChange(e,i.session_id,"canceled",{reason:"canceled by user"}),this.broadcastQueuePositions(),this.drainNext(),!0}return this.running.has(e)&&this.config.cancelableRunning?(this.callbacks.onCancelRunning(e),!0):!1}complete(e){this.running.delete(e),this.clearTimer(e),this.drainNext()}clear(e){const t=[],i=[];for(const s of this.queued)s.session_id===e?(this.clearTimer(s.event_id),this.callbacks.onStateChange(s.event_id,e,"canceled",{reason:"queue cleared"}),t.push(s.event_id)):i.push(s);return this.queued=i,t.length>0&&this.broadcastQueuePositions(),t}snapshot(e){const t=[...this.running.values()].filter(n=>n.session_id===e),i=t.map(n=>n.event_id),s=t.map(n=>({event_id:n.event_id,content_preview:this.buildQueueItemTitle(n.content),title:this.buildQueueItemTitle(n.content),summary:this.buildQueueItemTitle(n.content)})),u=this.queued.flatMap((n,c)=>n.session_id===e?[{event_id:n.event_id,position:c+1,content_preview:this.buildQueueItemTitle(n.content),title:this.buildQueueItemTitle(n.content),summary:this.buildQueueItemTitle(n.content)}]:[]);return{running:i,running_items:s,queued:u}}hasCapacity(){return this.running.size<this.config.maxConcurrent}get runningCount(){return this.running.size}get queuedCount(){return this.queued.length}destroy(){for(const e of this.timers.values())clearTimeout(e);this.timers.clear(),this.queued=[],this.running.clear()}enqueue(e){this.queued.push(e);const t=this.queued.length;if(this.callbacks.onStateChange(e.event_id,e.session_id,"queued",{queue_position:t,queue_total:t,actions:this.config.cancelableQueued?[{type:"cancel"}]:[],content_preview:this.buildQueueItemTitle(e.content)}),this.config.queueTimeoutMs>0){const i=setTimeout(()=>{this.timeoutEvent(e.event_id)},this.config.queueTimeoutMs);i.unref(),this.timers.set(e.event_id,i)}}timeoutEvent(e){const t=this.queued.findIndex(s=>s.event_id===e);if(t<0)return;const[i]=this.queued.splice(t,1);this.timers.delete(e),this.callbacks.onStateChange(e,i.session_id,"failed",{reason:"queue timeout"}),this.broadcastQueuePositions(),this.drainNext()}startRunning(e){this.running.set(e.event_id,e),this.callbacks.onStateChange(e.event_id,e.session_id,"running",{actions:this.config.cancelableRunning?[{type:"stop"}]:[],content_preview:this.buildQueueItemTitle(e.content)}),this.callbacks.onDeliver(e)}drainNext(){for(;this.running.size<this.config.maxConcurrent&&this.queued.length>0;){const e=this.queued.shift();this.clearTimer(e.event_id),this.startRunning(e)}this.queued.length>0&&this.broadcastQueuePositions()}broadcastQueuePositions(){const e=this.queued.length;for(let t=0;t<e;t++){const i=this.queued[t];this.callbacks.onStateChange(i.event_id,i.session_id,"queued",{queue_position:t+1,queue_total:e,actions:this.config.cancelableQueued?[{type:"cancel"}]:[],content_preview:this.buildQueueItemTitle(i.content)})}}clearTimer(e){const t=this.timers.get(e);t&&(clearTimeout(t),this.timers.delete(e))}buildQueueItemTitle(e){const t=String(e??"").replace(/\s+/g," ").trim();return t?t.length>64?`${t.slice(0,64)}...`:t:"Message"}}export{o as EventQueue};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{AgentInstance as e}from"./bridge.js";import{RespawnManager as l}from"./respawn-manager.js";import{AdapterPool as a,PoolFullError as d}from"./adapter-pool.js";import{RevokeHandler as p}from"./revoke-handler.js";import{SendController as s}from"./send-controller.js";import{EventQueue as f}from"./event-queue.js";import{parseSessionControlCommand as u,handleSessionControlCommand as c,handleSessionControlLocalAction as T}from"./session-controller.js";import{DeferredEventManager as b}from"./deferred-events.js";import{buildToolUseCard as v,buildToolResultCard as A,buildLocalGrixCardLink as E,normalizeToolCardText as L,compactToolCardText as R}from"./tool-card-utils.js";export{a as AdapterPool,e as AgentInstance,b as DeferredEventManager,f as EventQueue,d as PoolFullError,l as RespawnManager,p as RevokeHandler,s as SendController,E as buildLocalGrixCardLink,A as buildToolResultCard,v as buildToolUseCard,R as compactToolCardText,c as handleSessionControlCommand,T as handleSessionControlLocalAction,L as normalizeToolCardText,u as parseSessionControlCommand};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{log as t}from"../core/log/index.js";const l=3e3,n=6e4,i=10,r=300*1e3,o=6e4,p=1e4,h=3;class w{attempts=0;slowRetryTimer=null;respawnTimer=null;healthCheckTimer=null;healthFailures=0;get exhausted(){return this.attempts>=i}resetAttempts(){this.attempts=0}resetHealthFailures(){this.healthFailures=0}stopSlowRetry(){this.slowRetryTimer&&(clearTimeout(this.slowRetryTimer),this.slowRetryTimer=null)}stopHealthCheck(){this.healthCheckTimer&&(clearInterval(this.healthCheckTimer),this.healthCheckTimer=null)}stop(){this.stopSlowRetry(),this.respawnTimer&&(clearTimeout(this.respawnTimer),this.respawnTimer=null)}stopAll(){this.stopHealthCheck(),this.stop()}startHealthCheck(e){this.stopHealthCheck(),e.pingAgent&&(this.healthFailures=0,this.healthCheckTimer=setInterval(()=>{if(e.stopped){this.stopHealthCheck();return}if(!e.pingAgent){this.stopHealthCheck();return}e.agentAlive!==!1&&e.pingAgent(p).then(s=>{s?this.healthFailures=0:(this.healthFailures++,t.info(e.name,`Health check failed (${this.healthFailures}/${h})`),this.healthFailures>=h&&(t.error(e.name,"Health check exhausted, triggering respawn"),this.healthFailures=0,e.onAbortActiveRun&&e.onAbortActiveRun("health check exhausted"),this.scheduleRespawn(e)))}).catch(()=>{this.healthFailures++,t.info(e.name,`Health check error (${this.healthFailures}/${h})`),this.healthFailures>=h&&(t.error(e.name,"Health check exhausted (error), triggering respawn"),this.healthFailures=0,e.onAbortActiveRun&&e.onAbortActiveRun("health check exhausted"),this.scheduleRespawn(e))})},o))}scheduleRespawn(e){try{e.onCleanupAgent()}catch{}if(this.respawnTimer&&(clearTimeout(this.respawnTimer),this.respawnTimer=null),this.attempts++,this.attempts>i){t.error(e.name,`Max respawn attempts reached, will retry in ${r/1e3}s`),this.scheduleSlowRetry(e);return}const s=Math.min(l*2**(this.attempts-1),n);t.info(e.name,`Respawn attempt ${this.attempts}/${i} in ${s}ms`),this.respawnTimer=setTimeout(()=>{this.respawnTimer=null,!e.stopped&&e.onRespawnNeeded().catch(a=>{t.error(e.name,`Respawn failed: ${a}`),this.scheduleRespawn(e)})},s)}scheduleSlowRetry(e){this.stopSlowRetry(),this.slowRetryTimer=setTimeout(()=>{e.stopped||(t.info(e.name,"Slow retry: attempting respawn"),this.attempts=0,e.onRespawnNeeded().catch(s=>{t.error(e.name,`Slow retry failed: ${s}`),this.scheduleRespawn(e)}))},r)}}export{w as RespawnManager};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{log as r}from"../core/log/index.js";const o=6e4;class c{ttlMs;recentRevokes=new Map;constructor(e=o){this.ttlMs=e}checkAndTrack(e){return this.pruneExpired(),this.recentRevokes.has(e)?!1:(this.recentRevokes.set(e,Date.now()),r.info("revoke",`Event revoked: ${e}`),!0)}pruneExpired(){const e=Date.now();for(const[t,s]of this.recentRevokes.entries())e-s>this.ttlMs&&this.recentRevokes.delete(t)}}export{c as RevokeHandler};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const l=Object.freeze({responseDelivery:"auto",toolEvents:"send",thinkingEvents:"send"});function h(e){const n=c(e);if(!n)return{patch:null};if(!Object.prototype.hasOwnProperty.call(n,"connector"))return{patch:null};const s=c(n.connector);if(!s)return{patch:null,error:"connector must be an object"};const t=s,o={},v=i(t.response_delivery,t.responseDelivery);if(v!==void 0){const r=a(v);if(!r)return{patch:null,error:"invalid response_delivery"};o.responseDelivery=r}const p=i(t.tool_events,t.toolEvents);if(p!==void 0){const r=d(p);if(!r)return{patch:null,error:"invalid tool_events"};o.toolEvents=r}const f=i(t.thinking_events,t.thinkingEvents);if(f!==void 0){const r=E(f);if(!r)return{patch:null,error:"invalid thinking_events"};o.thinkingEvents=r}return{patch:Object.keys(o).length>0?o:null}}function g(e,n){return n?{responseDelivery:n.responseDelivery??e.responseDelivery,toolEvents:n.toolEvents??e.toolEvents,thinkingEvents:n.thinkingEvents??e.thinkingEvents}:e}function y(e){const n=c(e);if(!n)return null;const s=a(i(n.response_delivery,n.responseDelivery)),t=d(i(n.tool_events,n.toolEvents)),o=E(i(n.thinking_events,n.thinkingEvents));return!s&&!t&&!o?null:{responseDelivery:s??l.responseDelivery,toolEvents:t??l.toolEvents,thinkingEvents:o??l.thinkingEvents}}function k(e){return e.toolEvents==="drop"}function m(e){return e.thinkingEvents==="drop"}function a(e){const n=u(e);return n==="auto"||n==="stream"||n==="single_message"?n:null}function d(e){const n=u(e);return n==="send"||n==="drop"?n:null}function E(e){const n=u(e);return n==="send"||n==="drop"?n:null}function u(e){if(typeof e!="string")return null;const n=e.trim().toLowerCase();return n||null}function c(e){return!e||typeof e!="object"||Array.isArray(e)?null:e}function i(...e){for(const n of e)if(n!==void 0)return n}export{l as DEFAULT_CONNECTOR_RUNTIME_CONFIG,g as applyConnectorRuntimeConfigPatch,h as extractConnectorRuntimeConfigPatch,m as isThinkingEventDropped,k as isToolEventDropped,y as normalizeRuntimeConfigSnapshot};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{log as a}from"../core/log/index.js";import{splitTextForAibotProtocol as m}from"../core/protocol/protocol-text.js";import{isThinkingEventDropped as g,isToolEventDropped as d,normalizeRuntimeConfigSnapshot as p}from"./runtime-config.js";class E{handle;connectorRuntimeConfig;eventRuntimeConfigs=new Map;thinkingSeqMap=new Map;toolCardSeq=0;bufferedEventTexts=new Map;constructor(e){this.connectorRuntimeConfig={...e}}bind(e){this.handle=e}setGlobalRuntimeConfig(e){this.connectorRuntimeConfig=e}getGlobalRuntimeConfig(){return this.connectorRuntimeConfig}captureEventRuntimeConfig(e){const t=p(e.connector_runtime_config)??this.connectorRuntimeConfig;this.eventRuntimeConfigs.set(e.event_id,{responseDelivery:t.responseDelivery,toolEvents:t.toolEvents,thinkingEvents:t.thinkingEvents})}resolveEventRuntimeConfig(e){if(e){const t=this.eventRuntimeConfigs.get(e);if(t)return t}return this.connectorRuntimeConfig}shouldDropToolDisplayEvent(e){return d(this.resolveEventRuntimeConfig(e))}shouldDropThinkingDisplayEvent(e){return g(this.resolveEventRuntimeConfig(e))}shouldDropCodexDisplayEvent(e,t){return this.shouldDropToolDisplayEvent(e)?t==="item/completed":!1}shouldDropAcpRawDisplayEvent(e,t){return!!(this.shouldDropToolDisplayEvent(e)&&(t==="tool_use"||t==="tool_result")||this.shouldDropThinkingDisplayEvent(e)&&t==="thinking")}sendReply(e,t,n,i,s,o){if(!o?.skipToolFilter&&this.shouldDropToolDisplayEvent(e)&&_(n,s))return;if(this.resolveEventRuntimeConfig(e).responseDelivery==="stream"&&!s){const r=m(n),f=e?`reply_stream_${e}`:`reply_stream_${Date.now()}`;if(r.length===0){this.sendStreamChunk(e,t,"",1,!0,f,i);return}for(let u=0;u<r.length;u++)this.sendStreamChunk(e,t,r[u],u+1,!1,f,i);this.sendStreamChunk(e,t,"",r.length+1,!0,f,i);return}this.handle.sendMsg({event_id:e,session_id:t,msg_type:1,content:n,quoted_message_id:i||void 0,...s?{extra:s}:{}})}sendStreamChunk(e,t,n,i,s,o,l){if(this.finalizeThinking(e,t),this.resolveEventRuntimeConfig(e).responseDelivery==="single_message"&&e){this.bufferStreamChunk(e,t,n,s,l);return}this.handle.sendStreamChunk({event_id:e,session_id:t,delta_content:n,chunk_seq:i,is_finish:s,...o?{client_msg_id:o}:{},...l?{quoted_message_id:l}:{}})}sendEventResult(e,t,n,i){this.finalizeThinking(e,""),this.flushBufferedStreamText(e),this.handle.sendEventResult({event_id:e,status:t,...n?{msg:n}:{},...i?{code:i}:{},updated_at:Date.now()}),this.eventRuntimeConfigs.delete(e)}sendThinking(e,t,n){if(this.shouldDropThinkingDisplayEvent(e))return;const i=(this.thinkingSeqMap.get(e)??0)+1;this.thinkingSeqMap.set(e,i),this.handle.sendStreamChunk({event_id:e,session_id:t,delta_content:n,chunk_seq:i,is_finish:!1,client_msg_id:`${e}_thinking`})}sendToolExecutionCard(e,t,n,i){if(this.shouldDropToolDisplayEvent(e)){a.info("send-ctrl",`[tool-card] DROP event=${e} session=${t} summary=${n.summaryText}`);return}const o={summary_text:n.summaryText};n.detailText&&(o.detail_text=n.detailText),a.info("send-ctrl",`[tool-card] SEND event=${e} session=${t} summary=${n.summaryText}`),this.handle.sendMsg({event_id:e,session_id:t,client_msg_id:i??`tool_${++this.toolCardSeq}_${Date.now()}`,msg_type:1,content:"",extra:{channel_data:{grix:{toolExecution:o}},agent_api_origin:!0}})}finalizeThinking(e,t){const n=this.thinkingSeqMap.get(e);n!==void 0&&(this.thinkingSeqMap.delete(e),t&&this.handle.sendStreamChunk({event_id:e,session_id:t,delta_content:"",chunk_seq:n+1,is_finish:!0,client_msg_id:`${e}_thinking`}))}bufferOnly(e,t,n,i,s){this.bufferStreamChunk(e,t,n,i,s)}bufferStreamChunk(e,t,n,i,s){const o=this.bufferedEventTexts.get(e);o?(n&&(o.text+=n),!o.quotedMessageId&&s&&(o.quotedMessageId=s)):this.bufferedEventTexts.set(e,{sessionId:t,text:n??"",quotedMessageId:s||void 0}),i&&this.flushBufferedStreamText(e)}flushBufferedStreamText(e){const t=this.bufferedEventTexts.get(e);t&&(this.bufferedEventTexts.delete(e),t.text&&this.handle.sendMsg({event_id:e,session_id:t.sessionId,msg_type:1,content:t.text,...t.quotedMessageId?{quoted_message_id:t.quotedMessageId}:{}}))}}function _(h,e){return e&&typeof e=="object"&&e.channel_data?.grix?.toolExecution?!0:/^\[tool\]/.test(h)||/^\[tool result\]/.test(h)}export{E as SendController};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import{SESSION_CONTROL_VERBS as l,SESSION_CONTROL_ERROR_CODES as g}from"../adapter/claude/protocol-contract.js";import{parseInteractionMessage as b,readCardSubmissionParam as _}from"../core/protocol/interaction-parser.js";import{log as S}from"../core/log/index.js";function M(a){const t=String(a.content??"").trim();if(!t.startsWith("/grix ")){const s=b(t);for(const n of s.cardSubmissions){if(n.cardId!=="agent_open_session")continue;const i=_(n,"cwd")??"";return i?{verb:l.open,args:i}:null}return null}const o=t.slice(6).trim().split(/\s+/),c=o[0]?.toLowerCase(),m=o.slice(1).join(" ");return Object.values(l).includes(c)?{verb:c,args:m}:null}function w(a,t,o,c){const{verb:m,args:r}=a,s=t.session_id,n=()=>c.sendEventAck(t.event_id,s),i=(e,d)=>c.sendEventResult(t.event_id,e,d);switch(m){case l.open:{const e=r.trim();if(!e){n(),i("failed",{msg:"Usage: /grix open <working-directory>",code:g.cwdRequired});return}o.getSessionBindings().set(s,e),o.onSessionBound?.(s,e),n(),i("responded",{msg:`Session bound to ${e}`});break}case l.where:{const e=o.getSessionBindings().get(s)||o.getCwd()||"not set";n(),i("responded",{msg:`Working directory: ${e}`});break}case l.status:{const e=o.getStatus(),d=o.getAcpSessionOptions(),f=d?`
|
|
2
|
+
Mode: ${d.currentModeId} (${d.modes.map(p=>p.id).join(", ")})`:"",u=d?`
|
|
3
|
+
Model: ${d.currentModelId} (${d.models.map(p=>p.modelId).join(", ")})`:"";n(),i("responded",{msg:`Status: alive=${e.alive} busy=${e.busy} adapter=${e.adapterType}${f}${u}`});break}case l.stop:{if(!o.getSessionBindings().get(s)){n(),i("failed",{msg:"No session binding found for this session",code:g.bindingMissing});return}o.cancelActiveRun?.().catch(()=>{}),n(),i("responded",{msg:`Session ${s} stopped`});break}case l.restart:{const e=o.getSessionBindings().get(s);if(!e){n(),i("failed",{msg:"No session binding found for this session",code:g.bindingMissing});return}o.cancelActiveRun?.().catch(()=>{}),n(),i("responded",{msg:`Session ${s} restarted for ${e}`});break}case"set_mode":{const e=r.trim();if(!e){n(),i("failed",{msg:"Usage: /grix set_mode <mode-id>"});return}o.setMode(e).then(d=>{d&&o.onModeSet?.(e),n(),i(d?"responded":"failed",{msg:d?`Mode set to ${e}`:`Failed to set mode ${e}`})});break}case"set_model":{const e=r.trim();if(!e){n(),i("failed",{msg:"Usage: /grix set_model <model-id>"});return}o.setModel(e).then(d=>{d&&o.onModelSet?.(e),n(),i(d?"responded":"failed",{msg:d?`Model set to ${e}`:`Failed to set model ${e}`})});break}case"list_options":{const e=o.getAcpSessionOptions();if(!e){n(),i("failed",{msg:"No ACP session active"});return}const d=e.modes.map(u=>`${u.id}: ${u.name}`).join(`
|
|
4
|
+
`),f=e.models.map(u=>`${u.modelId}: ${u.name}`).join(`
|
|
5
|
+
`);n(),i("responded",{msg:`Modes (current: ${e.currentModeId}):
|
|
6
|
+
${d}
|
|
7
|
+
|
|
8
|
+
Models (current: ${e.currentModelId}):
|
|
9
|
+
${f}`});break}case"approve":{const e=r.trim();if(!e||!o.isAcpAlive){n(),i("failed",{msg:"Usage: /grix approve <tool-call-id>"});return}const d=o.getPendingApproval(e);d&&(o.deletePendingApproval(e),o.respondPermission(d.requestId,{behavior:"allow"}).catch(()=>{})),n(),i("responded",{msg:d?`Approved: ${e}`:`No pending approval: ${e}`});break}case"reject":{const e=r.trim();if(!e||!o.isAcpAlive){n(),i("failed",{msg:"Usage: /grix reject <tool-call-id>"});return}const d=o.getPendingApproval(e);d&&(o.deletePendingApproval(e),o.respondPermission(d.requestId,{behavior:"deny"}).catch(()=>{})),n(),i("responded",{msg:d?`Rejected: ${e}`:`No pending approval: ${e}`});break}default:n(),i("failed",{msg:`Unknown command: /grix ${m}`,code:g.verbInvalid})}}function A(a,t,o){const c=a.params??{},m=String(c.verb??"").toLowerCase(),r=o.sendLocalActionResult;switch(m){case l.open:{const s=String(c.session_id??""),n=String(c.cwd??"");if(!n){r(a.action_id,"failed",void 0,g.cwdRequired,"session cwd is required");return}s&&t.getSessionBindings().set(s,n),s&&t.onSessionBound?.(s,n),r(a.action_id,"ok",{outcome:"opened",binding:{cwd:n}});break}case l.stop:{const s=String(c.session_id??"");if(!s){r(a.action_id,"failed",void 0,"session_id_required","session_id is required for stop");return}t.cancelActiveRun?.().catch(()=>{}),r(a.action_id,"ok",{outcome:"stopped",sessionId:s});break}case l.status:{const s=String(c.session_id??""),i=t.getSessionBindings().get(s)??"",e=t.getStatus();r(a.action_id,"ok",{outcome:"status",verb:"status",binding:{aibotSessionId:s,cwd:i,workerStatus:e.alive?e.busy?"busy":"ready":"stopped"}});break}case l.where:{const s=String(c.session_id??""),i=t.getSessionBindings().get(s)??"";r(a.action_id,"ok",{outcome:"where",verb:"where",binding:{aibotSessionId:s,cwd:i}});break}case"restart":{const s=String(c.session_id??"");if(!s){r(a.action_id,"failed",void 0,"session_id_required","session_id is required for restart");return}t.cancelActiveRun?.().catch(()=>{}),r(a.action_id,"ok",{outcome:"restarted",sessionId:s});break}case l.setMode:{const s=String(c.mode_id??"");if(!s||!t.isAcpAlive){r(a.action_id,"failed",void 0,"set_mode_invalid","mode_id is required and ACP session must be active");return}t.setMode(s).then(n=>{n&&t.onModeSet?.(s),r(a.action_id,n?"ok":"failed",n?{outcome:"mode_set",modeId:s}:void 0,n?void 0:"set_mode_failed",n?void 0:`failed to set mode ${s}`)});break}case l.setModel:{const s=String(c.model_id??"");if(!s||!t.isAcpAlive){r(a.action_id,"failed",void 0,"set_model_invalid","model_id is required and ACP session must be active");return}t.setModel(s).then(n=>{n&&t.onModelSet?.(s),r(a.action_id,n?"ok":"failed",n?{outcome:"model_set",modelId:s}:void 0,n?void 0:"set_model_failed",n?void 0:`failed to set model ${s}`)});break}case l.listOptions:{const s=t.getAcpSessionOptions(),n=s?{modes:s.modes.map(i=>({id:i.id,name:i.name})),currentModeId:s.currentModeId,models:s.models.map(i=>({modelId:i.modelId,name:i.name})),currentModelId:s.currentModelId,available_models:s.models.map(i=>({id:i.modelId,displayName:i.name})),available_modes:s.modes.map(i=>({id:i.id,displayName:i.name}))}:{modes:[],currentModeId:"",models:[],currentModelId:"",available_models:[],available_modes:[]};S.info("session-controller",`[toolbar] listOptions currentModelId=${n.currentModelId} models=${JSON.stringify(n.models?.map(i=>i.modelId))}`),r(a.action_id,"ok",n);break}default:r(a.action_id,"failed",void 0,g.verbInvalid,`session control verb ${m} is not supported`)}}export{w as handleSessionControlCommand,A as handleSessionControlLocalAction,M as parseSessionControlCommand};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const f=500,d=140;function o(t){return typeof t=="string"?t.trim().replace(/\s+/g," "):t==null?"":typeof t=="number"||typeof t=="boolean"?String(t):""}function i(t,r){const e=o(t);return e.length<=r?e:r<=3?e.slice(0,r):`${e.slice(0,r-3)}...`}function l(t){const r=o(t);if(!r)return"";try{const e=JSON.parse(r),n=T(e);if(n)return n}catch{}return r}function a(t){const r=o(t);if(!r)return"";try{return T(JSON.parse(r))}catch{return""}}function T(t){if(!t||typeof t!="object"||Array.isArray(t))return"";const r=t;for(const e of["command","file_path","path","pattern","query","description","url","text"]){const n=o(r[e]);if(n)return n}return""}function _(t,r){const e=o(t)||"tool",n=l(r);return{summaryText:n?`${e}: ${i(n,140)}`:e,...n?{detailText:i(n,500)}:{}}}function p(t,r){const e=o(r);if(e==="(failed)")return{summaryText:"(failed)"};if(e.startsWith("(failed) ")){const u=e.slice(9),c=a(u);return{summaryText:c?`${i(c,140)} (failed)`:"(failed)",...c?{detailText:i(c,500)}:{}}}const n=a(e);if(n)return{summaryText:i(n,140),detailText:i(n,500)};const s=i(e,500);return{summaryText:t||"result",...s?{detailText:s}:{}}}function I(t,r,e){const n=new URLSearchParams;return n.set("d",JSON.stringify(e)),`[${t}](grix://card/${r}?${n.toString()})`}export{I as buildLocalGrixCardLink,p as buildToolResultCard,_ as buildToolUseCard,i as compactToolCardText,o as normalizeToolCardText};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{readAllowlist as a,writeAllowlist as r}from"./allowlist-store.js";import{log as s}from"../log/index.js";class u{filePath;writeQueue=Promise.resolve();constructor(e){this.filePath=e}async checkAccess(e){if(!e?.trim())return!1;const t=a(this.filePath);return t.includes(e)?!0:t.length===0?this.autoAdd(e):!1}async addOwner(e){e?.trim()&&(this.writeQueue=this.writeQueue.then(async()=>{const t=a(this.filePath);t.includes(e)||(t.push(e),await r(this.filePath,t))}).catch(t=>{s.error("allowlist-gate",`addOwner failed for ${e}: ${t instanceof Error?t.message:t}`)}),await this.writeQueue)}async removeOwner(e){e?.trim()&&(this.writeQueue=this.writeQueue.then(async()=>{const t=a(this.filePath),i=t.filter(l=>l!==e);i.length!==t.length&&await r(this.filePath,i)}).catch(t=>{s.error("allowlist-gate",`removeOwner failed for ${e}: ${t instanceof Error?t.message:t}`)}),await this.writeQueue)}async listOwners(){return a(this.filePath)}autoAdd(e){return new Promise(t=>{this.writeQueue=this.writeQueue.then(async()=>{const i=a(this.filePath);if(i.includes(e)){t(!0);return}if(i.length>0){t(!1);return}await r(this.filePath,[e]),t(!0)}).catch(i=>{s.error("allowlist-gate",`autoAdd failed for ${e}: ${i instanceof Error?i.message:i}`),t(!1)})})}}export{u as AllowlistGate};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{readJSONFile as i,writeJSONFileAtomic as o}from"../util/json-file.js";function l(t){const r=i(t);return r&&typeof r=="object"&&"owners"in r&&Array.isArray(r.owners)?r.owners.filter(e=>typeof e=="string"&&e.trim().length>0):[]}async function a(t,r){await o(t,{owners:r})}export{l as readAllowlist,a as writeAllowlist};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{AllowlistGate as t}from"./allowlist-gate.js";import{readAllowlist as e,writeAllowlist as i}from"./allowlist-store.js";export{t as AllowlistGate,e as readAllowlist,i as writeAllowlist};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{EventEmitter as f}from"node:events";import{randomUUID as p}from"node:crypto";import l from"node:os";import _ from"ws";import{log as u}from"../log/index.js";function k(m){return m.replace(/(?<=[\[:,\[]\s*)(\d{16,})(?=\s*[,}\]\n])/g,'"$1"')}function g(m){const e=[...m??["stream_chunk","local_action_v1","agent_invoke"]];return e.includes("event_result_ack")||e.push("event_result_ack"),e}const v="aibot-agent-api-v1",$=1;class d extends f{static DROPPABLE_COMMANDS=new Set(["update_binding_card"]);static BUFFER_OVERFLOW_RETAIN_COMMANDS=new Set(["event_result","codex_event","client_stream_chunk"]);static MAX_OUTBOUND_BUFFER_SIZE=1e3;static BACKPRESSURE_THRESHOLD=64*1024;ws=null;seq=0;heartbeatTimer=null;heartbeatSec=30;connected=!1;reconnecting=!1;reconnectAttempts=0;config;pendingInvokes=new Map;pendingRequests=new Map;outboundBuffer=[];constructor(e){super(),this.config={url:e.url,agentId:e.agentId,apiKey:e.apiKey,clientType:e.clientType,clientVersion:e.clientVersion??"",adapterHint:e.adapterHint??"",capabilities:g(e.capabilities),localActions:e.localActions??["exec_approve","exec_reject"],skills:e.skills}}get isConnected(){return this.connected}async connect(){return new Promise((e,t)=>{const i=new _(this.config.url);this.ws=i;const a=setTimeout(()=>{t(new Error("Auth timeout: no auth_ack received within 15s")),this.cleanupSocket()},15e3),r=++this.seq,o=setTimeout(()=>{this.pendingRequests.delete(r),t(new Error("Auth request timeout")),this.cleanupSocket()},15e3);this.pendingRequests.set(r,{expected:["auth_ack"],resolve:s=>{clearTimeout(a);const n=s.payload;n.code===0?(this.connected=!0,this.reconnectAttempts=0,n.heartbeat_sec&&(this.heartbeatSec=n.heartbeat_sec),this.startHeartbeat(),this.flushOutboundBuffer(),this.emit("auth",n),e(n)):t(new Error(`Auth failed: code=${n.code} msg=${n.msg}`))},reject:s=>{clearTimeout(a),t(s)},timer:o}),i.on("open",()=>{const s={agent_id:this.config.agentId,api_key:this.config.apiKey,client_type:this.config.clientType,protocol_version:v,contract_version:$,capabilities:this.config.capabilities??[],local_actions:this.config.localActions,skills:this.config.skills};this.config.clientVersion&&(s.client="grix-connector",s.client_version=this.config.clientVersion,s.host_type=this.config.clientType,s.host_version=this.config.clientVersion),this.config.adapterHint&&(s.adapter_hint=this.config.adapterHint),s.host_meta={hostname:l.hostname(),platform:l.platform(),arch:l.arch(),os_release:l.release()},this.config.concurrency&&(s.concurrency=this.config.concurrency),this.sendPacket("auth",s,r)}),i.on("message",s=>{let n;try{n=JSON.parse(k(s.toString()))}catch{return}try{this.handlePacket(n)}catch(h){this.emitClientError(new Error(`handlePacket error: ${h}`))}}),i.on("close",(s,n)=>{this.connected=!1,this.stopHeartbeat(),this.rejectAllPendingRequests("websocket closed"),this.emit("close",s,n.toString()),s!==1e3&&this.attemptReconnect()}),i.on("error",s=>{this.emitClientError(s instanceof Error?s:new Error(String(s))),this.connected||t(s)})})}handlePacket(e){if(e.seq>0&&this.pendingRequests.has(e.seq)){const t=this.pendingRequests.get(e.seq);this.pendingRequests.delete(e.seq),clearTimeout(t.timer),t.expected.includes(e.cmd)?t.resolve(e):t.reject(new Error(`unexpected response: got ${e.cmd}, expected ${t.expected.join("/")}`));return}switch(e.cmd){case"auth_ack":break;case"ping":{this.sendPacket("pong",e.payload??{});break}case"event_msg":{this.emit("event",e.payload);break}case"local_action":{this.emit("localAction",e.payload);break}case"event_stop":{this.emit("stop",e.payload);break}case"event_revoke":{this.emit("revoke",e.payload);break}case"event_cancel":{this.emit("eventCancel",e.payload);break}case"queue_clear":{this.emit("queueClear",e.payload);break}case"kicked":{if(this.emit("kicked",e.payload),this.connected=!1,this.stopHeartbeat(),this.rejectAllPendingRequests("kicked"),this.outboundBuffer.length=0,this.reconnectAttempts=Math.max(this.reconnectAttempts,3),this.ws){try{this.ws.close(4001,"kicked")}catch{}this.ws=null}break}case"error":{const t=e.payload;this.emitClientError(new Error(`Server error: code=${t.code} msg=${t.msg}`));break}case"agent_invoke_result":{this.handleInvokeResult(e.payload);break}case"send_ack":case"send_nack":case"local_action_ack":break;default:break}}sendEventAck(e){this.sendPacket("event_ack",e)}sendStreamChunk(e){this.sendPacket("client_stream_chunk",e)}sendMsg(e){this.sendPacket("send_msg",e)}sendEventResult(e){if(!this.ws||this.ws.readyState!==_.OPEN){this.sendPacket("event_result",e);return}this.sendEventResultReliable(e)}sendLocalActionResult(e){this.sendPacket("local_action_result",e)}sendEventStopAck(e){this.sendPacket("event_stop_ack",e)}sendEventStopResult(e){this.sendPacket("event_stop_result",e)}sendSessionActivitySet(e){this.sendPacket("session_activity_set",e)}sendCodexEvent(e){this.sendPacket("codex_event",e)}sendUpdateBindingCard(e){this.sendPacket("update_binding_card",e)}sendPing(){this.sendPacket("ping",{})}sendEventState(e){this.sendPacket("event_state",e)}sendEventCancelResult(e){this.sendPacket("event_cancel_result",e)}sendQueueClearResult(e){this.sendPacket("queue_clear_result",e)}sendQueueSnapshot(e){this.sendPacket("queue_snapshot",e)}agentInvoke(e,t,i=15e3){return new Promise((a,r)=>{const o=p(),s=Math.max(1e3,Math.min(i,6e4)),n=setTimeout(()=>{this.pendingInvokes.delete(o),r(new Error(`agent_invoke timeout: ${e}`))},s);this.pendingInvokes.set(o,{resolve:a,reject:r,timer:n}),this.sendPacket("agent_invoke",{invoke_id:o,action:e,params:t,timeout_ms:s})})}request(e,t,i){return new Promise((a,r)=>{const o=++this.seq,s=setTimeout(()=>{this.pendingRequests.delete(o),r(new Error(`request timeout: ${e} (expected ${i.expected.join("/")})`))},i.timeoutMs);this.pendingRequests.set(o,{expected:i.expected,resolve:a,reject:r,timer:s}),this.sendPacket(e,t,o)||(this.pendingRequests.delete(o),clearTimeout(s),r(new Error(`send failed: ${e}`)))})}async sendStreamChunkRequest(e,t=2e4){return this.request("client_stream_chunk",e,{expected:["send_ack","send_nack","error"],timeoutMs:t})}async sendText(e,t=2e4){return this.request("send_msg",{msg_type:1,...e},{expected:["send_ack","send_nack","error"],timeoutMs:t})}async sendMedia(e,t=2e4){return this.request("send_msg",{...e,msg_type:2},{expected:["send_ack","send_nack","error"],timeoutMs:t})}async deleteMessage(e,t,i=2e4){return this.request("delete_msg",{session_id:e,msg_id:t},{expected:["send_ack","send_nack","error"],timeoutMs:i})}async sendEventResultRequest(e,t=5e3){return this.request("event_result",e,{expected:["send_ack","send_nack","error"],timeoutMs:t})}disconnect(){this.connected=!1,this.reconnecting=!1,this.reconnectAttempts=0,this.stopHeartbeat(),this.rejectAllPendingInvokes("disconnect"),this.rejectAllPendingRequests("disconnect"),this.outboundBuffer.length=0,this.ws&&(this.ws.close(1e3,"client disconnect"),this.ws=null)}async attemptReconnect(){if(!this.reconnecting)for(this.reconnecting=!0,this.emit("disconnected");this.reconnecting;){const e=Math.min(1e3*2**this.reconnectAttempts,3e4),t=Math.floor(e*.2*Math.random());if(this.reconnectAttempts++,await new Promise(i=>setTimeout(i,e+t)),!this.reconnecting)return;try{const i=await this.connect(),a=this.reconnectAttempts;this.reconnectAttempts=0,this.reconnecting=!1,u.info("aibot",`reconnect succeeded attempt=${a}`),this.emit("auth",i);return}catch(i){u.warn("aibot",`reconnect failed attempt=${this.reconnectAttempts} err=${i instanceof Error?i.message:i}`)}}}sendPacket(e,t,i){if(this.ws&&this.ws.readyState===_.OPEN){const a=this.ws.bufferedAmount>d.BACKPRESSURE_THRESHOLD;if(!a||!d.DROPPABLE_COMMANDS.has(e)){if(a&&d.DROPPABLE_COMMANDS.has(e))return!1;const r=i??++this.seq,o={cmd:e,seq:r,payload:t};try{const s=this.ws.readyState,n=this.ws.bufferedAmount;return this.ws.send(JSON.stringify(o),h=>{if(e==="event_result"){const c=t;h?u.warn("aibot",`event_result ws send callback failed event=${c.event_id??""} status=${c.status??""} seq=${r} readyState=${s} bufferedAmount=${n} err=${h.message}`):u.info("aibot",`event_result ws send callback ok event=${c.event_id??""} status=${c.status??""} seq=${r} readyState=${s} bufferedAmount=${n}`)}else if(e==="client_stream_chunk"){const c=t;h?u.warn("aibot",`stream_chunk ws send callback failed event=${c.event_id??""} session=${c.session_id??""} seq=${r} chunk_seq=${c.chunk_seq??""} is_finish=${c.is_finish??""} readyState=${s} bufferedAmount=${n} err=${h.message}`):u.info("aibot",`stream_chunk ws send ok event=${c.event_id??""} session=${c.session_id??""} seq=${r} chunk_seq=${c.chunk_seq??""} is_finish=${c.is_finish??""} readyState=${s} bufferedAmount=${n}`)}}),!0}catch(s){return this.emitClientError(new Error(`sendPacket failed: ${s}`)),!1}}}if(d.DROPPABLE_COMMANDS.has(e)||i!==void 0)return!1;if(this.outboundBuffer.length>=d.MAX_OUTBOUND_BUFFER_SIZE&&(this.outboundBuffer=this.outboundBuffer.filter(a=>d.BUFFER_OVERFLOW_RETAIN_COMMANDS.has(a.cmd)),this.outboundBuffer.length>=d.MAX_OUTBOUND_BUFFER_SIZE&&this.outboundBuffer.shift()),this.outboundBuffer.push({cmd:e,payload:t}),e==="client_stream_chunk"){const a=t;u.info("aibot",`stream_chunk buffered (ws not open) event=${a.event_id??""} session=${a.session_id??""} chunk_seq=${a.chunk_seq??""} is_finish=${a.is_finish??""} ws=${this.ws?`state=${this.ws.readyState}`:"null"}`)}return!1}async sendEventResultReliable(e){for(let r=1;r<=3;r++){const o=this.ws?.readyState??-1,s=this.ws?.bufferedAmount??0;u.info("aibot",`event_result send attempt event=${e.event_id} status=${e.status} attempt=${r}/3 readyState=${o} bufferedAmount=${s}`);try{const n=await this.sendEventResultRequest(e,5e3);if(n.cmd==="send_ack"){const c=n.payload;u.info("aibot",`event_result ack event=${e.event_id} status=${e.status} attempt=${r}/3 ack_event=${c.event_id??""} ack_status=${c.status??""}`);return}const h=n.payload;u.warn("aibot",`event_result rejected event=${e.event_id} status=${e.status} attempt=${r}/3 cmd=${n.cmd} code=${h.code??""} msg=${h.msg??""}`);return}catch(n){const h=n instanceof Error?n.message:String(n);if(u.warn("aibot",`event_result attempt failed event=${e.event_id} status=${e.status} attempt=${r}/3 err=${h}`),r===3){this.emitClientError(new Error(`event_result ack failed after 3 attempts: event=${e.event_id} status=${e.status}`));return}await new Promise(c=>setTimeout(c,750*r))}}}emitClientError(e){if(this.listenerCount("error")===0){u.warn("aibot",`Client error (no listeners): ${e.message}`);return}this.emit("error",e)}flushOutboundBuffer(){if(this.outboundBuffer.length===0||!this.ws||this.ws.readyState!==_.OPEN)return;const e=this.outboundBuffer;this.outboundBuffer=[];for(const{cmd:t,payload:i}of e){const a={cmd:t,seq:++this.seq,payload:i};try{this.ws.send(JSON.stringify(a))}catch{break}}}handleInvokeResult(e){const t=this.pendingInvokes.get(e.invoke_id);t&&(this.pendingInvokes.delete(e.invoke_id),clearTimeout(t.timer),e.code===0?t.resolve(e.data??null):t.reject(new Error(`agent_invoke error code=${e.code}: ${e.msg??""}`)))}rejectAllPendingInvokes(e){for(const[,t]of this.pendingInvokes)clearTimeout(t.timer),t.reject(new Error(`agent_invoke canceled: ${e}`));this.pendingInvokes.clear()}rejectAllPendingRequests(e){for(const[,t]of this.pendingRequests)clearTimeout(t.timer),t.reject(new Error(`request canceled: ${e}`));this.pendingRequests.clear()}cleanupSocket(){if(this.ws){try{this.ws.close()}catch{}this.ws=null}}startHeartbeat(){this.stopHeartbeat(),this.heartbeatTimer=setInterval(()=>{this.connected&&this.request("ping",{ts:Date.now()},{expected:["pong"],timeoutMs:5e3}).catch(()=>{this.connected&&(this.cleanupSocket(),this.attemptReconnect())})},this.heartbeatSec*1e3)}stopHeartbeat(){this.heartbeatTimer&&(clearInterval(this.heartbeatTimer),this.heartbeatTimer=null)}}export{d as AibotClient};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
class c{client;_status="ready";connectedAt;listeners=[];constructor(e){this.client=e,this.connectedAt=Date.now(),this.addInternalListener("close",()=>{this._status!=="closing"&&(this._status="reconnecting")}),this.addInternalListener("auth",()=>{this._status="ready"}),this.addInternalListener("disconnected",()=>{this._status!=="closing"&&this._status!=="closed"&&(this._status="reconnecting")})}get status(){return this._status}getStatusSnapshot(){return{status:this._status,connectedAt:this.connectedAt,reconnectAttempts:0}}sendEventAck(e){this.client.sendEventAck(e)}sendStreamChunk(e){this.client.sendStreamChunk(e)}sendMsg(e){this.client.sendMsg(e)}sendEventResult(e){this.client.sendEventResult(e)}sendLocalActionResult(e){this.client.sendLocalActionResult(e)}sendEventStopAck(e){this.client.sendEventStopAck(e)}sendEventStopResult(e){this.client.sendEventStopResult(e)}sendSessionActivitySet(e){this.client.sendSessionActivitySet(e)}sendCodexEvent(e){this.client.sendCodexEvent(e)}sendUpdateBindingCard(e){this.client.sendUpdateBindingCard(e)}sendPing(){this.client.sendPing()}sendEventState(e){this.client.sendEventState(e)}sendEventCancelResult(e){this.client.sendEventCancelResult(e)}sendQueueClearResult(e){this.client.sendQueueClearResult(e)}sendQueueSnapshot(e){this.client.sendQueueSnapshot(e)}agentInvoke(e,t,s){return this.client.agentInvoke(e,t,s)}sendStreamChunkRequest(e,t){return this.client.sendStreamChunkRequest(e,t)}sendText(e,t){return this.client.sendText(e,t)}sendMedia(e,t){return this.client.sendMedia(e,t)}deleteMessage(e,t,s){return this.client.deleteMessage(e,t,s)}sendEventResultRequest(e,t){return this.client.sendEventResultRequest(e,t)}sendCodexEventReliable(e,t){return this.client.request("codex_event",e,{expected:["send_ack","send_nack","error"],timeoutMs:t??2e4})}onEvent(e){return this.subscribe("event",e)}onLocalAction(e){return this.subscribe("localAction",e)}onStop(e){return this.subscribe("stop",e)}onRevoke(e){return this.subscribe("revoke",e)}onEventCancel(e){return this.subscribe("eventCancel",e)}onQueueClear(e){return this.subscribe("queueClear",e)}onKicked(e){return this.subscribe("kicked",e)}onError(e){return this.subscribe("error",e)}onClose(e){return this.subscribe("close",e)}onDisconnected(e){return this.subscribe("disconnected",e)}disconnect(){this._status="closing",this.removeAllListeners(),this.client.disconnect(),this._status="closed"}subscribe(e,t){return this.client.on(e,t),this.listeners.push({event:e,handler:t}),()=>{this.client.removeListener(e,t);const s=this.listeners.findIndex(n=>n.event===e&&n.handler===t);s>=0&&this.listeners.splice(s,1)}}addInternalListener(e,t){this.client.on(e,t),this.listeners.push({event:e,handler:t})}removeAllListeners(){for(const{event:e,handler:t}of this.listeners)this.client.removeListener(e,t);this.listeners.length=0}}export{c as AibotConnectionHandleImpl};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{AibotClient as u}from"./client.js";import{AibotConnectionHandleImpl as f}from"./connection-handle.js";import{log as a}from"../log/index.js";class b{_status="closed";get status(){return this._status}async connect(o,m={}){const{maxRetries:d,aborted:l,label:h}=m,s=h??o.agentId;this._status="connecting";let r=0;const p=o.apiKey.length>8?o.apiKey.slice(0,4)+"****"+o.apiKey.slice(-4):"****";for(;;){if(l?.())throw this._status="closed",new Error("connection aborted");const i=new u(o);try{const t=await i.connect();this._status="ready",a.info("connection-manager",`Connected: agent=${o.agentId} key=${p} adapter=${t.adapter_id??""}`);const e=new f(i);return e.onError(n=>{a.error(s,`Aibot connection error: ${n instanceof Error?n.message:n}`)}),e.onClose((n,c)=>{a.info(s,`Aibot connection closed: code=${n} reason=${c}`)}),e.onDisconnected(()=>{a.info(s,"Aibot disconnected, attempting reconnect...")}),e}catch(t){if(r++,i.disconnect(),d!==void 0&&r>d)throw this._status="closed",new Error(`connection failed after ${r} attempts: ${t instanceof Error?t.message:t}`);if(l?.())throw this._status="closed",new Error("connection aborted");const e=Math.min(1e3*2**Math.min(r-1,5),3e4),n=Math.floor(e*.2*Math.random());a.error("connection-manager",`Connect failed (attempt ${r}), retrying in ${e+n}ms: ${t instanceof Error?t.message:t}`),await new Promise(c=>setTimeout(c,e+n))}}}}export{b as ConnectionManager};
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{AibotClient as e}from"./client.js";import{ConnectionManager as r}from"./connection-manager.js";import{AibotConnectionHandleImpl as m}from"./connection-handle.js";export{e as AibotClient,m as AibotConnectionHandleImpl,r as ConnectionManager};
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{GRIX_HOME_ENV as s,DEFAULT_GRIX_HOME as t,resolveRuntimePaths as E,ensureRuntimeDirs as R}from"./paths.js";export{t as DEFAULT_GRIX_HOME,s as GRIX_HOME_ENV,R as ensureRuntimeDirs,E as resolveRuntimePaths};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{mkdir as r}from"node:fs/promises";import{join as e}from"node:path";import{homedir as n}from"node:os";const s="GRIX_CONNECTOR_HOME",a=e(n(),".grix");function d(i){const o=i||(process.env[s]?process.env[s]:a);return{rootDir:o,configDir:e(o,"config"),logDir:e(o,"log"),dataDir:e(o,"data"),serviceDir:e(o,"service"),allowlistFile:e(o,"allowlist.json"),stateFile:e(o,"service.json"),pidFile:e(o,"grix-connector.pid"),daemonLockFile:e(o,"daemon.lock.json"),daemonStatusFile:e(o,"daemon-status.json"),stdoutLogFile:e(o,"service","daemon.out.log"),stderrLogFile:e(o,"service","daemon.err.log"),contextsDir:e(o,"data","session-contexts"),hookSignalsPath:e(o,"data","hook-signals.json"),hookSignalsLogPath:e(o,"log","hook-signals.log"),elicitationRequestsDir:e(o,"data","elicitation-requests"),eventStatesDir:e(o,"data","event-states"),questionRequestsDir:e(o,"data","question-requests"),permissionRequestsDir:e(o,"data","permission-requests"),agentGlobalConfigsFile:e(o,"data","agent-global-configs.json")}}async function m(i){const o=[i.rootDir,i.configDir,i.logDir,i.dataDir,i.serviceDir];await Promise.all(o.map(t=>r(t,{recursive:!0})))}export{a as DEFAULT_GRIX_HOME,s as GRIX_HOME_ENV,m as ensureRuntimeDirs,d as resolveRuntimePaths};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
async function i(s){const{sessionContextStore:e,sessionID:n,agentID:o,transcriptPath:c,workingDir:r,maxAgeMs:a}=s,t=await e.inspectMatchingContext({sessionID:n,agentID:o,transcriptPath:c,workingDir:r,maxAgeMs:a});return t.status==="matched"&&t.context?.context?{status:"resolved",context:t.context.context,source:"session_context",reason:"matched from persisted session context"}:t.status==="stale"?{status:"unresolved",context:null,source:"session_context",reason:"session_context_stale"}:t.status==="transcript_mismatch"?{status:"unresolved",context:null,source:"session_context",reason:"session_context_transcript_mismatch"}:t.status==="cwd_mismatch"?{status:"unresolved",context:null,source:"session_context",reason:"session_context_cwd_mismatch"}:{status:"unresolved",context:null,source:"none",reason:"no_channel_context"}}export{i as resolveHookChannelContext};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{mkdir as a}from"node:fs/promises";import{join as i}from"node:path";import{readJSONFile as c,writeJSONFileAtomic as o}from"../util/json-file.js";class x{contextsDir;constructor(t){this.contextsDir=t}async init(){await a(this.contextsDir,{recursive:!0})}resolvePath(t,e){const s=r=>r.replace(/[^a-zA-Z0-9_-]/g,"-"),n=e?`--${s(e)}`:"";return i(this.contextsDir,`${s(t)}${n}.json`)}async put(t){const e={schema_version:1,session_id:t.session_id,agent_id:t.agent_id,transcript_path:t.transcript_path,cwd:t.cwd,updated_at:Date.now(),context:t.context},s=this.resolvePath(t.session_id,t.agent_id);return await o(s,e),e}async get(t,e){const s=this.resolvePath(t,e?.agentId),n=c(s);return!n||typeof n!="object"?null:this.normalizeEntry(n)}async inspectMatchingContext(t){const e=await this.get(t.sessionID,{agentId:t.agentID});if(!e)return{status:"missing",context:null};const s=t.maxAgeMs??1800*1e3;return Date.now()-e.updated_at>s?{status:"stale",context:e}:t.transcriptPath&&e.transcript_path&&e.transcript_path!==t.transcriptPath?{status:"transcript_mismatch",context:e}:t.workingDir&&e.cwd&&e.cwd!==t.workingDir?{status:"cwd_mismatch",context:e}:e.context?.chat_id?{status:"matched",context:e}:{status:"missing",context:e}}normalizeEntry(t){if(!t.context||typeof t.context!="object")return null;const e=t.context;return e.chat_id?{schema_version:1,session_id:t.session_id??"",agent_id:t.agent_id,transcript_path:t.transcript_path,cwd:t.cwd,updated_at:t.updated_at??0,context:{chat_id:e.chat_id,message_id:e.message_id??""}}:null}}export{x as ChannelContextStore};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{ChannelContextStore as o}from"./channel-context-store.js";import{extractLatestGrixChannelTag as r,extractChannelContextFromJson as x}from"./transcript-channel-context.js";import{resolveHookChannelContext as C}from"./channel-context-resolution.js";export{o as ChannelContextStore,x as extractChannelContextFromJson,r as extractLatestGrixChannelTag,C as resolveHookChannelContext};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const i=/<channel[^>]*chat_id="([^"]*)"[^>]*message_id="([^"]*)"/g;function d(t){let n=null;const e=new RegExp(i.source,i.flags);let a;for(;(a=e.exec(t))!==null;)n={chat_id:a[1],message_id:a[2]};return n}function r(t){try{const e=JSON.parse(t).meta;if(e?.chat_id&&e?.message_id)return{chat_id:String(e.chat_id),message_id:String(e.message_id),event_id:e.event_id?String(e.event_id):void 0,sender_id:e.sender_id?String(e.sender_id):void 0}}catch{}return null}export{r as extractChannelContextFromJson,d as extractLatestGrixChannelTag};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{mkdir as m}from"node:fs/promises";import{join as c,basename as l}from"node:path";import{homedir as u}from"node:os";import{listFiles as _}from"./list-files.js";function d(){return u()}async function f(a,s){const o=a.params??{},t=o.parent_id?.trim(),i=o.show_hidden??!1;let e;t?e=t:e=s.resolveCwd()||d();try{return{status:"ok",result:{files:await _(e,i),current_path:e}}}catch(r){return r?.code==="ENOENT"?{status:"failed",error_code:"path_not_found",error_msg:`Directory not found: ${e}`}:r?.code==="ENOTDIR"?{status:"failed",error_code:"not_a_directory",error_msg:`Not a directory: ${e}`}:{status:"failed",error_code:"list_failed",error_msg:String(r.message||r)}}}async function p(a,s){const o=a.params??{},t=o.name?.trim(),i=o.parent_id?.trim();if(!t)return{status:"failed",error_code:"name_required",error_msg:"Folder name is required"};if(/[/\\]/.test(t))return{status:"failed",error_code:"invalid_name",error_msg:"Folder name must not contain path separators"};const e=i||s.resolveCwd()||d(),r=c(e,t);try{return await m(r),{status:"ok",result:{id:r,name:l(r),is_directory:!0}}}catch(n){return n?.code==="EEXIST"?{status:"failed",error_code:"already_exists",error_msg:`Folder already exists: ${r}`}:{status:"failed",error_code:"create_failed",error_msg:String(n.message||n)}}}export{p as handleCreateFolderAction,f as handleFileListAction};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{readdir as r,stat as m}from"node:fs/promises";import{join as l,extname as d}from"node:path";const x={pdf:"application/pdf",doc:"application/msword",docx:"application/vnd.openxmlformats-officedocument.wordprocessingml.document",xls:"application/vnd.ms-excel",xlsx:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",ppt:"application/vnd.ms-powerpoint",pptx:"application/vnd.openxmlformats-officedocument.presentationml.presentation",txt:"text/plain",md:"text/markdown",csv:"text/csv",json:"application/json",xml:"application/xml",yaml:"text/yaml",yml:"text/yaml",html:"text/html",css:"text/css",js:"text/javascript",ts:"text/typescript",zip:"application/zip",rar:"application/x-rar-compressed","7z":"application/x-7z-compressed",tar:"application/x-tar",gz:"application/gzip",jpg:"image/jpeg",jpeg:"image/jpeg",png:"image/png",gif:"image/gif",webp:"image/webp",svg:"image/svg+xml",mp4:"video/mp4",mov:"video/quicktime",avi:"video/x-msvideo",mkv:"video/x-matroska",webm:"video/webm",mp3:"audio/mpeg",wav:"audio/wav",flac:"audio/flac",aac:"audio/aac"};function n(a){const p=d(a).slice(1).toLowerCase();return x[p]}async function f(a,p=!1){const c=await r(a,{withFileTypes:!0}),s=[];for(const i of c){if(!p&&i.name.startsWith("."))continue;const t=l(a,i.name),e={id:t,name:i.name,is_directory:i.isDirectory()};try{if(i.isDirectory()){const o=await m(t);e.modified_at=o.mtime.toISOString()}else{const o=await m(t);e.size=o.size,e.modified_at=o.mtime.toISOString(),e.mime_type=n(i.name)}}catch{}s.push(e)}return s.sort((i,t)=>i.is_directory!==t.is_directory?i.is_directory?-1:1:i.name.localeCompare(t.name)),s}export{f as listFiles,n as resolveMimeType};
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{realpath as c,stat as l,mkdir as m}from"node:fs/promises";import{resolve as _,join as f}from"node:path";import{realHomeDir as u}from"./utils.js";async function h(e,i){if(!e.name||typeof e.name!="string")return{status:"failed",error_code:"name_required",error_msg:"Folder name is required"};const t=e.name.trim();if(!t)return{status:"failed",error_code:"name_empty",error_msg:"Folder name cannot be empty or whitespace"};if(/[/\\]/.test(t))return{status:"failed",error_code:"name_invalid",error_msg:"Folder name cannot contain path separators"};const s=i.fallbackDir??u(),o=e.parent_id?_(e.parent_id):s;let a;try{a=await c(o)}catch{return{status:"failed",error_code:"parent_not_found",error_msg:`Parent directory not found: ${o}`}}try{if(!(await l(a)).isDirectory())return{status:"failed",error_code:"parent_not_directory",error_msg:`Parent path is not a directory: ${o}`}}catch{return{status:"failed",error_code:"parent_not_accessible",error_msg:`Cannot access parent directory: ${o}`}}const n=f(a,t);try{await m(n)}catch(r){const d=r instanceof Error?r.message:String(r);return r&&typeof r=="object"&&"code"in r&&r.code==="EEXIST"?{status:"failed",error_code:"already_exists",error_msg:`A file or folder named "${t}" already exists`}:{status:"failed",error_code:"create_failed",error_msg:d}}return{status:"ok",result:{folder:{id:n,name:t,is_directory:!0}}}}export{h as handleCreateFolderAction};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{handleFileListAction as r}from"./list-handler.js";import{handleCreateFolderAction as i}from"./create-folder.js";import{listFiles as m}from"./list-files.js";import{realHomeDir as f}from"./utils.js";export{i as handleCreateFolderAction,r as handleFileListAction,m as listFiles,f as realHomeDir};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{access as d,readdir as f,stat as u}from"node:fs/promises";import{join as x}from"node:path";const g={pdf:"application/pdf",doc:"application/msword",docx:"application/vnd.openxmlformats-officedocument.wordprocessingml.document",xls:"application/vnd.ms-excel",xlsx:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",ppt:"application/vnd.ms-powerpoint",pptx:"application/vnd.openxmlformats-officedocument.presentationml.presentation",txt:"text/plain",md:"text/markdown",csv:"text/csv",json:"application/json",xml:"application/xml",yaml:"text/yaml",yml:"text/yaml",html:"text/html",css:"text/css",js:"application/javascript",ts:"application/typescript",zip:"application/zip",rar:"application/x-rar-compressed","7z":"application/x-7z-compressed",tar:"application/x-tar",gz:"application/gzip",jpg:"image/jpeg",jpeg:"image/jpeg",png:"image/png",gif:"image/gif",webp:"image/webp",svg:"image/svg+xml",mp4:"video/mp4",mov:"video/quicktime",avi:"video/x-msvideo",mkv:"video/x-matroska",webm:"video/webm",mp3:"audio/mpeg",wav:"audio/wav",flac:"audio/flac",aac:"audio/aac"};function w(t){const i=t.includes(".")?t.split(".").pop().toLowerCase():"";return g[i]}async function j(){const t=[];for(let i=65;i<=90;i++){const e=String.fromCharCode(i),n=`${e}:\\`;try{await d(n),t.push({id:n,name:`${e}:`,is_directory:!0})}catch{}}return t}async function _(t,i){const e=await f(t,{withFileTypes:!0}),n=i?.showHidden??!1,l=v(i?.allowedExtensions),s=[];for(const o of e){if(!n&&o.name.startsWith("."))continue;const a=x(t,o.name);let r=o.isDirectory(),p,m;try{const c=await u(a);r=c.isDirectory(),r||(p=c.size),m=c.mtime.toISOString()}catch{}!r&&!y(o.name,l)||s.push({id:a,name:o.name,is_directory:r,size:p,modified_at:m,mime_type:r?void 0:w(o.name)})}return s.sort((o,a)=>o.is_directory!==a.is_directory?o.is_directory?-1:1:o.name.toLowerCase().localeCompare(a.name.toLowerCase())),s}function v(t){if(!t||t.length===0)return null;const i=t.map(e=>e.trim().toLowerCase()).filter(e=>e.length>0).map(e=>e.startsWith(".")?e:`.${e}`);return i.length===0?null:new Set(i)}function y(t,i){if(i==null)return!0;const e=t.lastIndexOf(".");if(e<=0||e>=t.length-1)return!1;const n=t.substring(e).toLowerCase();return i.has(n)}export{_ as listFiles,j as listWindowsDrives};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{realpath as a,stat as n}from"node:fs/promises";import{resolve as c}from"node:path";import{listFiles as l,listWindowsDrives as d}from"./list-files.js";import{realHomeDir as f}from"./utils.js";async function g(t,s){if(process.platform==="win32"&&!t.parent_id)return{status:"ok",result:{files:await d(),current_path:""}};const i=s.fallbackDir??f(),e=t.parent_id?c(t.parent_id):i;let o;try{o=await a(e)}catch{return{status:"failed",error_code:"path_not_found",error_msg:`Directory not found: ${e}`}}try{if(!(await n(o)).isDirectory())return{status:"failed",error_code:"not_a_directory",error_msg:`Path is not a directory: ${e}`}}catch{return{status:"failed",error_code:"path_not_accessible",error_msg:`Cannot access path: ${e}`}}try{return{status:"ok",result:{files:await l(o,{showHidden:t.show_hidden,allowedExtensions:t.allowed_extensions}),current_path:o}}}catch(r){return{status:"failed",error_code:"list_failed",error_msg:r instanceof Error?r.message:String(r)}}}export{g as handleFileListAction};
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{homedir as e}from"node:os";function o(){const r=process.env.USER||process.env.LOGNAME||process.env.USERNAME;if(r){if(process.platform==="darwin")return`/Users/${r}`;if(process.platform==="linux")return`/home/${r}`;if(process.platform==="win32")return`C:\\Users\\${r}`}return e()}export{o as realHomeDir};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{mkdir as S}from"node:fs/promises";import{open as d,readFile as g,unlink as s}from"node:fs/promises";import{join as f}from"node:path";import{randomUUID as v}from"node:crypto";import{readJSONFile as k,writeJSONFileAtomic as c}from"../util/json-file.js";import{appendRotatingFileSync as w}from"../log/rotation.js";const l=20,p=3e4,y=5e3;class U{statePath;logPath;lockPath;rotationOptions;constructor(e,a,n=e+".lock",o={}){this.statePath=e,this.logPath=a,this.lockPath=n,this.rotationOptions=o}get filePath(){return this.statePath}async recordHookEvent(e){const a=P(e);await this.withStateLock(async n=>{n.recent_events.push(a),n.recent_events.sort((o,i)=>o.event_at-i.event_at),n.recent_events.length>l&&(n.recent_events=n.recent_events.slice(-l)),n.latest_event=a,n.updated_at=Date.now()}),this.appendEventLog(a)}async readState(){const e=k(this.statePath);return!e||typeof e!="object"?this.emptyState():this.normalizeState(e)}async reset(){await this.withStateLock(async()=>{await c(this.statePath,this.emptyState())})}emptyState(){return{schema_version:1,updated_at:0,latest_event:null,recent_events:[]}}normalizeState(e){const a=Array.isArray(e.recent_events)?e.recent_events:[];return{schema_version:1,updated_at:e.updated_at??0,latest_event:e.latest_event?_(e.latest_event):null,recent_events:a.filter(n=>typeof n=="object"&&n!==null).map(_)}}async withStateLock(e){await S(f(this.statePath,"..").replace(/\/[^/]+$/,""),{recursive:!0}).catch(()=>{});const a=Date.now()+y;let n=!1;for(;!n&&Date.now()<a;)try{await(await d(this.lockPath,"wx")).close(),n=!0}catch(o){if(o.code==="EEXIST"){const m=await g(this.lockPath,"utf8").catch(()=>""),r=Number(m);!isNaN(r)&&Date.now()-r>p?await s(this.lockPath).catch(()=>{}):await new Promise(u=>setTimeout(u,100))}else throw o}if(!n)throw new Error("hook-signal-store: lock acquisition timeout");try{await D(this.lockPath,String(Date.now()));const o=await this.readState(),i=await e(o);return await c(this.statePath,o),i}finally{await s(this.lockPath).catch(()=>{})}}appendEventLog(e){const a=E(e),n=`${new Date(e.event_at).toISOString()} ${a}
|
|
2
|
+
`;try{w(this.logPath,n,this.rotationOptions)}catch{}}}function h(t){if(t!=null){if(typeof t=="string")return t;try{return JSON.stringify(t)}catch{return String(t)}}}function P(t){return{event_id:v(),hook_event_name:String(t.hook_event_name??""),event_at:Date.now(),detail:T(t),tool_input:h(t.tool_input)}}function _(t){return{event_id:String(t.event_id??""),hook_event_name:String(t.hook_event_name??""),event_at:t.event_at??0,detail:String(t.detail??""),tool_input:h(t.tool_input)}}function E(t){const e=t.hook_event_name,a=t.detail?`:${t.detail}`:"";return`${e}${a}`}function T(t){switch(String(t.hook_event_name??"")){case"SessionStart":return String(t.source??"");case"PreToolUse":case"PermissionRequest":case"PostToolUse":case"PostToolUseFailure":case"PermissionDenied":return String(t.tool_name??t.reason??"");case"Elicitation":return String(t.mode??"");case"ElicitationResult":return String(t.action??t.mode??"");case"Notification":return String(t.matcher??t.notification_type??t.type??"");case"StopFailure":return String(t.stop_reason??t.error??t.detail??"");case"Stop":return String(t.stop_reason??"");case"PreCompact":case"PostCompact":return String(t.trigger??"");case"ConfigChange":return String(t.source??"");default:return""}}async function D(t,e){const{writeFile:a}=await import("node:fs/promises");await a(t,e,"utf8")}export{U as HookSignalStore};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{HookSignalStore as e}from"./hook-signal-store.js";export{e as HookSignalStore};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import r from"node:path";import{appendRotatingFile as o}from"./rotation.js";class h{filePath;rotationOptions;writeChain=Promise.resolve();constructor(t,i,e){const n=i.replace(/[^a-zA-Z0-9._-]+/g,"-");this.filePath=r.join(t,"bridges",`${n}.jsonl`),this.rotationOptions={maxBytes:e?.maxBytes??50*1024*1024,maxFiles:e?.maxFiles??5}}logOutbound(t,i,e){this.writeEntry({ts:new Date().toISOString(),direction:"outbound",method:t,id:i,summary:e})}logInbound(t,i,e){this.writeEntry({ts:new Date().toISOString(),direction:"inbound",method:t,id:i,summary:e})}logStderr(t){this.writeEntry({ts:new Date().toISOString(),direction:"stderr",summary:t.slice(0,500)})}logLifecycle(t){this.writeEntry({ts:new Date().toISOString(),direction:"lifecycle",summary:t})}writeEntry(t){const i=this.writeChain;this.writeChain=i.catch(()=>{}).then(async()=>{await o(this.filePath,`${JSON.stringify(t)}
|
|
2
|
+
`,this.rotationOptions)}).catch(()=>{})}async close(){await this.writeChain}}export{h as BridgeEventLog};
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import{readFile as p}from"node:fs/promises";import g from"node:path";import{appendRotatingFile as f}from"./rotation.js";const u=2e3;function d(o){return o.length<=u?o:o.slice(0,u)+`... [truncated, total ${o.length} chars]`}class y{logsDir;rotationOptions;writeQueues=new Map;constructor(t,e={}){this.logsDir=t,this.rotationOptions=e}resolveFilePath(t){const e=t.replace(/[^a-zA-Z0-9._-]+/g,"-");return g.join(this.logsDir,"sessions",e,"conversation.jsonl")}enqueueWrite(t,e){const n=(this.writeQueues.get(t)??Promise.resolve()).catch(()=>{}).then(e).catch(s=>{console.error(`[conversation-log] Write failed for session ${t}: ${s instanceof Error?s.message:s}`)});this.writeQueues.set(t,n),n.finally(()=>{this.writeQueues.get(t)===n&&this.writeQueues.delete(t)})}append(t,e){this.enqueueWrite(t,async()=>{const i=this.resolveFilePath(t);await f(i,`${JSON.stringify(e)}
|
|
2
|
+
`,this.rotationOptions)})}async readHistory(t,e=20){const i=this.resolveFilePath(t);let n;try{n=await p(i,"utf8")}catch{return[]}const h=n.split(`
|
|
3
|
+
`).filter(r=>r.trim()).slice(-e),a=[];for(const r of h)try{const c=JSON.parse(r),l=(c.content??"").trim();l&&!m(l)&&a.push(c)}catch{}return a}logInbound(t,e){this.append(t,{ts:Date.now(),dir:"inbound",event_id:e.event_id??"",msg_id:e.msg_id??"",sender_id:e.sender_id??"",msg_type:e.msg_type??1,content_len:e.content?.length??0,content:e.content?d(e.content):void 0})}logOutbound(t,e,i,n){this.append(t,{ts:Date.now(),dir:"outbound",event_id:e,kind:i,text_len:n?.length??0,content:n?d(n):void 0})}logResult(t,e,i,n){this.append(t,{ts:Date.now(),dir:"result",event_id:e,status:i,msg:n})}async close(){const t=[...this.writeQueues.values()];await Promise.all(t.map(e=>e.catch(()=>{})))}}function m(o){const t=o.trim();return t.startsWith("grix://")||t.includes("[[Tool]")||t.includes("[[exec-approval-resolution")}export{y as ConversationLog};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{GRIX_PATHS as t,ensureGrixDirs as r,initLogger as L,log as i,setLogLevel as n,setTagLogLevel as g}from"./logger.js";import{DEFAULT_LOG_MAX_BYTES as a,DEFAULT_LOG_MAX_FILES as p,LOG_MAX_BYTES_ENV as l,LOG_MAX_FILES_ENV as s,resolveLogRotationOptions as A,rotateFileIfNeeded as E,appendRotatingFile as F,rotateFileIfNeededSync as d,appendRotatingFileSync as f,installProcessLogRotation as S}from"./rotation.js";import{ConversationLog as G}from"./conversation-log.js";import{AgentApiPacketLog as m}from"./packet-log.js";import{BridgeEventLog as I}from"./bridge-event-log.js";export{m as AgentApiPacketLog,I as BridgeEventLog,G as ConversationLog,a as DEFAULT_LOG_MAX_BYTES,p as DEFAULT_LOG_MAX_FILES,t as GRIX_PATHS,l as LOG_MAX_BYTES_ENV,s as LOG_MAX_FILES_ENV,F as appendRotatingFile,f as appendRotatingFileSync,r as ensureGrixDirs,L as initLogger,S as installProcessLogRotation,i as log,A as resolveLogRotationOptions,E as rotateFileIfNeeded,d as rotateFileIfNeededSync,n as setLogLevel,g as setTagLogLevel};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import{createWriteStream as d,mkdirSync as v,existsSync as O}from"node:fs";import{join as S}from"node:path";import{resolveRuntimePaths as w}from"../config/paths.js";const l=w(),u={base:l.rootDir,config:l.configDir,log:l.logDir,data:l.dataDir};function I(){for(const e of Object.values(u))O(e)||v(e,{recursive:!0})}const f={trace:0,debug:1,info:2,warn:3,error:4};function a(e,n){if(!e)return n;const o=e.trim().toLowerCase();return o in f?o:n}function x(e){const n=new Map;if(!e)return n;for(const o of e.split(",")){const r=o.indexOf("=");if(r<0)continue;const s=o.slice(0,r).trim(),m=a(o.slice(r+1),"info");s&&n.set(s,m)}return n}let g=a(process.env.GRIX_CONNECTOR_LOG_LEVEL,"info");const p=x(process.env.GRIX_CONNECTOR_LOG_TAG_LEVELS);function _(e){return p.get(e)??g}function t(e,n){return f[n]>=f[_(e)]}function T(e){g=e}function $(e,n){p.set(e,n)}let L=null;function b(){const e=new Date().toISOString().slice(0,10),n=S(u.log,`grix-connector-${e}.log`);L=d(n,{flags:"a"})}function E(){return new Date().toISOString().slice(11,19)}function i(e){try{L?.write(e)}catch{}}function c(e,n,o,r){const s=e==="info"?"":`${e.toUpperCase()} `;return`${E()} [${n}] ${s}${o}${r.length?" "+r.map(String).join(" "):""}`}const C={trace(e,n,...o){if(!t(e,"trace"))return;const r=c("trace",e,n,o);console.log(r),i(r+`
|
|
2
|
+
`)},debug(e,n,...o){if(!t(e,"debug"))return;const r=c("debug",e,n,o);console.log(r),i(r+`
|
|
3
|
+
`)},info(e,n,...o){if(!t(e,"info"))return;const r=c("info",e,n,o);console.log(r),i(r+`
|
|
4
|
+
`)},warn(e,n,...o){if(!t(e,"warn"))return;const r=c("warn",e,n,o);console.warn(r),i(r+`
|
|
5
|
+
`)},error(e,n,...o){if(!t(e,"error"))return;const r=c("error",e,n,o);console.error(r),i(r+`
|
|
6
|
+
`)}};export{u as GRIX_PATHS,I as ensureGrixDirs,b as initLogger,C as log,T as setLogLevel,$ as setTagLogLevel};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import u from"node:path";import{appendRotatingFile as h}from"./rotation.js";const l=128*1024*1024,m=5,d=200,p=500;class f{filePath;rotationOptions;writeChain=Promise.resolve();contextSamples=new Map;constructor(e,t){this.filePath=u.join(e,"agentapi-packets.jsonl"),this.rotationOptions={maxBytes:t?.maxBytes??l,maxFiles:t?.maxFiles??m}}logInbound(e){this.writeEntry({...e,direction:"inbound",ts:new Date().toISOString()})}logOutbound(e){this.writeEntry({...e,direction:"outbound",ts:new Date().toISOString()})}logInboundPacket(e,t,s){if(e==="local_action"&&g(s)){const n=i(s,"session_id")??"",c=Date.now(),_=this.contextSamples.get(n)??0;if(c-_<p)return;this.contextSamples.set(n,c)}if(e==="local_action_ack")return;const o=s;this.writeEntry({direction:"inbound",ts:new Date().toISOString(),cmd:e,seq:t,eventId:i(o,"event_id"),sessionId:i(o,"session_id"),threadId:i(o,"thread_id"),msgId:i(o,"msg_id"),actionId:i(o,"action_id"),codexMethod:i(o,"codex_method"),invokeId:i(o,"invoke_id"),payloadSummary:r(e,s)})}logOutboundPacket(e,t,s,o){const n=s;this.writeEntry({direction:"outbound",ts:new Date().toISOString(),cmd:e,seq:t,sendStatus:o,eventId:i(n,"event_id"),sessionId:i(n,"session_id"),threadId:i(n,"thread_id"),msgId:i(n,"msg_id"),actionId:i(n,"action_id"),codexMethod:i(n,"codex_method"),invokeId:i(n,"invoke_id"),payloadSummary:r(e,s)})}writeEntry(e){const t=this.writeChain;this.writeChain=t.catch(()=>{}).then(async()=>{e.payloadSummary&&e.payloadSummary.length>d&&(e.payloadSummary=e.payloadSummary.slice(0,d)+"..."),await h(this.filePath,`${JSON.stringify(e)}
|
|
2
|
+
`,this.rotationOptions)}).catch(()=>{})}async close(){await this.writeChain}}function g(a){return a?.action_type==="get_context"}function i(a,e){const t=a?.[e];if(typeof t=="string"&&t.trim())return t.trim()}function r(a,e){const t=e;if(!t)return"";switch(a){case"auth":return`agent_id=${t.agent_id??""} client_type=${t.client_type??""}`;case"auth_ack":return`code=${t.code??""} adapter_id=${t.adapter_id??""}`;case"event_msg":case"event_ack":case"event_result":return`event_id=${t.event_id??""} status=${t.status??""}`;case"send_msg":case"client_stream_chunk":return`session_id=${t.session_id??""} is_finish=${t.is_finish??""}`;case"local_action":return`action_type=${t.action_type??""} action_id=${t.action_id??""}`;case"local_action_result":return`action_id=${t.action_id??""} status=${t.status??""}`;case"codex_event":return`codex_method=${t.codex_method??""} seq=${t.codex_sequence??""}`;case"agent_invoke":return`action=${t.action??""} invoke_id=${t.invoke_id??""}`;case"agent_invoke_result":return`invoke_id=${t.invoke_id??""} code=${t.code??""}`;case"session_activity_set":return`session_id=${t.session_id??""} kind=${t.kind??""} active=${t.active??""}`;default:return""}}export{f as AgentApiPacketLog};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{appendFileSync as B,existsSync as y,mkdirSync as x,renameSync as d,statSync as S,unlinkSync as L}from"node:fs";import{appendFile as w,mkdir as g,rename as _,rm as N,stat as l}from"node:fs/promises";import m from"node:path";const O=10*1024*1024,A=5,I="GRIX_CONNECTOR_LOG_MAX_BYTES",T="GRIX_CONNECTOR_LOG_MAX_FILES";function p(t,r){const e=typeof t=="number"?t:Number.parseInt(String(t??""),10);return Number.isFinite(e)&&e>0?Math.floor(e):r}function G(t=process.env){return{maxBytes:p(t[I],O),maxFiles:p(t[T],A)}}function E(t={}){const r=G();return{maxBytes:p(t.maxBytes,r.maxBytes),maxFiles:p(t.maxFiles,r.maxFiles)}}function u(t,r){return`${t}.${r}`}async function R(t,r=0,e={}){const{maxBytes:n,maxFiles:s}=E(e),f=await l(t).catch(o=>{if(o.code==="ENOENT")return null;throw o});if(!(!f||f.size+r<=n)){await g(m.dirname(t),{recursive:!0}),await N(u(t,s),{force:!0});for(let o=s-1;o>=1;o-=1){const c=u(t,o),i=u(t,o+1);await _(c,i).catch(a=>{if(a.code!=="ENOENT")throw a})}await _(t,u(t,1))}}async function Y(t,r,e={}){const n=typeof r=="string"?r:Buffer.from(r),s=typeof n=="string"?Buffer.byteLength(n):n.byteLength;await g(m.dirname(t),{recursive:!0}),await R(t,s,e),await w(t,n)}function X(t,r=0,e={}){const{maxBytes:n,maxFiles:s}=E(e);if(!y(t)||S(t).size+r<=n)return;x(m.dirname(t),{recursive:!0});const o=u(t,s);y(o)&&L(o);for(let c=s-1;c>=1;c-=1){const i=u(t,c);y(i)&&d(i,u(t,c+1))}d(t,u(t,1))}function b(t,r,e={}){const n=Buffer.from(r);x(m.dirname(t),{recursive:!0}),X(t,n.byteLength,e),B(t,n)}function $(t,r,e={}){F(process.stdout,t,e),F(process.stderr,r,e)}function F(t,r,e){const n=t.write.bind(t);t.write=((s,f,o)=>{const c=typeof f=="function"?f:o;try{const a=M(s,typeof f=="string"?f:"utf8");return b(r,a,e),c?.(null),!0}catch(i){return n(`grix-connector log write failed: ${i instanceof Error?i.message:String(i)}
|
|
2
|
+
`),c?.(i instanceof Error?i:new Error(String(i))),!1}})}function M(t,r){return Buffer.isBuffer(t)?t:t instanceof Uint8Array?Buffer.from(t):Buffer.from(String(t??""),r)}export{O as DEFAULT_LOG_MAX_BYTES,A as DEFAULT_LOG_MAX_FILES,I as LOG_MAX_BYTES_ENV,T as LOG_MAX_FILES_ENV,Y as appendRotatingFile,b as appendRotatingFileSync,$ as installProcessLogRotation,G as resolveLogRotationOptions,R as rotateFileIfNeeded,X as rotateFileIfNeededSync};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{splitTextForAibotProtocol as l}from"../protocol/protocol-text.js";import{PHASE2_TOOL_NAMES as f,ACCESS_CONTROL_ACTION_MAP as a}from"./tools.js";const v=f;function A(n){return v.has(n)}function E(n,e,t){switch(e){case"grix_reply":return p(n,t);case"grix_complete":return S(n,t);case"grix_event_ack":return m(n,t);case"grix_composing":return g(n,t);case"grix_access_control":return x(n,t);case"grix_status":return h(n);default:return c(`\u672A\u77E5\u4E8B\u4EF6\u5DE5\u5177: ${e}`)}}function p(n,e){const t=String(e.event_id??""),i=String(e.session_id??""),s=String(e.text??""),u=e.quoted_message_id;if(!i)return c("\u7F3A\u5C11\u5FC5\u586B\u53C2\u6570: session_id");if(!s)return c("\u7F3A\u5C11\u5FC5\u586B\u53C2\u6570: text");const r=l(s),_=`reply_${t||"proactive"}_${Date.now()}`;if(!t){for(let o=0;o<r.length;o++)n.sendMsg({session_id:i,client_msg_id:`${_}_${o+1}`,msg_type:1,content:r[o],quoted_message_id:o===0?u:void 0});return d({ok:!0,direct:!0,chunks:r.length,client_msg_id:_})}for(let o=0;o<r.length;o++)n.sendStreamChunk({event_id:t||void 0,session_id:i,delta_content:r[o],chunk_seq:o+1,is_finish:!1,client_msg_id:_,quoted_message_id:o===0?u:void 0});return n.sendStreamChunk({event_id:t||void 0,session_id:i,delta_content:"",chunk_seq:r.length+1,is_finish:!0,client_msg_id:_}),d({ok:!0,chunks:r.length,client_msg_id:_})}function S(n,e){const t=String(e.event_id??""),i=String(e.status??""),s=e.msg;return t?["responded","canceled","failed"].includes(i)?(n.sendEventResult({event_id:t,status:i,msg:s}),d({ok:!0,event_id:t,status:i})):c("status \u5FC5\u987B\u4E3A responded\u3001canceled \u6216 failed"):c("\u7F3A\u5C11\u5FC5\u586B\u53C2\u6570: event_id")}function m(n,e){const t=String(e.event_id??""),i=e.session_id;return t?(n.sendEventAck({event_id:t,session_id:i,received_at:Date.now()}),d({ok:!0,event_id:t})):c("\u7F3A\u5C11\u5FC5\u586B\u53C2\u6570: event_id")}function g(n,e){const t=String(e.session_id??""),i=e.active===!0,s=e.event_id;return t?(n.sendSessionActivitySet({session_id:t,kind:"composing",active:i,ref_event_id:s,ttl_ms:i?3e4:void 0}),d({ok:!0,session_id:t,active:i})):c("\u7F3A\u5C11\u5FC5\u586B\u53C2\u6570: session_id")}function x(n,e){const t=String(e.action??""),i=a[t];if(!i)return c(`\u672A\u77E5 access_control action: ${t}`);const s={};return e.code!=null&&(s.code=e.code),e.sender_id!=null&&(s.sender_id=e.sender_id),e.policy!=null&&(s.policy=e.policy),{content:[{type:"text",text:"__ASYNC_ACCESS_CONTROL__"}],_async:{verb:i,payload:s}}}function h(n){const e=n.status,t=n.getStatusSnapshot();return d({connected:e==="ready",status:e,connected_at:t.connectedAt})}function d(n){return{content:[{type:"text",text:JSON.stringify(n)}],isError:!1}}function c(n){return{content:[{type:"text",text:n}],isError:!0}}export{v as EVENT_TOOL_NAMES,E as executeEventTool,A as isEventTool};
|