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

A Talk conversation can be exported as Markdown from the terminal with the same text as the page's download #15885

Merged opened this

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

Merged

scout/forge:scout/15858-a-talk-conversation-can-be-exported-as-m → main 0 commits fork 2 threads · 2 unresolved

  • Merged. scout/15858-a-talk-conversation-can-be-exported-as-m was merged into main as f477464.

Review

Sign in to review this change.
3 files changed +108 −7
docs/features/talk.md +1 −1
@@ -146,4 +146,4 @@ After you confirm a held action, the thread shows a receipt: the verb, the objec
146146
147147 `GET /talk/:id.md` (also `/talk/:id/download`; the Download link beside Archive) returns the conversation as one `text/markdown` attachment: a title, then each turn under `## who · when` in the order written, attachments as links. Only its owner gets it; anyone else sees not found. The MCP `say` tool with `action: download` and `conversation` returns the same text as `markdown`. Code: `src/core/conversation_markdown.ts`; test: `test/conversation-download.test.ts`.
148148
149 The terminal's Talk screen reads the same file: `d` on the list or in a chat shows the conversation as the Markdown `/talk/:id/download` serves (`conversationMarkdown`), its owner's alone. Each turn's words sit in a fenced block, so no message can forge a turn heading; past a million characters the earliest turns are left out and the file says so. The MCP `say` tool's `download` with no `conversation` takes the latest one.
149 The terminal's Talk screen reads the same file: `d` on the list, or `d` or `x` in an open chat, shows the conversation as the Markdown `/talk/:id/download` serves (`conversationMarkdown`), its owner's alone. Each turn's words sit in a fenced block, so no message can forge a turn heading; past a million characters the earliest turns are left out and the file says so. The MCP `say` tool's `download` with no `conversation` takes the latest one.
src/tui/screens/talk_chats.ts +14 −6
@@ -4,9 +4,9 @@
44 * so a change here is the next thing a web viewer's list or stream reads.
55 * `a` flips between the live list and the archived one; Enter opens one,
66 * to read it and, with `s`, say something in it through `talk()`. `d` reads the conversation
7 * as the Markdown file /talk/:id/download serves (`conversationMarkdown`, its owner's alone).
8 * An empty conversation lists the page's starter prompts (#13317), numbered: 1, 2 or 3
9 * sends that one through the same `talk()`.
7 * as the Markdown file /talk/:id/download serves; in an open conversation `x` does the same
8 * (`conversationMarkdown`, its owner's alone). An empty conversation lists the page's starter
9 * prompts (#13317), numbered: 1, 2 or 3 sends that one through the same `talk()`.
1010 */
1111
1212 import { pathRefs, refTarget } from "../../text/talk_links.ts";
@@ -42,13 +42,21 @@ const BAD_TITLE = `A title is up to ${TITLE_MAX} characters, on one line.`;
4242
4343 const WHO: Readonly<Record<string, string>> = { person: "You", agent: "Forge", tool: "Tool", confirm: "Asks" };
4444
45 /**
46 * The person's conversation as the Markdown the page's /talk/:id/download serves; none when it is
47 * not theirs.
48 */
49 export function chatMarkdown(f: Session, chat: Conversation) {

This comment described downloadScreen; it now heads chatMarkdown. Move it back and write one for chatMarkdown.

50 return f.user === undefined ? undefined : conversationMarkdown(f.app.db, f.user.id, chat.id, publicOrigin(f.app.config.url, `http://localhost:${f.app.config.port}`));
51 }
52
4553 /**
4654 * The conversation as the Markdown download holds it, to read and scroll; the file itself is the
4755 * web's /talk/:id/download.
4856 */
4957 function downloadScreen(f: Session, ctx: Ctx, chat: Conversation): Screen {
5058 const scroll = new Scroll();
51 const made = f.user === undefined ? undefined : conversationMarkdown(f.app.db, f.user.id, chat.id, publicOrigin(f.app.config.url, `http://localhost:${f.app.config.port}`));
59 const made = chatMarkdown(f, chat);
5260 const text = made?.ok === true ? made.value.text : "Not a conversation of yours.";
5361 const head = made?.ok === true ? `${made.value.name}${made.value.truncated ? " · truncated" : ""} · the file is at /talk/${chat.id}/download` : "";
5462 let cache: { w: number; lines: { text: string; style: ReturnType<typeof style> }[][] } | undefined;
@@ -150,7 +158,7 @@ function chatScreen(f: Session, ctx: Ctx, chat: Conversation, hit?: string): Scr
150158 binding(["1", "2", "3"], "starter"),
151159 binding(["r"], "reload"),
152160 binding(["p"], "pane"),
153 binding(["d"], "download"),
161 binding(["d", "x"], "export as Markdown"),
154162 binding(["esc"], "back"),
155163 ],
156164 render(area, s) {
@@ -198,7 +206,7 @@ function chatScreen(f: Session, ctx: Ctx, chat: Conversation, hit?: string): Scr
198206 ctx.invalidate();
199207 return true;
200208 }
201 if (e.key === "d") {
209 if (e.key === "d" || e.key === "x") {
202210 ctx.push(downloadScreen(f, ctx, chat));
203211 return true;
204212 }
test/tui-talk-export.test.ts added +93
@@ -0,0 +1,93 @@
1 /**
2 * `x` in an open terminal Talk conversation exports it as Markdown (#15858): the text is the one
3 * behind the page's /talk/:id/download, and a conversation that is not yours exports nothing.
4 */
5 import { afterAll, beforeAll, expect, test } from "bun:test";
6 import { mkdtemp, rm } from "node:fs/promises";
7 import { tmpdir } from "node:os";
8 import { join } from "node:path";
9 import { createApp, type App } from "../src/app.ts";
10 import { appendTurn, createConversation, listConversations, renameConversation } from "../src/core/conversations.ts";
11 import { createToken } from "../src/core/tokens.ts";
12 import { createUser, type User } from "../src/core/users.ts";
13 import { serve, type Servers } from "../src/http/server.ts";
14 import { chatMarkdown } from "../src/tui/screens/talk_chats.ts";
15 import type { Session } from "../src/tui/session.ts";
16 import { createForgeApp } from "../src/tui/forge.ts";
17 import { headlessTerminal } from "../src/tui/term/terminal.ts";
18 import { quietLogs } from "./helpers/globals.ts";
19 import { settleApp } from "./helpers/tui.ts";
20
21 quietLogs();
22
23 let dir: string;
24 let app: App;
25 let servers: Servers;
26 let alice: User;
27 let bob: User;
28 let token: string;
29 let convo: string;
30
31 async function drive(user: User, keys: string[]): Promise<{ text: string; session: Session }> {
32 const terminal = headlessTerminal(120, 60);
33 const { app: tui, session } = createForgeApp(app, { sshUsername: user.username, remoteAddress: "127.0.0.1", fingerprint: "SHA256:test", user }, { terminal, exitHooks: false });
34 tui.start();
35 await settleApp(tui);
36 for (const k of keys) {
37 terminal.feed(k);
38 await settleApp(tui);
39 }
40 const text = tui.screen.toText();
41 tui.stop?.();
42 return { text, session };
43 }
44
45 beforeAll(async () => {
46 dir = await mkdtemp(join(tmpdir(), "forge-tui-export-"));
47 app = createApp({ dataDir: dir, port: 0, host: "forge.test" });
48 servers = await serve(app);
49 const mk = async (username: string, first = false) => {
50 const r = await createUser(app.db, { origin: "seed", trusted: true, username, email: `${username}@forge.local`, password: "a-good-passphrase", adminIfFirst: first });
51 if (!r.ok) throw new Error(r.error);
52 return r.value;
53 };
54 alice = await mk("alice", true);
55 bob = await mk("bob");
56 token = createToken(app.db, alice.id, "personal", "rw").token;
57 const c = createConversation(app.db, alice.id);
58 if (!c.ok) throw new Error("conversation");
59 convo = c.value.id;
60 renameConversation(app.db, alice.id, convo, "Export me");
61 appendTurn(app.db, alice.id, convo, { role: "person", text: "first question" });
62 appendTurn(app.db, alice.id, convo, { role: "agent", text: "second: an answer" });
63 });
64 afterAll(async () => {
65 servers.stop();
66 app.db.close();
67 await rm(dir, { recursive: true, force: true });
68 });
69
70 const open = [":", "Conversations", "\r", "\r"];
71
72 const chatOf = (user: User) => listConversations(app.db, user.id, {}).find((c) => c.id === convo);
73
outdated

as any on session and chat; use real types. The screen check compares trimmed lines only, not the body exactly.

74 test("the terminal export text is the page's download body, and x shows it line for line", async () => {
75 const page = await (await fetch(`${servers.url}/talk/${convo}/download`, { headers: { authorization: `Bearer ${token}` } })).text();
76 const { text, session } = await drive(alice, [...open, "x"]);
77 const chat = chatOf(alice);
78 if (chat === undefined) throw new Error("chat");
79 const made = chatMarkdown(session, chat);
80 expect(made?.ok === true ? made.value.text : undefined).toBe(page);
81 // The header lines come first, then the body unwrapped at this width.
82 const want = page.trimEnd().split("\n");
83 const all = text.split("\n").map((l) => l.trimEnd());
84 const shown = all.slice(all.indexOf(want[0]!));
85 expect(shown.slice(0, want.length)).toEqual(want);
86 });
87
88 test("another person's conversation exports nothing", async () => {
89 const { session } = await drive(bob, []);
90 const chat = chatOf(alice);
91 if (chat === undefined) throw new Error("chat");
92 expect(chatMarkdown(session, chat)?.ok).toBe(false);
93 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs