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): 官方渠道图片未送达时说明原因并点名附件

Dashcd7e2d0dfa09
diff --git a/apps/desktop/src/main/cindy-media/taskImageDelivery.ts b/apps/desktop/src/main/cindy-media/taskImageDelivery.ts
index 3248899c70..83e2aaae00 100644
--- a/apps/desktop/src/main/cindy-media/taskImageDelivery.ts
+++ b/apps/desktop/src/main/cindy-media/taskImageDelivery.ts
@@ -185,8 +185,9 @@ async function captureTaskImages(sessionId: string, client?: DbClient) {
compensate: remove,
});
},
- materialize: (text: string) =>
+ materialize: (text: string, onFailure?: (source: string, error: unknown) => void) =>
materializeTaskImageMarkdown(text, {
+ onFailure,
importImage: async (source) => {
const image = await readTaskImage(source, roots);
assertStillValid();
@@ -227,25 +228,38 @@ export async function materializeTaskImageText(sessionId: string, text: string):
}
/** Keep the existing replacement table so projections share the same imported bytes. */
-export async function materializeTaskImageTextResult(sessionId: string, text: string) {
+export async function materializeTaskImageTextResult(
+ sessionId: string,
+ text: string,
+ log?: { warn(msg: string): void },
+) {
const client = getCurrentDbClientSnapshot()?.client;
const unchanged = { text, replacements: new Map<string, string>(), failures: [] as string[] };
if (!client || !hasLocalTaskImages(text)) return unchanged;
return withSessionMediaRefLock(client.drizzle, sessionId, () =>
- materializeTaskImageTextUnlocked(client, sessionId, unchanged),
+ materializeTaskImageTextUnlocked(client, sessionId, unchanged, log),
);
}
+const errorMessage = (error: unknown): string =>
+ error instanceof Error ? error.message : String(error);
+
async function materializeTaskImageTextUnlocked(
client: DbClient,
sessionId: string,
unchanged: Awaited<ReturnType<typeof materializeTaskImageMarkdown>>,
+ log?: { warn(msg: string): void },
) {
let scope: Awaited<ReturnType<typeof captureTaskImages>> = null;
try {
scope = await captureTaskImages(sessionId, client);
- if (!scope) return unchanged;
- const result = await scope.materialize(unchanged.text);
+ if (!scope) {
+ log?.warn('task image import skipped: task is unavailable on this host');
+ return unchanged;
+ }
+ const result = await scope.materialize(unchanged.text, (source, error) => {
+ log?.warn(`task image import failed (${path.basename(source)}): ${errorMessage(error)}`);
+ });
scope.assertStillValid();
const [current] = await scope.db
.select({
@@ -269,7 +283,8 @@ async function materializeTaskImageTextUnlocked(
)
throw new Error('task-image: task changed');
return result;
- } catch {
+ } catch (error) {
+ log?.warn(`task image import abandoned: ${errorMessage(error)}`);
await scope?.rollback().catch(() => {});
return unchanged;
}
diff --git a/apps/desktop/src/main/cindy-media/taskImageMarkdown.ts b/apps/desktop/src/main/cindy-media/taskImageMarkdown.ts
index e02221be07..f283684ada 100644
--- a/apps/desktop/src/main/cindy-media/taskImageMarkdown.ts
+++ b/apps/desktop/src/main/cindy-media/taskImageMarkdown.ts
@@ -159,6 +159,8 @@ export function isPathWithin(root: string, target: string): boolean {
export interface TaskImageMaterializeDeps {
/** Returns a stable managed URL. Validates scope and image bytes before importing. */
importImage(source: string): Promise<string>;
+ /** Diagnostics only; a failed image still keeps its original reference. */
+ onFailure?(source: string, error: unknown): void;
}
export async function materializeTaskImageMarkdown(text: string, deps: TaskImageMaterializeDeps) {
@@ -169,8 +171,9 @@ export async function materializeTaskImageMarkdown(text: string, deps: TaskImage
if (!source || replacements.has(ref.url) || failures.includes(ref.url)) continue;
try {
replacements.set(ref.url, await deps.importImage(source));
- } catch {
+ } catch (error) {
failures.push(ref.url);
+ deps.onFailure?.(source, error);
}
}
return { text: rewriteTaskImageReferences(text, replacements), replacements, failures };
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 8960f23eee..3e85da363f 100644
--- a/apps/desktop/src/main/hook-control/__tests__/outbound.test.ts
+++ b/apps/desktop/src/main/hook-control/__tests__/outbound.test.ts
@@ -221,7 +221,28 @@ describe('collectOutboundAttachments', () => {
expect(r.text).not.toContain('已作为附件发送');
expect(r.text).toContain('🖼️ _a_');
expect(r.text).toContain('b');
- expect(r.text).toContain('Attachment delivery incomplete: 2 items');
+ expect(r.text).toContain('Attachment delivery incomplete: 2 items could not be sent (a, b).');
+ });
+
+ it('未导入的本地图片按说明点名, 提示文案由调用方按界面语言给出', async () => {
+ // 2026-10-26 Slack 实踩: 工作目录外的预览图没有入库, 群里只看到英文计数,
+ // 不知道是哪几张没发出去。
+ const r = await collectOutboundAttachments(
+ '![首页纪念行](/tmp/preview/banner.png)\n\n![](/tmp/preview/stars-dark.png)',
+ [],
+ {
+ resolveImageUrl: () => {
+ throw new Error('local images are never resolved here');
+ },
+ formatIncompleteNotice: (names) => `未发送:${names.join('、')}`,
+ log,
+ },
+ );
+ expect(r.attachments).toHaveLength(0);
+ expect(r.skipped).toBe(2);
+ expect(r.text).toContain('🖼️ _首页纪念行_');
+ expect(r.text).not.toContain('/tmp/preview');
+ expect(r.text).toContain('未发送:首页纪念行、stars-dark.png');
});
it('refScanText: 引用范围可宽于正文, 正文变换仍只作用于正文', async () => {
diff --git a/apps/desktop/src/main/hook-control/outbound.ts b/apps/desktop/src/main/hook-control/outbound.ts
index 736c911e19..378c0e3e5a 100644
--- a/apps/desktop/src/main/hook-control/outbound.ts
+++ b/apps/desktop/src/main/hook-control/outbound.ts
@@ -66,7 +66,10 @@ export function buildHookPromptNote(im: string | undefined): string {
'`[文件名](xdt-file:///绝对路径)`;图片写 ' +
'`![说明](工具返回的媒体地址或当前任务图片的绝对路径)`。' +
'不要给本地路径拼接 xdt-image:// 或 cindy-media://。' +
- `Cindy 会保存可读取的任务图片,并在回复结束后作为 ${platform} 附件发回,无需另调导入工具;` +
+ '当前工作目录内的 PNG/JPEG/GIF/WebP 图片会自动保存,' +
+ `并在回复结束后作为 ${platform} 附件发回;` +
+ '其它位置的图片(如 /tmp、下载目录)先用 media 工具的 import_image 导入,再引用返回的地址;' +
+ 'SVG 等其它格式先转成 PNG。' +
'xdt-file 文件必须位于当前工作目录内;无法读取、超限或目录外的附件不会发送,' +
'最终回复会明确显示附件发送不完整。' +
'不要用 cindy_feishu_bot 发送,除非用户明确要求发到飞书。';
@@ -172,6 +175,8 @@ export interface OutboundDeps {
* 正文变换仍然只作用于 finalText —— 这里扩大的只是"哪些引用要被收集成附件"。
*/
refScanText?: string;
+ /** 附件未全部发出时附加在正文末尾的提示; 省略 = 英文默认提示。 */
+ formatIncompleteNotice?: (skippedNames: string[]) => string;
/** realpath 校验(生产: fs.promises.realpath; 测试注入)。 */
realpath?: (absPath: string) => Promise<string>;
/** 读文件字节(生产: fs.promises.readFile)。 */
@@ -187,6 +192,19 @@ export interface OutboundResult {
skipped: number;
}
+/** 提示里附件名的展示形态: 单行、限长; 说明为空时退回文件名。 */
+function attachmentDisplayName(label: string, fallbackPath?: string): string {
+ const name = label.replace(/\s+/g, ' ').trim() || (fallbackPath ? path.basename(fallbackPath) : '');
+ return name.length > 40 ? `${name.slice(0, 39)}…` : name;
+}
+
+function defaultIncompleteNotice(skippedNames: string[]): string {
+ const count = skippedNames.length;
+ const named = skippedNames.filter(Boolean);
+ return `⚠️ Attachment delivery incomplete: ${count} item${count === 1 ? '' : 's'} could not be sent`
+ + (named.length > 0 ? ` (${named.join(', ')}).` : '.');
+}
+
// Remote output may use another OS's path syntax. Recognize it for redaction
// only; this does not grant the local importer access to that path.
function isLocalImage(url: string): boolean {
@@ -253,7 +271,10 @@ export async function collectOutboundAttachments(
: MAX_OUT_IMAGE_BYTES;
const fileAbsPathByUrl = new Map<string, string | null>();
let totalBytes = 0;
- let skipped = 0;
+ const skippedNames: string[] = [];
+ const skip = (name: string): void => {
+ skippedNames.push(name);
+ };
const isAllowedFilePath = async (absPath: string): Promise<boolean> => {
const roots =
@@ -285,24 +306,25 @@ export async function collectOutboundAttachments(
const push = async (
absPath: string,
maxBytes: number,
+ name: string,
mimeOverride?: string,
): Promise<boolean> => {
if (deps.provider === 'x' && sentMediaPaths.has(absPath)) return true;
if (deps.provider === 'x' && ![
'image/jpeg', 'image/png', 'image/webp', 'image/gif', 'video/mp4', 'video/quicktime',
].includes(mimeOverride ?? guessMime(absPath))) {
- skipped += 1;
+ skip(name);
return false;
}
if (attachments.length >= MAX_OUT_ATTACHMENTS) {
- skipped += 1;
+ skip(name);
return false;
}
let bytes: Buffer;
try {
bytes = await readFile(absPath);
} catch (err) {
- skipped += 1;
+ skip(name);
deps.log.warn(
`outbound attachment read failed (${absPath}): ${err instanceof Error ? err.message : String(err)}`,
);
@@ -313,7 +335,7 @@ export async function collectOutboundAttachments(
bytes.length > maxBytes ||
totalBytes + bytes.length > MAX_OUT_TOTAL_BYTES
) {
- skipped += 1;
+ skip(name);
deps.log.warn(`outbound attachment skipped by size (${absPath}: ${bytes.length} bytes)`);
return false;
}
@@ -334,6 +356,7 @@ export async function collectOutboundAttachments(
// 1. 图片: 文本引用 + tool_result 旁路, 按 absPath 去重(模型常重复引用)
const imageAbsPaths: string[] = [];
const seenImage = new Set<string>();
+ const imageNameByPath = new Map<string, string>();
const imageRefs = taskImageReferences(refScanText).filter((ref) => isLocalImage(ref.url)
|| ref.url.startsWith('xdt-image://') || ref.url.startsWith('cindy-media://'));
const visitedImageUrls = new Set<string>();
@@ -341,15 +364,19 @@ export async function collectOutboundAttachments(
if (visitedImageUrls.has(ref.url)) continue;
visitedImageUrls.add(ref.url);
// Local images must already have been imported by the task-scoped adapter.
- if (isLocalImage(ref.url)) { skipped += 1; continue; }
+ if (isLocalImage(ref.url)) {
+ skip(attachmentDisplayName(ref.alt, localTaskImagePath(ref.url) ?? ref.url));
+ continue;
+ }
try {
const { absPath } = deps.resolveImageUrl(ref.url);
if (!seenImage.has(absPath)) {
seenImage.add(absPath);
imageAbsPaths.push(absPath);
+ imageNameByPath.set(absPath, attachmentDisplayName(ref.alt, absPath));
}
} catch (err) {
- skipped += 1;
+ skip(attachmentDisplayName(ref.alt));
deps.log.warn(
`resolve xdt-image failed (${ref.url}): ${err instanceof Error ? err.message : String(err)}`,
);
@@ -362,7 +389,7 @@ export async function collectOutboundAttachments(
}
}
for (const absPath of imageAbsPaths) {
- await push(absPath, mediaLimit(absPath));
+ await push(absPath, mediaLimit(absPath), imageNameByPath.get(absPath) ?? attachmentDisplayName('', absPath));
}
if (deps.provider === 'x') {
@@ -372,9 +399,9 @@ export async function collectOutboundAttachments(
seenLinks.add(ref.url);
try {
const { absPath } = deps.resolveImageUrl(ref.url);
- await push(absPath, mediaLimit(absPath));
+ await push(absPath, mediaLimit(absPath), attachmentDisplayName(ref.label, absPath));
} catch {
- skipped += 1;
+ skip(attachmentDisplayName(ref.label));
deps.log.warn('outbound managed media link could not be read');
}
}
@@ -391,17 +418,18 @@ export async function collectOutboundAttachments(
fileAbsPathByUrl.set(url, absPath);
} catch {
fileAbsPathByUrl.set(url, null);
- skipped += 1;
+ skip(attachmentDisplayName(ref.alt));
deps.log.warn('outbound file attachment skipped because xdt-file URL was invalid');
continue;
}
if (seenFile.has(absPath)) continue;
seenFile.add(absPath);
+ const name = attachmentDisplayName(ref.alt, absPath);
if (!(await isAllowedFilePath(absPath))) {
- skipped += 1;
+ skip(name);
continue;
}
- if (await push(absPath, deps.provider === 'x' ? mediaLimit(absPath) : MAX_OUT_FILE_BYTES)) sentFileAbsPaths.add(absPath);
+ if (await push(absPath, deps.provider === 'x' ? mediaLimit(absPath) : MAX_OUT_FILE_BYTES, name)) sentFileAbsPaths.add(absPath);
}
// Collection is not a delivery receipt. Keep captions without claiming the channel sent them.
@@ -433,10 +461,8 @@ export async function collectOutboundAttachments(
return absPath !== null && absPath !== undefined && sentFileAbsPaths.has(absPath) ? '' : alt;
},
});
- const warning =
- skipped > 0
- ? `⚠️ Attachment delivery incomplete: ${skipped} item${skipped === 1 ? '' : 's'} could not be sent.`
- : '';
+ const skipped = skippedNames.length;
+ const warning = skipped > 0 ? (deps.formatIncompleteNotice ?? defaultIncompleteNotice)(skippedNames) : '';
const text = warning
? `${transformed.trimEnd()}${transformed.trim().length > 0 ? '\n\n' : ''}${warning}`
: transformed;
diff --git a/apps/desktop/src/main/hook-control/session-runner.ts b/apps/desktop/src/main/hook-control/session-runner.ts
index 1cf109d41d..b5816bbb98 100644
--- a/apps/desktop/src/main/hook-control/session-runner.ts
+++ b/apps/desktop/src/main/hook-control/session-runner.ts
@@ -152,7 +152,7 @@ import {
registerHookInteraction,
} from './interactions.js';
import { collectOutboundAttachments, buildHookPromptNote, hasOutboundRefs } from './outbound.js';
-import { getResolvedMainLocale } from '../i18n.js';
+import { getResolvedMainLocale, t } from '../i18n.js';
import { buildUiLanguageErrorNote } from '../maker-ipc/uiLanguageErrorNote.js';
type MainOwnedImChannel = Extract<TurnPermissionOrigin, { kind: 'im' }>['channel'];
@@ -567,6 +567,16 @@ function turnTextsFor(observer: HookTurnObserver): HookTurnTexts {
};
}
+/** 附件未发出提示跟随界面语言, 并点名没发出的附件。 */
+function formatHookIncompleteNotice(skippedNames: string[]): string {
+ const locale = getResolvedMainLocale();
+ const separator = locale === 'en' || locale === 'ko' ? ', ' : '、';
+ const names = skippedNames.map((name) => name || t('imBot.unnamedAttachment', locale));
+ return t('imBot.outboundAttachmentsIncomplete', locale)
+ .replace('{{count}}', String(skippedNames.length))
+ .replace('{{names}}', names.join(separator));
+}
+
/**
* 收口时的出站附件收集(run() 与 watchContinuation 共用)。
*
@@ -594,7 +604,7 @@ async function collectOutboundForFinalText(
// once so a temporary source changing cannot split the body from its attachment.
const scanText = stripInternalWebCitations(texts.wholeTurn);
const materialized = canReadLocal
- ? await materializeTaskImageTextResult(sessionId, scanText)
+ ? await materializeTaskImageTextResult(sessionId, scanText, log)
: { text: scanText, replacements: new Map<string, string>() };
const wholeTurn = materialized.text;
const publicText = texts.wholeTurn === texts.publicText ? wholeTurn
@@ -610,6 +620,7 @@ async function collectOutboundForFinalText(
allowedFileRoots,
provider,
...(wholeTurn !== publicText ? { refScanText: wholeTurn } : {}),
+ formatIncompleteNotice: formatHookIncompleteNotice,
log,
});
if (collected.skipped > 0) {
diff --git a/apps/desktop/src/renderer/i18n/locales/en/common.json b/apps/desktop/src/renderer/i18n/locales/en/common.json
index f009b17d78..46e2939b73 100644
--- a/apps/desktop/src/renderer/i18n/locales/en/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/en/common.json
@@ -10661,7 +10661,9 @@
"suggestion2": "2. Create your own dedicated App on the {{service}} Open Platform so you and your colleague each have your own.",
"dismiss": "Dismiss",
"createOwnApp": "Create my App ↗"
- }
+ },
+ "outboundAttachmentsIncomplete": "⚠️ Attachment delivery incomplete: {{count}} not sent ({{names}}).",
+ "unnamedAttachment": "Untitled attachment"
},
"markdown": {},
"diffPanel": {
diff --git a/apps/desktop/src/renderer/i18n/locales/ja/common.json b/apps/desktop/src/renderer/i18n/locales/ja/common.json
index da0d3ce0af..9f6f8515cc 100644
--- a/apps/desktop/src/renderer/i18n/locales/ja/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/ja/common.json
@@ -10626,7 +10626,9 @@
"suggestion2": "2. {{service}} オープンプラットフォームで自分専用の App を別途作成し、同僚と分けて使用する。",
"dismiss": "閉じる",
"createOwnApp": "自分の App を作成 ↗"
- }
+ },
+ "outboundAttachmentsIncomplete": "⚠️ {{count}} 件の添付ファイルを送信できませんでした:{{names}}",
+ "unnamedAttachment": "名前のない添付ファイル"
},
"markdown": {},
"diffPanel": {
diff --git a/apps/desktop/src/renderer/i18n/locales/ko/common.json b/apps/desktop/src/renderer/i18n/locales/ko/common.json
index f4c3ea4721..388f2454a5 100644
--- a/apps/desktop/src/renderer/i18n/locales/ko/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/ko/common.json
@@ -10626,7 +10626,9 @@
"suggestion2": "2. {{service}} 오픈 플랫폼에서 자신만의 별도 App을 만들어 동료와 각자 사용합니다.",
"dismiss": "무시",
"createOwnApp": "내 App 만들기 ↗"
- }
+ },
+ "outboundAttachmentsIncomplete": "⚠️ 첨부 파일 {{count}}개를 보내지 못했습니다: {{names}}",
+ "unnamedAttachment": "이름 없는 첨부 파일"
},
"markdown": {},
"diffPanel": {
diff --git a/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json b/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json
index 47a3c4b687..f4f1aa8b5b 100644
--- a/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json
@@ -10626,7 +10626,9 @@
"suggestion2": "2. 到 {{service}} 开放平台为自己另建一个独立 App,与同事各用各的。",
"dismiss": "关闭",
"createOwnApp": "创建我的 App ↗"
- }
+ },
+ "outboundAttachmentsIncomplete": "⚠️ 有 {{count}} 个附件未能发送:{{names}}",
+ "unnamedAttachment": "未命名附件"
},
"markdown": {},
"diffPanel": {
diff --git a/apps/desktop/src/renderer/i18n/locales/zh-TW/common.json b/apps/desktop/src/renderer/i18n/locales/zh-TW/common.json
index 0fc55911ca..149ff690e4 100644
--- a/apps/desktop/src/renderer/i18n/locales/zh-TW/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/zh-TW/common.json
@@ -10626,7 +10626,9 @@
"suggestion2": "2. 到 {{service}} 開放平臺為自己另建一個獨立 App,與同事各用各的。",
"dismiss": "關閉",
"createOwnApp": "建立我的 App ↗"
- }
+ },
+ "outboundAttachmentsIncomplete": "⚠️ 有 {{count}} 個附件未能傳送:{{names}}",
+ "unnamedAttachment": "未命名附件"
},
"markdown": {},
"diffPanel": {
diff --git a/docs/dev-rules/media-storage-and-protocols.md b/docs/dev-rules/media-storage-and-protocols.md
index 3efb52e38c..854fa59ba9 100644
--- a/docs/dev-rules/media-storage-and-protocols.md
+++ b/docs/dev-rules/media-storage-and-protocols.md
@@ -310,6 +310,9 @@
Agent transcript 不改写,单张失败不阻塞正文或其它图片。IM 出站复用同一导入入口。
官方渠道只对整轮内容导入一次,最终正文复用该次导入的地址替换表;正文与附件扫描
指向同一副本,不因临时源文件在收口时删除或替换而再次读取出不同结果。
+ 官方渠道的每张导入失败都要把原因(目录外、格式不支持、超限等)写入 main 日志;正文末尾的
+ 未送达提示按界面语言点名未发出的附件。渠道说明须告知 Agent 自动保存的范围,工作目录外的
+ 图片先用 `import_image` 导入,SVG 等格式先转成 PNG。
SSH 官方渠道不导入或读取控制端文件,仍复用出站引用清理:本地图片地址(含远端
Windows 路径)只保留说明,并提示附件发送不完整,不将不可用的媒体地址发到 IM。
与图片共用目标的普通链接也指向保存后的同一图片;外发时这些本地链接降为显示文字,