"use strict";
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2024-2026 Gracker (Chris)
// This file is part of SmartPerfetto. See LICENSE for details.
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerCodeAwareLookupForEcho = registerCodeAwareLookupForEcho;
exports.registerCodeAwareCanary = registerCodeAwareCanary;
exports.sanitizeCodeAwareText = sanitizeCodeAwareText;
exports.clearCodeAwareOutputGuards = clearCodeAwareOutputGuards;
const llmEchoOutputFilter_1 = require("./llmEchoOutputFilter");
const sessionStreams = new Map();
function streamFor(sessionId) {
    let stream = sessionStreams.get(sessionId);
    if (!stream) {
        stream = new llmEchoOutputFilter_1.LLMEchoOutputStream();
        sessionStreams.set(sessionId, stream);
    }
    return stream;
}
function registerCodeAwareLookupForEcho(sessionId, result) {
    if (!sessionId)
        return;
    const stream = streamFor(sessionId);
    for (const hit of result.hits) {
        if (!hit.snippet || !hit.metadata?.codebaseId || !hit.metadata.filePath)
            continue;
        const ref = {
            chunkId: hit.chunkId,
            codebaseId: hit.metadata.codebaseId,
            filePath: hit.metadata.filePath,
            ...(hit.metadata.lineRange ? { lineRange: hit.metadata.lineRange } : {}),
            ...(hit.metadata.symbol ? { symbol: hit.metadata.symbol } : {}),
        };
        stream.registerSnippet(hit.snippet, ref);
    }
}
function registerCodeAwareCanary(sessionId, canary) {
    if (!sessionId || !canary)
        return;
    streamFor(sessionId).registerCanary(canary);
}
function sanitizeCodeAwareText(sessionId, text) {
    if (!sessionId || !text)
        return text;
    const stream = sessionStreams.get(sessionId);
    if (!stream)
        return text;
    return stream.write(text) + stream.flush();
}
function clearCodeAwareOutputGuards(sessionId) {
    const stream = sessionStreams.get(sessionId);
    stream?.destroy();
    sessionStreams.delete(sessionId);
}
//# sourceMappingURL=codeAwareOutputRegistry.js.map