diff options
| author | batsumaru <> | 2026-09-15 15:13:34 +0900 |
|---|---|---|
| committer | batsumaru <> | 2026-09-15 15:13:34 +0900 |
| commit | 1a67c5edb6fa6165e99cc25ed380303ed817af09 (patch) | |
| tree | d2eab77e7275a559bfd639fd80b15321b5e514f3 /agent.py | |
| parent | 5629cabbb38c7d91e66a419ba7cb3a93a9a988a1 (diff) | |
Cleaned up project tree
Diffstat (limited to 'agent.py')
| -rw-r--r-- | agent.py | 128 |
1 files changed, 0 insertions, 128 deletions
diff --git a/agent.py b/agent.py deleted file mode 100644 index a79fd74..0000000 --- a/agent.py +++ /dev/null @@ -1,128 +0,0 @@ -import json - -from context import ContextBuilder -from identity import AgentIdentity, UserProfile -from llm import LLM -from memory import WorkingMemory -from tools import ( - MemoryAddFactTool, - MemoryAddNoteTool, - MemoryRemoveFactTool, - MemoryRemoveNoteTool, - TaskStartTool, - ToolError, - ToolRegistry, -) - - -class Agent: - def __init__( - self, - llm: LLM, - agent_identity: AgentIdentity, - user_profile: UserProfile, - tools: ToolRegistry, - ) -> None: - self.llm = llm - self.tools = tools - - self.context = ContextBuilder( - agent_identity, - user_profile, - ) - - self.working_memory = WorkingMemory() - - self.tools.register(MemoryAddFactTool(self.working_memory)) - self.tools.register(MemoryRemoveFactTool(self.working_memory)) - self.tools.register(MemoryRemoveNoteTool(self.working_memory)) - self.tools.register(MemoryAddNoteTool(self.working_memory)) - self.tools.register(TaskStartTool(self.working_memory)) - - self.messages: list[dict[str, object]] = [] - - def chat(self, user_input: str) -> str: - print(f"\n[memory: {self.working_memory}]") - self.messages.append( - { - "role": "user", - "content": user_input, - } - ) - - for _ in range(5): - response = self.llm.chat( - self.context.build_messages( - self.messages, - self.working_memory, - ), - tools=self.tools.schemas(), - ) - - if response.tool_calls: - print(f"\n[tool call: {response.tool_calls}]\n") - - if not response.tool_calls: - answer = response.content or "" - - self.messages.append( - { - "role": "assistant", - "content": answer, - } - ) - - return answer - - self.messages.append( - { - "role": "assistant", - "content": response.content, - "tool_calls": [ - { - "id": call.id, - "type": "function", - "function": { - "name": call.name, - "arguments": json.dumps(call.arguments), - }, - } - for call in response.tool_calls - ], - } - ) - - for call in response.tool_calls: - tool = self.tools.get(call.name) - - if tool is None: - result = f"Unknown tool: {call.name}" - else: - try: - result = tool.execute(call.arguments).content - except ToolError as error: - result = f"Tool error: {error}" - - self.messages.append( - { - "role": "tool", - "tool_call_id": call.id, - "content": result, - } - ) - raise RuntimeError("Tool call limit exceeded") - - def end_task(self) -> None: - self.working_memory.end_task() - - def start_task(self, objective: str) -> None: - self.working_memory.start_task(objective) - - def pause_task(self) -> None: - self.working_memory.pause_task() - - def resume_task(self) -> None: - self.working_memory.resume_task() - - def complete_task(self) -> None: - self.working_memory.complete_task() |
