#!/usr/bin/env node
import { M as getEnvString } from "./logger-4ntIRWmf.js";
import { t as OpenAiChatCompletionProvider } from "./chat-DM2FMbcQ.js";
//#region src/providers/nvidia/chat.ts
const NVIDIA_NIM_API_BASE_URL = "https://integrate.api.nvidia.com/v1";
const NVIDIA_RESERVED_PROVIDER_SUBTYPES = new Set([
	"chat",
	"completion",
	"embedding",
	"embeddings",
	"image",
	"moderation",
	"realtime",
	"responses"
]);
function calculateNvidiaCost(config, promptTokens, completionTokens, cached = false) {
	const inputCost = config.inputCost ?? config.cost;
	const outputCost = config.outputCost ?? config.cost;
	if (inputCost === void 0 || outputCost === void 0 || promptTokens === void 0 || completionTokens === void 0) return;
	return cached ? 0 : inputCost * promptTokens + outputCost * completionTokens;
}
var NvidiaProvider = class extends OpenAiChatCompletionProvider {
	constructor(modelName, providerOptions) {
		const explicitBaseUrl = providerOptions.config?.apiBaseUrl;
		const envBaseUrl = providerOptions.env?.NVIDIA_API_BASE_URL || getEnvString("NVIDIA_API_BASE_URL");
		super(modelName, {
			...providerOptions,
			config: {
				...providerOptions.config,
				apiBaseUrl: explicitBaseUrl || envBaseUrl || NVIDIA_NIM_API_BASE_URL,
				apiKeyEnvar: providerOptions.config?.apiKeyEnvar || "NVIDIA_API_KEY"
			}
		});
	}
	id() {
		return `nvidia:${this.modelName}`;
	}
	toString() {
		return `[NVIDIA NIM Provider ${this.modelName}]`;
	}
	getApiKey() {
		const envar = this.config?.apiKeyEnvar || "NVIDIA_API_KEY";
		return this.config?.apiKey || this.env?.[envar] || getEnvString(envar);
	}
	getOrganization() {}
	getApiUrl() {
		return this.config.apiBaseUrl || NVIDIA_NIM_API_BASE_URL;
	}
	async callApi(prompt, context, callApiOptions) {
		const response = await super.callApi(prompt, context, callApiOptions);
		if (response.error) return response;
		response.cost = calculateNvidiaCost({
			...this.config,
			...context?.prompt?.config
		}, response.tokenUsage?.prompt, response.tokenUsage?.completion, response.cached);
		return response;
	}
	toJSON() {
		return {
			provider: "nvidia",
			model: this.modelName,
			config: {
				...this.config,
				...this.config.apiKey && { apiKey: void 0 }
			}
		};
	}
};
function createNvidiaProvider(providerPath, options = {}) {
	const splits = providerPath.split(":");
	if (NVIDIA_RESERVED_PROVIDER_SUBTYPES.has(splits[1])) throw new Error(`Unsupported NVIDIA NIM provider subtype "${splits[1]}" in "${providerPath}". Use "nvidia:<model>" for chat-completion models.`);
	const modelName = splits.slice(1).join(":");
	if (!modelName) throw new Error(`Invalid NVIDIA NIM provider path: "${providerPath}" — expected "nvidia:<model>"`);
	const providerOptions = options.config ? { ...options.config } : {};
	if (options.env && !providerOptions.env) providerOptions.env = options.env;
	if (options.id && !providerOptions.id) providerOptions.id = options.id;
	return new NvidiaProvider(modelName, providerOptions);
}
//#endregion
export { createNvidiaProvider };

//# sourceMappingURL=chat-BKCz5I4x.js.map