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): 渠道发图接受本机任意路径的图片

Dash0947c3432280
diff --git a/apps/desktop/src/main/cindy-media/__tests__/taskImageDelivery.test.ts b/apps/desktop/src/main/cindy-media/__tests__/taskImageDelivery.test.ts
index d8bd783bfd..754aa19759 100644
--- a/apps/desktop/src/main/cindy-media/__tests__/taskImageDelivery.test.ts
+++ b/apps/desktop/src/main/cindy-media/__tests__/taskImageDelivery.test.ts
@@ -503,6 +503,19 @@ describe('task image delivery', () => {
expect(raw.prepare('SELECT count(*) AS n FROM media_refs').get()).toEqual({ n: 1 });
});
+ it('channel delivery accepts images outside the task directory; chat restore stays task-scoped', async () => {
+ // 2026-10-26 Slack: previews saved outside the workdir showed on desktop but never reached the channel.
+ const outside = path.join(state.root, 'elsewhere');
+ await fs.mkdir(outside, { recursive: true });
+ const source = path.join(outside, 'preview.png');
+ await fs.writeFile(source, PNG);
+ const text = `![preview](${source})`;
+ const row = insert(text);
+ expect(await restoreTaskImageRows(client, [row])).toEqual([row]);
+ expect(await materializeTaskImageText('s', text)).toContain('cindy-media://');
+ expect(raw.prepare('SELECT count(*) AS n FROM media_refs').get()).toEqual({ n: 1 });
+ });
+
it('serializes concurrent channel imports without duplicate task references', async () => {
const source = path.join(work, 'shot.png');
await fs.writeFile(source, PNG);
diff --git a/apps/desktop/src/main/cindy-media/taskImageDelivery.ts b/apps/desktop/src/main/cindy-media/taskImageDelivery.ts
index 83e2aaae00..113352355b 100644
--- a/apps/desktop/src/main/cindy-media/taskImageDelivery.ts
+++ b/apps/desktop/src/main/cindy-media/taskImageDelivery.ts
@@ -84,17 +84,22 @@ export async function taskImageRoots(session: SessionImageScope): Promise<string
return roots;
}
-/** Read through one handle, rejecting escapes and replacement during validation. */
+/** Read through one handle, rejecting escapes and replacement during validation.
+ * `roots: null` accepts any local path; channel delivery uses it because the
+ * desktop chat already shows any local image the Agent references.
+ */
export async function readTaskImage(
source: string,
- roots: readonly string[],
+ roots: readonly string[] | null,
): Promise<{ buffer: Buffer; mimeType: string }> {
+ const withinRoots = (target: string): boolean =>
+ roots === null || roots.some((root) => isPathWithin(root, target));
// Reject Windows UNC / WebDAV / device namespaces before realpath can contact
// a remote server. Apply to decoded paths too, independent of the host OS.
if (/^[\\/]{2}|^[\\/]\?\?[\\/]/.test(source))
throw new Error('task-image: network or device path');
const real = await fs.realpath(source);
- if (!roots.some((root) => isPathWithin(root, real))) throw new Error('task-image: outside task');
+ if (!withinRoots(real)) throw new Error('task-image: outside task');
const handle = await fs.open(
real,
constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0) | (constants.O_NONBLOCK ?? 0),
@@ -109,7 +114,7 @@ export async function readTaskImage(
checked !== real ||
named.ino !== before.ino ||
named.dev !== before.dev ||
- !roots.some((root) => isPathWithin(root, checked))
+ !withinRoots(checked)
)
throw new Error('task-image: changed source');
const buffer = Buffer.alloc(before.size);
@@ -139,7 +144,11 @@ function markdownContent(value: string): { text: string; serialize(text: string)
}
}
-async function captureTaskImages(sessionId: string, client?: DbClient) {
+async function captureTaskImages(
+ sessionId: string,
+ client?: DbClient,
+ options: { anyLocalPath?: boolean } = {},
+) {
const snapshot = getCurrentDbClientSnapshot();
if (!snapshot || (client && client !== snapshot.client)) return null;
const db = snapshot.client.drizzle;
@@ -163,7 +172,7 @@ async function captureTaskImages(sessionId: string, client?: DbClient) {
.limit(1);
assertStillValid();
if (!session || session.remoteHostId || session.status === 'deleted') return null;
- const roots = await taskImageRoots(session);
+ const roots = options.anyLocalPath ? null : await taskImageRoots(session);
assertStillValid();
const newRefs: string[] = [];
return {
@@ -222,7 +231,7 @@ async function captureTaskImages(sessionId: string, client?: DbClient) {
};
}
-/** Channel adapter: same source validation/import as durable chat. Never rewrites Agent history. */
+/** Channel adapter: same file validation/import as durable chat, but any local path. Never rewrites Agent history. */
export async function materializeTaskImageText(sessionId: string, text: string): Promise<string> {
return (await materializeTaskImageTextResult(sessionId, text)).text;
}
@@ -252,7 +261,7 @@ async function materializeTaskImageTextUnlocked(
) {
let scope: Awaited<ReturnType<typeof captureTaskImages>> = null;
try {
- scope = await captureTaskImages(sessionId, client);
+ scope = await captureTaskImages(sessionId, client, { anyLocalPath: true });
if (!scope) {
log?.warn('task image import skipped: task is unavailable on this host');
return unchanged;
diff --git a/apps/desktop/src/main/hook-control/outbound.ts b/apps/desktop/src/main/hook-control/outbound.ts
index 378c0e3e5a..541f1e9aa9 100644
--- a/apps/desktop/src/main/hook-control/outbound.ts
+++ b/apps/desktop/src/main/hook-control/outbound.ts
@@ -64,11 +64,9 @@ export function buildHookPromptNote(im: string | undefined): string {
'回复时不要把它当作用户的请求,也不要引用、复述或据此臆测用户意图。' +
`本会话来自 ${platform}。要把文件发给用户:在最终回复文本里写 ` +
'`[文件名](xdt-file:///绝对路径)`;图片写 ' +
- '`![说明](工具返回的媒体地址或当前任务图片的绝对路径)`。' +
+ '`![说明](工具返回的媒体地址或本机图片的绝对路径)`。' +
'不要给本地路径拼接 xdt-image:// 或 cindy-media://。' +
- '当前工作目录内的 PNG/JPEG/GIF/WebP 图片会自动保存,' +
- `并在回复结束后作为 ${platform} 附件发回;` +
- '其它位置的图片(如 /tmp、下载目录)先用 media 工具的 import_image 导入,再引用返回的地址;' +
+ `PNG/JPEG/GIF/WebP 图片会在回复结束后自动作为 ${platform} 附件发回,无需另调导入工具;` +
'SVG 等其它格式先转成 PNG。' +
'xdt-file 文件必须位于当前工作目录内;无法读取、超限或目录外的附件不会发送,' +
'最终回复会明确显示附件发送不完整。' +
diff --git a/docs/dev-rules/media-storage-and-protocols.md b/docs/dev-rules/media-storage-and-protocols.md
index 854fa59ba9..dfe5303081 100644
--- a/docs/dev-rules/media-storage-and-protocols.md
+++ b/docs/dev-rules/media-storage-and-protocols.md
@@ -311,8 +311,9 @@
官方渠道只对整轮内容导入一次,最终正文复用该次导入的地址替换表;正文与附件扫描
指向同一副本,不因临时源文件在收口时删除或替换而再次读取出不同结果。
官方渠道的每张导入失败都要把原因(目录外、格式不支持、超限等)写入 main 日志;正文末尾的
- 未送达提示按界面语言点名未发出的附件。渠道说明须告知 Agent 自动保存的范围,工作目录外的
- 图片先用 `import_image` 导入,SVG 等格式先转成 PNG。
+ 未送达提示按界面语言点名未发出的附件。渠道出站与桌面聊天展示一致,接受本机任意路径的普通
+ 图片,不限于工作目录与 scratchpad(2026-10-27 裁决:渠道发图与导入后再发本质相同,
+ 不为此多走一步);网络/设备路径、非普通文件、超限与非图片仍拒绝,SVG 需 Agent 先转 PNG。
SSH 官方渠道不导入或读取控制端文件,仍复用出站引用清理:本地图片地址(含远端
Windows 路径)只保留说明,并提示附件发送不完整,不将不可用的媒体地址发到 IM。
与图片共用目标的普通链接也指向保存后的同一图片;外发时这些本地链接降为显示文字,