@openchambery/web 1.19.7-beta.6 → 1.19.7-beta.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/dist/assets/{AgentAvatar-ziUs4sw3.js → AgentAvatar-Di8F4QJh.js} +1 -1
  2. package/dist/assets/{AssistantView-CCy18vfR.js → AssistantView-AWe-3bXR.js} +1 -1
  3. package/dist/assets/{ChatContainer-DJe_JLcz.js → ChatContainer-XHQSSICA.js} +35 -35
  4. package/dist/assets/{ContextPanel-Zu_D3BhW.js → ContextPanel-BT6bgBJ_.js} +3 -3
  5. package/dist/assets/{DiagramView-DXBSE1Qg.js → DiagramView-DNLNZOT6.js} +1 -1
  6. package/dist/assets/{FadeInOnReveal-BAlsRFJH.js → FadeInOnReveal-CV2s6YN8.js} +1 -1
  7. package/dist/assets/{MarkdownRendererImpl-CznjvgQG.js → MarkdownRendererImpl-C6E0KL8H.js} +1 -1
  8. package/dist/assets/MarkstreamRendererImpl-BKsc-avF.js +1 -0
  9. package/dist/assets/{MobileDetailNavigation-Cd1aYfhC.js → MobileDetailNavigation-Sg0VMlOz.js} +1 -1
  10. package/dist/assets/{MobileShareBridge-BSAjAbV2.js → MobileShareBridge-QTUpK3s7.js} +2 -2
  11. package/dist/assets/{MobileSurface-Dzgv8_-B.js → MobileSurface-BasV1X13.js} +1 -1
  12. package/dist/assets/{MultiRunWindow-DUNzqNmB.js → MultiRunWindow-BxyF4Xy4.js} +1 -1
  13. package/dist/assets/{OnboardingScreen-CJYyR54B.js → OnboardingScreen-D6rtE7rj.js} +1 -1
  14. package/dist/assets/{OverlayScrollbar-C-jZ8ITG.js → OverlayScrollbar-DlNZho3q.js} +1 -1
  15. package/dist/assets/{SettingsView-Ncm_fo9e.js → SettingsView-BywiwJ_a.js} +1 -1
  16. package/dist/assets/{SettingsWindow-h0RnxmRw.js → SettingsWindow-zQ1dc-wl.js} +1 -1
  17. package/dist/assets/{TerminalView-CxNW-E1G.js → TerminalView-BMXYfo8d.js} +1 -1
  18. package/dist/assets/{ToolOutputDialog-BxhqfMo9.js → ToolOutputDialog-CFFkJzQ4.js} +1 -1
  19. package/dist/assets/{appThemeRegistry-B-rV3gVd.js → appThemeRegistry-4oEk9SR8.js} +2 -2
  20. package/dist/assets/{clipboard-Bz0Zm9VJ.js → clipboard-D35XSwID.js} +1 -1
  21. package/dist/assets/{desktopRelayRestore-LBt0CBOj.js → desktopRelayRestore-8pvI_dSc.js} +1 -1
  22. package/dist/assets/{dialog-HCE4eLUW.js → dialog-x2hirWBd.js} +1 -1
  23. package/dist/assets/{es-D9KcV5TX.js → es-C8Sqjsia.js} +1 -1
  24. package/dist/assets/{fileQueries-Dnn15WsA.js → fileQueries-DGHZqZHc.js} +1 -1
  25. package/dist/assets/{fr-DPA9qUl3.js → fr-p-Zwm_j2.js} +1 -1
  26. package/dist/assets/{gitApi-BQk5cH8O.js → gitApi-Dqg8EDsg.js} +1 -1
  27. package/dist/assets/{insertionBoundaries-7UaZ97RD.js → insertionBoundaries-BtiZxiKR.js} +1 -1
  28. package/dist/assets/{ja-Dsif3NoD.js → ja-B6REu1qP.js} +1 -1
  29. package/dist/assets/{ko-Dixx4Ctg.js → ko-CJBNH9IZ.js} +1 -1
  30. package/dist/assets/{main-D4zLYOaH.js → main-Blsi9j8P.js} +2 -2
  31. package/dist/assets/{main-D2whc6Yv.js → main-D4qq97ip.js} +3 -3
  32. package/dist/assets/{markdownTheme-TdhQOZSN.js → markdownTheme-SPe4jOwX.js} +1 -1
  33. package/dist/assets/{miniChat-C6qw4n5u.js → miniChat-QgI11lL4.js} +2 -2
  34. package/dist/assets/{mobile-D2-A0hzp.js → mobile-BjMMtvjD.js} +2 -2
  35. package/dist/assets/{multirun-BLs0gcI6.js → multirun-C9CKfXyS.js} +1 -1
  36. package/dist/assets/{persistence-CmjKImz2.js → persistence-w-AiX2i4.js} +1 -1
  37. package/dist/assets/{pl-Bj78ZGgf.js → pl-B2cBI_7K.js} +1 -1
  38. package/dist/assets/{platform-skqcMARt.js → platform-4eCmsdol.js} +1 -1
  39. package/dist/assets/{projectMeta-BAfqxsS4.js → projectMeta-DzGPV_uF.js} +1 -1
  40. package/dist/assets/{pt-BR-CFL3xSAf.js → pt-BR-CrCc9hmt.js} +1 -1
  41. package/dist/assets/{radio-Yoba9qwf.js → radio-TX0aocEX.js} +1 -1
  42. package/dist/assets/{renderElectronMiniChatApp-DI61MYvq.js → renderElectronMiniChatApp-DZOBP2No.js} +2 -2
  43. package/dist/assets/{renderMobileApp-DsKjY4c_.js → renderMobileApp-DP1_xIqJ.js} +10 -7
  44. package/dist/assets/{runtimeConfig-_bkBy5Wd.js → runtimeConfig-ClsErHd9.js} +1 -1
  45. package/dist/assets/{runtimeEndpointReset-CpAYissz.js → runtimeEndpointReset-DoLMDfAs.js} +1 -1
  46. package/dist/assets/{shikiHighlight-8q791YEM.js → shikiHighlight-B25fE64n.js} +1 -1
  47. package/dist/assets/{sortable-tabs-strip-Bj8N0VWf.js → sortable-tabs-strip-CNDgyX0B.js} +1 -1
  48. package/dist/assets/{store-DzpbXWX1.js → store-CL3WP48T.js} +2 -2
  49. package/dist/assets/{terminalApi-CNqqwjfG.js → terminalApi-CGtOtc8I.js} +1 -1
  50. package/dist/assets/{uk-BY60Ub3Z.js → uk-C8YKAz0x.js} +1 -1
  51. package/dist/assets/{useAppFontEffects-BkdcivZb.js → useAppFontEffects-D1u63a6o.js} +2 -2
  52. package/dist/assets/{useAssistantUIStore-DaplPyb0.js → useAssistantUIStore-I0VWrB5V.js} +1 -1
  53. package/dist/assets/{useDesktopWindowControlsLayout-CzAHnjTa.js → useDesktopWindowControlsLayout-DW_ouBDs.js} +1 -1
  54. package/dist/assets/{useEffectiveDirectory-NardZbAS.js → useEffectiveDirectory-D976zCCc.js} +1 -1
  55. package/dist/assets/{useFontPreferences-D8emhcNv.js → useFontPreferences-BLZ3y6mN.js} +1 -1
  56. package/dist/assets/{useMobileNavigationStore-lqmpSW1v.js → useMobileNavigationStore-lcj5GHR3.js} +4 -4
  57. package/dist/assets/{useSessionAutoCleanup-DUTte2bI.js → useSessionAutoCleanup-DMNa_krc.js} +1 -1
  58. package/dist/assets/useUIStore-CNLmCoPU.js +1 -0
  59. package/dist/assets/{useWorkerHighlightedLines-cPHAAzjo.js → useWorkerHighlightedLines-BQNOf2U4.js} +1 -1
  60. package/dist/assets/{zh-CN-Cox1LaOW.js → zh-CN-CCYwUnmB.js} +1 -1
  61. package/dist/assets/{zh-TW-B6RU0mxa.js → zh-TW-D8Y6py08.js} +1 -1
  62. package/dist/index.html +7 -7
  63. package/dist/mini-chat.html +7 -7
  64. package/dist/mobile.html +7 -7
  65. package/package.json +1 -1
  66. package/server/lib/notifications/APNS.md +4 -4
  67. package/server/lib/notifications/DOCUMENTATION.md +1 -1
  68. package/server/lib/notifications/apns-runtime.js +139 -20
  69. package/server/lib/notifications/apns-runtime.test.js +66 -7
  70. package/server/lib/notifications/apns-titles.js +62 -62
  71. package/server/lib/notifications/apns-titles.test.js +3 -1
  72. package/server/lib/notifications/routes.js +1 -1
  73. package/server/lib/notifications/runtime.js +25 -49
  74. package/server/lib/opencode/settings-runtime.js +16 -0
  75. package/dist/assets/MarkstreamRendererImpl-DorvdTK9.js +0 -1
  76. package/dist/assets/useUIStore-B7KJS5G7.js +0 -1
@@ -1 +1 @@
1
- import{f as e,x as t}from"./runtime-switch-BkpTTbEi.js";import{It as n}from"./persistence-CmjKImz2.js";import{St as r}from"./platform-skqcMARt.js";var i=1,a=1,o=0,s=`/api/terminal/ws`,c=1200,l=250,u=2e4,d=5e3,f=`__openchamberTerminalTransportState`,p=new TextEncoder,m=new TextDecoder,h=t=>e().websocket(t),g=e=>{let t=p.encode(JSON.stringify(e)),n=new Uint8Array(t.length+1);return n[0]=i,n.set(t,1),n},_=e=>e?((e.transports??[]).includes(`ws`)||e.preferred===`ws`)&&typeof e.ws?.path==`string`&&e.ws.path.length>0:!1,v=e=>e.streamCapability?.ws?.path??e.inputCapability?.ws?.path??s,y=e=>{switch(e){case`SESSION_NOT_FOUND`:return Error(`Terminal session not found`);case`NOT_BOUND`:return Error(`Terminal session is not bound`);case`WRITE_FAIL`:return Error(`Failed to write to terminal`);case`RATE_LIMIT`:return Error(`Terminal websocket is rate limited`);case`BAD_FRAME`:return Error(`Terminal websocket protocol violation`);default:return Error(`Terminal websocket error`)}},b=class{socket=null;socketUrl=``;boundSessionId=null;requestedSessionId=null;openPromise=null;reconnectTimeout=null;keepaliveInterval=null;closed=!1;subscriptions=new Map;activeSubscriptionToken=null;replayCursorBySession=new Map;configure(e){if(e){if(this.socketUrl===e){this.closed=!1,this.isConnectedOrConnecting()||this.ensureConnected();return}this.socketUrl=e,this.closed=!1,this.resetConnection(),this.ensureConnected()}}subscribe(e,t,n,r){let i=Symbol(e),o={token:i,sessionId:e,onEvent:t,onError:n,maxRetries:r?.maxRetries??3,initialRetryDelay:r?.initialRetryDelay??1e3,maxRetryDelay:r?.maxRetryDelay??8e3,connectionTimeout:r?.connectionTimeout??1e4,retryCount:0,connected:!1,connectionTimeoutId:null};return this.subscriptions.set(i,o),this.activeSubscriptionToken=i,this.requestedSessionId=e,this.ensureConnected(),this.boundSessionId===e&&this.socket&&this.socket.readyState===a?(o.retryCount=0,o.connected=!0,o.onEvent({type:`connected`})):(this.boundSessionId=null,this.startConnectionTimeout(o),this.bindActiveSession()),()=>{if(this.clearConnectionTimeout(o),this.subscriptions.delete(i),this.activeSubscriptionToken===i&&(this.activeSubscriptionToken=null),this.hasSubscriptionForSession(e)||(this.boundSessionId===e&&(this.boundSessionId=null),this.requestedSessionId===e&&(this.requestedSessionId=null)),this.subscriptions.size===0){this.clearReconnectTimeout(),this.resetConnection();return}this.promoteActiveSubscription()}}async sendInput(e,t){if(!e||!t||this.closed||!this.socketUrl)return!1;let n=await this.getOpenSocket(c);if(!n||n.readyState!==a)return!1;try{return this.boundSessionId!==e&&(this.requestedSessionId=e,n.send(g({t:`b`,s:e,r:this.replayCursorBySession.get(e)??0,v:2}))),n.send(t),!0}catch{return this.handleSocketFailure(Error(`Terminal websocket send failed`)),!1}}unbindSession(e){e&&(this.boundSessionId===e&&(this.boundSessionId=null),this.requestedSessionId===e&&(this.requestedSessionId=null))}close(){this.closed=!0,this.clearReconnectTimeout();for(let e of this.subscriptions.values())this.clearConnectionTimeout(e);this.resetConnection(),this.socketUrl=``,this.subscriptions.clear(),this.activeSubscriptionToken=null,this.replayCursorBySession.clear()}prime(){this.closed||!this.socketUrl||this.isConnectedOrConnecting()||this.ensureConnected()}isConnectedOrConnecting(e){return this.closed||e&&this.socketUrl!==e?!1:this.socket&&(this.socket.readyState===a||this.socket.readyState===o)?!0:this.openPromise!==null}getActiveSubscription(){return this.activeSubscriptionToken?this.subscriptions.get(this.activeSubscriptionToken)??null:null}hasSubscriptionForSession(e){for(let t of this.subscriptions.values())if(t.sessionId===e)return!0;return!1}promoteActiveSubscription(){if(this.getActiveSubscription())return;let e=this.subscriptions.values().next().value;if(!e){this.activeSubscriptionToken=null;return}this.activeSubscriptionToken=e.token,this.requestedSessionId=e.sessionId,this.boundSessionId===e.sessionId?e.connected||(e.retryCount=0,e.connected=!0,this.clearConnectionTimeout(e),e.onEvent({type:`connected`})):(this.boundSessionId=null,this.startConnectionTimeout(e),this.bindActiveSession())}emitToSession(e,t){for(let n of this.subscriptions.values())n.sessionId===e&&n.onEvent(t)}markSessionConnected(e,t){for(let n of this.subscriptions.values())n.sessionId===e&&(n.retryCount=0,n.connected=!0,this.clearConnectionTimeout(n),n.onEvent(t))}emitErrorToSession(e,t,n){for(let r of this.subscriptions.values())r.sessionId===e&&(r.connected=!1,n&&this.clearConnectionTimeout(r),r.onError?.(t,n))}startConnectionTimeout(e){this.clearConnectionTimeout(e),e.connectionTimeoutId=setTimeout(()=>{e.connected||!this.subscriptions.has(e.token)||this.getActiveSubscription()?.token===e.token&&this.handleSocketFailure(Error(`Connection timeout`))},e.connectionTimeout)}clearConnectionTimeout(e){e.connectionTimeoutId&&=(clearTimeout(e.connectionTimeoutId),null)}async getOpenSocket(e){if(this.socket&&this.socket.readyState===a||(this.ensureConnected(),this.socket&&this.socket.readyState===a))return this.socket;let t=await Promise.race([this.openPromise??Promise.resolve(null),new Promise(t=>{setTimeout(()=>t(null),e)})]);return t&&t.readyState===a?t:this.socket&&this.socket.readyState===a?this.socket:null}ensureConnected(){this.closed||!this.socketUrl||this.socket&&(this.socket.readyState===a||this.socket.readyState===o)||(this.openPromise||=(this.clearReconnectTimeout(),(async()=>{try{await t()}catch{}return new Promise(e=>{let t=!1,n=null,i=r=>{t||(t=!0,n&&=(clearTimeout(n),null),this.openPromise=null,e(r))};try{let e=r(h(this.socketUrl));e.binaryType=`arraybuffer`,e.onopen=()=>{this.socket=e,this.startKeepalive(),i(e)},e.onmessage=e=>{this.handleSocketMessage(e.data)},e.onerror=()=>{!this.closed&&!this.getActiveSubscription()&&this.scheduleReconnect(Error(`Terminal websocket error`))},e.onclose=()=>{this.socket===e&&(this.socket=null,this.boundSessionId=null,this.stopKeepalive(),this.closed||this.scheduleReconnect(Error(`Terminal stream connection error`))),i(null)},this.socket=e,n=setTimeout(()=>{e.readyState===o&&(e.close(),i(null))},d)}catch{i(null),this.closed||this.scheduleReconnect(Error(`Terminal websocket open failed`))}})})()))}bindActiveSession(){let e=this.getActiveSubscription();if(!(!e||!this.socket||this.socket.readyState!==a)){this.requestedSessionId=e.sessionId;try{this.socket.send(g({t:`b`,s:e.sessionId,r:this.replayCursorBySession.get(e.sessionId)??0,v:2}))}catch{this.handleSocketFailure(Error(`Terminal websocket bind failed`))}}}scheduleReconnect(e){if(this.closed||!this.socketUrl||this.reconnectTimeout)return;this.promoteActiveSubscription();let t=this.getActiveSubscription();if(!t)return;let n=t.retryCount+1,r=t.initialRetryDelay,i=t.maxRetryDelay,a=t.maxRetries;if(n>a){this.clearConnectionTimeout(t),this.emitErrorToSession(t.sessionId,e,!0);return}t.retryCount=n;for(let e of this.subscriptions.values())e.sessionId===t.sessionId&&(e.connected=!1,e.retryCount=n,e.onEvent({type:`reconnecting`,attempt:n,maxAttempts:a}));this.startConnectionTimeout(t);let o=Math.min(r*2**Math.max(n-1,0),i)+Math.floor(Math.random()*l);this.reconnectTimeout=setTimeout(()=>{this.reconnectTimeout=null,this.ensureConnected(),this.bindActiveSession()},o)}clearReconnectTimeout(){this.reconnectTimeout&&=(clearTimeout(this.reconnectTimeout),null)}sendControl(e){if(!this.socket||this.socket.readyState!==a)return!1;try{return this.socket.send(g(e)),!0}catch{return this.handleSocketFailure(Error(`Terminal websocket control send failed`)),!1}}startKeepalive(){this.stopKeepalive(),this.keepaliveInterval=setInterval(()=>{this.closed||this.sendControl({t:`p`,v:2})},u)}stopKeepalive(){this.keepaliveInterval&&=(clearInterval(this.keepaliveInterval),null)}async handleSocketMessage(e){let t=await this.asUint8Array(e);if(t&&t.length>0&&t[0]===i){this.handleControlMessage(t);return}let n=await this.asText(e);if(!n)return;let r=this.boundSessionId??this.requestedSessionId??this.getActiveSubscription()?.sessionId;r&&this.emitToSession(r,{type:`data`,data:n})}handleControlMessage(e){if(e.length<2)return;let t;try{t=JSON.parse(m.decode(e.subarray(1)))}catch{this.handleSocketFailure(Error(`Terminal websocket control parse failed`));return}let n=this.getActiveSubscription();switch(t.t){case`ok`:this.bindActiveSession();return;case`po`:return;case`d`:{let e=t.s??this.boundSessionId??this.requestedSessionId;if(!e)return;typeof t.i==`number`&&Number.isFinite(t.i)&&this.replayCursorBySession.set(e,Math.max(this.replayCursorBySession.get(e)??0,Math.trunc(t.i))),typeof t.d==`string`&&t.d.length>0&&this.emitToSession(e,{type:`data`,data:t.d});return}case`bok`:{let e=t.s??this.requestedSessionId;if(this.boundSessionId=e,!e)return;this.markSessionConnected(e,{type:`connected`,runtime:t.runtime,ptyBackend:t.ptyBackend});return}case`x`:{let e=t.s??this.boundSessionId??n?.sessionId??null;if(!e){this.boundSessionId=null;return}this.boundSessionId===e&&(this.boundSessionId=null),this.requestedSessionId===e&&(this.requestedSessionId=null),this.replayCursorBySession.delete(e);for(let n of this.subscriptions.values())n.sessionId===e&&(n.connected=!1,this.clearConnectionTimeout(n),n.onEvent({type:`exit`,exitCode:t.exitCode,signal:t.signal??null}));return}case`e`:{let e=y(t.c),r=t.f===!0;(t.c===`NOT_BOUND`||t.c===`SESSION_NOT_FOUND`)&&(this.boundSessionId=null);let i=t.s??this.requestedSessionId??n?.sessionId??null;if(i){if(t.c===`SESSION_NOT_FOUND`){this.emitErrorToSession(i,e,!0);return}this.emitErrorToSession(i,e,r)}else n&&(n.connected=!1,r&&this.clearConnectionTimeout(n),n.onError?.(e,r));t.f===!0&&this.handleSocketFailure(e);return}default:return}}async asUint8Array(e){if(e instanceof ArrayBuffer)return new Uint8Array(e);if(e instanceof Uint8Array)return e;if(typeof Blob<`u`&&e instanceof Blob){let t=await e.arrayBuffer();return new Uint8Array(t)}return null}async asText(e){if(typeof e==`string`)return e;let t=await this.asUint8Array(e);return t?m.decode(t):``}handleSocketFailure(e){this.boundSessionId=null,this.requestedSessionId=null,this.resetConnection(),this.scheduleReconnect(e)}resetConnection(){if(this.openPromise=null,this.stopKeepalive(),this.socket){let e=this.socket;this.socket=null,e.onopen=null,e.onmessage=null,e.onerror=null,e.onclose=null,(e.readyState===a||e.readyState===o)&&e.close()}this.boundSessionId=null}},x=()=>{let e=globalThis;return e[f]||(e[f]={inputCapability:null,streamCapability:null,manager:null}),e[f]},S=()=>{let e=x();return e.manager||=new b,e.manager},C=e=>{let t=x();if(t.inputCapability=e?.input??null,t.streamCapability=e?.stream??null,!_(t.inputCapability)&&!_(t.streamCapability)){t.manager?.close(),t.manager=null;return}let n=h(v(t));n&&S().configure(n)},w=async(e,t)=>{let r=await n(`/api/terminal/${e}/input`,{method:`POST`,headers:{"Content-Type":`text/plain`},body:t});if(!r.ok){let e=await r.json().catch(()=>({error:`Failed to send terminal input`}));throw Error(e.error||`Failed to send terminal input`)}};async function T(e){let t=await n(`/api/terminal/create`,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify({cwd:e.cwd,cols:e.cols??80,rows:e.rows??24})});if(!t.ok){let e=await t.json().catch(()=>({error:`Failed to create terminal`}));throw Error(e.error||`Failed to create terminal session`)}let r=await t.json();return C(r.capabilities),r}var E=(t,n,r,i={})=>{let a=i.maxRetries??3,o=i.initialRetryDelay??1e3,s=i.maxRetryDelay??8e3,c=i.connectionTimeout??1e4,l=null,u=0,d=null,f=null,p=!1,m=!1,h=()=>{d&&=(clearTimeout(d),null),f&&=(clearTimeout(f),null)},g=()=>{p||(p=!0,h(),l&&=(l.close(),null))},_=(e,t)=>{if(!(p||m)){if(u<a&&!t){u+=1;let e=Math.min(o*2**(u-1),s);n({type:`reconnecting`,attempt:u,maxAttempts:a}),d=setTimeout(()=>{!p&&!m&&v()},e);return}r?.(e,!0),g()}},v=()=>{if(p||m||l&&l.readyState!==EventSource.CLOSED)return;l=new EventSource(e().sse(`/api/terminal/${t}/stream`));let i=!1;f=setTimeout(()=>{!i&&l?.readyState!==EventSource.OPEN&&(l?.close(),_(Error(`Connection timeout`),!1))},c),l.onopen=()=>{i||(i=!0,u=0,h(),n({type:`connected`}))},l.onmessage=e=>{try{let r=JSON.parse(e.data);r.type===`exit`&&(x().manager?.unbindSession(t),m=!0,g()),n(r)}catch(e){r?.(e,!1)}},l.onerror=()=>{h();let e=m||l?.readyState===EventSource.CLOSED;l?.close(),l=null,m||_(Error(`Terminal stream connection error`),e)}};return v(),g};function D(e,t,n,r={}){let i=x();if(i.streamCapability&&!_(i.streamCapability))return E(e,t,n,r);let a=S(),o=h(v(i));return o?(a.configure(o),a.subscribe(e,t,n,r)):E(e,t,n,r)}async function O(e,t){let n=x();n.manager&&await n.manager.sendInput(e,t)||await w(e,t)}async function k(e,t,r){let i=await n(`/api/terminal/${e}/resize`,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify({cols:t,rows:r})});if(!i.ok){let e=await i.json().catch(()=>({error:`Failed to resize terminal`}));throw Error(e.error||`Failed to resize terminal`)}}async function A(e){x().manager?.unbindSession(e);let t=await n(`/api/terminal/${e}`,{method:`DELETE`});if(!t.ok){let e=await t.json().catch(()=>({error:`Failed to close terminal`}));throw Error(e.error||`Failed to close terminal`)}}async function j(e,t){x().manager?.unbindSession(e);let r=await n(`/api/terminal/${e}/restart`,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify({cwd:t.cwd,cols:t.cols??80,rows:t.rows??24})});if(!r.ok){let e=await r.json().catch(()=>({error:`Failed to restart terminal`}));throw Error(e.error||`Failed to restart terminal`)}let i=await r.json();return C(i.capabilities),i}async function M(e){let t=await n(`/api/terminal/force-kill`,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify(e)});if(!t.ok){let e=await t.json().catch(()=>({error:`Failed to force kill terminal`}));throw Error(e.error||`Failed to force kill terminal`)}e.sessionId&&x().manager?.unbindSession(e.sessionId)}function N(){let e=x();e.manager?.close(),e.manager=null,e.inputCapability=null,e.streamCapability=null}function P(){let e=x();if(e.inputCapability&&e.streamCapability&&!_(e.inputCapability)&&!_(e.streamCapability))return;let t=h(v(e)||s);if(!t)return;let n=S();n.isConnectedOrConnecting(t)||(n.configure(t),n.prime())}export{M as a,j as c,N as i,O as l,D as n,P as o,T as r,k as s,A as t};
1
+ import{f as e,x as t}from"./runtime-switch-BkpTTbEi.js";import{It as n}from"./persistence-w-AiX2i4.js";import{St as r}from"./platform-4eCmsdol.js";var i=1,a=1,o=0,s=`/api/terminal/ws`,c=1200,l=250,u=2e4,d=5e3,f=`__openchamberTerminalTransportState`,p=new TextEncoder,m=new TextDecoder,h=t=>e().websocket(t),g=e=>{let t=p.encode(JSON.stringify(e)),n=new Uint8Array(t.length+1);return n[0]=i,n.set(t,1),n},_=e=>e?((e.transports??[]).includes(`ws`)||e.preferred===`ws`)&&typeof e.ws?.path==`string`&&e.ws.path.length>0:!1,v=e=>e.streamCapability?.ws?.path??e.inputCapability?.ws?.path??s,y=e=>{switch(e){case`SESSION_NOT_FOUND`:return Error(`Terminal session not found`);case`NOT_BOUND`:return Error(`Terminal session is not bound`);case`WRITE_FAIL`:return Error(`Failed to write to terminal`);case`RATE_LIMIT`:return Error(`Terminal websocket is rate limited`);case`BAD_FRAME`:return Error(`Terminal websocket protocol violation`);default:return Error(`Terminal websocket error`)}},b=class{socket=null;socketUrl=``;boundSessionId=null;requestedSessionId=null;openPromise=null;reconnectTimeout=null;keepaliveInterval=null;closed=!1;subscriptions=new Map;activeSubscriptionToken=null;replayCursorBySession=new Map;configure(e){if(e){if(this.socketUrl===e){this.closed=!1,this.isConnectedOrConnecting()||this.ensureConnected();return}this.socketUrl=e,this.closed=!1,this.resetConnection(),this.ensureConnected()}}subscribe(e,t,n,r){let i=Symbol(e),o={token:i,sessionId:e,onEvent:t,onError:n,maxRetries:r?.maxRetries??3,initialRetryDelay:r?.initialRetryDelay??1e3,maxRetryDelay:r?.maxRetryDelay??8e3,connectionTimeout:r?.connectionTimeout??1e4,retryCount:0,connected:!1,connectionTimeoutId:null};return this.subscriptions.set(i,o),this.activeSubscriptionToken=i,this.requestedSessionId=e,this.ensureConnected(),this.boundSessionId===e&&this.socket&&this.socket.readyState===a?(o.retryCount=0,o.connected=!0,o.onEvent({type:`connected`})):(this.boundSessionId=null,this.startConnectionTimeout(o),this.bindActiveSession()),()=>{if(this.clearConnectionTimeout(o),this.subscriptions.delete(i),this.activeSubscriptionToken===i&&(this.activeSubscriptionToken=null),this.hasSubscriptionForSession(e)||(this.boundSessionId===e&&(this.boundSessionId=null),this.requestedSessionId===e&&(this.requestedSessionId=null)),this.subscriptions.size===0){this.clearReconnectTimeout(),this.resetConnection();return}this.promoteActiveSubscription()}}async sendInput(e,t){if(!e||!t||this.closed||!this.socketUrl)return!1;let n=await this.getOpenSocket(c);if(!n||n.readyState!==a)return!1;try{return this.boundSessionId!==e&&(this.requestedSessionId=e,n.send(g({t:`b`,s:e,r:this.replayCursorBySession.get(e)??0,v:2}))),n.send(t),!0}catch{return this.handleSocketFailure(Error(`Terminal websocket send failed`)),!1}}unbindSession(e){e&&(this.boundSessionId===e&&(this.boundSessionId=null),this.requestedSessionId===e&&(this.requestedSessionId=null))}close(){this.closed=!0,this.clearReconnectTimeout();for(let e of this.subscriptions.values())this.clearConnectionTimeout(e);this.resetConnection(),this.socketUrl=``,this.subscriptions.clear(),this.activeSubscriptionToken=null,this.replayCursorBySession.clear()}prime(){this.closed||!this.socketUrl||this.isConnectedOrConnecting()||this.ensureConnected()}isConnectedOrConnecting(e){return this.closed||e&&this.socketUrl!==e?!1:this.socket&&(this.socket.readyState===a||this.socket.readyState===o)?!0:this.openPromise!==null}getActiveSubscription(){return this.activeSubscriptionToken?this.subscriptions.get(this.activeSubscriptionToken)??null:null}hasSubscriptionForSession(e){for(let t of this.subscriptions.values())if(t.sessionId===e)return!0;return!1}promoteActiveSubscription(){if(this.getActiveSubscription())return;let e=this.subscriptions.values().next().value;if(!e){this.activeSubscriptionToken=null;return}this.activeSubscriptionToken=e.token,this.requestedSessionId=e.sessionId,this.boundSessionId===e.sessionId?e.connected||(e.retryCount=0,e.connected=!0,this.clearConnectionTimeout(e),e.onEvent({type:`connected`})):(this.boundSessionId=null,this.startConnectionTimeout(e),this.bindActiveSession())}emitToSession(e,t){for(let n of this.subscriptions.values())n.sessionId===e&&n.onEvent(t)}markSessionConnected(e,t){for(let n of this.subscriptions.values())n.sessionId===e&&(n.retryCount=0,n.connected=!0,this.clearConnectionTimeout(n),n.onEvent(t))}emitErrorToSession(e,t,n){for(let r of this.subscriptions.values())r.sessionId===e&&(r.connected=!1,n&&this.clearConnectionTimeout(r),r.onError?.(t,n))}startConnectionTimeout(e){this.clearConnectionTimeout(e),e.connectionTimeoutId=setTimeout(()=>{e.connected||!this.subscriptions.has(e.token)||this.getActiveSubscription()?.token===e.token&&this.handleSocketFailure(Error(`Connection timeout`))},e.connectionTimeout)}clearConnectionTimeout(e){e.connectionTimeoutId&&=(clearTimeout(e.connectionTimeoutId),null)}async getOpenSocket(e){if(this.socket&&this.socket.readyState===a||(this.ensureConnected(),this.socket&&this.socket.readyState===a))return this.socket;let t=await Promise.race([this.openPromise??Promise.resolve(null),new Promise(t=>{setTimeout(()=>t(null),e)})]);return t&&t.readyState===a?t:this.socket&&this.socket.readyState===a?this.socket:null}ensureConnected(){this.closed||!this.socketUrl||this.socket&&(this.socket.readyState===a||this.socket.readyState===o)||(this.openPromise||=(this.clearReconnectTimeout(),(async()=>{try{await t()}catch{}return new Promise(e=>{let t=!1,n=null,i=r=>{t||(t=!0,n&&=(clearTimeout(n),null),this.openPromise=null,e(r))};try{let e=r(h(this.socketUrl));e.binaryType=`arraybuffer`,e.onopen=()=>{this.socket=e,this.startKeepalive(),i(e)},e.onmessage=e=>{this.handleSocketMessage(e.data)},e.onerror=()=>{!this.closed&&!this.getActiveSubscription()&&this.scheduleReconnect(Error(`Terminal websocket error`))},e.onclose=()=>{this.socket===e&&(this.socket=null,this.boundSessionId=null,this.stopKeepalive(),this.closed||this.scheduleReconnect(Error(`Terminal stream connection error`))),i(null)},this.socket=e,n=setTimeout(()=>{e.readyState===o&&(e.close(),i(null))},d)}catch{i(null),this.closed||this.scheduleReconnect(Error(`Terminal websocket open failed`))}})})()))}bindActiveSession(){let e=this.getActiveSubscription();if(!(!e||!this.socket||this.socket.readyState!==a)){this.requestedSessionId=e.sessionId;try{this.socket.send(g({t:`b`,s:e.sessionId,r:this.replayCursorBySession.get(e.sessionId)??0,v:2}))}catch{this.handleSocketFailure(Error(`Terminal websocket bind failed`))}}}scheduleReconnect(e){if(this.closed||!this.socketUrl||this.reconnectTimeout)return;this.promoteActiveSubscription();let t=this.getActiveSubscription();if(!t)return;let n=t.retryCount+1,r=t.initialRetryDelay,i=t.maxRetryDelay,a=t.maxRetries;if(n>a){this.clearConnectionTimeout(t),this.emitErrorToSession(t.sessionId,e,!0);return}t.retryCount=n;for(let e of this.subscriptions.values())e.sessionId===t.sessionId&&(e.connected=!1,e.retryCount=n,e.onEvent({type:`reconnecting`,attempt:n,maxAttempts:a}));this.startConnectionTimeout(t);let o=Math.min(r*2**Math.max(n-1,0),i)+Math.floor(Math.random()*l);this.reconnectTimeout=setTimeout(()=>{this.reconnectTimeout=null,this.ensureConnected(),this.bindActiveSession()},o)}clearReconnectTimeout(){this.reconnectTimeout&&=(clearTimeout(this.reconnectTimeout),null)}sendControl(e){if(!this.socket||this.socket.readyState!==a)return!1;try{return this.socket.send(g(e)),!0}catch{return this.handleSocketFailure(Error(`Terminal websocket control send failed`)),!1}}startKeepalive(){this.stopKeepalive(),this.keepaliveInterval=setInterval(()=>{this.closed||this.sendControl({t:`p`,v:2})},u)}stopKeepalive(){this.keepaliveInterval&&=(clearInterval(this.keepaliveInterval),null)}async handleSocketMessage(e){let t=await this.asUint8Array(e);if(t&&t.length>0&&t[0]===i){this.handleControlMessage(t);return}let n=await this.asText(e);if(!n)return;let r=this.boundSessionId??this.requestedSessionId??this.getActiveSubscription()?.sessionId;r&&this.emitToSession(r,{type:`data`,data:n})}handleControlMessage(e){if(e.length<2)return;let t;try{t=JSON.parse(m.decode(e.subarray(1)))}catch{this.handleSocketFailure(Error(`Terminal websocket control parse failed`));return}let n=this.getActiveSubscription();switch(t.t){case`ok`:this.bindActiveSession();return;case`po`:return;case`d`:{let e=t.s??this.boundSessionId??this.requestedSessionId;if(!e)return;typeof t.i==`number`&&Number.isFinite(t.i)&&this.replayCursorBySession.set(e,Math.max(this.replayCursorBySession.get(e)??0,Math.trunc(t.i))),typeof t.d==`string`&&t.d.length>0&&this.emitToSession(e,{type:`data`,data:t.d});return}case`bok`:{let e=t.s??this.requestedSessionId;if(this.boundSessionId=e,!e)return;this.markSessionConnected(e,{type:`connected`,runtime:t.runtime,ptyBackend:t.ptyBackend});return}case`x`:{let e=t.s??this.boundSessionId??n?.sessionId??null;if(!e){this.boundSessionId=null;return}this.boundSessionId===e&&(this.boundSessionId=null),this.requestedSessionId===e&&(this.requestedSessionId=null),this.replayCursorBySession.delete(e);for(let n of this.subscriptions.values())n.sessionId===e&&(n.connected=!1,this.clearConnectionTimeout(n),n.onEvent({type:`exit`,exitCode:t.exitCode,signal:t.signal??null}));return}case`e`:{let e=y(t.c),r=t.f===!0;(t.c===`NOT_BOUND`||t.c===`SESSION_NOT_FOUND`)&&(this.boundSessionId=null);let i=t.s??this.requestedSessionId??n?.sessionId??null;if(i){if(t.c===`SESSION_NOT_FOUND`){this.emitErrorToSession(i,e,!0);return}this.emitErrorToSession(i,e,r)}else n&&(n.connected=!1,r&&this.clearConnectionTimeout(n),n.onError?.(e,r));t.f===!0&&this.handleSocketFailure(e);return}default:return}}async asUint8Array(e){if(e instanceof ArrayBuffer)return new Uint8Array(e);if(e instanceof Uint8Array)return e;if(typeof Blob<`u`&&e instanceof Blob){let t=await e.arrayBuffer();return new Uint8Array(t)}return null}async asText(e){if(typeof e==`string`)return e;let t=await this.asUint8Array(e);return t?m.decode(t):``}handleSocketFailure(e){this.boundSessionId=null,this.requestedSessionId=null,this.resetConnection(),this.scheduleReconnect(e)}resetConnection(){if(this.openPromise=null,this.stopKeepalive(),this.socket){let e=this.socket;this.socket=null,e.onopen=null,e.onmessage=null,e.onerror=null,e.onclose=null,(e.readyState===a||e.readyState===o)&&e.close()}this.boundSessionId=null}},x=()=>{let e=globalThis;return e[f]||(e[f]={inputCapability:null,streamCapability:null,manager:null}),e[f]},S=()=>{let e=x();return e.manager||=new b,e.manager},C=e=>{let t=x();if(t.inputCapability=e?.input??null,t.streamCapability=e?.stream??null,!_(t.inputCapability)&&!_(t.streamCapability)){t.manager?.close(),t.manager=null;return}let n=h(v(t));n&&S().configure(n)},w=async(e,t)=>{let r=await n(`/api/terminal/${e}/input`,{method:`POST`,headers:{"Content-Type":`text/plain`},body:t});if(!r.ok){let e=await r.json().catch(()=>({error:`Failed to send terminal input`}));throw Error(e.error||`Failed to send terminal input`)}};async function T(e){let t=await n(`/api/terminal/create`,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify({cwd:e.cwd,cols:e.cols??80,rows:e.rows??24})});if(!t.ok){let e=await t.json().catch(()=>({error:`Failed to create terminal`}));throw Error(e.error||`Failed to create terminal session`)}let r=await t.json();return C(r.capabilities),r}var E=(t,n,r,i={})=>{let a=i.maxRetries??3,o=i.initialRetryDelay??1e3,s=i.maxRetryDelay??8e3,c=i.connectionTimeout??1e4,l=null,u=0,d=null,f=null,p=!1,m=!1,h=()=>{d&&=(clearTimeout(d),null),f&&=(clearTimeout(f),null)},g=()=>{p||(p=!0,h(),l&&=(l.close(),null))},_=(e,t)=>{if(!(p||m)){if(u<a&&!t){u+=1;let e=Math.min(o*2**(u-1),s);n({type:`reconnecting`,attempt:u,maxAttempts:a}),d=setTimeout(()=>{!p&&!m&&v()},e);return}r?.(e,!0),g()}},v=()=>{if(p||m||l&&l.readyState!==EventSource.CLOSED)return;l=new EventSource(e().sse(`/api/terminal/${t}/stream`));let i=!1;f=setTimeout(()=>{!i&&l?.readyState!==EventSource.OPEN&&(l?.close(),_(Error(`Connection timeout`),!1))},c),l.onopen=()=>{i||(i=!0,u=0,h(),n({type:`connected`}))},l.onmessage=e=>{try{let r=JSON.parse(e.data);r.type===`exit`&&(x().manager?.unbindSession(t),m=!0,g()),n(r)}catch(e){r?.(e,!1)}},l.onerror=()=>{h();let e=m||l?.readyState===EventSource.CLOSED;l?.close(),l=null,m||_(Error(`Terminal stream connection error`),e)}};return v(),g};function D(e,t,n,r={}){let i=x();if(i.streamCapability&&!_(i.streamCapability))return E(e,t,n,r);let a=S(),o=h(v(i));return o?(a.configure(o),a.subscribe(e,t,n,r)):E(e,t,n,r)}async function O(e,t){let n=x();n.manager&&await n.manager.sendInput(e,t)||await w(e,t)}async function k(e,t,r){let i=await n(`/api/terminal/${e}/resize`,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify({cols:t,rows:r})});if(!i.ok){let e=await i.json().catch(()=>({error:`Failed to resize terminal`}));throw Error(e.error||`Failed to resize terminal`)}}async function A(e){x().manager?.unbindSession(e);let t=await n(`/api/terminal/${e}`,{method:`DELETE`});if(!t.ok){let e=await t.json().catch(()=>({error:`Failed to close terminal`}));throw Error(e.error||`Failed to close terminal`)}}async function j(e,t){x().manager?.unbindSession(e);let r=await n(`/api/terminal/${e}/restart`,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify({cwd:t.cwd,cols:t.cols??80,rows:t.rows??24})});if(!r.ok){let e=await r.json().catch(()=>({error:`Failed to restart terminal`}));throw Error(e.error||`Failed to restart terminal`)}let i=await r.json();return C(i.capabilities),i}async function M(e){let t=await n(`/api/terminal/force-kill`,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify(e)});if(!t.ok){let e=await t.json().catch(()=>({error:`Failed to force kill terminal`}));throw Error(e.error||`Failed to force kill terminal`)}e.sessionId&&x().manager?.unbindSession(e.sessionId)}function N(){let e=x();e.manager?.close(),e.manager=null,e.inputCapability=null,e.streamCapability=null}function P(){let e=x();if(e.inputCapability&&e.streamCapability&&!_(e.inputCapability)&&!_(e.streamCapability))return;let t=h(v(e)||s);if(!t)return;let n=S();n.isConnectedOrConnecting(t)||(n.configure(t),n.prime())}export{M as a,j as c,N as i,O as l,D as n,P as o,T as r,k as s,A as t};