import { stableDigest, stableJsonStringify } from "@best-agent/foundation";
import { semanticToolInputDigest } from "../provider-turn/provider-tool-repeat-guard.js";
export function takeCustomLoopToolReplayResult(state, toolName, toolInput) {
    const toolInputKey = stableToolInputKey(toolInput);
    const inputDigest = rawToolInputDigest(toolInput);
    const inputStableDigest = semanticToolInputDigest(toolInput);
    const matchIndex = state.toolResults.findIndex((result) => result.toolName === toolName &&
        (result.toolInputStableDigest === inputStableDigest ||
            result.toolInputKey === toolInputKey ||
            result.toolInputDigest === inputDigest));
    if (matchIndex < 0) {
        return undefined;
    }
    const [match] = state.toolResults.splice(matchIndex, 1);
    return match?.output;
}
export function customLoopEvidenceReplayKey(record) {
    return stableJsonStringify({
        confidence: record.confidence,
        kind: record.kind,
        payload: record.payload,
        relatedFilePaths: [...(record.relatedFilePaths ?? [])].sort(),
        sourceItemId: record.sourceItemId,
        summary: record.summary,
        threadId: record.threadId,
        turnId: record.turnId,
    });
}
export function stableToolInputKey(value) {
    return stableJsonStringify(value);
}
export function isSameToolInputValue(left, right) {
    return stableToolInputKey(left) === stableToolInputKey(right);
}
export function rawToolInputDigest(value) {
    return stableDigest(value);
}
//# sourceMappingURL=custom-loop-replay.js.map