From 72c456f39cf4f03e1e392dce58d7388c23683281 Mon Sep 17 00:00:00 2001 From: "coderabbitai[bot]" <136622811+coderabbitai[bot]@users.noreply.github.com> Date: Sat, 4 Oct 2025 05:17:32 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=9D=20Add=20docstrings=20to=20`main`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Docstrings generation was requested by @MomentDerek. * https://github.com/QuantumNous/new-api/pull/1967#issuecomment-3367913086 The following files were modified: * `relay/channel/gemini/relay-gemini.go` --- relay/channel/gemini/relay-gemini.go | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/relay/channel/gemini/relay-gemini.go b/relay/channel/gemini/relay-gemini.go index c8e9c7579..18252db86 100644 --- a/relay/channel/gemini/relay-gemini.go +++ b/relay/channel/gemini/relay-gemini.go @@ -910,6 +910,20 @@ func handleFinalStream(c *gin.Context, info *relaycommon.RelayInfo, resp *dto.Ch return nil } +// GeminiChatStreamHandler processes a streaming Gemini chat HTTP response, converts streamed chunks to OpenAI-style +// streaming messages, forwards them to the client, and accumulates usage metrics. +// +// GeminiChatStreamHandler reads Gemini stream chunks, translates them into intermediate OpenAI-style stream +// responses (including handling tool calls and inline media), sends an initial empty start response when the first +// chunk arrives, streams subsequent deltas, and sends a final stop and usage response when the stream ends. +// It tracks prompt/completion/reasoning token counts and accounts for inline images when computing usage. +// +// Parameters: +// - c: the Gin request context used for writing responses and logging. +// - info: relay metadata including upstream model name and token counts. +// - resp: the upstream Gemini HTTP response body to consume. +// +// Returns the accumulated usage metrics on success, or a NewAPIError when no responses were received from Gemini. func GeminiChatStreamHandler(c *gin.Context, info *relaycommon.RelayInfo, resp *http.Response) (*dto.Usage, *types.NewAPIError) { // responseText := "" id := helper.GetResponseID(c) @@ -961,9 +975,15 @@ func GeminiChatStreamHandler(c *gin.Context, info *relaycommon.RelayInfo, resp * // send first response emptyResponse := helper.GenerateStartEmptyResponse(id, createAt, info.UpstreamModelName, nil) if response.IsToolCall() { - emptyResponse.Choices[0].Delta.ToolCalls = make([]dto.ToolCallResponse, 1) - emptyResponse.Choices[0].Delta.ToolCalls[0] = *response.GetFirstToolCall() - emptyResponse.Choices[0].Delta.ToolCalls[0].Function.Arguments = "" + if len(emptyResponse.Choices) > 0 { + toolCalls := response.Choices[0].Delta.ToolCalls + copiedToolCalls := make([]dto.ToolCallResponse, len(toolCalls)) + for idx := range toolCalls { + copiedToolCalls[idx] = toolCalls[idx] + copiedToolCalls[idx].Function.Arguments = "" + } + emptyResponse.Choices[0].Delta.ToolCalls = copiedToolCalls + } finishReason = constant.FinishReasonToolCalls err = handleStream(c, info, emptyResponse) if err != nil { @@ -1190,4 +1210,4 @@ func GeminiImageHandler(c *gin.Context, info *relaycommon.RelayInfo, resp *http. } return usage, nil -} +} \ No newline at end of file