Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

feat(chat): copy a live selection from the transcript menu

vastsacb0709324f8b
diff --git a/apps/desktop/src/components/ContextMenu.tsx b/apps/desktop/src/components/ContextMenu.tsx
index 7362e2ca1..e6d2493c9 100644
--- a/apps/desktop/src/components/ContextMenu.tsx
+++ b/apps/desktop/src/components/ContextMenu.tsx
@@ -44,7 +44,7 @@ export type ContextMenuItem = {
danger?: boolean;
/** Hairline above this item, splitting the menu into groups. */
separatorBefore?: boolean;
- onSelect: () => void;
+ onSelect: (selection: string) => void;
};
export type ContextMenuRequest = {
@@ -60,6 +60,8 @@ export type ContextMenuRequest = {
export type ContextMenuState = ContextMenuRequest & {
/** Where the pointer asked for the surface. */
point: ContextMenuPoint;
+ /** Live selection at open time; empty when the caret was collapsed. */
+ selection: string;
};
/**
@@ -77,6 +79,26 @@ function pointForEvent(event: ReactMouseEvent<HTMLElement>): ContextMenuPoint {
};
}
+/**
+ * A selection in the composer or another row is not "this turn's excerpt".
+ * Anchor or focus inside the right-clicked node is enough: a range that
+ * starts in this row still belongs to Copy here.
+ */
+function snapshotSelection(root: EventTarget): string {
+ const live = window.getSelection();
+ if (!live || live.rangeCount === 0 || live.isCollapsed) return "";
+ const text = live.toString();
+ if (!text || !(root instanceof Node)) return "";
+ const { anchorNode, focusNode } = live;
+ if (
+ (anchorNode && root.contains(anchorNode)) ||
+ (focusNode && root.contains(focusNode))
+ ) {
+ return text;
+ }
+ return "";
+}
+
export function useContextMenu() {
const [state, setState] = useState<ContextMenuState | null>(null);
const closeContextMenu = useCallback(() => setState(null), []);
@@ -90,7 +112,14 @@ export function useContextMenu() {
if (!request.items.length) return;
event.preventDefault();
event.stopPropagation();
- setState({ ...request, point: pointForEvent(event) });
+ /*
+ Snapshot the live selection before the menu takes focus: focusing a
+ menuitem collapses the range, and Copy would then only see the whole
+ turn. A collapsed caret, or a selection that lives outside this
+ target, is stored as empty so Copy still falls back.
+ */
+ const selection = snapshotSelection(event.currentTarget);
+ setState({ ...request, point: pointForEvent(event), selection });
},
[],
);
@@ -275,7 +304,7 @@ export function ContextMenu({
// Close first: an item that opens a dialog must not leave a menu
// layered over it.
onClose();
- item.onSelect();
+ item.onSelect(state.selection);
}}
>
{item.icon ? (
diff --git a/apps/desktop/src/features/chat/transcript/TranscriptMenu.tsx b/apps/desktop/src/features/chat/transcript/TranscriptMenu.tsx
index 5297cab3d..169c28efd 100644
--- a/apps/desktop/src/features/chat/transcript/TranscriptMenu.tsx
+++ b/apps/desktop/src/features/chat/transcript/TranscriptMenu.tsx
@@ -27,6 +27,7 @@ import {
type ContextMenuRequest,
} from "../../../components/ContextMenu";
import { useAppStore } from "../../../stores/app-store";
+import { copySelectionOrFallback } from "../../../lib/chat-transcript-text";
export type OpenTranscriptMenu = (
event: ReactMouseEvent<HTMLElement>,
@@ -65,9 +66,11 @@ export function useChatTextActions() {
const showToast = useAppStore((state) => state.showToast);
const copyText = useCallback(
- async (text: string) => {
+ async (text: string, selection?: string) => {
+ const payload = copySelectionOrFallback(selection, text);
+ if (!payload) return;
try {
- await navigator.clipboard.writeText(text);
+ await navigator.clipboard.writeText(payload);
showToast(t("chat.copied"), { variant: "success" });
} catch {
showToast(t("chat.copyFailed"), { variant: "error" });
diff --git a/apps/desktop/src/features/chat/transcript/menu-items.tsx b/apps/desktop/src/features/chat/transcript/menu-items.tsx
index 89b440c3e..03c6e4ccb 100644
--- a/apps/desktop/src/features/chat/transcript/menu-items.tsx
+++ b/apps/desktop/src/features/chat/transcript/menu-items.tsx
@@ -29,7 +29,7 @@ import type { ContextMenuItem } from "../../../components/ContextMenu";
/** Actions the builders call; the components own their real implementations. */
export type MenuItemActions = {
- copyText: (text: string) => void;
+ copyText: (text: string, selection?: string) => void;
selectText: (element: HTMLElement | null) => void;
};
@@ -71,7 +71,7 @@ export function userMessageMenuItems({
id: "copy",
label: t("chat.copy"),
icon: copyIcon(),
- onSelect: () => actions.copyText(text),
+ onSelect: (selection) => actions.copyText(text, selection),
});
items.push({
id: "select-text",
@@ -153,7 +153,7 @@ export function assistantTurnMenuItems({
id: "copy",
label: t("chat.copy"),
icon: copyIcon(),
- onSelect: () => actions.copyText(answer),
+ onSelect: (selection) => actions.copyText(answer, selection),
});
items.push({
id: "select-text",
@@ -207,6 +207,7 @@ export function conversationMenuItems({
label: t("chat.copyConversation"),
icon: copyIcon(),
disabled: !conversation,
+ // The labelled thread is the action; a live selection belongs to Copy on a turn.
onSelect: () => actions.copyText(conversation),
},
{
diff --git a/apps/desktop/src/lib/chat-transcript-text.ts b/apps/desktop/src/lib/chat-transcript-text.ts
index 75d881411..3795a5fc4 100644
--- a/apps/desktop/src/lib/chat-transcript-text.ts
+++ b/apps/desktop/src/lib/chat-transcript-text.ts
@@ -40,3 +40,15 @@ export function conversationPlainText(
}
return blocks.join("\n\n");
}
+
+/**
+ * Copy prefers a live selection over the whole turn. A collapsed caret is not
+ * a selection, so it falls through to the fallback rather than writing "".
+ * The excerpt is copied as selected, including surrounding whitespace.
+ */
+export function copySelectionOrFallback(
+ selection: string | undefined,
+ fallback: string,
+): string {
+ return selection ? selection : fallback;
+}
diff --git a/apps/desktop/test/chat-context-menu-items.test.mjs b/apps/desktop/test/chat-context-menu-items.test.mjs
index 606494f1a..a9e0c199c 100644
--- a/apps/desktop/test/chat-context-menu-items.test.mjs
+++ b/apps/desktop/test/chat-context-menu-items.test.mjs
@@ -159,3 +159,43 @@ test("an empty conversation disables copy instead of writing a blank clipboard",
});
assert.equal(items.find((item) => item.id === "copy-conversation").disabled, true);
});
+
+test("copy on a speaking turn prefers the live selection", () => {
+ const copied = [];
+ const copyActions = {
+ copyText: (text, selection) => copied.push({ text, selection }),
+ selectText: noop,
+ };
+ const items = userMessageMenuItems({
+ t,
+ text: "Fix the crash",
+ selectTarget: null,
+ editable: true,
+ running: false,
+ revision: null,
+ actions: copyActions,
+ onEdit: noop,
+ onDelete: noop,
+ onActivateRevision: noop,
+ });
+ items.find((item) => item.id === "copy").onSelect("this line");
+ assert.deepEqual(copied, [{ text: "Fix the crash", selection: "this line" }]);
+});
+
+test("copy conversation ignores a live selection", () => {
+ const copied = [];
+ const copyActions = {
+ copyText: (text, selection) => copied.push({ text, selection }),
+ selectText: noop,
+ };
+ const items = conversationMenuItems({
+ t,
+ conversation: "You:\nHi",
+ scrollRef: { current: null },
+ contentRef: { current: null },
+ actions: copyActions,
+ onReturnToLatest: noop,
+ });
+ items.find((item) => item.id === "copy-conversation").onSelect("this line");
+ assert.deepEqual(copied, [{ text: "You:\nHi", selection: undefined }]);
+});
diff --git a/apps/desktop/test/chat-context-menu-surface.test.mjs b/apps/desktop/test/chat-context-menu-surface.test.mjs
index 2296e2ee6..418d1a517 100644
--- a/apps/desktop/test/chat-context-menu-surface.test.mjs
+++ b/apps/desktop/test/chat-context-menu-surface.test.mjs
@@ -30,6 +30,11 @@ test("the pointer-anchored menu portals, measures, and closes without trapping f
assert.match(menuSource, /window\.addEventListener\("keydown", onKeyDown, true\)/);
assert.match(menuSource, /if \(event\.key === "Tab"\)/);
assert.match(menuSource, /onContextMenu=\{/);
+ assert.match(menuSource, /function snapshotSelection\(/);
+ assert.match(menuSource, /live\.isCollapsed/);
+ assert.match(menuSource, /root\.contains\(anchorNode\)/);
+ assert.match(menuSource, /root\.contains\(focusNode\)/);
+ assert.match(menuSource, /item\.onSelect\(state\.selection\)/);
assert.match(menuSource, /if \(!request\.items\.length\) return;/);
});
diff --git a/apps/desktop/test/chat-context-menu.test.mjs b/apps/desktop/test/chat-context-menu.test.mjs
index 91e16f7cb..6d3fa36e7 100644
--- a/apps/desktop/test/chat-context-menu.test.mjs
+++ b/apps/desktop/test/chat-context-menu.test.mjs
@@ -1,7 +1,7 @@
import assert from "node:assert/strict";
import test from "node:test";
import { placeContextMenu } from "../src/lib/context-menu.ts";
-import { conversationPlainText } from "../src/lib/chat-transcript-text.ts";
+import { conversationPlainText, copySelectionOrFallback } from "../src/lib/chat-transcript-text.ts";
test("a menu that fits the viewport stays at the pointer", () => {
assert.deepEqual(
@@ -47,3 +47,9 @@ test("an empty conversation copies nothing so the action can stay disabled", ()
"",
);
});
+
+test("copy prefers a live selection over the whole turn", () => {
+ assert.equal(copySelectionOrFallback(" this line ", "whole message"), " this line ");
+ assert.equal(copySelectionOrFallback("", "whole message"), "whole message");
+ assert.equal(copySelectionOrFallback(undefined, "whole message"), "whole message");
+});
diff --git a/docs/spec/04-ux/08-component-spec.md b/docs/spec/04-ux/08-component-spec.md
index 0a0124d25..a531dc6cd 100644
--- a/docs/spec/04-ux/08-component-spec.md
+++ b/docs/spec/04-ux/08-component-spec.md
@@ -1676,8 +1676,12 @@ Single message render — either user (plaintext) or assistant (markdown streami
menu: Copy conversation, Select conversation text, Scroll to top, Jump to
latest. Quote, Annotate, and Open side chat stay retired (ADR 0268). A
streaming or empty assistant turn that would produce no items opens
- nothing. Copying from the menu reports through the toast host because the
- surface closes as soon as the item runs.
+ nothing. Copy on a speaking-turn menu writes the live selection in that
+ turn captured when the menu opened; a collapsed caret, or a selection
+ outside the row, falls back to the whole turn.
+ Copy conversation still writes the labelled thread. Copying from the
+ menu reports through the toast host because the surface closes as soon
+ as the item runs.
Fork creates and activates an independent session whose snapshot ends at the
selected assistant response, requires an idle source, and leaves that
source's transcript, live runtime, and provider cache state untouched (D134).
diff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.md
index 91e1e3506..37c6cff64 100644
--- a/docs/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/spec/06-delivery/04-e2e-test-plan.md
@@ -3427,15 +3427,20 @@ identify the platform validation still needed.
- **Preconditions**: A session contains a completed user prompt and a
completed assistant answer; the conversation pane is focused.
- **Steps**: 1) Right-click the user plate. 2) Choose Copy, then Select
- message text. 3) Right-click the assistant turn and choose Copy.
- 4) Right-click empty space below the last turn and choose Copy
- conversation. 5) Press Escape on an open menu, then Tab. 6) Right-click
+ message text. 3) Select a phrase in the user plate, right-click that
+ plate, and choose Copy; collapse the caret, right-click again, and
+ choose Copy. 4) Right-click the assistant turn and choose Copy.
+ 5) Right-click empty space below the last turn and choose Copy
+ conversation. 6) Press Escape on an open menu, then Tab. 7) Right-click
a markdown link in the answer.
- **Expected**: The user menu lists Copy, Select message text, Edit, and
a separated Delete; the assistant menu lists Copy, Select message text,
- Regenerate, and Branch. Copy writes the message or the labelled
- conversation to the clipboard and shows a toast. Select text highlights
- the bubble. Escape and Tab dismiss the menu without running an item.
+ Regenerate, and Branch. Copy writes the live selection in the
+ right-clicked turn when the menu opened over one; a collapsed caret
+ or a selection outside that row falls back to the whole turn. Copy
+ conversation writes the labelled thread. Both show a toast. Select
+ text highlights the bubble. Escape and Tab dismiss the menu without
+ running an item.
A link still offers Open in default browser, Open in work panel, and
Copy link address. Quote, Annotate, and Open side chat are absent
(ADR 0268). The surface is a viewport-fixed body-level layer and does
diff --git a/docs/zh-CN/spec/04-ux/08-component-spec.md b/docs/zh-CN/spec/04-ux/08-component-spec.md
index 1726219a8..19ce86f2c 100644
--- a/docs/zh-CN/spec/04-ux/08-component-spec.md
+++ b/docs/zh-CN/spec/04-ux/08-component-spec.md
@@ -1192,7 +1192,7 @@ row 而不是在转录本中添加树镶边。
Retry/Regenerate 启动,root 用户轮流保留在实时记录中
tail 不得将该寻呼机从用户气泡中移动或分离。寻呼机是
消息操作工具栏的一部分:默认隐藏,并与复制一起在行悬停或键盘聚焦时显示。
- 右键用户消息或助手回合会打开与悬停操作相同的词汇(复制、选中文本,以及该行自己的编辑 / 删除 / 重新生成 / 分叉 / 版本项),作为文档级、指针锚定的菜单。右键空白记录、系统行、权限卡或结果卡会打开对话菜单:复制整个对话、选中对话文本、滚动到顶部、跳到最新。引用、批注、打开侧边聊天仍按 ADR 0268 退役。流式或空助手回合若没有可提供的项则不打开菜单。菜单项的复制结果走 toast,因为表面在项执行时就会关闭。
+ 右键用户消息或助手回合会打开与悬停操作相同的词汇(复制、选中文本,以及该行自己的编辑 / 删除 / 重新生成 / 分叉 / 版本项),作为文档级、指针锚定的菜单。右键空白记录、系统行、权限卡或结果卡会打开对话菜单:复制整个对话、选中对话文本、滚动到顶部、跳到最新。引用、批注、打开侧边聊天仍按 ADR 0268 退役。流式或空助手回合若没有可提供的项则不打开菜单。说话回合上的「复制」写入打开菜单时该行内的选中文本;光标折叠或选区在行外时退回整段。复制整个对话仍写入带说话人标签的整段对话。菜单项的复制结果走 toast,因为表面在项执行时就会关闭。
Fork 创建并激活一个独立的会话,其快照结束于
选择辅助响应,需要一个空闲源,然后留下
源的转录本、实时运行时和提供程序缓存状态保持不变 (D134)。
diff --git a/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md b/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md
index 58371e5e8..d4f5b3be8 100644
--- a/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md
@@ -2195,8 +2195,8 @@ MainChat 弥补了缺口。 Maximized/fullscreen 调用保留最新的
#### E2E-CHAT-transcript-context-menu:右键消息或记录
- **前提条件**:会话中有一条完成的用户提示和一条完成的助手回答;对话面板已聚焦。
-- **步骤**:1)右键用户板。2)选择复制,再选择选中消息文本。3)右键助手回合并选择复制。4)右键最后一回合下方空白处,选择复制整个对话。5)在打开的菜单上按 Escape,再按 Tab。6)右键回答里的 markdown 链接。
-- **预期**:用户菜单列出复制、选中消息文本、编辑,以及分隔后的删除;助手菜单列出复制、选中消息文本、重新生成和分叉。复制把该消息或带说话人标签的整段对话写入剪贴板并显示 toast。选中文本会高亮气泡。Escape 和 Tab 关闭菜单且不执行项。链接仍提供在默认浏览器打开、在工作面板打开、复制链接地址。引用、批注、打开侧边聊天不出现(ADR 0268)。表面是视口固定的文档级层,不会改变记录高度。
+- **步骤**:1)右键用户板。2)选择复制,再选择选中消息文本。3)在用户板里选中一段文字,再右键该板并选择复制;收起选区后再右键复制一次。4)右键助手回合并选择复制。5)右键最后一回合下方空白处,选择复制整个对话。6)在打开的菜单上按 Escape,再按 Tab。7)右键回答里的 markdown 链接。
+- **预期**:用户菜单列出复制、选中消息文本、编辑,以及分隔后的删除;助手菜单列出复制、选中消息文本、重新生成和分叉。复制写入打开菜单时该行内的选中文本;光标折叠或选区在行外时退回整段。复制整个对话仍写入带说话人标签的整段对话。两者都显示 toast。选中文本会高亮气泡。Escape 和 Tab 关闭菜单且不执行项。链接仍提供在默认浏览器打开、在工作面板打开、复制链接地址。引用、批注、打开侧边聊天不出现(ADR 0268)。表面是视口固定的文档级层,不会改变记录高度。
- **链接规格**:`04-ux/08-component-spec.md` §8.3 / §8.5,
`04-ux/09-interaction-patterns.md`(浮动下拉表面),
ADR 0268