From c87e5521a43032379a69249db9582db7d5dfae9c Mon Sep 17 00:00:00 2001 From: Yige Date: Tue, 3 Nov 2026 17:49:55 +0800 Subject: [PATCH] =?UTF-8?q?fix(vision):=20=E6=8C=89=E5=86=85=E5=AE=B9?= =?UTF-8?q?=E5=9D=97=E6=8F=90=E5=8F=96=20Anthropic=20=E8=A7=86=E8=A7=89?= =?UTF-8?q?=E6=96=87=E6=9C=AC=E5=B9=B6=E6=9B=B4=E6=96=B0=E9=A2=84=E8=AE=BE?= =?UTF-8?q?=E8=87=B3=20v137=20(#130)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Corona --- __tests__/api-provider-response.test.mjs | 39 +++++++++++++++++++ .../configured-vision-anthropic.json | 2 +- .../configured-vision-anthropic.md | 26 +++++++++++-- manifest.json | 4 +- schemas/api-provider.schema.json | 11 ++++-- 5 files changed, 72 insertions(+), 10 deletions(-) create mode 100644 __tests__/api-provider-response.test.mjs diff --git a/__tests__/api-provider-response.test.mjs b/__tests__/api-provider-response.test.mjs new file mode 100644 index 0000000..6a626ba --- /dev/null +++ b/__tests__/api-provider-response.test.mjs @@ -0,0 +1,39 @@ +import { test } from 'node:test' +import assert from 'node:assert/strict' +import { readFileSync } from 'node:fs' +import { loadSchemas } from '../scripts/validate.mjs' + +const read = path => JSON.parse(readFileSync(new URL(`../${path}`, import.meta.url), 'utf8')) +const validate = loadSchemas()['api-provider'] + +test('Anthropic 视觉配置显式选择整个内容块数组,兼容首块为推理的响应', () => { + const config = read('api-providers/image-understanding/configured-vision-anthropic.json') + assert.deepEqual(config.response, { textPath: 'content', textFormat: 'text-blocks' }) + assert.equal(validate(config), true, JSON.stringify(validate.errors)) +}) + +test('字符串响应格式可省略,旧 OpenAI 视觉配置仍有效', () => { + const config = read('api-providers/image-understanding/configured-vision-openai.json') + assert.equal(Object.hasOwn(config.response, 'textFormat'), false) + assert.equal(validate(config), true, JSON.stringify(validate.errors)) + assert.equal(Object.hasOwn(config.response, 'textFormat'), false) + config.response.textFormat = 'string' + assert.equal(validate(config), true, JSON.stringify(validate.errors)) +}) + +test('响应格式拒绝未知值和非字符串,避免默默接受未实现的提取策略', () => { + for (const textFormat of ['thinking-blocks', null, true, []]) { + const config = read('api-providers/image-understanding/configured-vision-anthropic.json') + config.response.textFormat = textFormat + assert.equal(validate(config), false) + assert.ok(validate.errors.some(error => error.instancePath === '/response/textFormat')) + } +}) + +test('响应配置继续拒绝未知字段', () => { + const config = read('api-providers/image-understanding/configured-vision-anthropic.json') + config.response.includeThinking = true + assert.equal(validate(config), false) + assert.ok(validate.errors.some(error => error.keyword === 'additionalProperties' + && error.params.additionalProperty === 'includeThinking')) +}) diff --git a/api-providers/image-understanding/configured-vision-anthropic.json b/api-providers/image-understanding/configured-vision-anthropic.json index 0d47d02..5641783 100644 --- a/api-providers/image-understanding/configured-vision-anthropic.json +++ b/api-providers/image-understanding/configured-vision-anthropic.json @@ -27,6 +27,6 @@ ] } }, - "response": { "textPath": "content.0.text" }, + "response": { "textPath": "content", "textFormat": "text-blocks" }, "timeoutMs": 30000 } diff --git a/docs/data-sources/api-providers/configured-vision-anthropic.md b/docs/data-sources/api-providers/configured-vision-anthropic.md index cfb66e3..4d81521 100644 --- a/docs/data-sources/api-providers/configured-vision-anthropic.md +++ b/docs/data-sources/api-providers/configured-vision-anthropic.md @@ -1,6 +1,8 @@ # 视觉模型(Anthropic 兼容,复用默认映射) -旧 docs.anthropic.com 跳转至此官方域名,正文证明 Messages 的 image/base64/media_type/data 结构;max_tokens=1024 是本仓输出预算策略,官网示例使用该值不证明它是默认或上限。认证、API 版本和 content.0.text 不因本篇已读而视作全部核验;任意网关仍需验收。 +旧 docs.anthropic.com 跳转至此官方域名,正文证明 Messages 的 image/base64/media_type/data 结构;max_tokens=1024 是本仓输出预算策略,官网示例使用该值不证明它是默认或上限。认证和 API 版本不因本篇已读而视作全部核验;任意网关仍需验收。 + +2026-11-03 重新读取官方 Vision 和 Messages API 正文。Messages 的响应 `content` 是异构内容块数组,文本块使用 `type=text` 与字符串 `text`,推理块另有独立类型;不能假定首块就是文本。因此将提取路径改为 `content`,显式使用客户端的 `text-blocks` 格式,仅顺序连接顶层文本块并忽略推理等其他块。`textFormat` 和换行连接是 DesireCore 的声明式消费契约,不是 Anthropic 请求参数。该配置依赖主仓新增的消费支持;旧 Schema 应拒绝候选并继续使用原有效快照,不允许把数组转换成字符串。本仓校验不代表新客户端已正式发布,也不代表任意兼容网关实测通过。 数据区中的 claims 是复核边界,pending 字段是当前配置快照,不能当官方证明。enabled/priority/timeout 是产品策略,未复制到合同中。更新合同必须重新读官方正文、记录实际证明字段;结构校验不等于账号可用。 @@ -9,7 +11,7 @@ { "kind": "api-contract", "config": "api-providers/image-understanding/configured-vision-anthropic.json", - "checkedAt": "2026-10-03", + "checkedAt": "2026-11-03", "status": "partial", "sources": [ { @@ -18,11 +20,26 @@ "checkedAt": "2026-10-03", "resolvedUrl": "https://platform.claude.com/docs/en/build-with-claude/vision", "contentSha256": "b35b04f339283acf2d250590830e7c552575c27d78c9b694523a6417a7a58044" + }, + { + "url": "https://platform.claude.com/docs/en/build-with-claude/vision.md", + "retrieval": "fetched", + "checkedAt": "2026-11-03", + "resolvedUrl": "https://platform.claude.com/docs/en/build-with-claude/vision.md", + "contentSha256": "f67ae8c652f2fc05bf681e7020268d186c5f34b7c713852934b533548c9108ed" + }, + { + "url": "https://platform.claude.com/docs/en/api/messages/create.md", + "retrieval": "fetched", + "checkedAt": "2026-11-03", + "resolvedUrl": "https://platform.claude.com/docs/en/api/messages/create.md", + "contentSha256": "7bdae71980a2aff09491755da1744932b5cf722f459c1d4a2a8c818539f82ce3" } ], "verifiedFields": [ "request.bodyTemplate.messages.0.content.0.type", - "request.bodyTemplate.messages.0.content.0.source.type" + "request.bodyTemplate.messages.0.content.0.source.type", + "response.textPath" ], "claims": { "endpoint": "${baseUrl}/messages", @@ -61,7 +78,8 @@ } }, "response": { - "textPath": "content.0.text" + "textPath": "content", + "textFormat": "text-blocks" } } } diff --git a/manifest.json b/manifest.json index 90d60fe..0d7e67b 100644 --- a/manifest.json +++ b/manifest.json @@ -1,6 +1,6 @@ { "version": "1.0.0", - "presetDataVersion": 136, - "updatedAt": "2026-11-02", + "presetDataVersion": 137, + "updatedAt": "2026-11-03", "description": "DesireCore 官方配置中心" } diff --git a/schemas/api-provider.schema.json b/schemas/api-provider.schema.json index 9006039..490292d 100644 --- a/schemas/api-provider.schema.json +++ b/schemas/api-provider.schema.json @@ -2,7 +2,7 @@ "$schema": "http://json-schema.org/draft-07/schema#", "$id": "https://desirecore.net/schemas/config-center/api-provider.schema.json", "title": "ApiProvider", - "description": "api-providers//*.json:声明式外部 API 供应商(web_search / image_understanding 工具后端)。镜像 desirecore 主仓 lib/schemas/agent-service/api-provider.ts。", + "description": "api-providers//*.json:声明式外部 API 供应商(web_search / image_understanding 工具后端)。镜像 desirecore 主仓 packages/schemas/src/agent-service/api-provider.ts。", "type": "object", "required": ["id", "name", "capability", "enabled", "endpoint", "auth", "request", "response"], "additionalProperties": false, @@ -47,7 +47,7 @@ }, "response": { "type": "object", - "description": "响应提取", + "description": "响应解析。web_search 用 resultsPath + item;image_understanding 用 textPath + textFormat", "additionalProperties": false, "properties": { "resultsPath": { "type": "string", "description": "web_search 结果数组的点路径" }, @@ -61,7 +61,12 @@ "pageAgePath": { "type": "string" } } }, - "textPath": { "type": "string", "description": "image_understanding 结果文本的点路径" }, + "textPath": { "type": "string", "description": "image_understanding:响应结果的点路径。string 格式指向字符串(如 choices.0.message.content);text-blocks 格式指向内容块数组(如 content)" }, + "textFormat": { + "type": "string", + "enum": ["string", "text-blocks"], + "description": "image_understanding:textPath 指向值的格式,省略时为 string,仅接受字符串;text-blocks 仅按原顺序读取顶层数组中 type=text 且 text 为字符串的内容,以换行连接,忽略推理、工具、图片及其他块,不递归提取" + }, "successCondition": { "type": "object", "description": "可选业务成功判定;HTTP 成功后读取 path 并与 equals 严格比较,不匹配时按 Provider 失败继续降级",