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(wechat): 发图工具与聊天同规则,图片可来自本机任意路径

Dashf0f2a6917f7a
diff --git a/docs/dev-rules/media-storage-and-protocols.md b/docs/dev-rules/media-storage-and-protocols.md
index ada99a51ff..5860701f0a 100644
--- a/docs/dev-rules/media-storage-and-protocols.md
+++ b/docs/dev-rules/media-storage-and-protocols.md
@@ -296,7 +296,9 @@
官方渠道的每张导入失败都要把原因(目录外、格式不支持、超限等)写入 main 日志;正文末尾的
未送达提示按界面语言点名未发出的附件。渠道出站与桌面聊天展示一致,接受本机任意路径的普通
图片,不限于工作目录与 scratchpad(2026-10-27 裁决:渠道发图与导入后再发本质相同,
- 不为此多走一步);网络/设备路径、非普通文件、超限与非图片仍拒绝,SVG 需 Agent 先转 PNG。
+ 不为此多走一步);原则是 Cindy 聊天里能显示的图片就能发到 IM,群聊中由非主人触发的轮次同样适用,
+ 个人微信 `send_file_to_user` 的图片同此规则(按内容识别),普通文件仍限工作目录。
+ 网络/设备路径、非普通文件、超限与非图片仍拒绝,SVG 需 Agent 先转 PNG。
当前会话所在渠道一律用回复里的图片引用发图:文本渠道收集为附件,飞书、Telegram、Discord
卡片先改写为受管引用再原位上传;渠道发送工具只用于跨会话推送,不引导 Agent 在当前会话用它发图。
SSH 官方渠道不导入或读取控制端文件,仍复用出站引用清理:本地图片地址(含远端
diff --git a/packages/lizi-mcps/src/__tests__/wechatMcpServer.test.ts b/packages/lizi-mcps/src/__tests__/wechatMcpServer.test.ts
index 8c232260d6..c033230390 100644
--- a/packages/lizi-mcps/src/__tests__/wechatMcpServer.test.ts
+++ b/packages/lizi-mcps/src/__tests__/wechatMcpServer.test.ts
@@ -92,7 +92,7 @@ describe('cindy_wechat proactive routing', () => {
}
});
- it('rejects files outside the current working directory', async () => {
+ it('rejects non-image files outside the working directory but sends images from any path', async () => {
const workingDir = await fs.mkdtemp(path.join(os.tmpdir(), 'cindy-wechat-workdir-'));
const outsideDir = await fs.mkdtemp(path.join(os.tmpdir(), 'cindy-wechat-outside-'));
const outsidePath = path.join(outsideDir, 'secret.txt');
@@ -116,6 +116,19 @@ describe('cindy_wechat proactive routing', () => {
});
expect(JSON.stringify(call)).toContain('PATH_OUT_OF_BOUNDS');
expect(sendFile).not.toHaveBeenCalled();
+ // Images follow the chat rule: Cindy's own chat shows any local image, so WeChat may send it.
+ const imagePath = path.join(outsideDir, 'preview.png');
+ await fs.writeFile(imagePath, Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]));
+ sendFile.mockResolvedValue({ ok: true, messageId: 'm1' });
+ await client.callTool({ name: 'call_tool', arguments: { name: 'send_file_to_user', args: { absPath: imagePath } } });
+ expect(sendFile).toHaveBeenCalledWith('peer-session', await fs.realpath(imagePath), undefined);
+ // A renamed document is still a document.
+ const disguised = path.join(outsideDir, 'secret.png');
+ await fs.writeFile(disguised, 'not-for-wechat');
+ sendFile.mockClear();
+ const blocked = await client.callTool({ name: 'call_tool', arguments: { name: 'send_file_to_user', args: { absPath: disguised } } });
+ expect(JSON.stringify(blocked)).toContain('PATH_OUT_OF_BOUNDS');
+ expect(sendFile).not.toHaveBeenCalled();
} finally {
await client.close();
await server.close();
diff --git a/packages/lizi-mcps/src/cindy_wechatMcpServer.ts b/packages/lizi-mcps/src/cindy_wechatMcpServer.ts
index 96173e7ebf..dce9348129 100644
--- a/packages/lizi-mcps/src/cindy_wechatMcpServer.ts
+++ b/packages/lizi-mcps/src/cindy_wechatMcpServer.ts
@@ -19,8 +19,9 @@ type WechatMcpDeps = WechatBotMcpHostDeps & {
*
* The host resolves the receiver from the current WeChat session or the most
* recent peer seen by the active binding. The model never supplies an
- * arbitrary peer id. File sends are additionally confined to the session
- * working directory after resolving symlinks.
+ * arbitrary peer id. Images follow the chat rule: any local image Cindy can show
+ * in its own chat may be sent. Other files stay confined to the session working
+ * directory after resolving symlinks.
*/
export function createWechatMcpServer(deps: WechatMcpDeps): McpServer {
const server = new McpServer({ name: 'cindy_wechat', version: '1.0.0' });
@@ -43,7 +44,7 @@ export function createWechatMcpServer(deps: WechatMcpDeps): McpServer {
{
name: 'send_file_to_user',
description:
- '向当前个人微信联系人发送当前工作目录内不超过 5 MB 的图片或文件。参数:{ absPath: 绝对路径, displayName?: 展示文件名 }。',
+ '向当前个人微信联系人发送不超过 5 MB 的图片(本机任意路径)或文件(须在当前工作目录内)。参数:{ absPath: 绝对路径, displayName?: 展示文件名 }。',
},
],
}),
@@ -152,9 +153,12 @@ async function resolveFileWithinWorkingDir(
candidate: string,
workingDir: string | undefined,
): Promise<string> {
- if (!workingDir) throw new WechatFilePathError('WORKING_DIR_UNAVAILABLE');
if (!path.isAbsolute(candidate)) throw new WechatFilePathError('PATH_MUST_BE_ABSOLUTE');
- const [root, file] = await Promise.all([fs.realpath(workingDir), fs.realpath(candidate)]);
+ // Same rule as the chat and channel replies: a regular image file may come from any local path.
+ const real = await fs.realpath(candidate);
+ if (await isRegularImageFile(real)) return real;
+ if (!workingDir) throw new WechatFilePathError('WORKING_DIR_UNAVAILABLE');
+ const [root, file] = [await fs.realpath(workingDir), real];
const relative = path.relative(root, file);
if (relative === '..' || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative)) {
throw new WechatFilePathError('PATH_OUT_OF_BOUNDS');
@@ -162,6 +166,26 @@ async function resolveFileWithinWorkingDir(
return file;
}
+/** PNG / JPEG / GIF / WebP by content, not by name, so a renamed document is not treated as an image. */
+async function isRegularImageFile(file: string): Promise<boolean> {
+ const handle = await fs.open(file, 'r');
+ try {
+ if (!(await handle.stat()).isFile()) return false;
+ const head = Buffer.alloc(12);
+ const { bytesRead } = await handle.read(head, 0, head.length, 0);
+ const bytes = head.subarray(0, bytesRead);
+ const starts = (sig: number[], at = 0) => sig.every((b, i) => bytes[at + i] === b);
+ return (
+ starts([0x89, 0x50, 0x4e, 0x47]) ||
+ starts([0xff, 0xd8, 0xff]) ||
+ starts([0x47, 0x49, 0x46, 0x38]) ||
+ (starts([0x52, 0x49, 0x46, 0x46]) && starts([0x57, 0x45, 0x42, 0x50], 8))
+ );
+ } finally {
+ await handle.close();
+ }
+}
+
class WechatFilePathError extends Error {
constructor(readonly code: string) {
super(code);