Files
archie/ui/src/settings/SystemModelsPage.vue
T
sam 74856915ee feat(ui): settings pages edit through TanStack Form with shadcn-vue Field
Each control-plane kind has one store-owned TanStack form, so edits survive
navigation and the save bar submits every dirty section. Shared editors emit
new values, map entries are written whole, unset resources stay null, and the
container runtime page warns when max uptime is under the task limit.
2026-10-26 19:17:03 +11:00

330 lines
17 KiB
Vue

<script setup lang="ts">
import ApplyStatusRows from "./ApplyStatusRows.vue";
import { computed, onMounted, ref, watch } from "vue";
import { storeToRefs } from "pinia";
import { Plus, Trash2 } from "@lucide/vue";
import PageHeader from "@/base/PageHeader.vue";
import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from "@/components/ui/accordion";
import { Button } from "@/components/ui/button";
import { Field, FieldContent, FieldDescription, FieldGroup, FieldLabel } from "@/components/ui/field";
import { Input } from "@/components/ui/input";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { Section } from "@/components/ui/section";
import { Switch } from "@/components/ui/switch";
import { useOrgStore } from "@/stores/org";
import { resourcesForPage, useSettingsStore } from "@/stores/settings";
import DraftHint from "./DraftHint.vue";
import HistoryLink from "./HistoryLink.vue";
import { availableProviders, aliasModelOptions } from "./model-choices";
import SecretRefField from "./SecretRefField.vue";
interface Provider {
class: string;
api_key_env?: string;
api_key_ref: { engine: string; key: string };
base_url?: string;
}
// default runs agent steps and chat when none is named; embedding and
// transcription serve their own purposes. Others are named by steps and chat.
const KNOWN_ALIASES = ["default", "embedding", "transcription"];
// Provider classes the runtime SDK registers.
const CLASSES = ["openai", "anthropic", "azure", "cohere", "deepseek", "gemini", "groq", "mistral", "ollama", "perplexity", "xai"];
const store = useSettingsStore();
const orgStore = useOrgStore();
const { catalog: resourceCatalog, catalogError } = storeToRefs(store);
onMounted(() => Promise.all([store.load(), store.loadConfig(), orgStore.loadOrg()]));
// Map keys are free text, so entries are read from the draft and written whole
// through setDraftEntry; a key is never a form field path.
const resources = computed(() => resourcesForPage(resourceCatalog.value, "models"));
const providers = computed(() => store.draftValue("provider-settings") as Record<string, Provider> | undefined);
const aliases = computed(() => store.draftValue("model-aliases") as Record<string, string> | undefined);
const errors = computed(() =>
["provider-settings", "model-aliases"].map((k) => store.pageErrorFor(k)).filter(Boolean),
);
const providerEntries = computed(() => Object.entries(providers.value ?? {}));
const configuredIds = computed(() => providerEntries.value.map(([id]) => id));
const modelCatalog = computed(() => store.config?.catalog ?? []);
const modelOptions = computed(() => aliasModelOptions(modelCatalog.value, configuredIds.value));
const available = computed(() => availableProviders(modelCatalog.value, configuredIds.value));
const aliasNames = computed(() => [
...KNOWN_ALIASES,
...Object.keys(aliases.value ?? {}).filter((a) => !KNOWN_ALIASES.includes(a)),
]);
// An unset alias is an absent key: the server refuses an empty model.
function setAlias(alias: string, model: string) {
store.setDraftEntry("model-aliases", alias, model.trim() || undefined);
}
function removeAlias(alias: string) {
store.setDraftEntry("model-aliases", alias, undefined);
}
// Embedding runs through the SDK's embed package, which only these classes
// implement (internal/infrastructure/embedding).
const EMBED_CLASSES = ["openai", "openai-compatible", "gemini", "ollama", "cohere", "mistral"];
const embeddingProviders = computed(() =>
providerEntries.value.filter(([, p]) => EMBED_CLASSES.includes(p.class)).map(([id]) => id),
);
// The provider is held locally until a model is typed: the alias itself is
// only stored once both halves exist.
const embeddingProvider = ref("");
const embeddingModel = ref("");
watch(
() => aliases.value?.embedding,
(value) => {
if (!value) return;
embeddingProvider.value = value.split("/")[0] ?? "";
embeddingModel.value = value.split("/").slice(1).join("/");
},
{ immediate: true },
);
function setEmbedding(provider: string, model: string) {
embeddingProvider.value = provider;
embeddingModel.value = model;
setAlias("embedding", provider && model ? `${provider}/${model}` : "");
}
const providerKnown = (model: string) => configuredIds.value.includes(model.split("/")[0]!);
const aliasIssues = computed(() => store.issuesFor("model-aliases"));
const aliasIssue = (alias: string) => aliasIssues.value.find((entry) => entry.path === alias);
const newAlias = ref("");
function addAlias() {
const name = newAlias.value.trim();
if (!aliases.value || !name || aliasNames.value.includes(name)) return;
store.setDraftEntry("model-aliases", name, modelOptions.value[0] ?? "");
newAlias.value = "";
}
// Adding a provider the catalog found usable carries its class, key variable
// and endpoint; any other name starts blank.
const newProvider = ref("");
function addProvider() {
const id = newProvider.value.trim();
if (!providers.value || !id || providers.value[id]) return;
const found = modelCatalog.value.find((p) => p.id === id);
store.setDraftEntry("provider-settings", id, {
class: found?.class || (CLASSES.includes(id) ? id : "openai"),
api_key_env: found?.api_key_env,
base_url: found?.base_url,
api_key_ref: { engine: "", key: "" },
});
newProvider.value = "";
}
function setProvider(name: string, next: Partial<Provider>) {
const current = providers.value?.[name];
if (current) store.setDraftEntry("provider-settings", name, { ...current, ...next });
}
function removeProvider(name: string) {
store.setDraftEntry("provider-settings", name, undefined);
}
const keySource = (p: Provider) =>
p.api_key_ref.key ? `${p.api_key_ref.engine} / ${p.api_key_ref.key}` : p.api_key_env ? `env ${p.api_key_env}` : "no key";
// Org access: which instance models each other org may use, and whether it may
// add its own providers. An org with no entry may use nothing.
interface OrgPolicy {
allowed: string[];
own_providers: boolean;
}
const policies = computed(() => store.draftValue("org-model-policy") as Record<string, OrgPolicy> | undefined);
const otherOrgs = computed(() => (orgStore.instanceAdmin ? orgStore.orgs.filter((o) => o.id !== "org-sys") : []));
const providerWildcards = computed(() => configuredIds.value.map((id) => `${id}/*`));
function policyFor(id: string): OrgPolicy {
return policies.value?.[id] ?? { allowed: [], own_providers: false };
}
function setPolicy(id: string, next: Partial<OrgPolicy>) {
if (!policies.value) return;
const merged = { ...policyFor(id), ...next };
const empty = !merged.allowed.length && !merged.own_providers;
store.setDraftEntry("org-model-policy", id, empty ? undefined : merged);
}
const allowedText = (id: string) => policyFor(id).allowed.join(", ");
function setAllowed(id: string, text: string) {
setPolicy(id, { allowed: text.split(",").map((ref) => ref.trim()).filter(Boolean) });
}
</script>
<template>
<div>
<PageHeader title="Models">
<HistoryLink :kinds="resources.map((r) => r.kind)" />
</PageHeader>
<ApplyStatusRows v-for="resource in resources" :key="resource.kind" :kind="resource.kind" class="mb-4" />
<p v-for="e in [catalogError, ...errors].filter(Boolean)" :key="String(e)" class="mb-4 text-sm text-danger" role="alert">
{{ e }}
</p>
<template v-if="aliases">
<datalist id="alias-models">
<option v-for="m in modelOptions" :key="m" :value="m" />
</datalist>
<Section title="Aliases">
<p v-if="!aliases.default" class="mb-3 text-sm text-warn" role="alert">Set the default alias.</p>
<div v-for="alias in aliasNames" :key="alias" class="flex flex-wrap items-center gap-3 border-b border-border py-2.5 last-of-type:border-b-0">
<label :for="`alias-${alias}`" class="w-28 text-sm font-medium max-sm:w-full">{{ alias }}</label>
<template v-if="alias === 'embedding'">
<Select :model-value="embeddingProvider" @update:model-value="(v) => setEmbedding(String(v), embeddingModel)">
<SelectTrigger class="w-44 font-mono" aria-label="Embedding provider"><SelectValue placeholder="unset" /></SelectTrigger>
<SelectContent>
<SelectItem v-for="id in embeddingProviders" :key="id" :value="id">{{ id }}</SelectItem>
</SelectContent>
</Select>
<Input
:id="`alias-${alias}`"
:model-value="embeddingModel"
class="min-w-0 max-w-64 flex-1 font-mono"
placeholder="model"
:disabled="!embeddingProvider"
@update:model-value="(v) => setEmbedding(embeddingProvider, String(v))"
/>
<span v-if="!embeddingProviders.length" class="text-xs text-fg-subtle">Needs an openai, gemini, ollama, cohere or mistral provider.</span>
</template>
<template v-else>
<Input
:id="`alias-${alias}`"
:model-value="aliases[alias] ?? ''"
list="alias-models"
class="min-w-0 max-w-md flex-1 font-mono max-sm:max-w-none max-sm:basis-full"
placeholder="unset"
:aria-invalid="!!aliasIssue(alias)"
@update:model-value="(v) => setAlias(alias, String(v))"
/>
<DraftHint kind="model-aliases" :path="alias" />
<span v-if="aliasIssue(alias)" class="text-xs text-danger">{{ aliasIssue(alias)?.message }}</span>
<span v-else-if="aliases[alias] && !providerKnown(aliases[alias])" class="text-xs text-warn">Provider not configured.</span>
</template>
<Button v-if="!KNOWN_ALIASES.includes(alias)" variant="ghost" size="icon" class="ml-auto" :aria-label="`Remove alias ${alias}`" @click="removeAlias(alias)">
<Trash2 />
</Button>
</div>
<form class="flex flex-wrap gap-2 border-t border-border pt-3" @submit.prevent="addAlias">
<Input v-model="newAlias" class="max-w-56 font-mono" placeholder="alias" aria-label="New alias" />
<Button type="submit" variant="outline" size="sm" :disabled="!newAlias.trim()"><Plus data-icon="inline-start" />Add alias</Button>
</form>
</Section>
</template>
<template v-if="policies && otherOrgs.length">
<datalist id="org-allowed">
<option v-for="m in [...providerWildcards, ...modelOptions]" :key="m" :value="m" />
</datalist>
<Section title="Org access">
<div v-for="o in otherOrgs" :key="o.id" class="flex flex-wrap items-center gap-3 border-b border-border py-2.5 last-of-type:border-b-0">
<span class="w-28 truncate text-sm font-medium max-sm:w-full" :title="o.id">{{ o.name }}</span>
<Input
:model-value="allowedText(o.id)"
list="org-allowed"
class="min-w-0 max-w-md flex-1 font-mono max-sm:max-w-none max-sm:basis-full"
placeholder="no models"
:aria-label="`Models ${o.name} may use`"
@update:model-value="(v) => setAllowed(o.id, String(v))"
/>
<label class="flex items-center gap-2 text-xs text-fg-muted">
<Switch :model-value="policyFor(o.id).own_providers" @update:model-value="(v) => setPolicy(o.id, { own_providers: !!v })" />
Own providers</label>
<DraftHint kind="org-model-policy" :path="o.id" />
</div>
</Section>
</template>
<template v-if="providers">
<datalist id="available-providers">
<option v-for="p in available" :key="p.id" :value="p.id">{{ p.name }}</option>
</datalist>
<Section title="Providers">
<Accordion type="multiple">
<AccordionItem v-for="[name, provider] in providerEntries" :key="name" :value="name">
<AccordionTrigger class="hover:no-underline">
<span class="flex min-w-0 flex-1 flex-wrap items-center gap-x-3">
<span class="w-28 text-left font-mono text-sm font-medium">{{ name }}</span>
<span v-if="provider.class !== name" class="font-mono text-xs text-fg-muted">{{ provider.class }}</span>
<span class="min-w-0 truncate font-mono text-xs text-fg-subtle">{{ keySource(provider) }}</span>
</span>
</AccordionTrigger>
<AccordionContent>
<FieldGroup class="@container/field-group">
<Field orientation="responsive" class="border-t-0 pt-0">
<FieldContent><FieldLabel>Class</FieldLabel></FieldContent>
<div class="flex min-w-0 flex-wrap items-center gap-3 @md/field-group:flex-1">
<Select :model-value="provider.class" @update:model-value="(v) => setProvider(name, { class: String(v) })">
<SelectTrigger class="w-48 font-mono" :aria-label="`Class for ${name}`"><SelectValue /></SelectTrigger>
<SelectContent>
<SelectItem v-for="c in CLASSES.includes(provider.class) ? CLASSES : [provider.class, ...CLASSES]" :key="c" :value="c">{{ c }}</SelectItem>
</SelectContent>
</Select>
<DraftHint kind="provider-settings" :path="`${name}.class`" />
</div>
</Field>
<Field orientation="responsive">
<FieldContent>
<FieldLabel :for="`prov-${name}-url`">Base URL</FieldLabel>
<FieldDescription>Empty: provider default.</FieldDescription>
</FieldContent>
<div class="flex min-w-0 flex-wrap items-center gap-3 @md/field-group:flex-1">
<Input
:id="`prov-${name}-url`"
:model-value="provider.base_url ?? ''"
class="max-w-md font-mono"
@update:model-value="(v) => setProvider(name, { base_url: String(v) })"
/>
<DraftHint kind="provider-settings" :path="`${name}.base_url`" />
</div>
</Field>
<Field orientation="responsive">
<FieldContent>
<FieldLabel :for="`prov-${name}-env`">API key environment variable</FieldLabel>
<FieldDescription>Used when no secret reference is set.</FieldDescription>
</FieldContent>
<div class="flex min-w-0 flex-wrap items-center gap-3 @md/field-group:flex-1">
<Input
:id="`prov-${name}-env`"
:model-value="provider.api_key_env ?? ''"
aria-label="API key environment variable"
placeholder="API key environment variable"
class="max-w-md font-mono"
@update:model-value="(v) => setProvider(name, { api_key_env: String(v) })"
/>
<DraftHint kind="provider-settings" :path="`${name}.api_key_env`" />
</div>
</Field>
<Field orientation="responsive">
<FieldContent><FieldLabel>API key</FieldLabel></FieldContent>
<div class="flex min-w-0 flex-wrap items-center gap-3 @md/field-group:flex-1">
<SecretRefField
:model-value="provider.api_key_ref"
:id-prefix="`prov-${name}-key`"
:fallback="provider.api_key_env ? `env ${provider.api_key_env}` : undefined"
@update:model-value="(v) => setProvider(name, { api_key_ref: v })"
/>
</div>
</Field>
</FieldGroup>
<div class="flex justify-end pb-1">
<Button variant="ghost" size="sm" class="text-danger" @click="removeProvider(name)"><Trash2 data-icon="inline-start" />Remove</Button>
</div>
</AccordionContent>
</AccordionItem>
</Accordion>
<form class="flex flex-wrap items-center gap-2 border-t border-border pt-3" @submit.prevent="addProvider">
<Input v-model="newProvider" list="available-providers" class="max-w-56 font-mono" placeholder="provider" aria-label="New provider" />
<Button type="submit" variant="outline" size="sm" :disabled="!newProvider.trim()"><Plus data-icon="inline-start" />Add provider</Button>
<span v-if="available.length" class="text-xs text-fg-subtle">Available:
<Button v-for="p in available" :key="p.id" type="button" variant="link" size="xs" class="h-auto px-1 font-mono text-fg-subtle" @click="newProvider = p.id; addProvider()">{{ p.id }}</Button>
</span>
</form>
</Section>
</template>
</div>
</template>