Core message processing loop.
(self)
| 454 | return convert_chat_messages_to_llm_format(self.history_messages) |
| 455 | |
| 456 | async def message_loop(self): |
| 457 | """Core message processing loop.""" |
| 458 | # Send welcome message on new session (no history) |
| 459 | if self.welcome_message and not self.history_messages: |
| 460 | await self.websocket.send_json({"type": "done", "role": "assistant", "content": self.welcome_message}) |
| 461 | |
| 462 | while True: |
| 463 | data = await self.websocket.receive_json() |
| 464 | |
| 465 | # Set a unique trace ID for this specific message processing. |
| 466 | trace_id = str(uuid.uuid4())[:12] |
| 467 | set_trace_id(trace_id) |
| 468 | |
| 469 | content = data.get("content", "") |
| 470 | display_content = data.get("display_content", "") |
| 471 | file_name = data.get("file_name", "") |
| 472 | override_model_id = data.get("model_id") |
| 473 | is_onboarding_trigger = data.get("kind") == "onboarding_trigger" |
| 474 | logger.info(f"[WS] Received: {content[:50]}" + (" [onboarding]" if is_onboarding_trigger else "")) |
| 475 | |
| 476 | if not content and not is_onboarding_trigger: |
| 477 | continue |
| 478 | |
| 479 | if is_onboarding_trigger: |
| 480 | if await self._handle_onboarding_trigger_guard(): |
| 481 | continue |
| 482 | content = "Please begin the onboarding." |
| 483 | |
| 484 | self.current_user_text = content |
| 485 | effective_llm_model = await self._resolve_effective_model(override_model_id) |
| 486 | |
| 487 | # Quota Checks |
| 488 | if not await self._check_quotas(): |
| 489 | continue |
| 490 | |
| 491 | # Add user message to in-memory context |
| 492 | self.conversation.append({"role": "user", "content": content}) |
| 493 | |
| 494 | # Save user message to DB |
| 495 | await self._save_user_message(content, display_content, file_name, is_onboarding_trigger) |
| 496 | |
| 497 | # OpenClaw routing check |
| 498 | if self.agent_type == "openclaw": |
| 499 | await self._route_openclaw(content) |
| 500 | continue |
| 501 | |
| 502 | # Detect task creation intent |
| 503 | task_match = re.search( |
| 504 | r"(?:创建|新建|添加|建一个|帮我建|create|add)(?:一个|a )?(?:任务|待办|todo|task)[,,:::\\s]*(.+)", |
| 505 | content, |
| 506 | re.IGNORECASE, |
| 507 | ) |
| 508 | |
| 509 | # Invoke LLM and stream response |
| 510 | if effective_llm_model: |
| 511 | assistant_response, thinking_content, queued_messages = await self._run_llm_and_stream( |
| 512 | effective_llm_model, is_onboarding_trigger |
| 513 | ) |
no test coverage detected