Skip to content

Refactor(go-models)/llm token usage for longcat - #17480

Merged
JinHai-CN merged 2 commits into
infiniflow:mainfrom
jay77721:refactor/llm-response-longcat
Jul 28, 2026
Merged

JinHai-CN merged 2 commits into
infiniflow:mainfrom
jay77721:refactor/llm-response-longcat

Conversation

@jay77721

Copy link
Copy Markdown
Member

Summary

Add token usage reporting for the LongCat (Meituan) model provider. Related
to #17284.

Changes

  • Add typed LongCatChatResponse struct mirroring the LongCat
    OpenAI-compatible schema
  • Parse token usage (prompt_tokens, completion_tokens, total_tokens)
    from non-streaming chat completion responses
  • Add stream_options.include_usage: true to streaming requests so LongCat
    returns aggregate usage as the final SSE event
  • 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 LongCat API
    behavior)

Verification

  • Real API tested against api.longcat.chat — both streaming and
    non-streaming usage parsed correctly
  • bash build.sh --test ./internal/entity/models/ — all tests pass

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>>
@dosubot dosubot Bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Jul 28, 2026
@coderabbitai

coderabbitai Bot commented Jul 28, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

LongCat chat responses now use typed OpenAI-compatible parsing, preserve reasoning-only responses, map token usage, and request and apply usage data during streaming.

Changes

LongCat response and usage handling

Layer / File(s) Summary
Typed chat response parsing and validation
internal/entity/models/longcat.go, internal/entity/models/longcat_test.go
Adds LongCatChatResponse, delegates parsing to shared helpers, supports reasoning-only responses, and maps response usage into TokenUsage.
Streaming usage propagation
internal/entity/models/longcat.go, internal/entity/models/longcat_test.go
Requests streaming usage, resets prior usage and tool-call results, decodes SSE usage events, and stores the resulting usage. Tests verify request and result 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
Loading

Possibly related PRs

Poem

I’m a bunny with tokens, hopping in streams,
Parsing thoughts, tool calls, and dreams.
Usage now follows each message through,
Typed fields make the counts shine bright. 🐇

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title matches the main change: adding token usage support for the LongCat model provider.
Description check ✅ Passed The description includes the required Summary section and enough change and verification detail.

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Haruko386
Haruko386 marked this pull request as draft July 28, 2026 08:24
@Haruko386
Haruko386 marked this pull request as ready for review July 28, 2026 08:24
@Haruko386 Haruko386 added the ci Continue Integration label Jul 28, 2026

@coderabbitai coderabbitai Bot left a comment

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.

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

📥 Commits

Reviewing files that changed from the base of the PR and between b8bb229 and c56b1f0.

📒 Files selected for processing (2)
  • internal/entity/models/longcat.go
  • internal/entity/models/longcat_test.go

Comment thread internal/entity/models/longcat_test.go Resolved
@jay77721
jay77721 force-pushed the refactor/llm-response-longcat branch from 8dbc9dc to c56b1f0 Compare July 28, 2026 08:39

@Haruko386 Haruko386 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Jul 28, 2026
@Haruko386 Haruko386 removed the ci Continue Integration label Jul 28, 2026
@Haruko386
Haruko386 marked this pull request as draft July 28, 2026 08:41
@Haruko386
Haruko386 marked this pull request as ready for review July 28, 2026 08:41
@Haruko386 Haruko386 added the ci Continue Integration label Jul 28, 2026
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]>
@jay77721
jay77721 force-pushed the refactor/llm-response-longcat branch from 67f0981 to e67899b Compare July 28, 2026 11:37
@JinHai-CN
JinHai-CN merged commit b02df50 into infiniflow:main Jul 28, 2026
4 of 6 checks passed
buua436 pushed a commit to buua436/ragflow that referenced this pull request Jul 29, 2026
### Summary

Add token usage reporting for the LongCat (Meituan) model provider.
Related
  to infiniflow#17284.

Co-authored-by: Claude Fable 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci Continue Integration lgtm This PR has been approved by a maintainer size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants