mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 05:01:03 +03:00
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:
committed by
GitHub
Unverified
parent
57f4a364e3
commit
49f52ff2d5
@@ -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 }
|
||||
|
||||
@@ -42,7 +42,6 @@ export const configure = (input: LanguageModelOptions = {}) => {
|
||||
id: modelID,
|
||||
compatibility: {
|
||||
maxTokensField: "max_tokens",
|
||||
supportsUsageInStreaming: true,
|
||||
reasoningField: "reasoning_content",
|
||||
supportsStrictMode: false,
|
||||
},
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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. */
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user