Skip to main content

Python SDK for Sleigh runtime server.

Project description

Sleigh Python SDK

Python SDK for the Sleigh runtime server.

Two client variants are included:

  • LangChain Tool variant: one-call as_langchain_tool() (returns StructuredTool)
  • MCP variant: expose runtime APIs as MCP tools over stdio

1. Install

pip install sleigh-sdk

Optional extras:

pip install "sleigh-sdk[langchain]"
pip install "sleigh-sdk[mcp]"

2. Base Python Client

from sleigh_sdk import SleighClient

client = SleighClient(base_url="http://127.0.0.1:10122")
session_token = client.create_session_token()["session_token"]
created = client.create_sandbox(session_token=session_token, image="python:3.11-slim")
sandbox_id = created["sandbox_id"]

3. Ordered Workflow (AI Coding)

Run multiple steps in one request and stop early on failure/timeout:

sandbox_id = created["sandbox_id"]
result = client.run_workflow(
    session_token=session_token,
    steps=[
        {"action": "exec_command", "sandbox_id": sandbox_id, "command": "echo hello", "wait": True, "wait_timeout_seconds": 10},
        {"action": "create_snapshot", "sandbox_id": sandbox_id},
        {"action": "exec_command", "sandbox_id": sandbox_id, "command": "uname -a", "wait": True},
    ],
)
print(result["stopped_early"], result["steps"])

Note: in SDK validation, every workflow step must include sandbox_id.


3.1 Exec Completion Webhook Subscription

Subscribe a callback for a specific exec task:

sub = client.subscribe_exec_webhook(
    session_token=session_token,
    sandbox_id=sandbox_id,
    exec_id="exec_xxx",
    webhook_url="https://your-domain/webhook/notify/evt_xxx",
)
print(sub)

Server sends a signed POST when exec reaches terminal state.


4. Sandbox Read API (AI Coding)

read_result = client.read_sandbox(
    session_token=session_token,
    sandbox_id=sandbox_id,
    command="rg",
    args=["TODO", "/workspace"],
    timeout_seconds=10,
    max_output_bytes=65536,
    max_lines=200,
)
print(read_result)

5. Mount + Environment Copy

List available mount workspace directories first:

dirs = client.list_mount_workspaces(session_token=session_token)
print(dirs["items"])

Mount is now server-enforced read-only:

mount_result = client.mount_path(
    session_token=session_token,
    sandbox_id=sandbox_id,
    workspace_path="/project-a",
    container_path="/workspace",
)
print(mount_result)

List available environment directories first:

env_dirs = client.list_environment_workspaces(session_token=session_token)
print(env_dirs["items"])

Copy one allowlisted environment directory into sandbox filesystem (non-mount path, via docker cp):

copy_result = client.copy_environment(
    session_token=session_token,
    sandbox_id=sandbox_id,
    environment_path="/env-a",
    sandbox_path="/app",
)
print(copy_result)

6. Code Write API (Sandbox Semantic)

code_write targets:

  • POST /sandboxes/{id}/ops/code/write
  • validates sandbox auth and targets file inside sandbox filesystem
  • sandbox_path is required and must be an absolute file path in sandbox
  • service exports target file directory to host temp workspace, applies edit, and syncs back
  • quality checks: run pre-commit when config exists; otherwise auto-detect language for fallback checks
  • write_mode=context_edit is default for partial edits; pass raw snippets with old_text, new_text, and optional before_context/after_context/occurrence
  • write_mode=replace_file is supported for full overwrite by raw source content
  • For agent friendliness, LangChain tool also supports explicit actions: code_write_context_edit and code_write_replace_file
result = client.code_write(
    session_token=session_token,
    sandbox_id=sandbox_id,
    sandbox_path="/app/calculator.py",
    write_mode="context_edit",
    before_context="    def multiply(self, a, b):\n        return a * b\n\n",
    old_text="    def multiply(self, a, b):\n        return a * b\n",
    new_text="    def multiply(self, a, b):\n        return a * b\n\n    def sqrt(self, a):\n        if a < 0:\n            raise ValueError('Cannot sqrt negative number!')\n        return a ** 0.5\n",
)

# Full overwrite mode (raw source content)
rewrite_result = client.code_write(
    session_token=session_token,
    sandbox_id=sandbox_id,
    sandbox_path="/app/calculator.py",
    write_mode="replace_file",
    content="print('hello from overwrite mode')\n",
)

7. Low-Memory Guard (Create + Expand)

When host available memory ratio:

  • < 10%: create/expand is blocked
  • >= 10% and < 15%: create requires confirm_low_memory=True; expand proceeds with warning in response reason
created = client.create_sandbox(
    session_token=session_token,
    image="python:3.11-slim",
    confirm_low_memory=True,
    request_timeout_seconds=180,
)

8. More Examples

  • LangChain integration: ../README_langchain.md
  • MCP integration: ../README_mcp.md

9. Session Exec History

list_session_exec_tasks accepts optional session_id. If omitted, SDK uses session_token as session_id automatically:

history = client.list_session_exec_tasks(
    session_token=session_token,
    limit=20,
)
print(history)

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

sleigh_sdk-0.1.27.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

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

sleigh_sdk-0.1.27-py3-none-any.whl (15.1 kB view details)

Uploaded Python 3

File details

Details for the file sleigh_sdk-0.1.27.tar.gz.

File metadata

  • Download URL: sleigh_sdk-0.1.27.tar.gz
  • Upload date:
  • Size: 14.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for sleigh_sdk-0.1.27.tar.gz
Algorithm Hash digest
SHA256 8498d32b82d0da9c762133fa4cfd945061e4bbe68062d293a8f4adaf4dff8c35
MD5 5b505c4b6f63a6bd48d412d1fbe66988
BLAKE2b-256 214738863acf1c4d9f211fe77bba6acdbe4a528359ed7881da7af0a2e741c7e3

See more details on using hashes here.

File details

Details for the file sleigh_sdk-0.1.27-py3-none-any.whl.

File metadata

  • Download URL: sleigh_sdk-0.1.27-py3-none-any.whl
  • Upload date:
  • Size: 15.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for sleigh_sdk-0.1.27-py3-none-any.whl
Algorithm Hash digest
SHA256 1381370f6d241eee4e4566f317fc04891d5b42bbcedd583ade590ca8affa463d
MD5 0acee7d166a80ab7f3f46abd51a06fb6
BLAKE2b-256 a714f609f38bfa4078713d9b8c9049fba193b34e46dcba45e05c496f193eda23

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