ensureValidToken 确保 token 有效
(account *config.Account)
| 2101 | continue |
| 2102 | } |
| 2103 | h.recordFailureWithDetails("openai", model, account.ID, err) |
| 2104 | sendStreamError(err) |
| 2105 | return |
| 2106 | } |
| 2107 | |
| 2108 | processText("", false, true) |
| 2109 | if eventThinkingOpen { |
| 2110 | sendChunk("", 3) |
| 2111 | } |
| 2112 | |
| 2113 | if realInputTokens > 0 { |
| 2114 | inputTokens = realInputTokens |
| 2115 | } else if inputTokens <= 0 { |
| 2116 | inputTokens = estimatedInputTokens |
| 2117 | } |
| 2118 | outputContent, extractedReasoning := extractThinkingFromContent(rawContentBuilder.String()) |
| 2119 | reasoningOutput := rawReasoningBuilder.String() |
| 2120 | if thinking && reasoningOutput == "" && extractedReasoning != "" { |
| 2121 | reasoningOutput = extractedReasoning |
| 2122 | } |
| 2123 | if !thinking { |
| 2124 | reasoningOutput = "" |
| 2125 | } |
| 2126 | outputTokens = estimateApproxTokens(outputContent) + estimateApproxTokens(reasoningOutput) |
| 2127 | for _, tc := range toolCalls { |
| 2128 | outputTokens += estimateApproxTokens(tc.Function.Name) |
| 2129 | outputTokens += estimateApproxTokens(tc.Function.Arguments) |
| 2130 | } |
| 2131 | |
| 2132 | h.recordSuccessForApiKey(apiKeyID, inputTokens, outputTokens, credits) |
| 2133 | h.pool.RecordSuccess(account.ID) |
| 2134 | h.pool.UpdateStats(account.ID, inputTokens+outputTokens, credits) |
| 2135 | h.recordSuccessLog("openai", model, account.ID, inputTokens+outputTokens, credits, time.Since(reqStart).Milliseconds()) |
| 2136 | finishReason := mapOpenAIFinishReason(upstreamStopReason, len(toolCalls)) |
| 2137 | |
| 2138 | chunk := map[string]interface{}{ |
| 2139 | "id": chatID, |
| 2140 | "object": "chat.completion.chunk", |
| 2141 | "created": time.Now().Unix(), |
| 2142 | "model": model, |
| 2143 | "choices": []map[string]interface{}{{ |
| 2144 | "index": 0, |
| 2145 | "delta": map[string]interface{}{}, |
| 2146 | "finish_reason": finishReason, |
no test coverage detected