webdaemon 25.6.0 → 25.8.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
CHANGED
|
@@ -1,22 +1,24 @@
|
|
|
1
|
-
async function bt(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 Ae(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 bt(String(o),8)}var k={name:"RSASSA-PKCS1-v1_5",hash:"SHA-256"},_=/https?:\/\/[^\/]+$/,kt=/^\/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;#a;#u;#i;#l;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.#a=new URL(r),this.#u=new URL(s),this.#i=new URL(i),this.#i.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.#p(t);this.#l=e,this.#m(),await this.#g(n)}async fetchSignatory(){let t=String(this.getIssuer());this.#l=await o.#f(t),this.#m()}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.#g(n)}async#g(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.#l)throw"validateSignatory: no signatory";let t=this.#l.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.#i.protocol==="http:"||this.#i.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(!kt.test(n.pathname))throw`App signatory issuer path '${n.pathname}' must match /issuer/<appId>`}static async#p(t){let e=o.#d(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.#C(t,a),a})();o.#s.set(t,r);try{return await r}finally{o.#s.delete(t)}}static#d(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#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,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.#a.origin}getParty(){return this.#a.host}getSub(){return this.#u.origin}getSrc(){return String(this.#i)}getCounterparty(){return this.#u.host}getSignatorySrc(){if(!this.#l)throw"getSignatorySrc: no signatory";return this.#l.src}getSourceUrl(){return this.#i}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 Ot=/^[\w\-.:]+(?:\/[\w\-.:]+)*$/,vt=/^[\w\-.:%_]+(?:\/[\w\-.:%_]+)*$/,T=class{#t;constructor(t){this.#t=t}getItem(t){return Jt(this.#t,t)}getItemsLike(t){return Mt(this.#t,t)}setItem(t,e){return It(this.#t,t,e)}removeItem(t){return $t(this.#t,t)}};function D(o){if(o.match(Ot)==null)throw`DaemonStorage: Invalid key: '${o}`}function Et(o){if(o.match(vt)==null)throw`DaemonStorage: Invalid like key: ${o}`}function Nt(o){try{return JSON.stringify(o)}catch{throw"DaemonStorage: Invalid value"}}async function It(o,t,e){D(t);let n=Nt(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 Jt(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 Mt(o,t){Et(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 $t(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:"#"},Ut={Ash:encodeURIComponent("\xE6")},A=class{params=new URLSearchParams;extractHash(t){t=t.startsWith(Y.Hash)?t.substring(1):t;let e=t.indexOf(Ut.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",Lt="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 A,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(Lt,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 jt="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??jt,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 Dt={name:"RSASSA-PKCS1-v1_5",modulusLength:2048,publicExponent:new Uint8Array([1,0,1]),hash:"SHA-256"},X=class{#t;#e;constructor(t=Dt){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 xt(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 xt(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;#a;#u;#i;#l;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.#a=new URL(r),this.#u=new URL(s),this.#i=new URL(i),this.#i.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.#p(t);this.#l=e,this.#d(),await this.#g(n)}async fetchSignatory(){let t=String(this.getIssuer());this.#l=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.#g(n)}async#g(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.#l)throw"validateSignatory: no signatory";let t=this.#l.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.#i.protocol==="http:"||this.#i.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#p(t){let e=o.#m(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.#C(t,a),a})();o.#s.set(t,r);try{return await r}finally{o.#s.delete(t)}}static#m(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#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.#a.origin}getParty(){return this.#a.host}getSub(){return this.#u.origin}getSrc(){return String(this.#i)}getCounterparty(){return this.#u.host}getSignatorySrc(){if(!this.#l)throw"getSignatorySrc: no signatory";return this.#l.src}getSourceUrl(){return this.#i}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 $t(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 $t(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:"#"},Mt={Ash:encodeURIComponent("\xE6")},k=class{params=new URLSearchParams;extractHash(t){t=t.startsWith(Y.Hash)?t.substring(1):t;let e=t.indexOf(Mt.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-----
|
|
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 Fe(){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 z=class{#t;#e;#o;#s;#n;#r;#a;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.#u()}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:j}=h.ok;return this.#a=m,h.ok}async checkState(){if(!this.#r||!this.#a)throw"checkState: No token or claimCode";let t=new URL(`${this.#r.getAud()}/offer/check`);t.searchParams.append("claimCode",this.#a);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.#a)throw"confirmClaim: No token or claimCode";let e=new URL(`${this.#r.getAud()}/offer/confirm`),n=await this.#r.asSignedBase64(),r={claimCode:this.#a,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#u(){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 Gt="qrcode",Q=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(Gt),this.#t.src=e,new Promise((n,r)=>{this.#t.onload=n,this.#t.onerror=r})}};function Z(o){if(o===null)return o;if(Array.isArray(o))return o.map(Z);if(typeof o=="object"){let t={};for(let[e,n]of Object.entries(o))t[e]=Z(n);return t}return o}function Ye(o){return o!==null}function qe(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 Xe(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 ze(o,t=[]){return new Response(null,{status:302,headers:{Location:o.toString(),"Access-Control-Allow-Origin":"*",...t}})}function Qe(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 tt="daemon",Kt="config",Ft="event",et={Config:"config"},ot="config",Bt=1e3*60*60,d=class o extends EventTarget{static#t=new o;static Ev=et;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==`/${tt}/${Kt}`&&t.headers.get("Content-Type")=="application/json"}static isEvent(t){let e=new URL(t.url);return t.method=="POST"&&e.pathname==`/${tt}/${Ft}`&&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(ot,JSON.stringify(e)),this.dispatchEvent(new CustomEvent(et.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(ot);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=Bt){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}`,j=Date.now(),Pt={iss:c,aud:h,sub:m,src:n??`${u.origin}${u.pathname}${u.search}`,scope:t,iat:j,exp:j+r},V=new p(Pt);return await V.signWith(o.getPrivateKey()),V}static async getStorage(){let t=await o.getStorageToken();return new T(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 S=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:Vt(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 Ht(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 Ht(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 Vt(o){let t=new Headers(o);return t.set("Content-Type","application/json"),t}var E=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(Yt).filter(nt),...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 S({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)}},N=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)}},I=class{#t;#e=[];constructor(t){this.#t=new S({post:t.postRpc})}async refreshCatalog(){let t=await this.#t.listTools();this.#e=t.map(_t).filter(nt)}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 _t(o){let t=o;return t?.name?{type:"function",name:t.name,execution:"server",description:t.description,parameters:t.inputSchema}:null}function Yt(o){return o?.tool?.name?{type:"function",name:o.tool.name,execution:"server",description:o.tool.description,parameters:o.tool.inputSchema}:null}function nt(o){return o!=null}var qt="2025-06-18",$={"Access-Control-Allow-Origin":"*"},Wt="POST, GET",rt=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)}},M=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 zt(t);if(t.method==="DELETE"||t.method!=="POST")return it();let e;try{e=await t.json()}catch{return R(J(null,-32700,"Parse error"))}let n=Qt(e.id);if(e.jsonrpc!=="2.0"||typeof e.method!="string")return R(J(n,-32600,"Invalid Request"));if(e.id===void 0)return Xt();try{let r=await this.#s(e.method,e.params,t);return R({jsonrpc:"2.0",id:n,result:r})}catch(r){return r instanceof f?R(J(n,r.code,r.message)):R(J(n,-32603,String(r)))}}async#s(t,e,n){switch(t){case"initialize":return{protocolVersion:qt,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 st(s)}catch(s){if(s instanceof f)throw s;return st({error:String(s)},!0)}}},f=class extends Error{code;constructor(t,e){super(e),this.code=t}};function st(o,t=!1){return{content:[{type:"text",text:typeof o=="string"?o:JSON.stringify(o)}],structuredContent:o,isError:t}}function J(o,t,e){return{jsonrpc:"2.0",id:o,error:{code:t,message:e}}}function R(o){return new Response(JSON.stringify(o),{headers:{...$,"Content-Type":"application/json"}})}function Xt(){return new Response(null,{status:202,headers:$})}function zt(o){let t=new URL(o.url);return new Response(`event: endpoint
|
|
7
|
+
-----END PRIVATE KEY-----`}};async function Fe(){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 z=class{#t;#e;#o;#s;#n;#r;#a;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.#u()}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:d,_checkState:j}=h.ok;return this.#a=d,h.ok}async checkState(){if(!this.#r||!this.#a)throw"checkState: No token or claimCode";let t=new URL(`${this.#r.getAud()}/offer/check`);t.searchParams.append("claimCode",this.#a);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.#a)throw"confirmClaim: No token or claimCode";let e=new URL(`${this.#r.getAud()}/offer/confirm`),n=await this.#r.asSignedBase64(),r={claimCode:this.#a,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#u(){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 Dt="qrcode",Q=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(Dt),this.#t.src=e,new Promise((n,r)=>{this.#t.onload=n,this.#t.onerror=r})}};function Z(o){if(o===null)return o;if(Array.isArray(o))return o.map(Z);if(typeof o=="object"){let t={};for(let[e,n]of Object.entries(o))t[e]=Z(n);return t}return o}function Ye(o){return o!==null}function qe(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 Xe(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 ze(o,t=[]){return new Response(null,{status:302,headers:{Location:o.toString(),"Access-Control-Allow-Origin":"*",...t}})}function Qe(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 tt="daemon",Gt="config",Kt="event",et={Config:"config"},ot="config",Ft=1e3*60*60,m=class o extends EventTarget{static#t=new o;static Ev=et;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==`/${tt}/${Gt}`&&t.headers.get("Content-Type")=="application/json"}static isEvent(t){let e=new URL(t.url);return t.method=="POST"&&e.pathname==`/${tt}/${Kt}`&&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(ot,JSON.stringify(e)),this.dispatchEvent(new CustomEvent(et.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(ot);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=Ft){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}`,d=`${i}//${a}`,j=Date.now(),Pt={iss:c,aud:h,sub:d,src:n??`${u.origin}${u.pathname}${u.search}`,scope:t,iat:j,exp:j+r},V=new p(Pt);return await V.signWith(o.getPrivateKey()),V}static async getStorage(){let t=await o.getStorageToken();return new T(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 S=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:Ht(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 Bt(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 Bt(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 Ht(o){let t=new Headers(o);return t.set("Content-Type","application/json"),t}var E=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(_t).filter(nt),...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 S({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)}},N=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)}},I=class{#t;#e=[];constructor(t){this.#t=new S({post:t.postRpc})}async refreshCatalog(){let t=await this.#t.listTools();this.#e=t.map(Vt).filter(nt)}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 Vt(o){let t=o;return t?.name?{type:"function",name:t.name,execution:"server",description:t.description,parameters:t.inputSchema}:null}function _t(o){return o?.tool?.name?{type:"function",name:o.tool.name,execution:"server",description:o.tool.description,parameters:o.tool.inputSchema}:null}function ao(o,t){return!t||t===o?o:o?`${t}
|
|
8
|
+
|
|
9
|
+
${o}`:t}function nt(o){return o!=null}var Yt="2025-06-18",M={"Access-Control-Allow-Origin":"*"},qt="POST, GET",rt=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 Xt(t);if(t.method==="DELETE"||t.method!=="POST")return it();let e;try{e=await t.json()}catch{return R(J(null,-32700,"Parse error"))}let n=zt(e.id);if(e.jsonrpc!=="2.0"||typeof e.method!="string")return R(J(n,-32600,"Invalid Request"));if(e.id===void 0)return Wt();try{let r=await this.#s(e.method,e.params,t);return R({jsonrpc:"2.0",id:n,result:r})}catch(r){return r instanceof f?R(J(n,r.code,r.message)):R(J(n,-32603,String(r)))}}async#s(t,e,n){switch(t){case"initialize":return{protocolVersion:Yt,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 st(s)}catch(s){if(s instanceof f)throw s;return st({error:String(s)},!0)}}},f=class extends Error{code;constructor(t,e){super(e),this.code=t}};function st(o,t=!1){return{content:[{type:"text",text:typeof o=="string"?o:JSON.stringify(o)}],structuredContent:o,isError:t}}function J(o,t,e){return{jsonrpc:"2.0",id:o,error:{code:t,message:e}}}function R(o){return new Response(JSON.stringify(o),{headers:{...M,"Content-Type":"application/json"}})}function Wt(){return new Response(null,{status:202,headers:M})}function Xt(o){let t=new URL(o.url);return new Response(`event: endpoint
|
|
8
10
|
data: ${t.pathname}
|
|
9
11
|
|
|
10
|
-
`,{headers:{
|
|
11
|
-
When answering expert MCP questions, prefer semantic and structured memory under root "${s}" when looking up remembered information.`:"",a=await this.#G(t,n);return`${r}${i}${a}`}async#G(t,e){try{let n=
|
|
12
|
+
`,{headers:{...M,"Content-Type":"text/event-stream","Cache-Control":"no-cache"}})}function it(){return new Response(null,{status:405,headers:{...M,Allow:qt}})}function zt(o){return typeof o=="string"||typeof o=="number"||o===null?o:null}var Qt="useTools",lt=/[\s,;\|]+/,U=class{#t;constructor(t){this.#t=t}hasCapability(t,e){return(this.#t.scope[t]??"").split(lt).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 ut(o){let{token:t,providerSource:e,toolName:n,capability:r,providerYaml:s}=o;if(!t)return!1;if(at(t,e,r))return!0;if(!s)return!1;let i=String(t.getSourceUrl());return at(t,i,Qt)&&Zt(s,n,r)&&te(s.grant,t.getCounterparty(),t.getSrc(),r)}function at(o,t,e){try{return o.hasCapability(t,e)}catch{return!1}}function Zt(o,t,e){return!!o.mcp?.tools.some(n=>n.name===t&&n.capability===e)}function te(o,t,e,n){for(let r in o??{}){if(!ct(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(ct(e,i)&&ee(s[i]).includes(n))return!0}return!1}function ee(o){return o.split(lt).filter(t=>t.length>0)}function ct(o,t){return new RegExp(`^${t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&").replace(/%/g,".*").replace(/_/g,".")}$`).test(o)}var oe=4,ne=.2,ht="x-tabserver-token",L="x-tabserver-oauth-context",yt="x-tabserver-counterparty",H="expert_ask",pt="expertAsk",gt=class{#t;#e;#o;#s;#n;#r;#a=new Map;#u=new m;#i;#l;constructor(t){this.#t=t.targetScope,this.#e=t.defaultSystemPrompt,this.#o=t.defaultTemperature??ne,this.#s=t.maxToolRounds??oe,this.#n=t.mcpPath??"/rpc",this.#r=t.resolveToolProviders,this.#i={temperature:this.#o,toolsEnabled:!0,toolDetails:!0,thinking:t.defaultThinking??!1},this.#l=new $(new B(this),{context:fe,serverInfo:{name:"ai-chat-expert",version:"1"}})}async handler(t){if(m.shouldHandle(t))return await this.#u.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.#l.handle(t);switch(e.pathname){case"/session":return g({ok:await this.#C(t)});case"/settings":return g({ok:await this.#f(t)});case"/models":return g({ok:await this.#y(t)});case"/chats":return g({ok:await this.#I(t)});case"/send":return g({ok:await this.#g(t)});case"/client-tool-results":return g({ok:await this.#d(t)});case"/close":return g({ok:await this.#J(t)});case"/delete":return g({ok:await this.#$(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.#N(void 0),s=w(t),i=await this.#x(r,{expert:!0},s),a=this.#i.temperature,c=await this.#w(t,r,void 0,i,a);G(c),c.toolRounds=0,await this.#T(c);let l=[];try{let u=await this.#c(c.targetParty,"/ai/chat/send",{chatId:c.chatId,message:n,options:this.#S(c,this.#i.toolsEnabled,a)},c.forward),h=await this.#p(c,u.message,this.#i.toolsEnabled,!1,a,l);if(h.clientToolCalls?.length)throw"Expert MCP cannot execute browser client tools";let d=we(h.events);if(!d)throw"Expert did not return an answer";return{answer:d}}finally{let u=x(t,c.targetParty,c.chatId);this.#a.delete(u),await this.#c(c.targetParty,"/ai/chat/close",{chatId:c.chatId},c.forward).catch(h=>console.error(h))}}async#g(t){let e=await dt(t);if(typeof e.message!="string"||!e.message.trim())throw"Message is required";let n=this.#v(e),r=this.#E(e),s=this.#P(e),i=this.#h(e),a=P(e),c=await this.#w(t,i,a,void 0,s,this.#O(e));G(c),c.toolRounds=0,mt(c,e),n&&await this.#T(c);let l=[],u=await this.#c(c.targetParty,"/ai/chat/send",{chatId:c.chatId,message:e.message.trim(),options:this.#S(c,n,s)},c.forward);return await this.#p(c,u.message,n,r,s,l)}async#d(t){let e=await dt(t),n=this.#v(e),r=this.#E(e),s=this.#P(e),i=this.#h(e),a=P(e),c=await this.#w(t,i,a,void 0,s);mt(c,e);let l=se(c,e.toolResults),u=[];for(let d of l)F(u,d.name??"client_tool",d.content,r,d.isError);let h=[...c.pendingToolResults,...l];return G(c),await this.#m(c,h,n,r,s,u)}async#p(t,e,n,r,s,i){let a=e;for(;;){ue(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 le(t,c,r,i);if(u.length)return t.pendingClientToolCalls=new Map(u.map(d=>[d.id,d])),t.pendingToolResults=l,{events:i,clientToolCalls:u};let h=await this.#m(t,l,n,r,s,i);return h.clientToolCalls?.length,h}}async#m(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.#S(t,n,s)},t.forward);return await this.#p(t,a.message,n,r,s,i)}async#C(t){let e=await C(t),n=this.#h(e),r=P(e),s=w(t),i=await this.#w(t,n,r,r?void 0:await this.#D(e,n,s),this.#P(e),this.#O(e));return wt(i,e.clientTools),await this.#T(i),{chatId:i.chatId,history:await this.#M(i),closed:r?await this.#U(n,r,s):null}}async#f(t){let e=await C(t);return Object.keys(e).length&&this.#B(e),this.#F()}async#y(t){let e=await C(t),n=this.#h(e),r=await this.#c(n,"/ai/chat/models",{},w(t));return{models:Array.isArray(r.models)?r.models:[]}}async#I(t){let e=await C(t),n=this.#h(e),r=await this.#c(n,"/ai/chat/list",{},w(t));return{chats:Array.isArray(r.chats)?r.chats:[]}}async#J(t){let e=await C(t),n=this.#h(e),r=P(e);if(!r)throw"chatId is required";let s=w(t),i=x(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#$(t){let e=await C(t),n=this.#h(e),r=P(e);if(!r)throw"chatId is required";let s=x(t,n,r);return await this.#c(n,"/ai/chat/delete",{chatId:r},w(t)),this.#a.delete(s),!0}async#w(t,e,n,r,s=this.#o,i){let a=x(t,e,n);if(n){let h=this.#a.get(a);if(h)return h}let c=w(t),l=n?{id:n}:await this.#c(e,"/ai/chat/create",{system:r??await this.#x(e,{expert:!1},c),temperature:s,...i?{model:i}:{}},c);a=x(t,e,String(l.id));let u={chatId:String(l.id),targetParty:e,forward:c,toolRuntime:await this.#L(e,c),clientTools:[],clientToolNames:new Set,pendingClientToolCalls:new Map,pendingToolResults:[],toolRounds:0};return await this.#T(u),this.#a.set(a,u),u}async#M(t){let e=await this.#c(t.targetParty,"/ai/chat/history",{chatId:t.chatId},t.forward);return Array.isArray(e.messages)?e.messages:[]}async#U(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}#k(t,e,n){return new E({postParty:(r,s)=>this.#c(t,r,s,e,n),postPartyRaw:(r,s)=>this.#R(t,r,s,e,n)})}async#L(t,e){let n=await this.#r?.(t,e);if(!n?.length)return this.#k(t,e);let r=m.getConfig().system.party;return new N(n.map(s=>({key:s.party,runtime:s.party===r?this.#k(s.party,e,s.scope):this.#j(s.party,e),allowedTools:s.tools?new Set(s.tools):void 0})))}#j(t,e){return new I({postRpc:n=>this.#R(t,"/mcp/rpc",n,e)})}async#T(t){await t.toolRuntime.refreshCatalog()}#S(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.#R(t,e,n,r,s);if("error"in i)throw i.error;return i.ok}async#R(t,e,n,r,s){let i=m.getConfig(),a=await m.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[L]=r.oauthContext),r?.counterpartyContext&&(l[yt]=r.counterpartyContext),await(await fetch(c,{method:"POST",headers:l,body:JSON.stringify(n)})).json()}async#D(t,e,n){let r=typeof t.systemPrompt=="string"&&t.systemPrompt.trim()?t.systemPrompt.trim():this.#A();return await this.#x(e,{basePrompt:r,expert:!1},n)}#O(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}#v(t){return typeof t.toolsEnabled=="boolean"?t.toolsEnabled:this.#i.toolsEnabled}#E(t){return typeof t.toolDetails=="boolean"?t.toolDetails:this.#i.toolDetails}#h(t){return this.#N(t.targetParty)}#N(t){let{system:{party:e,protocol:n}}=m.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#x(t,e,n){let r=e.basePrompt??this.#A(),s=await Se(),i=e.expert&&s?`
|
|
13
|
+
When answering expert MCP questions, prefer semantic and structured memory under root "${s}" when looking up remembered information.`:"",a=await this.#G(t,n);return`${r}${i}${a}`}async#G(t,e){try{let n=Tt(),r=n?await this.#K(t,n,e):await this.#b(t,{},e);return Te(r)}catch{return`
|
|
12
14
|
|
|
13
|
-
Semantic memory catalog is unavailable for this session.`}}async#K(t,e,n){let r=await this.#b(t,{root:e},n);return r.length?r:await this.#b(t,{},n)}async#b(t,e,n){let r=await this.#c(t,"/ai/memory/catalog",{kind:"semantic",...e.root?{root:e.root}:{}},n);return Array.isArray(r.resources)?r.resources:[]}#
|
|
15
|
+
Semantic memory catalog is unavailable for this session.`}}async#K(t,e,n){let r=await this.#b(t,{root:e},n);return r.length?r:await this.#b(t,{},n)}async#b(t,e,n){let r=await this.#c(t,"/ai/memory/catalog",{kind:"semantic",...e.root?{root:e.root}:{}},n);return Array.isArray(r.resources)?r.resources:[]}#A(){return this.#i.systemPrompt??this.#e}#F(){return{...this.#i,systemPrompt:this.#A()}}#B(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 dt(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 C(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 mt(o,t){Object.hasOwn(t,"clientTools")&&wt(o,t.clientTools)}function wt(o,t){let e=Array.isArray(t)?t.filter(re):[];o.clientTools=e,o.clientToolNames=new Set(e.map(n=>n.name))}function re(o){return!!o&&typeof o=="object"&&!Array.isArray(o)&&o.type==="function"&&typeof o.name=="string"&&o.execution==="client"}function se(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(!ie(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 ie(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 G(o){o.pendingClientToolCalls.clear(),o.pendingToolResults=[]}function P(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 x(o,t,e){return`${ae(o)}
|
|
14
16
|
${t}
|
|
15
|
-
${e??"default"}`}function
|
|
17
|
+
${e??"default"}`}function ae(o){let t=o.headers.get(ht);if(t)return t;let e=o.headers.get(L);if(e)return e;throw`This request must provide a token in '${ht}'`}function w(o){let t=o.headers.get(L);return{oauthContext:t,counterpartyContext:ce(o,t)}}function ce(o,t){let e=o.headers.get(yt);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 le(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:pe(i,a)}),a){s.push(i);continue}try{let c=await o.toolRuntime.executeToolCall(i);F(n,i.name,c,e),r.push({toolCallId:i.id,name:i.name,content:c})}catch(c){let l=String(c);F(n,i.name,l,e,!0),r.push({toolCallId:i.id,name:i.name,content:l,isError:!0})}}return{toolResults:r,clientToolCalls:s}}function ue(o,t){he(o,t.reasoning),t.content?o.push({role:"assistant",content:t.content}):t.toolCalls?.length||o.push({role:"assistant",content:""})}function he(o,t){for(let e of t??[])e.redacted||!e.text||o.push({role:"thinking",content:e.text})}function pe(o,t){return`${t?"Client tool call":"Agent tool call"}: ${o.name}
|
|
16
18
|
${JSON.stringify(o.input,null,2)}`}function F(o,t,e,n,r=!1){n&&o.push({role:"tool-result",content:ge(t,e,r),...r?{error:!0}:{}})}function ge(o,t,e=!1){return`${e?"Tool error":"Tool result"}: ${o}
|
|
17
|
-
${t}`}var B=class{#t;constructor(t){this.#t=t}listTools(){return[
|
|
19
|
+
${t}`}var B=class{#t;constructor(t){this.#t=t}listTools(){return[de()]}async callTool(t,e,n){if(t!==H)throw new f(-32602,`Unknown tool: ${t}`);if(me(n),typeof e.question!="string"||!e.question.trim())throw"question is required";return await this.#t.askExpert(n.request,e.question)}};function de(){return{name:H,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 me(o){if(!ut({token:o.token,providerSource:o.source,toolName:H,capability:pt,providerYaml:o.yaml}))throw`Missing capability: ${pt}`}function fe(o){let{system:{party:t,source:e,protocol:n,sourcePrefix:r},content:{yaml:s}}=m.getConfig();return{party:t,source:e,protocol:n,sourcePrefix:r,yaml:s,token:ye(o),request:o}}function ye(o){let t=p.from(o);if(t)return t;let e=o.headers.get(L);return e?new U(JSON.parse(e)):null}function we(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 Te(o){if(!o.length)return`
|
|
18
20
|
|
|
19
21
|
Available semantic memories:
|
|
20
|
-
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=K(e.root),r=K(e.collection);if(!n||!r)continue;t.push(`- root: ${n}`),t.push(` collection: ${r}`);let s=K(e.description);s&&t.push(` description: ${s}`);let i=
|
|
21
|
-
`)}function K(o){return typeof o=="string"?o:""}function
|
|
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=K(e.root),r=K(e.collection);if(!n||!r)continue;t.push(`- root: ${n}`),t.push(` collection: ${r}`);let s=K(e.description);s&&t.push(` description: ${s}`);let i=ft(e.schema),a=Ce(i?.textProperties);a.length&&t.push(` textProperties: ${a.join(", ")}`);let c=ft(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(`
|
|
23
|
+
`)}function K(o){return typeof o=="string"?o:""}function ft(o){return o&&typeof o=="object"&&!Array.isArray(o)?o:null}function Ce(o){return Array.isArray(o)?o.filter(t=>typeof t=="string"):[]}function Tt(){let{system:{source:o}}=m.getConfig();return new URL(o).searchParams.get("root")?.trim().replace(/^\/+|\/+$/g,"")||void 0}async function Se(){let{system:{source:o}}=m.getConfig(),t=new URL(o),e=Tt();return e||(t.hash="",await b(t))}var Ct="x-forwarded-host",St="x-forwarded-proto",Rt="x-forwarded-pathname";function Re(o){return o.headers.has(St)?o.headers.get(St)+":":new URL(o.url).protocol}function Pe(o){return o.headers.has(Ct)?o.headers.get(Ct)||"":new URL(o.url).host}function xe(o){return o.headers.has(Rt)?o.headers.get(Rt)||"":new URL(o.url).pathname}function be(o){let t=Re(o),e=Pe(o),n=xe(o);return new URL(`${t}//${e}${n}`)}function Co(o){let t=new URL(o.url),e=be(o);return e.search=t.search,e.hash=t.hash,e}function So(o){return new URL(o.url).pathname}function Ro(o,t){let e=Ae(o);return t in e?e[t]:null}function Ae(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{E as AgentToolRuntime,N as AggregateToolRuntime,y as BrowserApp,Gt as CONFIG_PATH,v as ClientToolRegistry,tt as DAEMON_PREFIX,Kt as EVENT_PATH,I as GatewayToolRuntime,gt as GenerativeChatAgent,W as GenerativeChatClient,Dt as ImgClass,S as JsonRpcMcpClient,$ as JsonRpcMcpServer,X as KeyPair,m as Lifecycle,rt as McpToolRegistry,U as OAuthMcpToken,z as ParentHelper,f as ProtocolError,Q as QrCode,T as Storage,p as Token,Qt as USE_TOOLS_CAPABILITY,Qe as buildCookie,Z as cloneJSONValue,ao as composeToolDescription,je as createDefaultBrowserChatTools,b as getAppId,Pe as getClientHost,xe as getClientPathname,Re as getClientProtocol,Co as getClientUrl,be as getClientUrlPhp,Ro as getCookie,Ae as getCookies,It as getItem,Jt as getItemsLike,Fe as getLaunchAlert,So as getPathname,ut as hasMcpToolCapability,qe as isDefined,Bt as mcpToolErrorMessage,st as mcpToolResult,Ye as notNull,$t as removeItem,g as response,ze as response302,Xe as response404,Nt as setItem,ke as shortHexDigest,xt as shortSafeDigest,_t as toChatTool,at as tokenHasCapability,Zt as yamlDeclaresMcpToolCapability,te as yamlGrantsCapability};
|
|
22
24
|
//# sourceMappingURL=index.js.map
|