import { a as normalizeLowercaseStringOrEmpty, c as normalizeOptionalString } from "./string-coerce-DKw2K5wM.js";
import { i as normalizeProviderId, n as findNormalizedProviderValue } from "./provider-id-Dq06Bcx6.js";
import { _ as resolveSessionAgentId } from "./agent-scope-CZ-IyS6E.js";
import { a as resolveAgentDir, o as resolveAgentWorkspaceDir } from "./agent-scope-config-CT5O0iC8.js";
import { a as getActivePluginRegistry } from "./runtime-lhkBPfci.js";
import { _ as normalizeProviderTransportWithPlugin } from "./provider-runtime-BFL8aOfT.js";
import { i as normalizeStaticProviderModelId } from "./model-ref-shared-BgNtEdOM.js";
import { n as extractModelCompat } from "./provider-model-compat-CcFqbs95.js";
import { m as normalizeToolName } from "./tool-policy-0aaOlOoa.js";
import { n as resolveEffectiveToolPolicy } from "./agent-tools.policy-BpoOU1rr.js";
import { i as getPluginToolMeta, t as buildPluginToolMetadataKey } from "./tools-Cyfie2t2.js";
import { E as getChannelAgentToolMeta } from "./agent-tools.before-tool-call-CISgec2q.js";
import { t as createNodmixCodingTools } from "./agent-tools-fN_yDRs4.js";
import { r as resolveToolDisplay } from "./tool-display-DTmCe__J.js";
import { a as resolveBundledStaticCatalogModel, t as resolveModel } from "./model-BVoghDPc.js";
import { a as normalizeAgentRuntimeTools, t as filterRuntimeCompatibleTools } from "./tool-schema-projection-CsvPwf80.js";
import { n as summarizeToolDescriptionText } from "./tool-description-summary-DfZpSQrA.js";
//#region src/agents/tools-effective-inventory-groups.ts
function groupLabel(source) {
	switch (source) {
		case "plugin": return "Connected tools";
		case "channel": return "Channel tools";
		case "mcp": return "MCP server tools";
		default: return "Built-in tools";
	}
}
function buildEffectiveToolInventoryGroups(entries) {
	const groupsBySource = /* @__PURE__ */ new Map();
	for (const entry of entries) {
		const tools = groupsBySource.get(entry.source) ?? [];
		tools.push(entry);
		groupsBySource.set(entry.source, tools);
	}
	return [
		"core",
		"plugin",
		"channel",
		"mcp"
	].map((source) => {
		const tools = groupsBySource.get(source);
		if (!tools || tools.length === 0) return null;
		return {
			id: source,
			label: groupLabel(source),
			source,
			tools
		};
	}).filter((group) => group !== null);
}
//#endregion
//#region src/agents/tools-effective-inventory-build.ts
function resolveEffectiveToolLabel(tool) {
	const rawLabel = normalizeOptionalString(tool.label) ?? "";
	if (rawLabel && normalizeLowercaseStringOrEmpty(rawLabel) !== normalizeLowercaseStringOrEmpty(tool.name)) return rawLabel;
	return resolveToolDisplay({ name: tool.name }).title;
}
function resolveRawToolDescription(tool) {
	return normalizeOptionalString(tool.description) ?? "";
}
function summarizeToolDescription(tool) {
	return summarizeToolDescriptionText({
		rawDescription: resolveRawToolDescription(tool),
		displaySummary: tool.displaySummary
	});
}
function resolveEffectiveToolSource(tool, fallbackTool) {
	const pluginMeta = getPluginToolMeta(tool) ?? (fallbackTool ? getPluginToolMeta(fallbackTool) : void 0);
	if (pluginMeta) {
		if (pluginMeta.pluginId === "bundle-mcp") return {
			source: "mcp",
			pluginId: pluginMeta.pluginId
		};
		return {
			source: "plugin",
			pluginId: pluginMeta.pluginId
		};
	}
	const channelMeta = getChannelAgentToolMeta(tool) ?? (fallbackTool ? getChannelAgentToolMeta(fallbackTool) : void 0);
	if (channelMeta) return {
		source: "channel",
		channelId: channelMeta.channelId
	};
	return { source: "core" };
}
function buildUnsupportedToolSchemaNotice(params) {
	const source = params.tool ? resolveEffectiveToolSource(params.tool, params.fallbackTool) : { source: "core" };
	const owner = source.source === "plugin" && source.pluginId ? ` from plugin "${source.pluginId}"` : source.source === "channel" && source.channelId ? ` from channel "${source.channelId}"` : "";
	return {
		id: `unsupported-tool-schema:${params.diagnostic.toolName}`,
		severity: "warning",
		message: `Tool "${params.diagnostic.toolName}"${owner} has an unsupported runtime input schema (${params.diagnostic.violations.join(", ")}) and was quarantined before model projection. Fix or disable the owner, or remove the tool from active allowlists.`
	};
}
function buildUnsupportedToolSchemaNotices(params) {
	return params.diagnostics.map((diagnostic) => buildUnsupportedToolSchemaNotice({
		diagnostic,
		tool: params.tools[diagnostic.toolIndex],
		fallbackTool: params.rawToolsByName.get(diagnostic.toolName)
	}));
}
function disambiguateLabels(entries) {
	const counts = /* @__PURE__ */ new Map();
	for (const entry of entries) counts.set(entry.label, (counts.get(entry.label) ?? 0) + 1);
	return entries.map((entry) => {
		if ((counts.get(entry.label) ?? 0) < 2) return entry;
		const suffix = entry.pluginId ?? entry.channelId ?? entry.id;
		return {
			...entry,
			label: `${entry.label} (${suffix})`
		};
	});
}
function buildEffectiveToolInventoryEntries(tools, rawToolsByName = /* @__PURE__ */ new Map()) {
	const pluginToolMetadata = new Map((getActivePluginRegistry()?.toolMetadata ?? []).map((entry) => [buildPluginToolMetadataKey(entry.pluginId, entry.metadata.toolName), entry.metadata]));
	return disambiguateLabels(tools.map((tool) => {
		const source = resolveEffectiveToolSource(tool, rawToolsByName.get(tool.name));
		const metadata = source.pluginId ? pluginToolMetadata.get(buildPluginToolMetadataKey(source.pluginId, tool.name)) : void 0;
		return Object.assign({
			id: tool.name,
			label: normalizeOptionalString(metadata?.displayName) ?? resolveEffectiveToolLabel(tool),
			description: normalizeOptionalString(metadata?.description) ?? summarizeToolDescription(tool),
			rawDescription: normalizeOptionalString(metadata?.description) ?? resolveRawToolDescription(tool) ?? summarizeToolDescription(tool),
			...metadata?.risk ? { risk: metadata.risk } : {},
			...metadata?.tags ? { tags: metadata.tags } : {}
		}, source);
	}).toSorted((a, b) => a.label.localeCompare(b.label)));
}
function buildRuntimeCompatibleToolInventory(params) {
	const rawToolsByName = new Map(params.tools.map((tool) => [tool.name, tool]));
	const normalizedTools = normalizeAgentRuntimeTools({
		tools: [...params.tools],
		provider: params.modelProvider ?? "",
		config: params.cfg,
		workspaceDir: params.workspaceDir,
		modelId: params.modelId,
		modelApi: params.modelApi ?? void 0,
		model: params.runtimeModel
	});
	const projection = filterRuntimeCompatibleTools(normalizedTools);
	return {
		entries: buildEffectiveToolInventoryEntries(projection.tools, rawToolsByName),
		notices: buildUnsupportedToolSchemaNotices({
			diagnostics: projection.diagnostics,
			tools: normalizedTools,
			rawToolsByName
		})
	};
}
//#endregion
//#region src/agents/tools-effective-inventory.ts
function listIncludesTool(list, toolName) {
	if (!Array.isArray(list)) return false;
	const normalizedToolName = normalizeToolName(toolName);
	return list.some((entry) => normalizeToolName(entry) === normalizedToolName);
}
function policyDeniesTool(policy, toolName) {
	return listIncludesTool(policy?.deny, toolName) || listIncludesTool(policy?.deny, "group:ui") || listIncludesTool(policy?.deny, "group:openclaw");
}
function hasExplicitBrowserIntent(cfg) {
	return cfg.browser?.enabled !== false && Boolean(cfg.browser || cfg.plugins?.entries?.browser);
}
function buildToolInventoryNotices(params) {
	if (params.entries.some((entry) => normalizeToolName(entry.id) === "browser") || !hasExplicitBrowserIntent(params.cfg)) return;
	if ([
		params.effectivePolicy.globalPolicy,
		params.effectivePolicy.globalProviderPolicy,
		params.effectivePolicy.agentPolicy,
		params.effectivePolicy.agentProviderPolicy
	].some((policy) => policyDeniesTool(policy, "browser"))) return [{
		id: "browser-denied-by-policy",
		severity: "info",
		message: "Browser is configured, but this session does not expose the browser tool because tool policy denies it. Remove the browser deny entry to use browser automation."
	}];
	if (params.profile !== "full") return [{
		id: "browser-filtered-by-profile",
		severity: "info",
		message: "Browser is configured, but the current tool profile does not include the browser tool. Add tools.alsoAllow: [\"browser\"] or agents.list[].tools.alsoAllow: [\"browser\"]; tools.subagents.tools.allow alone cannot add it back after profile filtering."
	}];
	if (Array.isArray(params.cfg.plugins?.allow) && !listIncludesTool(params.cfg.plugins.allow, "browser")) return [{
		id: "browser-plugin-not-allowed",
		severity: "warning",
		message: "Browser is configured, but plugins.allow does not include browser. Add \"browser\" to plugins.allow or remove the restrictive plugin allowlist."
	}];
}
function applyProviderTransportNormalization(params) {
	const normalized = normalizeProviderTransportWithPlugin({
		provider: params.provider,
		config: params.cfg,
		workspaceDir: params.workspaceDir,
		context: {
			config: params.cfg,
			workspaceDir: params.workspaceDir,
			provider: params.provider,
			api: params.runtimeModel.api,
			baseUrl: params.runtimeModel.baseUrl
		}
	});
	if (!normalized) return params.runtimeModel;
	return {
		...params.runtimeModel,
		api: normalized.api ?? params.runtimeModel.api,
		baseUrl: normalized.baseUrl ?? params.runtimeModel.baseUrl
	};
}
function resolveConfiguredFallbackApi(providerConfig) {
	const explicitApi = normalizeOptionalString(providerConfig?.api);
	if (explicitApi) return explicitApi;
	return normalizeOptionalString(providerConfig?.baseUrl) ? "openai-completions" : "openai-responses";
}
function resolveDynamicRuntimeModelContext(params) {
	const runtimeModel = resolveModel(params.provider, params.modelId, params.agentDir, params.cfg, { workspaceDir: params.workspaceDir }).model;
	if (!runtimeModel) return {};
	return {
		modelApi: runtimeModel.api,
		runtimeModel
	};
}
function resolveEffectiveToolInventoryRuntimeModelContext(params) {
	const provider = normalizeProviderId(params.modelProvider ?? "");
	const modelId = params.modelId?.trim() ?? "";
	if (!provider || !modelId) return {};
	const agentId = params.agentId?.trim() || resolveSessionAgentId({ config: params.cfg });
	const workspaceDir = params.workspaceDir ?? resolveAgentWorkspaceDir(params.cfg, agentId);
	const providerConfig = findNormalizedProviderValue(params.cfg.models?.providers, provider);
	const configuredModels = Array.isArray(providerConfig?.models) ? providerConfig.models : [];
	const normalizedModelId = normalizeStaticProviderModelId(provider, modelId);
	const normalizedModelKey = normalizeLowercaseStringOrEmpty(normalizedModelId);
	const providerPrefixedModelKey = normalizeLowercaseStringOrEmpty(`${provider}/${normalizedModelId}`);
	const configuredModel = configuredModels.find((model) => {
		const key = normalizeLowercaseStringOrEmpty(normalizeStaticProviderModelId(provider, model.id));
		return key === normalizedModelKey || key === providerPrefixedModelKey;
	});
	const bundledStaticModel = resolveBundledStaticCatalogModel({
		provider,
		modelId,
		cfg: params.cfg,
		workspaceDir
	});
	if (configuredModel) {
		const configuredApi = normalizeOptionalString(configuredModel.api) ?? normalizeOptionalString(providerConfig?.api) ?? normalizeOptionalString(bundledStaticModel?.api) ?? resolveConfiguredFallbackApi(providerConfig);
		const runtimeModel = applyProviderTransportNormalization({
			cfg: params.cfg,
			provider,
			workspaceDir,
			runtimeModel: {
				...bundledStaticModel,
				...configuredModel,
				id: configuredModel.id,
				name: configuredModel.name ?? bundledStaticModel?.name ?? configuredModel.id,
				provider,
				api: configuredApi,
				baseUrl: normalizeOptionalString(configuredModel.baseUrl) ?? normalizeOptionalString(providerConfig?.baseUrl) ?? normalizeOptionalString(bundledStaticModel?.baseUrl)
			}
		});
		return {
			modelApi: runtimeModel.api,
			runtimeModel
		};
	}
	if (!bundledStaticModel) return resolveDynamicRuntimeModelContext({
		cfg: params.cfg,
		agentDir: params.agentDir,
		workspaceDir,
		provider,
		modelId
	});
	const runtimeModel = applyProviderTransportNormalization({
		cfg: params.cfg,
		provider,
		workspaceDir,
		runtimeModel: {
			...bundledStaticModel,
			api: normalizeOptionalString(providerConfig?.api) ?? bundledStaticModel.api,
			baseUrl: normalizeOptionalString(providerConfig?.baseUrl) ?? bundledStaticModel.baseUrl
		}
	});
	return {
		modelApi: runtimeModel.api,
		runtimeModel
	};
}
function resolveEffectiveModelCompat(params) {
	const provider = normalizeProviderId(params.modelProvider ?? "");
	const modelId = params.modelId?.trim() ?? "";
	if (!provider || !modelId) return;
	const providerConfig = findNormalizedProviderValue(params.cfg.models?.providers, provider);
	const models = Array.isArray(providerConfig?.models) ? providerConfig.models : [];
	if (models.length === 0) return;
	const normalizedModelId = normalizeStaticProviderModelId(provider, modelId);
	const normalizedModelKey = normalizeLowercaseStringOrEmpty(normalizedModelId);
	const providerPrefixedModelKey = normalizeLowercaseStringOrEmpty(`${provider}/${normalizedModelId}`);
	return extractModelCompat(models.find((model) => {
		const key = normalizeLowercaseStringOrEmpty(normalizeStaticProviderModelId(provider, model.id));
		return key === normalizedModelKey || key === providerPrefixedModelKey;
	}));
}
function resolveEffectiveToolInventory(params) {
	const agentId = params.agentId?.trim() || resolveSessionAgentId({
		sessionKey: params.sessionKey,
		config: params.cfg
	});
	const workspaceDir = params.workspaceDir ?? resolveAgentWorkspaceDir(params.cfg, agentId);
	const agentDir = params.agentDir ?? resolveAgentDir(params.cfg, agentId);
	const runtimeModelContext = params.modelApi || params.runtimeModel ? {
		modelApi: params.modelApi ?? params.runtimeModel?.api,
		runtimeModel: params.runtimeModel
	} : resolveEffectiveToolInventoryRuntimeModelContext({
		cfg: params.cfg,
		agentId,
		agentDir,
		workspaceDir,
		modelProvider: params.modelProvider,
		modelId: params.modelId
	});
	const modelCompat = resolveEffectiveModelCompat({
		cfg: params.cfg,
		modelProvider: params.modelProvider,
		modelId: params.modelId
	});
	const projectedInventory = buildRuntimeCompatibleToolInventory({
		tools: createNodmixCodingTools({
			agentId,
			sessionKey: params.sessionKey,
			workspaceDir,
			agentDir,
			config: params.cfg,
			modelProvider: params.modelProvider,
			modelId: params.modelId,
			modelApi: runtimeModelContext.modelApi,
			modelCompat,
			messageProvider: params.messageProvider,
			senderId: params.senderId,
			senderName: params.senderName ?? void 0,
			senderUsername: params.senderUsername ?? void 0,
			senderE164: params.senderE164 ?? void 0,
			agentAccountId: params.accountId ?? void 0,
			currentChannelId: params.currentChannelId,
			currentThreadTs: params.currentThreadTs,
			currentMessageId: params.currentMessageId,
			groupId: params.groupId ?? void 0,
			groupChannel: params.groupChannel ?? void 0,
			groupSpace: params.groupSpace ?? void 0,
			replyToMode: params.replyToMode,
			allowGatewaySubagentBinding: true,
			modelHasVision: params.modelHasVision,
			requireExplicitMessageTarget: params.requireExplicitMessageTarget,
			disableMessageTool: params.disableMessageTool
		}),
		cfg: params.cfg,
		workspaceDir,
		modelProvider: params.modelProvider,
		modelId: params.modelId,
		modelApi: runtimeModelContext.modelApi,
		runtimeModel: runtimeModelContext.runtimeModel
	});
	const effectivePolicy = resolveEffectiveToolPolicy({
		config: params.cfg,
		agentId,
		sessionKey: params.sessionKey,
		modelProvider: params.modelProvider,
		modelId: params.modelId
	});
	const profile = effectivePolicy.providerProfile ?? effectivePolicy.profile ?? "full";
	const entries = projectedInventory.entries;
	const notices = [...projectedInventory.notices, ...buildToolInventoryNotices({
		cfg: params.cfg,
		profile,
		entries,
		effectivePolicy
	}) ?? []];
	return {
		agentId,
		profile,
		groups: buildEffectiveToolInventoryGroups(entries),
		...notices.length > 0 ? { notices } : {}
	};
}
//#endregion
export { resolveEffectiveToolInventoryRuntimeModelContext as n, buildEffectiveToolInventoryGroups as r, resolveEffectiveToolInventory as t };
