import { resolveEffectiveAgentProfileForSession } from "../../config/agent-profile.js";
import { shouldCreateBundleMcpRuntimeForAttempt } from "./bundle-mcp-policy.js";
import { getOrCreateSessionMcpRuntime } from "./bundle-mcp-runtime.js";
import { materializeBundleMcpToolsForRun } from "./bundle-mcp-materialize.js";
import "./bundle-mcp-tools.js";
//#region src/agent/mcp/resolve-embedded-mcp-tools.ts
async function resolveEmbeddedMcpToolsForTurn(params) {
	const disabledTools = (params.cfg ? resolveEffectiveAgentProfileForSession(params.cfg, params.sessionKey) : void 0)?.tools.disable;
	if (!shouldCreateBundleMcpRuntimeForAttempt({
		cfg: params.cfg,
		disabledTools
	})) return {
		tools: [],
		dispose: async () => {}
	};
	const reserved = new Set(params.baseTools.map((t) => t.name));
	const runtime = await getOrCreateSessionMcpRuntime({
		sessionId: params.sessionKey,
		sessionKey: params.sessionKey,
		workspaceDir: params.workspaceDir,
		cfg: params.cfg
	});
	let materialized;
	try {
		materialized = await materializeBundleMcpToolsForRun({
			runtime,
			reservedToolNames: reserved,
			disposeRuntime: params.cleanupOnTurnEnd ? async () => {
				await runtime.dispose();
			} : void 0
		});
	} catch (error) {
		await runtime.dispose().catch(() => {});
		throw error;
	}
	return {
		tools: materialized.tools.filter((tool) => {
			if (disabledTools?.has(tool.name)) return false;
			return true;
		}),
		dispose: materialized.dispose
	};
}
function mergeTurnTools(baseTools, mcpTools) {
	if (mcpTools.length === 0) return baseTools;
	return [...baseTools, ...mcpTools];
}
//#endregion
export { mergeTurnTools, resolveEmbeddedMcpToolsForTurn };

//# sourceMappingURL=resolve-embedded-mcp-tools.js.map