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。

CR4: 回复产物只上传交付类扩展名

nana8394198850b1
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 1876166f4a..8d2ddc133a 100644
--- a/apps/desktop/src/main/companion-cloud/__tests__/replyArtifacts.test.ts
+++ b/apps/desktop/src/main/companion-cloud/__tests__/replyArtifacts.test.ts
@@ -10,7 +10,7 @@ describe('collectReplyReferences', () => {
it('finds media addresses and the files a reply names, in prose, inline code and links', () => {
const text = [
`![chart](cindy-media://blobs/${HASH}.png)`,
- '做好了 documents/报告.docx,另见 `notes/todo.txt`。',
+ '做好了 documents/报告.docx,另见 `notes/todo.md`。',
'[表格](documents/q3.xlsx) 和 [导出](xdt-file:///work/out/data.csv)',
'放在了summary.pdf里,见documents/a.pdf。',
'```\ndocuments/example-only.docx\n```',
@@ -18,13 +18,22 @@ describe('collectReplyReferences', () => {
].join('\n');
const found = collectReplyReferences(text);
expect(found.blobs).toEqual([`cindy-media://blobs/${HASH}.png`]);
- expect(found.paths).toEqual(expect.arrayContaining(['documents/报告.docx', 'notes/todo.txt', 'documents/q3.xlsx', '/work/out/data.csv', 'summary.pdf', 'documents/a.pdf']));
+ expect(found.paths).toEqual(expect.arrayContaining(['documents/报告.docx', 'notes/todo.md', 'documents/q3.xlsx', '/work/out/data.csv', 'summary.pdf', 'documents/a.pdf']));
expect(found.paths).not.toContain('documents/example-only.docx');
expect(found.paths.some(item => item.includes('example.com') || item.includes('x.dev'))).toBe(false);
});
it('does not take a bare word with a dot, or a name without a deliverable extension, as a file', () => {
- expect(collectReplyReferences('版本是 v1.2,见 README.md 和 config.json').paths).toEqual([]);
+ expect(collectReplyReferences('版本是 v1.2,见 config.json 和 notes.txt').paths).toEqual([]);
+ });
+
+ 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',
+ '[配置](config/settings.toml) [密钥](xdt-file:///work/keys/id_rsa.key.json) ![图](assets/logo.png)',
+ '交付:out/report.docx out/data.csv out/deck.pptx out/voice.mp3 out/demo.mp4 out/readme.md out/bundle.zip',
+ ].join('\n');
+ expect(collectReplyReferences(text).paths).toEqual(['assets/logo.png', 'out/report.docx', 'out/data.csv', 'out/deck.pptx', 'out/voice.mp3', 'out/demo.mp4', 'out/readme.md', 'out/bundle.zip']);
});
});
@@ -51,6 +60,20 @@ describe('replyArtifactAttachments', () => {
expect(files[0]).toMatchObject({ name: 'report.docx', category: 'file', path: path.join(root, 'documents', 'report.docx'), url: null, size: 10 });
});
+ it('sends a deliverable from the work folder but never code or configuration, even when the reply names it', async () => {
+ await fs.mkdir(path.join(root, 'config'));
+ await fs.mkdir(path.join(root, 'src', 'keys'), { recursive: true });
+ await fs.writeFile(path.join(root, 'config', 'production.yml'), 'secret: 1');
+ await fs.writeFile(path.join(root, 'src', 'keys', 'service-account.json'), '{}');
+ await fs.writeFile(path.join(root, 'src', 'index.ts'), 'export {}');
+ await fs.writeFile(path.join(root, 'documents', 'chart.png'), 'png');
+ // A link to a configuration file by an allowed name does not make it a delivery either.
+ await fs.symlink(path.join(root, 'config', 'production.yml'), path.join(root, 'documents', 'looks-fine.pdf'));
+ const text = '见 config/production.yml、src/keys/service-account.json、src/index.ts、documents/report.docx、[图](documents/chart.png)、documents/looks-fine.pdf';
+ 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'));
diff --git a/apps/desktop/src/main/companion-cloud/replyArtifacts.ts b/apps/desktop/src/main/companion-cloud/replyArtifacts.ts
index d9154871ea..072b01d84c 100644
--- a/apps/desktop/src/main/companion-cloud/replyArtifacts.ts
+++ b/apps/desktop/src/main/companion-cloud/replyArtifacts.ts
@@ -15,16 +15,27 @@ import type { CompanionMirrorAttachment } from './mirror.js';
export const REPLY_ARTIFACTS_MAX = 8;
const BLOB_URL = /\bcindy-media:\/\/blobs\/[0-9a-f]{64}\.[a-z0-9]+/g;
-/** A bare file name (no folder) counts only for the formats people are handed as deliverables. */
-const DELIVERABLE_EXT = new Set(['docx', 'doc', 'xlsx', 'xls', 'pptx', 'ppt', 'pdf', 'csv', 'tsv', 'zip', 'key', 'pages', 'numbers']);
+/**
+ * Only deliverables go to the room, whatever form the path is written in: documents, spreadsheets,
+ * presentations, PDF, CSV/TSV, archives, Markdown, images, audio and video. Code, configuration,
+ * data (json/yaml/env/txt) and key files are never sent, even inside the work folder, because the
+ * reply naming them is not the owner choosing to hand them over.
+ */
+const DELIVERABLE_EXT = new Set([
+ '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',
+]);
const MIME_BY_EXT: Record<string, string> = {
- png: 'image/png', jpg: 'image/jpeg', jpeg: 'image/jpeg', gif: 'image/gif', webp: 'image/webp',
- pdf: 'application/pdf', zip: 'application/zip', csv: 'text/csv', tsv: 'text/tab-separated-values',
- txt: 'text/plain', md: 'text/markdown', json: 'application/json', html: 'text/html',
+ png: 'image/png', jpg: 'image/jpeg', jpeg: 'image/jpeg', gif: 'image/gif', webp: 'image/webp', heic: 'image/heic', bmp: 'image/bmp',
+ pdf: 'application/pdf', zip: 'application/zip', csv: 'text/csv', tsv: 'text/tab-separated-values', md: 'text/markdown',
+ mp3: 'audio/mpeg', wav: 'audio/wav', m4a: 'audio/mp4', aac: 'audio/aac', ogg: 'audio/ogg', flac: 'audio/flac',
+ mp4: 'video/mp4', mov: 'video/quicktime', webm: 'video/webm', m4v: 'video/x-m4v',
docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
};
+const isDeliverablePath = (candidate: string) => DELIVERABLE_EXT.has(/\.([A-Za-z0-9]{1,8})$/.exec(candidate)?.[1]?.toLowerCase() ?? '');
/** UNC / WebDAV / device paths are never opened (realpath could contact a remote server). */
const NETWORK_PATH = /^[\\/]{2}|^[\\/]\?\?[\\/]/;
@@ -36,7 +47,7 @@ export function collectReplyReferences(text: string): ReplyReferences {
const blobs = [...new Set(prose.match(BLOB_URL) ?? [])];
const paths: string[] = [];
const add = (candidate: string | null) => {
- if (candidate && !candidate.includes('\0') && !paths.includes(candidate)) paths.push(candidate);
+ if (candidate && !candidate.includes('\0') && isDeliverablePath(candidate) && !paths.includes(candidate)) paths.push(candidate);
};
// Markdown link / image targets (a local path, or an xdt-file link).
for (const match of prose.matchAll(/\]\(\s*<?([^)>\n]+?)>?(?:\s+"[^"]*")?\s*\)/g)) {
@@ -54,9 +65,7 @@ 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]+$/, '')])) {
- const ext = /\.([A-Za-z0-9]{1,8})$/.exec(candidate)?.[1]?.toLowerCase();
- if (!ext) continue;
- if (candidate.includes('/') ? candidate.length > 1 : DELIVERABLE_EXT.has(ext) && /^[\p{L}\p{N}_.@ -]+$/u.test(candidate)) add(candidate);
+ if (candidate.includes('/') ? candidate.length > 1 : /^[\p{L}\p{N}_.@ -]+$/u.test(candidate)) add(candidate);
}
}
return { blobs, paths };
@@ -156,6 +165,7 @@ export async function replyArtifactAttachments(
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));
+ if (!isDeliverablePath(real)) continue;
const root = roots.find(item => within(item, real));
if (!root || seen.has(real)) continue;
// Hidden files and folders (.env, .git, …) are never part of a delivery.
diff --git a/docs/product-rules/cloud-companions.md b/docs/product-rules/cloud-companions.md
index d4d87f28c9..c6d888bf26 100644
--- a/docs/product-rules/cloud-companions.md
+++ b/docs/product-rules/cloud-companions.md
@@ -132,8 +132,11 @@
签名地址过期——对象存储返回 401/403)时这一轮停下、下一轮连同这条消息重试,换电脑时的下载与合并 Cindy 时的搬运同样按这条区分(暂时失败则
接管或合并本次失败、稍后重试)。只发附件没有文字的消息同样同步。
- 伙伴回复里引用的产物随回复上云(`replyArtifacts.ts`):正文里的 `cindy-media://blobs/…` 地址(该任务能读、本机有字节的),
- 以及回复里点名的文件——Markdown 链接目标、`xdt-file://` 链接、行内代码或正文里的路径(`documents/报告.docx`;不带目录的名字
- 只认 docx / xlsx / pptx / pdf / csv / zip 等交付格式),代码块里的示例不算。文件必须真实存在于该任务自己的工作目录内
+ 以及回复里点名的文件——Markdown 链接目标、`xdt-file://` 链接、行内代码或正文里的路径(`documents/报告.docx`),代码块里的示例不算。
+ **不论路径怎么写,只上传交付类扩展名**: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`、
+ `src/keys/service-account.json` 都不会上云);符号链接按真实路径再判一次扩展名。文件必须真实存在于该任务自己的工作目录内
(符号链接按真实路径复核,隐藏文件与目录如 `.env`、`.git` 不上传,目录外与 UNC 路径不读),单个不超过 25 MB,每条回复最多 8 个;
经与主人附件同一条房间附件通道上传,永久 / 暂时失败的区分与主人附件一致(暂时失败这一轮停下重试,永久失败只发正文)。
工具在这一轮生成并存入媒体仓的图片(工具结果里的 `xdt_image_url` / `xdt_image_urls`,聊天里作为这一轮的图片独立显示,回复正文可以不提)同样作为附件随回复上云:取上一条用户 / 伙伴消息之后到这条回复为止的工具结果,落在其后第一条被导入的回复上,计入每条回复 8 个的上限,只带图片本身,不带工具调用明细(§1 第 2 条);结果要求不显示(`_xdt_render_image: false`)或不是媒体仓地址的不带。
@@ -278,7 +281,7 @@
之后再换回原电脑时那边跟随它自己的默认,不会恢复当初选的模型;提示只在伙伴聊天打开时出现一次(设置里不常驻)。
`permissions` 在新建副本上是默认档,不推断也不提示原电脑上是什么档。技能的 `skills` 列表只是引用,伙伴自有技能仍按
`companion.skill.v1` 随行;能力值读不出来时本轮不上传、不写墓碑,交出前的 flush 因此失败而不是丢掉它。
-- 回复产物的已知限制:只认回复文字里点名的路径与地址,是启发式的(例如不带目录又不是交付格式的文件名不会被识别);
+- 回复产物的已知限制:只认回复文字里点名的路径与地址,是启发式的(例如扩展名不在上面交付格式清单里的文件,哪怕就是用户要的,也不会上云);
视频、音频等非图片的 `cindy-media://` 产物在新电脑上以文件链接出现,正文里原地址不会变成可播放;`workspace/` 本身仍不搬,
正文里的相对路径在新电脑的工作目录里不存在,打开要用追加的文件链接;同一个文件被多条回复引用会各上传一次。
- 未覆盖、留作后续:手机与非主力电脑离线编辑资料;IM 渠道按伙伴绑定;离线时在其他端直接发送并排队;