-
Notifications
You must be signed in to change notification settings - Fork 737
fix(providers): detect gateway missing Messages API and surface actionable error (#158) #161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,8 @@ | |
| * layer logs them with `reason`. | ||
| */ | ||
|
|
||
| import type { ProviderId } from '@open-codesign/shared'; | ||
| import { looksLikeGatewayMissingMessagesApi } from '@open-codesign/providers'; | ||
| import type { ProviderId, WireApi } from '@open-codesign/shared'; | ||
| import { CodesignError, ERROR_CODES } from '@open-codesign/shared'; | ||
|
|
||
| export const PROVIDER_KEY_HELP_URL: Partial<Record<ProviderId, string>> = { | ||
|
|
@@ -98,9 +99,29 @@ export function rewriteUpstreamMessage( | |
| * 4xx errors are rewritten — everything else is rethrown unchanged so the | ||
| * retry/network layer keeps its own taxonomy. | ||
| */ | ||
| export function remapProviderError(err: unknown, provider: string | undefined): unknown { | ||
| export function remapProviderError( | ||
| err: unknown, | ||
| provider: string | undefined, | ||
| wire?: WireApi | undefined, | ||
| ): unknown { | ||
| if (!(err instanceof Error)) return err; | ||
| if (err instanceof CodesignError && err.code === ERROR_CODES.PROVIDER_ABORTED) return err; | ||
| // Third-party Anthropic relays often reply to POST /v1/messages with 5xx + | ||
| // "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. Guard on | ||
| // wire === 'anthropic' because the actionable hint ("switch wire to | ||
| // openai-chat") only makes sense for Anthropic-compatible endpoints — a 501 | ||
| // from an OpenAI/Google wire is just a generic upstream error. | ||
| if ( | ||
| wire === 'anthropic' && | ||
| !(err instanceof CodesignError && err.code === ERROR_CODES.PROVIDER_GATEWAY_INCOMPATIBLE) && | ||
| looksLikeGatewayMissingMessagesApi(err) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里仍缺少 建议最小修复: 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,
});
} |
||
| ) { | ||
| return new CodesignError(err.message, ERROR_CODES.PROVIDER_GATEWAY_INCOMPATIBLE, { | ||
| cause: err, | ||
| }); | ||
| } | ||
| const status = statusFromError(err); | ||
| if (status === undefined || status < 400 || status >= 500) return err; | ||
| const { message, rewritten } = rewriteUpstreamMessage(err.message, provider, status); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { looksLikeGatewayMissingMessagesApi } from './gateway-compat'; | ||
|
|
||
| describe('looksLikeGatewayMissingMessagesApi', () => { | ||
| it('matches plain "not implemented"', () => { | ||
| expect(looksLikeGatewayMissingMessagesApi(new Error('500 not implemented'))).toBe(true); | ||
| }); | ||
|
|
||
| it('matches "Not Implemented" with different case and spacing', () => { | ||
| expect(looksLikeGatewayMissingMessagesApi(new Error('Not Implemented'))).toBe(true); | ||
| }); | ||
|
|
||
| it('matches "Messages API not supported"', () => { | ||
| expect( | ||
| looksLikeGatewayMissingMessagesApi(new Error('Messages API not supported on this relay')), | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| it('matches "unsupported Messages API" phrasing', () => { | ||
| expect(looksLikeGatewayMissingMessagesApi(new Error('unsupported messages api endpoint'))).toBe( | ||
| true, | ||
| ); | ||
| }); | ||
|
|
||
| it('matches bare 501 status code in text', () => { | ||
| expect(looksLikeGatewayMissingMessagesApi(new Error('HTTP 501 from gateway'))).toBe(true); | ||
| }); | ||
|
|
||
| it('ignores ordinary 500 messages that do not mention not-implemented', () => { | ||
| expect(looksLikeGatewayMissingMessagesApi(new Error('500 internal server error'))).toBe(false); | ||
| }); | ||
|
|
||
| it('handles non-Error inputs safely', () => { | ||
| expect(looksLikeGatewayMissingMessagesApi(undefined)).toBe(false); | ||
| expect(looksLikeGatewayMissingMessagesApi(null)).toBe(false); | ||
| expect(looksLikeGatewayMissingMessagesApi('not implemented')).toBe(true); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /** | ||
| * Gateway compatibility detection. | ||
| * | ||
| * Third-party Anthropic-compatible relays (sub2api, claude2api, anyrouter…) | ||
| * frequently implement GET /v1/models (which is what our connection test | ||
| * hits) but stub out POST /v1/messages with "not implemented" / 501. That | ||
| * combination passes the onboarding check but explodes on the first real | ||
| * generation. Treating it as a retryable 5xx wastes the user's time with | ||
| * exponential backoff and surfaces a misleading "check your API key" blurb. | ||
| * | ||
| * This helper detects the tell-tale upstream text so both the retry layer | ||
| * (to short-circuit) and the core error remapper (to tag it with an | ||
| * actionable code) can react correctly. | ||
| */ | ||
|
|
||
| const NOT_IMPLEMENTED_PATTERNS: readonly RegExp[] = [ | ||
| /not\s+implemented/i, | ||
| /unsupported.*messages?\s*api/i, | ||
| /messages?\s*api.*not\s*supported/i, | ||
| /\b501\b/, | ||
| ]; | ||
|
|
||
| export function looksLikeGatewayMissingMessagesApi(err: unknown): boolean { | ||
| const msg = err instanceof Error ? err.message : String(err ?? ''); | ||
| if (!msg) return false; | ||
| return NOT_IMPLEMENTED_PATTERNS.some((re) => re.test(msg)); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 专用,存在误导风险。建议最小修复: