import { anthropicShellSessionDetectionPractice } from "./anthropic.js";
import { deepmindShellSessionDetectionPractice } from "./deepmind.js";
import { openaiShellSessionDetectionPractice } from "./openai.js";
import { buildShellPracticeAuditMetadata, createShellBaseToolDefinition, createShellCoreHandler, injectRuntimeInvocationMetadata, jsonSchema, } from "../../_shared/baseToolAdapter.js";
import { executeShellSessionDetection as executeShellSessionDetectionCore, } from "./core.js";
import { shellSessionDetectionDependencyDeclarations, } from "./dependencies.js";
export * from "./core.js";
export const shellSessionDetectionProviderPractices = [
    anthropicShellSessionDetectionPractice,
    openaiShellSessionDetectionPractice,
    deepmindShellSessionDetectionPractice,
];
function orderedPractices(preferredProvider) {
    if (preferredProvider === undefined) {
        return shellSessionDetectionProviderPractices;
    }
    return [
        ...shellSessionDetectionProviderPractices.filter((practice) => practice.providerName === preferredProvider),
        ...shellSessionDetectionProviderPractices.filter((practice) => practice.providerName !== preferredProvider),
    ];
}
export function selectShellSessionDetectionPractice(dependencies = {}) {
    for (const practice of orderedPractices(dependencies.preferredProvider)) {
        const provider = practice.createProvider(dependencies);
        if (provider !== undefined) {
            return { providerName: practice.providerName, practice, provider };
        }
    }
    return {
        providerName: "praxis-native",
        practice: {
            providerName: "praxis-native",
            source: { kind: "praxis-native", label: "Praxis dry-run fallback" },
            directCliSupport: false,
            sideEffectPolicy: "runtime-governed",
            notes: ["No injected or host session detection provider is currently available; dry-run remains available."],
            createProvider: () => undefined,
        },
    };
}
function auditMetadata(selection) {
    return buildShellPracticeAuditMetadata({
        providerName: selection.providerName,
        sourceLabel: selection.practice.source.label,
        sourceKind: selection.practice.source.kind,
        sourcePath: selection.practice.source.path,
        directCliSupport: selection.practice.directCliSupport,
        sideEffectPolicy: selection.practice.sideEffectPolicy,
        notes: selection.practice.notes,
    });
}
function recordValue(value) {
    return value !== null && typeof value === "object" && !Array.isArray(value)
        ? value
        : {};
}
function requestValue(value) {
    return recordValue(value);
}
export async function executeShellSessionDetection(request = {}) {
    const normalizedRequest = requestValue(request);
    const context = recordValue(normalizedRequest.context);
    const selection = selectShellSessionDetectionPractice({
        executor: normalizedRequest.executor,
        provider: normalizedRequest.provider,
        preferredProvider: normalizedRequest.preferredProvider,
    });
    return executeShellSessionDetectionCore({
        ...normalizedRequest,
        provider: selection.provider,
        context: {
            ...context,
            auditMetadata: {
                ...recordValue(context.auditMetadata),
                ...auditMetadata(selection),
            },
        },
    });
}
const shellDetectionContextSchema = {
    type: "object",
    additionalProperties: true,
    properties: {
        runtimeId: { type: "string" },
        sessionId: { type: "string" },
        invocationId: { type: "string" },
        dryRun: { type: "boolean" },
        guard: {
            type: "object",
            additionalProperties: true,
            properties: {
                allowed: { type: "boolean" },
                accepted: { type: "boolean" },
                reason: { type: "string" },
            },
        },
    },
};
export const shellSessionDetectionBestPracticeDescriptor = {
    toolId: "shell.sessionDetection",
    bestPractice: "runtime-execEngine-shellSessionProvider",
    sourcePriority: ["cli", "agent-sdk", "api-sdk", "praxis-native"],
    providerOrder: ["anthropic", "openai", "deepmind"],
    dependencies: shellSessionDetectionDependencyDeclarations,
};
export const shellSessionDetectionBaseToolDefinition = createShellBaseToolDefinition({
    toolId: "shell.sessionDetection",
    title: "Shell Session Detection",
    description: "Detect shell session shape through dry-run hints or a governed runtime shell provider.",
    summary: "Use shell.sessionDetection to classify session material while runtime owns process access.",
    storageGroup: "shellDetection",
    riskLevel: "risky",
    permissionHints: ["shell:session:detect"],
    dependencies: shellSessionDetectionDependencyDeclarations,
    inputSchema: jsonSchema("shell.sessionDetection.input", {
        type: "object",
        additionalProperties: true,
        properties: {
            target: {
                type: "object",
                additionalProperties: true,
                properties: {
                    sessionId: { type: "string" },
                    processId: { type: "integer", minimum: 1 },
                    tty: { type: "string" },
                    shellExecutable: { type: "string" },
                    knownInteractive: { type: "boolean" },
                },
            },
            context: shellDetectionContextSchema,
            preferredProvider: { type: "string", enum: ["anthropic", "openai", "deepmind", "praxis-native"] },
        },
    }),
});
export const shellSessionDetectionHandler = createShellCoreHandler(shellSessionDetectionBaseToolDefinition, async (request) => {
    const input = recordValue(request.input);
    const inputContext = recordValue(input.context);
    const practice = selectShellSessionDetectionPractice({
        ...input,
        executor: request.executor,
    });
    return executeShellSessionDetectionCore({
        ...input,
        provider: practice.provider,
        context: {
            ...inputContext,
            runtimeId: inputContext.runtimeId ?? request.runtimeId,
            sessionId: inputContext.sessionId ?? request.sessionId,
            invocationId: inputContext.invocationId ?? request.toolCallId,
            auditMetadata: injectRuntimeInvocationMetadata({
                ...auditMetadata(practice),
                ...recordValue(request.metadata),
            }, recordValue(inputContext.auditMetadata), request),
        },
    });
});
