import { getLogger } from "../../util/logger.js";
import { anthropicImpl } from "./impl/anthropic.js";
import { clarkImpl } from "./impl/clark.js";
export const createLLMProvider = (settings, getJwt, llmConfig, getAttributes) => {
    const providerType = llmConfig?.provider || "bedrock";
    const applyAuthAndAttributes = (options) => {
        const jwt = getJwt?.();
        const aiAttributes = getAttributes?.();
        const billing = options.providerOptions?.["billing"];
        const retryMetadata = billing && typeof billing === "object" && billing !== null
            ? billing
            : undefined;
        const existingHeaders = options.headers ?? {};
        const headers = {};
        Object.entries(existingHeaders)
            .filter(([_, value]) => typeof value === "string")
            .forEach(([key, value]) => {
            headers[key] = value;
        });
        if (jwt) {
            headers.Authorization = `Bearer ${jwt}`;
            headers["x-superblocks-authorization"] = `Bearer ${jwt}`;
        }
        const mergedAttributes = {
            ...(options.attributes ?? {}),
            ...(aiAttributes ?? {}),
            ...(retryMetadata ?? {}),
        };
        return {
            ...options,
            headers,
            attributes: mergedAttributes,
        };
    };
    const wrapDefaults = (model) => {
        const wrapped = Object.create(model);
        wrapped.doGenerate = (options) => {
            return model.doGenerate(applyAuthAndAttributes(options));
        };
        wrapped.doStream = (options) => {
            return model.doStream(applyAuthAndAttributes(options));
        };
        return wrapped;
    };
    getLogger().info(`Creating LLM provider: type=${providerType}`, {
        config: llmConfig || {},
    });
    let implFunc;
    switch (providerType) {
        case "bedrock":
        case "vertex":
        case "snowflake":
            implFunc = clarkImpl;
            break;
        case "anthropic":
        default:
            implFunc = anthropicImpl;
            break;
    }
    const impl = implFunc(settings);
    const languageModels = {
        classifyPrompt: wrapDefaults(impl.classifyPrompt),
        explain: wrapDefaults(impl.explain),
        generateBroadEdit: wrapDefaults(impl.generateBroadEdit),
        summarizeMessages: wrapDefaults(impl.summarizeMessages),
        fast: wrapDefaults(impl.fast),
    };
    const modelForTask = (modelId) => {
        return languageModels[modelId];
    };
    return {
        name: providerType,
        modelForTask,
    };
};
//# sourceMappingURL=provider.js.map