File size: 3,371 Bytes
0af0679
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""
Tool definitions and handlers for Claude
"""
import json
from .notification import push

# Tool functions that Claude can call
def record_user_details(email, name="Name not provided", notes="not provided"):
    """Record user contact information when they express interest"""
    push(f"Recording {name} with email {email} and notes {notes}")
    return {"recorded": "ok"}

def record_unknown_question(question):
    """Record questions that couldn't be answered"""
    push(f"Recording unknown question: {question}")
    return {"recorded": "ok"}

def search_faq(query):
    """Search the FAQ for a question or topic"""
    push(f"Searching FAQ for: {query}")
    return {"search_results": "ok"}

# Tool definitions in the format Claude expects
tool_schemas = [
    {
        "name": "record_user_details",
        "description": "Use this tool to record that a user is interested in being in touch and provided an email address",
        "input_schema": {
            "type": "object",
            "properties": {
                "email": {"type": "string", "description": "The email address of this user"},
                "name": {"type": "string", "description": "The user's name, if they provided it"},
                "notes": {"type": "string", "description": "Any additional context from the conversation"}
            },
            "required": ["email"]
        }
    },
    {
        "name": "record_unknown_question",
        "description": "Use this tool to record any question that couldn't be answered",
        "input_schema": {
            "type": "object",
            "properties": {
                "question": {"type": "string", "description": "The question that couldn't be answered"}
            },
            "required": ["question"]
        }
    },
    {
        "name": "search_faq",
        "description": "Searches a list of frequently asked questions.",
        "input_schema": {
            "type": "object",
            "properties": {
                "query": {"type": "string", "description": "The user's question or topic to search for in the FAQ."}
            },
            "required": ["query"]
        }
    }
]

# Map of tool names to functions
tool_functions = {
    "record_user_details": record_user_details,
    "record_unknown_question": record_unknown_question,
    "search_faq": search_faq
}

def handle_tool_calls(tool_calls):
    """Process tool calls from Claude and execute the appropriate functions"""
    results = []
    for tool_call in tool_calls:
        tool_name = tool_call.name
        arguments = tool_call.input  # This is already a dict
        print(f"Tool called: {tool_name}", flush=True)
                
        # Get the function from tool_functions and call it with the arguments
        tool_func = tool_functions.get(tool_name)
        if tool_func:
            result = tool_func(**arguments)
        else:
            print(f"No function found for tool: {tool_name}")
            result = {"error": f"Tool {tool_name} not found"}
        
        # Format the result for Claude's response
        results.append({
            "role": "user", 
            "content": [
                {
                    "type": "tool_result",
                    "tool_use_id": tool_call.id,
                    "content": json.dumps(result)
                }
            ]
        })
    return results