@autoark-ai/eva-client-sdk-ts 0.0.2-dev → 0.0.4-dev

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/GATEWAY_TERMS.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Version 1.0 — Effective Date: July 20, 2026
4
4
 
5
- The official Eva Gateway is a hosted service separate from the Eva Client SDK. The SDK license does not grant any right to access or use the Gateway.
5
+ The official EVA Gateway is a hosted service separate from the EVA Client SDK. The SDK license does not grant any right to access or use the Gateway.
6
6
 
7
7
  Access to and use of the Gateway are governed by the terms and policies published on the official AutoArk AI website:
8
8
 
package/LICENSE CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Version 1.0 — Effective Date: July 20, 2026
4
4
 
5
- This Proprietary SDK License Agreement (the “Agreement”) governs your use of the Eva Client SDK package published as `@autoark-ai/eva-client-sdk-ts` (the “SDK”). “Licensor” means AutoArk AI and the legal entity identified as the SDK publisher or operator on the applicable official website, order form, or service console. “You” or “Licensee” means the individual or entity that downloads, installs, copies, or uses the SDK.
5
+ This Proprietary SDK License Agreement (the “Agreement”) governs your use of the EVA Client SDK package published as `@autoark-ai/eva-client-sdk-ts` (the “SDK”). “Licensor” means AutoArk AI and the legal entity identified as the SDK publisher or operator on the applicable official website, order form, or service console. “You” or “Licensee” means the individual or entity that downloads, installs, copies, or uses the SDK.
6
6
 
7
7
  By downloading, installing, copying, or using the SDK, You accept this Agreement. If You use the SDK for an entity, You represent that You have authority to bind that entity. If You do not agree, do not use the SDK.
8
8
 
@@ -10,7 +10,7 @@ By downloading, installing, copying, or using the SDK, You accept this Agreement
10
10
 
11
11
  - “Application” means a software product or service developed and controlled by Licensee that incorporates the SDK for use as part of that product or service.
12
12
  - “Compiled Output” means JavaScript bundles or other executable artifacts produced from an unmodified SDK through the Permitted Build Operations in Section 3.
13
- - “Official Gateway” means the Eva Gateway endpoints selected and embedded by Licensor in an authorized SDK release.
13
+ - “Official Gateway” means the EVA Gateway endpoints selected and embedded by Licensor in an authorized SDK release.
14
14
  - “Third-Party Components” means software or model assets identified in `THIRD_PARTY_NOTICES.md`.
15
15
 
16
16
  ## 2. Limited License Grant
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # Eva TypeScript SDK
1
+ # EVA TypeScript SDK
2
2
 
3
- `@autoark-ai/eva-client-sdk-ts` 是浏览器优先的 Eva 多轮语音对话 SDK。它提供一个稳定的 Agent Facade、可观察的消息与事件,以及可替换的音频输入、输出、AEC 和摄像头扩展点。
3
+ `@autoark-ai/eva-client-sdk-ts` 是浏览器优先的 EVA 多轮语音对话 SDK。它提供一个稳定的 Agent Facade、可观察的消息与事件,以及可替换的音频输入、输出、AEC 和摄像头扩展点。
4
4
 
5
5
  ## 安装
6
6
 
@@ -53,7 +53,7 @@ import {
53
53
 
54
54
  ## 浏览器最小用法
55
55
 
56
- 下面的 model 与 voice 组合已在 browser example 验证:
56
+ 下面演示 SDK 公共 API 的最小组合。代码中的 `model``voice` 是接入示例值,实际可用性应按所选 Gateway 环境确认;
57
57
 
58
58
  ```ts
59
59
  import {
@@ -86,11 +86,11 @@ const transports: MediaTransportsConfig = {
86
86
  const agent = createEvaVoiceDialogueAgent({
87
87
  apiKey: applicationManagedApiKey,
88
88
  asr: {
89
- model: "ark_asr_3b",
90
- sampleRate: 16_000,
89
+ model: "fun_asr",
90
+ sampleRate: 48_000,
91
91
  },
92
92
  llm: {
93
- model: "qiduoduo_chat_vlm",
93
+ model: "doubao-seed-2-0-mini-nothink",
94
94
  },
95
95
  tts: {
96
96
  model: "cosyvoice_tts",
@@ -161,7 +161,7 @@ Agent 一旦开始停止,就不再接受新的 turn 或事件订阅。需要
161
161
 
162
162
  | 字段 | 必填 | 说明 |
163
163
  |---|---:|---|
164
- | `apiKey` | 是 | 应用提供并管理的 Eva Gateway AK |
164
+ | `apiKey` | 是 | 应用提供并管理的 EVA Gateway AK |
165
165
  | `asr.model` | 是 | ASR model 标识 |
166
166
  | `asr.sampleRate` | 是 | ASR 接收的目标 PCM 采样率,必须为正整数 |
167
167
  | `llm.model` | 是 | LLM model 标识 |
@@ -266,12 +266,15 @@ interface StructuredError {
266
266
  source: "sdk" | "provider" | "gateway" | "media";
267
267
  provider?: string;
268
268
  statusCode?: number;
269
+ traceId?: string;
269
270
  role?: "audio-input" | "audio-output" | "aec" | "camera";
270
271
  operation?: "start" | "capture" | "stop";
271
272
  reason?: "not_configured" | "permission_denied" | "device_unavailable" | "unsupported" | "timeout" | "invalid_data" | "operation_failed";
272
273
  }
273
274
  ```
274
275
 
276
+ Gateway 错误的 `traceId` 只在响应头提供合法 `autoark-trace-id` 时出现,用于把客户端错误与 Gateway 日志关联;错误分类仍使用 `source`、`provider` 与 `statusCode`。SDK 不从响应 body、`request_id` 或 `x-request-id` 回退生成该字段。
277
+
275
278
  事件和错误不会额外回显 AK、Authorization header 或原始 provider 响应。
276
279
 
277
280
  ## 可扩展 Media SPI
@@ -1,7 +1,7 @@
1
1
  # Third-Party Notices
2
2
 
3
3
  This package redistributes or relies on the third-party components listed below. The notices and
4
- license terms in this file apply only to those components, not to the Eva SDK as a whole.
4
+ license terms in this file apply only to those components, not to the EVA SDK as a whole.
5
5
 
6
6
  ## Silero VAD v6.2.1 ONNX model
7
7
 
package/dist/index.d.ts CHANGED
@@ -37,6 +37,8 @@ export interface StructuredError {
37
37
  provider?: string;
38
38
  /** HTTP status code when a Gateway response supplied one. */
39
39
  statusCode?: number;
40
+ /** Opaque Gateway log-correlation identifier; present only for a validated Gateway response header. */
41
+ traceId?: string;
40
42
  /** Media role that failed; present only when `source` is `media`. */
41
43
  role?: MediaRole;
42
44
  /** Media lifecycle operation that failed; present only when `source` is `media`. */
@@ -121,7 +123,7 @@ interface RuntimeConfig {
121
123
  */
122
124
  metadata?: JsonObject;
123
125
  }
124
- /** Public managed configuration for one Eva voice-dialogue agent instance. */
126
+ /** Public managed configuration for one EVA voice-dialogue agent instance. */
125
127
  export interface EvaVoiceDialogueAgentConfig extends RuntimeConfig {
126
128
  /**
127
129
  * Gateway credential shared by the built-in ASR, LLM, and TTS stages.
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
- function C(e){if(!Pe(e))throw new TypeError("Metadata must be a JSON-compatible object");return xe(e,new Set)}function Re(e,t){if(e===null||typeof e=="boolean"||typeof e=="string")return e;if(typeof e=="number"){if(!Number.isFinite(e))throw new TypeError("Metadata numbers must be finite");return e}if(Array.isArray(e))return Ie(e,t,()=>e.map(r=>Re(r,t)));if(Pe(e))return xe(e,t);throw new TypeError("Metadata must contain only JSON-compatible values")}function xe(e,t){return Ie(e,t,()=>{if(Reflect.ownKeys(e).some(n=>typeof n!="string"))throw new TypeError("Metadata object keys must be strings");let r={};for(let[n,a]of Object.entries(e))r[n]=Re(a,t);return r})}function Ie(e,t,r){if(t.has(e))throw new TypeError("Metadata must not contain cycles");t.add(e);try{return r()}finally{t.delete(e)}}function Pe(e){if(typeof e!="object"||e===null||Array.isArray(e))return!1;let t=Object.getPrototypeOf(e);return t===Object.prototype||t===null}var h=class extends Error{fatal;source="sdk";constructor(t,r={}){super(t,{cause:r.cause}),this.name="EvaSdkError",this.fatal=r.fatal??!0}},N=class extends h{provider;source="provider";constructor(t,r){super(t,r),this.name="StageProviderError",this.provider=r.provider}},W=class extends h{provider;statusCode;source="gateway";constructor(t,r){super(t,r),this.name="GatewayAccessError",this.provider=r.provider,r.statusCode!==void 0&&(this.statusCode=r.statusCode)}},O=class extends h{role;operation;reason;source="media";constructor(t,r){super(t,r),this.name="MediaIoError",this.role=r.role,this.operation=r.operation,this.reason=r.reason}};function w(e,t={}){return e instanceof h?e:new h(t.message??"SDK operation failed",{fatal:t.fatal??!0,cause:e})}function E(e,t){return e instanceof h?e:new N(t.message??"Stage provider failed",{provider:t.provider,fatal:t.fatal??!0,cause:e})}function L(e,t){if(e instanceof h)return e;let r={provider:t.provider,fatal:t.fatal??!0,cause:e};return t.statusCode!==void 0&&(r.statusCode=t.statusCode),new W(t.message??vt(t.statusCode),r)}function vt(e){return e===void 0?"Gateway request failed":`Gateway request failed with status ${e}`}var St=new Set(["pcm_s16le"]);function ie(e,t){if(!St.has(e.format))throw new h("Unsupported audio format",{fatal:!0});return{kind:"audio.input",streamId:t.streamId,...t.turnId!==void 0?{turnId:t.turnId}:{},...t.sequence!==void 0?{sequence:t.sequence}:{},partial:!0,final:!1,...t.timestamp!==void 0?{timestamp:t.timestamp}:{},metadata:t.metadata??{},audio:e.data,sampleRate:e.sampleRate,channels:e.channels}}function oe(e){return{data:e.audio,sampleRate:e.sampleRate,channels:e.channels,format:"pcm_s16le"}}function Fe(){let e,t=()=>{let r=e;if(r!==void 0)return e=void 0,r.controller.abort(),r};return{begin(r){t();let n=new AbortController;return e={...r,controller:n,signal:n.signal},e},current(){return e},cancel:t,complete(r){return e!==r?!1:(e=void 0,!0)},isCurrent(r){return e===r&&!r.signal.aborted},stop:t}}function ke(){let e="";return{push(t){if(t.length===0)return[];e+=t;let r=wt(e);return e=r.rest,r.sentences},flush(){let t=e.trim();return e="",t.length===0?[]:[t]},clear(){e=""}}}var bt=new Set(["\u3002","\uFF01","\uFF1F","!","?","\uFF1B",";","\u2026"]),At=new Set(['"',"'","\u201D","\u2019",")","\uFF09","]","\u3011","}","\u300B","\u300D","\u300F"]),Et=/(?:Mr|Mrs|Ms|Dr|Prof|Sr|Jr|St|vs|etc|e\.g|i\.e)\.$/i;function wt(e){let t=[],r=0,n=0;for(;n<e.length;){if(!Tt(e,n)){n+=1;continue}let a=n+1;for(;a<e.length&&At.has(e[a]);)a+=1;let i=a;for(;i<e.length&&/\s/u.test(e[i]);)i+=1;if(i>=e.length)break;let s=e.slice(r,a).trim();s.length>0&&t.push(s),r=i,n=i}return{sentences:t,rest:e.slice(r)}}function Tt(e,t){let r=e[t];if(bt.has(r))return!(r==="\u2026"&&e[t+1]==="\u2026");if(r!==".")return!1;let n=e[t-1],a=e[t+1];return n!==void 0&&a!==void 0&&/\d/u.test(n)&&/\d/u.test(a)||a==="."?!1:!Et.test(e.slice(0,t+1))}function Me(e){let t=e,r=0,n=()=>{let o,l=new Promise(m=>{o=m});return{id:r,queue:[],invalidated:l,invalidate:o,currentController:void 0,pump:void 0}},a=n(),i=!1,s,u=()=>{let o=a;r+=1,o.queue.length=0,o.currentController?.abort(),o.invalidate(),a=n()},c=()=>{i=!0,u()};t.parentSignal.aborted?c():t.parentSignal.addEventListener("abort",c,{once:!0});let d=o=>{o.pump!==void 0||o.queue.length===0||(o.pump=p(o).catch(l=>{o===a&&(s=l,i=!0,u())}).finally(()=>{o.pump=void 0,o===a&&o.queue.length>0&&d(o)}))};async function p(o){for(;o.queue.length>0&&!t.parentSignal.aborted;){let l=o.queue.shift(),m=new AbortController;o.currentController=m;let v={signal:m.signal,isCurrent:()=>!t.parentSignal.aborted&&!m.signal.aborted&&o===a&&o.id===r};try{await t.process(l,v)}finally{o.currentController===m&&(o.currentController=void 0)}}}return{enqueue(o){i||t.parentSignal.aborted||o.trim().length===0||(a.queue.push(o),d(a))},clearAndAbort:u,async close(){i=!0;let o=a,l=o.pump;if(l!==void 0&&await Promise.race([l,o.invalidated]),t.parentSignal.removeEventListener("abort",c),s!==void 0)throw s}}}function Oe(e){let t=e,r=!1,n;return{start(){r||n!==void 0||(t.onStarted(),r=!0)},async close(){if(!r){n!==void 0&&await n;return}r=!1,n=Promise.resolve(t.onStopped()).finally(()=>{n=void 0}),await n},isActive(){return r}}}function Le(e){let t=e,r=t.now??Rt,n=r(),a,i,s=t.source==="text"||t.source==="greeting"?n:void 0,u,c,d,p=!1,o={},l=()=>{let m=t.source==="text"?0:o.vadMs??j(n,a),v=t.source==="text"?0:o.asrMs??j(i??a,s),f=o.llmFirstTokenMs??j(s,u),g=o.ttsFirstAudioMs??j(u,c),b=o.playbackMs??j(c,d),y={};D(y,"vadMs",m),D(y,"asrMs",v),D(y,"llmFirstTokenMs",f),D(y,"ttsFirstAudioMs",g),D(y,"playbackMs",b),Object.freeze(y);let S=[m,v,f,g],A=S.every(P=>P!==void 0)?S.reduce((P,ae)=>P+ae,0):void 0;return Object.freeze({turnId:t.turnId,...A!==void 0?{totalMs:A}:{},stages:y})};return{markVadStarted(){a??=r()},markAsrStarted(){i??=r()},markAsrFinal(){s??=r()},markLlmFirstToken(){u??=r()},markTtsFirstAudio(){c??=r()},markPlaybackStarted(){d??=r()},recordStageMetadata(m,v){let f=Ct[m],g=v[f];typeof g=="number"&&Number.isFinite(g)&&g>=0&&(o[f]=Math.round(g))},snapshot:l,takeSnapshot(){if(p)return;let m=l();if(Object.keys(m.stages).length!==0)return p=!0,m}}}var Ct={vad:"vadMs",asr:"asrMs",llm:"llmFirstTokenMs",tts:"ttsFirstAudioMs"};function Rt(){return typeof performance>"u"?Date.now():performance.now()}function j(e,t){if(!(e===void 0||t===void 0))return Math.max(0,Math.round(t-e))}function D(e,t,r){r!==void 0&&(e[t]=r)}function De(e,t={}){let r=xt(t.preSpeechMs),n=It(t.maxUtteranceMs),a=[],i=[],s=new Set,u=0,c,d=0,p=0,o=!1,l,m=()=>{for(let f of s)f();s.clear()},v=f=>{l??=new h(f,{fatal:!0}),o=!0,c=void 0,i.length=0,m()};return{async*vadAudio(){try{for await(let f of e){if(l!==void 0)throw l;let g=je(f);for(a.push(f),u+=g;a.length>1;){let b=a[0],y=je(b);if(u-y<r)break;a.shift(),u-=y}if(c!==void 0&&(c.frames.push(f),p+=g,p>n))throw v("VAD utterance exceeded max duration"),l;yield f}}catch(f){throw l??=f,o=!0,m(),f}},start(f,g=d+1){d=g,c={turnId:f,generation:g,frames:[...a]},p=u},stop(){c!==void 0&&c.frames.length>0&&(i.length=0,i.push(c)),c=void 0,p=0,a.length=0,u=0,m()},async*utterances(){for(;;){let f=i.pop();if(i.length=0,f!==void 0){yield{turnId:f.turnId,generation:f.generation,audio:Pt(f.frames)};continue}if(l!==void 0)throw l;if(o)return;await new Promise(g=>s.add(g))}},close(){o||(o=!0,c=void 0,m())}}}function xt(e){return Number.isFinite(e)&&e!==void 0&&e>=0?e:200}function It(e){return Number.isFinite(e)&&e!==void 0&&e>0?e:6e4}function je(e){return e.sampleRate<=0||e.channels<=0?0:e.audio.byteLength/2/e.channels/e.sampleRate*1e3}async function*Pt(e){for(let t of e)yield t}var Ft=1500,V=class extends Error{turnId;generation;constructor(t,r,n){super("Camera capture cancellation failed",{cause:t}),this.name="CameraCaptureSettlementError",this.turnId=r,this.generation=n,Object.defineProperty(this,"mediaError",{value:t,enumerable:!1,configurable:!1,writable:!1})}},q=class{source;now;settlementDeadlineMs;onFault;controlTail=Promise.resolve();acceptedControl=Promise.resolve();stopOperation;running=!1;stopping=!1;acceptedEnabled=!1;acceptedRequestId=0;active=!1;sessionIdentity=0;sessionController;pendingStart;pendingCapture;fault;constructor(t){this.source=t.source,this.now=t.now??Date.now,this.settlementDeadlineMs=t.cancellationSettlementDeadlineMs??Ft,this.onFault=t.onFault}isActive(){return this.active&&!this.stopping&&this.fault===void 0}currentFault(){return this.fault}setEnabled(t){if(this.stopping)return Promise.reject(this.faultedControlError(t?"start":"stop"));if(this.fault!==void 0)return Promise.reject(this.faultedControlError(t?"start":"stop"));if(this.acceptedEnabled===t)return this.acceptedControl;this.acceptedEnabled=t;let r=++this.acceptedRequestId;if(t||this.abortPendingWork(),!this.running)return this.acceptedControl=Promise.resolve(),this.acceptedControl;let n=this.enqueueControl(()=>this.applyEnabled(t,r));return this.acceptedControl=n.catch(a=>{throw this.acceptedRequestId===r&&(this.acceptedEnabled=!1),a}),this.acceptedControl}async startRuntime(){if(!this.running&&(this.running=!0,this.stopping=!1,!!this.acceptedEnabled))try{this.acceptedControl=this.enqueueControl(()=>this.applyEnabled(!0,this.acceptedRequestId)),await this.acceptedControl}catch(t){throw this.acceptedEnabled=!1,t}}async stopRuntime(){if(this.stopping)return this.stopOperation??Promise.resolve();this.stopping=!0,this.running=!1,this.acceptedEnabled=!1,this.abortPendingWork();let t=this.enqueueControl(async()=>{let r=this.source;if(r!==void 0)try{await this.stopSourceAfterCaptureSettlement(r,!0)}catch(n){throw this.markFault("stop","operation_failed",n)}finally{this.active=!1,this.sessionController=void 0}});return this.stopOperation=t,t}async beginCapture(t,r,n){if(await this.cancelPendingCaptureAndWait(),!this.isActive()||this.source===void 0)return;let a=new AbortController,i=Mt(),s=this.now(),u={turnId:t,generation:r},d=Promise.resolve().then(()=>this.source.capture(a.signal)).then(p=>{if(!(a.signal.aborted||i.settled))try{kt(p),k(i,{status:"success",snapshot:p,captureMs:Math.max(0,this.now()-s)})}catch(o){k(i,{status:"failure",error:F("capture","invalid_data",!1,o)})}},p=>{a.signal.aborted||i.settled||k(i,{status:"failure",error:F("capture",Ve(p),!1,p)})}).finally(()=>{u.timeoutHandle!==void 0&&clearTimeout(u.timeoutHandle),this.pendingCapture===u&&(this.pendingCapture=void 0)});return Object.assign(u,{controller:a,result:i,settlement:d}),u.timeoutHandle=setTimeout(()=>{i.settled||(a.abort(),k(i,{status:"failure",error:F("capture","timeout",!1)}),this.watchCaptureSettlement(u))},n),this.pendingCapture=u,{turnId:t,generation:r,result:i.promise}}async cancelPendingCaptureAndWait(){if(this.fault!==void 0)throw this.faultedControlError("capture");let t=this.pendingCapture;if(t!==void 0){t.controller.abort(),k(t.result,{status:"cancelled"});try{await this.settleWithin(t.settlement)}catch(r){let n=this.markFault("capture","operation_failed",r);throw new V(n,t.turnId,t.generation)}}}enqueueControl(t){let r=this.controlTail.then(t,t);return this.controlTail=r.catch(()=>{}),r}async applyEnabled(t,r){if(this.fault!==void 0)throw this.faultedControlError(t?"start":"stop");t?await this.startSession(r):await this.stopSession()}async startSession(t){if(this.active)return;let r=this.source;if(r===void 0)throw F("start","not_configured",!1);let n=++this.sessionIdentity,a=new AbortController;this.sessionController=a;let i=Promise.resolve().then(()=>r.start(a.signal)),s={controller:a,settlement:i.then(()=>{})};this.pendingStart=s;try{if(await Ot(i,a.signal),a.signal.aborted||n!==this.sessionIdentity||this.stopping)throw se();this.active=!0}catch(u){throw this.active=!1,Lt(u)||a.signal.aborted?u:F("start",Ve(u),!1,u)}finally{s.settlement.finally(()=>{this.pendingStart===s&&(this.pendingStart=void 0)}).catch(()=>{})}}async stopSession(){this.active=!1,this.abortPendingWork();let t=this.source;if(t!==void 0)try{await this.stopSourceAfterCaptureSettlement(t,!1),this.sessionController=void 0}catch(r){throw this.markFault("stop","operation_failed",r)}}async stopSourceAfterCaptureSettlement(t,r){let n=this.pendingStart?.settlement??Promise.resolve(),a=this.pendingCapture?.settlement??Promise.resolve();try{await this.settleWithin(a.catch(()=>{}))}catch(i){throw r&&await this.settleWithin(Promise.all([n.catch(()=>{}),Promise.resolve().then(()=>t.stop())])).catch(()=>{}),i}await this.settleWithin(Promise.all([n.catch(()=>{}),Promise.resolve().then(()=>t.stop())]).then(()=>{}))}abortPendingWork(){this.sessionController?.abort(),this.pendingStart?.controller.abort();let t=this.pendingCapture;t!==void 0&&(t.controller.abort(),k(t.result,{status:"cancelled"}))}async watchCaptureSettlement(t){try{await this.settleWithin(t.settlement)}catch(r){let n=this.markFault("capture","operation_failed",r);this.onFault?.(n,t.turnId,t.generation)}}settleWithin(t){return new Promise((r,n)=>{let a=setTimeout(()=>{n(new Error("Camera cancellation settlement deadline exceeded"))},this.settlementDeadlineMs);t.then(()=>{clearTimeout(a),r()},i=>{clearTimeout(a),n(i)})})}markFault(t,r,n){return this.fault===void 0&&(this.fault=F(t,r,!0,n)),this.active=!1,this.fault}faultedControlError(t){return F(t,"operation_failed",!0,this.fault)}};function kt(e){if(!(e.data instanceof Uint8Array)||e.data.byteLength===0)throw new Error("Camera snapshot bytes are empty");if(!/^image\/[a-z0-9.+-]+$/i.test(e.mimeType))throw new Error("Camera snapshot MIME is invalid");if(!Number.isInteger(e.width)||e.width<=0)throw new Error("Camera snapshot width is invalid");if(!Number.isInteger(e.height)||e.height<=0)throw new Error("Camera snapshot height is invalid")}function F(e,t,r,n){return new O("Camera operation failed",{role:"camera",operation:e,reason:t,fatal:r,cause:n})}function Ve(e){let t=e instanceof Error?e.name:"";return t==="NotAllowedError"||t==="SecurityError"?"permission_denied":t==="NotFoundError"||t==="NotReadableError"||t==="OverconstrainedError"?"device_unavailable":t==="NotSupportedError"?"unsupported":"operation_failed"}function Mt(){let e,t;return{promise:new Promise((n,a)=>{e=n,t=a}),resolve(n){e(n)},reject(n){t(n)},settled:!1}}function k(e,t){e.settled||(e.settled=!0,e.resolve(t))}function Ot(e,t){return t.aborted?Promise.reject(se()):new Promise((r,n)=>{let a=()=>n(se());t.addEventListener("abort",a,{once:!0}),e.then(r,n).finally(()=>{t.removeEventListener("abort",a)}).catch(()=>{})})}function se(){return new DOMException("Camera operation aborted","AbortError")}function Lt(e){return e instanceof Error&&e.name==="AbortError"}var _=class{constructor(t){this.config=t;this.agentMetadata=C(t.metadata??{}),this.cameraController=new q({...t.transports?.camera!==void 0?{source:t.transports.camera}:{},...t.now!==void 0?{now:t.now}:{},onFault:(r,n)=>{this.reportCameraFault(r,this.envelope("speech",n,this.agentMetadata))}})}config;listeners=new Set;tasks=new Set;pendingAsrControllers=new Map;turnScopes=Fe();cameraController;cameraCaptures=new Map;cameraFaultReported=!1;admissionTail=Promise.resolve();inputControlTail=Promise.resolve();rootController;started=!1;stopping=!1;inputEnabled=!0;inputSessionCounter=0;inputSession;speechGeneration=0;turnCounter=0;skipTts=!1;activeTtsTurn;committedHistory=[];turnTimings=new Map;messages=[];usedTurnIds=new Set;agentMetadata;messageCounter=0;onEvent(t){return this.listeners.add(t),()=>{this.listeners.delete(t)}}async start(){if(this.started)return;this.started=!0,this.rootController=new AbortController,this.inputEnabled&&this.canRunSpeechInput()&&await this.serializeInputControl(()=>this.reconcileInputSession());try{await this.cameraController.startRuntime()}catch(r){this.emit(x(this.envelope("camera",void 0,this.agentMetadata),w(r,{message:"Camera session failed",fatal:!1})))}let t=this.config.greeting;t!==void 0&&t.mode!=="disabled"&&this.track(this.scheduleGreeting(t))}async stop(){this.stopping=!0,this.inputEnabled=!1;let t=this.turnScopes.current();t!==void 0&&this.emitTurnLatency(t),this.rootController?.abort(),this.speechGeneration+=1,this.abortPendingAsr({emitLatency:!1});let r=this.inputSession===void 0?Promise.resolve():this.releaseInputSession(this.inputSession),n=this.cameraController.stopRuntime();this.turnScopes.stop();let a;try{await r}catch(i){a=i}try{await n}catch(i){a??=i}try{await this.config.transports?.output.stop()}catch(i){a=i}try{await this.config.transports?.aec.release()}catch(i){a??=i}if(a!==void 0)throw w(a,{message:"Dialogue runtime stop failed"})}async drain(){for(;this.tasks.size>0;)await Promise.allSettled([...this.tasks])}getMessages(){return this.messages.map(t=>({...t,metadata:C(t.metadata)}))}scheduleGreeting(t){let r=this.reserveTurnId(),n="greeting",a=this.envelope(n,r,this.agentMetadata);return this.beginTurnTiming(r,"greeting"),this.serializeAdmission(async()=>{if(this.rootController?.signal.aborted===!0)return;let i=this.turnScopes.begin({streamId:n,turnId:r});this.track(this.runAssistantTurn(n,r,t.mode==="dynamic"?t.prompt:t.text,a,i,{commitHistory:!1,...t.mode==="static"?{staticReply:t.text}:{},messageMetadata:this.agentMetadata,recordAssistant:!0}))})}async submitText(t,r={}){if(t.trim().length===0)return;this.started||await this.start();let n=this.reserveTurnId(r.turnId),a="manual-text",i=this.effectiveMetadata(r.metadata),s=this.envelope(a,n,i),u={kind:"text",streamId:a,turnId:n,partial:!1,final:!0,metadata:i,text:t};this.beginTurnTiming(n,"text"),await this.serializeAdmission(async()=>{this.speechGeneration+=1,this.abortPendingAsr(),await this.cancelCameraCaptureWithoutBlocking(s),await this.interruptActiveTurn(s,"manual_text"),this.commitMessage(n,"user",t,i),this.emit(Ge(u,"text"));let c=this.turnScopes.begin({streamId:a,turnId:n});this.track(this.runAssistantTurn(a,n,t,s,c,{messageMetadata:i}))})}async setSkipTts(t){if(this.skipTts===t||(this.skipTts=t,!t))return;let r=this.activeTtsTurn;if(!(r===void 0||!this.turnScopes.isCurrent(r.scope))){r.aggregator.clear(),r.worker.clearAndAbort();try{await this.config.transports?.output.flush(),this.activeTtsTurn===r&&this.turnScopes.isCurrent(r.scope)&&await r.playback.close()}catch(n){if(this.activeTtsTurn===r&&this.turnScopes.isCurrent(r.scope)){let a=E(n,{provider:"runtime"});throw this.emit(x(r.base,a)),a}throw E(n,{provider:"runtime"})}}}async setAudioInputEnabled(t){if(this.stopping)throw new h("Dialogue runtime is stopped",{fatal:!0});if(this.inputEnabled===t)return this.inputControlTail;if(this.inputEnabled=t,t||(this.speechGeneration+=1,this.abortPendingAsr({emitLatency:!1}),await this.cancelCameraCaptureWithoutBlocking(this.envelope("camera",void 0,this.agentMetadata)),this.inputSession!==void 0&&(this.inputSession.controller.abort(),this.releaseInputSession(this.inputSession).catch(()=>{}))),!!this.started)return this.serializeInputControl(()=>this.reconcileInputSession())}async setCameraCaptureEnabled(t){if(this.stopping)throw new h("Dialogue runtime is stopped",{fatal:!0});await this.cameraController.setEnabled(t)}serializeInputControl(t){let r=this.inputControlTail.then(t,t);return this.inputControlTail=r.catch(()=>{}),r}async reconcileInputSession(){let t=this.inputSession;if(!this.started||this.stopping||!this.inputEnabled||!this.canRunSpeechInput()){t!==void 0&&await this.releaseInputSession(t);return}t!==void 0&&!t.controller.signal.aborted||(t!==void 0&&await this.releaseInputSession(t),!(this.stopping||!this.inputEnabled||!this.canRunSpeechInput())&&await this.startInputSession())}async startInputSession(){let t=this.config.transports;if(t===void 0||this.config.providers.vad===void 0)return;let r={identity:++this.inputSessionCounter,controller:new AbortController};this.inputSession=r;let n=t.input;try{if(await n.start(),!this.isCurrentInputSession(r)){await this.releaseInputSession(r);return}let a=n.frames(r.controller.signal),i=this.runSpeechLoop(r.controller.signal,a,r.identity);this.track(i),i.finally(()=>{this.isCurrentInputSession(r)&&!r.controller.signal.aborted&&this.releaseInputSession(r).catch(()=>{})}).catch(()=>{})}catch(a){let i=r.controller.signal.aborted||!this.inputEnabled||this.stopping;if(await this.releaseInputSession(r),i)return;throw this.inputEnabled=!1,w(a,{message:"Audio input session failed"})}}releaseInputSession(t){if(t.releasePromise!==void 0)return t.releasePromise;t.controller.abort();let r=this.inputSession===t,n=this.config.transports?.input;return t.releasePromise=(async()=>{try{r&&await n?.stop()}catch(a){throw w(a,{message:"Audio input release failed"})}finally{this.inputSession===t&&(this.inputSession=void 0)}})(),t.releasePromise}isCurrentInputSession(t){return this.inputSession?.identity===t.identity&&!t.controller.signal.aborted&&this.inputEnabled&&!this.stopping}canRunSpeechInput(){return this.config.transports!==void 0&&this.config.providers.vad!==void 0}async runSpeechLoop(t,r,n){let a=this.config.transports,i=this.config.providers.vad;if(a===void 0||i===void 0)return;let s="speech",u,c,d=new AbortController,p=()=>d.abort();t.aborted?p():t.addEventListener("abort",p,{once:!0});let o=d.signal;try{let l=this.nearEndFrames(r,s,o),m=De(l),v=(async()=>{try{for await(let y of i.run(m.vadAudio(),{signal:o})){if(o.aborted)return;y.state==="started"?await this.serializeAdmission(async()=>{if(o.aborted)return;this.speechGeneration+=1,c=this.speechGeneration,u=this.reserveTurnId();let S=this.beginTurnTiming(u,"speech");S.recordStageMetadata("vad",y.metadata),S.markVadStarted(),this.abortPendingAsr(),m.start(u,c);let A=this.envelope(s,u,this.agentMetadata);if(await this.interruptActiveTurn(A,"user_speech"),o.aborted||c!==this.speechGeneration)return;this.emit(G(A,"speech.started"));let P;try{P=await this.cameraController.beginCapture(u,c,this.config.camera?.captureTimeoutMs??1500)}catch(ae){this.reportCameraFaultFromUnknown(ae,A,"Camera capture failed")}P!==void 0&&this.cameraCaptures.set(u,this.settleCameraCapture(P,A))}):y.state==="stopped"&&u!==void 0&&c!==void 0&&(m.stop(),this.emit(G(this.envelope(s,u,this.agentMetadata),"speech.stopped")),u=void 0,c=void 0)}}catch(y){let S=o.aborted||t.aborted;throw p(),this.abortPendingAsr({emitLatency:!S,inputSessionIdentity:n}),S||this.turnScopes.cancel(),y}finally{m.close()}})(),f=(async()=>{try{for await(let y of m.utterances()){if(o.aborted)return;if(!this.isCurrentSpeechGeneration(y.generation))continue;let S=Ut(o);this.pendingAsrControllers.set(S.controller,{streamId:s,turnId:y.turnId,inputSessionIdentity:n});let A=!1;try{A=await this.runAsr(y.audio,s,y.turnId,this.envelope(s,y.turnId,this.agentMetadata),y.generation,S.controller.signal)}finally{this.pendingAsrControllers.delete(S.controller),S.unlink()}!A&&this.isCurrentSpeechGeneration(y.generation)&&!o.aborted&&this.emitTurnLatency({streamId:s,turnId:y.turnId})}}catch(y){let S=o.aborted||t.aborted;throw p(),this.abortPendingAsr({emitLatency:!S,inputSessionIdentity:n}),S||this.turnScopes.cancel(),m.close(),y}})(),b=(await Promise.allSettled([v,f])).find(y=>y.status==="rejected");if(b!==void 0)throw b.reason}catch(l){!B(l)&&!t.aborted&&this.emit(x(this.envelope(s,u,this.agentMetadata),E(l,{provider:"runtime"})))}finally{this.abortPendingAsr({emitLatency:!1,inputSessionIdentity:n}),t.removeEventListener("abort",p)}}async runAsr(t,r,n,a,i,s){let u=!1,c=!1,d=async()=>{c||s.aborted||!this.isCurrentSpeechGeneration(i)||(c=!0,await this.cancelCameraCaptureWithoutBlocking(a))};try{let p=this.turnTimings.get(n),o=!1;p?.markAsrStarted();for await(let l of this.config.providers.asr.run(t,{signal:s})){if(s.aborted||!this.isCurrentSpeechGeneration(i))return!1;let m={...l,streamId:r,turnId:n};if(p?.recordStageMetadata("asr",l.metadata),l.final){if(o)continue;let v=!1;if(await this.serializeAdmission(async()=>{s.aborted||!this.isCurrentSpeechGeneration(i)||(p?.markAsrFinal(),l.text.trim().length>0&&this.commitMessage(n,"user",l.text,this.agentMetadata),this.emit(Ge(m,"speech")),v=l.text.trim().length>0)}),v){let f=await this.cameraCaptures.get(n);await this.serializeAdmission(async()=>{if(s.aborted||!this.isCurrentSpeechGeneration(i))return;let g=this.turnScopes.begin({streamId:r,turnId:n});this.track(this.runAssistantTurn(r,n,l.text,a,g,{messageMetadata:this.agentMetadata,...f!==void 0?{cameraSnapshot:f}:{}})),u=!0})}else await d();o=!0}else o||this.emit(Dt(m,"speech"))}return u||await d(),u}catch(p){return!B(p)&&!s.aborted&&this.isCurrentSpeechGeneration(i)&&this.emit(x(a,E(p,{provider:"asr"}))),await d(),!1}finally{this.cameraCaptures.delete(n)}}async runAssistantTurn(t,r,n,a,i,s={}){let u=i.signal,c=ke(),d=Oe({onStarted:()=>{this.turnScopes.isCurrent(i)&&this.emit(G(a,"playback.started"))},onStopped:()=>{this.turnScopes.isCurrent(i)&&this.emit(G(a,"playback.stopped"))}}),p=!1,o=Me({parentSignal:u,process:async(m,v)=>{try{for await(let f of this.ttsFrames(m,t,r,v.signal)){if(!v.isCurrent()||!this.turnScopes.isCurrent(i))return;let g=this.turnTimings.get(r);g?.recordStageMetadata("tts",f.metadata),g?.markTtsFirstAudio();let b=this.config.transports;if(b===void 0)continue;let y=oe(f);if(g?.markPlaybackStarted(),d.start(),await b.output.enqueue(y),!v.isCurrent()||!this.turnScopes.isCurrent(i)||(await b.aec.pushFarEnd(y),!v.isCurrent()||!this.turnScopes.isCurrent(i)))return}}catch(f){!B(f)&&v.isCurrent()&&this.turnScopes.isCurrent(i)&&(p=!0,o.clearAndAbort(),this.emit(x(a,E(f,{provider:"tts"}))))}}}),l={scope:i,base:a,aggregator:c,worker:o,playback:d};this.activeTtsTurn=l;try{if(!this.turnScopes.isCurrent(i))return;this.emit(G(a,"reply.started"));let m="",v=!1,f=s.staticReply!==void 0?jt(s.staticReply,t,r):this.config.providers.llm.run({messages:this.llmMessages(n,s.commitHistory!==!1,s.cameraSnapshot),streamId:t,turnId:r,metadata:s.messageMetadata??this.agentMetadata},{signal:u});for await(let g of f){if(u.aborted||!this.turnScopes.isCurrent(i))return;if(g.text.length>0){let b=this.turnTimings.get(r);if(b?.recordStageMetadata("llm",g.metadata),b?.markLlmFirstToken(),m+=g.text,this.emit(_e(a,"reply.partial",g.text)),!p&&!this.skipTts)for(let y of c.push(g.text))o.enqueue(y)}if(g.final){if(v=!0,!p&&!this.skipTts)for(let b of c.flush())o.enqueue(b);s.commitHistory!==!1&&this.commitHistory(n,m),s.recordAssistant!==!1&&this.commitMessage(r,"assistant",m,s.messageMetadata??this.agentMetadata),this.emit(_e(a,"reply.final",m));break}}if(!this.turnScopes.isCurrent(i))return;if(!v){c.clear(),o.clearAndAbort();return}if(await o.close(),!this.turnScopes.isCurrent(i))return;d.isActive()&&(await this.config.transports?.output.drain(),this.turnScopes.isCurrent(i)&&await d.close())}catch(m){!B(m)&&!u.aborted&&this.turnScopes.isCurrent(i)&&this.emit(x(a,E(m,{provider:"llm"})))}finally{this.turnScopes.isCurrent(i)&&this.emitTurnLatency(i),c.clear(),o.clearAndAbort(),this.activeTtsTurn===l&&(this.activeTtsTurn=void 0),this.turnScopes.complete(i)}}llmMessages(t,r,n){return[...this.config.systemPrompt!==void 0&&this.config.systemPrompt.length>0?[{role:"system",content:this.config.systemPrompt}]:[],...r&&this.config.history!==void 0?this.committedHistory.flatMap(({user:a,assistant:i})=>[{role:"user",content:a},{role:"assistant",content:i}]):[],{role:"user",content:n===void 0?t:[{type:"text",text:t},{type:"image",data:n.data,mimeType:n.mimeType}]}]}async settleCameraCapture(t,r){let n=await t.result;if(!(!this.isCurrentSpeechGeneration(t.generation)||this.stopping)&&n.status!=="cancelled"){if(n.status==="failure"){this.emit(x(r,n.error));return}return this.emit(Vt(r,n.snapshot,n.captureMs)),n.snapshot}}async cancelCameraCaptureWithoutBlocking(t){try{await this.cameraController.cancelPendingCaptureAndWait()}catch(r){this.reportCameraFaultFromUnknown(r,t,"Camera cancellation failed")}}reportCameraFaultFromUnknown(t,r,n){if(t instanceof V){this.reportCameraFault(t.mediaError,this.envelope("speech",t.turnId,this.agentMetadata));return}let a=t instanceof h?t:w(t,{message:n,fatal:!0});this.reportCameraFault(a,r)}reportCameraFault(t,r){this.cameraFaultReported||(this.cameraFaultReported=!0,this.emit(x(r,t)))}commitHistory(t,r){let n=this.config.history?.maxTurns;if(n===void 0)return;this.committedHistory.push({user:t,assistant:r});let a=this.committedHistory.length-n;a>0&&this.committedHistory.splice(0,a)}ttsFrames(t,r,n,a){return this.config.providers.tts.run({kind:"text",streamId:r,turnId:n,partial:!1,final:!0,metadata:{},text:t},{signal:a})}async*nearEndFrames(t,r,n){let a=0;for await(let i of t){if(n.aborted)return;let s=await this.config.transports?.aec.processNearEnd(i);s!==void 0&&(yield ie(s,{streamId:r,sequence:a++,metadata:{}}))}}emit(t){for(let r of this.listeners)r(t)}track(t){this.tasks.add(t),t.finally(()=>this.tasks.delete(t)).catch(()=>{})}serializeAdmission(t){let r=this.admissionTail.then(t,t);return this.admissionTail=r.catch(()=>{}),r}async interruptActiveTurn(t,r){let n=this.turnScopes.current();if(!(n===void 0||(this.emitTurnLatency(n),this.turnScopes.cancel()===void 0))){try{await this.config.transports?.output.flush()}catch(i){this.emit(x(this.envelope(t.streamId,t.turnId,t.metadata),E(i,{provider:"runtime"})))}this.emit(Gt(t,r))}}abortPendingAsr(t={}){for(let[r,n]of this.pendingAsrControllers)t.inputSessionIdentity!==void 0&&n.inputSessionIdentity!==t.inputSessionIdentity||(t.emitLatency!==!1?this.emitTurnLatency(n):this.turnTimings.delete(n.turnId),r.abort(),this.pendingAsrControllers.delete(r))}isCurrentSpeechGeneration(t){return t===this.speechGeneration}reserveTurnId(t){let r=t??this.generatedTurnId();if(r.trim().length===0)throw new h("turnId must not be empty",{fatal:!0});if(this.usedTurnIds.has(r))throw new h("turnId must be unique within an agent session",{fatal:!0});return this.usedTurnIds.add(r),r}generatedTurnId(){do this.turnCounter+=1;while(this.usedTurnIds.has(`turn-${this.turnCounter}`));return`turn-${this.turnCounter}`}beginTurnTiming(t,r){let n=Le({turnId:t,source:r,...this.config.now!==void 0?{now:this.config.now}:{}});return this.turnTimings.set(t,n),n}emitTurnLatency(t){let n=this.turnTimings.get(t.turnId)?.takeSnapshot();this.turnTimings.delete(t.turnId),n!==void 0&&this.emit(_t(this.envelope(t.streamId,t.turnId,this.agentMetadata),n))}envelope(t,r,n={}){return{streamId:t,...r!==void 0?{turnId:r}:{},partial:!1,final:!1,metadata:{...n}}}effectiveMetadata(t){try{let r=C(t??{});return C({...this.agentMetadata,...r})}catch(r){throw new h("Turn metadata must be JSON-compatible",{fatal:!0,cause:r})}}commitMessage(t,r,n,a){this.messageCounter+=1,this.messages.push({id:`message-${this.messageCounter}`,turnId:t,role:r,content:n,createdAt:(this.config.now??Date.now)(),metadata:C(a)})}};async function*jt(e,t,r){yield{kind:"llm",streamId:t,turnId:r,partial:!1,final:!0,metadata:{},text:e}}function Ge(e,t){return{...Ue(e),type:"transcript.final",partial:!1,final:!0,text:e.text,source:t}}function Dt(e,t){return{...Ue(e),type:"transcript.partial",partial:!0,final:!1,text:e.text,source:t}}function G(e,t){return{...e,type:t,partial:!1,final:!0}}function Vt(e,t,r){return{...e,type:"image.captured",partial:!1,final:!0,image:{mimeType:t.mimeType,width:t.width,height:t.height,sizeBytes:t.data.byteLength,captureMs:r}}}function _e(e,t,r){return{...e,type:t,partial:t==="reply.partial",final:t==="reply.final",text:r}}function Gt(e,t){return{...e,type:"interruption",partial:!1,final:!0,reason:t}}function _t(e,t){return{...e,type:"turn.latency",partial:!1,final:!0,latency:t}}function x(e,t){return{...e,type:"error",partial:!1,final:!0,error:t}}function Ue(e){return{streamId:e.streamId,...e.turnId!==void 0?{turnId:e.turnId}:{},...e.sequence!==void 0?{sequence:e.sequence}:{},partial:e.partial,final:e.final,...e.timestamp!==void 0?{timestamp:e.timestamp}:{},metadata:e.metadata,...e.frameId!==void 0?{frameId:e.frameId}:{}}}function B(e){return e instanceof Error&&e.name==="AbortError"}function Ut(e){let t=new AbortController,r=()=>t.abort();return e.aborted?(r(),{controller:t,unlink(){}}):(e.addEventListener("abort",r,{once:!0}),{controller:t,unlink(){e.removeEventListener("abort",r)}})}async function H(e,t={}){if(ue(e.channels,"channels"),ue(e.sourceSampleRate,"sourceSampleRate"),ue(e.targetSampleRate,"targetSampleRate"),e.sourceSampleRate===e.targetSampleRate)return new z(e);let r=await(t.load??Nt)(),n=Wt(r),a=await n.create(e.channels,e.sourceSampleRate,e.targetSampleRate,{converterType:n.ConverterType.SRC_SINC_FASTEST});return new z(e,a)}var z=class{constructor(t,r){this.converter=r;this.channels=t.channels,this.sourceSampleRate=t.sourceSampleRate,this.targetSampleRate=t.targetSampleRate}converter;channels;sourceSampleRate;targetSampleRate;destroyed=!1;simple(t){return this.assertUsable(t),this.converter?.simple(t)??t}full(t){return this.assertUsable(t),this.converter?.full(t)??t}destroy(){this.destroyed||(this.destroyed=!0,this.converter?.destroy())}assertUsable(t){if(this.destroyed)throw new Error("Resampler has been destroyed");if(t.length%this.channels!==0)throw new Error("Interleaved audio length must be divisible by channels")}};async function Nt(){return import("@alexanderolsen/libsamplerate-js")}function Wt(e){if(typeof e!="object"||e===null)throw new Error("libsamplerate module did not load as an object");let t=e,r=t.default??t;if(typeof r.create!="function"||typeof r.ConverterType?.SRC_SINC_FASTEST!="number")throw new Error("libsamplerate module has an incompatible API");return r}function ue(e,t){if(!Number.isFinite(e)||!Number.isInteger(e)||e<=0)throw new Error(`${t} must be a finite positive integer`)}var qt="https://eva-gateway-ali.dev.autoarkai.com",J={asr:"/v1/audio/transcriptions",llm:"/llm/v1/chat/completions",tts:"/v1/audio/speech"};function K(e){return`${qt}${e}`}function Bt(){return new DOMException("Operation aborted","AbortError")}function zt(e){if(e?.aborted===!0)throw Bt()}function Ne(e){let t=e.trim();if(t.startsWith("data:"))return t.slice(5).trimStart()}async function*$(e,t={}){let{signal:r,isTerminator:n}=t,a=new TextDecoder,i="",s=!1;for await(let c of e){if(zt(r),s)continue;i+=a.decode(c,{stream:!0});let d=i.indexOf(`
2
- `);for(;d>=0;){let p=i.slice(0,d);i=i.slice(d+1);let o=Ne(p);if(o!==void 0){if(n?.(o)===!0){s=!0,i="";break}yield o}d=i.indexOf(`
3
- `)}}if(s)return;i+=a.decode();let u=Ne(i);u!==void 0&&n?.(u)!==!0&&(yield u)}function de(e){return e==="[DONE]"}async function*Y(e){let t=e.getReader(),r=!1;try{for(;;){let{value:n,done:a}=await t.read();if(a===!0){r=!0;break}n!==void 0&&(yield n)}}finally{try{r||await t.cancel().catch(()=>{})}finally{t.releaseLock()}}}function We(e){let t=atob(e),r=new Uint8Array(t.length);for(let n=0;n<t.length;n+=1)r[n]=t.charCodeAt(n);return r}function le(e){try{return JSON.parse(e)}catch{return}}function Ht(e){try{return We(e)}catch{return}}function ce(e,t,r){return{kind:"asr",streamId:t.streamId,...t.turnId!==void 0?{turnId:t.turnId}:{},partial:!r,final:r,metadata:{},text:e}}function qe(e,t,r){return{kind:"llm",streamId:t.streamId,...t.turnId!==void 0?{turnId:t.turnId}:{},partial:!r,final:r,metadata:{},text:e}}function Be(e,t,r){return{kind:"tts.audio",streamId:t.streamId,...t.turnId!==void 0?{turnId:t.turnId}:{},partial:!r,final:r,metadata:{},audio:e,sampleRate:t.sampleRate,channels:t.channels}}async function*ze(e,t){let r="",n=!1;for await(let a of e){let i=le(a);i!==void 0&&(i.type==="transcript.text.delta"?(r+=i.delta??"",yield ce(r,t,!1)):i.type==="transcript.text.done"&&(yield ce(i.text??r,t,!0),n=!0))}n||(yield ce(r,t,!0))}async function*He(e,t){for await(let r of e){let n=le(r);if(n===void 0)continue;let a=n.choices?.[0]?.delta?.content;typeof a=="string"&&a.length>0&&(yield qe(a,t,!1))}yield qe("",t,!0)}async function*Je(e,t){let r,n=!1;for await(let a of e){if(n)continue;let i=le(a);if(i!==void 0)if(i.type==="speech.audio.delta"&&typeof i.audio=="string"){let s=Ht(i.audio);if(s===void 0)continue;r!==void 0&&(yield Be(r,t,!1)),r=s}else i.type==="speech.audio.done"&&(n=!0)}r!==void 0&&(yield Be(r,t,!0))}function Q(e){return e!==void 0?e:globalThis.fetch.bind(globalThis)}function X(e){return{Authorization:`Bearer ${e}`}}function Jt(e){return e instanceof DOMException&&e.name==="AbortError"}async function Z(e,t,r,n){let a;try{a=await e(t,r)}catch(i){throw Jt(i)?i:L(i,{provider:n})}if(!a.ok){let i;try{i=await a.text()}catch{i=void 0}throw L(i,{provider:n,statusCode:a.status})}return a}function ee(e,t){let r=e.body;if(r===null)throw L("empty response body",{provider:t});return r}var Kt=16e3,re=1;function $t(){return new DOMException("Operation aborted","AbortError")}function pe(e){if(e?.aborted===!0)throw $t()}function Yt(e){let t=e.reduce((a,i)=>a+i.length,0),r=new Uint8Array(t),n=0;for(let a of e)r.set(a,n),n+=a.length;return r}async function Qt(e,t,r){te(t.sampleRate,"ASR target sampleRate"),te(t.channels??re,"ASR fallback channels");let n=t.createResampler??H,a=[],i,s;for await(let d of e){if(pe(r),te(d.sampleRate,"ASR source sampleRate"),te(d.channels,"ASR source channels"),i===void 0)i=d.sampleRate,s=d.channels;else if(d.sampleRate!==i||d.channels!==s)throw new RangeError("ASR source sampleRate and channels must remain stable within an utterance");er(d.audio,d.channels),a.push(d.audio)}pe(r);let u=Yt(a);if(i===void 0||s===void 0)return{bytes:u,sampleRate:t.sampleRate,channels:t.channels??re};if(i===t.sampleRate)return{bytes:u,sampleRate:t.sampleRate,channels:s};let c=await n({channels:s,sourceSampleRate:i,targetSampleRate:t.sampleRate});try{let d=c.simple(Xt(u));if(d.length%s!==0)throw new RangeError("Resampled audio is not aligned to its channel count");return{bytes:Zt(d),sampleRate:t.sampleRate,channels:s}}finally{c.destroy()}}function Xt(e){let t=new Float32Array(e.byteLength/2),r=new DataView(e.buffer,e.byteOffset,e.byteLength);for(let n=0;n<t.length;n+=1)t[n]=r.getInt16(n*2,!0)/32768;return t}function Zt(e){let t=new Uint8Array(e.length*2),r=new DataView(t.buffer);for(let n=0;n<e.length;n+=1){let a=Math.max(-1,Math.min(1,e[n])),i=a<0?Math.round(a*32768):Math.round(a*32767);r.setInt16(n*2,i,!0)}return t}function er(e,t){if(e.byteLength%2!==0)throw new RangeError("ASR PCM16 frame must contain an even number of bytes");if(e.byteLength/2%t!==0)throw new RangeError("ASR PCM16 frame must align to its channel count")}function te(e,t){if(!Number.isFinite(e)||!Number.isInteger(e)||e<=0)throw new RangeError(`${t} must be a finite positive integer`)}function tr(e){return Symbol.asyncIterator in Object(e)}async function rr(e,t){if(tr(e)){let r="",n="tts",a;for await(let i of e)pe(t),r+=i.text,n=i.streamId,a=i.turnId;return a!==void 0?{text:r,streamId:n,turnId:a}:{text:r,streamId:n}}return e.turnId!==void 0?{text:e.text,streamId:e.streamId,turnId:e.turnId}:{text:e.text,streamId:e.streamId}}function nr(e){return e.map(t=>{if(Array.isArray(t.content)&&t.content.length===0)throw new h("Gateway LLM content parts must not be empty",{fatal:!0});let r=typeof t.content=="string"?t.content:t.content.map(a=>{if(a.type==="text")return{type:"text",text:a.text};if(a.data.byteLength===0||!/^image\/[a-z0-9.+-]+$/i.test(a.mimeType))throw new h("Gateway LLM image content is invalid",{fatal:!0});return{type:"image_url",image_url:{url:`data:${a.mimeType};base64,${ar(a.data)}`}}}),n={role:t.role,content:r};return t.name!==void 0&&(n.name=t.name),t.toolCallId!==void 0&&(n.tool_call_id=t.toolCallId),n})}function ar(e){let r="";for(let n=0;n<e.length;n+=32768)r+=String.fromCharCode(...e.subarray(n,n+32768));return btoa(r)}function me(e){let t=Q(e.fetch),r=X(e.apiKey);return{run(n,a){return(async function*(){let s=a?.signal,u;try{u=await Qt(n,e,s)}catch(f){throw f instanceof DOMException&&f.name==="AbortError"?f:E(f,{provider:"gateway-asr",message:"Gateway ASR audio preprocessing failed"})}let{bytes:c,sampleRate:d,channels:p}=u,o=new FormData;o.append("model",e.model),o.append("stream","true"),o.append("audio_format","pcm"),o.append("sample_rate",String(d)),o.append("channels",String(p)),e.hotwords!==void 0&&o.append("hotwords",e.hotwords),o.append("file",new Blob([c],{type:"application/octet-stream"}),"audio.pcm");let l={method:"POST",headers:r,body:o};s!==void 0&&(l.signal=s);let m=await Z(t,K(J.asr),l,"asr"),v=$(Y(ee(m,"asr")),{...s!==void 0?{signal:s}:{},isTerminator:de});yield*ze(v,{streamId:"speech"})})()}}}function fe(e){let t=Q(e.fetch),n={...X(e.apiKey),"Content-Type":"application/json"};return{run(a,i){return(async function*(){let u=i?.signal,c={model:e.model,stream:!0,messages:nr(a.messages)};e.temperature!==void 0&&(c.temperature=e.temperature),e.maxTokens!==void 0&&(c.max_tokens=e.maxTokens),e.topP!==void 0&&(c.top_p=e.topP);let d={method:"POST",headers:n,body:JSON.stringify(c)};u!==void 0&&(d.signal=u);let p=await Z(t,K(J.llm),d,"llm"),o=$(Y(ee(p,"llm")),{...u!==void 0?{signal:u}:{},isTerminator:de}),l=a.turnId!==void 0?{streamId:a.streamId,turnId:a.turnId}:{streamId:a.streamId};yield*He(o,l)})()}}}function he(e){let t=Q(e.fetch),n={...X(e.apiKey),"Content-Type":"application/json"};return{run(a,i){return(async function*(){let u=i?.signal,{text:c,streamId:d,turnId:p}=await rr(a,u),o=e.sampleRate??Kt,l={model:e.model,input:c,response_format:"pcm",stream_format:"sse",sample_rate:o};e.voice!==void 0&&(l.voice=e.voice),e.speed!==void 0&&(l.speed=e.speed),e.pitchRate!==void 0&&(l.pitch_rate=e.pitchRate);let m={method:"POST",headers:n,body:JSON.stringify(l)};u!==void 0&&(m.signal=u);let v=await Z(t,K(J.tts),m,"tts"),f=$(Y(ee(v,"tts")),{...u!==void 0?{signal:u}:{}});yield*Je(f,p!==void 0?{streamId:d,turnId:p,sampleRate:o,channels:re}:{streamId:d,sampleRate:o,channels:re})})()}}}import*as M from"onnxruntime-web";var ir=new URL("./assets/silero_vad_v6.onnx",import.meta.url).href;async function Ke(e={},t){let r=e.modelUrl??ir,n=await(e.modelFetcher??or)(r,t),a=await M.InferenceSession.create(n);return{async run(i){let s=await a.run({input:new M.Tensor("float32",i.input,[1,i.input.length]),state:new M.Tensor("float32",i.state,[2,1,128]),sr:new M.Tensor("int64",BigInt64Array.from([BigInt(i.sampleRate)]),[])}),u=s.output,c=s.stateN;if(u===void 0||c===void 0||u.type!=="float32"||c.type!=="float32"||!(u.data instanceof Float32Array)||!(c.data instanceof Float32Array)||u.data.length!==1||!sr(c.dims,[2,1,128]))throw new Error("Silero VAD v6 returned an invalid result");return ge({speechProbability:u.data[0],state:Float32Array.from(c.data)})}}}function ge(e){if(!Number.isFinite(e.speechProbability)||e.speechProbability<0||e.speechProbability>1||e.state.length!==256||!e.state.every(Number.isFinite))throw new Error("Silero VAD v6 returned an invalid result");return e}async function or(e,t){let r=await fetch(e,t!==void 0?{signal:t}:{});if(!r.ok)throw new Error("Silero VAD model fetch failed");return r.arrayBuffer()}function sr(e,t){return e.length===t.length&&e.every((r,n)=>r===t[n])}var $e=16e3,ye=512,ne=64;function be(e={}){return new Se(e)}var Se=class{constructor(t){this.options=t}options;run(t,r){return this.runFrames(t,r)}async*runFrames(t,r){let n=r?.signal,a=this.options.positiveSpeechThreshold??.5,i=this.options.negativeSpeechThreshold??.35,s=Math.max(1,Math.ceil((this.options.silenceThresholdMs??200)/32)),u=!1,c=0,d=new Float32Array(256),p=new Float32Array(ne),o,l,m,v=[];try{if(U(n))return;let f=await Ye(this.options.createSession!==void 0?this.options.createSession():Ke(this.options,n),n);for await(let g of t){if(U(n))return;if(o=g,l!==void 0&&g.sampleRate!==l)throw new RangeError("VAD source sampleRate cannot change within a run");l??=g.sampleRate,m??=await H({channels:1,sourceSampleRate:l,targetSampleRate:$e});let b=ur(g);for(v.push(...m.full(b));v.length>=ye;){let y=Float32Array.from(v.splice(0,ye)),S=new Float32Array(ne+ye);S.set(p),S.set(y,ne);let A=ge(await Ye(f.run({input:S,state:d,sampleRate:$e}),n));if(U(n))return;d=Float32Array.from(A.state),p=S.slice(S.length-ne),A.speechProbability>=a?(c=0,u||(u=!0,yield ve(g,"started",A.speechProbability))):u&&A.speechProbability<i?(c+=1,c>=s&&(u=!1,c=0,yield ve(g,"stopped",A.speechProbability))):u&&(c=0)}}u&&!U(n)&&o!==void 0&&(yield ve(o,"stopped"))}catch(f){if(U(n))return;throw E(f,{provider:"silero-vad"})}finally{m?.destroy()}}};function U(e){return e?.aborted===!0}function Ye(e,t){return t===void 0?e:t.aborted?Promise.reject(Qe()):new Promise((r,n)=>{let a=()=>{t.removeEventListener("abort",a),n(Qe())};t.addEventListener("abort",a,{once:!0}),e.then(i=>{t.removeEventListener("abort",a),r(i)},i=>{t.removeEventListener("abort",a),n(i)})})}function Qe(){let e=new Error("Operation aborted");return e.name="AbortError",e}function ur(e){if(!Number.isInteger(e.channels)||e.channels<=0)throw new RangeError("Audio frame channels must be positive");let t=e.channels*2;if(e.audio.byteLength%t!==0)throw new RangeError("Audio frame PCM must align to its channel count");let r=e.audio.byteLength/t,n=new Float32Array(r),a=new DataView(e.audio.buffer,e.audio.byteOffset,e.audio.byteLength);for(let i=0;i<r;i+=1){let s=0;for(let u=0;u<e.channels;u+=1)s+=a.getInt16((i*e.channels+u)*2,!0)/32768;n[i]=s/e.channels}return n}function ve(e,t,r){return{kind:"vad",streamId:e.streamId,...e.turnId!==void 0?{turnId:e.turnId}:{},partial:t==="started",final:t==="stopped",metadata:e.metadata,state:t,...r!==void 0?{confidence:r}:{}}}function Ae(e){return e.fetch!==void 0?{fetch:e.fetch}:{}}function Xe(e,t){return me({apiKey:t.apiKey,model:e.model,sampleRate:e.sampleRate,...Ae(t)})}function Ze(e,t){return fe({apiKey:t.apiKey,model:e.model,...e.temperature!==void 0?{temperature:e.temperature}:{},...e.maxTokens!==void 0?{maxTokens:e.maxTokens}:{},...Ae(t)})}function et(e,t){return he({apiKey:t.apiKey,model:e.model,...e.voice!==void 0?{voice:e.voice}:{},...e.speakingRate!==void 0?{speed:e.speakingRate}:{},...e.sampleRate!==void 0?{sampleRate:e.sampleRate}:{},...e.pitch!==void 0?{pitchRate:e.pitch}:{},...Ae(t)})}function tt(e,t){if(e===void 0)return;if(e.sensitivity!==void 0&&!(e.sensitivity>0&&e.sensitivity<=1))throw new h("VAD sensitivity must be within (0, 1]",{fatal:!0});if(e.silenceThresholdMs!==void 0&&(!Number.isFinite(e.silenceThresholdMs)||e.silenceThresholdMs<=0))throw new h("VAD silenceThresholdMs must be finite and greater than 0",{fatal:!0});let r=e.sensitivity??.5;return be({positiveSpeechThreshold:r,negativeSpeechThreshold:Math.max(0,r-.15),...e.silenceThresholdMs!==void 0?{silenceThresholdMs:e.silenceThresholdMs}:{},...t.createSileroSession!==void 0?{createSession:t.createSileroSession}:{}})}var dr=10,cr=1500,lr="\u8BF7\u7528\u4E00\u53E5\u7B80\u77ED\u3001\u81EA\u7136\u7684\u8BDD\u5411\u7528\u6237\u6253\u62DB\u547C\u3002";function Ee(e={}){return{create(t){let r={apiKey:t.apiKey,...e.fetch!==void 0?{fetch:e.fetch}:{},...e.createSileroSession!==void 0?{createSileroSession:e.createSileroSession}:{}},n=tt(t.vad,r);return{asr:Xe(t.asr,r),llm:Ze(t.llm,r),tts:et(t.tts,r),...n!==void 0?{vad:n}:{}}}}}function we(e,t){rt(e.asr.sampleRate,"ASR sampleRate"),e.tts.sampleRate!==void 0&&rt(e.tts.sampleRate,"TTS sampleRate");let r=pr(e.camera?.captureTimeoutMs),n=e.transports?.input!==void 0,a={apiKey:e.apiKey,asr:e.asr,tts:e.tts,llm:e.llm};e.vad!==void 0&&(a.vad=e.vad);let i=t.create(a);if(n&&i.vad===void 0)throw new h("Audio input requires a VAD provider",{fatal:!0});let s={systemPrompt:e.systemPrompt??"",greeting:fr(e.greeting),metadata:mr(e.metadata),camera:{captureTimeoutMs:r},providers:i};return e.history!==void 0&&(s.history={maxTurns:hr(e.history.maxTurns)}),e.transports!==void 0&&(s.transports=e.transports),s}function pr(e){let t=e??cr;if(!Number.isFinite(t)||!Number.isInteger(t)||t<=0)throw new h("Camera captureTimeoutMs must be a finite positive integer",{fatal:!0});return t}function mr(e){try{return C(e??{})}catch(t){throw new h("Agent metadata must be JSON-compatible",{fatal:!0,cause:t})}}function fr(e){if(e===void 0||e.mode==="disabled")return{mode:"disabled"};if(e.mode==="static"){if(e.text.trim().length===0)throw new h("Static greeting text must not be empty",{fatal:!0});return{mode:"static",text:e.text}}return{mode:"dynamic",prompt:e.prompt===void 0||e.prompt.trim().length===0?lr:e.prompt}}function hr(e){let t=e??dr;if(!Number.isInteger(t)||t<=0)throw new h("History maxTurns must be a positive integer",{fatal:!0});return t}function rt(e,t){if(!Number.isFinite(e)||!Number.isInteger(e)||e<=0)throw new h(`${t} must be a finite positive integer`,{fatal:!0})}function nt(e,t,r){return{...T(e),type:"transcript.final",final:!0,partial:!1,text:t,source:r}}function at(e,t){return{...T(e),type:"transcript.partial",final:!1,partial:!0,text:t,source:"speech"}}function it(e){return{...T(e),type:"speech.started",partial:!1,final:!0}}function ot(e,t){return{...T(e),type:"image.captured",partial:!1,final:!0,image:{...t}}}function st(e){return{...T(e),type:"speech.stopped",partial:!1,final:!0}}function ut(e,t){return{...T(e),type:"interruption",partial:!1,final:!0,reason:t}}function dt(e){return{...T(e),type:"reply.started",partial:!1,final:!0}}function ct(e,t){return{...T(e),type:"reply.partial",partial:!0,final:!1,text:t}}function lt(e,t){return{...T(e),type:"reply.final",partial:!1,final:!0,text:t}}function pt(e){return{...T(e),type:"playback.started",partial:!1,final:!0}}function mt(e){return{...T(e),type:"playback.stopped",partial:!1,final:!0}}function ft(e,t){return{...T(e),type:"turn.latency",partial:!1,final:!0,latency:t}}function ht(e,t){return{...e,type:"error",partial:!1,final:!0,error:gr(t)}}function T(e){if(e.turnId===void 0)throw new h("Runtime event is missing turn identity",{fatal:!0});return{...e,turnId:e.turnId}}function gr(e){return{message:e.message,fatal:e.fatal,source:e.source,...e.provider!==void 0?{provider:e.provider}:{},...e.statusCode!==void 0?{statusCode:e.statusCode}:{},...e.role!==void 0?{role:e.role}:{},...e.operation!==void 0?{operation:e.operation}:{},...e.reason!==void 0?{reason:e.reason}:{}}}function Ce(e){let t=new Set,r=new Map,n=e.onEvent(a=>{let i=vr(a,yr(r,a.streamId));if(i!==void 0)for(let s of t)s(i)});return{onEvent(a){return t.add(a),()=>{t.delete(a)}},close(){n(),t.clear()}}}function yr(e,t){let r=e.get(t)??0;return e.set(t,r+1),r}function vr(e,t){let r=Sr(e,t);switch(e.type){case"speech.started":return it(r);case"image.captured":return ot(r,e.image);case"speech.stopped":return st(r);case"transcript.partial":return at(r,e.text);case"transcript.final":return nt(r,e.text,e.source);case"interruption":return ut(r,e.reason);case"reply.started":return dt(r);case"reply.partial":return ct(r,e.text);case"reply.final":return lt(r,e.text);case"playback.started":return pt(r);case"playback.stopped":return mt(r);case"turn.latency":return ft(r,e.latency);case"error":return ht(r,e.error);default:return}}function Sr(e,t){return{streamId:e.streamId,...e.turnId!==void 0?{turnId:e.turnId}:{},sequence:t,partial:e.partial,final:e.final,...e.timestamp!==void 0?{timestamp:e.timestamp}:{},metadata:br(e.metadata),...e.frameId!==void 0?{frameId:e.frameId}:{}}}var gt=/(api.?key|authorization|headers?|raw|body|sse|pcm|provider.?object|secret|token|credential|password|cookies?)/i,I=Symbol("unsafe-metadata");function br(e){let t={};for(let[r,n]of Object.entries(e)){if(gt.test(r))continue;let a=Te(n,new Set);a!==I&&(t[r]=a)}return t}function Te(e,t){if(e===null||typeof e=="string"||typeof e=="boolean")return e;if(typeof e=="number")return Number.isFinite(e)?e:I;if(typeof e!="object"||t.has(e))return I;t.add(e);try{if(Array.isArray(e)){let n=[];for(let a of e){let i=Te(a,t);if(i===I)return I;n.push(i)}return n}if(Object.getPrototypeOf(e)!==Object.prototype&&Object.getPrototypeOf(e)!==null)return I;let r={};for(let[n,a]of Object.entries(e)){if(gt.test(n))continue;let i=Te(a,t);if(i===I)return I;r[n]=i}return r}finally{t.delete(e)}}function yt(e){return Ar(e,Ee())}function Ar(e,t){let r=we(e,t);return Er(new _(r))}function Er(e){let t=Ce(e),r="created",n=!1,a,i,s=new Set,u=(d,p,o=!0)=>{let l;return l=(async()=>{try{if(await d(),n)throw R("Agent media control was cancelled by stop")}catch(m){throw n?R("Agent media control was cancelled by stop"):o&&m instanceof h?m:new h(p,{cause:m})}finally{s.delete(l)}})(),s.add(l),l};return{start(){if(n||r==="stopped")return Promise.reject(R("Agent is stopped"));if(a!==void 0)return a;let d=(async()=>{try{if(await e.start(),n)throw R("Agent start was cancelled by stop");r="running"}catch(p){if(n)throw R("Agent start was cancelled by stop");try{await e.stop()}catch{}throw r="created",a=void 0,p instanceof h?p:w(p,{message:"Agent start failed"})}})();return a=d,d},submitText(d,p){if(n||r!=="running")return Promise.reject(R(r==="created"?"Agent has not started":"Agent is stopped"));let o;try{o=p===void 0?void 0:wr(p)}catch(l){return Promise.reject(w(l,{message:"Turn metadata must be JSON-compatible"}))}return e.submitText(d,o).catch(l=>{throw w(l,{message:"Agent text submission failed"})})},setAudioInputEnabled(d){return n||r==="stopped"?Promise.reject(R("Agent is stopped")):u(()=>e.setAudioInputEnabled(d),"Agent audio input update failed",!1)},setCameraCaptureEnabled(d){return n||r==="stopped"?Promise.reject(R("Agent is stopped")):u(()=>e.setCameraCaptureEnabled(d),"Agent camera capture update failed")},setTtsEnabled(d){return n||r==="stopped"?Promise.reject(R("Agent is stopped")):u(()=>e.setSkipTts(!d),"Agent TTS update failed")},getMessages(){return e.getMessages()},onEvent(d){if(n||r==="stopped")throw R(r==="stopped"?"Agent is stopped":"Agent is stopping");let p=t.onEvent(d),o=!0;return()=>{o&&(o=!1,p())}},stop(){if(i!==void 0)return i;n=!0;let d=a,p=[...s],o=(async()=>{let l;try{try{await e.stop()}catch(m){l=m}if(await Promise.allSettled([...d===void 0?[]:[d],...p]),l!==void 0)throw w(l,{message:"Agent stop failed"})}finally{t.close(),r="stopped"}})();return i=o,o}}}function wr(e){return{...e.turnId!==void 0?{turnId:e.turnId}:{},...e.metadata!==void 0?{metadata:C(e.metadata)}:{}}}function R(e){return new h(e,{fatal:!0})}export{h as EvaSdkError,yt as createEvaVoiceDialogueAgent};
1
+ function C(e){if(!Le(e))throw new TypeError("Metadata must be a JSON-compatible object");return Me(e,new Set)}function ke(e,t){if(e===null||typeof e=="boolean"||typeof e=="string")return e;if(typeof e=="number"){if(!Number.isFinite(e))throw new TypeError("Metadata numbers must be finite");return e}if(Array.isArray(e))return Oe(e,t,()=>e.map(r=>ke(r,t)));if(Le(e))return Me(e,t);throw new TypeError("Metadata must contain only JSON-compatible values")}function Me(e,t){return Oe(e,t,()=>{if(Reflect.ownKeys(e).some(n=>typeof n!="string"))throw new TypeError("Metadata object keys must be strings");let r={};for(let[n,a]of Object.entries(e))r[n]=ke(a,t);return r})}function Oe(e,t,r){if(t.has(e))throw new TypeError("Metadata must not contain cycles");t.add(e);try{return r()}finally{t.delete(e)}}function Le(e){if(typeof e!="object"||e===null||Array.isArray(e))return!1;let t=Object.getPrototypeOf(e);return t===Object.prototype||t===null}var g=class extends Error{fatal;source="sdk";constructor(t,r={}){super(t,{cause:r.cause}),this.name="EvaSdkError",this.fatal=r.fatal??!0}},B=class extends g{provider;source="provider";constructor(t,r){super(t,r),this.name="StageProviderError",this.provider=r.provider}},z=class extends g{provider;statusCode;source="gateway";constructor(t,r){super(t,r),this.name="GatewayAccessError",this.provider=r.provider,r.statusCode!==void 0&&(this.statusCode=r.statusCode);let n=k(r.traceId);n!==void 0&&(this.traceId=n)}},D=class extends g{role;operation;reason;source="media";constructor(t,r){super(t,r),this.name="MediaIoError",this.role=r.role,this.operation=r.operation,this.reason=r.reason}};function w(e,t={}){return e instanceof g?e:new g(t.message??"SDK operation failed",{fatal:t.fatal??!0,cause:e})}function E(e,t){return e instanceof g?e:new B(t.message??"Stage provider failed",{provider:t.provider,fatal:t.fatal??!0,cause:e})}function F(e,t){if(e instanceof g)return e;let r={provider:t.provider,fatal:t.fatal??!0,cause:e};t.statusCode!==void 0&&(r.statusCode=t.statusCode);let n=k(t.traceId);return n!==void 0&&(r.traceId=n),new z(t.message??Ct(t.statusCode,De(t.gatewayType)),r)}function V(e){let t=e;if(typeof e=="string")try{t=JSON.parse(e)}catch{return}if(!je(t))return;let r=Object.hasOwn(t,"error")?t.error:t;if(je(r))return De(r.type)}function k(e){return typeof e=="string"&&/^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$/.test(e)?e:void 0}function Ct(e,t){let r=t===void 0?"":`: ${t}`;return e===void 0?`Gateway request failed${r}`:`Gateway request failed with status ${e}${r}`}function De(e){return typeof e=="string"&&/^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/.test(e)?e:void 0}function je(e){return e!==null&&typeof e=="object"}var xt=new Set(["pcm_s16le"]);function se(e,t){if(!xt.has(e.format))throw new g("Unsupported audio format",{fatal:!0});return{kind:"audio.input",streamId:t.streamId,...t.turnId!==void 0?{turnId:t.turnId}:{},...t.sequence!==void 0?{sequence:t.sequence}:{},partial:!0,final:!1,...t.timestamp!==void 0?{timestamp:t.timestamp}:{},metadata:t.metadata??{},audio:e.data,sampleRate:e.sampleRate,channels:e.channels}}function ue(e){return{data:e.audio,sampleRate:e.sampleRate,channels:e.channels,format:"pcm_s16le"}}function Ve(){let e,t=()=>{let r=e;if(r!==void 0)return e=void 0,r.controller.abort(),r};return{begin(r){t();let n=new AbortController;return e={...r,controller:n,signal:n.signal},e},current(){return e},cancel:t,complete(r){return e!==r?!1:(e=void 0,!0)},isCurrent(r){return e===r&&!r.signal.aborted},stop:t}}function Ge(){let e="";return{push(t){if(t.length===0)return[];e+=t;let r=Ft(e);return e=r.rest,r.sentences},flush(){let t=e.trim();return e="",t.length===0?[]:[t]},clear(){e=""}}}var Rt=new Set(["\u3002","\uFF01","\uFF1F","!","?","\uFF1B",";","\u2026"]),It=new Set(['"',"'","\u201D","\u2019",")","\uFF09","]","\u3011","}","\u300B","\u300D","\u300F"]),Pt=/(?:Mr|Mrs|Ms|Dr|Prof|Sr|Jr|St|vs|etc|e\.g|i\.e)\.$/i;function Ft(e){let t=[],r=0,n=0;for(;n<e.length;){if(!kt(e,n)){n+=1;continue}let a=n+1;for(;a<e.length&&It.has(e[a]);)a+=1;let i=a;for(;i<e.length&&/\s/u.test(e[i]);)i+=1;if(i>=e.length)break;let o=e.slice(r,a).trim();o.length>0&&t.push(o),r=i,n=i}return{sentences:t,rest:e.slice(r)}}function kt(e,t){let r=e[t];if(Rt.has(r))return!(r==="\u2026"&&e[t+1]==="\u2026");if(r!==".")return!1;let n=e[t-1],a=e[t+1];return n!==void 0&&a!==void 0&&/\d/u.test(n)&&/\d/u.test(a)||a==="."?!1:!Pt.test(e.slice(0,t+1))}function _e(e){let t=e,r=0,n=()=>{let s,l=new Promise(m=>{s=m});return{id:r,queue:[],invalidated:l,invalidate:s,currentController:void 0,pump:void 0}},a=n(),i=!1,o,u=()=>{let s=a;r+=1,s.queue.length=0,s.currentController?.abort(),s.invalidate(),a=n()},c=()=>{i=!0,u()};t.parentSignal.aborted?c():t.parentSignal.addEventListener("abort",c,{once:!0});let d=s=>{s.pump!==void 0||s.queue.length===0||(s.pump=p(s).catch(l=>{s===a&&(o=l,i=!0,u())}).finally(()=>{s.pump=void 0,s===a&&s.queue.length>0&&d(s)}))};async function p(s){for(;s.queue.length>0&&!t.parentSignal.aborted;){let l=s.queue.shift(),m=new AbortController;s.currentController=m;let S={signal:m.signal,isCurrent:()=>!t.parentSignal.aborted&&!m.signal.aborted&&s===a&&s.id===r};try{await t.process(l,S)}finally{s.currentController===m&&(s.currentController=void 0)}}}return{enqueue(s){i||t.parentSignal.aborted||s.trim().length===0||(a.queue.push(s),d(a))},clearAndAbort:u,async close(){i=!0;let s=a,l=s.pump;if(l!==void 0&&await Promise.race([l,s.invalidated]),t.parentSignal.removeEventListener("abort",c),o!==void 0)throw o}}}function Ue(e){let t=e,r=!1,n;return{start(){r||n!==void 0||(t.onStarted(),r=!0)},async close(){if(!r){n!==void 0&&await n;return}r=!1,n=Promise.resolve(t.onStopped()).finally(()=>{n=void 0}),await n},isActive(){return r}}}function Ne(e){let t=e,r=t.now??Ot,n=r(),a,i,o=t.source==="text"||t.source==="greeting"?n:void 0,u,c,d,p=!1,s={},l=()=>{let m=t.source==="text"?0:s.vadMs??G(n,a),S=t.source==="text"?0:s.asrMs??G(i??a,o),f=s.llmFirstTokenMs??G(o,u),h=s.ttsFirstAudioMs??G(u,c),b=s.playbackMs??G(c,d),y={};_(y,"vadMs",m),_(y,"asrMs",S),_(y,"llmFirstTokenMs",f),_(y,"ttsFirstAudioMs",h),_(y,"playbackMs",b),Object.freeze(y);let v=[m,S,f,h],A=v.every(P=>P!==void 0)?v.reduce((P,oe)=>P+oe,0):void 0;return Object.freeze({turnId:t.turnId,...A!==void 0?{totalMs:A}:{},stages:y})};return{markVadStarted(){a??=r()},markAsrStarted(){i??=r()},markAsrFinal(){o??=r()},markLlmFirstToken(){u??=r()},markTtsFirstAudio(){c??=r()},markPlaybackStarted(){d??=r()},recordStageMetadata(m,S){let f=Mt[m],h=S[f];typeof h=="number"&&Number.isFinite(h)&&h>=0&&(s[f]=Math.round(h))},snapshot:l,takeSnapshot(){if(p)return;let m=l();if(Object.keys(m.stages).length!==0)return p=!0,m}}}var Mt={vad:"vadMs",asr:"asrMs",llm:"llmFirstTokenMs",tts:"ttsFirstAudioMs"};function Ot(){return typeof performance>"u"?Date.now():performance.now()}function G(e,t){if(!(e===void 0||t===void 0))return Math.max(0,Math.round(t-e))}function _(e,t,r){r!==void 0&&(e[t]=r)}function qe(e,t={}){let r=Lt(t.preSpeechMs),n=jt(t.maxUtteranceMs),a=[],i=[],o=new Set,u=0,c,d=0,p=0,s=!1,l,m=()=>{for(let f of o)f();o.clear()},S=f=>{l??=new g(f,{fatal:!0}),s=!0,c=void 0,i.length=0,m()};return{async*vadAudio(){try{for await(let f of e){if(l!==void 0)throw l;let h=We(f);for(a.push(f),u+=h;a.length>1;){let b=a[0],y=We(b);if(u-y<r)break;a.shift(),u-=y}if(c!==void 0&&(c.frames.push(f),p+=h,p>n))throw S("VAD utterance exceeded max duration"),l;yield f}}catch(f){throw l??=f,s=!0,m(),f}},start(f,h=d+1){d=h,c={turnId:f,generation:h,frames:[...a]},p=u},stop(){c!==void 0&&c.frames.length>0&&(i.length=0,i.push(c)),c=void 0,p=0,a.length=0,u=0,m()},async*utterances(){for(;;){let f=i.pop();if(i.length=0,f!==void 0){yield{turnId:f.turnId,generation:f.generation,audio:Dt(f.frames)};continue}if(l!==void 0)throw l;if(s)return;await new Promise(h=>o.add(h))}},close(){s||(s=!0,c=void 0,m())}}}function Lt(e){return Number.isFinite(e)&&e!==void 0&&e>=0?e:200}function jt(e){return Number.isFinite(e)&&e!==void 0&&e>0?e:6e4}function We(e){return e.sampleRate<=0||e.channels<=0?0:e.audio.byteLength/2/e.channels/e.sampleRate*1e3}async function*Dt(e){for(let t of e)yield t}var Vt=1500,U=class extends Error{turnId;generation;constructor(t,r,n){super("Camera capture cancellation failed",{cause:t}),this.name="CameraCaptureSettlementError",this.turnId=r,this.generation=n,Object.defineProperty(this,"mediaError",{value:t,enumerable:!1,configurable:!1,writable:!1})}},H=class{source;now;settlementDeadlineMs;onFault;controlTail=Promise.resolve();acceptedControl=Promise.resolve();stopOperation;running=!1;stopping=!1;acceptedEnabled=!1;acceptedRequestId=0;active=!1;sessionIdentity=0;sessionController;pendingStart;pendingCapture;fault;constructor(t){this.source=t.source,this.now=t.now??Date.now,this.settlementDeadlineMs=t.cancellationSettlementDeadlineMs??Vt,this.onFault=t.onFault}isActive(){return this.active&&!this.stopping&&this.fault===void 0}currentFault(){return this.fault}setEnabled(t){if(this.stopping)return Promise.reject(this.faultedControlError(t?"start":"stop"));if(this.fault!==void 0)return Promise.reject(this.faultedControlError(t?"start":"stop"));if(this.acceptedEnabled===t)return this.acceptedControl;this.acceptedEnabled=t;let r=++this.acceptedRequestId;if(t||this.abortPendingWork(),!this.running)return this.acceptedControl=Promise.resolve(),this.acceptedControl;let n=this.enqueueControl(()=>this.applyEnabled(t,r));return this.acceptedControl=n.catch(a=>{throw this.acceptedRequestId===r&&(this.acceptedEnabled=!1),a}),this.acceptedControl}async startRuntime(){if(!this.running&&(this.running=!0,this.stopping=!1,!!this.acceptedEnabled))try{this.acceptedControl=this.enqueueControl(()=>this.applyEnabled(!0,this.acceptedRequestId)),await this.acceptedControl}catch(t){throw this.acceptedEnabled=!1,t}}async stopRuntime(){if(this.stopping)return this.stopOperation??Promise.resolve();this.stopping=!0,this.running=!1,this.acceptedEnabled=!1,this.abortPendingWork();let t=this.enqueueControl(async()=>{let r=this.source;if(r!==void 0)try{await this.stopSourceAfterCaptureSettlement(r,!0)}catch(n){throw this.markFault("stop","operation_failed",n)}finally{this.active=!1,this.sessionController=void 0}});return this.stopOperation=t,t}async beginCapture(t,r,n){if(await this.cancelPendingCaptureAndWait(),!this.isActive()||this.source===void 0)return;let a=new AbortController,i=_t(),o=this.now(),u={turnId:t,generation:r},d=Promise.resolve().then(()=>this.source.capture(a.signal)).then(p=>{if(!(a.signal.aborted||i.settled))try{Gt(p),O(i,{status:"success",snapshot:p,captureMs:Math.max(0,this.now()-o)})}catch(s){O(i,{status:"failure",error:M("capture","invalid_data",!1,s)})}},p=>{a.signal.aborted||i.settled||O(i,{status:"failure",error:M("capture",Be(p),!1,p)})}).finally(()=>{u.timeoutHandle!==void 0&&clearTimeout(u.timeoutHandle),this.pendingCapture===u&&(this.pendingCapture=void 0)});return Object.assign(u,{controller:a,result:i,settlement:d}),u.timeoutHandle=setTimeout(()=>{i.settled||(a.abort(),O(i,{status:"failure",error:M("capture","timeout",!1)}),this.watchCaptureSettlement(u))},n),this.pendingCapture=u,{turnId:t,generation:r,result:i.promise}}async cancelPendingCaptureAndWait(){if(this.fault!==void 0)throw this.faultedControlError("capture");let t=this.pendingCapture;if(t!==void 0){t.controller.abort(),O(t.result,{status:"cancelled"});try{await this.settleWithin(t.settlement)}catch(r){let n=this.markFault("capture","operation_failed",r);throw new U(n,t.turnId,t.generation)}}}enqueueControl(t){let r=this.controlTail.then(t,t);return this.controlTail=r.catch(()=>{}),r}async applyEnabled(t,r){if(this.fault!==void 0)throw this.faultedControlError(t?"start":"stop");t?await this.startSession(r):await this.stopSession()}async startSession(t){if(this.active)return;let r=this.source;if(r===void 0)throw M("start","not_configured",!1);let n=++this.sessionIdentity,a=new AbortController;this.sessionController=a;let i=Promise.resolve().then(()=>r.start(a.signal)),o={controller:a,settlement:i.then(()=>{})};this.pendingStart=o;try{if(await Ut(i,a.signal),a.signal.aborted||n!==this.sessionIdentity||this.stopping)throw de();this.active=!0}catch(u){throw this.active=!1,Nt(u)||a.signal.aborted?u:M("start",Be(u),!1,u)}finally{o.settlement.finally(()=>{this.pendingStart===o&&(this.pendingStart=void 0)}).catch(()=>{})}}async stopSession(){this.active=!1,this.abortPendingWork();let t=this.source;if(t!==void 0)try{await this.stopSourceAfterCaptureSettlement(t,!1),this.sessionController=void 0}catch(r){throw this.markFault("stop","operation_failed",r)}}async stopSourceAfterCaptureSettlement(t,r){let n=this.pendingStart?.settlement??Promise.resolve(),a=this.pendingCapture?.settlement??Promise.resolve();try{await this.settleWithin(a.catch(()=>{}))}catch(i){throw r&&await this.settleWithin(Promise.all([n.catch(()=>{}),Promise.resolve().then(()=>t.stop())])).catch(()=>{}),i}await this.settleWithin(Promise.all([n.catch(()=>{}),Promise.resolve().then(()=>t.stop())]).then(()=>{}))}abortPendingWork(){this.sessionController?.abort(),this.pendingStart?.controller.abort();let t=this.pendingCapture;t!==void 0&&(t.controller.abort(),O(t.result,{status:"cancelled"}))}async watchCaptureSettlement(t){try{await this.settleWithin(t.settlement)}catch(r){let n=this.markFault("capture","operation_failed",r);this.onFault?.(n,t.turnId,t.generation)}}settleWithin(t){return new Promise((r,n)=>{let a=setTimeout(()=>{n(new Error("Camera cancellation settlement deadline exceeded"))},this.settlementDeadlineMs);t.then(()=>{clearTimeout(a),r()},i=>{clearTimeout(a),n(i)})})}markFault(t,r,n){return this.fault===void 0&&(this.fault=M(t,r,!0,n)),this.active=!1,this.fault}faultedControlError(t){return M(t,"operation_failed",!0,this.fault)}};function Gt(e){if(!(e.data instanceof Uint8Array)||e.data.byteLength===0)throw new Error("Camera snapshot bytes are empty");if(!/^image\/[a-z0-9.+-]+$/i.test(e.mimeType))throw new Error("Camera snapshot MIME is invalid");if(!Number.isInteger(e.width)||e.width<=0)throw new Error("Camera snapshot width is invalid");if(!Number.isInteger(e.height)||e.height<=0)throw new Error("Camera snapshot height is invalid")}function M(e,t,r,n){return new D("Camera operation failed",{role:"camera",operation:e,reason:t,fatal:r,cause:n})}function Be(e){let t=e instanceof Error?e.name:"";return t==="NotAllowedError"||t==="SecurityError"?"permission_denied":t==="NotFoundError"||t==="NotReadableError"||t==="OverconstrainedError"?"device_unavailable":t==="NotSupportedError"?"unsupported":"operation_failed"}function _t(){let e,t;return{promise:new Promise((n,a)=>{e=n,t=a}),resolve(n){e(n)},reject(n){t(n)},settled:!1}}function O(e,t){e.settled||(e.settled=!0,e.resolve(t))}function Ut(e,t){return t.aborted?Promise.reject(de()):new Promise((r,n)=>{let a=()=>n(de());t.addEventListener("abort",a,{once:!0}),e.then(r,n).finally(()=>{t.removeEventListener("abort",a)}).catch(()=>{})})}function de(){return new DOMException("Camera operation aborted","AbortError")}function Nt(e){return e instanceof Error&&e.name==="AbortError"}var W=class{constructor(t){this.config=t;this.agentMetadata=C(t.metadata??{}),this.cameraController=new H({...t.transports?.camera!==void 0?{source:t.transports.camera}:{},...t.now!==void 0?{now:t.now}:{},onFault:(r,n)=>{this.reportCameraFault(r,this.envelope("speech",n,this.agentMetadata))}})}config;listeners=new Set;tasks=new Set;pendingAsrControllers=new Map;turnScopes=Ve();cameraController;cameraCaptures=new Map;cameraFaultReported=!1;admissionTail=Promise.resolve();inputControlTail=Promise.resolve();rootController;started=!1;stopping=!1;inputEnabled=!0;inputSessionCounter=0;inputSession;speechGeneration=0;turnCounter=0;skipTts=!1;activeTtsTurn;committedHistory=[];turnTimings=new Map;messages=[];usedTurnIds=new Set;agentMetadata;messageCounter=0;onEvent(t){return this.listeners.add(t),()=>{this.listeners.delete(t)}}async start(){if(this.started)return;this.started=!0,this.rootController=new AbortController,this.inputEnabled&&this.canRunSpeechInput()&&await this.serializeInputControl(()=>this.reconcileInputSession());try{await this.cameraController.startRuntime()}catch(r){this.emit(R(this.envelope("camera",void 0,this.agentMetadata),w(r,{message:"Camera session failed",fatal:!1})))}let t=this.config.greeting;t!==void 0&&t.mode!=="disabled"&&this.track(this.scheduleGreeting(t))}async stop(){this.stopping=!0,this.inputEnabled=!1;let t=this.turnScopes.current();t!==void 0&&this.emitTurnLatency(t),this.rootController?.abort(),this.speechGeneration+=1,this.abortPendingAsr({emitLatency:!1});let r=this.inputSession===void 0?Promise.resolve():this.releaseInputSession(this.inputSession),n=this.cameraController.stopRuntime();this.turnScopes.stop();let a;try{await r}catch(i){a=i}try{await n}catch(i){a??=i}try{await this.config.transports?.output.stop()}catch(i){a=i}try{await this.config.transports?.aec.release()}catch(i){a??=i}if(a!==void 0)throw w(a,{message:"Dialogue runtime stop failed"})}async drain(){for(;this.tasks.size>0;)await Promise.allSettled([...this.tasks])}getMessages(){return this.messages.map(t=>({...t,metadata:C(t.metadata)}))}scheduleGreeting(t){let r=this.reserveTurnId(),n="greeting",a=this.envelope(n,r,this.agentMetadata);return this.beginTurnTiming(r,"greeting"),this.serializeAdmission(async()=>{if(this.rootController?.signal.aborted===!0)return;let i=this.turnScopes.begin({streamId:n,turnId:r});this.track(this.runAssistantTurn(n,r,t.mode==="dynamic"?t.prompt:t.text,a,i,{commitHistory:!1,...t.mode==="static"?{staticReply:t.text}:{},messageMetadata:this.agentMetadata,recordAssistant:!0}))})}async submitText(t,r={}){if(t.trim().length===0)return;this.started||await this.start();let n=this.reserveTurnId(r.turnId),a="manual-text",i=this.effectiveMetadata(r.metadata),o=this.envelope(a,n,i),u={kind:"text",streamId:a,turnId:n,partial:!1,final:!0,metadata:i,text:t};this.beginTurnTiming(n,"text"),await this.serializeAdmission(async()=>{this.speechGeneration+=1,this.abortPendingAsr(),await this.cancelCameraCaptureWithoutBlocking(o),await this.interruptActiveTurn(o,"manual_text"),this.commitMessage(n,"user",t,i),this.emit(ze(u,"text"));let c=this.turnScopes.begin({streamId:a,turnId:n});this.track(this.runAssistantTurn(a,n,t,o,c,{messageMetadata:i}))})}async setSkipTts(t){if(this.skipTts===t||(this.skipTts=t,!t))return;let r=this.activeTtsTurn;if(!(r===void 0||!this.turnScopes.isCurrent(r.scope))){r.aggregator.clear(),r.worker.clearAndAbort();try{await this.config.transports?.output.flush(),this.activeTtsTurn===r&&this.turnScopes.isCurrent(r.scope)&&await r.playback.close()}catch(n){if(this.activeTtsTurn===r&&this.turnScopes.isCurrent(r.scope)){let a=E(n,{provider:"runtime"});throw this.emit(R(r.base,a)),a}throw E(n,{provider:"runtime"})}}}async setAudioInputEnabled(t){if(this.stopping)throw new g("Dialogue runtime is stopped",{fatal:!0});if(this.inputEnabled===t)return this.inputControlTail;if(this.inputEnabled=t,t||(this.speechGeneration+=1,this.abortPendingAsr({emitLatency:!1}),await this.cancelCameraCaptureWithoutBlocking(this.envelope("camera",void 0,this.agentMetadata)),this.inputSession!==void 0&&(this.inputSession.controller.abort(),this.releaseInputSession(this.inputSession).catch(()=>{}))),!!this.started)return this.serializeInputControl(()=>this.reconcileInputSession())}async setCameraCaptureEnabled(t){if(this.stopping)throw new g("Dialogue runtime is stopped",{fatal:!0});await this.cameraController.setEnabled(t)}serializeInputControl(t){let r=this.inputControlTail.then(t,t);return this.inputControlTail=r.catch(()=>{}),r}async reconcileInputSession(){let t=this.inputSession;if(!this.started||this.stopping||!this.inputEnabled||!this.canRunSpeechInput()){t!==void 0&&await this.releaseInputSession(t);return}t!==void 0&&!t.controller.signal.aborted||(t!==void 0&&await this.releaseInputSession(t),!(this.stopping||!this.inputEnabled||!this.canRunSpeechInput())&&await this.startInputSession())}async startInputSession(){let t=this.config.transports;if(t===void 0||this.config.providers.vad===void 0)return;let r={identity:++this.inputSessionCounter,controller:new AbortController};this.inputSession=r;let n=t.input;try{if(await n.start(),!this.isCurrentInputSession(r)){await this.releaseInputSession(r);return}let a=n.frames(r.controller.signal),i=this.runSpeechLoop(r.controller.signal,a,r.identity);this.track(i),i.finally(()=>{this.isCurrentInputSession(r)&&!r.controller.signal.aborted&&this.releaseInputSession(r).catch(()=>{})}).catch(()=>{})}catch(a){let i=r.controller.signal.aborted||!this.inputEnabled||this.stopping;if(await this.releaseInputSession(r),i)return;throw this.inputEnabled=!1,w(a,{message:"Audio input session failed"})}}releaseInputSession(t){if(t.releasePromise!==void 0)return t.releasePromise;t.controller.abort();let r=this.inputSession===t,n=this.config.transports?.input;return t.releasePromise=(async()=>{try{r&&await n?.stop()}catch(a){throw w(a,{message:"Audio input release failed"})}finally{this.inputSession===t&&(this.inputSession=void 0)}})(),t.releasePromise}isCurrentInputSession(t){return this.inputSession?.identity===t.identity&&!t.controller.signal.aborted&&this.inputEnabled&&!this.stopping}canRunSpeechInput(){return this.config.transports!==void 0&&this.config.providers.vad!==void 0}async runSpeechLoop(t,r,n){let a=this.config.transports,i=this.config.providers.vad;if(a===void 0||i===void 0)return;let o="speech",u,c,d=new AbortController,p=()=>d.abort();t.aborted?p():t.addEventListener("abort",p,{once:!0});let s=d.signal;try{let l=this.nearEndFrames(r,o,s),m=qe(l),S=(async()=>{try{for await(let y of i.run(m.vadAudio(),{signal:s})){if(s.aborted)return;y.state==="started"?await this.serializeAdmission(async()=>{if(s.aborted)return;this.speechGeneration+=1,c=this.speechGeneration,u=this.reserveTurnId();let v=this.beginTurnTiming(u,"speech");v.recordStageMetadata("vad",y.metadata),v.markVadStarted(),this.abortPendingAsr(),m.start(u,c);let A=this.envelope(o,u,this.agentMetadata);if(await this.interruptActiveTurn(A,"user_speech"),s.aborted||c!==this.speechGeneration)return;this.emit(N(A,"speech.started"));let P;try{P=await this.cameraController.beginCapture(u,c,this.config.camera?.captureTimeoutMs??1500)}catch(oe){this.reportCameraFaultFromUnknown(oe,A,"Camera capture failed")}P!==void 0&&this.cameraCaptures.set(u,this.settleCameraCapture(P,A))}):y.state==="stopped"&&u!==void 0&&c!==void 0&&(m.stop(),this.emit(N(this.envelope(o,u,this.agentMetadata),"speech.stopped")),u=void 0,c=void 0)}}catch(y){let v=s.aborted||t.aborted;throw p(),this.abortPendingAsr({emitLatency:!v,inputSessionIdentity:n}),v||this.turnScopes.cancel(),y}finally{m.close()}})(),f=(async()=>{try{for await(let y of m.utterances()){if(s.aborted)return;if(!this.isCurrentSpeechGeneration(y.generation))continue;let v=Jt(s);this.pendingAsrControllers.set(v.controller,{streamId:o,turnId:y.turnId,inputSessionIdentity:n});let A=!1;try{A=await this.runAsr(y.audio,o,y.turnId,this.envelope(o,y.turnId,this.agentMetadata),y.generation,v.controller.signal)}finally{this.pendingAsrControllers.delete(v.controller),v.unlink()}!A&&this.isCurrentSpeechGeneration(y.generation)&&!s.aborted&&this.emitTurnLatency({streamId:o,turnId:y.turnId})}}catch(y){let v=s.aborted||t.aborted;throw p(),this.abortPendingAsr({emitLatency:!v,inputSessionIdentity:n}),v||this.turnScopes.cancel(),m.close(),y}})(),b=(await Promise.allSettled([S,f])).find(y=>y.status==="rejected");if(b!==void 0)throw b.reason}catch(l){!J(l)&&!t.aborted&&this.emit(R(this.envelope(o,u,this.agentMetadata),E(l,{provider:"runtime"})))}finally{this.abortPendingAsr({emitLatency:!1,inputSessionIdentity:n}),t.removeEventListener("abort",p)}}async runAsr(t,r,n,a,i,o){let u=!1,c=!1,d=async()=>{c||o.aborted||!this.isCurrentSpeechGeneration(i)||(c=!0,await this.cancelCameraCaptureWithoutBlocking(a))};try{let p=this.turnTimings.get(n),s=!1;p?.markAsrStarted();for await(let l of this.config.providers.asr.run(t,{signal:o})){if(o.aborted||!this.isCurrentSpeechGeneration(i))return!1;let m={...l,streamId:r,turnId:n};if(p?.recordStageMetadata("asr",l.metadata),l.final){if(s)continue;let S=!1;if(await this.serializeAdmission(async()=>{o.aborted||!this.isCurrentSpeechGeneration(i)||(p?.markAsrFinal(),l.text.trim().length>0&&this.commitMessage(n,"user",l.text,this.agentMetadata),this.emit(ze(m,"speech")),S=l.text.trim().length>0)}),S){let f=await this.cameraCaptures.get(n);await this.serializeAdmission(async()=>{if(o.aborted||!this.isCurrentSpeechGeneration(i))return;let h=this.turnScopes.begin({streamId:r,turnId:n});this.track(this.runAssistantTurn(r,n,l.text,a,h,{messageMetadata:this.agentMetadata,...f!==void 0?{cameraSnapshot:f}:{}})),u=!0})}else await d();s=!0}else s||this.emit(qt(m,"speech"))}return u||await d(),u}catch(p){return!J(p)&&!o.aborted&&this.isCurrentSpeechGeneration(i)&&this.emit(R(a,E(p,{provider:"asr"}))),await d(),!1}finally{this.cameraCaptures.delete(n)}}async runAssistantTurn(t,r,n,a,i,o={}){let u=i.signal,c=Ge(),d=Ue({onStarted:()=>{this.turnScopes.isCurrent(i)&&this.emit(N(a,"playback.started"))},onStopped:()=>{this.turnScopes.isCurrent(i)&&this.emit(N(a,"playback.stopped"))}}),p=!1,s=_e({parentSignal:u,process:async(m,S)=>{try{for await(let f of this.ttsFrames(m,t,r,S.signal)){if(!S.isCurrent()||!this.turnScopes.isCurrent(i))return;let h=this.turnTimings.get(r);h?.recordStageMetadata("tts",f.metadata),h?.markTtsFirstAudio();let b=this.config.transports;if(b===void 0)continue;let y=ue(f);if(h?.markPlaybackStarted(),d.start(),await b.output.enqueue(y),!S.isCurrent()||!this.turnScopes.isCurrent(i)||(await b.aec.pushFarEnd(y),!S.isCurrent()||!this.turnScopes.isCurrent(i)))return}}catch(f){!J(f)&&S.isCurrent()&&this.turnScopes.isCurrent(i)&&(p=!0,s.clearAndAbort(),this.emit(R(a,E(f,{provider:"tts"}))))}}}),l={scope:i,base:a,aggregator:c,worker:s,playback:d};this.activeTtsTurn=l;try{if(!this.turnScopes.isCurrent(i))return;this.emit(N(a,"reply.started"));let m="",S=!1,f=o.staticReply!==void 0?Wt(o.staticReply,t,r):this.config.providers.llm.run({messages:this.llmMessages(n,o.commitHistory!==!1,o.cameraSnapshot),streamId:t,turnId:r,metadata:o.messageMetadata??this.agentMetadata},{signal:u});for await(let h of f){if(u.aborted||!this.turnScopes.isCurrent(i))return;if(h.text.length>0){let b=this.turnTimings.get(r);if(b?.recordStageMetadata("llm",h.metadata),b?.markLlmFirstToken(),m+=h.text,this.emit(He(a,"reply.partial",h.text)),!p&&!this.skipTts)for(let y of c.push(h.text))s.enqueue(y)}if(h.final){if(S=!0,!p&&!this.skipTts)for(let b of c.flush())s.enqueue(b);o.commitHistory!==!1&&this.commitHistory(n,m),o.recordAssistant!==!1&&this.commitMessage(r,"assistant",m,o.messageMetadata??this.agentMetadata),this.emit(He(a,"reply.final",m));break}}if(!this.turnScopes.isCurrent(i))return;if(!S){c.clear(),s.clearAndAbort();return}if(await s.close(),!this.turnScopes.isCurrent(i))return;d.isActive()&&(await this.config.transports?.output.drain(),this.turnScopes.isCurrent(i)&&await d.close())}catch(m){!J(m)&&!u.aborted&&this.turnScopes.isCurrent(i)&&this.emit(R(a,E(m,{provider:"llm"})))}finally{this.turnScopes.isCurrent(i)&&this.emitTurnLatency(i),c.clear(),s.clearAndAbort(),this.activeTtsTurn===l&&(this.activeTtsTurn=void 0),this.turnScopes.complete(i)}}llmMessages(t,r,n){return[...this.config.systemPrompt!==void 0&&this.config.systemPrompt.length>0?[{role:"system",content:this.config.systemPrompt}]:[],...r&&this.config.history!==void 0?this.committedHistory.flatMap(({user:a,assistant:i})=>[{role:"user",content:a},{role:"assistant",content:i}]):[],{role:"user",content:n===void 0?t:[{type:"text",text:t},{type:"image",data:n.data,mimeType:n.mimeType}]}]}async settleCameraCapture(t,r){let n=await t.result;if(!(!this.isCurrentSpeechGeneration(t.generation)||this.stopping)&&n.status!=="cancelled"){if(n.status==="failure"){this.emit(R(r,n.error));return}return this.emit(Bt(r,n.snapshot,n.captureMs)),n.snapshot}}async cancelCameraCaptureWithoutBlocking(t){try{await this.cameraController.cancelPendingCaptureAndWait()}catch(r){this.reportCameraFaultFromUnknown(r,t,"Camera cancellation failed")}}reportCameraFaultFromUnknown(t,r,n){if(t instanceof U){this.reportCameraFault(t.mediaError,this.envelope("speech",t.turnId,this.agentMetadata));return}let a=t instanceof g?t:w(t,{message:n,fatal:!0});this.reportCameraFault(a,r)}reportCameraFault(t,r){this.cameraFaultReported||(this.cameraFaultReported=!0,this.emit(R(r,t)))}commitHistory(t,r){let n=this.config.history?.maxTurns;if(n===void 0)return;this.committedHistory.push({user:t,assistant:r});let a=this.committedHistory.length-n;a>0&&this.committedHistory.splice(0,a)}ttsFrames(t,r,n,a){return this.config.providers.tts.run({kind:"text",streamId:r,turnId:n,partial:!1,final:!0,metadata:{},text:t},{signal:a})}async*nearEndFrames(t,r,n){let a=0;for await(let i of t){if(n.aborted)return;let o=await this.config.transports?.aec.processNearEnd(i);o!==void 0&&(yield se(o,{streamId:r,sequence:a++,metadata:{}}))}}emit(t){for(let r of this.listeners)r(t)}track(t){this.tasks.add(t),t.finally(()=>this.tasks.delete(t)).catch(()=>{})}serializeAdmission(t){let r=this.admissionTail.then(t,t);return this.admissionTail=r.catch(()=>{}),r}async interruptActiveTurn(t,r){let n=this.turnScopes.current();if(!(n===void 0||(this.emitTurnLatency(n),this.turnScopes.cancel()===void 0))){try{await this.config.transports?.output.flush()}catch(i){this.emit(R(this.envelope(t.streamId,t.turnId,t.metadata),E(i,{provider:"runtime"})))}this.emit(zt(t,r))}}abortPendingAsr(t={}){for(let[r,n]of this.pendingAsrControllers)t.inputSessionIdentity!==void 0&&n.inputSessionIdentity!==t.inputSessionIdentity||(t.emitLatency!==!1?this.emitTurnLatency(n):this.turnTimings.delete(n.turnId),r.abort(),this.pendingAsrControllers.delete(r))}isCurrentSpeechGeneration(t){return t===this.speechGeneration}reserveTurnId(t){let r=t??this.generatedTurnId();if(r.trim().length===0)throw new g("turnId must not be empty",{fatal:!0});if(this.usedTurnIds.has(r))throw new g("turnId must be unique within an agent session",{fatal:!0});return this.usedTurnIds.add(r),r}generatedTurnId(){do this.turnCounter+=1;while(this.usedTurnIds.has(`turn-${this.turnCounter}`));return`turn-${this.turnCounter}`}beginTurnTiming(t,r){let n=Ne({turnId:t,source:r,...this.config.now!==void 0?{now:this.config.now}:{}});return this.turnTimings.set(t,n),n}emitTurnLatency(t){let n=this.turnTimings.get(t.turnId)?.takeSnapshot();this.turnTimings.delete(t.turnId),n!==void 0&&this.emit(Ht(this.envelope(t.streamId,t.turnId,this.agentMetadata),n))}envelope(t,r,n={}){return{streamId:t,...r!==void 0?{turnId:r}:{},partial:!1,final:!1,metadata:{...n}}}effectiveMetadata(t){try{let r=C(t??{});return C({...this.agentMetadata,...r})}catch(r){throw new g("Turn metadata must be JSON-compatible",{fatal:!0,cause:r})}}commitMessage(t,r,n,a){this.messageCounter+=1,this.messages.push({id:`message-${this.messageCounter}`,turnId:t,role:r,content:n,createdAt:(this.config.now??Date.now)(),metadata:C(a)})}};async function*Wt(e,t,r){yield{kind:"llm",streamId:t,turnId:r,partial:!1,final:!0,metadata:{},text:e}}function ze(e,t){return{...Je(e),type:"transcript.final",partial:!1,final:!0,text:e.text,source:t}}function qt(e,t){return{...Je(e),type:"transcript.partial",partial:!0,final:!1,text:e.text,source:t}}function N(e,t){return{...e,type:t,partial:!1,final:!0}}function Bt(e,t,r){return{...e,type:"image.captured",partial:!1,final:!0,image:{mimeType:t.mimeType,width:t.width,height:t.height,sizeBytes:t.data.byteLength,captureMs:r}}}function He(e,t,r){return{...e,type:t,partial:t==="reply.partial",final:t==="reply.final",text:r}}function zt(e,t){return{...e,type:"interruption",partial:!1,final:!0,reason:t}}function Ht(e,t){return{...e,type:"turn.latency",partial:!1,final:!0,latency:t}}function R(e,t){return{...e,type:"error",partial:!1,final:!0,error:t}}function Je(e){return{streamId:e.streamId,...e.turnId!==void 0?{turnId:e.turnId}:{},...e.sequence!==void 0?{sequence:e.sequence}:{},partial:e.partial,final:e.final,...e.timestamp!==void 0?{timestamp:e.timestamp}:{},metadata:e.metadata,...e.frameId!==void 0?{frameId:e.frameId}:{}}}function J(e){return e instanceof Error&&e.name==="AbortError"}function Jt(e){let t=new AbortController,r=()=>t.abort();return e.aborted?(r(),{controller:t,unlink(){}}):(e.addEventListener("abort",r,{once:!0}),{controller:t,unlink(){e.removeEventListener("abort",r)}})}async function $(e,t={}){if(ce(e.channels,"channels"),ce(e.sourceSampleRate,"sourceSampleRate"),ce(e.targetSampleRate,"targetSampleRate"),e.sourceSampleRate===e.targetSampleRate)return new K(e);let r=await(t.load??Kt)(),n=$t(r),a=await n.create(e.channels,e.sourceSampleRate,e.targetSampleRate,{converterType:n.ConverterType.SRC_SINC_FASTEST});return new K(e,a)}var K=class{constructor(t,r){this.converter=r;this.channels=t.channels,this.sourceSampleRate=t.sourceSampleRate,this.targetSampleRate=t.targetSampleRate}converter;channels;sourceSampleRate;targetSampleRate;destroyed=!1;simple(t){return this.assertUsable(t),this.converter?.simple(t)??t}full(t){return this.assertUsable(t),this.converter?.full(t)??t}destroy(){this.destroyed||(this.destroyed=!0,this.converter?.destroy())}assertUsable(t){if(this.destroyed)throw new Error("Resampler has been destroyed");if(t.length%this.channels!==0)throw new Error("Interleaved audio length must be divisible by channels")}};async function Kt(){return import("@alexanderolsen/libsamplerate-js")}function $t(e){if(typeof e!="object"||e===null)throw new Error("libsamplerate module did not load as an object");let t=e,r=t.default??t;if(typeof r.create!="function"||typeof r.ConverterType?.SRC_SINC_FASTEST!="number")throw new Error("libsamplerate module has an incompatible API");return r}function ce(e,t){if(!Number.isFinite(e)||!Number.isInteger(e)||e<=0)throw new Error(`${t} must be a finite positive integer`)}var Yt="https://eva-gateway-ali.dev.autoarkai.com",Y={asr:"/v1/audio/transcriptions",llm:"/llm/v1/chat/completions",tts:"/v1/audio/speech"};function Q(e){return`${Yt}${e}`}function Qt(){return new DOMException("Operation aborted","AbortError")}function Zt(e){if(e?.aborted===!0)throw Qt()}function Ke(e){let t=e.trim();if(t.startsWith("data:"))return t.slice(5).trimStart()}async function*Z(e,t={}){let{signal:r,isTerminator:n}=t,a=new TextDecoder,i="",o=!1;for await(let c of e){if(Zt(r),o)continue;i+=a.decode(c,{stream:!0});let d=i.indexOf(`
2
+ `);for(;d>=0;){let p=i.slice(0,d);i=i.slice(d+1);let s=Ke(p);if(s!==void 0){if(n?.(s)===!0){o=!0,i="";break}yield s}d=i.indexOf(`
3
+ `)}}if(o)return;i+=a.decode();let u=Ke(i);u!==void 0&&n?.(u)!==!0&&(yield u)}function le(e){return e==="[DONE]"}async function*X(e){let t=e.getReader(),r=!1;try{for(;;){let{value:n,done:a}=await t.read();if(a===!0){r=!0;break}n!==void 0&&(yield n)}}finally{try{r||await t.cancel().catch(()=>{})}finally{t.releaseLock()}}}function $e(e){let t=atob(e),r=new Uint8Array(t.length);for(let n=0;n<t.length;n+=1)r[n]=t.charCodeAt(n);return r}function me(e){try{return JSON.parse(e)}catch{return}}function Xt(e){try{return $e(e)}catch{return}}function fe(e){return e!==null&&typeof e=="object"}function pe(e,t,r){return{kind:"asr",streamId:t.streamId,...t.turnId!==void 0?{turnId:t.turnId}:{},partial:!r,final:r,metadata:{},text:e}}function Ye(e,t,r){return{kind:"llm",streamId:t.streamId,...t.turnId!==void 0?{turnId:t.turnId}:{},partial:!r,final:r,metadata:{},text:e}}function Qe(e,t,r){return{kind:"tts.audio",streamId:t.streamId,...t.turnId!==void 0?{turnId:t.turnId}:{},partial:!r,final:r,metadata:{},audio:e,sampleRate:t.sampleRate,channels:t.channels}}async function*Ze(e,t){let r="",n=!1;for await(let a of e){let i=me(a);if(!fe(i))continue;he(i,"asr",t.traceId);let o=i;o.type==="transcript.text.delta"?(r+=o.delta??"",yield pe(r,t,!1)):o.type==="transcript.text.done"&&(yield pe(o.text??r,t,!0),n=!0)}n||(yield pe(r,t,!0))}async function*Xe(e,t){for await(let r of e){let n=me(r);if(!fe(n))continue;he(n,"llm",t.traceId);let i=n.choices?.[0]?.delta?.content;typeof i=="string"&&i.length>0&&(yield Ye(i,t,!1))}yield Ye("",t,!0)}async function*et(e,t){let r,n=!1;for await(let a of e){if(n)continue;let i=me(a);if(!fe(i))continue;he(i,"tts",t.traceId);let o=i;if(o.type==="speech.audio.delta"&&typeof o.audio=="string"){let u=Xt(o.audio);if(u===void 0)continue;r!==void 0&&(yield Qe(r,t,!1)),r=u}else o.type==="speech.audio.done"&&(n=!0)}r!==void 0&&(yield Qe(r,t,!0))}function he(e,t,r){if(!Object.hasOwn(e,"error"))return;let n=e.error,a=V(n);throw F(n,{provider:t,...a!==void 0?{gatewayType:a}:{},...r!==void 0?{traceId:r}:{}})}var er="autoark-trace-id";function ee(e){return e!==void 0?e:globalThis.fetch.bind(globalThis)}function te(e){return{Authorization:`Bearer ${e}`}}function L(e){return k(e.get(er))}function tr(e){return e instanceof DOMException&&e.name==="AbortError"}async function re(e,t,r,n){let a;try{a=await e(t,r)}catch(i){throw tr(i)?i:F(i,{provider:n})}if(!a.ok){let i=L(a.headers),o;try{o=await a.text()}catch{o=void 0}let u=o===void 0?void 0:V(o);throw F(o,{provider:n,statusCode:a.status,...u!==void 0?{gatewayType:u}:{},...i!==void 0?{traceId:i}:{}})}return a}function ne(e,t){let r=e.body;if(r===null){let n=L(e.headers);throw F("empty response body",{provider:t,...n!==void 0?{traceId:n}:{}})}return r}var rr=16e3,ge=1;function nr(){return new DOMException("Operation aborted","AbortError")}function ye(e){if(e?.aborted===!0)throw nr()}function ar(e){let t=e.reduce((a,i)=>a+i.length,0),r=new Uint8Array(t),n=0;for(let a of e)r.set(a,n),n+=a.length;return r}async function ir(e,t,r){ae(t.sampleRate,"ASR target sampleRate"),ae(t.channels??ge,"ASR fallback channels");let n=t.createResampler??$,a=[],i,o;for await(let d of e){if(ye(r),ae(d.sampleRate,"ASR source sampleRate"),ae(d.channels,"ASR source channels"),i===void 0)i=d.sampleRate,o=d.channels;else if(d.sampleRate!==i||d.channels!==o)throw new RangeError("ASR source sampleRate and channels must remain stable within an utterance");ur(d.audio,d.channels),a.push(d.audio)}ye(r);let u=ar(a);if(i===void 0||o===void 0)return{bytes:u,sampleRate:t.sampleRate,channels:t.channels??ge};if(i===t.sampleRate)return{bytes:u,sampleRate:t.sampleRate,channels:o};let c=await n({channels:o,sourceSampleRate:i,targetSampleRate:t.sampleRate});try{let d=c.simple(or(u));if(d.length%o!==0)throw new RangeError("Resampled audio is not aligned to its channel count");return{bytes:sr(d),sampleRate:t.sampleRate,channels:o}}finally{c.destroy()}}function or(e){let t=new Float32Array(e.byteLength/2),r=new DataView(e.buffer,e.byteOffset,e.byteLength);for(let n=0;n<t.length;n+=1)t[n]=r.getInt16(n*2,!0)/32768;return t}function sr(e){let t=new Uint8Array(e.length*2),r=new DataView(t.buffer);for(let n=0;n<e.length;n+=1){let a=Math.max(-1,Math.min(1,e[n])),i=a<0?Math.round(a*32768):Math.round(a*32767);r.setInt16(n*2,i,!0)}return t}function ur(e,t){if(e.byteLength%2!==0)throw new RangeError("ASR PCM16 frame must contain an even number of bytes");if(e.byteLength/2%t!==0)throw new RangeError("ASR PCM16 frame must align to its channel count")}function ae(e,t){if(!Number.isFinite(e)||!Number.isInteger(e)||e<=0)throw new RangeError(`${t} must be a finite positive integer`)}function dr(e){return Symbol.asyncIterator in Object(e)}async function cr(e,t){if(dr(e)){let r="",n="tts",a;for await(let i of e)ye(t),r+=i.text,n=i.streamId,a=i.turnId;return a!==void 0?{text:r,streamId:n,turnId:a}:{text:r,streamId:n}}return e.turnId!==void 0?{text:e.text,streamId:e.streamId,turnId:e.turnId}:{text:e.text,streamId:e.streamId}}function lr(e){return e.map(t=>{if(Array.isArray(t.content)&&t.content.length===0)throw new g("Gateway LLM content parts must not be empty",{fatal:!0});let r=typeof t.content=="string"?t.content:t.content.map(a=>{if(a.type==="text")return{type:"text",text:a.text};if(a.data.byteLength===0||!/^image\/[a-z0-9.+-]+$/i.test(a.mimeType))throw new g("Gateway LLM image content is invalid",{fatal:!0});return{type:"image_url",image_url:{url:`data:${a.mimeType};base64,${pr(a.data)}`}}}),n={role:t.role,content:r};return t.name!==void 0&&(n.name=t.name),t.toolCallId!==void 0&&(n.tool_call_id=t.toolCallId),n})}function pr(e){let r="";for(let n=0;n<e.length;n+=32768)r+=String.fromCharCode(...e.subarray(n,n+32768));return btoa(r)}function Se(e){let t=ee(e.fetch),r=te(e.apiKey);return{run(n,a){return(async function*(){let o=a?.signal,u;try{u=await ir(n,e,o)}catch(h){throw h instanceof DOMException&&h.name==="AbortError"?h:E(h,{provider:"gateway-asr",message:"Gateway ASR audio preprocessing failed"})}let{bytes:c,sampleRate:d,channels:p}=u,s=new FormData;s.append("model",e.model),s.append("stream","true"),s.append("audio_format","pcm"),s.append("sample_rate",String(d)),s.append("channels",String(p)),e.hotwords!==void 0&&s.append("hotwords",e.hotwords),s.append("file",new Blob([c],{type:"application/octet-stream"}),"audio.pcm");let l={method:"POST",headers:r,body:s};o!==void 0&&(l.signal=o);let m=await re(t,Q(Y.asr),l,"asr"),S=L(m.headers),f=Z(X(ne(m,"asr")),{...o!==void 0?{signal:o}:{},isTerminator:le});yield*Ze(f,{streamId:"speech",...S!==void 0?{traceId:S}:{}})})()}}}function ve(e){let t=ee(e.fetch),n={...te(e.apiKey),"Content-Type":"application/json"};return{run(a,i){return(async function*(){let u=i?.signal,c={model:e.model,stream:!0,messages:lr(a.messages)};e.temperature!==void 0&&(c.temperature=e.temperature),e.maxTokens!==void 0&&(c.max_tokens=e.maxTokens),e.topP!==void 0&&(c.top_p=e.topP);let d={method:"POST",headers:n,body:JSON.stringify(c)};u!==void 0&&(d.signal=u);let p=await re(t,Q(Y.llm),d,"llm"),s=L(p.headers),l=Z(X(ne(p,"llm")),{...u!==void 0?{signal:u}:{},isTerminator:le}),m={streamId:a.streamId,...a.turnId!==void 0?{turnId:a.turnId}:{},...s!==void 0?{traceId:s}:{}};yield*Xe(l,m)})()}}}function be(e){let t=ee(e.fetch),n={...te(e.apiKey),"Content-Type":"application/json"};return{run(a,i){return(async function*(){let u=i?.signal,{text:c,streamId:d,turnId:p}=await cr(a,u),s=e.sampleRate??rr,l={model:e.model,input:c,response_format:"pcm",stream_format:"sse",sample_rate:s};e.voice!==void 0&&(l.voice=e.voice),e.speed!==void 0&&(l.speed=e.speed),e.pitchRate!==void 0&&(l.pitch_rate=e.pitchRate);let m={method:"POST",headers:n,body:JSON.stringify(l)};u!==void 0&&(m.signal=u);let S=await re(t,Q(Y.tts),m,"tts"),f=L(S.headers),h=Z(X(ne(S,"tts")),{...u!==void 0?{signal:u}:{}}),b={streamId:d,sampleRate:s,channels:ge,...p!==void 0?{turnId:p}:{},...f!==void 0?{traceId:f}:{}};yield*et(h,b)})()}}}import*as j from"onnxruntime-web";var mr=new URL("./assets/silero_vad_v6.onnx",import.meta.url).href;async function tt(e={},t){let r=e.modelUrl??mr,n=await(e.modelFetcher??fr)(r,t),a=await j.InferenceSession.create(n);return{async run(i){let o=await a.run({input:new j.Tensor("float32",i.input,[1,i.input.length]),state:new j.Tensor("float32",i.state,[2,1,128]),sr:new j.Tensor("int64",BigInt64Array.from([BigInt(i.sampleRate)]),[])}),u=o.output,c=o.stateN;if(u===void 0||c===void 0||u.type!=="float32"||c.type!=="float32"||!(u.data instanceof Float32Array)||!(c.data instanceof Float32Array)||u.data.length!==1||!hr(c.dims,[2,1,128]))throw new Error("Silero VAD v6 returned an invalid result");return Ae({speechProbability:u.data[0],state:Float32Array.from(c.data)})}}}function Ae(e){if(!Number.isFinite(e.speechProbability)||e.speechProbability<0||e.speechProbability>1||e.state.length!==256||!e.state.every(Number.isFinite))throw new Error("Silero VAD v6 returned an invalid result");return e}async function fr(e,t){let r=await fetch(e,t!==void 0?{signal:t}:{});if(!r.ok)throw new Error("Silero VAD model fetch failed");return r.arrayBuffer()}function hr(e,t){return e.length===t.length&&e.every((r,n)=>r===t[n])}var rt=16e3,Ee=512,ie=64;function Ce(e={}){return new Te(e)}var Te=class{constructor(t){this.options=t}options;run(t,r){return this.runFrames(t,r)}async*runFrames(t,r){let n=r?.signal,a=this.options.positiveSpeechThreshold??.5,i=this.options.negativeSpeechThreshold??.35,o=Math.max(1,Math.ceil((this.options.silenceThresholdMs??200)/32)),u=!1,c=0,d=new Float32Array(256),p=new Float32Array(ie),s,l,m,S=[];try{if(q(n))return;let f=await nt(this.options.createSession!==void 0?this.options.createSession():tt(this.options,n),n);for await(let h of t){if(q(n))return;if(s=h,l!==void 0&&h.sampleRate!==l)throw new RangeError("VAD source sampleRate cannot change within a run");l??=h.sampleRate,m??=await $({channels:1,sourceSampleRate:l,targetSampleRate:rt});let b=gr(h);for(S.push(...m.full(b));S.length>=Ee;){let y=Float32Array.from(S.splice(0,Ee)),v=new Float32Array(ie+Ee);v.set(p),v.set(y,ie);let A=Ae(await nt(f.run({input:v,state:d,sampleRate:rt}),n));if(q(n))return;d=Float32Array.from(A.state),p=v.slice(v.length-ie),A.speechProbability>=a?(c=0,u||(u=!0,yield we(h,"started",A.speechProbability))):u&&A.speechProbability<i?(c+=1,c>=o&&(u=!1,c=0,yield we(h,"stopped",A.speechProbability))):u&&(c=0)}}u&&!q(n)&&s!==void 0&&(yield we(s,"stopped"))}catch(f){if(q(n))return;throw E(f,{provider:"silero-vad"})}finally{m?.destroy()}}};function q(e){return e?.aborted===!0}function nt(e,t){return t===void 0?e:t.aborted?Promise.reject(at()):new Promise((r,n)=>{let a=()=>{t.removeEventListener("abort",a),n(at())};t.addEventListener("abort",a,{once:!0}),e.then(i=>{t.removeEventListener("abort",a),r(i)},i=>{t.removeEventListener("abort",a),n(i)})})}function at(){let e=new Error("Operation aborted");return e.name="AbortError",e}function gr(e){if(!Number.isInteger(e.channels)||e.channels<=0)throw new RangeError("Audio frame channels must be positive");let t=e.channels*2;if(e.audio.byteLength%t!==0)throw new RangeError("Audio frame PCM must align to its channel count");let r=e.audio.byteLength/t,n=new Float32Array(r),a=new DataView(e.audio.buffer,e.audio.byteOffset,e.audio.byteLength);for(let i=0;i<r;i+=1){let o=0;for(let u=0;u<e.channels;u+=1)o+=a.getInt16((i*e.channels+u)*2,!0)/32768;n[i]=o/e.channels}return n}function we(e,t,r){return{kind:"vad",streamId:e.streamId,...e.turnId!==void 0?{turnId:e.turnId}:{},partial:t==="started",final:t==="stopped",metadata:e.metadata,state:t,...r!==void 0?{confidence:r}:{}}}function xe(e){return e.fetch!==void 0?{fetch:e.fetch}:{}}function it(e,t){return Se({apiKey:t.apiKey,model:e.model,sampleRate:e.sampleRate,...xe(t)})}function ot(e,t){return ve({apiKey:t.apiKey,model:e.model,...e.temperature!==void 0?{temperature:e.temperature}:{},...e.maxTokens!==void 0?{maxTokens:e.maxTokens}:{},...xe(t)})}function st(e,t){return be({apiKey:t.apiKey,model:e.model,...e.voice!==void 0?{voice:e.voice}:{},...e.speakingRate!==void 0?{speed:e.speakingRate}:{},...e.sampleRate!==void 0?{sampleRate:e.sampleRate}:{},...e.pitch!==void 0?{pitchRate:e.pitch}:{},...xe(t)})}function ut(e,t){if(e===void 0)return;if(e.sensitivity!==void 0&&!(e.sensitivity>0&&e.sensitivity<=1))throw new g("VAD sensitivity must be within (0, 1]",{fatal:!0});if(e.silenceThresholdMs!==void 0&&(!Number.isFinite(e.silenceThresholdMs)||e.silenceThresholdMs<=0))throw new g("VAD silenceThresholdMs must be finite and greater than 0",{fatal:!0});let r=e.sensitivity??.5;return Ce({positiveSpeechThreshold:r,negativeSpeechThreshold:Math.max(0,r-.15),...e.silenceThresholdMs!==void 0?{silenceThresholdMs:e.silenceThresholdMs}:{},...t.createSileroSession!==void 0?{createSession:t.createSileroSession}:{}})}var yr=10,Sr=1500,vr="\u8BF7\u7528\u4E00\u53E5\u7B80\u77ED\u3001\u81EA\u7136\u7684\u8BDD\u5411\u7528\u6237\u6253\u62DB\u547C\u3002";function Re(e={}){return{create(t){let r={apiKey:t.apiKey,...e.fetch!==void 0?{fetch:e.fetch}:{},...e.createSileroSession!==void 0?{createSileroSession:e.createSileroSession}:{}},n=ut(t.vad,r);return{asr:it(t.asr,r),llm:ot(t.llm,r),tts:st(t.tts,r),...n!==void 0?{vad:n}:{}}}}}function Ie(e,t){dt(e.asr.sampleRate,"ASR sampleRate"),e.tts.sampleRate!==void 0&&dt(e.tts.sampleRate,"TTS sampleRate");let r=br(e.camera?.captureTimeoutMs),n=e.transports?.input!==void 0,a={apiKey:e.apiKey,asr:e.asr,tts:e.tts,llm:e.llm};e.vad!==void 0&&(a.vad=e.vad);let i=t.create(a);if(n&&i.vad===void 0)throw new g("Audio input requires a VAD provider",{fatal:!0});let o={systemPrompt:e.systemPrompt??"",greeting:Er(e.greeting),metadata:Ar(e.metadata),camera:{captureTimeoutMs:r},providers:i};return e.history!==void 0&&(o.history={maxTurns:wr(e.history.maxTurns)}),e.transports!==void 0&&(o.transports=e.transports),o}function br(e){let t=e??Sr;if(!Number.isFinite(t)||!Number.isInteger(t)||t<=0)throw new g("Camera captureTimeoutMs must be a finite positive integer",{fatal:!0});return t}function Ar(e){try{return C(e??{})}catch(t){throw new g("Agent metadata must be JSON-compatible",{fatal:!0,cause:t})}}function Er(e){if(e===void 0||e.mode==="disabled")return{mode:"disabled"};if(e.mode==="static"){if(e.text.trim().length===0)throw new g("Static greeting text must not be empty",{fatal:!0});return{mode:"static",text:e.text}}return{mode:"dynamic",prompt:e.prompt===void 0||e.prompt.trim().length===0?vr:e.prompt}}function wr(e){let t=e??yr;if(!Number.isInteger(t)||t<=0)throw new g("History maxTurns must be a positive integer",{fatal:!0});return t}function dt(e,t){if(!Number.isFinite(e)||!Number.isInteger(e)||e<=0)throw new g(`${t} must be a finite positive integer`,{fatal:!0})}function ct(e,t,r){return{...T(e),type:"transcript.final",final:!0,partial:!1,text:t,source:r}}function lt(e,t){return{...T(e),type:"transcript.partial",final:!1,partial:!0,text:t,source:"speech"}}function pt(e){return{...T(e),type:"speech.started",partial:!1,final:!0}}function mt(e,t){return{...T(e),type:"image.captured",partial:!1,final:!0,image:{...t}}}function ft(e){return{...T(e),type:"speech.stopped",partial:!1,final:!0}}function ht(e,t){return{...T(e),type:"interruption",partial:!1,final:!0,reason:t}}function gt(e){return{...T(e),type:"reply.started",partial:!1,final:!0}}function yt(e,t){return{...T(e),type:"reply.partial",partial:!0,final:!1,text:t}}function St(e,t){return{...T(e),type:"reply.final",partial:!1,final:!0,text:t}}function vt(e){return{...T(e),type:"playback.started",partial:!1,final:!0}}function bt(e){return{...T(e),type:"playback.stopped",partial:!1,final:!0}}function At(e,t){return{...T(e),type:"turn.latency",partial:!1,final:!0,latency:t}}function Et(e,t){return{...e,type:"error",partial:!1,final:!0,error:Tr(t)}}function T(e){if(e.turnId===void 0)throw new g("Runtime event is missing turn identity",{fatal:!0});return{...e,turnId:e.turnId}}function Tr(e){let t=e.source==="gateway"?k(e.traceId):void 0;return{message:e.message,fatal:e.fatal,source:e.source,...e.provider!==void 0?{provider:e.provider}:{},...e.statusCode!==void 0?{statusCode:e.statusCode}:{},...t!==void 0?{traceId:t}:{},...e.role!==void 0?{role:e.role}:{},...e.operation!==void 0?{operation:e.operation}:{},...e.reason!==void 0?{reason:e.reason}:{}}}function Fe(e){let t=new Set,r=new Map,n=e.onEvent(a=>{let i=xr(a,Cr(r,a.streamId));if(i!==void 0)for(let o of t)o(i)});return{onEvent(a){return t.add(a),()=>{t.delete(a)}},close(){n(),t.clear()}}}function Cr(e,t){let r=e.get(t)??0;return e.set(t,r+1),r}function xr(e,t){let r=Rr(e,t);switch(e.type){case"speech.started":return pt(r);case"image.captured":return mt(r,e.image);case"speech.stopped":return ft(r);case"transcript.partial":return lt(r,e.text);case"transcript.final":return ct(r,e.text,e.source);case"interruption":return ht(r,e.reason);case"reply.started":return gt(r);case"reply.partial":return yt(r,e.text);case"reply.final":return St(r,e.text);case"playback.started":return vt(r);case"playback.stopped":return bt(r);case"turn.latency":return At(r,e.latency);case"error":return Et(r,e.error);default:return}}function Rr(e,t){return{streamId:e.streamId,...e.turnId!==void 0?{turnId:e.turnId}:{},sequence:t,partial:e.partial,final:e.final,...e.timestamp!==void 0?{timestamp:e.timestamp}:{},metadata:Ir(e.metadata),...e.frameId!==void 0?{frameId:e.frameId}:{}}}var wt=/(api.?key|authorization|headers?|raw|body|sse|pcm|provider.?object|secret|token|credential|password|cookies?)/i,I=Symbol("unsafe-metadata");function Ir(e){let t={};for(let[r,n]of Object.entries(e)){if(wt.test(r))continue;let a=Pe(n,new Set);a!==I&&(t[r]=a)}return t}function Pe(e,t){if(e===null||typeof e=="string"||typeof e=="boolean")return e;if(typeof e=="number")return Number.isFinite(e)?e:I;if(typeof e!="object"||t.has(e))return I;t.add(e);try{if(Array.isArray(e)){let n=[];for(let a of e){let i=Pe(a,t);if(i===I)return I;n.push(i)}return n}if(Object.getPrototypeOf(e)!==Object.prototype&&Object.getPrototypeOf(e)!==null)return I;let r={};for(let[n,a]of Object.entries(e)){if(wt.test(n))continue;let i=Pe(a,t);if(i===I)return I;r[n]=i}return r}finally{t.delete(e)}}function Tt(e){return Pr(e,Re())}function Pr(e,t){let r=Ie(e,t);return Fr(new W(r))}function Fr(e){let t=Fe(e),r="created",n=!1,a,i,o=new Set,u=(d,p,s=!0)=>{let l;return l=(async()=>{try{if(await d(),n)throw x("Agent media control was cancelled by stop")}catch(m){throw n?x("Agent media control was cancelled by stop"):s&&m instanceof g?m:new g(p,{cause:m})}finally{o.delete(l)}})(),o.add(l),l};return{start(){if(n||r==="stopped")return Promise.reject(x("Agent is stopped"));if(a!==void 0)return a;let d=(async()=>{try{if(await e.start(),n)throw x("Agent start was cancelled by stop");r="running"}catch(p){if(n)throw x("Agent start was cancelled by stop");try{await e.stop()}catch{}throw r="created",a=void 0,p instanceof g?p:w(p,{message:"Agent start failed"})}})();return a=d,d},submitText(d,p){if(n||r!=="running")return Promise.reject(x(r==="created"?"Agent has not started":"Agent is stopped"));let s;try{s=p===void 0?void 0:kr(p)}catch(l){return Promise.reject(w(l,{message:"Turn metadata must be JSON-compatible"}))}return e.submitText(d,s).catch(l=>{throw w(l,{message:"Agent text submission failed"})})},setAudioInputEnabled(d){return n||r==="stopped"?Promise.reject(x("Agent is stopped")):u(()=>e.setAudioInputEnabled(d),"Agent audio input update failed",!1)},setCameraCaptureEnabled(d){return n||r==="stopped"?Promise.reject(x("Agent is stopped")):u(()=>e.setCameraCaptureEnabled(d),"Agent camera capture update failed")},setTtsEnabled(d){return n||r==="stopped"?Promise.reject(x("Agent is stopped")):u(()=>e.setSkipTts(!d),"Agent TTS update failed")},getMessages(){return e.getMessages()},onEvent(d){if(n||r==="stopped")throw x(r==="stopped"?"Agent is stopped":"Agent is stopping");let p=t.onEvent(d),s=!0;return()=>{s&&(s=!1,p())}},stop(){if(i!==void 0)return i;n=!0;let d=a,p=[...o],s=(async()=>{let l;try{try{await e.stop()}catch(m){l=m}if(await Promise.allSettled([...d===void 0?[]:[d],...p]),l!==void 0)throw w(l,{message:"Agent stop failed"})}finally{t.close(),r="stopped"}})();return i=s,s}}}function kr(e){return{...e.turnId!==void 0?{turnId:e.turnId}:{},...e.metadata!==void 0?{metadata:C(e.metadata)}:{}}}function x(e){return new g(e,{fatal:!0})}export{g as EvaSdkError,Tt as createEvaVoiceDialogueAgent};
package/package.json CHANGED
@@ -1,7 +1,13 @@
1
1
  {
2
2
  "name": "@autoark-ai/eva-client-sdk-ts",
3
- "version": "0.0.2-dev",
4
- "description": "Eva 端侧 TypeScript SDK(契约驱动、浏览器优先)。",
3
+ "version": "0.0.4-dev",
4
+ "description": "EVA 端侧 TypeScript SDK(契约驱动、浏览器优先)。",
5
+ "keywords": [
6
+ "eva-client-sdk",
7
+ "typescript",
8
+ "conversational-ai",
9
+ "voice-agent"
10
+ ],
5
11
  "license": "SEE LICENSE IN LICENSE",
6
12
  "type": "module",
7
13
  "main": "./dist/index.js",
@@ -47,7 +53,6 @@
47
53
  "build": "node scripts/build.mjs",
48
54
  "check": "npm run build && npm run typecheck && npm run typecheck:test && npm run depcruise && npm test && npm run test:browser && npm run verify:pack",
49
55
  "depcruise": "depcruise src --config .dependency-cruiser.cjs",
50
- "quickstart:prepare": "npm run version:sync && npm run build && node scripts/prepare-browser-quickstart.mjs",
51
56
  "test": "vitest run",
52
57
  "test:browser": "vitest run --config vitest.browser.config.ts",
53
58
  "verify:consumer": "node scripts/verify-clean-consumer.mjs",