Add your description here
Project description
OpenCode Client
A Python client for interacting with the OpenCode server, enabling programmatic access to AI-powered coding sessions with file operations and conversation history.
Installation
pip install opencode-client
Quick Start
import asyncio
from opencode_client import OpenCodeClient
async def main():
# Initialize client
client = OpenCodeClient(
base_url="http://localhost:8080",
model_provider="anthropic",
model="claude-sonnet-4",
timeout=600,
)
# Create a session
session = await client.create_current_session(title="My Coding Session")
# Send a message
response = await client.send_message(
"Write a hello world function in Python"
)
print(response.to_string())
asyncio.run(main())
Features
- Session Management: Create, list, update, and delete coding sessions
- Message Handling: Send text and file-based messages with system prompts
- File Operations: Search files by name or content, read directories, check file status
- Chat History: Automatic tracking of conversation history
Usage Examples
Session Management
# Create a new session
session = await client.create_current_session(title="Debug Session")
# List all sessions
sessions = await client.list_sessions()
# Update session title
await client.update_session(title="New Title")
# Abort running session
await client.abort_session()
# Delete session
await client.delete_session()
Sending Messages
# Simple text message
response = await client.send_message("Explain async/await in Python")
# Message with system prompt
response = await client.send_message(
"Write tests for this function",
system_message="You are an expert in pytest",
)
# Message with file attachment
response = await client.send_message(
"Review this code", file="path/to/script.py"
)
# Message with multiple files
response = await client.send_message(
"Compare these implementations", file=["version1.py", "version2.py"]
)
# Message with URL
response = await client.send_message(
"Analyze this code", file="https://example.com/code.py"
)
File Operations
# Search files by name
files = await client.search_file_by_name("README")
# Search files by content
matches = await client.search_file_by_text("def main")
# Read directory contents
file_info = await client.read_directory_files("/path/to/dir")
# Get file status
status = await client.get_files_status()
Chat History
# Access structured chat history
for message in client.chat_history:
print(message)
# Access string representation of chat history
for msg_string in client.string_chat_history:
print(msg_string)
Configuration
Client Parameters
base_url: API endpoint URLmodel_provider: Provider name (e.g., "anthropic", "openai")model: Model identifier (e.g., "claude-sonnet-4")timeout: Request timeout in seconds (default: 600)
Advanced Usage
Working with Multiple Sessions
# Create parent session
parent = await client.create_current_session(title="Parent Session")
# Create child session
child = await client.create_current_session(
title="Child Session", parent_id=parent.id
)
# Send message to specific session
response = await client.send_message(
"Help with this bug", session_id=parent.id
)
Error Handling
try:
response = await client.send_message("Your question")
except ValueError as e:
print(f"Invalid input: {e}")
except httpx.HTTPError as e:
print(f"API error: {e}")
Contributing
We welcome contributions! Please read our Contributing Guide to get started.
License
This project is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file opencode_client-0.1.0.tar.gz.
File metadata
- Download URL: opencode_client-0.1.0.tar.gz
- Upload date:
- Size: 24.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.22
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
148adcfb1a78edad1ffd67cbfaf7c54421df5d51494c5561a595f742fd5e378b
|
|
| MD5 |
8bf8a9fb4f1605639517b7cabf7521c0
|
|
| BLAKE2b-256 |
2e10afe21a8d3a69546a919156863f5de46f5b8716eedb2cedbf9637fd55a84e
|
File details
Details for the file opencode_client-0.1.0-py3-none-any.whl.
File metadata
- Download URL: opencode_client-0.1.0-py3-none-any.whl
- Upload date:
- Size: 3.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.22
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
05b13fae17699c6a3494d2d5dd19c712fc51e6a8d7f679d959b04b9737aa880f
|
|
| MD5 |
317d89c53a822b287984eeb6206397c5
|
|
| BLAKE2b-256 |
4b13c9ad63acbe539f531978526faa54be10339336dd52ad760cd64fb973c823
|