diff --git a/agent.py b/agent.py index 26f6fc0..9a970cd 100644 --- a/agent.py +++ b/agent.py @@ -12,43 +12,71 @@ class Agent: self.mcp = MCPClient(MCP_URL) self.tools = [] + self.messages = [ { "role": "system", "content": [ { - "text": "Du bist N.O.R.A, ein Smart-Home Assistent. Nutze Tools wenn nötig." + "text": ( + "Du bist N.O.R.A, ein Smart-Home Assistent. " + "Du steuerst Geräte über Tools. " + "Nutze Tools wenn sinnvoll." + ) } ] } ] + # ------------------------- + # TOOL LOADING + # ------------------------- async def load_tools(self): - res = await self.mcp.list_tools() + try: + res = await self.mcp.list_tools() - self.tools = [] - - for t in res.tools: - self.tools.append({ - "toolSpec": { - "name": t.name, - "description": t.description, - "inputSchema": t.inputSchema + self.tools = [ + { + "toolSpec": { + "name": t.name, + "description": t.description, + "inputSchema": t.inputSchema + } } - }) + for t in res.tools + ] - self.logger.info(f"{len(self.tools)} Tools geladen") + self.logger.info(f"{len(self.tools)} Tools geladen") + except Exception as e: + self.logger.error(f"Tool Load Error: {e}") + self.tools = [] + + # ------------------------- + # TOOL EXECUTION + # ------------------------- async def _run_tool(self, tool): - name = tool["name"] - args = tool.get("input", {}) + try: + name = tool.get("name") + args = tool.get("input", {}) or {} - self.logger.info(f"Tool Call: {name} -> {args}") + self.logger.info(f"Tool Call → {name} | args={args}") - result = await self.mcp.call_tool(name, args) + result = await self.mcp.call_tool(name, args) - return result + # MCP result stabilisieren + if hasattr(result, "content"): + return result.content + return result + + except Exception as e: + self.logger.error(f"Tool Error ({tool.get('name')}): {e}") + return f"ERROR: {e}" + + # ------------------------- + # MAIN LOOP + # ------------------------- async def run(self, user_input: str): self.messages.append({ @@ -58,28 +86,42 @@ class Agent: for _ in range(8): - response = await self.llm.chat( - messages=self.messages, - tools=self.tools - ) + try: + response = await self.llm.chat( + messages=self.messages, + tools=self.tools + ) + + except Exception as e: + self.logger.error(f"LLM Error: {e}") + return f"LLM Fehler: {e}" output = response.get("output", {}) message = output.get("message", {}) content = message.get("content", []) tool_uses = [] - final_text = "" + final_text = [] + # ------------------------- + # PARSE RESPONSE SAFE + # ------------------------- for c in content: + tool = c.get("toolUse") if tool: tool_uses.append(tool) if "text" in c: - final_text += c["text"] + final_text.append(c["text"]) + # ------------------------- + # TOOL EXECUTION PATH + # ------------------------- if tool_uses: + self.logger.info(f"{len(tool_uses)} Tool(s) detected") + self.messages.append({ "role": "assistant", "content": content @@ -88,11 +130,12 @@ class Agent: tool_results = [] for tool in tool_uses: + result = await self._run_tool(tool) tool_results.append({ "toolResult": { - "toolUseId": tool["toolUseId"], + "toolUseId": tool.get("toolUseId"), "content": [ { "text": str(result) @@ -108,12 +151,18 @@ class Agent: continue + # ------------------------- + # FINAL RESPONSE + # ------------------------- if final_text: + + text = "\n".join(final_text) + self.messages.append({ "role": "assistant", - "content": [{"text": final_text}] + "content": [{"text": text}] }) - return final_text + return text return "Tool Loop Limit erreicht" \ No newline at end of file