Zhenran Xu
2026-03-30 0f9a259b487fed51fd781553b3bd279da0bb2a4d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# -*- coding: utf-8 -*-
"""
智能体基类 - 所有阶段智能体的抽象接口
"""
 
import logging
from abc import ABC, abstractmethod
from typing import Any, Optional, Dict, Callable
 
logger = logging.getLogger(__name__)
 
 
class AgentInterface(ABC):
    """所有智能体必须实现的接口"""
 
    def __init__(self, name: str = ""):
        self.name = name
        self.cancellation_check: Optional[Callable] = None
        self.progress_callback: Optional[Callable] = None
 
    def set_cancellation_check(self, fn: Callable):
        self.cancellation_check = fn
 
    def set_progress_callback(self, fn: Callable):
        self.progress_callback = fn
 
    def _report_progress(self, phase: str, step_desc: str, percent: float, data: dict = None):
        if self.progress_callback:
            self.progress_callback(phase, step_desc, percent, data)
 
    def _check_cancel(self):
        if self.cancellation_check and self.cancellation_check():
            raise RuntimeError(f"Agent [{self.name}] cancelled by user")
 
    def _cancellable_query(self, llm, prompt: str, **kwargs):
        """在 LLM 调用前后检查取消状态"""
        self._check_cancel()
        result = llm.query(prompt, **kwargs)
        self._check_cancel()
        return result
 
    # -------- 抽象方法 --------
 
    @abstractmethod
    async def process(self, input_data: Any, intervention: Optional[Dict] = None) -> Dict:
        """
        核心处理逻辑
 
        Args:
            input_data: 来自上一阶段的输入数据
            intervention: 用户介入修改内容
 
        Returns:
            dict: { "payload": ..., "requires_intervention": bool }
        """
        pass