feat(schema): add streamUsage compatibility to turn off stream_options (#57255)

Co-authored-by: rekram1-node <63023139+rekram1-node@users.noreply.github.com>
This commit is contained in:
opencode-agent[bot]
2026-10-19 21:36:10 -05:00
committed by GitHub
Unverified
parent 57f4a364e3
commit 49f52ff2d5
10 changed files with 60 additions and 7 deletions
+1 -5
View File
@@ -730,8 +730,6 @@ const detectSupportsStore = (provider: string, baseURL: string | undefined): boo
)
}
const detectSupportsUsageInStreaming = (): boolean => true
const detectSupportsStrictMode = (provider: string, baseURL: string | undefined): boolean => {
const p = provider.toLowerCase()
const url = (baseURL ?? "").toLowerCase()
@@ -790,8 +788,6 @@ export const fromRequest = Effect.fn("OpenAIChat.fromRequest")(function* (
const detectedMaxTokensField = detectMaxTokensField(provider, baseURL)
const maxTokensField = request.model.compatibility?.maxTokensField ?? detectedMaxTokensField
const supportsStore = request.model.compatibility?.supportsStore ?? detectSupportsStore(provider, baseURL)
const supportsUsageInStreaming =
request.model.compatibility?.supportsUsageInStreaming ?? detectSupportsUsageInStreaming()
const supportsStrictMode =
request.model.compatibility?.supportsStrictMode ?? detectSupportsStrictMode(provider, baseURL)
const zaiToolStream =
@@ -810,7 +806,7 @@ export const fromRequest = Effect.fn("OpenAIChat.fromRequest")(function* (
: flattened.tools.map((tool) => lowerTool(tool, lowering, supportsStrictMode)),
tool_choice: hasActiveTools && request.toolChoice ? yield* lowerToolChoice(request.toolChoice) : undefined,
stream: true as const,
...(supportsUsageInStreaming ? { stream_options: { include_usage: true } } : {}),
...((request.model.compatibility?.streamUsage ?? true) ? { stream_options: { include_usage: true } } : {}),
...(zaiToolStream && hasActiveTools ? { tool_stream: true } : {}),
...(maxTokensField === "max_completion_tokens"
? { max_completion_tokens: generation?.maxTokens }
-1
View File
@@ -42,7 +42,6 @@ export const configure = (input: LanguageModelOptions = {}) => {
id: modelID,
compatibility: {
maxTokensField: "max_tokens",
supportsUsageInStreaming: true,
reasoningField: "reasoning_content",
supportsStrictMode: false,
},
+2 -1
View File
@@ -221,7 +221,8 @@ export class LanguageModelCompatibility extends Schema.Class<LanguageModelCompat
requireFinishReason: Schema.optional(Schema.Boolean),
requireAssistantAfterTool: Schema.optional(Schema.Boolean),
supportsStore: Schema.optional(Schema.Boolean),
supportsUsageInStreaming: Schema.optional(Schema.Boolean),
/** Request usage in the stream with `stream_options.include_usage`. Defaults to true. */
streamUsage: Schema.optional(Schema.Boolean),
supportsStrictMode: Schema.optional(Schema.Boolean),
// Accepts `prompt_cache_key` in the request body. Chat omits the key unless
// this is true; other protocols omit it only when this is false.
@@ -64,6 +64,16 @@ describe("OpenAI Chat route", () => {
}),
)
it.effect("omits stream usage options when compatibility disables them", () =>
Effect.gen(function* () {
const prepared = yield* compileRequest(
LLM.request({ model: LanguageModel.update(model, { compatibility: { streamUsage: false } }), prompt: "Hello" }),
)
expect(prepared.body).not.toHaveProperty("stream_options")
}),
)
it.effect("sends store only to OpenAI and Azure unless compatibility opts in", () =>
Effect.gen(function* () {
const compatible = OpenAICompatible.configure({ baseURL: "https://api.compatible.test/v1", apiKey: "test" })
@@ -640,6 +640,7 @@ export type ModelCompatibility = {
requireFinishReason?: boolean
requireAssistantAfterTool?: boolean
supportsPromptCacheKey?: boolean
streamUsage?: boolean
cacheFormat?: ModelCacheFormat
supportsThinkingBlockBinding?: boolean
supportsEffortUpdates?: boolean
@@ -652,6 +653,7 @@ export type ProviderCompatibility = {
requireFinishReason?: boolean
requireAssistantAfterTool?: boolean
supportsPromptCacheKey?: boolean
streamUsage?: boolean
cacheFormat?: ModelCacheFormat
}
@@ -241,6 +241,31 @@ describe("ConfigProviderPlugin.Plugin", () => {
}),
)
it.effect("passes configured stream usage compatibility to the runtime model", () =>
Effect.gen(function* () {
const providers = yield* Provider.Service
const models = yield* Model.Service
const providerID = Provider.ID.make("strict")
yield* providers.transform((editor) => {
editor.update(providerID, (provider) => {
provider.package = "@opencode/ai/providers/openai-compatible"
provider.settings = { baseURL: "https://strict.test/v1" }
})
editor.models.update(providerID, Model.ID.make("catalog"), () => {})
})
yield* addPlugin([
new Document({
type: "document",
info: decode({ providers: { strict: { compatibility: { streamUsage: false } } } }),
}),
])
const catalog = required(yield* models.get(providerID, Model.ID.make("catalog")))
expect(catalog.compatibility).toEqual({ streamUsage: false })
expect((yield* ModelResolver.fromCatalogModel(catalog)).compatibility?.streamUsage).toBe(false)
}),
)
it.effect("keeps the provider websocket policy out of model settings", () =>
Effect.gen(function* () {
const providers = yield* Provider.Service
+6
View File
@@ -16947,6 +16947,9 @@
"supportsPromptCacheKey": {
"type": "boolean"
},
"streamUsage": {
"type": "boolean"
},
"cacheFormat": {
"$ref": "#/components/schemas/Model.CacheFormat"
},
@@ -18069,6 +18072,9 @@
"supportsPromptCacheKey": {
"type": "boolean"
},
"streamUsage": {
"type": "boolean"
},
"cacheFormat": {
"$ref": "#/components/schemas/Model.CacheFormat"
}
@@ -25,6 +25,8 @@ export const Compatibility = Schema.Struct({
requireFinishReason: Schema.Boolean.pipe(optional),
requireAssistantAfterTool: Schema.Boolean.pipe(optional),
supportsPromptCacheKey: Schema.Boolean.pipe(optional),
/** Request usage in the stream with `stream_options.include_usage`. Defaults to true. */
streamUsage: Schema.Boolean.pipe(optional),
/** Prompt-cache marker syntax the endpoint accepts. Overrides the route default; both send `cache_control`, and `alibaba` skips tool definitions. */
cacheFormat: CacheFormat.pipe(optional),
/** Supports Anthropic's thinking-prefix mismatch controls. Overrides model-ID detection. */
+6
View File
@@ -16947,6 +16947,9 @@
"supportsPromptCacheKey": {
"type": "boolean"
},
"streamUsage": {
"type": "boolean"
},
"cacheFormat": {
"$ref": "#/components/schemas/Model.CacheFormat"
},
@@ -18069,6 +18072,9 @@
"supportsPromptCacheKey": {
"type": "boolean"
},
"streamUsage": {
"type": "boolean"
},
"cacheFormat": {
"$ref": "#/components/schemas/Model.CacheFormat"
}
+6
View File
@@ -16947,6 +16947,9 @@
"supportsPromptCacheKey": {
"type": "boolean"
},
"streamUsage": {
"type": "boolean"
},
"cacheFormat": {
"$ref": "#/components/schemas/Model.CacheFormat"
},
@@ -18069,6 +18072,9 @@
"supportsPromptCacheKey": {
"type": "boolean"
},
"streamUsage": {
"type": "boolean"
},
"cacheFormat": {
"$ref": "#/components/schemas/Model.CacheFormat"
}