Skip to main content

Client library for AI agent runtime communication over WebSocket

Project description

runtimeuse (Python)

Python client library for communicating with a runtimeuse agent runtime over WebSocket.

Handles the WebSocket connection lifecycle, message dispatch, artifact upload handshake, cancellation, and structured result parsing -- so you can focus on what to do with agent results rather than wire protocol details.

Installation

pip install runtimeuse

Quick Start

Start the runtime inside any sandbox, then connect from outside:

import asyncio
from runtimeuse import RuntimeUseClient, InvocationMessage, ResultMessageInterface

async def main():
    # Start the runtime in a sandbox (provider-specific)
    sandbox = Sandbox.create()
    sandbox.run("npx -y runtimeuse")
    ws_url = sandbox.get_url(8080)

    # Connect and invoke
    client = RuntimeUseClient(ws_url=ws_url)

    invocation = InvocationMessage(
        message_type="invocation_message",
        source_id="my-run-001",
        system_prompt="You are a helpful assistant.",
        user_prompt="Do the thing and return the result.",
        output_format_json_schema_str='{"type":"json_schema","schema":{"type":"object"}}',
        secrets_to_redact=["sk-secret-key"],
        agent_env={"API_KEY": "sk-secret-key"},
    )

    async def on_result(result: ResultMessageInterface):
        print(f"Success: {result.structured_output.get('success')}")
        print(f"Output: {result.structured_output}")

    await client.invoke(
        invocation=invocation,
        on_result_message=on_result,
        result_message_cls=ResultMessageInterface,
    )

asyncio.run(main())

For local development without a sandbox, connect directly:

client = RuntimeUseClient(ws_url="ws://localhost:8080")

Usage

RuntimeUseClient

Manages the WebSocket connection to the agent runtime and runs the message loop: sends an invocation, iterates the response stream, and dispatches typed messages to your callbacks.

client = RuntimeUseClient(ws_url="ws://localhost:8080")

await client.invoke(
    invocation=invocation,
    on_result_message=on_result,
    result_message_cls=ResultMessageInterface,
    on_assistant_message=on_assistant,       # optional
    on_artifact_upload_request=on_artifact,  # optional -- return (presigned_url, content_type)
    on_error_message=on_error,               # optional
    is_cancelled=check_cancelled,            # optional -- async () -> bool
    timeout=300,                             # optional -- seconds
)

Artifact Upload Handshake

When the agent runtime requests an artifact upload, provide a callback that returns a presigned URL and content type. The client sends the response back automatically.

async def on_artifact(request: ArtifactUploadRequestMessageInterface) -> tuple[str, str]:
    presigned_url = await my_storage.create_presigned_url(request.filename)
    content_type = guess_content_type(request.filename)
    return presigned_url, content_type

Cancellation

Pass an is_cancelled callback to cancel a running invocation. When it returns True, the client sends a cancel message to the runtime and raises CancelledException.

from runtimeuse import CancelledException

async def check_cancelled() -> bool:
    return await db.is_run_cancelled(run_id)

try:
    await client.invoke(
        invocation=invocation,
        on_result_message=on_result,
        result_message_cls=ResultMessageInterface,
        is_cancelled=check_cancelled,
    )
except CancelledException:
    print("Run was cancelled")

Custom Result Types

Subclass ResultMessageInterface to add domain-specific fields:

from runtimeuse import ResultMessageInterface

class MyResultMessage(ResultMessageInterface):
    custom_score: float | None = None

await client.invoke(
    invocation=invocation,
    on_result_message=handle_my_result,
    result_message_cls=MyResultMessage,
)

API Reference

Message Types

Class Description
InvocationMessage Sent to the runtime to start an agent invocation
ResultMessageInterface Structured result from the agent
AssistantMessageInterface Intermediate assistant text messages
ArtifactUploadRequestMessageInterface Runtime requesting a presigned URL for artifact upload
ArtifactUploadResponseMessageInterface Response with presigned URL sent back to runtime
ErrorMessageInterface Error from the agent runtime
CancelMessage Sent to cancel a running invocation
CommandInterface Pre/post invocation shell command
RuntimeEnvironmentDownloadableInterface File to download into the runtime before invocation

Exceptions

Class Description
CancelledException Raised when is_cancelled() returns True

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

runtimeuse_client-0.4.0.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.

runtimeuse_client-0.4.0-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file runtimeuse_client-0.4.0.tar.gz.

File metadata

  • Download URL: runtimeuse_client-0.4.0.tar.gz
  • Upload date:
  • Size: 26.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for runtimeuse_client-0.4.0.tar.gz
Algorithm Hash digest
SHA256 185c5b7f2c54af75acb55ddaadbab92d381f9b7f9c323a01f1688efd3075a6a3
MD5 0bf7f6e4407bd0d3fde251346804d704
BLAKE2b-256 22e71fc23798793c209588e0e2f93e0f1d20b5ccd1481a39ae43cb54089a17d5

See more details on using hashes here.

Provenance

The following attestation bundles were made for runtimeuse_client-0.4.0.tar.gz:

Publisher: publish-runtimeuse-client-python.yml on getlark/runtimeuse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file runtimeuse_client-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for runtimeuse_client-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fcb72d343537657792032548363db020fecc9871e7b617f94ce79289b18d8769
MD5 d90f149355ae0a0271113d35e5a8c0ba
BLAKE2b-256 e4e7548b82f2285305c56e81b96aea493c8659fbf97980b12bc63d864257e8f5

See more details on using hashes here.

Provenance

The following attestation bundles were made for runtimeuse_client-0.4.0-py3-none-any.whl:

Publisher: publish-runtimeuse-client-python.yml on getlark/runtimeuse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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