Python SDK for TRUCE CORE — Trust Infrastructure for Agent Commerce
Project description
TRUCE SDK for Python
Python client library for TRUCE CORE -- trust infrastructure for autonomous LLM agent commerce.
Installation
pip install truce-sdk
Or install from source:
cd sdk/python
pip install -e .
Quickstart
from truce import TruceClient
client = TruceClient("http://localhost:8000")
agent = client.register_agent("FIRM001", "Acme Corp", "trading-bot-1")
session = client.authenticate(agent.agent_id)
offer = client.submit_offer(raw_text="Selling 500 MT of wheat at $320/MT FOB, 30 day delivery, NET30")
Async Support
import asyncio
from truce import AsyncTruceClient
async def main():
async with AsyncTruceClient("http://localhost:8000") as client:
agent = await client.register_agent("FIRM001", "Acme Corp", "trading-bot-1")
session = await client.authenticate(agent.agent_id)
offer = await client.submit_offer(raw_text="Buying 200 MT of rice at $450/MT CIF")
asyncio.run(main())
API Reference
KYA Gateway (Identity & Trust)
| Method | Description |
|---|---|
register_agent(firm_id, firm_name, agent_name, public_key) |
Register a new agent |
authenticate(agent_id, ttl_minutes) |
Authenticate and get JWT session |
get_baseline(agent_id) |
Get behavioral baseline |
get_anomaly_score(agent_id) |
Get KYA-B anomaly score |
Offers
| Method | Description |
|---|---|
submit_offer(raw_text, agent_id, firm_id) |
Submit offer via full pipeline |
get_offer(offer_id) |
Get offer by ID |
list_offers(status, agent_id, firm_id) |
List offers with filters |
cancel_offer(offer_id) |
Cancel an open offer |
Sanitization Engine
| Method | Description |
|---|---|
extract_claims(raw_text, agent_id) |
LLM-powered claim extraction |
validate_tcos(offer_data) |
Validate against TCOS schema |
notarize(raw_text, agent_id, firm_id) |
Full extract-validate-notarize pipeline |
Matching Engine
| Method | Description |
|---|---|
find_matches(offer_id) |
Find compatible counterparty offers |
execute_match(buyer_offer_id, seller_offer_id) |
Execute match and lock escrow |
get_match(match_id) |
Get match details |
Escrow
| Method | Description |
|---|---|
get_escrow(match_id) |
Get escrow state |
confirm_escrow(match_id, side) |
Confirm buyer or seller side |
release_escrow(match_id, reason) |
Release/cancel escrow |
AVX (Agent Volatility Index)
| Method | Description |
|---|---|
ingest_avx_events(events) |
Ingest market events |
get_avx(sector) |
Compute AVX score for sector |
get_avx_history(sector) |
Get AVX score history |
list_avx_sectors() |
List all tracked sectors |
ALPHA (Trust Score)
| Method | Description |
|---|---|
compute_alpha(agent_id, counterparty_id, sector) |
Compute composite ALPHA trust score |
get_alpha_history(agent_id) |
Get ALPHA score history for an agent |
Webhooks
| Method | Description |
|---|---|
register_webhook(url, events, secret) |
Register a webhook endpoint |
list_webhooks() |
List all registered webhooks |
unregister_webhook(webhook_id) |
Unregister a webhook by ID |
get_webhook_deliveries(webhook_id) |
Get delivery history for a webhook |
Review Queue
| Method | Description |
|---|---|
list_pending_reviews() |
List offers pending human review |
list_all_reviews() |
List all review items (any status) |
get_review(review_id) |
Get details of a review item |
approve_review(review_id, reviewer, notes) |
Approve a SOFT_HOLD offer |
reject_review(review_id, reviewer, reason) |
Reject a SOFT_HOLD offer |
RISK (Data Product)
| Method | Description |
|---|---|
assess_risk(agent_id, sector) |
Compute comprehensive risk profile |
assess_portfolio_risk(agent_ids, sector) |
Portfolio-level risk assessment |
analyze_sector_risk(sector, top_n) |
Sector-wide risk analysis |
configure_risk_alert(agent_id, threshold) |
Set up risk threshold alerts |
get_risk_alerts(agent_id) |
Get risk alert history |
CLI
truce status --url http://localhost:8000 # Check API health
truce register --firm FIRM001 --name bot-1 # Register an agent
truce score --agent AGT-001 # Compute ALPHA score
truce offers # List active offers
truce avx --sector commodities # Get AVX score
truce benchmark dataset.jsonl # Run TATF benchmark
Error Handling
The SDK raises typed exceptions for different HTTP error codes:
from truce import TruceClient, NotFoundError, ValidationError
client = TruceClient("http://localhost:8000")
try:
offer = client.get_offer("nonexistent-id")
except NotFoundError as e:
print(f"Not found: {e.message}")
except ValidationError as e:
print(f"Invalid input: {e.detail}")
| Exception | HTTP Status | When |
|---|---|---|
AuthenticationError |
401 | Invalid or expired JWT |
AuthorizationError |
403 | HARD_BLOCK or insufficient permissions |
NotFoundError |
404 | Resource not found |
ConflictError |
409 | State conflict (e.g., duplicate escrow) |
ValidationError |
422 | Schema validation or extraction failure |
ServerError |
5xx | Upstream server errors |
Full Trade Lifecycle Example
from truce import TruceClient
client = TruceClient("http://localhost:8000")
# 1. Register two agents
buyer = client.register_agent("FIRM_A", "Alpha Trading", "buyer-bot")
seller = client.register_agent("FIRM_B", "Beta Exports", "seller-bot")
# 2. Authenticate both
client.authenticate(buyer.agent_id)
buy_offer = client.submit_offer(
raw_text="Buying 100 MT of soybeans at $500/MT CIF, 45 day delivery, NET30",
agent_id=buyer.agent_id,
firm_id=buyer.firm_id,
)
client.authenticate(seller.agent_id)
sell_offer = client.submit_offer(
raw_text="Selling 200 MT of soybeans at $490/MT CIF, 30 day delivery, NET30",
agent_id=seller.agent_id,
firm_id=seller.firm_id,
)
# 3. Find and execute match
matches = client.find_matches(buy_offer.offer_id)
if matches.total_found > 0:
result = client.execute_match(buy_offer.offer_id, sell_offer.offer_id)
print(f"Match executed: {result.match.match_id}")
print(f"Settlement: {result.match.settlement_price} x {result.match.settlement_quantity}")
# 4. Confirm escrow from both sides
client.confirm_escrow(result.match.match_id, "buyer")
client.confirm_escrow(result.match.match_id, "seller")
print("Trade settled!")
Requirements
- Python 3.9+
- httpx >= 0.25
- pydantic >= 2.0
- websockets >= 12.0
License
Apache 2.0 -- see LICENSE for details.
Links
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
truce_sdk-0.1.0.tar.gz
(36.1 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
truce_sdk-0.1.0-py3-none-any.whl
(28.4 kB
view details)
File details
Details for the file truce_sdk-0.1.0.tar.gz.
File metadata
- Download URL: truce_sdk-0.1.0.tar.gz
- Upload date:
- Size: 36.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d539d1724459348026f7450a69888636f4d24f00463d7f6f8b5126e941285a2a
|
|
| MD5 |
8f6f79c9184fcf5afbd83dadccb22a58
|
|
| BLAKE2b-256 |
a7f977b7442c1c99a5b76a6a4f48f15ff1665c55d9f00945c3a6b5ebc19d2ca2
|
File details
Details for the file truce_sdk-0.1.0-py3-none-any.whl.
File metadata
- Download URL: truce_sdk-0.1.0-py3-none-any.whl
- Upload date:
- Size: 28.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5e3cb92c37aa4719472749f48478bb669fb6390f3f2bd29ef602cec2b22210a9
|
|
| MD5 |
129ebc7fc72abc91d1bb48eb734e684e
|
|
| BLAKE2b-256 |
e9c1d3ac3d02f614aaba1e3b22ed90fb5d54f75b4e3f02d0d6e96580bfc95c31
|