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

186 lines
7.8 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 { runQuery, type LogState } from "./task-run";
import { firstLine } from "./first-line";
import { statusKind, statusLabel } from "@/lib/task-meta";
import { cn } from "@/lib/utils";
import DispatchLinks from "@/bindings/DispatchLinks.vue";
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;
});
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 one control: the reap an operator reaches for once a
// task is finished with. Whether this task offers it at all is the server's
// action list, never this page's opinion about the status.
const HEAD_ACTIONS = ["archive", "merge"];
// The run's inputs and written outputs, one row per name.
const io = computed(() => {
const rows = (prefix: string, values?: Record<string, unknown>) =>
Object.entries(values ?? {}).map(([name, value]) => ({
label: `${prefix} ${name}`,
value: typeof value === "string" ? value : JSON.stringify(value),
}));
return [...rows("input", run.attempts?.inputs), ...rows("output", run.attempts?.outputs)];
});
const headerActions = computed(() =>
shownActionIds(run.task?.actions, HEAD_ACTIONS),
);
</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>
<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>
<div v-if="description" class="mt-4">
<h2 class="text-sm font-medium">Description</h2>
<p class="mt-1 whitespace-pre-wrap break-words text-sm text-fg-muted">{{ description }}</p>
</div>
<dl
v-if="io.length || run.attempts?.called_by"
class="mt-3 grid grid-cols-[auto_1fr] gap-x-3 gap-y-0.5 text-xs"
>
<template v-if="run.attempts?.called_by">
<dt class="text-fg-subtle">Called by</dt>
<dd>
<RouterLink :to="`/tasks/${run.attempts.called_by}`" class="font-mono text-fg-muted hover:text-foreground hover:underline"
>task #{{ run.attempts.called_by }}</RouterLink
>
</dd>
</template>
<template v-for="row in io" :key="row.label">
<dt class="text-fg-subtle">{{ row.label }}</dt>
<dd class="font-mono break-words text-fg-muted">{{ row.value }}</dd>
</template>
</dl>
<Alert
v-if="failure"
:variant="failure.parked ? 'default' : 'destructive'"
:class="cn('mt-4', failure.parked && 'border-warn/40 bg-warn-soft')"
>
<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 break-words text-fg-muted">{{ failure.detail }}</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>