/**
 * Logic for extracting model provider from model IDs. Ported from dd-trace-js
 */
import { getLogger } from "../../util/logger.js";
/**
 * Get the canonical model ID and provider in the format expected by LLMObs.
 *
 */
export function getCanonicalModel(params) {
    const { provider: providerFull, modelId } = params;
    if (providerFull.includes("vertex") && providerFull.includes("anthropic")) {
        // https://github.com/DataDog/dd-trace-js/blob/40b188053da19c1c0bde7ab01b63e02d831fb9cb/packages/dd-trace/src/llmobs/plugins/vertexai.js#L21
        return { provider: "anthropic", modelId };
    }
    const providerParts = providerFull.split(".");
    const provider = providerParts[0] ?? params.provider;
    if (provider === "amazon-bedrock") {
        const model = parseBedrockModelId(modelId);
        if (model.modelName === "custom" || model.modelProvider === "custom") {
            getLogger().warn(`Unknown model name/provider (params=${JSON.stringify(params)})`, {
                modelName: model.modelName,
                modelProvider: model.modelProvider,
                params,
            });
        }
        return {
            provider: model.modelProvider ?? provider,
            modelId: model.modelName ?? modelId,
        };
    }
    return { provider, modelId };
}
const BEDROCK_MODEL_TYPE_IDENTIFIERS = [
    "foundation-model/",
    "custom-model/",
    "provisioned-model/",
    "imported-module/",
    "prompt/",
    "endpoint/",
    "inference-profile/",
    "default-prompt-router/",
];
function parseBedrockModelId(modelId) {
    // Best effort to extract the model provider and model name from the bedrock model ID.
    // modelId can be a 1/2 period-separated string or a full AWS ARN, based on the following formats:
    // 1. Base model: "{model_provider}.{model_name}"
    // 2. Cross-region model: "{region}.{model_provider}.{model_name}"
    // 3. Other: Prefixed by AWS ARN "arn:aws{+region?}:bedrock:{region}:{account-id}:"
    //     a. Foundation model: ARN prefix + "foundation-model/{region?}.{model_provider}.{model_name}"
    //     b. Custom model: ARN prefix + "custom-model/{model_provider}.{model_name}"
    //     c. Provisioned model: ARN prefix + "provisioned-model/{model-id}"
    //     d. Imported model: ARN prefix + "imported-module/{model-id}"
    //     e. Prompt management: ARN prefix + "prompt/{prompt-id}"
    //     f. Sagemaker: ARN prefix + "endpoint/{model-id}"
    //     g. Inference profile: ARN prefix + "{application-?}inference-profile/{model-id}"
    //     h. Default prompt router: ARN prefix + "default-prompt-router/{prompt-id}"
    // If model provider cannot be inferred from the modelId formatting, then default to "custom"
    modelId = modelId.toLowerCase();
    if (!modelId.startsWith("arn:aws")) {
        const modelMeta = modelId.split(".");
        if (modelMeta.length < 2) {
            return { modelProvider: "custom", modelName: modelMeta[0] };
        }
        return { modelProvider: modelMeta.at(-2), modelName: modelMeta.at(-1) };
    }
    for (const identifier of BEDROCK_MODEL_TYPE_IDENTIFIERS) {
        if (!modelId.includes(identifier)) {
            continue;
        }
        modelId = modelId.split(identifier).pop() || modelId;
        if (["foundation-model/", "custom-model/"].includes(identifier)) {
            const modelMeta = modelId.split(".");
            if (modelMeta.length < 2) {
                return { modelProvider: "custom", modelName: modelId };
            }
            return { modelProvider: modelMeta.at(-2), modelName: modelMeta.at(-1) };
        }
        return { modelProvider: "custom", modelName: modelId };
    }
    return { modelProvider: "custom", modelName: "custom" };
}
//# sourceMappingURL=utils.js.map