Skip to content

fix(providers): detect gateway missing Messages API and surface actionable error (#158)#161

Merged
hqhq1025 merged 2 commits intomainfrom
fix/gateway-messages-api-158
Apr 23, 2026
Merged

fix(providers): detect gateway missing Messages API and surface actionable error (#158)#161
hqhq1025 merged 2 commits intomainfrom
fix/gateway-messages-api-158

Conversation

@hqhq1025
Copy link
Copy Markdown
Collaborator

Summary

第三方中转(sub2api / claude2api / anyrouter / cpa 反代等)经常实现了 `/v1/models` 但没实现完整的 Anthropic Messages API。结果:测试连接通过,真实生成 500 not implemented。当前文案是通用 "The provider returned an error. Check your API key" —— 完全误导用户。

What changed

  • 新增 `packages/providers/src/gateway-compat.ts`:`looksLikeGatewayMissingMessagesApi` 用 4 个 pattern 识别 not-implemented 文案
  • `packages/providers/src/retry.ts`:5xx 命中 not-implemented pattern 时 `retry: false`,避免用户白等 3 次指数退避
  • `packages/core/src/errors.ts`:`remapProviderError` 命中时归类成新的 `PROVIDER_GATEWAY_INCOMPATIBLE`
  • `packages/shared/src/error-codes.ts` + i18n (en/zh-CN):新错误码 + 用户文案 "Your gateway returned 'not implemented' for the Messages API. Try switching wire to openai-chat in Settings, or use a gateway that supports the Anthropic Messages API."

Test plan

  • `packages/providers/src/gateway-compat.test.ts` (7 cases) covers 4 not-implemented patterns + non-matching 5xx
  • `packages/providers/src/retry.test.ts` regression: 500 + "not implemented" → `retry: false`
  • `packages/core/src/errors.test.ts` (2 new cases) covers PROVIDER_GATEWAY_INCOMPATIBLE classification
  • `pnpm test` providers (139) + core (218) all green
  • `pnpm typecheck` 10/10
  • `pnpm lint` clean

Closes #158

@github-actions github-actions Bot added area:core packages/core (generation orchestration) area:providers packages/providers (pi-ai adapter, model calls) labels Apr 22, 2026
Copy link
Copy Markdown
Contributor

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Findings

  • [Major] PROVIDER_GATEWAY_INCOMPATIBLE 误判到非 Anthropic provider — 当前分支在命中 looksLikeGatewayMissingMessagesApi(err) 时没有限制 provider,可能把 OpenAI/Google 等返回的普通 501/not implemented 也映射成 Anthropic Messages API 不兼容,导致 UI 给出错误引导。证据:packages/core/src/errors.ts:109
    Suggested fix:
    if (
      provider === 'anthropic' &&
      !(err instanceof CodesignError && err.code === ERROR_CODES.PROVIDER_GATEWAY_INCOMPATIBLE) &&
      looksLikeGatewayMissingMessagesApi(err)
    ) {
      return new CodesignError(err.message, ERROR_CODES.PROVIDER_GATEWAY_INCOMPATIBLE, {
        cause: err,
      });
    }

Summary
Review mode: initial
发现 1 个 Major 问题(provider 作用域缺失导致错误分类)。其余本次改动中未发现新增依赖、直连 provider SDK、静默 fallback 或 UI token 违规。docs/VISION.mddocs/PRINCIPLES.md 在当前 checkout 中不存在(Not found in repo/docs),因此未能基于这两份文档做交叉校验。

Testing
Not run (automation)

open-codesign Bot

// "not implemented" while their /v1/models endpoint works. Catch that shape
// before any other classification so the UI can suggest switching wire
// instead of the misleading default "check your API key" message.
if (
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里缺少 provider 作用域判断。当前逻辑会把任何 provider 的 501/not implemented 都标记为 PROVIDER_GATEWAY_INCOMPATIBLE,但该错误文案是 Anthropic Messages API 专用,存在误导风险。

建议最小修复:

if (provider === 'anthropic' && looksLikeGatewayMissingMessagesApi(err)) {
  // ...
}

@hqhq1025 hqhq1025 force-pushed the fix/gateway-messages-api-158 branch from 6dff02d to 25e81de Compare April 22, 2026 14:19
Copy link
Copy Markdown
Contributor

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Findings

  • [Major] PROVIDER_GATEWAY_INCOMPATIBLE 仍会误判到非 Anthropic provider — 当前分类逻辑只要命中 looksLikeGatewayMissingMessagesApi(err) 就直接改写错误码,没有限制 provider === 'anthropic',会把其他 provider 的普通 501/not implemented 也映射成 Anthropic Messages API 不兼容,导致错误引导。证据:packages/core/src/errors.ts:109
    Suggested fix:
    if (
      provider === 'anthropic' &&
      !(err instanceof CodesignError && err.code === ERROR_CODES.PROVIDER_GATEWAY_INCOMPATIBLE) &&
      looksLikeGatewayMissingMessagesApi(err)
    ) {
      return new CodesignError(err.message, ERROR_CODES.PROVIDER_GATEWAY_INCOMPATIBLE, {
        cause: err,
      });
    }

Summary
Review mode: follow-up after new commits
仍有 1 个 Major 问题(provider 作用域缺失导致错误分类);其余本次变更中未发现新增依赖、直连 provider SDK、静默 fallback 或 UI token 硬编码。docs/VISION.mddocs/PRINCIPLES.md 在当前 checkout 不存在(Not found in repo/docs),未能据此交叉校验。

Testing
Not run (automation)

open-codesign Bot

// instead of the misleading default "check your API key" message.
if (
!(err instanceof CodesignError && err.code === ERROR_CODES.PROVIDER_GATEWAY_INCOMPATIBLE) &&
looksLikeGatewayMissingMessagesApi(err)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里仍缺少 provider 作用域。当前会把任何 provider 的 501/not implemented 都映射为 PROVIDER_GATEWAY_INCOMPATIBLE,但该文案是 Anthropic Messages API 专用,存在误导风险。

建议最小修复:

if (
  provider === 'anthropic' &&
  !(err instanceof CodesignError && err.code === ERROR_CODES.PROVIDER_GATEWAY_INCOMPATIBLE) &&
  looksLikeGatewayMissingMessagesApi(err)
) {
  return new CodesignError(err.message, ERROR_CODES.PROVIDER_GATEWAY_INCOMPATIBLE, {
    cause: err,
  });
}

Copy link
Copy Markdown
Contributor

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Findings

  • No new blocker/major/minor findings on the latest diff.

Summary

  • Review mode: follow-up after new commits
  • 本次新增提交已修复上一轮指出的误分类问题(按 wire === 'anthropic' 限定 gateway incompatibility 检测),在本次变更触及范围内未发现新的正确性/安全性/约束违规问题。
  • docs/VISION.mddocs/PRINCIPLES.md:Not found in repo/docs(当前 checkout 不存在),因此无法基于这两份文档做交叉一致性复核。
  • 残余风险:looksLikeGatewayMissingMessagesApi 仍依赖上游错误文案模式匹配,后续若网关文案变化可能需要补充 pattern 与回归用例。

Testing

  • Not run (automation)

open-codesign Bot

…nable error (#158)

Third-party Anthropic relays (sub2api, claude2api, anyrouter…) often
implement GET /v1/models but stub POST /v1/messages with "not
implemented" / 501. The connection test passes but real generation
fails. Previously this was treated as a retryable 5xx — users waited
three rounds of exponential backoff only to see a generic "check your
API key" message that points them in the wrong direction.

- Add looksLikeGatewayMissingMessagesApi in @open-codesign/providers
- Short-circuit retry.ts on 5xx + not-implemented bodies
- Tag the error with new PROVIDER_GATEWAY_INCOMPATIBLE code in core's
  remapProviderError so the UI surfaces an actionable message (switch
  wire to openai-chat, or use a gateway that supports the Messages API)
- Locale entries for en and zh-CN

Signed-off-by: hqhq1025 <1506751656@qq.com>
…re (#158)

The "not implemented" → PROVIDER_GATEWAY_INCOMPATIBLE remap (and the
matching retry short-circuit) only makes sense for Anthropic-compatible
endpoints — the actionable hint tells the user to switch wire to
openai-chat, which is misleading when the user is already on an
OpenAI/Google wire and just hit a transient 501.

Gate both call sites on wire === 'anthropic': non-anthropic wires fall
through to the generic PROVIDER_ERROR path and keep retrying 5xx
normally. Adds counter-example tests on openai-chat / openai-responses.

Signed-off-by: hqhq1025 <1506751656@qq.com>
@hqhq1025 hqhq1025 force-pushed the fix/gateway-messages-api-158 branch from 3e26a3a to 87e1b9c Compare April 23, 2026 03:08
Copy link
Copy Markdown
Contributor

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Findings

  • 未发现达到可报告阈值的 Blocker/Major/Minor/Nit 问题(基于本次新增/修改行)。

Summary

  • Review mode: follow-up after new commits
  • 已按最新 PR diff 全量复核本轮改动(12 files),未发现由本次变更引入的正确性、安全性(OWASP Top 10 相关)、约束违规(直接 SDK 导入、静默降级、UI token 违规、新增依赖许可/体积)问题。
  • docs/VISION.mddocs/PRINCIPLES.md: Not found in repo/docs(当前 checkout 不存在),无法据此做交叉一致性复核。
  • 残余风险:looksLikeGatewayMissingMessagesApi 仍为文案模式匹配,若第三方网关返回文本变体变化,需补充 pattern 与回归用例。

Testing

  • Not run (automation)

open-codesign Bot

@hqhq1025 hqhq1025 merged commit 5664e59 into main Apr 23, 2026
7 checks passed
@hqhq1025 hqhq1025 deleted the fix/gateway-messages-api-158 branch April 23, 2026 03:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core packages/core (generation orchestration) area:providers packages/providers (pi-ai adapter, model calls)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: 第三方中转服务测试连接通过但生成返回 500 not implemented

1 participant