Files
archie/ui/src/org/OrgAgentsPage.vue
T

213 lines
7.7 KiB
Vue

<script setup lang="ts">
import PageHeader from "@/base/PageHeader.vue";
import { storeToRefs } from "pinia";
import { onMounted, ref, watch } from "vue";
import { Bot, KeyRound, Plus, ShieldCheck, User } from "@lucide/vue";
import IdentityGrantsCard from "@/settings/IdentityGrantsCard.vue";
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from "@/components/ui/alert-dialog";
import { Button } from "@/components/ui/button";
import { Empty, EmptyHeader, EmptyTitle } from "@/components/ui/empty";
import { Input } from "@/components/ui/input";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { Badge } from "@/components/ui/badge";
import { Section } from "@/components/ui/section";
import { api } from "@/lib/api";
import { useOrgStore, type Identity } from "@/stores/org";
// Agent assignment is independent of membership roles.
const store = useOrgStore();
const { org } = storeToRefs(store);
store.watchIdentities();
const serves = ref(new Set<string>());
const error = ref("");
const busy = ref("");
async function load(): Promise<void> {
const id = org.value?.id;
serves.value = new Set();
if (!id) return;
try {
const response = await api.orgAgents<{ agents: Identity[] }>(id);
if (org.value?.id !== id) return;
serves.value = new Set(response.agents.map((agent) => agent.id));
error.value = "";
} catch (cause) {
if (org.value?.id === id) error.value = String((cause as Error).message || cause);
}
}
onMounted(async () => {
try {
await store.loadOrg();
} catch (cause) {
error.value = String((cause as Error).message || cause);
return;
}
await load();
});
watch(() => org.value?.id, () => void load());
const name = ref("");
const kind = ref<Identity["kind"]>("bot");
async function create(): Promise<void> {
if (await store.createIdentity(name.value.trim(), kind.value)) name.value = "";
}
const renaming = ref<string | null>(null);
const draftName = ref("");
function startRename(value: Identity) {
renaming.value = value.id;
draftName.value = value.display_name;
}
async function finishRename(value: Identity) {
// Enter and the blur that follows it both land here; only the first commits.
if (renaming.value !== value.id) return;
const next = draftName.value.trim();
renaming.value = null;
if (next && next !== value.display_name) await store.identityCommand(value, "rename", next);
}
async function assign(value: Identity): Promise<void> {
if (!org.value) return;
busy.value = value.id;
try {
await api.assignOrgAgent(org.value.id, value.id);
serves.value = new Set([...serves.value, value.id]);
error.value = "";
} catch (cause) {
error.value = String((cause as Error).message || cause);
} finally {
busy.value = "";
}
}
const retiring = ref<Identity | null>(null);
async function retire() {
if (retiring.value) await store.identityCommand(retiring.value, "retire");
retiring.value = null;
}
const icons = { system: ShieldCheck, bot: Bot, service_account: KeyRound, user: User };
const lifecycle = { active: "idle", suspended: "warn", retired: "idle" } as const;
/** A bot or service account serves an org; people join one as members. */
const isAgent = (value: Identity) =>
value.kind === "bot" || value.kind === "service_account";
</script>
<template>
<div>
<PageHeader title="Agents" />
<p v-if="store.identityError || error" role="alert" class="mb-4 text-sm text-danger">
{{ store.identityError || error }}
</p>
<form class="mb-4 flex flex-wrap items-center gap-2" @submit.prevent="create">
<Input v-model="name" class="max-w-64" placeholder="Display name" aria-label="Display name" required />
<Select v-model="kind">
<SelectTrigger class="w-44" aria-label="Kind"><SelectValue /></SelectTrigger>
<SelectContent>
<SelectItem value="bot">Bot</SelectItem>
<SelectItem value="service_account">Service account</SelectItem>
<SelectItem value="user">User</SelectItem>
</SelectContent>
</Select>
<Button type="submit" size="sm" :disabled="!name.trim() || !!store.identityBusy"><Plus data-icon="inline-start" />Create</Button>
</form>
<Section>
<ul class="divide-y divide-border" aria-label="Agents">
<li v-for="value in store.identities" :key="value.id" class="flex flex-wrap items-center gap-3 py-3 first:pt-0 last:pb-0">
<span class="grid size-8 shrink-0 place-items-center rounded-md bg-secondary text-fg-muted">
<component :is="icons[value.kind]" class="size-4" aria-hidden="true" />
</span>
<div class="min-w-0 flex-1">
<Input
v-if="renaming === value.id"
v-model="draftName"
class="max-w-64"
aria-label="Display name"
autofocus
@keydown.enter="finishRename(value)"
@keydown.esc="renaming = null"
@blur="finishRename(value)"
/>
<p v-else class="truncate text-sm font-medium" :class="value.lifecycle === 'retired' && 'text-fg-subtle'">
{{ value.display_name }}
</p>
<p class="text-xs text-fg-subtle">{{ value.kind.replace("_", " ") }}</p>
</div>
<Badge v-if="isAgent(value) && serves.has(value.id)" variant="outline" class="font-mono">{{ org?.id }}</Badge>
<Badge v-if="value.lifecycle !== 'active'" :variant="lifecycle[value.lifecycle]" class="capitalize">{{ value.lifecycle }}</Badge>
<div v-if="value.kind !== 'system' && value.lifecycle !== 'retired'" class="flex flex-wrap gap-1">
<Button
v-if="isAgent(value) && !serves.has(value.id)"
variant="ghost"
size="sm"
:title="`Assign to ${org?.name}`"
:disabled="busy === value.id"
@click="assign(value)"
>Assign</Button
>
<Button variant="ghost" size="sm" :disabled="!!store.identityBusy" @click="startRename(value)">Rename</Button>
<Button
v-if="value.lifecycle === 'active'"
variant="ghost"
size="sm"
:disabled="!!store.identityBusy"
@click="store.identityCommand(value, 'suspend')"
>Suspend</Button
>
<Button v-else variant="ghost" size="sm" :disabled="!!store.identityBusy" @click="store.identityCommand(value, 'reactivate')"
>Reactivate</Button
>
<Button variant="ghost" size="sm" class="text-danger" :disabled="!!store.identityBusy" @click="retiring = value">Retire</Button>
</div>
</li>
<li v-if="!store.identitiesLoaded" class="py-3 text-sm text-fg-muted" role="status">Loading agents…</li>
<li v-else-if="!store.identities.length && !store.identityError">
<Empty class="py-6">
<EmptyHeader>
<EmptyTitle>No identities yet</EmptyTitle>
</EmptyHeader>
</Empty>
</li>
</ul>
</Section>
<IdentityGrantsCard />
<AlertDialog :open="!!retiring" @update:open="(open) => !open && (retiring = null)">
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Retire {{ retiring?.display_name }}?</AlertDialogTitle>
<AlertDialogDescription>This cannot be undone.</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction class="bg-destructive text-destructive-foreground" @click="retire">Retire</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</div>
</template>