feat(app):
1. Add new functional features to the agent; 2. Enhance the voice output; 3. Modify the end_user binding; 4. Delete and modify the tools.
This commit is contained in:
@@ -32,6 +32,21 @@ class EndUserRepository:
|
||||
db_logger.error(f"查询应用 {app_id} 下宿主时出错: {str(e)}")
|
||||
raise
|
||||
|
||||
def get_end_users_by_workspace(self, workspace_id: uuid.UUID) -> List[EndUser]:
|
||||
"""获取指定 workspace 下的所有 end_user"""
|
||||
try:
|
||||
end_users = (
|
||||
self.db.query(EndUser)
|
||||
.filter(EndUser.workspace_id == workspace_id)
|
||||
.all()
|
||||
)
|
||||
db_logger.info(f"成功查询工作空间 {workspace_id} 下的 {len(end_users)} 个终端用户")
|
||||
return end_users
|
||||
except Exception as e:
|
||||
self.db.rollback()
|
||||
db_logger.error(f"查询工作空间 {workspace_id} 下终端用户时出错: {str(e)}")
|
||||
raise
|
||||
|
||||
def get_end_user_by_id(self, end_user_id: uuid.UUID) -> Optional[EndUser]:
|
||||
"""根据 end_user_id 查询宿主"""
|
||||
try:
|
||||
@@ -52,14 +67,14 @@ class EndUserRepository:
|
||||
|
||||
def get_or_create_end_user(
|
||||
self,
|
||||
app_id: uuid.UUID,
|
||||
workspace_id: uuid.UUID,
|
||||
other_id: str,
|
||||
original_user_id: Optional[str] = None
|
||||
) -> EndUser:
|
||||
"""获取或创建终端用户
|
||||
|
||||
Args:
|
||||
app_id: 应用ID
|
||||
workspace_id: 工作空间ID
|
||||
other_id: 第三方ID
|
||||
original_user_id: 原始用户ID (存储到 other_id)
|
||||
"""
|
||||
@@ -68,26 +83,27 @@ class EndUserRepository:
|
||||
end_user = (
|
||||
self.db.query(EndUser)
|
||||
.filter(
|
||||
EndUser.app_id == app_id,
|
||||
EndUser.workspace_id == workspace_id,
|
||||
EndUser.other_id == other_id
|
||||
)
|
||||
.order_by(EndUser.created_at.asc())
|
||||
.first()
|
||||
)
|
||||
|
||||
if end_user:
|
||||
db_logger.debug(f"找到现有终端用户: 应用ID {app_id}、第三方ID {other_id}")
|
||||
db_logger.debug(f"找到现有终端用户: 应用ID {workspace_id}、第三方ID {other_id}")
|
||||
return end_user
|
||||
|
||||
# 创建新用户
|
||||
end_user = EndUser(
|
||||
app_id=app_id,
|
||||
workspace_id=workspace_id,
|
||||
other_id=other_id
|
||||
)
|
||||
self.db.add(end_user)
|
||||
self.db.commit()
|
||||
self.db.refresh(end_user)
|
||||
|
||||
db_logger.info(f"创建新终端用户: (other_id: {other_id}) for app {app_id}")
|
||||
db_logger.info(f"创建新终端用户: (other_id: {other_id}) for workspace {workspace_id}")
|
||||
return end_user
|
||||
|
||||
except Exception as e:
|
||||
@@ -314,8 +330,7 @@ class EndUserRepository:
|
||||
try:
|
||||
end_users = (
|
||||
self.db.query(EndUser)
|
||||
.join(App, EndUser.app_id == App.id)
|
||||
.filter(App.workspace_id == workspace_id)
|
||||
.filter(EndUser.workspace_id == workspace_id)
|
||||
.all()
|
||||
)
|
||||
db_logger.info(f"成功查询工作空间 {workspace_id} 下的 {len(end_users)} 个终端用户")
|
||||
@@ -402,45 +417,79 @@ class EndUserRepository:
|
||||
db_logger.error(f"获取终端用户 {end_user_id} 的 memory_config_id 时出错: {str(e)}")
|
||||
raise
|
||||
|
||||
def batch_update_memory_config_id(
|
||||
self,
|
||||
app_id: uuid.UUID,
|
||||
memory_config_id: uuid.UUID
|
||||
# def batch_update_memory_config_id(
|
||||
# self,
|
||||
# app_id: uuid.UUID,
|
||||
# memory_config_id: uuid.UUID
|
||||
# ) -> int:
|
||||
# """批量更新应用下所有终端用户的 memory_config_id
|
||||
#
|
||||
# Args:
|
||||
# app_id: 应用ID
|
||||
# memory_config_id: 新的记忆配置ID
|
||||
#
|
||||
# Returns:
|
||||
# int: 更新的行数
|
||||
# """
|
||||
# try:
|
||||
# from sqlalchemy import update
|
||||
#
|
||||
# stmt = (
|
||||
# update(EndUser)
|
||||
# .where(EndUser.app_id == app_id)
|
||||
# .values(memory_config_id=memory_config_id)
|
||||
# )
|
||||
#
|
||||
# result = self.db.execute(stmt)
|
||||
# self.db.commit()
|
||||
#
|
||||
# updated_count = result.rowcount
|
||||
#
|
||||
# db_logger.info(
|
||||
# f"批量更新终端用户记忆配置: app_id={app_id}, "
|
||||
# f"memory_config_id={memory_config_id}, updated_count={updated_count}"
|
||||
# )
|
||||
#
|
||||
# return updated_count
|
||||
#
|
||||
# except Exception as e:
|
||||
# self.db.rollback()
|
||||
# db_logger.error(
|
||||
# f"批量更新终端用户记忆配置时出错: app_id={app_id}, "
|
||||
# f"memory_config_id={memory_config_id}, error={str(e)}"
|
||||
# )
|
||||
# raise
|
||||
|
||||
def batch_update_memory_config_id_by_workspace(
|
||||
self,
|
||||
workspace_id: uuid.UUID,
|
||||
memory_config_id: uuid.UUID
|
||||
) -> int:
|
||||
"""批量更新应用下所有终端用户的 memory_config_id
|
||||
|
||||
Args:
|
||||
app_id: 应用ID
|
||||
memory_config_id: 新的记忆配置ID
|
||||
|
||||
Returns:
|
||||
int: 更新的行数
|
||||
"""
|
||||
"""批量更新工作空间下所有终端用户的 memory_config_id"""
|
||||
try:
|
||||
from sqlalchemy import update
|
||||
|
||||
stmt = (
|
||||
update(EndUser)
|
||||
.where(EndUser.app_id == app_id)
|
||||
.where(EndUser.workspace_id == workspace_id)
|
||||
.values(memory_config_id=memory_config_id)
|
||||
)
|
||||
|
||||
|
||||
result = self.db.execute(stmt)
|
||||
self.db.commit()
|
||||
|
||||
|
||||
updated_count = result.rowcount
|
||||
|
||||
|
||||
db_logger.info(
|
||||
f"批量更新终端用户记忆配置: app_id={app_id}, "
|
||||
f"批量更新终端用户记忆配置: workspace_id={workspace_id}, "
|
||||
f"memory_config_id={memory_config_id}, updated_count={updated_count}"
|
||||
)
|
||||
|
||||
|
||||
return updated_count
|
||||
|
||||
except Exception as e:
|
||||
self.db.rollback()
|
||||
db_logger.error(
|
||||
f"批量更新终端用户记忆配置时出错: app_id={app_id}, "
|
||||
f"批量更新终端用户记忆配置时出错: workspace_id={workspace_id}, "
|
||||
f"memory_config_id={memory_config_id}, error={str(e)}"
|
||||
)
|
||||
raise
|
||||
@@ -492,7 +541,7 @@ class EndUserRepository:
|
||||
"""
|
||||
try:
|
||||
from sqlalchemy import update
|
||||
|
||||
|
||||
stmt = (
|
||||
update(EndUser)
|
||||
.where(EndUser.memory_config_id == memory_config_id)
|
||||
@@ -519,10 +568,16 @@ class EndUserRepository:
|
||||
)
|
||||
raise
|
||||
|
||||
def get_end_users_by_app_id(db: Session, app_id: uuid.UUID) -> List[EndUser]:
|
||||
"""根据应用ID查询宿主(返回 EndUser ORM 列表)"""
|
||||
# def get_end_users_by_app_id(db: Session, app_id: uuid.UUID) -> List[EndUser]:
|
||||
# """根据应用ID查询宿主(返回 EndUser ORM 列表)"""
|
||||
# repo = EndUserRepository(db)
|
||||
# end_users = repo.get_end_users_by_app_id(app_id)
|
||||
# return end_users
|
||||
|
||||
def get_end_users_by_workspace(db: Session, workspace_id: uuid.UUID) -> List[EndUser]:
|
||||
"""根据工作空间ID查询终端用户(返回 EndUser ORM 列表)"""
|
||||
repo = EndUserRepository(db)
|
||||
end_users = repo.get_end_users_by_app_id(app_id)
|
||||
end_users = repo.get_end_users_by_workspace(workspace_id)
|
||||
return end_users
|
||||
|
||||
def get_end_user_by_id(db: Session, end_user_id: uuid.UUID) -> Optional[EndUser]:
|
||||
|
||||
@@ -27,7 +27,7 @@ class ToolRepository:
|
||||
from app.models.app_model import App
|
||||
from app.models.workflow_model import WorkflowConfig
|
||||
from app.models.workspace_model import Workspace
|
||||
|
||||
|
||||
result = db.query(Workspace.tenant_id).join(
|
||||
App, App.workspace_id == Workspace.id
|
||||
).join(
|
||||
@@ -35,7 +35,7 @@ class ToolRepository:
|
||||
).filter(
|
||||
WorkflowConfig.id == workflow_id
|
||||
).first()
|
||||
|
||||
|
||||
return result[0] if result else None
|
||||
|
||||
@staticmethod
|
||||
@@ -67,18 +67,19 @@ class ToolRepository:
|
||||
|
||||
@staticmethod
|
||||
def find_by_tenant(
|
||||
db: Session,
|
||||
tenant_id: uuid.UUID,
|
||||
name: Optional[str] = None,
|
||||
tool_type: Optional[ToolType] = None,
|
||||
status: Optional[ToolStatus] = None,
|
||||
is_enabled: Optional[bool] = None
|
||||
db: Session,
|
||||
tenant_id: uuid.UUID,
|
||||
name: Optional[str] = None,
|
||||
tool_type: Optional[ToolType] = None,
|
||||
status: Optional[ToolStatus] = None,
|
||||
is_enabled: Optional[bool] = None
|
||||
) -> List[ToolConfig]:
|
||||
"""根据租户查找工具"""
|
||||
"""根据租户查找工具(只返回未删除的)"""
|
||||
query = db.query(ToolConfig).filter(
|
||||
ToolConfig.tenant_id == tenant_id
|
||||
ToolConfig.tenant_id == tenant_id,
|
||||
ToolConfig.is_active.is_(True)
|
||||
)
|
||||
|
||||
|
||||
if name:
|
||||
query = query.filter(ToolConfig.name.ilike(f"%{name}%"))
|
||||
if tool_type:
|
||||
@@ -91,8 +92,17 @@ class ToolRepository:
|
||||
return query.all()
|
||||
|
||||
@staticmethod
|
||||
def find_by_id_and_tenant(db:Session, tool_id: uuid.UUID, tenant_id: uuid.UUID) -> Optional[ToolConfig]:
|
||||
"""根据ID和租户查找工具"""
|
||||
def find_by_id_and_tenant(db: Session, tool_id: uuid.UUID, tenant_id: uuid.UUID) -> Optional[ToolConfig]:
|
||||
"""根据ID和租户查找工具(只返回未删除的)"""
|
||||
return db.query(ToolConfig).filter(
|
||||
ToolConfig.id == tool_id,
|
||||
ToolConfig.tenant_id == tenant_id,
|
||||
ToolConfig.is_active.is_(True)
|
||||
).first()
|
||||
|
||||
@staticmethod
|
||||
def find_by_id_and_tenant_all(db: Session, tool_id: uuid.UUID, tenant_id: uuid.UUID) -> Optional[ToolConfig]:
|
||||
"""根据ID和租户查找工具(返回所有工具包括删除的)"""
|
||||
return db.query(ToolConfig).filter(
|
||||
ToolConfig.id == tool_id,
|
||||
ToolConfig.tenant_id == tenant_id
|
||||
@@ -100,29 +110,26 @@ class ToolRepository:
|
||||
|
||||
@staticmethod
|
||||
def count_by_tenant(db: Session, tenant_id: uuid.UUID) -> int:
|
||||
"""统计租户工具数量"""
|
||||
"""统计租户工具数量(只统计未删除的)"""
|
||||
return db.query(ToolConfig).filter(
|
||||
ToolConfig.tenant_id == tenant_id
|
||||
ToolConfig.tenant_id == tenant_id,
|
||||
ToolConfig.is_active.is_(True)
|
||||
).count()
|
||||
|
||||
@staticmethod
|
||||
def get_status_statistics(db: Session, tenant_id: uuid.UUID) -> List[tuple]:
|
||||
"""获取状态统计"""
|
||||
return db.query(
|
||||
ToolConfig.status,
|
||||
func.count(ToolConfig.id).label('count')
|
||||
).filter(
|
||||
ToolConfig.tenant_id == tenant_id
|
||||
return db.query(ToolConfig.status, func.count(ToolConfig.id).label('count')).filter(
|
||||
ToolConfig.tenant_id == tenant_id,
|
||||
ToolConfig.is_active.is_(True)
|
||||
).group_by(ToolConfig.status).all()
|
||||
|
||||
@staticmethod
|
||||
def get_type_statistics(db: Session, tenant_id: uuid.UUID) -> List[tuple]:
|
||||
"""获取类型统计"""
|
||||
return db.query(
|
||||
ToolConfig.tool_type,
|
||||
func.count(ToolConfig.id).label('count')
|
||||
).filter(
|
||||
ToolConfig.tenant_id == tenant_id
|
||||
return db.query(ToolConfig.tool_type, func.count(ToolConfig.id).label('count')).filter(
|
||||
ToolConfig.tenant_id == tenant_id,
|
||||
ToolConfig.is_active.is_(True)
|
||||
).group_by(ToolConfig.tool_type).all()
|
||||
|
||||
@staticmethod
|
||||
@@ -130,6 +137,7 @@ class ToolRepository:
|
||||
"""统计租户启用的工具数量"""
|
||||
return db.query(ToolConfig).filter(
|
||||
ToolConfig.tenant_id == tenant_id,
|
||||
ToolConfig.is_active.is_(True),
|
||||
ToolConfig.is_enabled == True
|
||||
).count()
|
||||
|
||||
@@ -138,7 +146,8 @@ class ToolRepository:
|
||||
"""检查租户是否已有内置工具"""
|
||||
return db.query(ToolConfig).filter(
|
||||
ToolConfig.tenant_id == tenant_id,
|
||||
ToolConfig.tool_type == ToolType.BUILTIN.value
|
||||
ToolConfig.tool_type == ToolType.BUILTIN.value,
|
||||
ToolConfig.is_active.is_(True)
|
||||
).count() > 0
|
||||
|
||||
|
||||
@@ -194,10 +203,10 @@ class ToolExecutionRepository:
|
||||
|
||||
@staticmethod
|
||||
def find_by_tool_and_tenant(
|
||||
db: Session,
|
||||
tool_id: uuid.UUID,
|
||||
tenant_id: uuid.UUID,
|
||||
limit: int = 100
|
||||
db: Session,
|
||||
tool_id: uuid.UUID,
|
||||
tenant_id: uuid.UUID,
|
||||
limit: int = 100
|
||||
) -> List[ToolExecution]:
|
||||
"""根据工具和租户查找执行记录"""
|
||||
return db.query(ToolExecution).join(
|
||||
@@ -205,4 +214,4 @@ class ToolExecutionRepository:
|
||||
).filter(
|
||||
ToolConfig.id == tool_id,
|
||||
ToolConfig.tenant_id == tenant_id
|
||||
).order_by(ToolExecution.started_at.desc()).limit(limit).all()
|
||||
).order_by(ToolExecution.started_at.desc()).limit(limit).all()
|
||||
|
||||
Reference in New Issue
Block a user