Skip to main content

Python SDK for building Dhara extensions

Project description

dhara-extension-py

Python SDK for building Dhara extensions.

Installation

pip install dhara-extension

Or install from source:

cd packages/dhara-extension-py
pip install -e .

Quick Start

Create a file hello-ext/main.py:

#!/usr/bin/env python3
"""A simple Dhara extension using the Python SDK."""

from dhara_extension import Extension

ext = Extension(
    name="hello-ext",
    version="1.0.0",
    description="A friendly hello extension",
)

@ext.tool(
    name="hello",
    description="Greet someone by name",
    parameters={
        "type": "object",
        "properties": {
            "name": {
                "type": "string",
                "description": "Name to greet",
            },
        },
        "required": ["name"],
    },
)
def hello(input_data):
    name = input_data.get("name", "World")
    greeting = f"Hello, {name}! Welcome to Dhara."
    return {
        "content": [
            {"type": "text", "text": greeting},
        ],
    }

@ext.tool(
    name="count",
    description="Count items in a list",
)
def count(input_data):
    items = input_data.get("items", [])
    return {
        "content": [
            {"type": "text", "text": f"Counted {len(items)} items."},
        ],
    }

if __name__ == "__main__":
    ext.run()

And a manifest.json:

{
    "name": "hello-ext",
    "version": "1.0.0",
    "runtime": {
        "type": "subprocess",
        "command": "python3 main.py",
        "protocol": "json-rpc"
    },
    "provides": {
        "tools": ["hello", "count"]
    },
    "capabilities": []
}

API Reference

Extension(name, version, description="", debug=False)

Main extension class.

  • tool(name, description, parameters, capabilities) — Decorator to register a tool handler
  • run() — Start the JSON-RPC stdin/stdout loop

create_extension(name, version, description="")

Convenience function to create an Extension instance.

Protocol

Extensions communicate with Dhara via JSON-RPC 2.0 over stdin/stdout:

→ {"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}
← {"jsonrpc":"2.0","id":1,"result":{"protocolVersion":"0.1.0","name":"hello-ext","tools":[...]}}

→ {"jsonrpc":"2.0","id":2,"method":"tools/execute",
     "params":{"toolName":"hello","input":{"name":"World"}}}
← {"jsonrpc":"2.0","id":2,"result":{"content":[{"type":"text","text":"Hello, World!"}]}}

See the Dhara Extension Protocol for details.

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

dhara_extension-0.6.1.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

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

dhara_extension-0.6.1-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file dhara_extension-0.6.1.tar.gz.

File metadata

  • Download URL: dhara_extension-0.6.1.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for dhara_extension-0.6.1.tar.gz
Algorithm Hash digest
SHA256 2fbe0296f8e4f9f322f70a1b2e23bd2890ae6945b2e9f2a7e3992a84bedf4799
MD5 f3d2faacb50b0e06456e2cfe1f583763
BLAKE2b-256 731eccdcf0271d71e8c7a908373678f23f33ad47585ddcd32f85c8d3d3c392ad

See more details on using hashes here.

File details

Details for the file dhara_extension-0.6.1-py3-none-any.whl.

File metadata

File hashes

Hashes for dhara_extension-0.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 28a4ea23cd1b3d7302b63b2474468f25191d5d805bae8e88c7999f90183ee236
MD5 7c62b85ceb86f5b0b156565cf7c03615
BLAKE2b-256 aafdf7dc4bf119e861d9f8d32cbe3cde2448777c425e300b059f6fd57a993468

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