Repository navigation
fix(go): resolve custom tenant model in LLM agent canvas component - #17187
Conversation
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.
📝 WalkthroughWalkthrough
ChangesTenant model resolution
Estimated code review effort: 3 (Moderate) | ~20 minutes Possibly related PRs
Suggested labels: Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 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.
🧹 Nitpick comments (1)
internal/agent/component/llm.go (1)
315-327: 🚀 Performance & Scalability | 🔵 TrivialNew DB round-trip on every
Invokecall — consider caching resolved tenant model config.
resolveChatModelRefnow runs unconditionally before validation, and per the upstream contract it performs DAO lookups (TenantModelDAO.GetByID,TenantModelProviderDAO.GetByID,TenantModelInstanceDAO.GetByID) plusresolveTenantLLMConfig. SinceLLMComponent.Invokeis 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
📒 Files selected for processing (2)
internal/agent/component/llm.gointernal/agent/component/llm_test.go
wangq8
left a comment
There was a problem hiding this comment.
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.
Summary
The LLM component (
internal/agent/component/llm.go) did not resolve tenant-scoped model references (tenant_model.idormodel@instance@provider) before dispatching to the model driver. TheAgentandCategorizecomponents already calledresolveChatModelRefto map canvas model IDs to real provider names, driver config, and credentials — but theLLMcomponent passed the rawmodel_idverbatim. This caused custom-added models to fail with HTTP 400 when selected in the agent canvas.This PR adds
resolveChatModelRefat the start ofLLMComponent.Invoke, mirroring the existing resolution logic inAgentComponent.InvokeandCategorizeComponent.Invoke.Additionally,
TestLLM_ResolvesTenantModelIDis added to guard tenant model ID resolution through the full provider/instance/model chain.