import { BestAgentError } from "@best-agent/foundation";
const OPENAI_COMPATIBLE_CAPABILITIES = {
    defaultDecisionTransport: "native_tools",
    supportedDecisionFidelities: ["tool_calls", "structured_control"],
    supportedDecisionTransports: ["native_tools"],
    supportsBackgroundJobs: false,
    supportsPauseResume: false,
    supportsResumableStream: false,
    supportsNativeWebSearch: false,
    supportsWebhookCompletion: false,
};
const OPENAI_NATIVE_CAPABILITIES = {
    defaultDecisionTransport: "native_tools",
    supportedDecisionFidelities: ["tool_calls", "structured_control"],
    supportedDecisionTransports: ["native_tools"],
    supportsBackgroundJobs: true,
    supportsPauseResume: false,
    supportsResumableStream: true,
    supportsNativeWebSearch: true,
    supportsWebhookCompletion: true,
};
const ANTHROPIC_CAPABILITIES = {
    defaultDecisionTransport: "native_tools",
    supportedDecisionFidelities: ["tool_calls", "structured_control"],
    supportedDecisionTransports: ["native_tools"],
    supportsBackgroundJobs: false,
    supportsPauseResume: true,
    supportsResumableStream: false,
    supportsNativeWebSearch: false,
    supportsWebhookCompletion: false,
};
const GOOGLE_CAPABILITIES = {
    defaultDecisionTransport: "native_tools",
    supportedDecisionFidelities: ["tool_calls", "structured_control"],
    supportedDecisionTransports: ["native_tools"],
    supportsBackgroundJobs: false,
    supportsPauseResume: false,
    supportsResumableStream: false,
    supportsNativeWebSearch: false,
    supportsWebhookCompletion: false,
};
const PROVIDER_METADATA = {
    openai: {
        capabilities: OPENAI_NATIVE_CAPABILITIES,
        family: "openai-compatible",
    },
    "openai-responses": {
        capabilities: OPENAI_NATIVE_CAPABILITIES,
        family: "openai-compatible",
        defaultCompatibilityMode: "native",
    },
    anthropic: {
        capabilities: ANTHROPIC_CAPABILITIES,
        family: "anthropic",
    },
    google: {
        capabilities: GOOGLE_CAPABILITIES,
        family: "google",
    },
    xai: {
        capabilities: OPENAI_COMPATIBLE_CAPABILITIES,
        family: "openai-compatible",
        defaultBaseURL: "https://api.x.ai/v1",
        defaultCompatibilityMode: "compatible",
    },
    "xai-responses": {
        capabilities: OPENAI_NATIVE_CAPABILITIES,
        family: "openai-compatible",
        defaultBaseURL: "https://api.x.ai/v1",
        defaultCompatibilityMode: "native",
    },
    deepseek: {
        capabilities: OPENAI_COMPATIBLE_CAPABILITIES,
        family: "openai-compatible",
        defaultBaseURL: "https://api.deepseek.com/v1",
        defaultCompatibilityMode: "compatible",
    },
    moonshotai: {
        capabilities: OPENAI_COMPATIBLE_CAPABILITIES,
        family: "openai-compatible",
        defaultBaseURL: "https://api.moonshot.ai/v1",
        defaultCompatibilityMode: "compatible",
    },
    aihubmix: {
        capabilities: OPENAI_COMPATIBLE_CAPABILITIES,
        family: "openai-compatible",
        defaultBaseURL: "https://aihubmix.com/v1",
        defaultCompatibilityMode: "compatible",
    },
    "aihubmix-responses": {
        capabilities: OPENAI_NATIVE_CAPABILITIES,
        family: "openai-compatible",
        defaultBaseURL: "https://aihubmix.com/v1",
        defaultCompatibilityMode: "native",
    },
    zenmux: {
        capabilities: OPENAI_COMPATIBLE_CAPABILITIES,
        family: "openai-compatible",
        defaultBaseURL: "https://zenmux.ai/api/v1",
        defaultCompatibilityMode: "compatible",
    },
    customProvider: {
        capabilities: OPENAI_COMPATIBLE_CAPABILITIES,
        family: "openai-compatible",
        defaultCompatibilityMode: "compatible",
        requiresBaseURL: true,
    },
};
export function getProviderFamily(kind) {
    return PROVIDER_METADATA[kind]?.family;
}
export function isOpenAiCompatibleProviderKind(kind) {
    return PROVIDER_METADATA[kind]?.family === "openai-compatible";
}
export function getProviderCapabilities(config) {
    const normalized = normalizeProviderConfig(config);
    const metadata = PROVIDER_METADATA[normalized.kind];
    if (!metadata) {
        throw new BestAgentError("PROVIDER_NOT_SUPPORTED", `Provider ${String(normalized.kind)} is not supported.`);
    }
    if (metadata.family !== "openai-compatible") {
        return metadata.capabilities;
    }
    return resolveOpenAiCompatibleCapabilities(normalized);
}
export function normalizeProviderConfig(config) {
    const metadata = PROVIDER_METADATA[config.kind];
    if (!metadata) {
        throw new BestAgentError("PROVIDER_NOT_SUPPORTED", `Provider ${String(config.kind)} is not supported.`);
    }
    const baseURL = config.baseURL?.trim() || metadata.defaultBaseURL;
    const compatibilityMode = config.compatibilityMode ?? metadata.defaultCompatibilityMode ?? "auto";
    const decisionTransport = config.decisionTransport ?? "auto";
    if (!baseURL) {
        if (metadata.requiresBaseURL) {
            throw new BestAgentError("PROVIDER_BASE_URL_REQUIRED", `Provider ${config.kind} requires an explicit baseURL such as https://example.com/v1.`);
        }
        return {
            ...config,
            compatibilityMode,
            decisionTransport,
        };
    }
    let parsed;
    try {
        parsed = new URL(baseURL);
    }
    catch {
        throw new BestAgentError("PROVIDER_BASE_URL_INVALID", `Provider baseURL must be an absolute URL such as https://api.openai.com/v1. Received: ${baseURL}`);
    }
    if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
        throw new BestAgentError("PROVIDER_BASE_URL_INVALID", `Provider baseURL must use http or https. Received: ${baseURL}`);
    }
    return {
        ...config,
        baseURL: parsed.toString().replace(/\/$/, ""),
        compatibilityMode,
        decisionTransport,
    };
}
export function resolveProviderDecisionTransport(config) {
    if (config.decisionTransport &&
        config.decisionTransport !== "auto" &&
        config.decisionTransport !== "native_tools") {
        throw new BestAgentError("PROVIDER_DECISION_INVALID", `Unsupported provider decision transport ${String(config.decisionTransport)}. Use native_tools or auto.`);
    }
    return "native_tools";
}
function resolveOpenAiCompatibleCapabilities(config) {
    const nativeCapabilities = {
        ...OPENAI_NATIVE_CAPABILITIES,
        supportsNativeWebSearch: (config.kind === "openai" || config.kind === "openai-responses") &&
            resolveOpenAiCompatibleMode(config) === "native",
    };
    if (config.compatibilityMode === "compatible") {
        return OPENAI_COMPATIBLE_CAPABILITIES;
    }
    if (config.compatibilityMode === "native") {
        return nativeCapabilities;
    }
    if (!config.baseURL) {
        return nativeCapabilities;
    }
    try {
        return new URL(config.baseURL).hostname !== "api.openai.com"
            ? OPENAI_COMPATIBLE_CAPABILITIES
            : nativeCapabilities;
    }
    catch {
        return nativeCapabilities;
    }
}
export function resolveOpenAiCompatibleMode(config) {
    if (config.compatibilityMode === "compatible") {
        return "compatible";
    }
    if (config.compatibilityMode === "native") {
        return "native";
    }
    if (!config.baseURL) {
        return "native";
    }
    try {
        return new URL(config.baseURL).hostname !== "api.openai.com" ? "compatible" : "native";
    }
    catch {
        return "native";
    }
}
//# sourceMappingURL=provider-metadata.js.map