import { createLogger } from "../../utils/logger/index.js";
import { init_logger } from "../../utils/logger.js";
import { formatHeartbeatMessage, formatProgressMessage } from "../lifecycle/progress.js";
//#region src/agent/feedback/feedback-coordinator.ts
init_logger();
const log = createLogger("FeedbackCoordinator");
var FeedbackCoordinator = class {
	progressManager;
	bus;
	currentStreamHandle = null;
	currentContext = null;
	constructor(config) {
		this.progressManager = config.progressManager;
		this.bus = config.bus;
	}
	/**
	* Set the current context for feedback
	*/
	setContext(context) {
		this.currentContext = context;
	}
	/**
	* Clear the current context
	*/
	clearContext() {
		this.currentContext = null;
	}
	/**
	* Start a task and initialize feedback
	*/
	startTask() {
		this.progressManager.startTask();
	}
	/**
	* End a task and cleanup feedback
	*/
	endTask() {
		this.progressManager.endTask();
		this.clearStreamHandle();
	}
	/**
	* Set the stream handle for progress updates
	*/
	setStreamHandle(handle) {
		this.currentStreamHandle = handle;
		if (handle) this.progressManager.setCallbacks({
			onProgress: (msg) => this.sendProgressMessage(msg),
			onStreamStart: (toolName, toolArgs) => {
				const stage = this.getToolStage(toolName);
				if (this.currentStreamHandle?.setProgress) {
					let detail;
					if (toolArgs.path) detail = String(toolArgs.path);
					else if (toolArgs.command) {
						const cmd = String(toolArgs.command);
						detail = cmd.length > 30 ? cmd.slice(0, 30) + "..." : cmd;
					}
					this.currentStreamHandle.setProgress(stage, detail);
				}
			},
			onStreamEnd: () => {
				if (this.currentStreamHandle?.setProgress) this.currentStreamHandle.setProgress("idle");
			},
			onHeartbeat: (elapsedMs, stage) => {
				this.sendHeartbeat(elapsedMs, stage);
			}
		});
	}
	/**
	* Clear the stream handle
	*/
	clearStreamHandle() {
		this.currentStreamHandle = null;
	}
	/**
	* Send progress message via stream or direct message
	*/
	async sendProgressMessage(msg) {
		if (!this.currentContext) return;
		if (this.currentStreamHandle?.updateProgress && msg.stage !== "idle") this.currentStreamHandle.updateProgress("", msg.stage, msg.message);
		else if (msg.type === "error") {
			const formatted = formatProgressMessage(msg);
			await this.bus.publishOutbound({
				channel: this.currentContext.channel,
				chat_id: this.currentContext.chatId,
				content: formatted,
				type: "message"
			});
		}
	}
	/**
	* Send heartbeat for long-running tasks
	*/
	async sendHeartbeat(elapsedMs, stage) {
		if (!this.currentContext) return;
		log.info({
			elapsedMs,
			stage
		}, "Progress heartbeat");
		const formatted = formatHeartbeatMessage(elapsedMs, stage);
		await this.bus.publishOutbound({
			channel: this.currentContext.channel,
			chat_id: this.currentContext.chatId,
			content: formatted,
			type: "message"
		});
	}
	/**
	* Map tool name to progress stage
	*/
	getToolStage(toolName) {
		const name = toolName.toLowerCase();
		if (name.includes("read") || name.includes("file")) return "reading";
		if (name.includes("search") || name.includes("grep") || name.includes("web")) return "searching";
		if (name.includes("write") || name.includes("edit")) return "writing";
		if (name.includes("bash") || name.includes("shell") || name.includes("exec")) return "executing";
		return "executing";
	}
	/**
	* Update progress via stream
	*/
	updateProgress(text, stage, detail) {
		if (this.currentStreamHandle?.updateProgress) this.currentStreamHandle.updateProgress(text, stage, detail);
	}
	/**
	* Set progress stage
	*/
	setProgress(stage, detail) {
		if (this.currentStreamHandle?.setProgress) this.currentStreamHandle.setProgress(stage, detail);
	}
	/**
	* Send a direct message to the current context
	*/
	async sendMessage(content) {
		if (!this.currentContext) {
			log.warn({
				hasStreamHandle: Boolean(this.currentStreamHandle),
				contentPreview: content.slice(0, 80)
			}, "Outbound message skipped: no session context (setContext not called or cleared)");
			return;
		}
		await this.bus.publishOutbound({
			channel: this.currentContext.channel,
			chat_id: this.currentContext.chatId,
			content,
			type: "message"
		});
	}
	/**
	* Get the progress manager instance
	*/
	getProgressManager() {
		return this.progressManager;
	}
};
//#endregion
export { FeedbackCoordinator };

//# sourceMappingURL=feedback-coordinator.js.map