const require_tools = require("../../client/stream/handles/tools.cjs");
const require_namespace = require("../namespace.cjs");
const require_tool_calls = require("../tool-calls.cjs");
const require_runtime = require("./runtime.cjs");
//#region src/stream/projections/tool-calls.ts
function toolCallsProjection(namespace) {
	const ns = [...namespace];
	return {
		key: `toolCalls|${require_namespace.namespaceKey(ns)}`,
		namespace: ns,
		initial: [],
		open({ thread, store, rootBus }) {
			const assembler = new require_tools.ToolCallAssembler();
			const applyToolsEvent = (event) => {
				if (require_tools.shouldIgnoreScopedTaskToolEvent(ns, event)) return;
				const tc = assembler.consume(event);
				if (tc == null) return;
				const next = require_tool_calls.upsertToolCall(store.getSnapshot(), tc);
				store.setValue(next);
			};
			if (require_namespace.isRootNamespace(ns) && rootBus.channels.includes("tools")) {
				const unsubscribe = rootBus.subscribe((event) => {
					if (event.method !== "tools") return;
					if (!require_namespace.isRootNamespace(event.params.namespace)) return;
					applyToolsEvent(event);
				});
				return { dispose() {
					unsubscribe();
				} };
			}
			let runtime;
			const openSubscription = () => {
				runtime = require_runtime.openProjectionSubscription({
					thread,
					channels: ["tools"],
					namespace: ns,
					onEvent(event) {
						if (event.method !== "tools") return;
						applyToolsEvent(event);
					}
				});
			};
			let disposed = false;
			(async () => {
				if (!(await rootBus.trySeedFromHistory?.({
					kind: "toolCalls",
					namespace: ns,
					store
				}) === true) && !disposed) openSubscription();
			})();
			return { async dispose() {
				disposed = true;
				await runtime?.dispose();
			} };
		}
	};
}
//#endregion
exports.toolCallsProjection = toolCallsProjection;

//# sourceMappingURL=tool-calls.cjs.map