{"version":3,"file":"tool-calls.cjs","names":["namespaceKey","ToolCallAssembler","shouldIgnoreScopedTaskToolEvent","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 {\n  shouldIgnoreScopedTaskToolEvent,\n  ToolCallAssembler,\n} 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        if (shouldIgnoreScopedTaskToolEvent(ns, event)) return;\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      let runtime: ProjectionRuntime | undefined;\n      const openSubscription = () => {\n        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\n      let disposed = false;\n      void (async () => {\n        const seeded =\n          (await rootBus.trySeedFromHistory?.({\n            kind: \"toolCalls\",\n            namespace: ns,\n            store,\n          })) === true;\n        if (!seeded && !disposed) openSubscription();\n      })();\n\n      return {\n        async dispose() {\n          disposed = true;\n          await runtime?.dispose();\n        },\n      };\n    },\n  };\n}\n"],"mappings":";;;;;AAqBA,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;AACnD,QAAIC,cAAAA,gCAAgC,IAAI,MAAM,CAAE;IAChD,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,IAAI;GACJ,MAAM,yBAAyB;AAC7B,cAAUC,gBAAAA,2BAA2B;KACnC;KACA,UAAU,CAAC,QAAQ;KACnB,WAAW;KACX,QAAQ,OAAO;AACb,UAAI,MAAM,WAAW,QAAS;AAC9B,sBAAgB,MAAoB;;KAEvC,CAAC;;GAGJ,IAAI,WAAW;AACf,IAAM,YAAY;AAOhB,QAAI,EALD,MAAM,QAAQ,qBAAqB;KAClC,MAAM;KACN,WAAW;KACX;KACD,CAAC,KAAM,SACK,CAAC,SAAU,mBAAkB;OAC1C;AAEJ,UAAO,EACL,MAAM,UAAU;AACd,eAAW;AACX,UAAM,SAAS,SAAS;MAE3B;;EAEJ"}