Skip to content

refactor: the request body build - #17279

Closed
Haruko386 wants to merge 1 commit into
infiniflow:mainfrom
Haruko386:major
Closed

Haruko386 wants to merge 1 commit into
infiniflow:mainfrom
Haruko386:major

Conversation

@Haruko386

Copy link
Copy Markdown
Member

Summary

As title

@Haruko386 Haruko386 added the ☯️ refactor Pull request that refactor/refine code label Jul 23, 2026
@dosubot dosubot Bot added the size:XXL This PR changes 1000+ lines, ignoring generated files. label Jul 23, 2026
@Haruko386
Haruko386 marked this pull request as draft July 23, 2026 05:22
@Haruko386
Haruko386 marked this pull request as ready for review July 23, 2026 05:22
@Haruko386 Haruko386 added the ci Continue Integration label Jul 23, 2026
@coderabbitai

coderabbitai Bot commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

This PR centralizes chat request-body construction in buildRequestBody, migrates numerous provider chat methods to use it for streaming and non-streaming requests, and preserves provider-specific reasoning, thinking, tool, URL, and response-handling logic where required.

Changes

Chat request construction

Layer / File(s) Summary
Shared request-body builder
internal/entity/models/base_model.go
Adds common construction for model, messages, stream mode, temperature, sampling parameters, stop sequences, and tool settings.
Provider chat method migrations
internal/entity/models/*.go
Replaces duplicated request-map and message conversion logic with buildRequestBody calls across streaming and non-streaming chat paths.
Provider-specific adapters
internal/entity/models/cometapi.go, deepseek.go, futurmix.go, groq.go, perplexity.go, siliconflow.go, togetherai.go, xinference.go
Retains or extracts provider-specific reasoning, thinking, request creation, URL, payload, and type-handling behavior around the shared body.

Estimated code review effort: 4 (Complex) | ~60 minutes

Possibly related PRs

Suggested reviewers: chuenchen309

Poem

I’m a rabbit with payloads tucked neat,
Building chat bodies swift and complete.
Streams hop onward, tools find their way,
Thinking fields dance in the array.
One helper now guides every leap!

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 18.18% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ❓ Inconclusive The description only says "As title" and does not provide the required summary or background. Expand the Summary with the PR goal, context, and a brief explanation of what the request-body refactor changes.
✅ Passed checks (3 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 is concise and accurately summarizes the main change: refactoring request body construction.

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 left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Waiting for test

@Haruko386
Haruko386 marked this pull request as draft July 23, 2026 05:27
@Haruko386
Haruko386 marked this pull request as ready for review July 23, 2026 05:27

@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: 6

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
internal/entity/models/tokenhub.go (1)

153-170: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Reject non-streaming config before streaming requests.

buildRequestBody(..., true) always sets "stream": true, so these streaming entrypoints now ignore a caller-supplied chatModelConfig.Stream == false and proceed with SSE. Add a guard before building the request, or centralize this validation, for consistency across ChatStreamlyWithSender.

  • internal/entity/models/tokenhub.go#L153
  • internal/entity/models/tokenpony.go#L137
  • internal/entity/models/stepfun.go#L137
🤖 Prompt for 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.

In `@internal/entity/models/tokenhub.go` around lines 153 - 170, Reject
configurations with Stream set to false before building or sending streaming
requests in ChatStreamlyWithSender. Apply the same validation consistently at
internal/entity/models/tokenhub.go:153-170,
internal/entity/models/tokenpony.go:137-157, and
internal/entity/models/stepfun.go:137-156, preserving the existing request flow
for valid streaming configurations.
internal/entity/models/ollama.go (1)

180-190: 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Guard modelConfig before dereferencing it in ChatStreamlyWithSender.

The streaming path builds a request with a nil-safe modelConfig, but then accesses modelConfig.Effort / modelConfig.Thinking without a nil check, while ChatWithMessages guards that block. Mirror the non-stream guard or return early when modelConfig == nil before applying think.

🤖 Prompt for 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.

In `@internal/entity/models/ollama.go` around lines 180 - 190, Guard the
think-configuration logic in ChatStreamlyWithSender with a nil check for
modelConfig before accessing Effort or Thinking. Mirror the existing modelConfig
guard used by ChatWithMessages, preserving the current Effort and Thinking
behavior when configuration is present.
🧹 Nitpick comments (1)
internal/entity/models/astraflow.go (1)

88-89: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Remove the empty if chatModelConfig != nil {} block.

This is a leftover no-op after the refactor and dead code.

♻️ Proposed cleanup
 	reqBody := buildRequestBody(chatModelConfig, modelName, messages, false)
-
-	if chatModelConfig != nil {
-	}

 	jsonData, err := json.Marshal(reqBody)
As per coding guidelines: "Remove dead tests, commented-out code, stale documentation..."
🤖 Prompt for 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.

In `@internal/entity/models/astraflow.go` around lines 88 - 89, Remove the empty
no-op `if chatModelConfig != nil {}` block from the surrounding model
configuration logic, leaving the remaining `chatModelConfig` handling unchanged.

Source: Coding guidelines

🤖 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/base_model.go`:
- Around line 253-260: Update the tool injection condition in buildRequestBody
to use len(cfg.Tools) > 0 instead of only checking cfg.Tools != nil, so empty
tool collections add neither "tools" nor "tool_choice"; preserve the existing
tool-choice selection for non-empty tools.

In `@internal/entity/models/cohere.go`:
- Around line 71-76: Remove the generic top_p field from the request body before
applying the Cohere-specific p value in both ChatWithMessages and
ChatStreamlyWithSender. Update the request-building logic at
internal/entity/models/cohere.go lines 71-76 and 178-183 so only p is sent to
the native Cohere API.

In `@internal/entity/models/deepseek.go`:
- Around line 78-108: The ChatWithMessages effort handling switch must reject
unrecognized effort values instead of sending an empty thinking type. Add a Go
fallback branch to the switch around ChatWithMessages that returns
fmt.Errorf("invalid effort level"), matching ChatStreamlyWithSender, while
preserving all existing recognized effort mappings.

In `@internal/entity/models/futurmix.go`:
- Around line 186-207: Update the FuturMix streaming method around
buildRequestBody and the request creation: pass true for the stream flag, use
streamCallTimeout for the context, and set the required Content-Type and
Authorization headers on req before calling f.baseModel.httpClient.Do.
- Around line 116-140: Update the request construction in the FuturMix chat
method around http.NewRequestWithContext to restore the headers previously
applied by newFuturMixJSONRequest: set Content-Type to application/json and
Authorization to Bearer plus the configured API key before calling
f.baseModel.httpClient.Do(req).

In `@internal/entity/models/huggingface.go`:
- Around line 174-176: Guard the streaming thinking checks against nil model
configuration before reading Thinking. In internal/entity/models/huggingface.go
lines 174-176 and internal/entity/models/lmstudio.go lines 193-194, update the
conditions around modelConfig.Thinking to require modelConfig != nil while
preserving the existing thinking behavior when configuration is present.

---

Outside diff comments:
In `@internal/entity/models/ollama.go`:
- Around line 180-190: Guard the think-configuration logic in
ChatStreamlyWithSender with a nil check for modelConfig before accessing Effort
or Thinking. Mirror the existing modelConfig guard used by ChatWithMessages,
preserving the current Effort and Thinking behavior when configuration is
present.

In `@internal/entity/models/tokenhub.go`:
- Around line 153-170: Reject configurations with Stream set to false before
building or sending streaming requests in ChatStreamlyWithSender. Apply the same
validation consistently at internal/entity/models/tokenhub.go:153-170,
internal/entity/models/tokenpony.go:137-157, and
internal/entity/models/stepfun.go:137-156, preserving the existing request flow
for valid streaming configurations.

---

Nitpick comments:
In `@internal/entity/models/astraflow.go`:
- Around line 88-89: Remove the empty no-op `if chatModelConfig != nil {}` block
from the surrounding model configuration logic, leaving the remaining
`chatModelConfig` handling unchanged.
🪄 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: 9fe367e2-035d-4197-91bc-abb5d8dd6cc3

📥 Commits

Reviewing files that changed from the base of the PR and between 8af2467 and 18b3622.

📒 Files selected for processing (51)
  • internal/entity/models/302ai.go
  • internal/entity/models/aliyun.go
  • internal/entity/models/astraflow.go
  • internal/entity/models/avian.go
  • internal/entity/models/baichuan.go
  • internal/entity/models/baidu.go
  • internal/entity/models/base_model.go
  • internal/entity/models/cohere.go
  • internal/entity/models/cometapi.go
  • internal/entity/models/deepinfra.go
  • internal/entity/models/deepseek.go
  • internal/entity/models/futurmix.go
  • internal/entity/models/gitee.go
  • internal/entity/models/gpustack.go
  • internal/entity/models/groq.go
  • internal/entity/models/huaweicloud.go
  • internal/entity/models/huggingface.go
  • internal/entity/models/hunyuan.go
  • internal/entity/models/jiekouai.go
  • internal/entity/models/jina.go
  • internal/entity/models/lmstudio.go
  • internal/entity/models/localai.go
  • internal/entity/models/longcat.go
  • internal/entity/models/minimax.go
  • internal/entity/models/mistral.go
  • internal/entity/models/modelscope.go
  • internal/entity/models/moonshot.go
  • internal/entity/models/n1n.go
  • internal/entity/models/novita.go
  • internal/entity/models/nvidia.go
  • internal/entity/models/ollama.go
  • internal/entity/models/openai.go
  • internal/entity/models/openrouter.go
  • internal/entity/models/orcarouter.go
  • internal/entity/models/perplexity.go
  • internal/entity/models/ppio.go
  • internal/entity/models/qiniu.go
  • internal/entity/models/ragcon.go
  • internal/entity/models/siliconflow.go
  • internal/entity/models/stepfun.go
  • internal/entity/models/togetherai.go
  • internal/entity/models/tokenhub.go
  • internal/entity/models/tokenpony.go
  • internal/entity/models/upstage.go
  • internal/entity/models/vllm.go
  • internal/entity/models/volcengine.go
  • internal/entity/models/xai.go
  • internal/entity/models/xiaomi.go
  • internal/entity/models/xinference.go
  • internal/entity/models/xunfei.go
  • internal/entity/models/zhipu-ai.go

Comment thread internal/entity/models/base_model.go Resolved
Comment thread internal/entity/models/cohere.go Resolved
Comment thread internal/entity/models/deepseek.go Resolved
Comment thread internal/entity/models/futurmix.go Resolved
Comment thread internal/entity/models/futurmix.go Resolved
Comment thread internal/entity/models/huggingface.go Resolved
@Haruko386
Haruko386 marked this pull request as draft July 23, 2026 07:17
@Haruko386
Haruko386 marked this pull request as ready for review July 23, 2026 07:17

@Haruko386 Haruko386 left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

done

@Haruko386 Haruko386 closed this Jul 23, 2026
@Haruko386

Copy link
Copy Markdown
Member Author

github bug, can not update this PR, so, close and open a new PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci Continue Integration ☯️ refactor Pull request that refactor/refine code size:XXL This PR changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant