import { join } from "node:path";
import { createArtifactRouter, registerArtifactDir } from "../tools/artifact-protocol.js";
/**
 * Create an ExtensionContext for use in event handlers and tool execution.
 * Context values are resolved at call time, so host changes are reflected.
 */
export function createExtensionContext(source) {
    return {
        get ui() {
            source.assertActive();
            return source.getUIContext();
        },
        get mode() {
            source.assertActive();
            return source.getMode();
        },
        get hasUI() {
            source.assertActive();
            return source.hasUI();
        },
        get cwd() {
            source.assertActive();
            return source.getCwd();
        },
        get sessionManager() {
            source.assertActive();
            return source.getSessionManager();
        },
        get modelRegistry() {
            source.assertActive();
            return source.getModelRegistry();
        },
        get model() {
            source.assertActive();
            return source.getModel();
        },
        get internalResourceRouter() {
            source.assertActive();
            const sessionDir = source.getSessionManager().getSessionDir();
            if (!sessionDir)
                return undefined;
            const artifactsDir = join(sessionDir, "artifacts");
            registerArtifactDir(artifactsDir);
            return createArtifactRouter(() => [artifactsDir]);
        },
        get orchestrationContext() {
            source.assertActive();
            return source.getOrchestrationContext();
        },
        isIdle: () => {
            source.assertActive();
            return source.isIdle();
        },
        isProjectTrusted: () => {
            source.assertActive();
            return source.isProjectTrusted();
        },
        get signal() {
            source.assertActive();
            return source.getSignal();
        },
        abort: () => {
            source.assertActive();
            source.abort();
        },
        hasPendingMessages: () => {
            source.assertActive();
            return source.hasPendingMessages();
        },
        shutdown: () => {
            source.assertActive();
            source.shutdown();
        },
        getContextUsage: () => {
            source.assertActive();
            return source.getContextUsage();
        },
        compact: (options) => {
            source.assertActive();
            source.compact(options);
        },
        getSystemPrompt: () => {
            source.assertActive();
            return source.getSystemPrompt();
        },
    };
}
export function createExtensionCommandContext(source) {
    // Use property descriptors instead of object spread so the guarded getters from
    // createExtensionContext() stay lazy. A spread would eagerly read them once and
    // freeze old values into the returned object, bypassing stale-instance checks.
    const context = Object.defineProperties({}, Object.getOwnPropertyDescriptors(createExtensionContext(source)));
    context.getSystemPromptOptions = () => {
        source.assertActive();
        return source.getSystemPromptOptions();
    };
    context.waitForIdle = () => {
        source.assertActive();
        return source.waitForIdle();
    };
    context.newSession = (options) => {
        source.assertActive();
        return source.newSession(options);
    };
    context.fork = (entryId, options) => {
        source.assertActive();
        return source.fork(entryId, options);
    };
    context.navigateTree = (targetId, options) => {
        source.assertActive();
        return source.navigateTree(targetId, options);
    };
    context.switchSession = (sessionPath, options) => {
        source.assertActive();
        return source.switchSession(sessionPath, options);
    };
    context.reload = () => {
        source.assertActive();
        return source.reload();
    };
    return context;
}
//# sourceMappingURL=runner-context.js.map