Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { defineDynamic, type DynamicResolveContext } from 'eve/tools'; |
| 2 | import type { EveToolCollection } from './provider'; |
| 3 | |
| 4 | type EveSession = { tools: () => Promise<EveToolCollection> }; |
| 5 | type EveSessionSource<S extends EveSession> = |
| 6 | S | Promise<S> | ((context: DynamicResolveContext) => S | Promise<S>); |
| 7 | |
| 8 | // step.started re-evaluates the source each step, so a resolver form follows |
| 9 | // the current principal and a failed fetch retries next step; cached per |
| 10 | // resolved session since session.tools() is a network call, so a fixed |
| 11 | // session's tool set stays stable. Rejected requests are evicted. |
| 12 | export function defineComposioTools<S extends EveSession>(source: EveSessionSource<S>) { |
| 13 | const cache = new WeakMap<S, Promise<EveToolCollection>>(); |
| 14 | |
| 15 | const resolveTools = async (context: DynamicResolveContext): Promise<EveToolCollection> => { |
| 16 | const session = await (typeof source === 'function' ? source(context) : source); |
| 17 | const cached = cache.get(session); |
| 18 | if (cached) return cached; |
| 19 | |
| 20 | const pending = session.tools(); |
| 21 | cache.set(session, pending); |
| 22 | try { |
| 23 | return await pending; |
| 24 | } catch (error) { |
| 25 | if (cache.get(session) === pending) cache.delete(session); |
| 26 | throw error; |
| 27 | } |
| 28 | }; |
| 29 | |
| 30 | return defineDynamic({ |
| 31 | events: { |
| 32 | 'step.started': (_event, context) => resolveTools(context), |
| 33 | }, |
| 34 | }); |
| 35 | } |