mirror of
https://github.com/jasonxu114514/opencode2api.git
synced 2026-12-05 08:02:53 +00:00
fix: preserve Chat usage for shaped free requests
This commit is contained in:
1 parent
79378c8744
commit
b689a5b3a6
1 file changed
+23
@@ -344,6 +344,9 @@ func prepareAnonymousBody(body []byte, protocol wire.Protocol) []byte {
|
||||
payload["stream"] = true
|
||||
changed = true
|
||||
}
|
||||
if ensureAnonymousChatUsage(payload, protocol) {
|
||||
changed = true
|
||||
}
|
||||
if ensureAnonymousTools(payload, protocol) {
|
||||
changed = true
|
||||
}
|
||||
@@ -357,6 +360,26 @@ func prepareAnonymousBody(body []byte, protocol wire.Protocol) []byte {
|
||||
return encoded
|
||||
}
|
||||
|
||||
// ensureAnonymousChatUsage keeps token usage available when a non-streaming
|
||||
// request is forced onto the Chat SSE path. OpenAI-compatible Chat streams
|
||||
// require stream_options.include_usage for the final usage event.
|
||||
func ensureAnonymousChatUsage(payload map[string]any, protocol wire.Protocol) bool {
|
||||
if protocol != wire.Chat {
|
||||
return false
|
||||
}
|
||||
options, ok := payload["stream_options"].(map[string]any)
|
||||
if !ok {
|
||||
payload["stream_options"] = map[string]any{"include_usage": true}
|
||||
return true
|
||||
}
|
||||
includeUsage, ok := options["include_usage"].(bool)
|
||||
if ok && includeUsage {
|
||||
return false
|
||||
}
|
||||
options["include_usage"] = true
|
||||
return true
|
||||
}
|
||||
|
||||
// ensureAnonymousTools appends minimal definitions for any missing core
|
||||
// tool so the request reads as an agent session upstream. It reports
|
||||
// whether the payload changed. Tools the client already declared are left
|
||||
|
||||
Reference in new issue
Block a user