2026-03-23 14:25:47 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from functools import lru_cache
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
import re
|
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
|
|
import yaml
|
|
|
|
|
|
|
|
|
|
from core.agentscope.tools.tool_config import AgentTool
|
2026-03-24 12:38:11 +08:00
|
|
|
from models.automation_jobs import ScheduleType
|
|
|
|
|
from schemas.automation import (
|
|
|
|
|
AutomationJobConfig,
|
|
|
|
|
MessageContextConfig,
|
|
|
|
|
)
|
2026-03-23 14:25:47 +08:00
|
|
|
|
|
|
|
|
_CONFIG_NAME_PATTERN = re.compile(r"^[a-z0-9][a-z0-9_-]{0,63}$")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _automation_yaml_path(config_name: str) -> Path:
|
|
|
|
|
if not _CONFIG_NAME_PATTERN.fullmatch(config_name):
|
|
|
|
|
raise ValueError("invalid automation config name")
|
|
|
|
|
return (
|
|
|
|
|
Path(__file__).resolve().parents[2]
|
|
|
|
|
/ "core"
|
|
|
|
|
/ "config"
|
|
|
|
|
/ "static"
|
|
|
|
|
/ "automation"
|
|
|
|
|
/ f"{config_name}.yaml"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@lru_cache(maxsize=16)
|
|
|
|
|
def load_static_automation_job_config(*, config_name: str) -> AutomationJobConfig:
|
|
|
|
|
path = _automation_yaml_path(config_name)
|
|
|
|
|
with path.open("r", encoding="utf-8") as file:
|
|
|
|
|
loaded: Any = yaml.safe_load(file) or {}
|
|
|
|
|
if not isinstance(loaded, dict):
|
|
|
|
|
raise ValueError(f"invalid automation config format: {path}")
|
|
|
|
|
config = AutomationJobConfig.model_validate(loaded)
|
|
|
|
|
if config_name == "memory_extraction":
|
|
|
|
|
if config.enabled_tools != [AgentTool.MEMORY_WRITE, AgentTool.MEMORY_FORGET]:
|
|
|
|
|
raise ValueError(
|
|
|
|
|
"memory_extraction enabled_tools must be [memory.write, memory.forget]"
|
|
|
|
|
)
|
|
|
|
|
if config.context != MessageContextConfig(window_count=2):
|
|
|
|
|
raise ValueError(
|
|
|
|
|
"memory_extraction context must be latest_chat/day with window_count=2"
|
|
|
|
|
)
|
2026-03-24 12:38:11 +08:00
|
|
|
if config.schedule is None:
|
|
|
|
|
raise ValueError("memory_extraction schedule must be configured")
|
|
|
|
|
if config.schedule.type != ScheduleType.DAILY:
|
|
|
|
|
raise ValueError("memory_extraction schedule type must be daily")
|
2026-03-23 14:25:47 +08:00
|
|
|
return config
|