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.2.0b0.tar.gz (26.6 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.2.0b0-py3-none-any.whl (3.3 kB view details)

Uploaded Python 3

File details

Details for the file opencode_client-0.2.0b0.tar.gz.

File metadata

  • Download URL: opencode_client-0.2.0b0.tar.gz
  • Upload date:
  • Size: 26.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.3

File hashes

Hashes for opencode_client-0.2.0b0.tar.gz
Algorithm Hash digest
SHA256 ec5d7e640e0778e01e49ca43439e0a858c11f31dbc4f331ecc1597d9cb154a4c
MD5 347f2f91502c843f85edb2e277c0a06f
BLAKE2b-256 61b5d151b472839a7a45b079898f821a4d40044fc591bb402e834db20a999289

See more details on using hashes here.

File details

Details for the file opencode_client-0.2.0b0-py3-none-any.whl.

File metadata

File hashes

Hashes for opencode_client-0.2.0b0-py3-none-any.whl
Algorithm Hash digest
SHA256 74688bf6939f2ba56f2830f9c64c11e51c3c1e6ea3152ca0886983300453eb6a
MD5 045513db25f171f908df41b57c55db6c
BLAKE2b-256 106d0408fbba463527de0606358a222f07b48a83cbed0aa912a49e13867cccdb

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