import { t as isTruthyEnvValue } from "../env-D9zNF4UR.js";
import { c as toSafeImportPath } from "../plugin-module-loader-cache-CQs2UiUc.js";
import { A as listRegisteredPluginAgentPromptGuidance, C as claimPluginInteractiveCallbackDedupe, D as clearPluginCommands, O as clearPluginCommandsForPlugin, T as releasePluginInteractiveCallbackDedupe, _ as clearPluginInteractiveHandlers, a as validateCommandName, b as registerPluginInteractiveHandler, c as CONVERSATION_HOOK_NAMES, d as PluginApprovalResolutions, f as isConversationHookName, g as stripPromptMutationFieldsFromLegacyHookResult, h as PLUGIN_PROMPT_MUTATION_RESULT_FIELDS, i as registerPluginCommand, l as PLUGIN_HOOK_NAMES, m as isPromptInjectionHookName, o as validatePluginCommandDefinition, p as isPluginHookName, s as AGENT_PROMPT_SURFACE_KINDS, u as PROMPT_INJECTION_HOOK_NAMES, v as clearPluginInteractiveHandlersForPlugin, w as commitPluginInteractiveCallbackDedupe, x as resolvePluginInteractiveNamespaceMatch } from "../command-registration-C5OQroLZ.js";
import { a as resetGlobalHookRunner, i as initializeGlobalHookRunner, n as getGlobalPluginRegistry, o as runGlobalGatewayStopSafely, r as hasGlobalHooks, t as getGlobalHookRunner } from "../hook-runner-global-DH2IyZit.js";
import { n as normalizePluginHttpPath } from "../http-route-overlap-BissJoOn.js";
import { t as getPluginRuntimeGatewayRequestScope } from "../gateway-request-scope-bTSJ8nCA.js";
import { o as detachPluginConversationBinding, p as requestPluginConversationBinding, s as getCurrentPluginConversationBinding } from "../conversation-binding-D8BWPBBz.js";
import { n as listProviderPluginCommandSpecs, t as getPluginCommandSpecs } from "../command-specs-0su7HR1c.js";
import { i as testing, n as listPluginCommands, r as matchPluginCommand, t as executePluginCommand } from "../commands-CMSq7Kjk.js";
import { n as withPluginHttpRouteRegistry, t as registerPluginHttpRoute } from "../http-registry-tuEHohsJ.js";
//#region src/plugins/interactive-binding-helpers.ts
function createInteractiveConversationBindingHelpers(params) {
	const { registration, senderId, conversation } = params;
	const pluginRoot = registration.pluginRoot;
	return {
		requestConversationBinding: async (binding = {}) => {
			if (!pluginRoot) return {
				status: "error",
				message: "This interaction cannot bind the current conversation."
			};
			return requestPluginConversationBinding({
				pluginId: registration.pluginId,
				pluginName: registration.pluginName,
				pluginRoot,
				requestedBySenderId: senderId,
				conversation,
				binding
			});
		},
		detachConversationBinding: async () => {
			if (!pluginRoot) return { removed: false };
			return detachPluginConversationBinding({
				pluginRoot,
				conversation
			});
		},
		getCurrentConversationBinding: async () => {
			if (!pluginRoot) return null;
			return getCurrentPluginConversationBinding({
				pluginRoot,
				conversation
			});
		}
	};
}
//#endregion
//#region src/plugins/interactive.ts
async function dispatchPluginInteractiveHandler(params) {
	const match = resolvePluginInteractiveNamespaceMatch(params.channel, params.data);
	if (!match) return {
		matched: false,
		handled: false,
		duplicate: false
	};
	const dedupeKey = params.dedupeId?.trim();
	if (dedupeKey && !claimPluginInteractiveCallbackDedupe(dedupeKey)) return {
		matched: true,
		handled: true,
		duplicate: true
	};
	try {
		await params.onMatched?.();
		const resolved = await params.invoke(match);
		if (dedupeKey) commitPluginInteractiveCallbackDedupe(dedupeKey);
		const shouldExposeResult = !!resolved && typeof resolved === "object" && Object.keys(resolved).some((key) => key !== "handled");
		return {
			matched: true,
			handled: resolved?.handled ?? true,
			duplicate: false,
			...shouldExposeResult ? { result: resolved } : {}
		};
	} catch (error) {
		if (dedupeKey) releasePluginInteractiveCallbackDedupe(dedupeKey);
		throw error;
	}
}
//#endregion
//#region src/plugins/lazy-service-module.ts
function resolveExport(mod, names) {
	for (const name of names) {
		const value = mod[name];
		if (typeof value === "function") return value;
	}
	return null;
}
async function defaultLoadOverrideModule(specifier, importModule = async (source) => await import(source)) {
	return importModule(toSafeImportPath(specifier));
}
async function startLazyPluginServiceModule(params) {
	const skipEnvVar = params.skipEnvVar?.trim();
	if (skipEnvVar && isTruthyEnvValue(process.env[skipEnvVar])) return null;
	const overrideEnvVar = params.overrideEnvVar?.trim();
	const override = overrideEnvVar ? process.env[overrideEnvVar]?.trim() : void 0;
	const loadOverrideModule = params.loadOverrideModule ?? defaultLoadOverrideModule;
	const validatedOverride = override && params.validateOverrideSpecifier ? params.validateOverrideSpecifier(override) : override;
	const mod = validatedOverride ? await loadOverrideModule(validatedOverride) : await params.loadDefaultModule();
	const start = resolveExport(mod, params.startExportNames);
	if (!start) return null;
	const stop = params.stopExportNames && params.stopExportNames.length > 0 ? resolveExport(mod, params.stopExportNames) : null;
	await start();
	return { stop: stop ?? (async () => {}) };
}
//#endregion
export { AGENT_PROMPT_SURFACE_KINDS, CONVERSATION_HOOK_NAMES, PLUGIN_HOOK_NAMES, PLUGIN_PROMPT_MUTATION_RESULT_FIELDS, PROMPT_INJECTION_HOOK_NAMES, PluginApprovalResolutions, testing as __testing, testing, clearPluginCommands, clearPluginCommandsForPlugin, clearPluginInteractiveHandlers, clearPluginInteractiveHandlersForPlugin, createInteractiveConversationBindingHelpers, defaultLoadOverrideModule, dispatchPluginInteractiveHandler, executePluginCommand, getGlobalHookRunner, getGlobalPluginRegistry, getPluginCommandSpecs, getPluginRuntimeGatewayRequestScope, hasGlobalHooks, initializeGlobalHookRunner, isConversationHookName, isPluginHookName, isPromptInjectionHookName, listPluginCommands, listProviderPluginCommandSpecs, listRegisteredPluginAgentPromptGuidance, matchPluginCommand, normalizePluginHttpPath, registerPluginCommand, registerPluginHttpRoute, registerPluginInteractiveHandler, resetGlobalHookRunner, runGlobalGatewayStopSafely, startLazyPluginServiceModule, stripPromptMutationFieldsFromLegacyHookResult, validateCommandName, validatePluginCommandDefinition, withPluginHttpRouteRegistry };
