eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 1.63 kB
JavaScript
import{McpConnectionClient}from"#runtime/connections/mcp-client.js";import{OpenApiConnectionClient}from"#runtime/connections/openapi-client.js";var ConnectionRegistryImpl=class{#e=new Map;#t;#n;#r=new Map;#i=new Map;constructor(e){this.#n=e,this.#t=e}async replaceDynamicConnections(e,t){let n=e===`session`?new Map(t):this.#r,r=e===`turn`?new Map(t):this.#i,i=this.#a(n,r),a=new Map(this.#t.map(e=>[e.connectionName,e])),o=new Map(i.map(e=>[e.connectionName,e])),s=[];for(let[e,t]of this.#e)a.get(e)!==o.get(e)&&(this.#e.delete(e),s.push(t));this.#r=n,this.#i=r,this.#t=i,await Promise.allSettled(s.map(e=>e.close()))}#a(e,t){let n=new Map(e);for(let[e,r]of t)n.set(e,r);let r=new Map,i=new Map(this.#n.map(e=>[e.connectionName,e]));for(let[e,t]of n)for(let n of t){let t=r.get(n.connectionName);if(t!==void 0&&t!==e)throw Error(`Dynamic connection "${n.connectionName}" from resolver "${e}" collides with dynamic resolver "${t}". Namespace the map key manually.`);r.set(n.connectionName,e),i.set(n.connectionName,n)}return[...i.values()]}getClient(e){let t=this.#e.get(e);if(t!==void 0)return t;let n=this.#t.find(t=>t.connectionName===e);if(n===void 0)throw Error(`Connection "${e}" is not registered.`);let r=n.protocol===`openapi`?new OpenApiConnectionClient(n):new McpConnectionClient(n);return this.#e.set(e,r),r}getConnectionApproval(e){return this.#t.find(t=>t.connectionName===e)?.approval}getConnectionNames(){return this.#t.map(e=>e.connectionName)}getConnections(){return this.#t}async dispose(){let e=[...this.#e.values()].map(e=>e.close());await Promise.allSettled(e),this.#e.clear()}};export{ConnectionRegistryImpl};