webdaemon 25.9.1 → 26.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +13 -11
- package/dist/index.js.map +4 -4
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/ts/GenerativeChatAgent.d.ts +3 -21
- package/dist/types/ts/GenerativeChatAgent.d.ts.map +1 -1
- package/dist/types/ts/GenerativeChatEvents.d.ts +65 -0
- package/dist/types/ts/GenerativeChatEvents.d.ts.map +1 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,24 +1,26 @@
|
|
|
1
|
-
async function Pt(o,t=0){let e=new TextEncoder().encode(o);if(!crypto.subtle)throw"Requires secure origin (localhost or https:)";let n=await crypto.subtle.digest("SHA-1",e),r=new Uint8Array(n),s=String.fromCodePoint(...r),a=btoa(s).replaceAll("+","-").replaceAll("/","_").replaceAll("=","");return t?a.substring(0,t):a}async function ke(o,t=0){let e=new TextEncoder().encode(o);if(!crypto.subtle)throw"Requires secure origin (localhost or https:)";let n=await crypto.subtle.digest("SHA-1",e),s=Array.from(new Uint8Array(n)).map(i=>i.toString(16).padStart(2,"0")).join("");return t?s.substring(0,t):s}async function b(o){return typeof o=="string"&&(o=new URL(o)),await Pt(String(o),8)}var A={name:"RSASSA-PKCS1-v1_5",hash:"SHA-256"},_=/https?:\/\/[^\/]+$/,bt=/^\/issuer\/[a-f0-9]{32}$/,At=1e3*30,p=class o{static Source={PARTY_CONTROL:"party:control"};static Counterparty={PUBLIC:"public"};static Capability={OPEN:"open",CLOSE:"close",GRANT:"grant",REVOKE:"revoke",OFFER:"offer",RETRACT:"retract",DELETE:"delete",CATALOG:"catalog",ALIAS:"alias",UNALIAS:"unalias",GET_ITEM:"getitem",SET_ITEM:"setitem",ALERT:"alert",LOG:"log",CHAT:"chat",READ_MEMORY:"read_memory",WRITE_MEMORY:"write_memory",READ:"read",WRITE:"write"};static DEFAULT_EXPIRY_MILLIS=1e3*60*60*24;static TOKEN_HEADER="x-tabserver-token";static SCOPE_SEPARATOR=/[\s,;\|]+/;static#t=1e3*60;static#e=2048;static#o=new Map;static#s=new Map;#n=null;#r=null;#c;#l;#u;#i;constructor(t){let e=null;if(typeof t=="object")e=t;else if(typeof t=="string")try{e=JSON.parse(atob(t))}catch{throw"Invalid token format"}else throw`Cannot construct token from ${typeof t}`;this.#r=e.sig,delete e.sig,this.#n=e;let{iss:n,aud:r,sub:s,src:i,scope:a,iat:c,exp:l}=e;if(!n||!r||!s||!i||!a||!c||!l)throw"Token must include iss, aud, sub, src, scope, iat and exp";if(!r.match(_)||!s.match(_))throw"The aud and sub attributes must be origins";if(this.#c=new URL(r),this.#l=new URL(s),this.#u=new URL(i),this.#u.hash)throw"The src attribute must have no fragment component.";this.#n=e}async signWith(t){if(this.#n==null)throw"No payload to sign";let e=await t,n=await crypto.subtle.importKey("jwk",e,A,!0,["sign"]),r=JSON.stringify(this.#n),s=new TextEncoder().encode(r).buffer,i=new Uint8Array(s),a=await crypto.subtle.sign(A,n,i),c=o.bytesToBase64(new Uint8Array(a));return this.#r=c,c}async verifySignatory(){let t=String(this.getIssuer()),{signatory:e,publicKey:n}=await o.#C(t);this.#i=e,this.#d(),await this.#p(n)}async fetchSignatory(){let t=String(this.getIssuer());this.#i=await o.#f(t),this.#d()}async checkSignature(t){if(!this.#n||!this.#r)throw"checkSignature: no payload or signatureBase64";let e=await t,n=await o.#y(e,String(this.getIssuer()));await this.#p(n)}async#p(t){if(!this.#n||!this.#r)throw"checkSignature: no payload or signatureBase64";let e=JSON.stringify(this.#n),n=new TextEncoder().encode(e),r=o.base64ToBytes(this.#r);if(!await crypto.subtle.verify(A,t,r,n))throw`Signature rejected by issuer ${this.getIssuer()}`}#d(){if(!this.#i)throw"validateSignatory: no signatory";let t=this.#i.src,e=this.getSrc(),n=this.getIssuer();if(n.origin!==this.getSub())throw`Token issuer origin '${n.origin}' should be token sub '${this.getSub()}'`;if(e===o.Source.PARTY_CONTROL){if(t!==o.Source.PARTY_CONTROL)throw`Token src '${e}' cannot be signed by signatory src '${t}'`}else if(this.#u.protocol==="http:"||this.#u.protocol==="https:"){if(t!==e&&t!==o.Source.PARTY_CONTROL)throw`Token src '${e}' cannot be signed by signatory src '${t}'`}else throw`Unsupported token src '${e}'`;if(t===o.Source.PARTY_CONTROL){if(!n.pathname.startsWith("/device/"))throw`Party control signatory issuer path '${n.pathname}' must start with /device/`;return}let r=new URL(t);if(r.protocol!=="http:"&&r.protocol!=="https:")throw`Invalid signatory src '${t}'`;if(!bt.test(n.pathname))throw`App signatory issuer path '${n.pathname}' must match /issuer/<appId>`}static async#C(t){let e=o.#g(t);if(e)return e;let n=o.#s.get(t);if(n)return await n;let r=(async()=>{let s=await o.#f(t),i=await o.#y(s.jwk,t),a={signatory:s,publicKey:i,expiresAt:Date.now()+o.#t};return o.#m(t,a),a})();o.#s.set(t,r);try{return await r}finally{o.#s.delete(t)}}static#g(t){let e=o.#o.get(t);return e?e.expiresAt<=Date.now()?(o.#o.delete(t),null):(o.#o.delete(t),o.#o.set(t,e),e):null}static#m(t,e){for(o.#o.has(t)&&o.#o.delete(t),o.#o.set(t,e);o.#o.size>o.#e;){let n=o.#o.keys().next().value;if(!n)break;console.log("Evicting token signatory cache entry due max size",{issuer:t,entryExpiresAt:e.expiresAt,cacheMaxEntries:o.#e}),o.#o.delete(n)}}static async#f(t){try{let n=await(await fetch(t,{headers:{"content-type":"application/json"}})).json();if("error"in n)throw n.error;let r=n.ok;if(!r||typeof r!="object"||!r.jwk||typeof r.jwk!="object")throw"Invalid signatory response";if(typeof r.src!="string")throw"Invalid signatory src";return r}catch(e){throw`Failed to read signatory at ${t}: ${e}`}}static async#y(t,e){if(!t||typeof t!="object"||"error"in t)throw`Missing or invalid public JWK at issuer ${e}`;try{return await crypto.subtle.importKey("jwk",t,A,!0,["verify"])}catch{throw`Missing or invalid public JWK at issuer ${e}`}}getSignedPayload(){if(!this.#r||!this.#n)throw"getSignedPayload: no signatureBase64 or payload";return{...this.#n,sig:this.#r}}getPayload(){if(!this.#n)throw"getPayload: no payload";return this.#n}getAud(){return this.#c.origin}getParty(){return this.#c.host}getSub(){return this.#l.origin}getSrc(){return String(this.#u)}getCounterparty(){return this.#l.host}getSignatorySrc(){if(!this.#i)throw"getSignatorySrc: no signatory";return this.#i.src}getSourceUrl(){return this.#u}getIssuer(){return new URL(this.getPayload().iss)}getSources(){if(!this.#n)throw"getSources: no payload";let t=[];for(let e in this.#n.scope)t.push(e);return t}getCapabilities(t){if(!this.#n)throw"getCapabilities: no payload";let e=this.#n.scope;if(!(t in e))throw`Source '${t}' not in scope`;return e[t].split(o.SCOPE_SEPARATOR)}hasCapability(t,e){return this.getCapabilities(t).includes(e)}checkPeriod(){if(!this.#n)throw"checkPeriod: no payload";let{iat:t,exp:e}=this.#n,n=Date.now();if(n<t-At)throw"Token is not yet valid";if(n>e)throw"Token has expired"}asSignedBase64(){return btoa(JSON.stringify(this.getSignedPayload()))}static bytesToBase64(t){let e=Array.from(t,n=>String.fromCodePoint(n)).join("");return btoa(e)}static base64ToBytes(t){let e=atob(t);return Uint8Array.from(e,n=>n.codePointAt(0)??0)}static from(t){let e=t.headers.get(o.TOKEN_HEADER);return e?new o(e):null}static toSearchString(t){let e=new URLSearchParams;for(let n in t){let s=t[n].asSignedBase64();e.append(n,s)}return e.toString()}};var kt=/^[\w\-.:]+(?:\/[\w\-.:]+)*$/,Ot=/^[\w\-.:%_]+(?:\/[\w\-.:%_]+)*$/,T=class{#t;constructor(t){this.#t=t}getItem(t){return It(this.#t,t)}getItemsLike(t){return Jt(this.#t,t)}setItem(t,e){return Nt(this.#t,t,e)}removeItem(t){return Mt(this.#t,t)}};function D(o){if(o.match(kt)==null)throw`DaemonStorage: Invalid key: '${o}`}function vt(o){if(o.match(Ot)==null)throw`DaemonStorage: Invalid like key: ${o}`}function Et(o){try{return JSON.stringify(o)}catch{throw"DaemonStorage: Invalid value"}}async function Nt(o,t,e){D(t);let n=Et(e),r=`${o.getAud()}/storage/${t}`;return(await fetch(r,{method:"PUT",headers:{"X-Tabserver-Token":o.asSignedBase64(),"Content-Type":"application/json"},body:n})).json()}async function It(o,t){D(t);let e=`${o.getAud()}/storage/${t}`;return(await fetch(e,{method:"GET",headers:{"X-Tabserver-Token":o.asSignedBase64()}})).json()}async function Jt(o,t){vt(t);let e=new URL(`${o.getAud()}/storage`);return e.searchParams.append("like",t),await(await fetch(e,{method:"GET",headers:{"X-Tabserver-Token":o.asSignedBase64()}})).json()}async function Mt(o,t){D(t);let e=`${o.getAud()}/storage/${t}`;return(await fetch(e,{method:"DELETE",headers:{"X-Tabserver-Token":o.asSignedBase64()}})).json()}var Y={Ash:"\xE6",Hash:"#"},$t={Ash:encodeURIComponent("\xE6")},k=class{params=new URLSearchParams;extractHash(t){t=t.startsWith(Y.Hash)?t.substring(1):t;let e=t.indexOf($t.Ash);if(e===-1)return t;let n=new URLSearchParams(t.slice(e));return this.extract(n),t.slice(0,e)}extractQuery(t){let e=new URLSearchParams(t);return this.extract(e),e.toString()}extract(t){let e=Array.from(t.keys());for(let n of e)n.startsWith(Y.Ash)&&(this.params.set(n.substring(1),t.get(n)??""),t.delete(n))}getParams(){return this.params}};var O="party",q="prefix",Ut="lastKnownDaemon",y=class o{static#t;#e;#o;#s;#n;static async getInstance(t=void 0){if(!t&&!o.#t)throw"Must provide app name";if(t&&o.#t&&t!==o.#t.#e)throw`Cannot change app name from ${o.#t.#e} to ${t}`;if(t&&!o.#t&&(o.#t=new o(t),await o.#t.init()),!o.#t)throw"No instance of BrowserApp";return o.#t}constructor(t){this.#e=t,this.#o=`${t}Party`,this.#s=`${t}Tokens`,this.#n=`${t}AppUrl`}async init(){let t=new URL(globalThis.location.href),e=new k,n=e.extractHash(t.hash),r=e.extractQuery(t.search),s=e.getParams();if(s.has(O)){sessionStorage[this.#s]=s.toString();let i=this.getToken();await i.verifySignatory(),sessionStorage[this.#o]=i.getParty(),localStorage.setItem(Ut,i.getParty());let a=i.getSourceUrl();if(a.origin!==t.origin||a.pathname!==t.pathname||a.search!==t.search)throw`Party token is for different app: ${a}`}return t.hash=n,t.search=r,(s.has(O)||sessionStorage[this.#n]===void 0)&&(sessionStorage[this.#n]=t.toString()),globalThis.history.replaceState(null,"",t.toString()),this}isOrphan(){return sessionStorage[this.#o]===void 0||sessionStorage[this.#s]===void 0}getAppName(){return this.#e}getAppUrl(){return sessionStorage[this.#n]}getParty(){return sessionStorage[this.#o]}getPartyOrigin(){return`${globalThis.location.protocol}//${this.getParty()}`}getDefaultPrefix(t=null){t||(t=this.getAppUrl());let e=new URL(t);return e.hash="",b(e)}getPrefix(){return this.hasParam(q)?Promise.resolve(this.getParam(q)??""):this.getDefaultPrefix()}async getAgentUrl(t){let e=this.getPartyOrigin(),n=await this.getPrefix(),r=`${e}/tab/${n}`;return t?`${r}/${t}`:r}async getAgentEndpoint(t){let{party:e=this.getParty(),appUrl:n=this.getAppUrl(),prefix:r=await this.getDefaultPrefix(n),tab:s}=t,a=`${new URL(n).protocol}//${e}/tab/${r}`;return s?`${a}/${s}`:a}getTokenBase64(t=O){let e=sessionStorage[this.#s],r=new URLSearchParams(e).get(t);if(!r)throw`No token for ${t}, check 'audience' in YML`;return r}getToken(t=O){let e=this.getTokenBase64(t);return new p(e)}getParam(t){let e=new URL(globalThis.location.href).searchParams;for(let[r,s]of e)if(t.toLowerCase()==r.toLowerCase())return s;return document.querySelector("link[rel=webdaemon]")?.getAttribute(t)??null}hasParam(t){return new URL(globalThis.location.href).searchParams.has(t)?!0:document.querySelector("link[rel=webdaemon]")?.hasAttribute(t)??!1}async getLaunchAlert(){let t=this.getPartyOrigin(),e=new URL(`${t}/catalog`);e.searchParams.append("alert","");let r=await(await fetch(e,{headers:{"X-Tabserver-Token":this.getTokenBase64(),Accept:"application/json"}})).json();if("error"in r)throw r.error;return r.ok.alert.find(i=>i.type=="LAUNCH"&&i.sourceUrl==new URL(this.getAppUrl()))||null}async resolveLaunchAlert(){let t=this.getPartyOrigin(),e=new URL(`${t}/resolve`),r=await(await fetch(e,{method:"POST",headers:{"X-Tabserver-Token":this.getTokenBase64(),"Content-Type":"application/json"},body:JSON.stringify({type:"LAUNCH",source:this.getAppUrl()})})).json();if("error"in r)throw r.error}async rejectLaunchAlert(){let t=this.getPartyOrigin(),e=new URL(`${t}/reject`),r=await(await fetch(e,{method:"POST",headers:{"X-Tabserver-Token":this.getTokenBase64(),"Content-Type":"application/json"},body:JSON.stringify({type:"LAUNCH",source:this.getAppUrl()})})).json();if("error"in r)throw r.error}getStorage(){return new T(this.getToken())}};var Lt="agent",v=class{#t=new Map;register(t,e,n){return this.#t.set(t,{descriptor:e,execute:n}),this}descriptors(){return[...this.#t.values()].map(t=>t.descriptor)}async executeToolCalls(t,e={}){let n=[];for(let r of t){let s=this.#t.get(r.name);if(!s){n.push({toolCallId:r.id,name:r.name,content:`No client tool named ${r.name}`,isError:!0});continue}try{let i=await s.execute(r.input||{},e);n.push({toolCallId:r.id,name:r.name,content:JSON.stringify(i)})}catch(i){n.push({toolCallId:r.id,name:r.name,content:String(i),isError:!0})}}return n}},W=class{#t;#e;#o;#s=null;#n;constructor(t={}){this.#t=t.app,this.#e=t.appName,this.#o=t.agentTab??Lt,this.#n=t.clientTools??new v}get app(){return this.#t??null}get agentUrl(){return this.#s}get clientTools(){return this.#n}async init(){return this.#t??=await y.getInstance(this.#e),this.#t.isOrphan()||(this.#s=await this.#t.getAgentUrl(this.#o)),this}isOrphan(){return!!this.#t?.isOrphan()}getParty(){return this.#r().getParty()}listChats({targetParty:t}){return this.postAgent("/chats",{targetParty:t})}listModels({targetParty:t}){return this.postAgent("/models",{targetParty:t})}createSession(t){return this.postAgent("/session",{targetParty:t.targetParty,...t.chatId?{chatId:t.chatId}:{},systemPrompt:t.systemPrompt,temperature:t.temperature,...t.model?{model:t.model}:{},clientTools:this.#n.descriptors()})}async sendMessage(t){let e=await this.postAgent("/send",{targetParty:t.targetParty,chatId:t.chatId,message:t.message,temperature:t.temperature,toolsEnabled:t.toolsEnabled,toolDetails:t.toolDetails});for(await t.onConversationResult?.(e);e.clientToolCalls?.length;){await t.onClientToolCalls?.(e.clientToolCalls);let n=await this.#n.executeToolCalls(e.clientToolCalls,{targetParty:t.targetParty});e=await this.sendClientToolResults({targetParty:t.targetParty,chatId:t.chatId,toolResults:n,temperature:t.temperature,toolsEnabled:t.toolsEnabled,toolDetails:t.toolDetails}),await t.onConversationResult?.(e)}return e}sendClientToolResults(t){return this.postAgent("/client-tool-results",{targetParty:t.targetParty,chatId:t.chatId,toolResults:t.toolResults,temperature:t.temperature,toolsEnabled:t.toolsEnabled,toolDetails:t.toolDetails})}closeChat({targetParty:t,chatId:e}){return this.postAgent("/close",{targetParty:t,chatId:e})}deleteChat({targetParty:t,chatId:e}){return this.postAgent("/delete",{targetParty:t,chatId:e})}async postAgent(t,e){if(!this.#s)throw"Agent URL is not available";let r=await(await fetch(`${this.#s}${t}`,{method:"POST",headers:{"Content-Type":"application/json","X-Tabserver-Token":this.#r().getTokenBase64()},body:JSON.stringify(e)})).json();if("error"in r)throw r.error;return r.ok}normalizeTargetParty(t){let e=String(t??"").trim();if(!e)throw"Daemon host is required";try{let n=e.includes("://")?new URL(e):new URL(`${globalThis.location.protocol}//${e}`);if(!n.host)throw"missing host";return n.host}catch{throw`Invalid daemon host: ${t}`}}#r(){if(!this.#t)throw"Browser app is not initialised";return this.#t}};function je(o=void 0){let t=new v;return t.register("browser_daemon_identity",{type:"function",name:"browser_daemon_identity",execution:"client",description:"Get the active chat daemon and the counterparty it is talking to, derived from the browser launch token and active chat target.",parameters:{type:"object",properties:{},additionalProperties:!1}},async(e,n={})=>{let s=(o??await y.getInstance()).getToken(),i=n.targetParty||s.getParty();return{aud:n.targetAud||`${globalThis.location.protocol}//${i}`,sub:s.getSub(),party:i,counterparty:s.getCounterparty(),src:s.getSrc()}}),t.register("browser_language",{type:"function",name:"browser_language",execution:"client",description:"Get the browser language preferences for this page.",parameters:{type:"object",properties:{},additionalProperties:!1}},()=>({language:navigator.language,languages:Array.from(navigator.languages??[])})),t.register("browser_time",{type:"function",name:"browser_time",execution:"client",description:"Get the current timezone and local time from the browser.",parameters:{type:"object",properties:{},additionalProperties:!1}},()=>{let e=new Date,n=Intl.DateTimeFormat().resolvedOptions().timeZone;return{timeZone:n,isoTime:e.toISOString(),localTime:e.toLocaleString(void 0,{timeZone:n}),offsetMinutes:-e.getTimezoneOffset()}}),t}var jt={name:"RSASSA-PKCS1-v1_5",modulusLength:2048,publicExponent:new Uint8Array([1,0,1]),hash:"SHA-256"},X=class{#t;#e;constructor(t=jt){if(this.#t=t,!crypto.subtle)throw"Crypto.subtle requires secure environment"}async generate(){this.#e=await crypto.subtle.generateKey(this.#t,!0,["sign","verify"])}async publicJwk(){if(!this.#e)throw"Must generate keypair";let t=this.#e.publicKey;return await crypto.subtle.exportKey("jwk",t)}async privateJwk(){if(!this.#e)throw"Must generate keypair";let t=this.#e.privateKey;return await crypto.subtle.exportKey("jwk",t)}async publicPem(){if(!this.#e)throw"Must generate keypair";let t=this.#e.publicKey,e=await crypto.subtle.exportKey("spki",t);return`-----BEGIN PUBLIC KEY-----
|
|
1
|
+
async function Ot(o,t=0){let e=new TextEncoder().encode(o);if(!crypto.subtle)throw"Requires secure origin (localhost or https:)";let n=await crypto.subtle.digest("SHA-1",e),r=new Uint8Array(n),s=String.fromCodePoint(...r),a=btoa(s).replaceAll("+","-").replaceAll("/","_").replaceAll("=","");return t?a.substring(0,t):a}async function Ee(o,t=0){let e=new TextEncoder().encode(o);if(!crypto.subtle)throw"Requires secure origin (localhost or https:)";let n=await crypto.subtle.digest("SHA-1",e),s=Array.from(new Uint8Array(n)).map(i=>i.toString(16).padStart(2,"0")).join("");return t?s.substring(0,t):s}async function A(o){return typeof o=="string"&&(o=new URL(o)),await Ot(String(o),8)}var k={name:"RSASSA-PKCS1-v1_5",hash:"SHA-256"},X=/https?:\/\/[^\/]+$/,vt=/^\/issuer\/[a-f0-9]{32}$/,Et=1e3*30,p=class o{static Source={PARTY_CONTROL:"party:control"};static Counterparty={PUBLIC:"public"};static Capability={OPEN:"open",CLOSE:"close",GRANT:"grant",REVOKE:"revoke",OFFER:"offer",RETRACT:"retract",DELETE:"delete",CATALOG:"catalog",ALIAS:"alias",UNALIAS:"unalias",GET_ITEM:"getitem",SET_ITEM:"setitem",ALERT:"alert",LOG:"log",CHAT:"chat",READ_MEMORY:"read_memory",WRITE_MEMORY:"write_memory",READ:"read",WRITE:"write"};static DEFAULT_EXPIRY_MILLIS=1e3*60*60*24;static TOKEN_HEADER="x-tabserver-token";static SCOPE_SEPARATOR=/[\s,;\|]+/;static#t=1e3*60;static#e=2048;static#o=new Map;static#s=new Map;#n=null;#r=null;#l;#a;#u;#h;constructor(t){let e=null;if(typeof t=="object")e=t;else if(typeof t=="string")try{e=JSON.parse(atob(t))}catch{throw"Invalid token format"}else throw`Cannot construct token from ${typeof t}`;this.#r=e.sig,delete e.sig,this.#n=e;let{iss:n,aud:r,sub:s,src:i,scope:a,iat:c,exp:l}=e;if(!n||!r||!s||!i||!a||!c||!l)throw"Token must include iss, aud, sub, src, scope, iat and exp";if(!r.match(X)||!s.match(X))throw"The aud and sub attributes must be origins";if(this.#l=new URL(r),this.#a=new URL(s),this.#u=new URL(i),this.#u.hash)throw"The src attribute must have no fragment component.";this.#n=e}async signWith(t){if(this.#n==null)throw"No payload to sign";let e=await t,n=await crypto.subtle.importKey("jwk",e,k,!0,["sign"]),r=JSON.stringify(this.#n),s=new TextEncoder().encode(r).buffer,i=new Uint8Array(s),a=await crypto.subtle.sign(k,n,i),c=o.bytesToBase64(new Uint8Array(a));return this.#r=c,c}async verifySignatory(){let t=String(this.getIssuer()),{signatory:e,publicKey:n}=await o.#T(t);this.#h=e,this.#m(),await this.#i(n)}async fetchSignatory(){let t=String(this.getIssuer());this.#h=await o.#y(t),this.#m()}async checkSignature(t){if(!this.#n||!this.#r)throw"checkSignature: no payload or signatureBase64";let e=await t,n=await o.#g(e,String(this.getIssuer()));await this.#i(n)}async#i(t){if(!this.#n||!this.#r)throw"checkSignature: no payload or signatureBase64";let e=JSON.stringify(this.#n),n=new TextEncoder().encode(e),r=o.base64ToBytes(this.#r);if(!await crypto.subtle.verify(k,t,r,n))throw`Signature rejected by issuer ${this.getIssuer()}`}#m(){if(!this.#h)throw"validateSignatory: no signatory";let t=this.#h.src,e=this.getSrc(),n=this.getIssuer();if(n.origin!==this.getSub())throw`Token issuer origin '${n.origin}' should be token sub '${this.getSub()}'`;if(e===o.Source.PARTY_CONTROL){if(t!==o.Source.PARTY_CONTROL)throw`Token src '${e}' cannot be signed by signatory src '${t}'`}else if(this.#u.protocol==="http:"||this.#u.protocol==="https:"){if(t!==e&&t!==o.Source.PARTY_CONTROL)throw`Token src '${e}' cannot be signed by signatory src '${t}'`}else throw`Unsupported token src '${e}'`;if(t===o.Source.PARTY_CONTROL){if(!n.pathname.startsWith("/device/"))throw`Party control signatory issuer path '${n.pathname}' must start with /device/`;return}let r=new URL(t);if(r.protocol!=="http:"&&r.protocol!=="https:")throw`Invalid signatory src '${t}'`;if(!vt.test(n.pathname))throw`App signatory issuer path '${n.pathname}' must match /issuer/<appId>`}static async#T(t){let e=o.#f(t);if(e)return e;let n=o.#s.get(t);if(n)return await n;let r=(async()=>{let s=await o.#y(t),i=await o.#g(s.jwk,t),a={signatory:s,publicKey:i,expiresAt:Date.now()+o.#t};return o.#C(t,a),a})();o.#s.set(t,r);try{return await r}finally{o.#s.delete(t)}}static#f(t){let e=o.#o.get(t);return e?e.expiresAt<=Date.now()?(o.#o.delete(t),null):(o.#o.delete(t),o.#o.set(t,e),e):null}static#C(t,e){for(o.#o.has(t)&&o.#o.delete(t),o.#o.set(t,e);o.#o.size>o.#e;){let n=o.#o.keys().next().value;if(!n)break;console.log("Evicting token signatory cache entry due max size",{issuer:t,entryExpiresAt:e.expiresAt,cacheMaxEntries:o.#e}),o.#o.delete(n)}}static async#y(t){try{let n=await(await fetch(t,{headers:{"content-type":"application/json"}})).json();if("error"in n)throw n.error;let r=n.ok;if(!r||typeof r!="object"||!r.jwk||typeof r.jwk!="object")throw"Invalid signatory response";if(typeof r.src!="string")throw"Invalid signatory src";return r}catch(e){throw`Failed to read signatory at ${t}: ${e}`}}static async#g(t,e){if(!t||typeof t!="object"||"error"in t)throw`Missing or invalid public JWK at issuer ${e}`;try{return await crypto.subtle.importKey("jwk",t,k,!0,["verify"])}catch{throw`Missing or invalid public JWK at issuer ${e}`}}getSignedPayload(){if(!this.#r||!this.#n)throw"getSignedPayload: no signatureBase64 or payload";return{...this.#n,sig:this.#r}}getPayload(){if(!this.#n)throw"getPayload: no payload";return this.#n}getAud(){return this.#l.origin}getParty(){return this.#l.host}getSub(){return this.#a.origin}getSrc(){return String(this.#u)}getCounterparty(){return this.#a.host}getSignatorySrc(){if(!this.#h)throw"getSignatorySrc: no signatory";return this.#h.src}getSourceUrl(){return this.#u}getIssuer(){return new URL(this.getPayload().iss)}getSources(){if(!this.#n)throw"getSources: no payload";let t=[];for(let e in this.#n.scope)t.push(e);return t}getCapabilities(t){if(!this.#n)throw"getCapabilities: no payload";let e=this.#n.scope;if(!(t in e))throw`Source '${t}' not in scope`;return e[t].split(o.SCOPE_SEPARATOR)}hasCapability(t,e){return this.getCapabilities(t).includes(e)}checkPeriod(){if(!this.#n)throw"checkPeriod: no payload";let{iat:t,exp:e}=this.#n,n=Date.now();if(n<t-Et)throw"Token is not yet valid";if(n>e)throw"Token has expired"}asSignedBase64(){return btoa(JSON.stringify(this.getSignedPayload()))}static bytesToBase64(t){let e=Array.from(t,n=>String.fromCodePoint(n)).join("");return btoa(e)}static base64ToBytes(t){let e=atob(t);return Uint8Array.from(e,n=>n.codePointAt(0)??0)}static from(t){let e=t.headers.get(o.TOKEN_HEADER);return e?new o(e):null}static toSearchString(t){let e=new URLSearchParams;for(let n in t){let s=t[n].asSignedBase64();e.append(n,s)}return e.toString()}};var Nt=/^[\w\-.:]+(?:\/[\w\-.:]+)*$/,It=/^[\w\-.:%_]+(?:\/[\w\-.:%_]+)*$/,C=class{#t;constructor(t){this.#t=t}getItem(t){return Ut(this.#t,t)}getItemsLike(t){return Lt(this.#t,t)}setItem(t,e){return $t(this.#t,t,e)}removeItem(t){return jt(this.#t,t)}};function G(o){if(o.match(Nt)==null)throw`DaemonStorage: Invalid key: '${o}`}function Jt(o){if(o.match(It)==null)throw`DaemonStorage: Invalid like key: ${o}`}function Mt(o){try{return JSON.stringify(o)}catch{throw"DaemonStorage: Invalid value"}}async function $t(o,t,e){G(t);let n=Mt(e),r=`${o.getAud()}/storage/${t}`;return(await fetch(r,{method:"PUT",headers:{"X-Tabserver-Token":o.asSignedBase64(),"Content-Type":"application/json"},body:n})).json()}async function Ut(o,t){G(t);let e=`${o.getAud()}/storage/${t}`;return(await fetch(e,{method:"GET",headers:{"X-Tabserver-Token":o.asSignedBase64()}})).json()}async function Lt(o,t){Jt(t);let e=new URL(`${o.getAud()}/storage`);return e.searchParams.append("like",t),await(await fetch(e,{method:"GET",headers:{"X-Tabserver-Token":o.asSignedBase64()}})).json()}async function jt(o,t){G(t);let e=`${o.getAud()}/storage/${t}`;return(await fetch(e,{method:"DELETE",headers:{"X-Tabserver-Token":o.asSignedBase64()}})).json()}var q={Ash:"\xE6",Hash:"#"},Dt={Ash:encodeURIComponent("\xE6")},O=class{params=new URLSearchParams;extractHash(t){t=t.startsWith(q.Hash)?t.substring(1):t;let e=t.indexOf(Dt.Ash);if(e===-1)return t;let n=new URLSearchParams(t.slice(e));return this.extract(n),t.slice(0,e)}extractQuery(t){let e=new URLSearchParams(t);return this.extract(e),e.toString()}extract(t){let e=Array.from(t.keys());for(let n of e)n.startsWith(q.Ash)&&(this.params.set(n.substring(1),t.get(n)??""),t.delete(n))}getParams(){return this.params}};var v="party",z="prefix",Gt="lastKnownDaemon",y=class o{static#t;#e;#o;#s;#n;static async getInstance(t=void 0){if(!t&&!o.#t)throw"Must provide app name";if(t&&o.#t&&t!==o.#t.#e)throw`Cannot change app name from ${o.#t.#e} to ${t}`;if(t&&!o.#t&&(o.#t=new o(t),await o.#t.init()),!o.#t)throw"No instance of BrowserApp";return o.#t}constructor(t){this.#e=t,this.#o=`${t}Party`,this.#s=`${t}Tokens`,this.#n=`${t}AppUrl`}async init(){let t=new URL(globalThis.location.href),e=new O,n=e.extractHash(t.hash),r=e.extractQuery(t.search),s=e.getParams();if(s.has(v)){sessionStorage[this.#s]=s.toString();let i=this.getToken();await i.verifySignatory(),sessionStorage[this.#o]=i.getParty(),localStorage.setItem(Gt,i.getParty());let a=i.getSourceUrl();if(a.origin!==t.origin||a.pathname!==t.pathname||a.search!==t.search)throw`Party token is for different app: ${a}`}return t.hash=n,t.search=r,(s.has(v)||sessionStorage[this.#n]===void 0)&&(sessionStorage[this.#n]=t.toString()),globalThis.history.replaceState(null,"",t.toString()),this}isOrphan(){return sessionStorage[this.#o]===void 0||sessionStorage[this.#s]===void 0}getAppName(){return this.#e}getAppUrl(){return sessionStorage[this.#n]}getParty(){return sessionStorage[this.#o]}getPartyOrigin(){return`${globalThis.location.protocol}//${this.getParty()}`}getDefaultPrefix(t=null){t||(t=this.getAppUrl());let e=new URL(t);return e.hash="",A(e)}getPrefix(){return this.hasParam(z)?Promise.resolve(this.getParam(z)??""):this.getDefaultPrefix()}async getAgentUrl(t){let e=this.getPartyOrigin(),n=await this.getPrefix(),r=`${e}/tab/${n}`;return t?`${r}/${t}`:r}async getAgentEndpoint(t){let{party:e=this.getParty(),appUrl:n=this.getAppUrl(),prefix:r=await this.getDefaultPrefix(n),tab:s}=t,a=`${new URL(n).protocol}//${e}/tab/${r}`;return s?`${a}/${s}`:a}getTokenBase64(t=v){let e=sessionStorage[this.#s],r=new URLSearchParams(e).get(t);if(!r)throw`No token for ${t}, check 'audience' in YML`;return r}getToken(t=v){let e=this.getTokenBase64(t);return new p(e)}getParam(t){let e=new URL(globalThis.location.href).searchParams;for(let[r,s]of e)if(t.toLowerCase()==r.toLowerCase())return s;return document.querySelector("link[rel=webdaemon]")?.getAttribute(t)??null}hasParam(t){return new URL(globalThis.location.href).searchParams.has(t)?!0:document.querySelector("link[rel=webdaemon]")?.hasAttribute(t)??!1}async getLaunchAlert(){let t=this.getPartyOrigin(),e=new URL(`${t}/catalog`);e.searchParams.append("alert","");let r=await(await fetch(e,{headers:{"X-Tabserver-Token":this.getTokenBase64(),Accept:"application/json"}})).json();if("error"in r)throw r.error;return r.ok.alert.find(i=>i.type=="LAUNCH"&&i.sourceUrl==new URL(this.getAppUrl()))||null}async resolveLaunchAlert(){let t=this.getPartyOrigin(),e=new URL(`${t}/resolve`),r=await(await fetch(e,{method:"POST",headers:{"X-Tabserver-Token":this.getTokenBase64(),"Content-Type":"application/json"},body:JSON.stringify({type:"LAUNCH",source:this.getAppUrl()})})).json();if("error"in r)throw r.error}async rejectLaunchAlert(){let t=this.getPartyOrigin(),e=new URL(`${t}/reject`),r=await(await fetch(e,{method:"POST",headers:{"X-Tabserver-Token":this.getTokenBase64(),"Content-Type":"application/json"},body:JSON.stringify({type:"LAUNCH",source:this.getAppUrl()})})).json();if("error"in r)throw r.error}getStorage(){return new C(this.getToken())}};var Ft="agent",E=class{#t=new Map;register(t,e,n){return this.#t.set(t,{descriptor:e,execute:n}),this}descriptors(){return[...this.#t.values()].map(t=>t.descriptor)}async executeToolCalls(t,e={}){let n=[];for(let r of t){let s=this.#t.get(r.name);if(!s){n.push({toolCallId:r.id,name:r.name,content:`No client tool named ${r.name}`,isError:!0});continue}try{let i=await s.execute(r.input||{},e);n.push({toolCallId:r.id,name:r.name,content:JSON.stringify(i)})}catch(i){n.push({toolCallId:r.id,name:r.name,content:String(i),isError:!0})}}return n}},Q=class{#t;#e;#o;#s=null;#n;constructor(t={}){this.#t=t.app,this.#e=t.appName,this.#o=t.agentTab??Ft,this.#n=t.clientTools??new E}get app(){return this.#t??null}get agentUrl(){return this.#s}get clientTools(){return this.#n}async init(){return this.#t??=await y.getInstance(this.#e),this.#t.isOrphan()||(this.#s=await this.#t.getAgentUrl(this.#o)),this}isOrphan(){return!!this.#t?.isOrphan()}getParty(){return this.#r().getParty()}listChats({targetParty:t}){return this.postAgent("/chats",{targetParty:t})}listModels({targetParty:t}){return this.postAgent("/models",{targetParty:t})}createSession(t){return this.postAgent("/session",{targetParty:t.targetParty,...t.chatId?{chatId:t.chatId}:{},systemPrompt:t.systemPrompt,temperature:t.temperature,...t.model?{model:t.model}:{},clientTools:this.#n.descriptors()})}async sendMessage(t){let e=await this.postAgent("/send",{targetParty:t.targetParty,chatId:t.chatId,message:t.message,temperature:t.temperature,toolsEnabled:t.toolsEnabled,toolDetails:t.toolDetails});for(await t.onConversationResult?.(e);e.clientToolCalls?.length;){await t.onClientToolCalls?.(e.clientToolCalls);let n=await this.#n.executeToolCalls(e.clientToolCalls,{targetParty:t.targetParty});e=await this.sendClientToolResults({targetParty:t.targetParty,chatId:t.chatId,toolResults:n,temperature:t.temperature,toolsEnabled:t.toolsEnabled,toolDetails:t.toolDetails}),await t.onConversationResult?.(e)}return e}sendClientToolResults(t){return this.postAgent("/client-tool-results",{targetParty:t.targetParty,chatId:t.chatId,toolResults:t.toolResults,temperature:t.temperature,toolsEnabled:t.toolsEnabled,toolDetails:t.toolDetails})}closeChat({targetParty:t,chatId:e}){return this.postAgent("/close",{targetParty:t,chatId:e})}deleteChat({targetParty:t,chatId:e}){return this.postAgent("/delete",{targetParty:t,chatId:e})}async postAgent(t,e){if(!this.#s)throw"Agent URL is not available";let r=await(await fetch(`${this.#s}${t}`,{method:"POST",headers:{"Content-Type":"application/json","X-Tabserver-Token":this.#r().getTokenBase64()},body:JSON.stringify(e)})).json();if("error"in r)throw r.error;return r.ok}normalizeTargetParty(t){let e=String(t??"").trim();if(!e)throw"Daemon host is required";try{let n=e.includes("://")?new URL(e):new URL(`${globalThis.location.protocol}//${e}`);if(!n.host)throw"missing host";return n.host}catch{throw`Invalid daemon host: ${t}`}}#r(){if(!this.#t)throw"Browser app is not initialised";return this.#t}};function Fe(o=void 0){let t=new E;return t.register("browser_daemon_identity",{type:"function",name:"browser_daemon_identity",execution:"client",description:"Get the active chat daemon and the counterparty it is talking to, derived from the browser launch token and active chat target.",parameters:{type:"object",properties:{},additionalProperties:!1}},async(e,n={})=>{let s=(o??await y.getInstance()).getToken(),i=n.targetParty||s.getParty();return{aud:n.targetAud||`${globalThis.location.protocol}//${i}`,sub:s.getSub(),party:i,counterparty:s.getCounterparty(),src:s.getSrc()}}),t.register("browser_language",{type:"function",name:"browser_language",execution:"client",description:"Get the browser language preferences for this page.",parameters:{type:"object",properties:{},additionalProperties:!1}},()=>({language:navigator.language,languages:Array.from(navigator.languages??[])})),t.register("browser_time",{type:"function",name:"browser_time",execution:"client",description:"Get the current timezone and local time from the browser.",parameters:{type:"object",properties:{},additionalProperties:!1}},()=>{let e=new Date,n=Intl.DateTimeFormat().resolvedOptions().timeZone;return{timeZone:n,isoTime:e.toISOString(),localTime:e.toLocaleString(void 0,{timeZone:n}),offsetMinutes:-e.getTimezoneOffset()}}),t}var Kt={name:"RSASSA-PKCS1-v1_5",modulusLength:2048,publicExponent:new Uint8Array([1,0,1]),hash:"SHA-256"},Z=class{#t;#e;constructor(t=Kt){if(this.#t=t,!crypto.subtle)throw"Crypto.subtle requires secure environment"}async generate(){this.#e=await crypto.subtle.generateKey(this.#t,!0,["sign","verify"])}async publicJwk(){if(!this.#e)throw"Must generate keypair";let t=this.#e.publicKey;return await crypto.subtle.exportKey("jwk",t)}async privateJwk(){if(!this.#e)throw"Must generate keypair";let t=this.#e.privateKey;return await crypto.subtle.exportKey("jwk",t)}async publicPem(){if(!this.#e)throw"Must generate keypair";let t=this.#e.publicKey,e=await crypto.subtle.exportKey("spki",t);return`-----BEGIN PUBLIC KEY-----
|
|
2
2
|
${btoa(String.fromCharCode(...new Uint8Array(e))).match(/.{1,64}/g)?.join(`
|
|
3
3
|
`)??""}
|
|
4
4
|
-----END PUBLIC KEY-----`}async privatePem(){if(!this.#e)throw"Must generate keypair";let t=this.#e.privateKey,e=await crypto.subtle.exportKey("pkcs8",t);return`-----BEGIN PRIVATE KEY-----
|
|
5
5
|
${btoa(String.fromCharCode(...new Uint8Array(e))).match(/.{1,64}/g)?.join(`
|
|
6
6
|
`)??""}
|
|
7
|
-
-----END PRIVATE KEY-----`}};async function
|
|
7
|
+
-----END PRIVATE KEY-----`}};async function He(){let o=await y.getInstance(),t=o.getPartyOrigin(),e=new URL(`${t}/catalog`);e.searchParams.append("alert","");let r=await(await fetch(e,{headers:{"X-Tabserver-Token":o.getTokenBase64(),Accept:"application/json"}})).json();if("error"in r)throw r.error;return r.ok.alert.find(i=>i.type=="LAUNCH"&&i.sourceUrl==new URL(o.getAppUrl()))||null}var tt=class{#t;#e;#o;#s;#n;#r;#l;async initWithKey(t,e,n,r){this.#e=e.startsWith("https:")?"https:":"http:",this.#o=t,this.#n=e,this.#t=n,this.#s=r,await this.#a()}async checkActivate(){if(!this.#r)throw"checkActivate: No token";let t=new URL(`${this.#r.getAud()}/activate`),e=this.#r.asSignedBase64(),n={},r;try{r=await fetch(t,{method:"POST",headers:{"x-tabserver-token":e,"content-type":"application/json"},body:JSON.stringify(n)});let{ok:s,error:i}=await r.json();if(i)throw i;return!!s.isNew}catch(s){throw console.error(s),`Cannot activate ${this.#r.getParty()}`}}async makeOffer(t,e=null){let{type:n="TRANSIENT",ttl:r=300,expiry:s=30,payload:i}=e??{};if(!this.#r)throw"makeOffer: No token";let a=new URL(`${this.#r.getAud()}/offer`),c=this.#r.asSignedBase64(),l={role:"party",type:n,ttl:r,description:`Offered by ${this.#r.getCounterparty()}`,payload:i,expiry:new Date(Date.now()+1e3*s),flow:t},h=await(await fetch(a,{method:"POST",headers:{"x-tabserver-token":c,"content-type":"application/json"},body:JSON.stringify(l)})).json();if("error"in h)throw h.error;let{claimCode:m,_checkState:D}=h.ok;return this.#l=m,h.ok}async checkState(){if(!this.#r||!this.#l)throw"checkState: No token or claimCode";let t=new URL(`${this.#r.getAud()}/offer/check`);t.searchParams.append("claimCode",this.#l);let e=this.#r.asSignedBase64(),r=await(await fetch(t,{headers:{"x-tabserver-token":e}})).json();if("error"in r)throw r.error;return r.ok.checkState}async confirmClaim(t){if(!this.#r||!this.#l)throw"confirmClaim: No token or claimCode";let e=new URL(`${this.#r.getAud()}/offer/confirm`),n=await this.#r.asSignedBase64(),r={claimCode:this.#l,checkCode:t},i=await(await fetch(e,{method:"POST",headers:{"x-tabserver-token":n,"content-type":"application/json"},body:JSON.stringify(r)})).json();if("error"in i)throw i.error;return i.ok}getChild(){if(!this.#o)throw"getChild: no child";return this.#o}getChildOrigin(){if(!this.#s||!this.#o)throw"getDaemonUrl: no issUrl or daemon";return`${this.#e}//${this.#o}`}async#a(){if(!this.#s||!this.#o||!this.#t)throw"buildToken: missing iss, child or privateJwk";let t=this.#s,e=this.getChildOrigin(),n=new URL(this.#s).origin,r;if(this.#n){let a=new URL(this.#n);r=`${a.origin}${a.pathname}${a.search}`}else r="party:control";let s={iss:t,aud:e,sub:n,src:r,scope:{"party:control":"offer"},iat:Date.now(),exp:Date.now()+1e3*60*3},i=new p(s);await i.signWith(Promise.resolve(this.#t)),this.#r=i}};var Bt="qrcode",et=class{#t;#e;constructor(t,e){this.#t=t,this.#e=e}generate(){let t=qrcode(0,"L");t.addData(this.#e),t.make();let e=t.createDataURL();return this.#t.classList.add(Bt),this.#t.src=e,new Promise((n,r)=>{this.#t.onload=n,this.#t.onerror=r})}};function ot(o){if(o===null)return o;if(Array.isArray(o))return o.map(ot);if(typeof o=="object"){let t={};for(let[e,n]of Object.entries(o))t[e]=ot(n);return t}return o}function qe(o){return o!==null}function ze(o){return o!==void 0}function g(o,t){if("ok"in o){let i=JSON.stringify(o);return new Response(i,{status:200,headers:{...t,"Content-Type":"application/json","Access-Control-Allow-Origin":"*"}})}let e=o.error,[,n,r]=e.match(/^(\d{3})\s(.+)/)??[null,"200",e],s=JSON.stringify({error:r});return new Response(s,{status:Number(n),headers:{...t,"Content-Type":"application/json","Access-Control-Allow-Origin":"*"}})}function Ze(o={error:"404 Not Found"}){let t=JSON.stringify(o);return new Response(t,{status:404,headers:{"Content-Type":"application/json","Access-Control-Allow-Origin":"*"}})}function to(o,t=[]){return new Response(null,{status:302,headers:{Location:o.toString(),"Access-Control-Allow-Origin":"*",...t}})}function eo(o){let{name:t,value:e,domain:n,sameSite:r,path:s="/",expires:i,secure:a=!0,httpOnly:c=!0}=o,l=[];return l.push(`${encodeURIComponent(t)}=${encodeURIComponent(e)}`),n&&l.push(`Domain=${n}`),r&&l.push(`SameSite=${r}`),l.push(`Path=${s}`),i&&l.push(`Expires=${i.toUTCString()}`),a&&l.push("Secure"),c&&l.push("HttpOnly"),l.join("; ")}var nt="daemon",_t="config",Ht="event",rt={Config:"config"},st="config",Vt=1e3*60*60,d=class o extends EventTarget{static#t=new o;static Ev=rt;static getInstance(){return this.#t}static shouldHandle(t){return o.isConfig(t)||o.isEvent(t)}static isConfig(t){let e=new URL(t.url);return t.method=="POST"&&e.pathname==`/${nt}/${_t}`&&t.headers.get("Content-Type")=="application/json"}static isEvent(t){let e=new URL(t.url);return t.method=="POST"&&e.pathname==`/${nt}/${Ht}`&&t.headers.get("Content-Type")=="application/json"}async handler(t){return o.isConfig(t)?await this.handleConfig(t):o.isEvent(t)?await this.handleEvent(t):g({error:"Invalid daemon request"})}async handleConfig(t){let e=await t.json();return sessionStorage.setItem(st,JSON.stringify(e)),this.dispatchEvent(new CustomEvent(rt.Config,{detail:e})),g({ok:!0})}async handleEvent(t){let{type:e,payload:n}=await t.json();return this.dispatchEvent(new CustomEvent(e,{detail:n})),g({ok:!0})}static getConfig(){let t=sessionStorage.getItem(st);if(!t)throw"DaemonConfig not ready";return JSON.parse(t)}static getPrivateKey(){let{system:{privateJwk:t}}=o.getConfig();return Promise.resolve(t)}static async getTokenFor(t,e=null,n=null,r=Vt){let s=o.getConfig();if(!s)throw"Lifecycle configuration not yet available";let{system:{protocol:i,party:a,issuer:c,source:l}}=s,u=new URL(l),h=e?`${i}//${e}`:`${i}//${a}`,m=`${i}//${a}`,D=Date.now(),kt={iss:c,aud:h,sub:m,src:n??`${u.origin}${u.pathname}${u.search}`,scope:t,iat:D,exp:D+r},W=new p(kt);return await W.signWith(o.getPrivateKey()),W}static async getStorage(){let t=await o.getStorageToken();return new C(t)}static getStorageToken(){let{system:{party:t}}=o.getConfig(),e={[p.Source.PARTY_CONTROL]:`${p.Capability.GET_ITEM} ${p.Capability.SET_ITEM}`};return o.getTokenFor(e,t)}};var R=class{#t;#e=1;constructor(t){if(t.post){this.#t=t.post;return}if(!t.url)throw new Error("JsonRpcMcpClient requires post or url");let e=t.fetch??fetch;this.#t=async n=>{let r=typeof t.headers=="function"?await t.headers():t.headers??{};return await(await e(t.url,{method:"POST",headers:Wt(r),body:JSON.stringify(n)})).json()}}async listTools(){let e=(await this.#o("tools/list",{}))?.tools;return Array.isArray(e)?e:[]}async callTool(t,e={}){let n=await this.#o("tools/call",{name:t,arguments:e});if(n?.isError)throw Yt(n);return n?.structuredContent??null}async#o(t,e){let n=await this.#t({jsonrpc:"2.0",id:this.#e++,method:t,params:e});if(n.error)throw n.error.message??JSON.stringify(n.error);return n.result}};function Yt(o){let t=o?.content;if(Array.isArray(t)){let e=t[0];if(typeof e?.text=="string")return e.text}return"MCP tool failed"}function Wt(o){let t=new Headers(o);return t.set("Content-Type","application/json"),t}var N=class{#t;#e;#o=[];#s=new Map;#n=new Map;constructor(t){this.#t=t.postParty,this.#e=t.postPartyRaw}get toolCatalog(){return this.#o}async refreshCatalog(){let t=await this.#t("/ai/tools/catalog",{});this.setCatalog(t.tools)}setCatalog(t){let e=Array.isArray(t)?t:[];this.#o=e,this.#s=new Map(this.#o.filter(n=>n.tool?.name&&n.route).map(n=>[n.tool.name,n.route])),this.#n.clear()}chatTools(t={}){return[...this.#o.map(qt).filter(it),...t.clientTools??[]]}async executeToolCalls(t){let e=[];for(let n of t)try{e.push({toolCallId:n.id,name:n.name,content:await this.executeToolCall(n)})}catch(r){e.push({toolCallId:n.id,name:n.name,content:String(r),isError:!0})}return e}async executeToolCall(t){let e=this.#s.get(t.name);if(!e)throw new Error(`No discovered tool named ${t.name}`);if(e.method!=="POST"||!e.path)throw new Error(`Unsupported route for tool ${t.name}`);if(e.kind==="platform"){let n=await this.#t(e.path,t.input);return JSON.stringify(n)}if(e.kind==="mcp")return await this.#r(e,t);throw new Error(`Unsupported route kind for tool ${t.name}`)}async#r(t,e){let n=this.#n.get(t.path);n||(n=new R({post:s=>this.#e(t.path,s)}),this.#n.set(t.path,n));let r=await n.callTool(t.mcpToolName||e.name,{...e.input});return JSON.stringify(r)}},I=class{#t;#e=new Map;constructor(t){this.#t=t}async refreshCatalog(){await Promise.all(this.#t.map(t=>t.runtime.refreshCatalog()))}chatTools(t={}){this.#e.clear();let e=this.#t.map(s=>s.runtime.chatTools().filter(i=>i.execution==="server"&&this.#o(s,i.name))),n=new Map;for(let s of e)for(let i of s)n.set(i.name,(n.get(i.name)??0)+1);let r=[];return this.#t.forEach((s,i)=>{for(let a of e[i]){let l=(n.get(a.name)??0)>1?`${i}__${a.name}`:a.name;this.#e.set(l,{provider:s,realName:a.name}),r.push({...a,name:l})}}),[...r,...t.clientTools??[]]}async executeToolCall(t){let e=this.#e.get(t.name);if(!e)throw new Error(`No discovered tool named ${t.name}`);return await e.provider.runtime.executeToolCall({...t,name:e.realName})}#o(t,e){return!t.allowedTools||t.allowedTools.has(e)}},J=class{#t;#e=[];constructor(t){this.#t=new R({post:t.postRpc})}async refreshCatalog(){let t=await this.#t.listTools();this.#e=t.map(Xt).filter(it)}chatTools(t={}){return[...this.#e,...t.clientTools??[]]}async executeToolCall(t){let e=await this.#t.callTool(t.name,{...t.input});return JSON.stringify(e)}};function Xt(o){let t=o;return t?.name?{type:"function",name:t.name,execution:"server",description:t.description,parameters:t.inputSchema}:null}function qt(o){return o?.tool?.name?{type:"function",name:o.tool.name,execution:"server",description:o.tool.description,parameters:o.tool.inputSchema}:null}function uo(o,t){return!t||t===o?o:o?`${t}
|
|
8
8
|
|
|
9
|
-
${o}`:t}function
|
|
9
|
+
${o}`:t}function it(o){return o!=null}var zt="2025-06-18",U={"Access-Control-Allow-Origin":"*"},Qt="POST, GET",at=class{#t;constructor(t){this.#t=t}listTools(){return this.#t.map(t=>({name:t.name,...t.title===void 0?{}:{title:t.title},description:t.description,inputSchema:t.inputSchema,...t.outputSchema===void 0?{}:{outputSchema:t.outputSchema},...t.annotations===void 0?{}:{annotations:t.annotations}}))}async callTool(t,e,n){let r=this.#t.find(s=>s.name===t);if(!r)throw new f(-32602,`Unknown tool: ${t}`);return await r.call(e,n)}},$=class{#t;#e;#o;constructor(t,e){this.#t=t,typeof e=="function"?(this.#e=e,this.#o={name:"mcp-server",version:"1"}):(this.#e=e.context,this.#o=e.serverInfo??{name:"mcp-server",version:"1"})}async handle(t){if(t.method==="GET")return te(t);if(t.method==="DELETE"||t.method!=="POST")return lt();let e;try{e=await t.json()}catch{return x(M(null,-32700,"Parse error"))}let n=ee(e.id);if(e.jsonrpc!=="2.0"||typeof e.method!="string")return x(M(n,-32600,"Invalid Request"));if(e.id===void 0)return Zt();try{let r=await this.#s(e.method,e.params,t);return x({jsonrpc:"2.0",id:n,result:r})}catch(r){return r instanceof f?x(M(n,r.code,r.message)):x(M(n,-32603,String(r)))}}async#s(t,e,n){switch(t){case"initialize":return{protocolVersion:zt,capabilities:{tools:{listChanged:!1}},serverInfo:this.#o};case"notifications/initialized":return{};case"tools/list":return{tools:await this.#t.listTools(this.#e(n))};case"tools/call":return await this.#n(e,n);default:throw new f(-32601,`Method not found: ${t}`)}}async#n(t,e){if(!t||typeof t!="object"||Array.isArray(t))throw new f(-32602,"tools/call params must be an object");let{name:n,arguments:r={}}=t;if(typeof n!="string"||!r||typeof r!="object"||Array.isArray(r))throw new f(-32602,"Invalid tools/call params");try{let s=await this.#t.callTool(n,r,this.#e(e));return ct(s)}catch(s){if(s instanceof f)throw s;return ct({error:String(s)},!0)}}},f=class extends Error{code;constructor(t,e){super(e),this.code=t}};function ct(o,t=!1){return{content:[{type:"text",text:typeof o=="string"?o:JSON.stringify(o)}],structuredContent:o,isError:t}}function M(o,t,e){return{jsonrpc:"2.0",id:o,error:{code:t,message:e}}}function x(o){return new Response(JSON.stringify(o),{headers:{...U,"Content-Type":"application/json"}})}function Zt(){return new Response(null,{status:202,headers:U})}function te(o){let t=new URL(o.url);return new Response(`event: endpoint
|
|
10
10
|
data: ${t.pathname}
|
|
11
11
|
|
|
12
|
-
`,{headers:{
|
|
13
|
-
|
|
12
|
+
`,{headers:{...U,"Content-Type":"text/event-stream","Cache-Control":"no-cache"}})}function lt(){return new Response(null,{status:405,headers:{...U,Allow:Qt}})}function ee(o){return typeof o=="string"||typeof o=="number"||o===null?o:null}function F(o,t){oe(o,t.reasoning),t.content?o.push({role:"assistant",content:t.content}):t.toolCalls?.length||o.push({role:"assistant",content:""})}function oe(o,t){for(let e of t??[])e.redacted||!e.text||o.push({role:"thinking",content:e.text})}function K(o,t){return`${t?"Client tool call":"Agent tool call"}: ${o.name}
|
|
13
|
+
${JSON.stringify(o.input,null,2)}`}function P(o,t,e,n,r=!1){n&&o.push({role:"tool-result",content:ne(t,e,r),...r?{error:!0}:{}})}function ne(o,t,e=!1){return`${e?"Tool error":"Tool result"}: ${o}
|
|
14
|
+
${t}`}function ut(o,t={}){let e=t.toolDetails??!0,n=t.clientToolNames,r=[];for(let s of o)if(s.role==="assistant"){if(F(r,s),e)for(let i of s.toolCalls??[])r.push({role:"tool",content:K(i,n?.has(i.name)??!1)})}else if(s.role==="tool")for(let i of s.toolResults??[])P(r,i.name??"tool",i.content,e,i.isError);else s.role==="user"&&s.content&&r.push({role:"user",content:s.content});return r}var re="useTools",gt=/[\s,;\|]+/,L=class{#t;constructor(t){this.#t=t}hasCapability(t,e){return(this.#t.scope[t]??"").split(gt).includes(e)}getParty(){return this.#t.party??""}getCounterparty(){return this.#t.counterparty}getSourceUrl(){return new URL(this.#t.src)}getSrc(){return this.#t.src}};function dt(o){let{token:t,providerSource:e,toolName:n,capability:r,providerYaml:s}=o;if(!t)return!1;if(ht(t,e,r))return!0;if(!s)return!1;let i=String(t.getSourceUrl());return ht(t,i,re)&&se(s,n,r)&&ie(s.grant,t.getCounterparty(),t.getSrc(),r)}function ht(o,t,e){try{return o.hasCapability(t,e)}catch{return!1}}function se(o,t,e){return!!o.mcp?.tools.some(n=>n.name===t&&n.capability===e)}function ie(o,t,e,n){for(let r in o??{}){if(!pt(t.toLowerCase(),r.toLowerCase()))continue;let s=o[r];if(Array.isArray(s)){if(s.includes(n))return!0;continue}for(let i in s)if(pt(e,i)&&ae(s[i]).includes(n))return!0}return!1}function ae(o){return o.split(gt).filter(t=>t.length>0)}function pt(o,t){return new RegExp(`^${t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&").replace(/%/g,".*").replace(/_/g,".")}$`).test(o)}var ce=4,le=.2,mt="x-tabserver-token",j="x-tabserver-oauth-context",St="x-tabserver-counterparty",H="x-tabserver-continuation",Y="expert_ask",ft="expertAsk",yt=class{#t;#e;#o;#s;#n;#r;#l;#a=new Map;#u=new Map;#h=new d;#i;#m;constructor(t){this.#t=t.targetScope,this.#e=t.defaultSystemPrompt,this.#o=t.defaultTemperature??le,this.#s=t.maxToolRounds??ce,this.#n=t.mcpPath??"/rpc",this.#r=t.resolveToolProviders,this.#l=t.resolveMemorySelection,this.#i={temperature:this.#o,toolsEnabled:!0,toolDetails:!0,thinking:t.defaultThinking??!1},this.#m=new $(new V(this),{context:Te,serverInfo:{name:"ai-chat-expert",version:"1"}})}async handler(t){if(d.shouldHandle(t))return await this.#h.handler(t);try{if(t.method!=="POST")throw"405 Method Not Allowed";let e=new URL(t.url);if(e.pathname===this.#n)return await this.#m.handle(t);switch(e.pathname){case"/session":return g({ok:await this.#$(t)});case"/settings":return g({ok:await this.#U(t)});case"/models":return g({ok:await this.#L(t)});case"/chats":return g({ok:await this.#j(t)});case"/send":return g({ok:await this.#C(t)});case"/client-tool-results":return g({ok:await this.#y(t)});case"/close":return g({ok:await this.#D(t)});case"/delete":return g({ok:await this.#G(t)});default:throw"404 Not Found"}}catch(e){return console.error(e),g({error:String(e)})}}async askExpert(t,e){let n=e.trim();if(!n)throw"Question is required";let r=this.#M(void 0),s=T(t),i=await this.#A(r,{expert:!0},s),a=this.#i.temperature,c=t.headers.get(H),l=c?ge(t,c):null;if(l)return await this.#T(t,r,s,l,i,a,n);let u=await this.#d(t,r,void 0,i,a);try{return{answer:await this.#f(u,n,a)}}finally{this.#a.delete(w(t,u.targetParty,u.chatId)),await this.#c(u.targetParty,"/ai/chat/close",{chatId:u.chatId},u.forward).catch(h=>console.error(h))}}async#T(t,e,n,r,s,i,a){let c=this.#u.get(r);try{let l=await this.#d(t,e,c,s,i);return c||this.#u.set(r,l.chatId),{answer:await this.#f(l,a,i)}}catch(l){if(c&&await this.#E(e,c,n)){this.#u.delete(r),this.#a.delete(w(t,e,c));let u=await this.#d(t,e,void 0,s,i);return this.#u.set(r,u.chatId),{answer:await this.#f(u,a,i)}}throw l}finally{let l=this.#u.get(r);l&&this.#a.delete(w(t,e,l))}}async#f(t,e,n){B(t),t.toolRounds=0,await this.#w(t);let r=[],s=await this.#c(t.targetParty,"/ai/chat/send",{chatId:t.chatId,message:e,options:this.#R(t,this.#i.toolsEnabled,n)},t.forward),i=await this.#g(t,s.message,this.#i.toolsEnabled,!1,n,r);if(i.clientToolCalls?.length)throw"Expert MCP cannot execute browser client tools";let a=Se(i.events);if(!a)throw"Expert did not return an answer";return a}async#C(t){let e=await wt(t);if(typeof e.message!="string"||!e.message.trim())throw"Message is required";let n=this.#J(e),r=this.#b(e),s=this.#P(e),i=this.#p(e),a=b(e),c=await this.#d(t,i,a,void 0,s,this.#I(e));B(c),c.toolRounds=0,Tt(c,e),n&&await this.#w(c);let l=[],u=await this.#c(c.targetParty,"/ai/chat/send",{chatId:c.chatId,message:e.message.trim(),options:this.#R(c,n,s)},c.forward);return await this.#g(c,u.message,n,r,s,l)}async#y(t){let e=await wt(t),n=this.#J(e),r=this.#b(e),s=this.#P(e),i=this.#p(e),a=b(e),c=await this.#d(t,i,a,void 0,s);Tt(c,e);let l=he(c,e.toolResults),u=[];for(let m of l)P(u,m.name??"client_tool",m.content,r,m.isError);let h=[...c.pendingToolResults,...l];return B(c),await this.#v(c,h,n,r,s,u)}async#g(t,e,n,r,s,i){let a=e;for(;;){F(i,a);let c=a.toolCalls??[];if(!c.length)return{events:i};if(t.toolRounds>=this.#s)return i.push({role:"tool-result",content:"Stopped after too many tool rounds.",error:!0}),{events:i};let{toolResults:l,clientToolCalls:u}=await fe(t,c,r,i);if(u.length)return t.pendingClientToolCalls=new Map(u.map(m=>[m.id,m])),t.pendingToolResults=l,{events:i,clientToolCalls:u};let h=await this.#v(t,l,n,r,s,i);return h.clientToolCalls?.length,h}}async#v(t,e,n,r,s,i){if(t.toolRounds>=this.#s)return i.push({role:"tool-result",content:"Stopped after too many tool rounds.",error:!0}),{events:i};t.toolRounds++;let a=await this.#c(t.targetParty,"/ai/chat/send",{chatId:t.chatId,message:{role:"user",content:"",toolResults:e},options:this.#R(t,n,s)},t.forward);return await this.#g(t,a.message,n,r,s,i)}async#$(t){let e=await S(t),n=this.#p(e),r=b(e),s=T(t),i=await this.#d(t,n,r,r?void 0:await this.#_(e,n,s),this.#P(e),this.#I(e));Rt(i,e.clientTools),await this.#w(i);let a=await this.#F(i);return{chatId:i.chatId,history:a,historyEvents:ut(a,{toolDetails:this.#b(e),clientToolNames:i.clientToolNames}),closed:r?await this.#E(n,r,s):null}}async#U(t){let e=await S(t);return Object.keys(e).length&&this.#W(e),this.#Y()}async#L(t){let e=await S(t),n=this.#p(e),r=await this.#c(n,"/ai/chat/models",{},T(t));return{models:Array.isArray(r.models)?r.models:[]}}async#j(t){let e=await S(t),n=this.#p(e),r=await this.#c(n,"/ai/chat/list",{},T(t));return{chats:Array.isArray(r.chats)?r.chats:[]}}async#D(t){let e=await S(t),n=this.#p(e),r=b(e);if(!r)throw"chatId is required";let s=T(t),i=w(t,n,r),a=this.#a.get(i);return a?(await this.#c(a.targetParty,"/ai/chat/close",{chatId:a.chatId},a.forward),this.#a.delete(i),!0):(await this.#c(n,"/ai/chat/close",{chatId:r},s),!0)}async#G(t){let e=await S(t),n=this.#p(e),r=b(e);if(!r)throw"chatId is required";let s=w(t,n,r);return await this.#c(n,"/ai/chat/delete",{chatId:r},T(t)),this.#a.delete(s),!0}async#d(t,e,n,r,s=this.#o,i){let a=w(t,e,n);if(n){let h=this.#a.get(a);if(h)return h}let c=T(t),l=n?{id:n}:await this.#c(e,"/ai/chat/create",{system:r??await this.#A(e,{expert:!1},c),temperature:s,...i?{model:i}:{}},c);c.continuation??=String(l.id),a=w(t,e,String(l.id));let u={chatId:String(l.id),targetParty:e,forward:c,toolRuntime:await this.#K(e,c),clientTools:[],clientToolNames:new Set,pendingClientToolCalls:new Map,pendingToolResults:[],toolRounds:0};return await this.#w(u),this.#a.set(a,u),u}async#F(t){let e=await this.#c(t.targetParty,"/ai/chat/history",{chatId:t.chatId},t.forward);return Array.isArray(e.messages)?e.messages:[]}async#E(t,e,n){let r=await this.#c(t,"/ai/chat/list",{},n),i=(Array.isArray(r.chats)?r.chats:[]).find(a=>a.id===e);return typeof i?.closed=="string"?i.closed:null}#N(t,e,n,r){return new N({postParty:(s,i)=>this.#c(t,s,r&&s==="/ai/memory/catalog"?{...i,collections:r}:i,e,n),postPartyRaw:(s,i)=>this.#x(t,s,i,e,n)})}async#S(t,e){if(this.#l&&t===d.getConfig().system.party)return await this.#l(t,e??{oauthContext:null,counterpartyContext:null,continuation:null})}async#K(t,e){let n=await this.#r?.(t,e);if(!n?.length)return this.#N(t,e,void 0,await this.#S(t,e));let r=d.getConfig().system.party;return new I(await Promise.all(n.map(async s=>({key:s.party,runtime:s.party===r?this.#N(s.party,e,s.scope,await this.#S(s.party,e)):this.#B(s.party,e),allowedTools:s.tools?new Set(s.tools):void 0}))))}#B(t,e){return new J({postRpc:n=>this.#x(t,"/mcp/rpc",n,e)})}async#w(t){await t.toolRuntime.refreshCatalog()}#R(t,e,n){let r=this.#i.thinking,s={temperature:n,thinking:r};if(!e)return s;let i=t.toolRuntime.chatTools({clientTools:t.clientTools});return i.length?{...s,tools:i,toolChoice:"auto"}:s}async#c(t,e,n,r,s){let i=await this.#x(t,e,n,r,s);if("error"in i)throw i.error;return i.ok}async#x(t,e,n,r,s){let i=d.getConfig(),a=await d.getTokenFor({[i.system.source]:s??this.#t},t),c=`${i.system.protocol}//${t}${e}`,l={"Content-Type":"application/json","X-Tabserver-Token":a.asSignedBase64()};return r?.oauthContext&&(l[j]=r.oauthContext),r?.counterpartyContext&&(l[St]=r.counterpartyContext),r?.continuation&&(l[H]=r.continuation),await(await fetch(c,{method:"POST",headers:l,body:JSON.stringify(n)})).json()}async#_(t,e,n){let r=typeof t.systemPrompt=="string"&&t.systemPrompt.trim()?t.systemPrompt.trim():this.#O();return await this.#A(e,{basePrompt:r,expert:!1},n)}#I(t){return typeof t.model=="string"&&t.model.trim()?t.model.trim():void 0}#P(t){return typeof t.temperature=="number"&&Number.isFinite(t.temperature)&&t.temperature>=0&&t.temperature<=2?Math.round(t.temperature*10)/10:this.#i.temperature}#J(t){return typeof t.toolsEnabled=="boolean"?t.toolsEnabled:this.#i.toolsEnabled}#b(t){return typeof t.toolDetails=="boolean"?t.toolDetails:this.#i.toolDetails}#p(t){return this.#M(t.targetParty)}#M(t){let{system:{party:e,protocol:n}}=d.getConfig();if(t==null||t==="")return e;if(typeof t!="string")throw"targetParty must be a string";let r=t.trim();if(!r)return e;try{let s=r.includes("://")?new URL(r):new URL(`${n}//${r}`);if(!s.host)throw"missing host";return s.host}catch{throw`Invalid targetParty: ${t}`}}async#A(t,e,n){let r=e.basePrompt??this.#O(),s=await Pe(),i=e.expert&&s?`
|
|
15
|
+
When answering expert MCP questions, prefer semantic and structured memory under root "${s}" when looking up remembered information.`:"",a=await this.#H(t,n);return`${r}${i}${a}`}async#H(t,e){try{let n=await this.#S(t,e),r=xt(),s=r?await this.#V(t,r,e,n):await this.#k(t,{collections:n},e);return Re(s)}catch{return`
|
|
14
16
|
|
|
15
|
-
Semantic memory catalog is unavailable for this session.`}}async#
|
|
17
|
+
Semantic memory catalog is unavailable for this session.`}}async#V(t,e,n,r){let s=await this.#k(t,{root:e,collections:r},n);return s.length?s:await this.#k(t,{collections:r},n)}async#k(t,e,n){let r=await this.#c(t,"/ai/memory/catalog",{kind:"semantic",...e.root?{root:e.root}:{},...e.collections?{collections:e.collections}:{}},n);return Array.isArray(r.resources)?r.resources:[]}#O(){return this.#i.systemPrompt??this.#e}#Y(){return{...this.#i,systemPrompt:this.#O()}}#W(t){if(t.systemPrompt!==void 0){if(typeof t.systemPrompt!="string")throw"systemPrompt must be a string";this.#i.systemPrompt=t.systemPrompt.trim()?t.systemPrompt:this.#e}if(t.temperature!==void 0){if(typeof t.temperature!="number"||!Number.isFinite(t.temperature)||t.temperature<0||t.temperature>2)throw"temperature must be between 0 and 2";this.#i.temperature=Math.round(t.temperature*10)/10}if(t.toolsEnabled!==void 0){if(typeof t.toolsEnabled!="boolean")throw"toolsEnabled must be a boolean";this.#i.toolsEnabled=t.toolsEnabled}if(t.toolDetails!==void 0){if(typeof t.toolDetails!="boolean")throw"toolDetails must be a boolean";this.#i.toolDetails=t.toolDetails}if(t.thinking!==void 0){if(typeof t.thinking!="boolean")throw"thinking must be a boolean";this.#i.thinking=t.thinking}}};async function wt(o){if(!o.headers.get("Content-Type")?.startsWith("application/json"))throw"Content-Type must be 'application/json'";let t=await o.json();if(!t||typeof t!="object"||Array.isArray(t))throw"Request body must be an object";return t}async function S(o){if(!o.headers.get("Content-Type")?.startsWith("application/json"))return{};let t=await o.json();return!t||typeof t!="object"||Array.isArray(t)?{}:t}function Tt(o,t){Object.hasOwn(t,"clientTools")&&Rt(o,t.clientTools)}function Rt(o,t){let e=Array.isArray(t)?t.filter(ue):[];o.clientTools=e,o.clientToolNames=new Set(e.map(n=>n.name))}function ue(o){return!!o&&typeof o=="object"&&!Array.isArray(o)&&o.type==="function"&&typeof o.name=="string"&&o.execution==="client"}function he(o,t){if(!Array.isArray(t))throw"toolResults must be an array";let e=new Set(o.pendingClientToolCalls.keys());if(!e.size)throw"No pending client tool calls";let n=[];for(let r of t){if(!pe(r))throw"Invalid client tool result";if(!e.delete(r.toolCallId))throw`Unexpected client tool result: ${r.toolCallId}`;if(o.pendingClientToolCalls.get(r.toolCallId)?.name!==r.name)throw`Client tool result name mismatch: ${r.toolCallId}`;n.push(r)}if(e.size)throw`Missing client tool results: ${[...e].join(", ")}`;return n}function pe(o){return!!o&&typeof o=="object"&&!Array.isArray(o)&&typeof o.toolCallId=="string"&&typeof o.name=="string"&&typeof o.content=="string"&&(o.isError===void 0||typeof o.isError=="boolean")}function B(o){o.pendingClientToolCalls.clear(),o.pendingToolResults=[]}function b(o){if(!(o.chatId===void 0||o.chatId===null||o.chatId==="")){if(typeof o.chatId!="string")throw"chatId must be a string";return o.chatId}}function w(o,t,e){return`${de(o)}
|
|
16
18
|
${t}
|
|
17
|
-
${e??"default"}`}function
|
|
18
|
-
${
|
|
19
|
-
${t}`}var
|
|
19
|
+
${e??"default"}`}function ge(o,t){try{let e=p.from(o);return e?`${e.getSub()}
|
|
20
|
+
${e.getSrc()}
|
|
21
|
+
${t}`:null}catch{return null}}function de(o){let t=o.headers.get(mt);if(t)return t;let e=o.headers.get(j);if(e)return e;throw`This request must provide a token in '${mt}'`}function T(o){let t=o.headers.get(j);return{oauthContext:t,counterpartyContext:me(o,t),continuation:o.headers.get(H)}}function me(o,t){let e=o.headers.get(St);if(e)return e;if(t)return null;try{let n=p.from(o);return n?JSON.stringify({counterparty:n.getCounterparty(),src:n.getSrc()}):null}catch{return null}}async function fe(o,t,e,n){let r=[],s=[];for(let i of t){let a=o.clientToolNames.has(i.name);if(e&&n.push({role:"tool",content:K(i,a)}),a){s.push(i);continue}try{let c=await o.toolRuntime.executeToolCall(i);P(n,i.name,c,e),r.push({toolCallId:i.id,name:i.name,content:c})}catch(c){let l=String(c);P(n,i.name,l,e,!0),r.push({toolCallId:i.id,name:i.name,content:l,isError:!0})}}return{toolResults:r,clientToolCalls:s}}var V=class{#t;constructor(t){this.#t=t}listTools(){return[ye()]}async callTool(t,e,n){if(t!==Y)throw new f(-32602,`Unknown tool: ${t}`);if(we(n),typeof e.question!="string"||!e.question.trim())throw"question is required";return await this.#t.askExpert(n.request,e.question)}};function ye(){return{name:Y,title:"Ask Expert",description:"Ask this AI Chat expert a question. The expert may use its configured memory and tools before answering.",inputSchema:{type:"object",properties:{question:{type:"string",description:"Question to ask the expert."}},required:["question"],additionalProperties:!1},outputSchema:{type:"object",properties:{answer:{type:"string"}},required:["answer"],additionalProperties:!1},annotations:{readOnlyHint:!0,destructiveHint:!1}}}function we(o){if(!dt({token:o.token,providerSource:o.source,toolName:Y,capability:ft,providerYaml:o.yaml}))throw`Missing capability: ${ft}`}function Te(o){let{system:{party:t,source:e,protocol:n,sourcePrefix:r},content:{yaml:s}}=d.getConfig();return{party:t,source:e,protocol:n,sourcePrefix:r,yaml:s,token:Ce(o),request:o}}function Ce(o){let t=p.from(o);if(t)return t;let e=o.headers.get(j);return e?new L(JSON.parse(e)):null}function Se(o){for(let t=o.length-1;t>=0;t--){let e=o[t];if(e.role==="assistant"&&!e.error)return e.content}return null}function Re(o){if(!o.length)return`
|
|
20
22
|
|
|
21
23
|
Available semantic memories:
|
|
22
|
-
None discovered.`;let t=["","","Available semantic memories:","Use semantic_query with the exact root and collection when these memories are relevant."];for(let e of o){let n=
|
|
23
|
-
`)}function
|
|
24
|
+
None discovered.`;let t=["","","Available semantic memories:","Use semantic_query with the exact root and collection when these memories are relevant."];for(let e of o){let n=_(e.root),r=_(e.collection);if(!n||!r)continue;t.push(`- root: ${n}`),t.push(` collection: ${r}`);let s=_(e.description);s&&t.push(` description: ${s}`);let i=Ct(e.schema),a=xe(i?.textProperties);a.length&&t.push(` textProperties: ${a.join(", ")}`);let c=Ct(i?.propertyTypes);c&&t.push(` propertyTypes: ${JSON.stringify(c)}`),t.push(` query with: semantic_query({ root: ${JSON.stringify(n)}, collection: ${JSON.stringify(r)}, query: { text: "<search text>", limit: 5 } })`)}return t.join(`
|
|
25
|
+
`)}function _(o){return typeof o=="string"?o:""}function Ct(o){return o&&typeof o=="object"&&!Array.isArray(o)?o:null}function xe(o){return Array.isArray(o)?o.filter(t=>typeof t=="string"):[]}function xt(){let{system:{source:o}}=d.getConfig();return new URL(o).searchParams.get("root")?.trim().replace(/^\/+|\/+$/g,"")||void 0}async function Pe(){let{system:{source:o}}=d.getConfig(),t=new URL(o),e=xt();return e||(t.hash="",await A(t))}var Pt="x-forwarded-host",bt="x-forwarded-proto",At="x-forwarded-pathname";function be(o){return o.headers.has(bt)?o.headers.get(bt)+":":new URL(o.url).protocol}function Ae(o){return o.headers.has(Pt)?o.headers.get(Pt)||"":new URL(o.url).host}function ke(o){return o.headers.has(At)?o.headers.get(At)||"":new URL(o.url).pathname}function Oe(o){let t=be(o),e=Ae(o),n=ke(o);return new URL(`${t}//${e}${n}`)}function bo(o){let t=new URL(o.url),e=Oe(o);return e.search=t.search,e.hash=t.hash,e}function Ao(o){return new URL(o.url).pathname}function ko(o,t){let e=ve(o);return t in e?e[t]:null}function ve(o){let t={},e=o.headers.get("Cookie");if(e)for(let n of e.split("; ")){let[r,s]=n.split("=");t[r]=s}return t}export{N as AgentToolRuntime,I as AggregateToolRuntime,y as BrowserApp,_t as CONFIG_PATH,E as ClientToolRegistry,nt as DAEMON_PREFIX,Ht as EVENT_PATH,J as GatewayToolRuntime,yt as GenerativeChatAgent,Q as GenerativeChatClient,Bt as ImgClass,R as JsonRpcMcpClient,$ as JsonRpcMcpServer,Z as KeyPair,d as Lifecycle,at as McpToolRegistry,L as OAuthMcpToken,tt as ParentHelper,f as ProtocolError,et as QrCode,C as Storage,p as Token,re as USE_TOOLS_CAPABILITY,F as appendAssistantEvent,oe as appendReasoningEvents,P as appendToolResultEvent,eo as buildCookie,ot as cloneJSONValue,uo as composeToolDescription,Fe as createDefaultBrowserChatTools,A as getAppId,Ae as getClientHost,ke as getClientPathname,be as getClientProtocol,bo as getClientUrl,Oe as getClientUrlPhp,ko as getCookie,ve as getCookies,Ut as getItem,Lt as getItemsLike,He as getLaunchAlert,Ao as getPathname,dt as hasMcpToolCapability,ut as historyToRenderEvents,ze as isDefined,Yt as mcpToolErrorMessage,ct as mcpToolResult,qe as notNull,jt as removeItem,g as response,to as response302,Ze as response404,$t as setItem,Ee as shortHexDigest,Ot as shortSafeDigest,qt as toChatTool,ht as tokenHasCapability,K as toolCallMessage,ne as toolResultMessage,se as yamlDeclaresMcpToolCapability,ie as yamlGrantsCapability};
|
|
24
26
|
//# sourceMappingURL=index.js.map
|