{"version":3,"file":"tool-calls.cjs","names":["namespaceKey","ToolCallAssembler","upsertToolCall","isRootNamespace","openProjectionSubscription"],"sources":["../../../src/stream/projections/tool-calls.ts"],"sourcesContent":["/**\n * Namespace-scoped `tools` projection.\n *\n * Opens `thread.subscribe({ channels: [\"tools\"], namespaces: [ns] })`,\n * feeds events through {@link ToolCallAssembler}, and surfaces an\n * array of {@link AssembledToolCall}s that grows as calls are\n * discovered. Each handle exposes reactive {@link AssembledToolCall.status},\n * {@link AssembledToolCall.error}, and {@link AssembledToolCall.output}\n * (`null` until the call succeeds).\n */\nimport type { ToolsEvent } from \"@langchain/protocol\";\nimport { ToolCallAssembler } from \"../../client/stream/handles/tools.js\";\nimport type { AssembledToolCall } from \"../../client/stream/handles/tools.js\";\nimport type { ProjectionSpec, ProjectionRuntime } from \"../types.js\";\nimport { isRootNamespace, namespaceKey } from \"../namespace.js\";\nimport { upsertToolCall } from \"../tool-calls.js\";\nimport { openProjectionSubscription } from \"./runtime.js\";\n\nexport function toolCallsProjection(\n  namespace: readonly string[]\n): ProjectionSpec<AssembledToolCall[]> {\n  const ns = [...namespace];\n  const key = `toolCalls|${namespaceKey(ns)}`;\n\n  return {\n    key,\n    namespace: ns,\n    initial: [],\n    open({ thread, store, rootBus }): ProjectionRuntime {\n      const assembler = new ToolCallAssembler();\n\n      const applyToolsEvent = (event: ToolsEvent): void => {\n        const data = event.params.data;\n        if (\n          ns.length > 0 &&\n          event.params.namespace.length === ns.length &&\n          data.tool_name === \"task\"\n        ) {\n          return;\n        }\n        const tc = assembler.consume(event);\n        if (tc == null) return;\n        const next = upsertToolCall(store.getSnapshot(), tc);\n        store.setValue(next);\n      };\n\n      // See `messagesProjection` — root-scoped projections short-\n      // circuit onto the root bus when the requested channels are\n      // covered by the controller's root pump.\n      const rootShortCircuit =\n        isRootNamespace(ns) && rootBus.channels.includes(\"tools\");\n\n      if (rootShortCircuit) {\n        const unsubscribe = rootBus.subscribe((event) => {\n          if (event.method !== \"tools\") return;\n          if (!isRootNamespace(event.params.namespace)) return;\n          applyToolsEvent(event as ToolsEvent);\n        });\n        return {\n          dispose() {\n            unsubscribe();\n          },\n        };\n      }\n\n      const runtime = openProjectionSubscription({\n        thread,\n        channels: [\"tools\"],\n        namespace: ns,\n        onEvent(event) {\n          if (event.method !== \"tools\") return;\n          applyToolsEvent(event as ToolsEvent);\n        },\n      });\n\n      return {\n        async dispose() {\n          await runtime.dispose();\n        },\n      };\n    },\n  };\n}\n"],"mappings":";;;;;AAkBA,SAAgB,oBACd,WACqC;CACrC,MAAM,KAAK,CAAC,GAAG,UAAU;AAGzB,QAAO;EACL,KAHU,aAAaA,kBAAAA,aAAa,GAAG;EAIvC,WAAW;EACX,SAAS,EAAE;EACX,KAAK,EAAE,QAAQ,OAAO,WAA8B;GAClD,MAAM,YAAY,IAAIC,cAAAA,mBAAmB;GAEzC,MAAM,mBAAmB,UAA4B;IACnD,MAAM,OAAO,MAAM,OAAO;AAC1B,QACE,GAAG,SAAS,KACZ,MAAM,OAAO,UAAU,WAAW,GAAG,UACrC,KAAK,cAAc,OAEnB;IAEF,MAAM,KAAK,UAAU,QAAQ,MAAM;AACnC,QAAI,MAAM,KAAM;IAChB,MAAM,OAAOC,mBAAAA,eAAe,MAAM,aAAa,EAAE,GAAG;AACpD,UAAM,SAAS,KAAK;;AAStB,OAFEC,kBAAAA,gBAAgB,GAAG,IAAI,QAAQ,SAAS,SAAS,QAAQ,EAErC;IACpB,MAAM,cAAc,QAAQ,WAAW,UAAU;AAC/C,SAAI,MAAM,WAAW,QAAS;AAC9B,SAAI,CAACA,kBAAAA,gBAAgB,MAAM,OAAO,UAAU,CAAE;AAC9C,qBAAgB,MAAoB;MACpC;AACF,WAAO,EACL,UAAU;AACR,kBAAa;OAEhB;;GAGH,MAAM,UAAUC,gBAAAA,2BAA2B;IACzC;IACA,UAAU,CAAC,QAAQ;IACnB,WAAW;IACX,QAAQ,OAAO;AACb,SAAI,MAAM,WAAW,QAAS;AAC9B,qBAAgB,MAAoB;;IAEvC,CAAC;AAEF,UAAO,EACL,MAAM,UAAU;AACd,UAAM,QAAQ,SAAS;MAE1B;;EAEJ"}