feat(go-models): migrate batch 5 model drivers to unified handlers (#17700)

## Summary

Relate to #17284. Completes the batch 5 migration of 7 OpenAI-compatible
drivers (`vllm`, `volcengine`, `xai`, `xiaomi`, `xinference`, `xunfei`,
`zhipu-ai`) onto the unified request/response helpers
(`doRequest`/`doStreamRequest` +
`HandleNonStreamingResponse`/`HandleStreamingResponse` +
`ParserConfig`), established by `deepseek` in #17634.

This branch is rebased on the current `pr/migrate-models-batch5` and
fixes the issues in the previous state of the PR.

Co-authored-by: Haruko386 <tryeverypossible@163.com>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
jay77721
2026-08-03 20:19:22 +08:00
committed by GitHub
parent 1d141aff18
commit d357eea8ef
17 changed files with 478 additions and 1486 deletions

View File

@@ -27,12 +27,7 @@ import (
// HandleNonStreamingResponse processes a complete non-streaming chat
// response using the ParserConfig's ResponseParser to extract usage.
func HandleNonStreamingResponse(
body []byte,
modelUsage *common.ModelUsage,
chatConfig *ChatConfig,
cfg *ParserConfig,
) (*ChatResponse, error) {
func HandleNonStreamingResponse(body []byte, modelUsage *common.ModelUsage, chatConfig *ChatConfig, cfg *ParserConfig) (*ChatResponse, error) {
var result map[string]any
if err := json.Unmarshal(body, &result); err != nil {
return nil, fmt.Errorf("failed to parse response: %w", err)
@@ -75,13 +70,7 @@ func HandleNonStreamingResponse(
// HandleStreamingResponse processes a streaming chat response using the
// ParserConfig's StreamParser to extract usage from each event.
func HandleStreamingResponse(
body io.Reader,
modelUsage *common.ModelUsage,
chatConfig *ChatConfig,
cfg *ParserConfig,
sender func(*string, *string) error,
) error {
func HandleStreamingResponse(body io.Reader, modelUsage *common.ModelUsage, chatConfig *ChatConfig, cfg *ParserConfig, sender func(*string, *string) error) error {
if sender == nil {
return fmt.Errorf("sender is required")
}
@@ -103,6 +92,14 @@ func HandleStreamingResponse(
return fmt.Errorf("upstream stream error: %v", apiErr)
}
// Some providers emit a terminal event that carries only a root-level
// finish_reason with no choices. Check it before the choices guard so
// the stream terminates cleanly instead of being rejected as "ended
// before [DONE] or finish_reason".
if finishReason, ok := event["finish_reason"].(string); ok && finishReason != "" {
sawTerminal = true
}
choices, ok := event["choices"].([]any)
if !ok || len(choices) == 0 {
return nil
@@ -142,9 +139,6 @@ func HandleStreamingResponse(
if finishReason, ok := firstChoice["finish_reason"].(string); ok && finishReason != "" {
sawTerminal = true
}
if finishReason, ok := event["finish_reason"].(string); ok && finishReason != "" {
sawTerminal = true
}
return nil
})