mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 23:14:31 +00:00
fix(providers): strip trailing /v1 from Anthropic baseUrl to prevent double-path
The pi-ai Anthropic provider constructs the full API endpoint as
`${baseUrl}/v1/messages`. If a user configures
`models.providers.anthropic.baseUrl` with a trailing `/v1`
(e.g. "https://api.anthropic.com/v1"), the resolved URL becomes
"https://api.anthropic.com/v1/v1/messages" which the Anthropic API
rejects with a 404 / connection failure.
This regression appeared in v2026.2.22 when @mariozechner/pi-ai bumped
from 0.54.0 to 0.54.1, which started appending the /v1 segment where
the previous version did not.
Fix: in normalizeModelCompat(), detect anthropic-messages models and
strip a single trailing /v1 (with optional trailing slash) from the
configured baseUrl before it is handed to pi-ai. Models with baseUrls
that do not end in /v1 are unaffected. Non-anthropic-messages models
are not touched.
Adds 6 unit tests covering the normalisation scenarios.
Fixes #24709
(cherry picked from commit 4c4857fdcb)
This commit is contained in:
committed by
Peter Steinberger
parent
01c1f68ab3
commit
ac6cec7677
@@ -12,8 +12,34 @@ function isDashScopeCompatibleEndpoint(baseUrl: string): boolean {
|
||||
);
|
||||
}
|
||||
|
||||
function isAnthropicMessagesModel(model: Model<Api>): model is Model<"anthropic-messages"> {
|
||||
return model.api === "anthropic-messages";
|
||||
}
|
||||
|
||||
/**
|
||||
* pi-ai constructs the Anthropic API endpoint as `${baseUrl}/v1/messages`.
|
||||
* If a user configures `baseUrl` with a trailing `/v1` (e.g. the previously
|
||||
* recommended format "https://api.anthropic.com/v1"), the resulting URL
|
||||
* becomes "…/v1/v1/messages" which the Anthropic API rejects with a 404.
|
||||
*
|
||||
* Strip a single trailing `/v1` (with optional trailing slash) from the
|
||||
* baseUrl for anthropic-messages models so users with either format work.
|
||||
*/
|
||||
function normalizeAnthropicBaseUrl(baseUrl: string): string {
|
||||
return baseUrl.replace(/\/v1\/?$/, "");
|
||||
}
|
||||
export function normalizeModelCompat(model: Model<Api>): Model<Api> {
|
||||
const baseUrl = model.baseUrl ?? "";
|
||||
|
||||
// Normalise anthropic-messages baseUrl: strip trailing /v1 that users may
|
||||
// have included in their config. pi-ai appends /v1/messages itself.
|
||||
if (isAnthropicMessagesModel(model) && baseUrl) {
|
||||
const normalised = normalizeAnthropicBaseUrl(baseUrl);
|
||||
if (normalised !== baseUrl) {
|
||||
return { ...model, baseUrl: normalised } as Model<"anthropic-messages">;
|
||||
}
|
||||
}
|
||||
|
||||
const isZai = model.provider === "zai" || baseUrl.includes("api.z.ai");
|
||||
const isMoonshot =
|
||||
model.provider === "moonshot" ||
|
||||
|
||||
Reference in New Issue
Block a user