-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
64 lines (56 loc) · 3.3 KB
/
Copy pathapp.py
File metadata and controls
64 lines (56 loc) · 3.3 KB
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
from utils import generate_response, load_messages, insert_message, execute_function, save_messages, insert_tool_message
from tools.other_tools import final_response_description, final_response
from tools.website import website_generator_description, website_generator
from tools.code import claude_code_github_description, claude_code_github
from tools.calculator import python_calculator_description, python_calculator
import hal9 as h9
import os
import sys
# load messages
messages = load_messages()
# load tools
tools_descriptions = [final_response_description, website_generator_description, claude_code_github_description, python_calculator_description]
tools_functions = [final_response, website_generator, claude_code_github, python_calculator]
SYSTEM_PROMPT = """You are Hal9, a helpful and highly capable AI assistant.
Tool routing rules:
1. For greetings, small talk, or anything that doesn't need a specialized capability, call final_response immediately with a friendly, direct reply.
2. When the user wants to build or update a website, use website_generator, then call final_response with a summary.
3. When the user wants to change code in a GitHub repository, use claude_code_github. If they name a fork and a source/upstream repo, set repo to the fork and pr_repo to the upstream so the PR is opened on the source. Then call final_response with the PR URL.
4. For arithmetic, math, unit conversions, or other simple calculations, use python_calculator, then call final_response with the result.
5. Never mention tools or internal processes to the user.
6. If a tool fails, do not retry it blindly — explain the issue and suggest an alternative, then call final_response.
7. Always end by calling final_response with the user-facing answer.
8. Whenever a tool result includes a link to something built or deployed (e.g. a website's path), present it in final_message as a proper markdown link — e.g. `[my-website](/javier/website-1789153067343)` — never as bare/plain text."""
# Always keep the system prompt current so routing fixes apply to existing chats.
if messages and messages[0].get("role") == "system":
messages[0]["content"] = SYSTEM_PROMPT
else:
messages.insert(0, {"role": "system", "content": SYSTEM_PROMPT})
# read input with support for multiple lines
user_input = sys.stdin.read()
h9.event("User Prompt", f"{user_input}")
user_input = user_input.replace("\f", "\n")
messages = insert_message(messages, "user", user_input)
steps = 0
max_steps = 6
completed = False
while steps < max_steps:
steps += 1
response = generate_response(messages, tools_descriptions, tool_choice="required")
response_message = response.choices[0].message
tool_calls = getattr(response_message, "tool_calls", None)
if not tool_calls:
content = (getattr(response_message, "content", None) or "").strip()
if content:
print(content)
messages = insert_message(messages, "assistant", content)
completed = True
break
tool_result = execute_function(response, tools_functions)
insert_tool_message(messages, response, tool_result)
if tool_calls[0].function.name == "final_response":
completed = True
break
if not completed:
print("Unable to generate a satisfactory response in time")
save_messages(messages, file_path="./.storage/.messages.json")