Files
archie/ui/src/tasks/TaskHeader.vue
T

207 lines
9.1 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import { computed, nextTick, ref, watch } from "vue";
import { useRouter } from "vue-router";
import { ArrowUpRight, CircleAlert } from "@lucide/vue";
import {
Breadcrumb,
BreadcrumbItem,
BreadcrumbLink,
BreadcrumbList,
BreadcrumbPage,
BreadcrumbSeparator,
} from "@/components/ui/breadcrumb";
import { Badge } from "@/components/ui/badge";
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
import { Button } from "@/components/ui/button";
import { api } from "@/lib/api";
import { useOrgStore } from "@/stores/org";
import { runQuery, type LogState } from "./task-run";
import { firstLine } from "./first-line";
import { approvalLabels, statusKind, statusLabel } from "@/lib/task-meta";
import DispatchLinks from "@/bindings/DispatchLinks.vue";
import { MetadataList } from "@/components/ui/metadata-list";
import { Section } from "@/components/ui/section";
import StartNewRun from "./StartNewRun.vue";
import TaskRowActions from "./TaskRowActions.vue";
import { shownActionIds } from "./task-actions";
import { useTaskRun } from "./use-task-run";
/**
* The task's head: where it sits, what it is, its state and forge links, and
* the page's one decision, starting a new run. A parked task opens with the
* reason it stopped.
*/
const props = defineProps<{ id: string }>();
const run = useTaskRun();
const title = computed(() => firstLine(run.task?.title || `Task #${props.id}`));
const description = computed(() => run.task?.body?.trim() || (run.task?.title !== title.value ? run.task?.title : ""));
const status = computed(() => run.task?.status ?? "");
const router = useRouter();
const attempt = computed(() => run.attempts?.attempts?.find((a) => a.attempt === run.attemptNumber));
const failure = computed(() => {
const stage = attempt.value?.stages?.find((s) => s.error);
const current = run.attemptNumber === run.currentAttempt;
if (current && status.value === "parked") return { title: "Parked", detail: run.task?.park_reason || stage?.error || "", parked: true };
if (attempt.value?.status === "failed" || (current && status.value === "dead")) return { title: `Attempt ${run.attemptNumber} failed${stage?.name ? ` during ${stage.name}` : ""}`, detail: stage?.error || run.task?.park_reason || "", parked: false };
return null;
});
// The daemon names a workflow's identity by its id in the parked reason; this
// page knows which ids are identities and shows each by name, linked to the
// page that manages it. An id this install does not know keeps its raw text.
const identities = useOrgStore();
identities.watchIdentities();
const detailParts = computed(() => {
const text = failure.value?.detail ?? "";
const known = identities.identities.map((actor) => actor.id).filter((id) => text.includes(id));
if (!known.length) return [{ text, to: "" }];
const pattern = new RegExp(`(${known.map((id) => id.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|")})`, "g");
return text
.split(pattern)
.map((part) => (known.includes(part) ? { text: identities.identityName(part), to: "/org/agents" } : { text: part, to: "" }));
});
const jumping = ref(false);
const jumpError = ref("");
watch(() => run.attemptNumber, () => { jumpError.value = ""; });
async function jumpToLog(): Promise<void> {
const selected = run.attemptNumber;
if (selected == null) return;
jumping.value = true;
jumpError.value = "";
try {
const logs = await api.taskLogs<LogState>(props.id, { attempt: selected, level: "ERROR", limit: 1 });
if (selected !== run.attemptNumber) return;
const entry = logs.entries?.[0];
if (!entry?.id) {
jumpError.value = "No error log line was recorded for this attempt.";
return;
}
run.setFilters({ level: "ERROR", stage: "" });
run.setTab("log");
await nextTick();
await router.replace({ query: runQuery("log", selected), hash: `#log-line-${entry.id}` });
const line = document.querySelector<HTMLElement>(`[data-log-id="${entry.id}"]`);
line?.scrollIntoView({ block: "center" });
line?.focus({ preventScroll: true });
} catch {
if (selected === run.attemptNumber) jumpError.value = "Could not read this attempt’s error log.";
} finally {
jumping.value = false;
}
}
// The head has room for the reap an operator reaches for once a task is
// finished with. Whether this task offers it is the server's action list,
// never this page's opinion about the status. Approval answers live with the
// thing being approved.
const HEAD_ACTIONS = computed(() => ["archive", "merge"]);
// The run's inputs and written outputs, one row per name.
const rows = (values?: Record<string, unknown>) =>
Object.entries(values ?? {}).map(([label, value]) => ({
label,
mono: true,
value: typeof value === "string" ? value : JSON.stringify(value),
}));
const sections = computed(() =>
[
{ title: "Inputs", rows: rows(run.attempts?.inputs) },
{ title: "Outputs", rows: rows(run.attempts?.outputs) },
].filter((section) => section.rows.length),
);
const headerActions = computed(() =>
shownActionIds(run.task?.actions, HEAD_ACTIONS.value),
);
</script>
<template>
<div class="mb-6">
<Breadcrumb class="mb-2">
<BreadcrumbList>
<BreadcrumbItem><BreadcrumbLink as-child><RouterLink to="/tasks">Tasks</RouterLink></BreadcrumbLink></BreadcrumbItem>
<BreadcrumbSeparator />
<BreadcrumbItem><BreadcrumbPage class="font-mono">#{{ id }}</BreadcrumbPage></BreadcrumbItem>
</BreadcrumbList>
</Breadcrumb>
<div class="flex flex-wrap items-start justify-between gap-4">
<div class="min-w-0">
<h1 class="text-2xl font-semibold break-words text-balance">{{ title }}</h1>
<div class="mt-2 flex flex-wrap items-center gap-x-3 gap-y-1 text-sm">
<Badge v-if="status" :variant="statusKind(status)">{{ statusLabel(status) }}</Badge>
<Badge v-if="run.task?.approval" variant="outline">{{ approvalLabels[run.task.approval] ?? run.task.approval }}</Badge>
<span v-if="run.task?.workflow" class="font-mono text-xs text-fg-muted">{{ run.task.workflow }}</span>
<a
v-if="run.task?.issue_url && run.task?.issue_number"
class="inline-flex items-center gap-0.5 font-mono text-xs text-fg-muted hover:text-foreground"
:href="run.task.issue_url"
target="_blank"
rel="noreferrer"
>{{ run.task.owner }}/{{ run.task.repo }} #{{ run.task.issue_number }}<ArrowUpRight class="size-3" aria-hidden="true"
/></a>
<DispatchLinks kind="tasks" :id="id" />
<a
v-if="run.task?.pr_url && run.task?.pr_number"
class="inline-flex items-center gap-0.5 font-mono text-xs text-fg-muted hover:text-foreground"
:href="run.task.pr_url"
target="_blank"
rel="noreferrer"
>PR #{{ run.task.pr_number }}<ArrowUpRight class="size-3" aria-hidden="true"
/></a>
</div>
</div>
<div class="flex flex-wrap items-center gap-2">
<TaskRowActions
v-if="run.task && headerActions.length"
:task="run.task"
:only="HEAD_ACTIONS"
@done="run.refreshAll()"
/>
<StartNewRun compact :id="id" />
</div>
</div>
<Section v-if="description" title="Description" class="mt-4 mb-0">
<p class="whitespace-pre-wrap break-words text-sm text-fg-muted">{{ description }}</p>
</Section>
<div v-if="sections.length || run.attempts?.called_by" class="mt-3 flex flex-wrap items-start gap-3">
<p v-if="run.attempts?.called_by" class="text-xs text-fg-subtle">
Called by
<RouterLink :to="`/tasks/${run.attempts.called_by}`" class="font-mono text-fg-muted hover:text-foreground hover:underline"
>task #{{ run.attempts.called_by }}</RouterLink
>
</p>
<Section v-for="section in sections" :key="section.title" :title="section.title" class="mb-0 min-w-0">
<MetadataList :rows="section.rows" class="text-fg-muted" />
</Section>
</div>
<Alert
v-if="failure"
:variant="failure.parked ? 'warn' : 'destructive'"
class="mt-4"
>
<CircleAlert aria-hidden="true" />
<AlertTitle class="flex flex-wrap items-center justify-between gap-2">
<span>{{ failure.title }}</span>
<Button variant="ghost" size="sm" :disabled="jumping || run.attemptNumber == null" @click="jumpToLog">Jump to log line</Button>
</AlertTitle>
<AlertDescription>
<p v-if="failure.detail" class="mt-0.5 font-mono text-xs text-fg-muted [overflow-wrap:anywhere]">
<template v-for="(part, index) in detailParts" :key="index">
<RouterLink v-if="part.to" :to="part.to" class="underline decoration-dotted hover:text-foreground">{{ part.text }}</RouterLink>
<template v-else>{{ part.text }}</template>
</template>
</p>
<p v-if="jumpError" class="mt-1 text-xs" role="alert">{{ jumpError }}</p>
<Button v-if="failure.parked && run.attempts?.recovery" as-child size="sm" class="mt-2">
<RouterLink :to="run.attempts.recovery.link">{{ run.attempts.recovery.label }}</RouterLink>
</Button>
</AlertDescription>
</Alert>
</div>
</template>