MCPcopy
hub / github.com/HKUDS/DeepCode / _process_system_message

Method _process_system_message

nanobot/nanobot/agent/loop.py:270–362  ·  view source on GitHub ↗

Process a system message (e.g., subagent announce). The chat_id field contains "original_channel:original_chat_id" to route the response back to the correct destination.

(self, msg: InboundMessage)

Source from the content-addressed store, hash-verified

268 )
269
270 async def _process_system_message(self, msg: InboundMessage) -> OutboundMessage | None:
271 """
272 Process a system message (e.g., subagent announce).
273
274 The chat_id field contains "original_channel:original_chat_id" to route
275 the response back to the correct destination.
276 """
277 logger.info(f"Processing system message from {msg.sender_id}")
278
279 # Parse origin from chat_id (format: "channel:chat_id")
280 if ":" in msg.chat_id:
281 parts = msg.chat_id.split(":", 1)
282 origin_channel = parts[0]
283 origin_chat_id = parts[1]
284 else:
285 # Fallback
286 origin_channel = "cli"
287 origin_chat_id = msg.chat_id
288
289 # Use the origin session for context
290 session_key = f"{origin_channel}:{origin_chat_id}"
291 session = self.sessions.get_or_create(session_key)
292
293 # Update tool contexts
294 message_tool = self.tools.get("message")
295 if isinstance(message_tool, MessageTool):
296 message_tool.set_context(origin_channel, origin_chat_id)
297
298 spawn_tool = self.tools.get("spawn")
299 if isinstance(spawn_tool, SpawnTool):
300 spawn_tool.set_context(origin_channel, origin_chat_id)
301
302 cron_tool = self.tools.get("cron")
303 if isinstance(cron_tool, CronTool):
304 cron_tool.set_context(origin_channel, origin_chat_id)
305
306 # Build messages with the announce content
307 messages = self.context.build_messages(
308 history=session.get_history(),
309 current_message=msg.content,
310 channel=origin_channel,
311 chat_id=origin_chat_id,
312 )
313
314 # Agent loop (limited for announce handling)
315 iteration = 0
316 final_content = None
317
318 while iteration < self.max_iterations:
319 iteration += 1
320
321 response = await self.provider.chat(
322 messages=messages, tools=self.tools.get_definitions(), model=self.model
323 )
324
325 if response.has_tool_calls:
326 tool_call_dicts = [
327 {

Callers 1

_process_messageMethod · 0.95

Calls 13

OutboundMessageClass · 0.90
get_or_createMethod · 0.80
getMethod · 0.80
build_messagesMethod · 0.80
get_definitionsMethod · 0.80
add_assistant_messageMethod · 0.80
add_tool_resultMethod · 0.80
add_messageMethod · 0.80
saveMethod · 0.80
set_contextMethod · 0.45
get_historyMethod · 0.45
chatMethod · 0.45

Tested by

no test coverage detected