|
@@ -15,13 +15,43 @@ from datetime import datetime
|
|
|
import uuid
|
|
import uuid
|
|
|
|
|
|
|
|
|
|
|
|
|
-def _resolve_input_log_dir(content_finder_root: Path) -> Path:
|
|
|
|
|
- """与 .env 中 INPUT_LOG_PATH 一致:目录;相对路径相对 content_finder 根目录。"""
|
|
|
|
|
- raw = os.getenv("INPUT_LOG_PATH", ".cache/input_log")
|
|
|
|
|
|
|
+def _resolve_repo_root() -> Path:
|
|
|
|
|
+ # /.../Agent/examples/content_finder/core.py -> repo root is /.../Agent
|
|
|
|
|
+ return Path(__file__).resolve().parents[2]
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def _resolve_dir_from_env(repo_root: Path, raw: str) -> Path:
|
|
|
p = Path(raw).expanduser()
|
|
p = Path(raw).expanduser()
|
|
|
- if p.is_absolute():
|
|
|
|
|
- return p if not p.suffix else p.parent
|
|
|
|
|
- return (content_finder_root / p).resolve()
|
|
|
|
|
|
|
+ return p.resolve() if p.is_absolute() else (repo_root / p).resolve()
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def _resolve_log_file_path(
|
|
|
|
|
+ *,
|
|
|
|
|
+ content_finder_root: Path,
|
|
|
|
|
+ output_dir_path: Path,
|
|
|
|
|
+ trace_id: str | None,
|
|
|
|
|
+ execution_id: str,
|
|
|
|
|
+) -> Path:
|
|
|
|
|
+ """
|
|
|
|
|
+ 解析日志输出路径。
|
|
|
|
|
+
|
|
|
|
|
+ 规则:
|
|
|
|
|
+ - 如果设置了 INPUT_LOG_PATH:
|
|
|
|
|
+ - 值为 OUTPUT_DIR / ${OUTPUT_DIR}:写入 OUTPUT_DIR/<trace_id>/log.txt
|
|
|
|
|
+ - 绝对/相对路径:视为“目录”,写入 <dir>/run_log_<timestamp>.txt(兼容旧行为)
|
|
|
|
|
+ - 未设置 INPUT_LOG_PATH:默认写入 OUTPUT_DIR/<trace_id>/log.txt
|
|
|
|
|
+ """
|
|
|
|
|
+ raw = (os.getenv("INPUT_LOG_PATH") or "").strip()
|
|
|
|
|
+ dir_name = trace_id or execution_id
|
|
|
|
|
+
|
|
|
|
|
+ if raw in {"OUTPUT_DIR", "${OUTPUT_DIR}"} or raw == "":
|
|
|
|
|
+ return (output_dir_path / dir_name / "log.txt").resolve()
|
|
|
|
|
+
|
|
|
|
|
+ p = Path(raw).expanduser()
|
|
|
|
|
+ if not p.is_absolute():
|
|
|
|
|
+ p = (content_finder_root / p).resolve()
|
|
|
|
|
+ log_dir = p if not p.suffix else p.parent
|
|
|
|
|
+ return (log_dir / f"run_log_{datetime.now().strftime('%Y%m%d_%H%M%S')}.txt").resolve()
|
|
|
|
|
|
|
|
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
|
|
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
|
|
|
|
|
|
|
@@ -60,7 +90,7 @@ from tools import (
|
|
|
logger = logging.getLogger(__name__)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
# 默认搜索词
|
|
# 默认搜索词
|
|
|
-DEFAULT_QUERY = "毛泽东,反腐倡廉"
|
|
|
|
|
|
|
+DEFAULT_QUERY = "毛泽东"
|
|
|
DEFAULT_DEMAND_ID = 1
|
|
DEFAULT_DEMAND_ID = 1
|
|
|
|
|
|
|
|
|
|
|
|
@@ -107,10 +137,9 @@ async def run_agent(
|
|
|
|
|
|
|
|
# output 目录(相对路径相对 content_finder)
|
|
# output 目录(相对路径相对 content_finder)
|
|
|
content_finder_root = Path(__file__).resolve().parent
|
|
content_finder_root = Path(__file__).resolve().parent
|
|
|
|
|
+ repo_root = _resolve_repo_root()
|
|
|
output_dir = os.getenv("OUTPUT_DIR", ".cache/output")
|
|
output_dir = os.getenv("OUTPUT_DIR", ".cache/output")
|
|
|
- output_dir_path = Path(output_dir).expanduser()
|
|
|
|
|
- if not output_dir_path.is_absolute():
|
|
|
|
|
- output_dir_path = (content_finder_root / output_dir_path).resolve()
|
|
|
|
|
|
|
+ output_dir_path = _resolve_dir_from_env(repo_root, output_dir)
|
|
|
|
|
|
|
|
# 构建消息(替换 %query%、%output_dir%、%demand_id%)
|
|
# 构建消息(替换 %query%、%output_dir%、%demand_id%)
|
|
|
demand_id_str = str(demand_id) if demand_id is not None else ""
|
|
demand_id_str = str(demand_id) if demand_id is not None else ""
|
|
@@ -131,9 +160,10 @@ async def run_agent(
|
|
|
|
|
|
|
|
skills_dir = str(Path(__file__).parent / "skills")
|
|
skills_dir = str(Path(__file__).parent / "skills")
|
|
|
|
|
|
|
|
- Path(trace_dir).mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
|
+ trace_dir_path = _resolve_dir_from_env(repo_root, trace_dir)
|
|
|
|
|
+ trace_dir_path.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
|
|
- store = FileSystemTraceStore(base_path=trace_dir)
|
|
|
|
|
|
|
+ store = FileSystemTraceStore(base_path=str(trace_dir_path))
|
|
|
|
|
|
|
|
allowed_tools = [
|
|
allowed_tools = [
|
|
|
"douyin_search",
|
|
"douyin_search",
|
|
@@ -182,10 +212,6 @@ async def run_agent(
|
|
|
execution_id = str(uuid.uuid4())
|
|
execution_id = str(uuid.uuid4())
|
|
|
|
|
|
|
|
try:
|
|
try:
|
|
|
- log_dir = _resolve_input_log_dir(content_finder_root)
|
|
|
|
|
- log_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
- log_file_path = log_dir / f"run_log_{datetime.now().strftime('%Y%m%d_%H%M%S')}.txt"
|
|
|
|
|
-
|
|
|
|
|
run_result: Optional[Dict[str, Any]] = None
|
|
run_result: Optional[Dict[str, Any]] = None
|
|
|
|
|
|
|
|
with build_log(execution_id) as log_buffer:
|
|
with build_log(execution_id) as log_buffer:
|
|
@@ -222,6 +248,13 @@ async def run_agent(
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
full_log = log_buffer.getvalue()
|
|
full_log = log_buffer.getvalue()
|
|
|
|
|
+ log_file_path = _resolve_log_file_path(
|
|
|
|
|
+ content_finder_root=content_finder_root,
|
|
|
|
|
+ output_dir_path=output_dir_path,
|
|
|
|
|
+ trace_id=trace_id,
|
|
|
|
|
+ execution_id=execution_id,
|
|
|
|
|
+ )
|
|
|
|
|
+ log_file_path.parent.mkdir(parents=True, exist_ok=True)
|
|
|
with open(log_file_path, "w", encoding="utf-8") as f:
|
|
with open(log_file_path, "w", encoding="utf-8") as f:
|
|
|
f.write(full_log)
|
|
f.write(full_log)
|
|
|
|
|
|