Python bridge for HyperToken distributed simulation engine
Project description
HyperToken Python Bridge
Train AI agents on HyperToken environments using Python and your favorite RL libraries.
HyperToken is a distributed simulation engine for multi-agent games. This package provides a Python interface via WebSocket, compatible with PettingZoo and gymnasium.
Installation
pip install hypertoken
# With gymnasium support (recommended)
pip install hypertoken[gymnasium]
# With Stable-Baselines3 support
pip install hypertoken[sb3]
# With RLlib support
pip install hypertoken[rllib]
# Everything
pip install hypertoken[all]
Quick Start
1. Start the HyperToken Server
# From the hypertoken repository
npx tsx bridge/server.ts --env blackjack --port 9999
# Or with npm script
npm run bridge:blackjack
2. Connect from Python
from hypertoken import HyperTokenAECEnv
# PettingZoo AEC API
env = HyperTokenAECEnv("ws://localhost:9999")
env.reset(seed=42)
for agent in env.agent_iter():
obs, reward, term, trunc, info = env.last()
if term or trunc:
action = None
else:
action = env.action_space(agent).sample()
env.step(action)
print(f"Final rewards: {env.rewards()}")
env.close()
3. Train with Stable-Baselines3
from stable_baselines3 import PPO
from hypertoken import HyperTokenClient
# See examples/train_sb3.py for the full wrapper
env = BlackjackGymWrapper("ws://localhost:9999")
model = PPO("MlpPolicy", env, verbose=1)
model.learn(total_timesteps=50_000)
model.save("blackjack_ppo")
Available Environments
| Environment | Agents | Actions | Description |
|---|---|---|---|
blackjack |
1-6 | Hit, Stand, Double, Split, Insurance | Casino blackjack |
Blackjack Actions
| ID | Action | Description |
|---|---|---|
| 0 | Hit | Take another card |
| 1 | Stand | End turn with current hand |
| 2 | Double | Double bet, take one card, stand |
| 3 | Split | Split pair into two hands |
| 4 | Insurance | Take insurance bet (if dealer shows Ace) |
Blackjack Observation
7-element normalized vector:
[handValue, dealerUpcard, isSoft, canDouble, canSplit, canInsurance, deckRatio]
All values normalized to [0, 1] range.
API Reference
HyperTokenAECEnv
PettingZoo AEC (Agent Environment Cycle) compatible environment.
env = HyperTokenAECEnv(url, render_mode=None)
# Properties
env.possible_agents # List of all possible agent names
env.agents # List of currently active agents
env.agent_selection # Current agent whose turn it is
# Spaces
env.observation_space(agent) # gymnasium.spaces.Box
env.action_space(agent) # gymnasium.spaces.Discrete
# Core API
env.reset(seed=None)
env.step(action)
obs, reward, term, trunc, info = env.last()
# Iteration
for agent in env.agent_iter():
...
# State queries
env.rewards() # Dict[str, float]
env.terminations() # Dict[str, bool]
env.truncations() # Dict[str, bool]
env.infos() # Dict[str, Dict]
env.action_mask(agent) # np.ndarray[bool] or None
# Cleanup
env.close()
HyperTokenParallelEnv
PettingZoo Parallel API compatible environment.
env = HyperTokenParallelEnv(url, render_mode=None)
observations, infos = env.reset()
while env.agents:
actions = {agent: policy(obs) for agent, obs in observations.items()}
observations, rewards, terms, truncs, infos = env.step(actions)
env.close()
HyperTokenClient
Low-level WebSocket client for direct server communication.
client = HyperTokenClient(url, timeout=30.0)
client.connect()
client.reset(seed=42)
obs = client.observe("player_0")
client.step(action=0)
result = client.last()
latency = client.ping() # milliseconds
client.close()
Performance
Typical latency over WebSocket:
- Local connection: ~1-2ms per step
- Same network: ~5-10ms per step
For maximum performance, run the server and Python client on the same machine.
from hypertoken import HyperTokenClient
client = HyperTokenClient("ws://localhost:9999")
client.connect()
# Measure latency
latencies = [client.ping() for _ in range(100)]
print(f"Mean latency: {sum(latencies)/len(latencies):.2f}ms")
client.disconnect()
Examples
See the examples/ directory:
train_sb3.py- Train with Stable-Baselines3train_pettingzoo.py- Multi-agent training with PettingZoo APIjupyter_demo.ipynb- Interactive Jupyter notebook
Development
# Install dev dependencies
pip install -e ".[dev]"
# Run tests
pytest
# Format code
black hypertoken/
ruff check hypertoken/
# Type check
mypy hypertoken/
Architecture
┌─────────────────────┐ WebSocket ┌─────────────────────┐
│ Python Client │ ◄──────────────────► │ Node.js Server │
│ (pip install) │ JSON messages │ (HyperToken Engine) │
│ │ │ │
│ - PettingZoo API │ │ - AECEnvironment │
│ - gymnasium API │ │ - BlackjackAEC │
│ - numpy arrays │ │ - WASM acceleration │
└─────────────────────┘ └─────────────────────┘
License
Apache 2.0
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
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
File details
Details for the file hypertoken-0.1.0.tar.gz.
File metadata
- Download URL: hypertoken-0.1.0.tar.gz
- Upload date:
- Size: 16.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fb468e504e2ff87a22f8d9c538bb424a2807148c7f884662a6c4f77f108ebb61
|
|
| MD5 |
b86416dc81c9cb411cb710cf6280fdec
|
|
| BLAKE2b-256 |
d684aa5ca69fbb9da95cf273eb3f40d01537d6a779d538974274e90f2c295893
|
File details
Details for the file hypertoken-0.1.0-py3-none-any.whl.
File metadata
- Download URL: hypertoken-0.1.0-py3-none-any.whl
- Upload date:
- Size: 16.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2ac4690c05b727c74d223f008bad45a0be50fa873b2a9c3c821357caac625fc7
|
|
| MD5 |
341647f616896df32a6e3c83e96c818a
|
|
| BLAKE2b-256 |
09a4a9665786436794ee8b7bf7e4c30f4b31d8b435d19a3abce0a2feac22e1dc
|