added sandbox and bash tool
This commit is contained in:
+46
-9
@@ -1,15 +1,16 @@
|
||||
import asyncio
|
||||
from http.client import responses
|
||||
|
||||
from anthropic import AsyncAnthropic
|
||||
|
||||
from agent.config import settings
|
||||
from agent.history import ConversationHistory
|
||||
from agent.tools import TOOL_SCHEMAS, dispatch_tool
|
||||
|
||||
client = AsyncAnthropic(api_key=settings.anthropic_api_key)
|
||||
history = ConversationHistory()
|
||||
|
||||
|
||||
async def run_turn(user_message: str, history: list[dict] = None) -> str:
|
||||
async def run_turn(user_message: str, history: list[dict] = None, sandbox=None) -> str:
|
||||
|
||||
if history is None:
|
||||
history = []
|
||||
@@ -17,21 +18,57 @@ async def run_turn(user_message: str, history: list[dict] = None) -> str:
|
||||
# add the new user message to history
|
||||
messages = history + [{"role": "user", "content": user_message}]
|
||||
|
||||
message = await client.messages.create(
|
||||
response = await client.messages.create(
|
||||
model=settings.model,
|
||||
max_tokens=settings.max_tokens,
|
||||
tools=TOOL_SCHEMAS,
|
||||
messages=messages,
|
||||
)
|
||||
|
||||
return message
|
||||
while response.stop_reason == "tool_use":
|
||||
tool_results = []
|
||||
for block in response.content:
|
||||
if block.type == "tool_use":
|
||||
result = await dispatch_tool(
|
||||
tool_name=block.name, tool_input=block.input, sandbox=sandbox
|
||||
)
|
||||
tool_results.append(
|
||||
{"type": "tool_result", "tool_use_id": block.id, "content": result}
|
||||
)
|
||||
|
||||
messages = messages + [
|
||||
{"role": "assistant", "content": response.content},
|
||||
{"role": "user", "content": tool_results},
|
||||
]
|
||||
|
||||
response = await client.messages.create(
|
||||
model=settings.model,
|
||||
max_tokens=settings.max_tokens,
|
||||
tools=TOOL_SCHEMAS,
|
||||
messages=messages,
|
||||
)
|
||||
|
||||
return next(block.text for block in response.content if hasattr(block, "text"))
|
||||
|
||||
|
||||
async def run_session():
|
||||
async def run_session(sandbox=None):
|
||||
"""simple CLI session - temporary until TUI is built"""
|
||||
history = ConversationHistory()
|
||||
|
||||
print("Codeing agent ready. Type /quit to quit.")
|
||||
|
||||
while True:
|
||||
user_input = input("You: ")
|
||||
user_input = input("You: ").strip()
|
||||
# __ UI commands_______
|
||||
if not user_input:
|
||||
continue
|
||||
if user_input == "/quit":
|
||||
print("Goodbye")
|
||||
break
|
||||
|
||||
history.add_message("user", user_input)
|
||||
|
||||
response = await run_turn(user_input, history.get_all())
|
||||
history.add_message("assistant", response.content[0].text)
|
||||
response = await run_turn(user_input, history.get_all(), sandbox)
|
||||
history.add_message("assistant", response)
|
||||
|
||||
print(f"Assistant: {response.content[0].text}")
|
||||
print(f"\nAssistant: {response}")
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
from tools.bash import bash
|
||||
|
||||
TOOL_SCHEMAS = [
|
||||
{
|
||||
"name": "bash",
|
||||
"description": "Execute a bash command in the isolated sandbox environment. Use this to run shell commands, install packages, run scripts, etc.",
|
||||
"input_schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"command": {
|
||||
"type": "string",
|
||||
"description": "The bash command to execute (e.g., 'ls -la', 'python script.py', 'pip install requests')",
|
||||
}
|
||||
},
|
||||
"required": ["command"],
|
||||
},
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
async def dispatch_tool(tool_name: str, tool_input: dict, sandbox) -> str:
|
||||
"""Route tool calls to implementations."""
|
||||
|
||||
if tool_name == "bash":
|
||||
return await bash(command=tool_input["command"], sandbox=sandbox)
|
||||
|
||||
return f"Unknown tool: {tool_name}"
|
||||
Reference in New Issue
Block a user