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): open managed Markdown video links (#4219)

Dante4cbbd9fccbcb
diff --git a/apps/mobile/src/__tests__/messageMarkdown.test.ts b/apps/mobile/src/__tests__/messageMarkdown.test.ts
index 9a707206d0..11894c7886 100644
--- a/apps/mobile/src/__tests__/messageMarkdown.test.ts
+++ b/apps/mobile/src/__tests__/messageMarkdown.test.ts
@@ -349,6 +349,44 @@ describe('messageMarkdown', () => {
]);
});
+ it('classifies managed video Markdown links for the mobile media viewer', () => {
+ const hash = 'a'.repeat(64);
+ expect(parseMobileMarkdownInlines(
+ `播放 [海洋视频](cindy-media://blobs/${hash}.mp4) 继续`,
+ )).toEqual([
+ { type: 'text', text: '播放 ' },
+ {
+ type: 'link',
+ text: '海洋视频',
+ url: `cindy-media://blobs/${hash}.mp4`,
+ managedMediaKind: 'video',
+ },
+ { type: 'text', text: ' 继续' },
+ ]);
+
+ for (const extension of ['webm', 'mov']) {
+ expect(parseMobileMarkdownInlines(
+ `[video](cindy-media://blobs/${hash}.${extension})`,
+ )).toEqual([{
+ type: 'link',
+ text: 'video',
+ url: `cindy-media://blobs/${hash}.${extension}`,
+ managedMediaKind: 'video',
+ }]);
+ }
+ });
+
+ it('keeps unsupported or malformed cindy-media links as literal text', () => {
+ const hash = 'a'.repeat(64);
+ for (const input of [
+ `[image](cindy-media://blobs/${hash}.png)`,
+ '[video](cindy-media://blobs/not-a-sha.mp4)',
+ `[video](cindy-media://other/${hash}.mp4)`,
+ ]) {
+ expect(parseMobileMarkdownInlines(input)).toEqual([{ type: 'text', text: input }]);
+ }
+ });
+
it('does not swallow fullwidth parentheses or CJK punctuation after a bare URL', () => {
expect(parseMobileMarkdownInlines(
'诊断已写在 https://github.com/example/app/issues/3561#issuecomment-5391602790(无 @)。',
diff --git a/apps/mobile/src/__tests__/messageMediaThumbnailWiring.test.ts b/apps/mobile/src/__tests__/messageMediaThumbnailWiring.test.ts
index e553c23472..8666721301 100644
--- a/apps/mobile/src/__tests__/messageMediaThumbnailWiring.test.ts
+++ b/apps/mobile/src/__tests__/messageMediaThumbnailWiring.test.ts
@@ -88,12 +88,27 @@ describe('mobile message media thumbnail wiring', () => {
expect(pendingImage).not.toContain('Image.getSize');
expect(pendingBranch).toContain('<FileChip');
expect(pendingBranch).toContain('<MarkdownBody');
+ expect(pendingBranch).not.toContain('onOpenPayload=');
expect(pending.indexOf('<AttachmentThumbStrip')).toBeLessThan(pending.indexOf('{hasBody ?'));
expect(pending).toContain('item.fileNames.map(renderFile)');
expect(pending).not.toContain('height: 72');
expect(pending).not.toContain('contentFit="cover"');
});
+ it('does not expose a managed-media press handler without a payload viewer', () => {
+ const markdownBody = rendererSource.slice(
+ rendererSource.indexOf('function MarkdownBody'),
+ rendererSource.indexOf('function renderInline'),
+ );
+ expect(markdownBody).toContain('const openMarkdownMedia = useMemo(() => onOpenPayload');
+ expect(markdownBody).toContain(': undefined, [onOpenPayload]);');
+ const mediaCallback = markdownBody.slice(
+ markdownBody.indexOf('const openMarkdownMedia'),
+ markdownBody.indexOf('// Preserve the inline renderer'),
+ );
+ expect(mediaCallback).not.toContain('if (!onOpenPayload) return;');
+ });
+
it('exempts images from close-time release in the payload viewer', () => {
expect(rendererSource).toContain("remoteMedia.kind !== 'image'");
// 查看器取件插队头;重试按钮显式 forceRefresh 穿透负缓存(挂载取件不传)
diff --git a/apps/mobile/src/session/MessageRenderer.tsx b/apps/mobile/src/session/MessageRenderer.tsx
index 306cd73918..ac7ad51b48 100644
--- a/apps/mobile/src/session/MessageRenderer.tsx
+++ b/apps/mobile/src/session/MessageRenderer.tsx
@@ -5015,6 +5015,11 @@ function MarkdownBody({
markdownImageCacheKey,
onOpenPayload,
]);
+ const openMarkdownMedia = useMemo(() => onOpenPayload
+ ? (url: string, title: string, kind: 'video') => {
+ onOpenPayload(buildMediaPayload({ kind, url, title, previewable: false }, title));
+ }
+ : undefined, [onOpenPayload]);
// Preserve the inline renderer while streaming or unrelated task metadata
// changes; referenced task title changes still refresh every affected chip.
const remoteSessions = useRemoteSessions();
@@ -5039,6 +5044,7 @@ function MarkdownBody({
baseStyle,
keyPrefix,
onOpenImage: openMarkdownImage,
+ onOpenMedia: openMarkdownMedia,
onOpenSessionLink,
sessionReferenceDetails,
sessionLinkTitles,
@@ -5048,7 +5054,7 @@ function MarkdownBody({
),
// renderInline also reads translated fallback labels. Invalidate completed
// memoized text blocks when useTranslation refreshes its bound translator.
- [onOpenSessionLink, openMarkdownImage, sessionLinkTitles, sessionReferenceDetails, streaming, styles, t],
+ [onOpenSessionLink, openMarkdownImage, openMarkdownMedia, sessionLinkTitles, sessionReferenceDetails, streaming, styles, t],
);
const textRunGroupingOptions = Platform.OS === 'android'
? ANDROID_SELECTABLE_TEXT_RUN_GROUPING_OPTIONS
@@ -5566,6 +5572,7 @@ function renderInline(
/** text_run 合并树里多个块共父,key 需要块级前缀防冲突。 */
keyPrefix?: string;
onOpenImage?: (url: string, alt?: string) => void;
+ onOpenMedia?: (url: string, title: string, kind: 'video') => void;
onOpenPayload?: (payload: MessagePayload) => void;
onOpenSessionLink?: (url: string) => void;
sessionReferenceDetails?: Readonly<Record<string, string>>;
@@ -5591,6 +5598,21 @@ function renderInline(
case 'text':
return <SpanText key={spanKey(`text:${index}`)} style={ctx.baseStyle}>{inline.text}</SpanText>;
case 'link': {
+ if (inline.managedMediaKind) {
+ const mediaKind = inline.managedMediaKind;
+ const openManagedMedia = ctx.onOpenMedia
+ ? () => ctx.onOpenMedia?.(inline.url, inline.text, mediaKind)
+ : undefined;
+ return (
+ <SpanText
+ key={spanKey(`media-link:${index}:${inline.url}`)}
+ onPress={openManagedMedia}
+ style={clickableInlineStyle(styles, openManagedMedia, ctx.baseStyle)}
+ >
+ {inline.text}
+ </SpanText>
+ );
+ }
const session = parseSessionDeepLinkUrl(inline.url);
if (session) {
return (
diff --git a/apps/mobile/src/session/messageMarkdown.ts b/apps/mobile/src/session/messageMarkdown.ts
index 3c84902f13..b3ad2830d4 100644
--- a/apps/mobile/src/session/messageMarkdown.ts
+++ b/apps/mobile/src/session/messageMarkdown.ts
@@ -19,7 +19,7 @@ export type MobileMarkdownInline =
// 普通正文,套等宽会让同一句里点亮/未点亮的路径在字体、底色、下划线三处齐变;作者
// 手写且 label 像文件名的仍保留 chip(那是作者的排版意图)。见 DESIGN.md §14.5。
// 与桌面 remarkLocalPathLinks 打的 data-bare-path 标记是同一件事的两端实现。
- | { type: 'link'; text: string; url: string; bare?: true }
+ | { type: 'link'; text: string; url: string; bare?: true; managedMediaKind?: 'video' }
| { type: 'strong'; text: string }
| { type: 'emphasis'; text: string }
| { type: 'code'; text: string }
@@ -1082,19 +1082,25 @@ function findNextInlineToken(
// 兼容存量消息)session|project/(session 渲染成会话 chip;project 在
// renderInline 里显示 label 纯文本,不落 Linking.openURL——桌面端粘贴
// chip 化后会按 [标题](深链) 发送,不 tokenize 会把整段渲染成原始
- // markdown 源码,review P1)。
+ // markdown 源码,review P1)。cindy-media 只收字节仓严格地址中的视频
+ // 后缀;由 managedMediaKind 让渲染层走既有媒体查看器/远端取件,绝不把
+ // 私有 scheme 交给 Linking.openURL。
matchRegex(
input,
from,
new RegExp(
- `\\[([^\\]]+)\\]\\(((?:https?://|(?:${DEEP_LINK_SCHEME_GROUP})://(?:session|project)/)[^)\\s]+)\\)`,
+ `\\[([^\\]]+)\\]\\(((?:https?://|(?:${DEEP_LINK_SCHEME_GROUP})://(?:session|project)/)[^)\\s]+|cindy-media://blobs/[0-9a-f]{64}\\.(?:mp4|webm|mov))\\)`,
'g',
),
- (match) => ({
- type: 'link' as const,
- text: match[1],
- url: trimUrlPunctuation(match[2]),
- }),
+ (match) => {
+ const url = trimUrlPunctuation(match[2]);
+ return {
+ type: 'link' as const,
+ text: match[1],
+ url,
+ ...(url.startsWith('cindy-media://') ? { managedMediaKind: 'video' as const } : {}),
+ };
+ },
),
// 本地路径链接:[README.md](/abs/path/README.md:17) 这类模型高频输出形态。
// URL 经 classifyChatPathLinkTarget 判形状(http/session 之外的路径形态才收),