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 type { JsonValue } from 'eve/connections'; |
| 2 | |
| 3 | /** |
| 4 | * eve rejects a dynamic tool whose callbacks carry no durable descriptor: each |
| 5 | * callback must be a JSON-serializable closure plus a function that takes that |
| 6 | * closure as its first argument, so a parked or replayed call can be rebuilt |
| 7 | * after the process that resolved it is gone. eve's build transform stamps the |
| 8 | * descriptor onto `defineTool` calls it finds in the agent's own source, and it |
| 9 | * never runs on this package inside `node_modules` — so the provider stamps its |
| 10 | * own descriptors here. |
| 11 | * |
| 12 | * The descriptor key is a global-registry symbol, the same identity eve's |
| 13 | * runtime reads, so no eve internal has to be imported. |
| 14 | */ |
| 15 | const DURABLE_CALLBACK = Symbol.for('eve:durable-dynamic-callback'); |
| 16 | |
| 17 | /** Snapshot persisted alongside a dynamic tool; must stay JSON-serializable. */ |
| 18 | export type DurableClosure = Record<string, JsonValue>; |
| 19 | |
| 20 | /** |
| 21 | * Binds `callback` to `closure` and stamps eve's durable descriptor on the |
| 22 | * result. eve registers the callback under the tool's name at resolve time, |
| 23 | * persists only `closure`, and calls `callback(closure, ...args)` on replay; |
| 24 | * direct callers get the ordinary `(...args)` signature. |
| 25 | */ |
| 26 | export function withDurableClosure< |
| 27 | TClosure extends DurableClosure, |
| 28 | TArgs extends unknown[], |
| 29 | TResult, |
| 30 | >( |
| 31 | closure: TClosure, |
| 32 | callback: (closure: TClosure, ...args: TArgs) => TResult |
| 33 | ): (...args: TArgs) => TResult { |
| 34 | const live = (...args: TArgs): TResult => callback(closure, ...args); |
| 35 | Object.defineProperty(live, DURABLE_CALLBACK, { |
| 36 | configurable: true, |
| 37 | value: { callback, closure }, |
| 38 | }); |
| 39 | return live; |
| 40 | } |