import { A as getEnvString } from "./logger-CfDSdx1q.js";
import { t as OpenAiChatCompletionProvider } from "./chat-yK5XvXCV.js";
import { t as OpenAiEmbeddingProvider } from "./embedding-Dk5RS-Ds.js";
function buildFireworksProviderConfig(config, env) {
	const envBaseUrl = env?.FIREWORKS_API_BASE_URL || getEnvString("FIREWORKS_API_BASE_URL");
	return {
		...config,
		apiBaseUrl: config?.apiBaseUrl || envBaseUrl || "https://api.fireworks.ai/inference/v1",
		apiKeyEnvar: config?.apiKeyEnvar || "FIREWORKS_API_KEY"
	};
}
function resolveFireworksApiKey(config, env) {
	const envar = config?.apiKeyEnvar || "FIREWORKS_API_KEY";
	return config?.apiKey || env?.[envar] || getEnvString(envar);
}
function resolveFireworksApiUrl(config) {
	if (config?.apiHost) return `https://${config.apiHost}/v1`;
	return config?.apiBaseUrl || "https://api.fireworks.ai/inference/v1";
}
//#endregion
//#region src/providers/fireworks/embedding.ts
var FireworksEmbeddingProvider = class extends OpenAiEmbeddingProvider {
	constructor(modelName, providerOptions) {
		super(modelName, {
			...providerOptions,
			config: buildFireworksProviderConfig(providerOptions.config, providerOptions.env)
		});
	}
	id() {
		return `fireworks:embedding:${this.modelName}`;
	}
	toString() {
		return `[Fireworks AI Embedding Provider ${this.modelName}]`;
	}
	getApiKey() {
		return resolveFireworksApiKey(this.config, this.env);
	}
	getOrganization() {}
	getApiUrl() {
		return resolveFireworksApiUrl(this.config);
	}
};
//#endregion
//#region src/providers/fireworks/chat.ts
const FIREWORKS_EMBEDDING_SUBTYPES = new Set(["embedding", "embeddings"]);
const FIREWORKS_RESERVED_PROVIDER_SUBTYPES = new Set([
	"chat",
	"completion",
	"image",
	"moderation",
	"realtime",
	"responses"
]);
function calculateFireworksCost(config, promptTokens, completionTokens, cached = false, cachedInputTokens = 0) {
	if (cached) return 0;
	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;
	const cacheHitTokens = Math.min(Math.max(cachedInputTokens, 0), promptTokens);
	const uncachedPromptTokens = promptTokens - cacheHitTokens;
	const cacheReadCost = config.cacheReadInputCost ?? inputCost;
	return inputCost * uncachedPromptTokens + cacheReadCost * cacheHitTokens + outputCost * completionTokens;
}
var FireworksProvider = class extends OpenAiChatCompletionProvider {
	constructor(modelName, providerOptions) {
		super(modelName, {
			...providerOptions,
			config: buildFireworksProviderConfig(providerOptions.config, providerOptions.env)
		});
	}
	id() {
		return `fireworks:${this.modelName}`;
	}
	toString() {
		return `[Fireworks AI Provider ${this.modelName}]`;
	}
	getApiKey() {
		return resolveFireworksApiKey(this.config, this.env);
	}
	getOrganization() {}
	getApiUrl() {
		return resolveFireworksApiUrl(this.config);
	}
	async callApi(prompt, context, callApiOptions) {
		const response = await super.callApi(prompt, context, callApiOptions);
		if (response.error) return response;
		const cost = calculateFireworksCost({
			...this.config,
			...context?.prompt?.config
		}, response.tokenUsage?.prompt, response.tokenUsage?.completion, response.cached, readFireworksCachedPromptTokens(response));
		if (cost !== void 0) response.cost = cost;
		return response;
	}
};
function readFireworksCachedPromptTokens(response) {
	const fromUsageDetails = response.tokenUsage?.completionDetails?.cacheReadInputTokens ?? 0;
	const headerRecord = response.metadata?.http?.headers;
	const headerValue = headerRecord?.["fireworks-cached-prompt-tokens"] ?? headerRecord?.["Fireworks-Cached-Prompt-Tokens"];
	const fromHeader = typeof headerValue === "string" ? Number.parseInt(headerValue, 10) : 0;
	return Math.max(fromUsageDetails, fromHeader > 0 ? fromHeader : 0);
}
function createFireworksProvider(providerPath, providerOptions = {}) {
	const splits = providerPath.split(":");
	const subtype = splits[1];
	const constructorOptions = {
		...providerOptions.config,
		env: providerOptions.env
	};
	if (FIREWORKS_EMBEDDING_SUBTYPES.has(subtype)) {
		const embeddingModel = splits.slice(2).join(":");
		if (!embeddingModel) throw new Error(`Fireworks embedding provider needs a model identifier (e.g. "fireworks:embedding:accounts/fireworks/models/qwen3-embedding-8b"), got "${providerPath}".`);
		return new FireworksEmbeddingProvider(embeddingModel, constructorOptions);
	}
	if (FIREWORKS_RESERVED_PROVIDER_SUBTYPES.has(subtype)) throw new Error(`The fireworks:${subtype}:* subtype is reserved for a future dedicated provider. Pass the model directly, e.g. "fireworks:accounts/fireworks/models/llama-v3p3-70b-instruct".`);
	const modelName = splits.slice(1).join(":");
	if (!modelName) throw new Error(`Fireworks provider needs a model identifier (e.g. "fireworks:accounts/fireworks/models/llama-v3p3-70b-instruct"), got "${providerPath}".`);
	return new FireworksProvider(modelName, constructorOptions);
}
//#endregion
export { createFireworksProvider };

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