diff --git a/src/api/routes.py b/src/api/routes.py index 7f2af90..2778ee6 100644 --- a/src/api/routes.py +++ b/src/api/routes.py @@ -68,13 +68,18 @@ def format_messages_with_tools(messages: list, tools: Optional[list] = None) -> tool_instructions = """You have access to these tools: read: Read a file (filePath) -write: Write to a file (filePath, content) +write: Write to a file (filePath, content) bash: Run a shell command (command) -When you need to use a tool, respond with ONLY this JSON format: -{"tool_calls": [{"id": "call_1", "type": "function", "function": {"name": "TOOL_NAME", "arguments": "{\\"param\\": \\"value\\"}"}}]} +When you need to use a tool, respond with ONLY this format: +TOOL: tool_name +ARGUMENTS: {"param": "value"} -Do not explain - just output the JSON when using tools.""" +Example: +TOOL: read +ARGUMENTS: {"filePath": "hello.txt"} + +Do not explain - just output TOOL: and ARGUMENTS: when using tools.""" # Add to system message or create one has_system = False @@ -264,6 +269,28 @@ def parse_tool_calls(text: str) -> tuple: except Exception as e: pass + # Try new simple format: TOOL: name\nARGUMENTS: {...} + tool_pattern = r'TOOL:\s*(\w+)\s*\nARGUMENTS:\s*(\{[^}]*\})' + tool_match = re.search(tool_pattern, text, re.IGNORECASE) + if tool_match: + tool_name = tool_match.group(1) + args_str = tool_match.group(2) + try: + args_dict = json.loads(args_str) + tool_calls = [{ + "id": "call_1", + "type": "function", + "function": { + "name": tool_name, + "arguments": json.dumps(args_dict) + } + }] + # Extract content before the tool call + content = text[:tool_match.start()].strip() + return content, tool_calls + except json.JSONDecodeError: + pass + # Try alternative format: look for function call patterns # Pattern: function_name(arg1=value1, arg2=value2) func_pattern = r'(\w+)\s*\(([^)]*)\)'