193 lines
10 KiB
Vue
193 lines
10 KiB
Vue
<script setup lang="ts">
|
|
import { computed, onMounted, ref, watch } from "vue";
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog";
|
|
import { Input } from "@/components/ui/input";
|
|
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
|
|
import { SettingRow } from "@/components/ui/setting-row";
|
|
import { Switch } from "@/components/ui/switch";
|
|
import { Textarea } from "@/components/ui/textarea";
|
|
import { errors } from "@/copy/errors";
|
|
import { api, describeError, type Extension, type ExtensionScope, type InstallationInput, type InstallationUpdate, type PackageView, type SettingField } from "@/lib/api";
|
|
import { fromLines, toLines } from "@/lib/lines";
|
|
import { useRead } from "@/lib/use-read";
|
|
|
|
/** One installation's configuration. A new installation names the accepted
|
|
* package and the scope that owns it; an existing one changes only its
|
|
* settings, grants and state. */
|
|
const props = defineProps<{
|
|
extension: Extension | null;
|
|
packages: PackageView[];
|
|
scopes: ExtensionScope[];
|
|
}>();
|
|
const open = defineModel<boolean>("open", { required: true });
|
|
const emit = defineEmits<{ saved: [] }>();
|
|
|
|
const credentials = useRead(() => api.credentials());
|
|
onMounted(credentials.reload);
|
|
|
|
const creating = computed(() => !props.extension);
|
|
const schema = computed<SettingField[]>(() => props.extension?.settings_schema ?? []);
|
|
const selected = computed(() => props.packages.find((p) => p.name === packageName.value));
|
|
const contract = computed(() => props.extension?.contract ?? selected.value?.contract ?? "");
|
|
const isMCP = computed(() => contract.value === "mcp");
|
|
const scope = ref<ExtensionScope>("person");
|
|
const packageName = ref("");
|
|
const name = ref("");
|
|
const settings = ref<Record<string, string>>({});
|
|
const other = ref("");
|
|
const credentialSlots = ref("");
|
|
const egress = ref("");
|
|
const env = ref("");
|
|
const args = ref("");
|
|
const url = ref("");
|
|
const tools = ref("[]");
|
|
const enabled = ref(true);
|
|
// "" runs the package's default version; a version pins the installation.
|
|
const version = ref("");
|
|
const DEFAULT_VERSION = "default";
|
|
const accepted = computed(() => (selectedPackage.value?.versions ?? []).filter((v) => v.accepted));
|
|
const selectedPackage = computed(() => props.packages.find((p) => p.name === (props.extension?.package ?? packageName.value)));
|
|
const busy = ref(false);
|
|
const actionError = ref("");
|
|
|
|
/** The credentials this installation may name: the instance's, plus its own
|
|
* scope's. The server returns only the caller's organisation and person, so
|
|
* the scope is enough to tell them apart. */
|
|
const credentialNames = computed(() =>
|
|
(credentials.data.value ?? [])
|
|
.filter((c) => c.scope === "instance" || c.scope === scope.value)
|
|
.map((c) => c.name),
|
|
);
|
|
|
|
// A dialog's action closes it before its click handler runs, so the target
|
|
// outlives the open flag rather than being reset on close.
|
|
watch(open, (value) => {
|
|
if (value) reset();
|
|
});
|
|
|
|
function reset(): void {
|
|
const e = props.extension;
|
|
const known = new Set((e?.settings_schema ?? []).map((field) => field.key));
|
|
settings.value = Object.fromEntries(Object.entries(e?.settings ?? {}).filter(([key]) => known.has(key)));
|
|
other.value = toLines(Object.fromEntries(Object.entries(e?.settings ?? {}).filter(([key]) => !known.has(key))));
|
|
credentialSlots.value = toLines(e?.credentials ?? {});
|
|
egress.value = (e?.egress ?? []).join("\n");
|
|
env.value = (e?.env ?? []).join("\n");
|
|
args.value = (e?.args ?? []).join("\n");
|
|
url.value = e?.url ?? "";
|
|
tools.value = e?.tools ? JSON.stringify(e.tools, null, 2) : "[]";
|
|
enabled.value = e?.enabled ?? true;
|
|
version.value = e?.version_pinned ? (e.version ?? "") : "";
|
|
packageName.value = e?.package ?? installable.value[0]?.name ?? "";
|
|
scope.value = "person";
|
|
name.value = "";
|
|
actionError.value = "";
|
|
}
|
|
|
|
// A package can be installed once the instance accepted a version of it.
|
|
const installable = computed(() => props.packages.filter((p) => p.versions.some((v) => v.accepted)));
|
|
|
|
const list = (text: string) => text.split("\n").map((line) => line.trim()).filter(Boolean);
|
|
|
|
async function submit(): Promise<void> {
|
|
busy.value = true;
|
|
actionError.value = "";
|
|
try {
|
|
const body: InstallationUpdate = {
|
|
settings: { ...fromLines(other.value), ...Object.fromEntries(Object.entries(settings.value).filter(([, value]) => value !== "")) },
|
|
credentials: fromLines(credentialSlots.value),
|
|
egress: list(egress.value),
|
|
env: list(env.value),
|
|
enabled: enabled.value,
|
|
version: version.value || undefined,
|
|
};
|
|
if (isMCP.value) {
|
|
body.url = url.value.trim();
|
|
body.args = list(args.value);
|
|
body.tools = JSON.parse(tools.value || "[]");
|
|
}
|
|
if (props.extension) {
|
|
await api.updateInstallation(props.extension.id, body);
|
|
} else {
|
|
const create: InstallationInput = { package: packageName.value, scope: scope.value, ...body };
|
|
if (name.value.trim()) create.name = name.value.trim();
|
|
await api.createInstallation(create);
|
|
}
|
|
emit("saved");
|
|
open.value = false;
|
|
} catch (cause) {
|
|
actionError.value = cause instanceof SyntaxError ? `Tools: ${cause.message}` : describeError(cause, errors.common.unknown);
|
|
} finally {
|
|
busy.value = false;
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<Dialog v-model:open="open">
|
|
<DialogContent class="max-h-[90dvh] overflow-y-auto sm:max-w-2xl">
|
|
<DialogHeader><DialogTitle>{{ creating ? "New installation" : `Edit ${extension?.name}` }}</DialogTitle></DialogHeader>
|
|
<form id="installation-form" @submit.prevent="submit">
|
|
<SettingRow v-if="creating" label="Package" for="installation-package">
|
|
<Select v-model="packageName">
|
|
<SelectTrigger id="installation-package" class="w-full"><SelectValue placeholder="Accepted package" /></SelectTrigger>
|
|
<SelectContent>
|
|
<SelectItem v-for="pkg in installable" :key="pkg.name" :value="pkg.name">
|
|
{{ pkg.display_name || pkg.name }}<span v-if="pkg.display_name" class="text-fg-subtle"> ({{ pkg.name }})</span>
|
|
</SelectItem>
|
|
</SelectContent>
|
|
</Select>
|
|
</SettingRow>
|
|
<SettingRow v-else label="Package" for="installation-package-name">{{ extension?.package }}</SettingRow>
|
|
<SettingRow v-if="accepted.length > 1" label="Version" for="installation-version">
|
|
<Select :model-value="version || DEFAULT_VERSION" @update:model-value="(value) => (version = value === DEFAULT_VERSION ? '' : String(value))">
|
|
<SelectTrigger id="installation-version" class="w-full"><SelectValue /></SelectTrigger>
|
|
<SelectContent>
|
|
<SelectItem :value="DEFAULT_VERSION">Default ({{ selectedPackage?.default_version }})</SelectItem>
|
|
<SelectItem v-for="v in accepted" :key="v.version" :value="v.version">{{ v.version }}</SelectItem>
|
|
</SelectContent>
|
|
</Select>
|
|
</SettingRow>
|
|
<SettingRow v-if="creating" label="Scope" for="installation-scope" hint="Who owns the installation and its configuration.">
|
|
<Select v-model="scope">
|
|
<SelectTrigger id="installation-scope" class="w-full"><SelectValue /></SelectTrigger>
|
|
<SelectContent>
|
|
<SelectItem v-for="value in scopes" :key="value" :value="value">{{ value === "org" ? "Organisation" : value === "instance" ? "Instance" : "Person" }}</SelectItem>
|
|
</SelectContent>
|
|
</Select>
|
|
</SettingRow>
|
|
<SettingRow v-if="creating" label="Name" for="installation-name" hint="What a binding calls it. Default: the package's name.">
|
|
<Input id="installation-name" v-model="name" pattern="[a-z0-9][a-z0-9-]*" :placeholder="selected?.name" />
|
|
</SettingRow>
|
|
<template v-if="isMCP">
|
|
<SettingRow label="URL" for="installation-url" hint="For a server reached over HTTP instead of run from a path."><Input id="installation-url" v-model="url" type="url" /></SettingRow>
|
|
<SettingRow label="Arguments" for="installation-args" hint="One per line."><Textarea id="installation-args" v-model="args" class="font-mono" rows="2" /></SettingRow>
|
|
<SettingRow label="Tools" for="installation-tools" hint="The tools it may offer, as JSON: name, kind, read_only, reversibility, summary.">
|
|
<Textarea id="installation-tools" v-model="tools" class="font-mono" rows="5" />
|
|
</SettingRow>
|
|
</template>
|
|
<SettingRow v-for="field in schema" :key="field.key" :label="field.label || field.key" :for="`installation-set-${field.key}`" :hint="field.description">
|
|
<Select v-if="field.options?.length" v-model="settings[field.key]">
|
|
<SelectTrigger :id="`installation-set-${field.key}`" class="w-full"><SelectValue /></SelectTrigger>
|
|
<SelectContent><SelectItem v-for="option in field.options" :key="option" :value="option">{{ option }}</SelectItem></SelectContent>
|
|
</Select>
|
|
<Input v-else :id="`installation-set-${field.key}`" v-model="settings[field.key]" :required="field.required" :placeholder="field.secret ? 'engine:key' : undefined" />
|
|
</SettingRow>
|
|
<SettingRow :label="schema.length ? 'Other settings' : 'Settings'" for="installation-settings" hint="key=value, one per line. A key ending in _ref takes engine:key.">
|
|
<Textarea id="installation-settings" v-model="other" class="font-mono" rows="3" />
|
|
</SettingRow>
|
|
<SettingRow label="Credentials" for="installation-credentials" hint="slot=credential, one per line.">
|
|
<Textarea id="installation-credentials" v-model="credentialSlots" class="font-mono" rows="2" :placeholder="credentialNames.join('\n')" />
|
|
</SettingRow>
|
|
<SettingRow label="Egress" for="installation-egress" hint="Hosts it may reach, one per line."><Textarea id="installation-egress" v-model="egress" class="font-mono" rows="2" /></SettingRow>
|
|
<SettingRow label="Environment" for="installation-env" hint="Host variables it may see, one per line."><Textarea id="installation-env" v-model="env" class="font-mono" rows="2" /></SettingRow>
|
|
<SettingRow label="Run" for="installation-enabled" hint="Start it when it is saved."><Switch id="installation-enabled" v-model="enabled" /></SettingRow>
|
|
<p v-if="actionError" role="alert" class="pt-2 text-caption text-danger">{{ actionError }}</p>
|
|
</form>
|
|
<DialogFooter><Button type="submit" form="installation-form" :disabled="busy || (creating && !packageName)">{{ creating ? "Install" : "Save and restart" }}</Button></DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
</template>
|