173 lines
7.9 KiB
Vue
173 lines
7.9 KiB
Vue
<script setup lang="ts">
|
|
import { Plus } from "@lucide/vue";
|
|
import { computed, onMounted, ref } from "vue";
|
|
|
|
import PageHeader from "@/base/PageHeader.vue";
|
|
import { Button } from "@/components/ui/button";
|
|
import { DataTable, type DataTableColumn } from "@/components/ui/data-table";
|
|
import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle } from "@/components/ui/alert-dialog";
|
|
import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog";
|
|
import { DropdownMenuItem } from "@/components/ui/dropdown-menu";
|
|
import { Empty, EmptyHeader, EmptyTitle } from "@/components/ui/empty";
|
|
import { Input } from "@/components/ui/input";
|
|
import { MoreMenu } from "@/components/ui/more-menu";
|
|
import { ReadError } from "@/components/ui/read-error";
|
|
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
|
|
import { SettingRow } from "@/components/ui/setting-row";
|
|
import { Skeleton } from "@/components/ui/skeleton";
|
|
import { errors } from "@/copy/errors";
|
|
import { api, describeError, type AgentModel } from "@/lib/api";
|
|
import { useRead } from "@/lib/use-read";
|
|
|
|
/** The models agent loops use, by alias. "default" is the one a workflow gets
|
|
* when it names none. */
|
|
const { data, error, reload } = useRead(() => api.agentModels());
|
|
const credentials = useRead(() => api.credentials());
|
|
onMounted(() => {
|
|
void reload();
|
|
void credentials.reload();
|
|
});
|
|
|
|
interface Row extends AgentModel { alias: string }
|
|
const rows = computed<Row[]>(() => Object.entries(data.value ?? {}).map(([alias, m]) => ({ alias, ...m })));
|
|
const columns: DataTableColumn<Row>[] = [
|
|
{ id: "alias", label: "Alias", fixed: true, sort: (r) => r.alias },
|
|
{ id: "model", label: "Model", fixed: true, sort: (r) => r.model },
|
|
{ id: "class", label: "Provider", sort: (r) => r.class },
|
|
{ id: "base", label: "Base URL", hidden: true },
|
|
{ id: "key", label: "API key" },
|
|
];
|
|
|
|
const NONE = "__none__";
|
|
const editing = ref(false);
|
|
const isNew = ref(true);
|
|
const form = ref({ alias: "", class: "", model: "", base_url: "", api_key_credential: NONE });
|
|
function open(row?: Row): void {
|
|
isNew.value = !row;
|
|
actionError.value = "";
|
|
tested.value = "";
|
|
form.value = {
|
|
alias: row?.alias ?? "",
|
|
class: row?.class ?? "",
|
|
model: row?.model ?? "",
|
|
base_url: row?.base_url ?? "",
|
|
api_key_credential: row?.api_key_credential || NONE,
|
|
};
|
|
editing.value = true;
|
|
}
|
|
const model = (): AgentModel => ({
|
|
class: form.value.class.trim(),
|
|
model: form.value.model.trim(),
|
|
base_url: form.value.base_url.trim() || undefined,
|
|
api_key_credential: form.value.api_key_credential === NONE ? undefined : form.value.api_key_credential,
|
|
});
|
|
|
|
const busy = ref(false);
|
|
const actionError = ref("");
|
|
const tested = ref("");
|
|
async function test(): Promise<void> {
|
|
busy.value = true;
|
|
actionError.value = "";
|
|
tested.value = "";
|
|
try {
|
|
const answer = await api.testAgentModel(model());
|
|
tested.value = `${answer.model} answered “${answer.answer.trim()}”`;
|
|
} catch (cause) {
|
|
actionError.value = describeError(cause, errors.common.unknown);
|
|
} finally {
|
|
busy.value = false;
|
|
}
|
|
}
|
|
async function save(): Promise<void> {
|
|
busy.value = true;
|
|
actionError.value = "";
|
|
try {
|
|
const alias = form.value.alias.trim();
|
|
await api.putAgentModel(alias === "default" ? "" : alias, model());
|
|
editing.value = false;
|
|
await reload();
|
|
} catch (cause) {
|
|
actionError.value = describeError(cause, errors.common.unknown);
|
|
} finally {
|
|
busy.value = false;
|
|
}
|
|
}
|
|
const pending = ref<Row | null>(null);
|
|
const removeError = ref("");
|
|
async function remove(row: Row): Promise<void> {
|
|
busy.value = true;
|
|
removeError.value = "";
|
|
try {
|
|
await api.deleteAgentModel(row.alias === "default" ? "" : row.alias);
|
|
await reload();
|
|
} catch (cause) {
|
|
removeError.value = describeError(cause, errors.common.unknown);
|
|
} finally {
|
|
busy.value = false;
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<div>
|
|
<PageHeader title="Models">
|
|
<Button size="sm" aria-label="Add model" title="Add model" @click="open()"><Plus /></Button>
|
|
</PageHeader>
|
|
<ReadError v-if="error" :message="error" retry-label="Retry" @retry="reload" />
|
|
<div v-else-if="!data" role="status" aria-label="Loading models" class="space-y-2"><Skeleton class="h-8 w-full" /></div>
|
|
<DataTable v-else :columns="columns" :rows="rows" :row-key="(r) => r.alias" storage-key="agent-models">
|
|
<template #cell-alias="{ row }"><span class="font-medium">{{ row.alias }}</span></template>
|
|
<template #cell-model="{ row }"><span class="font-mono text-caption">{{ row.model }}</span></template>
|
|
<template #cell-class="{ row }">{{ row.class }}</template>
|
|
<template #cell-base="{ row }"><span class="font-mono text-caption">{{ row.base_url }}</span></template>
|
|
<template #cell-key="{ row }">{{ row.api_key_credential }}</template>
|
|
<template #actions="{ row }">
|
|
<MoreMenu :label="`${row.alias} actions`"><DropdownMenuItem @select="open(row)">Edit</DropdownMenuItem><DropdownMenuItem class="text-danger" @select="pending = row">Delete</DropdownMenuItem></MoreMenu>
|
|
</template>
|
|
<template #empty><Empty><EmptyHeader><EmptyTitle>No models</EmptyTitle></EmptyHeader></Empty></template>
|
|
</DataTable>
|
|
|
|
<p v-if="removeError" role="alert" class="pt-2 text-caption text-danger">{{ removeError }}</p>
|
|
|
|
<AlertDialog :open="pending !== null" @update:open="(v: boolean) => { if (!v) pending = null; }">
|
|
<AlertDialogContent>
|
|
<AlertDialogHeader>
|
|
<AlertDialogTitle>Delete model?</AlertDialogTitle>
|
|
<AlertDialogDescription>Delete the alias "{{ pending?.alias }}"? Workflows that name it will fail.</AlertDialogDescription>
|
|
</AlertDialogHeader>
|
|
<AlertDialogFooter>
|
|
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
|
<AlertDialogAction @click="if (pending) { const row = pending; pending = null; void remove(row); }">Delete model</AlertDialogAction>
|
|
</AlertDialogFooter>
|
|
</AlertDialogContent>
|
|
</AlertDialog>
|
|
|
|
<Dialog v-model:open="editing">
|
|
<DialogContent class="sm:max-w-2xl">
|
|
<DialogHeader><DialogTitle>{{ isNew ? "Add model" : `Edit ${form.alias}` }}</DialogTitle></DialogHeader>
|
|
<form id="model-form" @submit.prevent="save">
|
|
<SettingRow v-if="isNew" label="Alias" for="model-alias" hint="Workflows name it; default is used when they name none."><Input id="model-alias" v-model="form.alias" required /></SettingRow>
|
|
<SettingRow label="Provider" for="model-class"><Input id="model-class" v-model="form.class" placeholder="openai" required /></SettingRow>
|
|
<SettingRow label="Model" for="model-name"><Input id="model-name" v-model="form.model" required /></SettingRow>
|
|
<SettingRow label="Base URL" for="model-base" hint="Empty uses the provider's own."><Input id="model-base" v-model="form.base_url" type="url" /></SettingRow>
|
|
<SettingRow label="API key" for="model-key" hint="A bearer credential.">
|
|
<Select v-model="form.api_key_credential">
|
|
<SelectTrigger id="model-key" class="w-full"><SelectValue /></SelectTrigger>
|
|
<SelectContent>
|
|
<SelectItem :value="NONE">None</SelectItem>
|
|
<SelectItem v-for="c in (credentials.data.value ?? []).filter((c) => c.kind === 'bearer')" :key="c.name" :value="c.name">{{ c.name }}</SelectItem>
|
|
</SelectContent>
|
|
</Select>
|
|
</SettingRow>
|
|
<p v-if="tested" role="status" class="pt-2 text-caption text-ok">{{ tested }}</p>
|
|
<p v-if="actionError" role="alert" class="pt-2 text-caption text-danger">{{ actionError }}</p>
|
|
</form>
|
|
<DialogFooter>
|
|
<Button variant="outline" :disabled="busy || !form.class || !form.model" @click="test">Test</Button>
|
|
<Button type="submit" form="model-form" :disabled="busy">Save</Button>
|
|
</DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
</div>
|
|
</template>
|