|
@@ -1,6 +1,5 @@
|
|
|
"""demand 示例的最小可运行入口。"""
|
|
"""demand 示例的最小可运行入口。"""
|
|
|
|
|
|
|
|
-import asyncio
|
|
|
|
|
import copy
|
|
import copy
|
|
|
import importlib
|
|
import importlib
|
|
|
import json
|
|
import json
|
|
@@ -41,7 +40,7 @@ from agent.llm import create_openrouter_llm_call
|
|
|
from agent.llm.prompts import SimplePrompt
|
|
from agent.llm.prompts import SimplePrompt
|
|
|
from agent.trace import FileSystemTraceStore, Message, Trace
|
|
from agent.trace import FileSystemTraceStore, Message, Trace
|
|
|
from agent.utils import setup_logging
|
|
from agent.utils import setup_logging
|
|
|
-from log_capture import build_log, log
|
|
|
|
|
|
|
+from examples.demand.log_capture import build_log, log
|
|
|
|
|
|
|
|
# 导入项目配置
|
|
# 导入项目配置
|
|
|
from examples.demand.config import DEBUG, LOG_FILE, LOG_LEVEL, RUN_CONFIG, TRACE_STORE_PATH
|
|
from examples.demand.config import DEBUG, LOG_FILE, LOG_LEVEL, RUN_CONFIG, TRACE_STORE_PATH
|
|
@@ -172,13 +171,26 @@ def _avg_score_for_joined_name(name: str, score_map: dict) -> float:
|
|
|
return sum(float(score_map.get(part, 0.0)) for part in parts) / len(parts)
|
|
return sum(float(score_map.get(part, 0.0)) for part in parts) / len(parts)
|
|
|
|
|
|
|
|
|
|
|
|
|
-def _create_demand_task(execution_id: int) -> Optional[int]:
|
|
|
|
|
|
|
+def _create_demand_task(
|
|
|
|
|
+ execution_id: int,
|
|
|
|
|
+ name: Optional[str] = None,
|
|
|
|
|
+ platform: Optional[str] = None,
|
|
|
|
|
+) -> Optional[int]:
|
|
|
"""创建 demand_task 记录,返回任务ID。"""
|
|
"""创建 demand_task 记录,返回任务ID。"""
|
|
|
try:
|
|
try:
|
|
|
|
|
+ # 数据库字段 demand_task.name: varchar(32)
|
|
|
|
|
+ if name is not None:
|
|
|
|
|
+ name = str(name)[:32]
|
|
|
|
|
+ # 数据库字段 demand_task.platform: varchar(32)
|
|
|
|
|
+ if platform is not None:
|
|
|
|
|
+ platform = str(platform)[:32]
|
|
|
|
|
+
|
|
|
task_id = mysql_db.insert(
|
|
task_id = mysql_db.insert(
|
|
|
"demand_task",
|
|
"demand_task",
|
|
|
{
|
|
{
|
|
|
"execution_id": execution_id,
|
|
"execution_id": execution_id,
|
|
|
|
|
+ "name": name,
|
|
|
|
|
+ "platform": platform,
|
|
|
"status": 0,
|
|
"status": 0,
|
|
|
"log": "",
|
|
"log": "",
|
|
|
},
|
|
},
|
|
@@ -280,10 +292,9 @@ def write_demand_items_to_mysql(execution_id: int, merge_level2: str) -> int:
|
|
|
return len(rows)
|
|
return len(rows)
|
|
|
|
|
|
|
|
|
|
|
|
|
-async def run_once(execution_id, merge_level2) -> str:
|
|
|
|
|
- # task_id = _create_demand_task(execution_id=execution_id)
|
|
|
|
|
- # task_status = 2
|
|
|
|
|
|
|
+async def run_once(execution_id, merge_level2, task_id: Optional[int] = None) -> str:
|
|
|
task_log_text = ""
|
|
task_log_text = ""
|
|
|
|
|
+ task_status = 0
|
|
|
|
|
|
|
|
TopicBuildAgentContext.set_execution_id(execution_id)
|
|
TopicBuildAgentContext.set_execution_id(execution_id)
|
|
|
prepare(execution_id)
|
|
prepare(execution_id)
|
|
@@ -356,35 +367,57 @@ async def run_once(execution_id, merge_level2) -> str:
|
|
|
|
|
|
|
|
# agent 执行完成后:把本地 result JSON 写入 MySQL 表 demand_content
|
|
# agent 执行完成后:把本地 result JSON 写入 MySQL 表 demand_content
|
|
|
# element_names -> name(逗号分隔);reason/desc -> ext_data JSON;merge_leve2 -> demand_content.merge_leve2
|
|
# element_names -> name(逗号分隔);reason/desc -> ext_data JSON;merge_leve2 -> demand_content.merge_leve2
|
|
|
- # try:
|
|
|
|
|
- # write_demand_items_to_mysql(execution_id=execution_id, merge_level2=merge_level2)
|
|
|
|
|
- # except Exception as e:
|
|
|
|
|
- # log(f"[mysql] 写入 demand_content 异常:{e}")
|
|
|
|
|
|
|
+ try:
|
|
|
|
|
+ write_demand_items_to_mysql(execution_id=execution_id, merge_level2=merge_level2)
|
|
|
|
|
+ except Exception as e:
|
|
|
|
|
+ log(f"[mysql] 写入 demand_content 异常:{e}")
|
|
|
|
|
|
|
|
task_log_text = log_buffer.getvalue()
|
|
task_log_text = log_buffer.getvalue()
|
|
|
- with open(log_file_path, "w", encoding="utf-8") as f:
|
|
|
|
|
- f.write(task_log_text)
|
|
|
|
|
-
|
|
|
|
|
task_status = 1
|
|
task_status = 1
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
|
|
+ if not task_log_text:
|
|
|
|
|
+ # 如果异常发生在 build_log 内部,尽量回收已产生的日志
|
|
|
|
|
+ try:
|
|
|
|
|
+ existing = locals().get("log_buffer")
|
|
|
|
|
+ if existing is not None:
|
|
|
|
|
+ task_log_text = existing.getvalue() # type: ignore[attr-defined]
|
|
|
|
|
+ except Exception:
|
|
|
|
|
+ pass
|
|
|
if not task_log_text:
|
|
if not task_log_text:
|
|
|
task_log_text = f"[run] 执行异常: {e}"
|
|
task_log_text = f"[run] 执行异常: {e}"
|
|
|
task_status = 2
|
|
task_status = 2
|
|
|
raise
|
|
raise
|
|
|
- # finally:
|
|
|
|
|
- # _finish_demand_task(task_id=task_id, status=task_status, task_log=task_log_text)
|
|
|
|
|
|
|
+ finally:
|
|
|
|
|
+ if task_log_text:
|
|
|
|
|
+ try:
|
|
|
|
|
+ with open(log_file_path, "w", encoding="utf-8") as f:
|
|
|
|
|
+ f.write(task_log_text)
|
|
|
|
|
+ except Exception:
|
|
|
|
|
+ # 兜底:即使写文件失败,也要确保 MySQL 状态被更新
|
|
|
|
|
+ pass
|
|
|
|
|
+ _finish_demand_task(task_id=task_id, status=task_status, task_log=task_log_text)
|
|
|
|
|
|
|
|
return final_text
|
|
return final_text
|
|
|
|
|
|
|
|
|
|
|
|
|
-async def main(cluster_name, platform_type) -> None:
|
|
|
|
|
- execution_id = None
|
|
|
|
|
- if platform_type == "piaoquan":
|
|
|
|
|
- execution_id = piaoquan_prepare(cluster_name)
|
|
|
|
|
- elif platform_type == "changwen":
|
|
|
|
|
- execution_id = changwen_prepare(cluster_name)
|
|
|
|
|
- if execution_id:
|
|
|
|
|
- await run_once(execution_id, cluster_name)
|
|
|
|
|
|
|
+async def main(
|
|
|
|
|
+ cluster_name: str,
|
|
|
|
|
+ platform_type: str,
|
|
|
|
|
+ execution_id: Optional[int] = None,
|
|
|
|
|
+ task_id: Optional[int] = None,
|
|
|
|
|
+) -> dict:
|
|
|
|
|
+ if execution_id is None:
|
|
|
|
|
+ if platform_type == "piaoquan":
|
|
|
|
|
+ execution_id = piaoquan_prepare(cluster_name)
|
|
|
|
|
+ elif platform_type == "changwen":
|
|
|
|
|
+ execution_id = changwen_prepare(cluster_name)
|
|
|
|
|
+ else:
|
|
|
|
|
+ execution_id = None
|
|
|
|
|
+ if not execution_id:
|
|
|
|
|
+ return {"execution_id": None, "final_text": ""}
|
|
|
|
|
+
|
|
|
|
|
+ final_text = await run_once(execution_id, cluster_name, task_id=task_id)
|
|
|
|
|
+ return {"execution_id": execution_id, "final_text": final_text}
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if __name__ == "__main__":
|