mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-28 05:16:23 -06:00
fix(openai-completions): enable local streaming usage compat (#68711) (thanks @gaineyllc)
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { resolveOpenAICompletionsCompatDefaults } from "./openai-completions-compat.js";
|
||||
import {
|
||||
detectOpenAICompletionsCompat,
|
||||
resolveOpenAICompletionsCompatDefaults,
|
||||
} from "./openai-completions-compat.js";
|
||||
|
||||
describe("resolveOpenAICompletionsCompatDefaults", () => {
|
||||
it("keeps streaming usage enabled for provider-declared compatible endpoints", () => {
|
||||
@@ -33,4 +36,49 @@ describe("resolveOpenAICompletionsCompatDefaults", () => {
|
||||
}).supportsUsageInStreaming,
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it.each([
|
||||
"vllm",
|
||||
"localai",
|
||||
"sglang",
|
||||
"llama-cpp",
|
||||
"llama.cpp",
|
||||
"llamacpp",
|
||||
"jan",
|
||||
"lmstudio",
|
||||
"lm-studio",
|
||||
"text-generation-webui",
|
||||
"tabby",
|
||||
"tabbyapi",
|
||||
])("enables streaming usage compat for known local provider %s", (provider) => {
|
||||
expect(
|
||||
resolveOpenAICompletionsCompatDefaults({
|
||||
provider,
|
||||
endpointClass: "custom",
|
||||
knownProviderFamily: provider,
|
||||
}).supportsUsageInStreaming,
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("matches known local providers case-insensitively", () => {
|
||||
expect(
|
||||
resolveOpenAICompletionsCompatDefaults({
|
||||
provider: "vLLM",
|
||||
endpointClass: "local",
|
||||
knownProviderFamily: "vllm",
|
||||
}).supportsUsageInStreaming,
|
||||
).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("detectOpenAICompletionsCompat", () => {
|
||||
it("enables streaming usage compat for vLLM on a local OpenAI-compatible endpoint", () => {
|
||||
const detected = detectOpenAICompletionsCompat({
|
||||
provider: "vllm",
|
||||
baseUrl: "http://127.0.0.1:8000/v1",
|
||||
id: "Qwen/Qwen3-Coder-Next-FP8",
|
||||
});
|
||||
|
||||
expect(detected.defaults.supportsUsageInStreaming).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -30,6 +30,27 @@ function isDefaultRouteProvider(provider: string | undefined, ...ids: string[])
|
||||
return provider !== undefined && ids.includes(provider);
|
||||
}
|
||||
|
||||
const KNOWN_LOCAL_STREAMING_USAGE_PROVIDERS = new Set([
|
||||
"jan",
|
||||
"llama-cpp",
|
||||
"llama.cpp",
|
||||
"llamacpp",
|
||||
"lm-studio",
|
||||
"lmstudio",
|
||||
"localai",
|
||||
"sglang",
|
||||
"tabby",
|
||||
"tabbyapi",
|
||||
"text-generation-webui",
|
||||
"vllm",
|
||||
]);
|
||||
|
||||
function isKnownLocalStreamingUsageProvider(...ids: Array<string | undefined>): boolean {
|
||||
return ids.some(
|
||||
(id) => id !== undefined && KNOWN_LOCAL_STREAMING_USAGE_PROVIDERS.has(id.toLowerCase()),
|
||||
);
|
||||
}
|
||||
|
||||
export function resolveOpenAICompletionsCompatDefaults(
|
||||
input: OpenAICompletionsCompatDefaultsInput,
|
||||
): OpenAICompletionsCompatDefaults {
|
||||
@@ -67,6 +88,10 @@ export function resolveOpenAICompletionsCompatDefaults(
|
||||
endpointClass === "mistral-public" ||
|
||||
knownProviderFamily === "mistral" ||
|
||||
(isDefaultRoute && isDefaultRouteProvider(provider, "chutes"));
|
||||
const supportsKnownLocalStreamingUsage = isKnownLocalStreamingUsageProvider(
|
||||
provider,
|
||||
knownProviderFamily,
|
||||
);
|
||||
return {
|
||||
supportsStore:
|
||||
!isNonStandard && knownProviderFamily !== "mistral" && !usesExplicitProxyLikeEndpoint,
|
||||
@@ -77,7 +102,8 @@ export function resolveOpenAICompletionsCompatDefaults(
|
||||
endpointClass !== "xai-native" &&
|
||||
!usesExplicitProxyLikeEndpoint,
|
||||
supportsUsageInStreaming:
|
||||
!isNonStandard && (!usesConfiguredNonOpenAIEndpoint || supportsNativeStreamingUsageCompat),
|
||||
supportsKnownLocalStreamingUsage ||
|
||||
(!isNonStandard && (!usesConfiguredNonOpenAIEndpoint || supportsNativeStreamingUsageCompat)),
|
||||
maxTokensField: usesMaxTokens ? "max_tokens" : "max_completion_tokens",
|
||||
thinkingFormat: isZai ? "zai" : isOpenRouterLike ? "openrouter" : "openai",
|
||||
visibleReasoningDetailTypes: isOpenRouterLike ? ["response.output_text", "response.text"] : [],
|
||||
|
||||
@@ -1353,13 +1353,13 @@ describe("openai transport stream", () => {
|
||||
expect(params.stream_options).toMatchObject({ include_usage: true });
|
||||
});
|
||||
|
||||
it("always includes stream_options.include_usage for non-standard backends like llama-cpp", () => {
|
||||
it("always includes stream_options.include_usage for known local backends like llama-cpp", () => {
|
||||
const params = buildOpenAICompletionsParams(
|
||||
{
|
||||
id: "llama-3",
|
||||
name: "Llama 3",
|
||||
api: "openai-completions",
|
||||
provider: "custom-cpa",
|
||||
provider: "llama-cpp",
|
||||
baseUrl: "http://localhost:8080/v1",
|
||||
reasoning: false,
|
||||
input: ["text"],
|
||||
|
||||
Reference in New Issue
Block a user