import { buildLanguageKey, buildLengthKey, buildSummaryCacheKey, hashJson, normalizeContentForHash, } from "../cache.js";
import { resolveOutputLanguageSetting, resolveSummaryLength } from "../run/run-settings.js";
import { buildAgentPromptHash } from "./agent.js";
export function buildAgentCacheKey({ pageUrl, cacheContent, model, length, language, automationEnabled, }) {
    const contentHash = hashJson({
        pageUrl: pageUrl.trim(),
        content: normalizeContentForHash(cacheContent),
    });
    const promptHash = buildAgentPromptHash(automationEnabled);
    const { lengthArg } = resolveSummaryLength(length, "xl");
    const outputLanguage = resolveOutputLanguageSetting({
        raw: language,
        fallback: { kind: "auto" },
    });
    const modelKey = typeof model === "string" && model.trim() ? model.trim() : "auto";
    return buildSummaryCacheKey({
        contentHash,
        promptHash,
        model: modelKey,
        lengthKey: buildLengthKey(lengthArg),
        languageKey: buildLanguageKey(outputLanguage),
    });
}
function filterChatHistoryMessages(raw) {
    if (!Array.isArray(raw))
        return [];
    const now = Date.now();
    return raw.flatMap((item) => {
        if (!item || typeof item !== "object")
            return [];
        const message = item;
        if (message.role !== "user" && message.role !== "assistant" && message.role !== "toolResult") {
            return [];
        }
        return [
            {
                ...message,
                timestamp: typeof message.timestamp === "number" ? message.timestamp : now,
            },
        ];
    });
}
function resolveCacheStore(cacheState) {
    return cacheState.mode === "default" ? cacheState.store : null;
}
export function readAgentHistory({ cacheState, cacheInput, }) {
    const cacheStore = resolveCacheStore(cacheState);
    if (!cacheStore)
        return null;
    const cached = cacheStore.getJson("chat", buildAgentCacheKey(cacheInput));
    if (!cached)
        return null;
    const rawMessages = Array.isArray(cached)
        ? cached
        : typeof cached === "object" &&
            cached &&
            Array.isArray(cached.messages)
            ? cached.messages
            : null;
    return rawMessages ? filterChatHistoryMessages(rawMessages) : null;
}
export function writeAgentHistory({ cacheState, cacheInput, messages, assistant, }) {
    const cacheStore = resolveCacheStore(cacheState);
    if (!cacheStore)
        return;
    const history = filterChatHistoryMessages([
        ...(Array.isArray(messages) ? messages : []),
        assistant,
    ]);
    cacheStore.setJson("chat", buildAgentCacheKey(cacheInput), { messages: history }, cacheState.ttlMs);
}
//# sourceMappingURL=agent-cache.js.map