import { parseCodexThreadForkResponse, parseCodexThreadRollbackResponse, } from "./app-server-transport.js";
async function forkCodexThread(client, params) {
    if (client.forkThread) {
        return client.forkThread(params);
    }
    return parseCodexThreadForkResponse(await client.request("thread/fork", params));
}
async function rollbackCodexThread(client, params) {
    if (client.rollbackThread) {
        return client.rollbackThread(params);
    }
    return parseCodexThreadRollbackResponse(await client.request("thread/rollback", params));
}
export async function revertCodexConversation(input) {
    if (!input.threadId) {
        throw new Error("Codex thread is not ready for rewind");
    }
    const targetTurnIndex = input.userMessageTurns.resolve(input.messageId);
    if (targetTurnIndex === null) {
        throw new Error(`Codex could not find user message ${input.messageId} in the current thread`);
    }
    const currentUserTurnCount = input.userMessageTurns.count();
    const numTurns = currentUserTurnCount - targetTurnIndex;
    if (numTurns < 0) {
        throw new Error(`Codex user message ${input.messageId} is outside the current thread`);
    }
    // Fork is non-destructive: the old thread file stays on disk and remains
    // recoverable with `codex resume <old-uuid>` if the rewind target was wrong.
    const forked = await forkCodexThread(input.client, {
        threadId: input.threadId,
        cwd: input.cwd ?? null,
        model: input.model ?? null,
        serviceTier: input.serviceTier ?? null,
        excludeTurns: false,
        persistExtendedHistory: true,
    });
    const forkedThreadId = forked.thread.id;
    // Codex rollback is chat-only by design. File edits from rewound turns stay
    // on disk; a future file primitive would be a separate capability.
    const rolledBack = await rollbackCodexThread(input.client, {
        threadId: forkedThreadId,
        numTurns,
    });
    await input.setThreadId(rolledBack.thread.id);
}
//# sourceMappingURL=rewind.js.map