Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(shared): filter logical and commented source file literals
DavidShend7f942d4da1a
diff --git a/apps/desktop/src/main/device-link/__tests__/mobileToolProjection.test.ts b/apps/desktop/src/main/device-link/__tests__/mobileToolProjection.test.tsindex 5d411d25f7..9775ae725f 100644--- a/apps/desktop/src/main/device-link/__tests__/mobileToolProjection.test.ts+++ b/apps/desktop/src/main/device-link/__tests__/mobileToolProjection.test.ts@@ -17,6 +17,8 @@ describe('mobile tool projection', () => {it('keeps large source output as text instead of promoting fixtures to file declarations', () => {const source = "const urls = ['xdt-file://open?path=%2Ftmp%2Ffixture.pdf', 'xdt-file:///tmp/example.html'];\n"+ "const path = enabled ? 'xdt-file:///tmp/conditional.pdf' : undefined\n"+ + "const fallback = candidate || 'xdt-file:///tmp/logical.pdf'\n"+ + "const list = [/* first */ 'xdt-file:///tmp/a.pdf', // next\n 'xdt-file:///tmp/b.pdf']\n"+ '// source 中文\n'.repeat(1500);const row = { ...tool('Read'), role: 'tool_result', content: source };const live = projectMobileToolPush('local-db:messages:created', { message: row }) as { message: typeof row };diff --git a/apps/mobile/src/__tests__/pluginResultRendering.test.ts b/apps/mobile/src/__tests__/pluginResultRendering.test.tsindex e4bb196c56..b3816fd8d5 100644--- a/apps/mobile/src/__tests__/pluginResultRendering.test.ts+++ b/apps/mobile/src/__tests__/pluginResultRendering.test.ts@@ -13,7 +13,9 @@ function messages(content: unknown): RemoteMessage[] {describe('plugin results are visible outside collapsed tool details', () => {it.each([true, false])('does not render source fixtures as delivered files (streaming=%s)', (isSessionStreaming) => {const source = "const urls = ['xdt-file://open?path=%2Ftmp%2Ffixture.pdf', 'xdt-file:///tmp/example.html'];\n"- + "const path = enabled ? 'xdt-file:///tmp/conditional.pdf' : undefined";+ + "const path = enabled ? 'xdt-file:///tmp/conditional.pdf' : undefined\n"+ + "const fallback = candidate || 'xdt-file:///tmp/logical.pdf'\n"+ + "const list = [/* first */ 'xdt-file:///tmp/a.pdf', // next\n 'xdt-file:///tmp/b.pdf']";for (const content of [source, { text: source }]) {const rows = messages(content);rows[0].content = { toolName: 'Read', toolUseId: 'u', input: { file_path: '/tmp/source.ts' } };diff --git a/packages/maker-shared/src/__tests__/pluginResults.test.ts b/packages/maker-shared/src/__tests__/pluginResults.test.tsindex c1702f3ec3..4f6ffd8133 100644--- a/packages/maker-shared/src/__tests__/pluginResults.test.ts+++ b/packages/maker-shared/src/__tests__/pluginResults.test.ts@@ -162,6 +162,28 @@ describe('portable plugin results', () => {}}});+ it.each(['&&', '||', '??'])('ignores logical-expression source literals after %s', (operator) => {+ const url = 'xdt-file:///tmp/logical.pdf';+ for (const quote of ["'", '"', String.fromCharCode(96)]) {+ const source = 'const path = candidate ' + operator + ' ' + quote + url + quote;+ expect(files(source)).toEqual([]);+ expect(files(JSON.stringify({ text: source }))).toEqual([]);+ expect(files(source + '\nSaved ' + quote + url + quote)).toEqual([{ url, title: 'logical.pdf' }]);+ expect(files('const path = candidate ' + operator + ' /* fallback */ ' + quote + url + quote)).toEqual([]);+ }+ });+ it.each(['/* next file */', '// next file\n', '/* multi\nline "comment" */'])('filters source arrays across comments: %s', (comment) => {+ const a = 'xdt-file:///tmp/first.pdf';+ const b = 'xdt-file:///tmp/second.pdf';+ for (const quote of ["'", '"', String.fromCharCode(96)]) {+ const list = quote + a + quote + ', ' + comment + ' ' + quote + b + quote;+ const source = 'const paths = [ ' + comment + ' ' + list + ' ]';+ expect(files(source)).toEqual([]);+ expect(files(JSON.stringify({ text: source }))).toEqual([]);+ expect(files(source + '\nSaved "' + b + '"')).toEqual([{ url: b, title: 'second.pdf' }]);+ expect(files('Saved ' + list)).toEqual([{ url: a, title: 'first.pdf' }, { url: b, title: 'second.pdf' }]);+ }+ });it.each(["xdt-file:///tmp/O'Brien.pdf","xdt-file:///C:/reports/O'Brien.pdf",diff --git a/packages/maker-shared/src/payloadSummary.ts b/packages/maker-shared/src/payloadSummary.tsindex b0f52695cc..00988f8afc 100644--- a/packages/maker-shared/src/payloadSummary.ts+++ b/packages/maker-shared/src/payloadSummary.ts@@ -488,14 +488,23 @@ function absoluteToolFilePath(url: string): string | null {}}+function withoutSourceComments(text: string): string {+ // Skip complete string literals before recognizing comments, so URL schemes+ // and comment-like file names inside strings remain part of the context.+ return text.replace(+ /"(?:\\[\s\S]|[^"\\])*"|'(?:\\[\s\S]|[^'\\])*'|`(?:\\[\s\S]|[^`\\])*`|\/\*[\s\S]*?\*\/|(?<![:/])\/\/[^\r\n]*/g,+ (token) => token.startsWith('/') ? token.replace(/[^\r\n]/g, ' ') : token,+ );+}+function looksLikeQuotedSourceLiteral(text: string, index: number, end: number): boolean {const before = text[index - 1];const after = text[end];if (before !== '`' && before !== '\'' && before !== '"') return false;// Quotes alone also occur in prose and inline Markdown. Require source syntax// around the literal (assignment, collection entry, argument or conditional).- const prefix = text.slice(0, index - 1).trimEnd();- return /(?:[=\[(,?]|\breturn)$/.test(prefix)+ const prefix = withoutSourceComments(text.slice(0, index - 1)).trimEnd();+ return /(?:[=\[(,?]|&&|\|\||\breturn)$/.test(prefix)// A colon alone is also a prose label ("File:"). Require a preceding// conditional, allowing indented continuation lines but not new prose.|| (prefix.endsWith(':') && /\?(?:[^;\r\n]|\r?\n[ \t])*:$/.test(prefix))@@ -546,7 +555,10 @@ export function extractPayloadToolResultFiles(text: string): PayloadToolFile[] {: null;const url = closingQuote ? match[0].slice(0, closingQuote.index) : match[0];urlPattern.lastIndex = start + url.length;- const source = previous && /^["'`]\s*,\s*["'`]$/.test(value.slice(previous.end, start))+ const continuesList = previous && /^["'`]$/.test(value[previous.end])+ && /^["'`]$/.test(quote)+ && withoutSourceComments(value.slice(previous.end + 1, start - 1)).trim() === ',';+ const source = previous && continuesList? previous.source: looksLikeQuotedSourceLiteral(value, start, start + url.length);if (!source) add(url);