perso-interactive-sdk-web 1.2.2 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -59,6 +59,7 @@ app.post("/api/session", async (req, res) => {
59
59
  llm_type: "<llm_name>",
60
60
  tts_type: "<tts_name>",
61
61
  stt_type: "<stt_name>",
62
+ // text_normalization_config: "<textnormalizationconfig_id>", // optional
62
63
  });
63
64
  res.json({ sessionId });
64
65
  } catch (error) {
@@ -70,6 +71,14 @@ app.post("/api/session", async (req, res) => {
70
71
  app.listen(3000, () => console.log("Server running on port 3000"));
71
72
  ```
72
73
 
74
+ #### Using a SessionTemplate
75
+
76
+ If you have pre-configured session templates, pass the template ID directly instead of assembling params manually:
77
+
78
+ ```javascript
79
+ const sessionId = await createSessionId(API_SERVER, API_KEY, "<sessiontemplate_id>");
80
+ ```
81
+
73
82
  > ⚠️ **Security Warning**: Never use `createSessionId` on the client-side in production. Exposing your API key in browser code can lead to unauthorized access and quota abuse. Always create sessions on the server and pass only the `sessionId` to the client.
74
83
 
75
84
  #### Client-side Testing Only
@@ -92,6 +101,7 @@ const sessionId = await createSessionId(apiServer, apiKey, {
92
101
  llm_type: "<llm_name>",
93
102
  tts_type: "<tts_name>",
94
103
  stt_type: "<stt_name>",
104
+ // text_normalization_config: "<textnormalizationconfig_id>", // optional
95
105
  });
96
106
 
97
107
  const session = await createSession(apiServer, sessionId, 1920, 1080, []);
@@ -270,8 +280,11 @@ For direct browser usage via `<script>` tag without a bundler. The SDK exposes a
270
280
 
271
281
  | Export | Description |
272
282
  | ---------------------------------------------- | ------------------------------ |
283
+ | `createSessionId(apiServer, apiKey, sessionTemplateId)` | Create a session ID from a SessionTemplate |
273
284
  | `createSessionId(apiServer, apiKey, params)` | Create a new session ID |
274
285
  | `getIntroMessage(apiServer, apiKey, promptId)` | Get intro message for a prompt |
286
+ | `getSessionTemplates(apiServer, apiKey)` | Get available session templates |
287
+ | `getSessionTemplate(apiServer, apiKey, sessionTemplateId)` | Get a single session template by ID |
275
288
  | `PersoUtilServer` | Low-level API utilities |
276
289
  | `ApiError` | Error class for API errors |
277
290
 
@@ -291,9 +304,14 @@ For direct browser usage via `<script>` tag without a bundler. The SDK exposes a
291
304
  | `getPrompts(apiServer, apiKey)` | Get available prompts |
292
305
  | `getDocuments(apiServer, apiKey)` | Get available documents |
293
306
  | `getMcpServers(apiServer, apiKey)` | Get available MCP servers |
307
+ | `getTextNormalizations(apiServer, apiKey)` | Get available text normalization configs |
308
+ | `getTextNormalization(apiServer, apiKey, configId)` | Download text normalization ruleset (pre-signed URL) |
294
309
  | `getAllSettings(apiServer, apiKey)` | Get all settings at once |
295
310
  | `getSessionInfo(apiServer, sessionId)` | Get session metadata |
311
+ | `makeTTS(apiServer, params)` | Generate TTS audio from text (standalone) |
312
+ | `createSessionId(apiServer, apiKey, sessionTemplateId)` | Create session ID from a SessionTemplate (exposes API key) |
296
313
  | `createSessionId(apiServer, apiKey, params)` | Create session ID (exposes API key in browser) |
314
+ | `getSessionTemplates(apiServer, apiKey)` | Get available session templates |
297
315
  | `ApiError` | Error class for API errors |
298
316
  | `LLMError` | Error class for LLM errors |
299
317
  | `LLMStreamingResponseError` | Error class for streaming errors |
@@ -314,7 +332,7 @@ For direct browser usage via `<script>` tag without a bundler. The SDK exposes a
314
332
  | `processChat(message)` | Send a message to the LLM |
315
333
  | `processLLM(options)` | Stream LLM responses with full control |
316
334
  | `processTTSTF(message)` | Speak a message without LLM |
317
- | `processTTS(message, options?)` | Generate TTS audio from text (returns Blob) |
335
+ | `processTTS(message, options?)` | Generate TTS audio from text (returns Blob). Options: `resample`, `locale`, `output_format` |
318
336
  | `processSTF(file, format, message)` | Send audio/video to STF pipeline |
319
337
  | `startProcessSTT(timeout?)` | Start recording voice for STT |
320
338
  | `stopProcessSTT(language?)` | Stop recording and get text |
@@ -1 +1 @@
1
- "use strict";var t,e,s=require("emoji-regex");class a extends Error{constructor(){super("WebRTC connection timeout")}}class r extends Error{errorCode;code;detail;attr;constructor(t,e,s,a){let r;r=null!=a?`${t}:${a}_${s}`:`${t}:${s}`,super(r),this.errorCode=t,this.code=e,this.detail=s,this.attr=a}}class n extends Error{underlyingError;constructor(t){super(),this.underlyingError=t}}class o extends Error{description;constructor(t){super(),this.description=t}}class i extends Error{underlyingError;constructor(t){super(`STT Error: ${t.detail}`),this.underlyingError=t}}class l extends Error{underlyingError;constructor(t){super(t.message),this.underlyingError=t}}class c extends Error{description;constructor(t){super(`TTS decode error: ${t}`),this.description=t}}!function(t){t.LLM="LLM",t.TTS="TTS",t.STT="STT",t.STF_ONPREMISE="STF_ONPREMISE",t.STF_WEBRTC="STF_WEBRTC"}(t||(t={})),function(t){t.SESSION_START="SESSION_START",t.SESSION_DURING="SESSION_DURING",t.SESSION_LOG="SESSION_LOG",t.SESSION_END="SESSION_END",t.SESSION_ERROR="SESSION_ERROR",t.SESSION_TTS="SESSION_TTS",t.SESSION_STT="SESSION_STT",t.SESSION_LLM="SESSION_LLM"}(e||(e={}));class h{static async getLLMs(t,e){const s=fetch(`${t}/api/v1/settings/llm_type/`,{headers:{"PersoLive-APIKey":e},method:"GET"}),a=await s;return await this.parseJson(a)}static async getModelStyles(t,e){const s=fetch(`${t}/api/v1/settings/modelstyle/?platform_type=webrtc`,{headers:{"PersoLive-APIKey":e},method:"GET"}),a=await s;return await this.parseJson(a)}static async getBackgroundImages(t,e){const s=fetch(`${t}/api/v1/background_image/`,{headers:{"PersoLive-APIKey":e},method:"GET"}),a=await s;return await this.parseJson(a)}static async getTTSs(t,e){const s=fetch(`${t}/api/v1/settings/tts_type/`,{headers:{"PersoLive-APIKey":e},method:"GET"}),a=await s;return await this.parseJson(a)}static async getSTTs(t,e){const s=fetch(`${t}/api/v1/settings/stt_type/`,{headers:{"PersoLive-APIKey":e},method:"GET"}),a=await s;return await this.parseJson(a)}static async makeTTS(t,{sessionId:e,text:s}){const a=await fetch(`${t}/api/v1/session/${e}/tts/`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({text:s})});return await this.parseJson(a)}static async getPrompts(t,e){const s=fetch(`${t}/api/v1/prompt/`,{headers:{"PersoLive-APIKey":e},method:"GET"}),a=await s;return await this.parseJson(a)}static async getDocuments(t,e){const s=fetch(`${t}/api/v1/document/`,{headers:{"PersoLive-APIKey":e},method:"GET"}),a=await s;return await this.parseJson(a)}static async getMcpServers(t,e){const s=fetch(`${t}/api/v1/settings/mcp_type/`,{headers:{"PersoLive-APIKey":e},method:"GET"}),a=await s;return await this.parseJson(a)}static async getSessionInfo(t,e){const s=fetch(`${t}/api/v1/session/${e}/`,{method:"GET"}),a=await s;return await this.parseJson(a)}static async sessionEvent(t,e,s){const a=await fetch(`${t}/api/v1/session/${e}/event/create/`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({detail:"",event:s})});await this.parseJson(a)}static async makeSTT(t,e,s,a){const r=new FormData;r.append("audio",s),a&&r.append("language",a);const n=await fetch(`${t}/api/v1/session/${e}/stt/`,{method:"POST",body:r});return await this.parseJson(n)}static async makeLLM(t,e,s,a){const n=await fetch(`${t}/api/v1/session/${e}/llm/v2/`,{body:JSON.stringify(s),headers:{"Content-Type":"application/json"},method:"POST",signal:a});if(!n.ok){const t=await n.json(),e=t.errors?.[0]??{code:"UNKNOWN_ERROR",detail:`Server returned status ${n.status} with no error details`,attr:null};throw new r(n.status,e.code,e.detail,e.attr)}return n.body.getReader()}static async getIceServers(t,e){const s=await fetch(`${t}/api/v1/session/${e}/ice-servers/`,{method:"GET"});return(await this.parseJson(s)).ice_servers}static async exchangeSDP(t,e,s){const a=await fetch(`${t}/api/v1/session/${e}/exchange/`,{body:JSON.stringify({client_sdp:s}),headers:{"Content-Type":"application/json"},method:"POST"});return(await this.parseJson(a)).server_sdp}static async parseJson(t){const e=await t.json();if(t.ok)return e;{const s=e.errors?.[0]??{code:"UNKNOWN_ERROR",detail:`Server returned status ${t.status} with no error details`,attr:null};throw new r(t.status,s.code,s.detail,s.attr)}}}class d extends Error{constructor(t){super(`WAV parse error: ${t}`),this.name="WavParseError"}}function u(t,e,s=1){const a=2*t.length,r=new ArrayBuffer(44+a),n=new DataView(r);g(n,0,"RIFF"),n.setUint32(4,36+a,!0),g(n,8,"WAVE"),g(n,12,"fmt "),n.setUint32(16,16,!0),n.setUint16(20,1,!0),n.setUint16(22,s,!0),n.setUint32(24,e,!0),n.setUint32(28,e*s*2,!0),n.setUint16(32,2*s,!0),n.setUint16(34,16,!0),g(n,36,"data"),n.setUint32(40,a,!0);let o=44;for(let e=0;e<t.length;e++){const s=Math.max(-1,Math.min(1,t[e]));n.setInt16(o,s<0?32768*s:32767*s,!0),o+=2}return r}function p(t,e,s){let a="";for(let r=0;r<s;r++)a+=String.fromCharCode(t.getUint8(e+r));return a}function g(t,e,s){for(let a=0;a<s.length;a++)t.setUint8(e+a,s.charCodeAt(a))}function S(t,e,s){switch(s){case 8:return(t.getUint8(e)-128)/128;case 16:return t.getInt16(e,!0)/32768;case 24:{const s=t.getUint8(e),a=t.getUint8(e+1),r=t.getUint8(e+2)<<16|a<<8|s;return(r>8388607?r-16777216:r)/8388608}case 32:return t.getInt32(e,!0)/2147483648;default:return 0}}class m extends Error{constructor(t){super(`Audio resample error: ${t}`),this.name="AudioResampleError"}}async function f(t,e,s,a=1){if(0===t.length)throw new m("Cannot resample empty audio data");if(e<=0||s<=0)throw new m(`Invalid sample rate: original=${e}, target=${s}`);if(e===s)return t;try{const r=t.length/e,n=Math.ceil(r*s),o=new OfflineAudioContext(a,t.length,e).createBuffer(a,t.length,e);o.getChannelData(0).set(t);const i=new OfflineAudioContext(a,n,s),l=i.createBufferSource();l.buffer=o,l.connect(i.destination),l.start(0);return(await i.startRendering()).getChannelData(0)}catch(t){const a=t instanceof Error?t.message:String(t);throw new m(`Failed to resample audio from ${e}Hz to ${s}Hz: ${a}`)}}const w=16e3;async function y(t,e=!0){let s;try{const e=atob(t),a=new Array(e.length);for(let t=0;t<e.length;t++)a[t]=e.charCodeAt(t);s=new Uint8Array(a).buffer}catch{throw new c("Invalid Base64 audio data")}const a=function(t){const e=new Uint8Array(t);if(e.length>=4){const t=String.fromCharCode(e[0],e[1],e[2],e[3]);if("RIFF"===t)return"audio/wav";if(t.startsWith("ID3"))return"audio/mpeg";if(255===e[0]&&!(224&~e[1]))return"audio/mpeg"}return"audio/wav"}(s);if(!e)return new Blob([s],{type:a});try{const t=await async function(t,e){if("audio/wav"===e){const e=function(t){const e=new DataView(t);if(t.byteLength<44)throw new d("File too small to be a valid WAV");if("RIFF"!==p(e,0,4))throw new d("Missing RIFF header");if("WAVE"!==p(e,8,4))throw new d("Missing WAVE format identifier");let s=12,a=!1,r=0,n=0,o=0,i=0;for(;s<t.byteLength-8;){const l=p(e,s,4),c=e.getUint32(s+4,!0);if("fmt "===l){if(s+24>t.byteLength)throw new d("fmt chunk extends beyond file");r=e.getUint16(s+8,!0),n=e.getUint16(s+10,!0),o=e.getUint32(s+12,!0),i=e.getUint16(s+22,!0),a=!0,s+=8+c;break}const h=s+8+c;if(h<=s)break;s=h}if(!a)throw new d("Missing fmt chunk");if(1!==r)throw new d(`Unsupported audio format: ${r} (only PCM format 1 is supported)`);if(0===n||n>8)throw new d(`Invalid channel count: ${n}`);if(0===o||o>192e3)throw new d(`Invalid sample rate: ${o}`);if(![8,16,24,32].includes(i))throw new d(`Unsupported bits per sample: ${i}`);let l=-1,c=0;for(;s<t.byteLength-8;){const t=p(e,s,4),a=e.getUint32(s+4,!0);if("data"===t){l=s+8,c=a;break}const r=s+8+a;if(r<=s)break;s=r}if(-1===l)throw new d("Missing data chunk");const h=t.byteLength-l,u=Math.min(c,h),g=i/8,m=Math.floor(u/(g*n)),f=new Float32Array(m*n);let w=0;for(let s=0;s<m*n;s++){const a=l+s*g;if(a+g>t.byteLength)break;f[w++]=Math.max(-1,Math.min(1,S(e,a,i)))}if(2===n){const t=new Float32Array(m);for(let e=0;e<m;e++)t[e]=(f[2*e]+f[2*e+1])/2;return{sampleRate:o,channels:1,bitsPerSample:i,samples:t}}return{sampleRate:o,channels:n,bitsPerSample:i,samples:f.slice(0,w)}}(t);if(e.sampleRate===w)return{samples:e.samples,sampleRate:e.sampleRate};return{samples:await f(e.samples,e.sampleRate,w,e.channels),sampleRate:w}}const s=new AudioContext({sampleRate:w});try{const e=await s.decodeAudioData(t.slice(0));if(e.sampleRate===w)return{samples:e.getChannelData(0),sampleRate:w};return{samples:await f(e.getChannelData(0),e.sampleRate,w,1),sampleRate:w}}finally{await s.close()}}(s,a),e=u(t.samples,w,1);return new Blob([e],{type:"audio/wav"})}catch{return new Blob([s],{type:a})}}var T;exports.ChatState=void 0,(T=exports.ChatState||(exports.ChatState={})).RECORDING="RECORDING",T.LLM="LLM",T.ANALYZING="ANALYZING",T.SPEAKING="SPEAKING",T.TTS="TTS";class C{config;messageHistory=[];constructor(t){this.config=t}async*processLLM(t){if(0===t.message.length)throw new Error("Message cannot be empty");const e=t.tools??this.config.clientTools,s=e.map(t=>({type:"function",function:{description:t.description,name:t.name,parameters:t.parameters}})),a={newMessageHistory:[{role:"user",content:t.message}],allChunks:[],message:"",lastYieldedChunkCount:0,pendingToolCallsMessage:null,aborted:!1,streamingError:null};let i=0,l=[...this.messageHistory,...a.newMessageHistory];this.config.callbacks.onChatStateChange(exports.ChatState.LLM,null);try{for(;;){if(t.signal?.aborted)return void(a.allChunks.length>0&&(yield{type:"message",chunks:[...a.allChunks],message:a.message,finish:!0}));let c;try{c=await h.makeLLM(this.config.apiServer,this.config.sessionId,{messages:l,tools:s},t.signal)}catch(t){if(t instanceof r)return void(yield{type:"error",error:new n(t)});throw t}if(a.streamingError=null,yield*this.parseSSEStream(c,a,t),a.streamingError)return;if(a.aborted)return void(a.allChunks.length>0&&(yield{type:"message",chunks:[...a.allChunks],message:a.message,finish:!0}));if(null!=a.pendingToolCallsMessage){yield*this.executeToolCalls(a,e);const t=a.lastToolCallResults,s=t.length>0&&a.pendingToolCallsMessage.tool_calls.length!==t.length,r=t.some(t=>!t.chatTool.executeOnly);if(s||r){if(i++,i>=10)return void(yield{type:"error",error:new n(new o("Tool follow-up loop exceeded maximum rounds (10)"))});l=[...this.messageHistory,...a.newMessageHistory],a.pendingToolCallsMessage=null;continue}}return this.messageHistory.push(...a.newMessageHistory),void(yield{type:"message",chunks:[...a.allChunks],message:a.message,finish:!0})}}finally{this.config.callbacks.onChatStateChange(null,exports.ChatState.LLM)}}async*parseSSEStream(t,e,s){const a=new TextDecoder("utf-8");let r="",i="";e.pendingToolCallsMessage=null;const l=()=>e.allChunks.length>e.lastYieldedChunkCount?(e.lastYieldedChunkCount=e.allChunks.length,{type:"message",chunks:[...e.allChunks],message:e.message,finish:!1}):null;for(;;){const{done:c,value:h}=await t.read();if(c)break;let d;for(r+=a.decode(h,{stream:!0});-1!==(d=r.indexOf("\n"));){if(s.signal?.aborted)return void(e.aborted=!0);const t=r.slice(0,d).trim();if(r=r.slice(d+1),!t.startsWith("data: {"))return e.streamingError=new n(new o("Failed to parse SSE response")),void(yield{type:"error",error:e.streamingError});let a;try{a=JSON.parse(t.slice(6).trim())}catch{return e.streamingError=new n(new o("Failed to parse SSE JSON")),void(yield{type:"error",error:e.streamingError})}if("success"!==a.status)return e.streamingError=new n(new o(a.reason)),void(yield{type:"error",error:e.streamingError});if(i.length>0&&"message"!=a.type){e.newMessageHistory.push({role:"assistant",type:"message",content:i}),i="";const t=l();t&&(yield t)}"message"!==a.type?"tool_call"!==a.type||null==a.tool_calls?"tool"!==a.role||"tool_call"===a.type&&e.newMessageHistory.push({role:a.role,type:a.type,content:a.content,tool_call_id:a.tool_call_id}):(e.newMessageHistory.push({role:"assistant",type:a.type,content:a.content,tool_calls:a.tool_calls}),e.pendingToolCallsMessage=a,yield{type:"tool_call",tool_calls:a.tool_calls}):(i+=a.content,e.message+=a.content,e.allChunks.push(a.content))}const u=l();u&&(yield u)}const c=r.trim();if(c.length>0){if(!c.startsWith("data: {"))return e.streamingError=new n(new o("Failed to parse SSE response")),void(yield{type:"error",error:e.streamingError});let t;try{t=JSON.parse(c.slice(6).trim())}catch{return e.streamingError=new n(new o("Failed to parse SSE JSON")),void(yield{type:"error",error:e.streamingError})}if("success"!==t.status)return e.streamingError=new n(new o(t.reason)),void(yield{type:"error",error:e.streamingError});if("message"===t.type)i+=t.content,e.message+=t.content,e.allChunks.push(t.content);else if("tool_call"===t.type&&null!=t.tool_calls){if(i.length>0){e.newMessageHistory.push({role:"assistant",type:"message",content:i}),i="";const t=l();t&&(yield t)}e.newMessageHistory.push({role:"assistant",type:t.type,content:t.content,tool_calls:t.tool_calls}),e.pendingToolCallsMessage=t,yield{type:"tool_call",tool_calls:t.tool_calls}}}i.length>0&&e.newMessageHistory.push({role:"assistant",type:"message",content:i})}async*executeToolCalls(t,e){const s=t=>{for(const s of e)if(s.name===t)return s;return null},a=[];for(const e of t.pendingToolCallsMessage.tool_calls){const t=s(e.function.name);null!=t&&a.push((async()=>{try{const s=await t.call(JSON.parse(e.function.arguments));return{toolCallId:e.id,chatTool:t,chatToolResult:s}}catch(s){return{toolCallId:e.id,chatTool:t,chatToolResult:{error:s.message}}}})())}const r=await Promise.all(a);t.lastToolCallResults=r;for(const e of r)t.newMessageHistory.push({role:"tool",content:JSON.stringify(e.chatToolResult),tool_call_id:e.toolCallId}),yield{type:"tool_result",tool_call_id:e.toolCallId,result:e.chatToolResult}}addToHistory(t){this.messageHistory.push(t)}getHistory(){return this.messageHistory}}const E=`data:application/javascript,${encodeURIComponent("\nclass RecorderProcessor extends AudioWorkletProcessor {\n constructor() {\n super();\n this.isRecording = true;\n \n // Listen for stop message from main thread\n this.port.onmessage = (event) => {\n if (event.data.type === 'stop') {\n this.isRecording = false;\n // Send confirmation back to main thread\n this.port.postMessage({ type: 'stopped' });\n }\n };\n }\n\n process(inputs, outputs, parameters) {\n const input = inputs[0];\n if (input && input.length > 0 && this.isRecording) {\n // Clone the audio data and send to main thread\n const channelData = new Float32Array(input[0]);\n this.port.postMessage({ type: 'audio', data: channelData });\n }\n // Return true to keep the processor alive until stopped\n return this.isRecording;\n }\n}\n\nregisterProcessor('recorder-processor', RecorderProcessor);\n")}`;class v{audioContext=null;mediaStream=null;workletNode=null;sourceNode=null;audioChunks=[];isRecordingState=!1;channels;targetSampleRate;constructor(t={}){this.channels=t.channels||1,this.targetSampleRate=t.targetSampleRate}async start(){if(this.isRecordingState)throw new Error("WavRecorder is already recording");if(this.mediaStream=await navigator.mediaDevices.getUserMedia({audio:!0}),this.audioContext=new AudioContext,"running"!==this.audioContext.state)try{await this.audioContext.resume()}catch(t){console.warn("WavRecorder: Failed to resume AudioContext:",t)}await this.audioContext.audioWorklet.addModule(E),this.sourceNode=this.audioContext.createMediaStreamSource(this.mediaStream),this.workletNode=new AudioWorkletNode(this.audioContext,"recorder-processor"),this.audioChunks=[],this.workletNode.port.onmessage=t=>{"audio"===t.data.type&&this.audioChunks.push(t.data.data)},this.sourceNode.connect(this.workletNode),this.workletNode.connect(this.audioContext.destination),this.isRecordingState=!0}async stop(){if(!this.isRecordingState)throw new Error("WavRecorder is not recording");this.isRecordingState=!1,await new Promise(t=>{this.workletNode.port.onmessage=e=>{"stopped"===e.data.type?t():"audio"===e.data.type&&this.audioChunks.push(e.data.data)},this.workletNode.port.postMessage({type:"stop"})}),this.workletNode?.disconnect(),this.sourceNode?.disconnect(),this.mediaStream?.getTracks().forEach(t=>t.stop());const t=this.audioChunks,e=this.audioContext.sampleRate;try{const s=t.reduce((t,e)=>t+e.length,0),a=new Float32Array(s);let r,n,o=0;for(const e of t)a.set(e,o),o+=e.length;this.targetSampleRate&&this.targetSampleRate!==e?(r=await f(a,e,this.targetSampleRate,this.channels),n=this.targetSampleRate):(r=a,n=e);const i=u(r,n,this.channels),l=new Blob([i],{type:"audio/wav"});return new File([l],"recording.wav",{type:"audio/wav"})}finally{await(this.audioContext?.close()),this.audioContext=null,this.mediaStream=null,this.workletNode=null,this.sourceNode=null,this.audioChunks=[]}}isRecording(){return this.isRecordingState}}class R extends EventTarget{pc;dc;streams=[];pingTime;pingIntervalId=null;constructor(t,e){super(),this.pc=t,this.dc=e,this.pingTime=Date.now()+3e3,this.pc.addEventListener("track",t=>{this.streams=this.streams.concat(t.streams)}),this.pc.addEventListener("connectionstatechange",()=>{"disconnected"!==this.pc.connectionState&&"failed"!==this.pc.connectionState||this.close()}),this.dc.onopen=()=>{this.pingIntervalId=setInterval(()=>{this.ping(),Date.now()-this.pingTime>5e3&&this.close()},1e3)},this.dc.onclose=()=>{null!=this.pingIntervalId&&clearInterval(this.pingIntervalId)},this.#t({live:!0,code:200,reason:"OK"}),this.setMessageCallback("ping",()=>{this.pingTime=Date.now()})}static async create(s,a,r,n,o){const i=await h.getSessionInfo(s,a);if(!(Array.isArray(i.capability)&&i.capability.some(e=>e.name===t.STF_ONPREMISE||e.name===t.STF_WEBRTC)))return await h.sessionEvent(s,a,e.SESSION_START),null;const l=await h.getIceServers(s,a);let c=await R.createPeerConnection(l),d=c.createDataChannel("message",{protocol:"message"}),u=new R(c,d);o?o.getTracks().forEach(function(t){c.addTrack(t,o)}):c.addTransceiver("audio",{direction:"recvonly"});const p=c.addTransceiver("video",{direction:"recvonly"}),g=RTCRtpReceiver.getCapabilities("video");null!=g&&p.setCodecPreferences(g.codecs);const S=await c.createOffer();await c.setLocalDescription(S);const m=await h.exchangeSDP(s,a,S);return await c.setRemoteDescription(m),await R.waitFor(()=>u.isReady(),100,50),u.changeSize(r,n),u}static async createPeerConnection(t){return new RTCPeerConnection({sdpSemantics:"unified-plan",iceServers:t})}static async waitFor(t,e,s){let r=0;if(await new Promise(a=>{const n=setInterval(()=>{r+=1,r>=s&&(clearInterval(n),a("bad")),t()&&(clearInterval(n),a("good"))},e)}),r>=s)throw new a}isReady(){return this.streams.length>0&&"open"===this.dc.readyState}#t(t){this.dispatchEvent(new CustomEvent("status",{detail:t}))}subscribeStatus(t){return this.addEventListener("status",t),()=>{this.removeEventListener("status",t)}}getStream(){return this.streams[0]}sendMessage(t,e){this.dc.send(JSON.stringify({type:t,data:e}))}ttstf(t){this.sendMessage("ttstf",{message:t})}static BACKPRESSURE_THRESHOLD=524288;sendFile(t,e=65536){return new Promise((s,a)=>{const r=this.pc.createDataChannel("file",{protocol:"file"});r.onerror=t=>{r.close(),a(new Error(`File channel error: ${t}`))},r.addEventListener("message",async n=>{try{if(0===n.data.length){const s=new Uint8Array(await t.arrayBuffer());let n=0;const o=()=>{for(;n<s.length;){if(r.bufferedAmount>R.BACKPRESSURE_THRESHOLD)return r.bufferedAmountLowThreshold=R.BACKPRESSURE_THRESHOLD/2,r.onbufferedamountlow=()=>{r.onbufferedamountlow=null,r.onclose=null,o()},void(r.onclose=()=>{r.onbufferedamountlow=null,a(new Error("File channel closed during transfer"))});r.send(s.slice(n,n+e)),n+=e}r.send(new Uint8Array(0))};o()}else r.close(),s(n.data)}catch(t){r.close(),a(t)}})})}async stf(t,e,s){const a=await this.sendFile(t);return this.sendMessage("stf",{message:s,file_ref:a,format:e}),a}recordStart(){this.sendMessage("record-start",{})}recordEndStt(t){this.sendMessage("record-end-stt",{language:t})}recordEndTranslate(t,e){this.sendMessage("record-end-translate",{src_lang:t,dst_lang:e})}changeSize(t,e){this.sendMessage("change-size",{width:t,height:e})}setTemplate(t,e){this.sendMessage("set-template",{model:t,dress:e})}clearBuffer(){this.sendMessage("clear-buffer",{})}ping(){this.sendMessage("ping",{})}setMessageCallback(t,e){const s=s=>{const a=JSON.parse(s.data);a.type===t&&e(a.data)};return this.dc.addEventListener("message",s),()=>{this.dc.removeEventListener("message",s)}}async tts(t,e=!0){return y(t,e)}close(){this.dc.close(),this.pc.close(),this.#t({live:!1,code:408,reason:"Request Timeout"})}closeSelf(){this.dc.close(),this.pc.close(),this.#t({live:!1,code:200,reason:"OK"})}}class I{apiServer;sessionId;perso;clientTools;chatStatesHandler=new EventTarget;chatLogHandler=new EventTarget;sttEventHandler=null;errorHandler=new EventTarget;lastStfTimeoutHandle=null;stfTotalDuration=0;stfTimeoutStartTime=0;messageHistory=[];chatLog=[];llmProcessor;chatStateMap=new Map([[exports.ChatState.RECORDING,0],[exports.ChatState.LLM,0],[exports.ChatState.ANALYZING,0],[exports.ChatState.SPEAKING,0],[exports.ChatState.TTS,0]]);emojiRegex=s();sttRecorder=null;sttTimeoutHandle=null;sttTimeoutAudioFile=null;heartbeatIntervalId=null;legacyVoiceChatMode;stream;constructor(t,e,s,a,r){this.apiServer=t,this.sessionId=e,this.perso=s,this.clientTools=a,this.legacyVoiceChatMode=r?.legacyVoiceChatMode??!1,this.stream=r?.stream??null,this.resetChatState(),this.llmProcessor=new C({apiServer:t,sessionId:e,clientTools:a,callbacks:{onChatStateChange:(t,e)=>this.setChatState(t,e),onError:t=>this.setError(t),onChatLog:(t,e)=>this.addMessageToChatLog(t,e),onTTSTF:t=>this.processTTSTFInternal(t)}}),this.startHeartbeat(),s&&(s.subscribeStatus(t=>{!1===t.detail?.live&&this.stopHeartbeat()}),s.setMessageCallback("stf",t=>{if(this.chatStateMap.get(exports.ChatState.ANALYZING)||this.chatStateMap.get(exports.ChatState.SPEAKING))if(this.setChatState(exports.ChatState.SPEAKING,exports.ChatState.ANALYZING),null!==this.lastStfTimeoutHandle){clearTimeout(this.lastStfTimeoutHandle);let e=Date.now();this.stfTotalDuration+=t.duration+1e3-(e-this.stfTimeoutStartTime),this.stfTimeoutStartTime=e,this.lastStfTimeoutHandle=setTimeout(()=>{this.lastStfTimeoutHandle=null,this.stfTimeoutStartTime=0,this.stfTotalDuration=0,this.setChatState(null,exports.ChatState.SPEAKING)},this.stfTotalDuration)}else this.stfTimeoutStartTime=Date.now(),this.stfTotalDuration=t.duration+2e3,this.lastStfTimeoutHandle=setTimeout(()=>{this.lastStfTimeoutHandle=null,this.stfTimeoutStartTime=0,this.stfTotalDuration=0,this.setChatState(null,exports.ChatState.SPEAKING)},this.stfTotalDuration)}),s.setMessageCallback("stt",t=>{if(this.setChatState(null,exports.ChatState.ANALYZING),null!=this.sttEventHandler)this.sttEventHandler.dispatchEvent(new CustomEvent("stt",{detail:t.text}));else{if(""===t.text)return;this.processChat(t.text)}}),s.setMessageCallback("stt-error",t=>{this.setChatState(null,exports.ChatState.ANALYZING)}))}llmJob=null;async processChat(t){0!==t.trim().length&&(this.pipelineSuppressed=!1,this.addMessageToChatLog(t,!0),this.llmJob=this.processChatInternal(t))}processLLM(t){return this.pipelineSuppressed=!1,this.llmProcessor.processLLM(t)}getMessageHistory(){return this.llmProcessor.getHistory()}processCustomChat(t){0!==t.trim().length&&this.processTTSTFInternal(t)}processTTSTF(t){0!==t.trim().length&&(this.pipelineSuppressed=!1,this.messageHistory.push({role:"assistant",type:"message",content:t}),this.addMessageToChatLog(t,!1),this.processTTSTFInternal(t))}async transcribeAudio(t,e){const s=t instanceof File?t:new File([t],"audio.wav",{type:t.type});try{return(await h.makeSTT(this.apiServer,this.sessionId,s,e)).text}catch(t){if(t instanceof r)throw new i(t);throw t}}async processSTF(t,e,s){if(!this.perso)throw new Error("processSTF requires WebRTC (STF mode)");this.pipelineSuppressed=!1,this.setChatState(exports.ChatState.ANALYZING);try{const a=await this.perso.stf(t,e,s);return this.pipelineSuppressed?(this.setChatState(null,exports.ChatState.ANALYZING),a):a}catch(t){throw this.setChatState(null,exports.ChatState.ANALYZING),t}}async processTTS(t,e={}){const{resample:s=!0}=e,a=this.removeEmoji(t).trim();if(0===a.length)return;this.pipelineSuppressed=!1;const n=/[.?!]$/.test(a)?a:a+".";this.setChatState(exports.ChatState.TTS,null);try{const{audio:t}=await h.makeTTS(this.apiServer,{sessionId:this.sessionId,text:n});if(this.pipelineSuppressed)return;return await y(t,s)}catch(t){t instanceof r||t instanceof c?this.setError(new l(t)):this.setError(t instanceof Error?t:new Error(String(t)))}finally{this.setChatState(null,exports.ChatState.TTS)}}startVoiceChat(){if(!this.perso)throw new Error("startVoiceChat requires WebRTC (STF mode)");return this.pipelineSuppressed=!1,this.setChatState(exports.ChatState.RECORDING),this.perso.recordStart()}stopVoiceChat(){if(!this.perso)throw new Error("stopVoiceChat requires WebRTC (STF mode)");this.setChatState(exports.ChatState.ANALYZING,exports.ChatState.RECORDING),this.perso.recordEndStt()}async startProcessSTT(t){if(this.sttRecorder?.isRecording())throw new Error("STT recording is already in progress");this.pipelineSuppressed=!1,this.setChatState(exports.ChatState.RECORDING);try{this.sttRecorder=new v({targetSampleRate:16e3}),await this.sttRecorder.start(),t&&t>0&&(this.sttTimeoutHandle=setTimeout(async()=>{if(this.sttTimeoutHandle=null,this.sttRecorder?.isRecording()){try{this.sttTimeoutAudioFile=await this.sttRecorder.stop()}catch{this.sttTimeoutAudioFile=null,this.setChatState(null,exports.ChatState.RECORDING)}this.sttRecorder=null}},t))}catch(t){throw this.setChatState(null,exports.ChatState.RECORDING),this.sttRecorder=null,t}}lastRecordedAudioFile=null;async stopProcessSTT(t){let e;if(this.sttTimeoutHandle&&(clearTimeout(this.sttTimeoutHandle),this.sttTimeoutHandle=null),this.setChatState(null,exports.ChatState.RECORDING),this.sttTimeoutAudioFile)e=this.sttTimeoutAudioFile,this.sttTimeoutAudioFile=null;else{if(!this.sttRecorder?.isRecording())throw this.sttRecorder?(this.sttRecorder=null,new Error("STT recording is not in progress")):new Error("STT recording has not been started");e=await this.sttRecorder.stop(),this.sttRecorder=null}this.lastRecordedAudioFile=e;try{return(await h.makeSTT(this.apiServer,this.sessionId,e,t)).text}catch(t){if(t instanceof r)throw new i(t);throw t}}isSTTRecording(){return(this.sttRecorder?.isRecording()??!1)||null!==this.sttTimeoutAudioFile}changeSize(t,e){this.perso?.changeSize(t,e)}async clearBuffer(){this.perso?.clearBuffer(),await this.clearLLMJob(),null!==this.lastStfTimeoutHandle&&(clearTimeout(this.lastStfTimeoutHandle),this.lastStfTimeoutHandle=null),this.pipelineSuppressed=!0,this.resetChatState()}setSrc(t){t.srcObject=this.getRemoteStream()??null}getRemoteStream(){return this.perso?.getStream()}getLocalStream(){return this.stream}stopSession(){this.close()}onClose(t){return this.perso?this.perso.subscribeStatus(e=>{null!=e.detail&&!1===e.detail.live&&t(200===e.detail.code)}):()=>{}}subscribeChatStates(t){const e=e=>{t(e.detail.status)};return this.chatStatesHandler.addEventListener("status",e),()=>{this.chatStatesHandler.removeEventListener("status",e)}}subscribeChatLog(t){const e=e=>{t(e.detail.chatLog)};return this.chatLogHandler.addEventListener("chatLog",e),()=>{this.chatLogHandler.removeEventListener("chatLog",e)}}setSttResultCallback(t){const e=e=>{t(e.detail)};return this.sttEventHandler=new EventTarget,this.sttEventHandler.addEventListener("stt",e),()=>{this.sttEventHandler?.removeEventListener("stt",e),this.sttEventHandler=null}}setErrorHandler(t){const e=e=>{t(e.detail.error)};return this.errorHandler.addEventListener("error",e),()=>{this.errorHandler.removeEventListener("error",e)}}getSessionId(){return this.sessionId}async processChatInternal(t){this.setChatState(exports.ChatState.LLM);const e=this.clientTools.map(t=>({type:"function",function:{description:t.description,name:t.name,parameters:t.parameters}})),s=new Array;null===t||(t instanceof Array?s.push(...t):"string"==typeof t&&s.push({role:"user",content:t}));const a=await fetch(`${this.apiServer}/api/v1/session/${this.sessionId}/llm/v2/`,{body:JSON.stringify({messages:[...this.messageHistory,...s],tools:e}),headers:{"Content-Type":"application/json"},method:"POST"});if(!a.ok){const t=await a.json(),e=new n(new r(a.status,t.errors[0].code,t.errors[0].detail,t.errors[0].attr));return this.setError(e),void this.setChatState(null,exports.ChatState.LLM)}const i=a.body?.getReader(),l=new TextDecoder("utf-8");let c="",h=null,d="";for(;;){const{done:t,value:e}=await i.read();if(t)break;let a;for(d+=l.decode(e,{stream:!0});-1!==(a=d.indexOf("\n"));){if(this.llmCancel)return c.length>0&&this.addMessageToChatLog(c,!1),void this.setChatState(null,exports.ChatState.LLM);const t=d.slice(0,a).trim();if(d=d.slice(a+1),!t.startsWith("data: {")){const t=new n(new o("Failed to parse SSE response"));return this.setError(t),void this.setChatState(null,exports.ChatState.LLM)}const e=JSON.parse(t.slice(6).trim());if("success"!==e.status){const t=new n(new o(e.reason));return this.setError(t),void this.setChatState(null,exports.ChatState.LLM)}c.length>0&&"message"!=e.type&&(s.push({role:"assistant",type:"message",content:c}),this.addMessageToChatLog(c,!1),c=""),"message"!==e.type?"tool_call"!==e.type||null==e.tool_calls?"tool"!==e.role||"tool_call"===e.type&&s.push({role:e.role,type:e.type,content:e.content,tool_call_id:e.tool_call_id}):(s.push({role:"assistant",type:e.type,content:e.content,tool_calls:e.tool_calls}),h=e):(c+=e.content,this.processTTSTFInternal(e.content))}}if(this.llmCancel)this.setChatState(null,exports.ChatState.LLM);else{if(null!=h){const t=[];for(const e of h.tool_calls){const s=this.getChatTool(this.clientTools,e.function.name);null!=s&&t.push(new Promise(async t=>{try{const a=await s.call(JSON.parse(e.function.arguments));t({toolCallId:e.id,chatTool:s,chatToolResult:a})}catch(a){t({toolCallId:e.id,chatTool:s,chatToolResult:{result:"error!"}})}}))}const e=await Promise.all(t);for(const t of e)s.push({role:"tool",content:JSON.stringify(t.chatToolResult),tool_call_id:t.toolCallId});const a=e.length>0&&h.tool_calls.length!==e.length,r=e.some(t=>!t.chatTool.executeOnly);a||r?await this.processChatInternal(s):this.messageHistory.push(...s)}else this.messageHistory.push(...s);this.setChatState(null,exports.ChatState.LLM)}}getChatTool(t,e){for(const s of t)if(s.name===e)return s;return null}llmCancel=!1;pipelineSuppressed=!1;async clearLLMJob(){null!=this.llmJob&&(this.llmCancel=!0,await this.llmJob,this.llmCancel=!1)}processTTSTFInternal(t){const e=this.removeEmoji(t).trim();0!==e.length&&this.perso&&(this.setChatState(exports.ChatState.ANALYZING),this.perso.ttstf(e))}addMessageToChatLog(t,e){this.chatLog=[{text:t,isUser:e,timestamp:new Date},...this.chatLog],this.chatLogHandler.dispatchEvent(new CustomEvent("chatLog",{detail:{chatLog:this.chatLog}}))}setChatState(t=null,e=null){const s=new Map(this.chatStateMap);function a(t){t===exports.ChatState.ANALYZING?s.set(t,(s.get(t)||0)+1):s.set(t,1)}function r(t){t===exports.ChatState.ANALYZING?s.set(t,Math.max((s.get(t)||0)-1,0)):s.set(t,0)}if(null!=t)if(t instanceof Array)for(let e of t)a(e);else a(t);if(null!=e)if(e instanceof Array)for(let t of e)r(t);else r(e);const n=this.exchangeChatStateMapToSet(this.chatStateMap),o=this.exchangeChatStateMapToSet(s);this.chatStateMap=s,this.isEqualChatStateMap(n,o)||this.dispatchChatState(o)}resetChatState(){this.chatStateMap=new Map([[exports.ChatState.RECORDING,0],[exports.ChatState.LLM,0],[exports.ChatState.ANALYZING,0],[exports.ChatState.SPEAKING,0],[exports.ChatState.TTS,0]]),this.dispatchChatState(this.exchangeChatStateMapToSet(this.chatStateMap))}exchangeChatStateMapToSet(t){const e=new Set;for(const s of t)s[1]>0&&e.add(s[0]);return e}dispatchChatState(t){this.chatStatesHandler.dispatchEvent(new CustomEvent("status",{detail:{status:t}}))}isEqualChatStateMap(t,e){if(t.size!==e.size)return!1;for(const s of t)if(t.has(s)!==e.has(s))return!1;return!0}setError(t){this.errorHandler.dispatchEvent(new CustomEvent("error",{detail:{error:t}}))}close(){this.stopHeartbeat(),this.perso?.closeSelf()}startHeartbeat(){const t=async()=>{try{await h.sessionEvent(this.apiServer,this.sessionId,e.SESSION_DURING),null!==this.heartbeatIntervalId&&(this.heartbeatIntervalId=setTimeout(t,1e4))}catch(t){t instanceof r?this.setError(t):this.setError(t instanceof Error?t:new Error(String(t))),this.close()}};this.heartbeatIntervalId=setTimeout(t,1e4)}stopHeartbeat(){null!==this.heartbeatIntervalId&&(clearTimeout(this.heartbeatIntervalId),this.heartbeatIntervalId=null)}removeEmoji(t){return t.replace(this.emojiRegex,"")}}async function L(t,e,s,a,r,n){if("boolean"!=typeof r){const n=await R.create(t,e,s,a);return new I(t,e,n,r)}const o=n??[];let i,l;if(r)i=await navigator.mediaDevices.getUserMedia({audio:!0,video:!1}),l=()=>{};else{const t=new AudioContext,e=t.createOscillator();e.frequency.value=0;const s=t.createMediaStreamDestination();e.connect(s),e.start(),i=s.stream,l=()=>{e.stop(),e.disconnect(s),t.close()}}const c=await R.create(t,e,s,a,i);if(!c)return l(),new I(t,e,null,o);const h=new I(t,e,c,o,{stream:i,legacyVoiceChatMode:!0});return h.onClose(()=>{l()}),h}async function x(t,e){return await h.getLLMs(t,e)}async function M(t,e){return await h.getTTSs(t,e)}async function b(t,e){return await h.getSTTs(t,e)}async function N(t,e){return await h.getModelStyles(t,e)}async function A(t,e){return await h.getBackgroundImages(t,e)}async function _(t,e){return await h.getPrompts(t,e)}async function k(t,e){return await h.getDocuments(t,e)}async function O(t,e){return await h.getMcpServers(t,e)}exports.ApiError=r,exports.ChatTool=class{name;description;parameters;call;executeOnly;constructor(t,e,s,a,r=!1){this.name=t,this.description=e,this.parameters=s,this.call=a,this.executeOnly=r}},exports.LLMError=n,exports.LLMStreamingResponseError=o,exports.LlmProcessor=C,exports.STTError=i,exports.Session=I,exports.TTSDecodeError=c,exports.TTSError=l,exports.TTS_TARGET_SAMPLE_RATE=w,exports.WavRecorder=v,exports.createSession=async function(t,e,s,a,r,n){return"boolean"==typeof r?await L(t,e,s,a,r,n??[]):await L(t,e,s,a,r)},exports.createSessionId=async(e,s,a)=>{"undefined"!=typeof window&&console.warn("[perso-interactive-sdk-web] WARNING: createSessionId is being called from the browser. This exposes your API key and is not recommended for production. Use server-side session creation with 'perso-interactive-sdk-web/server' instead. See: https://github.com/perso-ai/perso-interactive-sdk-web#server-side");const r={capability:[],...a};a.using_stf_webrtc&&r.capability.push(t.STF_WEBRTC),a?.llm_type&&(r.capability.push(t.LLM),r.llm_type=a.llm_type),a?.tts_type&&(r.capability.push(t.TTS),r.tts_type=a.tts_type),a?.stt_type&&(r.capability.push(t.STT),r.stt_type=a.stt_type);const n=await fetch(`${e}/api/v1/session/`,{body:JSON.stringify(r),headers:{"PersoLive-APIKey":s,"Content-Type":"application/json"},method:"POST"});return(await h.parseJson(n)).session_id},exports.createWavRecorder=function(t){return new v(t)},exports.getAllSettings=async function(t,e){const s=await x(t,e),a=await N(t,e),r=await A(t,e);return{llms:s,ttsTypes:await M(t,e),sttTypes:await b(t,e),modelStyles:a,backgroundImages:r,prompts:await _(t,e),documents:await k(t,e),mcpServers:await O(t,e)}},exports.getBackgroundImages=A,exports.getDocuments=k,exports.getLLMs=x,exports.getMcpServers=O,exports.getModelStyles=N,exports.getPrompts=_,exports.getSTTs=b,exports.getSessionInfo=async function(t,e){return await h.getSessionInfo(t,e)},exports.getTTSs=M,exports.getWavSampleRate=function(t){const e=new DataView(t);if(t.byteLength<28)throw new d("File too small to be a valid WAV");if("RIFF"!==p(e,0,4))throw new d("Missing RIFF header");let s=12;for(;s<t.byteLength-8;){const a=p(e,s,4),r=e.getUint32(s+4,!0);if("fmt "===a){if(s+16>t.byteLength)throw new d("fmt chunk extends beyond file");return e.getUint32(s+12,!0)}const n=s+8+r;if(n<=s)break;s=n}throw new d("Missing fmt chunk")};
1
+ "use strict";var t,e,s=require("emoji-regex");class a extends Error{constructor(){super("WebRTC connection timeout")}}class r extends Error{errorCode;code;detail;attr;constructor(t,e,s,a){let r;r=null!=a?`${t}:${a}_${s}`:`${t}:${s}`,super(r),this.errorCode=t,this.code=e,this.detail=s,this.attr=a}}class n extends Error{underlyingError;constructor(t){super(),this.underlyingError=t}}class o extends Error{description;constructor(t){super(),this.description=t}}class i extends Error{underlyingError;constructor(t){super(`STT Error: ${t.detail}`),this.underlyingError=t}}class l extends Error{underlyingError;constructor(t){super(t.message),this.underlyingError=t}}class c extends Error{description;constructor(t){super(`TTS decode error: ${t}`),this.description=t}}!function(t){t.LLM="LLM",t.TTS="TTS",t.STT="STT",t.STF_ONPREMISE="STF_ONPREMISE",t.STF_WEBRTC="STF_WEBRTC"}(t||(t={})),function(t){t.SESSION_START="SESSION_START",t.SESSION_DURING="SESSION_DURING",t.SESSION_LOG="SESSION_LOG",t.SESSION_END="SESSION_END",t.SESSION_ERROR="SESSION_ERROR",t.SESSION_TTS="SESSION_TTS",t.SESSION_STT="SESSION_STT",t.SESSION_LLM="SESSION_LLM"}(e||(e={}));class h{static async getLLMs(t,e){const s=fetch(`${t}/api/v1/settings/llm_type/`,{headers:{"PersoLive-APIKey":e},method:"GET"}),a=await s;return await this.parseJson(a)}static async getModelStyles(t,e){const s=fetch(`${t}/api/v1/settings/modelstyle/?platform_type=webrtc`,{headers:{"PersoLive-APIKey":e},method:"GET"}),a=await s;return await this.parseJson(a)}static async getBackgroundImages(t,e){const s=fetch(`${t}/api/v1/background_image/`,{headers:{"PersoLive-APIKey":e},method:"GET"}),a=await s;return await this.parseJson(a)}static async getTTSs(t,e){const s=fetch(`${t}/api/v1/settings/tts_type/`,{headers:{"PersoLive-APIKey":e},method:"GET"}),a=await s;return await this.parseJson(a)}static async getSTTs(t,e){const s=fetch(`${t}/api/v1/settings/stt_type/`,{headers:{"PersoLive-APIKey":e},method:"GET"}),a=await s;return await this.parseJson(a)}static async makeTTS(t,{sessionId:e,text:s,locale:a,output_format:r}){const n={text:s};a&&(n.locale=a),r&&(n.output_format=r);const o=await fetch(`${t}/api/v1/session/${e}/tts/`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(n)});return await this.parseJson(o)}static async getPrompts(t,e){const s=fetch(`${t}/api/v1/prompt/`,{headers:{"PersoLive-APIKey":e},method:"GET"}),a=await s;return await this.parseJson(a)}static async getDocuments(t,e){const s=fetch(`${t}/api/v1/document/`,{headers:{"PersoLive-APIKey":e},method:"GET"}),a=await s;return await this.parseJson(a)}static async getTextNormalizations(t,e){const s=fetch(`${t}/api/v1/settings/text_normalization_config/`,{headers:{"PersoLive-APIKey":e},method:"GET"}),a=await s;return await this.parseJson(a)}static async downloadTextNormalization(t,e,s){const a=await fetch(`${t}/api/v1/settings/text_normalization_config/${s}/download/`,{headers:{"PersoLive-APIKey":e},method:"GET"});return await this.parseJson(a)}static async getSessionTemplates(t,e){const s=await fetch(`${t}/api/v1/session_template/`,{headers:{"PersoLive-APIKey":e},method:"GET"});return await this.parseJson(s)}static async getSessionTemplate(t,e,s){const a=await fetch(`${t}/api/v1/session_template/${s}/`,{headers:{"PersoLive-APIKey":e},method:"GET"});return await this.parseJson(a)}static async getMcpServers(t,e){const s=fetch(`${t}/api/v1/settings/mcp_type/`,{headers:{"PersoLive-APIKey":e},method:"GET"}),a=await s;return await this.parseJson(a)}static async getSessionInfo(t,e){const s=fetch(`${t}/api/v1/session/${e}/`,{method:"GET"}),a=await s;return await this.parseJson(a)}static async sessionEvent(t,e,s){const a=await fetch(`${t}/api/v1/session/${e}/event/create/`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({detail:"",event:s})});await this.parseJson(a)}static async makeSTT(t,e,s,a){const r=new FormData;r.append("audio",s),a&&r.append("language",a);const n=await fetch(`${t}/api/v1/session/${e}/stt/`,{method:"POST",body:r});return await this.parseJson(n)}static async makeLLM(t,e,s,a){const n=await fetch(`${t}/api/v1/session/${e}/llm/v2/`,{body:JSON.stringify(s),headers:{"Content-Type":"application/json"},method:"POST",signal:a});if(!n.ok){const t=await n.json(),e=t.errors?.[0]??{code:"UNKNOWN_ERROR",detail:`Server returned status ${n.status} with no error details`,attr:null};throw new r(n.status,e.code,e.detail,e.attr)}return n.body.getReader()}static async getIceServers(t,e){const s=await fetch(`${t}/api/v1/session/${e}/ice-servers/`,{method:"GET"});return(await this.parseJson(s)).ice_servers}static async exchangeSDP(t,e,s){const a=await fetch(`${t}/api/v1/session/${e}/exchange/`,{body:JSON.stringify({client_sdp:s}),headers:{"Content-Type":"application/json"},method:"POST"});return(await this.parseJson(a)).server_sdp}static async parseJson(t){const e=await t.json();if(t.ok)return e;{const s=e.errors?.[0]??{code:"UNKNOWN_ERROR",detail:`Server returned status ${t.status} with no error details`,attr:null};throw new r(t.status,s.code,s.detail,s.attr)}}}class d extends Error{constructor(t){super(`WAV parse error: ${t}`),this.name="WavParseError"}}function u(t,e,s=1){const a=2*t.length,r=new ArrayBuffer(44+a),n=new DataView(r);g(n,0,"RIFF"),n.setUint32(4,36+a,!0),g(n,8,"WAVE"),g(n,12,"fmt "),n.setUint32(16,16,!0),n.setUint16(20,1,!0),n.setUint16(22,s,!0),n.setUint32(24,e,!0),n.setUint32(28,e*s*2,!0),n.setUint16(32,2*s,!0),n.setUint16(34,16,!0),g(n,36,"data"),n.setUint32(40,a,!0);let o=44;for(let e=0;e<t.length;e++){const s=Math.max(-1,Math.min(1,t[e]));n.setInt16(o,s<0?32768*s:32767*s,!0),o+=2}return r}function p(t,e,s){let a="";for(let r=0;r<s;r++)a+=String.fromCharCode(t.getUint8(e+r));return a}function g(t,e,s){for(let a=0;a<s.length;a++)t.setUint8(e+a,s.charCodeAt(a))}function m(t,e,s){switch(s){case 8:return(t.getUint8(e)-128)/128;case 16:return t.getInt16(e,!0)/32768;case 24:{const s=t.getUint8(e),a=t.getUint8(e+1),r=t.getUint8(e+2)<<16|a<<8|s;return(r>8388607?r-16777216:r)/8388608}case 32:return t.getInt32(e,!0)/2147483648;default:return 0}}class S extends Error{constructor(t){super(`Audio resample error: ${t}`),this.name="AudioResampleError"}}async function f(t,e,s,a=1){if(0===t.length)throw new S("Cannot resample empty audio data");if(e<=0||s<=0)throw new S(`Invalid sample rate: original=${e}, target=${s}`);if(e===s)return t;try{const r=t.length/e,n=Math.ceil(r*s),o=new OfflineAudioContext(a,t.length,e).createBuffer(a,t.length,e);o.getChannelData(0).set(t);const i=new OfflineAudioContext(a,n,s),l=i.createBufferSource();l.buffer=o,l.connect(i.destination),l.start(0);return(await i.startRendering()).getChannelData(0)}catch(t){const a=t instanceof Error?t.message:String(t);throw new S(`Failed to resample audio from ${e}Hz to ${s}Hz: ${a}`)}}const y=16e3;async function w(t,e=!0){let s;try{const e=atob(t),a=new Array(e.length);for(let t=0;t<e.length;t++)a[t]=e.charCodeAt(t);s=new Uint8Array(a).buffer}catch{throw new c("Invalid Base64 audio data")}const a=function(t){const e=new Uint8Array(t);if(e.length>=4){const t=String.fromCharCode(e[0],e[1],e[2],e[3]);if("RIFF"===t)return"audio/wav";if(t.startsWith("ID3"))return"audio/mpeg";if(255===e[0]&&!(224&~e[1]))return"audio/mpeg"}return"audio/wav"}(s);if(!e)return new Blob([s],{type:a});try{const t=await async function(t,e){if("audio/wav"===e){const e=function(t){const e=new DataView(t);if(t.byteLength<44)throw new d("File too small to be a valid WAV");if("RIFF"!==p(e,0,4))throw new d("Missing RIFF header");if("WAVE"!==p(e,8,4))throw new d("Missing WAVE format identifier");let s=12,a=!1,r=0,n=0,o=0,i=0;for(;s<t.byteLength-8;){const l=p(e,s,4),c=e.getUint32(s+4,!0);if("fmt "===l){if(s+24>t.byteLength)throw new d("fmt chunk extends beyond file");r=e.getUint16(s+8,!0),n=e.getUint16(s+10,!0),o=e.getUint32(s+12,!0),i=e.getUint16(s+22,!0),a=!0,s+=8+c;break}const h=s+8+c;if(h<=s)break;s=h}if(!a)throw new d("Missing fmt chunk");if(1!==r)throw new d(`Unsupported audio format: ${r} (only PCM format 1 is supported)`);if(0===n||n>8)throw new d(`Invalid channel count: ${n}`);if(0===o||o>192e3)throw new d(`Invalid sample rate: ${o}`);if(![8,16,24,32].includes(i))throw new d(`Unsupported bits per sample: ${i}`);let l=-1,c=0;for(;s<t.byteLength-8;){const t=p(e,s,4),a=e.getUint32(s+4,!0);if("data"===t){l=s+8,c=a;break}const r=s+8+a;if(r<=s)break;s=r}if(-1===l)throw new d("Missing data chunk");const h=t.byteLength-l,u=Math.min(c,h),g=i/8,S=Math.floor(u/(g*n)),f=new Float32Array(S*n);let y=0;for(let s=0;s<S*n;s++){const a=l+s*g;if(a+g>t.byteLength)break;f[y++]=Math.max(-1,Math.min(1,m(e,a,i)))}if(2===n){const t=new Float32Array(S);for(let e=0;e<S;e++)t[e]=(f[2*e]+f[2*e+1])/2;return{sampleRate:o,channels:1,bitsPerSample:i,samples:t}}return{sampleRate:o,channels:n,bitsPerSample:i,samples:f.slice(0,y)}}(t);if(e.sampleRate===y)return{samples:e.samples,sampleRate:e.sampleRate};return{samples:await f(e.samples,e.sampleRate,y,e.channels),sampleRate:y}}const s=new AudioContext({sampleRate:y});try{const e=await s.decodeAudioData(t.slice(0));if(e.sampleRate===y)return{samples:e.getChannelData(0),sampleRate:y};return{samples:await f(e.getChannelData(0),e.sampleRate,y,1),sampleRate:y}}finally{await s.close()}}(s,a),e=u(t.samples,y,1);return new Blob([e],{type:"audio/wav"})}catch{return new Blob([s],{type:a})}}var T;exports.ChatState=void 0,(T=exports.ChatState||(exports.ChatState={})).RECORDING="RECORDING",T.LLM="LLM",T.ANALYZING="ANALYZING",T.SPEAKING="SPEAKING",T.TTS="TTS";class C{config;messageHistory=[];constructor(t){this.config=t}async*processLLM(t){if(0===t.message.length)throw new Error("Message cannot be empty");const e=t.tools??this.config.clientTools,s=e.map(t=>({type:"function",function:{description:t.description,name:t.name,parameters:t.parameters}})),a={newMessageHistory:[{role:"user",content:t.message}],allChunks:[],message:"",lastYieldedChunkCount:0,pendingToolCallsMessage:null,aborted:!1,streamingError:null};let i=0,l=[...this.messageHistory,...a.newMessageHistory];this.config.callbacks.onChatStateChange(exports.ChatState.LLM,null);try{for(;;){if(t.signal?.aborted)return void(a.allChunks.length>0&&(yield{type:"message",chunks:[...a.allChunks],message:a.message,finish:!0}));let c;try{c=await h.makeLLM(this.config.apiServer,this.config.sessionId,{messages:l,tools:s},t.signal)}catch(t){if(t instanceof r)return void(yield{type:"error",error:new n(t)});throw t}if(a.streamingError=null,yield*this.parseSSEStream(c,a,t),a.streamingError)return;if(a.aborted)return void(a.allChunks.length>0&&(yield{type:"message",chunks:[...a.allChunks],message:a.message,finish:!0}));if(null!=a.pendingToolCallsMessage){yield*this.executeToolCalls(a,e);const t=a.lastToolCallResults,s=t.length>0&&a.pendingToolCallsMessage.tool_calls.length!==t.length,r=t.some(t=>!t.chatTool.executeOnly);if(s||r){if(i++,i>=10)return void(yield{type:"error",error:new n(new o("Tool follow-up loop exceeded maximum rounds (10)"))});l=[...this.messageHistory,...a.newMessageHistory],a.pendingToolCallsMessage=null;continue}}return this.messageHistory.push(...a.newMessageHistory),void(yield{type:"message",chunks:[...a.allChunks],message:a.message,finish:!0})}}finally{this.config.callbacks.onChatStateChange(null,exports.ChatState.LLM)}}async*parseSSEStream(t,e,s){const a=new TextDecoder("utf-8");let r="",i="";e.pendingToolCallsMessage=null;const l=()=>e.allChunks.length>e.lastYieldedChunkCount?(e.lastYieldedChunkCount=e.allChunks.length,{type:"message",chunks:[...e.allChunks],message:e.message,finish:!1}):null;for(;;){const{done:c,value:h}=await t.read();if(c)break;let d;for(r+=a.decode(h,{stream:!0});-1!==(d=r.indexOf("\n"));){if(s.signal?.aborted)return void(e.aborted=!0);const t=r.slice(0,d).trim();if(r=r.slice(d+1),!t.startsWith("data: {"))return e.streamingError=new n(new o("Failed to parse SSE response")),void(yield{type:"error",error:e.streamingError});let a;try{a=JSON.parse(t.slice(6).trim())}catch{return e.streamingError=new n(new o("Failed to parse SSE JSON")),void(yield{type:"error",error:e.streamingError})}if("success"!==a.status)return e.streamingError=new n(new o(a.reason)),void(yield{type:"error",error:e.streamingError});if(i.length>0&&"message"!=a.type){e.newMessageHistory.push({role:"assistant",type:"message",content:i}),i="";const t=l();t&&(yield t)}"message"!==a.type?"tool_call"!==a.type||null==a.tool_calls?"tool"!==a.role||"tool_call"===a.type&&e.newMessageHistory.push({role:a.role,type:a.type,content:a.content,tool_call_id:a.tool_call_id}):(e.newMessageHistory.push({role:"assistant",type:a.type,content:a.content,tool_calls:a.tool_calls}),e.pendingToolCallsMessage=a,yield{type:"tool_call",tool_calls:a.tool_calls}):(i+=a.content,e.message+=a.content,e.allChunks.push(a.content))}const u=l();u&&(yield u)}const c=r.trim();if(c.length>0){if(!c.startsWith("data: {"))return e.streamingError=new n(new o("Failed to parse SSE response")),void(yield{type:"error",error:e.streamingError});let t;try{t=JSON.parse(c.slice(6).trim())}catch{return e.streamingError=new n(new o("Failed to parse SSE JSON")),void(yield{type:"error",error:e.streamingError})}if("success"!==t.status)return e.streamingError=new n(new o(t.reason)),void(yield{type:"error",error:e.streamingError});if("message"===t.type)i+=t.content,e.message+=t.content,e.allChunks.push(t.content);else if("tool_call"===t.type&&null!=t.tool_calls){if(i.length>0){e.newMessageHistory.push({role:"assistant",type:"message",content:i}),i="";const t=l();t&&(yield t)}e.newMessageHistory.push({role:"assistant",type:t.type,content:t.content,tool_calls:t.tool_calls}),e.pendingToolCallsMessage=t,yield{type:"tool_call",tool_calls:t.tool_calls}}}i.length>0&&e.newMessageHistory.push({role:"assistant",type:"message",content:i})}async*executeToolCalls(t,e){const s=t=>{for(const s of e)if(s.name===t)return s;return null},a=[];for(const e of t.pendingToolCallsMessage.tool_calls){const t=s(e.function.name);null!=t&&a.push((async()=>{try{const s=await t.call(JSON.parse(e.function.arguments));return{toolCallId:e.id,chatTool:t,chatToolResult:s}}catch(s){return{toolCallId:e.id,chatTool:t,chatToolResult:{error:s.message}}}})())}const r=await Promise.all(a);t.lastToolCallResults=r;for(const e of r)t.newMessageHistory.push({role:"tool",content:JSON.stringify(e.chatToolResult),tool_call_id:e.toolCallId}),yield{type:"tool_result",tool_call_id:e.toolCallId,result:e.chatToolResult}}addToHistory(t){this.messageHistory.push(t)}getHistory(){return this.messageHistory}}const v=`data:application/javascript,${encodeURIComponent("\nclass RecorderProcessor extends AudioWorkletProcessor {\n constructor() {\n super();\n this.isRecording = true;\n \n // Listen for stop message from main thread\n this.port.onmessage = (event) => {\n if (event.data.type === 'stop') {\n this.isRecording = false;\n // Send confirmation back to main thread\n this.port.postMessage({ type: 'stopped' });\n }\n };\n }\n\n process(inputs, outputs, parameters) {\n const input = inputs[0];\n if (input && input.length > 0 && this.isRecording) {\n // Clone the audio data and send to main thread\n const channelData = new Float32Array(input[0]);\n this.port.postMessage({ type: 'audio', data: channelData });\n }\n // Return true to keep the processor alive until stopped\n return this.isRecording;\n }\n}\n\nregisterProcessor('recorder-processor', RecorderProcessor);\n")}`;class E{audioContext=null;mediaStream=null;workletNode=null;sourceNode=null;audioChunks=[];isRecordingState=!1;channels;targetSampleRate;constructor(t={}){this.channels=t.channels||1,this.targetSampleRate=t.targetSampleRate}async start(){if(this.isRecordingState)throw new Error("WavRecorder is already recording");if(this.mediaStream=await navigator.mediaDevices.getUserMedia({audio:!0}),this.audioContext=new AudioContext,"running"!==this.audioContext.state)try{await this.audioContext.resume()}catch(t){console.warn("WavRecorder: Failed to resume AudioContext:",t)}await this.audioContext.audioWorklet.addModule(v),this.sourceNode=this.audioContext.createMediaStreamSource(this.mediaStream),this.workletNode=new AudioWorkletNode(this.audioContext,"recorder-processor"),this.audioChunks=[],this.workletNode.port.onmessage=t=>{"audio"===t.data.type&&this.audioChunks.push(t.data.data)},this.sourceNode.connect(this.workletNode),this.workletNode.connect(this.audioContext.destination),this.isRecordingState=!0}async stop(){if(!this.isRecordingState)throw new Error("WavRecorder is not recording");this.isRecordingState=!1,await new Promise(t=>{this.workletNode.port.onmessage=e=>{"stopped"===e.data.type?t():"audio"===e.data.type&&this.audioChunks.push(e.data.data)},this.workletNode.port.postMessage({type:"stop"})}),this.workletNode?.disconnect(),this.sourceNode?.disconnect(),this.mediaStream?.getTracks().forEach(t=>t.stop());const t=this.audioChunks,e=this.audioContext.sampleRate;try{const s=t.reduce((t,e)=>t+e.length,0),a=new Float32Array(s);let r,n,o=0;for(const e of t)a.set(e,o),o+=e.length;this.targetSampleRate&&this.targetSampleRate!==e?(r=await f(a,e,this.targetSampleRate,this.channels),n=this.targetSampleRate):(r=a,n=e);const i=u(r,n,this.channels),l=new Blob([i],{type:"audio/wav"});return new File([l],"recording.wav",{type:"audio/wav"})}finally{await(this.audioContext?.close()),this.audioContext=null,this.mediaStream=null,this.workletNode=null,this.sourceNode=null,this.audioChunks=[]}}isRecording(){return this.isRecordingState}}class R extends EventTarget{pc;dc;streams=[];pingTime;pingIntervalId=null;constructor(t,e){super(),this.pc=t,this.dc=e,this.pingTime=Date.now()+3e3,this.pc.addEventListener("track",t=>{this.streams=this.streams.concat(t.streams)}),this.pc.addEventListener("connectionstatechange",()=>{"disconnected"!==this.pc.connectionState&&"failed"!==this.pc.connectionState||this.close()}),this.dc.onopen=()=>{this.pingIntervalId=setInterval(()=>{this.ping(),Date.now()-this.pingTime>5e3&&this.close()},1e3)},this.dc.onclose=()=>{null!=this.pingIntervalId&&clearInterval(this.pingIntervalId)},this.#t({live:!0,code:200,reason:"OK"}),this.setMessageCallback("ping",()=>{this.pingTime=Date.now()})}static async create(s,a,r,n,o){const i=await h.getSessionInfo(s,a);if(!(Array.isArray(i.capability)&&i.capability.some(e=>e.name===t.STF_ONPREMISE||e.name===t.STF_WEBRTC)))return await h.sessionEvent(s,a,e.SESSION_START),null;const l=await h.getIceServers(s,a);let c=await R.createPeerConnection(l),d=c.createDataChannel("message",{protocol:"message"}),u=new R(c,d);o?o.getTracks().forEach(function(t){c.addTrack(t,o)}):c.addTransceiver("audio",{direction:"recvonly"});const p=c.addTransceiver("video",{direction:"recvonly"}),g=RTCRtpReceiver.getCapabilities("video");null!=g&&p.setCodecPreferences(g.codecs);const m=await c.createOffer();await c.setLocalDescription(m);const S=await h.exchangeSDP(s,a,m);return await c.setRemoteDescription(S),await R.waitFor(()=>u.isReady(),100,50),u.changeSize(r,n),u}static async createPeerConnection(t){return new RTCPeerConnection({sdpSemantics:"unified-plan",iceServers:t})}static async waitFor(t,e,s){let r=0;if(await new Promise(a=>{const n=setInterval(()=>{r+=1,r>=s&&(clearInterval(n),a("bad")),t()&&(clearInterval(n),a("good"))},e)}),r>=s)throw new a}isReady(){return this.streams.length>0&&"open"===this.dc.readyState}#t(t){this.dispatchEvent(new CustomEvent("status",{detail:t}))}subscribeStatus(t){return this.addEventListener("status",t),()=>{this.removeEventListener("status",t)}}getStream(){return this.streams[0]}sendMessage(t,e){this.dc.send(JSON.stringify({type:t,data:e}))}ttstf(t){this.sendMessage("ttstf",{message:t})}static BACKPRESSURE_THRESHOLD=524288;sendFile(t,e=65536){return new Promise((s,a)=>{const r=this.pc.createDataChannel("file",{protocol:"file"});r.onerror=t=>{r.close(),a(new Error(`File channel error: ${t}`))},r.addEventListener("message",async n=>{try{if(0===n.data.length){const s=new Uint8Array(await t.arrayBuffer());let n=0;const o=()=>{for(;n<s.length;){if(r.bufferedAmount>R.BACKPRESSURE_THRESHOLD)return r.bufferedAmountLowThreshold=R.BACKPRESSURE_THRESHOLD/2,r.onbufferedamountlow=()=>{r.onbufferedamountlow=null,r.onclose=null,o()},void(r.onclose=()=>{r.onbufferedamountlow=null,a(new Error("File channel closed during transfer"))});r.send(s.slice(n,n+e)),n+=e}r.send(new Uint8Array(0))};o()}else r.close(),s(n.data)}catch(t){r.close(),a(t)}})})}async stf(t,e,s){const a=await this.sendFile(t);return this.sendMessage("stf",{message:s,file_ref:a,format:e}),a}recordStart(){this.sendMessage("record-start",{})}recordEndStt(t){this.sendMessage("record-end-stt",{language:t})}recordEndTranslate(t,e){this.sendMessage("record-end-translate",{src_lang:t,dst_lang:e})}changeSize(t,e){this.sendMessage("change-size",{width:t,height:e})}setTemplate(t,e){this.sendMessage("set-template",{model:t,dress:e})}clearBuffer(){this.sendMessage("clear-buffer",{})}ping(){this.sendMessage("ping",{})}setMessageCallback(t,e){const s=s=>{const a=JSON.parse(s.data);a.type===t&&e(a.data)};return this.dc.addEventListener("message",s),()=>{this.dc.removeEventListener("message",s)}}async tts(t,e=!0){return w(t,e)}close(){this.dc.close(),this.pc.close(),this.#t({live:!1,code:408,reason:"Request Timeout"})}closeSelf(){this.dc.close(),this.pc.close(),this.#t({live:!1,code:200,reason:"OK"})}}class I{apiServer;sessionId;perso;clientTools;chatStatesHandler=new EventTarget;chatLogHandler=new EventTarget;sttEventHandler=null;errorHandler=new EventTarget;lastStfTimeoutHandle=null;stfTotalDuration=0;stfTimeoutStartTime=0;messageHistory=[];chatLog=[];llmProcessor;chatStateMap=new Map([[exports.ChatState.RECORDING,0],[exports.ChatState.LLM,0],[exports.ChatState.ANALYZING,0],[exports.ChatState.SPEAKING,0],[exports.ChatState.TTS,0]]);emojiRegex=s();sttRecorder=null;sttTimeoutHandle=null;sttTimeoutAudioFile=null;heartbeatIntervalId=null;legacyVoiceChatMode;stream;constructor(t,e,s,a,r){this.apiServer=t,this.sessionId=e,this.perso=s,this.clientTools=a,this.legacyVoiceChatMode=r?.legacyVoiceChatMode??!1,this.stream=r?.stream??null,this.resetChatState(),this.llmProcessor=new C({apiServer:t,sessionId:e,clientTools:a,callbacks:{onChatStateChange:(t,e)=>this.setChatState(t,e),onError:t=>this.setError(t),onChatLog:(t,e)=>this.addMessageToChatLog(t,e),onTTSTF:t=>this.processTTSTFInternal(t)}}),this.startHeartbeat(),s&&(s.subscribeStatus(t=>{!1===t.detail?.live&&this.stopHeartbeat()}),s.setMessageCallback("stf",t=>{if(this.chatStateMap.get(exports.ChatState.ANALYZING)||this.chatStateMap.get(exports.ChatState.SPEAKING))if(this.setChatState(exports.ChatState.SPEAKING,exports.ChatState.ANALYZING),null!==this.lastStfTimeoutHandle){clearTimeout(this.lastStfTimeoutHandle);let e=Date.now();this.stfTotalDuration+=t.duration+1e3-(e-this.stfTimeoutStartTime),this.stfTimeoutStartTime=e,this.lastStfTimeoutHandle=setTimeout(()=>{this.lastStfTimeoutHandle=null,this.stfTimeoutStartTime=0,this.stfTotalDuration=0,this.setChatState(null,exports.ChatState.SPEAKING)},this.stfTotalDuration)}else this.stfTimeoutStartTime=Date.now(),this.stfTotalDuration=t.duration+2e3,this.lastStfTimeoutHandle=setTimeout(()=>{this.lastStfTimeoutHandle=null,this.stfTimeoutStartTime=0,this.stfTotalDuration=0,this.setChatState(null,exports.ChatState.SPEAKING)},this.stfTotalDuration)}),s.setMessageCallback("stt",t=>{if(this.setChatState(null,exports.ChatState.ANALYZING),null!=this.sttEventHandler)this.sttEventHandler.dispatchEvent(new CustomEvent("stt",{detail:t.text}));else{if(""===t.text)return;this.processChat(t.text)}}),s.setMessageCallback("stt-error",t=>{this.setChatState(null,exports.ChatState.ANALYZING)}))}llmJob=null;async processChat(t){0!==t.trim().length&&(this.pipelineSuppressed=!1,this.addMessageToChatLog(t,!0),this.llmJob=this.processChatInternal(t))}processLLM(t){return this.pipelineSuppressed=!1,this.llmProcessor.processLLM(t)}getMessageHistory(){return this.llmProcessor.getHistory()}processCustomChat(t){0!==t.trim().length&&this.processTTSTFInternal(t)}processTTSTF(t){0!==t.trim().length&&(this.pipelineSuppressed=!1,this.messageHistory.push({role:"assistant",type:"message",content:t}),this.addMessageToChatLog(t,!1),this.processTTSTFInternal(t))}async transcribeAudio(t,e){const s=t instanceof File?t:new File([t],"audio.wav",{type:t.type});try{return(await h.makeSTT(this.apiServer,this.sessionId,s,e)).text}catch(t){if(t instanceof r)throw new i(t);throw t}}async processSTF(t,e,s){if(!this.perso)throw new Error("processSTF requires WebRTC (STF mode)");this.pipelineSuppressed=!1,this.setChatState(exports.ChatState.ANALYZING);try{const a=await this.perso.stf(t,e,s);return this.pipelineSuppressed?(this.setChatState(null,exports.ChatState.ANALYZING),a):a}catch(t){throw this.setChatState(null,exports.ChatState.ANALYZING),t}}async processTTS(t,e={}){const{resample:s=!1,locale:a,output_format:n}=e,o=this.removeEmoji(t).trim();if(0===o.length)return;this.pipelineSuppressed=!1;const i=/[.?!]$/.test(o)?o:o+".";this.setChatState(exports.ChatState.TTS,null);try{const t={sessionId:this.sessionId,text:i,...a&&{locale:a},...n&&{output_format:n}},{audio:e}=await h.makeTTS(this.apiServer,t);if(this.pipelineSuppressed)return;return await w(e,s)}catch(t){t instanceof r||t instanceof c?this.setError(new l(t)):this.setError(t instanceof Error?t:new Error(String(t)))}finally{this.setChatState(null,exports.ChatState.TTS)}}startVoiceChat(){if(!this.perso)throw new Error("startVoiceChat requires WebRTC (STF mode)");return this.pipelineSuppressed=!1,this.setChatState(exports.ChatState.RECORDING),this.perso.recordStart()}stopVoiceChat(){if(!this.perso)throw new Error("stopVoiceChat requires WebRTC (STF mode)");this.setChatState(exports.ChatState.ANALYZING,exports.ChatState.RECORDING),this.perso.recordEndStt()}async startProcessSTT(t){if(this.sttRecorder?.isRecording())throw new Error("STT recording is already in progress");this.pipelineSuppressed=!1,this.setChatState(exports.ChatState.RECORDING);try{this.sttRecorder=new E({targetSampleRate:16e3}),await this.sttRecorder.start(),t&&t>0&&(this.sttTimeoutHandle=setTimeout(async()=>{if(this.sttTimeoutHandle=null,this.sttRecorder?.isRecording()){try{this.sttTimeoutAudioFile=await this.sttRecorder.stop()}catch{this.sttTimeoutAudioFile=null,this.setChatState(null,exports.ChatState.RECORDING)}this.sttRecorder=null}},t))}catch(t){throw this.setChatState(null,exports.ChatState.RECORDING),this.sttRecorder=null,t}}lastRecordedAudioFile=null;async stopProcessSTT(t){let e;if(this.sttTimeoutHandle&&(clearTimeout(this.sttTimeoutHandle),this.sttTimeoutHandle=null),this.setChatState(null,exports.ChatState.RECORDING),this.sttTimeoutAudioFile)e=this.sttTimeoutAudioFile,this.sttTimeoutAudioFile=null;else{if(!this.sttRecorder?.isRecording())throw this.sttRecorder?(this.sttRecorder=null,new Error("STT recording is not in progress")):new Error("STT recording has not been started");e=await this.sttRecorder.stop(),this.sttRecorder=null}this.lastRecordedAudioFile=e;try{return(await h.makeSTT(this.apiServer,this.sessionId,e,t)).text}catch(t){if(t instanceof r)throw new i(t);throw t}}isSTTRecording(){return(this.sttRecorder?.isRecording()??!1)||null!==this.sttTimeoutAudioFile}changeSize(t,e){this.perso?.changeSize(t,e)}async clearBuffer(){this.perso?.clearBuffer(),await this.clearLLMJob(),null!==this.lastStfTimeoutHandle&&(clearTimeout(this.lastStfTimeoutHandle),this.lastStfTimeoutHandle=null),this.pipelineSuppressed=!0,this.resetChatState()}setSrc(t){t.srcObject=this.getRemoteStream()??null}getRemoteStream(){return this.perso?.getStream()}getLocalStream(){return this.stream}stopSession(){this.close()}onClose(t){return this.perso?this.perso.subscribeStatus(e=>{null!=e.detail&&!1===e.detail.live&&t(200===e.detail.code)}):()=>{}}subscribeChatStates(t){const e=e=>{t(e.detail.status)};return this.chatStatesHandler.addEventListener("status",e),()=>{this.chatStatesHandler.removeEventListener("status",e)}}subscribeChatLog(t){const e=e=>{t(e.detail.chatLog)};return this.chatLogHandler.addEventListener("chatLog",e),()=>{this.chatLogHandler.removeEventListener("chatLog",e)}}setSttResultCallback(t){const e=e=>{t(e.detail)};return this.sttEventHandler=new EventTarget,this.sttEventHandler.addEventListener("stt",e),()=>{this.sttEventHandler?.removeEventListener("stt",e),this.sttEventHandler=null}}setErrorHandler(t){const e=e=>{t(e.detail.error)};return this.errorHandler.addEventListener("error",e),()=>{this.errorHandler.removeEventListener("error",e)}}getSessionId(){return this.sessionId}async processChatInternal(t){this.setChatState(exports.ChatState.LLM);const e=this.clientTools.map(t=>({type:"function",function:{description:t.description,name:t.name,parameters:t.parameters}})),s=new Array;null===t||(t instanceof Array?s.push(...t):"string"==typeof t&&s.push({role:"user",content:t}));const a=await fetch(`${this.apiServer}/api/v1/session/${this.sessionId}/llm/v2/`,{body:JSON.stringify({messages:[...this.messageHistory,...s],tools:e}),headers:{"Content-Type":"application/json"},method:"POST"});if(!a.ok){const t=await a.json(),e=new n(new r(a.status,t.errors[0].code,t.errors[0].detail,t.errors[0].attr));return this.setError(e),void this.setChatState(null,exports.ChatState.LLM)}const i=a.body?.getReader(),l=new TextDecoder("utf-8");let c="",h=null,d="";for(;;){const{done:t,value:e}=await i.read();if(t)break;let a;for(d+=l.decode(e,{stream:!0});-1!==(a=d.indexOf("\n"));){if(this.llmCancel)return c.length>0&&this.addMessageToChatLog(c,!1),void this.setChatState(null,exports.ChatState.LLM);const t=d.slice(0,a).trim();if(d=d.slice(a+1),!t.startsWith("data: {")){const t=new n(new o("Failed to parse SSE response"));return this.setError(t),void this.setChatState(null,exports.ChatState.LLM)}const e=JSON.parse(t.slice(6).trim());if("success"!==e.status){const t=new n(new o(e.reason));return this.setError(t),void this.setChatState(null,exports.ChatState.LLM)}c.length>0&&"message"!=e.type&&(s.push({role:"assistant",type:"message",content:c}),this.addMessageToChatLog(c,!1),c=""),"message"!==e.type?"tool_call"!==e.type||null==e.tool_calls?"tool"!==e.role||"tool_call"===e.type&&s.push({role:e.role,type:e.type,content:e.content,tool_call_id:e.tool_call_id}):(s.push({role:"assistant",type:e.type,content:e.content,tool_calls:e.tool_calls}),h=e):(c+=e.content,this.processTTSTFInternal(e.content))}}if(this.llmCancel)this.setChatState(null,exports.ChatState.LLM);else{if(null!=h){const t=[];for(const e of h.tool_calls){const s=this.getChatTool(this.clientTools,e.function.name);null!=s&&t.push(new Promise(async t=>{try{const a=await s.call(JSON.parse(e.function.arguments));t({toolCallId:e.id,chatTool:s,chatToolResult:a})}catch(a){t({toolCallId:e.id,chatTool:s,chatToolResult:{result:"error!"}})}}))}const e=await Promise.all(t);for(const t of e)s.push({role:"tool",content:JSON.stringify(t.chatToolResult),tool_call_id:t.toolCallId});const a=e.length>0&&h.tool_calls.length!==e.length,r=e.some(t=>!t.chatTool.executeOnly);a||r?await this.processChatInternal(s):this.messageHistory.push(...s)}else this.messageHistory.push(...s);this.setChatState(null,exports.ChatState.LLM)}}getChatTool(t,e){for(const s of t)if(s.name===e)return s;return null}llmCancel=!1;pipelineSuppressed=!1;async clearLLMJob(){null!=this.llmJob&&(this.llmCancel=!0,await this.llmJob,this.llmCancel=!1)}processTTSTFInternal(t){const e=this.removeEmoji(t).trim();0!==e.length&&this.perso&&(this.setChatState(exports.ChatState.ANALYZING),this.perso.ttstf(e))}addMessageToChatLog(t,e){this.chatLog=[{text:t,isUser:e,timestamp:new Date},...this.chatLog],this.chatLogHandler.dispatchEvent(new CustomEvent("chatLog",{detail:{chatLog:this.chatLog}}))}setChatState(t=null,e=null){const s=new Map(this.chatStateMap);function a(t){t===exports.ChatState.ANALYZING?s.set(t,(s.get(t)||0)+1):s.set(t,1)}function r(t){t===exports.ChatState.ANALYZING?s.set(t,Math.max((s.get(t)||0)-1,0)):s.set(t,0)}if(null!=t)if(t instanceof Array)for(let e of t)a(e);else a(t);if(null!=e)if(e instanceof Array)for(let t of e)r(t);else r(e);const n=this.exchangeChatStateMapToSet(this.chatStateMap),o=this.exchangeChatStateMapToSet(s);this.chatStateMap=s,this.isEqualChatStateMap(n,o)||this.dispatchChatState(o)}resetChatState(){this.chatStateMap=new Map([[exports.ChatState.RECORDING,0],[exports.ChatState.LLM,0],[exports.ChatState.ANALYZING,0],[exports.ChatState.SPEAKING,0],[exports.ChatState.TTS,0]]),this.dispatchChatState(this.exchangeChatStateMapToSet(this.chatStateMap))}exchangeChatStateMapToSet(t){const e=new Set;for(const s of t)s[1]>0&&e.add(s[0]);return e}dispatchChatState(t){this.chatStatesHandler.dispatchEvent(new CustomEvent("status",{detail:{status:t}}))}isEqualChatStateMap(t,e){if(t.size!==e.size)return!1;for(const s of t)if(t.has(s)!==e.has(s))return!1;return!0}setError(t){this.errorHandler.dispatchEvent(new CustomEvent("error",{detail:{error:t}}))}close(){this.stopHeartbeat(),this.perso?.closeSelf()}startHeartbeat(){const t=async()=>{try{await h.sessionEvent(this.apiServer,this.sessionId,e.SESSION_DURING),null!==this.heartbeatIntervalId&&(this.heartbeatIntervalId=setTimeout(t,1e4))}catch(t){t instanceof r?this.setError(t):this.setError(t instanceof Error?t:new Error(String(t))),this.close()}};this.heartbeatIntervalId=setTimeout(t,1e4)}stopHeartbeat(){null!==this.heartbeatIntervalId&&(clearTimeout(this.heartbeatIntervalId),this.heartbeatIntervalId=null)}removeEmoji(t){return t.replace(this.emojiRegex,"")}}async function x(t,e,s,a,r,n){if("boolean"!=typeof r){const n=await R.create(t,e,s,a);return new I(t,e,n,r)}const o=n??[];let i,l;if(r)i=await navigator.mediaDevices.getUserMedia({audio:!0,video:!1}),l=()=>{};else{const t=new AudioContext,e=t.createOscillator();e.frequency.value=0;const s=t.createMediaStreamDestination();e.connect(s),e.start(),i=s.stream,l=()=>{e.stop(),e.disconnect(s),t.close()}}const c=await R.create(t,e,s,a,i);if(!c)return l(),new I(t,e,null,o);const h=new I(t,e,c,o,{stream:i,legacyVoiceChatMode:!0});return h.onClose(()=>{l()}),h}async function _(t,e){return await h.getLLMs(t,e)}async function L(t,e){return await h.getTTSs(t,e)}async function M(t,e){return await h.getSTTs(t,e)}async function b(t,e){return await h.getModelStyles(t,e)}async function N(t,e){return await h.getBackgroundImages(t,e)}async function A(t,e){return await h.getPrompts(t,e)}async function k(t,e){return await h.getDocuments(t,e)}async function P(t,e){return await h.getTextNormalizations(t,e)}async function O(t,e){return await h.getMcpServers(t,e)}exports.ApiError=r,exports.ChatTool=class{name;description;parameters;call;executeOnly;constructor(t,e,s,a,r=!1){this.name=t,this.description=e,this.parameters=s,this.call=a,this.executeOnly=r}},exports.LLMError=n,exports.LLMStreamingResponseError=o,exports.LlmProcessor=C,exports.STTError=i,exports.Session=I,exports.TTSDecodeError=c,exports.TTSError=l,exports.TTS_TARGET_SAMPLE_RATE=y,exports.WavRecorder=E,exports.createSession=async function(t,e,s,a,r,n){return"boolean"==typeof r?await x(t,e,s,a,r,n??[]):await x(t,e,s,a,r)},exports.createSessionId=async function(e,s,a){let r;if("undefined"!=typeof window&&console.warn("[perso-interactive-sdk-web] WARNING: createSessionId is being called from the browser. This exposes your API key and is not recommended for production. Use server-side session creation with 'perso-interactive-sdk-web/server' instead. See: https://github.com/perso-ai/perso-interactive-sdk-web#server-side"),"string"==typeof a){const n=await h.getSessionTemplate(e,s,a);if("webrtc"!==n.model_style.platform_type)throw new Error(`SessionTemplate "${a}" uses platform_type "${n.model_style.platform_type}", but only "webrtc" is supported`);r=function(e){const s=t=>e.capability.some(e=>e.name===t);return{using_stf_webrtc:s(t.STF_WEBRTC),model_style:e.model_style.name,prompt:e.prompt.prompt_id,document:e.document?.document_id,background_image:e.background_image?.backgroundimage_id,mcp_servers:e.mcp_servers.length?e.mcp_servers.map(t=>t.mcpserver_id):void 0,llm_type:s(t.LLM)?e.llm_type.name:void 0,tts_type:s(t.TTS)?e.tts_type.name:void 0,stt_type:s(t.STT)?e.stt_type.name:void 0,text_normalization_config:e.text_normalization_config?.textnormalizationconfig_id,text_normalization_locale:e.text_normalization_locale,padding_left:e.padding_left??void 0,padding_top:e.padding_top??void 0,padding_height:e.padding_height??void 0}}(n)}else r=a;const n={capability:[],...r};r.using_stf_webrtc&&n.capability.push(t.STF_WEBRTC),r?.llm_type&&(n.capability.push(t.LLM),n.llm_type=r.llm_type),r?.tts_type&&(n.capability.push(t.TTS),n.tts_type=r.tts_type),r?.stt_type&&(n.capability.push(t.STT),n.stt_type=r.stt_type);const o=await fetch(`${e}/api/v1/session/`,{body:JSON.stringify(n),headers:{"PersoLive-APIKey":s,"Content-Type":"application/json"},method:"POST"});return(await h.parseJson(o)).session_id},exports.createWavRecorder=function(t){return new E(t)},exports.getAllSettings=async function(t,e){const[s,a,r,n,o,i,l,c,h]=await Promise.all([_(t,e),L(t,e),M(t,e),b(t,e),N(t,e),A(t,e),k(t,e),O(t,e),P(t,e).catch(()=>[])]);return{llms:s,ttsTypes:a,sttTypes:r,modelStyles:n,backgroundImages:o,prompts:i,documents:l,mcpServers:c,textNormalizations:h}},exports.getBackgroundImages=N,exports.getDocuments=k,exports.getLLMs=_,exports.getMcpServers=O,exports.getModelStyles=b,exports.getPrompts=A,exports.getSTTs=M,exports.getSessionInfo=async function(t,e){return await h.getSessionInfo(t,e)},exports.getSessionTemplates=async function(t,e){return await h.getSessionTemplates(t,e)},exports.getTTSs=L,exports.getTextNormalization=async function(t,e,s){return await h.downloadTextNormalization(t,e,s)},exports.getTextNormalizations=P,exports.getWavSampleRate=function(t){const e=new DataView(t);if(t.byteLength<28)throw new d("File too small to be a valid WAV");if("RIFF"!==p(e,0,4))throw new d("Missing RIFF header");let s=12;for(;s<t.byteLength-8;){const a=p(e,s,4),r=e.getUint32(s+4,!0);if("fmt "===a){if(s+16>t.byteLength)throw new d("fmt chunk extends beyond file");return e.getUint32(s+12,!0)}const n=s+8+r;if(n<=s)break;s=n}throw new d("Missing fmt chunk")},exports.makeTTS=async function(t,e){return await h.makeTTS(t,e)};
@@ -1,3 +1,122 @@
1
+ interface Prompt {
2
+ prompt_id: string;
3
+ name: string;
4
+ description?: string;
5
+ system_prompt: string;
6
+ require_document?: boolean;
7
+ intro_message?: string;
8
+ }
9
+ interface SessionCapability {
10
+ name: SessionCapabilityName;
11
+ description?: string | null;
12
+ }
13
+ interface Document {
14
+ document_id: string;
15
+ title: string;
16
+ file: string;
17
+ description?: string;
18
+ search_count?: number;
19
+ ef_search?: number | null;
20
+ processed: boolean;
21
+ processed_v2: boolean;
22
+ created_at: string;
23
+ updated_at: string;
24
+ }
25
+ interface LLMType {
26
+ name: string;
27
+ service?: string;
28
+ }
29
+ interface TTSType {
30
+ name: string;
31
+ streamable?: boolean;
32
+ service: string;
33
+ model?: string | null;
34
+ voice?: string | null;
35
+ voice_settings?: unknown | null;
36
+ style?: string | null;
37
+ voice_extra_data?: unknown | null;
38
+ }
39
+ interface STTType {
40
+ name: string;
41
+ service: string;
42
+ options?: unknown | null;
43
+ }
44
+ interface TextNormalizationConfig {
45
+ textnormalizationconfig_id: string;
46
+ name: string;
47
+ created_at: string;
48
+ }
49
+ interface ModelStyleConfig {
50
+ modelstyleconfig_id: string;
51
+ key: string;
52
+ value: string;
53
+ }
54
+ interface AIHumanModelFile {
55
+ name: string;
56
+ file?: string | null;
57
+ }
58
+ interface ModelStyle {
59
+ name: string;
60
+ model: string;
61
+ model_file?: string | null;
62
+ model_files: AIHumanModelFile[];
63
+ style: string;
64
+ file?: string | null;
65
+ platform_type?: string;
66
+ configs: ModelStyleConfig[];
67
+ }
68
+ interface BackgroundImage {
69
+ backgroundimage_id: string;
70
+ title: string;
71
+ image: string;
72
+ created_at: string;
73
+ }
74
+ interface MCPServer {
75
+ mcpserver_id: string;
76
+ name: string;
77
+ description?: string;
78
+ url: string;
79
+ transport_protocol?: string;
80
+ server_timeout_sec?: number;
81
+ extra_data?: unknown | null;
82
+ }
83
+ interface SessionTemplate {
84
+ sessiontemplate_id: string;
85
+ name: string;
86
+ description: string | null;
87
+ prompt: Prompt;
88
+ capability: SessionCapability[];
89
+ document: Document | null;
90
+ llm_type: LLMType;
91
+ tts_type: TTSType;
92
+ stt_type: STTType;
93
+ text_normalization_config: TextNormalizationConfig | null;
94
+ text_normalization_locale: string | null;
95
+ model_style: ModelStyle;
96
+ background_image: BackgroundImage | null;
97
+ agent: string | null;
98
+ padding_left: number | null;
99
+ padding_top: number | null;
100
+ padding_height: number | null;
101
+ extra_data: unknown | null;
102
+ mcp_servers: MCPServer[];
103
+ created_at: string;
104
+ last_used_at: string | null;
105
+ }
106
+
107
+ declare enum SessionCapabilityName {
108
+ LLM = "LLM",
109
+ TTS = "TTS",
110
+ STT = "STT",
111
+ STF_ONPREMISE = "STF_ONPREMISE",
112
+ STF_WEBRTC = "STF_WEBRTC"
113
+ }
114
+ interface TextNormalizationDownload {
115
+ config_id: string;
116
+ config_name: string;
117
+ file_url: string;
118
+ }
119
+
1
120
  interface Chat {
2
121
  text: string;
3
122
  isUser: boolean;
@@ -264,6 +383,8 @@ declare class Session {
264
383
  processSTF(file: Blob, format: string, message: string): Promise<string>;
265
384
  processTTS(message: string, options?: {
266
385
  resample?: boolean;
386
+ locale?: string;
387
+ output_format?: string;
267
388
  }): Promise<Blob | undefined>;
268
389
  /**
269
390
  * Triggers the recording state and instructs Perso to buffer microphone
@@ -607,7 +728,27 @@ declare function getDocuments(apiServer: string, apiKey: string): Promise<any>;
607
728
  * @param apiServer Perso API server URL.
608
729
  * @param apiKey API key used for authentication.
609
730
  */
731
+ /**
732
+ * Retrieves available text normalization options.
733
+ * @param apiServer Perso API server URL.
734
+ * @param apiKey API key used for authentication.
735
+ */
736
+ declare function getTextNormalizations(apiServer: string, apiKey: string): Promise<any>;
737
+ /**
738
+ * Downloads the ruleset data file for a Text Normalization Config.
739
+ * Returns a pre-signed Blob Storage URL for the CSV file.
740
+ * @param apiServer Perso API server URL.
741
+ * @param apiKey API key used for authentication.
742
+ * @param configId Text Normalization Config ID.
743
+ */
744
+ declare function getTextNormalization(apiServer: string, apiKey: string, configId: string): Promise<TextNormalizationDownload>;
610
745
  declare function getMcpServers(apiServer: string, apiKey: string): Promise<any>;
746
+ /**
747
+ * Retrieves the list of session templates.
748
+ * @param apiServer Perso API server URL.
749
+ * @param apiKey API key used for authentication.
750
+ */
751
+ declare function getSessionTemplates(apiServer: string, apiKey: string): Promise<SessionTemplate[]>;
611
752
  /**
612
753
  * Convenience helper that fetches every dropdown-friendly resource needed to
613
754
  * build a Perso session configuration screen in one call chain.
@@ -624,6 +765,7 @@ declare function getAllSettings(apiServer: string, apiKey: string): Promise<{
624
765
  prompts: any;
625
766
  documents: any;
626
767
  mcpServers: any;
768
+ textNormalizations: any;
627
769
  }>;
628
770
  /**
629
771
  * Creates a Session with REST-based STT/TTS (current mode).
@@ -640,6 +782,20 @@ declare function createSession(apiServer: string, sessionId: string, width: numb
640
782
  * @param apiServer Perso API server URL.
641
783
  * @param sessionId Session id to inspect.
642
784
  */
785
+ /**
786
+ * Sends text to the TTS API and returns Base64-encoded audio.
787
+ * @param apiServer Perso API server URL.
788
+ * @param params Session ID and text to synthesize.
789
+ * @returns Object with Base64 audio string.
790
+ */
791
+ declare function makeTTS(apiServer: string, params: {
792
+ sessionId: string;
793
+ text: string;
794
+ locale?: string;
795
+ output_format?: string;
796
+ }): Promise<{
797
+ audio: string;
798
+ }>;
643
799
  declare function getSessionInfo(apiServer: string, sessionId: string): Promise<any>;
644
800
 
645
801
  type CreateSessionIdBody = {
@@ -655,6 +811,8 @@ type CreateSessionIdBody = {
655
811
  llm_type?: string;
656
812
  tts_type?: string;
657
813
  stt_type?: string;
814
+ text_normalization_config?: string;
815
+ text_normalization_locale?: string | null;
658
816
  };
659
817
  /**
660
818
  * Requests a new session creation ID by POSTing the desired runtime options to
@@ -664,25 +822,24 @@ type CreateSessionIdBody = {
664
822
  * client-side code, the API key will be exposed to the browser. For production
665
823
  * use, prefer creating session IDs server-side using `perso-interactive-sdk-web/server`.
666
824
  *
825
+ * @overload Creates a session from a SessionTemplate ID. Internally calls
826
+ * `getSessionTemplate` to resolve the template and maps it to the request body.
827
+ * @param apiServer Perso API server URL.
828
+ * @param apiKey API key used for authentication.
829
+ * @param sessionTemplateId SessionTemplate ID to resolve configuration from.
830
+ * @returns Session ID returned by the server.
831
+ * @throws {Error} If the template's `model_style.platform_type` is not `"webrtc"`.
832
+ * @throws {ApiError} If the template ID is invalid or API call fails.
833
+ */
834
+ declare function createSessionId(apiServer: string, apiKey: string, sessionTemplateId: string): Promise<string>;
835
+ /**
836
+ * @overload Creates a session from explicit runtime options.
667
837
  * @param apiServer Perso API server URL.
668
838
  * @param apiKey API key used for authentication.
669
- * @param params {
670
- * using_stf_webrtc: boolean;
671
- * llm_type?: string;
672
- * tts_type?: string;
673
- * stt_type?: string;
674
- * model_style: string;
675
- * prompt: string;
676
- * document?: string;
677
- * background_image?: string;
678
- * mcp_servers?: Array<string>;
679
- * padding_left?: number;
680
- * padding_top?: number;
681
- * padding_height?: number;
682
- * }
839
+ * @param params Runtime options for the session.
683
840
  * @returns Session ID returned by the server.
684
841
  */
685
- declare const createSessionId: (apiServer: string, apiKey: string, params: CreateSessionIdBody) => Promise<string>;
842
+ declare function createSessionId(apiServer: string, apiKey: string, params: CreateSessionIdBody): Promise<string>;
686
843
 
687
844
  declare class ApiError extends Error {
688
845
  errorCode: number;
@@ -716,5 +873,5 @@ declare function getWavSampleRate(arrayBuffer: ArrayBuffer): number;
716
873
 
717
874
  declare const TTS_TARGET_SAMPLE_RATE = 16000;
718
875
 
719
- export { ApiError, ChatState, ChatTool, LLMError, LLMStreamingResponseError, LlmProcessor, STTError, Session, TTSDecodeError, TTSError, TTS_TARGET_SAMPLE_RATE, WavRecorder, createSession, createSessionId, createWavRecorder, getAllSettings, getBackgroundImages, getDocuments, getLLMs, getMcpServers, getModelStyles, getPrompts, getSTTs, getSessionInfo, getTTSs, getWavSampleRate };
720
- export type { Chat, LLMStreamChunk, LlmProcessorCallbacks, LlmProcessorConfig, ProcessLLMOptions, WavRecorderOptions };
876
+ export { ApiError, ChatState, ChatTool, LLMError, LLMStreamingResponseError, LlmProcessor, STTError, Session, TTSDecodeError, TTSError, TTS_TARGET_SAMPLE_RATE, WavRecorder, createSession, createSessionId, createWavRecorder, getAllSettings, getBackgroundImages, getDocuments, getLLMs, getMcpServers, getModelStyles, getPrompts, getSTTs, getSessionInfo, getSessionTemplates, getTTSs, getTextNormalization, getTextNormalizations, getWavSampleRate, makeTTS };
877
+ export type { AIHumanModelFile, BackgroundImage, Chat, Document, LLMStreamChunk, LLMType, LlmProcessorCallbacks, LlmProcessorConfig, MCPServer, ModelStyle, ModelStyleConfig, ProcessLLMOptions, Prompt, STTType, SessionCapability, SessionTemplate, TTSType, TextNormalizationConfig, TextNormalizationDownload, WavRecorderOptions };