ai-assistant-discord-bot/examples/basic_usage.py
Claude Discord Bot 6b56463779 Initial commit: Core infrastructure (CRIT-001 through CRIT-005)
Implemented foundational modules for Claude Discord Coordinator:

- Project skeleton with uv (CRIT-003)
- Claude CLI subprocess runner with 11/11 tests passing (CRIT-004)
- SQLite session manager with 27/27 tests passing (CRIT-005)
- Comprehensive test suites for both modules
- Production-ready async/await patterns
- Full type hints and documentation

Technical highlights:
- Validated CLI pattern: claude -p --resume --output-format json
- bypassPermissions requires non-root user (discord-bot)
- WAL mode SQLite for concurrency
- asyncio.Lock for thread safety
- Context manager support

Progress: 5/18 tasks complete (28%)
Week 1: 5/6 complete

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-13 17:55:03 +00:00

87 lines
2.6 KiB
Python

#!/usr/bin/env python3
"""Basic usage example for ClaudeRunner.
Demonstrates:
1. Creating a new Claude session
2. Resuming an existing session with context preservation
3. Error handling and timeout configuration
"""
import asyncio
import sys
from claude_coordinator.claude_runner import ClaudeRunner
async def main():
"""Demonstrate basic ClaudeRunner usage."""
# Initialize runner with 2-minute timeout
runner = ClaudeRunner(default_timeout=120)
print("=" * 60)
print("ClaudeRunner Basic Usage Example")
print("=" * 60)
# Example 1: Create new session
print("\n1. Creating new session...")
response1 = await runner.run(
message="Hello! Please respond with 'Hi there!' and nothing else.",
model="sonnet"
)
if response1.success:
print(f" ✓ Success!")
print(f" Response: {response1.result}")
print(f" Session ID: {response1.session_id}")
print(f" Cost: ${response1.cost:.4f}")
print(f" Duration: {response1.duration_ms}ms")
else:
print(f" ✗ Error: {response1.error}")
sys.exit(1)
# Example 2: Resume session with context
print("\n2. Resuming session with context...")
session_id = response1.session_id
response2 = await runner.run(
message="What did I just ask you to say?",
session_id=session_id,
model="sonnet"
)
if response2.success:
print(f" ✓ Success!")
print(f" Response: {response2.result}")
print(f" Session ID preserved: {response2.session_id == session_id}")
print(f" Cost: ${response2.cost:.4f}")
else:
print(f" ✗ Error: {response2.error}")
sys.exit(1)
# Example 3: Using allowed tools and working directory
print("\n3. Using tool restrictions and working directory...")
response3 = await runner.run(
message="List the files in the current directory",
session_id=session_id,
cwd="/opt/projects/claude-coordinator",
allowed_tools=["Bash", "Read", "Glob"], # No Write or Edit
model="sonnet"
)
if response3.success:
print(f" ✓ Success!")
print(f" Response: {response3.result[:200]}...")
print(f" Cost: ${response3.cost:.4f}")
else:
print(f" ✗ Error: {response3.error}")
print("\n" + "=" * 60)
print("Total cost for this session: ${:.4f}".format(
(response1.cost or 0) + (response2.cost or 0) + (response3.cost or 0)
))
print("=" * 60)
if __name__ == "__main__":
asyncio.run(main())