Repository navigation
fix: unable to use agent in agent - #16910
Conversation
📝 WalkthroughWalkthrough
ChangesTenant chat model resolution
Component input and debug observability
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant AgentComponent
participant TenantModel
participant TenantModelInstance
AgentComponent->>TenantModel: resolve tenant chat model by ID
TenantModel-->>AgentComponent: return configuration or not found
AgentComponent->>TenantModelInstance: resolve instance fallback
TenantModelInstance-->>AgentComponent: return active model and credentials
AgentComponent->>AgentComponent: apply model, driver, API key, and base URL
Possibly related PRs
Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 inconclusive)
✅ 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: 2
🧹 Nitpick comments (3)
internal/agent/component/data_operations.go (1)
298-312: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winSimplify redundant string concatenation.
The logic slicing
exparound@to computecpnIDandvarNameand then reconstructing them viacpnID + "@" + varNameevaluates to the exact originalexpstring. This can be simplified to improve readability.♻️ Proposed fix
- cpnID, varName := "", exp - if at := strings.Index(exp, "@"); at > 0 { - cpnID = exp[:at] - varName = exp[at+1:] - } - name := exp - if cpnID != "" { - name = cpnID + "@" + varName - } - res[exp] = map[string]any{ - "name": name, - "value": nil, - "_retrieval": nil, - "_cpn_id": cpnID, - } + cpnID := "" + if at := strings.Index(exp, "@"); at > 0 { + cpnID = exp[:at] + } + res[exp] = map[string]any{ + "name": exp, + "value": nil, + "_retrieval": nil, + "_cpn_id": cpnID, + }🤖 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/agent/component/data_operations.go` around lines 298 - 312, In the expression-processing block, simplify the conditional name assignment by using the original exp value directly instead of reconstructing it from cpnID and varName. Preserve the existing cpnID parsing and _cpn_id field behavior, and remove only the redundant name concatenation logic.internal/agent/component/llm_credentials.go (1)
208-213: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueAvoid shadowing the imported
modelspackage.The local variable
modelsshadows the imported package"ragflow/internal/entity/models". Consider renaming it (e.g., totenantModels) to prevent confusion and potential compilation errors if the package is referenced later in the function.♻️ Proposed refactor
- models, err := dao.NewTenantModelDAO().GetModelsByInstanceID(instance.ID) + tenantModels, err := dao.NewTenantModelDAO().GetModelsByInstanceID(instance.ID) if err != nil { return "", "", apiKey, baseURL, false, fmt.Errorf("resolve models for tenant model instance id %q: %w", instanceID, err) } - candidates := make([]*entity.TenantModel, 0, len(models)) - for _, model := range models { + candidates := make([]*entity.TenantModel, 0, len(tenantModels)) + for _, model := range tenantModels {🤖 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/agent/component/llm_credentials.go` around lines 208 - 213, Rename the local result variable in the model-resolution flow around GetModelsByInstanceID from models to tenantModels, and update its length and iteration references accordingly, preserving the existing behavior and imported models package access.internal/agent/component/agent.go (1)
461-478: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueMerge redundant conditional checks.
The condition
if p.Driver == "" && p.ModelID != ""is evaluated twice consecutively. AssumingagentProviderLastSegmentSplitpopulatesp.Driveron success, the second block can be merged into anelsebranch of the first condition's innerifto simplify the flow and avoid redundant checks.♻️ Proposed refactor
if p.Driver == "" && p.ModelID != "" { if m, prov, ok := agentProviderLastSegmentSplit(p.ModelID); ok { p.Driver = prov p.ModelID = m - } - } - if p.Driver == "" && p.ModelID != "" { - modelID, driver, apiKey, baseURL, ok, err := resolveTenantChatModelByID(ctx, p.ModelID, p.APIKey, p.BaseURL) - if err != nil { - return nil, err - } - if ok { - p.ModelID = modelID - p.Driver = driver - p.APIKey = apiKey - p.BaseURL = baseURL - } + } else { + modelID, driver, apiKey, baseURL, ok, err := resolveTenantChatModelByID(ctx, p.ModelID, p.APIKey, p.BaseURL) + if err != nil { + return nil, err + } + if ok { + p.ModelID = modelID + p.Driver = driver + p.APIKey = apiKey + p.BaseURL = baseURL + } + } }🤖 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/agent/component/agent.go` around lines 461 - 478, Merge the consecutive conditionals in the model/provider resolution flow: within the outer `p.Driver == "" && p.ModelID != ""` check, run `resolveTenantChatModelByID` in an else branch when `agentProviderLastSegmentSplit` does not succeed. Preserve the existing assignments and error return behavior.
🤖 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/agent/component/data_operations.go`:
- Around line 272-284: Update DataOperationsComponent.GetInputForm to initialize
an output map, add each matched variable reference and its corresponding
name/type entry while iterating through all query inputs, and return the
accumulated map only after both loops complete; preserve the empty-map result
when no inputs match.
- Around line 286-287: Remove the TODO comment and its accompanying explanation
about moving the logic later from the affected data-operations code, leaving the
implementation unchanged.
---
Nitpick comments:
In `@internal/agent/component/agent.go`:
- Around line 461-478: Merge the consecutive conditionals in the model/provider
resolution flow: within the outer `p.Driver == "" && p.ModelID != ""` check, run
`resolveTenantChatModelByID` in an else branch when
`agentProviderLastSegmentSplit` does not succeed. Preserve the existing
assignments and error return behavior.
In `@internal/agent/component/data_operations.go`:
- Around line 298-312: In the expression-processing block, simplify the
conditional name assignment by using the original exp value directly instead of
reconstructing it from cpnID and varName. Preserve the existing cpnID parsing
and _cpn_id field behavior, and remove only the redundant name concatenation
logic.
In `@internal/agent/component/llm_credentials.go`:
- Around line 208-213: Rename the local result variable in the model-resolution
flow around GetModelsByInstanceID from models to tenantModels, and update its
length and iteration references accordingly, preserving the existing behavior
and imported models package access.
🪄 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: ddef2013-2ef7-4c93-b367-28bb608baf28
📒 Files selected for processing (4)
internal/agent/component/agent.gointernal/agent/component/data_operations.gointernal/agent/component/llm_credentials.gointernal/handler/agent_component.go
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
internal/agent/component/data_operations.go (1)
272-283: 🎯 Functional Correctness | 🔴 Critical | ⚡ Quick winReturn the accumulated map instead of an empty map.
The function populates the
resmap with input elements but discards it by returning an empty map at the end. This completely nullifies the fix made to the loop and will cause the data operations component to expose an empty input form.🐛 Proposed fix
func (d *DataOperationsComponent) GetInputForm() map[string]any { res := make(map[string]any) for _, input := range d.param.Query { for k, v := range getInputElementsFromText(input) { res[k] = map[string]any{ "name": v["name"], "type": "line", } } } - return map[string]any{} + return res }🤖 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/agent/component/data_operations.go` around lines 272 - 283, Update DataOperationsComponent.GetInputForm to return the populated res map instead of creating and returning a new empty map, preserving the accumulated input elements from the loop.
🤖 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.
Outside diff comments:
In `@internal/agent/component/data_operations.go`:
- Around line 272-283: Update DataOperationsComponent.GetInputForm to return the
populated res map instead of creating and returning a new empty map, preserving
the accumulated input elements from the loop.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: fd754ab4-0068-45ad-9d28-b269fd66455b
📒 Files selected for processing (1)
internal/agent/component/data_operations.go
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
internal/agent/component/llm_credentials.go (1)
272-275: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick winPrevent silent JSON unmarshaling failures for mixed-type objects.
If
instance.Extracontains non-string values (e.g., numbers, booleans, or nested configurations like{"timeout": 30, "base_url": "..."}), unmarshaling it directly into amap[string]stringwill fail. Because the error is ignored, this will silently leavebaseURLempty even if the"base_url"key is present.Use
map[string]anyto safely parse arbitrary JSON and perform a type assertion forbase_url.🛠️ Proposed fix
- var extra map[string]string - if err := json.Unmarshal([]byte(instance.Extra), &extra); err == nil { - baseURL = extra["base_url"] - } + var extra map[string]any + if err := json.Unmarshal([]byte(instance.Extra), &extra); err == nil { + if val, ok := extra["base_url"].(string); ok { + baseURL = val + } + }🤖 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/agent/component/llm_credentials.go` around lines 272 - 275, Update the Extra parsing in the credential-loading flow to unmarshal into map[string]any instead of map[string]string, allowing mixed-type JSON fields. Then retrieve extra["base_url"] with a string type assertion and assign baseURL only when the assertion succeeds, preserving the existing behavior for valid string values.
🤖 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.
Outside diff comments:
In `@internal/agent/component/llm_credentials.go`:
- Around line 272-275: Update the Extra parsing in the credential-loading flow
to unmarshal into map[string]any instead of map[string]string, allowing
mixed-type JSON fields. Then retrieve extra["base_url"] with a string type
assertion and assign baseURL only when the assertion succeeds, preserving the
existing behavior for valid string values.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 538169d8-d6b6-4592-87b7-27393bc34c9e
📒 Files selected for processing (1)
internal/agent/component/llm_credentials.go
|
LGTM |
Summary
As title
Agent:
Data_operations