openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
2 lines • 16.8 kB
JavaScript
const __vite__mapDeps=(i,m=__vite__mapDeps,d=(m.f||(m.f=["./composer-dictation-session-BhPkpaHO.js","./audio-Bq5-OuHh.js","./shared-B2BynUo8.js","./control-ui-connection-D5QrEAFW.css","./control-ui-boot-shared-styles-BorAdMVO.css","./response-body-Db_B5-4E.js","./string-literal-union-XE10pie5.js","./system-Bk-o-QjI.js","./primitives-ob2SrhpZ.js","./protocol-validator-CqgnR8zo.js","./json-schema-BDPIr-N8.js"])))=>i.map(i=>d[i]);
import{n as e}from"./rolldown-runtime-CNOWq4Kh.js";import{Gc as t,Kc as n,Kl as r,Qt as i,Xt as a,bc as o,kc as s,lu as c,oc as l,sc as u,uu as d,wa as ee,wc as f,xa as p,xc as m}from"./control-ui-connection-Bfj9beF_.js";import{Jk as h,Nn as te,Rn as g,Yk as _}from"./control-ui-boot-shared-DFTSUQQ0.js";import{u as ne}from"./deferred-ui-support-D-VedBtc.js";import{i as re,r as v,t as y}from"./talk-event-vocabulary-DeVLT5XG.js";function b(){return(b=e((()=>{re(),new Set(v),new Set(y)})))()}function ie(e){let t=r(e);if(t)return t===`webrtc-sdp`?`webrtc`:t===`json-pcm-websocket`?`provider-websocket`:t}function x(){return(x=e((()=>{b()})))()}function S(e){try{for(let n of t(e))try{if(!(n instanceof Error))continue;try{n.stack=String(n.stack)}catch{n.stack=`Stack trace unavailable: custom formatter failed`}}catch{}}catch{}}function C(){return(C=e((()=>{n()})))()}function w(){let e=globalThis,t=e[E];if(t instanceof Map)return t;let n=new Map;return e[E]=n,n}function T(e,t,n,r=`close-and-restart`){let i=globalThis,a;return Object.hasOwn(i,e)?a=i[e]:(a=t(),i[e]=a),n&&w().set(e,{lifecycle:r,reset:()=>n(a)}),a}var E;function D(){return(D=e((()=>{E=Symbol.for(`openclaw.globalSingletonLifecycleResets`)})))()}function ae(e){return k?k.runInAsyncScope(e):e()}var O,k;function A(){return(A=e((()=>{D(),O=typeof process>`u`?void 0:process.getBuiltinModule?.(`node:async_hooks`),k=O?T(Symbol.for(`openclaw.detachedAsyncContext`),()=>new O.AsyncResource(`openclaw.detached-async-context`)):void 0})))()}var j,M;function N(){return(N=e((()=>{C(),_(),A(),j=typeof process>`u`?void 0:process.getBuiltinModule(`node:async_hooks`).AsyncLocalStorage,M=class{constructor(e){if(this.options=e,this.pending=[],this.pendingWeight=0,this.active=!1,this.sealed=!1,this.overflowed=!1,this.acceptedSequence=0,this.settledPrefix=Promise.resolve(),!Number.isSafeInteger(e.maxPendingCount)||e.maxPendingCount<0)throw Error(`maxPendingCount must be a non-negative safe integer`);if(!Number.isFinite(e.maxPendingWeight)||e.maxPendingWeight<0)throw Error(`maxPendingWeight must be a non-negative finite number`)}get isIdle(){return!this.active&&this.pending.length===0}get didOverflow(){return this.overflowed}enqueue(e,t={}){if(this.sealed)return{accepted:!1,reason:`sealed`};let n=t.weight??1;if(!Number.isFinite(n)||n<0)throw Error(`queue task weight must be a non-negative finite number`);if(this.active&&(this.pending.length>=this.options.maxPendingCount||this.pendingWeight+n>this.options.maxPendingWeight))return t.sealOnOverflow===!1?{accepted:!1,reason:`capacity`}:(this.sealed=!0,this.overflowed=!0,{accepted:!1,reason:`overflow`});let{promise:r,resolve:i,reject:a}=h(),o={sequence:++this.acceptedSequence,weight:n,run:j?j.bind(e):e,resolve:e=>i(e),reject:a};return this.settledPrefix=r.then(()=>void 0,()=>void 0),this.active?(this.pending.push(o),this.pendingWeight+=n):(this.active=!0,ae(()=>void this.runTask(o))),{accepted:!0,completion:r}}seal(){this.sealed=!0}flush(e={}){let t=this.settledPrefix,n=this.acceptedSequence;return e.requireSuccess===!0?t.then(()=>{if(this.firstFailure&&this.firstFailure.sequence<=n)throw this.firstFailure.error}):t}async runTask(e){try{e.resolve(await e.run())}catch(t){this.firstFailure||=(S(t),{sequence:e.sequence,error:t}),e.reject(t)}finally{let e=this.pending.shift();e?(this.pendingWeight-=e.weight,queueMicrotask(()=>void this.runTask(e))):this.active=!1}}}})))()}function P(e){return s(e.trim(),F)}var F,I,L,R,z;function B(){return(B=e((()=>{N(),F=8e3,I=40,L=I*F,R=`Voice transcript persistence could not keep up; the realtime session was stopped.`,z={maxPendingCount:I,maxEntryChars:F,overflowMessage:R,createQueue:()=>new M({maxPendingCount:I,maxPendingWeight:L})}})))()}function oe(e,t){let n=ne(Y,e),r=n.get(t)??0,i=[...n.values()].reduce((e,t)=>e+t,0);if(r>=G||i>=K)throw Error(`Too many active or closing realtime Talk voice sessions`);n.set(t,r+1);let a=n,o=new AbortController,s=new AbortController,c=!1,l,u=()=>{if(c)return;c=!0;for(let e of l??[])clearTimeout(e);l=void 0;let e=(a.get(t)??1)-1;e>0?a.set(t,e):a.delete(t)};return{signal:o.signal,closeSignal:s.signal,beginDrain:()=>{c||l||(l=[setTimeout(()=>{o.abort()},q),setTimeout(()=>{o.abort(),s.abort(),u()},J)])},release:u}}async function V(e,t,n){let r;for(let n of[0,500,2e3]){if(e.aborted)throw o(`voice transcript persistence aborted`);try{n>0&&await f(n,e),await t();return}catch(t){if(e.aborted)throw o(`voice transcript persistence aborted`);r=t}}throw r instanceof Error?r:Error(n,{cause:r})}var H,U,W,G,K,q,J,Y;function X(){return(X=e((()=>{u(),m(),_(),B(),H=1024,U=1024,W=class{constructor(e,t,n){this.queue=e,this.write=t,this.onFailure=n,this.sequence=0,this.nextWriteOrder=1,this.items=new Map,this.changed=h()}observe(e){if(e.type===`settled`)return this.settle(this.requireItem(e.itemId)),[];if(this.items.has(e.itemId))return[];if(this.items.size>=H||e.itemId.length>U||(e.previousItemId?.length??0)>U)throw Error(`Realtime transcript item identity limit exceeded`);let t={previousItemId:e.previousItemId===void 0?this.lastItemId:e.previousItemId,role:e.role,settled:e.role===null};return e.role!==null&&this.enqueue(()=>this.writeThrough(t),z.maxEntryChars,()=>void 0),this.items.set(e.itemId,t),this.assignOrders()}publish(e){if(e.itemId!==void 0){let t=this.requireItem(e.itemId);if(t.role!==e.role)throw Error(`Realtime transcript item changed roles`);return t.settled?void 0:(e.final&&this.settle(t,P(e.text)||void 0),{...e,order:t.order})}if(e.final){let t=P(e.text);if(t){let n=String(++this.sequence);this.enqueue(()=>this.write(n,e.role,t),t.length)}}return e}close(){let e=new Set;for(let[t,n]of this.items)n.order===void 0&&e.add(t),n.settled||(e.add(t),this.settle(n)),n.previousItemId&&!this.items.has(n.previousItemId)&&e.add(n.previousItemId);return this.assignOrders(!0),[...e]}assignOrders(e=!1){let t=[];for(;;){let n=[...this.items].filter(([,e])=>e.order===void 0),r=n.find(([,e])=>e.previousItemId==null?this.lastItemId===void 0:e.previousItemId===this.lastItemId)??(e?n.find(([,e])=>e.previousItemId==null||!this.items.has(e.previousItemId)||this.items.get(e.previousItemId)?.order!==void 0)??n[0]:void 0);if(!r)break;let[i,a]=r;a.order=a.role===null?this.sequence:++this.sequence,this.lastItemId=i,a.role!==null&&t.push({itemId:i,order:a.order})}return this.notify(),t}async writeThrough(e){let t;for(;!e.written;){let e=[...this.items.values()].find(e=>e.role!==null&&e.order===this.nextWriteOrder);if(!e?.settled){await this.changed.promise;continue}e.written=!0,this.nextWriteOrder+=1;let n=e.text;if(e.text=void 0,n&&e.role!==null)try{await this.write(String(e.order),e.role,n)}catch(e){t??={error:e},this.onFailure(e)}}if(t)throw t.error}settle(e,t){e.settled||(e.settled=!0,e.text=t,this.notify())}notify(){this.changed.resolve(),this.changed=h()}requireItem(e){let t=this.items.get(e);if(!t||t.role===null)throw Error(`Realtime transcript refers to an unknown speech item`);return t}enqueue(e,t,n=this.onFailure){let r=this.queue.enqueue(e,{weight:t});if(!r.accepted)throw Error(z.overflowMessage);r.completion.catch(n)}},G=2,K=16,q=l,J=q+l,Y=new WeakMap})))()}async function se(e){let t=!1,n;if(await Promise.all([...Z].map(async r=>{try{await r.switchCameraIfEnabled(e)}catch(e){t=!0,n??=e}})),t)throw n}var Z,Q;function $(){return($=e((()=>{u(),x(),B(),p(),i(),g(),X(),d(),Z=new Set,Q=class{constructor(e,t,n={},r={},i={}){this.client=e,this.sessionKey=t,this.options=r,this.localOptions=i,this.transport=null,this.pendingStartup=null,this.closed=!1,this.closeCompletion=Promise.resolve(),this.lifecycleGeneration=0,this.videoOperation=0,this.transportGeneration=0,this.transcriptQueue=z.createQueue(),this.callbacks={...n,onStatus:(e,t)=>{(e===`idle`||e===`error`)&&this.localOptions.playback?.stop(),n.onStatus?.(e,t)}}}async start(){let e=oe(this.client,this.sessionKey),t=!1,n;try{this.retireTransport();let r=this.lifecycleGeneration;if(this.closed=!1,this.callbacks.onStatus?.(`connecting`,ee(`chat.voice.preparing`)),this.localOptions.split){let{SplitTalkTransport:e}=await c(async()=>{let{SplitTalkTransport:e}=await import(`./split-DvaGhBk_.js`);return{SplitTalkTransport:e}},__vite__mapDeps([0,1,2,3,4]),import.meta.url);if(this.closed||r!==this.lifecycleGeneration)return;let t=new e(this.client,this.sessionKey,this.callbacks,this.localOptions.split,this.localOptions.inputDeviceId);this.pendingStartup=t;try{if(await t.start()===`cancelled`||this.closed||r!==this.lifecycleGeneration){t.stop({disposition:`detach`});return}this.transport=t}catch(e){throw t.stop({disposition:`detach`}),e}finally{this.pendingStartup===t&&(this.pendingStartup=null)}return}let[{createRealtimeTalkTransport:i},a]=await Promise.all([c(()=>import(`./transport.runtime-Bnc9_koi.js`),__vite__mapDeps([5,6,7,8,9,10,1,2,3,4]),import.meta.url),this.resolveVideoCapability()]);if(this.closed||r!==this.lifecycleGeneration)return;n=new te(()=>void 0,e=>this.callbacks.onStatus?.(`connecting`,e)),this.pendingStartup=n;try{await n.open(this.localOptions.inputDeviceId)}catch(e){if(this.closed||r!==this.lifecycleGeneration)return;throw e}if(this.closed||r!==this.lifecycleGeneration)return;n.requireStream();let o=[`voice-transcript`,`voice-selection`];a&&o.push(`camera-frame`);let s=await this.createSession({...this.options,capabilities:o},r),{transport:l}=s;if(l===`managed-room`)throw Error(`Managed-room realtime Talk sessions are not available in this UI yet`);let u=s.voiceSessionId??(s.transport===`gateway-relay`?s.relaySessionId:void 0);if(!u)throw Error(`Realtime Talk session did not return a voice session id`);if(this.closed||r!==this.lifecycleGeneration){this.closeUnadoptedVoiceSession(u,l,e),t=!0;return}l!==`gateway-relay`&&(this.voiceSessionId=u,this.transportGeneration=r,this.clientVoiceSessionOwner=e,t=!0);let d=l===`gateway-relay`?this.callbacks:this.clientOwnedTranscriptCallbacks(u,r,e.signal),f=this.transcriptQueue,p=null,m=()=>{if(p?.stop({emitClosed:!1}),l===`gateway-relay`)p?e.release():this.closeUnadoptedVoiceSession(u,l,e);else if(this.clientVoiceSessionOwner===e){let e=this.detachVoiceSession();e&&this.closeLogicalVoiceSession(e)}},h;try{n.requireStream(),p=i(s,{client:this.client,sessionKey:this.sessionKey,voiceSessionId:u,flushTranscriptWrites:async()=>await f.flush(),callbacks:d,input:n,videoDeviceId:this.localOptions.videoDeviceId}),this.pendingStartup=p,this.callbacks.onVideoCapability?.(a&&typeof p.setVideoEnabled==`function`),h=this.pendingStartup===p?await p.start():`cancelled`}catch(e){throw this.pendingStartup===p&&(this.pendingStartup=null),m(),t=!0,e}if(this.pendingStartup===p&&(this.pendingStartup=null),h===`cancelled`||this.closed||r!==this.lifecycleGeneration){m(),t=!0;return}this.transport=p,this.selectedTransport=l,l===`gateway-relay`&&(this.voiceSessionId=u,this.transportGeneration=r,e.release()),t=!0;try{p.activate?.()}catch(e){throw this.transport===p&&this.retireTransport(),e}}finally{n&&this.pendingStartup===n&&(this.pendingStartup=null,n.stop()),t||e.release()}}async resolveVideoCapability(){if(!this.callbacks.onVideoCapability)return!1;try{let e=await this.client.request(`talk.catalog`,{sessionKey:this.sessionKey},{timeoutMs:l}),t=e.realtime.activeProvider;return t?e.realtime.providers.find(e=>e.id===t||e.aliases?.includes(t))?.supportsVideoFrames===!0:!1}catch{return!1}}async createSession(e,t){if(e.voiceChangeId&&e.transport===`gateway-relay`)return this.createRelaySession(e);try{return await this.client.request(`talk.client.create`,{sessionKey:this.sessionKey,...e},{timeoutMs:l})}catch(n){if(e.voiceChangeId||this.closed||this.lifecycleGeneration!==t)throw n;let r;try{r=await this.client.request(`talk.config`,{},{timeoutMs:l})}catch{throw n}let i=r.config?.talk?.realtime?.transport;if(!r.config||this.closed||this.lifecycleGeneration!==t||i!==void 0&&ie(i)!==`gateway-relay`)throw n;try{return await this.createRelaySession(e)}catch{throw n}}}createRelaySession(e){return this.client.request(`talk.session.create`,{sessionKey:this.sessionKey,...e,mode:`realtime`,transport:`gateway-relay`,brain:`agent-consult`,capabilities:[`voice-selection`]},{timeoutMs:l})}stop(e={}){try{return this.retireTransport(e)}finally{this.callbacks.onStatus?.(`idle`)}}getVoiceSessionId(){return this.closed?void 0:this.voiceSessionId}getTransport(){return this.closed?void 0:this.selectedTransport}retireTransport(e={disposition:`detach`}){this.lifecycleGeneration+=1,this.closed=!0,this.videoOperation+=1,this.rememberVideoEnabled(!1);let t=this.detachVoiceSession(),n=this.transport,r=this.pendingStartup,i=[];this.transport=null,this.selectedTransport=void 0,this.pendingStartup=null;try{i.push(Promise.resolve(r?.stop({...e,emitClosed:!1})))}finally{try{i.push(Promise.resolve(n?.stop(e)))}finally{t&&i.push(this.closeLogicalVoiceSession(t))}}return(t||n||r)&&(this.closeCompletion=Promise.all(i).then(()=>void 0),this.closeCompletion.catch(()=>void 0)),this.closeCompletion}closeUnadoptedVoiceSession(e,t,n){if(t===`gateway-relay`){this.client.request(`talk.session.close`,{sessionId:e},{timeoutMs:l}).catch(()=>void 0).finally(n.release);return}let r=z.createQueue();r.seal(),this.closeLogicalVoiceSession({voiceSessionId:e,transcriptQueue:r,owner:n})}clientOwnedTranscriptCallbacks(e,t,n){let r=new W(this.transcriptQueue,async(t,r,i)=>{await V(n,()=>this.client.request(`talk.client.transcript`,{sessionKey:this.sessionKey,voiceSessionId:e,entryId:t,role:r,text:i,timestamp:Date.now()},{signal:n,timeoutMs:l}),`voice transcript save failed`)},e=>{if(n.aborted)return;let r=`Voice transcript could not be saved: ${a(e)}`;console.warn(r,e),this.transportGeneration===t&&this.callbacks.onStatus?.(`error`,r)});this.transcriptItems=r;let i=()=>this.transportGeneration===t&&this.voiceSessionId===e,o=e=>{if(i())try{return e()}catch(e){this.failTranscriptPersistence(t,a(e));return}};return{...this.callbacks,onTalkEvent:e=>{if(i())try{e.type===`session.closed`&&this.retireTransport()}finally{this.callbacks.onTalkEvent?.(e)}},onTranscriptItem:e=>{let t=o(()=>r.observe(e));t?.length&&this.callbacks.onTranscriptOrder?.(t)},onTranscript:e=>{let t=o(()=>r.publish(e));t&&this.callbacks.onTranscript?.(t)}}}failTranscriptPersistence(e,t){this.transportGeneration===e&&this.voiceSessionId&&(this.retireTransport(),this.transportGeneration+=1,console.warn(t),this.callbacks.onStatus?.(`error`,t))}detachVoiceSession(){let e=this.voiceSessionId;if(!e)return;let t=this.clientVoiceSessionOwner?{voiceSessionId:e,generation:this.transportGeneration,transcriptQueue:this.transcriptQueue,owner:this.clientVoiceSessionOwner}:void 0,n=this.transcriptItems?.close()??[];if(this.transcriptItems=void 0,this.transcriptQueue.seal(),this.voiceSessionId=void 0,this.transcriptQueue=z.createQueue(),this.clientVoiceSessionOwner=void 0,n.length>0){let e=`Voice call closed with ${n.length} unfinished transcript item(s); finalized speech was preserved.`;console.warn(e)}return t}closeLogicalVoiceSession(e){let t=e.owner;t.beginDrain();let n=e.transcriptQueue.flush().then(()=>V(t.closeSignal,()=>this.client.request(`talk.client.close`,{sessionKey:this.sessionKey,voiceSessionId:e.voiceSessionId},{signal:t.closeSignal,timeoutMs:l}),`Realtime Talk voice session close failed`)).finally(t.release);n.catch(n=>{t.closeSignal.aborted||(console.warn(`Realtime Talk voice session close failed`,n),this.transportGeneration===e.generation&&this.callbacks.onStatus?.(`error`,`Realtime Talk voice session close failed`))});let r=n.then(()=>e.transcriptQueue.flush({requireSuccess:!0}));return r.catch(()=>void 0),r}async setVideoEnabled(e){let t=this.transport;if(this.closed||!t?.setVideoEnabled)throw Error(`Camera is unavailable for this realtime session`);let n=++this.videoOperation,r=Z.has(this);this.rememberVideoEnabled(e);try{await t.setVideoEnabled(e)}catch(e){throw n===this.videoOperation&&!this.closed&&this.transport===t&&this.rememberVideoEnabled(r),e}n===this.videoOperation&&(this.closed||this.transport!==t)&&this.rememberVideoEnabled(!1)}rememberVideoEnabled(e){e?Z.add(this):Z.delete(this)}async switchCamera(e){let t=e?.trim()||void 0;if(this.localOptions.videoDeviceId=t,this.closed||!this.transport?.switchCamera)throw Error(`Camera switching is unavailable for this realtime session`);await this.transport.switchCamera(t)}async switchCameraIfEnabled(e){if(Z.has(this))try{await this.switchCamera(e)}catch(e){throw this.callbacks.onVideoError?.(e),e}}}})))()}export{$ as n,se as r,Q as t};