Skip to main content

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 directory of files
reponse = await client.send_message(
    "What is the content of the `src/client` directory?",
    directory="./src/client",
)

# 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 URL
  • model_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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

opencode_client-0.1.1.tar.gz (24.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

opencode_client-0.1.1-py3-none-any.whl (3.2 kB view details)

Uploaded Python 3

File details

Details for the file opencode_client-0.1.1.tar.gz.

File metadata

  • Download URL: opencode_client-0.1.1.tar.gz
  • Upload date:
  • Size: 24.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.22

File hashes

Hashes for opencode_client-0.1.1.tar.gz
Algorithm Hash digest
SHA256 74a23417ad111bc78d1ce3fe2b13d931d35fcfafbdb4f177ecdb942b125ac7d8
MD5 c8c3ad484687fd80b0c8da1963101635
BLAKE2b-256 4479bac40e07ef8061dd7cd0bfc2d9b0e5fa8eaeffc01fe9aa6d8284913e6976

See more details on using hashes here.

File details

Details for the file opencode_client-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for opencode_client-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 14bd7335faaac5f67dc9801ebceb0989f7047bd83f3ba40d3390b6200671dcb4
MD5 df6e6fa6f29f2db91cd0b79577a49ef2
BLAKE2b-256 3e5e3a344982907363acf0ff98c3c2c839038796c8677541ced14c178fa4be16

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page