mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-26 02:31:40 +00:00
* fix(ai): treat models-list 404 as a warning and send Anthropic Bearer on custom bases Custom OpenAI-compatible gateways that omit a /models listing were shown as a hard detect failure on HTTP 404. Treat 404 as a warning. Official Anthropic stays on x-api-key only; custom Anthropic-compat bases also send Authorization: Bearer so Agent Plan and similar proxies authenticate. * fix(ai): use the versioned Ark Agent Plan messages endpoint
102 lines
4.3 KiB
TypeScript
102 lines
4.3 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
|
|
import {
|
|
buildModelDiscoveryHeaders,
|
|
isCustomAnthropicBaseURL,
|
|
resolveModelsDiscoveryEndpoint,
|
|
} from "./modelDiscoveryHeaders";
|
|
|
|
test("buildModelDiscoveryHeaders uses x-api-key+anthropic-version only on official anthropic host", () => {
|
|
for (const baseURL of [undefined, "", "https://api.anthropic.com", "https://api.anthropic.com/v1"]) {
|
|
assert.deepEqual(buildModelDiscoveryHeaders("anthropic", "sk-test", {}, baseURL), {
|
|
"x-api-key": "sk-test",
|
|
"anthropic-version": "2023-06-01",
|
|
});
|
|
assert.equal(isCustomAnthropicBaseURL(baseURL), false);
|
|
}
|
|
});
|
|
|
|
test("buildModelDiscoveryHeaders sends both x-api-key and Bearer auth on custom anthropic bases", () => {
|
|
for (const baseURL of [
|
|
"https://ark.cn-beijing.volces.com/api/plan",
|
|
"https://gateway.example/v1",
|
|
]) {
|
|
assert.equal(isCustomAnthropicBaseURL(baseURL), true);
|
|
assert.deepEqual(buildModelDiscoveryHeaders("anthropic", "sk-test", {}, baseURL), {
|
|
"x-api-key": "sk-test",
|
|
Authorization: "Bearer sk-test",
|
|
"anthropic-version": "2023-06-01",
|
|
});
|
|
}
|
|
});
|
|
|
|
test("buildModelDiscoveryHeaders uses Bearer auth for the openai-compatible family", () => {
|
|
assert.deepEqual(buildModelDiscoveryHeaders("openai", "sk-test"), {
|
|
Authorization: "Bearer sk-test",
|
|
});
|
|
});
|
|
|
|
test("buildModelDiscoveryHeaders uses x-goog-api-key for the google family", () => {
|
|
// Google Generative AI rejects Bearer auth — discovery has to match
|
|
// the createGoogle runtime client, which uses x-goog-api-key.
|
|
assert.deepEqual(buildModelDiscoveryHeaders("google", "AIza-test"), {
|
|
"x-goog-api-key": "AIza-test",
|
|
});
|
|
});
|
|
|
|
test("buildModelDiscoveryHeaders returns no headers when the api key is missing", () => {
|
|
assert.deepEqual(buildModelDiscoveryHeaders("anthropic", undefined), {});
|
|
assert.deepEqual(buildModelDiscoveryHeaders("openai", ""), {});
|
|
});
|
|
|
|
test("buildModelDiscoveryHeaders honors the style override on an anthropic providerId pointing at an OpenAI-compatible backend", () => {
|
|
// Regression: PR #1105 lets users pick `style` independently from
|
|
// `providerId`. Without this fix the discovery call still sent
|
|
// `x-api-key` because the old code switched on `providerId === "anthropic"`.
|
|
assert.deepEqual(buildModelDiscoveryHeaders("openai", "sk-test"), {
|
|
Authorization: "Bearer sk-test",
|
|
});
|
|
});
|
|
|
|
test("resolveModelsDiscoveryEndpoint follows the resolved style by default", () => {
|
|
assert.equal(resolveModelsDiscoveryEndpoint("openai"), "/models");
|
|
assert.equal(resolveModelsDiscoveryEndpoint("anthropic"), "/v1/models");
|
|
assert.equal(resolveModelsDiscoveryEndpoint("google"), undefined);
|
|
});
|
|
|
|
test("resolveModelsDiscoveryEndpoint overrides the preset path when style flips", () => {
|
|
// Anthropic providerId preset would otherwise pin /v1/models, but the user
|
|
// switched style to openai — pick /models instead so the path matches the
|
|
// protocol family the headers already speak.
|
|
assert.equal(resolveModelsDiscoveryEndpoint("openai", "/v1/models"), "/models");
|
|
assert.equal(resolveModelsDiscoveryEndpoint("anthropic", "/models"), "/v1/models");
|
|
});
|
|
|
|
test("resolveModelsDiscoveryEndpoint falls back to the preset path only when the style has no convention", () => {
|
|
// google has no STYLE_DEFAULT — preserve whatever the caller passed.
|
|
assert.equal(resolveModelsDiscoveryEndpoint("google", "/custom/list"), "/custom/list");
|
|
assert.equal(resolveModelsDiscoveryEndpoint("google", undefined), undefined);
|
|
});
|
|
|
|
test('custom discovery headers override default auth regardless of case', () => {
|
|
for (const [style, name] of [['openai', 'authorization'], ['anthropic', 'X-API-Key'], ['google', 'X-Goog-Api-Key']] as const) {
|
|
const headers = buildModelDiscoveryHeaders(style, 'default-key', { [name]: 'custom-key', 'X-Tenant': 'tenant' });
|
|
assert.equal(headers[name], 'custom-key');
|
|
assert.equal(Object.keys(headers).filter(key => key.toLowerCase() === name.toLowerCase()).length, 1);
|
|
assert.equal(headers['X-Tenant'], 'tenant');
|
|
}
|
|
|
|
const customAnthropicAuth = buildModelDiscoveryHeaders(
|
|
'anthropic',
|
|
'default-key',
|
|
{ authorization: 'Bearer custom-token' },
|
|
'https://ark.cn-beijing.volces.com/api/plan',
|
|
);
|
|
assert.deepEqual(customAnthropicAuth, {
|
|
'x-api-key': 'default-key',
|
|
'anthropic-version': '2023-06-01',
|
|
authorization: 'Bearer custom-token',
|
|
});
|
|
});
|