Skip to content

feat(go-models): migrate batch 1 model drivers to unified handlers - #17696

Merged
JinHai-CN merged 5 commits into
infiniflow:mainfrom
jay77721:pr/migrate-models-batch1
Aug 3, 2026
Merged

JinHai-CN merged 5 commits into
infiniflow:mainfrom
jay77721:pr/migrate-models-batch1

Conversation

@jay77721

@jay77721 jay77721 commented Aug 3, 2026 •

Copy link
Copy Markdown
Member

Summary

Relate to #17284

Migrate 10 OpenAI-compatible drivers (302ai, aliyun, astraflow, avian, azure_openai, baichuan, baidu, cometapi, deepinfra, futurmix) to use the unified response handlers (HandleNonStreamingResponse / HandleStreamingResponse), following the same pattern established by deepseek in #17634.

  • Cut ~150 lines per driver (1507 lines removed, 144 added across 10 files).
  • No functional changes — pure deduplication of HTTP plumbing.
  • Each driver now routes through baseModel.doRequest() and HandleNonStreamingResponse().

Migrate 10 model drivers to use shared doRequest/HandleNonStreamingResponse/HandleStreamingResponse helpers:
- 302ai (Model302ai)
- aliyun (AliyunModel)
- astraflow (AstraFlowModel)
- avian (AvianModel)
- azure_openai (AzureOpenAIModel)
- baichuan (BaichuanModel)
- baidu (BaiduModel)
- cometapi (CometAPIModel)
- deepinfra (DeepInfraModel)
- futurmix (FuturmixModel)

Each driver had ~150 lines of duplicated HTTP request building and response parsing logic replaced with calls to the shared base model helpers.
@dosubot dosubot Bot added the size:XXL This PR changes 1000+ lines, ignoring generated files. label Aug 3, 2026
@coderabbitai

coderabbitai Bot commented Aug 3, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 4bc1440a-ba8e-45e7-b88a-d80132ecc82d

📥 Commits

Reviewing files that changed from the base of the PR and between 8353fc7 and 99f1a75.

📒 Files selected for processing (13)
  • internal/entity/models/302ai.go
  • internal/entity/models/aliyun.go
  • internal/entity/models/astraflow.go
  • internal/entity/models/avian.go
  • internal/entity/models/azure_openai.go
  • internal/entity/models/baichuan.go
  • internal/entity/models/baidu.go
  • internal/entity/models/base_model.go
  • internal/entity/models/cometapi.go
  • internal/entity/models/cometapi_test.go
  • internal/entity/models/deepinfra.go
  • internal/entity/models/futurmix.go
  • internal/entity/models/response_handler.go
🚧 Files skipped from review as they are similar to previous changes (13)
  • internal/entity/models/cometapi_test.go
  • internal/entity/models/astraflow.go
  • internal/entity/models/azure_openai.go
  • internal/entity/models/302ai.go
  • internal/entity/models/deepinfra.go
  • internal/entity/models/response_handler.go
  • internal/entity/models/base_model.go
  • internal/entity/models/baichuan.go
  • internal/entity/models/futurmix.go
  • internal/entity/models/aliyun.go
  • internal/entity/models/avian.go
  • internal/entity/models/baidu.go
  • internal/entity/models/cometapi.go

📝 Walkthrough

Walkthrough

Ten provider drivers now use shared BaseModel request and response handlers. Streaming requests include usage data. Provider-specific response schemas and SSE processing were removed. CometAPI auxiliary requests use shared or direct response handling.

Changes

Provider handler migration

Layer / File(s) Summary
Non-streaming chat migration
internal/entity/models/{302ai,aliyun,astraflow,avian,azure_openai,baichuan,baidu,cometapi,deepinfra,futurmix}.go
Chat methods now use shared request execution and response parsing. Obsolete provider-specific response types and parsing code were removed.
Streaming response migration
internal/entity/models/{302ai,aliyun,astraflow,avian,azure_openai,baichuan,baidu,cometapi,deepinfra,futurmix}.go, internal/entity/models/base_model.go
Streaming methods now use shared transport and SSE handlers. Requests include usage options, thinking configuration is applied where supported, and streaming requests advertise text/event-stream.
CometAPI auxiliary request migration
internal/entity/models/cometapi.go, internal/entity/models/cometapi_test.go
Embedding, model-listing, and balance requests now use shared or direct response handling. The embedding error test expects the generic status message.
Shared reasoning extraction
internal/entity/models/response_handler.go
Reasoning extraction trims one leading newline and falls back to reasoning when reasoning_content is empty or absent.

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

Possibly related PRs

Suggested reviewers: haruko386

Poem

A rabbit sees shared handlers flow,
SSE streams now clearly show.
Usage rides each request trail,
Old response types leave the rail.
“Hop onward,” says the review hare.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.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
Title check ✅ Passed The title clearly and concisely describes migrating the first batch of Go model drivers to unified handlers.
Description check ✅ Passed The description includes the required Summary section and clearly states the migration scope, purpose, impact, and intended functional behavior.
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.

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.

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

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/avian.go (1)

55-61: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Restore the trailing-slash trim in chatURL.

GetBaseURL returns a user-configured base URL. If that value ends with /, fmt.Sprintf("%s/%s", ...) produces a double slash in the path. The other drivers in this batch still call strings.TrimSuffix(baseURL, "/"). Removing the trim here changes the produced URL for existing configurations.

🐛 Proposed fix
 func (a *AvianModel) chatURL(apiConfig *APIConfig) (string, error) {
 	baseURL, err := a.baseModel.GetBaseURL(apiConfig)
 	if err != nil {
 		return "", err
 	}
-	return fmt.Sprintf("%s/%s", baseURL, a.baseModel.URLSuffix.Chat), nil
+	return fmt.Sprintf("%s/%s", strings.TrimSuffix(baseURL, "/"), a.baseModel.URLSuffix.Chat), nil
 }
🤖 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/avian.go` around lines 55 - 61, Update
AvianModel.chatURL to trim a trailing slash from the baseURL returned by
GetBaseURL before joining it with URLSuffix.Chat, preserving the existing URL
format and error propagation.
internal/entity/models/azure_openai.go (1)

181-184: 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Add stream_options.include_usage to the Azure streaming request.

HandleStreamingResponse extracts modelUsage from stream usage chunks, and Azure OpenAI returns those chunks when using API version 2024-10-21 with stream_options.include_usage=true. Add the option to the streaming reqBody so Azure deployments record token usage.

🤖 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/azure_openai.go` around lines 181 - 184, Update the
streaming request body in the Azure OpenAI request flow to include
stream_options with include_usage enabled, alongside messages and stream. Ensure
HandleStreamingResponse receives Azure usage chunks for API version 2024-10-21
so modelUsage is recorded.
🧹 Nitpick comments (1)
internal/entity/models/futurmix.go (1)

51-58: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Remove endpointURL from FuturMixModel.

This receiver-scoped helper is not called; ChatWithMessages and ChatStreamlyWithSender already build the URL inline, so delete the stale helper to keep the model surface small.

🤖 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/futurmix.go` around lines 51 - 58, Remove the unused
FuturMixModel.endpointURL method, including its URL-building logic, while
leaving ChatWithMessages and ChatStreamlyWithSender unchanged since they already
construct their URLs inline.

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/aliyun.go`:
- Line 168: The streaming implementations in
internal/entity/models/aliyun.go:168-168 and
internal/entity/models/baidu.go:210-210 still duplicate transport logic. Replace
each manual transport block with the corresponding baseModel.doStreamRequest
call, preserving the existing context, URL, API config, request body, timeout,
and streaming callback arguments; remove bytes, encoding/json, and net/http
imports from each file when unused.

In `@internal/entity/models/avian.go`:
- Around line 79-90: Remove the entire chatModelConfig.Thinking conditional from
the request-building flow, including both enabled and disabled assignments to
reqBody["thinking"]. Keep buildRequestBody and all other request fields
unchanged.

---

Outside diff comments:
In `@internal/entity/models/avian.go`:
- Around line 55-61: Update AvianModel.chatURL to trim a trailing slash from the
baseURL returned by GetBaseURL before joining it with URLSuffix.Chat, preserving
the existing URL format and error propagation.

In `@internal/entity/models/azure_openai.go`:
- Around line 181-184: Update the streaming request body in the Azure OpenAI
request flow to include stream_options with include_usage enabled, alongside
messages and stream. Ensure HandleStreamingResponse receives Azure usage chunks
for API version 2024-10-21 so modelUsage is recorded.

---

Nitpick comments:
In `@internal/entity/models/futurmix.go`:
- Around line 51-58: Remove the unused FuturMixModel.endpointURL method,
including its URL-building logic, while leaving ChatWithMessages and
ChatStreamlyWithSender unchanged since they already construct their URLs inline.
🪄 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: 4c200b62-7071-44cd-9795-e6f532aac5eb

📥 Commits

Reviewing files that changed from the base of the PR and between 288be80 and c07a377.

📒 Files selected for processing (10)
  • internal/entity/models/302ai.go
  • internal/entity/models/aliyun.go
  • internal/entity/models/astraflow.go
  • internal/entity/models/avian.go
  • internal/entity/models/azure_openai.go
  • internal/entity/models/baichuan.go
  • internal/entity/models/baidu.go
  • internal/entity/models/cometapi.go
  • internal/entity/models/deepinfra.go
  • internal/entity/models/futurmix.go

Comment thread internal/entity/models/aliyun.go Resolved
Comment thread internal/entity/models/avian.go Resolved
@jay77721 jay77721 added the ci Continue Integration label Aug 3, 2026
…e avian dead Thinking block

- aliyun.go: Replace manual SSE transport block with baseModel.doStreamRequest call.
  Preserves context, URL, API config, request body, timeout, and callback arguments.
  Imports (bytes, encoding/json, net/http) retained for other methods.

- baidu.go: Same doStreamRequest migration as aliyun.
  Imports retained for other methods.

- avian.go: Remove dead Thinking conditional block from ChatWithMessages.
  buildRequestBody does not handle thinking; the reqBody['thinking'] assignments
  were never consumed by doRequest. Net -10 lines.

Part of infiniflow#17696 (batch 1).
@jay77721
jay77721 marked this pull request as draft August 3, 2026 02:39
@jay77721
jay77721 marked this pull request as ready for review August 3, 2026 02:39
@dosubot dosubot Bot added the ☯️ refactor Pull request that refactor/refine code label Aug 3, 2026
@jay77721 jay77721 added ci Continue Integration and removed ci Continue Integration labels Aug 3, 2026
@jay77721
jay77721 force-pushed the pr/migrate-models-batch1 branch from 986baf0 to 9dd80d0 Compare August 3, 2026 02:51
@jay77721
jay77721 marked this pull request as draft August 3, 2026 03:17
@jay77721
jay77721 marked this pull request as ready for review August 3, 2026 03:17
…back, embed error

- base_model.go: doStreamRequest now sets Accept: text/event-stream so
  providers that routed streaming through the shared handler (302ai, etc.)
  keep the header their old per-provider code set. Fixes
  TestAI302StreamForcesStreaming.
- response_handler.go: extractContentAndChoices falls back to a top-level
  "reasoning" string field when "reasoning_content" is absent/empty, matching
  Avian's response shape. Fixes TestAvianChatFallsBackToReasoningField.
- cometapi_test.go: assert the unified "status 401" error from doRequest
  instead of the removed provider-specific "CometAPI embeddings API error"
  prefix. Fixes TestCometAPIEmbedRejectsHTTPError.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@jay77721
jay77721 marked this pull request as draft August 3, 2026 03:42
@jay77721
jay77721 marked this pull request as ready for review August 3, 2026 03:42
@jay77721
jay77721 marked this pull request as draft August 3, 2026 03:42
@jay77721
jay77721 marked this pull request as ready for review August 3, 2026 03:42
@jay77721
jay77721 marked this pull request as draft August 3, 2026 03:51
@jay77721
jay77721 marked this pull request as ready for review August 3, 2026 03:51
JinHai-CN pushed a commit that referenced this pull request Aug 4, 2026
…ding (#17775)

## Summary

Relate to #17284.

Completes the migration of the four non-OpenAI-compatible model drivers
(`anthropic`, `cohere`, `google`, `bedrock`) onto the shared
usage-recording path. Earlier batches (#17634, #17643, #17696–#17700)
covered only the OpenAI-compatible cluster; these four providers ship
wire formats that do not fit the OpenAI `choices[0].delta` / `usage`
block template and so were left for a separate pass.

Per the maintainer's guidance for this batch, each driver is migrated on
its own terms rather than forced through a single template. The shared
machinery used is intentionally small: `recordResponseUsage`,
`parseChatCompletionResponse`, `BaseModel.newJSONPostRequest`, and the
existing `authHeader` hook for non-Bearer auth.

Co-authored-by: Haruko386 <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
@jay77721
jay77721 deleted the pr/migrate-models-batch1 branch August 5, 2026 07:17
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.

2 participants