JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.
Python
feat: 支持向小艺claw更新使用工具的状态
jingjinduo84091d2147b9
diff --git a/jiuwenclaw/agentserver/react_agent.py b/jiuwenclaw/agentserver/react_agent.pyindex f00bbcb78a..44d2616217 100644--- a/jiuwenclaw/agentserver/react_agent.py+++ b/jiuwenclaw/agentserver/react_agent.py@@ -610,6 +610,17 @@ class JiuClawReActAgent(ReActAgent):# Handle auto-scan evolution after answerhistory = self._pending_auto_evolution_history++ # If no auto evolution, send processing_complete directly+ if history is None and self._evolution_service is not None and session is not None:+ await session.write_stream(+ OutputSchema(+ type="processing_complete",+ index=0,+ payload={},+ )+ )+if history is not None and self._evolution_service is not None and session is not None:# Signal frontend that main processing is done before evolution starts,# so new user input is treated as a normal submit (not interrupt).@@ -809,6 +820,13 @@ class JiuClawReActAgent(ReActAgent):},))+ await session.write_stream(+ OutputSchema(+ type="thinking",+ index=0,+ payload={},+ )+ )except Exception:logger.debug("tool_result emit failed", exc_info=True)diff --git a/jiuwenclaw/channel/xiaoyi_channel.py b/jiuwenclaw/channel/xiaoyi_channel.pyindex 6e92c3e0a1..d0347afdd2 100644--- a/jiuwenclaw/channel/xiaoyi_channel.py+++ b/jiuwenclaw/channel/xiaoyi_channel.py@@ -23,6 +23,11 @@ from jiuwenclaw.utils import loggerfrom jiuwenclaw.channel.base import BaseChannel, ChannelMetadata, RobotMessageRouterfrom jiuwenclaw.schema.message import EventType, Message, ReqMethodfrom jiuwenclaw.channel.xiaoyi_utils.push import XiaoYiPushService, PushConfig+from jiuwenclaw.channel.xiaoyi_utils.formatter import (+ get_status_state_for_event,+ get_status_text_for_event,+ should_send_as_status_update,+)# 全局 XiaoyiChannel 实例引用(供手机端工具调用使用)@@ -366,6 +371,15 @@ class XiaoyiChannel(BaseChannel):logger.warning(f"XiaoyiChannel 发送文件响应失败 ({url_key}): {e}")return+ if should_send_as_status_update(msg.event_type):+ status_text = get_status_text_for_event(msg.event_type, msg.payload)+ status_state = get_status_state_for_event(msg.event_type, msg.payload)+ for url_key in list(self._ws_connections.keys()):+ await self._send_status_update_with_state(+ task_id, session_id, status_text, status_state, url_key+ )+ return+content = ""cron_job_name = ""if isinstance(msg.payload, dict):@@ -743,7 +757,7 @@ class XiaoyiChannel(BaseChannel):if self._is_session_waiting_for_push(session_id, task_id):break# Send status update- await self._send_status_update(task_id, session_id, "任务正在处理中,请稍后")+ await self._send_status_update(task_id, session_id, "任务正在处理中,请稍后~")except asyncio.CancelledError:pass@@ -829,6 +843,28 @@ class XiaoyiChannel(BaseChannel):for url_key in list(self._ws_connections.keys()):await self._send_agent_response(session_id, task_id, response, url_key)+ async def _send_status_update_with_state(+ self, task_id: str, session_id: str, message: str, state: str, url_key: str+ ) -> None:+ """发送状态更新消息(A2A 格式),支持自定义状态."""+ response = {+ "jsonrpc": "2.0",+ "id": f"msg_{int(time.time() * 1000)}",+ "result": {+ "taskId": task_id,+ "kind": "status-update",+ "final": False,+ "status": {+ "message": {+ "role": "agent",+ "parts": [{"kind": "text", "text": message}],+ },+ "state": state,+ },+ },+ }+ await self._send_agent_response(session_id, task_id, response, url_key)+def _is_session_active(self, session_id: str) -> bool:"""检查会话是否有活跃任务."""return session_id in self._session_activediff --git a/jiuwenclaw/channel/xiaoyi_utils/formatter.py b/jiuwenclaw/channel/xiaoyi_utils/formatter.pyindex 83b3678c71..98ec90d525 100644--- a/jiuwenclaw/channel/xiaoyi_utils/formatter.py+++ b/jiuwenclaw/channel/xiaoyi_utils/formatter.py@@ -553,20 +553,24 @@ def should_send_as_status_update(event_type: EventType | None) -> bool:# Status update 用于以下事件:# - CHAT_TOOL_CALL: 工具调用+ # - CHAT_TOOL_RESULT: 工具结果+ # - CHAT_PROCESSING_STATUS: 处理状态status_events = {EventType.CHAT_TOOL_CALL,- EventType.CHAT_TOOL_RESULT+ EventType.CHAT_TOOL_RESULT,+ EventType.CHAT_PROCESSING_STATUS,}return event_type in status_events-def get_status_state_for_event(event_type: EventType | None) -> str:+def get_status_state_for_event(event_type: EventType | None, payload: dict | None = None) -> str:"""根据事件类型获取状态值。Args:event_type: 事件类型+ payload: 消息载荷Returns:str: 状态值@@ -574,9 +578,15 @@ def get_status_state_for_event(event_type: EventType | None) -> str:if event_type is None:return "unknown"+ if event_type == EventType.CHAT_PROCESSING_STATUS:+ if payload and isinstance(payload, dict):+ is_processing = payload.get("is_processing", True)+ return "working" if is_processing else "completed"+ return "working"+status_map = {EventType.CHAT_TOOL_CALL: "working",- EventType.CHAT_PROCESSING_STATUS: "working",+ EventType.CHAT_TOOL_RESULT: "working",EventType.CHAT_FINAL: "completed",EventType.CHAT_ERROR: "failed",}@@ -598,14 +608,29 @@ def get_status_text_for_event(event_type: EventType | None, payload: dict | Noneif event_type is None:return "处理中"- if payload:- # 尝试从 payload 中提取自定义状态文本- if isinstance(payload, dict):- content = payload.get("content", "")- if isinstance(content, str) and content:- return content- if isinstance(content, dict):- return content.get("output", "处理中")+ if payload and isinstance(payload, dict):+ if event_type == EventType.CHAT_TOOL_CALL:+ tool_call = payload.get("tool_call", {})+ if isinstance(tool_call, dict):+ tool_name = tool_call.get("name", "")+ if tool_name:+ return f"正在使用工具:{tool_name}"+ return "正在使用工具..."+ if event_type == EventType.CHAT_TOOL_RESULT:+ tool_name = payload.get("tool_name", "")+ if tool_name:+ return f"工具 {tool_name} 执行完成"+ return "工具执行完成"+ if event_type == EventType.CHAT_PROCESSING_STATUS:+ is_processing = payload.get("is_processing", True)+ if is_processing:+ return "任务正在处理中,请稍后~"+ return "任务处理已完成~"+ content = payload.get("content", "")+ if isinstance(content, str) and content:+ return content+ if isinstance(content, dict):+ return content.get("output", "处理中")status_text_map = {EventType.CHAT_TOOL_CALL: "正在使用工具...",