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。

fix(chat): clean shared file definitions and preserve literal file URLs

ylliuuu66f2d82d4f7abe
diff --git a/apps/desktop/src/main/cindy-media/__tests__/taskImageDelivery.test.ts b/apps/desktop/src/main/cindy-media/__tests__/taskImageDelivery.test.ts
index 3a6fb53c87..232f069e04 100644
--- a/apps/desktop/src/main/cindy-media/__tests__/taskImageDelivery.test.ts
+++ b/apps/desktop/src/main/cindy-media/__tests__/taskImageDelivery.test.ts
@@ -1,4 +1,6 @@
import { createHookMediaStore } from '../hookOutbox';
+import { prepareChatReplyFiles } from '../../maker-ipc/chatServerReplyFiles';
+import { prepareChatReplyImages } from '../../maker-ipc/chatServerReplyImages';
import { createHookRequestLedger, HOOK_TERMINAL_DELIVERY_TTL_MS } from '../../hook-control/requestLedger';
import type { TurnEndPayload } from '@cindy/slack-hook-protocol';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
@@ -111,6 +113,21 @@ function insert(text: string) {
}
describe('task image delivery', () => {
+ // #6773: run both production preparation passes; a shared PDF definition
+ // must not be interpreted as an image after the file has been extracted.
+ it.each(['nested', 'sibling'])('delivers shared file/image references through both preparation passes (%s)', async mode => {
+ const pdf = path.join(work, 'report.pdf');
+ const bytes = Buffer.from('%PDF-1.4\nreport');
+ await fs.writeFile(pdf, bytes);
+ const label = mode === 'nested' ? '[![preview][report]][report]' : '[report][report] ![preview][report]';
+ const files = await prepareChatReplyFiles(`${label}\n\n[report]: <${pdf}>`, 's', () => {});
+ expect(files.attachments).toHaveLength(1);
+ expect(await files.attachments[0]!.readBytes!()).toEqual(bytes);
+ const images = await prepareChatReplyImages(files.text, 's', () => {});
+ expect(images.attachments).toEqual([]);
+ expect(images.text).not.toContain(pdf);
+ });
+
it.each(['before', 'during'].flatMap((order) =>
['publish', 'rollback'].map((result) => [order, result]),
))('reconciles a reused reference when cleanup runs %s %s', async (order, result) => {
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/chatServerReplyFiles.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/chatServerReplyFiles.test.ts
index 1541635dd1..5be8080e64 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/chatServerReplyFiles.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/chatServerReplyFiles.test.ts
@@ -64,15 +64,15 @@ describe('group reply file delivery', () => {
expect((await result.attachments[0]!.readBytes!()).toString()).toContain('%PDF');
expect(result.text).toContain(image);
expect(result.text).not.toContain(`[${image}]`);
- if (mode === 'shared-reference') expect(result.text).toContain(`[image]: <${pdf}>`);
+ if (mode === 'shared-reference') expect(result.text).not.toContain(`[image]: <${pdf}>`);
});
- it.each(['100% done.pdf', 'encoded%20.pdf'])('preserves legacy literal or once-encoded percent paths: %s', async name => {
+ it.each(['file', 'xdt-file'].flatMap(scheme => ['100% done.pdf', 'encoded%20.pdf'].map(name => [scheme, name])))('preserves literal or once-encoded percent paths: %s %s', async (scheme, name) => {
const file = path.join(work, name);
await fs.writeFile(file, 'percent report');
// A direct legacy URL is a URI with forward slashes on every platform.
- const direct = `xdt-file://${file.startsWith('/') ? '' : '/'}${file.replaceAll('\\', '/')}`;
- const encoded = pathToFileURL(file).href.replace(/^file:/, 'xdt-file:');
+ const direct = `${scheme}://${file.startsWith('/') ? '' : '/'}${file.replaceAll('\\', '/')}`;
+ const encoded = pathToFileURL(file).href.replace(/^file:/, `${scheme}:`);
const result = await prepareChatReplyFiles(`[file](<${name.startsWith('100') ? direct : encoded}>)`, 'lane', () => {});
expect((await result.attachments[0]!.readBytes!()).toString()).toBe('percent report');
});
@@ -196,8 +196,8 @@ describe('group reply file delivery', () => {
expect(await prepareChatReplyFiles(text, 'lane', () => { throw new Error('unexpected'); })).toEqual({ text, attachments: [] });
});
- it.each(['../private.pdf', 'file://server/private.pdf', '//server/private.pdf'])('refuses an out-of-scope or network file %s', async url => {
- await expect(prepareChatReplyFiles(`[file](${url})`, 'lane', () => {})).rejects.toThrow('ATTACHMENT_UNAVAILABLE');
+ it.each(['../private.pdf', 'file://server/private.pdf', 'file://server/100% done.pdf', 'file://user:secret@localhost/100% done.pdf', '//server/private.pdf'])('refuses an out-of-scope or network file %s', async url => {
+ await expect(prepareChatReplyFiles(`[file](<${url}>)`, 'lane', () => {})).rejects.toThrow('ATTACHMENT_UNAVAILABLE');
});
it('refuses a symlink escaping the task before reading its bytes', async () => {
diff --git a/apps/desktop/src/main/maker-ipc/chatServerReplyFiles.ts b/apps/desktop/src/main/maker-ipc/chatServerReplyFiles.ts
index 1623f0ef40..ebe8b5dddf 100644
--- a/apps/desktop/src/main/maker-ipc/chatServerReplyFiles.ts
+++ b/apps/desktop/src/main/maker-ipc/chatServerReplyFiles.ts
@@ -61,8 +61,6 @@ export function replyFileLinks(text: string) {
});
const links: Array<{ url: string; start: number; end: number; label: string }> = [];
const usedDefinitions = new Set<string>();
- const imageDefinitions = new Set<string>();
- visit(tree, 'imageReference', node => { imageDefinitions.add(node.identifier); });
visit(tree, node => {
if (node.type !== 'link' && node.type !== 'linkReference') return;
const target = node.type === 'link' ? node : definitions.get(node.identifier);
@@ -78,7 +76,8 @@ export function replyFileLinks(text: string) {
const edits = links.map(link => ({ start: link.start, end: link.end, text: link.label }));
for (const identifier of usedDefinitions) {
const node = definitions.get(identifier)!;
- if (!imageDefinitions.has(identifier) && node.position?.start.offset !== undefined && node.position.end.offset !== undefined)
+ // A shared image reference must not send this already-delivered file through image import.
+ if (node.position?.start.offset !== undefined && node.position.end.offset !== undefined)
edits.push({ start: node.position.start.offset, end: node.position.end.offset, text: '' });
}
let deliveredText = text;
@@ -118,7 +117,17 @@ function localPath(url: string, workDir: string): string {
if (process.platform === 'win32' && /^\/[A-Za-z]:[\\/]/.test(value)) value = value.slice(1);
}
}
- } else if (/^file:/i.test(url)) value = fileURLToPath(url);
+ } else if (/^file:/i.test(url)) {
+ try { value = fileURLToPath(url); }
+ catch (error) {
+ if (!(error instanceof URIError) || !['', 'localhost'].includes(new URL(url).hostname)) throw error;
+ // Match the existing literal-percent compatibility without admitting remote hosts.
+ const literal = /^file:(?:\/\/(?:localhost)?)?(\/[^?#]*)/i.exec(url)?.[1];
+ if (!literal) throw error;
+ value = literal;
+ if (process.platform === 'win32' && /^\/[A-Za-z]:[\\/]/.test(value)) value = value.slice(1);
+ }
+ }
} catch { throw unavailable(); }
// Never resolve a network/device path, including on another host platform.
if (/^[\\/]{2}|^[\\/]\?\?[\\/]/.test(value) || value.includes('\0')) throw unavailable();