Skip to content

LLM Wiki is a cross-platform desktop application that turns your documents into an organized, interlinked knowledge base — automatically. Instead of traditional RAG (retrieve-and-answer from scratch every time), the LLM incrementally builds and maintains a persistent wiki from your sources。

TypeScript

feat: support multiple custom llm providers

nash_su2fed492efbab
diff --git a/src/App.tsx b/src/App.tsx
index 89063565..709f3fa4 100644
--- a/src/App.tsx
+++ b/src/App.tsx
@@ -9,7 +9,7 @@ import { useLintStore } from "@/stores/lint-store"
import { useChatStore } from "@/stores/chat-store"
import { BASE_FONT_SIZE_PX, useZoomStore } from "@/stores/zoom-store"
import { openProject } from "@/commands/fs"
-import { getLastProject, getRecentProjects, saveLastProject, loadLlmConfig, loadLanguage, loadSearchApiConfig, loadEmbeddingConfig, loadMineruConfig, loadMultimodalConfig, loadOutputLanguage, loadProviderConfigs, loadActivePresetId, loadTaskModelRouting, loadProjectLlmOverride, loadProxyConfig, loadScheduledImportConfig, saveScheduledImportConfig, loadSourceWatchConfig, loadApiConfig, loadGeneralConfig, loadZoomLevel } from "@/lib/project-store"
+import { getLastProject, getRecentProjects, saveLastProject, loadLlmConfig, loadLanguage, loadSearchApiConfig, loadEmbeddingConfig, loadMineruConfig, loadMultimodalConfig, loadOutputLanguage, loadProviderConfigs, loadCustomLlmPresets, loadActivePresetId, loadTaskModelRouting, loadProjectLlmOverride, loadProxyConfig, loadScheduledImportConfig, saveScheduledImportConfig, loadSourceWatchConfig, loadApiConfig, loadGeneralConfig, loadZoomLevel } from "@/lib/project-store"
import { loadReviewItems, loadLintItems, loadChatHistory, loadChatPreferences } from "@/lib/persist"
import { setupAutoSave } from "@/lib/auto-save"
import { startClipWatcher } from "@/lib/clip-watcher"
@@ -292,6 +292,8 @@ function App() {
if (savedProviderConfigs) {
useWikiStore.getState().setProviderConfigs(savedProviderConfigs)
}
+ const savedCustomLlmPresets = await loadCustomLlmPresets()
+ useWikiStore.getState().setCustomLlmPresets(savedCustomLlmPresets)
const savedActivePreset = await loadActivePresetId()
if (savedActivePreset) {
useWikiStore.getState().setActivePresetId(savedActivePreset)
@@ -302,9 +304,9 @@ function App() {
// `llmConfig` snapshot from a previous launch would keep the
// old value. Overrides still win, so an explicit user choice
// is preserved.
- const { LLM_PRESETS } = await import("@/components/settings/llm-presets")
+ const { findLlmPreset } = await import("@/components/settings/llm-presets")
const { resolveConfig } = await import("@/components/settings/preset-resolver")
- const preset = LLM_PRESETS.find((p) => p.id === savedActivePreset)
+ const preset = findLlmPreset(savedActivePreset, savedCustomLlmPresets)
if (preset) {
const currentFallback = useWikiStore.getState().llmConfig
const override = (savedProviderConfigs ?? {})[savedActivePreset]
@@ -424,6 +426,7 @@ function App() {
llmState.globalLlmConfig,
llmState.providerConfigs,
projectLlmOverride,
+ llmState.customLlmPresets,
))
const projectOutputLang = await loadOutputLanguage(proj.id)
useWikiStore.getState().setOutputLanguage(projectOutputLang ?? "auto")
diff --git a/src/components/chat/chat-panel.tsx b/src/components/chat/chat-panel.tsx
index 8af9eb06..e682dd97 100644
--- a/src/components/chat/chat-panel.tsx
+++ b/src/components/chat/chat-panel.tsx
@@ -467,9 +467,10 @@ export function ChatPanel() {
const providerConfigs = useWikiStore((s) => s.providerConfigs)
const taskModelRouting = useWikiStore((s) => s.taskModelRouting)
const projectLlmOverride = useWikiStore((s) => s.projectLlmOverride)
+ const customLlmPresets = useWikiStore((s) => s.customLlmPresets)
const llmConfig = useMemo(
- () => resolveTaskLlmConfig("chat", baseLlmConfig, providerConfigs, taskModelRouting, projectLlmOverride),
- [baseLlmConfig, providerConfigs, taskModelRouting, projectLlmOverride],
+ () => resolveTaskLlmConfig("chat", baseLlmConfig, providerConfigs, taskModelRouting, projectLlmOverride, customLlmPresets),
+ [baseLlmConfig, providerConfigs, taskModelRouting, projectLlmOverride, customLlmPresets],
)
const searchApiConfig = useWikiStore((s) => s.searchApiConfig)
const anyTxtAvailable = hasConfiguredAnyTxt(searchApiConfig.anyTxt)
diff --git a/src/components/settings/llm-presets.ts b/src/components/settings/llm-presets.ts
index 879eaa11..bfcab623 100644
--- a/src/components/settings/llm-presets.ts
+++ b/src/components/settings/llm-presets.ts
@@ -1,4 +1,4 @@
-import type { AzureModelFamily } from "@/stores/wiki-store"
+import type { AzureModelFamily, CustomLlmPreset } from "@/stores/wiki-store"
/**
* Curated LLM provider presets.
@@ -534,6 +534,23 @@ export const LLM_PRESETS: LlmPreset[] = [
},
]
+export function availableLlmPresets(customPresets: CustomLlmPreset[] = []): LlmPreset[] {
+ return [
+ ...LLM_PRESETS,
+ ...customPresets.map((preset) => ({
+ id: preset.id,
+ label: preset.label,
+ hint: "Custom OpenAI- or Anthropic-compatible endpoint",
+ provider: "custom" as const,
+ apiMode: "chat_completions" as const,
+ })),
+ ]
+}
+
+export function findLlmPreset(id: string, customPresets: CustomLlmPreset[] = []): LlmPreset | undefined {
+ return availableLlmPresets(customPresets).find((preset) => preset.id === id)
+}
+
/**
* Best-effort reverse lookup: given the current LlmConfig fields, which
* preset does it most likely correspond to? Used so the dropdown can
diff --git a/src/components/settings/sections/llm-provider-section.tsx b/src/components/settings/sections/llm-provider-section.tsx
index 302a1be2..c4ba85f1 100644
--- a/src/components/settings/sections/llm-provider-section.tsx
+++ b/src/components/settings/sections/llm-provider-section.tsx
@@ -1,11 +1,12 @@
import { useEffect, useMemo, useState } from "react"
-import { ChevronDown, ChevronRight, AlertCircle, CheckCircle2, Loader2, XCircle } from "lucide-react"
+import { ChevronDown, ChevronRight, AlertCircle, CheckCircle2, Loader2, XCircle, Plus, Trash2 } from "lucide-react"
import { useTranslation } from "react-i18next"
import { invoke } from "@tauri-apps/api/core"
import { Input } from "@/components/ui/input"
+import { Button } from "@/components/ui/button"
import { Label } from "@/components/ui/label"
import { useWikiStore, type ProviderOverride, type ReasoningConfig, type ReasoningMode } from "@/stores/wiki-store"
-import { LLM_PRESETS, type LlmPreset } from "../llm-presets"
+import { availableLlmPresets, findLlmPreset, type LlmPreset } from "../llm-presets"
import { ContextSizeSelector } from "../context-size-selector"
import { disabledLlmConfig, resolveConfig } from "../preset-resolver"
import { normalizeEndpoint } from "@/lib/endpoint-normalizer"
@@ -18,6 +19,8 @@ export function LlmProviderSection() {
const { t } = useTranslation()
const providerConfigs = useWikiStore((s) => s.providerConfigs)
const setProviderConfigs = useWikiStore((s) => s.setProviderConfigs)
+ const customLlmPresets = useWikiStore((s) => s.customLlmPresets)
+ const setCustomLlmPresets = useWikiStore((s) => s.setCustomLlmPresets)
const activePresetId = useWikiStore((s) => s.activePresetId)
const setActivePresetId = useWikiStore((s) => s.setActivePresetId)
const setLlmConfig = useWikiStore((s) => s.setLlmConfig)
@@ -31,6 +34,7 @@ export function LlmProviderSection() {
const [expanded, setExpanded] = useState<Record<string, boolean>>({})
const [savedId, setSavedId] = useState<string | null>(null)
+ const presets = useMemo(() => availableLlmPresets(customLlmPresets), [customLlmPresets])
function toggleExpand(id: string) {
setExpanded((prev) => ({ ...prev, [id]: !prev[id] }))
@@ -43,12 +47,11 @@ export function LlmProviderSection() {
await saveProviderConfigs(newConfigs)
await saveActivePresetId(newActive)
if (newActive) {
- const preset = LLM_PRESETS.find((p) => p.id === newActive)
+ const preset = findLlmPreset(newActive, customLlmPresets)
if (preset) {
const resolved = resolveConfig(preset, newConfigs[newActive], globalLlmConfig)
setGlobalLlmConfig(resolved)
- const { resolveProjectLlmConfig } = await import("@/lib/llm-task-routing")
- setLlmConfig(resolveProjectLlmConfig(resolved, newConfigs, projectLlmOverride))
+ setLlmConfig(resolveProjectLlmConfig(resolved, newConfigs, projectLlmOverride, customLlmPresets))
await saveLlmConfig(resolved)
}
} else {
@@ -60,26 +63,24 @@ export function LlmProviderSection() {
// so the cleared values here do not affect the user's saved settings.
const cleared = disabledLlmConfig(globalLlmConfig)
setGlobalLlmConfig(cleared)
- const { resolveProjectLlmConfig } = await import("@/lib/llm-task-routing")
- setLlmConfig(resolveProjectLlmConfig(cleared, newConfigs, projectLlmOverride))
+ setLlmConfig(resolveProjectLlmConfig(cleared, newConfigs, projectLlmOverride, customLlmPresets))
await saveLlmConfig(cleared)
}
}
function updateOverride(id: string, patch: ProviderOverride) {
- const merged: ProviderOverride = { ...(providerConfigs[id] ?? {}), ...patch }
- const next = { ...providerConfigs, [id]: merged }
+ const currentConfigs = useWikiStore.getState().providerConfigs
+ const merged: ProviderOverride = { ...(currentConfigs[id] ?? {}), ...patch }
+ const next = { ...currentConfigs, [id]: merged }
setProviderConfigs(next)
persist(next, activePresetId).catch(() => {})
// If this preset is active, refresh the resolved LlmConfig live.
if (id === activePresetId) {
- const preset = LLM_PRESETS.find((p) => p.id === id)
+ const preset = findLlmPreset(id, customLlmPresets)
if (preset) {
const resolved = resolveConfig(preset, merged, globalLlmConfig)
setGlobalLlmConfig(resolved)
- import("@/lib/llm-task-routing").then(({ resolveProjectLlmConfig }) => {
- setLlmConfig(resolveProjectLlmConfig(resolved, next, projectLlmOverride))
- }).catch(() => {})
+ setLlmConfig(resolveProjectLlmConfig(resolved, next, projectLlmOverride, customLlmPresets))
}
}
setSavedId(id)
@@ -87,9 +88,10 @@ export function LlmProviderSection() {
}
function toggleActive(id: string) {
- const next = id === activePresetId ? null : id
+ const state = useWikiStore.getState()
+ const next = id === state.activePresetId ? null : id
setActivePresetId(next)
- persist(providerConfigs, next).catch(() => {})
+ persist(state.providerConfigs, next).catch(() => {})
}
async function updateTaskRouting(task: "chat" | "ingest", value: string) {
@@ -102,14 +104,69 @@ export function LlmProviderSection() {
await saveTaskModelRouting(next)
}
+ async function addCustomPreset() {
+ const current = useWikiStore.getState().customLlmPresets
+ if (current.length >= 50) return
+ const id = `custom-${crypto.randomUUID()}`
+ const next = [...current, {
+ id,
+ label: t("settings.sections.llm.customProfiles.defaultName", {
+ number: current.length + 1,
+ }),
+ }]
+ setCustomLlmPresets(next)
+ setExpanded((current) => ({ ...current, [id]: true }))
+ const { saveCustomLlmPresets } = await import("@/lib/project-store")
+ await saveCustomLlmPresets(next)
+ }
+
+ async function renameCustomPreset(id: string, label: string) {
+ const trimmed = label.trim()
+ if (!trimmed) return
+ const next = useWikiStore.getState().customLlmPresets
+ .map((preset) => preset.id === id ? { ...preset, label: trimmed.slice(0, 80) } : preset)
+ setCustomLlmPresets(next)
+ const { saveCustomLlmPresets } = await import("@/lib/project-store")
+ await saveCustomLlmPresets(next)
+ }
+
+ async function deleteCustomPreset(id: string) {
+ const state = useWikiStore.getState()
+ if (state.activePresetId === id) return
+ const nextPresets = state.customLlmPresets.filter((preset) => preset.id !== id)
+ const { [id]: _removed, ...nextConfigs } = state.providerConfigs
+ const nextRouting = {
+ chatPresetId: state.taskModelRouting.chatPresetId === id ? null : state.taskModelRouting.chatPresetId,
+ ingestPresetId: state.taskModelRouting.ingestPresetId === id ? null : state.taskModelRouting.ingestPresetId,
+ }
+ setCustomLlmPresets(nextPresets)
+ setProviderConfigs(nextConfigs)
+ setTaskModelRouting(nextRouting)
+ const { saveCustomLlmPresets, saveProviderConfigs, saveTaskModelRouting } = await import("@/lib/project-store")
+ await Promise.all([
+ saveCustomLlmPresets(nextPresets),
+ saveProviderConfigs(nextConfigs),
+ saveTaskModelRouting(nextRouting),
+ ])
+ if (project && state.projectLlmOverride.presetId === id) {
+ await updateProjectOverride({ enabled: false, presetId: null, model: "" })
+ }
+ }
+
async function updateProjectOverride(patch: Partial<typeof projectLlmOverride>) {
if (!project) return
// Read the latest snapshot synchronously. Multiple input events can arrive
// before React re-renders this closure; using the captured value would
// discard fields changed by the preceding event.
- const current = useWikiStore.getState().projectLlmOverride
+ const state = useWikiStore.getState()
+ const current = state.projectLlmOverride
const draft = { ...current, ...patch }
- const resolved = resolveProjectLlmConfig(globalLlmConfig, providerConfigs, draft)
+ const resolved = resolveProjectLlmConfig(
+ state.globalLlmConfig,
+ state.providerConfigs,
+ draft,
+ state.customLlmPresets,
+ )
const next = { ...draft, profile: draft.enabled ? projectLlmProfile(resolved) : undefined }
setProjectLlmOverride(next)
setLlmConfig(resolved)
@@ -147,6 +204,7 @@ export function LlmProviderSection() {
console.error("Failed to save project provider:", error)
})}
fallbackLabel={t("settings.sections.llm.projectOverride.selectProvider")}
+ presets={presets}
/>
<div className="space-y-1.5">
<Label htmlFor="project-llm-model">{t("settings.sections.llm.projectOverride.model")}</Label>
@@ -176,6 +234,7 @@ export function LlmProviderSection() {
console.error("Failed to save chat model routing:", error)
})}
fallbackLabel={t("settings.sections.llm.taskRouting.activeDefault")}
+ presets={presets}
/>
<TaskModelSelect
id="ingest-task-model"
@@ -185,6 +244,7 @@ export function LlmProviderSection() {
console.error("Failed to save ingest model routing:", error)
})}
fallbackLabel={t("settings.sections.llm.taskRouting.activeDefault")}
+ presets={presets}
/>
<p className="text-xs text-muted-foreground sm:col-span-2">
{t("settings.sections.llm.taskRouting.hint")}
@@ -192,7 +252,18 @@ export function LlmProviderSection() {
</div>
<div className="space-y-2">
- {LLM_PRESETS.map((preset) => (
+ <div className="flex justify-end">
+ <Button
+ variant="outline"
+ size="sm"
+ onClick={() => void addCustomPreset()}
+ disabled={customLlmPresets.length >= 50}
+ >
+ <Plus className="mr-1 h-4 w-4" />
+ {t("settings.sections.llm.customProfiles.add")}
+ </Button>
+ </div>
+ {presets.map((preset) => (
<PresetRow
key={preset.id}
preset={preset}
@@ -203,6 +274,9 @@ export function LlmProviderSection() {
onToggleActive={() => toggleActive(preset.id)}
onToggleExpand={() => toggleExpand(preset.id)}
onChange={(patch) => updateOverride(preset.id, patch)}
+ isUserCustom={preset.id.startsWith("custom-")}
+ onRename={(label) => void renameCustomPreset(preset.id, label)}
+ onDelete={() => void deleteCustomPreset(preset.id)}
/>
))}
</div>
@@ -216,12 +290,14 @@ function TaskModelSelect({
value,
onChange,
fallbackLabel,
+ presets,
}: {
id: string
label: string
value: string
onChange: (value: string) => void
fallbackLabel: string
+ presets: LlmPreset[]
}) {
return (
<div className="space-y-1.5">
@@ -233,7 +309,7 @@ function TaskModelSelect({
className="h-9 w-full rounded-md border border-input bg-background px-3 text-sm"
>
<option value="">{fallbackLabel}</option>
- {LLM_PRESETS.map((preset) => (
+ {presets.map((preset) => (
<option key={preset.id} value={preset.id}>{preset.label}</option>
))}
</select>
@@ -250,6 +326,9 @@ interface PresetRowProps {
onToggleActive: () => void
onToggleExpand: () => void
onChange: (patch: ProviderOverride) => void
+ isUserCustom: boolean
+ onRename: (label: string) => void
+ onDelete: () => void
}
type ProviderTestState =
@@ -266,6 +345,9 @@ function PresetRow({
onToggleActive,
onToggleExpand,
onChange,
+ isUserCustom,
+ onRename,
+ onDelete,
}: PresetRowProps) {
const { t } = useTranslation()
const ov = override ?? {}
@@ -381,6 +463,19 @@ function PresetRow({
{/* Expanded config panel */}
{isExpanded && (
<div className="space-y-4 border-t bg-background/50 px-4 py-3">
+ {isUserCustom && (
+ <div className="space-y-2">
+ <Label>{t("settings.sections.llm.customProfiles.name")}</Label>
+ <Input
+ defaultValue={preset.label}
+ maxLength={80}
+ onBlur={(event) => {
+ if (!event.target.value.trim()) event.target.value = preset.label
+ else onRename(event.target.value)
+ }}
+ />
+ </div>
+ )}
{preset.provider === "custom" && (
<div className="space-y-2">
<Label>{t("settings.sections.llm.apiMode")}</Label>
@@ -636,6 +731,14 @@ function PresetRow({
</div>
)}
</div>
+ {isUserCustom && (
+ <div className="flex justify-end border-t pt-3">
+ <Button variant="outline" size="sm" onClick={onDelete} disabled={isActive}>
+ <Trash2 className="mr-1 h-4 w-4" />
+ {t("settings.sections.llm.customProfiles.delete")}
+ </Button>
+ </div>
+ )}
</div>
)}
</div>
diff --git a/src/i18n/en.json b/src/i18n/en.json
index 4e9f3c2c..9e66d9e1 100644
--- a/src/i18n/en.json
+++ b/src/i18n/en.json
@@ -464,14 +464,14 @@
},
"llm": {
"title": "LLM Models",
- "description": "One row per vendor. Toggling one on automatically turns off the others. Changes save instantly; each vendor's API key is stored independently so switching doesn't lose data.",
+ "description": "Configure built-in providers or add multiple custom endpoints. Activating one automatically deactivates the others, while every provider keeps its own credentials and model settings.",
"taskRouting": {
"chat": "Chat model",
"ingest": "Ingest model",
"activeDefault": "Use active model",
"hint": "Choose separate models for interactive chat and document ingest. The active model remains the fallback."
},
- "projectOverride": {
+ "projectOverride": {
"enabled": "Use a project-specific model",
"provider": "Project provider",
"selectProvider": "Select a provider",
@@ -479,6 +479,12 @@
"modelPlaceholder": "Use the provider's configured model",
"hint": "When enabled, this project uses the selected provider and model for both Chat and Ingest. Credentials remain in the global provider settings."
},
+ "customProfiles": {
+ "add": "Add custom provider",
+ "defaultName": "Custom provider {{number}}",
+ "name": "Provider name",
+ "delete": "Delete custom provider"
+ },
"expand": "Expand config",
"collapse": "Collapse",
"toggleOff": "Click to deactivate",
diff --git a/src/i18n/zh.json b/src/i18n/zh.json
index 5f7aeba9..bc5f8d91 100644
--- a/src/i18n/zh.json
+++ b/src/i18n/zh.json
@@ -464,14 +464,14 @@
},
"llm": {
"title": "LLM 模型",
- "description": "每个厂商一条独立配置。打开开关即切换为当前活跃 provider;其他的会自动关闭。配置改动即时保存,每个厂商的 API Key 独立存储,切换之间不丢失。",
+ "description": "配置内置 Provider,或添加多个自定义端点。启用一个会自动停用其他项,每个 Provider 都独立保存凭据与模型设置。",
"taskRouting": {
"chat": "Chat 模型",
"ingest": "Ingest 模型",
"activeDefault": "使用当前活跃模型",
"hint": "可为交互聊天和文档摄取分别选择模型;未指定时继续使用当前活跃模型。"
},
- "projectOverride": {
+ "projectOverride": {
"enabled": "为当前项目使用独立模型",
"provider": "项目 Provider",
"selectProvider": "选择 Provider",
@@ -479,6 +479,12 @@
"modelPlaceholder": "使用 Provider 已配置的模型",
"hint": "启用后,当前项目的 Chat 和 Ingest 都使用这里选择的 Provider 与模型;API Key 仍保存在全局 Provider 设置中。"
},
+ "customProfiles": {
+ "add": "添加自定义 Provider",
+ "defaultName": "自定义 Provider {{number}}",
+ "name": "Provider 名称",
+ "delete": "删除自定义 Provider"
+ },
"expand": "展开配置",
"collapse": "收起",
"toggleOff": "点击关闭",
diff --git a/src/lib/llm-task-routing.test.ts b/src/lib/llm-task-routing.test.ts
index 36dfd5bc..96d0d5e5 100644
--- a/src/lib/llm-task-routing.test.ts
+++ b/src/lib/llm-task-routing.test.ts
@@ -44,6 +44,23 @@ describe("resolveTaskLlmConfig", () => {
ingestPresetId: "removed-provider",
})).toBe(fallback)
})
+
+ it("routes tasks through a user-defined custom provider", () => {
+ const custom = [{ id: "custom-team", label: "Team Gateway" }]
+ expect(resolveTaskLlmConfig(
+ "chat",
+ fallback,
+ { "custom-team": { apiKey: "team-key", model: "team-model", baseUrl: "https://gateway.example/v1" } },
+ { chatPresetId: "custom-team", ingestPresetId: null },
+ undefined,
+ custom,
+ )).toMatchObject({
+ provider: "custom",
+ apiKey: "team-key",
+ model: "team-model",
+ customEndpoint: "https://gateway.example/v1",
+ })
+ })
})
describe("resolveProjectLlmConfig", () => {
@@ -70,6 +87,20 @@ describe("resolveProjectLlmConfig", () => {
})
})
+ it("supports a user-defined custom provider as a project override", () => {
+ expect(resolveProjectLlmConfig(
+ fallback,
+ { "custom-team": { apiKey: "secret", baseUrl: "https://team.example/v1" } },
+ { enabled: true, presetId: "custom-team", model: "project-model" },
+ [{ id: "custom-team", label: "Team" }],
+ )).toMatchObject({
+ provider: "custom",
+ apiKey: "secret",
+ model: "project-model",
+ customEndpoint: "https://team.example/v1",
+ })
+ })
+
it("makes a project override take precedence over global task routing", () => {
expect(resolveTaskLlmConfig(
"chat",
diff --git a/src/lib/llm-task-routing.ts b/src/lib/llm-task-routing.ts
index f0da1ed7..3be330fd 100644
--- a/src/lib/llm-task-routing.ts
+++ b/src/lib/llm-task-routing.ts
@@ -1,8 +1,9 @@
-import { LLM_PRESETS } from "@/components/settings/llm-presets"
+import { findLlmPreset } from "@/components/settings/llm-presets"
import { resolveConfig } from "@/components/settings/preset-resolver"
import type {
LlmConfig,
ProjectLlmOverride,
+ CustomLlmPreset,
ProviderConfigs,
TaskModelRoutingConfig,
} from "@/stores/wiki-store"
@@ -14,9 +15,10 @@ export function resolveProjectLlmConfig(
globalConfig: LlmConfig,
providerConfigs: ProviderConfigs,
projectOverride: ProjectLlmOverride,
+ customPresets: CustomLlmPreset[] = [],
): LlmConfig {
if (!projectOverride.enabled || !projectOverride.presetId) return globalConfig
- const preset = LLM_PRESETS.find((candidate) => candidate.id === projectOverride.presetId)
+ const preset = findLlmPreset(projectOverride.presetId, customPresets)
if (!preset) return globalConfig
const baseOverride = providerConfigs[projectOverride.presetId]
const override = projectOverride.model.trim()
@@ -42,11 +44,12 @@ export function resolveTaskLlmConfig(
providerConfigs: ProviderConfigs,
routing: TaskModelRoutingConfig,
projectOverride?: ProjectLlmOverride,
+ customPresets: CustomLlmPreset[] = [],
): LlmConfig {
if (projectOverride?.enabled) return fallback
const presetId = task === "chat" ? routing.chatPresetId : routing.ingestPresetId
if (!presetId) return fallback
- const preset = LLM_PRESETS.find((candidate) => candidate.id === presetId)
+ const preset = findLlmPreset(presetId, customPresets)
if (!preset) return fallback
return resolveConfig(preset, providerConfigs[presetId], fallback)
}
@@ -60,5 +63,6 @@ export function getTaskLlmConfig(task: LlmTaskKind, fallback?: LlmConfig): LlmCo
state.providerConfigs,
state.taskModelRouting,
state.projectLlmOverride,
+ state.customLlmPresets,
)
}
diff --git a/src/lib/project-store.test.ts b/src/lib/project-store.test.ts
index 6642263d..c8796724 100644
--- a/src/lib/project-store.test.ts
+++ b/src/lib/project-store.test.ts
@@ -79,3 +79,19 @@ describe("project-store zoom normalization", () => {
expect(__projectStoreTest.normalizeZoomLevel("150")).toBe(1)
})
})
+
+describe("project-store custom LLM preset normalization", () => {
+ it("keeps valid unique presets and bounds labels", () => {
+ const longLabel = "x".repeat(100)
+ expect(__projectStoreTest.normalizeCustomLlmPresets([
+ { id: "custom-one", label: " Team Gateway " },
+ { id: "custom-one", label: "duplicate" },
+ { id: "custom-two", label: longLabel },
+ { id: "openai", label: "collision" },
+ { id: "custom-empty", label: " " },
+ ])).toEqual([
+ { id: "custom-one", label: "Team Gateway" },
+ { id: "custom-two", label: "x".repeat(80) },
+ ])
+ })
+})
diff --git a/src/lib/project-store.ts b/src/lib/project-store.ts
index 2310c48e..e10c4570 100644
--- a/src/lib/project-store.ts
+++ b/src/lib/project-store.ts
@@ -1,6 +1,6 @@
import { load } from "@tauri-apps/plugin-store"
import type { WikiProject } from "@/types/wiki"
-import type { ApiConfig, GeneralConfig, LlmConfig, SearchApiConfig, EmbeddingConfig, MineruConfig, MultimodalConfig, OutputLanguage, ProjectLlmOverride, ProviderConfigs, ProxyConfig, ScheduledImportConfig, SourceWatchConfig, TaskModelRoutingConfig } from "@/stores/wiki-store"
+import type { ApiConfig, CustomLlmPreset, GeneralConfig, LlmConfig, SearchApiConfig, EmbeddingConfig, MineruConfig, MultimodalConfig, OutputLanguage, ProjectLlmOverride, ProviderConfigs, ProxyConfig, ScheduledImportConfig, SourceWatchConfig, TaskModelRoutingConfig } from "@/stores/wiki-store"
import { normalizeSourceWatchConfig } from "@/lib/source-watch-config"
import { normalizePath } from "@/lib/path-utils"
import { DEFAULT_ZOOM_LEVEL, clampZoomLevel } from "@/stores/zoom-store"
@@ -46,7 +46,9 @@ const PROVIDER_CONFIGS_KEY = "providerConfigs"
const ACTIVE_PRESET_KEY = "activePresetId"
const TASK_MODEL_ROUTING_KEY = "taskModelRouting"
const PROJECT_LLM_OVERRIDES_KEY = "projectLlmOverrides"
+const CUSTOM_LLM_PRESETS_KEY = "customLlmPresets"
let projectLlmOverrideWrite = Promise.resolve()
+let customLlmPresetWrite = Promise.resolve()
export async function saveLlmConfig(config: LlmConfig): Promise<void> {
const store = await getStore()
@@ -68,6 +70,38 @@ export async function loadProviderConfigs(): Promise<ProviderConfigs | null> {
return (await store.get<ProviderConfigs>(PROVIDER_CONFIGS_KEY)) ?? null
}
+export async function saveCustomLlmPresets(presets: CustomLlmPreset[]): Promise<void> {
+ const normalized = normalizeCustomLlmPresets(presets)
+ const write = customLlmPresetWrite.then(async () => {
+ const store = await getStore()
+ await store.set(CUSTOM_LLM_PRESETS_KEY, normalized)
+ })
+ customLlmPresetWrite = write.catch(() => {})
+ await write
+}
+
+export async function loadCustomLlmPresets(): Promise<CustomLlmPreset[]> {
+ const store = await getStore()
+ return normalizeCustomLlmPresets(await store.get<unknown>(CUSTOM_LLM_PRESETS_KEY))
+}
+
+function normalizeCustomLlmPresets(value: unknown): CustomLlmPreset[] {
+ if (!Array.isArray(value)) return []
+ const seen = new Set<string>()
+ const normalized: CustomLlmPreset[] = []
+ for (const entry of value) {
+ if (normalized.length >= 50) break
+ const candidate = entry as Partial<CustomLlmPreset> | null
+ if (!candidate || typeof candidate !== "object") continue
+ if (typeof candidate.id !== "string" || !/^custom-[A-Za-z0-9-]{1,80}$/.test(candidate.id)) continue
+ const label = typeof candidate.label === "string" ? candidate.label.trim().slice(0, 80) : ""
+ if (!label || seen.has(candidate.id)) continue
+ seen.add(candidate.id)
+ normalized.push({ id: candidate.id, label })
+ }
+ return normalized
+}
+
export async function saveActivePresetId(id: string | null): Promise<void> {
const store = await getStore()
await store.set(ACTIVE_PRESET_KEY, id)
@@ -206,6 +240,7 @@ function normalizeZoomLevel(level: unknown): number {
export const __projectStoreTest = {
normalizeMineruConfig,
normalizeZoomLevel,
+ normalizeCustomLlmPresets,
}
export async function saveMineruConfig(config: MineruConfig): Promise<void> {
diff --git a/src/stores/wiki-store.ts b/src/stores/wiki-store.ts
index 44c17b68..ac705489 100644
--- a/src/stores/wiki-store.ts
+++ b/src/stores/wiki-store.ts
@@ -323,6 +323,11 @@ export interface ProviderOverride {
export type ProviderConfigs = Record<string, ProviderOverride>
+export interface CustomLlmPreset {
+ id: string
+ label: string
+}
+
export interface TaskModelRoutingConfig {
/** Null keeps chat on the globally active provider preset. */
chatPresetId: string | null
@@ -393,6 +398,7 @@ interface WikiState {
globalLlmConfig: LlmConfig
/** Per-provider-preset stored overrides (API key, model, endpoint, …). */
providerConfigs: ProviderConfigs
+ customLlmPresets: CustomLlmPreset[]
/** Which preset is currently active. `null` = no LLM configured. */
activePresetId: string | null
taskModelRouting: TaskModelRoutingConfig
@@ -424,6 +430,7 @@ interface WikiState {
setLlmConfig: (config: LlmConfig) => void
setGlobalLlmConfig: (config: LlmConfig) => void
setProviderConfigs: (configs: ProviderConfigs) => void
+ setCustomLlmPresets: (presets: CustomLlmPreset[]) => void
setActivePresetId: (id: string | null) => void
setTaskModelRouting: (config: TaskModelRoutingConfig) => void
setProjectLlmOverride: (config: ProjectLlmOverride) => void
@@ -476,6 +483,7 @@ export const useWikiStore = create<WikiState>((set) => ({
localCliIsolation: false,
},
providerConfigs: {},
+ customLlmPresets: [],
activePresetId: null,
taskModelRouting: {
chatPresetId: null,
@@ -631,6 +639,7 @@ export const useWikiStore = create<WikiState>((set) => ({
setLlmConfig: (llmConfig) => set({ llmConfig }),
setGlobalLlmConfig: (globalLlmConfig) => set({ globalLlmConfig }),
setProviderConfigs: (providerConfigs) => set({ providerConfigs }),
+ setCustomLlmPresets: (customLlmPresets) => set({ customLlmPresets }),
setActivePresetId: (activePresetId) => set({ activePresetId }),
setTaskModelRouting: (taskModelRouting) => set({ taskModelRouting }),
setProjectLlmOverride: (projectLlmOverride) => set({ projectLlmOverride }),