47 lines
1.5 KiB
Python
47 lines
1.5 KiB
Python
|
|
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
|
||
|
|
from schemas.automation import AutomationJobConfig, MessageContextConfig
|
||
|
|
|
||
|
|
_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"
|
||
|
|
)
|
||
|
|
return config
|