var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; import { i18n } from "@mariozechner/mini-lit"; import { Button } from "@mariozechner/mini-lit/dist/Button.js"; import { DialogBase } from "@mariozechner/mini-lit/dist/DialogBase.js"; import { Input } from "@mariozechner/mini-lit/dist/Input.js"; import { Label } from "@mariozechner/mini-lit/dist/Label.js"; import { Select } from "@mariozechner/mini-lit/dist/Select.js"; import { html } from "lit"; import { state } from "lit/decorators.js"; import { getAppStorage } from "../storage/app-storage.js"; import { discoverModels } from "../utils/model-discovery.js"; export class CustomProviderDialog extends DialogBase { constructor() { super(...arguments); this.name = ""; this.type = "openai-completions"; this.baseUrl = ""; this.apiKey = ""; this.testing = false; this.testError = ""; this.discoveredModels = []; this.modalWidth = "min(800px, 90vw)"; this.modalHeight = "min(700px, 90vh)"; } static async open(provider, initialType, onSave) { const dialog = new CustomProviderDialog(); dialog.provider = provider; dialog.initialType = initialType; dialog.onSaveCallback = onSave; document.body.appendChild(dialog); dialog.initializeFromProvider(); dialog.open(); dialog.requestUpdate(); } initializeFromProvider() { if (this.provider) { this.name = this.provider.name; this.type = this.provider.type; this.baseUrl = this.provider.baseUrl; this.apiKey = this.provider.apiKey || ""; this.discoveredModels = this.provider.models || []; } else { this.name = ""; this.type = this.initialType || "openai-completions"; this.baseUrl = ""; this.updateDefaultBaseUrl(); this.apiKey = ""; this.discoveredModels = []; } this.testError = ""; this.testing = false; } updateDefaultBaseUrl() { if (this.baseUrl) return; const defaults = { ollama: "http://localhost:11434", "llama.cpp": "http://localhost:8080", vllm: "http://localhost:8000", lmstudio: "http://localhost:1234", "openai-completions": "", "openai-responses": "", "anthropic-messages": "", }; this.baseUrl = defaults[this.type] || ""; } isAutoDiscoveryType() { return this.type === "ollama" || this.type === "llama.cpp" || this.type === "vllm" || this.type === "lmstudio"; } async testConnection() { if (!this.isAutoDiscoveryType()) return; this.testing = true; this.testError = ""; this.discoveredModels = []; try { const models = await discoverModels(this.type, this.baseUrl, this.apiKey || undefined); this.discoveredModels = models.map((model) => ({ ...model, provider: this.name || this.type, })); this.testError = ""; } catch (error) { this.testError = error instanceof Error ? error.message : String(error); this.discoveredModels = []; } finally { this.testing = false; this.requestUpdate(); } } async save() { if (!this.name || !this.baseUrl) { alert(i18n("Please fill in all required fields")); return; } try { const storage = getAppStorage(); const provider = { id: this.provider?.id || crypto.randomUUID(), name: this.name, type: this.type, baseUrl: this.baseUrl, apiKey: this.apiKey || undefined, models: this.isAutoDiscoveryType() ? undefined : this.provider?.models || [], }; await storage.customProviders.set(provider); if (this.onSaveCallback) { this.onSaveCallback(); } this.close(); } catch (error) { console.error("Failed to save provider:", error); alert(i18n("Failed to save provider")); } } renderContent() { const providerTypes = [ { value: "ollama", label: "Ollama (auto-discovery)" }, { value: "llama.cpp", label: "llama.cpp (auto-discovery)" }, { value: "vllm", label: "vLLM (auto-discovery)" }, { value: "lmstudio", label: "LM Studio (auto-discovery)" }, { value: "openai-completions", label: "OpenAI Completions Compatible" }, { value: "openai-responses", label: "OpenAI Responses Compatible" }, { value: "anthropic-messages", label: "Anthropic Messages Compatible" }, ]; return html `