Apply reviewed edits through the module APIs one request at a time. Keep failed and untouched drafts for retry, and remove successful edits from the review. (cherry picked from commit 952215e36325b76f6e3364d713263f502acf6d02)
355 lines
20 KiB
Vue
355 lines
20 KiB
Vue
<script setup lang="ts">
|
|
import { computed, onMounted, ref, watch } from "vue";
|
|
import { Plus, Trash2 } from "@lucide/vue";
|
|
|
|
import PageHeader from "@/base/PageHeader.vue";
|
|
import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from "@/components/ui/accordion";
|
|
import { Alert, AlertDescription } from "@/components/ui/alert";
|
|
import { Button } from "@/components/ui/button";
|
|
import { Input } from "@/components/ui/input";
|
|
import { Label } from "@/components/ui/label";
|
|
import { Section } from "@/components/ui/section";
|
|
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
|
|
import { Spinner } from "@/components/ui/spinner";
|
|
import { api, describeError, type AgentModel, type ModelPolicy, type ModelProvider } from "@/lib/api";
|
|
import { useRead } from "@/lib/use-read";
|
|
import { errors } from "@/copy/errors";
|
|
import { useSettingsStore } from "@/stores/settings";
|
|
import { canCreateProvider, providerInput } from "./provider-input.ts";
|
|
|
|
const store = useSettingsStore();
|
|
const providers = useRead(() => api.modelProviders());
|
|
const models = useRead(() => api.agentModels());
|
|
const classes = useRead(() => api.modelClasses());
|
|
const credentials = useRead(() => api.credentials());
|
|
const policy = useRead(() => api.modelPolicy());
|
|
onMounted(() => {
|
|
void providers.reload();
|
|
void models.reload();
|
|
void classes.reload();
|
|
void credentials.reload();
|
|
void policy.reload();
|
|
});
|
|
|
|
const NONE = "__none__";
|
|
const providerList = computed(() => [...(providers.data.value ?? [])].sort((a, b) => a.name.localeCompare(b.name)));
|
|
const providerById = computed(() => new Map(providerList.value.map((entry) => [entry.id, entry])));
|
|
const classList = computed(() => classes.data.value ?? []);
|
|
const classNames = computed(() => classList.value.map((entry) => entry.class));
|
|
const bearerCredentials = computed(() =>
|
|
(credentials.data.value ?? []).filter((entry) => entry.kind === "bearer" && entry.scope !== "person"),
|
|
);
|
|
|
|
const hasModels = computed(() => models.data.value !== undefined);
|
|
const hasProviders = computed(() => providers.data.value !== undefined);
|
|
const errorOf = (...reads: { error: { value: string } }[]) => reads.map((read) => read.error.value).find(Boolean) ?? "";
|
|
const saveError = computed(() => store.moduleKinds
|
|
.filter((kind) => kind.startsWith("model"))
|
|
.map(store.pageErrorFor)
|
|
.find(Boolean));
|
|
|
|
const error = ref("");
|
|
async function run(act: () => Promise<void>): Promise<boolean> {
|
|
error.value = "";
|
|
try {
|
|
await act();
|
|
return true;
|
|
} catch (cause) {
|
|
error.value = describeError(cause, errors.common.unknown);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Aliases: which provider and model each name resolves to.
|
|
interface AliasForm {
|
|
provider_id: string;
|
|
model: string;
|
|
}
|
|
const addedAliases = ref<string[]>([]);
|
|
const formOf = (model?: AgentModel): AliasForm => ({ provider_id: model?.provider_id ?? "", model: model?.model ?? "" });
|
|
const aliasPrefix = "model-alias/";
|
|
const aliasKind = (alias: string) => `${aliasPrefix}${alias}`;
|
|
const storedAlias = (alias: string) => store.moduleSaved(aliasKind(alias)) as AliasForm | undefined;
|
|
function trackAlias(alias: string, model?: AgentModel): void {
|
|
store.trackModuleSetting(aliasKind(alias), {
|
|
title: `Model alias: ${alias}`,
|
|
saved: formOf(model),
|
|
read: async () => formOf((await api.agentModels())[alias]),
|
|
write: async (value) => formOf(await api.putAgentModel(alias === "default" ? "" : alias, value)),
|
|
validate: (value) => [
|
|
...(!value.provider_id ? [{ path: "provider_id", label: "Provider", message: "Select a provider." }] : []),
|
|
...(!value.model.trim() ? [{ path: "model", label: "Model", message: "Enter a model." }] : []),
|
|
],
|
|
});
|
|
}
|
|
const trackedAliases = computed(() => store.moduleKinds
|
|
.filter((kind) => kind.startsWith(aliasPrefix)).map((kind) => kind.slice(aliasPrefix.length)));
|
|
watch(models.data, (rows) => {
|
|
if (!rows) return;
|
|
for (const alias of new Set(["default", ...Object.keys(rows), ...trackedAliases.value])) trackAlias(alias, rows[alias]);
|
|
});
|
|
const aliases = computed(() => [...new Set(["default", ...addedAliases.value,
|
|
...trackedAliases.value.filter((alias) => storedAlias(alias)?.model || store.changesFor(aliasKind(alias)).length),
|
|
])]);
|
|
const aliasField = (alias: string) => (store.draftValue(aliasKind(alias)) as AliasForm | undefined) ?? formOf();
|
|
function editAlias(alias: string, field: keyof AliasForm, value: string): void {
|
|
if (!hasModels.value || aliasBusy.value) return;
|
|
store.setDraftValue(aliasKind(alias), { ...aliasField(alias), [field]: value });
|
|
}
|
|
const aliasBusy = ref("");
|
|
const aliasError = ref<Record<string, string>>({});
|
|
const tested = ref<Record<string, string>>({});
|
|
async function aliasRun(alias: string, act: () => Promise<void>): Promise<void> {
|
|
aliasBusy.value = alias;
|
|
aliasError.value = { ...aliasError.value, [alias]: "" };
|
|
try {
|
|
await act();
|
|
} catch (cause) {
|
|
aliasError.value = { ...aliasError.value, [alias]: describeError(cause, errors.common.unknown) };
|
|
} finally {
|
|
aliasBusy.value = "";
|
|
}
|
|
}
|
|
const testAlias = (alias: string) =>
|
|
aliasRun(alias, async () => {
|
|
const field = aliasField(alias);
|
|
const answer = await api.testAgentModel({ provider_id: field.provider_id, model: field.model });
|
|
tested.value = { ...tested.value, [alias]: `${answer.model} answered “${answer.answer.trim()}”` };
|
|
});
|
|
const removeAlias = (alias: string) =>
|
|
aliasRun(alias, async () => {
|
|
if (storedAlias(alias)?.model) await api.deleteAgentModel(alias === "default" ? "" : alias);
|
|
addedAliases.value = addedAliases.value.filter((name) => name !== alias);
|
|
store.forgetModuleSetting(aliasKind(alias));
|
|
await models.reload();
|
|
});
|
|
const newAlias = ref("");
|
|
function addAlias(): void {
|
|
const name = newAlias.value.trim();
|
|
if (!hasModels.value || !name || aliases.value.includes(name)) return;
|
|
trackAlias(name);
|
|
addedAliases.value = [...addedAliases.value, name];
|
|
newAlias.value = "";
|
|
}
|
|
|
|
// Providers: the endpoints aliases call.
|
|
interface ProviderForm {
|
|
name: string;
|
|
class: string;
|
|
base_url: string;
|
|
api_key_credential: string;
|
|
}
|
|
const providerKind = (id: string) => `model-provider/${id}`;
|
|
const providerForm = (provider: ModelProvider): ProviderForm => ({
|
|
name: provider.name,
|
|
class: provider.class,
|
|
base_url: provider.base_url ?? "",
|
|
api_key_credential: provider.api_key_credential_id ?? "",
|
|
});
|
|
function trackProvider(provider: ModelProvider): void {
|
|
const { id } = provider;
|
|
store.trackModuleSetting(providerKind(id), {
|
|
title: (saved) => `Model provider: ${saved.name}`,
|
|
saved: providerForm(provider),
|
|
read: async () => {
|
|
const current = (await api.modelProviders()).find((row) => row.id === id);
|
|
if (!current) throw new Error("This provider was removed. Discard its draft.");
|
|
return providerForm(current);
|
|
},
|
|
write: async (field) => providerForm(await api.putModelProvider(id, providerInput({
|
|
name: field.name,
|
|
class: field.class,
|
|
base_url: field.base_url,
|
|
api_key_credential_id: field.api_key_credential || undefined,
|
|
}))),
|
|
validate: (field) => !field.name.trim() ? [{ path: "name", label: "Name", message: "Enter a name." }] : [],
|
|
});
|
|
}
|
|
watch(providers.data, (rows) => {
|
|
for (const provider of rows ?? []) trackProvider(provider);
|
|
});
|
|
const providerField = (id: string) => (store.draftValue(providerKind(id)) as ProviderForm | undefined) ?? providerForm(providerById.value.get(id)!);
|
|
const storedProviderName = (provider: ModelProvider) => (store.moduleSaved(providerKind(provider.id)) as ProviderForm | undefined)?.name ?? provider.name;
|
|
function editProvider(id: string, field: keyof ProviderForm, value: string): void {
|
|
if (providerBusy.value) return;
|
|
store.setDraftValue(providerKind(id), { ...providerField(id), [field]: value });
|
|
}
|
|
function credentialLabel(id: string): string {
|
|
const key = providerField(id).api_key_credential;
|
|
if (!key) return "no key";
|
|
const found = bearerCredentials.value.find((entry) => entry.id === key);
|
|
if (found) return found.name;
|
|
const saved = providerById.value.get(id);
|
|
if (saved?.api_key_credential_id === key && saved.api_key_credential) return saved.api_key_credential;
|
|
return "missing credential";
|
|
}
|
|
function hasUnknownCredential(id: string): boolean {
|
|
const key = providerField(id).api_key_credential;
|
|
return !!key && !bearerCredentials.value.some((entry) => entry.id === key);
|
|
}
|
|
const providerBusy = ref("");
|
|
const removeProvider = async (id: string): Promise<void> => {
|
|
providerBusy.value = id;
|
|
try {
|
|
await run(async () => {
|
|
await api.deleteModelProvider(id);
|
|
await providers.reload();
|
|
store.forgetModuleSetting(providerKind(id));
|
|
});
|
|
} finally {
|
|
providerBusy.value = "";
|
|
}
|
|
};
|
|
const newProvider = ref("");
|
|
const newClass = ref("");
|
|
const newProviderBaseURL = ref("");
|
|
const newProviderDefaultBaseURL = computed(() => classList.value.find((entry) => entry.class === newClass.value)?.default_base_url ?? "");
|
|
const canAddProvider = computed(() => canCreateProvider(newProvider.value, newClass.value, newProviderBaseURL.value, newProviderDefaultBaseURL.value));
|
|
watch(
|
|
classNames,
|
|
(names) => {
|
|
if (!newClass.value) newClass.value = names[0] ?? "";
|
|
},
|
|
{ immediate: true },
|
|
);
|
|
function addProvider(): void {
|
|
const name = newProvider.value.trim();
|
|
const classId = newClass.value || classNames.value[0];
|
|
if (!classId || !canCreateProvider(name, classId, newProviderBaseURL.value, newProviderDefaultBaseURL.value)) return;
|
|
void run(async () => {
|
|
await api.createModelProvider(providerInput({ name, class: classId, base_url: newProviderBaseURL.value }));
|
|
newProvider.value = "";
|
|
newProviderBaseURL.value = "";
|
|
await providers.reload();
|
|
});
|
|
}
|
|
|
|
// Policy: which aliases workflows and chat may name. Empty means any.
|
|
const POLICY = "models-policy";
|
|
const policyForm = (current: ModelPolicy) => ({
|
|
workflows: current.workflow_aliases?.join(", ") ?? "",
|
|
chat: current.chat_aliases?.join(", ") ?? "",
|
|
});
|
|
function aliasList(text: string): string[] | null {
|
|
const list = text.split(",").map((alias) => alias.trim()).filter(Boolean);
|
|
return list.length ? list : null;
|
|
}
|
|
watch(policy.data, (current) => {
|
|
if (!current) return;
|
|
store.trackModuleSetting(POLICY, {
|
|
title: "Model policy",
|
|
saved: policyForm(current),
|
|
read: async () => policyForm(await api.modelPolicy()),
|
|
write: async ({ workflows, chat }) => policyForm(await api.putModelPolicy({ workflow_aliases: aliasList(workflows), chat_aliases: aliasList(chat) })),
|
|
});
|
|
});
|
|
const policyFields = computed(() => (store.draftValue(POLICY) as ReturnType<typeof policyForm> | undefined));
|
|
function editPolicy(field: keyof ReturnType<typeof policyForm>, value: string): void {
|
|
if (policyFields.value) store.setDraftValue(POLICY, { ...policyFields.value, [field]: value });
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<div>
|
|
<PageHeader title="Models" />
|
|
|
|
<Alert v-if="error || saveError || errorOf(models, providers, classes, credentials, policy)" variant="destructive" class="mb-4">
|
|
<AlertDescription>{{ error || saveError || errorOf(models, providers, classes, credentials, policy) }}</AlertDescription>
|
|
</Alert>
|
|
|
|
<p v-if="!hasModels && !models.error.value" class="flex items-center gap-2 text-label text-fg-muted" role="status"><Spinner />Loading models…</p>
|
|
|
|
<Section v-else-if="hasModels" title="Aliases">
|
|
<p v-if="!storedAlias('default')?.model" class="mb-3 text-label text-warn" role="alert">Set the default alias.</p>
|
|
<div v-for="alias in aliases" :key="alias" class="border-b border-border py-2.5 last-of-type:border-b-0">
|
|
<div class="flex flex-wrap items-center gap-3">
|
|
<Label :for="`model-${alias}`" class="w-28 text-label font-medium @max-xl:w-full">{{ alias }}</Label>
|
|
<Select :model-value="aliasField(alias).provider_id" :disabled="store.savingDrafts || !!aliasBusy" @update:model-value="(value) => editAlias(alias, 'provider_id', String(value ?? ''))">
|
|
<SelectTrigger class="w-44 font-mono" :aria-label="`${alias} provider`"><SelectValue placeholder="provider" /></SelectTrigger>
|
|
<SelectContent>
|
|
<SelectItem v-for="provider in providerList" :key="provider.id" :value="provider.id">{{ storedProviderName(provider) }}</SelectItem>
|
|
</SelectContent>
|
|
</Select>
|
|
<Input :id="`model-${alias}`" :model-value="aliasField(alias).model" :disabled="store.savingDrafts || !!aliasBusy" class="min-w-40 flex-1 basis-40 font-mono" placeholder="model" @update:model-value="(value) => editAlias(alias, 'model', String(value))" />
|
|
<span class="ml-auto flex items-center gap-1">
|
|
<Button variant="outline" size="sm" :disabled="store.savingDrafts || !!aliasBusy || !aliasField(alias).provider_id || !aliasField(alias).model" @click="testAlias(alias)">Test</Button>
|
|
<Button v-if="alias !== 'default' || storedAlias(alias)?.model" variant="ghost" size="icon" :aria-label="`Remove alias ${alias}`" :disabled="store.savingDrafts || !!aliasBusy || (!!storedAlias(alias)?.model && store.changesFor(aliasKind(alias)).length > 0)" @click="removeAlias(alias)"><Trash2 /></Button>
|
|
</span>
|
|
</div>
|
|
<p v-if="tested[alias]" role="status" class="pt-1.5 text-caption text-ok">{{ tested[alias] }}</p>
|
|
<p v-if="aliasError[alias]" role="alert" class="pt-1.5 text-caption text-danger">{{ aliasError[alias] }}</p>
|
|
</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="store.savingDrafts || !newAlias.trim()"><Plus data-icon="inline-start" />Add alias</Button>
|
|
</form>
|
|
</Section>
|
|
|
|
<Section v-if="hasProviders" title="Providers">
|
|
<Accordion type="multiple">
|
|
<AccordionItem v-for="provider in providerList" :key="provider.id" :value="provider.id">
|
|
<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-label font-medium">{{ storedProviderName(provider) }}</span>
|
|
<span class="font-mono text-caption text-fg-muted">{{ providerField(provider.id).class }}</span>
|
|
<span class="min-w-0 truncate font-mono text-caption text-fg-subtle">{{ credentialLabel(provider.id) }}</span>
|
|
</span>
|
|
</AccordionTrigger>
|
|
<AccordionContent>
|
|
<div class="flex flex-wrap items-center gap-3 border-t-0 py-2">
|
|
<Label :for="`prov-${provider.id}-name`" class="w-28 text-label font-medium @max-xl:w-full">Name</Label>
|
|
<Input :id="`prov-${provider.id}-name`" :model-value="providerField(provider.id).name" class="max-w-md font-mono" :aria-label="`Name for ${storedProviderName(provider)}`" :disabled="store.savingDrafts || !!providerBusy" @update:model-value="(value) => editProvider(provider.id, 'name', String(value))" />
|
|
</div>
|
|
<div class="flex flex-wrap items-center gap-3 border-t-0 py-2">
|
|
<Label :for="`prov-${provider.id}-class`" class="w-28 text-label font-medium @max-xl:w-full">Class</Label>
|
|
<Select :model-value="providerField(provider.id).class" :disabled="store.savingDrafts || !!providerBusy" @update:model-value="(value) => editProvider(provider.id, 'class', String(value ?? ''))">
|
|
<SelectTrigger :id="`prov-${provider.id}-class`" class="w-48 font-mono" :aria-label="`Class for ${storedProviderName(provider)}`"><SelectValue /></SelectTrigger>
|
|
<SelectContent><SelectItem v-for="classId in classNames" :key="classId" :value="classId">{{ classId }}</SelectItem></SelectContent>
|
|
</Select>
|
|
</div>
|
|
<div class="flex flex-wrap items-center gap-3 border-t-0 py-2">
|
|
<Label :for="`prov-${provider.id}-url`" class="w-28 text-label font-medium @max-xl:w-full">Base URL</Label>
|
|
<Input :id="`prov-${provider.id}-url`" :model-value="providerField(provider.id).base_url" class="max-w-md font-mono" :placeholder="classList.find((entry) => entry.class === providerField(provider.id).class)?.default_base_url || 'class default'" :disabled="store.savingDrafts || !!providerBusy" @update:model-value="(value) => editProvider(provider.id, 'base_url', String(value))" />
|
|
</div>
|
|
<div class="flex flex-wrap items-center gap-3 border-t-0 py-2">
|
|
<Label :for="`prov-${provider.id}-key`" class="w-28 text-label font-medium @max-xl:w-full">API key</Label>
|
|
<Select :model-value="providerField(provider.id).api_key_credential || NONE" :disabled="store.savingDrafts || !!providerBusy" @update:model-value="(value) => editProvider(provider.id, 'api_key_credential', value === NONE ? '' : String(value ?? ''))">
|
|
<SelectTrigger :id="`prov-${provider.id}-key`" class="w-56" :aria-label="`API key for ${storedProviderName(provider)}`"><SelectValue /></SelectTrigger>
|
|
<SelectContent>
|
|
<SelectItem :value="NONE">No key</SelectItem>
|
|
<SelectItem v-if="hasUnknownCredential(provider.id)" :value="providerField(provider.id).api_key_credential" disabled>{{ credentialLabel(provider.id) }}</SelectItem>
|
|
<SelectItem v-for="credential in bearerCredentials" :key="credential.id" :value="credential.id">{{ credential.name }}</SelectItem>
|
|
</SelectContent>
|
|
</Select>
|
|
</div>
|
|
<div class="flex justify-end gap-1 pb-1">
|
|
<Button variant="ghost" size="sm" class="text-danger" :disabled="store.savingDrafts || !!providerBusy || store.changesFor(providerKind(provider.id)).length > 0" @click="removeProvider(provider.id)"><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" class="max-w-56 font-mono" placeholder="provider" aria-label="New provider" />
|
|
<Select v-model="newClass">
|
|
<SelectTrigger class="w-44 font-mono" aria-label="Provider class"><SelectValue placeholder="class" /></SelectTrigger>
|
|
<SelectContent><SelectItem v-for="classId in classNames" :key="classId" :value="classId">{{ classId }}</SelectItem></SelectContent>
|
|
</Select>
|
|
<Input v-model="newProviderBaseURL" class="min-w-56 flex-1 basis-56 font-mono" :placeholder="newProviderDefaultBaseURL || 'Base URL'" aria-label="Base URL for new provider" />
|
|
<Button type="submit" variant="outline" size="sm" :disabled="store.savingDrafts || !canAddProvider"><Plus data-icon="inline-start" />Add provider</Button>
|
|
</form>
|
|
</Section>
|
|
|
|
<Section v-if="policyFields" title="Policy">
|
|
<div class="flex flex-wrap items-center gap-3 py-2">
|
|
<Label for="policy-workflows" class="w-28 text-label font-medium @max-xl:w-full">Workflows</Label>
|
|
<Input id="policy-workflows" :model-value="policyFields.workflows" class="min-w-40 flex-1 basis-40 font-mono" placeholder="any alias" aria-label="Aliases workflows may use" :disabled="store.savingDrafts" @update:model-value="(value) => editPolicy('workflows', String(value))" />
|
|
</div>
|
|
<div class="flex flex-wrap items-center gap-3 py-2">
|
|
<Label for="policy-chat" class="w-28 text-label font-medium @max-xl:w-full">Chat</Label>
|
|
<Input id="policy-chat" :model-value="policyFields.chat" class="min-w-40 flex-1 basis-40 font-mono" placeholder="any alias" aria-label="Aliases chat may use" :disabled="store.savingDrafts" @update:model-value="(value) => editPolicy('chat', String(value))" />
|
|
</div>
|
|
|
|
</Section>
|
|
</div>
|
|
</template>
|