agent.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. """Agent loop — the core processing engine."""
  2. from __future__ import annotations
  3. import json
  4. import re
  5. from datetime import datetime
  6. from pathlib import Path
  7. from typing import Any, Awaitable, Callable
  8. from toyclaw.context import ContextBuilder
  9. from toyclaw.provider import LLMResponse, OpenAIProvider
  10. from toyclaw.session import Session, SessionManager
  11. from toyclaw.skills import SkillsLoader
  12. from toyclaw.tools.base import ToolRegistry
  13. _TOOL_RESULT_MAX = 500 # chars to keep per tool result in session history
  14. class Agent:
  15. """The agent loop: receive message → build context → LLM ↔ tools → respond.
  16. This is the heart of ToyClaw. Everything else feeds into or out of it.
  17. """
  18. def __init__(
  19. self,
  20. provider: OpenAIProvider,
  21. tools: ToolRegistry,
  22. workspace: Path,
  23. session_mgr: SessionManager | None = None,
  24. max_iterations: int = 30,
  25. memory_window: int = 100,
  26. on_output: Callable[[str], Awaitable[None]] | None = None,
  27. ):
  28. self.provider = provider
  29. self.tools = tools
  30. self.workspace = workspace
  31. self.sessions = session_mgr or SessionManager(workspace)
  32. self.max_iterations = max_iterations
  33. self.memory_window = memory_window
  34. self.on_output = on_output # callback for async output (subagent results, cron)
  35. self._ctx = ContextBuilder(workspace)
  36. self._skills = SkillsLoader(workspace)
  37. # ------------------------------------------------------------------
  38. # Public API
  39. # ------------------------------------------------------------------
  40. async def process(
  41. self,
  42. content: str,
  43. session_key: str = "cli:direct",
  44. channel: str = "cli",
  45. chat_id: str = "direct",
  46. ) -> str:
  47. """Process a single user message and return the final reply."""
  48. # Set tool contexts
  49. self._set_tool_context(channel, chat_id)
  50. session = self.sessions.get_or_create(session_key)
  51. history = session.get_history(max_messages=self.memory_window)
  52. print("\n history: " + str(history))
  53. skills_summary = self._skills.build_summary()
  54. messages = self._ctx.build_messages(
  55. history=history,
  56. user_message=content,
  57. channel=channel,
  58. chat_id=chat_id,
  59. skills_summary=skills_summary,
  60. )
  61. print("\n messages: " + str(messages))
  62. final, _, all_msgs = await self._run_loop(messages)
  63. if final is None:
  64. final = "I've completed processing but have no response to give."
  65. self._save_turn(session, all_msgs, skip=1 + len(history))
  66. self.sessions.save(session)
  67. return final
  68. # ------------------------------------------------------------------
  69. # Agent loop
  70. # ------------------------------------------------------------------
  71. async def _run_loop(
  72. self, messages: list[dict[str, Any]]
  73. ) -> tuple[str | None, list[str], list[dict[str, Any]]]:
  74. """Run the LLM ↔ tool iteration loop.
  75. Returns (final_content, tools_used, messages).
  76. """
  77. tools_used: list[str] = []
  78. final: str | None = None
  79. for _iter in range(self.max_iterations):
  80. response = await self.provider.chat(
  81. messages=messages,
  82. tools=self.tools.get_definitions(),
  83. )
  84. print("\n response: " + str(response))
  85. if response.has_tool_calls:
  86. # Append assistant message with tool calls
  87. tc_dicts = [
  88. {
  89. "id": tc.id,
  90. "type": "function",
  91. "function": {
  92. "name": tc.name,
  93. "arguments": json.dumps(tc.arguments, ensure_ascii=False),
  94. },
  95. }
  96. for tc in response.tool_calls
  97. ]
  98. self._ctx.add_assistant_message(messages, response.content, tc_dicts)
  99. # Execute each tool
  100. for tc in response.tool_calls:
  101. tools_used.append(tc.name)
  102. result = await self.tools.execute(tc.name, tc.arguments)
  103. self._ctx.add_tool_result(messages, tc.id, tc.name, result)
  104. else:
  105. # No tool calls → final response
  106. clean = self._strip_think(response.content)
  107. if response.finish_reason == "error":
  108. final = clean or "Sorry, I encountered an error."
  109. break
  110. self._ctx.add_assistant_message(messages, clean)
  111. final = clean
  112. break
  113. if final is None:
  114. final = (
  115. f"Reached the maximum number of iterations ({self.max_iterations}). "
  116. "Try breaking the task into smaller steps."
  117. )
  118. return final, tools_used, messages
  119. # ------------------------------------------------------------------
  120. # Helpers
  121. # ------------------------------------------------------------------
  122. def _set_tool_context(self, channel: str, chat_id: str) -> None:
  123. """Update routing context on tools that need it."""
  124. for name in ("cron", "spawn"):
  125. tool = self.tools.get(name)
  126. if tool and hasattr(tool, "set_context"):
  127. tool.set_context(channel, chat_id)
  128. def _save_turn(
  129. self, session: Session, messages: list[dict[str, Any]], skip: int
  130. ) -> None:
  131. """Persist new messages from this turn into the session."""
  132. now = datetime.now()
  133. for m in messages[skip:]:
  134. entry = dict(m)
  135. role = entry.get("role")
  136. content = entry.get("content")
  137. # Skip empty assistant messages (no content, no tool_calls)
  138. if role == "assistant" and not content and not entry.get("tool_calls"):
  139. continue
  140. # Strip runtime context tag from user messages
  141. if role == "user" and isinstance(content, str):
  142. entry["content"] = ContextBuilder.strip_runtime_tag(content)
  143. if not entry["content"]:
  144. continue
  145. # Truncate oversized tool results
  146. if role == "tool" and isinstance(content, str) and len(content) > _TOOL_RESULT_MAX:
  147. entry["content"] = content[:_TOOL_RESULT_MAX] + "\n... (truncated)"
  148. entry.setdefault("timestamp", now.isoformat())
  149. session.messages.append(entry)
  150. session.updated_at = now
  151. @staticmethod
  152. def _strip_think(text: str | None) -> str | None:
  153. """Remove <think>…</think> blocks that some models embed."""
  154. if not text:
  155. return None
  156. return re.sub(r"<think>[\s\S]*?</think>", "", text).strip() or None