import { buildSystemPromptSnapshot } from './prompt/build-system-prompt.js';
import { YagrRunEngine } from './runtime/run-engine.js';
export class YagrAgent {
    engine;
    dependencies;
    history = [];
    promptSnapshot;
    constructor(engine, dependencies = {}) {
        this.engine = engine;
        this.dependencies = dependencies;
        this.promptSnapshot = this.createPromptSnapshot();
    }
    async plan(spec) {
        return this.engine.generateWorkflow(spec);
    }
    async validate(generatedWorkflow) {
        return this.engine.validate(generatedWorkflow);
    }
    async create(spec) {
        const workflow = await this.plan(spec);
        const validation = await this.validate(workflow);
        if (!validation.valid) {
            return { workflow, validation };
        }
        const deployed = await this.engine.deploy(workflow);
        return { workflow, validation, deployed };
    }
    async list() {
        return this.engine.listWorkflows();
    }
    async activate(id) {
        return this.engine.activateWorkflow(id);
    }
    async deactivate(id) {
        return this.engine.deactivateWorkflow(id);
    }
    async delete(id) {
        return this.engine.deleteWorkflow(id);
    }
    async run(prompt, options = {}) {
        this.syncPromptSnapshotBeforeRun();
        const runner = this.createRunner();
        const { result, persistedMessages, workspaceInstructionsMayHaveChanged } = await runner.execute(prompt, options);
        if (workspaceInstructionsMayHaveChanged) {
            const nextSnapshot = this.createPromptSnapshot();
            const promptChanged = nextSnapshot.systemPrompt !== this.promptSnapshot.systemPrompt;
            this.promptSnapshot = nextSnapshot;
            if (promptChanged) {
                this.history.length = 0;
                result.sessionInvalidated = true;
                result.sessionInvalidationReason = 'Workspace instructions changed during the run. Conversation history was cleared and future runs will use the refreshed instruction set.';
            }
        }
        if (options.rememberConversation !== false && !result.sessionInvalidated) {
            this.history.push(...persistedMessages);
        }
        return result;
    }
    clearConversation() {
        this.history.length = 0;
        this.promptSnapshot = this.createPromptSnapshot();
    }
    createPromptSnapshot() {
        return (this.dependencies.buildPromptSnapshot ?? buildSystemPromptSnapshot)(this.engine);
    }
    createRunner() {
        return (this.dependencies.createRunner ?? ((engine, history, systemPrompt) => new YagrRunEngine(engine, history, systemPrompt)))(this.engine, this.history, this.promptSnapshot.systemPrompt);
    }
    syncPromptSnapshotBeforeRun() {
        const nextSnapshot = this.createPromptSnapshot();
        if (nextSnapshot.systemPrompt === this.promptSnapshot.systemPrompt) {
            return;
        }
        this.history.length = 0;
        this.promptSnapshot = nextSnapshot;
    }
}
//# sourceMappingURL=agent.js.map