fix: 补充默认智能体描述
为默认智能体提供说明并在缺失时自动回填,避免配置面板展示空描述。
This commit is contained in:
parent
03445438e5
commit
b08ddc118f
@ -14,6 +14,7 @@ from yuxi.utils.datetime_utils import utc_now_naive
|
||||
DEFAULT_AGENT_SLUG = "default-chatbot"
|
||||
DEFAULT_AGENT_NAME = "智能助手"
|
||||
DEFAULT_AGENT_BACKEND_ID = "ChatbotAgent"
|
||||
DEFAULT_AGENT_DESCRIPTION = "基础的对话机器人,可以回答问题,可在配置中启用需要的工具。"
|
||||
DEFAULT_SHARE_CONFIG = {"access_level": "global", "department_ids": [], "user_uids": []}
|
||||
ACCESS_LEVELS = {"global", "department", "user"}
|
||||
ADMIN_ROLES = {"admin", "superadmin"}
|
||||
@ -115,6 +116,9 @@ class AgentRepository:
|
||||
if agent.share_config != DEFAULT_SHARE_CONFIG:
|
||||
agent.share_config = DEFAULT_SHARE_CONFIG.copy()
|
||||
needs_update = True
|
||||
if not agent.description:
|
||||
agent.description = DEFAULT_AGENT_DESCRIPTION
|
||||
needs_update = True
|
||||
if not agent.is_default:
|
||||
return await self.set_default(agent=agent, updated_by=created_by)
|
||||
if needs_update:
|
||||
@ -128,7 +132,7 @@ class AgentRepository:
|
||||
slug=DEFAULT_AGENT_SLUG,
|
||||
backend_id=DEFAULT_AGENT_BACKEND_ID,
|
||||
name=DEFAULT_AGENT_NAME,
|
||||
description=None,
|
||||
description=DEFAULT_AGENT_DESCRIPTION,
|
||||
icon=None,
|
||||
pics=[],
|
||||
config_json={"context": {}},
|
||||
|
||||
62
backend/test/unit/repositories/test_agent_repository.py
Normal file
62
backend/test/unit/repositories/test_agent_repository.py
Normal file
@ -0,0 +1,62 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
import pytest
|
||||
|
||||
from yuxi.repositories.agent_repository import AgentRepository, DEFAULT_AGENT_DESCRIPTION, DEFAULT_SHARE_CONFIG
|
||||
|
||||
|
||||
class FakeDb:
|
||||
def __init__(self):
|
||||
self.added = None
|
||||
self.commit = AsyncMock()
|
||||
self.refresh = AsyncMock()
|
||||
|
||||
def add(self, item):
|
||||
self.added = item
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_ensure_default_agent_creates_description(monkeypatch):
|
||||
db = FakeDb()
|
||||
repo = AgentRepository(db)
|
||||
|
||||
async def get_by_slug(_slug):
|
||||
return None
|
||||
|
||||
monkeypatch.setattr(repo, "get_by_slug", get_by_slug)
|
||||
|
||||
agent = await repo.ensure_default_agent()
|
||||
|
||||
assert agent.description == DEFAULT_AGENT_DESCRIPTION
|
||||
assert db.added is agent
|
||||
db.commit.assert_awaited_once()
|
||||
db.refresh.assert_awaited_once_with(agent)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_ensure_default_agent_backfills_missing_description(monkeypatch):
|
||||
db = FakeDb()
|
||||
repo = AgentRepository(db)
|
||||
agent = SimpleNamespace(
|
||||
share_config=DEFAULT_SHARE_CONFIG.copy(),
|
||||
is_default=True,
|
||||
description=None,
|
||||
updated_by=None,
|
||||
updated_at=None,
|
||||
)
|
||||
|
||||
async def get_by_slug(_slug):
|
||||
return agent
|
||||
|
||||
monkeypatch.setattr(repo, "get_by_slug", get_by_slug)
|
||||
|
||||
result = await repo.ensure_default_agent(created_by="admin")
|
||||
|
||||
assert result is agent
|
||||
assert agent.description == DEFAULT_AGENT_DESCRIPTION
|
||||
assert agent.updated_by == "admin"
|
||||
db.commit.assert_awaited_once()
|
||||
db.refresh.assert_awaited_once_with(agent)
|
||||
Loading…
Reference in New Issue
Block a user