mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-11-18 15:48:39 +08:00
- Treat cancellation after an emitted terminal event as successful completion while retaining observed token usage. - Preserve buffered usage on genuine stream failures and share terminal detection with Vertex. - Add regression coverage for terminal disconnects, EOF, usage tails, and read errors. Closes: #6821
38 lines
1.0 KiB
Go
38 lines
1.0 KiB
Go
package helps
|
|
|
|
import (
|
|
"bytes"
|
|
"strings"
|
|
|
|
"github.com/tidwall/gjson"
|
|
)
|
|
|
|
// IsGeminiTerminalStreamChunk recognizes terminal output from Gemini stream translators.
|
|
func IsGeminiTerminalStreamChunk(chunk []byte) bool {
|
|
for _, line := range bytes.Split(chunk, []byte("\n")) {
|
|
trimmed := bytes.TrimSpace(line)
|
|
if bytes.Equal(trimmed, []byte("data: [DONE]")) || bytes.Equal(trimmed, []byte("[DONE]")) {
|
|
return true
|
|
}
|
|
payload := JSONPayload(line)
|
|
if len(payload) == 0 {
|
|
continue
|
|
}
|
|
eventType := gjson.GetBytes(payload, "type").String()
|
|
if eventType == "response.completed" || eventType == "response.incomplete" || eventType == "response.done" || eventType == "message_stop" {
|
|
return true
|
|
}
|
|
for _, path := range []string{
|
|
"choices.0.finish_reason",
|
|
"response.choices.0.finish_reason",
|
|
"candidates.0.finishReason",
|
|
"response.candidates.0.finishReason",
|
|
} {
|
|
if finishReason := gjson.GetBytes(payload, path); finishReason.Exists() && strings.TrimSpace(finishReason.String()) != "" {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|