export const DEFAULT_COMPLETION_NUDGE_AFTER = 10;
export const COMPLETION_NUDGE_TOOL_NAMES = new Set(["complete_slice"]);
const COMPLETION_NUDGE_CUSTOM_TYPE = "sf-completion-nudge";
const LOWERED_TEMPERATURE = 0.2;
const state = {
	active: false,
	unitType: "",
	unitId: "",
	toolCalls: 0,
	completionCalled: false,
	nudgeAfter: DEFAULT_COMPLETION_NUDGE_AFTER,
	reminderSent: false,
	strongSent: false,
	lowerTemperatureForNextRequest: false,
};
export function resolveCompletionNudgeAfter(value) {
	const n = Number(value);
	if (!Number.isFinite(n)) return DEFAULT_COMPLETION_NUDGE_AFTER;
	return Math.max(0, Math.floor(n));
}
export function resetCompletionNudgeState(
	unitType,
	unitId,
	configuredNudgeAfter,
) {
	const nudgeAfter = resolveCompletionNudgeAfter(configuredNudgeAfter);
	state.active = unitType === "complete-slice" && nudgeAfter > 0;
	state.unitType = unitType;
	state.unitId = unitId;
	state.toolCalls = 0;
	state.completionCalled = false;
	state.nudgeAfter = nudgeAfter;
	state.reminderSent = false;
	state.strongSent = false;
	state.lowerTemperatureForNextRequest = false;
}
export function clearCompletionNudgeState() {
	resetCompletionNudgeState("", "", DEFAULT_COMPLETION_NUDGE_AFTER);
}
export function recordCompletionNudgeToolCall(toolName) {
	if (!state.active) return;
	if (COMPLETION_NUDGE_TOOL_NAMES.has(toolName)) {
		state.completionCalled = true;
		state.lowerTemperatureForNextRequest = false;
		return;
	}
	state.toolCalls++;
}
export function getCompletionNudgeStateForTest() {
	return { ...state };
}
export function maybeInjectCompletionNudgeMessage(messages) {
	if (!state.active || state.completionCalled) return messages;
	const message = nextCompletionNudgeMessage();
	if (!message) return messages;
	return [
		...messages,
		{
			role: "custom",
			customType: COMPLETION_NUDGE_CUSTOM_TYPE,
			content: message,
			display: false,
			details: {
				unitType: state.unitType,
				unitId: state.unitId,
				toolCalls: state.toolCalls,
			},
			timestamp: Date.now(),
		},
	];
}
export function applyCompletionNudgeTemperature(payload) {
	if (!state.lowerTemperatureForNextRequest || state.completionCalled)
		return payload;
	state.lowerTemperatureForNextRequest = false;
	if (!payload || typeof payload !== "object") return payload;
	lowerTemperature(payload);
	return payload;
}
function nextCompletionNudgeMessage() {
	const firstThreshold = state.nudgeAfter;
	const secondThreshold = state.nudgeAfter * 2;
	if (!state.strongSent && state.toolCalls >= secondThreshold) {
		state.reminderSent = true;
		state.strongSent = true;
		state.lowerTemperatureForNextRequest = true;
		return `You've performed ${state.toolCalls} tool calls without calling complete_slice. Stop further investigation unless there is a specific blocker. Call complete_slice now with your summary.`;
	}
	if (!state.reminderSent && state.toolCalls >= firstThreshold) {
		state.reminderSent = true;
		return `You've performed ${state.toolCalls} tool calls of investigation. Per the slice plan you should now call complete_slice with your summary. If you genuinely need more context, say so explicitly; otherwise call the tool now.`;
	}
	return null;
}
function lowerTemperature(record) {
	record.temperature = lowerNumber(record.temperature);
	const generationConfig = record.generationConfig;
	if (generationConfig && typeof generationConfig === "object") {
		generationConfig.temperature = lowerNumber(generationConfig.temperature);
	}
	const config = record.config;
	if (config && typeof config === "object") {
		config.temperature = lowerNumber(config.temperature);
	}
}
function lowerNumber(value) {
	return typeof value === "number"
		? Math.min(value, LOWERED_TEMPERATURE)
		: LOWERED_TEMPERATURE;
}
