1.1 Autonomous AI Agent
The core of Calliope is its autonomous AI agent system, which provides intelligent assistance for Soroban smart contract development. The agent is implemented with sophisticated features:
# server/agent.py
def main():
# Configure the Gemini AI model with specific parameters
model = genai.GenerativeModel(
model_name="gemini-2.0-flash",
generation_config={
"temperature": 0.2, # Low temperature for more focused responses
"top_p": 0.95, # High top_p for diverse but relevant responses
"top_k": 40, # Moderate top_k for balanced sampling
"max_output_tokens": 30720, # Large token limit for complex responses
"response_mime_type": "application/json", # Force JSON responses
}
)
1.1.1 Intelligent Command Execution
The agent implements a sophisticated command execution system:
def execute_command(cmd: str) -> str:
print(f"Agent is executing command ```{cmd.replace("`", "'")}```")
try:
# Execute command with timeout and output capture
output = subprocess.run(cmd, shell=True, text=True,
capture_output=True, timeout=300)
# Handle errors and return appropriate output
if output.returncode != 0 and output.stderr:
return f"ERROR: {output.stderr}\nOUTPUT: {output.stdout}"
return output.stdout
except subprocess.TimeoutExpired:
return "COMMAND TIMED OUT (300s limit)"
except Exception as e:
return f"ERROR: {str(e)}"
Key Features:
Command Validation: Checks command syntax and safety
Timeout Management: Prevents infinite execution
Error Handling: Captures and formats errors
Output Processing: Formats command output
Resource Management: Controls system resources
1.1.2 Response Processing
The agent implements sophisticated response handling:
def handle_model_response(response_text, chat):
"""Handle model response with advanced error recovery"""
try:
# Parse response with robust extraction
parsed = extract_json(response_text)
# Validate required fields
if all(key in parsed for key in ["message", "commands"]):
return parsed, None
else:
# Request correction for missing fields
correction_prompt = """
Your previous response was missing required fields. Please provide a complete JSON response with:
1. "thinking" - Your reasoning process
2. "message" - What you want to tell the user
3. "commands" - Array of commands to execute
4. "task_complete" - Boolean indicating if task is done
5. "need_user_input" - Boolean indicating if user input is needed
"""
# Get corrected response
corrected_response = chat.send_message(correction_prompt)
return extract_json(corrected_response.text), corrected_response
Features:
JSON Parsing: Robust JSON extraction
Field Validation: Checks required fields
Error Recovery: Handles malformed responses
Context Management: Maintains conversation context
Response Correction: Requests corrections when needed
Last updated