Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(anthropic-compat-proxy): glm-5.2 的 tool_result 图像降级为说明性占位文本 (#840)
fico-hub1980469694c7
diff --git a/packages/anthropic-compat-proxy/src/transform.test.ts b/packages/anthropic-compat-proxy/src/transform.test.tsindex 0f812d5524..27b6698ac8 100644--- a/packages/anthropic-compat-proxy/src/transform.test.ts+++ b/packages/anthropic-compat-proxy/src/transform.test.ts@@ -14,6 +14,7 @@ import {stripEmptyThinkingFromBody,stripEncryptedContentFromBody,stripImageGenerationItemsWithoutIdFromBody,+ stripNonAnthropicFields,stripToolUseProviderSpecificFields,stripToolUseProviderSpecificFieldsFromBody,} from './transform.js';@@ -858,3 +859,70 @@ describe('recovery rule factories', () => {).not.toBeNull();});});++describe('stripNonAnthropicFields · glm-5.2 tool_result 图像降级 (#794)', () => {+ const imageBlock = {+ type: 'image',+ source: { type: 'base64', media_type: 'image/png', data: 'AAAABBBB' },+ };+ const makeBody = (model: string): Record<string, unknown> => ({+ model,+ messages: [+ { role: 'user', content: 'read the scan' },+ {+ role: 'user',+ content: [+ {+ type: 'tool_result',+ tool_use_id: 't1',+ content: [{ type: 'text', text: 'scan follows' }, imageBlock],+ },+ ],+ },+ ],+ });++ it.each(['glm-5.2', 'z-ai/glm-5.2', 'glm-5.2[1m]', 'z-ai/glm-5.2[1m]'])(+ '%s:tool_result 图像替换为说明性占位文本,图像字节不外泄',+ (model) => {+ const out = stripNonAnthropicFields(makeBody(model), ctx) as Record<string, unknown> | null;+ expect(out).not.toBeNull();+ const json = JSON.stringify(out);+ expect(json).not.toContain('AAAABBBB');+ expect(json).toContain('[image omitted:');+ expect(json).toContain('Do NOT guess');+ // 同一 tool_result 的文本块保留,块结构仍是 tool_result。+ expect(json).toContain('scan follows');+ expect(json).toContain('tool_result');+ },+ );++ it('glm-5.2 无图像时返回 null(cache 安全契约,字节透传)', () => {+ const body = {+ model: 'glm-5.2',+ messages: [+ {+ role: 'user',+ content: [+ { type: 'tool_result', tool_use_id: 't1', content: [{ type: 'text', text: 'ok' }] },+ ],+ },+ ],+ };+ expect(stripNonAnthropicFields(body, ctx)).toBeNull();+ });++ it('未登记的 model 不受影响(字节透传)', () => {+ expect(+ stripNonAnthropicFields(makeBody('claude-opus-5'), ctx),+ ).toBeNull();+ });++ it('user 消息里的图像不动,只处理 tool_result 内嵌图像', () => {+ const body = {+ model: 'glm-5.2',+ messages: [{ role: 'user', content: [imageBlock] }],+ };+ expect(stripNonAnthropicFields(body, ctx)).toBeNull();+ });+});diff --git a/packages/anthropic-compat-proxy/src/transform.ts b/packages/anthropic-compat-proxy/src/transform.tsindex ad1b35787f..4642bd2fff 100644--- a/packages/anthropic-compat-proxy/src/transform.ts+++ b/packages/anthropic-compat-proxy/src/transform.ts@@ -556,6 +556,58 @@ const stripGpt54Mini: ModelStripHandler = (body) => {return next;};+/**+ * 纯文本模型的 tool_result 图像降级(#794)。+ *+ * 实测(z-ai/glm-5.2,Anthropic 兼容直通):tool_result 里的 image block 原样发给+ * 上游,上游静默丢弃且不报错——模型把工具结果当成空,反复重读或直接臆造图片内容+ * (实测单任务空转 66 分钟并编造译文)。把 image block 替换为说明性占位文本:+ * 明确告知有图但当前模型收不到、禁止臆测、引导贴文本或换视觉模型。+ *+ * 只处理 tool_result 内嵌图像;user 消息图像走各桥接的 input_image 路径,不归这里。+ */+const TOOL_RESULT_IMAGE_OMITTED_TEXT =+ '[image omitted: this tool returned an image, but the current model is text-only, '+ + 'so the image could not be delivered. Do NOT guess or fabricate what the image '+ + 'contains. Tell the user the image could not be delivered to the current model, '+ + 'and ask them to paste the relevant content as text or switch to a vision-capable '+ + 'model.]';++function replaceToolResultImagesWithNotice(+ body: Record<string, unknown>,+): Record<string, unknown> | null {+ const messages = body.messages;+ if (!Array.isArray(messages)) return null;++ let replaced = 0;+ const nextMessages = messages.map((msg) => {+ if (!isPlainObject(msg) || !Array.isArray(msg.content)) return msg;+ let msgChanged = false;+ const nextContent = msg.content.map((block) => {+ if (!isPlainObject(block) || block.type !== 'tool_result' || !Array.isArray(block.content)) {+ return block;+ }+ let blockChanged = false;+ const nextInner = block.content.map((inner) => {+ if (!isPlainObject(inner) || inner.type !== 'image') return inner;+ replaced += 1;+ blockChanged = true;+ return { type: 'text', text: TOOL_RESULT_IMAGE_OMITTED_TEXT };+ });+ if (!blockChanged) return block;+ msgChanged = true;+ return { ...block, content: nextInner };+ });+ return msgChanged ? { ...msg, content: nextContent } : msg;+ });++ if (replaced === 0) return null; // cache 安全契约:无改动 → null → 字节透传+ return { ...body, messages: nextMessages };+}++/** glm-5.2 (智谱,官方仅文本/代码模态) —— 直通与网关两条路由同一 model id。 */+const stripGlm52: ModelStripHandler = (body) => replaceToolResultImagesWithNotice(body);+// ───────────────────────────────────────────────────────────────────────────// 分发表// ───────────────────────────────────────────────────────────────────────────@@ -570,6 +622,13 @@ const STRIP_HANDLERS: Readonly<Record<string, ModelStripHandler>> = {// 「折扣GPT」低价路由 —— 与 gpt-5.4 打同一个 Azure 后端, 同样会因 output_config 报 400,// 镜像 gpt-5.4 的 strip 行为 (复用同一 handler)。codex/gpt-5.5 暂不加, 与 gpt-5.5 一致。'codex/gpt-5.4': stripGpt54,+ // 纯文本模型 tool_result 图像会被上游静默吞掉 (#794) —— 带/不带命名空间前缀,+ // 以及 claude-code SDK 按目录 1M 窗口追加 [1m] 后缀 (toSdkModelString) 的形态+ // 都登记 (直通路由 body.model 可能保留 z-ai/ 前缀与 [1m] 后缀)。+ 'glm-5.2': stripGlm52,+ 'z-ai/glm-5.2': stripGlm52,+ 'glm-5.2[1m]': stripGlm52,+ 'z-ai/glm-5.2[1m]': stripGlm52,};/**