import { buildMcpPracticeAuditMetadata, createMcpBaseToolDefinition, createMcpCoreHandler, injectRuntimeInvocationMetadata, isJsonObject, jsonSchema, } from "../../_shared/baseToolAdapter.js";
import { anthropicMcpStreamPractice } from "./anthropic.js";
import { deepmindMcpStreamPractice } from "./deepmind.js";
import { mcpStreamDependencyDeclarations, } from "./dependencies.js";
import { openaiMcpStreamPractice } from "./openai.js";
import { executeMcpStream as executeMcpStreamCore, mcpStreamDescriptor, planMcpStream, } from "./core.js";
export * from "./core.js";
export const mcpStreamProviderPractices = [anthropicMcpStreamPractice, openaiMcpStreamPractice, deepmindMcpStreamPractice];
export const mcpStreamBestPracticeDescriptor = {
    toolId: "mcp.stream",
    bestPractice: "storage-owned-mcp-stream-with-runtime-owned-client",
    providerOrder: ["anthropic", "openai", "deepmind"],
    dependencies: mcpStreamDependencyDeclarations,
};
function orderedPractices(preferredProvider) {
    return preferredProvider === undefined
        ? mcpStreamProviderPractices
        : [
            ...mcpStreamProviderPractices.filter((practice) => practice.providerName === preferredProvider),
            ...mcpStreamProviderPractices.filter((practice) => practice.providerName !== preferredProvider),
        ];
}
export function selectMcpStreamPractice(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-owned-client",
            notes: ["No runtime MCP stream provider is available; dry-run remains available."],
            createProvider: () => undefined,
        },
    };
}
function practiceAuditMetadata(selection) {
    return buildMcpPracticeAuditMetadata({
        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,
    });
}
export async function executeMcpStream(request = {}) {
    const requestRecord = isJsonObject(request) ? request : {};
    const selection = selectMcpStreamPractice({
        executor: requestRecord.executor,
        provider: requestRecord.provider,
        preferredProvider: requestRecord.preferredProvider,
    });
    const context = isJsonObject(requestRecord.context) ? requestRecord.context : undefined;
    return executeMcpStreamCore(isJsonObject(request)
        ? { ...requestRecord, context: { ...context, auditMetadata: { ...(context?.auditMetadata ?? {}), ...practiceAuditMetadata(selection) } } }
        : request, selection.provider);
}
export const mcpStreamBaseToolDefinition = createMcpBaseToolDefinition({
    toolId: "mcp.stream",
    title: "MCP Stream",
    description: "Request runtime to start or collect a governed MCP streaming tool call without owning stream handles in baseTool.",
    summary: "Use mcp.stream for runtime-owned MCP streams; runtime owns stream ids, progress, buffers, backpressure, and cancellation.",
    storageGroup: "execution",
    riskLevel: "risky",
    permissionHints: ["mcp:access", "mcp:stream", "mcp:call"],
    dependencies: mcpStreamDependencyDeclarations,
    inputSchema: jsonSchema("mcp.stream.input", { type: "object", additionalProperties: true }),
    outputSchema: jsonSchema("mcp.stream.output", { type: "object", additionalProperties: true }),
});
export const mcpStreamHandler = createMcpCoreHandler(mcpStreamBaseToolDefinition, async (request) => {
    const selection = selectMcpStreamPractice({
        executor: request.executor,
        provider: request.input.provider,
        preferredProvider: request.input.preferredProvider,
    });
    const inputContext = request.input.context ?? {};
    const context = {
        ...inputContext,
        runtimeId: inputContext.runtimeId ?? request.runtimeId,
        sessionId: inputContext.sessionId ?? request.sessionId,
        invocationId: inputContext.invocationId ?? request.toolCallId,
        auditMetadata: injectRuntimeInvocationMetadata({ ...practiceAuditMetadata(selection), ...(request.metadata ?? {}) }, inputContext.auditMetadata, request),
    };
    return executeMcpStreamCore({ ...request.input, context }, selection.provider);
});
export { mcpStreamDescriptor, planMcpStream };
