From 70a8e12ff1c5053a5963416423013607a78903b1 Mon Sep 17 00:00:00 2001 From: Colt Coan <59135406+promptclickrun@users.noreply.github.com> Date: Sun, 29 Nov 2026 20:21:01 -0600 Subject: [PATCH] Add card save-as-image and multi-format Copy message menu Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- desktop/desktop/main.mjs | 41 + desktop/desktop/preload.cjs | 2 + desktop/desktop/remotehosts.mjs | 2 + desktop/src/A2uiSurface.tsx | 7 +- desktop/src/ChatActivity.tsx | 12 +- desktop/src/ChatView.tsx | 123 +- desktop/src/McpAppCard.tsx | 7 +- desktop/src/MessageDrawer.tsx | 128 +- desktop/src/WorkflowActionCard.tsx | 4 +- desktop/src/messageExport.ts | 1110 +++++++++++++++++ desktop/src/messageMenu.ts | 7 + desktop/src/messagedrawer.css | 63 +- desktop/src/types.ts | 8 + desktop/test/message-export.test.mjs | 71 ++ desktop/test/message-menu.test.mjs | 17 + .../botkins-export/android/build.gradle | 13 + .../android/src/main/AndroidManifest.xml | 1 + .../app/botkins/export/BotkinsExportModule.kt | 548 ++++++++ .../botkins-export/expo-module.config.json | 9 + mobile/modules/botkins-export/index.ts | 33 + .../botkins-export/ios/BotkinsExport.podspec | 19 + .../ios/BotkinsExportModule.swift | 577 +++++++++ mobile/modules/botkins-export/package.json | 6 + mobile/src/features/chat/MessageSheet.tsx | 54 +- mobile/src/features/chat/Messages.tsx | 178 ++- mobile/src/features/chat/messageExport.ts | 1110 +++++++++++++++++ mobile/src/features/chat/messageMenu.ts | 8 + mobile/src/ui/List.tsx | 18 +- mobile/tests/message-export.test.ts | 146 +++ mobile/tests/message-menu.test.ts | 21 + 30 files changed, 4286 insertions(+), 57 deletions(-) create mode 100644 desktop/src/messageExport.ts create mode 100644 desktop/test/message-export.test.mjs create mode 100644 mobile/modules/botkins-export/android/build.gradle create mode 100644 mobile/modules/botkins-export/android/src/main/AndroidManifest.xml create mode 100644 mobile/modules/botkins-export/android/src/main/java/app/botkins/export/BotkinsExportModule.kt create mode 100644 mobile/modules/botkins-export/expo-module.config.json create mode 100644 mobile/modules/botkins-export/index.ts create mode 100644 mobile/modules/botkins-export/ios/BotkinsExport.podspec create mode 100644 mobile/modules/botkins-export/ios/BotkinsExportModule.swift create mode 100644 mobile/modules/botkins-export/package.json create mode 100644 mobile/src/features/chat/messageExport.ts create mode 100644 mobile/tests/message-export.test.ts diff --git a/desktop/desktop/main.mjs b/desktop/desktop/main.mjs index d8ee70ea..6eb5d674 100644 --- a/desktop/desktop/main.mjs +++ b/desktop/desktop/main.mjs @@ -9,6 +9,7 @@ import { nativeTheme, session, } from "electron"; +import { writeFile } from "node:fs/promises"; import { fileURLToPath, pathToFileURL } from "node:url"; import { join } from "node:path"; import { chunk, fileDetails, publicFile } from "./artifact-files.mjs"; @@ -568,6 +569,46 @@ else installFace: faces.install, removeFace: faces.remove, purgeFaces: faces.purge, + saveCardImage: async (rect, suggestedName) => { + if (!win || win.isDestroyed()) throw new Error("Window is unavailable."); + const x = Math.max(0, Math.round(Number(rect?.x ?? 0))); + const y = Math.max(0, Math.round(Number(rect?.y ?? 0))); + const width = Math.max(1, Math.round(Number(rect?.width ?? 0))); + const height = Math.max(1, Math.round(Number(rect?.height ?? 0))); + const image = await win.webContents.capturePage({ x, y, width, height }); + const png = image.toPNG(); + const slug = String(suggestedName ?? "") + .trim() + .toLowerCase() + .replace(/[^a-z0-9]+/g, "-") + .replace(/^-+|-+$/g, "") + .slice(0, 64) || "card"; + const filePath = await host.dialogs.save({ + title: "Save card as image", + defaultPath: join(host.paths.documents(), `${slug}.png`), + filters: [{ name: "PNG Image", extensions: ["png"] }], + }); + if (!filePath) return { saved: false }; + await writeFile(filePath, png); + return { saved: true, path: filePath }; + }, + saveMessagePdf: async (htmlContent, title) => { + const pdf = await host.pdf(String(htmlContent ?? "")); + const slug = String(title ?? "") + .trim() + .toLowerCase() + .replace(/[^a-z0-9]+/g, "-") + .replace(/^-+|-+$/g, "") + .slice(0, 64) || "message"; + const filePath = await host.dialogs.save({ + title: "Save message as PDF", + defaultPath: join(host.paths.documents(), `${slug}.pdf`), + filters: [{ name: "PDF Document", extensions: ["pdf"] }], + }); + if (!filePath) return { saved: false }; + await writeFile(filePath, pdf); + return { saved: true, path: filePath }; + }, "link.pullRequests": (where, query) => runtime.link.prs.lookup(where, query), "link.pullRequest": (input) => runtime.link.prs.panel(input), "link.pullRequestDiff": (input) => runtime.link.prs.panel(input, true), diff --git a/desktop/desktop/preload.cjs b/desktop/desktop/preload.cjs index 5910b378..eb434615 100644 --- a/desktop/desktop/preload.cjs +++ b/desktop/desktop/preload.cjs @@ -431,6 +431,8 @@ for (const name of [ "retry", "compact", "copyText", + "saveCardImage", + "saveMessagePdf", "auth", "signIn", "cancelSignIn", diff --git a/desktop/desktop/remotehosts.mjs b/desktop/desktop/remotehosts.mjs index c382202a..2bbe6e83 100644 --- a/desktop/desktop/remotehosts.mjs +++ b/desktop/desktop/remotehosts.mjs @@ -46,6 +46,8 @@ export const HERE = new Set([ "viewing", "openLink", "copyText", + "saveCardImage", + "saveMessagePdf", "showEmojiPanel", "artifactMessagePage", "artifactMedia", diff --git a/desktop/src/A2uiSurface.tsx b/desktop/src/A2uiSurface.tsx index 6ef5c1c3..aa710ab6 100644 --- a/desktop/src/A2uiSurface.tsx +++ b/desktop/src/A2uiSurface.tsx @@ -1290,7 +1290,12 @@ export default function A2uiSurface({ } return ( -
+
{renderNode("root", { scopePath: "" }, 0)} {surface.state === "dispatched" && (
diff --git a/desktop/src/ChatActivity.tsx b/desktop/src/ChatActivity.tsx index c419823d..cd242484 100644 --- a/desktop/src/ChatActivity.tsx +++ b/desktop/src/ChatActivity.tsx @@ -1099,15 +1099,17 @@ export function RunCards({ run, prefill, pickModel, + onContextMenu, }: { run: Run; prefill: (s: string) => void; pickModel?: () => void; + onContextMenu?: (e: React.MouseEvent) => void; }) { return ( - <> +
{run.apps?.map((app) => ( -
+
@@ -1122,7 +1124,7 @@ export function RunCards({
))} {run.claudeArtifacts?.map((artifact) => ( -
+
@@ -1148,7 +1150,7 @@ export function RunCards({ ))} {run.cards?.map((card) => ( -
+

{card.title}

{card.blocks.map((b, i) => ( @@ -1180,6 +1182,6 @@ export function RunCards({ Choose chat model )} - +
); } diff --git a/desktop/src/ChatView.tsx b/desktop/src/ChatView.tsx index 95cf918e..9b5de855 100644 --- a/desktop/src/ChatView.tsx +++ b/desktop/src/ChatView.tsx @@ -70,6 +70,13 @@ import { thoughtParts, type IslandUpdate } from "./island"; import { clearReadingError, readAloud, useReading } from "./voice"; import MessageDrawer from "./MessageDrawer"; import type { MessageMenuTarget } from "./messageMenu"; +import { + formatAsPlainText, + formatForIMessage, + formatMessageHtmlForPdf, + shareableMarkdown, + type CopyFormat, +} from "./messageExport"; import { keptUnread } from "./unread"; import ContextDetails from "./ContextDetails"; import { BotAvatar, activityLabel, botActivity, chatActivity, useOutcome } from "./Avatar"; @@ -314,7 +321,11 @@ function ChatViewBody({ const [drawer, setDrawer] = useState<"" | "tasks">(""); const screenMenu = useMenu(); // The message whose drawer is open (MessageDrawer.tsx). - const [menu, setMenu] = useState<{ index: number; message: Message }>(); + const [menu, setMenu] = useState<{ + index: number; + message: Message; + cardCapture?: { el: HTMLElement; rect: { x: number; y: number; width: number; height: number }; title?: string }; + }>(); const [notice, setNotice] = useState(""); const [showChanges, setShowChanges] = useState(false); const [choosingProject, setChoosingProject] = useState(false); @@ -696,6 +707,38 @@ function ChatViewBody({ .copyText(m.text) .then(() => setNotice("Copied")) .catch((e) => setNotice(e.message)), + copyFormat: (m: Message, format: CopyFormat) => { + const text = m.text ?? ""; + if (format === "markdown") { + void window.botkins + .copyText(shareableMarkdown(text)) + .then(() => setNotice("Copied Markdown")) + .catch((e) => setNotice(e.message)); + return; + } + if (format === "imessage") { + void window.botkins + .copyText(formatForIMessage(text)) + .then(() => setNotice("Copied for iMessage")) + .catch((e) => setNotice(e.message)); + return; + } + if (format === "txt") { + void window.botkins + .copyText(formatAsPlainText(text)) + .then(() => setNotice("Copied plain text")) + .catch((e) => setNotice(e.message)); + return; + } + const title = chat.title && chat.title !== "New chat" ? chat.title : nameOf(m); + const html = formatMessageHtmlForPdf(text, title); + void window.botkins + .saveMessagePdf(html, title) + .then((res) => { + if (res?.saved) setNotice("Saved PDF"); + }) + .catch((e) => setNotice(e instanceof Error ? e.message : String(e))); + }, reply: (m: Message) => { setQuote(m.text.trim().slice(0, 1200)); focusComposer(); @@ -705,6 +748,20 @@ function ChatViewBody({ : forkThread(i) ).catch(e => setNotice(e.message)), }; + const saveCard = async (capture: { el: HTMLElement; rect: { x: number; y: number; width: number; height: number }; title?: string }) => { + // Wait two frames so the drawer scrim unmounts before Electron captures the page region. + await new Promise((resolve) => requestAnimationFrame(() => requestAnimationFrame(() => resolve()))); + const liveRect = capture.el.isConnected ? capture.el.getBoundingClientRect() : null; + const rect = liveRect && liveRect.width >= 2 && liveRect.height >= 2 + ? { x: liveRect.left, y: liveRect.top, width: liveRect.width, height: liveRect.height } + : capture.rect; + try { + const res = await window.botkins.saveCardImage(rect, capture.title); + if (res?.saved) setNotice("Saved card image"); + } catch (e) { + setNotice(e instanceof Error ? e.message : String(e)); + } + }; // What a message's drawer offers (MessageDrawer.tsx, messageMenu.ts). A reaction is sent at once. const reactionsOn = !state.remote?.using || state.remote.using.capabilities.includes("message-reactions-v1"); const me = state.team && "me" in state.team ? "member:" + state.team.me.memberId : "owner"; @@ -721,17 +778,21 @@ function ChatViewBody({ setNotice(error instanceof Error ? error.message : String(error)); } }; - const drawerFor = (m: Message, index: number): MessageMenuTarget => ({ - mine: m.reactions?.find((r) => r.actor === me)?.emoji, + const drawerFor = ( + m: Message, + index: number, + cardCapture?: { el: HTMLElement; rect: { x: number; y: number; width: number; height: number }; title?: string }, + ): MessageMenuTarget => ({ + mine: index >= 0 ? m.reactions?.find((r) => r.actor === me)?.emoji : undefined, time: m.time, pinned: !!chat.pinned, - reading: reading.key === readKey(index), + reading: index >= 0 && reading.key === readKey(index), actions: { - react: reactionsOn ? (emoji) => void reactNow(index, m.time ?? null, emoji) : undefined, - reply: () => act.reply(m), - fork: group ? undefined : () => act.fork(index), + react: index >= 0 && reactionsOn ? (emoji) => void reactNow(index, m.time ?? null, emoji) : undefined, + reply: m.text ? () => act.reply(m) : undefined, + fork: index >= 0 && !group ? () => act.fork(index) : undefined, forkLabel, - newChatFork: ownMain ? () => void forkThread(index).catch((e) => setNotice(e.message)) : undefined, + newChatFork: index >= 0 && ownMain ? () => void forkThread(index).catch((e) => setNotice(e.message)) : undefined, busy, // The chat stays unread while it is open (it would be read again at once); opening another chat ends that. markUnread: () => { @@ -740,10 +801,12 @@ function ChatViewBody({ }, pin: () => void window.botkins.setChatFlags(chat.id, { pinned: !chat.pinned }).then(() => setNotice(chat.pinned ? "Unpinned" : "Pinned")).catch((e) => setNotice(e.message)), copy: () => act.copy(m), - read: m.role !== "user" && m.text ? () => void readAloud(readKey(index), m.text, m.botId ?? bot.id) : undefined, - stop: () => void readAloud(readKey(index), m.text, m.botId ?? bot.id), + copyFormat: m.text ? (format) => act.copyFormat(m, format) : undefined, + saveCardImage: cardCapture ? () => void saveCard(cardCapture) : undefined, + read: index >= 0 && m.role !== "user" && m.text ? () => void readAloud(readKey(index), m.text, m.botId ?? bot.id) : undefined, + stop: index >= 0 ? () => void readAloud(readKey(index), m.text, m.botId ?? bot.id) : undefined, // The words become the browser's own selection, so they can be copied in part. - select: m.text + select: index >= 0 && m.text ? () => { const text = document.getElementById(`message-${chat.id}-${index}`)?.querySelector(".md, .command-output") ?? document.getElementById(`message-${chat.id}-${index}`); if (text) window.getSelection()?.selectAllChildren(text); @@ -846,10 +909,21 @@ function ChatViewBody({ } const timelineBusy = latest?.status === "Working"; + function cardCaptureFromEvent(e: React.MouseEvent) { + const el = (e.target as HTMLElement | null)?.closest("[data-card-capture]"); + if (!el) return undefined; + const r = el.getBoundingClientRect(); + if (r.width < 2 || r.height < 2) return undefined; + return { + el, + rect: { x: r.left, y: r.top, width: r.width, height: r.height }, + title: el.getAttribute("data-card-title") || undefined, + }; + } function messageMenu(e: React.MouseEvent, message: Message, index: number) { if (window.getSelection()?.toString()) return; e.preventDefault(); - setMenu({ message, index }); + setMenu({ message, index, cardCapture: cardCaptureFromEvent(e) }); } const accessibleChatIds = useMemo(() => new Set(state.chats.map((c) => c.id)), [state.chats]); const timelineMessage = (m: Message, index: number) => @@ -1083,7 +1157,28 @@ function ChatViewBody({ ) : !live && group && (folded || hasActivity) && fold} {group && !live && crewRunning && crewCard} - + { + if (window.getSelection()?.toString()) return; + const cardCapture = cardCaptureFromEvent(e); + if (!cardCapture) return; + e.preventDefault(); + const lastReply = reply.at(-1); + const fallbackMessage: Message = { + role: "assistant", + text: cardCapture.title || "", + time: run.time, + }; + setMenu({ + message: lastReply?.message ?? fallbackMessage, + index: lastReply ? loadedFrom + lastReply.index : -1, + cardCapture, + }); + }} + /> {!live && (failed.includes(run.status) || !!groupFailure) && (
{groupFailure || (run.ending === "timed-out" ? "Timed out" : run.status) + " ยท " + (run.detail || "Reply stopped.")}{!groupFailure && run.id === lastUserRun && !busy && }
)} @@ -1665,7 +1760,7 @@ function ChatViewBody({ )} {composer}
- {menu && setMenu(undefined)} />} + {menu && setMenu(undefined)} />}
{paneShown && narrow &&