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(im): 卡片渠道去掉图片标题、流式期间隐藏本机路径,提示按 Windows 路径取文件名

Dasha7564d904257
diff --git a/apps/desktop/src/main/cindy-media/taskImageDelivery.ts b/apps/desktop/src/main/cindy-media/taskImageDelivery.ts
index 113352355b..26f883a8ab 100644
--- a/apps/desktop/src/main/cindy-media/taskImageDelivery.ts
+++ b/apps/desktop/src/main/cindy-media/taskImageDelivery.ts
@@ -267,7 +267,7 @@ async function materializeTaskImageTextUnlocked(
return unchanged;
}
const result = await scope.materialize(unchanged.text, (source, error) => {
- log?.warn(`task image import failed (${path.basename(source)}): ${errorMessage(error)}`);
+ log?.warn(`task image import failed (${path.win32.basename(source)}): ${errorMessage(error)}`);
});
scope.assertStillValid();
const [current] = await scope.db
diff --git a/apps/desktop/src/main/hook-control/__tests__/outbound.test.ts b/apps/desktop/src/main/hook-control/__tests__/outbound.test.ts
index 3e85da363f..b3c662cdbb 100644
--- a/apps/desktop/src/main/hook-control/__tests__/outbound.test.ts
+++ b/apps/desktop/src/main/hook-control/__tests__/outbound.test.ts
@@ -243,6 +243,17 @@ describe('collectOutboundAttachments', () => {
expect(r.text).toContain('🖼️ _首页纪念行_');
expect(r.text).not.toContain('/tmp/preview');
expect(r.text).toContain('未发送:首页纪念行、stars-dark.png');
+
+ // Windows-style remote paths must not leak their directories on a POSIX host.
+ const win = await collectOutboundAttachments('![](C:\\Users\\alice\\private.png)', [], {
+ resolveImageUrl: () => {
+ throw new Error('unused');
+ },
+ formatIncompleteNotice: (names) => names.join('|'),
+ log,
+ });
+ expect(win.text).toContain('private.png');
+ expect(win.text).not.toContain('alice');
});
it('refScanText: 引用范围可宽于正文, 正文变换仍只作用于正文', async () => {
diff --git a/apps/desktop/src/main/hook-control/outbound.ts b/apps/desktop/src/main/hook-control/outbound.ts
index 541f1e9aa9..83452fe8e7 100644
--- a/apps/desktop/src/main/hook-control/outbound.ts
+++ b/apps/desktop/src/main/hook-control/outbound.ts
@@ -192,7 +192,7 @@ export interface OutboundResult {
/** 提示里附件名的展示形态: 单行、限长; 说明为空时退回文件名。 */
function attachmentDisplayName(label: string, fallbackPath?: string): string {
- const name = label.replace(/\s+/g, ' ').trim() || (fallbackPath ? path.basename(fallbackPath) : '');
+ const name = label.replace(/\s+/g, ' ').trim() || (fallbackPath ? path.win32.basename(fallbackPath) : '');
return name.length > 40 ? `${name.slice(0, 39)}…` : name;
}
diff --git a/apps/desktop/src/main/im/shared/__tests__/localMarkdownImages.test.ts b/apps/desktop/src/main/im/shared/__tests__/localMarkdownImages.test.ts
index 9379741cdb..3834cedf53 100644
--- a/apps/desktop/src/main/im/shared/__tests__/localMarkdownImages.test.ts
+++ b/apps/desktop/src/main/im/shared/__tests__/localMarkdownImages.test.ts
@@ -145,4 +145,14 @@ describe('materializeLocalMarkdownImageRefs', () => {
).toBe('前文\n![预览](cindy-media://blobs/a.png)\n失败\n后文\n\n有 1 张图片未能作为附件发送。');
expect(materialize).toHaveBeenCalledWith('s', '前文\n![预览](/tmp/a.png)\n![失败](/tmp/gone.png)\n后文');
});
+
+ it('drops image titles so card uploaders receive a bare managed URL', async () => {
+ const materialize = vi.fn(async () => '![预览](cindy-media://blobs/a.png "预览图")');
+ expect(
+ await materializeLocalMarkdownImageRefs(
+ { text: '![预览](/tmp/a.png "预览图")', sessionId: 's' },
+ { materialize },
+ ),
+ ).toBe('![预览](cindy-media://blobs/a.png)');
+ });
});
diff --git a/apps/desktop/src/main/im/shared/__tests__/turnRunnerSendOutcome.test.ts b/apps/desktop/src/main/im/shared/__tests__/turnRunnerSendOutcome.test.ts
index 2e4b730879..50ea89ac11 100644
--- a/apps/desktop/src/main/im/shared/__tests__/turnRunnerSendOutcome.test.ts
+++ b/apps/desktop/src/main/im/shared/__tests__/turnRunnerSendOutcome.test.ts
@@ -3104,6 +3104,18 @@ describe('turnRunner send outcome policy (feishu adapter characterization)', ()
expect(JSON.stringify(replace.mock.calls)).not.toContain('cindy-ui');
});
+ it('keeps a machine-local image path out of live card updates', async () => {
+ const replace = vi.fn();
+ mocks.feishuIm.startStreamingText.mockResolvedValue({
+ messageId: 'rich', append: vi.fn(), replace, finalize: vi.fn(), close: vi.fn(),
+ });
+ const h = setupSession(async () => ({ accepted: true }));
+ await runDefaultTurn();
+ h.emit({ type: 'text', data: { text: 'Look ![preview](/Users/alice/private/preview.png)', isFinal: false } });
+ await waitForAssertion(() => expect(replace).toHaveBeenCalledWith('Look preview'));
+ expect(JSON.stringify(replace.mock.calls)).not.toContain('/Users/alice');
+ });
+
it.each([
['```cindy-ui\n{"version":', 'Some content could not be displayed.', false],
['```cindy-ui\ninvalid\n```', 'Some content could not be displayed.', false],
diff --git a/apps/desktop/src/main/im/shared/localMarkdownImages.ts b/apps/desktop/src/main/im/shared/localMarkdownImages.ts
index e6d1675eda..16c9ade501 100644
--- a/apps/desktop/src/main/im/shared/localMarkdownImages.ts
+++ b/apps/desktop/src/main/im/shared/localMarkdownImages.ts
@@ -16,6 +16,8 @@ import {
rewriteTaskImageReferences,
} from '../../cindy-media/taskImageMarkdown';
+type TaskImageReferenceLike = ReturnType<typeof taskImageReferences>[number];
+
const DEFAULT_MAX_IMAGES = 4;
const DEFAULT_MAX_IMAGE_BYTES = 20 * 1024 * 1024;
@@ -144,10 +146,17 @@ export async function materializeLocalMarkdownImageRefs(
): Promise<string> {
const sourceText = await deps.materialize(params.sessionId, params.text);
const failed = new Map<string, string>();
+ const titled: TaskImageReferenceLike[] = [];
for (const ref of taskImageReferences(sourceText)) {
if (localTaskImagePath(ref.url)) failed.set(ref.url, ref.url);
+ // Card uploaders take the whole destination, so a quoted title would be read as part of the URL.
+ else if (ref.title && !ref.identifier && isManagedImageTarget(ref.url)) titled.push(ref);
+ }
+ let source = sourceText;
+ for (const ref of titled.reverse()) {
+ source = `${source.slice(0, ref.start)}![${ref.alt.replace(/[\\\[\]]/g, '\\$&')}](${ref.url})${source.slice(ref.end)}`;
}
- if (failed.size === 0) return sourceText;
- const body = rewriteTaskImageReferences(sourceText, failed, 'alt');
+ if (failed.size === 0) return source;
+ const body = rewriteTaskImageReferences(source, failed, 'alt');
return `${body.trimEnd()}\n\n有 ${failed.size} 张图片未能作为附件发送。`;
}
diff --git a/apps/desktop/src/main/im/shared/turnRunner.ts b/apps/desktop/src/main/im/shared/turnRunner.ts
index 4bd4f390ae..010cac76fc 100644
--- a/apps/desktop/src/main/im/shared/turnRunner.ts
+++ b/apps/desktop/src/main/im/shared/turnRunner.ts
@@ -161,6 +161,7 @@ import {
generateImSessionTitleText,
persistGeneratedSessionTitle,
} from './fbotTitle';
+import { localTaskImagePath, rewriteTaskImageReferences, taskImageReferences } from '../../cindy-media/taskImageMarkdown';
import { materializeLocalMarkdownImageRefs, materializeLocalMarkdownImages } from './localMarkdownImages';
import {
applyStatusTokenRate,
@@ -2943,7 +2944,16 @@ export function createTurnRunner(
/** Project only the outbound copy; persistence keeps the original component events. */
function composeOutboundBody(source: string, final: boolean): string {
- const body = stripInternalWebCitations(richAnswerText(source));
+ let body = stripInternalWebCitations(richAnswerText(source));
+ // Card channels patch the live body; a machine-local image path must not show before finalize imports it.
+ if (!final && output.kind === 'rich-card' && body.includes('![')) {
+ const local = new Map(
+ taskImageReferences(body)
+ .filter((ref) => localTaskImagePath(ref.url))
+ .map((ref) => [ref.url, ref.url] as const),
+ );
+ if (local.size > 0) body = rewriteTaskImageReferences(body, local, 'alt');
+ }
// A partial component can legitimately project to nothing. Never fall back
// to its protocol source; wait while streaming and explain an unreadable final.
return final && !body.trim() && hasRichAnswer(source) ? t('richAnswer.incomplete') : body;