-
Notifications
You must be signed in to change notification settings - Fork 1
Mention tasks in any chat surface #93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import assert from "node:assert/strict"; | ||
| import test from "node:test"; | ||
|
|
||
| import { TASK_KEY, matchTasks } from "./mentions.ts"; | ||
| import type { Task, TaskStatus } from "./types.ts"; | ||
|
|
||
| const task = (key: string, title: string, status: TaskStatus, updated: number) => | ||
| ({ | ||
| id: `id-${key}`, | ||
| key, | ||
| title, | ||
| outcome: "", | ||
| status, | ||
| discussion_channel_id: "c1", | ||
| created_by: "u1", | ||
| created_at: 0, | ||
| updated_at: updated, | ||
| position: 0, | ||
| }) as Task; | ||
|
|
||
| const tasks = [ | ||
| task("PW-1", "Ship the relay", "done", 300), | ||
| task("PW-42", "Allow mentioning tasks", "planned", 100), | ||
| task("ACME-7", "Cache the pricing endpoint", "running", 200), | ||
| ]; | ||
|
|
||
| test("open work is offered before finished work, newest first", () => { | ||
| assert.deepEqual( | ||
| matchTasks(tasks, "").map((found) => found.key), | ||
| ["ACME-7", "PW-42", "PW-1"], | ||
| ); | ||
| }); | ||
|
|
||
| test("a key or a word from the title finds the task", () => { | ||
| assert.deepEqual( | ||
| matchTasks(tasks, "pw-4").map((found) => found.key), | ||
| ["PW-42"], | ||
| ); | ||
| assert.deepEqual( | ||
| matchTasks(tasks, "pricing").map((found) => found.key), | ||
| ["ACME-7"], | ||
| ); | ||
| assert.deepEqual(matchTasks(tasks, "nothing here"), []); | ||
| }); | ||
|
|
||
| test("a key is recognised in prose but not inside a longer token", () => { | ||
| const find = (text: string) => text.match(new RegExp(TASK_KEY.source))?.[1]; | ||
| assert.equal(find("landed in PW-42, see above"), "PW-42"); | ||
| assert.equal(find("ACME-7"), "ACME-7"); | ||
| assert.equal(find("v2-1024-beta"), undefined); | ||
| assert.equal(find("plain words"), undefined); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // Mentioning a task is just writing its key. `PW-42` in a message is the same | ||
| // reference everywhere — typed by a person, printed by an agent, or copied out | ||
| // of the CLI — so the composer only has to help find the key, and the renderer | ||
| // only has to recognise the keys that name a real task. | ||
|
|
||
| import type { Task } from "./types"; | ||
|
|
||
| /// The shape of a key: the workspace prefix is configurable, so this matches | ||
| /// `ACME-7` as readily as `PW-42` and the caller decides which keys are real. | ||
| export const TASK_KEY = /(?<![\w-])([A-Za-z][A-Za-z0-9]*-\d+)(?![\w-])/; | ||
|
|
||
| const finished = (task: Task) => task.status === "done" || task.status === "canceled"; | ||
|
|
||
| /// Tasks worth offering for what has been typed after `#`, open work first and | ||
| /// most recently touched before the rest. | ||
| export function matchTasks(tasks: Task[], query: string, limit = 6): Task[] { | ||
| const needle = query.trim().toLowerCase(); | ||
| return tasks | ||
| .filter( | ||
| (task) => | ||
| task.key.toLowerCase().includes(needle) || | ||
| task.title.toLowerCase().includes(needle), | ||
| ) | ||
| .sort( | ||
| (a, b) => | ||
| Number(finished(a)) - Number(finished(b)) || b.updated_at - a.updated_at, | ||
| ) | ||
| .slice(0, limit); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| // table has to render as something calm rather than flickering into garbage. | ||
|
|
||
| import type { ReactNode } from "react"; | ||
| import { TASK_KEY } from "@client/mentions"; | ||
|
|
||
| // --- blocks ----------------------------------------------------------------- | ||
|
|
||
|
|
@@ -346,6 +347,9 @@ export interface InlineOptions { | |
| /// Handles that resolve to a real member, so `@nobody` stays plain text. | ||
| handles?: Set<string>; | ||
| onMention?: (handle: string) => void; | ||
| /// Uppercased task keys to task ids, so `PW-999` stays plain text. | ||
| tasks?: Map<string, string>; | ||
| onTask?: (taskId: string) => void; | ||
| } | ||
|
|
||
| /// Only schemes that can't execute anything. A `javascript:` href in an agent's | ||
|
|
@@ -371,6 +375,7 @@ const INLINE = new RegExp( | |
| "<(https?://[^>\\s]+)>", // autolink | ||
| "(?<![\\w/])(https?://[^\\s<>()\\[\\]]+[^\\s<>()\\[\\].,;:!?'\"])", // bare url | ||
| "@([a-z0-9][\\w-]*)", // mention | ||
| TASK_KEY.source, // task mention: PW-42 | ||
| ].join("|"), | ||
| "gi", | ||
| ); | ||
|
|
@@ -423,6 +428,7 @@ export function renderInline( | |
| autolink, | ||
| bareUrl, | ||
| mention, | ||
| taskKey, | ||
| ] = match; | ||
|
|
||
| if (code !== undefined) { | ||
|
|
@@ -484,6 +490,26 @@ export function renderInline( | |
| <span key={k}>@{mention}</span> | ||
| ), | ||
| ); | ||
| } else if (taskKey !== undefined) { | ||
| // Every surface writes the key the same way, so recognising it here makes | ||
| // a task someone typed and a task an agent cited the same live reference. | ||
| const taskId = options.tasks?.get(taskKey.toUpperCase()); | ||
| push( | ||
| taskId ? ( | ||
| <span | ||
| key={k} | ||
| className="mention task" | ||
| onClick={(event) => { | ||
| event.stopPropagation(); | ||
| options.onTask?.(taskId); | ||
|
Comment on lines
+502
to
+504
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a known task key is used as an explicit Markdown link label, such as Useful? React with 👍 / 👎. |
||
| }} | ||
| > | ||
| {taskKey.toUpperCase()} | ||
| </span> | ||
| ) : ( | ||
| <span key={k}>{taskKey}</span> | ||
| ), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Workspaces can validly configure a task prefix beginning with a digit because
update_workspaceretains any nonempty ASCII-alphanumeric prefix, producing keys such as2FA-1or123-4. This pattern requires the first character to be a letter, so those real tasks can be selected by the new composer menu but remain plain text in desktop messages. Match an alphanumeric leading character or tighten the server-side prefix validation consistently.Useful? React with 👍 / 👎.