Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions relay/channel/openai/relay-openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ func OaiStreamHandler(c *gin.Context, info *relaycommon.RelayInfo, resp *http.Re
var usage = &dto.Usage{}
var streamItems []string // store stream items
var lastStreamData string
var secondLastStreamData string // 存储倒数第二个stream data,用于音频模型

// 检查是否为音频模型
isAudioModel := strings.Contains(strings.ToLower(model), "audio")

helper.StreamScannerHandler(c, resp, info, func(data string) bool {
if lastStreamData != "" {
Expand All @@ -131,12 +135,35 @@ func OaiStreamHandler(c *gin.Context, info *relaycommon.RelayInfo, resp *http.Re
}
}
if len(data) > 0 {
// 对音频模型,保存倒数第二个stream data
if isAudioModel && lastStreamData != "" {
secondLastStreamData = lastStreamData
}

lastStreamData = data
streamItems = append(streamItems, data)
}
return true
})

// 对音频模型,从倒数第二个stream data中提取usage信息
if isAudioModel && secondLastStreamData != "" {
var streamResp struct {
Usage *dto.Usage `json:"usage"`
}
err := json.Unmarshal([]byte(secondLastStreamData), &streamResp)
if err == nil && streamResp.Usage != nil && service.ValidUsage(streamResp.Usage) {
usage = streamResp.Usage
containStreamUsage = true

if common.DebugEnabled {
logger.LogDebug(c, fmt.Sprintf("Audio model usage extracted from second last SSE: PromptTokens=%d, CompletionTokens=%d, TotalTokens=%d, InputTokens=%d, OutputTokens=%d",
usage.PromptTokens, usage.CompletionTokens, usage.TotalTokens,
usage.InputTokens, usage.OutputTokens))
}
}
}
Comment on lines +149 to +165

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

Verify that handleLastResponse doesn't overwrite audio model usage.

The usage extraction logic is correct and properly validates the usage before setting containStreamUsage = true. However, handleLastResponse is called after this block (line 169), which could potentially modify usage or reset containStreamUsage, thereby overwriting the audio model usage you just extracted.

Run the following script to examine handleLastResponse and verify it won't overwrite the audio model usage:


🏁 Script executed:

#!/bin/bash
# Description: Find and examine handleLastResponse function to verify it doesn't unconditionally overwrite usage or containStreamUsage

# Find the handleLastResponse function definition
ast-grep --pattern 'func handleLastResponse($$$) $$$'

# Also search for any assignments to containStreamUsage within handleLastResponse
rg -n -A 10 -B 2 'func handleLastResponse' 

Length of output: 2840


handleLastResponse will overwrite audio model usage extracted from secondLastStreamData if the last stream also contains valid usage.

The audio model usage extraction at lines 149-165 is undermined because handleLastResponse (called at line 169) unconditionally overwrites usage and containStreamUsage when lastStreamData contains valid usage. This causes the second-to-last SSE extraction to be discarded if the final SSE response has usage data, defeating the purpose of the audio model special handling.

To fix this, handleLastResponse should only update usage for audio models if containStreamUsage is not already set (to preserve the secondLastStreamData extraction), or explicitly document that final response usage takes precedence for all models.

🤖 Prompt for AI Agents
In relay/channel/openai/relay-openai.go around lines 149-165, the code extracts
usage from secondLastStreamData for audio models but handleLastResponse (invoked
at line 169) later unconditionally overwrites usage; update the logic so
handleLastResponse does not replace usage for audio models when
containStreamUsage is already true (i.e., if isAudioModel && containStreamUsage
then skip updating usage), or alternatively add a parameter/flag to
handleLastResponse indicating stream-derived usage should be preserved and honor
that flag to avoid overwriting the extraction from secondLastStreamData.


// 处理最后的响应
shouldSendLastResp := true
if err := handleLastResponse(lastStreamData, &responseId, &createAt, &systemFingerprint, &model, &usage,
Expand Down