"use strict";
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.AbstractRequestLogger = void 0;
exports.getCurrentCapturingToken = getCurrentCapturingToken;
exports.storeCapturingTokenForCorrelation = storeCapturingTokenForCorrelation;
exports.retrieveCapturingTokenByCorrelation = retrieveCapturingTokenByCorrelation;
exports.runWithCapturingToken = runWithCapturingToken;
const async_hooks_1 = require("async_hooks");
const requestLogger_1 = require("../common/requestLogger");
const lifecycle_1 = require("../../../util/vs/base/common/lifecycle");
const requestLogStorage = new async_hooks_1.AsyncLocalStorage();
/**
 * Correlation map for preserving CapturingToken across IPC boundaries.
 *
 * When requests cross the VS Code IPC boundary (e.g., BYOK providers),
 * AsyncLocalStorage context is lost. This map allows correlating requests
 * by storing the token before IPC and retrieving it on the other side.
 */
const capturingTokenCorrelationMap = new Map();
/**
 * Get the current CapturingToken from AsyncLocalStorage.
 * Returns undefined if not within a captureInvocation context.
 */
function getCurrentCapturingToken() {
    return requestLogStorage.getStore();
}
/**
 * Store the current CapturingToken with a correlation ID for cross-IPC retrieval.
 * Call this before making a request that will cross IPC boundaries.
 */
function storeCapturingTokenForCorrelation(correlationId) {
    const token = requestLogStorage.getStore();
    if (token) {
        capturingTokenCorrelationMap.set(correlationId, token);
    }
}
/**
 * Retrieve and remove a CapturingToken by correlation ID.
 * Returns undefined if no token was stored for this ID.
 */
function retrieveCapturingTokenByCorrelation(correlationId) {
    const token = capturingTokenCorrelationMap.get(correlationId);
    if (token) {
        capturingTokenCorrelationMap.delete(correlationId);
    }
    return token;
}
/**
 * Run a function within a CapturingToken context without going through IRequestLogger.
 * Used to restore context after IPC boundary crossing.
 */
function runWithCapturingToken(token, fn) {
    return requestLogStorage.run(token, fn);
}
class AbstractRequestLogger extends lifecycle_1.Disposable {
    get promptRendererTracing() {
        return false;
    }
    captureInvocation(request, fn) {
        return requestLogStorage.run(request, () => fn());
    }
    logContentExclusionRules(_repos, _rules, _durationMs) {
        // no-op by default; concrete implementations can override
    }
    logChatRequest(debugName, chatEndpoint, chatParams) {
        return new requestLogger_1.PendingLoggedChatRequest(this, debugName, chatEndpoint, chatParams);
    }
    enableWorkspaceEditTracing() {
        // no-op by default; concrete implementations can override
    }
    disableWorkspaceEditTracing() {
        // no-op by default; concrete implementations can override
    }
    /** Current request being made to the LM. */
    get currentRequest() {
        return requestLogStorage.getStore();
    }
}
exports.AbstractRequestLogger = AbstractRequestLogger;
//# sourceMappingURL=requestLogger.js.map