/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { randomBytes } from 'crypto';
/**
 * Manages the state of the active conversation context for the CLI session.
 * This is a singleton that holds the current conversationId and parentId.
 */
class ConversationContextManager {
    context = {};
    /**
     * Generates a new, random conversation ID.
     */
    generateConversationId() {
        return `conv_${randomBytes(16).toString('hex')}`;
    }
    /**
     * Starts a new conversation, generating a new ID and clearing the parent ID.
     */
    startNewConversation() {
        this.context = {
            conversationId: this.generateConversationId(),
            parentId: undefined,
        };
        if (process.env.DEBUG) {
            console.log(`[ConversationContext] Started new conversation: ${this.context.conversationId}`);
        }
    }
    /**
     * Retrieves the current conversation context.
     * If no conversation is active, it starts a new one.
     */
    getContext() {
        if (!this.context.conversationId) {
            this.startNewConversation();
        }
        return this.context;
    }
    /**
     * Updates the parent ID for the next turn in the conversation.
     * @param newParentId The ID of the most recent message, which becomes the parent for the next message.
     */
    setParentId(newParentId) {
        if (this.context.conversationId) {
            this.context.parentId = newParentId;
            if (process.env.DEBUG) {
                console.log(`[ConversationContext] Set parentId to: ${newParentId}`);
            }
        }
        else {
            console.warn('[ConversationContext] Cannot set parentId without an active conversation.');
        }
    }
    /**
     * Restores the full conversation context, e.g., when loading a session.
     * @param newContext The full context to restore.
     */
    setContext(newContext) {
        this.context = newContext;
        if (process.env.DEBUG) {
            console.log(`[ConversationContext] Restored context: convId=${newContext.conversationId}, parentId=${newContext.parentId}`);
        }
    }
    /**
     * Clears the current conversation context.
     */
    reset() {
        this.context = {};
    }
}
// Export a singleton instance.
export const ConversationContext = new ConversationContextManager();
//# sourceMappingURL=ConversationContext.js.map