import { getPrefixedLogger } from "../../../util/logger.js";
const logger = getPrefixedLogger("[context-v2-adapter]");
export class ContextV2Adapter {
    v2;
    responseCount = 0;
    lastUsage;
    constructor(context) {
        this.v2 = context;
    }
    static fromContext(context) {
        return new ContextV2Adapter(context);
    }
    setSystemPrompt(prompt) {
        this.v2.setSystemPrompt(prompt);
    }
    async initializeWith(messages) {
        // Add each message to the context
        for (const msg of messages) {
            if (msg.role === "user") {
                await this.v2.addUserMessage(msg);
            }
            else if (msg.role === "assistant" || msg.role === "tool") {
                // Not sure what we do for usage... for historical ModelMessages from the ai-sdk, we do not have real usage
                const minimalUsage = {
                    inputTokens: 0,
                    outputTokens: 0,
                    totalTokens: 0,
                };
                await this.v2.addResponse(msg, minimalUsage, { skipCompaction: true });
            }
        }
    }
    async startTurn(user) {
        await this.v2.addUserMessage(user);
        this.responseCount = 0;
    }
    startStep() {
        // No-op - v2 doesn't track step boundaries
    }
    async endStep(responses, usage) {
        const newResponses = responses.slice(this.responseCount);
        this.responseCount = responses.length;
        if (!usage) {
            logger.warn("endStep called without usage data - compaction may not trigger");
        }
        this.lastUsage = usage ?? this.lastUsage;
        // at the very least, we will have some data. on the first turn, this is 0. in the future,
        // either `usage` is defined and we will use that, or we will use the last usage.
        const defaultUsage = this.lastUsage ?? {
            inputTokens: 0,
            outputTokens: 0,
            totalTokens: 0,
        };
        // Only run compaction on the last response to avoid duplicate compaction
        // with stale usage data
        for (let i = 0; i < newResponses.length; i++) {
            const response = newResponses[i];
            const isLast = i === newResponses.length - 1;
            if (response) {
                await this.v2.addResponse(response, usage ?? defaultUsage, {
                    skipCompaction: !isLast,
                });
            }
        }
    }
    endTurn(_usage) {
        // No-op - v2 handles compaction automatically in addResponse
    }
    async getMessages() {
        return this.v2.getMessages();
    }
    storeSummary(toolCallId, summary) {
        this.v2.storeSummary(toolCallId, summary);
    }
    markSkillsNeedReload() {
        this.v2.markSkillsNeedReload();
    }
    checkAndClearSkillsNeedReload() {
        return this.v2.checkAndClearSkillsNeedReload();
    }
    setAttachments(attachments) {
        this.v2.setAttachments(attachments);
    }
    getAttachments() {
        return this.v2.getAttachments();
    }
    truncateAfterTimestamp(timestamp) {
        this.v2.truncateAfterTimestamp(timestamp);
    }
    setCompactionCallbacks(callbacks) {
        this.v2.setCompactionCallbacks(callbacks);
    }
}
//# sourceMappingURL=adapter.js.map