refactor: clean up agent architecture and coordination loop
Squash merged refactoring of agent architecture.
This commit is contained in:
+20
-11
@@ -2,9 +2,10 @@ import asyncio
|
||||
import logging
|
||||
import lmstudio as lms
|
||||
from typing import Any, Callable
|
||||
from core.interfaces import Agent
|
||||
from .prompt import CAVEMAN_PROMPT
|
||||
|
||||
logger: logging.Logger = logging.getLogger("agent-caveman")
|
||||
logger: logging.Logger = logging.getLogger("agent-base")
|
||||
|
||||
|
||||
class _ActResponseCapture:
|
||||
@@ -53,17 +54,17 @@ class _ActResponseCapture:
|
||||
return '\n'.join(self.responses) if self.responses else "No response captured."
|
||||
|
||||
|
||||
class CavemanAgent:
|
||||
"""Caveman AI agent - minimal token usage variant."""
|
||||
class BaseAgent(Agent):
|
||||
"""Base AI agent implementing common LMStudio interaction patterns."""
|
||||
|
||||
def __init__(self, model_name: str) -> None:
|
||||
self.model_name: str = model_name
|
||||
self.model: Any | None = None
|
||||
self.system_prompt: str = CAVEMAN_PROMPT
|
||||
self.system_prompt: str = ""
|
||||
|
||||
async def initialize(self) -> None:
|
||||
"""Initialize the LM Studio model."""
|
||||
logger.info(f"Initializing CavemanAgent with model: {self.model_name}")
|
||||
logger.info(f"Initializing agent with model: {self.model_name}")
|
||||
self.model = lms.llm(self.model_name)
|
||||
|
||||
async def run(self, user_input: str) -> str:
|
||||
@@ -78,12 +79,12 @@ class CavemanAgent:
|
||||
]
|
||||
|
||||
try:
|
||||
logger.info(f"Running CavemanAgent interactively (input length: {len(user_input)})")
|
||||
logger.info(f"Running agent interactively (input length: {len(user_input)})")
|
||||
response = await self.model.respond(user_input, messages=messages)
|
||||
logger.info(f"CavemanAgent responded successfully (response length: {len(response)})")
|
||||
logger.info(f"Agent responded successfully (response length: {len(response)})")
|
||||
return response
|
||||
except Exception as e:
|
||||
logger.error(f"CavemanAgent execution error: {e}")
|
||||
logger.error(f"Agent execution error: {e}")
|
||||
return f"Error in agent execution: {str(e)}"
|
||||
|
||||
async def run_with_tools(self, user_input: str, tools: list[Any]) -> str:
|
||||
@@ -94,14 +95,22 @@ class CavemanAgent:
|
||||
|
||||
try:
|
||||
capture = _ActResponseCapture()
|
||||
logger.info(f"Calling LMStudio act() on CavemanAgent with {len(tools)} tools...")
|
||||
logger.info(f"Calling LMStudio act() on agent with {len(tools)} tools...")
|
||||
result: lms.ActResult = self.model.act(user_input, tools=tools, on_message=capture)
|
||||
logger.info(f"act() on CavemanAgent returned: {result}")
|
||||
logger.info(f"act() on agent returned: {result}")
|
||||
response: str = capture.full_response
|
||||
if not response or response == "No response captured.":
|
||||
logger.warning(f"Act completed with {result.rounds} rounds but no response was captured.")
|
||||
return f"Act completed with {result.rounds} rounds but no response captured."
|
||||
return response
|
||||
except Exception as e:
|
||||
logger.error(f"CavemanAgent tool execution error: {e}")
|
||||
logger.error(f"Agent tool execution error: {e}")
|
||||
return f"Error in agent tool execution: {str(e)}"
|
||||
|
||||
|
||||
class CavemanAgent(BaseAgent):
|
||||
"""Caveman AI agent - minimal token usage variant."""
|
||||
|
||||
def __init__(self, model_name: str) -> None:
|
||||
super().__init__(model_name)
|
||||
self.system_prompt = CAVEMAN_PROMPT
|
||||
|
||||
Reference in New Issue
Block a user