@@ -51,7 +51,6 @@ async def save_reflection_config(
|
|||||||
status_code=status.HTTP_400_BAD_REQUEST,
|
status_code=status.HTTP_400_BAD_REQUEST,
|
||||||
detail="缺少必需参数: config_id"
|
detail="缺少必需参数: config_id"
|
||||||
)
|
)
|
||||||
|
|
||||||
api_logger.info(f"用户 {current_user.username} 保存反思配置,config_id: {config_id}")
|
api_logger.info(f"用户 {current_user.username} 保存反思配置,config_id: {config_id}")
|
||||||
|
|
||||||
memory_config = MemoryConfigRepository.update_reflection_config(
|
memory_config = MemoryConfigRepository.update_reflection_config(
|
||||||
@@ -102,7 +101,7 @@ async def start_workspace_reflection(
|
|||||||
current_user: User = Depends(get_current_user),
|
current_user: User = Depends(get_current_user),
|
||||||
db: Session = Depends(get_db),
|
db: Session = Depends(get_db),
|
||||||
) -> dict:
|
) -> dict:
|
||||||
"""Activate the reflection function for all matching applications in the workspace"""
|
"""启动工作空间中所有匹配应用的反思功能"""
|
||||||
workspace_id = current_user.current_workspace_id
|
workspace_id = current_user.current_workspace_id
|
||||||
reflection_service = MemoryReflectionService(db)
|
reflection_service = MemoryReflectionService(db)
|
||||||
|
|
||||||
@@ -111,42 +110,55 @@ async def start_workspace_reflection(
|
|||||||
|
|
||||||
service = WorkspaceAppService(db)
|
service = WorkspaceAppService(db)
|
||||||
result = service.get_workspace_apps_detailed(workspace_id)
|
result = service.get_workspace_apps_detailed(workspace_id)
|
||||||
|
|
||||||
reflection_results = []
|
reflection_results = []
|
||||||
|
|
||||||
for data in result['apps_detailed_info']:
|
for data in result['apps_detailed_info']:
|
||||||
if data['memory_configs'] == []:
|
# 跳过没有配置的应用
|
||||||
|
if not data['memory_configs']:
|
||||||
|
api_logger.debug(f"应用 {data['id']} 没有memory_configs,跳过")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
releases = data['releases']
|
releases = data['releases']
|
||||||
memory_configs = data['memory_configs']
|
memory_configs = data['memory_configs']
|
||||||
end_users = data['end_users']
|
end_users = data['end_users']
|
||||||
|
|
||||||
for base, config, user in zip(releases, memory_configs, end_users):
|
# 为每个配置和用户组合执行反思
|
||||||
# 安全地转换为整数,处理空字符串和None的情况
|
for config in memory_configs:
|
||||||
print(base['config'])
|
config_id_str = str(config['config_id'])
|
||||||
try:
|
|
||||||
base_config = int(base['config']) if base['config'] else 0
|
# 找到匹配此配置的所有release
|
||||||
config_id = int(config['config_id']) if config['config_id'] else 0
|
matching_releases = [r for r in releases if str(r['config']) == config_id_str]
|
||||||
except (ValueError, TypeError):
|
|
||||||
api_logger.warning(f"无效的配置ID: base['config']={base.get('config')}, config['config_id']={config.get('config_id')}")
|
if not matching_releases:
|
||||||
|
api_logger.debug(f"配置 {config_id_str} 没有匹配的release")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if base_config == config_id and base['app_id'] == user['app_id']:
|
# 为每个用户执行反思
|
||||||
# 调用反思服务
|
for user in end_users:
|
||||||
api_logger.info(f"为用户 {user['id']} 启动反思,config_id: {config['config_id']}")
|
api_logger.info(f"为用户 {user['id']} 启动反思,config_id: {config_id_str}")
|
||||||
|
|
||||||
reflection_result = await reflection_service.start_text_reflection(
|
try:
|
||||||
config_data=config,
|
reflection_result = await reflection_service.start_text_reflection(
|
||||||
end_user_id=user['id']
|
config_data=config,
|
||||||
)
|
end_user_id=user['id']
|
||||||
|
)
|
||||||
reflection_results.append({
|
|
||||||
"app_id": base['app_id'],
|
reflection_results.append({
|
||||||
"config_id": config['config_id'],
|
"app_id": data['id'],
|
||||||
"end_user_id": user['id'],
|
"config_id": config_id_str,
|
||||||
"reflection_result": reflection_result
|
"end_user_id": user['id'],
|
||||||
})
|
"reflection_result": reflection_result
|
||||||
|
})
|
||||||
|
except Exception as e:
|
||||||
|
api_logger.error(f"用户 {user['id']} 反思失败: {str(e)}")
|
||||||
|
reflection_results.append({
|
||||||
|
"app_id": data['id'],
|
||||||
|
"config_id": config_id_str,
|
||||||
|
"end_user_id": user['id'],
|
||||||
|
"reflection_result": {
|
||||||
|
"status": "错误",
|
||||||
|
"message": f"反思失败: {str(e)}"
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
return success(data=reflection_results, msg="反思配置成功")
|
return success(data=reflection_results, msg="反思配置成功")
|
||||||
|
|
||||||
|
|||||||
@@ -154,7 +154,7 @@ class MemoryConfigRepository:
|
|||||||
return memory_config_obj
|
return memory_config_obj
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def query_reflection_config_by_id(db: Session, config_id: uuid.UUID) -> MemoryConfig:
|
def query_reflection_config_by_id(db: Session, config_id: uuid.UUID|int|str) -> MemoryConfig:
|
||||||
"""构建反思配置查询语句,通过config_id查询反思配置(SQLAlchemy text() 命名参数)
|
"""构建反思配置查询语句,通过config_id查询反思配置(SQLAlchemy text() 命名参数)
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
|||||||
@@ -89,7 +89,6 @@ class WorkspaceAppService:
|
|||||||
|
|
||||||
for release in app_releases:
|
for release in app_releases:
|
||||||
memory_content = self._extract_memory_content(release.config)
|
memory_content = self._extract_memory_content(release.config)
|
||||||
memory_content=resolve_config_id(memory_content, self.db)
|
|
||||||
if memory_content and memory_content in processed_configs:
|
if memory_content and memory_content in processed_configs:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
@@ -122,16 +121,12 @@ class WorkspaceAppService:
|
|||||||
def _get_memory_config(self, memory_content: str) -> Dict[str, Any]:
|
def _get_memory_config(self, memory_content: str) -> Dict[str, Any]:
|
||||||
"""Retrieve memory_config information based on memory_content"""
|
"""Retrieve memory_config information based on memory_content"""
|
||||||
try:
|
try:
|
||||||
memory_config_result = MemoryConfigRepository.query_reflection_config_by_id(self.db, int(memory_content))
|
memory_content = resolve_config_id(memory_content, self.db)
|
||||||
|
memory_config_result = MemoryConfigRepository.query_reflection_config_by_id(self.db, (memory_content))
|
||||||
# memory_config_query, memory_config_params = MemoryConfigRepository.build_select_reflection(memory_content)
|
|
||||||
# memory_config_result = self.db.execute(text(memory_config_query), memory_config_params).fetchone()
|
|
||||||
# if memory_config_result is None:
|
|
||||||
# return None
|
|
||||||
|
|
||||||
if memory_config_result:
|
if memory_config_result:
|
||||||
return {
|
return {
|
||||||
"config_id": memory_config_result.config_id,
|
"config_id": memory_content,
|
||||||
"enable_self_reflexion": memory_config_result.enable_self_reflexion,
|
"enable_self_reflexion": memory_config_result.enable_self_reflexion,
|
||||||
"iteration_period": memory_config_result.iteration_period,
|
"iteration_period": memory_config_result.iteration_period,
|
||||||
"reflexion_range": memory_config_result.reflexion_range,
|
"reflexion_range": memory_config_result.reflexion_range,
|
||||||
|
|||||||
Reference in New Issue
Block a user