import { BestAgentError } from "@best-agent/foundation";
import { isToolInputValue } from "../../tool/tool-history-input.js";
export async function resolveReplayToolInput(call, resolveAsset) {
    if (!call.replayToolInputAssetId) {
        return call.toolInput;
    }
    return resolveAsset(call.replayToolInputAssetId, call.replayToolInputDigest);
}
export async function resolveActiveReplayToolInput(payload, resolveAsset) {
    if (!payload.activeReplayToolInputAssetId) {
        return payload.toolInput;
    }
    return resolveAsset(payload.activeReplayToolInputAssetId, payload.activeReplayToolInputDigest);
}
export async function resolveReplayToolInputAsset(input) {
    const data = await input.readAssetData(input.assetId);
    if (!data) {
        throw new BestAgentError("RUN_CHECKPOINT_INVALID", `Checkpoint replay input asset ${input.assetId} is missing.`);
    }
    let parsed;
    try {
        parsed = JSON.parse(data.toString("utf8"));
    }
    catch {
        throw new BestAgentError("RUN_CHECKPOINT_INVALID", `Checkpoint replay input asset ${input.assetId} is not valid JSON.`);
    }
    if (!isToolInputValue(parsed)) {
        throw new BestAgentError("RUN_CHECKPOINT_INVALID", `Checkpoint replay input asset ${input.assetId} is not a tool input value.`);
    }
    const digest = input.toolInputDigest(parsed);
    if (input.expectedDigest && input.expectedDigest !== digest) {
        throw new BestAgentError("RUN_CHECKPOINT_INVALID", `Checkpoint replay input asset ${input.assetId} digest mismatch.`);
    }
    return parsed;
}
export function toolReplayPolicyForFollowUp(input) {
    if (input.excludedToolNames.includes(input.toolName)) {
        return undefined;
    }
    return input.resolveReplayPolicy(input.toolName);
}
//# sourceMappingURL=provider-tool-replay.js.map