import { queueEmbeddedSteer } from "../embedded/runs.js";
import { buildDirectUserMessageContent } from "../service/build-direct-message-content.js";
import { runProcessDirectStreaming } from "../service/process-direct-streaming.js";
import { runProcessDirect } from "../service/process-direct-one-shot.js";
import { maybeEmitWebchatTts } from "../service/webchat-tts.js";
//#region src/agent/inbound/turn-dispatcher.ts
var TurnDispatcher = class {
	cfg;
	log;
	constructor(cfg) {
		this.cfg = cfg;
		this.log = cfg.log;
	}
	/** One-shot direct turn (CLI / embedded TUI). */
	processDirect(content, sessionKey = "agent:main:main", attachments, thinking) {
		return runProcessDirect(this.buildOneShotDeps(), {
			content,
			sessionKey,
			attachments,
			thinking
		});
	}
	/** Streaming direct turn (webchat SSE / CLI streaming). */
	async *processDirectStreaming(content, sessionKey = "agent:main:main", attachments, thinking, options) {
		yield* runProcessDirectStreaming(this.buildStreamingDeps(), {
			content,
			sessionKey,
			attachments,
			thinking,
			signal: options?.signal
		});
	}
	/** Push an out-of-band event into the live webchat stream for a session. */
	enqueueWebchatSseEvent(sessionKey, event) {
		const pub = this.cfg.sessionState.getWebchatPublisher(sessionKey);
		if (pub) pub(event);
	}
	/** Stream assistant text to live webchat session + notify transcript listeners. */
	notifyWebchatTranscriptAppend(sessionKey, assistantText) {
		const trimmed = assistantText.trim();
		if (trimmed) this.enqueueWebchatSseEvent(sessionKey, {
			type: "token",
			content: trimmed
		});
		this.cfg.onSessionTranscriptUpdated?.(sessionKey);
	}
	/**
	* Queue a steering user message into pi-agent's in-flight run (delivered
	* after current tool work, before the next LLM call). See `Agent.steer`
	* in `@earendil-works/pi-agent-core`.
	*/
	async steerWebchatSession(sessionKey, text) {
		const trimmed = text.trim();
		if (!trimmed) return false;
		try {
			return await queueEmbeddedSteer(sessionKey, trimmed);
		} catch (err) {
			this.log.warn({
				err,
				sessionKey
			}, "steerWebchatSession failed");
			return false;
		}
	}
	buildStreamingDeps() {
		const c = this.cfg;
		return {
			log: this.log,
			parseSessionKey: c.parseSessionKey,
			initDirectStreamingSession: c.initSessionContext,
			registerWebchatSsePublisher: (sk, publisher) => c.sessionState.registerWebchatPublisher(sk, publisher),
			unregisterWebchatSsePublisher: (sk) => c.sessionState.unregisterWebchatPublisher(sk),
			agentManager: c.agentManager,
			hydrateSessionWorkspaceFromStore: (sk) => c.sessionHydrator.workspace(sk),
			hydrateSessionModelFromStore: (sk) => c.sessionHydrator.model(sk),
			sessionStore: c.sessionStore,
			modelManager: c.modelManager,
			applyResolvedThinkingLevel: (sk, t) => c.sessionHydrator.thinking(sk, t),
			getConfig: c.getConfig,
			sessionConfigStore: c.sessionConfigStore,
			attachmentRootsForSession: c.attachmentRootsForSession,
			commandHandler: c.commandHandler,
			prepareInboundAttachments: c.prepareInboundAttachments,
			buildMessageContent: (text, prepared, sk) => buildDirectUserMessageContent({
				content: text,
				attachments: prepared,
				sessionKey: sk,
				config: c.requireConfig(),
				agentManager: c.agentManager,
				modelManager: c.modelManager
			}),
			recordPersistentGoalStreamOutcome: (sk, o) => c.sessionState.recordPersistentGoalStreamOutcome(sk, o),
			onTurnComplete: (sk, text) => {
				if (text) c.sessionState.setLastAssistantText(sk, text);
				c.enqueueMaybeAutoTitleAfterPersist(sk);
			},
			reloadWebchatTranscript: (sk) => {
				c.onSessionTranscriptUpdated?.(sk);
			},
			maybeEmitWebchatTts: (sk, hadVoice) => maybeEmitWebchatTts({
				config: c.getConfig(),
				sessionStore: c.sessionStore,
				getLastAssistantPlainText: (sessionKey) => c.sessionState.getLastAssistantText(sessionKey) ?? c.agentManager.getLastAssistantContent(sessionKey) ?? "",
				log: this.log
			}, sk, hadVoice),
			endDirectRequestContext: c.endDirectRequestContext,
			resetSession: c.resetSession
		};
	}
	buildOneShotDeps() {
		const c = this.cfg;
		const cfg = c.requireConfig();
		return {
			log: this.log,
			config: cfg,
			parseSessionKey: c.parseSessionKey,
			initSessionContext: (sk, channel, chatId) => {
				c.initSessionContext(sk, channel, chatId);
			},
			hydrateSessionWorkspaceFromStore: (sk) => c.sessionHydrator.workspace(sk),
			hydrateSessionModelFromStore: (sk) => c.sessionHydrator.model(sk),
			agentManager: c.agentManager,
			sessionStore: c.sessionStore,
			modelManager: c.modelManager,
			applyResolvedThinkingLevel: (sk, t) => c.sessionHydrator.thinking(sk, t),
			prepareInboundAttachments: c.prepareInboundAttachments,
			commandHandler: c.commandHandler,
			onTurnComplete: (sk, text) => {
				if (text) c.sessionState.setLastAssistantText(sk, text);
				c.enqueueMaybeAutoTitleAfterPersist(sk);
			},
			endDirectRequestContext: c.endDirectRequestContext,
			resetSession: c.resetSession
		};
	}
};
//#endregion
export { TurnDispatcher };

//# sourceMappingURL=turn-dispatcher.js.map