@intelliweave/embedded 1.8.61 → 1.8.62
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.
|
@@ -9,7 +9,7 @@ import{v4 as me}from"uuid";function re(d){let i=(d||"").split(`
|
|
|
9
9
|
`,yield*e()}function ge(){if(m().userID)return m().userID||"";if(typeof localStorage<"u"){let d=localStorage.getItem("intelliweave.uid")||"";return d||(d=me(),localStorage.setItem("intelliweave.uid",d),d)}else return me()}function fe(d){let i=d;if(i?.type=="object"&&i.properties)return d;let e={type:"object",properties:{},required:[]};if(d&&Array.isArray(d)){let t=d;for(let s of t)e.properties[s.name]={type:s.type||"string",description:s.description||""}}return e}var N=class N{constructor(i){this.module="IntelliWeave";this.module=i}get debugEnabled(){return N.debug?!0:typeof window<"u"&&m().debug}log(...i){this.debugEnabled&&console.log(`[IntelliWeave > ${this.module}]`,...i)}debug(...i){this.debugEnabled&&console.debug(`[IntelliWeave > ${this.module}]`,...i)}info(...i){this.debugEnabled&&console.info(`[IntelliWeave > ${this.module}]`,...i)}warn(...i){console.warn(`[IntelliWeave > ${this.module}]`,...i)}error(...i){console.error(`[IntelliWeave > ${this.module}]`,...i)}timer(i,...e){let t=Date.now();return this.debug(`[${i} 0ms] Started`,...e),(...s)=>this.debug(`[${i} ${Math.floor(Date.now()-t)}ms]`,...s)}};N.debug=!1;var h=N;import{countTokens as A}from"gpt-tokenizer";import{v4 as Ie}from"uuid";var K=class{constructor(){this.size=4096;this.groups=[]}createGroup(i){let e=new le;return e.id=i,this.groups.push(e),e}group(i){return this.groups.find(e=>e.id===i)}countTokens(){return this.groups.reduce((i,e)=>i+e.tokenCount,0)}removeOverflow(){let i=this.countTokens(),e=this.groups.reduce((t,s)=>t+s.weight,0);for(;i>this.size;){let t=this.groups.slice().sort((n,o)=>{let a=Math.floor(n.weight/e*this.size),c=Math.floor(o.weight/e*this.size),r=n.tokenCount-a;return o.tokenCount-c-r}),s=this.removeOneItem(t);if(!s)throw new Error("Too many items in the token window that cannot be removed.");i-=s.tokenCount}}removeOneItem(i){for(let e of i){let t=e.items.findIndex(n=>!n.cannotRemove);if(t===-1)continue;let s=e.items[t];return e.items.splice(t,1),e.tokenCount-=s.tokenCount,s}return null}},le=class{constructor(){this.id="";this.items=[];this.weight=1;this.tokenCount=0;this.separator=`
|
|
10
10
|
`;this.itemPadding=0;this.sortFunction=(i,e)=>i.sortOrder!==e.sortOrder?i.sortOrder-e.sortOrder:i.dateAdded!==e.dateAdded?i.dateAdded-e.dateAdded:i.content.localeCompare(e.content)}setItemPadding(i){return this.itemPadding=i,this.recalculateTokens(),this}sortBy(i){return this.sortFunction=i,this.items.sort(this.sortFunction),this}setSeparator(i){return this.separator==i?this:(this.separator=i,this.recalculateTokens(),this)}setWeight(i){return this.weight=i,this}recalculateTokens(){this.tokenCount=this.items.reduce((i,e)=>(e.tokenCount=A(e.content)+A(this.separator)+this.itemPadding,i+e.tokenCount),0)}add(i){typeof i=="string"&&(i={content:i});let e=i;e.id===void 0&&(e.id=Ie());let t=this.items.find(s=>s.id===e.id);return t?(this.tokenCount-=t.tokenCount,Object.assign(t,e),e=t):this.items.push(e),e.dateAdded===void 0&&(e.dateAdded=Date.now()),e.sortOrder===void 0&&(e.sortOrder=0),e.disabled===void 0&&(e.disabled=!1),e.tokenCount=e.disabled?0:A(i.content)+A(this.separator)+this.itemPadding,this.tokenCount+=e.tokenCount,this.items.sort(this.sortFunction),e}getAllAsString(){return this.getAll().map(i=>i.content).join(this.separator)}getAll(){return this.items.filter(i=>!i.disabled)}empty(){this.items=[],this.tokenCount=0}};var S=1,v=new h("ChatGPT"),F=class{constructor(i){this.id="";this.metadata={};this.config={apiKey:"",endpoint:"",model:"gpt-4-turbo",systemMessage:"",userID:"",stream:!0,maxTokens:4096};this.maxToolCallsPerMessage=10;this.stats={tokensUsed:0};this.tokenWindow=(()=>{let i=new K;return i.createGroup("context").setSeparator(`
|
|
11
11
|
|
|
12
|
-
`),i.createGroup("actions").setItemPadding(25),i.createGroup("messages").setItemPadding(10),i})();this.config={...this.config,...i},this.tokenWindow.size=this.config.maxTokens||4096}get contextGroup(){return this.tokenWindow.group("context")}get toolGroup(){return this.tokenWindow.group("actions")}get messageGroup(){return this.tokenWindow.group("messages")}async sendMessage(i){let e={role:"user",content:i};this.messageGroup.add({id:"msg-"+S++,content:JSON.stringify(e),customData:e}),await this.processMessages();let s=this.messageGroup.items[this.messageGroup.items.length-1]?.customData;return s?.role=="assistant"&&s.content||""}addAssistantMessage(i){let e={role:"assistant",content:i};this.messageGroup.add({id:"msg-"+S++,content:JSON.stringify(e),customData:e})}addUserMessage(i){let e={role:"user",content:i};this.messageGroup.add({id:"msg-"+S++,content:JSON.stringify(e),customData:e})}getMessages(){return this.messageGroup.items.map(i=>i.customData)}getLatestMessage(){return this.messageGroup.items.length?this.messageGroup.items[this.messageGroup.items.length-1]?.customData:void 0}async processMessages(){await this.config.onBeforeMessageProcessing?.(),v.debugEnabled&&v.debug("Process message state:",{context:this.contextGroup.getAllAsString(),tools:this.toolGroup.getAll().map(t=>t.customData),messages:this.messageGroup.items.map(t=>t.customData),tokens:{used:this.tokenWindow.countTokens(),max:this.tokenWindow.size}});let e=this.messageGroup.items[this.messageGroup.items.length-1]?.customData;if(e?.role=="user"||e?.role=="tool")await this.sendToAPI(),await this.processMessages();else if(e?.role=="assistant"&&e?.tool_calls?.length){for(let t of e.tool_calls)await this.processToolCall(t);await this.processMessages()}}async trimMessages(){for(this.tokenWindow.removeOverflow();this.messageGroup.items.length&&this.messageGroup.items[0].customData?.role=="tool";)this.messageGroup.items.shift()}async sendToAPI(i){this.config.systemMessage&&this.contextGroup.add({id:"_gpt_context",cannotRemove:!0,sortOrder:0,content:this.config.systemMessage}),await this.trimMessages();let e=0;for(let c=this.messageGroup.items.length-1;c>=0;c--){let r=this.messageGroup.items[c].customData;if(r.role=="assistant"&&r.tool_calls){if(e+=1,e>=this.maxToolCallsPerMessage)throw new Error(`Exceeded the maximum tool calls per message limit of ${this.maxToolCallsPerMessage}.`)}else if(r.role=="user")break}v.debugEnabled&&v.debug("Before LLM state:",{context:this.contextGroup.getAllAsString(),tools:this.toolGroup.getAll().map(c=>c.customData),messages:this.messageGroup.items.map(c=>c.customData),tokens:{used:this.tokenWindow.countTokens(),max:this.tokenWindow.size}});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.contextGroup.getAllAsString()},...this.messageGroup.getAll().map(c=>c.customData)]};for(let c of this.toolGroup.getAll()){let r=c.customData;r.description&&r.description.length>1024&&v.warn(`Tool description for "${r.name}" is too long, it will be truncated.`);let l={type:"function",function:{name:r.name,description:(r.description||"").substring(0,1024),parameters:r.params}};t.tools.push(l)}t.user||delete t.user,t.tools.length||delete t.tools;let s={};if(s["Content-Type"]="application/json",this.config.apiKey&&(s.Authorization=`Bearer ${this.config.apiKey}`),i)return t;let n=await fetch(this.config.endpoint||"https://api.openai.com/v1/chat/completions",{method:"POST",headers:s,body:JSON.stringify(t)});if(!n.ok){let c=`${n.status} ${n.statusText}`;try{let l=await n.json();c=l.errorText||l.error?.message||l.error||c}catch{}let r=new Error(c);throw r.code=n.status,r}let o=null,a=null;if(this.config.stream)for await(let c of pe(n.body)){if(c.data=="[DONE]")break;if(!c.data)continue;let r=JSON.parse(c.data);if(a){for(let l in r.choices[0].delta)if(typeof r.choices[0].delta[l]=="string"){if(l=="role"&&a[l]==r.choices[0].delta[l])continue;a[l]=(a[l]||"")+r.choices[0].delta[l]}for(let l of r.choices[0].delta.tool_calls||[]){if(a.tool_calls||(a.tool_calls=[]),!a.tool_calls[l.index]){a.tool_calls[l.index]=l;continue}let u=a.tool_calls[l.index];u.function=u.function||{},u.function.name=(u.function.name||"")+(l.function?.name||""),u.function.arguments=(u.function.arguments||"")+(l.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,v.debug(`Used ${o.usage?.total_tokens||0} tokens, total used: ${this.stats.tokensUsed}`),a=o.choices?.[0]?.message;if(a||(v.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.messageGroup.add({id:"msg-"+S++,content:JSON.stringify(a),customData:a}),a.content&&this.config.onAIMessage?.(a.content,!1)}processIncomingMessage(i){}registerTool(i){this.toolGroup.add({id:i.name,cannotRemove:!i.canRemove,content:JSON.stringify(i),customData:i})}async processToolCall(i){try{let t=this.toolGroup.items.find(a=>a.id==i.function.name)?.customData;if(!t)throw new Error(`Tool "${i.function.name}" not found.`);let s=JSON.parse(i.function.arguments);this.config.onAIToolStart?.(i.function.name,s);let n=await t.callback(s);typeof n!="string"&&(n=JSON.stringify(n)||""),(n===""||n==="undefined")&&(n="success");let o={role:"tool",content:n,tool_call_id:i.id};this.messageGroup.add({id:"msg-"+S++,content:JSON.stringify(o),customData:o})}catch(e){v.warn(`Unable to process tool call for "${i?.function?.name}"`,e);let t={role:"tool",content:`Error: ${e.message}`,tool_call_id:i.id};this.messageGroup.add({id:"msg-"+S++,content:JSON.stringify(t),customData:t})}}resetConversation(){this.messageGroup.empty(),this.toolGroup.empty(),this.contextGroup.empty()}};import{v4 as J}from"uuid";import Ve from"minisearch";var ye=[{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(d,i)=>{let e=await i.knowledgeBase.search(d.query),t=i;t._lastKBsearch=d.query,t._nextRequestUseKBitems=e,i.submitAnalyticsEvent({type:"kb-search",query:d.query,results:e});let s=e.filter(o=>o.type!="action").map(o=>"id="+o.id).join(", ")||"(none)",n=e.filter(o=>o.type=="action").map(o=>"name="+o._functionID).join(", ")||"(none)";return`Search complete, context has been updated. New info entries found: ${s}. New tools available: ${n}.`}}];import{Client as be}from"@modelcontextprotocol/sdk/client/index.js";import{StreamableHTTPClientTransport as Xe}from"@modelcontextprotocol/sdk/client/streamableHttp.js";import{SSEClientTransport as ke}from"@modelcontextprotocol/sdk/client/sse.js";var G={name:"@intelliweave/embedded",version:"1.8.61",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 && IW_TESTER_API_KEY=97150cfc-5b97-413e-8da3-e86d01f70057 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:{"@modelcontextprotocol/sdk":"^1.12.1","@types/json-schema":"^7.0.15","gpt-tokenizer":"^2.9.0",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","utility-types":"^3.11.0",uuid:"^10.0.0"}};import{LoggingMessageNotificationSchema as Me,ToolListChangedNotificationSchema as Re}from"@modelcontextprotocol/sdk/types.js";var p=new h("MCPKnowledgeClient"),D=class{constructor(i){this.tools=[];this.iwActions=[];this.stats={toolsCalled:0};this.lastSearchQuery="";this.lastSearchResults=[];this.config=i}async connect(){return this.client?this.client:this.connectionPromise?await this.connectionPromise:(this.connectionPromise=this.connectInternal(),this.connectionPromise.finally(()=>{this.connectionPromise=void 0}),await this.connectionPromise)}async connectInternal(){p.debug("Connecting to MCP client");let i=this.config.connect?await this.config.connect():await Promise.resolve().then(async()=>{let e=new be({name:G.name,version:G.version}),t=new Xe(new URL(this.config.baseURL));return await e.connect(t),p.debug("Connected with HTTP streaming mode"),e}).catch(async e=>{let t=new be({name:G.name,version:G.version}),s=new ke(new URL(this.config.baseURL));return await t.connect(s),p.debug("Connected with SSE mode"),t});return await this.disconnect(),this.client=i,i.onerror=e=>{p.error(`MCP client error: ${e.message}`)},i.onclose=()=>{p.debug("MCP client connection closed"),this.client=void 0},i.setNotificationHandler(Me,e=>{e.params.level=="critical"?p.error(`[Server] ${e.params.data}`):e.params.level=="emergency"?p.error(`[Server] ${e.params.data}`):e.params.level=="error"?p.error(`[Server] ${e.params.data}`):e.params.level=="warning"?p.warn(`[Server] ${e.params.data}`):e.params.level=="info"?p.info(`[Server] ${e.params.data}`):e.params.level=="debug"?p.debug(`[Server] ${e.params.data}`):p.log(`[Server] ${e.params.data}`)}),i.setNotificationHandler(Re,e=>{p.debug("Tool list changed",e),this.fetchTools()}),p.debug("Fetching tools from MCP server..."),await this.fetchTools(),i}async disconnect(){await this.client?.close(),this.client=void 0,this.tools=[],this.iwActions=[]}async fetchTools(){let i=[],e;for(;;){let s=await this.client.listTools({cursor:e});e=s.nextCursor;for(let n of s.tools||[])i.push(n);if(!s?.tools?.length||!e)break}let t=[];for(let s of i){let n=!!(this.config.searchToolName&&s.name==this.config.searchToolName&&!this.config.searchToolVisible);t.push({id:s.name,name:s.name,content:s.description||"",type:"action",isContext:!0,parameters:s.inputSchema,action:o=>this.performToolCall(s.name,o),disabled:n})}p.debug(`Fetched ${i.length} tools from MCP server.`),this.tools=i,this.iwActions=t}async search(i){if(i==this.lastSearchQuery)return this.lastSearchResults;await this.connect();let e=this.iwActions.slice(),t=await this.performSearchCall(i);return e=e.concat(t),this.lastSearchQuery=i,this.lastSearchResults=e,p.debug(`Search completed, found ${e.length} items.`),e}async performSearchCall(i){let e=[];if(!this.config.searchToolName)return e;let t=this.tools.find(n=>n.name==this.config.searchToolName);if(!t)return p.warn(`Search function ${this.config.searchToolName} not found on the MCP server.`),e;let s;if(t.inputSchema.required?.length==1){let n=t.inputSchema.required[0];s=await this.performToolCall(this.config.searchToolName,{[n]:i})}else if(t.inputSchema.properties&&Object.keys(t.inputSchema.properties).length==1){let n=Object.keys(t.inputSchema.properties)[0];s=await this.performToolCall(this.config.searchToolName,{[n]:i})}else s=await this.performToolCall(this.config.searchToolName,{});if(Array.isArray(s))for(let n=0;n<s.length;n++){let o=s[n];e.push({id:this.config.id+":"+this.config.searchToolName+":"+n,name:`Search result ${n+1} in ${this.config.searchToolName}`,type:"info",content:typeof o=="string"?o:JSON.stringify(o)})}else e.push({id:this.config.id+":"+this.config.searchToolName+":result",name:`Search result in ${this.config.searchToolName}`,type:"info",content:typeof s=="string"?s:JSON.stringify(s)});return e}async performToolCall(i,e){await this.connect(),p.debug(`Performing tool call for ${i} with params:`,e),this.stats.toolsCalled+=1;let t=await this.client.callTool({name:i,arguments:e});if(t.isError){let s=t.content?.[0]?.text||"Unknown error";throw p.error(`Error calling tool ${i}: ${s}`),new Error(`Error calling tool ${i}: ${s}`)}if(Array.isArray(t.content)&&t.content.length==1&&t.content[0].type=="text")try{return JSON.parse(t.content[0].text)}catch{}return t.content||[]}};var Z=new h("KnowledgeBase"),ce=1,z=class d{constructor(i){this._sources=[{id:"core.internal",query:async()=>ye}];this._windowSources=[];this.lastResults=[];this.manualEntries=[];this.ai=i}registerSource(i,e){let t=i;return typeof i=="function"&&(e=i,t=`source.${ce++}`),this._sources.push({id:t,query:e}),t}removeSource(i){this._sources=this.sources.filter(e=>e.id!==i&&e.query!==i)}addEntry(i){this.manualEntries.push(i)}removeEntry(i){this.manualEntries=this.manualEntries.filter(e=>e.id!==i)}get sources(){let i=this._sources;return m().knowledgeBaseSources&&(i=i.concat(m().knowledgeBaseSources)),i=i.concat(this._windowSources),i=i.filter(e=>!e.disabled),i}async search(i){Z.debug(`Searching knowledge base for: ${i}`);let e=new Event("webweaver_kb_search",{bubbles:!0,cancelable:!0});e.query=i,e.entries=[],e.sources=[],typeof document<"u"&&document.dispatchEvent(e),this._windowSources=e.sources;let s=(await Promise.all(this.sources.map(async c=>{try{let r=Date.now(),l=await c.query(i);return Z.debug(`Source '${c.id}' took ${Date.now()-r}ms`),l||[]}catch(r){return Z.warn(`Knowledge source '${c.id}' failed:`,r),[]}}))).flat();s=s.concat(e.entries),s=s.concat(this.manualEntries),m().knowledgeBase&&(s=s.concat(m().knowledgeBase)),s=s.filter(c=>c&&!c.disabled);for(let c=0;c<s.length;c++){let r=s[c];r.id=r.id||`temp.${c}`,r._functionID=r.id.replaceAll(/[^a-zA-Z0-9_]/g,"_"),!r.action&&r.type=="tour"&&(r.action=l=>{throw new Error("This tour does not have an action. You must perform the tour content manually.")}),!r.action&&r.type=="info"&&(r.action=l=>{throw new Error("This item does not have an action. Use the content to provide information to the user instead.")})}let n=new Ve({fields:["id","type","name","content","tags"],storeFields:[],searchOptions:{boost:{name:3,tags:2},fuzzy:.2}});n.addAll(s);let a=n.search(i).map(c=>s.find(r=>r.id==c.id));for(let c of s)c.isContext&&(a.find(r=>r.id===c.id)||a.push(c));return this.lastResults=a,Z.debug("Found results:",a),a}getCachedEntry(i){return this.lastResults.find(e=>e.id==i||e._functionID==i)}registerSourceFromURL(i,e){e||(e=`external.${ce++}`),Z.debug(`Registering remote knowledge base source: ${i}`);let t=[],s=[],n=!0,o=async(c,r)=>{Z.debug(`Calling remote knowledge base action: ${c.id}`);let l={type:"action",userID:this.ai.userID,extra:this.ai.extra,actionID:c.id,parameters:r},u=await fetch(i,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(l)});if(!u.ok)throw new Error(`HTTP Error ${u.status} ${u.statusText}`);let M=await u.json();return a(M.updateItems||[]),M.response},a=c=>{for(let r of c){if(!r.id){Z.warn("KB item skipped since it has no ID.",r);continue}let l=t.find(u=>u.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:u=>o(r,u)})}};this.registerSource(e,async c=>{if(n&&s.includes(c))return t;let r={type:"search",userID:this.ai?.userID||"",extra:this.ai?.extra||{},query:c},l=await fetch(i,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(r)});if(!l.ok)throw new Error(`HTTP Error ${l.status} ${l.statusText}`);let u=await l.json();return n=!u.noCache,s.includes(c)||s.push(c),a(u.items),t})}clone(){let i=new d(this.ai);return i._sources=this._sources,i._windowSources=this._windowSources,i.manualEntries=this.manualEntries,i}registerMCPSource(i){i.id||(i.id=`external.${ce++}`);let e=new D(i);return this.registerSource(i.id,t=>e.search(t)),e}};var Y=class{constructor(i){this.ai=i}async boolean(i){let e=await this.instruct({...i,instruction:`${i.instruction}
|
|
12
|
+
`),i.createGroup("actions").setItemPadding(25),i.createGroup("messages").setItemPadding(10),i})();this.config={...this.config,...i},this.tokenWindow.size=this.config.maxTokens||4096}get contextGroup(){return this.tokenWindow.group("context")}get toolGroup(){return this.tokenWindow.group("actions")}get messageGroup(){return this.tokenWindow.group("messages")}async sendMessage(i){let e={role:"user",content:i};this.messageGroup.add({id:"msg-"+S++,content:JSON.stringify(e),customData:e}),await this.processMessages();let s=this.messageGroup.items[this.messageGroup.items.length-1]?.customData;return s?.role=="assistant"&&s.content||""}addAssistantMessage(i){let e={role:"assistant",content:i};this.messageGroup.add({id:"msg-"+S++,content:JSON.stringify(e),customData:e})}addUserMessage(i){let e={role:"user",content:i};this.messageGroup.add({id:"msg-"+S++,content:JSON.stringify(e),customData:e})}getMessages(){return this.messageGroup.items.map(i=>i.customData)}getLatestMessage(){return this.messageGroup.items.length?this.messageGroup.items[this.messageGroup.items.length-1]?.customData:void 0}async processMessages(){await this.config.onBeforeMessageProcessing?.(),v.debugEnabled&&v.debug("Process message state:",{context:this.contextGroup.getAllAsString(),tools:this.toolGroup.getAll().map(t=>t.customData),messages:this.messageGroup.items.map(t=>t.customData),tokens:{used:this.tokenWindow.countTokens(),max:this.tokenWindow.size}});let e=this.messageGroup.items[this.messageGroup.items.length-1]?.customData;if(e?.role=="user"||e?.role=="tool")await this.sendToAPI(),await this.processMessages();else if(e?.role=="assistant"&&e?.tool_calls?.length){for(let t of e.tool_calls)await this.processToolCall(t);await this.processMessages()}}async trimMessages(){for(this.tokenWindow.removeOverflow();this.messageGroup.items.length&&this.messageGroup.items[0].customData?.role=="tool";)this.messageGroup.items.shift()}async sendToAPI(i){this.config.systemMessage&&this.contextGroup.add({id:"_gpt_context",cannotRemove:!0,sortOrder:0,content:this.config.systemMessage}),await this.trimMessages();let e=0;for(let c=this.messageGroup.items.length-1;c>=0;c--){let r=this.messageGroup.items[c].customData;if(r.role=="assistant"&&r.tool_calls){if(e+=1,e>=this.maxToolCallsPerMessage)throw new Error(`Exceeded the maximum tool calls per message limit of ${this.maxToolCallsPerMessage}.`)}else if(r.role=="user")break}v.debugEnabled&&v.debug("Before LLM state:",{context:this.contextGroup.getAllAsString(),tools:this.toolGroup.getAll().map(c=>c.customData),messages:this.messageGroup.items.map(c=>c.customData),tokens:{used:this.tokenWindow.countTokens(),max:this.tokenWindow.size}});let t={model:this.config.model,temperature:.2,user:this.config.userID,tools:[],stream:!!this.config.stream,messages:[{role:"system",content:this.contextGroup.getAllAsString()},...this.messageGroup.getAll().map(c=>c.customData)]};for(let c of this.toolGroup.getAll()){let r=c.customData;r.description&&r.description.length>1024&&v.warn(`Tool description for "${r.name}" is too long, it will be truncated.`);let l={type:"function",function:{name:r.name,description:(r.description||"").substring(0,1024),parameters:r.params}};t.tools.push(l)}t.user||delete t.user,t.tools.length||delete t.tools;let s={};if(s["Content-Type"]="application/json",this.config.apiKey&&(s.Authorization=`Bearer ${this.config.apiKey}`),i)return t;let n=await fetch(this.config.endpoint||"https://api.openai.com/v1/chat/completions",{method:"POST",headers:s,body:JSON.stringify(t)});if(!n.ok){let c=`${n.status} ${n.statusText}`;try{let l=await n.json();c=l.errorText||l.error?.message||l.error||c}catch{}let r=new Error(c);throw r.code=n.status,r}let o=null,a=null;if(this.config.stream)for await(let c of pe(n.body)){if(c.data=="[DONE]")break;if(!c.data)continue;let r=JSON.parse(c.data);if(a){for(let l in r.choices[0].delta)if(typeof r.choices[0].delta[l]=="string"){if(l=="role"&&a[l]==r.choices[0].delta[l])continue;a[l]=(a[l]||"")+r.choices[0].delta[l]}for(let l of r.choices[0].delta.tool_calls||[]){if(a.tool_calls||(a.tool_calls=[]),!a.tool_calls[l.index]){a.tool_calls[l.index]=l;continue}let u=a.tool_calls[l.index];u.function=u.function||{},u.function.name=(u.function.name||"")+(l.function?.name||""),u.function.arguments=(u.function.arguments||"")+(l.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,v.debug(`Used ${o.usage?.total_tokens||0} tokens, total used: ${this.stats.tokensUsed}`),a=o.choices?.[0]?.message;if(a||(v.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.messageGroup.add({id:"msg-"+S++,content:JSON.stringify(a),customData:a}),a.content&&this.config.onAIMessage?.(a.content,!1)}processIncomingMessage(i){}registerTool(i){this.toolGroup.add({id:i.name,cannotRemove:!i.canRemove,content:JSON.stringify(i),customData:i})}async processToolCall(i){try{let t=this.toolGroup.items.find(a=>a.id==i.function.name)?.customData;if(!t)throw new Error(`Tool "${i.function.name}" not found.`);let s=JSON.parse(i.function.arguments);this.config.onAIToolStart?.(i.function.name,s);let n=await t.callback(s);typeof n!="string"&&(n=JSON.stringify(n)||""),(n===""||n==="undefined")&&(n="success");let o={role:"tool",content:n,tool_call_id:i.id};this.messageGroup.add({id:"msg-"+S++,content:JSON.stringify(o),customData:o})}catch(e){v.warn(`Unable to process tool call for "${i?.function?.name}"`,e);let t={role:"tool",content:`Error: ${e.message}`,tool_call_id:i.id};this.messageGroup.add({id:"msg-"+S++,content:JSON.stringify(t),customData:t})}}resetConversation(){this.messageGroup.empty(),this.toolGroup.empty(),this.contextGroup.empty()}};import{v4 as J}from"uuid";import Ve from"minisearch";var ye=[{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(d,i)=>{let e=await i.knowledgeBase.search(d.query),t=i;t._lastKBsearch=d.query,t._nextRequestUseKBitems=e,i.submitAnalyticsEvent({type:"kb-search",query:d.query,results:e});let s=e.filter(o=>o.type!="action").map(o=>"id="+o.id).join(", ")||"(none)",n=e.filter(o=>o.type=="action").map(o=>"name="+o._functionID).join(", ")||"(none)";return`Search complete, context has been updated. New info entries found: ${s}. New tools available: ${n}.`}}];import{Client as be}from"@modelcontextprotocol/sdk/client/index.js";import{StreamableHTTPClientTransport as Xe}from"@modelcontextprotocol/sdk/client/streamableHttp.js";import{SSEClientTransport as ke}from"@modelcontextprotocol/sdk/client/sse.js";var G={name:"@intelliweave/embedded",version:"1.8.62",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 && IW_TESTER_API_KEY=97150cfc-5b97-413e-8da3-e86d01f70057 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:{"@modelcontextprotocol/sdk":"^1.12.1","@types/json-schema":"^7.0.15","gpt-tokenizer":"^2.9.0",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","utility-types":"^3.11.0",uuid:"^10.0.0"}};import{LoggingMessageNotificationSchema as Me,ToolListChangedNotificationSchema as Re}from"@modelcontextprotocol/sdk/types.js";var p=new h("MCPKnowledgeClient"),D=class{constructor(i){this.tools=[];this.iwActions=[];this.stats={toolsCalled:0};this.lastSearchQuery="";this.lastSearchResults=[];this.config=i}async connect(){return this.client?this.client:this.connectionPromise?await this.connectionPromise:(this.connectionPromise=this.connectInternal(),this.connectionPromise.finally(()=>{this.connectionPromise=void 0}),await this.connectionPromise)}async connectInternal(){p.debug("Connecting to MCP client");let i=this.config.connect?await this.config.connect():await Promise.resolve().then(async()=>{let e=new be({name:G.name,version:G.version}),t=new Xe(new URL(this.config.baseURL));return await e.connect(t),p.debug("Connected with HTTP streaming mode"),e}).catch(async e=>{let t=new be({name:G.name,version:G.version}),s=new ke(new URL(this.config.baseURL));return await t.connect(s),p.debug("Connected with SSE mode"),t});return await this.disconnect(),this.client=i,i.onerror=e=>{p.error(`MCP client error: ${e.message}`)},i.onclose=()=>{p.debug("MCP client connection closed"),this.client=void 0},i.setNotificationHandler(Me,e=>{e.params.level=="critical"?p.error(`[Server] ${e.params.data}`):e.params.level=="emergency"?p.error(`[Server] ${e.params.data}`):e.params.level=="error"?p.error(`[Server] ${e.params.data}`):e.params.level=="warning"?p.warn(`[Server] ${e.params.data}`):e.params.level=="info"?p.info(`[Server] ${e.params.data}`):e.params.level=="debug"?p.debug(`[Server] ${e.params.data}`):p.log(`[Server] ${e.params.data}`)}),i.setNotificationHandler(Re,e=>{p.debug("Tool list changed",e),this.fetchTools()}),p.debug("Fetching tools from MCP server..."),await this.fetchTools(),i}async disconnect(){await this.client?.close(),this.client=void 0,this.tools=[],this.iwActions=[]}async fetchTools(){let i=[],e;for(;;){let s=await this.client.listTools({cursor:e});e=s.nextCursor;for(let n of s.tools||[])i.push(n);if(!s?.tools?.length||!e)break}let t=[];for(let s of i){let n=!!(this.config.searchToolName&&s.name==this.config.searchToolName&&!this.config.searchToolVisible);t.push({id:s.name,name:s.name,content:s.description||"",type:"action",isContext:!0,parameters:s.inputSchema,action:o=>this.performToolCall(s.name,o),disabled:n})}p.debug(`Fetched ${i.length} tools from MCP server.`),this.tools=i,this.iwActions=t}async search(i){if(i==this.lastSearchQuery)return this.lastSearchResults;await this.connect();let e=this.iwActions.slice(),t=await this.performSearchCall(i);return e=e.concat(t),this.lastSearchQuery=i,this.lastSearchResults=e,p.debug(`Search completed, found ${e.length} items.`),e}async performSearchCall(i){let e=[];if(!this.config.searchToolName)return e;let t=this.tools.find(n=>n.name==this.config.searchToolName);if(!t)return p.warn(`Search function ${this.config.searchToolName} not found on the MCP server.`),e;let s;if(t.inputSchema.required?.length==1){let n=t.inputSchema.required[0];s=await this.performToolCall(this.config.searchToolName,{[n]:i})}else if(t.inputSchema.properties&&Object.keys(t.inputSchema.properties).length==1){let n=Object.keys(t.inputSchema.properties)[0];s=await this.performToolCall(this.config.searchToolName,{[n]:i})}else s=await this.performToolCall(this.config.searchToolName,{});if(Array.isArray(s))for(let n=0;n<s.length;n++){let o=s[n];e.push({id:this.config.id+":"+this.config.searchToolName+":"+n,name:`Search result ${n+1} in ${this.config.searchToolName}`,type:"info",content:typeof o=="string"?o:JSON.stringify(o)})}else e.push({id:this.config.id+":"+this.config.searchToolName+":result",name:`Search result in ${this.config.searchToolName}`,type:"info",content:typeof s=="string"?s:JSON.stringify(s)});return e}async performToolCall(i,e){await this.connect(),p.debug(`Performing tool call for ${i} with params:`,e),this.stats.toolsCalled+=1;let t=await this.client.callTool({name:i,arguments:e});if(t.isError){let s=t.content?.[0]?.text||"Unknown error";throw p.error(`Error calling tool ${i}: ${s}`),new Error(`Error calling tool ${i}: ${s}`)}if(Array.isArray(t.content)&&t.content.length==1&&t.content[0].type=="text")try{return JSON.parse(t.content[0].text)}catch{}return t.content||[]}};var Z=new h("KnowledgeBase"),ce=1,z=class d{constructor(i){this._sources=[{id:"core.internal",query:async()=>ye}];this._windowSources=[];this.lastResults=[];this.manualEntries=[];this.ai=i}registerSource(i,e){let t=i;return typeof i=="function"&&(e=i,t=`source.${ce++}`),this._sources.push({id:t,query:e}),t}removeSource(i){this._sources=this.sources.filter(e=>e.id!==i&&e.query!==i)}addEntry(i){this.manualEntries.push(i)}removeEntry(i){this.manualEntries=this.manualEntries.filter(e=>e.id!==i)}get sources(){let i=this._sources;return m().knowledgeBaseSources&&(i=i.concat(m().knowledgeBaseSources)),i=i.concat(this._windowSources),i=i.filter(e=>!e.disabled),i}async search(i){Z.debug(`Searching knowledge base for: ${i}`);let e=new Event("webweaver_kb_search",{bubbles:!0,cancelable:!0});e.query=i,e.entries=[],e.sources=[],typeof document<"u"&&document.dispatchEvent(e),this._windowSources=e.sources;let s=(await Promise.all(this.sources.map(async c=>{try{let r=Date.now(),l=await c.query(i);return Z.debug(`Source '${c.id}' took ${Date.now()-r}ms`),l||[]}catch(r){return Z.warn(`Knowledge source '${c.id}' failed:`,r),[]}}))).flat();s=s.concat(e.entries),s=s.concat(this.manualEntries),m().knowledgeBase&&(s=s.concat(m().knowledgeBase)),s=s.filter(c=>c&&!c.disabled);for(let c=0;c<s.length;c++){let r=s[c];r.id=r.id||`temp.${c}`,r._functionID=r.id.replaceAll(/[^a-zA-Z0-9_]/g,"_"),!r.action&&r.type=="tour"&&(r.action=l=>{throw new Error("This tour does not have an action. You must perform the tour content manually.")}),!r.action&&r.type=="info"&&(r.action=l=>{throw new Error("This item does not have an action. Use the content to provide information to the user instead.")})}let n=new Ve({fields:["id","type","name","content","tags"],storeFields:[],searchOptions:{boost:{name:3,tags:2},fuzzy:.2}});n.addAll(s);let a=n.search(i).map(c=>s.find(r=>r.id==c.id));for(let c of s)c.isContext&&(a.find(r=>r.id===c.id)||a.push(c));return this.lastResults=a,Z.debug("Found results:",a),a}getCachedEntry(i){return this.lastResults.find(e=>e.id==i||e._functionID==i)}registerSourceFromURL(i,e){e||(e=`external.${ce++}`),Z.debug(`Registering remote knowledge base source: ${i}`);let t=[],s=[],n=!0,o=async(c,r)=>{Z.debug(`Calling remote knowledge base action: ${c.id}`);let l={type:"action",userID:this.ai.userID,extra:this.ai.extra,actionID:c.id,parameters:r},u=await fetch(i,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(l)});if(!u.ok)throw new Error(`HTTP Error ${u.status} ${u.statusText}`);let M=await u.json();return a(M.updateItems||[]),M.response},a=c=>{for(let r of c){if(!r.id){Z.warn("KB item skipped since it has no ID.",r);continue}let l=t.find(u=>u.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:u=>o(r,u)})}};this.registerSource(e,async c=>{if(n&&s.includes(c))return t;let r={type:"search",userID:this.ai?.userID||"",extra:this.ai?.extra||{},query:c},l=await fetch(i,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(r)});if(!l.ok)throw new Error(`HTTP Error ${l.status} ${l.statusText}`);let u=await l.json();return n=!u.noCache,s.includes(c)||s.push(c),a(u.items),t})}clone(){let i=new d(this.ai);return i._sources=this._sources,i._windowSources=this._windowSources,i.manualEntries=this.manualEntries,i}registerMCPSource(i){i.id||(i.id=`external.${ce++}`);let e=new D(i);return this.registerSource(i.id,t=>e.search(t)),e}};var Y=class{constructor(i){this.ai=i}async boolean(i){let e=await this.instruct({...i,instruction:`${i.instruction}
|
|
13
13
|
|
|
14
14
|
Return only the text "true" or "false" to indicate the answer to the question. Do not include any additional text or explanations.`});if(e.toLowerCase().includes("true"))return!0;if(e.toLowerCase().includes("false"))return!1;throw new Error("The AI did not give a boolean answer: "+e)}async choose(i){let e=await this.instruct({...i,instruction:`${i.instruction}
|
|
15
15
|
|
|
Binary file
|
package/dist/node/node.js
CHANGED
|
@@ -9,7 +9,7 @@ import{v4 as _}from"uuid";function W(u){let t=(u||"").split(`
|
|
|
9
9
|
`,yield*e()}function U(){if(f().userID)return f().userID||"";if(typeof localStorage<"u"){let u=localStorage.getItem("intelliweave.uid")||"";return u||(u=_(),localStorage.setItem("intelliweave.uid",u),u)}else return _()}function j(u){let t=u;if(t?.type=="object"&&t.properties)return u;let e={type:"object",properties:{},required:[]};if(u&&Array.isArray(u)){let s=u;for(let n of s)e.properties[n.name]={type:n.type||"string",description:n.description||""}}return e}var C=class C{constructor(t){this.module="IntelliWeave";this.module=t}get debugEnabled(){return C.debug?!0:typeof window<"u"&&f().debug}log(...t){this.debugEnabled&&console.log(`[IntelliWeave > ${this.module}]`,...t)}debug(...t){this.debugEnabled&&console.debug(`[IntelliWeave > ${this.module}]`,...t)}info(...t){this.debugEnabled&&console.info(`[IntelliWeave > ${this.module}]`,...t)}warn(...t){console.warn(`[IntelliWeave > ${this.module}]`,...t)}error(...t){console.error(`[IntelliWeave > ${this.module}]`,...t)}timer(t,...e){let s=Date.now();return this.debug(`[${t} 0ms] Started`,...e),(...n)=>this.debug(`[${t} ${Math.floor(Date.now()-s)}ms]`,...n)}};C.debug=!1;var g=C;var b=new g("ONNXModel"),J=class u{constructor(t){this.stateTensors={};this.constantTensors={};this._runActive=!1;this.ignoreIfBusy=!1;this.session=t,b.debug(`Model input parameters: ${t.inputNames.join(", ")}`),b.debug(`Model output parameters: ${t.outputNames.join(", ")}`)}static isSupported(){return!!u.lib}static async load(t){if(!u.lib)throw new Error("ONNX runtime not loaded, please set the runtime loader. Example: ONNXModel.lib = () => import('onnxruntime-web')");this.onnx||(b.debug("Loading ONNX runtime"),this.onnx=await u.lib()),b.debug(`Loading model: ${t}`);let e=await this.onnx.InferenceSession.create(t);return new u(e)}makeTensor(t,e,s=0){let n=1;for(let l of e)n*=l;let i;if(t=="float32")i=new u.onnx.Tensor(new Float32Array(n),e);else if(t=="int8")i=new u.onnx.Tensor(new Int8Array(n),e);else if(t=="int16")i=new u.onnx.Tensor(new Int16Array(n),e);else if(t=="int32")i=new u.onnx.Tensor(new Int32Array(n),e);else if(t=="int64")i=new u.onnx.Tensor(new BigInt64Array(n),e);else if(t=="uint8")i=new u.onnx.Tensor(new Uint8Array(n),e);else if(t=="uint16")i=new u.onnx.Tensor(new Uint16Array(n),e);else if(t=="uint32")i=new u.onnx.Tensor(new Uint32Array(n),e);else if(t=="uint64")i=new u.onnx.Tensor(new BigUint64Array(n),e);else throw new Error(`Invalid type: ${t}`);return s!==0&&(t=="int64"||t=="uint64")?i.data.fill(BigInt(s)):s!==0&&i.data.fill(s),i}registerConstant(t,e){if(!this.session.inputNames.includes(t))throw new Error(`Model does not have an input named: ${t}`);return this.constantTensors[t]=e,e}makeConstant(t,e,s,n=0){return this.registerConstant(t,this.makeTensor(e,s,n))}registerState(t,e,s){if(e||(e=t),!this.session.inputNames.includes(t))throw new Error(`Model does not have an input named: ${t}`);if(!this.session.outputNames.includes(e))throw new Error(`Model does not have an output named: ${e}`);return this.stateTensors[t]={outputName:e,tensor:s},s}makeState(t,e,s,n,i=0){return this.registerState(t,e,this.makeTensor(s,n,i))}async run(t={}){if(this._runActive&&this.ignoreIfBusy)return b.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)t[e]=this.stateTensors[e].tensor;for(let e in this.constantTensors)t[e]=this.constantTensors[e];try{let e=await this.session.run(t);for(let s in this.stateTensors){let n=e[this.stateTensors[s].outputName];this.stateTensors[s].tensor=n}return e}finally{this._runActive=!1}}resetState(){b.debug("Resetting state tensors");for(let t in this.stateTensors)this.stateTensors[t].tensor.data.fill(0)}};function ue(u,t){let e=t.reduce((o,r)=>o+r.byteLength,0),s=new DataView(new ArrayBuffer(44));s.setUint8(0,82),s.setUint8(1,73),s.setUint8(2,70),s.setUint8(3,70),s.setUint32(4,44+e,!0),s.setUint8(8,87),s.setUint8(9,65),s.setUint8(10,86),s.setUint8(11,69);let n=1,i=32,l=n*i/8,c=u*l;return s.setUint8(12,102),s.setUint8(13,109),s.setUint8(14,116),s.setUint8(15,32),s.setUint32(16,16,!0),s.setUint16(20,3,!0),s.setUint16(22,n,!0),s.setUint32(24,u,!0),s.setUint32(28,c,!0),s.setUint16(32,l,!0),s.setUint16(34,i,!0),s.setUint8(36,100),s.setUint8(37,97),s.setUint8(38,116),s.setUint8(39,97),s.setUint32(40,e,!0),new File([s,...t],"audio.wav",{type:"audio/wav"})}var F=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 L=class{constructor(t,e){this.outputBufferSize=0;this.partialBuffers=[];this.partialBufferOffset=0;if(!t)throw new Error(`Invalid array class: ${t}`);if(!e||e<=0)throw new Error(`Invalid output buffer size: ${e}`);this.ArrayClass=t,this.outputBufferSize=e}get queuedSize(){return this.partialBuffers.reduce((t,e)=>t+e.length,0)}feed(t){this.partialBuffers.push(t)}get canDrain(){return this.partialBuffers.reduce((e,s)=>e+s.length,0)-this.partialBufferOffset>=this.outputBufferSize}drain(){if(!this.canDrain)return null;let t=this.ArrayClass,e=new t(this.outputBufferSize),s=0;for(;s!=e.length;){if(s>e.length)throw new Error(`Buffer overflow: ${s} > ${e.length}`);let n=e.length-s,i=this.partialBuffers[0],l=i.length-this.partialBufferOffset;l<n?(e.set(i.subarray(this.partialBufferOffset),s),s+=l,this.partialBuffers.shift(),this.partialBufferOffset=0):(e.set(i.subarray(this.partialBufferOffset,this.partialBufferOffset+n),s),s+=n,this.partialBufferOffset+=n)}return e}pad(){let t=this.queuedSize%this.outputBufferSize;if(t==0)return;let e=this.ArrayClass,s=new e(t);this.feed(s)}};var z=class{constructor(t,e,s,n){if(!t||!e||!s)throw new Error("Invalid settings specified for the resampler.");this.resampler=null,this.fromSampleRate=t,this.toSampleRate=e,this.channels=s||0,this.inputBufferSize=n,this.initialize()}initialize(){this.fromSampleRate==this.toSampleRate?(this.resampler=t=>t,this.ratioWeight=1):(this.fromSampleRate<this.toSampleRate?(this.linearInterpolation(),this.lastWeight=1):(this.multiTap(),this.tailExists=!1,this.lastWeight=0),this.initializeBuffers(),this.ratioWeight=this.fromSampleRate/this.toSampleRate)}bufferSlice(t){try{return this.outputBuffer.subarray(0,t)}catch{try{return this.outputBuffer.length=t,this.outputBuffer}catch{return this.outputBuffer.slice(0,t)}}}initializeBuffers(){this.outputBufferSize=Math.ceil(this.inputBufferSize*this.toSampleRate/this.fromSampleRate/this.channels*1.0000004768371582)+this.channels+this.channels;try{this.outputBuffer=new Float32Array(this.outputBufferSize),this.lastOutput=new Float32Array(this.channels)}catch{this.outputBuffer=[],this.lastOutput=[]}}linearInterpolation(){this.resampler=t=>{let e=t.length,s=this.channels,n,i,l,c,o,r,a,d,h;if(e%s!==0)throw new Error("Buffer was of incorrect sample length.");if(e<=0)return[];for(n=this.outputBufferSize,i=this.ratioWeight,l=this.lastWeight,c=0,o=0,r=0,a=0,d=this.outputBuffer;l<1;l+=i)for(o=l%1,c=1-o,this.lastWeight=l%1,h=0;h<this.channels;++h)d[a++]=this.lastOutput[h]*c+t[h]*o;for(l-=1,e-=s,r=Math.floor(l)*s;a<n&&r<e;){for(o=l%1,c=1-o,h=0;h<this.channels;++h)d[a++]=t[r+(h>0?h:0)]*c+t[r+(s+h)]*o;l+=i,r=Math.floor(l)*s}for(h=0;h<s;++h)this.lastOutput[h]=t[r++];return this.bufferSlice(a)}}multiTap(){this.resampler=t=>{let e=t.length,s,n,i=this.channels,l,c,o,r,a,d,h,S,T;if(e%i!==0)throw new Error("Buffer was of incorrect sample length.");if(e<=0)return[];for(s=this.outputBufferSize,n=[],l=this.ratioWeight,c=0,r=0,a=0,d=!this.tailExists,this.tailExists=!1,h=this.outputBuffer,S=0,T=0,o=0;o<i;++o)n[o]=0;do{if(d)for(c=l,o=0;o<i;++o)n[o]=0;else{for(c=this.lastWeight,o=0;o<i;++o)n[o]=this.lastOutput[o];d=!0}for(;c>0&&r<e;)if(a=1+r-T,c>=a){for(o=0;o<i;++o)n[o]+=t[r++]*a;T=r,c-=a}else{for(o=0;o<i;++o)n[o]+=t[r+(o>0?o:0)]*c;T+=c,c=0;break}if(c===0)for(o=0;o<i;++o)h[S++]=n[o]/l;else{for(this.lastWeight=c,o=0;o<i;++o)this.lastOutput[o]=n[o];this.tailExists=!0;break}}while(r<e&&S<s);return this.bufferSlice(S)}}resample(t){return this.fromSampleRate==this.toSampleRate?this.ratioWeight=1:(this.fromSampleRate<this.toSampleRate?this.lastWeight=1:(this.tailExists=!1,this.lastWeight=0),this.initializeBuffers(),this.ratioWeight=this.fromSampleRate/this.toSampleRate),this.resampler(t)}};function ge(u){let t=u.length,e=new Int16Array(t);for(;t--;){let s=Math.max(-1,Math.min(1,u[t]));e[t]=s<0?s*32768:s*32767}return e}function fe(u){let t=u.length,e=new BigInt64Array(t);for(;t--;){let s=Math.max(-1,Math.min(1,u[t]));e[t]=BigInt(Math.floor(s<0?s*32768:s*32767))*0x100000000000n}return e}function pe(u){let t=u.length,e=new Float32Array(t);for(;t--;){let s=u[t];e[t]=s>=32768?-(65536-s)/32768:s/32767}return e}import{countTokens as k}from"gpt-tokenizer";import{v4 as X}from"uuid";var B=class{constructor(){this.size=4096;this.groups=[]}createGroup(t){let e=new G;return e.id=t,this.groups.push(e),e}group(t){return this.groups.find(e=>e.id===t)}countTokens(){return this.groups.reduce((t,e)=>t+e.tokenCount,0)}removeOverflow(){let t=this.countTokens(),e=this.groups.reduce((s,n)=>s+n.weight,0);for(;t>this.size;){let s=this.groups.slice().sort((i,l)=>{let c=Math.floor(i.weight/e*this.size),o=Math.floor(l.weight/e*this.size),r=i.tokenCount-c;return l.tokenCount-o-r}),n=this.removeOneItem(s);if(!n)throw new Error("Too many items in the token window that cannot be removed.");t-=n.tokenCount}}removeOneItem(t){for(let e of t){let s=e.items.findIndex(i=>!i.cannotRemove);if(s===-1)continue;let n=e.items[s];return e.items.splice(s,1),e.tokenCount-=n.tokenCount,n}return null}},G=class{constructor(){this.id="";this.items=[];this.weight=1;this.tokenCount=0;this.separator=`
|
|
10
10
|
`;this.itemPadding=0;this.sortFunction=(t,e)=>t.sortOrder!==e.sortOrder?t.sortOrder-e.sortOrder:t.dateAdded!==e.dateAdded?t.dateAdded-e.dateAdded:t.content.localeCompare(e.content)}setItemPadding(t){return this.itemPadding=t,this.recalculateTokens(),this}sortBy(t){return this.sortFunction=t,this.items.sort(this.sortFunction),this}setSeparator(t){return this.separator==t?this:(this.separator=t,this.recalculateTokens(),this)}setWeight(t){return this.weight=t,this}recalculateTokens(){this.tokenCount=this.items.reduce((t,e)=>(e.tokenCount=k(e.content)+k(this.separator)+this.itemPadding,t+e.tokenCount),0)}add(t){typeof t=="string"&&(t={content:t});let e=t;e.id===void 0&&(e.id=X());let s=this.items.find(n=>n.id===e.id);return s?(this.tokenCount-=s.tokenCount,Object.assign(s,e),e=s):this.items.push(e),e.dateAdded===void 0&&(e.dateAdded=Date.now()),e.sortOrder===void 0&&(e.sortOrder=0),e.disabled===void 0&&(e.disabled=!1),e.tokenCount=e.disabled?0:k(t.content)+k(this.separator)+this.itemPadding,this.tokenCount+=e.tokenCount,this.items.sort(this.sortFunction),e}getAllAsString(){return this.getAll().map(t=>t.content).join(this.separator)}getAll(){return this.items.filter(t=>!t.disabled)}empty(){this.items=[],this.tokenCount=0}};var x=1,w=new g("ChatGPT"),E=class{constructor(t){this.id="";this.metadata={};this.config={apiKey:"",endpoint:"",model:"gpt-4-turbo",systemMessage:"",userID:"",stream:!0,maxTokens:4096};this.maxToolCallsPerMessage=10;this.stats={tokensUsed:0};this.tokenWindow=(()=>{let t=new B;return t.createGroup("context").setSeparator(`
|
|
11
11
|
|
|
12
|
-
`),t.createGroup("actions").setItemPadding(25),t.createGroup("messages").setItemPadding(10),t})();this.config={...this.config,...t},this.tokenWindow.size=this.config.maxTokens||4096}get contextGroup(){return this.tokenWindow.group("context")}get toolGroup(){return this.tokenWindow.group("actions")}get messageGroup(){return this.tokenWindow.group("messages")}async sendMessage(t){let e={role:"user",content:t};this.messageGroup.add({id:"msg-"+x++,content:JSON.stringify(e),customData:e}),await this.processMessages();let n=this.messageGroup.items[this.messageGroup.items.length-1]?.customData;return n?.role=="assistant"&&n.content||""}addAssistantMessage(t){let e={role:"assistant",content:t};this.messageGroup.add({id:"msg-"+x++,content:JSON.stringify(e),customData:e})}addUserMessage(t){let e={role:"user",content:t};this.messageGroup.add({id:"msg-"+x++,content:JSON.stringify(e),customData:e})}getMessages(){return this.messageGroup.items.map(t=>t.customData)}getLatestMessage(){return this.messageGroup.items.length?this.messageGroup.items[this.messageGroup.items.length-1]?.customData:void 0}async processMessages(){await this.config.onBeforeMessageProcessing?.(),w.debugEnabled&&w.debug("Process message state:",{context:this.contextGroup.getAllAsString(),tools:this.toolGroup.getAll().map(s=>s.customData),messages:this.messageGroup.items.map(s=>s.customData),tokens:{used:this.tokenWindow.countTokens(),max:this.tokenWindow.size}});let e=this.messageGroup.items[this.messageGroup.items.length-1]?.customData;if(e?.role=="user"||e?.role=="tool")await this.sendToAPI(),await this.processMessages();else if(e?.role=="assistant"&&e?.tool_calls?.length){for(let s of e.tool_calls)await this.processToolCall(s);await this.processMessages()}}async trimMessages(){for(this.tokenWindow.removeOverflow();this.messageGroup.items.length&&this.messageGroup.items[0].customData?.role=="tool";)this.messageGroup.items.shift()}async sendToAPI(t){this.config.systemMessage&&this.contextGroup.add({id:"_gpt_context",cannotRemove:!0,sortOrder:0,content:this.config.systemMessage}),await this.trimMessages();let e=0;for(let o=this.messageGroup.items.length-1;o>=0;o--){let r=this.messageGroup.items[o].customData;if(r.role=="assistant"&&r.tool_calls){if(e+=1,e>=this.maxToolCallsPerMessage)throw new Error(`Exceeded the maximum tool calls per message limit of ${this.maxToolCallsPerMessage}.`)}else if(r.role=="user")break}w.debugEnabled&&w.debug("Before LLM state:",{context:this.contextGroup.getAllAsString(),tools:this.toolGroup.getAll().map(o=>o.customData),messages:this.messageGroup.items.map(o=>o.customData),tokens:{used:this.tokenWindow.countTokens(),max:this.tokenWindow.size}});let s={model:this.config.model,temperature:.2,user:this.config.userID,tools:[],stream:!!this.config.stream,max_tokens:1024,messages:[{role:"system",content:this.contextGroup.getAllAsString()},...this.messageGroup.getAll().map(o=>o.customData)]};for(let o of this.toolGroup.getAll()){let r=o.customData;r.description&&r.description.length>1024&&w.warn(`Tool description for "${r.name}" is too long, it will be truncated.`);let a={type:"function",function:{name:r.name,description:(r.description||"").substring(0,1024),parameters:r.params}};s.tools.push(a)}s.user||delete s.user,s.tools.length||delete s.tools;let n={};if(n["Content-Type"]="application/json",this.config.apiKey&&(n.Authorization=`Bearer ${this.config.apiKey}`),t)return s;let i=await fetch(this.config.endpoint||"https://api.openai.com/v1/chat/completions",{method:"POST",headers:n,body:JSON.stringify(s)});if(!i.ok){let o=`${i.status} ${i.statusText}`;try{let a=await i.json();o=a.errorText||a.error?.message||a.error||o}catch{}let r=new Error(o);throw r.code=i.status,r}let l=null,c=null;if(this.config.stream)for await(let o of R(i.body)){if(o.data=="[DONE]")break;if(!o.data)continue;let r=JSON.parse(o.data);if(c){for(let a in r.choices[0].delta)if(typeof r.choices[0].delta[a]=="string"){if(a=="role"&&c[a]==r.choices[0].delta[a])continue;c[a]=(c[a]||"")+r.choices[0].delta[a]}for(let a of r.choices[0].delta.tool_calls||[]){if(c.tool_calls||(c.tool_calls=[]),!c.tool_calls[a.index]){c.tool_calls[a.index]=a;continue}let d=c.tool_calls[a.index];d.function=d.function||{},d.function.name=(d.function.name||"")+(a.function?.name||""),d.function.arguments=(d.function.arguments||"")+(a.function?.arguments||"")}}else c={chunkID:r.id,...r.choices[0].delta};c?.content&&!c.content.startsWith("{")&&this.config.onAIMessage?.(c.content,!0)}else l=await i.json(),this.stats.tokensUsed+=l.usage?.total_tokens||0,w.debug(`Used ${l.usage?.total_tokens||0} tokens, total used: ${this.stats.tokensUsed}`),c=l.choices?.[0]?.message;if(c||(w.warn("No response block in API response."),c={role:"assistant",content:""}),c.role=="user")throw new Error("API returned a user message, which is not allowed.");this.processIncomingMessage(c),this.messageGroup.add({id:"msg-"+x++,content:JSON.stringify(c),customData:c}),c.content&&this.config.onAIMessage?.(c.content,!1)}processIncomingMessage(t){}registerTool(t){this.toolGroup.add({id:t.name,cannotRemove:!t.canRemove,content:JSON.stringify(t),customData:t})}async processToolCall(t){try{let s=this.toolGroup.items.find(c=>c.id==t.function.name)?.customData;if(!s)throw new Error(`Tool "${t.function.name}" not found.`);let n=JSON.parse(t.function.arguments);this.config.onAIToolStart?.(t.function.name,n);let i=await s.callback(n);typeof i!="string"&&(i=JSON.stringify(i)||""),(i===""||i==="undefined")&&(i="success");let l={role:"tool",content:i,tool_call_id:t.id};this.messageGroup.add({id:"msg-"+x++,content:JSON.stringify(l),customData:l})}catch(e){w.warn(`Unable to process tool call for "${t?.function?.name}"`,e);let s={role:"tool",content:`Error: ${e.message}`,tool_call_id:t.id};this.messageGroup.add({id:"msg-"+x++,content:JSON.stringify(s),customData:s})}}resetConversation(){this.messageGroup.empty(),this.toolGroup.empty(),this.contextGroup.empty()}};import ne from"minisearch";var q=[{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(u,t)=>{let e=await t.knowledgeBase.search(u.query),s=t;s._lastKBsearch=u.query,s._nextRequestUseKBitems=e,t.submitAnalyticsEvent({type:"kb-search",query:u.query,results:e});let n=e.filter(l=>l.type!="action").map(l=>"id="+l.id).join(", ")||"(none)",i=e.filter(l=>l.type=="action").map(l=>"name="+l._functionID).join(", ")||"(none)";return`Search complete, context has been updated. New info entries found: ${n}. New tools available: ${i}.`}}];import{Client as H}from"@modelcontextprotocol/sdk/client/index.js";import{StreamableHTTPClientTransport as Q}from"@modelcontextprotocol/sdk/client/streamableHttp.js";import{SSEClientTransport as ee}from"@modelcontextprotocol/sdk/client/sse.js";var y={name:"@intelliweave/embedded",version:"1.8.61",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 && IW_TESTER_API_KEY=97150cfc-5b97-413e-8da3-e86d01f70057 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:{"@modelcontextprotocol/sdk":"^1.12.1","@types/json-schema":"^7.0.15","gpt-tokenizer":"^2.9.0",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","utility-types":"^3.11.0",uuid:"^10.0.0"}};import{LoggingMessageNotificationSchema as te,ToolListChangedNotificationSchema as se}from"@modelcontextprotocol/sdk/types.js";var m=new g("MCPKnowledgeClient"),M=class{constructor(t){this.tools=[];this.iwActions=[];this.stats={toolsCalled:0};this.lastSearchQuery="";this.lastSearchResults=[];this.config=t}async connect(){return this.client?this.client:this.connectionPromise?await this.connectionPromise:(this.connectionPromise=this.connectInternal(),this.connectionPromise.finally(()=>{this.connectionPromise=void 0}),await this.connectionPromise)}async connectInternal(){m.debug("Connecting to MCP client");let t=this.config.connect?await this.config.connect():await Promise.resolve().then(async()=>{let e=new H({name:y.name,version:y.version}),s=new Q(new URL(this.config.baseURL));return await e.connect(s),m.debug("Connected with HTTP streaming mode"),e}).catch(async e=>{let s=new H({name:y.name,version:y.version}),n=new ee(new URL(this.config.baseURL));return await s.connect(n),m.debug("Connected with SSE mode"),s});return await this.disconnect(),this.client=t,t.onerror=e=>{m.error(`MCP client error: ${e.message}`)},t.onclose=()=>{m.debug("MCP client connection closed"),this.client=void 0},t.setNotificationHandler(te,e=>{e.params.level=="critical"?m.error(`[Server] ${e.params.data}`):e.params.level=="emergency"?m.error(`[Server] ${e.params.data}`):e.params.level=="error"?m.error(`[Server] ${e.params.data}`):e.params.level=="warning"?m.warn(`[Server] ${e.params.data}`):e.params.level=="info"?m.info(`[Server] ${e.params.data}`):e.params.level=="debug"?m.debug(`[Server] ${e.params.data}`):m.log(`[Server] ${e.params.data}`)}),t.setNotificationHandler(se,e=>{m.debug("Tool list changed",e),this.fetchTools()}),m.debug("Fetching tools from MCP server..."),await this.fetchTools(),t}async disconnect(){await this.client?.close(),this.client=void 0,this.tools=[],this.iwActions=[]}async fetchTools(){let t=[],e;for(;;){let n=await this.client.listTools({cursor:e});e=n.nextCursor;for(let i of n.tools||[])t.push(i);if(!n?.tools?.length||!e)break}let s=[];for(let n of t){let i=!!(this.config.searchToolName&&n.name==this.config.searchToolName&&!this.config.searchToolVisible);s.push({id:n.name,name:n.name,content:n.description||"",type:"action",isContext:!0,parameters:n.inputSchema,action:l=>this.performToolCall(n.name,l),disabled:i})}m.debug(`Fetched ${t.length} tools from MCP server.`),this.tools=t,this.iwActions=s}async search(t){if(t==this.lastSearchQuery)return this.lastSearchResults;await this.connect();let e=this.iwActions.slice(),s=await this.performSearchCall(t);return e=e.concat(s),this.lastSearchQuery=t,this.lastSearchResults=e,m.debug(`Search completed, found ${e.length} items.`),e}async performSearchCall(t){let e=[];if(!this.config.searchToolName)return e;let s=this.tools.find(i=>i.name==this.config.searchToolName);if(!s)return m.warn(`Search function ${this.config.searchToolName} not found on the MCP server.`),e;let n;if(s.inputSchema.required?.length==1){let i=s.inputSchema.required[0];n=await this.performToolCall(this.config.searchToolName,{[i]:t})}else if(s.inputSchema.properties&&Object.keys(s.inputSchema.properties).length==1){let i=Object.keys(s.inputSchema.properties)[0];n=await this.performToolCall(this.config.searchToolName,{[i]:t})}else n=await this.performToolCall(this.config.searchToolName,{});if(Array.isArray(n))for(let i=0;i<n.length;i++){let l=n[i];e.push({id:this.config.id+":"+this.config.searchToolName+":"+i,name:`Search result ${i+1} in ${this.config.searchToolName}`,type:"info",content:typeof l=="string"?l:JSON.stringify(l)})}else e.push({id:this.config.id+":"+this.config.searchToolName+":result",name:`Search result in ${this.config.searchToolName}`,type:"info",content:typeof n=="string"?n:JSON.stringify(n)});return e}async performToolCall(t,e){await this.connect(),m.debug(`Performing tool call for ${t} with params:`,e),this.stats.toolsCalled+=1;let s=await this.client.callTool({name:t,arguments:e});if(s.isError){let n=s.content?.[0]?.text||"Unknown error";throw m.error(`Error calling tool ${t}: ${n}`),new Error(`Error calling tool ${t}: ${n}`)}if(Array.isArray(s.content)&&s.content.length==1&&s.content[0].type=="text")try{return JSON.parse(s.content[0].text)}catch{}return s.content||[]}};var v=new g("KnowledgeBase"),K=1,P=class u{constructor(t){this._sources=[{id:"core.internal",query:async()=>q}];this._windowSources=[];this.lastResults=[];this.manualEntries=[];this.ai=t}registerSource(t,e){let s=t;return typeof t=="function"&&(e=t,s=`source.${K++}`),this._sources.push({id:s,query:e}),s}removeSource(t){this._sources=this.sources.filter(e=>e.id!==t&&e.query!==t)}addEntry(t){this.manualEntries.push(t)}removeEntry(t){this.manualEntries=this.manualEntries.filter(e=>e.id!==t)}get sources(){let t=this._sources;return f().knowledgeBaseSources&&(t=t.concat(f().knowledgeBaseSources)),t=t.concat(this._windowSources),t=t.filter(e=>!e.disabled),t}async search(t){v.debug(`Searching knowledge base for: ${t}`);let e=new Event("webweaver_kb_search",{bubbles:!0,cancelable:!0});e.query=t,e.entries=[],e.sources=[],typeof document<"u"&&document.dispatchEvent(e),this._windowSources=e.sources;let n=(await Promise.all(this.sources.map(async o=>{try{let r=Date.now(),a=await o.query(t);return v.debug(`Source '${o.id}' took ${Date.now()-r}ms`),a||[]}catch(r){return v.warn(`Knowledge source '${o.id}' failed:`,r),[]}}))).flat();n=n.concat(e.entries),n=n.concat(this.manualEntries),f().knowledgeBase&&(n=n.concat(f().knowledgeBase)),n=n.filter(o=>o&&!o.disabled);for(let o=0;o<n.length;o++){let r=n[o];r.id=r.id||`temp.${o}`,r._functionID=r.id.replaceAll(/[^a-zA-Z0-9_]/g,"_"),!r.action&&r.type=="tour"&&(r.action=a=>{throw new Error("This tour does not have an action. You must perform the tour content manually.")}),!r.action&&r.type=="info"&&(r.action=a=>{throw new Error("This item does not have an action. Use the content to provide information to the user instead.")})}let i=new ne({fields:["id","type","name","content","tags"],storeFields:[],searchOptions:{boost:{name:3,tags:2},fuzzy:.2}});i.addAll(n);let c=i.search(t).map(o=>n.find(r=>r.id==o.id));for(let o of n)o.isContext&&(c.find(r=>r.id===o.id)||c.push(o));return this.lastResults=c,v.debug("Found results:",c),c}getCachedEntry(t){return this.lastResults.find(e=>e.id==t||e._functionID==t)}registerSourceFromURL(t,e){e||(e=`external.${K++}`),v.debug(`Registering remote knowledge base source: ${t}`);let s=[],n=[],i=!0,l=async(o,r)=>{v.debug(`Calling remote knowledge base action: ${o.id}`);let a={type:"action",userID:this.ai.userID,extra:this.ai.extra,actionID:o.id,parameters:r},d=await fetch(t,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(a)});if(!d.ok)throw new Error(`HTTP Error ${d.status} ${d.statusText}`);let h=await d.json();return c(h.updateItems||[]),h.response},c=o=>{for(let r of o){if(!r.id){v.warn("KB item skipped since it has no ID.",r);continue}let a=s.find(d=>d.id==r.id);if(a){a.name=r.name||a.name||"",a.content=r.content||a.content||"",a.disabled=r.disabled??a.disabled,a.isContext=r.isContext??a.isContext,a.parameters=r.parameters||a.parameters||[],a.tags=r.tags||a.tags,a.type=r.type||a.type;continue}s.push({...r,action:d=>l(r,d)})}};this.registerSource(e,async o=>{if(i&&n.includes(o))return s;let r={type:"search",userID:this.ai?.userID||"",extra:this.ai?.extra||{},query:o},a=await fetch(t,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(r)});if(!a.ok)throw new Error(`HTTP Error ${a.status} ${a.statusText}`);let d=await a.json();return i=!d.noCache,n.includes(o)||n.push(o),c(d.items),s})}clone(){let t=new u(this.ai);return t._sources=this._sources,t._windowSources=this._windowSources,t.manualEntries=this.manualEntries,t}registerMCPSource(t){t.id||(t.id=`external.${K++}`);let e=new M(t);return this.registerSource(t.id,s=>e.search(s)),e}};import{v4 as D}from"uuid";var A=class{constructor(t){this.ai=t}async boolean(t){let e=await this.instruct({...t,instruction:`${t.instruction}
|
|
12
|
+
`),t.createGroup("actions").setItemPadding(25),t.createGroup("messages").setItemPadding(10),t})();this.config={...this.config,...t},this.tokenWindow.size=this.config.maxTokens||4096}get contextGroup(){return this.tokenWindow.group("context")}get toolGroup(){return this.tokenWindow.group("actions")}get messageGroup(){return this.tokenWindow.group("messages")}async sendMessage(t){let e={role:"user",content:t};this.messageGroup.add({id:"msg-"+x++,content:JSON.stringify(e),customData:e}),await this.processMessages();let n=this.messageGroup.items[this.messageGroup.items.length-1]?.customData;return n?.role=="assistant"&&n.content||""}addAssistantMessage(t){let e={role:"assistant",content:t};this.messageGroup.add({id:"msg-"+x++,content:JSON.stringify(e),customData:e})}addUserMessage(t){let e={role:"user",content:t};this.messageGroup.add({id:"msg-"+x++,content:JSON.stringify(e),customData:e})}getMessages(){return this.messageGroup.items.map(t=>t.customData)}getLatestMessage(){return this.messageGroup.items.length?this.messageGroup.items[this.messageGroup.items.length-1]?.customData:void 0}async processMessages(){await this.config.onBeforeMessageProcessing?.(),w.debugEnabled&&w.debug("Process message state:",{context:this.contextGroup.getAllAsString(),tools:this.toolGroup.getAll().map(s=>s.customData),messages:this.messageGroup.items.map(s=>s.customData),tokens:{used:this.tokenWindow.countTokens(),max:this.tokenWindow.size}});let e=this.messageGroup.items[this.messageGroup.items.length-1]?.customData;if(e?.role=="user"||e?.role=="tool")await this.sendToAPI(),await this.processMessages();else if(e?.role=="assistant"&&e?.tool_calls?.length){for(let s of e.tool_calls)await this.processToolCall(s);await this.processMessages()}}async trimMessages(){for(this.tokenWindow.removeOverflow();this.messageGroup.items.length&&this.messageGroup.items[0].customData?.role=="tool";)this.messageGroup.items.shift()}async sendToAPI(t){this.config.systemMessage&&this.contextGroup.add({id:"_gpt_context",cannotRemove:!0,sortOrder:0,content:this.config.systemMessage}),await this.trimMessages();let e=0;for(let o=this.messageGroup.items.length-1;o>=0;o--){let r=this.messageGroup.items[o].customData;if(r.role=="assistant"&&r.tool_calls){if(e+=1,e>=this.maxToolCallsPerMessage)throw new Error(`Exceeded the maximum tool calls per message limit of ${this.maxToolCallsPerMessage}.`)}else if(r.role=="user")break}w.debugEnabled&&w.debug("Before LLM state:",{context:this.contextGroup.getAllAsString(),tools:this.toolGroup.getAll().map(o=>o.customData),messages:this.messageGroup.items.map(o=>o.customData),tokens:{used:this.tokenWindow.countTokens(),max:this.tokenWindow.size}});let s={model:this.config.model,temperature:.2,user:this.config.userID,tools:[],stream:!!this.config.stream,messages:[{role:"system",content:this.contextGroup.getAllAsString()},...this.messageGroup.getAll().map(o=>o.customData)]};for(let o of this.toolGroup.getAll()){let r=o.customData;r.description&&r.description.length>1024&&w.warn(`Tool description for "${r.name}" is too long, it will be truncated.`);let a={type:"function",function:{name:r.name,description:(r.description||"").substring(0,1024),parameters:r.params}};s.tools.push(a)}s.user||delete s.user,s.tools.length||delete s.tools;let n={};if(n["Content-Type"]="application/json",this.config.apiKey&&(n.Authorization=`Bearer ${this.config.apiKey}`),t)return s;let i=await fetch(this.config.endpoint||"https://api.openai.com/v1/chat/completions",{method:"POST",headers:n,body:JSON.stringify(s)});if(!i.ok){let o=`${i.status} ${i.statusText}`;try{let a=await i.json();o=a.errorText||a.error?.message||a.error||o}catch{}let r=new Error(o);throw r.code=i.status,r}let l=null,c=null;if(this.config.stream)for await(let o of R(i.body)){if(o.data=="[DONE]")break;if(!o.data)continue;let r=JSON.parse(o.data);if(c){for(let a in r.choices[0].delta)if(typeof r.choices[0].delta[a]=="string"){if(a=="role"&&c[a]==r.choices[0].delta[a])continue;c[a]=(c[a]||"")+r.choices[0].delta[a]}for(let a of r.choices[0].delta.tool_calls||[]){if(c.tool_calls||(c.tool_calls=[]),!c.tool_calls[a.index]){c.tool_calls[a.index]=a;continue}let d=c.tool_calls[a.index];d.function=d.function||{},d.function.name=(d.function.name||"")+(a.function?.name||""),d.function.arguments=(d.function.arguments||"")+(a.function?.arguments||"")}}else c={chunkID:r.id,...r.choices[0].delta};c?.content&&!c.content.startsWith("{")&&this.config.onAIMessage?.(c.content,!0)}else l=await i.json(),this.stats.tokensUsed+=l.usage?.total_tokens||0,w.debug(`Used ${l.usage?.total_tokens||0} tokens, total used: ${this.stats.tokensUsed}`),c=l.choices?.[0]?.message;if(c||(w.warn("No response block in API response."),c={role:"assistant",content:""}),c.role=="user")throw new Error("API returned a user message, which is not allowed.");this.processIncomingMessage(c),this.messageGroup.add({id:"msg-"+x++,content:JSON.stringify(c),customData:c}),c.content&&this.config.onAIMessage?.(c.content,!1)}processIncomingMessage(t){}registerTool(t){this.toolGroup.add({id:t.name,cannotRemove:!t.canRemove,content:JSON.stringify(t),customData:t})}async processToolCall(t){try{let s=this.toolGroup.items.find(c=>c.id==t.function.name)?.customData;if(!s)throw new Error(`Tool "${t.function.name}" not found.`);let n=JSON.parse(t.function.arguments);this.config.onAIToolStart?.(t.function.name,n);let i=await s.callback(n);typeof i!="string"&&(i=JSON.stringify(i)||""),(i===""||i==="undefined")&&(i="success");let l={role:"tool",content:i,tool_call_id:t.id};this.messageGroup.add({id:"msg-"+x++,content:JSON.stringify(l),customData:l})}catch(e){w.warn(`Unable to process tool call for "${t?.function?.name}"`,e);let s={role:"tool",content:`Error: ${e.message}`,tool_call_id:t.id};this.messageGroup.add({id:"msg-"+x++,content:JSON.stringify(s),customData:s})}}resetConversation(){this.messageGroup.empty(),this.toolGroup.empty(),this.contextGroup.empty()}};import ne from"minisearch";var q=[{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(u,t)=>{let e=await t.knowledgeBase.search(u.query),s=t;s._lastKBsearch=u.query,s._nextRequestUseKBitems=e,t.submitAnalyticsEvent({type:"kb-search",query:u.query,results:e});let n=e.filter(l=>l.type!="action").map(l=>"id="+l.id).join(", ")||"(none)",i=e.filter(l=>l.type=="action").map(l=>"name="+l._functionID).join(", ")||"(none)";return`Search complete, context has been updated. New info entries found: ${n}. New tools available: ${i}.`}}];import{Client as H}from"@modelcontextprotocol/sdk/client/index.js";import{StreamableHTTPClientTransport as Q}from"@modelcontextprotocol/sdk/client/streamableHttp.js";import{SSEClientTransport as ee}from"@modelcontextprotocol/sdk/client/sse.js";var y={name:"@intelliweave/embedded",version:"1.8.62",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 && IW_TESTER_API_KEY=97150cfc-5b97-413e-8da3-e86d01f70057 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:{"@modelcontextprotocol/sdk":"^1.12.1","@types/json-schema":"^7.0.15","gpt-tokenizer":"^2.9.0",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","utility-types":"^3.11.0",uuid:"^10.0.0"}};import{LoggingMessageNotificationSchema as te,ToolListChangedNotificationSchema as se}from"@modelcontextprotocol/sdk/types.js";var m=new g("MCPKnowledgeClient"),M=class{constructor(t){this.tools=[];this.iwActions=[];this.stats={toolsCalled:0};this.lastSearchQuery="";this.lastSearchResults=[];this.config=t}async connect(){return this.client?this.client:this.connectionPromise?await this.connectionPromise:(this.connectionPromise=this.connectInternal(),this.connectionPromise.finally(()=>{this.connectionPromise=void 0}),await this.connectionPromise)}async connectInternal(){m.debug("Connecting to MCP client");let t=this.config.connect?await this.config.connect():await Promise.resolve().then(async()=>{let e=new H({name:y.name,version:y.version}),s=new Q(new URL(this.config.baseURL));return await e.connect(s),m.debug("Connected with HTTP streaming mode"),e}).catch(async e=>{let s=new H({name:y.name,version:y.version}),n=new ee(new URL(this.config.baseURL));return await s.connect(n),m.debug("Connected with SSE mode"),s});return await this.disconnect(),this.client=t,t.onerror=e=>{m.error(`MCP client error: ${e.message}`)},t.onclose=()=>{m.debug("MCP client connection closed"),this.client=void 0},t.setNotificationHandler(te,e=>{e.params.level=="critical"?m.error(`[Server] ${e.params.data}`):e.params.level=="emergency"?m.error(`[Server] ${e.params.data}`):e.params.level=="error"?m.error(`[Server] ${e.params.data}`):e.params.level=="warning"?m.warn(`[Server] ${e.params.data}`):e.params.level=="info"?m.info(`[Server] ${e.params.data}`):e.params.level=="debug"?m.debug(`[Server] ${e.params.data}`):m.log(`[Server] ${e.params.data}`)}),t.setNotificationHandler(se,e=>{m.debug("Tool list changed",e),this.fetchTools()}),m.debug("Fetching tools from MCP server..."),await this.fetchTools(),t}async disconnect(){await this.client?.close(),this.client=void 0,this.tools=[],this.iwActions=[]}async fetchTools(){let t=[],e;for(;;){let n=await this.client.listTools({cursor:e});e=n.nextCursor;for(let i of n.tools||[])t.push(i);if(!n?.tools?.length||!e)break}let s=[];for(let n of t){let i=!!(this.config.searchToolName&&n.name==this.config.searchToolName&&!this.config.searchToolVisible);s.push({id:n.name,name:n.name,content:n.description||"",type:"action",isContext:!0,parameters:n.inputSchema,action:l=>this.performToolCall(n.name,l),disabled:i})}m.debug(`Fetched ${t.length} tools from MCP server.`),this.tools=t,this.iwActions=s}async search(t){if(t==this.lastSearchQuery)return this.lastSearchResults;await this.connect();let e=this.iwActions.slice(),s=await this.performSearchCall(t);return e=e.concat(s),this.lastSearchQuery=t,this.lastSearchResults=e,m.debug(`Search completed, found ${e.length} items.`),e}async performSearchCall(t){let e=[];if(!this.config.searchToolName)return e;let s=this.tools.find(i=>i.name==this.config.searchToolName);if(!s)return m.warn(`Search function ${this.config.searchToolName} not found on the MCP server.`),e;let n;if(s.inputSchema.required?.length==1){let i=s.inputSchema.required[0];n=await this.performToolCall(this.config.searchToolName,{[i]:t})}else if(s.inputSchema.properties&&Object.keys(s.inputSchema.properties).length==1){let i=Object.keys(s.inputSchema.properties)[0];n=await this.performToolCall(this.config.searchToolName,{[i]:t})}else n=await this.performToolCall(this.config.searchToolName,{});if(Array.isArray(n))for(let i=0;i<n.length;i++){let l=n[i];e.push({id:this.config.id+":"+this.config.searchToolName+":"+i,name:`Search result ${i+1} in ${this.config.searchToolName}`,type:"info",content:typeof l=="string"?l:JSON.stringify(l)})}else e.push({id:this.config.id+":"+this.config.searchToolName+":result",name:`Search result in ${this.config.searchToolName}`,type:"info",content:typeof n=="string"?n:JSON.stringify(n)});return e}async performToolCall(t,e){await this.connect(),m.debug(`Performing tool call for ${t} with params:`,e),this.stats.toolsCalled+=1;let s=await this.client.callTool({name:t,arguments:e});if(s.isError){let n=s.content?.[0]?.text||"Unknown error";throw m.error(`Error calling tool ${t}: ${n}`),new Error(`Error calling tool ${t}: ${n}`)}if(Array.isArray(s.content)&&s.content.length==1&&s.content[0].type=="text")try{return JSON.parse(s.content[0].text)}catch{}return s.content||[]}};var v=new g("KnowledgeBase"),K=1,P=class u{constructor(t){this._sources=[{id:"core.internal",query:async()=>q}];this._windowSources=[];this.lastResults=[];this.manualEntries=[];this.ai=t}registerSource(t,e){let s=t;return typeof t=="function"&&(e=t,s=`source.${K++}`),this._sources.push({id:s,query:e}),s}removeSource(t){this._sources=this.sources.filter(e=>e.id!==t&&e.query!==t)}addEntry(t){this.manualEntries.push(t)}removeEntry(t){this.manualEntries=this.manualEntries.filter(e=>e.id!==t)}get sources(){let t=this._sources;return f().knowledgeBaseSources&&(t=t.concat(f().knowledgeBaseSources)),t=t.concat(this._windowSources),t=t.filter(e=>!e.disabled),t}async search(t){v.debug(`Searching knowledge base for: ${t}`);let e=new Event("webweaver_kb_search",{bubbles:!0,cancelable:!0});e.query=t,e.entries=[],e.sources=[],typeof document<"u"&&document.dispatchEvent(e),this._windowSources=e.sources;let n=(await Promise.all(this.sources.map(async o=>{try{let r=Date.now(),a=await o.query(t);return v.debug(`Source '${o.id}' took ${Date.now()-r}ms`),a||[]}catch(r){return v.warn(`Knowledge source '${o.id}' failed:`,r),[]}}))).flat();n=n.concat(e.entries),n=n.concat(this.manualEntries),f().knowledgeBase&&(n=n.concat(f().knowledgeBase)),n=n.filter(o=>o&&!o.disabled);for(let o=0;o<n.length;o++){let r=n[o];r.id=r.id||`temp.${o}`,r._functionID=r.id.replaceAll(/[^a-zA-Z0-9_]/g,"_"),!r.action&&r.type=="tour"&&(r.action=a=>{throw new Error("This tour does not have an action. You must perform the tour content manually.")}),!r.action&&r.type=="info"&&(r.action=a=>{throw new Error("This item does not have an action. Use the content to provide information to the user instead.")})}let i=new ne({fields:["id","type","name","content","tags"],storeFields:[],searchOptions:{boost:{name:3,tags:2},fuzzy:.2}});i.addAll(n);let c=i.search(t).map(o=>n.find(r=>r.id==o.id));for(let o of n)o.isContext&&(c.find(r=>r.id===o.id)||c.push(o));return this.lastResults=c,v.debug("Found results:",c),c}getCachedEntry(t){return this.lastResults.find(e=>e.id==t||e._functionID==t)}registerSourceFromURL(t,e){e||(e=`external.${K++}`),v.debug(`Registering remote knowledge base source: ${t}`);let s=[],n=[],i=!0,l=async(o,r)=>{v.debug(`Calling remote knowledge base action: ${o.id}`);let a={type:"action",userID:this.ai.userID,extra:this.ai.extra,actionID:o.id,parameters:r},d=await fetch(t,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(a)});if(!d.ok)throw new Error(`HTTP Error ${d.status} ${d.statusText}`);let h=await d.json();return c(h.updateItems||[]),h.response},c=o=>{for(let r of o){if(!r.id){v.warn("KB item skipped since it has no ID.",r);continue}let a=s.find(d=>d.id==r.id);if(a){a.name=r.name||a.name||"",a.content=r.content||a.content||"",a.disabled=r.disabled??a.disabled,a.isContext=r.isContext??a.isContext,a.parameters=r.parameters||a.parameters||[],a.tags=r.tags||a.tags,a.type=r.type||a.type;continue}s.push({...r,action:d=>l(r,d)})}};this.registerSource(e,async o=>{if(i&&n.includes(o))return s;let r={type:"search",userID:this.ai?.userID||"",extra:this.ai?.extra||{},query:o},a=await fetch(t,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(r)});if(!a.ok)throw new Error(`HTTP Error ${a.status} ${a.statusText}`);let d=await a.json();return i=!d.noCache,n.includes(o)||n.push(o),c(d.items),s})}clone(){let t=new u(this.ai);return t._sources=this._sources,t._windowSources=this._windowSources,t.manualEntries=this.manualEntries,t}registerMCPSource(t){t.id||(t.id=`external.${K++}`);let e=new M(t);return this.registerSource(t.id,s=>e.search(s)),e}};import{v4 as D}from"uuid";var A=class{constructor(t){this.ai=t}async boolean(t){let e=await this.instruct({...t,instruction:`${t.instruction}
|
|
13
13
|
|
|
14
14
|
Return only the text "true" or "false" to indicate the answer to the question. Do not include any additional text or explanations.`});if(e.toLowerCase().includes("true"))return!0;if(e.toLowerCase().includes("false"))return!1;throw new Error("The AI did not give a boolean answer: "+e)}async choose(t){let e=await this.instruct({...t,instruction:`${t.instruction}
|
|
15
15
|
|
package/dist/react/react.js
CHANGED
|
@@ -9,7 +9,7 @@ import Te,{createContext as tt,useContext as it,useEffect as re,useMemo as st,us
|
|
|
9
9
|
`,yield*e()}function ye(){if(h().userID)return h().userID||"";if(typeof localStorage<"u"){let a=localStorage.getItem("intelliweave.uid")||"";return a||(a=ge(),localStorage.setItem("intelliweave.uid",a),a)}else return ge()}function be(a){let i=a;if(i?.type=="object"&&i.properties)return a;let e={type:"object",properties:{},required:[]};if(a&&Array.isArray(a)){let t=a;for(let s of t)e.properties[s.name]={type:s.type||"string",description:s.description||""}}return e}var N=class N{constructor(i){this.module="IntelliWeave";this.module=i}get debugEnabled(){return N.debug?!0:typeof window<"u"&&h().debug}log(...i){this.debugEnabled&&console.log(`[IntelliWeave > ${this.module}]`,...i)}debug(...i){this.debugEnabled&&console.debug(`[IntelliWeave > ${this.module}]`,...i)}info(...i){this.debugEnabled&&console.info(`[IntelliWeave > ${this.module}]`,...i)}warn(...i){console.warn(`[IntelliWeave > ${this.module}]`,...i)}error(...i){console.error(`[IntelliWeave > ${this.module}]`,...i)}timer(i,...e){let t=Date.now();return this.debug(`[${i} 0ms] Started`,...e),(...s)=>this.debug(`[${i} ${Math.floor(Date.now()-t)}ms]`,...s)}};N.debug=!1;var u=N;import{countTokens as A}from"gpt-tokenizer";import{v4 as Re}from"uuid";var K=class{constructor(){this.size=4096;this.groups=[]}createGroup(i){let e=new ce;return e.id=i,this.groups.push(e),e}group(i){return this.groups.find(e=>e.id===i)}countTokens(){return this.groups.reduce((i,e)=>i+e.tokenCount,0)}removeOverflow(){let i=this.countTokens(),e=this.groups.reduce((t,s)=>t+s.weight,0);for(;i>this.size;){let t=this.groups.slice().sort((n,o)=>{let r=Math.floor(n.weight/e*this.size),c=Math.floor(o.weight/e*this.size),l=n.tokenCount-r;return o.tokenCount-c-l}),s=this.removeOneItem(t);if(!s)throw new Error("Too many items in the token window that cannot be removed.");i-=s.tokenCount}}removeOneItem(i){for(let e of i){let t=e.items.findIndex(n=>!n.cannotRemove);if(t===-1)continue;let s=e.items[t];return e.items.splice(t,1),e.tokenCount-=s.tokenCount,s}return null}},ce=class{constructor(){this.id="";this.items=[];this.weight=1;this.tokenCount=0;this.separator=`
|
|
10
10
|
`;this.itemPadding=0;this.sortFunction=(i,e)=>i.sortOrder!==e.sortOrder?i.sortOrder-e.sortOrder:i.dateAdded!==e.dateAdded?i.dateAdded-e.dateAdded:i.content.localeCompare(e.content)}setItemPadding(i){return this.itemPadding=i,this.recalculateTokens(),this}sortBy(i){return this.sortFunction=i,this.items.sort(this.sortFunction),this}setSeparator(i){return this.separator==i?this:(this.separator=i,this.recalculateTokens(),this)}setWeight(i){return this.weight=i,this}recalculateTokens(){this.tokenCount=this.items.reduce((i,e)=>(e.tokenCount=A(e.content)+A(this.separator)+this.itemPadding,i+e.tokenCount),0)}add(i){typeof i=="string"&&(i={content:i});let e=i;e.id===void 0&&(e.id=Re());let t=this.items.find(s=>s.id===e.id);return t?(this.tokenCount-=t.tokenCount,Object.assign(t,e),e=t):this.items.push(e),e.dateAdded===void 0&&(e.dateAdded=Date.now()),e.sortOrder===void 0&&(e.sortOrder=0),e.disabled===void 0&&(e.disabled=!1),e.tokenCount=e.disabled?0:A(i.content)+A(this.separator)+this.itemPadding,this.tokenCount+=e.tokenCount,this.items.sort(this.sortFunction),e}getAllAsString(){return this.getAll().map(i=>i.content).join(this.separator)}getAll(){return this.items.filter(i=>!i.disabled)}empty(){this.items=[],this.tokenCount=0}};var C=1,v=new u("ChatGPT"),F=class{constructor(i){this.id="";this.metadata={};this.config={apiKey:"",endpoint:"",model:"gpt-4-turbo",systemMessage:"",userID:"",stream:!0,maxTokens:4096};this.maxToolCallsPerMessage=10;this.stats={tokensUsed:0};this.tokenWindow=(()=>{let i=new K;return i.createGroup("context").setSeparator(`
|
|
11
11
|
|
|
12
|
-
`),i.createGroup("actions").setItemPadding(25),i.createGroup("messages").setItemPadding(10),i})();this.config={...this.config,...i},this.tokenWindow.size=this.config.maxTokens||4096}get contextGroup(){return this.tokenWindow.group("context")}get toolGroup(){return this.tokenWindow.group("actions")}get messageGroup(){return this.tokenWindow.group("messages")}async sendMessage(i){let e={role:"user",content:i};this.messageGroup.add({id:"msg-"+C++,content:JSON.stringify(e),customData:e}),await this.processMessages();let s=this.messageGroup.items[this.messageGroup.items.length-1]?.customData;return s?.role=="assistant"&&s.content||""}addAssistantMessage(i){let e={role:"assistant",content:i};this.messageGroup.add({id:"msg-"+C++,content:JSON.stringify(e),customData:e})}addUserMessage(i){let e={role:"user",content:i};this.messageGroup.add({id:"msg-"+C++,content:JSON.stringify(e),customData:e})}getMessages(){return this.messageGroup.items.map(i=>i.customData)}getLatestMessage(){return this.messageGroup.items.length?this.messageGroup.items[this.messageGroup.items.length-1]?.customData:void 0}async processMessages(){await this.config.onBeforeMessageProcessing?.(),v.debugEnabled&&v.debug("Process message state:",{context:this.contextGroup.getAllAsString(),tools:this.toolGroup.getAll().map(t=>t.customData),messages:this.messageGroup.items.map(t=>t.customData),tokens:{used:this.tokenWindow.countTokens(),max:this.tokenWindow.size}});let e=this.messageGroup.items[this.messageGroup.items.length-1]?.customData;if(e?.role=="user"||e?.role=="tool")await this.sendToAPI(),await this.processMessages();else if(e?.role=="assistant"&&e?.tool_calls?.length){for(let t of e.tool_calls)await this.processToolCall(t);await this.processMessages()}}async trimMessages(){for(this.tokenWindow.removeOverflow();this.messageGroup.items.length&&this.messageGroup.items[0].customData?.role=="tool";)this.messageGroup.items.shift()}async sendToAPI(i){this.config.systemMessage&&this.contextGroup.add({id:"_gpt_context",cannotRemove:!0,sortOrder:0,content:this.config.systemMessage}),await this.trimMessages();let e=0;for(let c=this.messageGroup.items.length-1;c>=0;c--){let l=this.messageGroup.items[c].customData;if(l.role=="assistant"&&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(l.role=="user")break}v.debugEnabled&&v.debug("Before LLM state:",{context:this.contextGroup.getAllAsString(),tools:this.toolGroup.getAll().map(c=>c.customData),messages:this.messageGroup.items.map(c=>c.customData),tokens:{used:this.tokenWindow.countTokens(),max:this.tokenWindow.size}});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.contextGroup.getAllAsString()},...this.messageGroup.getAll().map(c=>c.customData)]};for(let c of this.toolGroup.getAll()){let l=c.customData;l.description&&l.description.length>1024&&v.warn(`Tool description for "${l.name}" is too long, it will be truncated.`);let d={type:"function",function:{name:l.name,description:(l.description||"").substring(0,1024),parameters:l.params}};t.tools.push(d)}t.user||delete t.user,t.tools.length||delete t.tools;let s={};if(s["Content-Type"]="application/json",this.config.apiKey&&(s.Authorization=`Bearer ${this.config.apiKey}`),i)return t;let n=await fetch(this.config.endpoint||"https://api.openai.com/v1/chat/completions",{method:"POST",headers:s,body:JSON.stringify(t)});if(!n.ok){let c=`${n.status} ${n.statusText}`;try{let d=await n.json();c=d.errorText||d.error?.message||d.error||c}catch{}let l=new Error(c);throw l.code=n.status,l}let o=null,r=null;if(this.config.stream)for await(let c of fe(n.body)){if(c.data=="[DONE]")break;if(!c.data)continue;let l=JSON.parse(c.data);if(r){for(let d in l.choices[0].delta)if(typeof l.choices[0].delta[d]=="string"){if(d=="role"&&r[d]==l.choices[0].delta[d])continue;r[d]=(r[d]||"")+l.choices[0].delta[d]}for(let d of l.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 m=r.tool_calls[d.index];m.function=m.function||{},m.function.name=(m.function.name||"")+(d.function?.name||""),m.function.arguments=(m.function.arguments||"")+(d.function?.arguments||"")}}else r={chunkID:l.id,...l.choices[0].delta};r?.content&&!r.content.startsWith("{")&&this.config.onAIMessage?.(r.content,!0)}else o=await n.json(),this.stats.tokensUsed+=o.usage?.total_tokens||0,v.debug(`Used ${o.usage?.total_tokens||0} tokens, total used: ${this.stats.tokensUsed}`),r=o.choices?.[0]?.message;if(r||(v.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.messageGroup.add({id:"msg-"+C++,content:JSON.stringify(r),customData:r}),r.content&&this.config.onAIMessage?.(r.content,!1)}processIncomingMessage(i){}registerTool(i){this.toolGroup.add({id:i.name,cannotRemove:!i.canRemove,content:JSON.stringify(i),customData:i})}async processToolCall(i){try{let t=this.toolGroup.items.find(r=>r.id==i.function.name)?.customData;if(!t)throw new Error(`Tool "${i.function.name}" not found.`);let s=JSON.parse(i.function.arguments);this.config.onAIToolStart?.(i.function.name,s);let n=await t.callback(s);typeof n!="string"&&(n=JSON.stringify(n)||""),(n===""||n==="undefined")&&(n="success");let o={role:"tool",content:n,tool_call_id:i.id};this.messageGroup.add({id:"msg-"+C++,content:JSON.stringify(o),customData:o})}catch(e){v.warn(`Unable to process tool call for "${i?.function?.name}"`,e);let t={role:"tool",content:`Error: ${e.message}`,tool_call_id:i.id};this.messageGroup.add({id:"msg-"+C++,content:JSON.stringify(t),customData:t})}}resetConversation(){this.messageGroup.empty(),this.toolGroup.empty(),this.contextGroup.empty()}};import{v4 as J}from"uuid";import Ne from"minisearch";var we=[{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(a,i)=>{let e=await i.knowledgeBase.search(a.query),t=i;t._lastKBsearch=a.query,t._nextRequestUseKBitems=e,i.submitAnalyticsEvent({type:"kb-search",query:a.query,results:e});let s=e.filter(o=>o.type!="action").map(o=>"id="+o.id).join(", ")||"(none)",n=e.filter(o=>o.type=="action").map(o=>"name="+o._functionID).join(", ")||"(none)";return`Search complete, context has been updated. New info entries found: ${s}. New tools available: ${n}.`}}];import{Client as ve}from"@modelcontextprotocol/sdk/client/index.js";import{StreamableHTTPClientTransport as Ee}from"@modelcontextprotocol/sdk/client/streamableHttp.js";import{SSEClientTransport as Le}from"@modelcontextprotocol/sdk/client/sse.js";var G={name:"@intelliweave/embedded",version:"1.8.61",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 && IW_TESTER_API_KEY=97150cfc-5b97-413e-8da3-e86d01f70057 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:{"@modelcontextprotocol/sdk":"^1.12.1","@types/json-schema":"^7.0.15","gpt-tokenizer":"^2.9.0",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","utility-types":"^3.11.0",uuid:"^10.0.0"}};import{LoggingMessageNotificationSchema as Pe,ToolListChangedNotificationSchema as Be}from"@modelcontextprotocol/sdk/types.js";var p=new u("MCPKnowledgeClient"),D=class{constructor(i){this.tools=[];this.iwActions=[];this.stats={toolsCalled:0};this.lastSearchQuery="";this.lastSearchResults=[];this.config=i}async connect(){return this.client?this.client:this.connectionPromise?await this.connectionPromise:(this.connectionPromise=this.connectInternal(),this.connectionPromise.finally(()=>{this.connectionPromise=void 0}),await this.connectionPromise)}async connectInternal(){p.debug("Connecting to MCP client");let i=this.config.connect?await this.config.connect():await Promise.resolve().then(async()=>{let e=new ve({name:G.name,version:G.version}),t=new Ee(new URL(this.config.baseURL));return await e.connect(t),p.debug("Connected with HTTP streaming mode"),e}).catch(async e=>{let t=new ve({name:G.name,version:G.version}),s=new Le(new URL(this.config.baseURL));return await t.connect(s),p.debug("Connected with SSE mode"),t});return await this.disconnect(),this.client=i,i.onerror=e=>{p.error(`MCP client error: ${e.message}`)},i.onclose=()=>{p.debug("MCP client connection closed"),this.client=void 0},i.setNotificationHandler(Pe,e=>{e.params.level=="critical"?p.error(`[Server] ${e.params.data}`):e.params.level=="emergency"?p.error(`[Server] ${e.params.data}`):e.params.level=="error"?p.error(`[Server] ${e.params.data}`):e.params.level=="warning"?p.warn(`[Server] ${e.params.data}`):e.params.level=="info"?p.info(`[Server] ${e.params.data}`):e.params.level=="debug"?p.debug(`[Server] ${e.params.data}`):p.log(`[Server] ${e.params.data}`)}),i.setNotificationHandler(Be,e=>{p.debug("Tool list changed",e),this.fetchTools()}),p.debug("Fetching tools from MCP server..."),await this.fetchTools(),i}async disconnect(){await this.client?.close(),this.client=void 0,this.tools=[],this.iwActions=[]}async fetchTools(){let i=[],e;for(;;){let s=await this.client.listTools({cursor:e});e=s.nextCursor;for(let n of s.tools||[])i.push(n);if(!s?.tools?.length||!e)break}let t=[];for(let s of i){let n=!!(this.config.searchToolName&&s.name==this.config.searchToolName&&!this.config.searchToolVisible);t.push({id:s.name,name:s.name,content:s.description||"",type:"action",isContext:!0,parameters:s.inputSchema,action:o=>this.performToolCall(s.name,o),disabled:n})}p.debug(`Fetched ${i.length} tools from MCP server.`),this.tools=i,this.iwActions=t}async search(i){if(i==this.lastSearchQuery)return this.lastSearchResults;await this.connect();let e=this.iwActions.slice(),t=await this.performSearchCall(i);return e=e.concat(t),this.lastSearchQuery=i,this.lastSearchResults=e,p.debug(`Search completed, found ${e.length} items.`),e}async performSearchCall(i){let e=[];if(!this.config.searchToolName)return e;let t=this.tools.find(n=>n.name==this.config.searchToolName);if(!t)return p.warn(`Search function ${this.config.searchToolName} not found on the MCP server.`),e;let s;if(t.inputSchema.required?.length==1){let n=t.inputSchema.required[0];s=await this.performToolCall(this.config.searchToolName,{[n]:i})}else if(t.inputSchema.properties&&Object.keys(t.inputSchema.properties).length==1){let n=Object.keys(t.inputSchema.properties)[0];s=await this.performToolCall(this.config.searchToolName,{[n]:i})}else s=await this.performToolCall(this.config.searchToolName,{});if(Array.isArray(s))for(let n=0;n<s.length;n++){let o=s[n];e.push({id:this.config.id+":"+this.config.searchToolName+":"+n,name:`Search result ${n+1} in ${this.config.searchToolName}`,type:"info",content:typeof o=="string"?o:JSON.stringify(o)})}else e.push({id:this.config.id+":"+this.config.searchToolName+":result",name:`Search result in ${this.config.searchToolName}`,type:"info",content:typeof s=="string"?s:JSON.stringify(s)});return e}async performToolCall(i,e){await this.connect(),p.debug(`Performing tool call for ${i} with params:`,e),this.stats.toolsCalled+=1;let t=await this.client.callTool({name:i,arguments:e});if(t.isError){let s=t.content?.[0]?.text||"Unknown error";throw p.error(`Error calling tool ${i}: ${s}`),new Error(`Error calling tool ${i}: ${s}`)}if(Array.isArray(t.content)&&t.content.length==1&&t.content[0].type=="text")try{return JSON.parse(t.content[0].text)}catch{}return t.content||[]}};var Z=new u("KnowledgeBase"),de=1,z=class a{constructor(i){this._sources=[{id:"core.internal",query:async()=>we}];this._windowSources=[];this.lastResults=[];this.manualEntries=[];this.ai=i}registerSource(i,e){let t=i;return typeof i=="function"&&(e=i,t=`source.${de++}`),this._sources.push({id:t,query:e}),t}removeSource(i){this._sources=this.sources.filter(e=>e.id!==i&&e.query!==i)}addEntry(i){this.manualEntries.push(i)}removeEntry(i){this.manualEntries=this.manualEntries.filter(e=>e.id!==i)}get sources(){let i=this._sources;return h().knowledgeBaseSources&&(i=i.concat(h().knowledgeBaseSources)),i=i.concat(this._windowSources),i=i.filter(e=>!e.disabled),i}async search(i){Z.debug(`Searching knowledge base for: ${i}`);let e=new Event("webweaver_kb_search",{bubbles:!0,cancelable:!0});e.query=i,e.entries=[],e.sources=[],typeof document<"u"&&document.dispatchEvent(e),this._windowSources=e.sources;let s=(await Promise.all(this.sources.map(async c=>{try{let l=Date.now(),d=await c.query(i);return Z.debug(`Source '${c.id}' took ${Date.now()-l}ms`),d||[]}catch(l){return Z.warn(`Knowledge source '${c.id}' failed:`,l),[]}}))).flat();s=s.concat(e.entries),s=s.concat(this.manualEntries),h().knowledgeBase&&(s=s.concat(h().knowledgeBase)),s=s.filter(c=>c&&!c.disabled);for(let c=0;c<s.length;c++){let l=s[c];l.id=l.id||`temp.${c}`,l._functionID=l.id.replaceAll(/[^a-zA-Z0-9_]/g,"_"),!l.action&&l.type=="tour"&&(l.action=d=>{throw new Error("This tour does not have an action. You must perform the tour content manually.")}),!l.action&&l.type=="info"&&(l.action=d=>{throw new Error("This item does not have an action. Use the content to provide information to the user instead.")})}let n=new Ne({fields:["id","type","name","content","tags"],storeFields:[],searchOptions:{boost:{name:3,tags:2},fuzzy:.2}});n.addAll(s);let r=n.search(i).map(c=>s.find(l=>l.id==c.id));for(let c of s)c.isContext&&(r.find(l=>l.id===c.id)||r.push(c));return this.lastResults=r,Z.debug("Found results:",r),r}getCachedEntry(i){return this.lastResults.find(e=>e.id==i||e._functionID==i)}registerSourceFromURL(i,e){e||(e=`external.${de++}`),Z.debug(`Registering remote knowledge base source: ${i}`);let t=[],s=[],n=!0,o=async(c,l)=>{Z.debug(`Calling remote knowledge base action: ${c.id}`);let d={type:"action",userID:this.ai.userID,extra:this.ai.extra,actionID:c.id,parameters:l},m=await fetch(i,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(d)});if(!m.ok)throw new Error(`HTTP Error ${m.status} ${m.statusText}`);let M=await m.json();return r(M.updateItems||[]),M.response},r=c=>{for(let l of c){if(!l.id){Z.warn("KB item skipped since it has no ID.",l);continue}let d=t.find(m=>m.id==l.id);if(d){d.name=l.name||d.name||"",d.content=l.content||d.content||"",d.disabled=l.disabled??d.disabled,d.isContext=l.isContext??d.isContext,d.parameters=l.parameters||d.parameters||[],d.tags=l.tags||d.tags,d.type=l.type||d.type;continue}t.push({...l,action:m=>o(l,m)})}};this.registerSource(e,async c=>{if(n&&s.includes(c))return t;let l={type:"search",userID:this.ai?.userID||"",extra:this.ai?.extra||{},query:c},d=await fetch(i,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(l)});if(!d.ok)throw new Error(`HTTP Error ${d.status} ${d.statusText}`);let m=await d.json();return n=!m.noCache,s.includes(c)||s.push(c),r(m.items),t})}clone(){let i=new a(this.ai);return i._sources=this._sources,i._windowSources=this._windowSources,i.manualEntries=this.manualEntries,i}registerMCPSource(i){i.id||(i.id=`external.${de++}`);let e=new D(i);return this.registerSource(i.id,t=>e.search(t)),e}};var Y=class{constructor(i){this.ai=i}async boolean(i){let e=await this.instruct({...i,instruction:`${i.instruction}
|
|
12
|
+
`),i.createGroup("actions").setItemPadding(25),i.createGroup("messages").setItemPadding(10),i})();this.config={...this.config,...i},this.tokenWindow.size=this.config.maxTokens||4096}get contextGroup(){return this.tokenWindow.group("context")}get toolGroup(){return this.tokenWindow.group("actions")}get messageGroup(){return this.tokenWindow.group("messages")}async sendMessage(i){let e={role:"user",content:i};this.messageGroup.add({id:"msg-"+C++,content:JSON.stringify(e),customData:e}),await this.processMessages();let s=this.messageGroup.items[this.messageGroup.items.length-1]?.customData;return s?.role=="assistant"&&s.content||""}addAssistantMessage(i){let e={role:"assistant",content:i};this.messageGroup.add({id:"msg-"+C++,content:JSON.stringify(e),customData:e})}addUserMessage(i){let e={role:"user",content:i};this.messageGroup.add({id:"msg-"+C++,content:JSON.stringify(e),customData:e})}getMessages(){return this.messageGroup.items.map(i=>i.customData)}getLatestMessage(){return this.messageGroup.items.length?this.messageGroup.items[this.messageGroup.items.length-1]?.customData:void 0}async processMessages(){await this.config.onBeforeMessageProcessing?.(),v.debugEnabled&&v.debug("Process message state:",{context:this.contextGroup.getAllAsString(),tools:this.toolGroup.getAll().map(t=>t.customData),messages:this.messageGroup.items.map(t=>t.customData),tokens:{used:this.tokenWindow.countTokens(),max:this.tokenWindow.size}});let e=this.messageGroup.items[this.messageGroup.items.length-1]?.customData;if(e?.role=="user"||e?.role=="tool")await this.sendToAPI(),await this.processMessages();else if(e?.role=="assistant"&&e?.tool_calls?.length){for(let t of e.tool_calls)await this.processToolCall(t);await this.processMessages()}}async trimMessages(){for(this.tokenWindow.removeOverflow();this.messageGroup.items.length&&this.messageGroup.items[0].customData?.role=="tool";)this.messageGroup.items.shift()}async sendToAPI(i){this.config.systemMessage&&this.contextGroup.add({id:"_gpt_context",cannotRemove:!0,sortOrder:0,content:this.config.systemMessage}),await this.trimMessages();let e=0;for(let c=this.messageGroup.items.length-1;c>=0;c--){let l=this.messageGroup.items[c].customData;if(l.role=="assistant"&&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(l.role=="user")break}v.debugEnabled&&v.debug("Before LLM state:",{context:this.contextGroup.getAllAsString(),tools:this.toolGroup.getAll().map(c=>c.customData),messages:this.messageGroup.items.map(c=>c.customData),tokens:{used:this.tokenWindow.countTokens(),max:this.tokenWindow.size}});let t={model:this.config.model,temperature:.2,user:this.config.userID,tools:[],stream:!!this.config.stream,messages:[{role:"system",content:this.contextGroup.getAllAsString()},...this.messageGroup.getAll().map(c=>c.customData)]};for(let c of this.toolGroup.getAll()){let l=c.customData;l.description&&l.description.length>1024&&v.warn(`Tool description for "${l.name}" is too long, it will be truncated.`);let d={type:"function",function:{name:l.name,description:(l.description||"").substring(0,1024),parameters:l.params}};t.tools.push(d)}t.user||delete t.user,t.tools.length||delete t.tools;let s={};if(s["Content-Type"]="application/json",this.config.apiKey&&(s.Authorization=`Bearer ${this.config.apiKey}`),i)return t;let n=await fetch(this.config.endpoint||"https://api.openai.com/v1/chat/completions",{method:"POST",headers:s,body:JSON.stringify(t)});if(!n.ok){let c=`${n.status} ${n.statusText}`;try{let d=await n.json();c=d.errorText||d.error?.message||d.error||c}catch{}let l=new Error(c);throw l.code=n.status,l}let o=null,r=null;if(this.config.stream)for await(let c of fe(n.body)){if(c.data=="[DONE]")break;if(!c.data)continue;let l=JSON.parse(c.data);if(r){for(let d in l.choices[0].delta)if(typeof l.choices[0].delta[d]=="string"){if(d=="role"&&r[d]==l.choices[0].delta[d])continue;r[d]=(r[d]||"")+l.choices[0].delta[d]}for(let d of l.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 m=r.tool_calls[d.index];m.function=m.function||{},m.function.name=(m.function.name||"")+(d.function?.name||""),m.function.arguments=(m.function.arguments||"")+(d.function?.arguments||"")}}else r={chunkID:l.id,...l.choices[0].delta};r?.content&&!r.content.startsWith("{")&&this.config.onAIMessage?.(r.content,!0)}else o=await n.json(),this.stats.tokensUsed+=o.usage?.total_tokens||0,v.debug(`Used ${o.usage?.total_tokens||0} tokens, total used: ${this.stats.tokensUsed}`),r=o.choices?.[0]?.message;if(r||(v.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.messageGroup.add({id:"msg-"+C++,content:JSON.stringify(r),customData:r}),r.content&&this.config.onAIMessage?.(r.content,!1)}processIncomingMessage(i){}registerTool(i){this.toolGroup.add({id:i.name,cannotRemove:!i.canRemove,content:JSON.stringify(i),customData:i})}async processToolCall(i){try{let t=this.toolGroup.items.find(r=>r.id==i.function.name)?.customData;if(!t)throw new Error(`Tool "${i.function.name}" not found.`);let s=JSON.parse(i.function.arguments);this.config.onAIToolStart?.(i.function.name,s);let n=await t.callback(s);typeof n!="string"&&(n=JSON.stringify(n)||""),(n===""||n==="undefined")&&(n="success");let o={role:"tool",content:n,tool_call_id:i.id};this.messageGroup.add({id:"msg-"+C++,content:JSON.stringify(o),customData:o})}catch(e){v.warn(`Unable to process tool call for "${i?.function?.name}"`,e);let t={role:"tool",content:`Error: ${e.message}`,tool_call_id:i.id};this.messageGroup.add({id:"msg-"+C++,content:JSON.stringify(t),customData:t})}}resetConversation(){this.messageGroup.empty(),this.toolGroup.empty(),this.contextGroup.empty()}};import{v4 as J}from"uuid";import Ne from"minisearch";var we=[{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(a,i)=>{let e=await i.knowledgeBase.search(a.query),t=i;t._lastKBsearch=a.query,t._nextRequestUseKBitems=e,i.submitAnalyticsEvent({type:"kb-search",query:a.query,results:e});let s=e.filter(o=>o.type!="action").map(o=>"id="+o.id).join(", ")||"(none)",n=e.filter(o=>o.type=="action").map(o=>"name="+o._functionID).join(", ")||"(none)";return`Search complete, context has been updated. New info entries found: ${s}. New tools available: ${n}.`}}];import{Client as ve}from"@modelcontextprotocol/sdk/client/index.js";import{StreamableHTTPClientTransport as Ee}from"@modelcontextprotocol/sdk/client/streamableHttp.js";import{SSEClientTransport as Le}from"@modelcontextprotocol/sdk/client/sse.js";var G={name:"@intelliweave/embedded",version:"1.8.62",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 && IW_TESTER_API_KEY=97150cfc-5b97-413e-8da3-e86d01f70057 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:{"@modelcontextprotocol/sdk":"^1.12.1","@types/json-schema":"^7.0.15","gpt-tokenizer":"^2.9.0",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","utility-types":"^3.11.0",uuid:"^10.0.0"}};import{LoggingMessageNotificationSchema as Pe,ToolListChangedNotificationSchema as Be}from"@modelcontextprotocol/sdk/types.js";var p=new u("MCPKnowledgeClient"),D=class{constructor(i){this.tools=[];this.iwActions=[];this.stats={toolsCalled:0};this.lastSearchQuery="";this.lastSearchResults=[];this.config=i}async connect(){return this.client?this.client:this.connectionPromise?await this.connectionPromise:(this.connectionPromise=this.connectInternal(),this.connectionPromise.finally(()=>{this.connectionPromise=void 0}),await this.connectionPromise)}async connectInternal(){p.debug("Connecting to MCP client");let i=this.config.connect?await this.config.connect():await Promise.resolve().then(async()=>{let e=new ve({name:G.name,version:G.version}),t=new Ee(new URL(this.config.baseURL));return await e.connect(t),p.debug("Connected with HTTP streaming mode"),e}).catch(async e=>{let t=new ve({name:G.name,version:G.version}),s=new Le(new URL(this.config.baseURL));return await t.connect(s),p.debug("Connected with SSE mode"),t});return await this.disconnect(),this.client=i,i.onerror=e=>{p.error(`MCP client error: ${e.message}`)},i.onclose=()=>{p.debug("MCP client connection closed"),this.client=void 0},i.setNotificationHandler(Pe,e=>{e.params.level=="critical"?p.error(`[Server] ${e.params.data}`):e.params.level=="emergency"?p.error(`[Server] ${e.params.data}`):e.params.level=="error"?p.error(`[Server] ${e.params.data}`):e.params.level=="warning"?p.warn(`[Server] ${e.params.data}`):e.params.level=="info"?p.info(`[Server] ${e.params.data}`):e.params.level=="debug"?p.debug(`[Server] ${e.params.data}`):p.log(`[Server] ${e.params.data}`)}),i.setNotificationHandler(Be,e=>{p.debug("Tool list changed",e),this.fetchTools()}),p.debug("Fetching tools from MCP server..."),await this.fetchTools(),i}async disconnect(){await this.client?.close(),this.client=void 0,this.tools=[],this.iwActions=[]}async fetchTools(){let i=[],e;for(;;){let s=await this.client.listTools({cursor:e});e=s.nextCursor;for(let n of s.tools||[])i.push(n);if(!s?.tools?.length||!e)break}let t=[];for(let s of i){let n=!!(this.config.searchToolName&&s.name==this.config.searchToolName&&!this.config.searchToolVisible);t.push({id:s.name,name:s.name,content:s.description||"",type:"action",isContext:!0,parameters:s.inputSchema,action:o=>this.performToolCall(s.name,o),disabled:n})}p.debug(`Fetched ${i.length} tools from MCP server.`),this.tools=i,this.iwActions=t}async search(i){if(i==this.lastSearchQuery)return this.lastSearchResults;await this.connect();let e=this.iwActions.slice(),t=await this.performSearchCall(i);return e=e.concat(t),this.lastSearchQuery=i,this.lastSearchResults=e,p.debug(`Search completed, found ${e.length} items.`),e}async performSearchCall(i){let e=[];if(!this.config.searchToolName)return e;let t=this.tools.find(n=>n.name==this.config.searchToolName);if(!t)return p.warn(`Search function ${this.config.searchToolName} not found on the MCP server.`),e;let s;if(t.inputSchema.required?.length==1){let n=t.inputSchema.required[0];s=await this.performToolCall(this.config.searchToolName,{[n]:i})}else if(t.inputSchema.properties&&Object.keys(t.inputSchema.properties).length==1){let n=Object.keys(t.inputSchema.properties)[0];s=await this.performToolCall(this.config.searchToolName,{[n]:i})}else s=await this.performToolCall(this.config.searchToolName,{});if(Array.isArray(s))for(let n=0;n<s.length;n++){let o=s[n];e.push({id:this.config.id+":"+this.config.searchToolName+":"+n,name:`Search result ${n+1} in ${this.config.searchToolName}`,type:"info",content:typeof o=="string"?o:JSON.stringify(o)})}else e.push({id:this.config.id+":"+this.config.searchToolName+":result",name:`Search result in ${this.config.searchToolName}`,type:"info",content:typeof s=="string"?s:JSON.stringify(s)});return e}async performToolCall(i,e){await this.connect(),p.debug(`Performing tool call for ${i} with params:`,e),this.stats.toolsCalled+=1;let t=await this.client.callTool({name:i,arguments:e});if(t.isError){let s=t.content?.[0]?.text||"Unknown error";throw p.error(`Error calling tool ${i}: ${s}`),new Error(`Error calling tool ${i}: ${s}`)}if(Array.isArray(t.content)&&t.content.length==1&&t.content[0].type=="text")try{return JSON.parse(t.content[0].text)}catch{}return t.content||[]}};var Z=new u("KnowledgeBase"),de=1,z=class a{constructor(i){this._sources=[{id:"core.internal",query:async()=>we}];this._windowSources=[];this.lastResults=[];this.manualEntries=[];this.ai=i}registerSource(i,e){let t=i;return typeof i=="function"&&(e=i,t=`source.${de++}`),this._sources.push({id:t,query:e}),t}removeSource(i){this._sources=this.sources.filter(e=>e.id!==i&&e.query!==i)}addEntry(i){this.manualEntries.push(i)}removeEntry(i){this.manualEntries=this.manualEntries.filter(e=>e.id!==i)}get sources(){let i=this._sources;return h().knowledgeBaseSources&&(i=i.concat(h().knowledgeBaseSources)),i=i.concat(this._windowSources),i=i.filter(e=>!e.disabled),i}async search(i){Z.debug(`Searching knowledge base for: ${i}`);let e=new Event("webweaver_kb_search",{bubbles:!0,cancelable:!0});e.query=i,e.entries=[],e.sources=[],typeof document<"u"&&document.dispatchEvent(e),this._windowSources=e.sources;let s=(await Promise.all(this.sources.map(async c=>{try{let l=Date.now(),d=await c.query(i);return Z.debug(`Source '${c.id}' took ${Date.now()-l}ms`),d||[]}catch(l){return Z.warn(`Knowledge source '${c.id}' failed:`,l),[]}}))).flat();s=s.concat(e.entries),s=s.concat(this.manualEntries),h().knowledgeBase&&(s=s.concat(h().knowledgeBase)),s=s.filter(c=>c&&!c.disabled);for(let c=0;c<s.length;c++){let l=s[c];l.id=l.id||`temp.${c}`,l._functionID=l.id.replaceAll(/[^a-zA-Z0-9_]/g,"_"),!l.action&&l.type=="tour"&&(l.action=d=>{throw new Error("This tour does not have an action. You must perform the tour content manually.")}),!l.action&&l.type=="info"&&(l.action=d=>{throw new Error("This item does not have an action. Use the content to provide information to the user instead.")})}let n=new Ne({fields:["id","type","name","content","tags"],storeFields:[],searchOptions:{boost:{name:3,tags:2},fuzzy:.2}});n.addAll(s);let r=n.search(i).map(c=>s.find(l=>l.id==c.id));for(let c of s)c.isContext&&(r.find(l=>l.id===c.id)||r.push(c));return this.lastResults=r,Z.debug("Found results:",r),r}getCachedEntry(i){return this.lastResults.find(e=>e.id==i||e._functionID==i)}registerSourceFromURL(i,e){e||(e=`external.${de++}`),Z.debug(`Registering remote knowledge base source: ${i}`);let t=[],s=[],n=!0,o=async(c,l)=>{Z.debug(`Calling remote knowledge base action: ${c.id}`);let d={type:"action",userID:this.ai.userID,extra:this.ai.extra,actionID:c.id,parameters:l},m=await fetch(i,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(d)});if(!m.ok)throw new Error(`HTTP Error ${m.status} ${m.statusText}`);let M=await m.json();return r(M.updateItems||[]),M.response},r=c=>{for(let l of c){if(!l.id){Z.warn("KB item skipped since it has no ID.",l);continue}let d=t.find(m=>m.id==l.id);if(d){d.name=l.name||d.name||"",d.content=l.content||d.content||"",d.disabled=l.disabled??d.disabled,d.isContext=l.isContext??d.isContext,d.parameters=l.parameters||d.parameters||[],d.tags=l.tags||d.tags,d.type=l.type||d.type;continue}t.push({...l,action:m=>o(l,m)})}};this.registerSource(e,async c=>{if(n&&s.includes(c))return t;let l={type:"search",userID:this.ai?.userID||"",extra:this.ai?.extra||{},query:c},d=await fetch(i,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(l)});if(!d.ok)throw new Error(`HTTP Error ${d.status} ${d.statusText}`);let m=await d.json();return n=!m.noCache,s.includes(c)||s.push(c),r(m.items),t})}clone(){let i=new a(this.ai);return i._sources=this._sources,i._windowSources=this._windowSources,i.manualEntries=this.manualEntries,i}registerMCPSource(i){i.id||(i.id=`external.${de++}`);let e=new D(i);return this.registerSource(i.id,t=>e.search(t)),e}};var Y=class{constructor(i){this.ai=i}async boolean(i){let e=await this.instruct({...i,instruction:`${i.instruction}
|
|
13
13
|
|
|
14
14
|
Return only the text "true" or "false" to indicate the answer to the question. Do not include any additional text or explanations.`});if(e.toLowerCase().includes("true"))return!0;if(e.toLowerCase().includes("false"))return!1;throw new Error("The AI did not give a boolean answer: "+e)}async choose(i){let e=await this.instruct({...i,instruction:`${i.instruction}
|
|
15
15
|
|