import { createLogger } from "../../utils/logger/index.js";
import { init_logger } from "../../utils/logger.js";
import { sanitizeToolCallIdsForCloudCodeAssist } from "./tool-call-id.js";
import { sanitizeToolCallInputs, sanitizeToolUseResultPairing, stripToolResultDetails } from "./session-transcript-repair.js";
import { resolveTranscriptPolicy } from "./transcript-policy.js";
import { dropThinkingBlocks } from "./thinking.js";
//#region src/agent/transcript/transcript-hygiene.ts
init_logger();
const log = createLogger("transcript-hygiene");
function resolvePolicyForModel(model) {
	return resolveTranscriptPolicy({
		modelApi: model.api,
		provider: model.provider,
		modelId: model.id
	});
}
/**
* Apply provider-aware transcript fixes: thinking drop, tool input cleanup,
* tool_use/tool_result pairing repair, strip non-LLM details, optional tool-call ID sanitize.
*/
function applySessionTranscriptHygiene(messages, model) {
	return applySessionTranscriptHygieneWithPolicy(messages, resolvePolicyForModel(model));
}
function applySessionTranscriptHygieneWithPolicy(messages, policy) {
	let out = messages;
	if (policy.dropThinkingBlocks) out = dropThinkingBlocks(out);
	out = sanitizeToolCallInputs(out);
	if (policy.repairToolUseResultPairing) out = sanitizeToolUseResultPairing(out);
	out = stripToolResultDetails(out);
	if (policy.sanitizeToolCallIds && policy.toolCallIdMode) {
		const mode = policy.toolCallIdMode;
		out = sanitizeToolCallIdsForCloudCodeAssist(out, mode);
	}
	return out;
}
function tryApplySessionTranscriptHygiene(messages, model) {
	try {
		return applySessionTranscriptHygiene(messages, model);
	} catch (err) {
		const em = err instanceof Error ? err.message : String(err);
		log.warn({
			err,
			errorMessage: em,
			provider: model.provider,
			modelId: model.id,
			modelApi: model.api,
			messageCount: messages.length
		}, `Transcript hygiene failed for ${model.provider}/${model.id}: ${em} (using raw messages)`);
		return messages;
	}
}
/**
* Hygiene for **writing session files** (gateway UI, history replay).
*
* Does **not** strip `thinking` blocks — those must remain on disk so web chat can show
* reasoning after refresh. `dropThinkingBlocks` exists for **LLM** safety when loading
* into the agent (`tryApplySessionTranscriptHygiene` in {@link prepareLoadedSessionMessages}).
*/
function applySessionTranscriptHygieneForPersistence(messages, model) {
	return applySessionTranscriptHygieneWithPolicy(messages, {
		...resolvePolicyForModel(model),
		dropThinkingBlocks: false
	});
}
function tryApplySessionTranscriptHygieneForPersistence(messages, model) {
	try {
		return applySessionTranscriptHygieneForPersistence(messages, model);
	} catch (err) {
		const em = err instanceof Error ? err.message : String(err);
		log.warn({
			err,
			errorMessage: em,
			provider: model.provider,
			modelId: model.id,
			messageCount: messages.length
		}, `Persistence transcript hygiene failed for ${model.provider}/${model.id}: ${em} (using raw messages)`);
		return messages;
	}
}
//#endregion
export { applySessionTranscriptHygiene, applySessionTranscriptHygieneForPersistence, applySessionTranscriptHygieneWithPolicy, resolvePolicyForModel, tryApplySessionTranscriptHygiene, tryApplySessionTranscriptHygieneForPersistence };

//# sourceMappingURL=transcript-hygiene.js.map