Skip to content

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

fix: 截图粘贴时跳过原生目录扫描等待

geng yue0271ec35d443
diff --git a/apps/desktop/src/features/chat/composer/hooks/useComposerAttachments.ts b/apps/desktop/src/features/chat/composer/hooks/useComposerAttachments.ts
index 29ed1f8f2..038d9f251 100644
--- a/apps/desktop/src/features/chat/composer/hooks/useComposerAttachments.ts
+++ b/apps/desktop/src/features/chat/composer/hooks/useComposerAttachments.ts
@@ -234,6 +234,9 @@ export function useComposerAttachments({
: pastedFiles;
const hasNativeFileTypes = event.clipboardData.types?.includes("Files");
const nativeFilePaste = !preferText && (hasNativeFileTypes || !text);
+ const screenshotPaste = files.length > 0 && files.every(file =>
+ file.type.toLowerCase().startsWith("image/") && !api.getDroppedFilePath(file),
+ );
const textLength = Array.from(text).length;
const isLargeTextPaste = !files.length && textLength > largePasteThreshold;
if (isLargeTextPaste || files.length || nativeFilePaste) {
@@ -248,7 +251,8 @@ export function useComposerAttachments({
const sourceRevision = readComposerDraftRevision(sourceDraftKey);
setPasting(true);
try {
- const native = !isLargeTextPaste ? await api.readClipboardDirectories() : null;
+ // 合成截图已有字节,无需启动系统目录探测进程;磁盘文件仍走原路径。
+ const native = !isLargeTextPaste && !screenshotPaste ? await api.readClipboardDirectories() : null;
if (!native && !files.length && !isLargeTextPaste) {
if (hasNativeFileTypes) throw new Error(t("chat.directoryPasteUnavailable"));
return;
diff --git a/apps/desktop/test/composer-directory-paste.test.mjs b/apps/desktop/test/composer-directory-paste.test.mjs
index 717f14e0b..6f9a605e1 100644
--- a/apps/desktop/test/composer-directory-paste.test.mjs
+++ b/apps/desktop/test/composer-directory-paste.test.mjs
@@ -174,3 +174,13 @@ test("Word text plus a generated image keeps text paste without a native file re
assert.deepEqual(run.nativeReads, []);
assert.deepEqual(run.textInsertions, ["Copied text from Word"]);
});
+
+test("screenshot-only paste transfers image bytes without waiting for native directory discovery", async () => {
+ const image = new File(["screenshot bytes"], "image.png", { type: "image/png" });
+ const run = harness(null, { files: [image] });
+ await run.paste();
+ assert.deepEqual(run.errors, []);
+ assert.deepEqual(run.nativeReads, []);
+ assert.equal(run.transfers.length, 1);
+ assert.equal(new TextDecoder().decode(run.transfers[0][0].data), "screenshot bytes");
+});
diff --git a/docs/spec/04-ux/08-component-spec.md b/docs/spec/04-ux/08-component-spec.md
index 184989401..7691bf74d 100644
--- a/docs/spec/04-ux/08-component-spec.md
+++ b/docs/spec/04-ux/08-component-spec.md
@@ -1,5 +1,11 @@
# 08. Component Spec
+Screenshot-only Composer paste (image bytes without a native disk path) skips
+native directory discovery and uses the existing bounded attachment writer.
+Native files and folder-only or mixed file/folder selections keep the directory
+metadata path. This removes the system-process wait from screenshot paste
+(Refs #2086, #1366), without copying directory trees.
+
### Copied directory references
Native folder paste adds compact cards in a wrapping row above the editable
diff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.md
index 6b73f1bfb..da2b3e9de 100644
--- a/docs/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/spec/06-delivery/04-e2e-test-plan.md
@@ -1,5 +1,15 @@
# 04. E2E Test Plan
+### E2E-COMPOSER-screenshot-without-directory-scan
+
+Paste a native folder, then a synthetic screenshot through real Composer,
+preload IPC and the bounded scratch writer in `e2e-composer-paste.mjs`.
+Run with `--screenshot-only` to verify the folder and screenshot route while
+excluding unrelated MP4 preview. Screenshot metadata never enters native
+directory discovery; scratch bytes are verified exactly in Main.
+`composer-directory-paste.test.mjs` verifies byte preservation and existing
+folder/mixed-selection behavior. No system clipboard contents are changed.
+
## E2E-SCHEDULED-independent-loading-and-route-return
- **Preconditions:** Synthetic tasks; controllable task, latest-run, history and
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 07b6adced..9c130a38c 100644
--- a/docs/zh-CN/spec/04-ux/08-component-spec.md
+++ b/docs/zh-CN/spec/04-ux/08-component-spec.md
@@ -1,5 +1,9 @@
# 08. 组件规格
+输入框粘贴只有截图字节且没有原生磁盘路径时,跳过系统目录探测,直接使用
+现有有界附件写入流程。磁盘文件、纯目录及混合文件/目录选择保留目录元数据
+路径,去除截图粘贴时的系统进程等待(Refs #2086、#1366),不复制目录树。
+
> **翻译说明:** 本页是与 [英文源规格](/spec/04-ux/08-component-spec) 一一对应的机器辅助翻译。代码、协议字段和标识符保持原文;如翻译与英文源事实有歧义,以英文版本为准。
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 d5ea19d98..2c228edf3 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
@@ -1,5 +1,13 @@
# 04. E2E 测试 Plan
+### E2E-COMPOSER-screenshot-without-directory-scan
+
+`e2e-composer-paste.mjs` 使用真实 Composer、preload IPC 和有界 scratch 写入,
+先粘贴原生目录,再粘贴合成截图。使用 `--screenshot-only` 验证这条路径,
+跳过无关的 MP4 预览;截图不进入原生目录探测,Main 精确校验 scratch 字节。
+`composer-directory-paste.test.mjs` 验证字节不变及原有目录/混合选择流程。
+不修改系统剪贴板内容。
+
## E2E-SCHEDULED-independent-loading-and-route-return
- **Preconditions:** Synthetic tasks; controllable task, latest-run, history and
diff --git a/scripts/e2e-composer-paste.mjs b/scripts/e2e-composer-paste.mjs
index 323d56b08..aeb48949f 100644
--- a/scripts/e2e-composer-paste.mjs
+++ b/scripts/e2e-composer-paste.mjs
@@ -106,7 +106,8 @@ ipcMain.handle("pi-desktop/composer/readClipboardDirectories", async () => {
};
const data = await readComposerClipboardDirectories(clipboard, () => {
throw new Error("a folder-only paste must not create an import token");
- }, () => nativeClipboardPaths(clipboard, "linux"));
+ }, () => nativeClipboardPaths(clipboard, process.platform === "win32" ? "win32" : "linux",
+ () => [path.join(__dirname, "copied folder")]));
return { ok: true, data };
});
ipcMain.handle("pi-desktop/composer/pasteFiles", async (_event, input) => {
@@ -298,8 +299,16 @@ app.whenReady().then(async () => {
await window.webContents.executeJavaScript("globalThis.composerPreviewPointerDone()");
});
await window.webContents.executeJavaScript('globalThis.composerPreviewPointer = input => new Promise(resolve => { globalThis.composerPreviewPointerDone = resolve; console.log("PI_PREVIEW_POINTER:" + JSON.stringify(input)); }); void 0');
- const result = await window.webContents.executeJavaScript("globalThis.composerPasteProbe()");
+ const result = await window.webContents.executeJavaScript("globalThis.composerPasteProbe(" + process.argv.includes("--screenshot-only") + ")");
assert.equal(result.ok, true, JSON.stringify(result));
+ if (process.argv.includes("--screenshot-only")) {
+ assert.equal(saved.length, 1);
+ assert.deepEqual(saved[0].mimeTypes, ["image/png"]);
+ assert.equal(revealedFolders.length, 1);
+ console.log("COMPOSER_PASTE_PROBE " + JSON.stringify({ ...result, scratchBytesVerified: true }));
+ app.quit();
+ return;
+ }
await opensDone;
assert.deepEqual(revealedFolders, [{ path: path.join(__dirname, "copied folder"), sessionId: "paste-a", directory: true }]);
assert.deepEqual(opened.map(path.extname), [".mp4", ".mp4", ".mp4"]);
@@ -366,7 +375,8 @@ app.whenReady().then(async () => {
assert.equal(draftCases.length, 4, draftRace.output.slice(-6000));
console.log(draftCases.join("\n"));
- const firstRun = await runElectron();
+ const screenshotOnly = process.argv.includes("--screenshot-only");
+ const firstRun = await runElectron(screenshotOnly ? ["--screenshot-only"] : []);
const pasteLine = firstRun.output
.split(/\r?\n/)
.find((line) => line.startsWith("COMPOSER_PASTE_PROBE "));
@@ -379,18 +389,22 @@ app.whenReady().then(async () => {
assert.equal(firstRun.code, 0, firstRun.output.slice(-4000));
assert.equal(pasteResult.ok, true);
- const restartRun = await runElectron(["--history-check"]);
- const restartLine = restartRun.output
- .split(/\r?\n/)
- .find((line) => line.startsWith("COMPOSER_HISTORY_RESTART "));
- assert(
- restartLine,
- `renderer returned no restart result (exit=${restartRun.code}): ${restartRun.output.slice(-3000)}`,
- );
- const restartResult = JSON.parse(restartLine.slice("COMPOSER_HISTORY_RESTART ".length));
- console.log("COMPOSER_HISTORY_RESTART " + JSON.stringify(restartResult));
- assert.equal(restartRun.code, 0, restartRun.output.slice(-4000));
- assert.equal(restartResult.ok, true);
+ if (screenshotOnly) {
+ assert.equal(pasteResult.screenshotWithoutDirectoryScan, true);
+ } else {
+ const restartRun = await runElectron(["--history-check"]);
+ const restartLine = restartRun.output
+ .split(/\r?\n/)
+ .find((line) => line.startsWith("COMPOSER_HISTORY_RESTART "));
+ assert(
+ restartLine,
+ `renderer returned no restart result (exit=${restartRun.code}): ${restartRun.output.slice(-3000)}`,
+ );
+ const restartResult = JSON.parse(restartLine.slice("COMPOSER_HISTORY_RESTART ".length));
+ console.log("COMPOSER_HISTORY_RESTART " + JSON.stringify(restartResult));
+ assert.equal(restartRun.code, 0, restartRun.output.slice(-4000));
+ assert.equal(restartResult.ok, true);
+ }
} finally {
await rm(temp, { recursive: true, force: true });
}
diff --git a/scripts/e2e/composer-paste.tsx b/scripts/e2e/composer-paste.tsx
index c50fd2642..38a369af1 100644
--- a/scripts/e2e/composer-paste.tsx
+++ b/scripts/e2e/composer-paste.tsx
@@ -157,7 +157,7 @@ function Mp4AttachmentChips({ scratchPath }: { scratchPath: string }) {
);
}
-globalThis.composerPasteProbe = async () => {
+globalThis.composerPasteProbe = async (screenshotOnly = false) => {
const i18n = createInstance();
await i18n.init({
lng: "en",
@@ -346,6 +346,23 @@ globalThis.composerPasteProbe = async () => {
!api.getDroppedFilePath(image),
"synthetic clipboard image unexpectedly has a native path",
);
+ if (screenshotOnly) {
+ const readDirectories = api.readClipboardDirectories;
+ let directoryScans = 0;
+ api.readClipboardDirectories = async () => {
+ directoryScans++;
+ throw new Error("screenshot entered native directory discovery");
+ };
+ try {
+ await paste("", [image]);
+ assert(directoryScans === 0, "screenshot entered native directory discovery");
+ assert(controller.fileReferences.length === 1 && controller.fileReferences[0].kind === "image",
+ "screenshot bytes did not become an image attachment");
+ return { ok: true, screenshotWithoutDirectoryScan: true };
+ } finally {
+ api.readClipboardDirectories = readDirectories;
+ }
+ }
const text = "Word paragraph 中文\nsecond line";
let editor = await paste(text, [image]);
assert(
@@ -555,7 +572,18 @@ globalThis.composerPasteProbe = async () => {
previewHost.remove();
}
- await paste("", [image]);
+ const readDirectories = api.readClipboardDirectories;
+ let screenshotDirectoryScans = 0;
+ api.readClipboardDirectories = async () => {
+ screenshotDirectoryScans++;
+ return readDirectories();
+ };
+ try {
+ await paste("", [image]);
+ } finally {
+ api.readClipboardDirectories = readDirectories;
+ }
+ assert(screenshotDirectoryScans === 0, "screenshot paste waited for native directory scanning");
assert(
controller.fileReferences.length === 1 &&
controller.fileReferences[0].kind === "image",