Skip to content

Commit 268bc5a

Browse files
authored
🐛 Fixed the issue where large language model information was not written to the database when importing agent configuration.
2 parents 5b66185 + 69082a1 commit 268bc5a

File tree

3 files changed

+1553
-4
lines changed

3 files changed

+1553
-4
lines changed

backend/consts/model.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,10 @@ class ExportAndImportAgentInfo(BaseModel):
274274
enabled: bool
275275
tools: List[ToolConfig]
276276
managed_agents: List[int]
277+
model_id: Optional[int] = None
278+
model_name: Optional[str] = None
279+
business_logic_model_id: Optional[int] = None
280+
business_logic_model_name: Optional[str] = None
277281

278282
class Config:
279283
arbitrary_types_allowed = True

backend/services/agent_service.py

Lines changed: 95 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from agents.agent_run_manager import agent_run_manager
1414
from agents.create_agent_info import create_agent_run_info, create_tool_config_list
1515
from agents.preprocess_manager import preprocess_manager
16-
from consts.const import MEMORY_SEARCH_START_MSG, MEMORY_SEARCH_DONE_MSG, MEMORY_SEARCH_FAIL_MSG, TOOL_TYPE_MAPPING, LANGUAGE, MESSAGE_ROLE
16+
from consts.const import MEMORY_SEARCH_START_MSG, MEMORY_SEARCH_DONE_MSG, MEMORY_SEARCH_FAIL_MSG, TOOL_TYPE_MAPPING, LANGUAGE, MESSAGE_ROLE, MODEL_CONFIG_MAPPING
1717
from consts.exceptions import MemoryPreparationException
1818
from consts.model import (
1919
AgentInfoRequest,
@@ -37,7 +37,7 @@
3737
search_blank_sub_agent_by_main_agent_id,
3838
update_agent
3939
)
40-
from database.model_management_db import get_model_by_model_id
40+
from database.model_management_db import get_model_by_model_id, get_model_id_by_display_name
4141
from database.remote_mcp_db import check_mcp_name_exists, get_mcp_server_by_name_and_tenant
4242
from database.tool_db import (
4343
check_tool_is_available,
@@ -52,6 +52,7 @@
5252
from services.remote_mcp_service import add_remote_mcp_server_list
5353
from services.tool_configuration_service import update_tool_list
5454
from utils.auth_utils import get_current_user_info, get_user_language
55+
from utils.config_utils import tenant_config_manager
5556
from utils.memory_utils import build_memory_config
5657
from utils.thread_utils import submit
5758

@@ -82,6 +83,57 @@ def _resolve_user_tenant_language(
8283
return user_id, tenant_id, get_user_language(http_request)
8384

8485

86+
def _resolve_model_with_fallback(
87+
model_display_name: str | None,
88+
exported_model_id: str | None,
89+
model_label: str,
90+
tenant_id: str
91+
) -> str | None:
92+
"""
93+
Resolve model_id from model_display_name with fallback to quick config LLM model.
94+
95+
Args:
96+
model_display_name: Display name of the model to lookup
97+
exported_model_id: Original model_id from export (for logging only)
98+
model_label: Label for logging (e.g., "Model", "Business logic model")
99+
tenant_id: Tenant ID for model lookup
100+
101+
Returns:
102+
Resolved model_id or None if not found and no fallback available
103+
"""
104+
if not model_display_name:
105+
return None
106+
107+
# Try to find model by display name in current tenant
108+
resolved_id = get_model_id_by_display_name(model_display_name, tenant_id)
109+
110+
if resolved_id:
111+
logger.info(
112+
f"{model_label} '{model_display_name}' found in tenant {tenant_id}, "
113+
f"mapped to model_id: {resolved_id} (exported model_id was: {exported_model_id})")
114+
return resolved_id
115+
116+
# Model not found, try fallback to quick config LLM model
117+
logger.warning(
118+
f"{model_label} '{model_display_name}' (exported model_id: {exported_model_id}) "
119+
f"not found in tenant {tenant_id}, falling back to quick config LLM model.")
120+
121+
quick_config_model = tenant_config_manager.get_model_config(
122+
key=MODEL_CONFIG_MAPPING["llm"],
123+
tenant_id=tenant_id
124+
)
125+
126+
if quick_config_model:
127+
fallback_id = quick_config_model.get("model_id")
128+
logger.info(
129+
f"Using quick config LLM model for {model_label.lower()}: "
130+
f"{quick_config_model.get('display_name')} (model_id: {fallback_id})")
131+
return fallback_id
132+
133+
logger.warning(f"No quick config LLM model found for tenant {tenant_id}")
134+
return None
135+
136+
85137
async def _stream_agent_chunks(
86138
agent_request: "AgentRequest",
87139
user_id: str,
@@ -429,6 +481,20 @@ async def export_agent_by_agent_id(agent_id: int, tenant_id: str, user_id: str)
429481
if tool.class_name == "KnowledgeBaseSearchTool":
430482
tool.metadata = {}
431483

484+
# Get model_id and model display name from agent_info
485+
model_id = agent_info.get("model_id")
486+
model_display_name = None
487+
if model_id is not None:
488+
model_info = get_model_by_model_id(model_id)
489+
model_display_name = model_info.get("display_name") if model_info is not None else None
490+
491+
# Get business_logic_model_id and business logic model display name
492+
business_logic_model_id = agent_info.get("business_logic_model_id")
493+
business_logic_model_display_name = None
494+
if business_logic_model_id is not None:
495+
business_logic_model_info = get_model_by_model_id(business_logic_model_id)
496+
business_logic_model_display_name = business_logic_model_info.get("display_name") if business_logic_model_info is not None else None
497+
432498
agent_info = ExportAndImportAgentInfo(agent_id=agent_id,
433499
name=agent_info["name"],
434500
display_name=agent_info["display_name"],
@@ -444,7 +510,11 @@ async def export_agent_by_agent_id(agent_id: int, tenant_id: str, user_id: str)
444510
"few_shots_prompt"),
445511
enabled=agent_info["enabled"],
446512
tools=tool_list,
447-
managed_agents=agent_relation_in_db)
513+
managed_agents=agent_relation_in_db,
514+
model_id=model_id,
515+
model_name=model_display_name,
516+
business_logic_model_id=business_logic_model_id,
517+
business_logic_model_name=business_logic_model_display_name)
448518
return agent_info
449519

450520

@@ -565,12 +635,33 @@ async def import_agent_by_agent_id(import_agent_info: ExportAndImportAgentInfo,
565635
if not import_agent_info.name.isidentifier():
566636
raise ValueError(
567637
f"Invalid agent name: {import_agent_info.name}. agent name must be a valid python variable name.")
638+
639+
# Resolve model IDs with fallback
640+
# Note: We use model_display_name for cross-tenant compatibility
641+
# The exported model_id is kept for reference/debugging only
642+
model_id = _resolve_model_with_fallback(
643+
model_display_name=import_agent_info.model_name,
644+
exported_model_id=import_agent_info.model_id,
645+
model_label="Model",
646+
tenant_id=tenant_id
647+
)
648+
649+
business_logic_model_id = _resolve_model_with_fallback(
650+
model_display_name=import_agent_info.business_logic_model_name,
651+
exported_model_id=import_agent_info.business_logic_model_id,
652+
model_label="Business logic model",
653+
tenant_id=tenant_id
654+
)
655+
568656
# create a new agent
569657
new_agent = create_agent(agent_info={"name": import_agent_info.name,
570658
"display_name": import_agent_info.display_name,
571659
"description": import_agent_info.description,
572660
"business_description": import_agent_info.business_description,
573-
"model_id": None,
661+
"model_id": model_id,
662+
"model_name": import_agent_info.model_name,
663+
"business_logic_model_id": business_logic_model_id,
664+
"business_logic_model_name": import_agent_info.business_logic_model_name,
574665
"max_steps": import_agent_info.max_steps,
575666
"provide_run_summary": import_agent_info.provide_run_summary,
576667
"duty_prompt": import_agent_info.duty_prompt,

0 commit comments

Comments
 (0)