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(mobile): ignore source xdt-file literals

DavidShen59b137cb14c3
diff --git a/packages/maker-shared/src/__tests__/pluginResults.test.ts b/packages/maker-shared/src/__tests__/pluginResults.test.ts
index 9eae75f524..fd0463833b 100644
--- a/packages/maker-shared/src/__tests__/pluginResults.test.ts
+++ b/packages/maker-shared/src/__tests__/pluginResults.test.ts
@@ -29,4 +29,57 @@ describe('portable plugin results', () => {
expect(files('Saved xdt-file://open?path=%2Ftmp%2Freport.pdf')[0].title).toBe('report.pdf');
expect(hasVisibleHistoryResult(JSON.stringify({ ok: true, result: { xdt_card_id: 'c' } }))).toBe(true);
});
+ it('ignores quoted protocol URLs when a tool prints source code or test fixtures', () => {
+ const source = [
+ "const urls = ['xdt-file://open?path=%2Ftmp%2Freport.pdf', 'xdt-file://open?path=%2Ftmp%2Findex.html'];",
+ "const template = `xdt-file://open?path=${encodeURIComponent(absPath)}`;",
+ ].join('\n');
+ expect(files(source)).toEqual([]);
+ expect(files('Saved xdt-file://open?path=%2Ftmp%2Freport.pdf')).toEqual([
+ { url: 'xdt-file://open?path=%2Ftmp%2Freport.pdf', title: 'report.pdf' },
+ ]);
+ });
+ it.each([
+ ['xdt-file:///tmp/report.pdf', 'report.pdf'],
+ ['xdt-file:///C:/reports/report.pdf', 'report.pdf'],
+ ['xdt-file:///tmp/report%20final.pdf', 'report final.pdf'],
+ ['xdt-file://open?path=%2Ftmp%2Freport.pdf', 'report.pdf'],
+ ['xdt-file://local/?path=C%3A%5Creports%5Creport.pdf', 'report.pdf'],
+ ])('retains absolute file references in both supported URL forms: %s', (url, title) => {
+ expect(files(`Saved [report](${url})`)).toEqual([{ url, title }]);
+ expect(files(JSON.stringify({ _xdt_model_files: [{ url, name: title }] }))).toEqual([{ url, title }]);
+ expect(files(JSON.stringify({ xdt_media_produced: [url] }))).toEqual([{ url, title }]);
+ });
+ it.each(['note', 'text', 'output'])('ignores source literals inside JSON %s while retaining real links', (field) => {
+ const source = [
+ "const a = 'xdt-file://open?path=%2Ftmp%2Ffixture.pdf';",
+ 'const b = "xdt-file:///tmp/fixture.html";',
+ 'const c = `xdt-file://open?path=${encodeURIComponent(absPath)}`;',
+ ].join('\n');
+ const url = 'xdt-file://open?path=%2Ftmp%2Factual.pdf';
+ for (const wrap of [
+ (text: string) => ({ [field]: text }),
+ (text: string) => ({ ok: true, result: { [field]: text } }),
+ (text: string) => ({ content: [{ type: 'text', text }] }),
+ ]) {
+ expect(files(JSON.stringify(wrap(source)))).toEqual([]);
+ expect(files(JSON.stringify(wrap(`${source}\nSaved ${url}`)))).toEqual([{ url, title: 'actual.pdf' }]);
+ }
+ });
+ it('keeps explicit file declarations when neighboring JSON text contains source examples', () => {
+ const url = 'xdt-file:///tmp/report.pdf';
+ expect(files(JSON.stringify({ ok: true, result: {
+ _xdt_model_files: [{ url, name: 'Report' }],
+ text: `const example = '${url}';`,
+ } }))).toEqual([{ url, title: 'Report' }]);
+ });
+ it.each([
+ 'xdt-file://report.pdf',
+ 'xdt-file://open?path=relative%2Freport.pdf',
+ 'xdt-file://open?path=${encodeURIComponent(absPath)}',
+ 'xdt-file://open?path=',
+ ])('rejects incomplete or non-absolute file references: %s', (url) => {
+ expect(files(`Saved ${url}`)).toEqual([]);
+ expect(files(JSON.stringify({ _xdt_model_files: [{ url }] }))).toEqual([]);
+ });
});
diff --git a/packages/maker-shared/src/payloadSummary.ts b/packages/maker-shared/src/payloadSummary.ts
index 528a410844..322af56ccc 100644
--- a/packages/maker-shared/src/payloadSummary.ts
+++ b/packages/maker-shared/src/payloadSummary.ts
@@ -466,6 +466,29 @@ export function extractPayloadToolCardIds(text: string): string[] {
export interface PayloadToolFile { url: string; title: string }
+const TOOL_FILE_URL_RE = /xdt-file:\/\/[^\s"'<>\\)`\]},;]+/g;
+
+function absoluteToolFilePath(url: string): string | null {
+ try {
+ // Keep both legacy direct-path links and the query-based local/open URLs.
+ const path = url.startsWith('xdt-file:///')
+ ? decodeURIComponent(url.slice('xdt-file://'.length))
+ : new URL(url).searchParams.get('path') ?? '';
+ return path.startsWith('/') || /^[A-Za-z]:[\\/]/.test(path) ? path : null;
+ } catch {
+ return null;
+ }
+}
+
+function looksLikeQuotedSourceLiteral(text: string, index: number, end: number): boolean {
+ const before = text[index - 1];
+ const after = text[end];
+ // Tool output often contains source files and test fixtures. A protocol URL
+ // surrounded by a string/template delimiter is source text, not a produced file.
+ return before === '`' || before === '\'' || before === '"'
+ || after === '`' || after === '\'' || after === '"';
+}
+
/** Existing 3D attachments and managed file references retain a usable file entry on Mobile. */
export function extractPayloadToolResultFiles(text: string): PayloadToolFile[] {
const parsed = parseToolResultPayload(text);
@@ -475,7 +498,9 @@ export function extractPayloadToolResultFiles(text: string): PayloadToolFile[] {
if (typeof url !== 'string' || !/^(?:xdt-file:\/\/[^\s]+|cindy-media:\/\/blobs\/[0-9a-f]{64}\.glb)$/.test(url)) return;
let name = url.split('/').pop()!;
if (url.startsWith('xdt-file://')) {
- try { name = (new URL(url).searchParams.get('path') ?? name).split(/[\\/]/).pop() || name; } catch { /* Keep the original reference as fallback. */ }
+ const path = absoluteToolFilePath(url);
+ if (path === null) return;
+ name = path.split(/[\\/]/).pop() || name;
}
if (!files.some((file) => file.url === url)) files.push({ url, title: typeof title === 'string' && title.trim() ? title : name });
};
@@ -483,8 +508,28 @@ export function extractPayloadToolResultFiles(text: string): PayloadToolFile[] {
for (const raw of parsed._xdt_model_files) { const file = readPayloadRecord(raw); add(file?.url, file?.name); }
}
if (Array.isArray(parsed?.xdt_media_produced)) for (const url of parsed.xdt_media_produced) add(url);
- // Files supplied as links in a result remain accessible without scanning arbitrary local paths.
- for (const match of text.matchAll(/xdt-file:\/\/[^\s"<>\\)]+/g)) add(match[0]);
+ // Scan decoded JSON values, not serialized JSON: its quotes are structural,
+ // while quotes inside a value can identify source/test literals. Use a stack
+ // so deeply nested tool output cannot overflow the call stack.
+ const pending: unknown[] = [parsed ?? text];
+ while (pending.length) {
+ const value = pending.pop();
+ if (typeof value === 'string') {
+ for (const match of value.matchAll(TOOL_FILE_URL_RE)) {
+ const start = match.index ?? 0;
+ if (!looksLikeQuotedSourceLiteral(value, start, start + match[0].length)) add(match[0]);
+ }
+ } else if (Array.isArray(value)) {
+ for (let i = value.length - 1; i >= 0; i--) pending.push(value[i]);
+ } else if (value !== null && typeof value === 'object') {
+ const entries = Object.entries(value);
+ for (let i = entries.length - 1; i >= 0; i--) {
+ const [key, child] = entries[i];
+ // Explicit declarations above already retain full URLs and display names.
+ if (key !== '_xdt_model_files' && key !== 'xdt_media_produced') pending.push(child);
+ }
+ }
+ }
return files;
}