import { createLogger } from "../../utils/logger/index.js";
import { init_logger } from "../../utils/logger.js";
import { getAgentDefaultModelRef, init_schema } from "../../config/schema.js";
import { resolveEffectiveAgentProfileForSession } from "../../config/agent-profile.js";
import { PERSISTENT_GOAL_CUSTOM_KEY, mergeCustomDataPatch, readPersistentGoal, serializePersistentGoal } from "./state.js";
import { resolveGoalUiLocale } from "./goal-locale.js";
import { evaluateAfterTurnHermesLike } from "./evaluate-turn.js";
import { appendGoalRun } from "./goal-run-store.js";
//#region src/agent/goals/post-turn.ts
init_schema();
init_logger();
const log = createLogger("PersistentGoal");
function buildHistoryExcerpt(messages, maxChars) {
	if (maxChars <= 0) return "";
	try {
		const raw = JSON.stringify(messages);
		if (raw.length <= maxChars) return raw;
		return raw.slice(-maxChars);
	} catch {
		return "";
	}
}
function resolveJudgeModelRef(config, sessionKey, storedJudge, runtimeSessionModelRef) {
	const fromCfg = config?.goals?.judgeModelRef?.trim();
	if (fromCfg) return fromCfg;
	if (storedJudge?.trim()) return storedJudge.trim();
	if (config) {
		const profile = resolveEffectiveAgentProfileForSession(config, sessionKey);
		if (profile.primaryModelRef?.trim()) return profile.primaryModelRef.trim();
	}
	if (runtimeSessionModelRef?.trim()) return runtimeSessionModelRef.trim();
	if (config) return getAgentDefaultModelRef(config);
}
async function appendAssistantLine(apis, sessionKey, text) {
	await apis.appendAssistantReceipt(sessionKey, text);
}
async function handlePersistentGoalPostTurn(opts) {
	const { apis, sessionKey, assistantPlainText, aborted, streamError, skipPersistentGoalPostTurn, config, runtimeSessionModelRef, signal, publishVerdictToChannel } = opts;
	if (skipPersistentGoalPostTurn) return;
	if (aborted || streamError) return;
	const meta = await apis.getSessionMetadata(sessionKey);
	if (!meta) return;
	const state = readPersistentGoal(meta.customData);
	if (!state || state.status !== "active") return;
	const judgeRef = resolveJudgeModelRef(config, sessionKey, state.judgeModelRef, runtimeSessionModelRef);
	if (!judgeRef) {
		log.warn({ sessionKey }, "Persistent goal: no judge model ref; skipping post-turn");
		return;
	}
	const goalsCfg = config?.goals;
	const historyCap = goalsCfg?.checklistHistoryChars ?? 24e3;
	let historyExcerpt = "";
	try {
		historyExcerpt = buildHistoryExcerpt(await apis.loadMessages(sessionKey), historyCap);
	} catch (err) {
		log.debug({
			err,
			sessionKey
		}, `Persistent goal: could not load messages for judge context: ${err instanceof Error ? err.message : String(err)}`);
	}
	const decision = await evaluateAfterTurnHermesLike(state, assistantPlainText, judgeRef, signal, {
		goalsSlice: goalsCfg,
		historyExcerpt,
		uiLocale: resolveGoalUiLocale(state)
	});
	const baseCustom = { ...meta.customData };
	if (decision.newState) {
		const merged = mergeCustomDataPatch(baseCustom, { [PERSISTENT_GOAL_CUSTOM_KEY]: serializePersistentGoal(decision.newState) });
		await apis.updateSessionMetadata(sessionKey, { customData: merged });
	}
	if (config) try {
		await appendGoalRun({
			config,
			sessionKey,
			decision,
			assistantPlainText
		});
	} catch (err) {
		log.warn({
			err,
			sessionKey
		}, `Persistent goal: goal run append failed: ${err instanceof Error ? err.message : String(err)}`);
	}
	if (decision.message) if (publishVerdictToChannel) try {
		await publishVerdictToChannel(decision.message);
	} catch (err) {
		log.warn({
			err,
			sessionKey
		}, `Persistent goal: channel verdict send failed: ${err instanceof Error ? err.message : String(err)}`);
	}
	else try {
		await appendAssistantLine(apis, sessionKey, decision.message);
	} catch (err) {
		log.warn({
			err,
			sessionKey
		}, `Persistent goal: failed to append status message: ${err instanceof Error ? err.message : String(err)}`);
	}
	if (decision.shouldContinue && decision.continuationPrompt) apis.scheduleContinuation(sessionKey, decision.continuationPrompt);
}
//#endregion
export { handlePersistentGoalPostTurn };

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