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): route local rich audio through the audio protocol

Jazera9d9fd2ae5cd
diff --git a/apps/desktop/src/main/security/__tests__/csp.test.ts b/apps/desktop/src/main/security/__tests__/csp.test.ts
index 8dd3b19f0d..71a76ee8e2 100644
--- a/apps/desktop/src/main/security/__tests__/csp.test.ts
+++ b/apps/desktop/src/main/security/__tests__/csp.test.ts
@@ -75,9 +75,17 @@ describe('buildContentSecurityPolicy — shared invariants', () => {
for (const scheme of ['xdt-image:', 'xdt-file:', 'xdt-video:', 'xdt-model:', 'cindy-remote-media:', 'cindy-media:']) {
expect(d['img-src']).toContain(scheme);
}
- for (const scheme of ['xdt-audio:', 'xdt-video:', 'cindy-remote-media:', 'cindy-media:']) {
+ // Local cindy-ui video uses xdt-file; audio uses the dedicated xdt-audio
+ // handler. Omitting either makes Chromium reject media before decoding.
+ for (const scheme of ['xdt-audio:', 'xdt-file:', 'xdt-video:', 'cindy-remote-media:', 'cindy-media:']) {
expect(d['media-src']).toContain(scheme);
}
+ for (const directive of ['script-src', 'connect-src', 'frame-src']) {
+ expect(d[directive]).not.toContain('xdt-file:');
+ expect(d[directive]).not.toContain('xdt-audio:');
+ }
+ expect(d['media-src']).not.toContain('*');
+ expect(d['media-src']).not.toContain('file:');
// data: / blob: needed for canvas, object URLs, inline assets.
expect(d['img-src']).toEqual(expect.arrayContaining(['data:', 'blob:']));
expect(d['media-src']).toEqual(expect.arrayContaining(['blob:']));
diff --git a/apps/desktop/src/main/security/csp.ts b/apps/desktop/src/main/security/csp.ts
index 96e0c2b324..b78933c412 100644
--- a/apps/desktop/src/main/security/csp.ts
+++ b/apps/desktop/src/main/security/csp.ts
@@ -56,6 +56,8 @@ const CUSTOM_IMG_SCHEMES = [
];
const CUSTOM_MEDIA_SCHEMES = [
'xdt-audio:',
+ // Local video (including cindy-ui) uses the extension-checked file handler.
+ 'xdt-file:',
'xdt-video:',
'xdt-image:',
'cindy-remote-media:',
diff --git a/apps/desktop/src/renderer/components/chat/MarkdownRenderer.tsx b/apps/desktop/src/renderer/components/chat/MarkdownRenderer.tsx
index dc37fb5726..0de1d0a6ac 100644
--- a/apps/desktop/src/renderer/components/chat/MarkdownRenderer.tsx
+++ b/apps/desktop/src/renderer/components/chat/MarkdownRenderer.tsx
@@ -2039,7 +2039,11 @@ export const MarkdownRenderer = memo(function MarkdownRenderer(props: MarkdownRe
if (node.kind === 'video' && youtubeVideo(raw)) return <VideoSitePreview src={raw} title={richString(node, 'alt', 'YouTube')} />;
const normalized = raw.startsWith('xdt-audio://') && props.allowPrivilegedLinks !== false ? raw : normalizeMarkdownImageSrc(raw, props.workingDir, props.allowPrivilegedLinks !== false);
if (!normalized) return null;
- const src = rewriteToRemoteMediaOrigin(normalized, props.currentSessionId ? toRemoteMediaOrigin(sessionFileCtx.origin, sessionFileCtx.workingDir) : undefined);
+ // Local audio has its own extension-checked protocol; xdt-file serves
+ // images/documents/video and rejects MP3. Preserve the encoded path and
+ // select the handler before wrapping the URL for a remote source device.
+ const playable = node.kind === 'audio' ? normalized.replace(/^xdt-file:/, 'xdt-audio:') : normalized;
+ const src = rewriteToRemoteMediaOrigin(playable, props.currentSessionId ? toRemoteMediaOrigin(sessionFileCtx.origin, sessionFileCtx.workingDir) : undefined);
return node.kind === 'audio' ? <InlineXdtAudioPlayer src={src} /> : <ChatVideoView src={src} filename={richString(node, 'alt', 'video')} variant="tool-output" sessionId={props.currentSessionId} />;
};
const parts = useMemo(() => props.renderRichAnswers ? splitRichAnswer(source) : [], [source, props.renderRichAnswers]);
diff --git a/apps/desktop/src/renderer/components/chat/__tests__/markdownVideoRenderer.test.tsx b/apps/desktop/src/renderer/components/chat/__tests__/markdownVideoRenderer.test.tsx
index f51f5a1979..e866191222 100644
--- a/apps/desktop/src/renderer/components/chat/__tests__/markdownVideoRenderer.test.tsx
+++ b/apps/desktop/src/renderer/components/chat/__tests__/markdownVideoRenderer.test.tsx
@@ -1,7 +1,10 @@
// @vitest-environment jsdom
-import { render, screen } from '@testing-library/react';
-import { describe, expect, it, vi } from 'vitest';
+import { cleanup, render, screen } from '@testing-library/react';
+import { afterEach, describe, expect, it, vi } from 'vitest';
+
+vi.mock('@/contexts/AuthContext', () => ({ useAuth: () => ({ user: { id: 'media-test' } }) }));
+afterEach(cleanup);
vi.mock('../ChatVideoView', () => ({
ChatVideoView: ({
@@ -32,6 +35,50 @@ vi.mock('../ChatSessionFileContext', async (importOriginal) => ({
import { parseRemoteMediaUrl } from '../../../../shared/remoteMediaUrl';
import { MarkdownRenderer } from '../MarkdownRenderer';
+// Production entry: cindy-ui audio must reach the audio-only handler, while
+// video retains xdt-file's streaming/Range support. The previous Markdown-only
+// cases never exercised rich media; a local .mp3 produced an unplayable URL.
+// Minimal regression: an audio node with src="试听.mp3" (unit/DOM tier).
+describe('MarkdownRenderer rich local media', () => {
+ const richMedia = (kind: string, src: string) => '```cindy-ui\n' + JSON.stringify({
+ version: 1, seq: 1, op: 'create', id: 'media', kind, props: { src },
+ }) + '\n```';
+
+ it.each([
+ ['试听 100%20.mp3', 'C:/workspace/试听 100%20.mp3'],
+ ['C:\\音乐\\主城 A.mp3', 'C:\\音乐\\主城 A.mp3'],
+ ['file:///C:/music/a%2520.mp3', 'C:/music/a%20.mp3'],
+ ['xdt-file://local/?path=C%3A%2Fmusic%2Fa.mp3', 'C:/music/a.mp3'],
+ ])('routes local audio %s through xdt-audio', (src, expectedPath) => {
+ const view = render(<MarkdownRenderer workingDir="C:/workspace" content={richMedia('audio', src)} renderRichAnswers />);
+ const audio = view.container.querySelector('audio');
+ expect(audio?.getAttribute('src')).toBe(`xdt-audio://local/?path=${encodeURIComponent(expectedPath)}`);
+ });
+
+ it.each(['xdt-audio://local/?path=%2Fmusic%2Fa.mp3', 'cindy-media://blobs/a.mp3', 'https://example.com/a.mp3'])('preserves playable audio URL %s', src => {
+ const view = render(<MarkdownRenderer workingDir="/workspace" content={richMedia('audio', src)} renderRichAnswers />);
+ expect(view.container.querySelector('audio')?.getAttribute('src')).toBe(src);
+ });
+
+ it('rewrites the corrected audio URL to its source device', () => {
+ const view = render(<MarkdownRenderer workingDir="/remote/proj" currentSessionId="s1" content={richMedia('audio', '试听.mp3')} renderRichAnswers />);
+ expect(parseRemoteMediaUrl(view.container.querySelector('audio')?.getAttribute('src') ?? '')).toEqual({
+ origin: { kind: 'device', deviceId: 'dev-1' },
+ origUrl: `xdt-audio://local/?path=${encodeURIComponent('/remote/proj/试听.mp3')}`,
+ });
+ });
+
+ it('keeps local video on the streaming file protocol', () => {
+ render(<MarkdownRenderer workingDir="/workspace" content={richMedia('video', 'clip.mp4')} renderRichAnswers />);
+ expect(screen.getByTestId('markdown-video').getAttribute('data-src')).toBe('xdt-file://local/?path=%2Fworkspace%2Fclip.mp4');
+ });
+
+ it('does not expose privileged audio in untrusted previews', () => {
+ const view = render(<MarkdownRenderer workingDir="/workspace" content={richMedia('audio', '/music/a.mp3')} renderRichAnswers allowPrivilegedLinks={false} />);
+ expect(view.container.querySelector('audio')).toBeNull();
+ });
+});
+
describe('MarkdownRenderer managed video targets', () => {
it.each([
'cindy-media://blobs/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.mp4',