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(desktop): 兼容旧格式本地图片链接 (#4991)

DavidShenXD1f63ff9c1a6f
diff --git a/apps/desktop/src/renderer/__tests__/markdownImagePathEncoding.test.ts b/apps/desktop/src/renderer/__tests__/markdownImagePathEncoding.test.ts
index 45d34a7f9c..28f3442821 100644
--- a/apps/desktop/src/renderer/__tests__/markdownImagePathEncoding.test.ts
+++ b/apps/desktop/src/renderer/__tests__/markdownImagePathEncoding.test.ts
@@ -77,6 +77,24 @@ describe('Markdown local image path encoding', () => {
}
});
+ it('normalizes legacy path-shaped xdt-file image URLs for the custom protocol', () => {
+ const path = 'D:/Users/test/My Pictures/image.png';
+ expect(normalizeMarkdownImageSrc(`xdt-file:///${path}`, '/repo', true)).toBe(
+ `xdt-file://local/?path=${encodeURIComponent(path)}`,
+ );
+ });
+
+ it('keeps legacy POSIX paths and cache-busting revisions intact', () => {
+ const path = '/Users/test/My Pictures/image.png';
+ expect(
+ normalizeMarkdownImageSrc(
+ 'xdt-file:///Users/test/My%20Pictures/image.png?v=message%202',
+ '/repo',
+ true,
+ ),
+ ).toBe('xdt-file://local/?path=' + encodeURIComponent(path) + '&v=message%202');
+ });
+
it('keeps malformed percent sequences instead of throwing during render', () => {
const path = '/tmp/100%done.png';
expect(normalizeMarkdownImageSrc(path, '/repo', true)).toBe(
diff --git a/apps/desktop/src/renderer/__tests__/markdownTarget.test.ts b/apps/desktop/src/renderer/__tests__/markdownTarget.test.ts
index cdf4f54b7b..c6acc41394 100644
--- a/apps/desktop/src/renderer/__tests__/markdownTarget.test.ts
+++ b/apps/desktop/src/renderer/__tests__/markdownTarget.test.ts
@@ -116,6 +116,10 @@ describe('classifyMarkdownLinkTarget', () => {
kind: 'local-image-url',
href: 'xdt-file://local/?path=%2Ftmp%2Fa.png',
});
+ expect(classifyMarkdownLinkTarget('xdt-file:///D:/tmp/a%20b.png')).toEqual({
+ kind: 'local-image-url',
+ href: `xdt-file://local/?path=${encodeURIComponent('D:/tmp/a b.png')}`,
+ });
});
it('turns explicit local links into pending local candidates until fs resolution proves them', () => {
diff --git a/apps/desktop/src/renderer/lib/localPathResolver.ts b/apps/desktop/src/renderer/lib/localPathResolver.ts
index 3d50cedd78..06da07591d 100644
--- a/apps/desktop/src/renderer/lib/localPathResolver.ts
+++ b/apps/desktop/src/renderer/lib/localPathResolver.ts
@@ -586,6 +586,35 @@ export function toLocalFileUrl(absPath: string, revision?: string): string {
return revision ? `${url}&v=${encodeURIComponent(revision)}` : url;
}
+/**
+ * Normalize the legacy path-shaped `xdt-file:///...` URL used by older
+ * messages into the query-shaped URL understood by the main-process handler.
+ *
+ * The custom protocol deliberately keeps the absolute path in `?path=` so
+ * Chromium cannot reinterpret a Windows drive letter as the URL host. Keep
+ * canonical `xdt-file://local/?path=...` URLs byte-for-byte unchanged; this
+ * helper is only a compatibility bridge for persisted/history messages.
+ */
+export function normalizeXdtFileUrlForRenderer(url: string): string {
+ if (!url.startsWith('xdt-file:///')) return url;
+
+ const queryOrHash = url.search(/[?#]/);
+ const rawPath = url.slice('xdt-file://'.length, queryOrHash >= 0 ? queryOrHash : undefined);
+ let absPath = safeDecodeURIComponent(rawPath);
+ // URL syntax adds a leading slash before a Windows drive letter.
+ if (/^\/[A-Za-z]:[\\/]/.test(absPath)) absPath = absPath.slice(1);
+ if (!(absPath.startsWith('/') || WIN_ABS_RE.test(absPath))) return url;
+
+ // Preserve the optional cache-busting revision used by renderer-created
+ // URLs, while ignoring unrelated legacy query parameters.
+ let revision: string | undefined;
+ if (queryOrHash >= 0 && url[queryOrHash] === '?') {
+ const match = /(?:^|&)v=([^&#]*)/.exec(url.slice(queryOrHash + 1));
+ if (match) revision = safeDecodeURIComponent(match[1]);
+ }
+ return toLocalFileUrl(absPath, revision);
+}
+
/**
* Normalize an original Markdown image destination for renderer loading.
*
@@ -615,11 +644,11 @@ export function normalizeMarkdownImageSrc(
src.startsWith('https://') ||
src.startsWith('xdt-image://') ||
src.startsWith('cindy-media://') ||
- src.startsWith('xdt-file://') ||
src.startsWith('cindy-remote-media://')
) {
return src;
}
+ if (src.startsWith('xdt-file://')) return normalizeXdtFileUrlForRenderer(src);
let localPath = src;
if (localPath.startsWith('file://')) {
diff --git a/apps/desktop/src/renderer/lib/markdownTarget.ts b/apps/desktop/src/renderer/lib/markdownTarget.ts
index b7a4aca4e0..4384de4013 100644
--- a/apps/desktop/src/renderer/lib/markdownTarget.ts
+++ b/apps/desktop/src/renderer/lib/markdownTarget.ts
@@ -2,6 +2,7 @@ import {
classifyMarkdownHref,
looksLikeDirectoryPath,
looksLikeFilePath,
+ normalizeXdtFileUrlForRenderer,
resolveKnownLocalFileHref,
type KnownLocalFileRef,
type LocalHrefKind,
@@ -295,7 +296,10 @@ export function classifyMarkdownLinkTarget(
if (raw.startsWith('#')) return { kind: 'anchor', id: decodeAnchorId(raw.slice(1)), href: raw };
if (raw.startsWith('xdt-audio://')) return { kind: 'audio', href: raw };
if (raw.startsWith('xdt-image://') || raw.startsWith('xdt-file://')) {
- return { kind: 'local-image-url', href: raw };
+ return {
+ kind: 'local-image-url',
+ href: raw.startsWith('xdt-file://') ? normalizeXdtFileUrlForRenderer(raw) : raw,
+ };
}
if (HTTP_URL_RE.test(raw)) return { kind: 'external', href: raw };