feat;私聊回滚wait,添加记忆总结模型配置
This commit is contained in:
@@ -164,22 +164,14 @@ class SummaryImporter:
|
||||
def _pick_default_summary_task(self, available_tasks: Dict[str, TaskConfig]) -> Tuple[Optional[str], Optional[TaskConfig]]:
|
||||
"""
|
||||
选择总结默认任务,避免错误落到 embedding 任务。
|
||||
优先级:replyer > utils > planner > tool_use > 其他非 embedding。
|
||||
优先级:memory > utils > planner;不再顺延到 replyer 或其他任务。
|
||||
"""
|
||||
preferred = ("replyer", "utils", "planner", "tool_use")
|
||||
preferred = ("memory", "utils", "planner")
|
||||
for name in preferred:
|
||||
cfg = available_tasks.get(name)
|
||||
if cfg and cfg.model_list:
|
||||
return name, cfg
|
||||
|
||||
for name, cfg in available_tasks.items():
|
||||
if name != "embedding" and cfg.model_list:
|
||||
return name, cfg
|
||||
|
||||
for name, cfg in available_tasks.items():
|
||||
if cfg.model_list:
|
||||
return name, cfg
|
||||
|
||||
return None, None
|
||||
|
||||
def _resolve_summary_model_config(self) -> Optional[TaskConfig]:
|
||||
@@ -187,6 +179,7 @@ class SummaryImporter:
|
||||
解析 summarization.model_name 为 TaskConfig。
|
||||
支持:
|
||||
- "auto"
|
||||
- "memory"(任务名)
|
||||
- "replyer"(任务名)
|
||||
- "some-model-name"(具体模型名)
|
||||
- ["utils:model1", "utils:model2", "replyer"](数组混合语法)
|
||||
@@ -199,7 +192,7 @@ class SummaryImporter:
|
||||
# 避免默认值本身触发类型校验异常。
|
||||
raw_cfg = self.plugin_config.get("summarization", {}).get("model_name", ["auto"])
|
||||
selectors = self._normalize_summary_model_selectors(raw_cfg)
|
||||
default_task_name, default_task_cfg = self._pick_default_summary_task(available_tasks)
|
||||
_default_task_name, default_task_cfg = self._pick_default_summary_task(available_tasks)
|
||||
|
||||
selected_models: List[str] = []
|
||||
base_cfg: Optional[TaskConfig] = None
|
||||
@@ -262,16 +255,11 @@ class SummaryImporter:
|
||||
_append_models(default_task_cfg.model_list)
|
||||
if base_cfg is None:
|
||||
base_cfg = default_task_cfg
|
||||
else:
|
||||
first_cfg = next(iter(available_tasks.values()))
|
||||
_append_models(first_cfg.model_list)
|
||||
if base_cfg is None:
|
||||
base_cfg = first_cfg
|
||||
|
||||
if not selected_models:
|
||||
return None
|
||||
|
||||
template_cfg = base_cfg or default_task_cfg or next(iter(available_tasks.values()))
|
||||
template_cfg = base_cfg or default_task_cfg or TaskConfig()
|
||||
return TaskConfig(
|
||||
model_list=selected_models,
|
||||
max_tokens=template_cfg.max_tokens,
|
||||
|
||||
@@ -58,7 +58,7 @@ LEGACY_ENV_PATH: Path = (PROJECT_ROOT / ".env").resolve().absolute()
|
||||
A_MEMORIX_LEGACY_CONFIG_PATH: Path = (CONFIG_DIR / "a_memorix.toml").resolve().absolute()
|
||||
MMC_VERSION: str = "1.0.0-pre.14"
|
||||
CONFIG_VERSION: str = "8.10.15"
|
||||
MODEL_CONFIG_VERSION: str = "1.16.0"
|
||||
MODEL_CONFIG_VERSION: str = "1.16.1"
|
||||
|
||||
logger = get_logger("config")
|
||||
|
||||
|
||||
@@ -25,6 +25,13 @@ DEFAULT_TASK_CONFIG_TEMPLATES: dict[str, dict[str, Any]] = {
|
||||
"slow_threshold": 15.0,
|
||||
"selection_strategy": "random",
|
||||
},
|
||||
"memory": {
|
||||
"model_list": [],
|
||||
"max_tokens": 4096,
|
||||
"temperature": 0.5,
|
||||
"slow_threshold": 30.0,
|
||||
"selection_strategy": "random",
|
||||
},
|
||||
"replyer": {
|
||||
"model_list": ["deepseek-v4-pro-think", "deepseek-v4-pro-nonthink"],
|
||||
"max_tokens": 4096,
|
||||
|
||||
@@ -440,6 +440,16 @@ class ModelTaskConfig(ConfigBase):
|
||||
)
|
||||
"""规划模型配置"""
|
||||
|
||||
memory: TaskConfig = Field(
|
||||
default_factory=TaskConfig,
|
||||
json_schema_extra={
|
||||
"x-widget": "custom",
|
||||
"x-icon": "brain",
|
||||
"advanced": True,
|
||||
},
|
||||
)
|
||||
"""记忆模型配置,用于长期记忆总结、抽取、写回等高质量记忆任务;留空时由调用方按需回退"""
|
||||
|
||||
utils: TaskConfig = Field(
|
||||
default_factory=TaskConfig,
|
||||
json_schema_extra={
|
||||
|
||||
@@ -30,6 +30,8 @@ from .tool_search import get_tool_spec as get_tool_search_tool_spec
|
||||
from .tool_search import handle_tool as handle_tool_search_tool
|
||||
from .view_complex_message import get_tool_spec as get_view_complex_message_tool_spec
|
||||
from .view_complex_message import handle_tool as handle_view_complex_message_tool
|
||||
from .wait import get_tool_spec as get_wait_tool_spec
|
||||
from .wait import handle_tool as handle_wait_tool
|
||||
|
||||
BuiltinToolHandler = Callable[[ToolInvocation, Optional[ToolExecutionContext]], Awaitable[ToolExecutionResult]]
|
||||
BuiltinToolRawHandler = Callable[
|
||||
@@ -70,6 +72,7 @@ def _get_query_memory_tool_spec() -> ToolSpec:
|
||||
BUILTIN_TOOL_ENTRIES: List[BuiltinToolEntry] = [
|
||||
BuiltinToolEntry("no_reply", get_no_reply_tool_spec, handle_no_reply_tool, stage="timing"),
|
||||
BuiltinToolEntry("continue", get_continue_tool_spec, handle_continue_tool, stage="timing"),
|
||||
BuiltinToolEntry("wait", get_wait_tool_spec, handle_wait_tool, stage="timing", chat_scope="private"),
|
||||
BuiltinToolEntry("finish", get_finish_tool_spec, handle_finish_tool, stage="action"),
|
||||
BuiltinToolEntry("reply", get_reply_tool_spec, handle_reply_tool, stage="action"),
|
||||
BuiltinToolEntry(
|
||||
@@ -145,12 +148,12 @@ def get_all_builtin_tool_specs(context: Optional[ToolAvailabilityContext] = None
|
||||
return [entry.build_spec() for entry in _get_builtin_tool_entries(context=context)]
|
||||
|
||||
|
||||
def get_timing_tools() -> List[ToolDefinitionInput]:
|
||||
def get_timing_tools(context: Optional[ToolAvailabilityContext] = None) -> List[ToolDefinitionInput]:
|
||||
"""获取 Timing Gate 阶段的兼容工具定义。"""
|
||||
|
||||
tool_specs = [
|
||||
entry.build_spec()
|
||||
for entry in _get_builtin_tool_entries(stage="timing", visibility="visible")
|
||||
for entry in _get_builtin_tool_entries(stage="timing", visibility="visible", context=context)
|
||||
]
|
||||
return [tool_spec.to_llm_definition() for tool_spec in tool_specs if tool_spec.enabled]
|
||||
|
||||
|
||||
51
src/maisaka/builtin_tool/wait.py
Normal file
51
src/maisaka/builtin_tool/wait.py
Normal file
@@ -0,0 +1,51 @@
|
||||
"""wait 内置工具。"""
|
||||
|
||||
from typing import Optional
|
||||
|
||||
from src.core.tooling import ToolExecutionContext, ToolExecutionResult, ToolInvocation, ToolSpec
|
||||
|
||||
from .context import BuiltinToolRuntimeContext
|
||||
|
||||
|
||||
def get_tool_spec() -> ToolSpec:
|
||||
"""获取 wait 工具声明。"""
|
||||
|
||||
return ToolSpec(
|
||||
name="wait",
|
||||
brief_description="暂停当前私聊对话并固定等待一段时间,期间不因新消息提前恢复。",
|
||||
detailed_description="参数说明:\n- seconds:integer,必填。等待的秒数。等待期间收到的新消息只会暂存,直到超时后再继续处理。",
|
||||
parameters_schema={
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"seconds": {
|
||||
"type": "integer",
|
||||
"description": "等待的秒数。",
|
||||
},
|
||||
},
|
||||
"required": ["seconds"],
|
||||
},
|
||||
provider_name="maisaka_builtin",
|
||||
provider_type="builtin",
|
||||
)
|
||||
|
||||
|
||||
async def handle_tool(
|
||||
tool_ctx: BuiltinToolRuntimeContext,
|
||||
invocation: ToolInvocation,
|
||||
context: Optional[ToolExecutionContext] = None,
|
||||
) -> ToolExecutionResult:
|
||||
"""执行 wait 内置工具。"""
|
||||
|
||||
del context
|
||||
seconds = invocation.arguments.get("seconds", 30)
|
||||
try:
|
||||
wait_seconds = int(seconds)
|
||||
except (TypeError, ValueError):
|
||||
wait_seconds = 30
|
||||
wait_seconds = max(0, wait_seconds)
|
||||
tool_ctx.runtime._enter_wait_state(seconds=wait_seconds, tool_call_id=invocation.call_id)
|
||||
return tool_ctx.build_success_result(
|
||||
invocation.tool_name,
|
||||
f"当前私聊对话循环进入等待状态,将固定等待 {wait_seconds} 秒;期间收到的新消息不会提前打断本次等待。",
|
||||
metadata={"pause_execution": True},
|
||||
)
|
||||
@@ -40,7 +40,7 @@ from .history_utils import drop_orphan_tool_results, normalize_tool_result_order
|
||||
from .display.prompt_cli_renderer import PromptCLIVisualizer
|
||||
from .visual_mode_utils import resolve_enable_visual_planner
|
||||
|
||||
TIMING_GATE_TOOL_NAMES = {"continue", "no_reply"}
|
||||
TIMING_GATE_TOOL_NAMES = {"continue", "no_reply", "wait"}
|
||||
REQUEST_TYPE_BY_REQUEST_KIND = {
|
||||
"planner": "maisaka_planner",
|
||||
"timing_gate": "maisaka_timing_gate",
|
||||
@@ -362,6 +362,7 @@ class MaisakaChatLoopService:
|
||||
"file_tools_section": tools_section,
|
||||
"group_chat_attention_block": self._build_group_chat_attention_block(),
|
||||
"identity": self._personality_prompt,
|
||||
"timing_gate_wait_rule": self._build_timing_gate_wait_rule(),
|
||||
"time_block": self._build_time_block(),
|
||||
}
|
||||
|
||||
@@ -398,6 +399,15 @@ class MaisakaChatLoopService:
|
||||
|
||||
return "在该聊天中的注意事项:\n" + "\n\n".join(prompt_lines) + "\n"
|
||||
|
||||
def _build_timing_gate_wait_rule(self) -> str:
|
||||
"""构造 Timing Gate 中 wait 工具的场景说明。"""
|
||||
|
||||
if self._is_group_chat is True:
|
||||
return ""
|
||||
if self._is_group_chat is False:
|
||||
return "- wait:固定再等待一段时间,时间到后再重新判断"
|
||||
return "- wait:仅私聊可用;如果当前是群聊,不要调用"
|
||||
|
||||
@staticmethod
|
||||
def _get_chat_prompt_for_chat(chat_id: str, is_group_chat: Optional[bool]) -> str:
|
||||
"""根据聊天流 ID 获取匹配的额外提示。"""
|
||||
|
||||
@@ -55,7 +55,7 @@ logger = get_logger("maisaka_reasoning_engine")
|
||||
|
||||
TIMING_GATE_CONTEXT_DROP_HEAD_RATIO = 0.7
|
||||
TIMING_GATE_MAX_ATTEMPTS = 3
|
||||
TIMING_GATE_TOOL_NAMES = {"continue", "no_reply"}
|
||||
TIMING_GATE_TOOL_NAMES = {"continue", "no_reply", "wait"}
|
||||
HISTORY_SILENT_TOOL_NAMES = {"finish"}
|
||||
|
||||
|
||||
@@ -143,30 +143,13 @@ class MaisakaReasoningEngine:
|
||||
tool_definitions=tool_definitions,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _build_timing_gate_fallback_prompt() -> str:
|
||||
"""构造 Timing Gate 子代理的兜底提示词。"""
|
||||
|
||||
return (
|
||||
"你是 Maisaka 的 timing gate 子代理,只负责决定当前会话下一步的节奏控制。\n"
|
||||
"你必须且只能调用一个工具,不要输出普通文本答案。\n"
|
||||
"可用工具只有两个:\n"
|
||||
"1. no_reply: 适合当前不继续本轮发言,等待新的外部消息或让用户继续说完。\n"
|
||||
"2. continue: 适合现在立刻进入下一轮正常思考、回复、查询和其他工具执行。\n"
|
||||
"如果需要真正回复消息、查询信息或使用其他工具,应该调用 continue,让主分支继续执行,而不是在这里完成。\n"
|
||||
"不要连续调用多个工具,也不要输出工具之外的计划。"
|
||||
)
|
||||
|
||||
def _build_timing_gate_system_prompt(self) -> str:
|
||||
"""构造 Timing Gate 子代理使用的系统提示词。"""
|
||||
|
||||
try:
|
||||
return load_prompt(
|
||||
"maisaka_timing_gate",
|
||||
**self._runtime._chat_loop_service.build_prompt_template_context(),
|
||||
)
|
||||
except Exception:
|
||||
return self._build_timing_gate_fallback_prompt()
|
||||
return load_prompt(
|
||||
"maisaka_timing_gate",
|
||||
**self._runtime._chat_loop_service.build_prompt_template_context(),
|
||||
)
|
||||
|
||||
async def _build_action_tool_definitions(self) -> tuple[list[dict[str, Any]], str]:
|
||||
"""构造 Action Loop 阶段可见的工具定义与 deferred tools 提示。"""
|
||||
@@ -242,7 +225,7 @@ class MaisakaReasoningEngine:
|
||||
async def _run_timing_gate(
|
||||
self,
|
||||
anchor_message: SessionMessage,
|
||||
) -> tuple[Literal["continue", "no_reply"], Any, list[str], list[dict[str, Any]]]:
|
||||
) -> tuple[Literal["continue", "no_reply", "wait"], Any, list[str], list[dict[str, Any]]]:
|
||||
"""运行 Timing Gate 子代理并返回控制决策。"""
|
||||
|
||||
if self._runtime._force_next_timing_continue:
|
||||
@@ -254,13 +237,19 @@ class MaisakaReasoningEngine:
|
||||
selected_tool_call: Optional[ToolCall] = None
|
||||
invalid_tool_text = ""
|
||||
for attempt_index in range(TIMING_GATE_MAX_ATTEMPTS):
|
||||
timing_tool_definitions = get_timing_tools(self._build_tool_availability_context())
|
||||
available_timing_tool_names = {
|
||||
str(tool_definition.get("name") or "").strip()
|
||||
for tool_definition in timing_tool_definitions
|
||||
if str(tool_definition.get("name") or "").strip()
|
||||
}
|
||||
response = await self._run_timing_gate_sub_agent(
|
||||
system_prompt=self._build_timing_gate_system_prompt(),
|
||||
tool_definitions=get_timing_tools(),
|
||||
tool_definitions=timing_tool_definitions,
|
||||
)
|
||||
selected_tool_call = None
|
||||
for tool_call in response.tool_calls:
|
||||
if tool_call.func_name in TIMING_GATE_TOOL_NAMES:
|
||||
if tool_call.func_name in available_timing_tool_names:
|
||||
selected_tool_call = tool_call
|
||||
break
|
||||
|
||||
@@ -332,7 +321,12 @@ class MaisakaReasoningEngine:
|
||||
self._append_timing_gate_execution_result(response, selected_tool_call, result)
|
||||
|
||||
timing_action = str(result.metadata.get("timing_action") or selected_tool_call.func_name).strip()
|
||||
if timing_action not in TIMING_GATE_TOOL_NAMES:
|
||||
available_timing_action_names = {
|
||||
str(tool_definition.get("name") or "").strip()
|
||||
for tool_definition in get_timing_tools(self._build_tool_availability_context())
|
||||
if str(tool_definition.get("name") or "").strip()
|
||||
}
|
||||
if timing_action not in available_timing_action_names:
|
||||
logger.warning(
|
||||
f"{self._runtime.log_prefix} Timing Gate 返回未知动作 {timing_action!r},将按 no_reply 处理"
|
||||
)
|
||||
@@ -388,7 +382,7 @@ class MaisakaReasoningEngine:
|
||||
hint_content = (
|
||||
"Timing Gate 上一轮选择了非法工具:"
|
||||
f"{normalized_tool_text}。\n"
|
||||
"Timing Gate 只能调用 continue 或 no_reply 中的一个工具。"
|
||||
"Timing Gate 只能调用当前可用的 continue、no_reply 或 wait 中的一个工具。"
|
||||
)
|
||||
self._runtime._chat_history.append(
|
||||
SessionBackedMessage(
|
||||
@@ -419,7 +413,12 @@ class MaisakaReasoningEngine:
|
||||
try:
|
||||
while self._runtime._running:
|
||||
queued_trigger = await self._runtime._internal_turn_queue.get()
|
||||
message_triggered = self._drain_ready_turn_triggers(queued_trigger)
|
||||
message_triggered, timeout_triggered = self._drain_ready_turn_triggers(queued_trigger)
|
||||
|
||||
if self._runtime._agent_state == self._runtime._STATE_WAIT and not timeout_triggered:
|
||||
self._runtime._message_turn_scheduled = False
|
||||
logger.debug(f"{self._runtime.log_prefix} 当前仍处于 wait 状态,忽略消息触发并继续等待超时")
|
||||
continue
|
||||
|
||||
if message_triggered:
|
||||
await self._runtime._wait_for_message_quiet_period()
|
||||
@@ -430,17 +429,30 @@ class MaisakaReasoningEngine:
|
||||
if self._runtime._has_pending_messages()
|
||||
else []
|
||||
)
|
||||
if not cached_messages:
|
||||
continue
|
||||
if cached_messages:
|
||||
self._runtime._agent_state = self._runtime._STATE_RUNNING
|
||||
self._runtime._update_stage_status(
|
||||
"消息整理",
|
||||
f"待处理消息 {len(cached_messages)} 条",
|
||||
)
|
||||
asyncio.create_task(self._runtime._trigger_batch_learning(cached_messages))
|
||||
if timeout_triggered:
|
||||
self._runtime._chat_history.append(
|
||||
self._build_wait_completed_message(has_new_messages=True)
|
||||
)
|
||||
await self._ingest_messages(cached_messages)
|
||||
anchor_message = cached_messages[-1]
|
||||
else:
|
||||
anchor_message = self._get_timeout_anchor_message()
|
||||
if anchor_message is None:
|
||||
logger.warning(f"{self._runtime.log_prefix} wait 超时后没有可复用的锚点消息,跳过本轮")
|
||||
continue
|
||||
logger.info(f"{self._runtime.log_prefix} 等待超时后开始新一轮思考")
|
||||
if self._runtime._pending_wait_tool_call_id:
|
||||
self._runtime._chat_history.append(
|
||||
self._build_wait_completed_message(has_new_messages=False)
|
||||
)
|
||||
|
||||
self._runtime._agent_state = self._runtime._STATE_RUNNING
|
||||
self._runtime._update_stage_status(
|
||||
"消息整理",
|
||||
f"待处理消息 {len(cached_messages)} 条",
|
||||
)
|
||||
asyncio.create_task(self._runtime._trigger_batch_learning(cached_messages))
|
||||
await self._ingest_messages(cached_messages)
|
||||
anchor_message = cached_messages[-1]
|
||||
try:
|
||||
timing_gate_required = True
|
||||
round_index = 0
|
||||
@@ -492,7 +504,7 @@ class MaisakaReasoningEngine:
|
||||
timing_tool_monitor_results,
|
||||
) = await self._run_timing_gate(anchor_message)
|
||||
timing_elapsed_seconds = time.time() - timing_started_at
|
||||
if timing_action != "continue":
|
||||
if timing_action == "no_reply":
|
||||
await self._runtime._wait_for_timing_gate_non_continue_cooldown(
|
||||
timing_elapsed_seconds
|
||||
)
|
||||
@@ -511,8 +523,12 @@ class MaisakaReasoningEngine:
|
||||
)
|
||||
timing_gate_required = self._mark_timing_gate_completed(timing_action)
|
||||
if timing_action != "continue":
|
||||
cycle_end_reason = "timing_no_reply"
|
||||
cycle_end_detail = "Timing Gate 选择 no_reply,本轮不会进入 Planner。"
|
||||
if timing_action == "wait":
|
||||
cycle_end_reason = "timing_wait"
|
||||
cycle_end_detail = "Timing Gate 选择 wait,本轮不会进入 Planner,将在等待结束后继续。"
|
||||
else:
|
||||
cycle_end_reason = "timing_no_reply"
|
||||
cycle_end_detail = "Timing Gate 选择 no_reply,本轮不会进入 Planner。"
|
||||
logger.debug(
|
||||
f"{self._runtime.log_prefix} Timing Gate 结束当前回合: "
|
||||
f"回合={round_index + 1} 动作={timing_action}"
|
||||
@@ -757,11 +773,12 @@ class MaisakaReasoningEngine:
|
||||
|
||||
def _drain_ready_turn_triggers(
|
||||
self,
|
||||
queued_trigger: Literal["message"],
|
||||
) -> bool:
|
||||
queued_trigger: Literal["message", "timeout"],
|
||||
) -> tuple[bool, bool]:
|
||||
"""合并当前已就绪的消息触发信号。"""
|
||||
|
||||
message_triggered = queued_trigger == "message"
|
||||
timeout_triggered = queued_trigger == "timeout"
|
||||
|
||||
while True:
|
||||
try:
|
||||
@@ -772,8 +789,33 @@ class MaisakaReasoningEngine:
|
||||
if next_trigger == "message":
|
||||
message_triggered = True
|
||||
continue
|
||||
if next_trigger == "timeout":
|
||||
timeout_triggered = True
|
||||
continue
|
||||
|
||||
return message_triggered
|
||||
return message_triggered, timeout_triggered
|
||||
|
||||
def _get_timeout_anchor_message(self) -> Optional[SessionMessage]:
|
||||
"""在 wait 超时后复用最近一条真实用户消息作为锚点。"""
|
||||
if self._runtime.message_cache:
|
||||
return self._runtime.message_cache[-1]
|
||||
return None
|
||||
|
||||
def _build_wait_completed_message(self, *, has_new_messages: bool) -> ToolResultMessage:
|
||||
"""构造 wait 完成后的工具结果消息。"""
|
||||
tool_call_id = self._runtime._pending_wait_tool_call_id or "wait_timeout"
|
||||
self._runtime._pending_wait_tool_call_id = None
|
||||
content = (
|
||||
"等待已结束,期间收到了新的用户输入。请结合这些新消息继续下一轮思考。"
|
||||
if has_new_messages
|
||||
else "等待已超时,期间没有收到新的用户输入。请基于现有上下文继续下一轮思考。"
|
||||
)
|
||||
return ToolResultMessage(
|
||||
content=content,
|
||||
timestamp=datetime.now(),
|
||||
tool_call_id=tool_call_id,
|
||||
tool_name="wait",
|
||||
)
|
||||
|
||||
async def _ingest_messages(self, messages: list[SessionMessage]) -> None:
|
||||
"""处理传入消息列表,将其转换为历史消息并加入聊天历史缓存。"""
|
||||
|
||||
@@ -62,6 +62,7 @@ class MaisakaHeartFlowChatting:
|
||||
"""会话级别的 Maisaka 运行时。"""
|
||||
|
||||
_STATE_RUNNING: Literal["running"] = "running"
|
||||
_STATE_WAIT: Literal["wait"] = "wait"
|
||||
_STATE_STOP: Literal["stop"] = "stop"
|
||||
|
||||
def __init__(self, session_id: str):
|
||||
@@ -84,7 +85,7 @@ class MaisakaHeartFlowChatting:
|
||||
# Keep all original messages for batching and later learning.
|
||||
self.message_cache: list[SessionMessage] = []
|
||||
self._last_processed_index = 0
|
||||
self._internal_turn_queue: asyncio.Queue[Literal["message"]] = asyncio.Queue()
|
||||
self._internal_turn_queue: asyncio.Queue[Literal["message", "timeout"]] = asyncio.Queue()
|
||||
|
||||
self._mcp_manager: Optional[MCPManager] = None
|
||||
self._mcp_host_bridge: Optional[MCPHostLLMBridge] = None
|
||||
@@ -102,6 +103,7 @@ class MaisakaHeartFlowChatting:
|
||||
self._talk_frequency_adjust = 1.0
|
||||
self._reply_latency_measurement_started_at: Optional[float] = None
|
||||
self._recent_reply_latencies: deque[tuple[float, float]] = deque()
|
||||
self._wait_timeout_task: Optional[asyncio.Task[None]] = None
|
||||
self._max_internal_rounds = MAX_INTERNAL_ROUNDS
|
||||
configured_context_size = (
|
||||
global_config.chat.max_context_size
|
||||
@@ -109,7 +111,8 @@ class MaisakaHeartFlowChatting:
|
||||
else global_config.chat.max_private_context_size
|
||||
)
|
||||
self._max_context_size = max(1, int(configured_context_size))
|
||||
self._agent_state: Literal["running", "stop"] = self._STATE_STOP
|
||||
self._agent_state: Literal["running", "wait", "stop"] = self._STATE_STOP
|
||||
self._pending_wait_tool_call_id: Optional[str] = None
|
||||
self._force_next_timing_continue = False
|
||||
self._force_next_timing_message_id = ""
|
||||
self._force_next_timing_reason = ""
|
||||
@@ -208,6 +211,7 @@ class MaisakaHeartFlowChatting:
|
||||
self._message_turn_scheduled = False
|
||||
self._message_debounce_required = False
|
||||
self._cancel_deferred_message_turn_task()
|
||||
self._cancel_wait_timeout_task()
|
||||
while not self._internal_turn_queue.empty():
|
||||
_ = self._internal_turn_queue.get_nowait()
|
||||
|
||||
@@ -938,6 +942,9 @@ class MaisakaHeartFlowChatting:
|
||||
|
||||
def _schedule_message_turn(self) -> None:
|
||||
"""为当前待处理消息安排一次内部 turn。"""
|
||||
if self._agent_state == self._STATE_WAIT:
|
||||
return
|
||||
|
||||
if not self._has_pending_messages() or self._message_turn_scheduled:
|
||||
return
|
||||
|
||||
@@ -1023,6 +1030,48 @@ class MaisakaHeartFlowChatting:
|
||||
def _enter_stop_state(self) -> None:
|
||||
"""切换到停止状态。"""
|
||||
self._agent_state = self._STATE_STOP
|
||||
self._pending_wait_tool_call_id = None
|
||||
self._cancel_wait_timeout_task()
|
||||
|
||||
def _enter_wait_state(self, seconds: Optional[float] = None, tool_call_id: Optional[str] = None) -> None:
|
||||
"""切换到等待状态。"""
|
||||
self._agent_state = self._STATE_WAIT
|
||||
self._pending_wait_tool_call_id = tool_call_id
|
||||
self._message_turn_scheduled = False
|
||||
self._cancel_deferred_message_turn_task()
|
||||
self._cancel_wait_timeout_task()
|
||||
if seconds is not None:
|
||||
self._wait_timeout_task = asyncio.create_task(
|
||||
self._schedule_wait_timeout(seconds=seconds, tool_call_id=tool_call_id)
|
||||
)
|
||||
|
||||
def _cancel_wait_timeout_task(self) -> None:
|
||||
"""取消当前 wait 对应的超时任务。"""
|
||||
if self._wait_timeout_task is None:
|
||||
return
|
||||
self._wait_timeout_task.cancel()
|
||||
self._wait_timeout_task = None
|
||||
|
||||
async def _schedule_wait_timeout(self, seconds: float, tool_call_id: Optional[str]) -> None:
|
||||
"""在 wait 到期后向内部循环投递 timeout 触发。"""
|
||||
try:
|
||||
if seconds > 0:
|
||||
await asyncio.sleep(seconds)
|
||||
if not self._running:
|
||||
return
|
||||
if self._agent_state != self._STATE_WAIT:
|
||||
return
|
||||
if self._pending_wait_tool_call_id != tool_call_id:
|
||||
return
|
||||
|
||||
logger.debug(f"{self.log_prefix} Maisaka 等待已超时")
|
||||
self._agent_state = self._STATE_RUNNING
|
||||
await self._internal_turn_queue.put("timeout")
|
||||
except asyncio.CancelledError:
|
||||
return
|
||||
finally:
|
||||
if self._wait_timeout_task is not None and self._pending_wait_tool_call_id == tool_call_id:
|
||||
self._wait_timeout_task = None
|
||||
|
||||
async def _trigger_batch_learning(self, messages: list[SessionMessage]) -> None:
|
||||
"""按同一批消息触发表达方式和黑话学习。"""
|
||||
|
||||
Reference in New Issue
Block a user