@intelliweave/embedded 1.6.47 → 1.6.49

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.
@@ -2,26 +2,26 @@ import{v4 as se}from"uuid";function ne(c){let s=(c||"").split(`
2
2
  `);for(;s.length>0&&s[0].trim()=="";)s.shift();for(;s.length>0&&s[s.length-1].trim()=="";)s.pop();let e=1/0;for(let t of s){let i=t.match(/^\s*/)?.[0].length||0;e=Math.min(e,i)}return e>0&&e<1/0&&(s=s.map(t=>t.substring(e))),s.join(`
3
3
  `)}var be={};function b(){return typeof window<"u"?window:typeof global<"u"?global:typeof globalThis<"u"?globalThis:typeof self<"u"?self:be}function u(){return b().intelliweave=b().intelliweave||b().webWeaver||{},b().intelliweave}async function*oe(c){let s="",e=function*(){for(;;){let n=s.indexOf(`
4
4
 
5
- `);if(n==-1)break;let a=s.slice(0,n);s=s.slice(n+2);let r={},l=a.split(`
6
- `);for(let o of l){n=o.indexOf(": ");let d=o.slice(0,n),p=o.slice(n+2);n==-1&&(d=o,p=""),d&&(r[d]!==void 0?r[d]+=`
7
- `+p:r[d]=p)}yield r}},t=new TextDecoder,i=c.getReader();for(;;){let{done:n,value:a}=await i.read();if(n)break;s+=t.decode(a,{stream:!0}),yield*e()}s+=t.decode()+`
5
+ `);if(n==-1)break;let o=s.slice(0,n);s=s.slice(n+2);let a={},l=o.split(`
6
+ `);for(let r of l){n=r.indexOf(": ");let d=r.slice(0,n),p=r.slice(n+2);n==-1&&(d=r,p=""),d&&(a[d]!==void 0?a[d]+=`
7
+ `+p:a[d]=p)}yield a}},t=new TextDecoder,i=c.getReader();for(;;){let{done:n,value:o}=await i.read();if(n)break;s+=t.decode(o,{stream:!0}),yield*e()}s+=t.decode()+`
8
8
 
9
- `,yield*e()}function ae(){if(u().userID)return u().userID;if(typeof localStorage<"u"){let c=localStorage.getItem("intelliweave.uid")||"";return c||(c=se(),localStorage.setItem("intelliweave.uid",c),c)}else return se()}var V=class V{constructor(s){this.module="IntelliWeave";this.module=s}get debugEnabled(){return V.debug?!0:typeof window<"u"&&u().debug}log(...s){this.debugEnabled&&console.log(`[IntelliWeave > ${this.module}]`,...s)}debug(...s){this.debugEnabled&&console.debug(`[IntelliWeave > ${this.module}]`,...s)}info(...s){this.debugEnabled&&console.info(`[IntelliWeave > ${this.module}]`,...s)}warn(...s){console.warn(`[IntelliWeave > ${this.module}]`,...s)}error(...s){console.error(`[IntelliWeave > ${this.module}]`,...s)}timer(s,...e){let t=Date.now();return this.debug(`[${s} 0ms] Started`,...e),(...i)=>this.debug(`[${s} ${Math.floor(Date.now()-t)}ms]`,...i)}};V.debug=!1;var h=V;var y=new h("ChatGPT"),T=class{constructor(s){this.id="";this.metadata={};this.config={apiKey:"",endpoint:"",model:"gpt-4-turbo",systemMessage:"",userID:"",stream:!0,maxTokens:4096};this.messages=[];this.tools=[];this.maxToolCallsPerMessage=10;this._hasRemovedToolCallHistorySinceLastMessage=0;this.stats={tokensUsed:0};this.config={...this.config,...s}}async sendMessage(s){this.messages.push({role:"user",content:s}),await this.processMessages(),this._hasRemovedToolCallHistorySinceLastMessage=0;let e=this.messages[this.messages.length-1];return e?.role=="assistant"&&e.content||""}async processMessages(){await this.config.onBeforeMessageProcessing?.(),y.debug("Process message state:",{context:this.config.systemMessage,tools:this.tools.slice(),messages:this.messages.slice(),removedToolCallHistory:this._hasRemovedToolCallHistorySinceLastMessage});let s=this.messages[this.messages.length-1];if(s?.role=="user"||s?.role=="tool")await this.trimMessages(),await this.sendToAPI(),await this.processMessages();else if(s?.role=="assistant"&&s?.tool_calls?.length){for(let e of s.tool_calls)await this.processToolCall(e);await this.processMessages()}}async trimMessages(){for(;;){let s=await this.sendToAPI(!0);if(JSON.stringify(s).length/3.8<this.config.maxTokens)break;if(this.messages.length<2){y.warn("[ChatGPT] Unable to trim messages, no messages left! Please check the size of the system message and the LLM's context window size.");break}for(this.messages=this.messages.slice(Math.floor(this.messages.length/2));this.messages.length&&this.messages[0].role=="tool";)this.messages.shift()}}async sendToAPI(s){let e=0;for(let l=this.messages.length-1;l>=0;l--)if(this.messages[l].role=="assistant"&&this.messages[l].tool_calls){if(e+=1,e>=this.maxToolCallsPerMessage)throw new Error(`Exceeded the maximum tool calls per message limit of ${this.maxToolCallsPerMessage}.`)}else if(this.messages[l].role=="user")break;y.debug("Before LLM state:",{context:this.config.systemMessage,tools:this.tools.slice(),messages:this.messages.slice(),removedToolCallHistory:this._hasRemovedToolCallHistorySinceLastMessage});let t={model:this.config.model,temperature:.2,user:this.config.userID,tools:[],stream:!!this.config.stream,max_tokens:1024,messages:[{role:"system",content:this.config.systemMessage},...this.messages]};for(let l of this.tools){l.description&&l.description.length>1024&&y.warn(`Tool description for "${l.name}" is too long, it will be truncated.`);let o={type:"function",function:{name:l.name,description:(l.description||"").substring(0,1024),parameters:{type:"object",properties:{}}}};if(Array.isArray(l.params))for(let d of l.params)o.function.parameters.properties[d.name]={type:d.type,description:d.description};else if(l.params){let d=l.params;for(let p in d)o.function.parameters.properties[p]={type:"string",description:d[p]}}t.tools.push(o)}t.user||delete t.user,t.tools.length||delete t.tools;let i={};if(i["Content-Type"]="application/json",this.config.apiKey&&(i.Authorization=`Bearer ${this.config.apiKey}`),s)return t;let n=await fetch(this.config.endpoint||"https://api.openai.com/v1/chat/completions",{method:"POST",headers:i,body:JSON.stringify(t)});if(!n.ok){let l=`${n.status} ${n.statusText}`;try{let d=await n.json();l=d.errorText||d.error?.message||d.error||l}catch{}let o=new Error(l);throw o.code=n.status,o}let a=null,r=null;if(this.config.stream)for await(let l of oe(n.body)){if(l.data=="[DONE]")break;if(!l.data)continue;let o=JSON.parse(l.data);if(r){for(let d in o.choices[0].delta)if(typeof o.choices[0].delta[d]=="string"){if(d=="role"&&r[d]==o.choices[0].delta[d])continue;r[d]=(r[d]||"")+o.choices[0].delta[d]}for(let d of o.choices[0].delta.tool_calls||[]){if(r.tool_calls||(r.tool_calls=[]),!r.tool_calls[d.index]){r.tool_calls[d.index]=d;continue}let p=r.tool_calls[d.index];p.function=p.function||{},p.function.name=(p.function.name||"")+(d.function?.name||""),p.function.arguments=(p.function.arguments||"")+(d.function?.arguments||"")}}else r={chunkID:o.id,...o.choices[0].delta};r?.content&&!r.content.startsWith("{")&&this.config.onAIMessage?.(r.content,!0)}else a=await n.json(),this.stats.tokensUsed+=a.usage?.total_tokens||0,y.debug(`Used ${a.usage?.total_tokens||0} tokens, total used: ${this.stats.tokensUsed}`),r=a.choices?.[0]?.message;if(r||(y.warn("No response block in API response."),r={role:"assistant",content:""}),r.role=="user")throw new Error("API returned a user message, which is not allowed.");this.processIncomingMessage(r),this.messages.push(r),r.content&&this.config.onAIMessage?.(r.content,!1)}processIncomingMessage(s){}registerTool(s){this.tools.push(s)}async processToolCall(s){try{let e=this.tools.find(n=>n.name==s.function.name);if(!e)throw new Error(`Tool "${s.function.name}" not found.`);let t=JSON.parse(s.function.arguments);this.config.onAIToolStart?.(s.function.name,t);let i=await e.callback(t);if(typeof i!="string"&&(i=JSON.stringify(i)||""),(i===""||i==="undefined")&&(i="success"),this._hasRemovedToolCallHistorySinceLastMessage<3&&e.removeFromMessageHistory){y.debug(`Removing tool call history for "${e.name}"`),this._hasRemovedToolCallHistorySinceLastMessage++,this.messages=this.messages.filter(n=>n.role!="assistant"||!n.tool_calls?.find(a=>a.id==s.id));return}else e.removeFromMessageHistory&&y.info(`The AI attempted to reuse a tool call that we removed from history. Skipping... "${e.name}"`);this.messages.push({role:"tool",content:i,tool_call_id:s.id})}catch(e){y.warn(`Unable to process tool call for "${s?.function?.name}"`,e),this.messages.push({role:"tool",content:`Error: ${e.message}`,tool_call_id:s.id})}}resetConversation(){this.messages=[]}};import{v4 as de}from"uuid";import ve from"minisearch";var re=[{id:"search",type:"action",name:"Search the knowledge base.",content:"Search the knowledge base for information, actions, tools, tours, UI elements, etc. Use this on EVERY request if you don't have information. If the user asks for personal information, use this. If the user asks you to do something, use this to find the tool you need.",isContext:!0,removeFromMessageHistory:!0,parameters:[{name:"query",type:"string",description:"The search query"}],action:async(c,s)=>{let e=await s.knowledgeBase.search(c.query);s.updateKnowledgeBase(e),s.submitAnalyticsEvent({type:"kb-search",query:c.query,results:e});let t=e.filter(n=>n.type!="action").map(n=>"id="+n.id).join(", ")||"(none)",i=e.filter(n=>n.type=="action").map(n=>"name="+n.id.replaceAll(/[^a-zA-Z0-9_]/g,"_")).join(", ")||"(none)";return`Search complete, context has been updated. New info entries found: ${t}. New tools available: ${i}.`}},{id:"ui.openURL",type:"action",name:"Open a URL in a new tab.",isContext:!0,get disabled(){return typeof window>"u"},content:"Opens the specified URL in a new tab.",parameters:[{name:"url",type:"string",description:"The URL to open"}],action:(c,s)=>{if(!window.open(c.url,"_blank"))throw new Error("Window blocked by popup blocker.");return s.submitAnalyticsEvent({type:"open-url",url:c.url}),"URL opened"}}];var x=new h("KnowledgeBase"),le=1,k=class c{constructor(s){this._sources=[{id:"core.internal",query:async()=>re}];this._windowSources=[];this.lastResults=[];this.manualEntries=[];this.ai=s}registerSource(s,e){let t=s;return typeof s=="function"&&(e=s,t=`source.${le++}`),this._sources.push({id:t,query:e}),t}removeSource(s){this._sources=this.sources.filter(e=>e.id!==s&&e.query!==s)}addEntry(s){this.manualEntries.push(s)}removeEntry(s){this.manualEntries=this.manualEntries.filter(e=>e.id!==s)}get sources(){let s=this._sources;return typeof window<"u"&&u().knowledgeBaseSources&&(s=s.concat(u().knowledgeBaseSources.map(e=>({id:e.name,query:e})))),s=s.concat(this._windowSources),s=s.filter(e=>!e.disabled),s}async search(s){x.debug(`Searching knowledge base for: ${s}`);let e=new Event("webweaver_kb_search",{bubbles:!0,cancelable:!0});e.query=s,e.entries=[],e.sources=[],typeof document<"u"&&document.dispatchEvent(e),this._windowSources=e.sources;let i=(await Promise.all(this.sources.map(async l=>{try{return await l.query(s)}catch(o){return x.warn(`Knowledge source '${l.id}' failed:`,o),[]}}))).flat();i=i.concat(e.entries),i=i.concat(this.manualEntries),u().knowledgeBase&&(i=i.concat(u().knowledgeBase)),i=i.filter(l=>l&&!l.disabled);for(let l=0;l<i.length;l++){let o=i[l];if(o.id=o.id||`temp.${l}`,!o.action&&o.type=="tour"&&(o.action=d=>{throw new Error("This tour does not have an action. You must perform the tour content manually.")}),!o.action&&o.type=="info"&&(o.action=d=>{throw new Error("This item does not have an action. Use the content to provide information to the user instead.")}),o.parameters&&!Array.isArray(o.parameters)){let d=o.parameters,p=[];for(let ie in d)p.push({name:ie,type:"string",description:o.parameters[ie]});o.parameters=p}o.content=ne(typeof o.content=="function"?o.content():o.content)}let n=new ve({fields:["id","type","name","content","tags"],storeFields:[],searchOptions:{boost:{name:3,tags:2},fuzzy:.2}});n.addAll(i);let r=n.search(s).map(l=>i.find(o=>o.id==l.id));for(let l of i)l.isContext&&(r.find(o=>o.id===l.id)||r.push(l));return this.lastResults=r,x.debug("Found results:",r),r}getCachedEntry(s){return this.lastResults.find(e=>e.id==s)}registerSourceFromURL(s,e){e||(e=`external.${le++}`),x.debug(`Registering remote knowledge base source: ${s}`);let t=[],i=async(a,r)=>{x.debug(`Calling remote knowledge base action: ${a.id}`);let l={type:"action",userID:this.ai.userID,extra:this.ai.extra,actionID:a.id,parameters:r},o=await fetch(s,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(l)});if(!o.ok)throw new Error(`HTTP Error ${o.status} ${o.statusText}`);let d=await o.json();return n(d.updateItems||[]),d.response},n=a=>{for(let r of a){if(!r.id){x.warn("KB item skipped since it has no ID.",r);continue}let l=t.find(o=>o.id==r.id);if(l){l.name=r.name||l.name||"",l.content=r.content||l.content||"",l.disabled=r.disabled??l.disabled,l.isContext=r.isContext??l.isContext,l.parameters=r.parameters||l.parameters||[],l.tags=r.tags||l.tags,l.type=r.type||l.type;continue}t.push({...r,action:o=>i(r,o)})}};this.registerSource(e,async a=>{let r={type:"search",userID:this.ai?.userID||"",extra:this.ai?.extra||{},query:a},l=await fetch(s,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(r)});if(!l.ok)throw new Error(`HTTP Error ${l.status} ${l.statusText}`);let o=await l.json();return n(o.items),t})}clone(){let s=new c(this.ai);return s._sources=this._sources,s._windowSources=this._windowSources,s.manualEntries=this.manualEntries,s}};var ce={name:"@intelliweave/embedded",version:"1.6.47",description:"Integrate IntelliWeave into your app or website.",main:"./dist/webpack/index.js",types:"./dist/webpack/index.d.ts",type:"module",exports:{".":"./dist/webpack/index.js","./component":"./dist/component/component.js","./node":"./dist/node/node.js","./react":"./dist/react/react.js","./webpack":"./dist/webpack/index.js"},scripts:{build:"npm run build:lib && npm run build:docs","build:lib":"tsx build.ts","build:dev":"cross-env DEVELOPMENT=1 npm run build","build:docs":"typedoc src/index.mts --out dist/docs/",deploy:'npm run build && gsutil cp ./dist/web-weaver.min.js gs://metapress-cdn/web-weaver.min.js && gcloud compute url-maps invalidate-cdn-cache mp-cdn-loadbalancer --project="mp-backend-api" --path "/web-weaver.min.js" --async',"start:server":"cd server && npm run start","deploy:server":"cd server && npm run deploy","llm:build":"cd llm-server && docker build -t web-weaver-llm .","llm:start":"npm run llm:build && docker run -it --rm -p 8000:80 --gpus=all web-weaver-llm","llm:deploy.docker":"npm run llm:build && gcloud auth configure-docker us-central1-docker.pkg.dev && docker tag web-weaver-llm us-central1-docker.pkg.dev/ydangle-web-companion/docker-artifacts/web-weaver-llm && docker push us-central1-docker.pkg.dev/ydangle-web-companion/docker-artifacts/web-weaver-llm","llm:deploy":'npm run llm:deploy.docker && gcloud run deploy web-weaver-llm --project=ydangle-web-companion --image=us-central1-docker.pkg.dev/ydangle-web-companion/docker-artifacts/web-weaver-llm --allow-unauthenticated --region=us-central1 --description="Web Weaver LLM" --concurrency=2 --min-instances=0 --timeout=5m --memory=16Gi --cpu=8',prepack:"npm run build",test:"npm run build && tsx --test"},keywords:["web","weaver","ai","assistant","chat"],author:"jjv360",license:"UNLICENSED",devDependencies:{"@types/audioworklet":"^0.0.64","@types/lodash":"^4.17.13","@types/react":"^18.3.12","@types/uuid":"^10.0.0",bestzip:"^2.2.1","cross-env":"^7.0.3","find-cache-dir":"^5.0.0",lodash:"^4.17.21","onnxruntime-web":"^1.20.0",react:"^18.3.1","replace-in-file":"^8.2.0",tsup:"^8.3.5",tsx:"^4.19.2",typedoc:"^0.27.6"},peerDependencies:{"onnxruntime-web":"^1.20.0",react:"^18 || ^19"},dependencies:{minisearch:"^6.3.0","rehype-document":"^7.0.3","rehype-external-links":"^3.0.0","rehype-format":"^5.0.0","rehype-stringify":"^10.0.0","remark-parse":"^11.0.0","remark-rehype":"^11.1.0",unified:"^11.0.4",uuid:"^10.0.0"}};var L=class{constructor(s){this.ai=s}async boolean(s,e){let t=this.ai.clone();t.resetConversation(),t.getContextPrefix=async()=>'You will receive a question and some data. Answer the question by replying only with the word "true" or "false".';let i=await t.sendMessage(s+`
9
+ `,yield*e()}function ae(){if(u().userID)return u().userID;if(typeof localStorage<"u"){let c=localStorage.getItem("intelliweave.uid")||"";return c||(c=se(),localStorage.setItem("intelliweave.uid",c),c)}else return se()}var V=class V{constructor(s){this.module="IntelliWeave";this.module=s}get debugEnabled(){return V.debug?!0:typeof window<"u"&&u().debug}log(...s){this.debugEnabled&&console.log(`[IntelliWeave > ${this.module}]`,...s)}debug(...s){this.debugEnabled&&console.debug(`[IntelliWeave > ${this.module}]`,...s)}info(...s){this.debugEnabled&&console.info(`[IntelliWeave > ${this.module}]`,...s)}warn(...s){console.warn(`[IntelliWeave > ${this.module}]`,...s)}error(...s){console.error(`[IntelliWeave > ${this.module}]`,...s)}timer(s,...e){let t=Date.now();return this.debug(`[${s} 0ms] Started`,...e),(...i)=>this.debug(`[${s} ${Math.floor(Date.now()-t)}ms]`,...i)}};V.debug=!1;var h=V;var y=new h("ChatGPT"),T=class{constructor(s){this.id="";this.metadata={};this.config={apiKey:"",endpoint:"",model:"gpt-4-turbo",systemMessage:"",userID:"",stream:!0,maxTokens:4096};this.messages=[];this.tools=[];this.maxToolCallsPerMessage=10;this._hasRemovedToolCallHistorySinceLastMessage=0;this.stats={tokensUsed:0};this.config={...this.config,...s}}async sendMessage(s){this.messages.push({role:"user",content:s}),await this.processMessages(),this._hasRemovedToolCallHistorySinceLastMessage=0;let e=this.messages[this.messages.length-1];return e?.role=="assistant"&&e.content||""}async processMessages(){await this.config.onBeforeMessageProcessing?.(),y.debug("Process message state:",{context:this.config.systemMessage,tools:this.tools.slice(),messages:this.messages.slice(),removedToolCallHistory:this._hasRemovedToolCallHistorySinceLastMessage});let s=this.messages[this.messages.length-1];if(s?.role=="user"||s?.role=="tool")await this.trimMessages(),await this.sendToAPI(),await this.processMessages();else if(s?.role=="assistant"&&s?.tool_calls?.length){for(let e of s.tool_calls)await this.processToolCall(e);await this.processMessages()}}async trimMessages(){for(;;){let s=await this.sendToAPI(!0);if(JSON.stringify(s).length/3.8<this.config.maxTokens)break;if(this.messages.length<2){y.warn("[ChatGPT] Unable to trim messages, no messages left! Please check the size of the system message and the LLM's context window size.");break}for(this.messages=this.messages.slice(Math.floor(this.messages.length/2));this.messages.length&&this.messages[0].role=="tool";)this.messages.shift()}}async sendToAPI(s){let e=0;for(let l=this.messages.length-1;l>=0;l--)if(this.messages[l].role=="assistant"&&this.messages[l].tool_calls){if(e+=1,e>=this.maxToolCallsPerMessage)throw new Error(`Exceeded the maximum tool calls per message limit of ${this.maxToolCallsPerMessage}.`)}else if(this.messages[l].role=="user")break;y.debug("Before LLM state:",{context:this.config.systemMessage,tools:this.tools.slice(),messages:this.messages.slice(),removedToolCallHistory:this._hasRemovedToolCallHistorySinceLastMessage});let t={model:this.config.model,temperature:.2,user:this.config.userID,tools:[],stream:!!this.config.stream,max_tokens:1024,messages:[{role:"system",content:this.config.systemMessage},...this.messages]};for(let l of this.tools){l.description&&l.description.length>1024&&y.warn(`Tool description for "${l.name}" is too long, it will be truncated.`);let r={type:"function",function:{name:l.name,description:(l.description||"").substring(0,1024),parameters:{type:"object",properties:{}}}};if(Array.isArray(l.params))for(let d of l.params)r.function.parameters.properties[d.name]={type:d.type,description:d.description};else if(l.params){let d=l.params;for(let p in d)r.function.parameters.properties[p]={type:"string",description:d[p]}}t.tools.push(r)}t.user||delete t.user,t.tools.length||delete t.tools;let i={};if(i["Content-Type"]="application/json",this.config.apiKey&&(i.Authorization=`Bearer ${this.config.apiKey}`),s)return t;let n=await fetch(this.config.endpoint||"https://api.openai.com/v1/chat/completions",{method:"POST",headers:i,body:JSON.stringify(t)});if(!n.ok){let l=`${n.status} ${n.statusText}`;try{let d=await n.json();l=d.errorText||d.error?.message||d.error||l}catch{}let r=new Error(l);throw r.code=n.status,r}let o=null,a=null;if(this.config.stream)for await(let l of oe(n.body)){if(l.data=="[DONE]")break;if(!l.data)continue;let r=JSON.parse(l.data);if(a){for(let d in r.choices[0].delta)if(typeof r.choices[0].delta[d]=="string"){if(d=="role"&&a[d]==r.choices[0].delta[d])continue;a[d]=(a[d]||"")+r.choices[0].delta[d]}for(let d of r.choices[0].delta.tool_calls||[]){if(a.tool_calls||(a.tool_calls=[]),!a.tool_calls[d.index]){a.tool_calls[d.index]=d;continue}let p=a.tool_calls[d.index];p.function=p.function||{},p.function.name=(p.function.name||"")+(d.function?.name||""),p.function.arguments=(p.function.arguments||"")+(d.function?.arguments||"")}}else a={chunkID:r.id,...r.choices[0].delta};a?.content&&!a.content.startsWith("{")&&this.config.onAIMessage?.(a.content,!0)}else o=await n.json(),this.stats.tokensUsed+=o.usage?.total_tokens||0,y.debug(`Used ${o.usage?.total_tokens||0} tokens, total used: ${this.stats.tokensUsed}`),a=o.choices?.[0]?.message;if(a||(y.warn("No response block in API response."),a={role:"assistant",content:""}),a.role=="user")throw new Error("API returned a user message, which is not allowed.");this.processIncomingMessage(a),this.messages.push(a),a.content&&this.config.onAIMessage?.(a.content,!1)}processIncomingMessage(s){}registerTool(s){this.tools.push(s)}async processToolCall(s){try{let e=this.tools.find(n=>n.name==s.function.name);if(!e)throw new Error(`Tool "${s.function.name}" not found.`);let t=JSON.parse(s.function.arguments);this.config.onAIToolStart?.(s.function.name,t);let i=await e.callback(t);if(typeof i!="string"&&(i=JSON.stringify(i)||""),(i===""||i==="undefined")&&(i="success"),this._hasRemovedToolCallHistorySinceLastMessage<3&&e.removeFromMessageHistory){y.debug(`Removing tool call history for "${e.name}"`),this._hasRemovedToolCallHistorySinceLastMessage++,this.messages=this.messages.filter(n=>n.role!="assistant"||!n.tool_calls?.find(o=>o.id==s.id));return}else e.removeFromMessageHistory&&y.info(`The AI attempted to reuse a tool call that we removed from history. Skipping... "${e.name}"`);this.messages.push({role:"tool",content:i,tool_call_id:s.id})}catch(e){y.warn(`Unable to process tool call for "${s?.function?.name}"`,e),this.messages.push({role:"tool",content:`Error: ${e.message}`,tool_call_id:s.id})}}resetConversation(){this.messages=[]}};import{v4 as de}from"uuid";import we from"minisearch";var re=[{id:"search",type:"action",name:"Search the knowledge base.",content:"Search the knowledge base for information, actions, tools, tours, UI elements, etc. Use this on EVERY request if you don't have information. If the user asks for personal information, use this. If the user asks you to do something, use this to find the tool you need.",isContext:!0,removeFromMessageHistory:!0,parameters:[{name:"query",type:"string",description:"The search query"}],action:async(c,s)=>{let e=await s.knowledgeBase.search(c.query);s.updateKnowledgeBase(e),s.submitAnalyticsEvent({type:"kb-search",query:c.query,results:e});let t=e.filter(n=>n.type!="action").map(n=>"id="+n.id).join(", ")||"(none)",i=e.filter(n=>n.type=="action").map(n=>"name="+n.id.replaceAll(/[^a-zA-Z0-9_]/g,"_")).join(", ")||"(none)";return`Search complete, context has been updated. New info entries found: ${t}. New tools available: ${i}.`}},{id:"ui.openURL",type:"action",name:"Open a URL in a new tab.",isContext:!0,get disabled(){return typeof window>"u"},content:"Opens the specified URL in a new tab.",parameters:[{name:"url",type:"string",description:"The URL to open"}],action:(c,s)=>{if(!window.open(c.url,"_blank"))throw new Error("Window blocked by popup blocker.");return s.submitAnalyticsEvent({type:"open-url",url:c.url}),"URL opened"}}];var x=new h("KnowledgeBase"),le=1,k=class c{constructor(s){this._sources=[{id:"core.internal",query:async()=>re}];this._windowSources=[];this.lastResults=[];this.manualEntries=[];this.ai=s}registerSource(s,e){let t=s;return typeof s=="function"&&(e=s,t=`source.${le++}`),this._sources.push({id:t,query:e}),t}removeSource(s){this._sources=this.sources.filter(e=>e.id!==s&&e.query!==s)}addEntry(s){this.manualEntries.push(s)}removeEntry(s){this.manualEntries=this.manualEntries.filter(e=>e.id!==s)}get sources(){let s=this._sources;return typeof window<"u"&&u().knowledgeBaseSources&&(s=s.concat(u().knowledgeBaseSources.map(e=>({id:e.name,query:e})))),s=s.concat(this._windowSources),s=s.filter(e=>!e.disabled),s}async search(s){x.debug(`Searching knowledge base for: ${s}`);let e=new Event("webweaver_kb_search",{bubbles:!0,cancelable:!0});e.query=s,e.entries=[],e.sources=[],typeof document<"u"&&document.dispatchEvent(e),this._windowSources=e.sources;let i=(await Promise.all(this.sources.map(async l=>{try{return await l.query(s)}catch(r){return x.warn(`Knowledge source '${l.id}' failed:`,r),[]}}))).flat();i=i.concat(e.entries),i=i.concat(this.manualEntries),u().knowledgeBase&&(i=i.concat(u().knowledgeBase)),i=i.filter(l=>l&&!l.disabled);for(let l=0;l<i.length;l++){let r=i[l];if(r.id=r.id||`temp.${l}`,!r.action&&r.type=="tour"&&(r.action=d=>{throw new Error("This tour does not have an action. You must perform the tour content manually.")}),!r.action&&r.type=="info"&&(r.action=d=>{throw new Error("This item does not have an action. Use the content to provide information to the user instead.")}),r.parameters&&!Array.isArray(r.parameters)){let d=r.parameters,p=[];for(let ie in d)p.push({name:ie,type:"string",description:r.parameters[ie]});r.parameters=p}r.content=ne(typeof r.content=="function"?r.content():r.content)}let n=new we({fields:["id","type","name","content","tags"],storeFields:[],searchOptions:{boost:{name:3,tags:2},fuzzy:.2}});n.addAll(i);let a=n.search(s).map(l=>i.find(r=>r.id==l.id));for(let l of i)l.isContext&&(a.find(r=>r.id===l.id)||a.push(l));return this.lastResults=a,x.debug("Found results:",a),a}getCachedEntry(s){return this.lastResults.find(e=>e.id==s)}registerSourceFromURL(s,e){e||(e=`external.${le++}`),x.debug(`Registering remote knowledge base source: ${s}`);let t=[],i=async(o,a)=>{x.debug(`Calling remote knowledge base action: ${o.id}`);let l={type:"action",userID:this.ai.userID,extra:this.ai.extra,actionID:o.id,parameters:a},r=await fetch(s,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(l)});if(!r.ok)throw new Error(`HTTP Error ${r.status} ${r.statusText}`);let d=await r.json();return n(d.updateItems||[]),d.response},n=o=>{for(let a of o){if(!a.id){x.warn("KB item skipped since it has no ID.",a);continue}let l=t.find(r=>r.id==a.id);if(l){l.name=a.name||l.name||"",l.content=a.content||l.content||"",l.disabled=a.disabled??l.disabled,l.isContext=a.isContext??l.isContext,l.parameters=a.parameters||l.parameters||[],l.tags=a.tags||l.tags,l.type=a.type||l.type;continue}t.push({...a,action:r=>i(a,r)})}};this.registerSource(e,async o=>{let a={type:"search",userID:this.ai?.userID||"",extra:this.ai?.extra||{},query:o},l=await fetch(s,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(a)});if(!l.ok)throw new Error(`HTTP Error ${l.status} ${l.statusText}`);let r=await l.json();return n(r.items),t})}clone(){let s=new c(this.ai);return s._sources=this._sources,s._windowSources=this._windowSources,s.manualEntries=this.manualEntries,s}};var ce={name:"@intelliweave/embedded",version:"1.6.49",description:"Integrate IntelliWeave into your app or website.",main:"./dist/webpack/index.js",types:"./dist/webpack/index.d.ts",type:"module",exports:{".":"./dist/webpack/index.js","./component":"./dist/component/component.js","./node":"./dist/node/node.js","./react":"./dist/react/react.js","./webpack":"./dist/webpack/index.js"},scripts:{build:"npm run build:lib && npm run build:docs","build:lib":"tsx build.ts","build:dev":"cross-env DEVELOPMENT=1 npm run build","build:docs":"typedoc src/index.mts --out dist/docs/",deploy:'npm run build && gsutil cp ./dist/web-weaver.min.js gs://metapress-cdn/web-weaver.min.js && gcloud compute url-maps invalidate-cdn-cache mp-cdn-loadbalancer --project="mp-backend-api" --path "/web-weaver.min.js" --async',"start:server":"cd server && npm run start","deploy:server":"cd server && npm run deploy","llm:build":"cd llm-server && docker build -t web-weaver-llm .","llm:start":"npm run llm:build && docker run -it --rm -p 8000:80 --gpus=all web-weaver-llm","llm:deploy.docker":"npm run llm:build && gcloud auth configure-docker us-central1-docker.pkg.dev && docker tag web-weaver-llm us-central1-docker.pkg.dev/ydangle-web-companion/docker-artifacts/web-weaver-llm && docker push us-central1-docker.pkg.dev/ydangle-web-companion/docker-artifacts/web-weaver-llm","llm:deploy":'npm run llm:deploy.docker && gcloud run deploy web-weaver-llm --project=ydangle-web-companion --image=us-central1-docker.pkg.dev/ydangle-web-companion/docker-artifacts/web-weaver-llm --allow-unauthenticated --region=us-central1 --description="Web Weaver LLM" --concurrency=2 --min-instances=0 --timeout=5m --memory=16Gi --cpu=8',prepack:"npm run build",test:"npm run build && tsx --test"},keywords:["web","weaver","ai","assistant","chat"],author:"jjv360",license:"UNLICENSED",devDependencies:{"@types/audioworklet":"^0.0.64","@types/lodash":"^4.17.13","@types/react":"^18.3.12","@types/uuid":"^10.0.0",bestzip:"^2.2.1","cross-env":"^7.0.3","find-cache-dir":"^5.0.0",lodash:"^4.17.21","onnxruntime-web":"^1.20.0",react:"^18.3.1","replace-in-file":"^8.2.0",tsup:"^8.3.5",tsx:"^4.19.2",typedoc:"^0.27.6"},peerDependencies:{"onnxruntime-web":"^1.20.0",react:"^18 || ^19"},dependencies:{minisearch:"^6.3.0","rehype-document":"^7.0.3","rehype-external-links":"^3.0.0","rehype-format":"^5.0.0","rehype-stringify":"^10.0.0","remark-parse":"^11.0.0","remark-rehype":"^11.1.0",unified:"^11.0.4",uuid:"^10.0.0"}};var L=class{constructor(s){this.ai=s}async boolean(s,e){let t=this.ai.clone();t.resetConversation(),t.getContextPrefix=async()=>'You will receive a question and some data. Answer the question by replying only with the word "true" or "false".';let i=await t.sendMessage(s+`
10
10
 
11
- `+JSON.stringify(e))||"";if(i.toLowerCase().includes("true"))return!0;if(i.toLowerCase().includes("false"))return!1;throw new Error("The AI did not give a boolean answer: "+i)}async choose(s,e,t){t=t.map(a=>a.trim());let i=this.ai.clone();i.resetConversation(),i.getContextPrefix=async()=>"You will receive a question and some data and options. Answer the question by replying with the option you want to choose. Only say the option you want, no additional text.";let n=await i.sendMessage("question:"+s+`
11
+ `+JSON.stringify(e))||"";if(i.toLowerCase().includes("true"))return!0;if(i.toLowerCase().includes("false"))return!1;throw new Error("The AI did not give a boolean answer: "+i)}async choose(s,e,t){t=t.map(o=>o.trim());let i=this.ai.clone();i.resetConversation(),i.getContextPrefix=async()=>"You will receive a question and some data and options. Answer the question by replying with the option you want to choose. Only say the option you want, no additional text.";let n=await i.sendMessage("question:"+s+`
12
12
 
13
13
  data:`+JSON.stringify(e)+`
14
14
 
15
15
  options:`+t.join(`
16
- `))||"";return t.find(a=>n.toLowerCase().includes(a.toLowerCase()))}async extract(s,e,t,i){let n=this.ai.clone();n.resetConversation(),n.getContextPrefix=async()=>"You will receive a question and some data. Answer the question by replying with the extracted data as valid json. If there are multiple extractions, separate them with commas.";let a=await n.sendMessage("question:"+s+`
16
+ `))||"";return t.find(o=>n.toLowerCase().includes(o.toLowerCase()))}async extract(s,e,t,i){let n=this.ai.clone();n.resetConversation(),n.getContextPrefix=async()=>"You will receive a question and some data. Answer the question by replying with the extracted data as valid json. If there are multiple extractions, separate them with commas.";let o=await n.sendMessage("question:"+s+`
17
17
 
18
18
  data:`+JSON.stringify(e)+`
19
19
 
20
- extractions:`+JSON.stringify(i))||"";a=a.replace(/```json/g,"").replace(/```/g,"").replace(/\n/g,"");let r=a.split(",").map(l=>l.trim()).join(",");return JSON.parse(r)}async generateMarkdown(s,e){return this.instruct("Generate a Markdown document based on the input text. Always include a header on every response. Give long detailed answers with many paragraphs, and explain concepts step by step.",s,e)}async instruct(s,e,t){let i=this.ai.clone();return i.resetConversation(),i.getContextPrefix=async()=>s,t&&i.addEventListener("output",a=>{t(a.detail.message)}),await i.sendMessage(e)}};var w=new h("Main"),B=class B extends EventTarget{constructor(){super(...arguments);this.conversationID=de();this.knowledgeBase=new k(this);this._lastKBentries=[];this.models=[];this.audio=null;this.apiKey="";this.logic=new L(this);this.userID=ae();this.extra=u().extra||{};this._lastSystemMsg="";this.isProcessing=!1}get loaded(){return!!(this.config&&this.currentModel)}async load(e){if(this.apiKey=e,!e)throw new Error("API key is required to load the AI.");try{await Promise.all([(async()=>{w.debug("Loading configuration...");let t=await fetch("https://intelliweave.ai/api/config/get",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({apiKey:e})});if(!t.ok)throw new Error(`Failed to load configuration: ${t.status} ${t.statusText}`);this.config=await t.json(),w.debug("Configuration loaded")})(),(async()=>{try{let t=await fetch("https://cdn.intelliweave.ai/models/silero_vad_3.onnx");if(!t.ok)throw new Error(`Failed to load VAD model: ${t.status} ${t.statusText}`);this.vadModel=await t.blob()}catch(t){w.warn(`Failed to load VAD model, some features will be unavailable. ${t.message}`)}})()]),this.models=[{id:this.config.id,config:this.config.model}],this.setModel(this.config.id);for(let t of this.config.knowledge||[])t.url&&this.knowledgeBase.registerSourceFromURL(t.url);return this.resetConversation(),this.dispatchEvent(new CustomEvent("load",{detail:{ai:this}})),typeof window<"u"&&window.dispatchEvent(new CustomEvent("webweaver_loaded",{detail:{ai:this}})),this.config}catch(t){throw w.warn("Failed to load:",t),this.error=t,this.dispatchEvent(new CustomEvent("error",{detail:{ai:this,error:t}})),typeof window<"u"&&window.dispatchEvent(new CustomEvent("webweaver_error",{detail:{ai:this,error:t}})),t}}setModel(e){let t=this.models.find(i=>i.id==e);if(!t)throw new Error(`Model with ID "${e}" not found.`);this.currentModel=new T({...t.config,onBeforeMessageProcessing:this.onBeforeMessageProcessing.bind(this),onAIMessage:this.processIncomingMessage.bind(this),onAIToolStart:(i,n)=>{let a=this._lastKBentries.find(r=>r.id?.replaceAll(/[^a-zA-Z0-9_]/g,"_")==i);this.onAIToolStart?.(a?.id||i,n)}}),this.currentModel.id=t.id,this.currentModel.metadata=t}async getContextPrefix(){let e=u().pageSummary||`You are ${this.config?.name||"IntelliWeave"}. ${this.config?.instructions||"Speak in short sentences."}`;return typeof e=="function"&&(e=await e()),e}async onBeforeMessageProcessing(){this._lastKBentries.length==0&&this.updateKnowledgeBase(await this.knowledgeBase.search("__intelliweaveblanksearchforcontextitems__"));let e=await this.getContextPrefix(),t=(this.currentModel.config.maxTokens||4e3)*.5*4,i=0;for(;;){let n=this._lastKBentries.slice(),a=0;for(;a<i;){let o=n.findLastIndex(d=>!d.isContext);if(o==-1)throw new Error('Too much context, and no more items to remove. Check the size and number of "isContext: true" knowledge base entries, or use an LLM with a larger context window.');n.splice(o,1)}let r=`${e}
20
+ extractions:`+JSON.stringify(i))||"";o=o.replace(/```json/g,"").replace(/```/g,"").replace(/\n/g,"");let a=o.split(",").map(l=>l.trim()).join(",");return JSON.parse(a)}async generateMarkdown(s,e){return this.instruct("Generate a Markdown document based on the input text. Always include a header on every response. Give long detailed answers with many paragraphs, and explain concepts step by step.",s,e)}async instruct(s,e,t){let i=this.ai.clone();return i.resetConversation(),i.getContextPrefix=async()=>s,t&&i.addEventListener("output",o=>{t(o.detail.message)}),await i.sendMessage(e)}};var v=new h("Main"),B=class B extends EventTarget{constructor(){super(...arguments);this.conversationID=de();this.knowledgeBase=new k(this);this._lastKBentries=[];this.models=[];this.audio=null;this.apiKey="";this.logic=new L(this);this.userID=ae();this.extra=u().extra||{};this._lastSystemMsg="";this.isProcessing=!1}get loaded(){return!!(this.config&&this.currentModel)}async load(e){if(this.apiKey=e,!e)throw new Error("API key is required to load the AI.");try{await Promise.all([(async()=>{v.debug("Loading configuration...");let t=await fetch("https://intelliweave.ai/api/config/get",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({apiKey:e})});if(!t.ok)throw new Error(`Failed to load configuration: ${t.status} ${t.statusText}`);this.config=await t.json(),v.debug("Configuration loaded")})(),(async()=>{try{let t=await fetch("https://cdn.intelliweave.ai/models/silero_vad_3.onnx");if(!t.ok)throw new Error(`Failed to load VAD model: ${t.status} ${t.statusText}`);this.vadModel=await t.blob()}catch(t){v.warn(`Failed to load VAD model, some features will be unavailable. ${t.message}`)}})()]),this.models=[{id:this.config.id,config:this.config.model}],this.setModel(this.config.id);for(let t of this.config.knowledge||[])t.url&&this.knowledgeBase.registerSourceFromURL(t.url);return this.resetConversation(),this.dispatchEvent(new CustomEvent("load",{detail:{ai:this}})),typeof window<"u"&&window.dispatchEvent(new CustomEvent("webweaver_loaded",{detail:{ai:this}})),this.config}catch(t){throw v.warn("Failed to load:",t),this.error=t,this.dispatchEvent(new CustomEvent("error",{detail:{ai:this,error:t}})),typeof window<"u"&&window.dispatchEvent(new CustomEvent("webweaver_error",{detail:{ai:this,error:t}})),t}}setModel(e){let t=this.models.find(i=>i.id==e);if(!t)throw new Error(`Model with ID "${e}" not found.`);this.currentModel=new T({...t.config,onBeforeMessageProcessing:this.onBeforeMessageProcessing.bind(this),onAIMessage:this.processIncomingMessage.bind(this),onAIToolStart:(i,n)=>{let o=this._lastKBentries.find(a=>a.id?.replaceAll(/[^a-zA-Z0-9_]/g,"_")==i);this.onAIToolStart?.(o?.id||i,n)}}),this.currentModel.id=t.id,this.currentModel.metadata=t}async getContextPrefix(){let e=u().pageSummary||`You are ${this.config?.name||"IntelliWeave"}. ${this.config?.instructions||"Speak in short sentences."}`;return typeof e=="function"&&(e=await e()),e}async onBeforeMessageProcessing(){this._lastKBentries.length==0&&this.updateKnowledgeBase(await this.knowledgeBase.search("__intelliweaveblanksearchforcontextitems__"));let e=await this.getContextPrefix(),t=(this.currentModel.config.maxTokens||4e3)*.5*4,i=0;for(;;){let n=this._lastKBentries.slice(),o=0;for(;o<i;){let r=n.findLastIndex(d=>!d.isContext);if(r==-1)throw new Error('Too much context, and no more items to remove. Check the size and number of "isContext: true" knowledge base entries, or use an LLM with a larger context window.');n.splice(r,1)}let a=`${e}
21
21
 
22
22
  You have access to a database of knowledge base items. These include "info" type items which provide information, "tour" type items which contain instructions you should follow (only do the first one from the list!), and "action" type items which become available as tool calls. Current info and tour items:
23
- `;r+=n.filter(o=>o.type=="info"||o.type=="tour"||o.type=="input-event").map(o=>JSON.stringify({id:o.id,type:o.type,name:o.name,content:typeof o.content=="function"?o.content():o.content})).join(`
24
- `),this.currentModel.config.systemMessage=r,this.currentModel.tools=n.filter(o=>o.type=="action"||o.type=="output-event").map(o=>({name:o.id.replaceAll(/[^a-zA-Z0-9_]/g,"_"),description:typeof o.content=="function"?o.content():o.content,params:o.parameters||[{name:"value",type:"string",description:"Input"}],removeFromMessageHistory:!!o.removeFromMessageHistory,callback:d=>this.toolRunKBAction(o,d),kb:o}));let l=this.currentModel.config.systemMessage;if(l+=JSON.stringify(this.currentModel.tools.map(o=>({name:o.name,description:o.description,params:o.params}))),l.length<=t)break;i+=1}this._lastSystemMsg!=this.currentModel.config.systemMessage&&(this._lastSystemMsg=this.currentModel.config.systemMessage,this.submitAnalyticsEvent({type:"system-msg",txt:this.currentModel.config.systemMessage}))}updateKnowledgeBase(e){this._lastKBentries=e}processIncomingMessage(e,t){t||this.submitAnalyticsEvent({type:"message",role:"assistant",message:e});let i="";this._lastOutput===void 0?(i=e,this._lastOutput=e):e.startsWith(this._lastOutput)?(i=e.substring(this._lastOutput.length),this._lastOutput=e):(w.warn("Message was changed during chunking. This should not happen.",e,this._lastOutput),i=e,this._lastOutput=e),t||(this._lastOutput=void 0),this.dispatchEvent(new CustomEvent("output",{detail:{ai:this,isChunk:t,message:e,messageChunk:i}})),this.onAIMessage?.(e,!!t)}async sendMessage(e){if(!this.currentModel)throw new Error("No model selected. Please call load() first.");if(this.isProcessing)return w.warn("Cannot send message while another message is being processed."),null;this.isProcessing=!0;try{return this.submitAnalyticsEvent({type:"message",role:"user",message:e}),this.dispatchEvent(new CustomEvent("input",{detail:{ai:this,message:e}})),await this.currentModel.sendMessage(e)}finally{this.isProcessing=!1}}async toolRunKBAction(e,t){try{this.dispatchEvent(new CustomEvent("toolstart",{detail:{knowledgeBaseEntry:e,input:t,ai:this}}));let i=await e.action(t,this);return this.submitAnalyticsEvent({type:"action",action:e.id,value:t,result:i}),this.dispatchEvent(new CustomEvent("tool",{detail:{knowledgeBaseEntry:e,input:t,ai:this,result:i}})),i}catch(i){throw this.submitAnalyticsEvent({type:"action",action:e.id,value:t,error:i.message}),this.dispatchEvent(new CustomEvent("tool",{detail:{knowledgeBaseEntry:e,input:t,ai:this,error:i}})),i}}submitAnalyticsEvent(e){u().analytics===!1||this.config?.analytics===!1||fetch("https://intelliweave.ai/api/analytics/post",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({...e,apiKey:this.apiKey,time:Date.now(),conversationID:this.conversationID})}).catch(t=>{w.debug("Failed to submit analytics event:",t)})}resetConversation(){this.currentModel&&(this.currentModel.resetConversation(),this.conversationID=de(),this._lastSystemMsg="")}insertAssistantMessage(e){if(!this.currentModel)throw new Error("No model selected. Please call load() first.");this.currentModel.messages.push({role:"assistant",content:e})}exportState(){return{type:"intelliweave/state/v1",conversationID:this.conversationID,messages:this.currentModel?.messages}}importState(e){if(!this.currentModel)throw new Error("No model selected. Please call load() first.");if(e?.type!="intelliweave/state/v1")throw new Error(`Invalid state type: ${e.type}`);this.conversationID=e.conversationID,this.currentModel.messages=e.messages}clone(){let e=new B;return e.apiKey=this.apiKey,e.config=this.config,e.models=this.models,this.config?.id&&e.setModel(this.config.id),e.audio=this.audio,e.vadModel=this.vadModel,e.userID=this.userID,e.extra=this.extra,e.knowledgeBase=this.knowledgeBase.clone(),e}};B.version=ce.version;var Z=B;var m=class extends HTMLElement{constructor(){super(...arguments);this._state={}}get root(){return this._shadow}static register(){this._isRegistered||(this._isRegistered=!0,this.tagName||(this.tagName="anonymous-component-"+this.name.toLowerCase().replace(/[^a-z0-9-]/g,"-")+"--"+Math.random().toString(36).substring(2)),window.customElements.define(this.tagName,this))}static create(e={},t=""){this.register();let i=document.createElement(this.tagName);for(let n in e)i.setAttribute(n,e[n]);return i.onBeforeCreate(),i.innerHTML=t,i}static add(e={},t=""){return this.register(),`<${this.tagName} ${Object.keys(e).map(i=>`${i}="${(e[i]+"").replaceAll('"',"&quot;")}"`).join(" ")}>${t}</${this.tagName}>`}static createElement(){return this.register(),document.createElement(this.tagName)}connectedCallback(){this._shadow||(this._shadow=this.attachShadow({mode:"closed"}),this._shadow.innerHTML=this.html(),this.onCreate()),this.onUpdate()}disconnectedCallback(){this._shadow=void 0,this.onDestroy()}html(){return""}onBeforeCreate(){}onCreate(){}onUpdate(){}onDestroy(){}attributeChangedCallback(e,t,i){this._shadow&&this.onUpdate()}get attr(){if(this._attrProxy)return this._attrProxy;let e=t=>(P=P||document.createElement("div"),P.innerText=t||"",P.innerHTML);return this._attrProxy=new Proxy({},{get:(t,i)=>e(this.getAttribute(i.toString())||""),set:(t,i,n)=>(this.getAttribute(i.toString())===n||(n?this.setAttribute(i.toString(),n):this.removeAttribute(i.toString()),this._shadow&&this.onUpdate()),!0)}),this._attrProxy}get state(){return this._stateProxy?this._stateProxy:(this._stateProxy=new Proxy({},{get:(e,t)=>this._state[t],set:(e,t,i)=>(this._state[t]=i,this._shadow&&this.onUpdate(),!0)}),this._stateProxy)}hasChild(e){return!!this.root?.getElementById(e)}child(e){let t=this.root?.getElementById(e);if(!t)throw new Error(`Child with ID ${e} not found`);return t}};m.observedAttributes=[],m.tagName="",m._isRegistered=!1;var P;var he='data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid" width="200" height="200" style="shape-rendering: auto; display: block; " xmlns:xlink="http://www.w3.org/1999/xlink"><g><circle cx="84" cy="50" r="10" fill="%23797979">%0A <animate attributeName="r" repeatCount="indefinite" dur="0.5s" calcMode="spline" keyTimes="0;1" values="10;0" keySplines="0 0.5 0.5 1" begin="0s"></animate>%0A <animate attributeName="fill" repeatCount="indefinite" dur="2s" calcMode="discrete" keyTimes="0;0.25;0.5;0.75;1" values="%23797979;%23797979;%23797979;%23797979;%23797979" begin="0s"></animate>%0A</circle><circle cx="16" cy="50" r="10" fill="%23797979">%0A <animate attributeName="r" repeatCount="indefinite" dur="2s" calcMode="spline" keyTimes="0;0.25;0.5;0.75;1" values="0;0;10;10;10" keySplines="0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1" begin="0s"></animate>%0A <animate attributeName="cx" repeatCount="indefinite" dur="2s" calcMode="spline" keyTimes="0;0.25;0.5;0.75;1" values="16;16;16;50;84" keySplines="0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1" begin="0s"></animate>%0A</circle><circle cx="50" cy="50" r="10" fill="%23797979">%0A <animate attributeName="r" repeatCount="indefinite" dur="2s" calcMode="spline" keyTimes="0;0.25;0.5;0.75;1" values="0;0;10;10;10" keySplines="0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1" begin="-0.5s"></animate>%0A <animate attributeName="cx" repeatCount="indefinite" dur="2s" calcMode="spline" keyTimes="0;0.25;0.5;0.75;1" values="16;16;16;50;84" keySplines="0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1" begin="-0.5s"></animate>%0A</circle><circle cx="84" cy="50" r="10" fill="%23797979">%0A <animate attributeName="r" repeatCount="indefinite" dur="2s" calcMode="spline" keyTimes="0;0.25;0.5;0.75;1" values="0;0;10;10;10" keySplines="0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1" begin="-1s"></animate>%0A <animate attributeName="cx" repeatCount="indefinite" dur="2s" calcMode="spline" keyTimes="0;0.25;0.5;0.75;1" values="16;16;16;50;84" keySplines="0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1" begin="-1s"></animate>%0A</circle><circle cx="16" cy="50" r="10" fill="%23797979">%0A <animate attributeName="r" repeatCount="indefinite" dur="2s" calcMode="spline" keyTimes="0;0.25;0.5;0.75;1" values="0;0;10;10;10" keySplines="0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1" begin="-1.5s"></animate>%0A <animate attributeName="cx" repeatCount="indefinite" dur="2s" calcMode="spline" keyTimes="0;0.25;0.5;0.75;1" values="16;16;16;50;84" keySplines="0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1" begin="-1.5s"></animate>%0A</circle><g></g></g><!-- [ldio] generated by https://loading.io --></svg>';var ue='data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" width="512" height="512" x="0" y="0" viewBox="0 0 475.085 475.085" style="enable-background:new 0 0 512 512" xml:space="preserve" class=""><g><path d="M237.541 328.897c25.128 0 46.632-8.946 64.523-26.83 17.888-17.884 26.833-39.399 26.833-64.525V91.365c0-25.126-8.938-46.632-26.833-64.525C284.173 8.951 262.669 0 237.541 0c-25.125 0-46.632 8.951-64.524 26.84-17.893 17.89-26.838 39.399-26.838 64.525v146.177c0 25.125 8.949 46.641 26.838 64.525 17.889 17.884 39.399 26.83 64.524 26.83z" fill="%23ffffff" opacity="1" data-original="%23000000" class=""></path><path d="M396.563 188.15c-3.606-3.617-7.898-5.426-12.847-5.426-4.944 0-9.226 1.809-12.847 5.426-3.613 3.616-5.421 7.898-5.421 12.845v36.547c0 35.214-12.518 65.333-37.548 90.362-25.022 25.03-55.145 37.545-90.36 37.545-35.214 0-65.334-12.515-90.365-37.545-25.028-25.022-37.541-55.147-37.541-90.362v-36.547c0-4.947-1.809-9.229-5.424-12.845-3.617-3.617-7.895-5.426-12.847-5.426s-9.235 1.809-12.85 5.426c-3.618 3.616-5.426 7.898-5.426 12.845v36.547c0 42.065 14.04 78.659 42.112 109.776 28.073 31.118 62.762 48.961 104.068 53.526v37.691h-73.089c-4.949 0-9.231 1.811-12.847 5.428-3.617 3.614-5.426 7.898-5.426 12.847 0 4.941 1.809 9.233 5.426 12.847 3.616 3.614 7.898 5.428 12.847 5.428h182.719c4.948 0 9.236-1.813 12.847-5.428 3.621-3.613 5.431-7.905 5.431-12.847 0-4.948-1.81-9.232-5.431-12.847-3.61-3.617-7.898-5.428-12.847-5.428h-73.08v-37.691c41.299-4.565 75.985-22.408 104.061-53.526 28.076-31.117 42.12-67.711 42.12-109.776v-36.547c0-4.946-1.813-9.225-5.435-12.845z" fill="%23ffffff" opacity="1" data-original="%23000000" class=""></path></g></svg>';var pe='data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" width="512" height="512" x="0" y="0" viewBox="0 0 448.075 448.075" style="enable-background:new 0 0 512 512" xml:space="preserve" class=""><g><path d="M352.021 16.075c0-6.08-3.52-11.84-8.96-14.4-5.76-2.88-12.16-1.92-16.96 1.92l-141.76 112.96 167.68 167.68zM443.349 420.747l-416-416c-6.24-6.24-16.384-6.24-22.624 0s-6.24 16.384 0 22.624l100.672 100.704h-9.376c-9.92 0-18.56 4.48-24.32 11.52-4.8 5.44-7.68 12.8-7.68 20.48v128c0 17.6 14.4 32 32 32h74.24l155.84 124.48c2.88 2.24 6.4 3.52 9.92 3.52 2.24 0 4.8-.64 7.04-1.6 5.44-2.56 8.96-8.32 8.96-14.4v-57.376l68.672 68.672c3.136 3.136 7.232 4.704 11.328 4.704s8.192-1.568 11.328-4.672c6.24-6.272 6.24-16.384 0-22.656z" fill="%23ffffff" opacity="1" data-original="%23000000" class=""></path></g></svg>';var F=class extends m{constructor(){super(...arguments);this.html=()=>`
23
+ `;a+=n.filter(r=>r.type=="info"||r.type=="tour"||r.type=="input-event").map(r=>JSON.stringify({id:r.id,type:r.type,name:r.name,content:typeof r.content=="function"?r.content():r.content})).join(`
24
+ `),this.currentModel.config.systemMessage=a,this.currentModel.tools=n.filter(r=>r.type=="action"||r.type=="output-event").map(r=>({name:r.id.replaceAll(/[^a-zA-Z0-9_]/g,"_"),description:typeof r.content=="function"?r.content():r.content,params:r.parameters||[{name:"value",type:"string",description:"Input"}],removeFromMessageHistory:!!r.removeFromMessageHistory,callback:d=>this.toolRunKBAction(r,d),kb:r}));let l=this.currentModel.config.systemMessage;if(l+=JSON.stringify(this.currentModel.tools.map(r=>({name:r.name,description:r.description,params:r.params}))),l.length<=t)break;i+=1}this._lastSystemMsg!=this.currentModel.config.systemMessage&&(this._lastSystemMsg=this.currentModel.config.systemMessage,this.submitAnalyticsEvent({type:"system-msg",txt:this.currentModel.config.systemMessage}))}updateKnowledgeBase(e){this._lastKBentries=e}processIncomingMessage(e,t){t||this.submitAnalyticsEvent({type:"message",role:"assistant",message:e});let i="";this._lastOutput===void 0?(i=e,this._lastOutput=e):e.startsWith(this._lastOutput)?(i=e.substring(this._lastOutput.length),this._lastOutput=e):(v.warn("Message was changed during chunking. This should not happen.",e,this._lastOutput),i=e,this._lastOutput=e),t||(this._lastOutput=void 0),this.dispatchEvent(new CustomEvent("output",{detail:{ai:this,isChunk:t,message:e,messageChunk:i}})),this.onAIMessage?.(e,!!t)}async sendMessage(e){if(!this.currentModel)throw new Error("No model selected. Please call load() first.");if(this.isProcessing)return v.warn("Cannot send message while another message is being processed."),null;this.isProcessing=!0;try{return this.submitAnalyticsEvent({type:"message",role:"user",message:e}),this.dispatchEvent(new CustomEvent("input",{detail:{ai:this,message:e}})),await this.currentModel.sendMessage(e)}finally{this.isProcessing=!1}}async toolRunKBAction(e,t){try{this.dispatchEvent(new CustomEvent("toolstart",{detail:{knowledgeBaseEntry:e,input:t,ai:this}}));let i=await e.action(t,this);return this.submitAnalyticsEvent({type:"action",action:e.id,value:t,result:i}),this.dispatchEvent(new CustomEvent("tool",{detail:{knowledgeBaseEntry:e,input:t,ai:this,result:i}})),i}catch(i){throw this.submitAnalyticsEvent({type:"action",action:e.id,value:t,error:i.message}),this.dispatchEvent(new CustomEvent("tool",{detail:{knowledgeBaseEntry:e,input:t,ai:this,error:i}})),i}}submitAnalyticsEvent(e){u().analytics===!1||this.config?.analytics===!1||fetch("https://intelliweave.ai/api/analytics/post",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({...e,apiKey:this.apiKey,time:Date.now(),conversationID:this.conversationID})}).catch(t=>{v.debug("Failed to submit analytics event:",t)})}resetConversation(){this.currentModel&&(this.currentModel.resetConversation(),this.conversationID=de(),this._lastSystemMsg="")}insertAssistantMessage(e){if(!this.currentModel)throw new Error("No model selected. Please call load() first.");this.currentModel.messages.push({role:"assistant",content:e})}exportState(){return{type:"intelliweave/state/v1",conversationID:this.conversationID,messages:this.currentModel?.messages}}importState(e){if(!this.currentModel)throw new Error("No model selected. Please call load() first.");if(e?.type!="intelliweave/state/v1")throw new Error(`Invalid state type: ${e.type}`);this.conversationID=e.conversationID,this.currentModel.messages=e.messages}clone(){let e=new B;return e.apiKey=this.apiKey,e.config=this.config,e.models=this.models,this.config?.id&&e.setModel(this.config.id),e.audio=this.audio,e.vadModel=this.vadModel,e.userID=this.userID,e.extra=this.extra,e.knowledgeBase=this.knowledgeBase.clone(),e}};B.version=ce.version;var Z=B;var m=class extends HTMLElement{constructor(){super(...arguments);this._state={}}get root(){return this._shadow}static register(){this._isRegistered||(this._isRegistered=!0,this.tagName||(this.tagName="anonymous-component-"+this.name.toLowerCase().replace(/[^a-z0-9-]/g,"-")+"--"+Math.random().toString(36).substring(2)),window.customElements.define(this.tagName,this))}static create(e={},t=""){this.register();let i=document.createElement(this.tagName);for(let n in e)i.setAttribute(n,e[n]);return i.onBeforeCreate(),i.innerHTML=t,i}static add(e={},t=""){return this.register(),`<${this.tagName} ${Object.keys(e).map(i=>`${i}="${(e[i]+"").replaceAll('"',"&quot;")}"`).join(" ")}>${t}</${this.tagName}>`}static createElement(){return this.register(),document.createElement(this.tagName)}connectedCallback(){this._shadow||(this._shadow=this.attachShadow({mode:"closed"}),this._shadow.innerHTML=this.html(),this.onCreate()),this.onUpdate()}disconnectedCallback(){this._shadow=void 0,this.onDestroy()}html(){return""}onBeforeCreate(){}onCreate(){}onUpdate(){}onDestroy(){}attributeChangedCallback(e,t,i){this._shadow&&this.onUpdate()}get attr(){if(this._attrProxy)return this._attrProxy;let e=t=>(P=P||document.createElement("div"),P.innerText=t||"",P.innerHTML);return this._attrProxy=new Proxy({},{get:(t,i)=>e(this.getAttribute(i.toString())||""),set:(t,i,n)=>(this.getAttribute(i.toString())===n||(n?this.setAttribute(i.toString(),n):this.removeAttribute(i.toString()),this._shadow&&this.onUpdate()),!0)}),this._attrProxy}get state(){return this._stateProxy?this._stateProxy:(this._stateProxy=new Proxy({},{get:(e,t)=>this._state[t],set:(e,t,i)=>(this._state[t]=i,this._shadow&&this.onUpdate(),!0)}),this._stateProxy)}hasChild(e){return!!this.root?.getElementById(e)}child(e){let t=this.root?.getElementById(e);if(!t)throw new Error(`Child with ID ${e} not found`);return t}};m.observedAttributes=[],m.tagName="",m._isRegistered=!1;var P;var he='data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid" width="200" height="200" style="shape-rendering: auto; display: block; " xmlns:xlink="http://www.w3.org/1999/xlink"><g><circle cx="84" cy="50" r="10" fill="%23797979">%0A <animate attributeName="r" repeatCount="indefinite" dur="0.5s" calcMode="spline" keyTimes="0;1" values="10;0" keySplines="0 0.5 0.5 1" begin="0s"></animate>%0A <animate attributeName="fill" repeatCount="indefinite" dur="2s" calcMode="discrete" keyTimes="0;0.25;0.5;0.75;1" values="%23797979;%23797979;%23797979;%23797979;%23797979" begin="0s"></animate>%0A</circle><circle cx="16" cy="50" r="10" fill="%23797979">%0A <animate attributeName="r" repeatCount="indefinite" dur="2s" calcMode="spline" keyTimes="0;0.25;0.5;0.75;1" values="0;0;10;10;10" keySplines="0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1" begin="0s"></animate>%0A <animate attributeName="cx" repeatCount="indefinite" dur="2s" calcMode="spline" keyTimes="0;0.25;0.5;0.75;1" values="16;16;16;50;84" keySplines="0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1" begin="0s"></animate>%0A</circle><circle cx="50" cy="50" r="10" fill="%23797979">%0A <animate attributeName="r" repeatCount="indefinite" dur="2s" calcMode="spline" keyTimes="0;0.25;0.5;0.75;1" values="0;0;10;10;10" keySplines="0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1" begin="-0.5s"></animate>%0A <animate attributeName="cx" repeatCount="indefinite" dur="2s" calcMode="spline" keyTimes="0;0.25;0.5;0.75;1" values="16;16;16;50;84" keySplines="0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1" begin="-0.5s"></animate>%0A</circle><circle cx="84" cy="50" r="10" fill="%23797979">%0A <animate attributeName="r" repeatCount="indefinite" dur="2s" calcMode="spline" keyTimes="0;0.25;0.5;0.75;1" values="0;0;10;10;10" keySplines="0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1" begin="-1s"></animate>%0A <animate attributeName="cx" repeatCount="indefinite" dur="2s" calcMode="spline" keyTimes="0;0.25;0.5;0.75;1" values="16;16;16;50;84" keySplines="0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1" begin="-1s"></animate>%0A</circle><circle cx="16" cy="50" r="10" fill="%23797979">%0A <animate attributeName="r" repeatCount="indefinite" dur="2s" calcMode="spline" keyTimes="0;0.25;0.5;0.75;1" values="0;0;10;10;10" keySplines="0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1" begin="-1.5s"></animate>%0A <animate attributeName="cx" repeatCount="indefinite" dur="2s" calcMode="spline" keyTimes="0;0.25;0.5;0.75;1" values="16;16;16;50;84" keySplines="0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1" begin="-1.5s"></animate>%0A</circle><g></g></g><!-- [ldio] generated by https://loading.io --></svg>';var ue='data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" width="512" height="512" x="0" y="0" viewBox="0 0 475.085 475.085" style="enable-background:new 0 0 512 512" xml:space="preserve" class=""><g><path d="M237.541 328.897c25.128 0 46.632-8.946 64.523-26.83 17.888-17.884 26.833-39.399 26.833-64.525V91.365c0-25.126-8.938-46.632-26.833-64.525C284.173 8.951 262.669 0 237.541 0c-25.125 0-46.632 8.951-64.524 26.84-17.893 17.89-26.838 39.399-26.838 64.525v146.177c0 25.125 8.949 46.641 26.838 64.525 17.889 17.884 39.399 26.83 64.524 26.83z" fill="%23ffffff" opacity="1" data-original="%23000000" class=""></path><path d="M396.563 188.15c-3.606-3.617-7.898-5.426-12.847-5.426-4.944 0-9.226 1.809-12.847 5.426-3.613 3.616-5.421 7.898-5.421 12.845v36.547c0 35.214-12.518 65.333-37.548 90.362-25.022 25.03-55.145 37.545-90.36 37.545-35.214 0-65.334-12.515-90.365-37.545-25.028-25.022-37.541-55.147-37.541-90.362v-36.547c0-4.947-1.809-9.229-5.424-12.845-3.617-3.617-7.895-5.426-12.847-5.426s-9.235 1.809-12.85 5.426c-3.618 3.616-5.426 7.898-5.426 12.845v36.547c0 42.065 14.04 78.659 42.112 109.776 28.073 31.118 62.762 48.961 104.068 53.526v37.691h-73.089c-4.949 0-9.231 1.811-12.847 5.428-3.617 3.614-5.426 7.898-5.426 12.847 0 4.941 1.809 9.233 5.426 12.847 3.616 3.614 7.898 5.428 12.847 5.428h182.719c4.948 0 9.236-1.813 12.847-5.428 3.621-3.613 5.431-7.905 5.431-12.847 0-4.948-1.81-9.232-5.431-12.847-3.61-3.617-7.898-5.428-12.847-5.428h-73.08v-37.691c41.299-4.565 75.985-22.408 104.061-53.526 28.076-31.117 42.12-67.711 42.12-109.776v-36.547c0-4.946-1.813-9.225-5.435-12.845z" fill="%23ffffff" opacity="1" data-original="%23000000" class=""></path></g></svg>';var pe='data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" width="512" height="512" x="0" y="0" viewBox="0 0 448.075 448.075" style="enable-background:new 0 0 512 512" xml:space="preserve" class=""><g><path d="M352.021 16.075c0-6.08-3.52-11.84-8.96-14.4-5.76-2.88-12.16-1.92-16.96 1.92l-141.76 112.96 167.68 167.68zM443.349 420.747l-416-416c-6.24-6.24-16.384-6.24-22.624 0s-6.24 16.384 0 22.624l100.672 100.704h-9.376c-9.92 0-18.56 4.48-24.32 11.52-4.8 5.44-7.68 12.8-7.68 20.48v128c0 17.6 14.4 32 32 32h74.24l155.84 124.48c2.88 2.24 6.4 3.52 9.92 3.52 2.24 0 4.8-.64 7.04-1.6 5.44-2.56 8.96-8.32 8.96-14.4v-57.376l68.672 68.672c3.136 3.136 7.232 4.704 11.328 4.704s8.192-1.568 11.328-4.672c6.24-6.272 6.24-16.384 0-22.656z" fill="%23ffffff" opacity="1" data-original="%23000000" class=""></path></g></svg>';var K=class extends m{constructor(){super(...arguments);this.html=()=>`
25
25
 
26
26
  <!-- Styling -->
27
27
  <style>
@@ -132,7 +132,7 @@ You have access to a database of knowledge base items. These include "info" type
132
132
  <!-- Loader bar -->
133
133
  <div id='loader'></div>
134
134
 
135
- `;this._wasLoading=!1;this.voiceAnimationPieces=[];this._micAnimFrameCounter=0}onCreate(){this.child("input-field").addEventListener("keypress",e=>this.onInputKeyPress(e),{capture:!0}),this.child("input-field").addEventListener("keydown",e=>e.stopPropagation(),{capture:!0}),this.child("llm-button").addEventListener("click",e=>this.onLLMButtonClick(e)),this.child("microphone-button").addEventListener("click",e=>this.onMicButtonClick(e)),this.child("cancel-speech-button").addEventListener("click",e=>this.onCancelSpeechButtonClick(e))}connectAI(e){this.ai=e,this.ai.audio?.speechRecognition.addEventListener("speech",t=>this.onSpeechEvent(t)),this.ai.audio?.speechRecognition.addEventListener("start",t=>this.onUpdate()),this.ai.audio?.speechRecognition.addEventListener("end",t=>this.onUpdate()),this.ai.audio?.speechOutput.addEventListener("speechstart",t=>this.onUpdate()),this.ai.audio?.speechOutput.addEventListener("speechend",t=>this.onUpdate()),this.onUpdate()}onUpdate(){this.child("input-field").style.display=this.attr.loading||this.ai?.audio?.speechRecognition.isRunning?"none":"",this.child("llm-button").style.display=this.attr.loading||!this.attr.llmButtonVisible||this.state.micActiv?"none":"",this.child("llm-button").innerText=this.attr.llmName||"LLM",this.child("loader").style.display=this.attr.loading?"block":"",this.child("microphone-button").style.display=!this.ai?.audio?.speechRecognition?.isSupported||this.attr.loading&&!this.ai?.audio?.speechRecognition.isRunning?"none":"",this.child("cancel-speech-button").style.display="none",this.ai?.audio?.speechRecognition.isRunning&&this.ai?.audio?.speechOutput.isSpeaking&&(this.child("microphone-button").style.display="none",this.child("cancel-speech-button").style.display=""),!this.attr.loading&&this._wasLoading&&this.child("input-field").focus(),this._wasLoading=this.attr.loading,this.child("microphone-button-bg").classList.toggle("active",!!this.ai?.audio?.speechRecognition.isRunning);let e=this.ai?.audio?.speechRecognition.isRunning&&!this.attr.loading;e&&!this.micStateAnimationTimer?this.micStateAnimationTimer=setInterval(()=>this.onMicAnimation(),1e3/60):!e&&this.micStateAnimationTimer&&(this.voiceAnimationPieces.forEach(t=>t.remove()),this.voiceAnimationPieces=[],clearInterval(this.micStateAnimationTimer),this.micStateAnimationTimer=void 0)}onInputKeyPress(e){if(e.stopPropagation(),e.key!="Enter")return;let t=this.child("input-field"),i=t.value?.trim();t.value="",i&&this.dispatchEvent(new CustomEvent("input-message",{detail:i}))}onLLMButtonClick(e){e.preventDefault(),this.dispatchEvent(new CustomEvent("llm-button-click"))}onMicButtonClick(e){e.preventDefault(),this.ai.audio?.speechRecognition.isRunning?this.ai.audio?.speechRecognition.stop():this.ai.audio?.speechRecognition.start()}onMicAnimation(){if(!this.attr.loading){if(this._micAnimFrameCounter++,this._micAnimFrameCounter%5==0){let e="rgba(140, 170, 200, 0.5)";this.ai?.audio?.speechRecognition.voiceDetection?.isModelLoaded||(e="rgba(255, 0, 0, 0.5)"),this.ai?.audio?.speechRecognition.voiceDetection?.isTranscribing&&(e="rgba(96, 66, 245, 0.9)"),this.ai?.audio?.speechRecognition.wordsCurrentlyBeingSpoken&&(e="rgba(0, 128, 255, 0.9)"),this.ai?.audio?.speechRecognition.voiceDetection?.isVoicePossiblyEnding&&(e="rgba(0, 128, 255, 0.5)"),this.ai?.isProcessing&&(e="rgba(255, 255, 0, 0.5)"),this.ai?.audio?.speechOutput.isSpeaking&&(e="rgba(0, 255, 0, 0.5)");let t=this.ai?.audio?.speechRecognition.volumeLevel||0,i=30,n=6,a=Math.round(t*(i-n)+n),r=document.createElement("div");r.posX=50,r.style.cssText=`position: absolute; top: calc(50% - ${a}px/2); left: 0px; width: 6px; height: ${a}px; background-color: ${e}; border-radius: 3px; pointer-events: none; `,this.root?.appendChild(r),this.voiceAnimationPieces.push(r)}for(let e=0;e<this.voiceAnimationPieces.length;e++){let t=this.voiceAnimationPieces[e];t.posX+=2,t.style.transform=`translate(${t.posX}px, 0px)`,!(t.posX<400)&&(this.voiceAnimationPieces.splice(e--,1),t.remove())}}}onSpeechEvent(e){this.attr.loading||e.detail.isFinal&&this.dispatchEvent(new CustomEvent("input-message",{detail:e.detail.transcript}))}onCancelSpeechButtonClick(e){this.ai.audio?.speechOutput.interrupt(),this.onUpdate()}};var $='data:image/svg+xml,<svg width="64" height="64" viewBox="0 0 64 64" fill="none" xmlns="http://www.w3.org/2000/svg">%0A<g filter="url(%23filter0_d_59_2)">%0A<path d="M45.8239 17.4141C45.7221 17.4141 45.6244 17.4547 45.5524 17.527C45.4804 17.5994 45.4399 17.6974 45.4399 17.7997V26.7988C45.4399 26.901 45.4804 26.9991 45.5524 27.0715C45.6244 27.1438 45.7221 27.1844 45.8239 27.1844C45.9258 27.1844 46.0235 27.1438 46.0955 27.0715C46.1675 26.9991 46.2079 26.901 46.2079 26.7988V17.7997C46.2079 17.6974 46.1675 17.5994 46.0955 17.527C46.0235 17.4547 45.9258 17.4141 45.8239 17.4141Z" fill="%23263238"/>%0A<path d="M17.792 17.7997V26.7988C17.792 26.901 17.8324 26.9991 17.9045 27.0715C17.9765 27.1438 18.0741 27.1844 18.176 27.1844C18.2778 27.1844 18.3755 27.1438 18.4475 27.0715C18.5195 26.9991 18.56 26.901 18.56 26.7988V17.7997C18.56 17.6974 18.5195 17.5994 18.4475 17.527C18.3755 17.4547 18.2778 17.4141 18.176 17.4141C18.0741 17.4141 17.9765 17.4547 17.9045 17.527C17.8324 17.5994 17.792 17.6974 17.792 17.7997Z" fill="%23263238"/>%0A<path d="M45.8239 20.371C47.0257 20.371 47.9999 19.3925 47.9999 18.1855C47.9999 16.9785 47.0257 16 45.8239 16C44.6222 16 43.6479 16.9785 43.6479 18.1855C43.6479 19.3925 44.6222 20.371 45.8239 20.371Z" fill="%2337474F"/>%0A<path d="M32 17.6713C23.147 17.6713 16 24.4007 16 33.2298V39.1031C16 40.3404 16.4894 41.527 17.3605 42.402C18.2317 43.2769 19.4132 43.7684 20.6451 43.7684H27.7791C27.8889 43.7677 27.9953 43.8068 28.0788 43.8786C28.1622 43.9504 28.2169 44.0501 28.233 44.1592C28.3097 44.9387 28.149 45.7232 27.7722 46.409C27.6767 46.6051 27.6412 46.8253 27.6702 47.0416C27.6991 47.258 27.7912 47.4609 27.9348 47.6248C28.0784 47.7887 28.2671 47.9062 28.4771 47.9626C28.6871 48.0189 28.909 48.0115 29.1149 47.9414C31.1206 47.2456 33.6033 46.02 35.1035 43.9618C35.1458 43.9024 35.2016 43.8539 35.2662 43.8203C35.3308 43.7866 35.4024 43.7689 35.4752 43.7684H43.3549C44.5868 43.7684 45.7683 43.2769 46.6395 42.402C47.5106 41.527 48 40.3404 48 39.1031V33.2298C48.0013 24.4007 40.853 17.6713 32 17.6713Z" fill="%23CFD8DC"/>%0A<path d="M21.6319 40.4259C21.0208 40.4259 20.4348 40.1821 20.0027 39.7481C19.5706 39.3142 19.3279 38.7256 19.3279 38.1119V33.0981C19.2132 26.2146 25.2297 20.9019 31.9999 20.8852C38.7908 20.8682 44.7889 26.1915 44.6719 33.0981V38.1119C44.6719 38.7256 44.4292 39.3142 43.9971 39.7481C43.565 40.1821 42.979 40.4259 42.3679 40.4259H21.6319Z" fill="%2337474F"/>%0A<path d="M18.176 20.371C19.3778 20.371 20.352 19.3925 20.352 18.1855C20.352 16.9785 19.3778 16 18.176 16C16.9742 16 16 16.9785 16 18.1855C16 19.3925 16.9742 20.371 18.176 20.371Z" fill="%2337474F"/>%0A<path d="M25.6001 31.4269C26.6604 31.4269 27.5201 30.5636 27.5201 29.4986C27.5201 28.4335 26.6604 27.5702 25.6001 27.5702C24.5397 27.5702 23.6801 28.4335 23.6801 29.4986C23.6801 30.5636 24.5397 31.4269 25.6001 31.4269Z" fill="%2303A9F4"/>%0A<path d="M38.4 31.4269C39.4604 31.4269 40.32 30.5636 40.32 29.4986C40.32 28.4335 39.4604 27.5702 38.4 27.5702C37.3396 27.5702 36.48 28.4335 36.48 29.4986C36.48 30.5636 37.3396 31.4269 38.4 31.4269Z" fill="%2303A9F4"/>%0A<path d="M28.4671 33.2267C28.3532 33.2267 28.2408 33.2521 28.1378 33.3011C28.0349 33.3501 27.9442 33.4215 27.8721 33.5101C27.8001 33.5987 27.7486 33.7023 27.7213 33.8134C27.694 33.9244 27.6917 34.0402 27.7145 34.1523C27.9256 35.1364 28.466 36.0181 29.2458 36.6506C30.0256 37.283 30.9976 37.628 31.9999 37.628C33.0022 37.628 33.9743 37.283 34.7541 36.6506C35.5338 36.0181 36.0743 35.1364 36.2854 34.1523C36.3081 34.0402 36.3058 33.9244 36.2786 33.8134C36.2513 33.7023 36.1998 33.5987 36.1277 33.5101C36.0557 33.4215 35.9649 33.3501 35.862 33.3011C35.7591 33.2521 35.6466 33.2267 35.5327 33.2267H28.4671Z" fill="white"/>%0A<path d="M35.2179 36.2144C34.8077 36.6603 34.3101 37.0162 33.7563 37.2595C33.2025 37.5029 32.6045 37.6285 32 37.6285C31.3955 37.6285 30.7976 37.5029 30.2438 37.2595C29.69 37.0162 29.1923 36.6603 28.7821 36.2144C29.2008 35.7831 29.7012 35.4403 30.2539 35.2063C30.8065 34.9723 31.4003 34.8517 32 34.8517C32.5998 34.8517 33.1935 34.9723 33.7462 35.2063C34.2988 35.4403 34.7992 35.7831 35.2179 36.2144Z" fill="%2303A9F4"/>%0A</g>%0A<defs>%0A<filter id="filter0_d_59_2" x="12" y="16" width="40" height="40" filterUnits="userSpaceOnUse" color-interpolation-filters="sRGB">%0A<feFlood flood-opacity="0" result="BackgroundImageFix"/>%0A<feColorMatrix in="SourceAlpha" type="matrix" values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 127 0" result="hardAlpha"/>%0A<feOffset dy="4"/>%0A<feGaussianBlur stdDeviation="2"/>%0A<feComposite in2="hardAlpha" operator="out"/>%0A<feColorMatrix type="matrix" values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.25 0"/>%0A<feBlend mode="normal" in2="BackgroundImageFix" result="effect1_dropShadow_59_2"/>%0A<feBlend mode="normal" in="SourceGraphic" in2="effect1_dropShadow_59_2" result="shape"/>%0A</filter>%0A</defs>%0A</svg>%0A';var M=class extends m{constructor(){super(...arguments);this.html=()=>`
135
+ `;this._wasLoading=!1;this.voiceAnimationPieces=[];this._micAnimFrameCounter=0}onCreate(){this.child("input-field").addEventListener("keypress",e=>this.onInputKeyPress(e),{capture:!0}),this.child("input-field").addEventListener("keydown",e=>e.stopPropagation(),{capture:!0}),this.child("llm-button").addEventListener("click",e=>this.onLLMButtonClick(e)),this.child("microphone-button").addEventListener("click",e=>this.onMicButtonClick(e)),this.child("cancel-speech-button").addEventListener("click",e=>this.onCancelSpeechButtonClick(e))}connectAI(e){this.ai=e,this.ai.audio?.speechRecognition.addEventListener("speech",t=>this.onSpeechEvent(t)),this.ai.audio?.speechRecognition.addEventListener("start",t=>this.onUpdate()),this.ai.audio?.speechRecognition.addEventListener("end",t=>this.onUpdate()),this.ai.audio?.speechOutput.addEventListener("speechstart",t=>this.onUpdate()),this.ai.audio?.speechOutput.addEventListener("speechend",t=>this.onUpdate()),this.onUpdate()}onUpdate(){this.child("input-field").style.display=this.attr.loading||this.ai?.audio?.speechRecognition.isRunning?"none":"",this.child("llm-button").style.display=this.attr.loading||!this.attr.llmButtonVisible||this.state.micActiv?"none":"",this.child("llm-button").innerText=this.attr.llmName||"LLM",this.child("loader").style.display=this.attr.loading?"block":"",this.child("microphone-button").style.display=!this.ai?.audio?.speechRecognition?.isSupported||this.attr.loading&&!this.ai?.audio?.speechRecognition.isRunning?"none":"",this.child("cancel-speech-button").style.display="none",this.ai?.audio?.speechRecognition.isRunning&&this.ai?.audio?.speechOutput.isSpeaking&&(this.child("microphone-button").style.display="none",this.child("cancel-speech-button").style.display=""),!this.attr.loading&&this._wasLoading&&this.child("input-field").focus(),this._wasLoading=this.attr.loading,this.child("microphone-button-bg").classList.toggle("active",!!this.ai?.audio?.speechRecognition.isRunning);let e=this.ai?.audio?.speechRecognition.isRunning&&!this.attr.loading;e&&!this.micStateAnimationTimer?this.micStateAnimationTimer=setInterval(()=>this.onMicAnimation(),1e3/60):!e&&this.micStateAnimationTimer&&(this.voiceAnimationPieces.forEach(t=>t.remove()),this.voiceAnimationPieces=[],clearInterval(this.micStateAnimationTimer),this.micStateAnimationTimer=void 0)}onInputKeyPress(e){if(e.stopPropagation(),e.key!="Enter")return;let t=this.child("input-field"),i=t.value?.trim();t.value="",i&&this.dispatchEvent(new CustomEvent("input-message",{detail:i}))}onLLMButtonClick(e){e.preventDefault(),this.dispatchEvent(new CustomEvent("llm-button-click"))}onMicButtonClick(e){e.preventDefault(),this.ai.audio?.speechRecognition.isRunning?this.ai.audio?.speechRecognition.stop():this.ai.audio?.speechRecognition.start()}onMicAnimation(){if(!this.attr.loading){if(this._micAnimFrameCounter++,this._micAnimFrameCounter%5==0){let e="rgba(140, 170, 200, 0.5)";this.ai?.audio?.speechRecognition.voiceDetection?.isModelLoaded||(e="rgba(255, 0, 0, 0.5)"),this.ai?.audio?.speechRecognition.voiceDetection?.isTranscribing&&(e="rgba(96, 66, 245, 0.9)"),this.ai?.audio?.speechRecognition.wordsCurrentlyBeingSpoken&&(e="rgba(0, 128, 255, 0.9)"),this.ai?.audio?.speechRecognition.voiceDetection?.isVoicePossiblyEnding&&(e="rgba(0, 128, 255, 0.5)"),this.ai?.isProcessing&&(e="rgba(255, 255, 0, 0.5)"),this.ai?.audio?.speechOutput.isSpeaking&&(e="rgba(0, 255, 0, 0.5)");let t=this.ai?.audio?.speechRecognition.volumeLevel||0,i=30,n=6,o=Math.round(t*(i-n)+n),a=document.createElement("div");a.posX=50,a.style.cssText=`position: absolute; top: calc(50% - ${o}px/2); left: 0px; width: 6px; height: ${o}px; background-color: ${e}; border-radius: 3px; pointer-events: none; `,this.root?.appendChild(a),this.voiceAnimationPieces.push(a)}for(let e=0;e<this.voiceAnimationPieces.length;e++){let t=this.voiceAnimationPieces[e];t.posX+=2,t.style.transform=`translate(${t.posX}px, 0px)`,!(t.posX<400)&&(this.voiceAnimationPieces.splice(e--,1),t.remove())}}}onSpeechEvent(e){this.attr.loading||e.detail.isFinal&&this.dispatchEvent(new CustomEvent("input-message",{detail:e.detail.transcript}))}onCancelSpeechButtonClick(e){this.ai.audio?.speechOutput.interrupt(),this.onUpdate()}};var $='data:image/svg+xml,<svg id="edmBubc6DX81" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 300 300" shape-rendering="geometricPrecision" text-rendering="geometricPrecision" project-id="f6f2d2bfa6d747199fb5009563ed8860" export-id="3107d0125cc14ffab066a17ed23ae565" cached="false"><defs><radialGradient id="edmBubc6DX86-fill" cx="0" cy="0" r="1" spreadMethod="pad" gradientUnits="userSpaceOnUse" gradientTransform="matrix(-58.520001 292.985 -172.197 -34.394001 203.907 57.158)"><stop id="edmBubc6DX86-fill-0" offset="0%" stop-color="%232b3d43"/><stop id="edmBubc6DX86-fill-1" offset="33%" stop-color="%23328897"/><stop id="edmBubc6DX86-fill-2" offset="58%" stop-color="%23328897"/><stop id="edmBubc6DX86-fill-3" offset="100%" stop-color="%237ebf96"/></radialGradient><radialGradient id="edmBubc6DX87-fill" cx="0" cy="0" r="1.112049" spreadMethod="pad" gradientUnits="userSpaceOnUse" gradientTransform="matrix(182.129 -94.016999 94.016999 182.129 43.45528 184.625838)"><stop id="edmBubc6DX87-fill-0" offset="0%" stop-color="%233f2d2d"/><stop id="edmBubc6DX87-fill-1" offset="28%" stop-color="%23c83b35"/><stop id="edmBubc6DX87-fill-2" offset="81%" stop-color="%23f69153"/><stop id="edmBubc6DX87-fill-3" offset="100%" stop-color="%23ffe388"/></radialGradient></defs><g transform="matrix(-.678224 0 0-.678224 235.380575 233.759269)"><g transform="translate(-.384843 0)"><g><path d="M170.6,53.5q-6,1.4-13.1,4.7-1.4.6-2.7,1.3-.1,0-.3.1c-14.8,7.1-30.6,18.5-46.7,35.1-17.9,17.7-34,38.5-43.3,57.7-9.1,18.6-10.6,33.2-4,39.9c2.7,2.8,6.6,4.2,11.6,4.2c4.3,0,9.4-1.1,15.1-3.2q3-1.1,6.2-2.6c33.5-15.5,75.2-57.8,93-94.2c9.1-18.6,10.5-33.2,3.9-40-4.1-4.1-10.9-5.1-19.7-3Z" fill="%23252d3a"/></g><path d="M169.3,54.4c8.7-2.1,15.4-1.1,19.4,3c6.5,6.6,5.1,20.9-3.9,39.2-17.4,35.8-58.3,77.2-91.2,92.5q-3.2,1.4-6.1,2.5v.1c-46,17-81.4-9.6-87.6-53.5C6.9,201.1,60.3,250,125,250c69.5,0,125.8-56.4,125.8-125.9q0-7.4-.8-14.5c-6.1-41.5-38.2-67.2-80.7-55.2Z" transform="matrix(.993225 0 0 1 2.099246-.787241)" fill="url(%23edmBubc6DX86-fill)"/><path d="M125,0C56,0,0,56,0,125q0,7.1.8,13.9c6.1,43.7,41.2,70.1,87,53.2v-.1c-5.6,2.1-10.6,3.1-14.8,3.1-4.8,0-8.6-1.3-11.3-4-6.4-6.6-5-20.8,3.9-39c9.1-18.7,24.7-39,42.2-56.2c15.7-16.2,31.1-27.3,45.6-34.2q.1-.1.2-.2q1.3-.6,2.6-1.2q7-3.2,12.8-4.6c42.3-11.9,74.2,13.7,80.2,54.8C242,48.3,189.1,0,125,0Z" transform="translate(1-1.250219)" fill="url(%23edmBubc6DX87-fill)"/></g></g></svg>%0D%0A';var M=class extends m{constructor(){super(...arguments);this.html=()=>`
136
136
 
137
137
  <!-- Styling -->
138
138
  <style>
@@ -143,12 +143,15 @@ You have access to a database of knowledge base items. These include "info" type
143
143
 
144
144
  #root {
145
145
  position: fixed;
146
+ top: 0px;
147
+ right: 0px;
146
148
  width: 32px;
147
149
  height: 32px;
148
150
  z-index: 200001;
149
151
  transition: top 0.5s, left 0.5s, width 0.5s, height 0.5s;
150
152
  animation: web-weaver-logo-hover 5s infinite ease-in-out;
151
153
  cursor: pointer;
154
+ overflow: visible!important;
152
155
  }
153
156
 
154
157
  @keyframes web-weaver-logo-hover {
@@ -160,9 +163,9 @@ You have access to a database of knowledge base items. These include "info" type
160
163
  </style>
161
164
 
162
165
  <!-- Logo -->
163
- <img id='root' src='${this.attr.logo||$}' />
166
+ <img id='root' src='${this.attr.logo||$}' />
164
167
 
165
- `;this._lastLogoSrc=""}onCreate(){this.layoutUpdateTimer=setInterval(()=>this.onUpdate(),100)}onDestroy(){clearInterval(this.layoutUpdateTimer)}onUpdate(){let e=this.child("root");if(!e)return;let t=this.attr.logo||$;this._lastLogoSrc!=t&&(this._lastLogoSrc=t,e.src=t);let i=this.attr.focusID?document.getElementById(this.attr.focusID):null;if(i){let n=i.getBoundingClientRect();if(!n)return;e.style.position="fixed",e.style.width="128px",e.style.height="128px",e.style.left=Math.round(n.x+n.width/2-128/2)+"px",e.style.top=Math.round(n.y+n.height/2-128/2)+"px"}else{let n=this.getBoundingClientRect();if(!n)return;e.style.width=n.width+"px",e.style.height=n.height+"px",e.style.left=n.x+"px",e.style.top=n.y+"px"}}refreshLayout(){setTimeout(()=>this.onUpdate(),100)}};M.observedAttributes=["logo","focusID"];import Ve from"rehype-external-links";import Te from"rehype-format";import ke from"rehype-stringify";import Le from"remark-parse";import Be from"remark-rehype";import{unified as Pe}from"unified";var I=class extends m{constructor(){super(...arguments);this.html=()=>`
168
+ `;this._lastLogoSrc=""}onCreate(){this.layoutUpdateTimer=setInterval(()=>this.onUpdate(),250)}onDestroy(){clearInterval(this.layoutUpdateTimer)}onUpdate(){let e=this.child("root");if(!e)return;let t=this.attr.logo||$;this._lastLogoSrc!=t&&(this._lastLogoSrc=t,e.src=t);let i=window.visualViewport?.offsetTop||0,n=window.visualViewport?.offsetLeft||0,o=this.attr.focusID?document.getElementById(this.attr.focusID):null;if(o){let a=o.getBoundingClientRect();if(!a)return;e.style.position="fixed",e.style.width="128px",e.style.height="128px",e.style.left=Math.round(a.x+a.width/2-128/2+n)+"px",e.style.top=Math.round(a.y+a.height/2-128/2+i)+"px"}else{let a=this.getBoundingClientRect();if(!a)return;e.style.width=a.width+"px",e.style.height=a.height+"px",e.style.left=Math.round(a.x+n)+"px",e.style.top=Math.round(a.y+i)+"px"}}refreshLayout(){setTimeout(()=>this.onUpdate(),100)}};M.observedAttributes=["logo","focusID"];import Ve from"rehype-external-links";import Te from"rehype-format";import ke from"rehype-stringify";import Le from"remark-parse";import Be from"remark-rehype";import{unified as Pe}from"unified";var E=class extends m{constructor(){super(...arguments);this.html=()=>`
166
169
 
167
170
  <!-- Styling -->
168
171
  <style>
@@ -249,7 +252,7 @@ You have access to a database of knowledge base items. These include "info" type
249
252
  <div class='llm-selector-item-icon' style="background-image: url('${t.icon}'); "></div>
250
253
  <div class='llm-selector-item-name'>${t.name}</div>
251
254
  <div class='llm-selector-item-provider'>${t.provider}</div>
252
- `,i.addEventListener("click",n=>{this.dispatchEvent(new CustomEvent("select",{detail:t.id}))}),e.appendChild(i)}}};I.observedAttributes=["open"];var G=new h("ONNXModel"),g=class c{constructor(s){this.stateTensors={};this.constantTensors={};this._runActive=!1;this.ignoreIfBusy=!1;this.session=s,G.debug(`Model input parameters: ${s.inputNames.join(", ")}`),G.debug(`Model output parameters: ${s.outputNames.join(", ")}`)}static isSupported(){return!!c.lib}static async load(s){if(!c.lib)throw new Error("ONNX runtime not loaded, please set the runtime loader. Example: ONNXModel.lib = () => import('onnxruntime-web')");this.onnx||(G.debug("Loading ONNX runtime"),this.onnx=await c.lib()),G.debug(`Loading model: ${s}`);let e=await this.onnx.InferenceSession.create(s);return new c(e)}makeTensor(s,e,t=0){let i=1;for(let a of e)i*=a;let n;if(s=="float32")n=new c.onnx.Tensor(new Float32Array(i),e);else if(s=="int8")n=new c.onnx.Tensor(new Int8Array(i),e);else if(s=="int16")n=new c.onnx.Tensor(new Int16Array(i),e);else if(s=="int32")n=new c.onnx.Tensor(new Int32Array(i),e);else if(s=="int64")n=new c.onnx.Tensor(new BigInt64Array(i),e);else if(s=="uint8")n=new c.onnx.Tensor(new Uint8Array(i),e);else if(s=="uint16")n=new c.onnx.Tensor(new Uint16Array(i),e);else if(s=="uint32")n=new c.onnx.Tensor(new Uint32Array(i),e);else if(s=="uint64")n=new c.onnx.Tensor(new BigUint64Array(i),e);else throw new Error(`Invalid type: ${s}`);return t!==0&&(s=="int64"||s=="uint64")?n.data.fill(BigInt(t)):t!==0&&n.data.fill(t),n}registerConstant(s,e){if(!this.session.inputNames.includes(s))throw new Error(`Model does not have an input named: ${s}`);return this.constantTensors[s]=e,e}makeConstant(s,e,t,i=0){return this.registerConstant(s,this.makeTensor(e,t,i))}registerState(s,e,t){if(e||(e=s),!this.session.inputNames.includes(s))throw new Error(`Model does not have an input named: ${s}`);if(!this.session.outputNames.includes(e))throw new Error(`Model does not have an output named: ${e}`);return this.stateTensors[s]={outputName:e,tensor:t},t}makeState(s,e,t,i,n=0){return this.registerState(s,e,this.makeTensor(t,i,n))}async run(s={}){if(this._runActive&&this.ignoreIfBusy)return G.debug("Ignoring run request because a previous run is still active");if(this._runActive)throw new Error("A previous run is still active");this._runActive=!0;for(let e in this.stateTensors)s[e]=this.stateTensors[e].tensor;for(let e in this.constantTensors)s[e]=this.constantTensors[e];try{let e=await this.session.run(s);for(let t in this.stateTensors){let i=e[this.stateTensors[t].outputName];this.stateTensors[t].tensor=i}return e}finally{this._runActive=!1}}resetState(){G.debug("Resetting state tensors");for(let s in this.stateTensors)this.stateTensors[s].tensor.data.fill(0)}};var me="data:application/javascript;base64,dmFyIGM9Y2xhc3N7Y29uc3RydWN0b3IoaSx0KXt0aGlzLm91dHB1dEJ1ZmZlclNpemU9MDt0aGlzLnBhcnRpYWxCdWZmZXJzPVtdO3RoaXMucGFydGlhbEJ1ZmZlck9mZnNldD0wO2lmKCFpKXRocm93IG5ldyBFcnJvcihgSW52YWxpZCBhcnJheSBjbGFzczogJHtpfWApO2lmKCF0fHx0PD0wKXRocm93IG5ldyBFcnJvcihgSW52YWxpZCBvdXRwdXQgYnVmZmVyIHNpemU6ICR7dH1gKTt0aGlzLkFycmF5Q2xhc3M9aSx0aGlzLm91dHB1dEJ1ZmZlclNpemU9dH1nZXQgcXVldWVkU2l6ZSgpe3JldHVybiB0aGlzLnBhcnRpYWxCdWZmZXJzLnJlZHVjZSgoaSx0KT0+aSt0Lmxlbmd0aCwwKX1mZWVkKGkpe3RoaXMucGFydGlhbEJ1ZmZlcnMucHVzaChpKX1nZXQgY2FuRHJhaW4oKXtyZXR1cm4gdGhpcy5wYXJ0aWFsQnVmZmVycy5yZWR1Y2UoKHQscik9PnQrci5sZW5ndGgsMCktdGhpcy5wYXJ0aWFsQnVmZmVyT2Zmc2V0Pj10aGlzLm91dHB1dEJ1ZmZlclNpemV9ZHJhaW4oKXtpZighdGhpcy5jYW5EcmFpbilyZXR1cm4gbnVsbDtsZXQgaT10aGlzLkFycmF5Q2xhc3MsdD1uZXcgaSh0aGlzLm91dHB1dEJ1ZmZlclNpemUpLHI9MDtmb3IoO3IhPXQubGVuZ3RoOyl7aWYocj50Lmxlbmd0aCl0aHJvdyBuZXcgRXJyb3IoYEJ1ZmZlciBvdmVyZmxvdzogJHtyfSA+ICR7dC5sZW5ndGh9YCk7bGV0IGE9dC5sZW5ndGgtcixzPXRoaX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=class extends AudioWorkletNode{constructor(e,t,i){super(e,"pcm-player-node",{numberOfInputs:0});this.sampleRate=0;this.format="int16";this.isCancelled=!1;if(!t||t<=0)throw new Error(`Invalid sample rate: ${t}`);if(!i||i!="int16"&&i!="float32")throw new Error(`Invalid format: ${i}`);this.sampleRate=t,this.format=i,this.port.onmessage=n=>this.onWorkletMessage(n),this.port.postMessage({action:"start",inputSampleRate:t,outputSampleRate:e.sampleRate,format:i})}feed(e){this.port.postMessage({action:"data",buffer:e.buffer},[e.buffer])}async play(e){if(this.isCancelled)throw new Error("PCMPlayerNode has already been cancelled.");if(this._playPromise)throw new Error("Already playing a stream");this._playPromise=new Promise(t=>this._playPromiseResolve=t),this.dispatchEvent(new CustomEvent("start",{detail:{player:this,stream:e}}));try{let t=e.getReader();for(;!this.isCancelled;){let{done:i,value:n}=await t.read();if(i||!n)break;this.feed(n)}}catch(t){Se.warn("Stream error:",t),this.dispatchEvent(new CustomEvent("error",{detail:{player:this,stream:e,error:t}}))}this.port.postMessage({action:"end"}),await this._playPromise}onWorkletMessage(e){e.data.action=="end"&&(this._playPromiseResolve?.(),this.dispatchEvent(new CustomEvent("end",{detail:{player:this}})))}stop(){this.isCancelled=!0,this.port.postMessage({action:"cancel"}),this._playPromiseResolve?.()}};var z=new h("SpeechOutput"),fe=.5,Me=.8,A=class extends EventTarget{constructor(s){super(),this.ai=s,this.ai.addEventListener("output",e=>this.onTextOutputFromAI(e)),this.ai.audio?.speechRecognition.addEventListener("speechstart",e=>this.interrupt()),this.ai.knowledgeBase.registerSource(()=>[{id:"system.voice",type:"info",name:"Voice active",isContext:!0,disabled:!1,content:"Text-to-speech is active. The user can hear your voice."}])}onTextOutputFromAI(s){s.detail.isChunk||this.ai?.audio?.speechRecognition.isRunning&&this.speak(s.detail.message)}async speak(s){if(!this.ai?.config?.voice?.providerID)return z.warn("No voice provider configured");let e=`speech-${Ie++}`;await this.ai.audio.beginAccess(e);try{await this._speakWithLock(s)}finally{this.ai.audio.endAccess(e)}}async _speakWithLock(s){this.interrupt(),this.ai.audio.speechRecognition.voiceDetection&&(this.ai.audio.speechRecognition.voiceDetection.sensitivity=Me);let e=this.ai?._voiceTracker||z.timer(`${this.ai.config.voice.providerID} voice`);e(`Speak: ${s}`),this.currentPlayerVolume?.disconnect(),this.currentPlayerVolume=this.ai.audio.context.createGain(),this.currentPlayerVolume.connect(this.ai.audio.context.destination);let t=this.currentPlayerVolume,i=new K(this.ai.audio.context,24e3,"int16");this.currentPlayer=i,i.connect(t),i.addEventListener("end",a=>{let r=a;e(`PCM stream ${r.detail.interrupted?"interrupted":"ended"}`),this.currentPlayer==i&&!r.detail.interrupted&&(this.currentPlayer=void 0,this.onSpeechEnd())});let n;if(this.ai.config.voice.providerID=="openai")n=await fetch("https://api.openai.com/v1/audio/speech",{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.ai.config.voice.apiKey}`},body:JSON.stringify({model:"tts-1",input:s,voice:this.ai.config.voice.voiceID,response_format:"pcm"})});else if(this.ai.config.voice.providerID=="elevenlabs")n=await fetch(`https://api.elevenlabs.io/v1/text-to-speech/${this.ai.config.voice.voiceID}?output_format=pcm_24000`,{method:"POST",headers:{"xi-api-key":`${this.ai.config.voice.apiKey}`,"Content-Type":"application/json"},body:JSON.stringify({text:s})});else{z.warn(`Unknown voice provider: ${this.ai.config.voice.providerID}`);return}if(e(`Received response ${n.status} ${n.statusText}`),!n.ok){z.warn(`Failed to generate voice sample: ${n.status} ${n.statusText}`);return}this.dispatchEvent(new CustomEvent("speechstart",{detail:{ai:this.ai,message:s}})),e("Playing PCM stream"),await i.play(n.body),e("Audio has ended")}get isSpeaking(){return!!this.currentPlayer}async interrupt(){if(!this.currentPlayerVolume)return;let s=this.currentPlayerVolume,e=this.currentPlayer;this.currentPlayerVolume=void 0,this.currentPlayer=void 0,this.ai?.audio?.speechRecognition.voiceDetection&&(this.ai.audio.speechRecognition.voiceDetection.sensitivity=fe);let t=400;s.gain.linearRampToValueAtTime(0,t/1e3),await new Promise(i=>setTimeout(i,t+250)),s.disconnect(),e?.stop(),e?.disconnect()}onSpeechEnd(){this.dispatchEvent(new CustomEvent("speechend",{detail:{ai:this.ai}})),this.ai?.audio?.speechRecognition.voiceDetection&&(this.ai.audio.speechRecognition.voiceDetection.sensitivity=fe)}},Ie=1;function N(c,s){let e=s.reduce((l,o)=>l+o.byteLength,0),t=new DataView(new ArrayBuffer(44));t.setUint8(0,82),t.setUint8(1,73),t.setUint8(2,70),t.setUint8(3,70),t.setUint32(4,44+e,!0),t.setUint8(8,87),t.setUint8(9,65),t.setUint8(10,86),t.setUint8(11,69);let i=1,n=32,a=i*n/8,r=c*a;return t.setUint8(12,102),t.setUint8(13,109),t.setUint8(14,116),t.setUint8(15,32),t.setUint32(16,16,!0),t.setUint16(20,3,!0),t.setUint16(22,i,!0),t.setUint32(24,c,!0),t.setUint32(28,r,!0),t.setUint16(32,a,!0),t.setUint16(34,n,!0),t.setUint8(36,100),t.setUint8(37,97),t.setUint8(38,116),t.setUint8(39,97),t.setUint32(40,e,!0),new File([t,...s],"audio.wav",{type:"audio/wav"})}var H=class extends AudioWorkletNode{constructor(e,t,i,n){super(e,"pcm-receiver-node",{numberOfInputs:1});this.format="int16";this.format=i;let a=["int16","int64","float32"];if(!t||t<=0)throw new Error(`Invalid sample rate: ${t}`);if(!a.includes(i))throw new Error(`Invalid format ${i}, must be one of: ${a.join(", ")}`);if(!n||n<=0)throw new Error(`Invalid buffer size: ${n}`);this.port.onmessage=r=>this.onWorkletMessage(r),this.port.postMessage({action:"start",inputSampleRate:e.sampleRate,outputSampleRate:t,format:i,bufferSize:n})}onWorkletMessage(e){if(e.data.action=="data"){let t=null;if(this.format=="int16"&&(t=new Int16Array(e.data.buffer)),this.format=="int64"&&(t=new BigInt64Array(e.data.buffer)),this.format=="float32"&&(t=new Float32Array(e.data.buffer)),!t)throw new Error(`Invalid format: ${this.format}`);this.onData(t),this.dispatchEvent(new CustomEvent("data",{detail:{data:t}}))}}onData(e){}};var q=16e3,D=256,X=8,R=new h("VoiceDetectionNode"),E=class E extends H{constructor(e){super(e,q,"float32",D*X);this.isVoiceActive=!1;this.lastVoiceActiveDate=0;this.voiceEndTimeout=50;this.sensitivity=.5;this.sentivityEnd=.2;this.nextVadReset=0;this.currentProbability=0;this._lastVoiceActive=!1;if(!E.vadModelURL)throw new Error("VAD model url not set, please load it and set it to VoiceDetectionNode.vadModelURL");this.loadModel()}get isVoicePossiblyEnding(){return this.isVoiceActive&&this.currentProbability<this.sensitivity}get sampleRate(){return q}get numberOfSamples(){return D}get numberOfSampleChunks(){return X}get outputBufferSize(){return D*X}get isModelLoaded(){return!!this.vad}async loadModel(){R.debug("Loading VAD model"),this.vad=await g.load(E.vadModelURL),this.vad.ignoreIfBusy=!0,R.debug("Model loaded"),this.vad.makeConstant("sr","int64",[1],q),this.vad.makeState("h","hn","float32",[2,X,64]),this.vad.makeState("c","cn","float32",[2,X,64])}async onData(e){if(this.vad)try{let t=await this.vad.run({input:new g.onnx.Tensor(e,[X,D])});if(!t)return;this.currentProbability=0;for(let a=0;a<t.output.data.length;a++)t.output.data[a]>this.currentProbability&&(this.currentProbability=t.output.data[a]);let i=this.isVoiceActive?this.sentivityEnd:this.sensitivity,n=this.currentProbability>i;if(!n&&this._lastVoiceActive&&(this.nextVadReset=Date.now(),this.vad.resetState()),this._lastVoiceActive=n,n&&!this.isVoiceActive?(this.lastVoiceActiveDate=Date.now(),this.isVoiceActive=!0,this.dispatchEvent(new CustomEvent("speechstart")),this.onSpeechStart(),R.debug("Started speaking")):n?this.lastVoiceActiveDate=Date.now():!n&&this.isVoiceActive&&Date.now()<this.lastVoiceActiveDate+this.voiceEndTimeout||!n&&this.isVoiceActive&&(this.isVoiceActive=!1,this.dispatchEvent(new CustomEvent("speechend")),this.onSpeechEnd(),R.debug("Stopped speaking after timeout")),!n){let a=Date.now();a>this.nextVadReset&&(this.nextVadReset=a+5e3,this.vad.resetState())}}catch(t){R.error("VAD failed:",t)}}onSpeechStart(){}onSpeechEnd(){}};E.vadModelURL="";var C=E;var ye=new h("VoiceChunkOutputNode"),W=class extends C{constructor(){super(...arguments);this.buffers=[];this.recordedBuffers=[];this._voiceRecording=!1;this.backBufferDurationSeconds=3}get bufferDuration(){return this.buffers.reduce((e,t)=>e+t.length,0)/8e3}async onData(e){if(await super.onData(e),this.isVoiceActive&&!this._voiceRecording){ye.debug(`Voice detected, sending ${this.buffers.length} existing chunks`),this._voiceRecording=!0;for(let t of this.buffers)this.recordedBuffers.push(t),this.dispatchEvent(new CustomEvent("voicedata",{detail:{data:t,isFinal:!1}})),this.onVoiceChunk(t);this.recordedBuffers.push(e),this.dispatchEvent(new CustomEvent("voicedata",{detail:{data:e,isFinal:!1}})),this.onVoiceChunk(e),this.buffers=[]}else if(this.isVoiceActive)this.recordedBuffers.push(e),this.dispatchEvent(new CustomEvent("voicedata",{detail:{data:e,isFinal:!1}})),this.onVoiceChunk(e);else if(!this.isVoiceActive&&this._voiceRecording){this.recordedBuffers.push(e),this.dispatchEvent(new CustomEvent("voicedata",{detail:{data:e,isFinal:!0}})),this.onVoiceChunk(e),this._voiceRecording=!1;let t=this.recordedBuffers.reduce((i,n)=>i+n.length,0);ye.debug(`Voice complete, recorded ${(t/8e3).toFixed(2)} seconds of audio, ${t*4/1024} KB of data`),this.dispatchEvent(new CustomEvent("voicedataend",{detail:{data:this.recordedBuffers}})),this.onVoiceEnd(this.recordedBuffers),this.recordedBuffers=[]}else for(this.buffers.push(e);this.bufferDuration>this.backBufferDurationSeconds;)this.buffers.shift()}onVoiceChunk(e){}onVoiceEnd(e){}};var Y=new h("OpenAITranscriptionNode"),J=class extends W{constructor(e,t){super(e);this.apiKey="";this.pendingBuffers=[];this.isTranscribing=!1;this.apiKey=t}async onVoiceEnd(e){let t=Y.timer("OpenAI Transcribe");this.isTranscribing=!0,this.pendingBuffers.push(...e);let i=N(this.sampleRate,this.pendingBuffers);this.lastRequestAbortController?.abort(),this.lastRequestAbortController=new AbortController;let n="";try{let a=new FormData;a.append("file",i),a.append("model","whisper-1"),a.append("response_format","text");let r=await fetch("https://api.openai.com/v1/audio/transcriptions",{method:"POST",headers:{Authorization:`Bearer ${this.apiKey}`},body:a,signal:this.lastRequestAbortController.signal});if(!r.ok)throw new Error(`Failed to generate voice sample: ${r.status} ${r.statusText}`);t("Response received"),n=await r.text(),t("Content received: "+n),this.lastRequestAbortController=void 0}catch(a){Y.error(`Failed to transcribe speech: ${a.message}`);return}finally{this.isTranscribing=!1}if(this.pendingBuffers=[],!n)return Y.debug("Transcription complete, but no text was found");Y.debug(`Transcription: ${n}`),this.onVoiceTranscription(n),this.dispatchEvent(new CustomEvent("transcription",{detail:{text:n}}))}onVoiceTranscription(e){}};var U=class extends WebSocket{constructor(e){super(e);this.pendingData=[];this.addEventListener("open",()=>this._onOpen())}send(e){this.readyState==WebSocket.OPEN?super.send(e):this.pendingData.push(e)}_onOpen(){for(let e of this.pendingData)super.send(e);this.pendingData=[]}};var f=new h("IntelliWeaveTranscriptionNode"),Re="wss://speech.intelliweave.ai/api/v1/transcribe",_=class _ extends W{constructor(e,t){super(e);this.apiAddress=Re;this.apiKey="";this.isTranscribing=!1;this.apiKey=t}async onVoiceChunk(e){this.isTranscribing=!0,this.ws?this.ws.send(e):(f.debug("Opening WebSocket connection"),this.ws=new U(this.apiAddress),this.ws.send(JSON.stringify({type:"hello",sampleRate:16e3,channels:1,format:"float32",apiKey:this.apiKey})),this.ws.send(e.buffer),this.ws.onopen=()=>{f.debug("WebSocket connection opened")},this.ws.addEventListener("message",i=>{let n=JSON.parse(i.data);if(n.error)return f.warn("Error: "+n.error);if(n.type!="transcription")return f.warn("Invalid response type",n);if(n.streaming&&!n.final)return f.debug("Partial transcription: "+n.partialText);if(this.isTranscribing=!1,!n.text.trim())return f.warn(`Empty transcription (${n.processingTime}ms)`);f.debug(`Transcription: ${n.text} (${n.processingTime}ms)`),this.onVoiceTranscription(n.text),this.dispatchEvent(new CustomEvent("transcription",{detail:{text:n.text}}))}),this.ws.addEventListener("close",()=>this.onSocketClose()),this.ws.addEventListener("error",i=>f.warn("WebSocket error")));let t=1e3*60*1;this.shutdownTimer&&clearTimeout(this.shutdownTimer),this.shutdownTimer=setTimeout(()=>{f.debug("Shutting down WebSocket connection"),this.ws?.close(),this.ws=void 0},t)}async onVoiceEnd(e){if(this.ws?.send(JSON.stringify({type:"end"})),_.debugExportWav){let t=N(this.sampleRate,e),i=document.createElement("a");i.href=URL.createObjectURL(t),i.download="recording.wav",i.click()}}onVoiceTranscription(e){}onSocketClose(){f.debug("WebSocket connection closed"),this.ws=void 0,this.isTranscribing=!1}};_.debugExportWav=!1;var O=_;var ee=new h("SpeechRecognition"),j=class extends EventTarget{constructor(e){super();this.isRunning=!1;this._skipEvents=!1;this.maxVolumeHeard=0;this.ai=e,this.ai.knowledgeBase.registerSource(()=>[{id:"system.microphone.enable",type:"action",name:"Enable or disable microphone",isContext:!0,content:`Starts or stops listening to input from the user through the microphone. ${this.isRunning?"Microphone access is currently enabled and you can hear the user.":"Microphone access is currently disabled."}.`,parameters:[{name:"enable",type:"boolean",description:"If true, enables the microphone. If false, disables it."}],action:async t=>{t.enable?await this.start():this.stop()}}])}get isSupported(){if(!g.lib||!this.ai?.vadModel||!b().AudioWorkletNode)return!1;if(this.ai?.config?.transcription?.providerID!="intelliweave"){if(!(this.ai?.config?.transcription?.providerID=="openai"&&this.ai.config?.transcription?.apiKey))return!1}return!0}async start(){if(!this.isSupported)throw new Error("Speech recognition not supported in this persona and browser.");if(!this.isRunning){this.isRunning=!0;try{await this.ai.audio.beginAccess("speech-recognition"),this.micStream=await navigator.mediaDevices.getUserMedia({audio:{channelCount:1,echoCancellation:!0,autoGainControl:!0,noiseSuppression:!0}});let e=this.ai.audio.context.createMediaStreamSource(this.micStream);this.analyserNode=this.ai.audio.context.createAnalyser(),this.analyserNode.fftSize=32,e.connect(this.analyserNode),this.analyserBuffer=new Float32Array(this.analyserNode.fftSize),C.vadModelURL=URL.createObjectURL(this.ai.vadModel),this.ai?.config?.transcription?.providerID=="openai"?(this.voiceDetection=new J(this.ai.audio.context,this.ai.config.transcription.apiKey),e.connect(this.voiceDetection)):(this.voiceDetection=new O(this.ai.audio.context,this.ai.apiKey),this.voiceDetection.apiAddress=this.ai?.config?.transcription?.url||this.voiceDetection.apiAddress,e.connect(this.voiceDetection)),this.voiceDetection.addEventListener("speechstart",t=>{this.ai._voiceTracker=ee.timer("voice interaction","Speech started"),this.dispatchEvent(new CustomEvent(t.type,{detail:t.detail}))}),this.voiceDetection.addEventListener("speechend",t=>{this.ai._voiceTracker?.("Speech ended"),this.dispatchEvent(new CustomEvent(t.type,{detail:t.detail}))}),this.voiceDetection.addEventListener("transcription",t=>{this.ai._voiceTracker?.(`Transcription: ${t.detail.text}`),this.onTranscription(t)}),this._skipEvents||this.dispatchEvent(new CustomEvent("start",{detail:{speechRecognition:this}}))}catch(e){ee.error("Failed to start speech recognition:",e),this.stop()}}}stop(){this.isRunning&&(this.isRunning=!1,this.ai.audio.endAccess("speech-recognition"),this.voiceDetection?.disconnect(),this.voiceDetection=void 0,this.micStream?.getTracks().forEach(e=>e.stop()),this.micStream=void 0,this.analyserNode=void 0,this.analyserBuffer=void 0,this._skipEvents||this.dispatchEvent(new CustomEvent("end",{detail:{speechRecognition:this}})))}get volumeLevel(){if(!this.analyserNode||!this.analyserBuffer)return 0;this.analyserNode.getFloatTimeDomainData(this.analyserBuffer);let e=0;for(let i of this.analyserBuffer)e+=i*i;let t=Math.sqrt(e/this.analyserBuffer.length);return t>this.maxVolumeHeard&&(this.maxVolumeHeard=t),this.maxVolumeHeard*=.999,this.maxVolumeHeard<.01&&(this.maxVolumeHeard=.01),Math.min(1,Math.max(0,t/this.maxVolumeHeard))}get wordsCurrentlyBeingSpoken(){return!!this.voiceDetection?.isVoiceActive}get isTranscribing(){return!!this.voiceDetection?.isTranscribing}onTranscription(e){let t=e.detail.text;ee.debug("Heard:",t),this.dispatchEvent(new CustomEvent("speech",{detail:{transcript:t,isFinal:!0}}))}async reset(){if(this.isRunning){this._skipEvents=!0;try{this.stop(),await this.start(),this._skipEvents=!1}catch(e){throw this._skipEvents=!1,e}}}};var Q=new h("AudioSystem"),S=class{constructor(s){this.locks=[];if(!s)throw new Error("AI reference is required. Please pass in the IntelliWeave instance to the constructor.");this.ai=s,this.ai.audio=this,this.speechRecognition=new j(this.ai),this.speechOutput=new A(this.ai)}static get isSupported(){return!(!g.lib||!b().AudioWorkletNode)}async beginAccess(s){Q.debug(`Began access for: ${s}`),this.locks.includes(s)||this.locks.push(s),!this.context&&(Q.debug("Creating AudioContext"),this.context=new AudioContext({latencyHint:"interactive"}),this.context.resume(),await Promise.all([this.context.audioWorklet.addModule(me),this.context.audioWorklet.addModule(ge)]))}endAccess(s){Q.debug(`Ended access for: ${s}`),this.locks=this.locks.filter(e=>e!=s),!this.locks.length&&(Q.debug("Closing AudioContext"),this.context?.close(),this.context=void 0)}};var bi=new h("Stream");import xi,{createContext as Ee,useContext as Zi,useEffect as Gi,useMemo as Xi,useState as Ci}from"react";var Ei=new h("React");var Vi=Ee(void 0);var te=new h("Embed"),v=class extends m{constructor(){super();this.config={};this.suggestions=[];this.html=()=>`
255
+ `,i.addEventListener("click",n=>{this.dispatchEvent(new CustomEvent("select",{detail:t.id}))}),e.appendChild(i)}}};E.observedAttributes=["open"];var X=new h("ONNXModel"),g=class c{constructor(s){this.stateTensors={};this.constantTensors={};this._runActive=!1;this.ignoreIfBusy=!1;this.session=s,X.debug(`Model input parameters: ${s.inputNames.join(", ")}`),X.debug(`Model output parameters: ${s.outputNames.join(", ")}`)}static isSupported(){return!!c.lib}static async load(s){if(!c.lib)throw new Error("ONNX runtime not loaded, please set the runtime loader. Example: ONNXModel.lib = () => import('onnxruntime-web')");this.onnx||(X.debug("Loading ONNX runtime"),this.onnx=await c.lib()),X.debug(`Loading model: ${s}`);let e=await this.onnx.InferenceSession.create(s);return new c(e)}makeTensor(s,e,t=0){let i=1;for(let o of e)i*=o;let n;if(s=="float32")n=new c.onnx.Tensor(new Float32Array(i),e);else if(s=="int8")n=new c.onnx.Tensor(new Int8Array(i),e);else if(s=="int16")n=new c.onnx.Tensor(new Int16Array(i),e);else if(s=="int32")n=new c.onnx.Tensor(new Int32Array(i),e);else if(s=="int64")n=new c.onnx.Tensor(new BigInt64Array(i),e);else if(s=="uint8")n=new c.onnx.Tensor(new Uint8Array(i),e);else if(s=="uint16")n=new c.onnx.Tensor(new Uint16Array(i),e);else if(s=="uint32")n=new c.onnx.Tensor(new Uint32Array(i),e);else if(s=="uint64")n=new c.onnx.Tensor(new BigUint64Array(i),e);else throw new Error(`Invalid type: ${s}`);return t!==0&&(s=="int64"||s=="uint64")?n.data.fill(BigInt(t)):t!==0&&n.data.fill(t),n}registerConstant(s,e){if(!this.session.inputNames.includes(s))throw new Error(`Model does not have an input named: ${s}`);return this.constantTensors[s]=e,e}makeConstant(s,e,t,i=0){return this.registerConstant(s,this.makeTensor(e,t,i))}registerState(s,e,t){if(e||(e=s),!this.session.inputNames.includes(s))throw new Error(`Model does not have an input named: ${s}`);if(!this.session.outputNames.includes(e))throw new Error(`Model does not have an output named: ${e}`);return this.stateTensors[s]={outputName:e,tensor:t},t}makeState(s,e,t,i,n=0){return this.registerState(s,e,this.makeTensor(t,i,n))}async run(s={}){if(this._runActive&&this.ignoreIfBusy)return X.debug("Ignoring run request because a previous run is still active");if(this._runActive)throw new Error("A previous run is still active");this._runActive=!0;for(let e in this.stateTensors)s[e]=this.stateTensors[e].tensor;for(let e in this.constantTensors)s[e]=this.constantTensors[e];try{let e=await this.session.run(s);for(let t in this.stateTensors){let i=e[this.stateTensors[t].outputName];this.stateTensors[t].tensor=i}return e}finally{this._runActive=!1}}resetState(){X.debug("Resetting state tensors");for(let s in this.stateTensors)this.stateTensors[s].tensor.data.fill(0)}};var me="data:application/javascript;base64,dmFyIGM9Y2xhc3N7Y29uc3RydWN0b3IoaSx0KXt0aGlzLm91dHB1dEJ1ZmZlclNpemU9MDt0aGlzLnBhcnRpYWxCdWZm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XS5sZW5ndGgsdFswXS5sZW5ndGgpO2ZvcihsZXQgcj0wO3I8cztyKyspaVthXVtyXS5zZXQodFswXVtyXSl9aWYoIXRoaXMucmVzYW1wbGVyKXJldHVybiEwO2xldCBoPW5ldyBGbG9hdDMyQXJyYXkoaVswXVswXS5sZW5ndGgpO2ZvcihoLnNldChpWzBdWzBdKSx0aGlzLmlucHV0QnVmZmVyLmZlZWQoaCk7dGhpcy5pbnB1dEJ1ZmZlci5jYW5EcmFpbjspe2xldCBhPXRoaXMuaW5wdXRCdWZmZXIuZHJhaW4oKSxzPXRoaXMucmVzYW1wbGVyLnJlc2FtcGxlKGEpO3RoaXMuZm9ybWF0PT0iaW50MTYiP3RoaXMub3V0cHV0QnVmZmVyLmZlZWQoZChzKSk6dGhpcy5mb3JtYXQ9PSJpbnQ2NCI/dGhpcy5vdXRwdXRCdWZmZXIuZmVlZCh3KHMpKTp0aGlzLmZvcm1hdD09ImZsb2F0MzIiJiZ0aGlzLm91dHB1dEJ1ZmZlci5mZWVkKHMpfWZvcig7dGhpcy5vdXRwdXRCdWZmZXIuY2FuRHJhaW47KXtsZXQgYT10aGlzLm91dHB1dEJ1ZmZlci5kcmFpbigpO2lmKCFhKWJyZWFrO3RoaXMucG9ydC5wb3N0TWVzc2FnZSh7YWN0aW9uOiJkYXRhIixidWZmZXI6YS5idWZmZXJ9LFthLmJ1ZmZlcl0pfXJldHVybiEwfX07cmVnaXN0ZXJQcm9jZXNzb3IoInBjbS1yZWNlaXZlci1ub2RlIix5KTsK";var Se=new h("PCMPlayerNode"),F=class extends AudioWorkletNode{constructor(e,t,i){super(e,"pcm-player-node",{numberOfInputs:0});this.sampleRate=0;this.format="int16";this.isCancelled=!1;if(!t||t<=0)throw new Error(`Invalid sample rate: ${t}`);if(!i||i!="int16"&&i!="float32")throw new Error(`Invalid format: ${i}`);this.sampleRate=t,this.format=i,this.port.onmessage=n=>this.onWorkletMessage(n),this.port.postMessage({action:"start",inputSampleRate:t,outputSampleRate:e.sampleRate,format:i})}feed(e){this.port.postMessage({action:"data",buffer:e.buffer},[e.buffer])}async play(e){if(this.isCancelled)throw new Error("PCMPlayerNode has already been cancelled.");if(this._playPromise)throw new Error("Already playing a stream");this._playPromise=new Promise(t=>this._playPromiseResolve=t),this.dispatchEvent(new CustomEvent("start",{detail:{player:this,stream:e}}));try{let t=e.getReader();for(;!this.isCancelled;){let{done:i,value:n}=await t.read();if(i||!n)break;this.feed(n)}}catch(t){Se.warn("Stream error:",t),this.dispatchEvent(new CustomEvent("error",{detail:{player:this,stream:e,error:t}}))}this.port.postMessage({action:"end"}),await this._playPromise}onWorkletMessage(e){e.data.action=="end"&&(this._playPromiseResolve?.(),this.dispatchEvent(new CustomEvent("end",{detail:{player:this}})))}stop(){this.isCancelled=!0,this.port.postMessage({action:"cancel"}),this._playPromiseResolve?.()}};var z=new h("SpeechOutput"),fe=.5,Me=.8,N=class extends EventTarget{constructor(s){super(),this.ai=s,this.ai.addEventListener("output",e=>this.onTextOutputFromAI(e)),this.ai.audio?.speechRecognition.addEventListener("speechstart",e=>this.interrupt()),this.ai.knowledgeBase.registerSource(()=>[{id:"system.voice",type:"info",name:"Voice active",isContext:!0,disabled:!1,content:"Text-to-speech is active. The user can hear your voice."}])}onTextOutputFromAI(s){s.detail.isChunk||this.ai?.audio?.speechRecognition.isRunning&&this.speak(s.detail.message)}async speak(s){if(!this.ai?.config?.voice?.providerID)return z.warn("No voice provider configured");let e=`speech-${Ee++}`;await this.ai.audio.beginAccess(e);try{await this._speakWithLock(s)}finally{this.ai.audio.endAccess(e)}}async _speakWithLock(s){this.interrupt(),this.ai.audio.speechRecognition.voiceDetection&&(this.ai.audio.speechRecognition.voiceDetection.sensitivity=Me);let e=this.ai?._voiceTracker||z.timer(`${this.ai.config.voice.providerID} voice`);e(`Speak: ${s}`),this.currentPlayerVolume?.disconnect(),this.currentPlayerVolume=this.ai.audio.context.createGain(),this.currentPlayerVolume.connect(this.ai.audio.context.destination);let t=this.currentPlayerVolume,i=new F(this.ai.audio.context,24e3,"int16");this.currentPlayer=i,i.connect(t),i.addEventListener("end",o=>{let a=o;e(`PCM stream ${a.detail.interrupted?"interrupted":"ended"}`),this.currentPlayer==i&&!a.detail.interrupted&&(this.currentPlayer=void 0,this.onSpeechEnd())});let n;if(this.ai.config.voice.providerID=="openai")n=await fetch("https://api.openai.com/v1/audio/speech",{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.ai.config.voice.apiKey}`},body:JSON.stringify({model:"tts-1",input:s,voice:this.ai.config.voice.voiceID,response_format:"pcm"})});else if(this.ai.config.voice.providerID=="elevenlabs")n=await fetch(`https://api.elevenlabs.io/v1/text-to-speech/${this.ai.config.voice.voiceID}?output_format=pcm_24000`,{method:"POST",headers:{"xi-api-key":`${this.ai.config.voice.apiKey}`,"Content-Type":"application/json"},body:JSON.stringify({text:s})});else{z.warn(`Unknown voice provider: ${this.ai.config.voice.providerID}`);return}if(e(`Received response ${n.status} ${n.statusText}`),!n.ok){z.warn(`Failed to generate voice sample: ${n.status} ${n.statusText}`);return}this.dispatchEvent(new CustomEvent("speechstart",{detail:{ai:this.ai,message:s}})),e("Playing PCM stream"),await i.play(n.body),e("Audio has ended")}get isSpeaking(){return!!this.currentPlayer}async interrupt(){if(!this.currentPlayerVolume)return;let s=this.currentPlayerVolume,e=this.currentPlayer;this.currentPlayerVolume=void 0,this.currentPlayer=void 0,this.ai?.audio?.speechRecognition.voiceDetection&&(this.ai.audio.speechRecognition.voiceDetection.sensitivity=fe);let t=400;s.gain.linearRampToValueAtTime(0,t/1e3),await new Promise(i=>setTimeout(i,t+250)),s.disconnect(),e?.stop(),e?.disconnect()}onSpeechEnd(){this.dispatchEvent(new CustomEvent("speechend",{detail:{ai:this.ai}})),this.ai?.audio?.speechRecognition.voiceDetection&&(this.ai.audio.speechRecognition.voiceDetection.sensitivity=fe)}},Ee=1;function A(c,s){let e=s.reduce((l,r)=>l+r.byteLength,0),t=new DataView(new ArrayBuffer(44));t.setUint8(0,82),t.setUint8(1,73),t.setUint8(2,70),t.setUint8(3,70),t.setUint32(4,44+e,!0),t.setUint8(8,87),t.setUint8(9,65),t.setUint8(10,86),t.setUint8(11,69);let i=1,n=32,o=i*n/8,a=c*o;return t.setUint8(12,102),t.setUint8(13,109),t.setUint8(14,116),t.setUint8(15,32),t.setUint32(16,16,!0),t.setUint16(20,3,!0),t.setUint16(22,i,!0),t.setUint32(24,c,!0),t.setUint32(28,a,!0),t.setUint16(32,o,!0),t.setUint16(34,n,!0),t.setUint8(36,100),t.setUint8(37,97),t.setUint8(38,116),t.setUint8(39,97),t.setUint32(40,e,!0),new File([t,...s],"audio.wav",{type:"audio/wav"})}var D=class extends AudioWorkletNode{constructor(e,t,i,n){super(e,"pcm-receiver-node",{numberOfInputs:1});this.format="int16";this.format=i;let o=["int16","int64","float32"];if(!t||t<=0)throw new Error(`Invalid sample rate: ${t}`);if(!o.includes(i))throw new Error(`Invalid format ${i}, must be one of: ${o.join(", ")}`);if(!n||n<=0)throw new Error(`Invalid buffer size: ${n}`);this.port.onmessage=a=>this.onWorkletMessage(a),this.port.postMessage({action:"start",inputSampleRate:e.sampleRate,outputSampleRate:t,format:i,bufferSize:n})}onWorkletMessage(e){if(e.data.action=="data"){let t=null;if(this.format=="int16"&&(t=new Int16Array(e.data.buffer)),this.format=="int64"&&(t=new BigInt64Array(e.data.buffer)),this.format=="float32"&&(t=new Float32Array(e.data.buffer)),!t)throw new Error(`Invalid format: ${this.format}`);this.onData(t),this.dispatchEvent(new CustomEvent("data",{detail:{data:t}}))}}onData(e){}};var q=16e3,H=256,G=8,R=new h("VoiceDetectionNode"),C=class C extends D{constructor(e){super(e,q,"float32",H*G);this.isVoiceActive=!1;this.lastVoiceActiveDate=0;this.voiceEndTimeout=50;this.sensitivity=.5;this.sentivityEnd=.2;this.nextVadReset=0;this.currentProbability=0;this._lastVoiceActive=!1;if(!C.vadModelURL)throw new Error("VAD model url not set, please load it and set it to VoiceDetectionNode.vadModelURL");this.loadModel()}get isVoicePossiblyEnding(){return this.isVoiceActive&&this.currentProbability<this.sensitivity}get sampleRate(){return q}get numberOfSamples(){return H}get numberOfSampleChunks(){return G}get outputBufferSize(){return H*G}get isModelLoaded(){return!!this.vad}async loadModel(){R.debug("Loading VAD model"),this.vad=await g.load(C.vadModelURL),this.vad.ignoreIfBusy=!0,R.debug("Model loaded"),this.vad.makeConstant("sr","int64",[1],q),this.vad.makeState("h","hn","float32",[2,G,64]),this.vad.makeState("c","cn","float32",[2,G,64])}async onData(e){if(this.vad)try{let t=await this.vad.run({input:new g.onnx.Tensor(e,[G,H])});if(!t)return;this.currentProbability=0;for(let o=0;o<t.output.data.length;o++)t.output.data[o]>this.currentProbability&&(this.currentProbability=t.output.data[o]);let i=this.isVoiceActive?this.sentivityEnd:this.sensitivity,n=this.currentProbability>i;if(!n&&this._lastVoiceActive&&(this.nextVadReset=Date.now(),this.vad.resetState()),this._lastVoiceActive=n,n&&!this.isVoiceActive?(this.lastVoiceActiveDate=Date.now(),this.isVoiceActive=!0,this.dispatchEvent(new CustomEvent("speechstart")),this.onSpeechStart(),R.debug("Started speaking")):n?this.lastVoiceActiveDate=Date.now():!n&&this.isVoiceActive&&Date.now()<this.lastVoiceActiveDate+this.voiceEndTimeout||!n&&this.isVoiceActive&&(this.isVoiceActive=!1,this.dispatchEvent(new CustomEvent("speechend")),this.onSpeechEnd(),R.debug("Stopped speaking after timeout")),!n){let o=Date.now();o>this.nextVadReset&&(this.nextVadReset=o+5e3,this.vad.resetState())}}catch(t){R.error("VAD failed:",t)}}onSpeechStart(){}onSpeechEnd(){}};C.vadModelURL="";var W=C;var ye=new h("VoiceChunkOutputNode"),I=class extends W{constructor(){super(...arguments);this.buffers=[];this.recordedBuffers=[];this._voiceRecording=!1;this.backBufferDurationSeconds=3}get bufferDuration(){return this.buffers.reduce((e,t)=>e+t.length,0)/8e3}async onData(e){if(await super.onData(e),this.isVoiceActive&&!this._voiceRecording){ye.debug(`Voice detected, sending ${this.buffers.length} existing chunks`),this._voiceRecording=!0;for(let t of this.buffers)this.recordedBuffers.push(t),this.dispatchEvent(new CustomEvent("voicedata",{detail:{data:t,isFinal:!1}})),this.onVoiceChunk(t);this.recordedBuffers.push(e),this.dispatchEvent(new CustomEvent("voicedata",{detail:{data:e,isFinal:!1}})),this.onVoiceChunk(e),this.buffers=[]}else if(this.isVoiceActive)this.recordedBuffers.push(e),this.dispatchEvent(new CustomEvent("voicedata",{detail:{data:e,isFinal:!1}})),this.onVoiceChunk(e);else if(!this.isVoiceActive&&this._voiceRecording){this.recordedBuffers.push(e),this.dispatchEvent(new CustomEvent("voicedata",{detail:{data:e,isFinal:!0}})),this.onVoiceChunk(e),this._voiceRecording=!1;let t=this.recordedBuffers.reduce((i,n)=>i+n.length,0);ye.debug(`Voice complete, recorded ${(t/8e3).toFixed(2)} seconds of audio, ${t*4/1024} KB of data`),this.dispatchEvent(new CustomEvent("voicedataend",{detail:{data:this.recordedBuffers}})),this.onVoiceEnd(this.recordedBuffers),this.recordedBuffers=[]}else for(this.buffers.push(e);this.bufferDuration>this.backBufferDurationSeconds;)this.buffers.shift()}onVoiceChunk(e){}onVoiceEnd(e){}};var Y=new h("OpenAITranscriptionNode"),J=class extends I{constructor(e,t){super(e);this.apiKey="";this.pendingBuffers=[];this.isTranscribing=!1;this.apiKey=t}async onVoiceEnd(e){let t=Y.timer("OpenAI Transcribe");this.isTranscribing=!0,this.pendingBuffers.push(...e);let i=A(this.sampleRate,this.pendingBuffers);this.lastRequestAbortController?.abort(),this.lastRequestAbortController=new AbortController;let n="";try{let o=new FormData;o.append("file",i),o.append("model","whisper-1"),o.append("response_format","text");let a=await fetch("https://api.openai.com/v1/audio/transcriptions",{method:"POST",headers:{Authorization:`Bearer ${this.apiKey}`},body:o,signal:this.lastRequestAbortController.signal});if(!a.ok)throw new Error(`Failed to generate voice sample: ${a.status} ${a.statusText}`);t("Response received"),n=await a.text(),t("Content received: "+n),this.lastRequestAbortController=void 0}catch(o){Y.error(`Failed to transcribe speech: ${o.message}`);return}finally{this.isTranscribing=!1}if(this.pendingBuffers=[],!n)return Y.debug("Transcription complete, but no text was found");Y.debug(`Transcription: ${n}`),this.onVoiceTranscription(n),this.dispatchEvent(new CustomEvent("transcription",{detail:{text:n}}))}onVoiceTranscription(e){}};var U=class extends WebSocket{constructor(e){super(e);this.pendingData=[];this.addEventListener("open",()=>this._onOpen())}send(e){this.readyState==WebSocket.OPEN?super.send(e):this.pendingData.push(e)}_onOpen(){for(let e of this.pendingData)super.send(e);this.pendingData=[]}};var f=new h("IntelliWeaveTranscriptionNode"),Re="wss://speech.intelliweave.ai/api/v1/transcribe",j=class j extends I{constructor(e,t){super(e);this.apiAddress=Re;this.apiKey="";this.isTranscribing=!1;this.apiKey=t}async onVoiceChunk(e){this.isTranscribing=!0,this.ws?this.ws.send(e):(f.debug("Opening WebSocket connection"),this.ws=new U(this.apiAddress),this.ws.send(JSON.stringify({type:"hello",sampleRate:16e3,channels:1,format:"float32",apiKey:this.apiKey})),this.ws.send(e.buffer),this.ws.onopen=()=>{f.debug("WebSocket connection opened")},this.ws.addEventListener("message",i=>{let n=JSON.parse(i.data);if(n.error)return f.warn("Error: "+n.error);if(n.type!="transcription")return f.warn("Invalid response type",n);if(n.streaming&&!n.final)return f.debug("Partial transcription: "+n.partialText);if(this.isTranscribing=!1,!n.text.trim())return f.warn(`Empty transcription (${n.processingTime}ms)`);f.debug(`Transcription: ${n.text} (${n.processingTime}ms)`),this.onVoiceTranscription(n.text),this.dispatchEvent(new CustomEvent("transcription",{detail:{text:n.text}}))}),this.ws.addEventListener("close",()=>this.onSocketClose()),this.ws.addEventListener("error",i=>f.warn("WebSocket error")));let t=1e3*60*1;this.shutdownTimer&&clearTimeout(this.shutdownTimer),this.shutdownTimer=setTimeout(()=>{f.debug("Shutting down WebSocket connection"),this.ws?.close(),this.ws=void 0},t)}async onVoiceEnd(e){if(this.ws?.send(JSON.stringify({type:"end"})),j.debugExportWav){let t=A(this.sampleRate,e),i=document.createElement("a");i.href=URL.createObjectURL(t),i.download="recording.wav",i.click()}}onVoiceTranscription(e){}onSocketClose(){f.debug("WebSocket connection closed"),this.ws=void 0,this.isTranscribing=!1}};j.debugExportWav=!1;var O=j;var ee=new h("SpeechRecognition"),Q=class extends EventTarget{constructor(e){super();this.isRunning=!1;this._skipEvents=!1;this.maxVolumeHeard=0;this.ai=e,this.ai.knowledgeBase.registerSource(()=>[{id:"system.microphone.enable",type:"action",name:"Enable or disable microphone",isContext:!0,content:`Starts or stops listening to input from the user through the microphone. ${this.isRunning?"Microphone access is currently enabled and you can hear the user.":"Microphone access is currently disabled."}.`,parameters:[{name:"enable",type:"boolean",description:"If true, enables the microphone. If false, disables it."}],action:async t=>{t.enable?await this.start():this.stop()}}])}get isSupported(){if(!g.lib||!this.ai?.vadModel||!b().AudioWorkletNode)return!1;if(this.ai?.config?.transcription?.providerID!="intelliweave"){if(!(this.ai?.config?.transcription?.providerID=="openai"&&this.ai.config?.transcription?.apiKey))return!1}return!0}async start(){if(!this.isSupported)throw new Error("Speech recognition not supported in this persona and browser.");if(!this.isRunning){this.isRunning=!0;try{await this.ai.audio.beginAccess("speech-recognition"),this.micStream=await navigator.mediaDevices.getUserMedia({audio:{channelCount:1,echoCancellation:!0,autoGainControl:!0,noiseSuppression:!0}});let e=this.ai.audio.context.createMediaStreamSource(this.micStream);this.analyserNode=this.ai.audio.context.createAnalyser(),this.analyserNode.fftSize=32,e.connect(this.analyserNode),this.analyserBuffer=new Float32Array(this.analyserNode.fftSize),W.vadModelURL=URL.createObjectURL(this.ai.vadModel),this.ai?.config?.transcription?.providerID=="openai"?(this.voiceDetection=new J(this.ai.audio.context,this.ai.config.transcription.apiKey),e.connect(this.voiceDetection)):(this.voiceDetection=new O(this.ai.audio.context,this.ai.apiKey),this.voiceDetection.apiAddress=this.ai?.config?.transcription?.url||this.voiceDetection.apiAddress,e.connect(this.voiceDetection)),this.voiceDetection.addEventListener("speechstart",t=>{this.ai._voiceTracker=ee.timer("voice interaction","Speech started"),this.dispatchEvent(new CustomEvent(t.type,{detail:t.detail}))}),this.voiceDetection.addEventListener("speechend",t=>{this.ai._voiceTracker?.("Speech ended"),this.dispatchEvent(new CustomEvent(t.type,{detail:t.detail}))}),this.voiceDetection.addEventListener("transcription",t=>{this.ai._voiceTracker?.(`Transcription: ${t.detail.text}`),this.onTranscription(t)}),this._skipEvents||this.dispatchEvent(new CustomEvent("start",{detail:{speechRecognition:this}}))}catch(e){ee.error("Failed to start speech recognition:",e),this.stop()}}}stop(){this.isRunning&&(this.isRunning=!1,this.ai.audio.endAccess("speech-recognition"),this.voiceDetection?.disconnect(),this.voiceDetection=void 0,this.micStream?.getTracks().forEach(e=>e.stop()),this.micStream=void 0,this.analyserNode=void 0,this.analyserBuffer=void 0,this._skipEvents||this.dispatchEvent(new CustomEvent("end",{detail:{speechRecognition:this}})))}get volumeLevel(){if(!this.analyserNode||!this.analyserBuffer)return 0;this.analyserNode.getFloatTimeDomainData(this.analyserBuffer);let e=0;for(let i of this.analyserBuffer)e+=i*i;let t=Math.sqrt(e/this.analyserBuffer.length);return t>this.maxVolumeHeard&&(this.maxVolumeHeard=t),this.maxVolumeHeard*=.999,this.maxVolumeHeard<.01&&(this.maxVolumeHeard=.01),Math.min(1,Math.max(0,t/this.maxVolumeHeard))}get wordsCurrentlyBeingSpoken(){return!!this.voiceDetection?.isVoiceActive}get isTranscribing(){return!!this.voiceDetection?.isTranscribing}onTranscription(e){let t=e.detail.text;ee.debug("Heard:",t),this.dispatchEvent(new CustomEvent("speech",{detail:{transcript:t,isFinal:!0}}))}async reset(){if(this.isRunning){this._skipEvents=!0;try{this.stop(),await this.start(),this._skipEvents=!1}catch(e){throw this._skipEvents=!1,e}}}};var _=new h("AudioSystem"),S=class{constructor(s){this.locks=[];if(!s)throw new Error("AI reference is required. Please pass in the IntelliWeave instance to the constructor.");this.ai=s,this.ai.audio=this,this.speechRecognition=new Q(this.ai),this.speechOutput=new N(this.ai)}static get isSupported(){return!(!g.lib||!b().AudioWorkletNode)}async beginAccess(s){_.debug(`Began access for: ${s}`),this.locks.includes(s)||this.locks.push(s),!this.context&&(_.debug("Creating AudioContext"),this.context=new AudioContext({latencyHint:"interactive"}),this.context.resume(),await Promise.all([this.context.audioWorklet.addModule(me),this.context.audioWorklet.addModule(ge)]))}endAccess(s){_.debug(`Ended access for: ${s}`),this.locks=this.locks.filter(e=>e!=s),!this.locks.length&&(_.debug("Closing AudioContext"),this.context?.close(),this.context=void 0)}};var bi=new h("Stream");import xi,{createContext as Ce,useContext as Zi,useEffect as Xi,useMemo as Gi,useState as Wi}from"react";var Ci=new h("React");var Vi=Ce(void 0);var te=new h("Embed"),w=class extends m{constructor(){super();this.config={};this.suggestions=[];this.html=()=>`
253
256
 
254
257
  <!-- Styling -->
255
258
  <style>
@@ -350,6 +353,27 @@ You have access to a database of knowledge base items. These include "info" type
350
353
  text-decoration: none;
351
354
  }
352
355
 
356
+ .circle {
357
+ width: 64px;
358
+ height: 64px;
359
+ border-radius: 50%;
360
+ box-shadow: 0px 0px 1px 1px rgba(255, 255, 255, 0.8);
361
+ position: fixed;
362
+ }
363
+
364
+ .pulse {
365
+ animation: pulse-animation 2s infinite;
366
+ }
367
+
368
+ @keyframes pulse-animation {
369
+ 0% {
370
+ box-shadow: 0 0 0 0px rgba(255, 255, 255, 0.2);
371
+ }
372
+ 100% {
373
+ box-shadow: 0 0 0 20px rgba(255, 255, 255, 0);
374
+ }
375
+ }
376
+
353
377
  </style>
354
378
 
355
379
  <!-- Embed font -->
@@ -360,6 +384,7 @@ You have access to a database of knowledge base items. These include "info" type
360
384
 
361
385
  <!-- Logo -->
362
386
  ${M.add({id:"web-weaver-logo",logo:this.attr.logo})}
387
+ <div id='pulseanim' class="circle pulse"></div>
363
388
 
364
389
  <!-- Inner container contains all the UI when the panel is open -->
365
390
  <div id='web-weaver-embed-inner'>
@@ -367,11 +392,11 @@ You have access to a database of knowledge base items. These include "info" type
367
392
  </div>
368
393
 
369
394
  <!-- LLM selector panel -->
370
- ${I.add({id:"llm-selector-panel"})}
395
+ ${E.add({id:"llm-selector-panel"})}
371
396
 
372
397
  <!-- Interaction panel -->
373
- ${F.add({id:"interaction-bar"})}
398
+ ${K.add({id:"interaction-bar"})}
374
399
 
375
400
  </div>
376
401
 
377
- `;this._isProcessing=!1;this.ai=new Z,S.isSupported&&(this.ai.audio=new S(this.ai))}onCreate(){u().embed&&te.warn("Only one <web-weaver-embed> element should be on the page."),u().embed=this,this.child("root").addEventListener("click",i=>this.onContainerClick(i)),this.child("web-weaver-logo").addEventListener("click",i=>this.onLogoClick(i)),this.child("interaction-bar").addEventListener("input-message",i=>this.processInput(i.detail)),this.child("interaction-bar").addEventListener("llm-button-click",i=>this.state.llmPanelOpen=!this.state.llmPanelOpen),this.child("llm-selector-panel").addEventListener("select",i=>this.onLLMModelSelect(i)),this.child("interaction-bar").connectAI(this.ai),this.ai.onAIMessage=this.onAIMessage.bind(this),this.ai.onAIToolStart=this.onAIToolStart.bind(this),this.ai.knowledgeBase.addEntry({id:"ui.focusElement",type:"action",name:"Focus on an element",content:"Focuses and draws the user's attention to an HTML element on the page.",isContext:!0,parameters:[{name:"elementID",type:"string",description:"The HTML ID of the element to focus."}],action:i=>{let n=i.elementID||"";if(n.startsWith("#")&&(n=n.substring(1)),!n)throw new Error("No element ID specified.");let a=document.getElementById(n);if(!a)throw new Error(`Could not find the element with ID "${n}".`);a.scrollIntoView({behavior:"smooth",block:"center",inline:"center"}),this.attr.focusID=n,this.ai.submitAnalyticsEvent({type:"focus-element",elementID:n})}}),this.ai.knowledgeBase.addEntry({id:"ui.suggestResponse",type:"action",name:"Suggest a response",content:"Add a button with a suggested response for the user's next message. Supports multiple calls at once.",isContext:!0,parameters:[{name:"text",type:"string",description:"The suggested response for the user's next message."}],action:i=>(this.suggestions.push(i.text),this.ai.submitAnalyticsEvent({type:"suggest-response",text:i.text}),"Suggestion button added")});let t=this.attr.apiKey||u().apiKey;if(!t)return te.warn("No API key specified, some features may be unavailable.");this.state.loading=!0,this.ai.load(t).then(i=>{this.config=i,u().introductionMessage=this.config.introductionMessage,u().analytics=this.config.analytics!==void 0?!!this.config.analytics:u().analytics,this.state.loading=!1,this.resetConversation()})}onUpdate(){this.child("web-weaver-embed-inner").style.display=this.attr.open?"":"none",this.child("interaction-bar").style.display=this.attr.open?"":"none",this.child("root").className=this.attr.open?"open":"",this.child("llm-selector-panel").attr.open=this.state.llmPanelOpen,this.child("root").style.right=this.attr.offsetX?this.attr.offsetX+"px":"20px",this.child("root").style.bottom=this.attr.offsetY?this.attr.offsetY+"px":"20px",this.child("interaction-bar").attr.loading=this.state.loading,this.child("interaction-bar").attr.llmName=this.ai.currentModel?.metadata?.name||this.ai.currentModel?.id||"None",this.child("interaction-bar").attr.llmButtonVisible=this.ai.models.length>1;let e=this.child("web-weaver-logo");e.attr.focusID=this.attr.focusID,e.refreshLayout(),this.child("llm-selector-panel").state.items=this.ai.models.sort((t,i)=>(i.priority||0)-(t.priority||0)),this.child("llm-selector-panel").state.selectedID=this.ai.currentModel?.id}onDestroy(){u().embed==this&&(u().embed=null)}onContainerClick(e){this.attr.open||(e.preventDefault(),this.attr.open=!0)}onLogoClick(e){e.preventDefault(),e.stopPropagation(),this.attr.focusID?this.attr.focusID="":this.attr.open=!this.attr.open}open(){this.attr.open=!0}close(){this.attr.open=!1}resetConversation(){let e=this.child("web-weaver-embed-inner");for(this.state.llmPanelOpen=!1;e.children.length>0;)e.children[0].remove();this.ai.resetConversation(),this.suggestions=[];let t=document.createElement("div");t.className="introduction-message",t.innerHTML=u().introductionMessage||`Welcome to <b>${document.title||"Web Weaver"}</b>. How can I help you?`,e.appendChild(t),this.ai.insertAssistantMessage(t.innerText);for(let i of u().introductionSuggestions||[]){let n=document.createElement("div");n.className="suggestion-button",n.innerText=i,n.addEventListener("click",a=>this.onSuggestionClick(a,i)),e.appendChild(n)}}async processInput(e){if(this._isProcessing)return;this._isProcessing=!0,this.state.loading=!0,this.state.llmPanelOpen=!1;let t=this.child("web-weaver-embed-inner");this.suggestions=[];try{for(let n of Array.from(t.querySelectorAll(".suggestion-button")))n.remove();this.attr.focusID="";let i=document.createElement("div");i.className="input",i.innerText=e,t.appendChild(i),t.scrollBy({top:1e5,behavior:"smooth"}),await this.ai.sendMessage(e);for(let n of this.suggestions){let a=document.createElement("div");a.className="suggestion-button",a.innerText=n,a.addEventListener("click",r=>this.onSuggestionClick(r,n)),t.appendChild(a)}t.scrollBy({top:1e5,behavior:"smooth"})}catch(i){te.error("Failed to process input:",i);let n=document.createElement("div");n.className="output",n.innerText="Sorry, there was a problem getting a response. "+i.message,t.appendChild(n);let a=document.createElement("div");a.className="suggestion-button",a.innerText="Reset conversation",a.addEventListener("click",r=>this.resetConversation()),t.appendChild(a)}this.state.loading=!1,this._isProcessing=!1}async onAIMessage(e){let t=await Pe().use(Le).use(Be).use(Ve,{target:"_blank",rel:["noopener","noreferrer"]}).use(Te).use(ke).processSync(e),i=this.child("web-weaver-embed-inner"),n=i.lastElementChild;if(n&&n.classList.contains("output"))n.innerHTML=t.toString("utf-8");else{let a=document.createElement("div");a.className="output",a.innerHTML=t.toString("utf-8"),i.appendChild(a)}for(let a of Array.from(i.querySelectorAll(".tool")))a.remove();i.scrollBy({top:1e6,behavior:"smooth"})}onAIToolStart(e,t){let i=`Running: ${e}`;e=="search"&&(i=`Searching: ${t?.query}`),e=="ui.focusElement"&&(i=`Focusing: #${t?.elementID}`),e=="ui.suggestResponse"&&(i=`Suggested response: ${t?.text}`);let n=this.child("web-weaver-embed-inner"),a=document.createElement("div");a.className="tool",a.innerText=i,n.appendChild(a),n.scrollBy({top:1e5,behavior:"smooth"})}onSuggestionClick(e,t){this.processInput(t)}onLLMModelSelect(e){e.preventDefault(),this.ai.setModel(e.detail),this.state.llmPanelOpen=!1}};v.tagName="intelliweave-embed",v.observedAttributes=["logo","focusID","open"];var vs=v;g.lib=()=>import("onnxruntime-web");v.register();export{vs as default};
402
+ `;this._isProcessing=!1;this.ai=new Z,S.isSupported&&(this.ai.audio=new S(this.ai))}onCreate(){u().embed&&te.warn("Only one <web-weaver-embed> element should be on the page."),u().embed=this,this.child("root").addEventListener("click",i=>this.onContainerClick(i)),this.child("web-weaver-logo").addEventListener("click",i=>this.onLogoClick(i)),this.child("interaction-bar").addEventListener("input-message",i=>this.processInput(i.detail)),this.child("interaction-bar").addEventListener("llm-button-click",i=>this.state.llmPanelOpen=!this.state.llmPanelOpen),this.child("llm-selector-panel").addEventListener("select",i=>this.onLLMModelSelect(i)),this.child("interaction-bar").connectAI(this.ai),this.ai.onAIMessage=this.onAIMessage.bind(this),this.ai.onAIToolStart=this.onAIToolStart.bind(this),this.ai.knowledgeBase.addEntry({id:"ui.focusElement",type:"action",name:"Focus on an element",content:"Focuses and draws the user's attention to an HTML element on the page.",isContext:!0,parameters:[{name:"elementID",type:"string",description:"The HTML ID of the element to focus."}],action:i=>{let n=i.elementID||"";if(n.startsWith("#")&&(n=n.substring(1)),!n)throw new Error("No element ID specified.");let o=document.getElementById(n);if(!o)throw new Error(`Could not find the element with ID "${n}".`);o.scrollIntoView({behavior:"smooth",block:"center",inline:"center"}),this.attr.focusID=n,this.ai.submitAnalyticsEvent({type:"focus-element",elementID:n})}}),this.ai.knowledgeBase.addEntry({id:"ui.suggestResponse",type:"action",name:"Suggest a response",content:"Add a button with a suggested response for the user's next message. Supports multiple calls at once.",isContext:!0,parameters:[{name:"text",type:"string",description:"The suggested response for the user's next message."}],action:i=>(this.suggestions.push(i.text),this.ai.submitAnalyticsEvent({type:"suggest-response",text:i.text}),"Suggestion button added")});let t=this.attr.apiKey||u().apiKey;if(!t)return te.warn("No API key specified, some features may be unavailable.");this.state.loading=!0,this.ai.load(t).then(i=>{this.config=i,u().introductionMessage=this.config.introductionMessage,u().analytics=this.config.analytics!==void 0?!!this.config.analytics:u().analytics,this.state.loading=!1,this.resetConversation()})}onUpdate(){this.child("web-weaver-embed-inner").style.display=this.attr.open?"":"none",this.child("interaction-bar").style.display=this.attr.open?"":"none",this.child("root").className=this.attr.open?"open":"",this.child("llm-selector-panel").attr.open=this.state.llmPanelOpen,this.child("pulseanim").style.display=this.attr.open?"none":"",this.child("root").style.right=this.attr.offsetX?this.attr.offsetX+"px":"20px",this.child("root").style.bottom=this.attr.offsetY?this.attr.offsetY+"px":"20px",this.child("interaction-bar").attr.loading=this.state.loading,this.child("interaction-bar").attr.llmName=this.ai.currentModel?.metadata?.name||this.ai.currentModel?.id||"None",this.child("interaction-bar").attr.llmButtonVisible=this.ai.models.length>1;let e=this.child("web-weaver-logo");e.attr.focusID=this.attr.focusID,e.refreshLayout(),this.child("llm-selector-panel").state.items=this.ai.models.sort((t,i)=>(i.priority||0)-(t.priority||0)),this.child("llm-selector-panel").state.selectedID=this.ai.currentModel?.id}onDestroy(){u().embed==this&&(u().embed=null)}onContainerClick(e){this.attr.open||(e.preventDefault(),this.attr.open=!0)}onLogoClick(e){e.preventDefault(),e.stopPropagation(),this.attr.focusID?this.attr.focusID="":this.attr.open=!this.attr.open}open(){this.attr.open=!0}close(){this.attr.open=!1}resetConversation(){let e=this.child("web-weaver-embed-inner");for(this.state.llmPanelOpen=!1;e.children.length>0;)e.children[0].remove();this.ai.resetConversation(),this.suggestions=[];let t=document.createElement("div");t.className="introduction-message",t.innerHTML=u().introductionMessage||`Welcome to <b>${document.title||"Web Weaver"}</b>. How can I help you?`,e.appendChild(t),this.ai.insertAssistantMessage(t.innerText);for(let i of u().introductionSuggestions||[]){let n=document.createElement("div");n.className="suggestion-button",n.innerText=i,n.addEventListener("click",o=>this.onSuggestionClick(o,i)),e.appendChild(n)}}async processInput(e){if(this._isProcessing)return;this._isProcessing=!0,this.state.loading=!0,this.state.llmPanelOpen=!1;let t=this.child("web-weaver-embed-inner");this.suggestions=[];try{for(let n of Array.from(t.querySelectorAll(".suggestion-button")))n.remove();this.attr.focusID="";let i=document.createElement("div");i.className="input",i.innerText=e,t.appendChild(i),t.lastElementChild?.scrollIntoView({behavior:"instant",block:"end"}),await this.ai.sendMessage(e);for(let n of this.suggestions){let o=document.createElement("div");o.className="suggestion-button",o.innerText=n,o.addEventListener("click",a=>this.onSuggestionClick(a,n)),t.appendChild(o)}t.lastElementChild?.scrollIntoView({behavior:"instant",block:"end"})}catch(i){te.error("Failed to process input:",i);let n=document.createElement("div");n.className="output",n.innerText="Sorry, there was a problem getting a response. "+i.message,t.appendChild(n);let o=document.createElement("div");o.className="suggestion-button",o.innerText="Reset conversation",o.addEventListener("click",a=>this.resetConversation()),t.appendChild(o)}this.state.loading=!1,this._isProcessing=!1}async onAIMessage(e){let t=await Pe().use(Le).use(Be).use(Ve,{target:"_blank",rel:["noopener","noreferrer"]}).use(Te).use(ke).processSync(e),i=this.child("web-weaver-embed-inner"),n=i.lastElementChild;if(n&&n.classList.contains("output"))n.innerHTML=t.toString("utf-8");else{let o=document.createElement("div");o.className="output",o.innerHTML=t.toString("utf-8"),i.appendChild(o)}for(let o of Array.from(i.querySelectorAll(".tool")))o.remove();i.lastElementChild?.scrollIntoView({behavior:"instant",block:"end"})}onAIToolStart(e,t){let i=`Running: ${e}`;e=="search"&&(i=`Searching: ${t?.query}`),e=="ui.focusElement"&&(i=`Focusing: #${t?.elementID}`),e=="ui.suggestResponse"&&(i=`Suggested response: ${t?.text}`);let n=this.child("web-weaver-embed-inner"),o=document.createElement("div");o.className="tool",o.innerText=i,n.appendChild(o),n.lastElementChild?.scrollIntoView({behavior:"instant",block:"end"})}onSuggestionClick(e,t){this.processInput(t)}onLLMModelSelect(e){e.preventDefault(),this.ai.setModel(e.detail),this.state.llmPanelOpen=!1}};w.tagName="intelliweave-embed",w.observedAttributes=["logo","focusID","open"];var ws=w;g.lib=()=>import("onnxruntime-web");w.register();export{ws as default};
Binary file