Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

CR13: 回复产物识别 Windows 反斜杠路径

nana750f5d4046e5
diff --git a/apps/desktop/src/main/companion-cloud/__tests__/replyArtifacts.test.ts b/apps/desktop/src/main/companion-cloud/__tests__/replyArtifacts.test.ts
index 0d6e6dc5a8..94b58ede4e 100644
--- a/apps/desktop/src/main/companion-cloud/__tests__/replyArtifacts.test.ts
+++ b/apps/desktop/src/main/companion-cloud/__tests__/replyArtifacts.test.ts
@@ -2,7 +2,7 @@ import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
-import { collectReplyReferences, replyArtifactAttachments, replyTextWithFiles, toolResultImageUrls, REPLY_ARTIFACTS_MAX } from '../replyArtifacts.js';
+import { collectReplyReferences, replyArtifactAttachments, replyPathToLookup, replyTextWithFiles, toolResultImageUrls, REPLY_ARTIFACTS_MAX } from '../replyArtifacts.js';
const HASH = 'a'.repeat(64);
@@ -27,6 +27,15 @@ describe('collectReplyReferences', () => {
expect(collectReplyReferences('版本是 v1.2,见 config.json 和 notes.txt').paths).toEqual([]);
});
+ it('takes Windows paths as paths: relative with backslashes, with a drive letter, in prose and in code', () => {
+ const text = [
+ '做好了 documents\\report.docx,另见 `out\\charts\\q3.png`。',
+ '绝对路径 C:\\work\\documents\\summary.pdf 和 `D:\\data\\sheet.xlsx`',
+ '不交付:config\\prod.yml、src\\keys\\id.json、tools\\run.bat',
+ ].join('\n');
+ expect(collectReplyReferences(text).paths).toEqual(['documents\\report.docx', 'out\\charts\\q3.png', 'C:\\work\\documents\\summary.pdf', 'D:\\data\\sheet.xlsx']);
+ });
+
it('names only deliverable formats, whatever form the path is written in', () => {
const text = [
'改了 config/production.yml 和 src/keys/service-account.json,还有 src/app.ts、deploy/.env、certs/server.pem、docs/notes.txt',
@@ -74,6 +83,21 @@ describe('replyArtifactAttachments', () => {
expect(files.map(file => file.name).sort()).toEqual(['chart.png', 'report.docx']);
});
+ it('finds a file named with backslashes inside the work folder, and still refuses what is outside, hidden or not a delivery', async () => {
+ await fs.writeFile(path.join(root, 'documents', 'chart.png'), 'png');
+ await fs.writeFile(path.join(root, 'documents', 'prod.yml'), 'x: 1');
+ await fs.writeFile(path.join(outside, 'secret.docx'), 'secret');
+ await fs.mkdir(path.join(root, '.private'));
+ await fs.writeFile(path.join(root, '.private', 'notes.docx'), 'hidden');
+ const text = [
+ '见 documents\\report.docx、`documents\\chart.png`、documents\\prod.yml、',
+ `..\\${path.basename(outside)}\\secret.docx、.private\\notes.docx、`,
+ '[表](documents\\report.docx)、C:\\no\\such\\drive\\a.docx',
+ ].join('');
+ const files = await replyArtifactAttachments({ id: 'm1' }, text, 's1', deps());
+ expect(files.map(file => file.name).sort()).toEqual(['chart.png', 'report.docx']);
+ });
+
it('never reads outside the work folder, through a link out of it, a hidden file, or an oversized one', async () => {
await fs.writeFile(path.join(outside, 'secret.docx'), 'secret');
await fs.symlink(path.join(outside, 'secret.docx'), path.join(root, 'documents', 'link.docx'));
@@ -154,3 +178,14 @@ describe('replyTextWithFiles', () => {
])).toBe(`看这个\n\n![chart.png](cindy-media://blobs/${'b'.repeat(64)}.png)\n\n[a (1).docx](xdt-file://local/?path=%2Fatt%2Fa%20%281%29.docx)\n\n[附件: gone.pdf]`);
});
});
+
+describe('replyPathToLookup', () => {
+ it('reads a relative name with either separator as folders inside the root, and leaves an absolute path alone', () => {
+ expect(replyPathToLookup('documents\\report.docx', '/work', path.posix)).toBe('/work/documents/report.docx');
+ expect(replyPathToLookup('documents/report.docx', '/work', path.posix)).toBe('/work/documents/report.docx');
+ expect(replyPathToLookup('/work/documents/report.docx', '/other', path.posix)).toBe('/work/documents/report.docx');
+ expect(replyPathToLookup('documents\\report.docx', 'C:\\work', path.win32)).toBe('C:\\work\\documents\\report.docx');
+ expect(replyPathToLookup('documents/report.docx', 'C:\\work', path.win32)).toBe('C:\\work\\documents\\report.docx');
+ expect(replyPathToLookup('C:\\work\\documents\\report.docx', 'C:\\other', path.win32)).toBe('C:\\work\\documents\\report.docx');
+ });
+});
diff --git a/apps/desktop/src/main/companion-cloud/replyArtifacts.ts b/apps/desktop/src/main/companion-cloud/replyArtifacts.ts
index 9fad59cfd6..533c8ca609 100644
--- a/apps/desktop/src/main/companion-cloud/replyArtifacts.ts
+++ b/apps/desktop/src/main/companion-cloud/replyArtifacts.ts
@@ -67,7 +67,8 @@ export function collectReplyReferences(text: string): ReplyReferences {
// Chinese text is written without spaces: try the token as is and without the words stuck to its ends.
const bare = token.replace(/^[\u3400-\u9fff\uff00-\uffef]+/, '');
for (const candidate of new Set([token, bare, token.replace(/[\u3400-\u9fff\uff00-\uffef]+$/, ''), bare.replace(/[\u3400-\u9fff\uff00-\uffef]+$/, '')])) {
- if (candidate.includes('/') ? candidate.length > 1 : /^[\p{L}\p{N}_.@ -]+$/u.test(candidate)) add(candidate);
+ // A Windows path (`documents\report.docx`, `C:\work\report.docx`) has a folder part just like a `/` one.
+ if (/[\\/]/.test(candidate) ? candidate.length > 1 : /^[\p{L}\p{N}_.@ -]+$/u.test(candidate)) add(candidate);
}
}
return { blobs, paths };
@@ -125,6 +126,16 @@ function within(root: string, target: string): boolean {
return relative === '' || (!relative.startsWith('..') && !path.isAbsolute(relative));
}
+/**
+ * The path to look up for a name the reply gave: an absolute one as written, a relative one inside
+ * `root`. Where the separator is `/`, a relative name written with backslashes (a reply composed on
+ * Windows) is read as folders too; where it is `\`, `join` already takes both.
+ */
+export function replyPathToLookup(candidate: string, root: string, impl: Pick<typeof path, 'isAbsolute' | 'join' | 'sep'> = path): string {
+ if (impl.isAbsolute(candidate)) return candidate;
+ return impl.join(root, impl.sep === '/' ? candidate.replace(/\\/g, '/') : candidate);
+}
+
export interface ReplyArtifactDeps {
/** The real folders this companion's task may read from (its work folder); empty when none. */
roots: (sessionId: string) => Promise<string[]>;
@@ -170,7 +181,7 @@ export async function replyArtifactAttachments(
if (NETWORK_PATH.test(candidate)) continue;
try {
// Relative names are the work folder's; an absolute one must still end up inside it.
- const real = await fs.realpath(path.isAbsolute(candidate) ? candidate : path.join(roots[0]!, candidate));
+ const real = await fs.realpath(replyPathToLookup(candidate, roots[0]!));
if (!isDeliverablePath(real)) continue;
const root = roots.find(item => within(item, real));
if (!root || seen.has(real)) continue;
diff --git a/docs/product-rules/cloud-companions.md b/docs/product-rules/cloud-companions.md
index 1e808a32c4..7aad2a485f 100644
--- a/docs/product-rules/cloud-companions.md
+++ b/docs/product-rules/cloud-companions.md
@@ -133,7 +133,7 @@
签名地址过期——对象存储返回 401/403)时这一轮停下、下一轮连同这条消息重试,换电脑时的下载与合并 Cindy 时的搬运同样按这条区分(暂时失败则
接管或合并本次失败、稍后重试)。只发附件没有文字的消息同样同步。
- 伙伴回复里引用的产物随回复上云(`replyArtifacts.ts`):正文里的 `cindy-media://blobs/…` 地址(该任务能读、本机有字节的),
- 以及回复里点名的文件——Markdown 链接目标、`xdt-file://` 链接、行内代码或正文里的路径(`documents/报告.docx`),代码块里的示例不算。
+ 以及回复里点名的文件——Markdown 链接目标、`xdt-file://` 链接、行内代码或正文里的路径(`documents/报告.docx`;Windows 写法 `documents\报告.docx`、`C:\work\报告.docx` 同样识别,后者要真的落在该任务工作目录里;UNC 路径仍不读),代码块里的示例不算。
**不论路径怎么写,只上传交付类扩展名**:docx / doc / xlsx / xls / pptx / ppt / pages / numbers / key、pdf、csv / tsv、zip、md、
图片(png / jpg / jpeg / gif / webp / heic / bmp)、音频(mp3 / wav / m4a / aac / ogg / flac)、视频(mp4 / mov / webm / m4v);
代码、配置、json / yaml / toml / env / txt、证书与密钥类一律不传,即使在工作目录里也被回复点了名(`config/production.yml`、