@tbox.cn/tboxcode-dsh-sdk 0.1.0-rc.13 → 0.1.0-rc.15

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/lib/index.d.ts CHANGED
@@ -81,7 +81,7 @@ declare class Supervisor {
81
81
  //#endregion
82
82
  //#region src/wire.d.ts
83
83
  /**
84
- * wire 契约(手写恰两处之二——sdk 对外面;规格真源 = dsh 0.2.1-rc.2 源码形状,
84
+ * wire 契约(手写恰两处之二——sdk 对外面;规格真源 = dsh 0.2.2-rc.1 源码形状(wire 形状较 0.2.1 未变),
85
85
  * 逐字段锚点见各类型注释;verify:upgrade 第五项帧比对守护漂移)。
86
86
  *
87
87
  * 编译域 / wire 域二分(bridge-typing-boundary-hardening):本文件是 wire 域——
@@ -470,6 +470,10 @@ type GatewayLogger = {
470
470
  };
471
471
  /** followup 鸭子面(桥经 cast 调用——载体一 host agent 同款,见 SessionChannel#runPromptPath)。 */
472
472
  type GatewayAgentLike = AgentLike & {
473
+ readonly model: {
474
+ current(): ConfigModelSelection | undefined;
475
+ select(selection: ConfigModelSelection): void | Promise<void>;
476
+ };
473
477
  followup(message: {
474
478
  id: string;
475
479
  content: readonly unknown[];
@@ -482,9 +486,12 @@ interface RemoteSessionState {
482
486
  readonly agent: GatewayAgentLike;
483
487
  header: WireSessionHeader;
484
488
  projections: WireJson;
485
- /** installModelSelection 注册的 'agent/request' 钩子(selection 请求时点提取)。 */
486
- selectionProbe: ((payload: unknown, next: () => Promise<unknown>) => Promise<unknown>) | undefined;
487
- lastPushedSelection: string | undefined;
489
+ /** select 回执/本地记录的选择(优先于 projection——请求时点语义)。 */
490
+ pendingSelection?: ConfigModelSelection;
491
+ /** pendingSelection 写入时的 projection 水位(snapshot 清 pending 的判据)。 */
492
+ pendingAtSeq?: number;
493
+ /** 部署默认行快照(远端 agent 缺失 route 时的网关侧兜底)。 */
494
+ fallbackSelection?: ConfigModelSelection;
488
495
  /** 挂起 prompt 记账(pushPrompt 置位 / turn/end 复位——中断状态机观测面)。 */
489
496
  promptInflight: boolean;
490
497
  }
package/lib/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import{MODE_TABLE as e,classify as t,isIgnorable as n}from"@tbox.cn/dsh-acp-bridge-core";import{spawn as r}from"node:child_process";import{randomUUID as i}from"node:crypto";import a,{WebSocketServer as o}from"ws";var s=class extends Error{kind;report;constructor(e,t,n){super(t),this.name=`SupervisorDeploymentError`,this.kind=e,this.report=n}};function c(e){let t;try{t=JSON.parse(e)}catch{return null}if(typeof t!=`object`||!t||Array.isArray(t))return null;let n=t;return n.v===1&&(n.event===`standby`||n.event===`ready`&&typeof n.port==`number`&&typeof n.token==`string`)?t:null}async function l(e,t){let n=await fetch(`http://127.0.0.1:${e}/connect?token=${encodeURIComponent(t)}&interactions=answer`,{redirect:`manual`});if(n.status!==302)throw Error(`/connect 凭据交换期待 302,实得 ${n.status}(port ${e})`);let r=n.headers.get(`set-cookie`);if(r===null)throw Error(`/connect 凭据交换缺 Set-Cookie`);let i=r.split(`;`)[0]??``;if(i===``)throw Error(`/connect 凭据交换缺 Set-Cookie`);return i}function u(e){return e.replace(/((?:token|access_token|refresh_token|cookie|authorization)=)[^&\s]+/gi,`$1[REDACTED]`).replace(/((?:authorization|cookie):\s*)[^\n]+/gi,`$1[REDACTED]`)}var d=class{#e;#t;#n;#r;#i=`idle`;#a=[];#o;#s=!1;#c=!1;#l=0;#u=[];#d=[];#f=[];#p=[];#m;#h=0;#g=[];#_=!1;#v;constructor(e={},t,n){this.#e={startMode:`immediate`,readyTimeoutMs:12e4,backoffBaseMs:1e3,backoffMaxMs:5e3,maxRestartAttempts:3,ringBufferLines:1e3,...e},this.#t=t??f(e),this.#n=n??l,this.#r=e.log??(()=>{})}phase(){return this.#i}onPhaseChange(e){return this.#a.push(e),()=>{this.#a=this.#a.filter(t=>t!==e)}}onStderrLine(e){return this.#p.push(e),()=>{this.#p=this.#p.filter(t=>t!==e)}}async start(){if(this.#i!==`idle`)throw Error(`Supervisor.start 仅 idle 态可调(当前 ${this.#i})`);this.#T(),await this.#S()}async connect(){this.#b(),this.#s=!0,this.#i===`standby`&&!this.#c&&this.#e.startMode===`on-first-connection`&&this.#O(),this.#i!==`ready`&&await this.#S();let e=this.#m;if(e!==void 0&&e.generation===this.#h)return e;let t=this.#y;if(t===void 0)throw Error(`ready 凭据缺席(状态机时序违例)`);let n=await this.#n(t.port,t.token),r={baseUrl:`http://127.0.0.1:${t.port}`,cookie:n,port:t.port,generation:this.#h};return this.#m=r,this.#r(`凭据交换完成(generation ${this.#h},port ${t.port})`),r}async reconnect(){this.#b(),this.#i!==`ready`&&await this.#S();let e=this.#y;if(e===void 0)throw Error(`ready 凭据缺席(状态机时序违例)`);let t=await this.#n(e.port,e.token),n={baseUrl:`http://127.0.0.1:${e.port}`,cookie:t,port:e.port,generation:this.#h};return this.#m=n,this.#r(`凭据再交换完成(generation ${this.#h},port ${e.port})`),n}crashReport(){return[`phase=${this.#i} restarts=${this.#l} generation=${this.#h}`,`exitCodes=${JSON.stringify(this.#u)}`,`phaseHistory=${this.#d.join(` → `)}`,`stderr(环形缓冲尾部 ${this.#f.length} 行):`,...this.#f.map(e=>` ${e}`)].join(`
2
2
  `)}async dispose(){if(this.#_)return;this.#_=!0,this.#v=void 0;let e=this.#o;e!==void 0&&(e.kill(`SIGTERM`),await new Promise(t=>{let n=setTimeout(()=>{e.kill(`SIGKILL`),t()},3e3);e.onExit(()=>{clearTimeout(n),t()})})),this.#w(Error(`Supervisor 已停机`)),this.#x(`disposed`)}#y;#b(){if(this.#_)throw Error(`Supervisor 已停机`);if(this.#i===`idle`)throw Error(`先调用 start()(connect 挂靠 spawn 生命周期)`);if(this.#v!==void 0)throw this.#v.error;if(this.#i===`failed-permanent`||this.#i===`failed-protocol`)throw new s(this.#i===`failed-permanent`?`permanent`:`protocol`,`Supervisor 终态 ${this.#i}`,this.crashReport())}#x(e){this.#i=e,this.#d.push(`${e}@${Date.now()}`),this.#a.forEach(t=>t(e))}#S(){return this.#i===`ready`?Promise.resolve():new Promise((e,t)=>{this.#g.push({generation:this.#h,resolve:e,reject:t})})}#C(){this.#g.splice(0).forEach(e=>e.resolve())}#w(e){this.#g.splice(0).forEach(t=>t.reject(e))}async#T(){for(;;){if(this.#_)return;if(await this.#D(),this.#v!==void 0){this.#x(this.#v.kind===`protocol`?`failed-protocol`:`failed-permanent`);return}return}}#E(e){if(this.#_)return;if(this.#l+=1,this.#l>this.#e.maxRestartAttempts){this.#v={kind:`permanent`,error:new s(`exhausted`,`崩溃退避序列耗尽(${this.#e.maxRestartAttempts} 次)——最后原因:${e}`,this.crashReport())},this.#x(`failed-permanent`),this.#w(this.#v.error);return}let t=Math.min(this.#e.backoffBaseMs*2**(this.#l-1),this.#e.backoffMaxMs);this.#x(`restarting`),this.#r(`瞬时失败(${e})——${t}ms 后第 ${this.#l} 次重启`),setTimeout(()=>{this.#_||this.#D().then(()=>{this.#v!==void 0&&(this.#x(this.#v.kind===`protocol`?`failed-protocol`:`failed-permanent`),this.#w(this.#v.error))})},t)}async#D(){this.#h+=1,this.#m=void 0,this.#c=!1,this.#y=void 0;let e=this.#h;this.#x(`booting`);let t=this.#e.command??[`tboxcode-dsh`],n=this.#t([...t,`server`]);this.#o=n;let r=!1,i;clearTimeout(i),i=setTimeout(()=>{this.#h!==e||this.#i===`ready`||(this.#r(`就绪确认超时——kill 子进程(瞬时面)`),n.kill(`SIGKILL`))},this.#e.readyTimeoutMs),n.onStdoutLine(t=>{if(this.#h!==e)return;let a=c(t);if(!r){if(a===null||a.event!==`standby`){this.#v={kind:`protocol`,error:new s(`protocol`,`首行校验失败(期待 standby 信封,实得 "${t.slice(0,80)}")`,this.crashReport())},this.#x(`failed-protocol`),this.#w(this.#v.error),n.kill(`SIGKILL`);return}r=!0,this.#x(`standby`),(this.#e.startMode===`immediate`||this.#s)&&this.#O();return}if(a===null){this.#i===`ready`&&this.#r(`stdout 违约(ready 后非静默): ${t.slice(0,120)}`),this.#k(`[stdout] ${t}`);return}a.event!==`standby`&&this.#i!==`ready`&&(this.#y=a,this.#x(`ready`),clearTimeout(i),this.#C())}),n.onStderrLine(t=>{if(this.#h!==e)return;let n=u(t);this.#k(n);for(let e of this.#p)try{e(n)}catch{}}),n.onExit(t=>{if(this.#h===e&&(clearTimeout(i),this.#u.push(t),!this.#_&&this.#v===void 0)){if(t===2){this.#v={kind:`permanent`,error:new s(`permanent`,`server 永久失败(退出码 2——config 非法 / 未设 model / profile 损坏)`,this.crashReport())},this.#x(`failed-permanent`),this.#w(this.#v.error);return}if(t===3){this.#v={kind:`protocol`,error:new s(`protocol`,`server 协议版本偏斜(退出码 3——部署错误最显眼信号)`,this.crashReport())},this.#x(`failed-protocol`),this.#w(this.#v.error);return}this.#E(`退出码 ${t??`signal`}`)}})}#O(){this.#c||this.#o===void 0||(this.#c=!0,this.#x(`starting`),this.#o.writeStdin(`${JSON.stringify({v:1,cmd:`start`})}\n`),this.#r(`start 已释放`))}#k(e){this.#f.push(`${Date.now()} ${e}`),this.#f.length>this.#e.ringBufferLines&&this.#f.splice(0,this.#f.length-this.#e.ringBufferLines)}};function f(e){return t=>{let n=r(t[0],t.slice(1),{stdio:[`pipe`,`pipe`,`pipe`],env:{...process.env,...e.env??{}}}),i=``,a=``,o=[],s=[],c=[],l=(e,t,n)=>{for(e+=t.toString();;){let t=e.indexOf(`
3
- `);if(t<0)break;let r=e.slice(0,t);e=e.slice(t+1),n.forEach(e=>e(r))}return e};return n.stdout?.on(`data`,e=>{i=l(i,e.toString(),o)}),n.stderr?.on(`data`,e=>{a=l(a,e.toString(),s)}),n.on(`close`,e=>{i!==``&&o.forEach(e=>e(i)),a!==``&&s.forEach(e=>e(a)),c.forEach(t=>t(e))}),{writeStdin:e=>n.stdin?.write(e),onStdoutLine:e=>o.push(e),onStderrLine:e=>s.push(e),onExit:e=>c.push(e),kill:e=>n.kill(e??`SIGTERM`)}}}function p(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function m(e){return!p(e)||typeof e.type!=`string`||e.type===``||typeof e.seq!=`number`||!Number.isInteger(e.seq)||typeof e.time!=`number`||!(`data`in e)?null:e}function h(e){if(!p(e)||e.type!==`event`)return null;let t=m(e.event);return t===null?null:{type:`event`,event:t}}function g(e){if(!p(e))return null;if(e.type===`snapshot`){let t=p(e.header)?e.header:null;if(t===null||typeof e.cursor!=`number`||typeof e.hasMore!=`boolean`)return null;let n=[];if(Array.isArray(e.records))for(let t of e.records){let e=h(t);if(e===null)return null;n.push(e)}else return null;return{type:`snapshot`,header:t,cursor:e.cursor,records:n,hasMore:e.hasMore,projections:e.projections,...p(e.assistantStream)?{assistantStream:e.assistantStream}:{}}}return e.type===`assistant-stream`?p(e.frame)?{type:`assistant-stream`,frame:e.frame}:null:h(e)}function _(e){if(!p(e)||typeof e.hasMore!=`boolean`||!Array.isArray(e.records))return null;let t=[];for(let n of e.records){let e=h(n);if(e===null)return null;t.push(e)}return{records:t,hasMore:e.hasMore}}function v(e){return p(e)&&(e.type===`ready`&&typeof e.clientId==`string`&&p(e.host)||e.type===`emit`&&typeof e.event==`string`&&Array.isArray(e.args)||e.type===`waterfall`&&typeof e.eventId==`string`&&typeof e.agentId==`string`&&typeof e.event==`string`&&p(e.request)||e.type===`cancel`&&typeof e.eventId==`string`)?e:null}var y=class extends Error{code;details;constructor(e){super(e.message),this.name=`CarrierRpcError`,this.code=e.code,this.details=e.details}},b=class extends Error{constructor(e){super(e),this.name=`CarrierTransportError`}},x=class{#e;#t;#n;#r;#i;#a;#o=!1;#s=!1;constructor(e){this.#e=e.baseUrl.replace(/\/$/,``),this.#t=e.cookie,this.#n=e.refreshCredentials}async unary(e,t){this.#p();try{return await this.#c(e,t)}catch(n){if(!(n instanceof b&&n.message.endsWith(`HTTP 401`))||this.#n===void 0)throw n;return await this.#f(),this.#c(e,t)}}async#c(e,t){let n=i(),r;try{r=await fetch(`${this.#e}/api/${e}`,{method:`POST`,headers:{"content-type":`application/json`,cookie:this.#t},body:JSON.stringify({type:`client-request`,rpcId:n,method:e,payload:{args:t}}),signal:AbortSignal.timeout(6e4)})}catch(t){throw t instanceof Error&&t.name===`TimeoutError`?new b(`unary ${e} 超时(上游 60s 无响应)`):t}if(r.status!==200)throw new b(`unary ${e} HTTP ${r.status}`);let a=await r.json();if(a.type!==`server-response`||a.rpcId!==n)throw new b(`unary ${e} envelope 畸形(type/rpcId 失配)`);if(a.result.ok)return a.result.value;throw new y(a.result.error)}openStream(e,t,n){this.#p();let r=i(),o=!1,s={canceled:!1,onFrame:e=>{e.type===`item`?n.onItem(e.value):e.type===`end`?(s.canceled=!0,n.onEnd?.()):e.type===`error`&&(s.canceled=!0,n.onError?.(e.error??{code:`gateway/internal`,message:`stream error 帧缺 error 字段`}))},onDown:e=>{s.canceled||(s.canceled=!0,e&&n.onError?.({code:`carrier/mux-closed`,message:`mux 插座意外关闭`}),n.onClose?.(e))}};return this.#l().then(n=>{if(o||s.canceled){process.stderr.write(`[carrier] open ABORTED (canceled=${String(o)} entry=${String(s.canceled)}) ${e}\n`);return}n.streams.set(r,s),n.socket.readyState===a.OPEN?(n.socket.send(JSON.stringify({type:`open`,streamId:r,endpoint:e,payload:{args:t}})),process.env.CARRIER_WIRE_DEBUG===`1`&&process.stderr.write(`[carrier] open sent ${e} streamId=${r.slice(0,8)}\n`)):process.stderr.write(`[carrier] open SKIPPED (readyState ${n.socket.readyState}) ${e}\n`)}).catch(e=>{o||s.canceled||(s.canceled=!0,n.onError?.({code:`carrier/mux-open`,message:e.message}),n.onClose?.(!0))}),{cancel:()=>{o=!0,s.canceled=!0;let e=this.#r;e!==void 0&&e.streams.delete(r)&&e.socket.readyState===a.OPEN&&e.socket.send(JSON.stringify({type:`cancel`,streamId:r}))}}}async#l(){let e=this.#r;if(e!==void 0&&e.socket.readyState===a.OPEN)return e;if(this.#i!==void 0)return this.#i;let t=this.#u().finally(()=>{this.#i===t&&(this.#i=void 0)});return this.#i=t,t}async#u(){this.#p(),this.#o&&this.#n!==void 0&&await this.#f();let e=new a(`${S(this.#e)}/api/remote.mux`,{headers:{cookie:this.#t}});await new Promise((t,n)=>{let r=setTimeout(()=>n(new b(`mux 插座打开超时`)),15e3);r.unref(),e.once(`open`,()=>{clearTimeout(r),t()}),e.once(`error`,e=>{clearTimeout(r),n(new b(`mux 插座打开失败: ${e.message}`))}),e.once(`unexpected-response`,(e,t)=>{clearTimeout(r),n(new b(`mux 升级被拒 HTTP ${t.statusCode??`?`}`))})});let t={socket:e,streams:new Map};e.on(`message`,e=>{let n;try{n=JSON.parse(String(e))}catch{return}typeof n.streamId==`string`&&t.streams.get(n.streamId)?.onFrame(n)});let n=!1;return e.on(`close`,()=>this.#d(t,n)),e.on(`error`,()=>{n=!0}),this.#r=t,this.#o=!0,t}#d(e,t){if(this.#r===e){this.#r=void 0;for(let n of e.streams.values())n.onDown(t);e.streams.clear()}}#f(){if(this.#a!==void 0)return this.#a;let e=(async()=>{let e=this.#n;if(e===void 0)return;let t=await e();this.#t=t.cookie,this.#e=t.baseUrl.replace(/\/$/,``)})().finally(()=>{this.#a===e&&(this.#a=void 0)});return this.#a=e,e}openEvents(e){let t,n=n=>{switch(n.type){case`ready`:t=n.clientId,e.onReady(n);return;case`emit`:e.onEmit(n);return;case`waterfall`:e.onWaterfall(n);return;case`cancel`:e.onCancel(n);return}},r=this.openStream(`$events`,{},{onItem:t=>{let i=v(t);if(i===null){e.onClose?.(!1),r.cancel();return}n(i)},onClose:t=>e.onClose?.(t)});return{respond:async(e,n)=>{if(t===void 0)throw new b(`waterfall 应答先于 ready 帧(clientId 缺席)`);await this.unary(`$events/result`,{clientId:t,eventId:e,outcome:n})},close:()=>r.cancel()}}dispose(){this.#s=!0,this.#r?.socket.close(),this.#r=void 0}#p(){if(this.#s)throw new b(`carrier 已释放`)}};function S(e){if(e.startsWith(`https://`))return`wss://${e.slice(8)}`;if(e.startsWith(`http://`))return`ws://${e.slice(7)}`;throw new b(`baseUrl 非法(期待 http(s)://,实得 ${e})`)}function C(e,r){let i=m(e);if(i===null)return r!==void 0&&(r.invalid+=1),null;if(t(i.type)===`project`)return typeof i.data!=`object`||i.data===null||Array.isArray(i.data)?(r!==void 0&&(r.opaqueWarn+=1),{kind:`opaque`,event:i,level:`warn`}):(r!==void 0&&(r.consumable+=1),{kind:`consumable`,event:i});let a=n(i)?`debug`:`warn`;return a===`debug`?r!==void 0&&(r.opaqueDebug+=1):r!==void 0&&(r.opaqueWarn+=1),{kind:`opaque`,event:i,level:a}}var w=class{#e;#t;#n={invalid:0,opaqueDebug:0,opaqueWarn:0,consumable:0};#r=new Map;#i=0;constructor(e,t){this.#e=e,this.#t=t?.reopenBaseMs??1e3}open(e,t){let n=this.#r.get(e);if(n!==void 0&&!n.closed)return n.handlers=t,{close:()=>this.close(e)};let r={sessionId:e,handlers:t,handle:void 0,cursor:void 0,baseline:void 0,streamSeenRevision:!1,reopening:!1,backoffMs:this.#t,closed:!1,pending:[]};return this.#r.set(e,r),this.#a(r),{close:()=>this.close(e)}}close(e){let t=this.#r.get(e);t!==void 0&&(t.closed=!0,t.handle?.cancel(),this.#r.delete(e))}stats(){let e=0,t=0;for(let n of this.#r.values())n.closed||(n.reopening||n.cursor===void 0?t++:e++);return{live:e,reopening:t,decoded:{...this.#n},gapsRepaired:this.#i}}#a(e){e.closed||(e.reopening=e.cursor!==void 0,e.handle=this.#e.openStream(`session/follow`,{request:{address:{kind:`session`,sessionId:e.sessionId},assistantStream:!0}},{onItem:t=>this.#s(e,t),onEnd:()=>this.#o(e,`stream end`),onError:t=>this.#o(e,`${t.code}: ${t.message}`),onClose:t=>{t&&this.#o(e,`socket error`)}}))}#o(e,t){if(e.closed)return;e.handle=void 0,e.handlers.onDown(t);let n=e.backoffMs;e.backoffMs=Math.min(e.backoffMs*2,3e4),e.baseline=void 0,e.streamSeenRevision=!1,e.reopening=!0,setTimeout(()=>{e.closed||this.#a(e)},n)}#s(e,t){if(t.type===`snapshot`){this.#c(e,t);return}if(e.cursor!==void 0){if(e.reopening){e.pending.push(t);return}this.#d(e,t)}}#c(e,t){let n=e.cursor;if(n!==void 0&&t.cursor<n){e.handlers.onDown(`cursor 回退:${String(t.cursor)} < ${String(n)}`);return}e.baseline=t.assistantStream,e.streamSeenRevision=!1,e.reopening=!0,e.handlers.onSnapshot(t.header,t.cursor,t.projections,t.assistantStream),this.#l(e,t,n)}async#l(e,t,n){try{let r=[];if(n!==void 0&&t.records.length>0){let i=t.records[0]?.event.seq??t.cursor;i>n+1&&(r.push(...await this.#u(e,n+1,i)),this.#i+=r.length)}for(let e of t.records)r.push(e.event);for(let t of r)this.#f(e,t);let i=e.pending;e.pending=[],e.reopening=!1,e.cursor=t.cursor,e.backoffMs=this.#t;for(let t of i)this.#d(e,t)}catch(t){e.handlers.onDown(`backlog 补齐失败:${String(t)}`)}}async#u(e,t,n){let r=[],i=n;for(;i>t;){let n=_(await this.#e.unary(`session/page`,{request:{address:{kind:`session`,sessionId:e.sessionId},throughSeq:i-1,beforeSeq:t,maxMessages:200}}));if(n===null)throw new b(`session/page 帧畸形(records/hasMore 缺失)`);for(let e of n.records)r.push(e.event);let a=n.records[0]?.event.seq;if(a===void 0||a<=t||(i=a,!n.hasMore))break}return r.sort((e,t)=>e.seq-t.seq),r}#d(e,t){if(t.type===`assistant-stream`){if(!e.streamSeenRevision){e.streamSeenRevision=!0;let n=(e.baseline?.revision??-1)+1;if(e.baseline!==void 0&&t.frame.revision!==n){e.handlers.onDown(`assistant-stream 衔接断裂:revision ${String(t.frame.revision)} ≠ ${String(n)}`);return}}e.handlers.onAssistantStream(t.frame);return}t.type===`event`&&this.#f(e,t.event)}#f(e,t){let n=C(t,this.#n);n!==null&&n.kind===`consumable`&&e.handlers.onEvent(n.event)}async readAll(e){return await new Promise((t,n)=>{let r=new Map,i,a,o=!1,s=this.#e.openStream(`session/follow`,{request:{address:{kind:`session`,sessionId:e}}},{onItem:c=>{o||c.type===`snapshot`&&(o=!0,i=c.header,a=c.projections,s.cancel(),this.#p(e,c,r).then(e=>t({header:i,events:e,projections:a}),e=>n(e)))},onEnd:()=>{o||(o=!0,n(new b(`session/follow 在 snapshot 前关闭`)))},onError:e=>{o||(o=!0,n(new y(e)))}})})}async#p(e,t,n){let r=[];for(let e of t.records)r.push(e.event);let i=t.records.length>0?t.records[0]?.event.seq:void 0;for(;t.hasMore===!0;){let n=_(await this.#e.unary(`session/page`,{request:{address:{kind:`session`,sessionId:e},throughSeq:t.cursor,...i===void 0?{}:{beforeSeq:i},maxMessages:200}}));if(n===null)throw new b(`session/page 帧畸形(records/hasMore 缺失)`);for(let e of n.records)r.push(e.event);if(!n.hasMore||n.records.length===0)break;let a=n.records[0]?.event.seq;if(a===void 0||a===0)break;i=a}let a=new Map;for(let e of r)a.set(e.seq,e);let o=[...a.values()].sort((e,t)=>e.seq-t.seq);for(let e=0;e<o.length;e++){let t=o[e];if(t?.seq!==e)throw new y({code:`gateway/internal`,message:`journal 连续性断裂:位置 ${String(e)} 实得 seq ${String(t?.seq)}`});let r=C(t,this.#n);r?.kind===`consumable`&&n.set(t.seq,r.event)}return[...n.values()]}},T=class{#e=new Map;#t=new Map;#n=new Map;#r=new Map;#i;#a;#o=0;#s=0;constructor(e,t){this.#i=e,this.#a=t}attachConnection(e,t,n){this.#e.set(e,{connId:e,send:t,ctx:n})}detachConnection(e){this.#e.delete(e);for(let[t,n]of this.#t)n.delete(e),n.size===0&&this.#t.delete(t),this.#a?.onSessionUnbind?.(e,t);for(let[t,n]of this.#n)n===e&&this.#n.delete(t)}bind(e,t){let n=this.#t.get(t);n===void 0&&(n=new Set,this.#t.set(t,n));let r=n.has(e);n.add(e),r||this.#a?.onSessionBind?.(e,t)}unbind(e,t){this.#t.get(t)?.delete(e),this.#a?.onSessionUnbind?.(e,t)}boundConnIds(e){return[...this.#t.get(e)??[]]}ctxOf(e){return this.#e.get(e)?.ctx}routeResolved(e,t,n){this.routeExt({method:e,payload:t},n===void 0?void 0:{excludeConnId:n})}beginReplayCount(e){this.#r.set(e,0)}takeReplayCount(e){let t=this.#r.get(e);return this.#r.delete(e),t}setTurnOrigin(e,t){this.#n.set(e,t)}clearTurnOrigin(e){this.#n.delete(e)}route(e){let t=e.sessionId;if(t!==void 0&&this.#a?.onSessionUpdate!==void 0){let n=e.update??e;E(this.#i,`onSessionUpdate`,()=>this.#a?.onSessionUpdate?.(t,n))}let n;if(t===void 0)n=[...this.#e.keys()];else{let r=this.#n.get(t),i=e.update?.sessionUpdate===`user_message_chunk`;n=[...this.#t.get(t)??[]],r!==void 0&&i&&(n=n.filter(e=>e!==r),this.#s++);let a=this.#a?.fanout===void 0?void 0:E(this.#i,`fanout`,()=>this.#a?.fanout?.(e,n));if(a!==void 0&&(n=[...a]),n.length===0){this.#o++;return}}for(let t of n){let n=this.#e.get(t);if(n===void 0)continue;let r=e;if(this.#a?.transformOutbound!==void 0){let t=E(this.#i,`transformOutbound`,()=>this.#a?.transformOutbound?.(e,n.ctx));if(t===null)continue;t!==void 0&&(r=t)}n.send({jsonrpc:`2.0`,method:`session/update`,params:r});let i=this.#r.get(t);i!==void 0&&this.#r.set(t,i+1)}}routeExt(e,t){let n=typeof e.payload.sessionId==`string`?e.payload.sessionId:void 0,r=n===void 0?[...this.#e.keys()]:[...this.#t.get(n)??[]];t?.excludeConnId!==void 0&&(r=r.filter(e=>e!==t.excludeConnId));for(let t of r)this.#e.get(t)?.send({jsonrpc:`2.0`,method:e.method,params:e.payload})}stats(){return{connections:this.#e.size,dropped:this.#o,suppressed:this.#s}}};function E(e,t,n){try{return n()}catch(n){e.warn(`gateway hook ${t} failed (fail-open): ${String(n)}`);return}}function D(e){if(typeof e!=`object`||!e)return e;let t=e;if(t.type!==`image`)return e;let n=t.attachment;if(typeof n!=`object`||!n)return e;let r=n;return typeof r.mediaType!=`string`||typeof r.data!=`string`?e:{type:`image`,mediaType:r.mediaType,data:r.data,...typeof r.name==`string`?{name:r.name}:{}}}const O=1e3,k=3e4;function A(e){let t=e,n=t?.values?.modelSelection?.next??t?.values?.modelSelection?.lastUsed;if(!(n==null||typeof n.provider!=`string`||typeof n.model!=`string`))return{provider:n.provider,model:n.model,...typeof n.reasoningEffort==`string`?{reasoningEffort:n.reasoningEffort}:{}}}function j(e){return{id:e.id,get header(){let t=e.header;return{...t?.cwd===void 0?{}:{cwd:t.cwd},...t?.origin===void 0?{}:{origin:t.origin},...t?.parentSession===void 0?{}:{parentSession:t.parentSession},...t?.createdAt===void 0?{}:{createdAt:t.createdAt}}},requestHeader(){let t=A(e.projections);if(t!==void 0)return{config:{provider:t.provider,model:t.model,...t.reasoningEffort===void 0?{}:{reasoningEffort:t.reasoningEffort}}}}}}function M(e,t,n){return{id:e.id,session:e.session,cancel:async n=>{await t.unary(`session/cancel`,{request:{sessionId:e.id}})},followup:t=>{n(e,t)}}}var N=class{logger;#e;#t;#n;#r=new Map;#i=new Map;#a=new Set;#o=new Set;#s=new Set;#c=new Set;#l=new Set;#u;#d=O;#f;#p=O;#m;#h={groups:[],failures:[]};#g=0;#_;#v=!1;#y;constructor(e){this.#e=e.carrier,this.#t=e.pool,this.#n=e.logger,this.logger=e.logger,this.#y=this.#E(),this.#O(),this.#A()}async createAgent(e){return await this.#b(e,{primeProjections:!1})}async resumeAgent(e){return await this.#b(e,{primeProjections:!0})}async#b(e,t){let n=(await this.#e.unary(`session/create`,{request:{sessionId:e.sessionId,cwd:e.cwd}})).sessionId,r=this.#r.get(n);if(r!==void 0)return await this.#i.get(n)?.catch(()=>{}),await e.configureScope?.(this.#C(r),r.agent),{agent:r.agent,dispose:async()=>this.#S(n)};let i={id:n,header:{version:1,id:n,createdAt:0,cwd:e.cwd,isSeeded:!1},projections:{},selectionProbe:void 0,lastPushedSelection:void 0,promptInflight:!1};i.session=j(i),i.agent=M(i,this.#e,(e,t)=>this.#w(e,t));let a=i;this.#r.set(n,a);try{if(t.primeProjections){let e=this.#x(n).then(e=>{e!==void 0&&(a.projections=e)});this.#i.set(n,e),await e,this.#i.delete(n)}this.#_===void 0&&(await this.#y,this.#_===void 0&&!this.#v&&(this.#v=!0,await this.#E())),await e.configureScope?.(this.#C(a),a.agent),this.#t.open(n,this.#D(a))}catch(e){throw await this.#S(n).catch(()=>{}),e}return{agent:a.agent,dispose:async()=>this.#S(n)}}async#x(e){try{let t=await this.#e.unary(`session/projections`,{request:{sessionId:e}});if(typeof t!=`object`||!t||Array.isArray(t))return;let n=t,r=n.values;return typeof r!=`object`||!r||Array.isArray(r)?void 0:{asOfSeq:typeof n.asOfSeq==`number`?n.asOfSeq:-1,values:r}}catch(t){this.#n.debug(`projection prime failed (${e}): ${String(t)}`);return}}#S(e){return this.#t.close(e),this.#r.delete(e),this.#i.delete(e),Promise.resolve()}#C(e){return{on:(t,n)=>(t===`agent/request`&&(e.selectionProbe=n),()=>{}),tools:{restrict:e=>void 0}}}async#w(e,t){if(e.promptInflight=!0,e.selectionProbe!==void 0)try{let t=await e.selectionProbe(void 0,async()=>({}));if(typeof t.provider==`string`&&typeof t.model==`string`){let n=JSON.stringify([t.provider,t.model,t.reasoningEffort]);n!==e.lastPushedSelection&&(await this.#e.unary(`session/selectModel`,{request:{sessionId:e.id,provider:t.provider,model:t.model,...typeof t.reasoningEffort==`string`?{reasoningEffort:t.reasoningEffort}:{}}}),e.lastPushedSelection=n)}}catch(e){this.#n.debug(`selection probe failed: ${String(e)}`)}let n=t.content.map(D);await this.#e.unary(`session/prompt`,{request:{requestId:t.id,sessionId:e.id,mode:`steer`,content:n}})}persistence={stat:async(e,t)=>{let n=await this.#e.unary(`tboxcode/sessions.stat`,{request:{sessionId:e}});if(n!==null)return{header:n.header}},list:async e=>(await this.#e.unary(`session/list`,{_request:{}})).items.map(e=>({header:{id:e.sessionId,...e.cwd===void 0?{}:{cwd:e.cwd},...e.origin===void 0?{}:{origin:e.origin},...e.parentSessionId===void 0?{}:{parentSession:e.parentSessionId},createdAt:e.updatedAt}})),openHistoryRead:async(e,t)=>{let n=await this.#t.readAll(e);return{read:async()=>({events:n.events}),close:async()=>{}}}};sessionStore={get:e=>this.#r.get(e)?.session,flush:async e=>{await this.#e.unary(`tboxcode/sessions.flush`,{request:{sessionId:e.id}})}};llm={listProviders:()=>this.#T().groups.map(e=>({id:e.id,name:e.name})),listModels:async e=>(this.#T().groups.find(t=>t.id===e)?.models??[]).map(e=>({id:String(e.id),name:String(e.name),...e.description===void 0?{}:{description:String(e.description)}})),resolveModelInfo:async(e,t)=>{let n=this.#T().groups.find(t=>t.id===e)?.models.find(e=>e.id===t)?.reasoning;return{reasoning:{defaultEffort:n?.defaultEffort,efforts:n?.efforts??[]}}},resolveCallConfig:async e=>e};#T(){return Date.now()-this.#g>3e4&&this.#E(),this.#h}async#E(){try{this.#h=await this.#e.unary(`session/modelCatalog`,{}),this.#g=Date.now();let e=await this.#e.unary(`tboxcode/selection.default`,{});this.#_=typeof e.provider==`string`&&typeof e.model==`string`?{provider:e.provider,model:e.model,...e.reasoningEffort===void 0?{}:{reasoningEffort:e.reasoningEffort}}:void 0}catch(e){this.#n.warn(`catalog warm failed: ${String(e)}`)}}async admitPromptContent(e){return e.map(e=>e.type===`text`?e:{type:`image`,attachment:{mediaType:e.mediaType,data:e.data,...e.name===void 0?{}:{name:e.name}}})}applyModeKnobs(t,n,r){let i=Object.entries(e).find(([,e])=>e.sandbox===r.sandbox&&e.approval===r.approval)?.[0];if(i===void 0)throw Error(`tboxcode: mode knobs 无对位(${String(r.sandbox)}/${String(r.approval)}——MODE_TABLE 外组合)`);return this.#e.unary(`tboxcode/mode`,{request:{agentId:t.id,mode:i}}).then(()=>void 0)}defaultSelection(){return this.#_}async executeCommand(e,t,n){let r=await this.#e.unary(`tboxcode/commands.execute`,{request:{agentId:e.id,line:t}});return r?.commandId===void 0?void 0:{commandId:r.commandId}}async listCommands(e){return await this.#e.unary(`tboxcode/commands.list`,{request:{agentId:e.id}})}async listUserInvocableSkills(e){return await this.#e.unary(`tboxcode/skills.userInvocable`,{request:{agentId:e.id}})}onSessionEvent(e){let t=e;return this.#a.add(t),()=>this.#a.delete(t)}onAssistantStream(e){return this.#o.add(e),()=>this.#o.delete(e)}onInboxClaimed(e){return this.#s.add(e),()=>this.#s.delete(e)}onAgentError(e){return this.#c.add(e),()=>this.#c.delete(e)}sessionToolDenies(){return[]}onCommandSurfaceChanged(e){return this.#l.add(e),()=>this.#l.delete(e)}sessionIds(){return[...this.#r.keys()]}hasInflightPrompt(e){return this.#r.get(e)?.promptInflight===!0}poolLive(){return this.#t.stats().live>0}poolEntries(){return this.#r.size}settlePrompt(e,t){let n=this.#r.get(e);if(!(n===void 0||!n.promptInflight)){n.promptInflight=!1;for(let e of this.#c)e({agent:n.agent,turn:0,error:t})}}#D(e){return{onSnapshot:(t,n,r)=>{e.header=t,e.projections=r},onEvent:t=>{t.type===`turn/end`&&(e.promptInflight=!1);for(let n of this.#a)n(e.session,t)},onAssistantStream:t=>{let n={type:t.type??(t.finish===void 0?t.index===0?`start`:`chunk`:`end`),attemptId:t.attemptId,...t.turn===void 0?{}:{turn:t.turn},...t.step===void 0?{}:{step:t.step},...t.chunk===void 0?{}:{chunk:t.chunk}};for(let t of this.#o)t({agent:e.agent,frame:n})},onDown:t=>this.#n.warn(`follow pool down (${e.id}): ${t}`)}}#O(){this.#u=this.#e.openStream(`tboxcode/agent.watch`,{},{onItem:e=>{this.#n.debug(`agent.watch item: ${JSON.stringify(e).slice(0,160)} states=${[...this.#r.keys()].join(`,`)}`);let t=this.#r.get(e.sessionId);if(t!==void 0){if(e.kind===`inboxClaimed`)for(let n of this.#s)n({agent:t.agent,message:{...e.messageId===void 0?{}:{id:e.messageId}},turn:e.turn??0});else if(e.kind===`error`)for(let n of this.#c)n({agent:t.agent,turn:0,error:e.error??`unknown agent error`})}},onError:e=>{this.#n.warn(`agent.watch stream error: ${JSON.stringify(e).slice(0,200)}`),this.#k()},onClose:e=>{e&&(this.#n.warn(`agent.watch stream closed`),this.#k())}})}#k(){let e=this.#d;this.#d=Math.min(this.#d*2,k),setTimeout(()=>{this.#O(),this.#d=O},e)}#A(){let e=()=>{for(let e of this.#l)e()};this.#f=P(this.#e,e,this.#n),this.#m=this.#e.openStream(`tboxcode/skills.watch`,{},{onItem:()=>e(),onError:()=>this.#j(),onClose:e=>{e&&this.#j()}})}#j(){let e=this.#p;this.#p=Math.min(this.#p*2,k),setTimeout(()=>{this.#A(),this.#p=O},e)}dispose(){this.#u?.cancel(),this.#f?.close(),this.#m?.cancel()}};function P(e,t,n){return e.openEvents({onReady:()=>n.debug(`commands/change $events ready`),onEmit:e=>{e.event===`commands/change`&&t()},onWaterfall:()=>{},onCancel:()=>{},onClose:e=>{e&&n.warn(`commands/change $events closed with error`)}})}var F=class{#e;#t;#n;#r;#i;#a;#o=!1;#s;constructor(e,t){this.#e=e,this.#t=t?.timeoutMs??12e4,this.#n=t?.tickMs??1e3,this.#r=t?.now??Date.now,this.#i=t?.logger??{warn:()=>{},info:()=>{},debug:()=>{}}}start(){this.#s=setInterval(()=>this.tick(),this.#n)}stop(){this.#s!==void 0&&clearInterval(this.#s),this.#s=void 0}tick(){if(!(this.#e.poolEntries()>0&&!this.#e.poolLive())){this.#a=void 0,this.#o=!1;return}if(this.#a===void 0){this.#a=this.#r();return}if(this.#o||this.#r()-this.#a<this.#t)return;this.#o=!0;let e=this.#r()-this.#a;for(let t of this.#e.sessionIds())this.#e.hasInflightPrompt(t)&&(this.#i.warn(`carrier timeout settle (${t}): downMs=${String(e)}`),this.#e.settle(t,Error(`tboxcode carrier-timeout (downMs=${String(e)})`)))}snapshot(){return{down:this.#a!==void 0,downSince:this.#a,settled:this.#o}}};const I=6e4,L=36e5,R=3e4,z=-32001;var B=class{#e=new Map;#t;constructor(e){this.#t=e}get size(){return this.#e.size}pendingOf(e){for(let t of this.#e.values())if(t.sessionId===e)return t}begin(e){let t=e.event===`user-questions/request`?`elicitation`:e.event===`approval/request`?`permission`:void 0;if(t===void 0||this.#e.has(e.eventId))return;let n=this.#t.sessionIdOfAgent(e.agentId);if(n===void 0){this.#l(e.eventId,{kind:`rejected`,error:{code:`gateway/agent-not-found`,message:`agent ${e.agentId} not managed by gateway`}});return}let r={kind:t,sessionId:n,eventId:e.eventId,frame:e,asked:new Map,state:`active`,budgetStartedAt:this.#t.now()};this.#e.set(e.eventId,r),this.#n(r),this.#s(r)}cancel(e){let t=this.#e.get(e.eventId);t===void 0||t.state===`settled`||this.#c(t,{kind:`result`,value:`cancelled`},`turn`)}attach(e,t){for(let n of this.#e.values()){if(n.sessionId!==t||n.state!==`grace`)continue;let r=n.graceTimer;n.graceTimer=void 0,r!==void 0&&this.#t.timers.clear(r),n.state=`active`,this.#t.logger.info(`answerer: grace broken by attach (${n.eventId} conn=${e})`),this.#n(n)}}switchBack(e,t){this.attach(e,t)}disconnect(e){for(let t of this.#e.values())t.asked.delete(e)&&t.state===`active`&&t.asked.size===0&&this.#o(t,`member disconnected`)}dispose(){for(let e of[...this.#e.values()])e.state!==`settled`&&this.#c(e,{kind:`result`,value:`cancelled`},`dispose`);this.#e.clear()}#n(e){let t=this.#t.eligibleConns(e.sessionId).filter(t=>!e.asked.has(t)).filter(t=>e.kind===`permission`||this.#t.capsOf(t));if(t.length===0){this.#o(e,`no eligible connections`);return}for(let n of t){let t={connId:n};e.kind===`permission`&&(t.timer=this.#t.timers.set(()=>{e.state!==`active`||!e.asked.has(n)||(e.asked.delete(n),this.#t.logger.info(`answerer: member timeout (${e.eventId} conn=${n})`),e.asked.size===0&&this.#o(e,`all members timed out`))},R)),e.asked.set(n,t),this.#r(e,n)}}async#r(e,t){let n=e.kind===`elicitation`?this.#t.buildQuestionParams(e.sessionId,e.frame):{callId:e.frame.request.callId};try{let r=await this.#t.dispatch(t,e.kind,n);if(e.state===`settled`||!e.asked.has(t))return;let i=this.#i(e,r);if(i.abstain){this.#a(e,t,`abstained`);return}this.#c(e,i.outcome,t)}catch(n){if(e.state===`settled`||!e.asked.has(t))return;if(n.code===z){this.#a(e,t,`abstained`);return}this.#t.logger.warn(`answerer: dispatch failed (${e.eventId} conn=${t}): ${n instanceof Error?n.message:String(n)}`),this.#a(e,t,`dispatch failed`)}}#i(e,t){if(e.kind===`permission`){let e=t?.outcome;return e?.outcome===`cancelled`?{abstain:!1,outcome:{kind:`result`,value:`cancelled`}}:{abstain:!1,outcome:{kind:`result`,value:e?.optionId===`allow-once`?`allowed-once`:`rejected`}}}let n=t;return n?.action===`accept`?{abstain:!1,outcome:{kind:`result`,value:n.content??{}}}:{abstain:!1,outcome:{kind:`rejected`,error:{code:n?.action===`decline`?`ASK_DECLINED`:`ASK_ABORTED`,message:`ask_user_question ${n?.action===`decline`?`declined`:`cancelled`} by client`}}}}#a(e,t,n){let r=e.asked.get(t);r?.timer!==void 0&&this.#t.timers.clear(r.timer),e.asked.delete(t),this.#t.logger.debug(`answerer: member dropped (${e.eventId} conn=${t} ${n})`),e.state===`active`&&e.asked.size===0&&this.#o(e,`all members out (${n})`)}#o(e,t){e.state===`active`&&(e.state=`grace`,e.graceTimer=this.#t.timers.set(()=>{this.#c(e,e.kind===`elicitation`?{kind:`rejected`,error:{code:`ASK_ABORTED`,message:`ask_user_question grace window expired`}}:{kind:`result`,value:`cancelled`},`grace`)},I),this.#t.logger.info(`answerer: grace entered (${e.eventId} ${t})`))}#s(e){let t=L-(this.#t.now()-e.budgetStartedAt);if(t<=0){this.#c(e,e.kind===`elicitation`?{kind:`rejected`,error:{code:`ASK_ABORTED`,message:`ask_user_question form budget exhausted`}}:{kind:`result`,value:`cancelled`},`budget`);return}this.#t.timers.set(()=>{e.state!==`settled`&&(this.#t.logger.warn(`answerer: form budget exhausted (${e.eventId})`),this.#c(e,e.kind===`elicitation`?{kind:`rejected`,error:{code:`ASK_ABORTED`,message:`ask_user_question form budget exhausted`}}:{kind:`result`,value:`cancelled`},`budget`))},t)}#c(e,t,n){if(e.state!==`settled`){e.state=`settled`,e.settledOutcome=t,e.respondedBy=n,e.graceTimer!==void 0&&(this.#t.timers.clear(e.graceTimer),e.graceTimer=void 0);for(let t of e.asked.values())t.timer!==void 0&&this.#t.timers.clear(t.timer);e.asked.clear(),this.#l(e.eventId,t),this.#t.resolvedNotify(e.kind,{sessionId:e.sessionId,eventId:e.eventId,outcome:t,respondedBy:n},typeof n==`string`&&n!==`grace`&&n!==`turn`&&n!==`dispose`&&n!==`budget`?n:void 0),this.#t.logger.info(`answerer: settled (${e.eventId} by=${n} kind=${t.kind})`)}}async#l(e,t){try{await this.#t.respond(e,t)}catch(t){this.#t.logger.warn(`answerer: respond failed (${e}): ${t instanceof Error?t.message:String(t)}`)}}};function V(e,t){e.gate.active||(e.gate.active=!0,e.gate.timer=setTimeout(()=>{e.gate.active&&(t.warn(`gateway load gate timeout (${e.connId})——释放缓存帧`),H(e,!0))},3e4),e.gate.timer.unref?.())}function H(e,t){if(!e.gate.active)return[];e.gate.active=!1,e.gate.timer!==void 0&&clearTimeout(e.gate.timer),e.gate.timer=void 0;let n=t?e.gate.frames.splice(0):[];return e.gate.frames.length=0,n}function U(e,t){e.socket.readyState===e.socket.OPEN&&e.socket.send(JSON.stringify(t))}function W(e,t,n){let{core:r,hooks:i,authenticate:a,logger:s}=n,c=new T(s,i),l=new o({noServer:!0}),u=new Set;return r.io.notify=async e=>{c.route(e)},r.io.extNotify=async e=>{c.routeExt(e)},e.on(`upgrade`,(e,n,o)=>{new URL(e.url??`/`,`http://localhost`).pathname===t&&(async()=>{if(!(a===void 0||await G(a,e,s))){n.write(`HTTP/1.1 401 Unauthorized\r
3
+ `);if(t<0)break;let r=e.slice(0,t);e=e.slice(t+1),n.forEach(e=>e(r))}return e};return n.stdout?.on(`data`,e=>{i=l(i,e.toString(),o)}),n.stderr?.on(`data`,e=>{a=l(a,e.toString(),s)}),n.on(`close`,e=>{i!==``&&o.forEach(e=>e(i)),a!==``&&s.forEach(e=>e(a)),c.forEach(t=>t(e))}),{writeStdin:e=>n.stdin?.write(e),onStdoutLine:e=>o.push(e),onStderrLine:e=>s.push(e),onExit:e=>c.push(e),kill:e=>n.kill(e??`SIGTERM`)}}}function p(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function m(e){return!p(e)||typeof e.type!=`string`||e.type===``||typeof e.seq!=`number`||!Number.isInteger(e.seq)||typeof e.time!=`number`||!(`data`in e)?null:e}function h(e){if(!p(e)||e.type!==`event`)return null;let t=m(e.event);return t===null?null:{type:`event`,event:t}}function g(e){if(!p(e))return null;if(e.type===`snapshot`){let t=p(e.header)?e.header:null;if(t===null||typeof e.cursor!=`number`||typeof e.hasMore!=`boolean`)return null;let n=[];if(Array.isArray(e.records))for(let t of e.records){let e=h(t);if(e===null)return null;n.push(e)}else return null;return{type:`snapshot`,header:t,cursor:e.cursor,records:n,hasMore:e.hasMore,projections:e.projections,...p(e.assistantStream)?{assistantStream:e.assistantStream}:{}}}return e.type===`assistant-stream`?p(e.frame)?{type:`assistant-stream`,frame:e.frame}:null:h(e)}function _(e){if(!p(e)||typeof e.hasMore!=`boolean`||!Array.isArray(e.records))return null;let t=[];for(let n of e.records){let e=h(n);if(e===null)return null;t.push(e)}return{records:t,hasMore:e.hasMore}}function v(e){return p(e)&&(e.type===`ready`&&typeof e.clientId==`string`&&p(e.host)||e.type===`emit`&&typeof e.event==`string`&&Array.isArray(e.args)||e.type===`waterfall`&&typeof e.eventId==`string`&&typeof e.agentId==`string`&&typeof e.event==`string`&&p(e.request)||e.type===`cancel`&&typeof e.eventId==`string`)?e:null}var y=class extends Error{code;details;constructor(e){super(e.message),this.name=`CarrierRpcError`,this.code=e.code,this.details=e.details}},b=class extends Error{constructor(e){super(e),this.name=`CarrierTransportError`}},x=class{#e;#t;#n;#r;#i;#a;#o=!1;#s=!1;constructor(e){this.#e=e.baseUrl.replace(/\/$/,``),this.#t=e.cookie,this.#n=e.refreshCredentials}async unary(e,t){this.#p();try{return await this.#c(e,t)}catch(n){if(!(n instanceof b&&n.message.endsWith(`HTTP 401`))||this.#n===void 0)throw n;return await this.#f(),this.#c(e,t)}}async#c(e,t){let n=i(),r;try{r=await fetch(`${this.#e}/api/${e}`,{method:`POST`,headers:{"content-type":`application/json`,cookie:this.#t},body:JSON.stringify({type:`client-request`,rpcId:n,method:e,payload:{args:t}}),signal:AbortSignal.timeout(6e4)})}catch(t){throw t instanceof Error&&t.name===`TimeoutError`?new b(`unary ${e} 超时(上游 60s 无响应)`):t}if(r.status!==200)throw new b(`unary ${e} HTTP ${r.status}`);let a=await r.json();if(a.type!==`server-response`||a.rpcId!==n)throw new b(`unary ${e} envelope 畸形(type/rpcId 失配)`);if(a.result.ok)return a.result.value;throw new y(a.result.error)}openStream(e,t,n){this.#p();let r=i(),o=!1,s={canceled:!1,onFrame:e=>{e.type===`item`?n.onItem(e.value):e.type===`end`?(s.canceled=!0,n.onEnd?.()):e.type===`error`&&(s.canceled=!0,n.onError?.(e.error??{code:`gateway/internal`,message:`stream error 帧缺 error 字段`}))},onDown:e=>{s.canceled||(s.canceled=!0,e&&n.onError?.({code:`carrier/mux-closed`,message:`mux 插座意外关闭`}),n.onClose?.(e))}};return this.#l().then(n=>{if(o||s.canceled){process.stderr.write(`[carrier] open ABORTED (canceled=${String(o)} entry=${String(s.canceled)}) ${e}\n`);return}n.streams.set(r,s),n.socket.readyState===a.OPEN?(n.socket.send(JSON.stringify({type:`open`,streamId:r,endpoint:e,payload:{args:t}})),process.env.CARRIER_WIRE_DEBUG===`1`&&process.stderr.write(`[carrier] open sent ${e} streamId=${r.slice(0,8)}\n`)):process.stderr.write(`[carrier] open SKIPPED (readyState ${n.socket.readyState}) ${e}\n`)}).catch(e=>{o||s.canceled||(s.canceled=!0,n.onError?.({code:`carrier/mux-open`,message:e.message}),n.onClose?.(!0))}),{cancel:()=>{o=!0,s.canceled=!0;let e=this.#r;e!==void 0&&e.streams.delete(r)&&e.socket.readyState===a.OPEN&&e.socket.send(JSON.stringify({type:`cancel`,streamId:r}))}}}async#l(){let e=this.#r;if(e!==void 0&&e.socket.readyState===a.OPEN)return e;if(this.#i!==void 0)return this.#i;let t=this.#u().finally(()=>{this.#i===t&&(this.#i=void 0)});return this.#i=t,t}async#u(){this.#p(),this.#o&&this.#n!==void 0&&await this.#f();let e=new a(`${S(this.#e)}/api/remote.mux`,{headers:{cookie:this.#t}});await new Promise((t,n)=>{let r=setTimeout(()=>n(new b(`mux 插座打开超时`)),15e3);r.unref(),e.once(`open`,()=>{clearTimeout(r),t()}),e.once(`error`,e=>{clearTimeout(r),n(new b(`mux 插座打开失败: ${e.message}`))}),e.once(`unexpected-response`,(e,t)=>{clearTimeout(r),n(new b(`mux 升级被拒 HTTP ${t.statusCode??`?`}`))})});let t={socket:e,streams:new Map};e.on(`message`,e=>{let n;try{n=JSON.parse(String(e))}catch{return}typeof n.streamId==`string`&&t.streams.get(n.streamId)?.onFrame(n)});let n=!1;return e.on(`close`,()=>this.#d(t,n)),e.on(`error`,()=>{n=!0}),this.#r=t,this.#o=!0,t}#d(e,t){if(this.#r===e){this.#r=void 0;for(let n of e.streams.values())n.onDown(t);e.streams.clear()}}#f(){if(this.#a!==void 0)return this.#a;let e=(async()=>{let e=this.#n;if(e===void 0)return;let t=await e();this.#t=t.cookie,this.#e=t.baseUrl.replace(/\/$/,``)})().finally(()=>{this.#a===e&&(this.#a=void 0)});return this.#a=e,e}openEvents(e){let t,n=n=>{switch(n.type){case`ready`:t=n.clientId,e.onReady(n);return;case`emit`:e.onEmit(n);return;case`waterfall`:e.onWaterfall(n);return;case`cancel`:e.onCancel(n);return}},r=this.openStream(`$events`,{},{onItem:t=>{let i=v(t);if(i===null){e.onClose?.(!1),r.cancel();return}n(i)},onClose:t=>e.onClose?.(t)});return{respond:async(e,n)=>{if(t===void 0)throw new b(`waterfall 应答先于 ready 帧(clientId 缺席)`);await this.unary(`$events/result`,{clientId:t,eventId:e,outcome:n})},close:()=>r.cancel()}}dispose(){this.#s=!0,this.#r?.socket.close(),this.#r=void 0}#p(){if(this.#s)throw new b(`carrier 已释放`)}};function S(e){if(e.startsWith(`https://`))return`wss://${e.slice(8)}`;if(e.startsWith(`http://`))return`ws://${e.slice(7)}`;throw new b(`baseUrl 非法(期待 http(s)://,实得 ${e})`)}function C(e,r){let i=m(e);if(i===null)return r!==void 0&&(r.invalid+=1),null;if(t(i.type)===`project`)return typeof i.data!=`object`||i.data===null||Array.isArray(i.data)?(r!==void 0&&(r.opaqueWarn+=1),{kind:`opaque`,event:i,level:`warn`}):(r!==void 0&&(r.consumable+=1),{kind:`consumable`,event:i});let a=n(i)?`debug`:`warn`;return a===`debug`?r!==void 0&&(r.opaqueDebug+=1):r!==void 0&&(r.opaqueWarn+=1),{kind:`opaque`,event:i,level:a}}var w=class{#e;#t;#n={invalid:0,opaqueDebug:0,opaqueWarn:0,consumable:0};#r=new Map;#i=0;constructor(e,t){this.#e=e,this.#t=t?.reopenBaseMs??1e3}open(e,t){let n=this.#r.get(e);if(n!==void 0&&!n.closed)return n.handlers=t,{close:()=>this.close(e)};let r={sessionId:e,handlers:t,handle:void 0,cursor:void 0,baseline:void 0,streamSeenRevision:!1,reopening:!1,backoffMs:this.#t,closed:!1,pending:[]};return this.#r.set(e,r),this.#a(r),{close:()=>this.close(e)}}close(e){let t=this.#r.get(e);t!==void 0&&(t.closed=!0,t.handle?.cancel(),this.#r.delete(e))}stats(){let e=0,t=0;for(let n of this.#r.values())n.closed||(n.reopening||n.cursor===void 0?t++:e++);return{live:e,reopening:t,decoded:{...this.#n},gapsRepaired:this.#i}}#a(e){e.closed||(e.reopening=e.cursor!==void 0,e.handle=this.#e.openStream(`session/follow`,{request:{address:{kind:`session`,sessionId:e.sessionId},assistantStream:!0}},{onItem:t=>this.#s(e,t),onEnd:()=>this.#o(e,`stream end`),onError:t=>this.#o(e,`${t.code}: ${t.message}`),onClose:t=>{t&&this.#o(e,`socket error`)}}))}#o(e,t){if(e.closed)return;e.handle=void 0,e.handlers.onDown(t);let n=e.backoffMs;e.backoffMs=Math.min(e.backoffMs*2,3e4),e.baseline=void 0,e.streamSeenRevision=!1,e.reopening=!0,setTimeout(()=>{e.closed||this.#a(e)},n)}#s(e,t){if(t.type===`snapshot`){this.#c(e,t);return}if(e.cursor!==void 0){if(e.reopening){e.pending.push(t);return}this.#d(e,t)}}#c(e,t){let n=e.cursor;if(n!==void 0&&t.cursor<n){e.handlers.onDown(`cursor 回退:${String(t.cursor)} < ${String(n)}`);return}e.baseline=t.assistantStream,e.streamSeenRevision=!1,e.reopening=!0,e.handlers.onSnapshot(t.header,t.cursor,t.projections,t.assistantStream),this.#l(e,t,n)}async#l(e,t,n){try{let r=[];if(n!==void 0&&t.records.length>0){let i=t.records[0]?.event.seq??t.cursor;i>n+1&&(r.push(...await this.#u(e,n+1,i)),this.#i+=r.length)}for(let e of t.records)r.push(e.event);for(let t of r)this.#f(e,t);let i=e.pending;e.pending=[],e.reopening=!1,e.cursor=t.cursor,e.backoffMs=this.#t;for(let t of i)this.#d(e,t)}catch(t){e.handlers.onDown(`backlog 补齐失败:${String(t)}`)}}async#u(e,t,n){let r=[],i=n;for(;i>t;){let n=_(await this.#e.unary(`session/page`,{request:{address:{kind:`session`,sessionId:e.sessionId},throughSeq:i-1,beforeSeq:t,maxMessages:200}}));if(n===null)throw new b(`session/page 帧畸形(records/hasMore 缺失)`);for(let e of n.records)r.push(e.event);let a=n.records[0]?.event.seq;if(a===void 0||a<=t||(i=a,!n.hasMore))break}return r.sort((e,t)=>e.seq-t.seq),r}#d(e,t){if(t.type===`assistant-stream`){if(!e.streamSeenRevision){e.streamSeenRevision=!0;let n=(e.baseline?.revision??-1)+1;if(e.baseline!==void 0&&t.frame.revision!==n){e.handlers.onDown(`assistant-stream 衔接断裂:revision ${String(t.frame.revision)} ≠ ${String(n)}`);return}}e.handlers.onAssistantStream(t.frame);return}t.type===`event`&&this.#f(e,t.event)}#f(e,t){let n=C(t,this.#n);n!==null&&n.kind===`consumable`&&e.handlers.onEvent(n.event)}async readAll(e){return await new Promise((t,n)=>{let r=new Map,i,a,o=!1,s=this.#e.openStream(`session/follow`,{request:{address:{kind:`session`,sessionId:e}}},{onItem:c=>{o||c.type===`snapshot`&&(o=!0,i=c.header,a=c.projections,s.cancel(),this.#p(e,c,r).then(e=>t({header:i,events:e,projections:a}),e=>n(e)))},onEnd:()=>{o||(o=!0,n(new b(`session/follow 在 snapshot 前关闭`)))},onError:e=>{o||(o=!0,n(new y(e)))}})})}async#p(e,t,n){let r=[];for(let e of t.records)r.push(e.event);let i=t.records.length>0?t.records[0]?.event.seq:void 0;for(;t.hasMore===!0;){let n=_(await this.#e.unary(`session/page`,{request:{address:{kind:`session`,sessionId:e},throughSeq:t.cursor,...i===void 0?{}:{beforeSeq:i},maxMessages:200}}));if(n===null)throw new b(`session/page 帧畸形(records/hasMore 缺失)`);for(let e of n.records)r.push(e.event);if(!n.hasMore||n.records.length===0)break;let a=n.records[0]?.event.seq;if(a===void 0||a===0)break;i=a}let a=new Map;for(let e of r)a.set(e.seq,e);let o=[...a.values()].sort((e,t)=>e.seq-t.seq);for(let e=0;e<o.length;e++){let t=o[e];if(t?.seq!==e)throw new y({code:`gateway/internal`,message:`journal 连续性断裂:位置 ${String(e)} 实得 seq ${String(t?.seq)}`});let r=C(t,this.#n);r?.kind===`consumable`&&n.set(t.seq,r.event)}return[...n.values()]}},T=class{#e=new Map;#t=new Map;#n=new Map;#r=new Map;#i;#a;#o=0;#s=0;constructor(e,t){this.#i=e,this.#a=t}attachConnection(e,t,n){this.#e.set(e,{connId:e,send:t,ctx:n})}detachConnection(e){this.#e.delete(e);for(let[t,n]of this.#t)n.delete(e),n.size===0&&this.#t.delete(t),this.#a?.onSessionUnbind?.(e,t);for(let[t,n]of this.#n)n===e&&this.#n.delete(t)}bind(e,t){let n=this.#t.get(t);n===void 0&&(n=new Set,this.#t.set(t,n));let r=n.has(e);n.add(e),r||this.#a?.onSessionBind?.(e,t)}unbind(e,t){this.#t.get(t)?.delete(e),this.#a?.onSessionUnbind?.(e,t)}boundConnIds(e){return[...this.#t.get(e)??[]]}ctxOf(e){return this.#e.get(e)?.ctx}routeResolved(e,t,n){this.routeExt({method:e,payload:t},n===void 0?void 0:{excludeConnId:n})}beginReplayCount(e){this.#r.set(e,0)}takeReplayCount(e){let t=this.#r.get(e);return this.#r.delete(e),t}setTurnOrigin(e,t){this.#n.set(e,t)}clearTurnOrigin(e){this.#n.delete(e)}route(e){let t=e.sessionId;if(t!==void 0&&this.#a?.onSessionUpdate!==void 0){let n=e.update??e;E(this.#i,`onSessionUpdate`,()=>this.#a?.onSessionUpdate?.(t,n))}let n;if(t===void 0)n=[...this.#e.keys()];else{let r=this.#n.get(t),i=e.update?.sessionUpdate===`user_message_chunk`;n=[...this.#t.get(t)??[]],r!==void 0&&i&&(n=n.filter(e=>e!==r),this.#s++);let a=this.#a?.fanout===void 0?void 0:E(this.#i,`fanout`,()=>this.#a?.fanout?.(e,n));if(a!==void 0&&(n=[...a]),n.length===0){this.#o++;return}}for(let t of n){let n=this.#e.get(t);if(n===void 0)continue;let r=e;if(this.#a?.transformOutbound!==void 0){let t=E(this.#i,`transformOutbound`,()=>this.#a?.transformOutbound?.(e,n.ctx));if(t===null)continue;t!==void 0&&(r=t)}n.send({jsonrpc:`2.0`,method:`session/update`,params:r});let i=this.#r.get(t);i!==void 0&&this.#r.set(t,i+1)}}routeExt(e,t){let n=typeof e.payload.sessionId==`string`?e.payload.sessionId:void 0,r=n===void 0?[...this.#e.keys()]:[...this.#t.get(n)??[]];t?.excludeConnId!==void 0&&(r=r.filter(e=>e!==t.excludeConnId));for(let t of r)this.#e.get(t)?.send({jsonrpc:`2.0`,method:e.method,params:e.payload})}stats(){return{connections:this.#e.size,dropped:this.#o,suppressed:this.#s}}};function E(e,t,n){try{return n()}catch(n){e.warn(`gateway hook ${t} failed (fail-open): ${String(n)}`);return}}function D(e){if(typeof e!=`object`||!e)return e;let t=e;if(t.type!==`image`)return e;let n=t.attachment;if(typeof n!=`object`||!n)return e;let r=n;return typeof r.mediaType!=`string`||typeof r.data!=`string`?e:{type:`image`,mediaType:r.mediaType,data:r.data,...typeof r.name==`string`?{name:r.name}:{}}}const O=1e3,k=3e4;function A(e){let t=e,n=t?.values?.modelSelection?.next??t?.values?.modelSelection?.lastUsed;if(!(n==null||typeof n.provider!=`string`||typeof n.model!=`string`))return{provider:n.provider,model:n.model,...typeof n.reasoningEffort==`string`?{reasoningEffort:n.reasoningEffort}:{}}}function j(e,t){return e===void 0||t===void 0?e===t:e.provider===t.provider&&e.model===t.model&&e.reasoningEffort===t.reasoningEffort}function M(e){if(typeof e!=`object`||!e)return-1;let t=e.asOfSeq;return typeof t==`number`?t:-1}function N(e){return e.pendingSelection??A(e.projections)??e.fallbackSelection}function P(e){return{id:e.id,get header(){let t=e.header;return{...t?.cwd===void 0?{}:{cwd:t.cwd},...t?.origin===void 0?{}:{origin:t.origin},...t?.parentSession===void 0?{}:{parentSession:t.parentSession},...t?.createdAt===void 0?{}:{createdAt:t.createdAt}}},requestHeader(){let t=N(e);if(t!==void 0)return{config:{provider:t.provider,model:t.model,...t.reasoningEffort===void 0?{}:{reasoningEffort:t.reasoningEffort}}}}}}function F(e,t,n){return{id:e.id,session:e.session,model:{current:()=>N(e),select:async n=>{j(e.pendingSelection??A(e.projections),n)||(e.pendingSelection=(await t.unary(`session/selectModel`,{request:{sessionId:e.id,...n}}))?.selected??n,e.pendingAtSeq=M(e.projections))}},cancel:async n=>{await t.unary(`session/cancel`,{request:{sessionId:e.id}})},followup:t=>{n(e,t)}}}var I=class{logger;#e;#t;#n;#r=new Map;#i=new Map;#a=new Set;#o=new Set;#s=new Set;#c=new Set;#l=new Set;#u;#d=O;#f;#p=O;#m;#h={groups:[],failures:[]};#g=0;#_;#v=!1;#y;constructor(e){this.#e=e.carrier,this.#t=e.pool,this.#n=e.logger,this.logger=e.logger,this.#y=this.#E(),this.#O(),this.#A()}async createAgent(e){return await this.#b(e)}async resumeAgent(e){return await this.#b(e)}async#b(e){let t=(await this.#e.unary(`session/create`,{request:{sessionId:e.sessionId,cwd:e.cwd}})).sessionId,n=this.#r.get(t);if(n!==void 0)return await this.#i.get(t)?.catch(()=>{}),n.fallbackSelection=this.#_,await e.configureScope?.(this.#C(n),n.agent),{agent:n.agent,dispose:async()=>this.#S(t)};let r={id:t,header:{version:1,id:t,createdAt:0,cwd:e.cwd,isSeeded:!1},projections:{},promptInflight:!1};r.session=P(r),r.agent=F(r,this.#e,(e,t)=>this.#w(e,t));let i=r;this.#r.set(t,i);try{let n=this.#x(t).then(e=>{e!==void 0&&(i.projections=e)});this.#i.set(t,n),await n,this.#i.delete(t),this.#_===void 0&&(await this.#y,this.#_===void 0&&!this.#v&&(this.#v=!0,await this.#E())),i.fallbackSelection=this.#_,await e.configureScope?.(this.#C(i),i.agent),this.#t.open(t,this.#D(i))}catch(e){throw await this.#S(t).catch(()=>{}),e}return{agent:i.agent,dispose:async()=>this.#S(t)}}async#x(e){try{let t=await this.#e.unary(`session/projections`,{request:{sessionId:e}});if(typeof t!=`object`||!t||Array.isArray(t))return;let n=t,r=n.values;return typeof r!=`object`||!r||Array.isArray(r)?void 0:{asOfSeq:typeof n.asOfSeq==`number`?n.asOfSeq:-1,values:r}}catch(t){this.#n.debug(`projection prime failed (${e}): ${String(t)}`);return}}#S(e){return this.#t.close(e),this.#r.delete(e),this.#i.delete(e),Promise.resolve()}#C(e){return{on:(e,t)=>()=>{},tools:{restrict:e=>void 0}}}async#w(e,t){e.promptInflight=!0;let n=t.content.map(D);await this.#e.unary(`session/prompt`,{request:{requestId:t.id,sessionId:e.id,mode:`steer`,content:n}})}persistence={stat:async(e,t)=>{let n=await this.#e.unary(`tboxcode/sessions.stat`,{request:{sessionId:e}});if(n!==null)return{header:n.header}},list:async e=>(await this.#e.unary(`session/list`,{_request:{}})).items.map(e=>({header:{id:e.sessionId,...e.cwd===void 0?{}:{cwd:e.cwd},...e.origin===void 0?{}:{origin:e.origin},...e.parentSessionId===void 0?{}:{parentSession:e.parentSessionId},createdAt:e.updatedAt}})),openHistoryRead:async(e,t)=>{let n=await this.#t.readAll(e);return{read:async()=>({events:n.events}),close:async()=>{}}}};sessionStore={get:e=>this.#r.get(e)?.session,flush:async e=>{await this.#e.unary(`tboxcode/sessions.flush`,{request:{sessionId:e.id}})}};llm={listProviders:()=>this.#T().groups.map(e=>({id:e.id,name:e.name})),listModels:async e=>(this.#T().groups.find(t=>t.id===e)?.models??[]).map(e=>({id:String(e.id),name:String(e.name),...e.description===void 0?{}:{description:String(e.description)}})),resolveModelInfo:async(e,t)=>{let n=this.#T().groups.find(t=>t.id===e)?.models.find(e=>e.id===t)?.reasoning;return{reasoning:{defaultEffort:n?.defaultEffort,efforts:n?.efforts??[]}}},resolveCallConfig:async e=>e};#T(){return Date.now()-this.#g>3e4&&this.#E(),this.#h}async#E(){try{this.#h=await this.#e.unary(`session/modelCatalog`,{}),this.#g=Date.now();let e=await this.#e.unary(`tboxcode/selection.default`,{});this.#_=typeof e.provider==`string`&&typeof e.model==`string`?{provider:e.provider,model:e.model,...e.reasoningEffort===void 0?{}:{reasoningEffort:e.reasoningEffort}}:void 0}catch(e){this.#n.warn(`catalog warm failed: ${String(e)}`)}}async admitPromptContent(e){return e.map(e=>e.type===`text`?e:{type:`image`,attachment:{mediaType:e.mediaType,data:e.data,...e.name===void 0?{}:{name:e.name}}})}applyModeKnobs(t,n,r){let i=Object.entries(e).find(([,e])=>e.sandbox===r.sandbox&&e.approval===r.approval)?.[0];if(i===void 0)throw Error(`tboxcode: mode knobs 无对位(${String(r.sandbox)}/${String(r.approval)}——MODE_TABLE 外组合)`);return this.#e.unary(`tboxcode/mode`,{request:{agentId:t.id,mode:i}}).then(()=>void 0)}defaultSelection(){return this.#_}async executeCommand(e,t,n){let r=await this.#e.unary(`tboxcode/commands.execute`,{request:{agentId:e.id,line:t}});return r?.commandId===void 0?void 0:{commandId:r.commandId}}async listCommands(e){return await this.#e.unary(`tboxcode/commands.list`,{request:{agentId:e.id}})}async listUserInvocableSkills(e){return await this.#e.unary(`tboxcode/skills.userInvocable`,{request:{agentId:e.id}})}onSessionEvent(e){let t=e;return this.#a.add(t),()=>this.#a.delete(t)}onAssistantStream(e){return this.#o.add(e),()=>this.#o.delete(e)}onInboxClaimed(e){return this.#s.add(e),()=>this.#s.delete(e)}onAgentError(e){return this.#c.add(e),()=>this.#c.delete(e)}sessionToolDenies(){return[]}onCommandSurfaceChanged(e){return this.#l.add(e),()=>this.#l.delete(e)}sessionIds(){return[...this.#r.keys()]}hasInflightPrompt(e){return this.#r.get(e)?.promptInflight===!0}poolLive(){return this.#t.stats().live>0}poolEntries(){return this.#r.size}settlePrompt(e,t){let n=this.#r.get(e);if(!(n===void 0||!n.promptInflight)){n.promptInflight=!1;for(let e of this.#c)e({agent:n.agent,turn:0,error:t})}}#D(e){return{onSnapshot:(t,n,r)=>{e.header=t;let i=M(r);e.pendingSelection!==void 0&&i>(e.pendingAtSeq??-1)&&A(r)!==void 0&&(e.pendingSelection=void 0,e.pendingAtSeq=void 0),e.projections=r},onEvent:t=>{t.type===`turn/end`&&(e.promptInflight=!1);for(let n of this.#a)n(e.session,t)},onAssistantStream:t=>{let n={type:t.type??(t.finish===void 0?t.index===0?`start`:`chunk`:`end`),attemptId:t.attemptId,...t.turn===void 0?{}:{turn:t.turn},...t.step===void 0?{}:{step:t.step},...t.chunk===void 0?{}:{chunk:t.chunk}};for(let t of this.#o)t({agent:e.agent,frame:n})},onDown:t=>this.#n.warn(`follow pool down (${e.id}): ${t}`)}}#O(){this.#u=this.#e.openStream(`tboxcode/agent.watch`,{},{onItem:e=>{this.#n.debug(`agent.watch item: ${JSON.stringify(e).slice(0,160)} states=${[...this.#r.keys()].join(`,`)}`);let t=this.#r.get(e.sessionId);if(t!==void 0){if(e.kind===`inboxClaimed`)for(let n of this.#s)n({agent:t.agent,message:{...e.messageId===void 0?{}:{id:e.messageId}},turn:e.turn??0});else if(e.kind===`error`)for(let n of this.#c)n({agent:t.agent,turn:0,error:e.error??`unknown agent error`})}},onError:e=>{this.#n.warn(`agent.watch stream error: ${JSON.stringify(e).slice(0,200)}`),this.#k()},onClose:e=>{e&&(this.#n.warn(`agent.watch stream closed`),this.#k())}})}#k(){let e=this.#d;this.#d=Math.min(this.#d*2,k),setTimeout(()=>{this.#O(),this.#d=O},e)}#A(){let e=()=>{for(let e of this.#l)e()};this.#f=L(this.#e,e,this.#n),this.#m=this.#e.openStream(`tboxcode/skills.watch`,{},{onItem:()=>e(),onError:()=>this.#j(),onClose:e=>{e&&this.#j()}})}#j(){let e=this.#p;this.#p=Math.min(this.#p*2,k),setTimeout(()=>{this.#A(),this.#p=O},e)}dispose(){this.#u?.cancel(),this.#f?.close(),this.#m?.cancel()}};function L(e,t,n){return e.openEvents({onReady:()=>n.debug(`commands/change $events ready`),onEmit:e=>{e.event===`commands/change`&&t()},onWaterfall:()=>{},onCancel:()=>{},onClose:e=>{e&&n.warn(`commands/change $events closed with error`)}})}var R=class{#e;#t;#n;#r;#i;#a;#o=!1;#s;constructor(e,t){this.#e=e,this.#t=t?.timeoutMs??12e4,this.#n=t?.tickMs??1e3,this.#r=t?.now??Date.now,this.#i=t?.logger??{warn:()=>{},info:()=>{},debug:()=>{}}}start(){this.#s=setInterval(()=>this.tick(),this.#n)}stop(){this.#s!==void 0&&clearInterval(this.#s),this.#s=void 0}tick(){if(!(this.#e.poolEntries()>0&&!this.#e.poolLive())){this.#a=void 0,this.#o=!1;return}if(this.#a===void 0){this.#a=this.#r();return}if(this.#o||this.#r()-this.#a<this.#t)return;this.#o=!0;let e=this.#r()-this.#a;for(let t of this.#e.sessionIds())this.#e.hasInflightPrompt(t)&&(this.#i.warn(`carrier timeout settle (${t}): downMs=${String(e)}`),this.#e.settle(t,Error(`tboxcode carrier-timeout (downMs=${String(e)})`)))}snapshot(){return{down:this.#a!==void 0,downSince:this.#a,settled:this.#o}}};const z=6e4,B=36e5,V=3e4,H=-32001;var U=class{#e=new Map;#t;constructor(e){this.#t=e}get size(){return this.#e.size}pendingOf(e){for(let t of this.#e.values())if(t.sessionId===e)return t}begin(e){let t=e.event===`user-questions/request`?`elicitation`:e.event===`approval/request`?`permission`:void 0;if(t===void 0||this.#e.has(e.eventId))return;let n=this.#t.sessionIdOfAgent(e.agentId);if(n===void 0){this.#l(e.eventId,{kind:`rejected`,error:{code:`gateway/agent-not-found`,message:`agent ${e.agentId} not managed by gateway`}});return}let r={kind:t,sessionId:n,eventId:e.eventId,frame:e,asked:new Map,state:`active`,budgetStartedAt:this.#t.now()};this.#e.set(e.eventId,r),this.#n(r),this.#s(r)}cancel(e){let t=this.#e.get(e.eventId);t===void 0||t.state===`settled`||this.#c(t,{kind:`result`,value:`cancelled`},`turn`)}attach(e,t){for(let n of this.#e.values()){if(n.sessionId!==t||n.state!==`grace`)continue;let r=n.graceTimer;n.graceTimer=void 0,r!==void 0&&this.#t.timers.clear(r),n.state=`active`,this.#t.logger.info(`answerer: grace broken by attach (${n.eventId} conn=${e})`),this.#n(n)}}switchBack(e,t){this.attach(e,t)}disconnect(e){for(let t of this.#e.values())t.asked.delete(e)&&t.state===`active`&&t.asked.size===0&&this.#o(t,`member disconnected`)}dispose(){for(let e of[...this.#e.values()])e.state!==`settled`&&this.#c(e,{kind:`result`,value:`cancelled`},`dispose`);this.#e.clear()}#n(e){let t=this.#t.eligibleConns(e.sessionId).filter(t=>!e.asked.has(t)).filter(t=>e.kind===`permission`||this.#t.capsOf(t));if(t.length===0){this.#o(e,`no eligible connections`);return}for(let n of t){let t={connId:n};e.kind===`permission`&&(t.timer=this.#t.timers.set(()=>{e.state!==`active`||!e.asked.has(n)||(e.asked.delete(n),this.#t.logger.info(`answerer: member timeout (${e.eventId} conn=${n})`),e.asked.size===0&&this.#o(e,`all members timed out`))},V)),e.asked.set(n,t),this.#r(e,n)}}async#r(e,t){let n=e.kind===`elicitation`?this.#t.buildQuestionParams(e.sessionId,e.frame):{callId:e.frame.request.callId};try{let r=await this.#t.dispatch(t,e.kind,n);if(e.state===`settled`||!e.asked.has(t))return;let i=this.#i(e,r);if(i.abstain){this.#a(e,t,`abstained`);return}this.#c(e,i.outcome,t)}catch(n){if(e.state===`settled`||!e.asked.has(t))return;if(n.code===H){this.#a(e,t,`abstained`);return}this.#t.logger.warn(`answerer: dispatch failed (${e.eventId} conn=${t}): ${n instanceof Error?n.message:String(n)}`),this.#a(e,t,`dispatch failed`)}}#i(e,t){if(e.kind===`permission`){let e=t?.outcome;return e?.outcome===`cancelled`?{abstain:!1,outcome:{kind:`result`,value:`cancelled`}}:{abstain:!1,outcome:{kind:`result`,value:e?.optionId===`allow-once`?`allowed-once`:`rejected`}}}let n=t;return n?.action===`accept`?{abstain:!1,outcome:{kind:`result`,value:n.content??{}}}:{abstain:!1,outcome:{kind:`rejected`,error:{code:n?.action===`decline`?`ASK_DECLINED`:`ASK_ABORTED`,message:`ask_user_question ${n?.action===`decline`?`declined`:`cancelled`} by client`}}}}#a(e,t,n){let r=e.asked.get(t);r?.timer!==void 0&&this.#t.timers.clear(r.timer),e.asked.delete(t),this.#t.logger.debug(`answerer: member dropped (${e.eventId} conn=${t} ${n})`),e.state===`active`&&e.asked.size===0&&this.#o(e,`all members out (${n})`)}#o(e,t){e.state===`active`&&(e.state=`grace`,e.graceTimer=this.#t.timers.set(()=>{this.#c(e,e.kind===`elicitation`?{kind:`rejected`,error:{code:`ASK_ABORTED`,message:`ask_user_question grace window expired`}}:{kind:`result`,value:`cancelled`},`grace`)},z),this.#t.logger.info(`answerer: grace entered (${e.eventId} ${t})`))}#s(e){let t=B-(this.#t.now()-e.budgetStartedAt);if(t<=0){this.#c(e,e.kind===`elicitation`?{kind:`rejected`,error:{code:`ASK_ABORTED`,message:`ask_user_question form budget exhausted`}}:{kind:`result`,value:`cancelled`},`budget`);return}this.#t.timers.set(()=>{e.state!==`settled`&&(this.#t.logger.warn(`answerer: form budget exhausted (${e.eventId})`),this.#c(e,e.kind===`elicitation`?{kind:`rejected`,error:{code:`ASK_ABORTED`,message:`ask_user_question form budget exhausted`}}:{kind:`result`,value:`cancelled`},`budget`))},t)}#c(e,t,n){if(e.state!==`settled`){e.state=`settled`,e.settledOutcome=t,e.respondedBy=n,e.graceTimer!==void 0&&(this.#t.timers.clear(e.graceTimer),e.graceTimer=void 0);for(let t of e.asked.values())t.timer!==void 0&&this.#t.timers.clear(t.timer);e.asked.clear(),this.#l(e.eventId,t),this.#t.resolvedNotify(e.kind,{sessionId:e.sessionId,eventId:e.eventId,outcome:t,respondedBy:n},typeof n==`string`&&n!==`grace`&&n!==`turn`&&n!==`dispose`&&n!==`budget`?n:void 0),this.#t.logger.info(`answerer: settled (${e.eventId} by=${n} kind=${t.kind})`)}}async#l(e,t){try{await this.#t.respond(e,t)}catch(t){this.#t.logger.warn(`answerer: respond failed (${e}): ${t instanceof Error?t.message:String(t)}`)}}};function W(e,t){e.gate.active||(e.gate.active=!0,e.gate.timer=setTimeout(()=>{e.gate.active&&(t.warn(`gateway load gate timeout (${e.connId})——释放缓存帧`),G(e,!0))},3e4),e.gate.timer.unref?.())}function G(e,t){if(!e.gate.active)return[];e.gate.active=!1,e.gate.timer!==void 0&&clearTimeout(e.gate.timer),e.gate.timer=void 0;let n=t?e.gate.frames.splice(0):[];return e.gate.frames.length=0,n}function K(e,t){e.socket.readyState===e.socket.OPEN&&e.socket.send(JSON.stringify(t))}function q(e,t,n){let{core:r,hooks:i,authenticate:a,logger:s}=n,c=new T(s,i),l=new o({noServer:!0}),u=new Set;r.io.notify=async e=>{c.route(e)},r.io.extNotify=async e=>{c.routeExt(e)};let d=(e,n,o)=>{new URL(e.url??`/`,`http://localhost`).pathname===t&&(async()=>{if(!(a===void 0||await J(a,e,s))){n.write(`HTTP/1.1 401 Unauthorized\r
4
4
  \r
5
- `),n.destroy();return}l.handleUpgrade(e,n,o,e=>{K(e,r,i,c,s,u)})})().catch(e=>{s.warn(`gateway upgrade failed: ${String(e)}`),n.destroy()})}),{close:()=>{for(let e of u)e.socket.close();l.close()},boundConnIds:e=>c.boundConnIds(e),ctxOf:e=>c.ctxOf(e),routeResolved:(e,t,n)=>c.routeResolved(e,t,n)}}async function G(e,t,n){try{return await e(t)}catch(e){return n.warn(`gateway authenticate failed: ${String(e)}`),!1}}function K(e,t,n,r,a,o){let s=i(),c={elicitation:!1},l={active:!1,frames:[],timer:void 0},u=t=>{if(l.active){l.frames.push(t);return}e.readyState===e.OPEN&&e.send(JSON.stringify(t))},d={connId:s,get caps(){return c},request:async(e,t)=>{throw Error(`tboxcode: server→client request 面直通由 attach 后覆写(answerer 切片消费)`)},notify:async(e,t)=>{u({jsonrpc:`2.0`,method:e,params:t})}};if(r.attachConnection(s,u,d),n?.onConnection!==void 0)try{n.onConnection(d)}catch(e){a.warn(`gateway hook onConnection failed (fail-open): ${String(e)}`)}let f=new TransformStream,p=f.writable.getWriter(),m={writable:new WritableStream({write:e=>{q(e,g,r,u)}}),readable:f.readable},h=t.attachConnection(m,{beforePrompt:e=>r.setTurnOrigin(e,s),afterPrompt:e=>r.clearTurnOrigin(e)}),g={connId:s,socket:e,conn:h,ctx:d,pending:new Map,get caps(){return c},gate:l};d.request=(e,t)=>h.client.request(e,t),o.add(g),e.on(`message`,e=>{let i=typeof e==`string`?e:Buffer.isBuffer(e)?e.toString(`utf8`):void 0;if(i===void 0)return;let o;try{o=JSON.parse(i)}catch{u({jsonrpc:`2.0`,id:null,error:{code:-32700,message:`Parse error`}});return}J(o,g,p,t,n,r,a)}),e.on(`close`,()=>{if(o.delete(g),r.detachConnection(s),n?.onConnectionClose!==void 0)try{n.onConnectionClose(s)}catch(e){a.warn(`gateway hook onConnectionClose failed (fail-open): ${String(e)}`)}p.close().catch(()=>{}),a.debug(`gateway connection detached (${s})`)}),e.on(`error`,e=>{a.warn(`gateway socket error (${s}): ${String(e)}`)})}function q(e,t,n,r){if(e.method===void 0&&e.id!==void 0){let r=t.pending.get(String(e.id));if(r!==void 0){t.pending.delete(String(e.id));let i=r.method===`session/load`,a=e.error===void 0;if(a){let i=e.result?.sessionId??r.params?.sessionId;r.method===`session/close`?i!==void 0&&n.unbind(t.connId,i):i!==void 0&&n.bind(t.connId,i)}else if(r.prebound){let e=r.params?.sessionId;e!==void 0&&n.unbind(t.connId,e)}if(i){let n=H(t,a);if(U(t,e),a)for(let e of n)U(t,e);return}}}r(e)}async function J(e,t,n,r,i,a,o){let s=e.method;if(s===void 0){await n.write(e);return}if(s===`initialize`){let n=e.params?.clientCapabilities;t.caps.elicitation=n?.elicitation!=null}if(s.startsWith(`_tbox.cn/`)){if(e.id===void 0){let n=i?.notifications?.[s];if(n===void 0)return;try{await n(e.params,t.ctx)}catch(e){o.warn(`gateway notification ${s} failed (fail-open): ${String(e)}`)}return}let n=i?.methods?.[s];if(n===void 0){t.socket.send(JSON.stringify({jsonrpc:`2.0`,id:e.id,error:{code:-32601,message:`Method not found: ${s}`}}));return}try{let r=await n(e.params,t.ctx);t.socket.send(JSON.stringify({jsonrpc:`2.0`,id:e.id,result:r??{}}))}catch(n){t.socket.send(JSON.stringify({jsonrpc:`2.0`,id:e.id,error:{code:-32603,message:n instanceof Error?n.message:String(n)}}))}return}if(i?.transformInbound!==void 0&&e.id!==void 0){let n;try{n=await i.transformInbound(s,e.params,t.ctx)}catch(t){o.warn(`gateway hook transformInbound failed (fail-open): ${String(t)}`),n=e.params}if(n===null){t.socket.send(JSON.stringify({jsonrpc:`2.0`,id:e.id,error:{code:-32602,message:`Invalid params: ${s} rejected by gateway`}}));return}e.params=n}if(s===`session/load`&&e.id!==void 0){let n=e.params?.sessionId,s=e.params?.cwd;if(n!==void 0&&s!==void 0&&r.hasSession(n)){a.bind(t.connId,n),V(t,o);try{let a=await r.joinSession({sessionId:n,cwd:s});U(t,{jsonrpc:`2.0`,id:e.id,result:{configOptions:a.configOptions}});for(let e of a.updates){let r=e;if(i?.transformOutbound!==void 0){let n;try{n=i.transformOutbound(e,t.ctx)}catch(t){o.warn(`gateway hook transformOutbound failed (fail-open): ${String(t)}`),n=e}if(n===null)continue;r=n}U(t,{jsonrpc:`2.0`,method:`session/update`,params:{sessionId:n,update:r}})}U(t,{jsonrpc:`2.0`,method:`_tbox.cn/acp_replay_complete`,params:{sessionId:n,notificationCount:a.updates.length}});for(let e of H(t,!0))U(t,e)}catch(r){H(t,!1),a.unbind(t.connId,n),U(t,{jsonrpc:`2.0`,id:e.id,error:{code:-32603,message:r instanceof Error?r.message:String(r)}})}return}}if((s===`session/new`||s===`session/load`||s===`session/resume`||s===`session/close`)&&e.id!==void 0){let n=!1;if(s===`session/load`){let r=e.params?.sessionId;r!==void 0&&!a.boundConnIds(r).includes(t.connId)&&(a.bind(t.connId,r),n=!0),V(t,o),a.beginReplayCount(t.connId)}if(t.pending.set(String(e.id),{method:s,params:e.params,prebound:n}),s===`session/close`){let n=e.params?.sessionId;n!==void 0&&a.unbind(t.connId,n)}}if(await n.write(e),s===`session/close`&&e.id===void 0){let n=e.params?.sessionId;n!==void 0&&a.unbind(t.connId,n)}}async function Y(e,t,n,r){try{return await n()}catch(n){return e.warn(`gateway hook ${t} failed (fail-open): ${String(n)}`),r}}const X=`@tbox.cn/tboxcode-dsh-sdk`;export{z as ABSTAIN_RPC_ERROR,y as CarrierRpcError,b as CarrierTransportError,x as DshCarrier,L as FORM_BUDGET_MS,T as FanoutRouter,w as FollowPool,I as GRACE_WINDOW_MS,B as InteractionRegistry,F as InterruptionMonitor,X as PACKAGE_NAME,R as PERMISSION_MEMBER_TIMEOUT_MS,N as RpcSessionDeps,d as Supervisor,s as SupervisorDeploymentError,W as attachAcpGateway,C as decodeWireEvent,l as exchangeCredentialsViaConnect,p as isRecord,P as openCommandsChangeEventStream,v as parseWireEventFrame,g as parseWireFollowFrame,h as parseWireHistoryRecord,_ as parseWirePage,m as parseWireSessionEvent,Y as runHookOpen};
5
+ `),n.destroy();return}l.handleUpgrade(e,n,o,e=>{Y(e,r,i,c,s,u)})})().catch(e=>{s.warn(`gateway upgrade failed: ${String(e)}`),n.destroy()})};return e.on(`upgrade`,d),{close:()=>{e.off(`upgrade`,d);for(let e of u)e.socket.close();l.close()},boundConnIds:e=>c.boundConnIds(e),ctxOf:e=>c.ctxOf(e),routeResolved:(e,t,n)=>c.routeResolved(e,t,n)}}async function J(e,t,n){try{return await e(t)}catch(e){return n.warn(`gateway authenticate failed: ${String(e)}`),!1}}function Y(e,t,n,r,a,o){let s=i(),c={elicitation:!1},l={active:!1,frames:[],timer:void 0},u=t=>{if(l.active){l.frames.push(t);return}e.readyState===e.OPEN&&e.send(JSON.stringify(t))},d={connId:s,get caps(){return c},request:async(e,t)=>{throw Error(`tboxcode: server→client request 面直通由 attach 后覆写(answerer 切片消费)`)},notify:async(e,t)=>{u({jsonrpc:`2.0`,method:e,params:t})}};if(r.attachConnection(s,u,d),n?.onConnection!==void 0)try{n.onConnection(d)}catch(e){a.warn(`gateway hook onConnection failed (fail-open): ${String(e)}`)}let f=new TransformStream,p=f.writable.getWriter(),m={writable:new WritableStream({write:e=>{X(e,g,r,u)}}),readable:f.readable},h=t.attachConnection(m,{beforePrompt:e=>r.setTurnOrigin(e,s),afterPrompt:e=>r.clearTurnOrigin(e)}),g={connId:s,socket:e,conn:h,ctx:d,pending:new Map,get caps(){return c},gate:l};d.request=(e,t)=>h.client.request(e,t),o.add(g),e.on(`message`,e=>{let i=typeof e==`string`?e:Buffer.isBuffer(e)?e.toString(`utf8`):void 0;if(i===void 0)return;let o;try{o=JSON.parse(i)}catch{u({jsonrpc:`2.0`,id:null,error:{code:-32700,message:`Parse error`}});return}Z(o,g,p,t,n,r,a)}),e.on(`close`,()=>{if(o.delete(g),r.detachConnection(s),n?.onConnectionClose!==void 0)try{n.onConnectionClose(s)}catch(e){a.warn(`gateway hook onConnectionClose failed (fail-open): ${String(e)}`)}p.close().catch(()=>{}),a.debug(`gateway connection detached (${s})`)}),e.on(`error`,e=>{a.warn(`gateway socket error (${s}): ${String(e)}`)})}function X(e,t,n,r){if(e.method===void 0&&e.id!==void 0){let r=t.pending.get(String(e.id));if(r!==void 0){t.pending.delete(String(e.id));let i=r.method===`session/load`,a=e.error===void 0;if(a){let i=e.result?.sessionId??r.params?.sessionId;r.method===`session/close`?i!==void 0&&n.unbind(t.connId,i):i!==void 0&&n.bind(t.connId,i)}else if(r.prebound){let e=r.params?.sessionId;e!==void 0&&n.unbind(t.connId,e)}if(i){let n=G(t,a);if(K(t,e),a)for(let e of n)K(t,e);return}}}r(e)}async function Z(e,t,n,r,i,a,o){let s=e.method;if(s===void 0){await n.write(e);return}if(s===`initialize`){let n=e.params?.clientCapabilities;t.caps.elicitation=n?.elicitation!=null}if(s.startsWith(`_tbox.cn/`)){if(e.id===void 0){let n=i?.notifications?.[s];if(n===void 0)return;try{await n(e.params,t.ctx)}catch(e){o.warn(`gateway notification ${s} failed (fail-open): ${String(e)}`)}return}let n=i?.methods?.[s];if(n===void 0){t.socket.send(JSON.stringify({jsonrpc:`2.0`,id:e.id,error:{code:-32601,message:`Method not found: ${s}`}}));return}try{let r=await n(e.params,t.ctx);t.socket.send(JSON.stringify({jsonrpc:`2.0`,id:e.id,result:r??{}}))}catch(n){t.socket.send(JSON.stringify({jsonrpc:`2.0`,id:e.id,error:{code:-32603,message:n instanceof Error?n.message:String(n)}}))}return}if(i?.transformInbound!==void 0&&e.id!==void 0){let n;try{n=await i.transformInbound(s,e.params,t.ctx)}catch(t){o.warn(`gateway hook transformInbound failed (fail-open): ${String(t)}`),n=e.params}if(n===null){t.socket.send(JSON.stringify({jsonrpc:`2.0`,id:e.id,error:{code:-32602,message:`Invalid params: ${s} rejected by gateway`}}));return}e.params=n}if(s===`session/load`&&e.id!==void 0){let n=e.params?.sessionId,s=e.params?.cwd;if(n!==void 0&&s!==void 0&&r.hasSession(n)){a.bind(t.connId,n),W(t,o);try{let a=await r.joinSession({sessionId:n,cwd:s});K(t,{jsonrpc:`2.0`,id:e.id,result:{configOptions:a.configOptions}});for(let e of a.updates){let r=e;if(i?.transformOutbound!==void 0){let n;try{n=i.transformOutbound(e,t.ctx)}catch(t){o.warn(`gateway hook transformOutbound failed (fail-open): ${String(t)}`),n=e}if(n===null)continue;r=n}K(t,{jsonrpc:`2.0`,method:`session/update`,params:{sessionId:n,update:r}})}K(t,{jsonrpc:`2.0`,method:`_tbox.cn/acp_replay_complete`,params:{sessionId:n,notificationCount:a.updates.length}});for(let e of G(t,!0))K(t,e)}catch(r){G(t,!1),a.unbind(t.connId,n),K(t,{jsonrpc:`2.0`,id:e.id,error:{code:-32603,message:r instanceof Error?r.message:String(r)}})}return}}if((s===`session/new`||s===`session/load`||s===`session/resume`||s===`session/close`)&&e.id!==void 0){let n=!1;if(s===`session/load`){let r=e.params?.sessionId;r!==void 0&&!a.boundConnIds(r).includes(t.connId)&&(a.bind(t.connId,r),n=!0),W(t,o),a.beginReplayCount(t.connId)}if(t.pending.set(String(e.id),{method:s,params:e.params,prebound:n}),s===`session/close`){let n=e.params?.sessionId;n!==void 0&&a.unbind(t.connId,n)}}if(await n.write(e),s===`session/close`&&e.id===void 0){let n=e.params?.sessionId;n!==void 0&&a.unbind(t.connId,n)}}async function Q(e,t,n,r){try{return await n()}catch(n){return e.warn(`gateway hook ${t} failed (fail-open): ${String(n)}`),r}}const $=`@tbox.cn/tboxcode-dsh-sdk`;export{H as ABSTAIN_RPC_ERROR,y as CarrierRpcError,b as CarrierTransportError,x as DshCarrier,B as FORM_BUDGET_MS,T as FanoutRouter,w as FollowPool,z as GRACE_WINDOW_MS,U as InteractionRegistry,R as InterruptionMonitor,$ as PACKAGE_NAME,V as PERMISSION_MEMBER_TIMEOUT_MS,I as RpcSessionDeps,d as Supervisor,s as SupervisorDeploymentError,q as attachAcpGateway,C as decodeWireEvent,l as exchangeCredentialsViaConnect,p as isRecord,L as openCommandsChangeEventStream,v as parseWireEventFrame,g as parseWireFollowFrame,h as parseWireHistoryRecord,_ as parseWirePage,m as parseWireSessionEvent,Q as runHookOpen};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tbox.cn/tboxcode-dsh-sdk",
3
- "version": "0.1.0-rc.13",
3
+ "version": "0.1.0-rc.15",
4
4
  "type": "module",
5
5
  "description": "AcpGateway 扩展点 + Supervisor + 直连 RPC 面(主入口薄 carrier,运行时零 dsh 包依赖;/embedded 子路径提供 in-process ACP core、bridge 投影与 model schema,并携带 dsh peer 闭包)+ 上链 ACP server 面 + wire 契约与 decodeWireEvent;agtcodingbox 唯一依赖面",
6
6
  "main": "lib/index.js",
@@ -18,25 +18,28 @@
18
18
  "files": [
19
19
  "lib"
20
20
  ],
21
+ "engines": {
22
+ "node": ">=24.9.0"
23
+ },
21
24
  "dependencies": {
22
- "@deepseek-ai/cordis": "4.0.5",
23
- "@deepseek-ai/dsh-agent": "0.2.1-rc.2",
24
- "@deepseek-ai/dsh-agent-default-model": "0.2.1-rc.2",
25
- "@deepseek-ai/dsh-attachment": "0.2.1-rc.2",
26
- "@deepseek-ai/dsh-commands": "0.2.1-rc.2",
27
- "@deepseek-ai/dsh-llm": "0.2.1-rc.2",
28
- "@deepseek-ai/dsh-mcp-client": "0.2.1-rc.2",
29
- "@deepseek-ai/dsh-sandbox-policy": "0.2.1-rc.2",
30
- "@deepseek-ai/dsh-session": "0.2.1-rc.2",
31
- "@deepseek-ai/dsh-session-persistence": "0.2.1-rc.2",
32
- "@deepseek-ai/dsh-skill": "0.2.1-rc.2",
33
- "@deepseek-ai/dsh-typert-registry": "0.2.1-rc.2",
34
- "@deepseek-ai/dsh-user-approval": "0.2.1-rc.2",
35
- "@deepseek-ai/dsh-user-questions": "0.2.1-rc.2",
25
+ "@deepseek-ai/cordis": "4.1.0",
26
+ "@deepseek-ai/dsh-agent": "0.2.2-rc.1",
27
+ "@deepseek-ai/dsh-agent-default-model": "0.2.2-rc.1",
28
+ "@deepseek-ai/dsh-attachment": "0.2.2-rc.1",
29
+ "@deepseek-ai/dsh-commands": "0.2.2-rc.1",
30
+ "@deepseek-ai/dsh-llm": "0.2.2-rc.1",
31
+ "@deepseek-ai/dsh-mcp-client": "0.2.2-rc.1",
32
+ "@deepseek-ai/dsh-sandbox-policy": "0.2.2-rc.1",
33
+ "@deepseek-ai/dsh-session": "0.2.2-rc.1",
34
+ "@deepseek-ai/dsh-session-persistence": "0.2.2-rc.1",
35
+ "@deepseek-ai/dsh-skill": "0.2.2-rc.1",
36
+ "@deepseek-ai/dsh-typert-registry": "0.2.2-rc.1",
37
+ "@deepseek-ai/dsh-user-approval": "0.2.2-rc.1",
38
+ "@deepseek-ai/dsh-user-questions": "0.2.2-rc.1",
36
39
  "ws": "^8.19.0",
37
- "@tbox.cn/dsh-acp-bridge-core": "0.1.0-rc.13",
38
- "@tbox.cn/tboxcode-dsh-llm": "0.1.0-rc.13",
39
- "@tbox.cn/dsh-acp-full": "0.1.0-rc.13"
40
+ "@tbox.cn/dsh-acp-bridge-core": "0.1.0-rc.15",
41
+ "@tbox.cn/tboxcode-dsh-llm": "0.1.0-rc.15",
42
+ "@tbox.cn/dsh-acp-full": "0.1.0-rc.15"
40
43
  },
41
44
  "devDependencies": {
42
45
  "@types/ws": "^8.18.1",