|
| 1 | +import { |
| 2 | + createAgent, |
| 3 | + type Agent, |
| 4 | + type AgentDefinition, |
| 5 | + type BaseEnv, |
| 6 | +} from "@intx/agent"; |
| 7 | + |
| 8 | +// XXX — @intx/agent resolveTools snapshots `byName` from each bundle's |
| 9 | +// definitions at createAgent and never consults a live getter. MCP tools |
| 10 | +// arrive later via DynamicToolRunner.addTools (servers connect after the TUI |
| 11 | +// is up; one OAuth-blocked server can also stall the post-connect reload |
| 12 | +// that would rebuild the snapshot). A miss then returns `unknown tool` |
| 13 | +// even though tool_search already listed the name from the live runner. |
| 14 | +// |
| 15 | +// resolveTools is not exported. During its synchronous walk it does |
| 16 | +// `new Map()` for that snapshot; we install a Map whose get() falls back |
| 17 | +// to the single live tool bundle so late names reach DynamicToolRunner.run. |
| 18 | +// Restore Map before createAgent awaits so only that snapshot is live. |
| 19 | +// Drop this wrapper when @intx/agent dispatches through the bundle's |
| 20 | +// current definitions (the characterization test in |
| 21 | +// tests/integration/mcp-late-dispatch.test.ts will fail first). |
| 22 | + |
| 23 | +const OriginalMap = globalThis.Map; |
| 24 | + |
| 25 | +export function isLiveToolBundle(value: unknown): boolean { |
| 26 | + if (typeof value !== "object" || value === null) return false; |
| 27 | + const candidate = value as Record<string, unknown>; |
| 28 | + return ( |
| 29 | + typeof candidate.addTools === "function" && |
| 30 | + typeof candidate.currentDefinitions === "function" && |
| 31 | + typeof candidate.run === "function" |
| 32 | + ); |
| 33 | +} |
| 34 | + |
| 35 | +export function fallbackLiveToolBundle<V>(map: Map<unknown, V>): V | undefined { |
| 36 | + let found: V | undefined; |
| 37 | + for (const value of map.values()) { |
| 38 | + if (!isLiveToolBundle(value)) continue; |
| 39 | + if (found !== undefined && found !== value) return undefined; |
| 40 | + found = value; |
| 41 | + } |
| 42 | + return found; |
| 43 | +} |
| 44 | + |
| 45 | +function createLiveDispatchMap<K, V>(iterable?: Iterable<readonly [K, V]> | null): Map<K, V> { |
| 46 | + const map = new OriginalMap<K, V>(iterable ?? undefined); |
| 47 | + const protoGet = OriginalMap.prototype.get.bind(map); |
| 48 | + map.get = (key: K) => { |
| 49 | + const hit = protoGet(key); |
| 50 | + if (hit !== undefined || typeof key !== "string") return hit; |
| 51 | + return fallbackLiveToolBundle(map) ?? hit; |
| 52 | + }; |
| 53 | + return map; |
| 54 | +} |
| 55 | + |
| 56 | +// Compatible with `new Map()` inside published @intx/agent. Not a class — |
| 57 | +// we only need a constructable that returns a Map with a live get(). |
| 58 | +const LiveDispatchMap = Object.assign( |
| 59 | + function LiveDispatchMap<K, V>(iterable?: Iterable<readonly [K, V]> | null): Map<K, V> { |
| 60 | + return createLiveDispatchMap(iterable); |
| 61 | + }, |
| 62 | + { prototype: OriginalMap.prototype }, |
| 63 | +) as unknown as MapConstructor; |
| 64 | + |
| 65 | +export function withLiveToolDispatchMap<T>(fn: () => T): T { |
| 66 | + const previous = globalThis.Map; |
| 67 | + globalThis.Map = LiveDispatchMap; |
| 68 | + try { |
| 69 | + return fn(); |
| 70 | + } finally { |
| 71 | + globalThis.Map = previous; |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +export function createAgentWithLiveToolDispatch<EnvReq extends BaseEnv>( |
| 76 | + def: AgentDefinition<EnvReq>, |
| 77 | + env: EnvReq, |
| 78 | +): Promise<Agent> { |
| 79 | + return withLiveToolDispatchMap(() => createAgent(def, env)); |
| 80 | +} |
0 commit comments