Skip to content

fix(go): resolve custom tenant model in LLM agent canvas component - #17187

Merged
wangq8 merged 2 commits into
infiniflow:mainfrom
euvre:fix/go-llm-custom-model-agent
Jul 23, 2026
Merged

wangq8 merged 2 commits into
infiniflow:mainfrom
euvre:fix/go-llm-custom-model-agent

Conversation

@euvre

@euvre euvre commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Summary

The LLM component (internal/agent/component/llm.go) did not resolve tenant-scoped model references (tenant_model.id or model@instance@provider) before dispatching to the model driver. The Agent and Categorize components already called resolveChatModelRef to map canvas model IDs to real provider names, driver config, and credentials — but the LLM component passed the raw model_id verbatim. This caused custom-added models to fail with HTTP 400 when selected in the agent canvas.

This PR adds resolveChatModelRef at the start of LLMComponent.Invoke, mirroring the existing resolution logic in AgentComponent.Invoke and CategorizeComponent.Invoke.

Additionally, TestLLM_ResolvesTenantModelID is added to guard tenant model ID resolution through the full provider/instance/model chain.

The LLM component did not resolve tenant-scoped model references
(tenant_model.id or model@instance@provider) before dispatching to
the model driver. This caused custom-added models to fail with HTTP 400
when selected in the agent canvas because the raw model_id was passed
verbatim to the provider API.

Fix by calling resolveChatModelRef at the start of
LLMComponent.Invoke, matching the existing behavior of Agent and
Categorize components.

Added TestLLM_ResolvesTenantModelID to guard tenant model ID
resolution through the full provider/instance/model chain.
@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Jul 21, 2026
@euvre euvre added the ci Continue Integration label Jul 21, 2026
@dosubot dosubot Bot added 🐞 bug Something isn't working, pull request that fix bug. 🧪 test Pull requests that update test cases. labels Jul 21, 2026
@coderabbitai

coderabbitai Bot commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

LLMComponent.Invoke now resolves tenant-scoped chat model references before validation and invocation. A new test creates tenant model records and verifies the resolved driver, model name, API key, and base URL reach the chat invoker.

Changes

Tenant model resolution

Layer / File(s) Summary
Resolve tenant model before chat invocation
internal/agent/component/llm.go, internal/agent/component/llm_test.go
LLMComponent.Invoke resolves tenant model references before validation, while the new test verifies the resolved model and connection fields passed to the stubbed invoker.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

Suggested labels: go

Suggested reviewers: qinling0210, yuzhichang

Poem

I’m a rabbit with models tucked neat,
Resolving each tenant’s credentials sweet.
Driver and endpoint, key in a row,
Into the chat request they go.
Hop, test, invoke—
The fields all prove!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the main change: resolving custom tenant model references in the LLM component.
Description check ✅ Passed The description includes the required Summary section and explains the bug, context, and test coverage.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.

@euvre
euvre requested a review from wangq8 July 21, 2026 10:25

@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.

🧹 Nitpick comments (1)
internal/agent/component/llm.go (1)

315-327: 🚀 Performance & Scalability | 🔵 Trivial

New DB round-trip on every Invoke call — consider caching resolved tenant model config.

resolveChatModelRef now runs unconditionally before validation, and per the upstream contract it performs DAO lookups (TenantModelDAO.GetByID, TenantModelProviderDAO.GetByID, TenantModelInstanceDAO.GetByID) plus resolveTenantLLMConfig. Since LLMComponent.Invoke is on the hot path for every chat turn, this adds at least 2-3 synchronous DB round trips per invocation with no apparent caching layer, which will show up as added latency and DB load at higher LLM traffic volumes.

Worth considering a short-lived cache (e.g. keyed by tenant+model id) for the provider/instance/model lookups, since these records change infrequently relative to invocation rate.

🤖 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.go` around lines 315 - 327, Cache the resolved
tenant model configuration used by LLMComponent.Invoke, keyed by tenant and
model reference, with a short expiration suitable for infrequently changing
provider, instance, and model records. Reuse cached results before calling
resolveChatModelRef, while preserving resolution errors and ensuring cache
entries are isolated between tenants.
🤖 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.

Nitpick comments:
In `@internal/agent/component/llm.go`:
- Around line 315-327: Cache the resolved tenant model configuration used by
LLMComponent.Invoke, keyed by tenant and model reference, with a short
expiration suitable for infrequently changing provider, instance, and model
records. Reuse cached results before calling resolveChatModelRef, while
preserving resolution errors and ensuring cache entries are isolated between
tenants.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 4471e640-a4db-4d70-a222-ea450bb5cc61

📥 Commits

Reviewing files that changed from the base of the PR and between edeb47c and 805500a.

📒 Files selected for processing (2)
  • internal/agent/component/llm.go
  • internal/agent/component/llm_test.go

@wangq8 wangq8 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM. Clean fix that mirrors the existing resolveChatModelRef pattern from AgentComponent and CategorizeComponent. The test covers the full provider/instance/model resolution chain including Driver, ModelName, APIKey, and BaseURL assertions. No regressions — the resolve call is placed before the existing empty-model check, which is the correct ordering.

@wangq8
wangq8 requested a review from Lynn-Inf July 22, 2026 11:36

@Lynn-Inf Lynn-Inf 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.

LGTM

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

Labels

🐞 bug Something isn't working, pull request that fix bug. ci Continue Integration size:S This PR changes 10-29 lines, ignoring generated files. 🧪 test Pull requests that update test cases.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants