feat(chat): link #N in replies to tasks that exist (#1882)
ci/woodpecker/pr/quality Pipeline was canceled
Quality / check (pull_request) Successful in 16m18s

This commit is contained in:
sam committed 2026-11-12 22:46:22 +11:00
1 parent 37ea698a28
commit 7ffbf0435c
4 files changed
+131 -2

No files matched your search

+2
View File
@@ -1,4 +1,5 @@
<script setup lang="ts">
import ChatTaskRef from "./ChatTaskRef.vue";
import type { ChatInline } from "./markdown";
/** One inline run: the spans of a paragraph, a heading or a table cell. */
@@ -29,6 +30,7 @@ defineProps<{ inline: ChatInline[] }>();
class="text-link underline-offset-4 hover:underline"
>{{ span.text }}</a
>
<ChatTaskRef v-else-if="span.kind === 'taskref'" :id="span.id" />
<em v-else-if="span.kind === 'em'">{{ span.text }}</em>
<template v-else>{{ span.text }}</template>
</template>
+59
View File
@@ -0,0 +1,59 @@
<script setup lang="ts">
import { onBeforeUnmount, ref, watch } from "vue";
import { useRoute } from "vue-router";
import { revealTaskRow, taskExists } from "./task-ref";
/**
* A #N in a reply. It becomes a link only once the tasks API confirms the
* task exists, so an issue number or a stray count stays plain text. On the
* Tasks page, where the row is already listed, it scrolls there instead of
* leaving the page.
*/
const props = defineProps<{ id: number }>();
const route = useRoute();
const exists = ref(false);
// A streaming reply shows #1 before #12; the short wait skips the partial one.
let timer = 0;
watch(
() => props.id,
(id) => {
exists.value = false;
window.clearTimeout(timer);
timer = window.setTimeout(async () => {
const found = await taskExists(id);
if (id === props.id) exists.value = found === true;
}, 250);
},
{ immediate: true },
);
onBeforeUnmount(() => window.clearTimeout(timer));
function onClick(event: MouseEvent, navigate: (e: MouseEvent) => void) {
if (route.path === "/tasks" && revealTaskRow(props.id)) {
event.preventDefault();
return;
}
navigate(event);
}
</script>
<template>
<RouterLink
v-if="exists"
v-slot="{ href, navigate }"
:to="`/tasks/${id}`"
custom
>
<a
:href="href"
:title="`Task ${id}`"
class="rounded bg-primary-soft px-1 py-0.5 font-mono text-[0.9em] text-link hover:underline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary"
@click="onClick($event, navigate)"
>#{{ id }}</a
>
</RouterLink>
<template v-else>#{{ id }}</template>
</template>
+20 -2
View File
@@ -15,6 +15,7 @@ export type ChatInline =
| { kind: "del"; text: string }
| { kind: "code"; text: string }
| { kind: "link"; text: string; href: string }
| { kind: "taskref"; id: number }
| { kind: "image"; alt: string; src: string };
/** One block of the document. Lists carry their items as inline runs. */
@@ -27,6 +28,23 @@ export type ChatBlock =
const INLINE =
/(!\[[^\]]*\]\((?:https?:\/\/[^\s)]+|data:image\/[^\s)]+|\/[^\s)]+)\)|~~[^~]+~~|\*\*[^*]+\*\*|__[^_]+__|`[^`]+`|\[[^\]]+\]\((?:https?:\/\/[^\s)]+|\/[^\s)]+)\)|\*[^*]+\*|_[^_]+_)/g;
// A #N the model wrote about a task. Not part of a word, entity or path.
const TASK_REF = /(?<![\w&#/])#(\d{1,9})(?!\w)/g;
// plainText splits a run of plain text around its task references.
function plainText(text: string): ChatInline[] {
const out: ChatInline[] = [];
let cursor = 0;
for (const match of text.matchAll(TASK_REF)) {
if (match.index > cursor)
out.push({ kind: "text", text: text.slice(cursor, match.index) });
out.push({ kind: "taskref", id: Number(match[1]) });
cursor = match.index + match[0].length;
}
if (cursor < text.length) out.push({ kind: "text", text: text.slice(cursor) });
return out;
}
// inlineMarkdown returns the children of one line: plain text and the element
// each matched span becomes, in order.
function inlineMarkdown(text: string): ChatInline[] {
@@ -34,7 +52,7 @@ function inlineMarkdown(text: string): ChatInline[] {
let cursor = 0;
for (const match of String(text).matchAll(INLINE)) {
if (match.index > cursor)
children.push({ kind: "text", text: text.slice(cursor, match.index) });
children.push(...plainText(text.slice(cursor, match.index)));
const value = match[0];
if (value.startsWith("![")) {
const img = value.match(/^!\[([^\]]*)\]\((.+)\)$/);
@@ -57,7 +75,7 @@ function inlineMarkdown(text: string): ChatInline[] {
cursor = match.index + value.length;
}
if (cursor < text.length)
children.push({ kind: "text", text: text.slice(cursor) });
children.push(...plainText(text.slice(cursor)));
return children;
}
+50
View File
@@ -0,0 +1,50 @@
import { api, ApiError } from "@/lib/api";
const FOUND_TTL_MS = 10 * 60_000;
const MISSING_TTL_MS = 30_000;
const lookups = new Map<number, { at: number; found: boolean }>();
const inflight = new Map<number, Promise<boolean | null>>();
/**
* Whether task `id` exists, asked of the tasks API. Answers are kept briefly
* so a reply that re-renders every frame asks once. A task that was missing a
* moment ago may have been created since, so that answer expires quickly. A
* failed lookup is null and is not kept: the reference stays plain text.
*/
export function taskExists(id: number): Promise<boolean | null> {
const known = lookups.get(id);
if (known && Date.now() - known.at < (known.found ? FOUND_TTL_MS : MISSING_TTL_MS))
return Promise.resolve(known.found);
let pending = inflight.get(id);
if (!pending) {
pending = api
.task(String(id))
.then(() => true)
.catch((err) => (err instanceof ApiError && err.status === 404 ? false : null))
.then((found) => {
if (found !== null) lookups.set(id, { at: Date.now(), found });
inflight.delete(id);
return found;
});
inflight.set(id, pending);
}
return pending;
}
const HIGHLIGHT = ["bg-primary-soft", "motion-safe:transition-colors", "motion-safe:duration-700"];
/**
* Scrolls to task `id`'s row when the page lists it and marks it once.
* Returns false when no such row is on screen.
*/
export function revealTaskRow(id: number): boolean {
const row = document.querySelector<HTMLElement>(`tr[data-row-key="${id}"]`);
if (!row) return false;
const still = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
row.scrollIntoView({ block: "center", behavior: still ? "auto" : "smooth" });
row.classList.add(...HIGHLIGHT);
window.setTimeout(() => row.classList.remove("bg-primary-soft"), 1600);
window.setTimeout(() => row.classList.remove(...HIGHLIGHT), 2400);
return true;
}