feat(webui): inspector shows proposed and unidentified events, names and pastes event types

This commit is contained in:
sam committed 2026-09-24 14:21:21 +10:00
1 parent c1e2a14390
commit 1c03c6bc1a
17 files changed
+945 -20

No files matched your search

+10 -4
View File
@@ -5,6 +5,8 @@ import { computed } from "vue";
import { Badge } from "@/components/ui/badge";
import { TableCell, TableRow } from "@/components/ui/table";
import { ago } from "@/lib/format";
import { eventTypes } from "./event-type-state";
import { captureIdentity } from "./event-types";
import { selected, type Capture } from "./state";
/**
@@ -15,6 +17,8 @@ import { selected, type Capture } from "./state";
*/
const props = defineProps<{ capture: Capture }>();
const identity = computed(() => captureIdentity(props.capture, eventTypes.value));
const isSelected = computed(() => selected.value?.id === props.capture.id);
// The name is set here because role="button" makes the cells presentational:
@@ -40,10 +44,12 @@ function open(): void {
>
<TableCell class="font-mono">{{ props.capture.source || "(unknown)" }}</TableCell>
<TableCell :title="props.capture.received_at || ''">{{ ago(props.capture.received_at) }}</TableCell>
<!-- A stored capture carries no claim from a binding, so this reads Unbound
for every row: the column is where a claim would appear, not a state a
reader can act on today. -->
<TableCell><Badge variant="idle">Unbound</Badge></TableCell>
<!-- An unidentified capture is never dispatched. -->
<TableCell>
<Badge :variant="identity.identified ? 'ok' : 'warn'" class="max-w-40">
<span class="truncate">{{ identity.label }}</span>
</Badge>
</TableCell>
<TableCell class="font-mono">{{ props.capture.content_type || "—" }}</TableCell>
<TableCell class="text-right">
<ChevronRight class="size-4 text-fg-subtle" />
+1 -1
View File
@@ -53,7 +53,7 @@ const webhookHint = "Point a webhook at /webhooks/capture/<source> and it will s
<TableRow>
<TableHead>Source</TableHead>
<TableHead>Received</TableHead>
<TableHead>Binding</TableHead>
<TableHead>Event type</TableHead>
<TableHead>Content type</TableHead>
<TableHead class="w-10"><span class="sr-only">Payload</span></TableHead>
</TableRow>
+6 -1
View File
@@ -6,6 +6,8 @@ import { useRoute, useRouter } from "vue-router";
import CaptureDetail from "./CaptureDetail.vue";
import CaptureStatus from "./CaptureStatus.vue";
import CapturesCard from "./CapturesCard.vue";
import EventTypesCard from "./EventTypesCard.vue";
import { loadEventTypes } from "./event-type-state";
import { load, selectById, selectNewest, selected, useCaptures } from "./state";
/**
@@ -66,7 +68,7 @@ onMounted(async () => {
}
window.addEventListener("resize", measure);
await load();
await Promise.all([load(), loadEventTypes()]);
// The URL names a selection before the window is read, so the restore waits
// for the list; an id it no longer holds falls back to the newest capture,
// which is what a page opened with nothing named starts on too.
@@ -100,4 +102,7 @@ watch(selected, (capture) => {
</div>
<CaptureDetail :max-height="paneCap" />
</div>
<div class="mt-4">
<EventTypesCard />
</div>
</template>
+79
View File
@@ -0,0 +1,79 @@
<script setup lang="ts">
import { computed } from "vue";
import { Alert, AlertDescription } from "@/components/ui/alert";
import { Button } from "@/components/ui/button";
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Spinner } from "@/components/ui/spinner";
import { Textarea } from "@/components/ui/textarea";
import RuleEditor from "./RuleEditor.vue";
import { closeDraft, draft, saveDraft, saveError, saving } from "./event-type-state";
/**
* One event-type editing session: naming a proposed group, creating a type
* from a pasted example, or editing a type's name and rule. A refusal (an
* overlapping rule, a taken name) is shown beside the Save button.
*/
const title = computed(() => {
switch (draft.value?.mode) {
case "proposal":
return "Name event type";
case "paste":
return "Event type from payload";
default:
return "Edit event type";
}
});
function onOpenChange(open: boolean) {
if (!open) closeDraft();
}
</script>
<template>
<Dialog :open="draft !== null" @update:open="onOpenChange">
<DialogContent v-if="draft" class="max-h-[85vh] overflow-y-auto sm:max-w-2xl">
<DialogHeader>
<DialogTitle>{{ title }}</DialogTitle>
<DialogDescription>Events on {{ draft.source || "the source" }} that match no type are never dispatched.</DialogDescription>
</DialogHeader>
<div class="flex flex-col gap-4">
<div class="flex flex-col gap-2">
<Label for="event-type-source">Source</Label>
<Input id="event-type-source" v-model="draft.source" class="font-mono" :disabled="draft.mode !== 'paste'" />
</div>
<div class="flex flex-col gap-2">
<Label for="event-type-name">Name</Label>
<Input id="event-type-name" v-model="draft.name" placeholder="pull_request.opened" />
</div>
<template v-if="draft.mode === 'paste'">
<div class="flex flex-col gap-2">
<Label for="event-type-headers">Headers</Label>
<Textarea id="event-type-headers" v-model="draft.headersText" class="min-h-20 font-mono text-xs" placeholder="X-GitHub-Event: pull_request" />
</div>
<div class="flex flex-col gap-2">
<Label for="event-type-body">Payload</Label>
<Textarea id="event-type-body" v-model="draft.body" class="min-h-40 font-mono text-xs" placeholder="{ }" />
</div>
</template>
<RuleEditor v-else v-model="draft.rule" />
</div>
<Alert v-if="saveError" variant="destructive">
<AlertDescription>{{ saveError }}</AlertDescription>
</Alert>
<DialogFooter>
<Button variant="outline" @click="closeDraft">Cancel</Button>
<Button :disabled="saving || !draft.name.trim() || !draft.source.trim()" @click="saveDraft">
<Spinner v-if="saving" />
Save
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</template>
+120
View File
@@ -0,0 +1,120 @@
<script setup lang="ts">
import { ClipboardPaste, Pencil, Tag, Trash2, TriangleAlert } from "@lucide/vue";
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from "@/components/ui/alert-dialog";
import { Button } from "@/components/ui/button";
import { Card, CardAction, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
import EventTypeDialog from "./EventTypeDialog.vue";
import {
confirmDelete,
editEventType,
eventTypes,
eventTypesEnabled,
eventTypesError,
nameProposal,
pasteExample,
pendingDelete,
proposals,
} from "./event-type-state";
import { ruleSummary } from "./event-types";
/**
* Proposed event types (groups of unidentified captures sharing a structure)
* and the named ones. Naming a proposal or pasting an example payload creates
* a type; a later event matching it is identified as it on arrival.
*/
function fieldCount(schema: Record<string, string> | null | undefined): number {
return schema ? Object.keys(schema).length : 0;
}
</script>
<template>
<Card v-if="eventTypesEnabled">
<CardHeader>
<CardTitle>Event types</CardTitle>
<CardAction>
<Button variant="outline" size="sm" @click="pasteExample()"><ClipboardPaste /> From payload</Button>
</CardAction>
</CardHeader>
<CardContent class="flex flex-col gap-6">
<Alert v-if="eventTypesError" variant="destructive">
<TriangleAlert />
<AlertTitle>Event types</AlertTitle>
<AlertDescription>{{ eventTypesError }}</AlertDescription>
</Alert>
<Table v-if="proposals.length">
<TableHeader>
<TableRow>
<TableHead>Proposed · source</TableHead>
<TableHead class="w-20 text-right">Events</TableHead>
<TableHead class="w-20 text-right">Fields</TableHead>
<TableHead>Rule</TableHead>
<TableHead class="w-24"><span class="sr-only">Name</span></TableHead>
</TableRow>
</TableHeader>
<TableBody>
<TableRow v-for="p in proposals" :key="p.source + p.key">
<TableCell class="font-mono">{{ p.source || "(unknown)" }}</TableCell>
<TableCell class="text-right tabular-nums">{{ p.count }}</TableCell>
<TableCell class="text-right tabular-nums">{{ fieldCount(p.schema) }}</TableCell>
<TableCell class="font-mono text-xs"><span class="block max-w-96 truncate" :title="ruleSummary(p.rule)">{{ ruleSummary(p.rule) }}</span></TableCell>
<TableCell class="text-right">
<Button variant="outline" size="sm" @click="nameProposal(p)"><Tag /> Name</Button>
</TableCell>
</TableRow>
</TableBody>
</Table>
<Table v-if="eventTypes.length">
<TableHeader>
<TableRow>
<TableHead>Source</TableHead>
<TableHead>Name</TableHead>
<TableHead>Rule</TableHead>
<TableHead class="w-20"><span class="sr-only">Actions</span></TableHead>
</TableRow>
</TableHeader>
<TableBody>
<TableRow v-for="t in eventTypes" :key="t.id">
<TableCell class="font-mono">{{ t.source }}</TableCell>
<TableCell>{{ t.name }}</TableCell>
<TableCell class="font-mono text-xs"><span class="block max-w-96 truncate" :title="ruleSummary(t.rule)">{{ ruleSummary(t.rule) }}</span></TableCell>
<TableCell class="text-right whitespace-nowrap">
<Button variant="ghost" size="icon-sm" :aria-label="`Edit ${t.name}`" @click="editEventType(t)"><Pencil /></Button>
<Button variant="ghost" size="icon-sm" :aria-label="`Delete ${t.name}`" @click="pendingDelete = t"><Trash2 /></Button>
</TableCell>
</TableRow>
</TableBody>
</Table>
<p v-if="!proposals.length && !eventTypes.length" class="text-sm text-fg-muted">No event types or proposals.</p>
</CardContent>
</Card>
<EventTypeDialog />
<AlertDialog :open="pendingDelete !== null" @update:open="(open) => { if (!open) pendingDelete = null; }">
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Delete this event type?</AlertDialogTitle>
<AlertDialogDescription>
Events matching "{{ pendingDelete?.name }}" will arrive unidentified and will not dispatch.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction variant="destructive" @click="confirmDelete">Delete</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</template>
+72
View File
@@ -0,0 +1,72 @@
<script setup lang="ts">
import { Plus, X } from "@lucide/vue";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import type { PayloadCondition, Rule } from "./event-types";
/**
* A match rule's conditions, edited in place: header equalities, then payload
* conditions. Every condition must hold for an event to match.
*/
const rule = defineModel<Rule>({ required: true });
function headers() {
return (rule.value.headers ||= []);
}
function payload() {
return (rule.value.payload ||= []);
}
function asOp(value: unknown): PayloadCondition["op"] {
return value === "present" ? "present" : "equals";
}
</script>
<template>
<div class="flex flex-col gap-4">
<div class="flex flex-col gap-2">
<Label>Headers</Label>
<div v-for="(h, i) in rule.headers || []" :key="`h${i}`" class="flex items-center gap-2">
<Input v-model="h.name" class="font-mono" aria-label="Header name" placeholder="X-GitHub-Event" />
<span class="text-fg-subtle">=</span>
<Input v-model="h.value" class="font-mono" aria-label="Header value" />
<Button variant="ghost" size="icon-sm" aria-label="Remove header condition" @click="headers().splice(i, 1)">
<X />
</Button>
</div>
<Button variant="outline" size="sm" class="w-fit" @click="headers().push({ name: '', value: '' })">
<Plus /> Header
</Button>
</div>
<div class="flex flex-col gap-2">
<Label>Payload</Label>
<div v-for="(p, i) in rule.payload || []" :key="`p${i}`" class="flex items-center gap-2">
<Input v-model="p.path" class="font-mono" aria-label="Payload path" placeholder="action" />
<Select :model-value="p.op" @update:model-value="(value) => (p.op = asOp(value))">
<SelectTrigger size="sm" class="w-28 shrink-0" aria-label="Condition">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectGroup>
<SelectItem value="equals">equals</SelectItem>
<SelectItem value="present">present</SelectItem>
</SelectGroup>
</SelectContent>
</Select>
<Input v-if="p.op === 'equals'" v-model="p.value" class="font-mono" aria-label="Payload value" />
<span v-else class="w-full" />
<Button variant="ghost" size="icon-sm" aria-label="Remove payload condition" @click="payload().splice(i, 1)">
<X />
</Button>
</div>
<Button variant="outline" size="sm" class="w-fit" @click="payload().push({ path: '', op: 'equals', value: '' })">
<Plus /> Payload condition
</Button>
</div>
</div>
</template>
+131
View File
@@ -0,0 +1,131 @@
import { ref } from "vue";
import { api } from "@/lib/api";
import { captures } from "./state";
import { cleanRule, parseHeaderLines, type EventType, type Proposal, type Rule } from "./event-types";
/**
* The inspector's event types: the named ones, and the groups of unidentified
* captures proposed as types. The server groups and infers; this module holds
* the read and one editing session at a time.
*/
interface EventTypesResponse {
enabled?: boolean;
event_types?: EventType[];
proposals?: Proposal[];
}
export const eventTypes = ref<EventType[]>([]);
export const proposals = ref<Proposal[]>([]);
export const eventTypesEnabled = ref(true);
export const eventTypesError = ref<string | null>(null);
/** The editor's modes: naming a proposal, pasting an example, editing a type. */
export type DraftMode = "proposal" | "paste" | "edit";
export interface Draft {
mode: DraftMode;
id: string | null;
source: string;
name: string;
rule: Rule;
headersText: string;
body: string;
exampleHeaders: Record<string, string>;
}
export const draft = ref<Draft | null>(null);
export const saving = ref(false);
export const saveError = ref<string | null>(null);
export async function loadEventTypes(): Promise<void> {
try {
const res = await api.eventTypes<EventTypesResponse>();
eventTypesEnabled.value = res.enabled !== false;
eventTypes.value = res.event_types || [];
proposals.value = res.proposals || [];
eventTypesError.value = null;
} catch (err) {
eventTypesError.value = (err as Error).message || String(err);
}
}
function copyRule(rule: Rule): Rule {
return {
headers: (rule.headers || []).map((h) => ({ ...h })),
payload: (rule.payload || []).map((p) => ({ ...p })),
};
}
function open(next: Draft): void {
saveError.value = null;
draft.value = next;
}
export function nameProposal(p: Proposal): void {
const sample = captures.value.find((c) => String(c.id) === p.sample_id);
open({
mode: "proposal",
id: null,
source: p.source,
name: "",
rule: copyRule(p.rule),
headersText: "",
body: sample?.body || "",
exampleHeaders: p.headers || {},
});
}
export function pasteExample(source = ""): void {
open({ mode: "paste", id: null, source, name: "", rule: { headers: [], payload: [] }, headersText: "", body: "", exampleHeaders: {} });
}
export function editEventType(t: EventType): void {
open({ mode: "edit", id: t.id, source: t.source, name: t.name, rule: copyRule(t.rule), headersText: "", body: "", exampleHeaders: {} });
}
export function closeDraft(): void {
draft.value = null;
}
export async function saveDraft(): Promise<void> {
const d = draft.value;
if (!d) return;
saving.value = true;
try {
if (d.mode === "edit" && d.id) {
await api.eventTypeUpdate(d.id, { name: d.name, rule: cleanRule(d.rule) });
} else {
const headers = d.mode === "paste" ? parseHeaderLines(d.headersText) : d.exampleHeaders;
await api.eventTypeCreate({
source: d.source,
name: d.name,
// A pasted example's rule is inferred by the server from what was pasted.
rule: d.mode === "paste" ? undefined : cleanRule(d.rule),
example: { headers, body: d.body },
});
}
draft.value = null;
await loadEventTypes();
} catch (err) {
saveError.value = (err as Error).message || String(err);
} finally {
saving.value = false;
}
}
/** The type awaiting a delete confirmation: its events become unidentified. */
export const pendingDelete = ref<EventType | null>(null);
export async function confirmDelete(): Promise<void> {
const t = pendingDelete.value;
pendingDelete.value = null;
if (!t) return;
try {
await api.eventTypeDelete(t.id);
await loadEventTypes();
} catch (err) {
eventTypesError.value = (err as Error).message || String(err);
}
}
+79
View File
@@ -0,0 +1,79 @@
/**
* Event types as the inspector reads them (docs/prds/event-automation.md,
* "Event types"). Pure: no Vue, so the rules here are tested without a DOM.
*/
export interface HeaderCondition {
name: string;
value: string;
}
export interface PayloadCondition {
path: string;
op: "equals" | "present";
value?: string;
}
export interface Rule {
headers: HeaderCondition[] | null;
payload: PayloadCondition[] | null;
}
export interface EventType {
id: string;
source: string;
name: string;
rule: Rule;
schema?: Record<string, string> | null;
}
export interface Proposal {
source: string;
key: string;
schema: Record<string, string> | null;
headers: Record<string, string> | null;
count: number;
sample_id: string;
rule: Rule;
}
/** parseHeaderLines reads pasted "Name: value" lines; anything else is skipped. */
export function parseHeaderLines(text: string): Record<string, string> {
const out: Record<string, string> = {};
for (const line of text.split("\n")) {
const at = line.indexOf(":");
if (at <= 0) continue;
const name = line.slice(0, at).trim();
if (name) out[name] = line.slice(at + 1).trim();
}
return out;
}
/** captureIdentity names what a capture was identified as when it arrived. */
export function captureIdentity(
capture: { id: unknown; event_type?: string },
types: EventType[],
): { label: string; identified: boolean } {
if (!capture.event_type) return { label: "Unidentified", identified: false };
const found = types.find((t) => t.id === capture.event_type);
return { label: found ? found.name : "Deleted type", identified: true };
}
/** ruleSummary renders a rule as one line of its conditions. */
export function ruleSummary(rule: Rule): string {
const parts = [
...(rule.headers || []).map((h) => `${h.name} = ${h.value}`),
...(rule.payload || []).map((p) => (p.op === "present" ? `${p.path} present` : `${p.path} = ${p.value ?? ""}`)),
];
return parts.length ? parts.join(" · ") : "any event";
}
/** cleanRule drops the editor's blank rows, and a present condition's value. */
export function cleanRule(rule: Rule): Rule {
return {
headers: (rule.headers || []).filter((h) => h.name.trim() !== "").map((h) => ({ name: h.name.trim(), value: h.value })),
payload: (rule.payload || [])
.filter((p) => p.path.trim() !== "")
.map((p) => (p.op === "present" ? { path: p.path.trim(), op: p.op } : { path: p.path.trim(), op: p.op, value: p.value ?? "" })),
};
}
+7 -1
View File
@@ -2,6 +2,7 @@ import { ref } from "vue";
import { api } from "@/lib/api";
import { useLiveResource } from "@/stores/live-updates";
import { loadEventTypes } from "./event-type-state";
/**
* The event inspector's shared state.
@@ -25,6 +26,8 @@ export interface Capture {
headers?: string;
body?: string;
authenticated?: boolean;
/** The event type it was identified as on arrival; empty is unidentified. */
event_type?: string;
}
interface CapturesResponse {
@@ -92,5 +95,8 @@ export function selectNewest(): void {
* The initial read belongs to the page, which has to await it before it can
* restore a selection from the address bar. */
export function useCaptures(): void {
useLiveResource("captures", () => void load());
useLiveResource("captures", () => {
void load();
void loadEventTypes();
});
}
+5
View File
@@ -157,6 +157,11 @@ export const api = {
updateInstall: <T = unknown>(snapshot: unknown) =>
request<T>("/api/chat/update/install", { method: "POST", body: { snapshot }, timeoutMs: 15 * 60_000 }),
captures: <T = unknown>(limit?: number) => request<T>("/api/captures" + qs({ limit })),
eventTypes: <T = unknown>() => request<T>("/api/event-types"),
eventTypeCreate: <T = unknown>(eventType: Payload) => request<T>("/api/event-types", { method: "POST", body: eventType }),
eventTypeUpdate: <T = unknown>(id: string, eventType: Payload) =>
request<T>(`/api/event-types/${encodeURIComponent(id)}`, { method: "PUT", body: eventType }),
eventTypeDelete: (id: string) => request<void>(`/api/event-types/${encodeURIComponent(id)}`, { method: "DELETE", parse: false }),
mappings: <T = unknown>() => request<T>("/api/mappings"),
mappingCreate: <T = unknown>(mapping: Payload) => request<T>("/api/mappings", { method: "POST", body: mapping }),
mappingUpdate: <T = unknown>(id: string, mapping: Payload) =>