import { prependEnvelopeTimestamp } from "../../../channels/envelope-timestamp.js";
//#region src/cli/commands/agent/stream-renderer.ts
/** Styled labels for terminal output */
const STYLE = {
	dim: "\x1B[2m",
	reset: "\x1B[0m",
	cyan: "\x1B[36m",
	yellow: "\x1B[33m",
	green: "\x1B[32m",
	red: "\x1B[31m",
	bold: "\x1B[1m",
	magenta: "\x1B[35m"
};
function formatToolArgs(args) {
	if (!args || typeof args !== "object") return "";
	const entries = Object.entries(args);
	if (entries.length === 0) return "";
	return entries.map(([key, value]) => {
		const stringValue = typeof value === "string" ? value : JSON.stringify(value);
		return `${key}=${stringValue.length > 80 ? stringValue.slice(0, 77) + "..." : stringValue}`;
	}).join(", ");
}
/**
* Consume the processDirectStreaming async generator and render
* thinking / tool-call / response tokens to stdout.
*
* Returns the final aggregated response text.
*/
async function renderStreamToTerminal(agent, message, sessionKey) {
	const stamped = message.trimStart().startsWith("/") ? message : prependEnvelopeTimestamp(message);
	const stream = agent.turnDispatcher.processDirectStreaming(stamped, sessionKey);
	let responseText = "";
	let isFirstToken = true;
	let isInThinking = false;
	let hasThinking = false;
	for await (const event of stream) switch (event.type) {
		case "thinking": {
			const content = event.content;
			if (event.status === "started") {
				isInThinking = true;
				hasThinking = true;
				process.stdout.write(`\n${STYLE.dim}${STYLE.magenta}💭 Thinking...${STYLE.reset}\n`);
				break;
			}
			if (content) {
				if (!hasThinking) {
					isInThinking = true;
					hasThinking = true;
					process.stdout.write(`\n${STYLE.dim}${STYLE.magenta}💭 Thinking...${STYLE.reset}\n`);
				}
				process.stdout.write(`${STYLE.dim}${content}${STYLE.reset}`);
			}
			break;
		}
		case "tool_start": {
			const toolName = event.toolName;
			const args = event.args;
			if (isInThinking) {
				isInThinking = false;
				process.stdout.write("\n");
			}
			const argsStr = formatToolArgs(args);
			process.stdout.write(`\n${STYLE.cyan}🔧 Tool: ${STYLE.bold}${toolName}${STYLE.reset}` + (argsStr ? `${STYLE.dim} (${argsStr})${STYLE.reset}` : "") + "\n");
			break;
		}
		case "tool_end": {
			const toolName = event.toolName;
			if (event.isError) process.stdout.write(`${STYLE.red}   ✗ ${toolName} failed${STYLE.reset}\n`);
			else process.stdout.write(`${STYLE.green}   ✓ ${toolName} done${STYLE.reset}\n`);
			break;
		}
		case "token": {
			const content = event.content;
			if (!content) break;
			if (isInThinking) {
				isInThinking = false;
				process.stdout.write("\n");
			}
			if (isFirstToken) {
				process.stdout.write("\n🤖: ");
				isFirstToken = false;
			}
			process.stdout.write(content);
			responseText += content;
			break;
		}
		case "message_end":
			if (!isFirstToken) process.stdout.write("\n");
			break;
		case "progress": break;
		default: break;
	}
	if (isFirstToken && !hasThinking) process.stdout.write("\n🤖: (no response)\n");
	return responseText;
}
//#endregion
export { renderStreamToTerminal };

//# sourceMappingURL=stream-renderer.js.map