const require_logger = require('./logger-DXu5t4bD.cjs');
require('./esm-C8GQ-4k9.cjs');
require('./pythonUtils-CJADVBl_.cjs');
require('./types-ykSCHP0x.cjs');
require('./util-BdlVY3dk.cjs');
require('./fetch-D5ITc9pG.cjs');
const require_cache = require('./cache-Cv2r51Z1.cjs');
require('./genaiTracer-C0huM5g6.cjs');
const require_chat = require('./chat-CwcRgAF_.cjs');
require('./transform-DTD-82AS.cjs');
require('./openai-BQhdK6dW.cjs');
require('./util-C-X2Ldab.cjs');
const require_completion = require('./completion-CrY96x28.cjs');

//#region src/providers/docker.ts
async function fetchLocalModels(apiBaseUrl) {
	try {
		const { data } = await require_cache.fetchWithCache(`${apiBaseUrl}/models`, void 0, void 0, "json", true, 0);
		return data?.data ?? [];
	} catch (e) {
		throw new Error(`Failed to connect to Docker Model Runner. Is it enabled? Are the API endpoints enabled? For details, see https://docs.docker.com/ai/model-runner. \n${e.message}`);
	}
}
async function hasLocalModel(modelId, apiBaseUrl) {
	return (await fetchLocalModels(apiBaseUrl)).some((model) => model && model.id?.toLocaleLowerCase() === modelId?.toLocaleLowerCase());
}
function parseProviderPath(providerPath) {
	const splits = providerPath.split(":");
	const type = splits[1];
	switch (type) {
		case "chat":
		case "completion":
		case "embeddings": return {
			type,
			model: splits.slice(2).join(":")
		};
		case "embedding": return {
			type: "embeddings",
			model: splits.slice(2).join(":")
		};
		default: return {
			type: "chat",
			model: splits.slice(1).join(":")
		};
	}
}
/**
* Factory for creating Docker Model Runner providers using OpenAI-compatible endpoints.
*/
function createDockerProvider(providerPath, options = {}) {
	const apiBaseUrl = (options?.env?.DOCKER_MODEL_RUNNER_BASE_URL ?? require_logger.getEnvString("DOCKER_MODEL_RUNNER_BASE_URL") ?? "http://localhost:12434") + "/engines/v1";
	const apiKey = options?.env?.DOCKER_MODEL_RUNNER_API_KEY ?? require_logger.getEnvString("DOCKER_MODEL_RUNNER_API_KEY") ?? "dmr";
	const openaiOptions = {
		...options,
		config: {
			...options.config || {},
			apiBaseUrl,
			apiKey
		}
	};
	const { type, model } = parseProviderPath(providerPath);
	switch (type) {
		case "chat":
		default: return new DMRChatCompletionProvider(model, openaiOptions);
		case "completion": return new DMRCompletionProvider(model, openaiOptions);
		case "embeddings": return new DMREmbeddingProvider(model, openaiOptions);
	}
}
var DMRChatCompletionProvider = class extends require_chat.OpenAiChatCompletionProvider {
	async callApi(prompt, context, callApiOptions) {
		if (!await hasLocalModel(this.modelName, this.getApiUrl())) require_logger.logger_default.warn(`Model '${this.modelName}' not found. Run 'docker model pull ${this.modelName}'.`);
		return super.callApi(prompt, context, callApiOptions);
	}
};
var DMRCompletionProvider = class extends require_completion.OpenAiCompletionProvider {
	async callApi(prompt, context, callApiOptions) {
		if (!await hasLocalModel(this.modelName, this.getApiUrl())) require_logger.logger_default.warn(`Model '${this.modelName}' not found. Run 'docker model pull ${this.modelName}'.`);
		return super.callApi(prompt, context, callApiOptions);
	}
};
var DMREmbeddingProvider = class extends require_completion.OpenAiEmbeddingProvider {
	async callEmbeddingApi(text) {
		if (!await hasLocalModel(this.modelName, this.getApiUrl())) require_logger.logger_default.warn(`Model '${this.modelName}' not found. Run 'docker model pull ${this.modelName}'.`);
		return super.callEmbeddingApi(text);
	}
};

//#endregion
exports.createDockerProvider = createDockerProvider;
//# sourceMappingURL=docker-0o-kPfdf.cjs.map