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(desktop): explain XD tool-result image loss (#2493)

tiammomo14a8640bbc16
diff --git a/apps/desktop/src/main/maker-host/__tests__/xdToolResultImageNotice.test.ts b/apps/desktop/src/main/maker-host/__tests__/xdToolResultImageNotice.test.ts
new file mode 100644
index 0000000000..90c7633b07
--- /dev/null
+++ b/apps/desktop/src/main/maker-host/__tests__/xdToolResultImageNotice.test.ts
@@ -0,0 +1,81 @@
+import { describe, expect, it } from 'vitest';
+
+import type { RequestTransformCtx } from '@cindy/anthropic-compat-proxy';
+
+import { createXdToolResultImageNoticeTransform } from '../xd-tool-result-image-notice.js';
+
+const imageBlock = {
+ type: 'image',
+ source: { type: 'base64', media_type: 'image/png', data: 'SECRET_IMAGE_BYTES' },
+};
+
+function context(upstreamBase: string): RequestTransformCtx {
+ return {
+ reqId: 1,
+ method: 'POST',
+ url: '/v1/messages',
+ headers: {},
+ upstreamBase,
+ };
+}
+
+function request(model = 'codex/gpt-5.6-sol'): Record<string, unknown> {
+ return {
+ model,
+ messages: [
+ {
+ role: 'user',
+ content: [
+ {
+ type: 'tool_result',
+ tool_use_id: 'read-1',
+ content: [{ type: 'text', text: 'image metadata' }, imageBlock],
+ },
+ ],
+ },
+ ],
+ };
+}
+
+describe('XD tool-result image notice', () => {
+ const transform = createXdToolResultImageNoticeTransform(
+ () => 'https://gateway.example.com/anthropic/',
+ );
+
+ it.each(['codex/gpt-5.6-sol', 'gpt-5.6-sol', 'gpt-5.6-sol[1m]'])(
+ 'replaces XD-routed %s tool-result images without exposing image bytes',
+ (model) => {
+ const output = transform(
+ request(model),
+ context('https://gateway.example.com:443/anthropic'),
+ );
+ const json = JSON.stringify(output);
+
+ expect(output).not.toBeNull();
+ expect(json).toContain('image metadata');
+ expect(json).toContain('current route cannot deliver images inside tool results');
+ expect(json).toContain('attach it directly to the conversation');
+ expect(json).not.toContain('SECRET_IMAGE_BYTES');
+ expect(json).not.toContain('text-only');
+ },
+ );
+
+ it('does not change the same model on a custom provider route', () => {
+ expect(transform(request(), context('https://custom.example.com/v1'))).toBeNull();
+ });
+
+ it('does not change other models or ordinary user images', () => {
+ expect(
+ transform(request('claude-opus-5'), context('https://gateway.example.com/anthropic')),
+ ).toBeNull();
+ expect(
+ transform(
+ {
+ model: 'codex/gpt-5.6-sol',
+ messages: [{ role: 'user', content: [imageBlock] }],
+ },
+ context('https://gateway.example.com/anthropic'),
+ ),
+ ).toBeNull();
+ });
+});
diff --git a/apps/desktop/src/main/maker-host/anthropic-compat-proxy-host.ts b/apps/desktop/src/main/maker-host/anthropic-compat-proxy-host.ts
index c8bfeaebcf..2f92ebc3ed 100644
--- a/apps/desktop/src/main/maker-host/anthropic-compat-proxy-host.ts
+++ b/apps/desktop/src/main/maker-host/anthropic-compat-proxy-host.ts
@@ -103,6 +103,7 @@ import {
authenticatePiProxySession,
getPiProxySessionProvider,
} from './pi-proxy-session-auth.js';
+import { createXdToolResultImageNoticeTransform } from './xd-tool-result-image-notice.js';
// scope = 'cc-proxy' → logger.ts 的 emit() 路由把这条流量并入统一 agent 流
// (agent-*.ndjson, source=proxy)。child(sub) 会继续保持 'cc-proxy/sub' 前缀, routing 一致。
@@ -596,6 +597,7 @@ export async function ensureAnthropicCompatProxyReady(): Promise<void> {
// image block、不碰 tool_use 结构,位于 repair/dedupe 之后安全;未命中时返回 null,
// 旧 #794 strip 继续兜底。
buildVisionBridgeProxyTransform(log),
+ createXdToolResultImageNoticeTransform(claudeUpstreamEndpoint),
// PI / Claude 走本 proxy 的 /responses 时(Gateway grok、自定义 LiteLLM)
// 不会经过 Codex 的 xAI 订阅 transform,必须在这里洗 ModelInput。
createActiveStripTransform({
diff --git a/apps/desktop/src/main/maker-host/xd-tool-result-image-notice.ts b/apps/desktop/src/main/maker-host/xd-tool-result-image-notice.ts
new file mode 100644
index 0000000000..026e1d85ae
--- /dev/null
+++ b/apps/desktop/src/main/maker-host/xd-tool-result-image-notice.ts
@@ -0,0 +1,44 @@
+import {
+ replaceToolResultImagesWithNotice,
+ type RequestTransform,
+} from '@cindy/anthropic-compat-proxy';
+
+const XD_TOOL_RESULT_IMAGE_MODELS = new Set(['codex/gpt-5.6-sol', 'gpt-5.6-sol']);
+const XD_TOOL_RESULT_IMAGE_NOTICE =
+ '[image omitted: this tool returned an image, but the current route cannot deliver ' +
+ 'images inside tool results. Do NOT guess or fabricate what the image contains. ' +
+ 'Tell the user the image could not be delivered through the current route, and ask ' +
+ 'them to attach it directly to the conversation.]';
+
+function isPlainObject(value: unknown): value is Record<string, unknown> {
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
+}
+
+function normalizeUpstreamBase(value: string): string | null {
+ try {
+ const url = new URL(value.trim());
+ if (url.protocol !== 'http:' && url.protocol !== 'https:') return null;
+ const port = url.port || (url.protocol === 'https:' ? '443' : '80');
+ return `${url.protocol}//${url.hostname}:${port}${url.pathname.replace(/\/+$/, '')}${url.search}`;
+ } catch {
+ return null;
+ }
+}
+
+/**
+ * Keep XD's known tool-result image limitation non-silent without changing other
+ * providers or the subscription bridge, which have different transport semantics.
+ */
+export function createXdToolResultImageNoticeTransform(
+ readXdUpstream: () => string,
+): RequestTransform {
+ return (body, ctx) => {
+ if (!isPlainObject(body) || typeof body.model !== 'string') return null;
+ const model = body.model.endsWith('[1m]') ? body.model.slice(0, -4) : body.model;
+ if (!XD_TOOL_RESULT_IMAGE_MODELS.has(model)) return null;
+ const actualUpstream = ctx.upstreamBase ? normalizeUpstreamBase(ctx.upstreamBase) : null;
+ const xdUpstream = normalizeUpstreamBase(readXdUpstream());
+ if (!actualUpstream || actualUpstream !== xdUpstream) return null;
+ return replaceToolResultImagesWithNotice(body, XD_TOOL_RESULT_IMAGE_NOTICE);
+ };
+}
diff --git a/packages/anthropic-compat-proxy/src/index.ts b/packages/anthropic-compat-proxy/src/index.ts
index a63451b588..0216397a20 100644
--- a/packages/anthropic-compat-proxy/src/index.ts
+++ b/packages/anthropic-compat-proxy/src/index.ts
@@ -48,6 +48,7 @@ export {
repairToolExchangeAdjacency,
repairToolExchangeAdjacencyFromBody,
repairToolExchangeStructureFromBody,
+ replaceToolResultImagesWithNotice,
stripEmptyAssistantMessagesFromBody,
stripEmptyTextFromBody,
stripEmptyThinkingFromBody,
diff --git a/packages/anthropic-compat-proxy/src/transform.ts b/packages/anthropic-compat-proxy/src/transform.ts
index a8c21e45ff..aeca43f418 100644
--- a/packages/anthropic-compat-proxy/src/transform.ts
+++ b/packages/anthropic-compat-proxy/src/transform.ts
@@ -1122,8 +1122,9 @@ const TOOL_RESULT_IMAGE_OMITTED_TEXT =
+ 'and ask them to paste the relevant content as text or switch to a vision-capable '
+ 'model.]';
-function replaceToolResultImagesWithNotice(
+export function replaceToolResultImagesWithNotice(
body: Record<string, unknown>,
+ noticeText = TOOL_RESULT_IMAGE_OMITTED_TEXT,
): Record<string, unknown> | null {
const messages = body.messages;
if (!Array.isArray(messages)) return null;
@@ -1141,7 +1142,7 @@ function replaceToolResultImagesWithNotice(
if (!isPlainObject(inner) || inner.type !== 'image') return inner;
replaced += 1;
blockChanged = true;
- return { type: 'text', text: TOOL_RESULT_IMAGE_OMITTED_TEXT };
+ return { type: 'text', text: noticeText };
});
if (!blockChanged) return block;
msgChanged = true;