Repository navigation
feat(go-api): support setting tenant default models by model_id - #16030
Conversation
📝 WalkthroughWalkthroughAdds a Changesmodel_id-based default model resolution
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. 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
🧹 Nitpick comments (1)
internal/service/tenant_test.go (1)
159-162: 💤 Low valueGlobal state mutation may cause test flakiness with parallel execution.
Swapping
dao.DBglobally affects any other test that runs concurrently in the same process. The defer restores it, but if tests run in parallel (which is the default forgo test), race conditions may occur.Consider adding
t.Setenvor using test-specific isolation patterns if parallel test execution is enabled for this package.🤖 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/service/tenant_test.go` around lines 159 - 162, The test in tenant_test.go is directly mutating the global dao.DB variable and restoring it via defer, which creates race conditions during parallel test execution. Refactor the code to avoid global state mutation by using dependency injection patterns that pass the test database through function parameters or constructor arguments instead of swapping the global variable. Alternatively, if the global mutation pattern must be retained, use proper synchronization mechanisms or configure the test to run serially rather than in parallel. The key is to isolate the database dependency at the test level rather than modifying shared global state that affects other concurrent tests.
🤖 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/service/tenant_test.go`:
- Around line 238-244: The `checkModelAvailable` function has a logic error in
its condition that checks both error and modelEntity status using OR operator,
which causes it to call err.Error() on a nil error when a model is found
successfully. Fix this by restructuring the condition to first check if err !=
nil to handle the error case, then use an else if to check if modelEntity != nil
for the success case where the model already exists. Once this root-cause fix is
applied to `checkModelAvailable`, update the test call to
`SetTenantDefaultModels` to use empty strings for modelProvider and
modelInstance parameters instead of passing "default" as a workaround, removing
the need for the acknowledge comment about bypassing the panic.
---
Nitpick comments:
In `@internal/service/tenant_test.go`:
- Around line 159-162: The test in tenant_test.go is directly mutating the
global dao.DB variable and restoring it via defer, which creates race conditions
during parallel test execution. Refactor the code to avoid global state mutation
by using dependency injection patterns that pass the test database through
function parameters or constructor arguments instead of swapping the global
variable. Alternatively, if the global mutation pattern must be retained, use
proper synchronization mechanisms or configure the test to run serially rather
than in parallel. The key is to isolate the database dependency at the test
level rather than modifying shared global state that affects other concurrent
tests.
🪄 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
Run ID: 288c9535-d76c-4e15-82b3-353e2f757b72
📒 Files selected for processing (3)
internal/handler/tenant.gointernal/service/tenant.gointernal/service/tenant_test.go
| // 4. Run SetTenantDefaultModels | ||
| s := NewTenantService() | ||
| // Set chat model using modelID, explicitly passing "default" as instance name to bypass pre-existing checkModelAvailable panic | ||
| err = s.SetTenantDefaultModels(userID, "", "default", "", "chat", modelID) | ||
| if err != nil { | ||
| t.Fatalf("SetTenantDefaultModels failed: %v", err) | ||
| } |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Verify the checkModelAvailable logic and confirm the potential nil-pointer dereference
rg -n -A 10 'func.*checkModelAvailable' internal/service/tenant.goRepository: infiniflow/ragflow
Length of output: 538
🏁 Script executed:
#!/bin/bash
# Get the complete checkModelAvailable function (from line 579 onwards)
rg -n -A 35 'func.*checkModelAvailable' internal/service/tenant.goRepository: infiniflow/ragflow
Length of output: 1370
🏁 Script executed:
#!/bin/bash
# Check the test code around lines 238-244
cat -n internal/service/tenant_test.go | sed -n '235,250p'Repository: infiniflow/ragflow
Length of output: 773
🏁 Script executed:
#!/bin/bash
# Verify the exact line numbers and code around what the review calls "lines 600-608"
sed -n '595,615p' internal/service/tenant.goRepository: infiniflow/ragflow
Length of output: 726
🏁 Script executed:
#!/bin/bash
# Check how SetTenantDefaultModels calls checkModelAvailable
sed -n '613,700p' internal/service/tenant.goRepository: infiniflow/ragflow
Length of output: 2440
🏁 Script executed:
#!/bin/bash
# Get more context about what parameters are passed when checkModelAvailable is called
rg -n 'checkModelAvailable' internal/service/tenant.go | head -20Repository: infiniflow/ragflow
Length of output: 293
Fix the logic error in checkModelAvailable that causes the test to require a workaround.
The bug is at lines 602-603. The condition if err != nil || modelEntity != nil executes when modelEntity is found (not nil) with no error, then immediately calls err.Error() on a nil error, causing a panic. The test comment itself acknowledges this: "to bypass pre-existing checkModelAvailable panic."
The condition should check for error before calling err.Error():
if err != nil {
// handle error case
} else if modelEntity != nil {
// model already exists
// return appropriate response
}Once fixed, the test at lines 238-244 can be updated to use the pure modelID-only path (passing empty strings for modelProvider and modelInstance) instead of explicitly passing "default" as a workaround.
🤖 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/service/tenant_test.go` around lines 238 - 244, The
`checkModelAvailable` function has a logic error in its condition that checks
both error and modelEntity status using OR operator, which causes it to call
err.Error() on a nil error when a model is found successfully. Fix this by
restructuring the condition to first check if err != nil to handle the error
case, then use an else if to check if modelEntity != nil for the success case
where the model already exists. Once this root-cause fix is applied to
`checkModelAvailable`, update the test call to `SetTenantDefaultModels` to use
empty strings for modelProvider and modelInstance parameters instead of passing
"default" as a workaround, removing the need for the acknowledge comment about
bypassing the panic.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #16030 +/- ##
=======================================
Coverage 93.16% 93.16%
=======================================
Files 10 10
Lines 717 717
Branches 118 118
=======================================
Hits 668 668
Misses 29 29
Partials 20 20 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…niflow#16030) ### Description Currently, when setting tenant default models (e.g., chat, embedding, rerank), the API only accepts the composite name (`model_name@model_instance@model_provider`). However, some integrations and front-end features prefer using the database `model_id` (UUID) directly. This PR adds support for `model_id` in default model configuration: 1. **Request Binding**: Added `model_id` (optional field) to the request body schema in the handler. 2. **Database Lookup**: If `model_id` is supplied, the service queries the database to resolve the respective provider, instance, and model names. 3. **Security Validation**: Verified that the provider associated with the resolved `model_id` belongs to the requesting tenant. 4. **Unit Tests**: Added `TestSetTenantDefaultModels_WithModelID` to verify DB ID resolution and tenant mapping.
Description
Currently, when setting tenant default models (e.g., chat, embedding, rerank), the API only accepts the composite name (
model_name@model_instance@model_provider). However, some integrations and front-end features prefer using the databasemodel_id(UUID) directly.This PR adds support for
model_idin default model configuration:model_id(optional field) to the request body schema in the handler.model_idis supplied, the service queries the database to resolve the respective provider, instance, and model names.model_idbelongs to the requesting tenant.TestSetTenantDefaultModels_WithModelIDto verify DB ID resolution and tenant mapping.