feat(ui): prioritise recent activity and pending approvals
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
2 files changed
+178
-72
No files matched your search
+147
-58
@@ -1,15 +1,19 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onBeforeUnmount, onMounted, ref } from "vue";
|
||||
import { nextTick, onBeforeUnmount, onMounted, ref } from "vue";
|
||||
import { RouterLink } from "vue-router";
|
||||
|
||||
import { Empty, EmptyHeader, EmptyTitle } from "@/components/ui/empty";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { ReadError } from "@/components/ui/read-error";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { CardTitle } from "@/components/ui/card";
|
||||
import { Section } from "@/components/ui/section";
|
||||
import { Spinner } from "@/components/ui/spinner";
|
||||
import { Timestamp } from "@/components/ui/timestamp";
|
||||
import { answerApproval, composerStream, latestMessages, messagesAfter, type Message, Unauthorized } from "./api";
|
||||
import {
|
||||
answerApproval, composerStream, latestMessages, messagesAfter, pendingApprovals, type Message,
|
||||
type PendingApproval, Unauthorized,
|
||||
} from "./api";
|
||||
|
||||
/**
|
||||
* The stream sends sequence numbers, not message bodies. Fetch messages
|
||||
@@ -18,26 +22,39 @@ import { answerApproval, composerStream, latestMessages, messagesAfter, type Mes
|
||||
const emit = defineEmits<{ unauthorized: [] }>();
|
||||
|
||||
const messages = ref<Message[]>([]);
|
||||
const approvals = ref<PendingApproval[]>([]);
|
||||
const approvalReadFailed = ref(false);
|
||||
const loading = ref(true);
|
||||
const error = ref("");
|
||||
const answering = ref<string | null>(null);
|
||||
const activity = ref<HTMLElement | null>(null);
|
||||
const atLatest = ref(true);
|
||||
const controller = new AbortController();
|
||||
let approvalRead = 0;
|
||||
let following = false;
|
||||
|
||||
/** Requests already answered: the answer arrives as its own message. */
|
||||
const answered = computed(() => {
|
||||
const ids = new Set<string>();
|
||||
for (const m of messages.value) if (m.kind === "approval_answer" && m.request_id) ids.add(m.request_id);
|
||||
return ids;
|
||||
});
|
||||
|
||||
const wantsAnswer = (m: Message) => m.kind === "approval_request" && !!m.request_id && !answered.value.has(m.request_id);
|
||||
|
||||
async function answer(m: Message, approved: boolean) {
|
||||
if (!m.request_id) return;
|
||||
answering.value = m.request_id;
|
||||
async function loadApprovals() {
|
||||
const read = ++approvalRead;
|
||||
try {
|
||||
await answerApproval(m.request_id, approved);
|
||||
await catchUp();
|
||||
const pending = await pendingApprovals(controller.signal);
|
||||
if (read !== approvalRead || controller.signal.aborted) return;
|
||||
approvals.value = pending;
|
||||
approvalReadFailed.value = false;
|
||||
} catch (err) {
|
||||
if (read !== approvalRead || controller.signal.aborted) return;
|
||||
if (err instanceof Unauthorized) fail(err);
|
||||
else approvalReadFailed.value = true;
|
||||
}
|
||||
}
|
||||
|
||||
async function answer(request: PendingApproval, approved: boolean) {
|
||||
if (answering.value) return;
|
||||
answering.value = request.id;
|
||||
try {
|
||||
await answerApproval(request.id, approved);
|
||||
++approvalRead;
|
||||
approvals.value = approvals.value.filter(({ id }) => id !== request.id);
|
||||
await loadApprovals();
|
||||
} catch (err) {
|
||||
fail(err);
|
||||
} finally {
|
||||
@@ -47,32 +64,59 @@ async function answer(m: Message, approved: boolean) {
|
||||
|
||||
const cursor = () => messages.value.at(-1)?.seq ?? 0;
|
||||
|
||||
function updateReadingPosition() {
|
||||
const view = activity.value;
|
||||
if (view) atLatest.value = view.scrollHeight - view.clientHeight - view.scrollTop <= 2;
|
||||
}
|
||||
|
||||
function showLatest(focus = false) {
|
||||
const view = activity.value;
|
||||
if (!view) return;
|
||||
view.scrollTop = view.scrollHeight;
|
||||
if (focus) view.focus({ preventScroll: true });
|
||||
updateReadingPosition();
|
||||
}
|
||||
|
||||
async function load() {
|
||||
loading.value = true;
|
||||
error.value = "";
|
||||
try {
|
||||
messages.value = await latestMessages(controller.signal);
|
||||
const [latest] = await Promise.all([latestMessages(controller.signal), loadApprovals()]);
|
||||
messages.value = latest;
|
||||
} catch (err) {
|
||||
fail(err);
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
await nextTick();
|
||||
showLatest();
|
||||
if (!following && !error.value && !controller.signal.aborted) {
|
||||
following = true;
|
||||
void follow();
|
||||
}
|
||||
}
|
||||
|
||||
async function catchUp() {
|
||||
const more = await messagesAfter(cursor(), controller.signal);
|
||||
messages.value.push(...more);
|
||||
async function catchUp(through?: number) {
|
||||
do {
|
||||
const more = await messagesAfter(cursor(), controller.signal);
|
||||
if (more.length === 0) break;
|
||||
updateReadingPosition();
|
||||
messages.value.push(...more);
|
||||
await nextTick();
|
||||
if (atLatest.value) showLatest();
|
||||
else updateReadingPosition();
|
||||
} while (through === undefined || cursor() < through);
|
||||
await loadApprovals();
|
||||
}
|
||||
|
||||
async function follow() {
|
||||
let backoff = 1000;
|
||||
while (!controller.signal.aborted) {
|
||||
try {
|
||||
for await (const seq of composerStream(controller.signal)) {
|
||||
for await (const seq of composerStream(controller.signal, catchUp)) {
|
||||
backoff = 1000;
|
||||
if (seq > cursor()) await catchUp();
|
||||
if (seq > cursor()) await catchUp(seq);
|
||||
}
|
||||
await catchUp();
|
||||
} catch (err) {
|
||||
if (controller.signal.aborted) return;
|
||||
if (err instanceof Unauthorized) return fail(err);
|
||||
@@ -88,49 +132,94 @@ function fail(err: unknown) {
|
||||
error.value = err instanceof Error ? err.message : String(err);
|
||||
}
|
||||
|
||||
onMounted(async () => {
|
||||
await load();
|
||||
void follow();
|
||||
});
|
||||
onMounted(load);
|
||||
onBeforeUnmount(() => controller.abort());
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>
|
||||
<div class="min-w-0">
|
||||
<Spinner v-if="loading" />
|
||||
<ReadError v-else-if="error" :message="error" retry-label="Retry" @retry="load" />
|
||||
<Empty v-else-if="messages.length === 0">
|
||||
<EmptyHeader><EmptyTitle>No messages yet</EmptyTitle></EmptyHeader>
|
||||
</Empty>
|
||||
<Section v-for="m in messages" v-else :key="m.id" :title="m.title">
|
||||
<template #action>
|
||||
<span class="flex items-center gap-2 text-caption text-muted-foreground">
|
||||
<RouterLink v-if="m.run_id" :to="{ name: 'run', params: { id: m.run_id } }" class="underline">Run</RouterLink>
|
||||
<Timestamp :at="m.created_at" />
|
||||
</span>
|
||||
</template>
|
||||
<p class="whitespace-pre-wrap text-label">{{ m.body }}</p>
|
||||
<details v-if="m.attempts?.length" class="mt-2 text-caption">
|
||||
<summary class="cursor-pointer text-muted-foreground">Delivery attempts</summary>
|
||||
<ul class="mt-1 flex flex-col gap-1">
|
||||
<li v-for="a in m.attempts" :key="`${a.channel}:${a.attempt}`" class="flex flex-wrap items-center gap-2">
|
||||
<span class="font-medium">{{ a.channel }}</span>
|
||||
<span class="text-muted-foreground">Attempt {{ a.attempt }}</span>
|
||||
<Badge :variant="a.ok ? 'secondary' : 'danger'">{{ a.ok ? "Delivered" : "Failed" }}</Badge>
|
||||
<span class="tabular-nums text-muted-foreground">{{ a.duration_ms }} ms</span>
|
||||
<Timestamp :at="a.at" />
|
||||
<span v-if="a.response" class="font-mono">{{ a.response }}</span>
|
||||
<span v-if="a.error" class="text-destructive">{{ a.error }}</span>
|
||||
<template v-else>
|
||||
<Section v-if="approvals.length || approvalReadFailed" title="Action required">
|
||||
<template #action>
|
||||
<Badge v-if="approvals.length" variant="secondary">{{ approvals.length }} pending</Badge>
|
||||
</template>
|
||||
<ReadError v-if="approvalReadFailed" message="Couldn't load pending approvals." retry-label="Retry" @retry="loadApprovals" />
|
||||
<ul v-else class="max-h-[24dvh] divide-y overflow-y-auto">
|
||||
<li v-for="request in approvals" :key="request.id" class="py-2 first:pt-0 last:pb-0">
|
||||
<details class="text-label">
|
||||
<summary class="cursor-pointer font-medium break-words">{{ request.action }}</summary>
|
||||
<p class="mt-2 whitespace-pre-wrap break-words">{{ request.summary }}</p>
|
||||
<div class="mt-2 flex flex-wrap items-center gap-2 text-caption text-muted-foreground">
|
||||
<span>Respond by <Timestamp :at="request.step_deadline || request.deadline" absolute /></span>
|
||||
<RouterLink
|
||||
v-if="request.run_id && request.run_id !== '00000000-0000-0000-0000-000000000000'"
|
||||
:to="{ name: 'run', params: { id: request.run_id } }"
|
||||
:aria-label="`View run for ${request.action} (${request.run_id})`"
|
||||
class="underline"
|
||||
>View run</RouterLink>
|
||||
</div>
|
||||
<div class="mt-3 flex flex-wrap gap-2">
|
||||
<Button size="sm" :disabled="answering !== null" @click="answer(request, true)">Approve</Button>
|
||||
<Button variant="outline" size="sm" :disabled="answering !== null" @click="answer(request, false)">Deny</Button>
|
||||
</div>
|
||||
</details>
|
||||
</li>
|
||||
</ul>
|
||||
</details>
|
||||
<div v-if="wantsAnswer(m)" class="mt-3 flex items-center gap-2">
|
||||
<Button size="sm" :disabled="answering === m.request_id" @click="answer(m, true)">Approve</Button>
|
||||
<Button variant="outline" size="sm" :disabled="answering === m.request_id" @click="answer(m, false)">Deny</Button>
|
||||
<span v-if="m.expires_at" class="text-caption text-muted-foreground">
|
||||
Expires <Timestamp :at="m.expires_at" />
|
||||
</span>
|
||||
</Section>
|
||||
<div class="min-w-0">
|
||||
<div class="mb-2 flex min-h-8 flex-wrap items-center justify-between gap-2">
|
||||
<CardTitle id="composer-activity">Recent activity</CardTitle>
|
||||
<Button v-if="!atLatest && messages.length" size="sm" variant="outline" @click="showLatest(true)">Jump to latest</Button>
|
||||
</div>
|
||||
<Empty v-if="messages.length === 0">
|
||||
<EmptyHeader><EmptyTitle>No messages yet</EmptyTitle></EmptyHeader>
|
||||
</Empty>
|
||||
<div
|
||||
v-else
|
||||
ref="activity"
|
||||
role="region"
|
||||
aria-labelledby="composer-activity"
|
||||
tabindex="0"
|
||||
class="max-h-[55dvh] overflow-y-auto rounded-lg border"
|
||||
@scroll.passive="updateReadingPosition"
|
||||
@toggle.capture="updateReadingPosition"
|
||||
>
|
||||
<ol class="divide-y">
|
||||
<li v-for="m in messages" :key="m.id" class="min-w-0 px-3 py-2">
|
||||
<div class="flex flex-wrap items-baseline gap-x-3 gap-y-1">
|
||||
<p class="min-w-0 flex-1 text-label font-medium break-words">{{ m.title }}</p>
|
||||
<Timestamp :at="m.created_at" class="text-caption text-muted-foreground" />
|
||||
<RouterLink
|
||||
v-if="m.run_id"
|
||||
:to="{ name: 'run', params: { id: m.run_id } }"
|
||||
:aria-label="`View run for ${m.title} (${m.run_id})`"
|
||||
class="text-caption text-muted-foreground underline"
|
||||
>View run</RouterLink>
|
||||
</div>
|
||||
<details v-if="m.body" class="mt-1 text-caption">
|
||||
<summary class="cursor-pointer text-muted-foreground">Details<span class="sr-only"> for {{ m.title }}</span></summary>
|
||||
<p class="mt-1 whitespace-pre-wrap break-words text-label">{{ m.body }}</p>
|
||||
</details>
|
||||
<details v-if="m.attempts?.length" class="mt-1 text-caption">
|
||||
<summary class="cursor-pointer text-muted-foreground">Delivery attempts</summary>
|
||||
<ul class="mt-1 flex flex-col gap-1">
|
||||
<li v-for="a in m.attempts" :key="`${a.channel}:${a.attempt}`" class="flex flex-wrap items-center gap-2">
|
||||
<span class="font-medium">{{ a.channel }}</span>
|
||||
<span class="text-muted-foreground">Attempt {{ a.attempt }}</span>
|
||||
<Badge :variant="a.ok ? 'secondary' : 'danger'">{{ a.ok ? "Delivered" : "Failed" }}</Badge>
|
||||
<span class="tabular-nums text-muted-foreground">{{ a.duration_ms }} ms</span>
|
||||
<Timestamp :at="a.at" />
|
||||
<span v-if="a.response" class="font-mono break-all">{{ a.response }}</span>
|
||||
<span v-if="a.error" class="text-destructive break-all">{{ a.error }}</span>
|
||||
</li>
|
||||
</ul>
|
||||
</details>
|
||||
</li>
|
||||
</ol>
|
||||
</div>
|
||||
</div>
|
||||
</Section>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
+31
-14
@@ -321,6 +321,17 @@ export const latestMessages = (signal?: AbortSignal) => get<Message[]>("/api/com
|
||||
export const messagesAfter = (seq: number, signal?: AbortSignal) =>
|
||||
get<Message[]>(`/api/composer/messages?after=${seq}`, signal);
|
||||
|
||||
export interface PendingApproval {
|
||||
id: string;
|
||||
run_id: string;
|
||||
action: string;
|
||||
summary: string;
|
||||
deadline: string;
|
||||
step_deadline?: string;
|
||||
}
|
||||
|
||||
export const pendingApprovals = (signal?: AbortSignal) => get<PendingApproval[]>("/api/approvals", signal);
|
||||
|
||||
/** Answers an approval request the composer is showing. */
|
||||
export async function answerApproval(id: string, approved: boolean): Promise<void> {
|
||||
const res = await fetch(`/api/approvals/${encodeURIComponent(id)}/answer`, {
|
||||
@@ -649,25 +660,31 @@ export const listJudges = (signal?: AbortSignal) => get<JudgeControl[]>("/api/co
|
||||
export const runTrail = (id: string, signal?: AbortSignal) => get<Trail>(`/api/runs/${encodeURIComponent(id)}`, signal);
|
||||
|
||||
/**
|
||||
* Yields each new composer message's seq as the server announces it. Returns
|
||||
* when the server ends the stream (a subscriber that fell behind is cut off),
|
||||
* so the caller reconnects and catches up from its cursor.
|
||||
* Yields sequence numbers for new messages. Subscribe before fetching missed
|
||||
* messages so updates published during catch-up are queued. If the server
|
||||
* disconnects a slow subscriber, the caller reconnects from its last sequence.
|
||||
*/
|
||||
export async function* composerStream(signal: AbortSignal): AsyncGenerator<number> {
|
||||
export async function* composerStream(signal: AbortSignal, onConnected?: () => Promise<void>): AsyncGenerator<number> {
|
||||
const res = await request("/api/composer/stream", signal);
|
||||
if (!res.body) return;
|
||||
const reader = res.body.pipeThrough(new TextDecoderStream()).getReader();
|
||||
let buffered = "";
|
||||
for (;;) {
|
||||
const { value, done } = await reader.read();
|
||||
if (done) return;
|
||||
buffered += value;
|
||||
let end: number;
|
||||
while ((end = buffered.indexOf("\n\n")) >= 0) {
|
||||
const frame = buffered.slice(0, end);
|
||||
buffered = buffered.slice(end + 2);
|
||||
const data = frame.split("\n").find((line) => line.startsWith("data: "));
|
||||
if (data) yield (JSON.parse(data.slice(6)) as { seq: number }).seq;
|
||||
try {
|
||||
await onConnected?.();
|
||||
for (;;) {
|
||||
const { value, done } = await reader.read();
|
||||
if (done) return;
|
||||
buffered += value;
|
||||
let end: number;
|
||||
while ((end = buffered.indexOf("\n\n")) >= 0) {
|
||||
const frame = buffered.slice(0, end);
|
||||
buffered = buffered.slice(end + 2);
|
||||
const data = frame.split("\n").find((line) => line.startsWith("data: "));
|
||||
if (data) yield (JSON.parse(data.slice(6)) as { seq: number }).seq;
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
void reader.cancel().catch(() => undefined);
|
||||
reader.releaseLock();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user