import { CoreSession } from "../session/core_session.js";
import { ConversationTurnRuntime, } from "./conversation_turn_runtime.js";
import { composeSessionPrompts } from "./session_prompt_composer.js";
export class ChatRuntime {
    sessionInstance;
    turnRuntime;
    currentPersona;
    riskLevel;
    promptContext;
    environment;
    latestPromptComposition;
    static create(options) {
        const promptComposition = composeSessionPrompts({
            persona: options.persona,
            riskLevel: options.riskLevel,
            cwd: options.promptContext.cwd,
            datetime: new Date(options.environment.now()).toISOString(),
            platform: options.environment.platform(),
            nodeVersion: options.environment.nodeVersion(),
            skillsBlock: options.promptContext.skillsBlock,
            projectContextBlock: options.promptContext.projectContextBlock,
        });
        const session = new CoreSession({
            persona: options.persona,
            systemPrompt: promptComposition.baseSystemPrompt,
            subagentPrompts: promptComposition.subagentPrompts,
            riskLevel: options.riskLevel,
            toolRegistry: options.toolRegistry,
            config: options.config,
            deps: options.deps,
            cwd: options.promptContext.cwd,
            home: options.promptContext.home,
            includeAgentContext: options.promptContext.includeAgentContext,
        });
        return new ChatRuntime({
            session,
            persona: options.persona,
            riskLevel: options.riskLevel,
            promptContext: options.promptContext,
            environment: options.environment,
            initialPromptComposition: promptComposition,
        });
    }
    constructor(options) {
        this.sessionInstance = options.session;
        this.turnRuntime = new ConversationTurnRuntime(this.sessionInstance);
        this.currentPersona = options.persona;
        this.riskLevel = options.riskLevel;
        this.promptContext = { ...options.promptContext };
        this.environment = options.environment;
        this.latestPromptComposition = options.initialPromptComposition ?? this.composePromptSet();
        if (!options.initialPromptComposition) {
            this.sessionInstance.setPersona(this.currentPersona, this.latestPromptComposition.baseSystemPrompt, this.latestPromptComposition.subagentPrompts);
        }
    }
    get session() {
        return this.sessionInstance;
    }
    get isTurnRunning() {
        return this.turnRuntime.isRunning;
    }
    get promptComposition() {
        return this.latestPromptComposition;
    }
    runTurn() {
        return this.turnRuntime.run();
    }
    interruptTurn() {
        return this.turnRuntime.interrupt();
    }
    setConfig(config) {
        this.sessionInstance.setConfig(config);
    }
    setRiskLevel(level) {
        const previous = this.riskLevel;
        this.riskLevel = level;
        this.sessionInstance.setRiskLevel(level);
        if (previous !== level) {
            this.rebuildSubagentPrompts();
        }
    }
    setPersona(persona, options) {
        this.currentPersona = persona;
        this.rebuildSystemPrompts(options);
    }
    updatePromptContext(context) {
        this.promptContext = {
            ...this.promptContext,
            ...context,
        };
        this.sessionInstance.setPromptContext({
            cwd: this.promptContext.cwd,
            home: this.promptContext.home,
            includeAgentContext: this.promptContext.includeAgentContext,
        });
    }
    rebuildSystemPrompts(options) {
        if (options?.skillsBlock !== undefined) {
            this.promptContext.skillsBlock = options.skillsBlock;
        }
        this.latestPromptComposition = this.composePromptSet(options?.skillsBlock);
        this.sessionInstance.setPersona(this.currentPersona, this.latestPromptComposition.baseSystemPrompt, this.latestPromptComposition.subagentPrompts);
    }
    rebuildSubagentPrompts(options) {
        if (options?.skillsBlock !== undefined) {
            this.promptContext.skillsBlock = options.skillsBlock;
        }
        const nextPromptComposition = this.composePromptSet(options?.skillsBlock);
        this.latestPromptComposition = {
            ...this.latestPromptComposition,
            subagentPrompts: nextPromptComposition.subagentPrompts,
        };
        this.sessionInstance.setPersona(this.currentPersona, this.latestPromptComposition.baseSystemPrompt, this.latestPromptComposition.subagentPrompts);
    }
    composePromptSet(skillsBlock) {
        return composeSessionPrompts({
            persona: this.currentPersona,
            riskLevel: this.riskLevel,
            cwd: this.promptContext.cwd,
            datetime: new Date(this.environment.now()).toISOString(),
            platform: this.environment.platform(),
            nodeVersion: this.environment.nodeVersion(),
            skillsBlock: skillsBlock ?? this.promptContext.skillsBlock,
            projectContextBlock: this.promptContext.projectContextBlock,
        });
    }
}
//# sourceMappingURL=chat_runtime.js.map