Repository navigation
Refactor(go-models)/llm token usage for longcat - #17480
Conversation
Add token usage reporting to LongCat's chat completion responses (non-streaming and streaming). Uses the shared parseChatCompletionResponse and applyStreamUsage helpers, aligned with the pattern established by PRs infiniflow#17289, infiniflow#17413, and infiniflow#17423. - Typed LongCatChatResponse struct mirroring the OpenAI-compatible schema - Extract usage from non-streaming responses (always present per API docs) - Add stream_options.include_usage for streaming usage events - Handle LongCat's thinking mode where content may be null and all output is returned in reasoning_content - Trim leading newline from reasoning_content (matches API behavior) - Tests: usage parsing, reasoning-only response, streaming UsageResult Related to infiniflow#17284 Co-Authored-By: Claude Fable 5 <<EMAIL>>
📝 WalkthroughWalkthroughLongCat chat responses now use typed OpenAI-compatible parsing, preserve reasoning-only responses, map token usage, and request and apply usage data during streaming. ChangesLongCat response and usage handling
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant LongCatClient
participant LongCatAPI
participant UsageDecoder
participant ChatModelConfig
LongCatClient->>LongCatAPI: Request stream with include_usage=true
LongCatAPI-->>LongCatClient: Emit SSE usage event
LongCatClient->>UsageDecoder: Decode stream usage
UsageDecoder-->>LongCatClient: Return token counts
LongCatClient->>ChatModelConfig: Apply UsageResult
Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@internal/entity/models/longcat_test.go`:
- Around line 266-270: Update the assertions for resp.Answer and
resp.ReasonContent to avoid dereferencing nil pointers in their t.Errorf
diagnostics. Preserve the nil-or-unexpected-value validation, but report each
pointer safely when the assertion fails so the test logs an error instead of
panicking.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 09b7eb09-e5d0-4133-894b-5d905d1d5cea
📒 Files selected for processing (2)
internal/entity/models/longcat.gointernal/entity/models/longcat_test.go
8dbc9dc to
c56b1f0
Compare
The CI preflight (lefthook gofmt check) failed because the struct literal in TestLongCatChatAcceptsReasoningOnlyResponse was mis-aligned. Also fix the nil-pointer-in-Errorf pattern that CodeRabbit flagged: 'if p == nil || *p != x' dereferences the pointer in the Errorf even when the nil branch is taken, panicking instead of reporting the assertion failure. Split into nil-check then value-check. Co-Authored-By: Claude Fable 5 <[email protected]>
67f0981 to
e67899b
Compare
### Summary Add token usage reporting for the LongCat (Meituan) model provider. Related to infiniflow#17284. Co-authored-by: Claude Fable 5 <[email protected]>
Summary
Add token usage reporting for the LongCat (Meituan) model provider. Related
to #17284.
Changes
LongCatChatResponsestruct mirroring the LongCatOpenAI-compatible schema
prompt_tokens,completion_tokens,total_tokens)from non-streaming chat completion responses
stream_options.include_usage: trueto streaming requests so LongCatreturns aggregate usage as the final SSE event
contentmay benulland all outputis returned in
reasoning_contentreasoning_content(matches LongCat APIbehavior)
Verification
api.longcat.chat— both streaming andnon-streaming usage parsed correctly
bash build.sh --test ./internal/entity/models/— all tests pass