RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
fix(go-models): align LongCat usage handling with DeepSeek and official docs (#17558)
jay77721b97969891cb2
diff --git a/internal/entity/models/longcat.go b/internal/entity/models/longcat.goindex 339917a4cf..28cee88fc7 100644--- a/internal/entity/models/longcat.go+++ b/internal/entity/models/longcat.go@@ -52,8 +52,7 @@ func (l *LongCatModel) Name() string {}// LongCatChatResponse mirrors the OpenAI-compatible response returned by-// LongCat's POST /openai/v1/chat/completions endpoint. Usage is always-// present on a successful response.+// LongCat's POST /openai/v1/chat/completions endpoint.type LongCatChatResponse struct {ID string `json:"id"`Object string `json:"object"`@@ -70,9 +69,12 @@ type LongCatChatResponse struct {} `json:"message"`} `json:"choices"`Usage struct {- PromptTokens int `json:"prompt_tokens"`- CompletionTokens int `json:"completion_tokens"`- TotalTokens int `json:"total_tokens"`+ PromptTokens int `json:"prompt_tokens"`+ CompletionTokens int `json:"completion_tokens"`+ TotalTokens int `json:"total_tokens"`+ CompletionTokensDetails struct {+ ReasoningTokens int `json:"reasoning_tokens"`+ } `json:"completion_tokens_details"`} `json:"usage"`}@@ -139,17 +141,13 @@ func (l *LongCatModel) ChatWithMessages(ctx context.Context, modelName string, mchoice := &result.Choices[0]content := choice.Message.Content// LongCat-Flash-Thinking may emit all output as reasoning_content- // with content left null/empty. That is a valid response — only- // reject when there is neither content, reasoning, nor tool calls.+ // with content left empty. That is a valid response — only reject when+ // there is neither content, reasoning, nor tool calls.if content == "" && choice.Message.ReasoningContent == "" && len(choice.Message.ToolCalls) == 0 {return chatResponseParts{}, fmt.Errorf("invalid content format")}- // LongCat 2.0 returns the chain-of-thought in a- // `reasoning_content` field on the message (OpenAI o-series shape,- // also used by kimi-k2.6 and DeepSeek-R1). The field is typically- // prefixed with a leading newline — trim it so callers see clean- // reasoning. Absent or empty means no reasoning was emitted.+ // reasoning_content is typically prefixed with a leading newline.reasonContent := choice.Message.ReasoningContentif reasonContent != "" && reasonContent[0] == '\n' {reasonContent = reasonContent[1:]@@ -241,6 +239,9 @@ func (l *LongCatModel) ChatStreamlyWithSender(ctx context.Context, modelName str}if found {applyStreamUsage(chatModelConfig, modelUsage, tokenUsage)+ // Aggregate counts only — the full event carries content/reasoning_content.+ common.Info(fmt.Sprintf("longcat: usage prompt=%d completion=%d total=%d",+ tokenUsage.PromptTokens, tokenUsage.CompletionTokens, tokenUsage.TotalTokens))}choices, ok := event["choices"].([]interface{})diff --git a/internal/entity/models/longcat_test.go b/internal/entity/models/longcat_test.goindex c37033241f..128dc17064 100644--- a/internal/entity/models/longcat_test.go+++ b/internal/entity/models/longcat_test.go@@ -176,9 +176,9 @@ func TestLongCatChatExtractsReasoningContent(t *testing.T) {}}-// TestLongCatChatParsesUsage verifies that the typed LongCat response-// parser extracts the OpenAI-compatible usage block into ChatResponse.Usage.-// LongCat always returns usage on success, so the field must be populated.+// TestLongCatChatParsesUsage verifies that the typed response parser+// extracts the OpenAI-compatible usage block, including the nested+// completion_tokens_details.reasoning_tokens from LongCat's thinking mode.func TestLongCatChatParsesUsage(t *testing.T) {ctx := t.Context()srv := newLongCatServer(t, "/openai/v1/chat/completions", func(t *testing.T, _ *http.Request, body map[string]interface{}, w http.ResponseWriter) {@@ -199,6 +199,9 @@ func TestLongCatChatParsesUsage(t *testing.T) {"prompt_tokens": 20,"completion_tokens": 15,"total_tokens": 35,+ "completion_tokens_details": map[string]interface{}{+ "reasoning_tokens": 78,+ },},})})@@ -280,9 +283,55 @@ func TestLongCatChatAcceptsReasoningOnlyResponse(t *testing.T) {}}+// TestLongCatChatDropsUndocumentedFields guards against re-introducing+// stop / reasoning_effort / response_format / tools etc. The LongCat+// docs only list model, messages, stream, max_tokens, temperature,+// top_p — anything else is undocumented and must not be sent, since+// the maintainer specifically flagged this on PR #14809.+func TestLongCatChatDropsUndocumentedFields(t *testing.T) {+ ctx := t.Context()+ srv := newLongCatServer(t, "/openai/v1/chat/completions", func(t *testing.T, _ *http.Request, body map[string]interface{}, w http.ResponseWriter) {+ for _, k := range []string{"stop", "reasoning_effort", "response_format", "tools", "tool_choice", "presence_penalty", "frequency_penalty", "n", "logprobs"} {+ if _, present := body[k]; present {+ t.Errorf("undocumented field %q must not be sent: %v", k, body[k])+ }+ }+ // Documented fields, on the other hand, MUST be forwarded when set.+ for _, k := range []string{"model", "messages", "stream", "max_tokens", "temperature", "top_p"} {+ if _, present := body[k]; !present {+ t.Errorf("documented field %q missing from request body", k)+ }+ }+ _ = json.NewEncoder(w).Encode(map[string]interface{}{+ "choices": []map[string]interface{}{{+ "message": map[string]interface{}{"content": "ok"},+ }},+ })+ })+ defer srv.Close()++ m := newLongCatForTest(srv.URL)+ apiKey := "test-key"+ mt := 32+ temp := 0.7+ topP := 0.9+ stop := []string{"END"}+ effort := "high"+ _, err := m.ChatWithMessages(ctx, "LongCat-Flash-Chat",+ []Message{{Role: "user", Content: "x"}},+ &APIConfig{ApiKey: &apiKey},+ // Deliberately pass Stop/Effort to prove they are filtered out.+ &ChatConfig{MaxTokens: &mt, Temperature: &temp, TopP: &topP, Stop: &stop, Effort: &effort},+ nil,+ )+ if err != nil {+ t.Fatalf("Chat: %v", err)+ }+}+// TestLongCatStreamRequestsIncludeUsage verifies that the streaming driver-// asks LongCat for aggregate token usage via stream_options.include_usage,-// and that the usage event populates chatConfig.UsageResult.+// requests aggregate usage via stream_options.include_usage and populates+// chatConfig.UsageResult from the final usage event.func TestLongCatStreamRequestsIncludeUsage(t *testing.T) {ctx := t.Context()srv := newLongCatServer(t, "/openai/v1/chat/completions", func(t *testing.T, _ *http.Request, body map[string]interface{}, w http.ResponseWriter) {@@ -320,49 +369,43 @@ func TestLongCatStreamRequestsIncludeUsage(t *testing.T) {}}-// TestLongCatChatDropsUndocumentedFields guards against re-introducing-// stop / reasoning_effort / response_format / tools etc. The LongCat-// docs only list model, messages, stream, max_tokens, temperature,-// top_p — anything else is undocumented and must not be sent, since-// the maintainer specifically flagged this on PR #14809.-func TestLongCatChatDropsUndocumentedFields(t *testing.T) {+// TestLongCatStreamParsesReasoningTokens verifies that a streaming usage+// event carrying completion_tokens_details.reasoning_tokens populates+// chatConfig.UsageResult.+func TestLongCatStreamParsesReasoningTokens(t *testing.T) {ctx := t.Context()srv := newLongCatServer(t, "/openai/v1/chat/completions", func(t *testing.T, _ *http.Request, body map[string]interface{}, w http.ResponseWriter) {- for _, k := range []string{"stop", "reasoning_effort", "response_format", "tools", "tool_choice", "presence_penalty", "frequency_penalty", "n", "logprobs"} {- if _, present := body[k]; present {- t.Errorf("undocumented field %q must not be sent: %v", k, body[k])- }+ streamOpts, ok := body["stream_options"].(map[string]interface{})+ if !ok {+ t.Errorf("stream_options missing: %v", body)+ return}- // Documented fields, on the other hand, MUST be forwarded when set.- for _, k := range []string{"model", "messages", "stream", "max_tokens", "temperature", "top_p"} {- if _, present := body[k]; !present {- t.Errorf("documented field %q missing from request body", k)- }+ if inc, _ := streamOpts["include_usage"].(bool); !inc {+ t.Errorf("stream_options.include_usage=%v, want true", streamOpts["include_usage"])}- _ = json.NewEncoder(w).Encode(map[string]interface{}{- "choices": []map[string]interface{}{{- "message": map[string]interface{}{"content": "ok"},- }},- })+ w.Header().Set("Content-Type", "text/event-stream")+ _, _ = io.WriteString(w,+ `data: {"choices":[{"index":0,"delta":{"content":"hi"}}]}`+"\n"++ `data: {"choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":10,"completion_tokens":8,"total_tokens":18,"completion_tokens_details":{"reasoning_tokens":5}}}`+"\n"++ `data: [DONE]`+"\n")})defer srv.Close()m := newLongCatForTest(srv.URL)apiKey := "test-key"- mt := 32- temp := 0.7- topP := 0.9- stop := []string{"END"}- effort := "high"- _, err := m.ChatWithMessages(ctx, "LongCat-Flash-Chat",+ chatConfig := &ChatConfig{}+ err := m.ChatStreamlyWithSender(ctx, "LongCat-2.0",[]Message{{Role: "user", Content: "x"}},- &APIConfig{ApiKey: &apiKey},- // Deliberately pass Stop/Effort to prove they are filtered out.- &ChatConfig{MaxTokens: &mt, Temperature: &temp, TopP: &topP, Stop: &stop, Effort: &effort},- nil,- )+ &APIConfig{ApiKey: &apiKey}, chatConfig, nil,+ func(*string, *string) error { return nil })if err != nil {- t.Fatalf("Chat: %v", err)+ t.Fatalf("stream: %v", err)+ }+ if chatConfig.UsageResult == nil {+ t.Fatal("UsageResult must be non-nil after stream with usage event")+ }+ if chatConfig.UsageResult.PromptTokens != 10 || chatConfig.UsageResult.CompletionTokens != 8 || chatConfig.UsageResult.TotalTokens != 18 {+ t.Errorf("UsageResult=%#v, want prompt=10 completion=8 total=18", chatConfig.UsageResult)}}