Public: anyone can read this; the crew can change it.

/talk lists each conversation with its message count and the time of its last message in plain words #14736

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/14720-talk-lists-each-conversation-with-its-me → main 7 commits fork 3 threads · 3 unresolved

  • Merged. scout/14720-talk-lists-each-conversation-with-its-me was merged into main as 66d7db1.

Review

Sign in to review this change.
6 files changed +154 −4
docs/features/talk-conversation-list.md added +14
@@ -0,0 +1,14 @@
1 ---
2 topic: Tell the crew
3 row: Which conversations are alive
4 ---
5 # Which conversations are alive
6
7 Each conversation in the list on `/talk` shows one line under its name: `N messages · last <time>`, for
8 example `3 messages · last 3h ago`, or `no messages yet` when nothing was said. Only what you and the agent said
9 counts; tool steps and replaced messages do not.
10
11 In the terminal, `ssh` in and open Talk: the conversations list shows the same line beside each name.
12
13 Both doors take the counts from one grouped query (`conversationActivity` in `src/core/conversations.ts`), however
14 many conversations are listed.
src/core/conversations.ts +25
@@ -150,6 +150,31 @@ export function listConversations(db: Database, userId: string, opts: { readonly
150150 return rows.map(conversationOf);
151151 }
152152
153 /** How many messages a conversation holds and when the last came. */
154 export interface Activity {
155 readonly messages: number;
156 readonly lastAt: string;
157 }
158
159 /**
160 * The message count and last message time of each listed conversation, in one grouped query. A
161 * message is something a person or the agent said; tool steps, confirms and replaced turns are not.
162 * A conversation with none is absent from the map.
163 */
164 export function conversationActivity(db: Database, userId: string, ids: readonly string[]): Map<string, Activity> {
165 const out = new Map<string, Activity>();
166 if (ids.length === 0) return out;
167 const marks = ids.map((_, i) => `?${i + 2}`).join(",");
168 const rows = db
169 .query(
170 `SELECT t.conversation_id AS id, COUNT(*) AS n, MAX(t.created_at) AS last FROM conversation_turn t JOIN conversation c ON c.id = t.conversation_id
171 WHERE c.user_id = ?1 AND t.conversation_id IN (${marks}) AND t.role IN ('person','agent') AND t.replaced_at IS NULL GROUP BY t.conversation_id`,
172 )
173 .all(userId, ...ids) as { id: string; n: number; last: string }[];
174 for (const r of rows) out.set(r.id, { messages: r.n, lastAt: r.last });
175 return out;
176 }
177
153178 /** Give a conversation a title (1 to `TITLE_MAX` characters, no line breaks); an empty one clears it. */
154179 export function renameConversation(
155180 db: Database,
src/tui/screens/talk_chats.ts +10 −3
@@ -15,6 +15,7 @@ import {
1515 archiveConversation,
1616 pinConversation,
1717 createConversation,
18 conversationActivity,
1819 findConversations,
1920 pageAround,
2021 type Found,
@@ -231,14 +232,20 @@ export function talkChatsScreen(f: Session, ctx: Ctx): Screen {
231232 let found: { typed: string; hits: Found[] } | undefined;
232233 const items = (): Conversation[] =>
233234 found !== undefined ? found.hits.map((h) => h.conversation) : f.user === undefined ? [] : listConversations(f.app.db, f.user.id, { archived });
234 const rows = (): ListRow[] =>
235 found !== undefined
235 const rows = (): ListRow[] => {
236 const list = items();
237 const activity = found !== undefined || f.user === undefined ? new Map() : conversationActivity(f.app.db, f.user.id, list.map((c) => c.id));
238 return found !== undefined
236239 ? found.hits.map((h) => ({
237240 title: h.conversation.title === "" ? "Untitled" : h.conversation.title,
238241 secondary: `${h.conversation.archivedAt === null ? "" : "archived · "}${h.snippet.replace(/\s+/g, " ")}`,
239242 meta: `/talk/${h.conversation.id}`,
240243 }))
241 : items().map((c) => ({ title: c.title === "" ? "Untitled" : c.title, secondary: `/talk/${c.id}`, meta: `active ${ago(c.updatedAt)}` }));
244 : list.map((c) => {
245 const a = activity.get(c.id);
246 return { title: c.title === "" ? "Untitled" : c.title, secondary: `/talk/${c.id}`, meta: a === undefined ? "no messages yet" : `${a.messages} ${a.messages === 1 ? "message" : "messages"} · last ${ago(a.lastAt)}` };
247 });

Changed terminal wording with no test (both doors, tests rule). Add a TUI test asserting this meta for none/one/many messages.

248 };
242249 const say = (text: string): void => {
243250 found = undefined;
244251 note = text;
src/web/pages/talk.ts +9 −1
@@ -22,6 +22,7 @@ import { REFUSED, sayNo, sayYes, UNDO_REFUSED, undoReceipt, waitingConfirms } fr
2222 import { filesOfTurns, type ConversationFile } from "../../core/conversation_files.ts";
2323 import {
2424 archiveConversation,
25 conversationActivity,
2526 type Conversation,
2627 DEFAULT_PAGE,
2728 findConversations,
@@ -282,6 +283,12 @@ export function turnHtml(turn: Turn, waiting: ReadonlySet<string>, cost?: Spend,
282283 return toolCard(id, call, call.split(/[\s(:{]/, 1)[0] ?? "tool", turn.createdAt, set);
283284 }
284285
286 /** `N messages · last 5m ago`, or `no messages yet`: the one line under a conversation's name. */
287 export function activityLine(a: { readonly messages: number; readonly lastAt: string } | undefined): Html {
288 if (a === undefined) return html`no messages yet`;
289 return html`${a.messages} ${a.messages === 1 ? "message" : "messages"} · last ${time(a.lastAt)}`;
290 }
291
285292 /**
286293 * The conversations list, then this one's name with Rename and Archive: each a plain form, so none
287294 * needs the script.
@@ -290,6 +297,7 @@ function chats(ctx: Ctx, conversation: Conversation, showArchived: boolean, type
290297 const user = ctx.auth.user!;
291298 const found = typed === "" ? undefined : findConversations(ctx.app.db, user.id, typed);
292299 const shown = listConversations(ctx.app.db, user.id, { archived: showArchived });
300 const activity = conversationActivity(ctx.app.db, user.id, shown.map((c) => c.id));
293301 const put = listConversations(ctx.app.db, user.id, { archived: !showArchived }).length;
294302 const archived = conversation.archivedAt !== null;
295303 return html`<nav class="talk-chats" aria-label="Conversations">
@@ -327,7 +335,7 @@ function chats(ctx: Ctx, conversation: Conversation, showArchived: boolean, type
327335 html`${i === 0 && c.pinnedAt !== null ? html`<li class="muted small" data-pinned-heading>Pinned</li>` : null}${row({
328336 href: urlOf(c),
329337 title: nameOf(c),
330 meta: html`${time(c.updatedAt)}`,
338 meta: activityLine(activity.get(c.id)),
331339 current: c.id === conversation.id,
332340 ...(c.archivedAt === null
333341 ? {
src/web/pages/talk_client.ts +2
@@ -74,6 +74,8 @@ export const talkCss = /* css */ `
7474 .talk-files { justify-content: flex-end; }
7575 .talk-more { text-align: center; }
7676 .talk-chats, .talk-here { margin-bottom: 12px; }
77 .talk-chats .row-meta, .talk-chats .row-title { overflow-wrap: anywhere; }
outdated

nowrap on a row-meta with a long count+time line can overflow a 360px phone row. Let it wrap under the title on narrow widths; keep tabular-nums.

78 .talk-chats .row-meta { font-variant-numeric: tabular-nums; }
7779 .talk-turn:target { box-shadow: -2px 0 0 var(--muted); scroll-margin-top: 4rem; }
7880 .talk-replaced { opacity: 0.55; }
7981 .talk-failed form { margin-top: 6px; }
test/talk-count-line.test.ts added +94
@@ -0,0 +1,94 @@
1 /** /talk (#14720): each conversation row says how many messages it holds and when the last came. */
2 import { afterAll, beforeAll, expect, test } from "bun:test";
3 import { mkdtemp, rm } from "node:fs/promises";
4 import { tmpdir } from "node:os";
5 import { join } from "node:path";
6 import { createApp, type App } from "../src/app.ts";
7 import { appendTurn, createConversation } from "../src/core/conversations.ts";
8 import { createSession } from "../src/core/sessions.ts";
9 import { createForgeApp } from "../src/tui/forge.ts";
10 import { talkChatsScreen } from "../src/tui/screens/talk_chats.ts";
11 import { headlessTerminal } from "../src/tui/term/terminal.ts";
12 import { settleApp } from "./helpers/tui.ts";
13 import { createUser, type User } from "../src/core/users.ts";
14 import { serve, type Servers } from "../src/http/server.ts";
15 import { promote } from "./helpers/members.ts";
16 import { quietLogs } from "./helpers/globals.ts";
17
18 quietLogs();
19
20 let dir: string;
21 let app: App;
22 let servers: Servers;
23 let u: User;
24
25 beforeAll(async () => {
26 dir = await mkdtemp(join(tmpdir(), "talk-count-"));
27 app = createApp({ dataDir: dir, port: 0 });
28 servers = await serve(app);
29 const made = await createUser(app.db, { origin: "seed", trusted: true, username: "counter", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true });
30 if (!made.ok) throw new Error(made.error);
31 promote(app.db, made.value);
32 u = made.value;
33 });
34 afterAll(async () => {
35 servers.stop();
36 await rm(dir, { recursive: true, force: true });
37 });
38
39 test("three conversations show their count and last time; one grouped query serves them all", async () => {
40 const mk = (title: string) => {
41 const c = createConversation(app.db, u.id);
42 if (!c.ok) throw new Error("no conversation");
43 app.db.query("UPDATE conversation SET title = ?2 WHERE id = ?1").run(c.value.id, title);
44 return c.value.id;
45 };
46 const empty = mk("Empty one");
47 const one = mk("Just one");
48 const many = mk("<b>Many</b>");
49 appendTurn(app.db, u.id, one, { role: "person", text: "hi" });
50 for (const [i, role] of ["person", "agent", "person"].entries()) appendTurn(app.db, u.id, many, { role, text: `m${i}` });
51 appendTurn(app.db, u.id, many, { role: "tool", text: "ls" });
52 const ago = new Date(Date.now() - 3 * 3600_000).toISOString();
53 app.db.query("UPDATE conversation_turn SET created_at = ?2 WHERE conversation_id = ?1").run(many, ago);
54 expect(empty).not.toBe(one);
55
56 const real = app.db.query.bind(app.db);
outdated

Restore db.query in a finally so a failed fetch does not leak the stub into other tests.

57 let counted = 0;
58 (app.db as unknown as { query: unknown }).query = (sql: string) => {
59 if (/conversation_turn/.test(sql) && /GROUP BY/.test(sql)) counted++;
60 return real(sql);
61 };
62 let res: Response;
63 try {
64 res = await fetch(`${servers.url}/talk`, { headers: { cookie: `forge_session=${createSession(app.db, u.id).token}` } });
65 } finally {
66 (app.db as unknown as { query: unknown }).query = real;
67 }
68 const page = await res.text();
69 const meta = [...page.matchAll(/<span class="row-meta">([\s\S]*?)<\/span>\s*<\/span>/g)].map((m) => m[1]!.replace(/<[^>]+>/g, "").replace(/\s+/g, " ").trim());
70 expect(counted).toBe(1);
71 expect(meta).toContain("no messages yet");
72 expect(meta).toContain("1 message · last just now");
73 expect(meta).toContain("3 messages · last 3h ago");
74 expect(page).toContain("&lt;b&gt;Many&lt;/b&gt;");
75 expect(page).not.toContain("<b>Many</b>");
76 });
77
78 test("the terminal list words the same three lines", async () => {
79 const { app: tui, session } = createForgeApp(
80 app,
81 { sshUsername: u.username, remoteAddress: "127.0.0.1", fingerprint: "SHA256:test", user: u },
82 { terminal: headlessTerminal(120, 40), exitHooks: false },
83 );
84 tui.start();
85 tui.ctx.push(talkChatsScreen(session, tui.ctx));
86 await settleApp(tui);
87 const text = tui.screen.toText();
88 expect(text).toContain("no messages yet");
89 expect(text).toContain("1 message · last just now");
90 expect(text).toContain("3 messages · last 3h ago");
91 expect(text).not.toContain("1 messages");
92 expect(text).not.toContain("active ");
93 tui.stop?.();
94 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs