import { createLogger } from "../../utils/logger/index.js";
import { init_logger } from "../../utils/logger.js";
import { loadConfig } from "../../config/loader.js";
import { getWorkspacePath } from "../../config/workspace-path-helpers.js";
import { ExtensionLoader } from "../../extensions/loader.js";
import "../../extensions/index.js";
import { MessageBus, MessageBusShutdownError } from "../../infra/bus/queue.js";
import "../../infra/bus/index.js";
import { AgentService } from "../../agent/service.js";
import "../../agent/index.js";
import { formatExamples, register } from "../registry.js";
import { getContextWithOpts } from "../context.js";
import "../../config/index.js";
import { listSessions } from "./agent/sessions.js";
import { renderStreamToTerminal } from "./agent/stream-renderer.js";
import { startInteractiveChat } from "./agent/interactive.js";
import { join } from "path";
import { Command } from "commander";
//#region src/cli/commands/agent.ts
init_logger();
const log = createLogger("AgentCommand");
function createAgentCommand(_ctx) {
	const cmd = new Command("agent").description("Chat with the AI agent").addHelpText("after", formatExamples([
		"xopc agent -m \"Hello\"                       # Single message",
		"xopc agent --model demo/demo-chat-7b -m \"Hi\"  # Override model for one shot",
		"xopc agent -i                                # Interactive chat mode",
		"xopc agent -i --session telegram:dm:123456  # Continue existing session",
		"xopc agent --list                            # List available sessions"
	])).option("--model <id>", "Model ref for this run (e.g. demo/demo-chat-7b, anthropic/claude-sonnet-4-5)").option("-m, --message <text>", "Single message to send").option("-i, --interactive", "Interactive chat mode").option("-s, --session <key>", "Continue an existing session (use --list to see available sessions)").option("-l, --list", "List available sessions and exit").action(async (options) => {
		const ctx = getContextWithOpts();
		const config = loadConfig(ctx.configPath);
		const workspace = getWorkspacePath(config) || ctx.workspacePath;
		if (options.list) {
			await listSessions();
			return;
		}
		const modelFromConfig = config.agents?.defaults?.model?.primary;
		const modelId = options.model?.trim() || modelFromConfig;
		const bus = new MessageBus();
		if (ctx.isVerbose) log.info({
			model: modelId,
			workspace,
			session: options.session
		}, "Starting agent");
		let sessionKey = options.session || "agent:main:main";
		if (options.session) {
			const { getSessionIndex } = await import("../utils/session.js");
			const session = await (await getSessionIndex()).getSessionMetadata(options.session);
			if (!session) {
				console.error(`❌ Session not found: ${options.session}`);
				console.log("Use --list to see available sessions.");
				process.exit(1);
			}
			console.log(`📂 Continuing session: ${options.session} (${session.messageCount} messages)\n`);
		}
		let extensionLoader = null;
		try {
			extensionLoader = new ExtensionLoader({
				workspaceDir: workspace,
				extensionsDir: join(workspace, ".extensions")
			});
			extensionLoader.setConfig(config);
			extensionLoader.setRuntimeContext({ bus });
			await extensionLoader.loadByActivationPlan();
			const n = extensionLoader.getRegistry().extensions.size;
			if (n > 0) log.info({ count: n }, "Extensions loaded");
		} catch (error) {
			const em = error instanceof Error ? error.message : String(error);
			log.warn({
				err: error,
				errorMessage: em
			}, `CLI agent: failed to load extensions: ${em}`);
		}
		const { createCliReadlineClarifyRequestFn } = await import("../../agent/tools/cli-clarify.js");
		const agent = new AgentService(bus, {
			workspace,
			model: modelId,
			config,
			extensionRegistry: extensionLoader?.getRegistry(),
			gatewayClarify: { requestClarification: createCliReadlineClarifyRequestFn() }
		});
		agent.start().catch((err) => {
			const em = err instanceof Error ? err.message : String(err);
			log.error({
				err,
				errorMessage: em
			}, `CLI agent service exited: ${em}`);
		});
		let running = true;
		(async () => {
			while (running) try {
				const msg = await bus.consumeOutbound();
				console.log(`\n📤 [${msg.channel}] ${msg.chat_id}: ${msg.content.slice(0, 100)}...`);
			} catch (error) {
				if (error instanceof MessageBusShutdownError) break;
				const em = error instanceof Error ? error.message : String(error);
				log.error({
					err: error,
					errorMessage: em
				}, `CLI outbound processor failed: ${em}`);
				await new Promise((resolve) => setTimeout(resolve, 1e3));
			}
		})();
		const shutdown = async () => {
			running = false;
			bus.shutdown();
			await agent.stop();
		};
		process.on("SIGINT", shutdown);
		process.on("SIGTERM", shutdown);
		if (options.message) {
			const oneShotModel = options.model?.trim();
			if (oneShotModel) await agent.switchModelForSession(sessionKey, oneShotModel);
			await renderStreamToTerminal(agent, options.message, sessionKey);
			if (oneShotModel) await agent.resetSessionModelToAgentDefault(sessionKey);
			await shutdown();
		} else if (options.interactive) {
			const interactiveModel = options.model?.trim();
			if (interactiveModel) await agent.switchModelForSession(sessionKey, interactiveModel);
			await startInteractiveChat(agent, {
				workspace,
				sessionKey,
				continuingSession: !!options.session
			});
		} else {
			await shutdown();
			cmd.help();
		}
	});
	return cmd;
}
register({
	id: "agent",
	name: "agent",
	description: "Chat with the AI agent",
	factory: createAgentCommand,
	metadata: {
		category: "runtime",
		examples: [
			"xopc agent -m \"Hello\"",
			"xopc agent --model demo/demo-chat-7b -m \"Hello demo!\"",
			"xopc agent -i"
		]
	}
});
//#endregion
export {};

//# sourceMappingURL=agent.js.map