import { createHash } from 'node:crypto';
import { PromptBuilder } from './prompt.js';
export class ContextCache {
    projectId;
    agentId;
    cachedPrompt;
    cachedConfigHash;
    cachedStaticSegment;
    cachedStaticHash;
    constructor(config) {
        this.projectId = config.projectId;
        this.agentId = config.agentId;
    }
    getSystemPrompt(input) {
        const hash = this.computeConfigHash(input);
        if (this.cachedPrompt && this.cachedConfigHash === hash) {
            return this.cachedPrompt;
        }
        const builder = new PromptBuilder({
            systemPrompt: input.systemPrompt,
            persona: input.persona,
            skills: input.skills,
            basePrompt: input.basePrompt,
            tools: input.tools,
            allowedTools: input.allowedTools,
            runtimeContext: input.runtimeContext,
        });
        this.cachedPrompt = builder.build();
        this.cachedConfigHash = hash;
        return this.cachedPrompt;
    }
    get configHash() {
        return this.cachedConfigHash;
    }
    needsRebuild(input) {
        if (!this.cachedConfigHash)
            return true;
        return this.computeConfigHash(input) !== this.cachedConfigHash;
    }
    getSystemPromptSegmented(input, contextLevel = 'full', workingDirectory) {
        const staticHash = this.computeStaticHash(input);
        const builder = new PromptBuilder({
            systemPrompt: input.systemPrompt,
            persona: input.persona,
            skills: input.skills,
            basePrompt: input.basePrompt,
            tools: input.tools,
            allowedTools: input.allowedTools,
            runtimeContext: input.runtimeContext,
        });
        const segments = builder.buildSegmented(contextLevel, workingDirectory);
        if (this.cachedStaticHash === staticHash && this.cachedStaticSegment !== undefined) {
            return {
                static: this.cachedStaticSegment,
                dynamic: segments.dynamic,
            };
        }
        this.cachedStaticSegment = segments.static;
        this.cachedStaticHash = staticHash;
        return segments;
    }
    invalidate() {
        this.cachedPrompt = undefined;
        this.cachedConfigHash = undefined;
        this.cachedStaticSegment = undefined;
        this.cachedStaticHash = undefined;
    }
    computeStaticHash(input) {
        const parts = [
            this.agentId,
            input.systemPrompt ?? '',
            input.persona?.identity?.role ?? '',
            input.persona?.identity?.description ?? '',
            input.basePrompt ?? '',
            ...(input.skills?.map((s) => s.metadata.name) ?? []),
        ];
        return createHash('sha256').update(parts.join('\0')).digest('hex').slice(0, 16);
    }
    computeConfigHash(input) {
        const parts = [
            this.agentId,
            input.systemPrompt ?? '',
            input.persona?.identity?.role ?? '',
            input.persona?.identity?.description ?? '',
            input.basePrompt ?? '',
            ...(input.skills?.map((s) => s.metadata.name) ?? []),
            ...(input.allowedTools ?? []),
            JSON.stringify(input.runtimeContext ?? {}),
        ];
        return createHash('sha256').update(parts.join('\0')).digest('hex').slice(0, 16);
    }
}
//# sourceMappingURL=context-cache.js.map