/**
 * Migration from V1 context format to V2.
 *
 * V1 format has:
 * - charsPerToken, l1Turns, l2Turns, l3Turns, attachments (no version field)
 * - TokenizedMessage format with tokenCount at top level
 * - toolSummariesById at top level
 *
 * V2 format has:
 * - version: 2, messages, toolSummariesById
 * - StoredMessage format with _superblocks metadata
 * - toolSummaries stored on individual messages
 */
function toStoredMessage(msg) {
    const { tokenCount, timestamp, hasBreakPoint, ...rest } = msg;
    return {
        ...rest,
        _superblocks: {
            tokenCount,
            timestamp: timestamp ?? Date.now(),
        },
    };
}
/**
 * Convert a message and attach any relevant tool summaries.
 * - For assistant messages: attach input summaries for tool-calls
 * - For tool messages: attach output summaries for tool-results
 */
function toStoredMessageWithSummaries(msg, toolSummariesById) {
    const stored = toStoredMessage(msg);
    if (!toolSummariesById) {
        return stored;
    }
    // Extract tool call/result IDs from message content
    if (msg.role === "assistant" && Array.isArray(msg.content)) {
        const inputSummaries = {};
        for (const part of msg.content) {
            if (typeof part === "object" &&
                part !== null &&
                "type" in part &&
                part.type === "tool-call" &&
                "toolCallId" in part) {
                const toolCallId = part.toolCallId;
                const summary = toolSummariesById[toolCallId];
                if (summary?.input) {
                    inputSummaries[toolCallId] = summary.input;
                }
            }
        }
        if (Object.keys(inputSummaries).length > 0) {
            stored._superblocks = {
                ...stored._superblocks,
                timestamp: stored._superblocks?.timestamp ?? Date.now(),
                toolSummaries: inputSummaries,
            };
        }
    }
    else if (msg.role === "tool" && Array.isArray(msg.content)) {
        const outputSummaries = {};
        for (const part of msg.content) {
            if (typeof part === "object" &&
                part !== null &&
                "type" in part &&
                part.type === "tool-result" &&
                "toolCallId" in part) {
                const toolCallId = part.toolCallId;
                const summary = toolSummariesById[toolCallId];
                if (summary?.output) {
                    outputSummaries[toolCallId] = summary.output;
                }
            }
        }
        if (Object.keys(outputSummaries).length > 0) {
            stored._superblocks = {
                ...stored._superblocks,
                timestamp: stored._superblocks?.timestamp ?? Date.now(),
                toolSummaries: outputSummaries,
            };
        }
    }
    return stored;
}
function flattenL3Turn(turn) {
    const messages = [toStoredMessage(turn.user)];
    if (turn.assistant) {
        messages.push(toStoredMessage(turn.assistant));
    }
    return messages;
}
function flattenL2Turn(turn) {
    const messages = [toStoredMessage(turn.user)];
    for (const response of turn.response) {
        messages.push(toStoredMessage(response));
    }
    return messages;
}
function flattenL1Turn(turn, toolSummariesById) {
    const messages = [toStoredMessage(turn.user)];
    for (const step of turn.steps) {
        for (const response of step) {
            // L1 turns are un-compacted, so attach summaries for future compaction
            messages.push(toStoredMessageWithSummaries(response, toolSummariesById));
        }
    }
    return messages;
}
/**
 * Migrate a V1 context record to V2 format.
 *
 * Flattens all turns from oldest to newest (l3 → l2 → l1) into a single
 * messages array. Does not extract system prompt - the new system prompt
 * will be set when the context is used.
 *
 * Tool summaries from V1 are migrated to message-level storage:
 * - L2/L3 turns are already compacted, so they don't need summaries
 * - L1 turns are un-compacted, so summaries are attached to messages
 */
export function migrateV1ToV2(record) {
    const messages = [];
    const { toolSummariesById } = record;
    // L3 and L2 turns are already compacted - no summaries needed
    for (const turn of record.l3Turns) {
        messages.push(...flattenL3Turn(turn));
    }
    for (const turn of record.l2Turns) {
        messages.push(...flattenL2Turn(turn));
    }
    // L1 turns are un-compacted - attach summaries for future compaction
    for (const turn of record.l1Turns) {
        messages.push(...flattenL1Turn(turn, toolSummariesById));
    }
    return {
        version: 2,
        messages,
        attachments: record.attachments,
    };
}
export const v1ToV2Migration = {
    fromVersion: 1,
    toVersion: 2,
    migrate: migrateV1ToV2,
};
//# sourceMappingURL=v1-to-v2.js.map