import { applyConfigOverrides, setConfigOverride } from "../../config/runtime-overrides.js";
import { formatThinkingLevels, normalizeReasoningLevel, normalizeThinkLevel, normalizeVerboseLevel } from "../../agent/transcript/thinking-types.js";
import { commandRegistry } from "../registry.js";
//#region src/chat-commands/builtins/thinking.ts
function effectiveAgentDefaults(ctx) {
	return applyConfigOverrides(ctx.getConfig?.() ?? ctx.config).agents?.defaults;
}
async function stripSessionAgentFields(store, sessionKey, fields) {
	if (!store) return;
	const existing = await store.get(sessionKey);
	if (!existing) return;
	const next = { ...existing };
	for (const f of fields) delete next[f];
	if (Object.keys(next).filter((k) => k !== "updatedAt").length === 0) await store.delete(sessionKey);
	else await store.set(sessionKey, next);
}
const thinkCommand = {
	id: "agent.think",
	name: "think",
	description: "Set thinking level (usage: /think <level>)",
	category: "model",
	scope: [
		"global",
		"private",
		"group"
	],
	acceptsArgs: true,
	examples: [
		"/think",
		"/think high",
		"/think off",
		"/think adaptive"
	],
	handler: async (ctx, args) => {
		await ctx.setTyping(true);
		if (!args.trim()) return {
			content: `🧠 *Thinking Level*\n\nCurrent: *${(await ctx.getThinkingLevel?.() ?? effectiveAgentDefaults(ctx)?.thinkingDefault) || "default"}*\n\nAvailable levels:\n• off - No thinking\n• minimal - Minimal thinking\n• low - Low thinking\n• medium - Medium thinking (default)\n• high - High thinking\n• xhigh - Extra high (certain models)\n• adaptive - Auto-adjust based on task\n\nUsage: /think <level>\nExample: /think high`,
			success: true
		};
		const level = normalizeThinkLevel(args.trim());
		if (!level) return {
			content: `❌ Invalid thinking level: ${args}\n\nAvailable levels: ${formatThinkingLevels()}\n\nUsage: /think <level>\nExample: /think high`,
			success: false
		};
		setConfigOverride([
			"agents",
			"defaults",
			"thinkingDefault"
		], level);
		await stripSessionAgentFields(ctx.getSessionConfigStore?.(), ctx.sessionKey, ["thinkingLevel"]);
		ctx.syncAgentThinkingLevel?.(level);
		return {
			content: `🧠 *Thinking Level Set*\n\nLevel: *${level}*\n${{
				off: "No thinking",
				minimal: "Minimal thinking effort",
				low: "Low thinking effort",
				medium: "Medium thinking effort",
				high: "High thinking effort",
				xhigh: "Extra high thinking effort",
				adaptive: "Auto-adjust based on task complexity"
			}[level]}\n\nApplies for this gateway process (until restart). Persists only if you use /config set.`,
			success: true
		};
	}
};
const reasoningCommand = {
	id: "agent.reasoning",
	name: "reasoning",
	description: "Set reasoning visibility (usage: /reasoning <mode>)",
	category: "model",
	scope: [
		"global",
		"private",
		"group"
	],
	acceptsArgs: true,
	examples: [
		"/reasoning",
		"/reasoning on",
		"/reasoning stream",
		"/reasoning off"
	],
	handler: async (ctx, args) => {
		await ctx.setTyping(true);
		if (!args.trim()) return {
			content: `💭 *Reasoning Visibility*\n\nCurrent: *${(await ctx.getReasoningLevel?.() ?? effectiveAgentDefaults(ctx)?.reasoningDefault) || "default"}*\n\nAvailable modes:\n• off - Hide reasoning\n• on - Show reasoning after completion\n• stream - Stream reasoning in real-time\n\nUsage: /reasoning <mode>\nExample: /reasoning stream`,
			success: true
		};
		const level = normalizeReasoningLevel(args.trim());
		if (!level) return {
			content: `❌ Invalid reasoning mode: ${args}\n\nAvailable modes: off, on, stream\n\nUsage: /reasoning <mode>\nExample: /reasoning stream`,
			success: false
		};
		setConfigOverride([
			"agents",
			"defaults",
			"reasoningDefault"
		], level);
		await stripSessionAgentFields(ctx.getSessionConfigStore?.(), ctx.sessionKey, ["reasoningLevel"]);
		return {
			content: `💭 *Reasoning Visibility Set*\n\nMode: *${level}*\n${{
				off: "Hide reasoning from user",
				on: "Show reasoning after completion",
				stream: "Stream reasoning in real-time"
			}[level]}\n\nApplies for this gateway process (until restart). Persists only if you use /config set.`,
			success: true
		};
	}
};
const verboseCommand = {
	id: "agent.verbose",
	name: "verbose",
	description: "Toggle verbose mode (usage: /verbose [on|off|full])",
	category: "model",
	scope: [
		"global",
		"private",
		"group"
	],
	acceptsArgs: true,
	examples: [
		"/verbose",
		"/verbose on",
		"/verbose off",
		"/verbose full"
	],
	handler: async (ctx, args) => {
		await ctx.setTyping(true);
		if (!args.trim()) {
			const newLevel = (await ctx.getVerboseLevel?.() ?? effectiveAgentDefaults(ctx)?.verboseDefault) === "on" ? "off" : "on";
			setConfigOverride([
				"agents",
				"defaults",
				"verboseDefault"
			], newLevel);
			await stripSessionAgentFields(ctx.getSessionConfigStore?.(), ctx.sessionKey, ["verboseLevel"]);
			return {
				content: `📝 *Verbose Mode*\n\nMode: *${newLevel}*\n\n${newLevel === "on" ? "Verbose output enabled (process lifetime)." : "Verbose output disabled (process lifetime)."}`,
				success: true
			};
		}
		const level = normalizeVerboseLevel(args.trim());
		if (!level) return {
			content: `❌ Invalid verbose mode: ${args}\n\nAvailable modes: off, on, full\n\nUsage: /verbose [mode]\nExample: /verbose on`,
			success: false
		};
		setConfigOverride([
			"agents",
			"defaults",
			"verboseDefault"
		], level);
		await stripSessionAgentFields(ctx.getSessionConfigStore?.(), ctx.sessionKey, ["verboseLevel"]);
		return {
			content: `📝 *Verbose Mode Set*\n\nMode: *${level}*\n${{
				off: "Minimal output",
				on: "Normal verbose output",
				full: "Full verbose output with all details"
			}[level]}\n\nApplies for this gateway process (until restart). Persists only if you use /config set.`,
			success: true
		};
	}
};
const enhancedStatusCommand = {
	id: "agent.status",
	name: "status",
	description: "Show current agent status including thinking settings",
	category: "system",
	scope: [
		"global",
		"private",
		"group"
	],
	handler: async (ctx) => {
		await ctx.setTyping(true);
		const defaults = effectiveAgentDefaults(ctx);
		const currentModel = ctx.getCurrentModel();
		const thinkingLevel = await ctx.getThinkingLevel?.() ?? defaults?.thinkingDefault;
		const reasoningLevel = await ctx.getReasoningLevel?.() ?? defaults?.reasoningDefault;
		const verboseLevel = await ctx.getVerboseLevel?.() ?? defaults?.verboseDefault;
		const usage = await ctx.getUsage();
		return {
			content: [
				"📊 *Agent Status*",
				"",
				`🤖 *Model:* \`${currentModel}\``,
				`🧠 *Thinking:* \`${thinkingLevel || "default"}\``,
				`💭 *Reasoning:* \`${reasoningLevel || "default"}\``,
				`📝 *Verbose:* \`${verboseLevel || "default"}\``,
				"",
				"---",
				"",
				"📈 *Session Usage*",
				`💬 Messages: ${usage.messageCount}`,
				`📥 Prompt: ${usage.promptTokens.toLocaleString()} tokens`,
				`📤 Completion: ${usage.completionTokens.toLocaleString()} tokens`,
				`📊 Total: ${usage.totalTokens.toLocaleString()} tokens`
			].join("\n"),
			success: true
		};
	}
};
function registerThinkingCommands() {
	commandRegistry.register(thinkCommand);
	commandRegistry.register(reasoningCommand);
	commandRegistry.register(verboseCommand);
	commandRegistry.register(enhancedStatusCommand);
}
//#endregion
export { registerThinkingCommands };

//# sourceMappingURL=thinking.js.map