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(companion-cloud): MC name reply image attachments by their alt text or image-<hash prefix>

nana5c12a93f0a7f
diff --git a/apps/desktop/src/main/companion-cloud/__tests__/replyArtifacts.test.ts b/apps/desktop/src/main/companion-cloud/__tests__/replyArtifacts.test.ts
index 8bf7f52759..1876166f4a 100644
--- a/apps/desktop/src/main/companion-cloud/__tests__/replyArtifacts.test.ts
+++ b/apps/desktop/src/main/companion-cloud/__tests__/replyArtifacts.test.ts
@@ -103,6 +103,16 @@ describe('images made by tools during the turn', () => {
});
expect(plain.map(file => file.url)).toEqual([url('b')]);
});
+
+ it('names an image by the alt text the reply gave it, else image-<hash prefix>, never the 64-digit hash', async () => {
+ const named = url('a'); const unnamed = url('b'); const tool = url('c'); const messy = url('d');
+ const files = await replyArtifactAttachments({ id: 'm1', createdAt: 100 },
+ `![绿色小青蛙](${named}) ![](${unnamed}) ![a/b:c*"d"](${messy}) ![ignored](https://x/y.png)\n\n\`\`\`\n![in code](${tool})\n\`\`\``, 's1', {
+ roots: async () => [], maxBytes: 1000, blob: async () => ({ mimeType: 'image/png', size: 1 }),
+ turnImages: async () => [tool],
+ });
+ expect(files.map(file => file.name)).toEqual(['绿色小青蛙.png', `image-${'b'.repeat(8)}.png`, 'a b c d.png', `image-${'c'.repeat(8)}.png`]);
+ });
});
describe('replyTextWithFiles', () => {
diff --git a/apps/desktop/src/main/companion-cloud/replyArtifacts.ts b/apps/desktop/src/main/companion-cloud/replyArtifacts.ts
index 506cb45163..d9154871ea 100644
--- a/apps/desktop/src/main/companion-cloud/replyArtifacts.ts
+++ b/apps/desktop/src/main/companion-cloud/replyArtifacts.ts
@@ -80,6 +80,20 @@ export function toolResultImageUrls(rawContent: string | null): string[] {
return [...new Set(candidates.filter((url): url is string => typeof url === 'string' && /^cindy-media:\/\/blobs\/[0-9a-f]{64}\.[a-z0-9]+$/.test(url)))];
}
+const ATTACHMENT_NAME_MAX = 60;
+
+/**
+ * The name a stored media blob gets in the room: the alt text the reply gave that image (`![alt](address)`),
+ * else `image-<first 8 of the hash>`, with the address's extension. The address's own last segment is a 64-digit hash.
+ */
+export function blobAttachmentName(url: string, text: string, category: 'image' | 'file'): string {
+ const [, hash = '', ext = ''] = /\/blobs\/([0-9a-f]{64})\.([a-z0-9]+)$/.exec(url) ?? [];
+ const prose = text.replace(/```[\s\S]*?(?:```|$)/g, ' ');
+ const alt = [...prose.matchAll(/!\[([^\]\n]*)\]\(\s*<?([^)>\s]+)/g)].find(match => match[2] === url)?.[1] ?? '';
+ const label = alt.replace(/[\\/:*?"<>|\u0000-\u001f]/g, ' ').replace(/\s+/g, ' ').trim().slice(0, ATTACHMENT_NAME_MAX).replace(/[. ]+$/, '');
+ return `${label || `${category}-${hash.slice(0, 8)}`}.${ext}`;
+}
+
function safeDecode(value: string): string {
try { return decodeURIComponent(value); } catch { return value; }
}
@@ -127,7 +141,8 @@ export async function replyArtifactAttachments(
try {
const blob = await deps.blob(url, sessionId);
if (!blob || blob.size <= 0 || blob.size > deps.maxBytes) continue;
- out.push({ id: `${row.id}:reply:${out.length}`, name: url.slice(url.lastIndexOf('/') + 1), category: blob.mimeType.startsWith('image/') ? 'image' : 'file',
+ const category = blob.mimeType.startsWith('image/') ? 'image' : 'file';
+ out.push({ id: `${row.id}:reply:${out.length}`, name: blobAttachmentName(url, text, category), category,
mimeType: blob.mimeType, size: blob.size, url, path: null });
} catch (error) { deps.log?.warn('Reply media not sent to the room', { code: (error as NodeJS.ErrnoException)?.code ?? 'UNKNOWN' }); }
}
diff --git a/docs/product-rules/cloud-companions.md b/docs/product-rules/cloud-companions.md
index c35249e0a8..d4d87f28c9 100644
--- a/docs/product-rules/cloud-companions.md
+++ b/docs/product-rules/cloud-companions.md
@@ -149,6 +149,10 @@
取不到(地址过期、网络失败、附件已删)时提示失败,再点一次即重试。只有附件没有文字的消息照常显示,没有文字气泡;
附件名不再作为文字重复显示。手机读的是房间原始消息,不含桌面端换电脑导入时追加在正文后的图片 / 文件链接,所以不会重复。
不认识的内容块不显示;没有名字的附件显示「附件」占位名。
+ 桌面端给回复里的媒体地址(`cindy-media://`)取附件名:正文里该图 `![alt](地址)` 的 alt 非空就用「alt.扩展名」
+ (去掉非法文件名字符、限 60 字),否则用「image-前 8 位哈希.扩展名」;工具生成、正文未点名的图同样按后者,不再用 64 位哈希。
+ 手机云端对话里,伙伴回复正文中桌面私有协议(`cindy-media://`、`xdt-image://`、`xdt-video://`、`file://`)的图片 markdown
+ 不显示(同一张图已在附件条里);http(s) 图片、普通链接和代码块里的写法原样保留。
- 房间里由主人直接发出的消息(例如未来的离线排队)在主力电脑上线后由它领取,作为「来自其他设备」的
消息投递进主任务;伙伴回复由导入同步回房间,不重复写。主任务未在运行时按它保存的权限档唤醒,不借后台唤醒的默认完全访问。房间消息等同同账号设备远程操作这台电脑,只在这台电脑开启「允许同账号设备控制本机」时投递(否则该条执行失败、不进主任务);房间消息没有经认证的设备标识,无法按「已撤销的控制设备」逐台拒绝。