An open API service indexing awesome lists of open source software.

https://github.com/flammafex/hypertoken

Distributed simulation engine for games and multi-agent AI research featuring: CRDT state sync, OpenAI Gym integration, and P2P multiplayer โ€” no servers required.
https://github.com/flammafex/hypertoken

ai-training automerge card-games crdt game-engine mcp multiplayer openai-gym p2p pettingzoo python reinforcement-learning serverless simulation typescript webtrc

Last synced: 2 days ago
JSON representation

Distributed simulation engine for games and multi-agent AI research featuring: CRDT state sync, OpenAI Gym integration, and P2P multiplayer โ€” no servers required.

Awesome Lists containing this project

README

          

# ๐Ÿงฉ HyperToken

**A game engine where the entire state is a CRDT.**

Deterministic replay, serverless multiplayer, forkable worldsโ€”all from one architectural decision. Built on **[Automerge](https://automerge.org/)** for distributed consensus, with **[OpenAI Gym](https://gymnasium.farama.org/)/[PettingZoo](https://pettingzoo.farama.org/)** interfaces so any game doubles as a training environment.

---

## ๐Ÿ”‘ What This Gets You

| Capability | How |
|------------|-----|
| **Serverless multiplayer** | CRDTs sync state across peers without a server |
| **Perfect replay** | Every action recorded with actor and timestamp |
| **Forkable worlds** | Snapshot state, explore alternatives, compare outcomes |
| **AI training environments** | Gym/PettingZoo interfaces out of the box |
| **Offline-first** | Peers diverge safely, converge mathematically |

```
Traditional Engine: Server decides โ†’ clients accept โ†’ monthly hosting bill
Blockchain Engine: Consensus decides โ†’ everyone pays gas โ†’ wait 15 seconds
HyperToken: CRDTs merge โ†’ everyone agrees โ†’ zero infrastructure
```

---

## ๐ŸŽฎ What You Can Build

**Card Games** โ€” Blackjack, Poker, Cuttle, custom TCGs. Tokens compose with provenance tracking.

**Strategy Games** โ€” Game theory simulations, tournaments, agent competitions. 14 Prisoner's Dilemma strategies included.

**Multiplayer Worlds** โ€” P2P sync, no servers, games that outlive their creators.

**Training Environments** โ€” Any game is automatically a Gym environment. Multi-agent via PettingZoo.

```bash
# Play now
npm run blackjack # Casino with AI & betting
npm run prisoners-dilemma # Game theory tournament
npm run poker # Texas Hold'em
npm run cuttle # Card combat

# Multiplayer
npm run blackjack:server # Host
npm run blackjack:client # Join
```

---

## โšก Quick Start

```bash
git clone https://github.com/flammafex/hypertoken.git
cd hypertoken
npm install
npm run build
npm run blackjack
```

Pre-built WASM binaries included. See [Getting Started Guide](./docs/GETTING_STARTED.md) for the full walkthrough.

---

## ๐Ÿ—๏ธ How It Works

### Tokens Compose With Provenance

```javascript
// Merge tokens โ€” result tracks where it came from
const enchantedSword = engine.dispatch("token:merge", {
tokens: [sword, fireEnchantment],
resultProperties: { label: "Flaming Sword" }
});
// enchantedSword._mergedFrom = [sword.id, fireEnchantment.id]
// enchantedSword._mergedAt = timestamp

// Split tokens โ€” pieces track their origin
const pieces = engine.dispatch("token:split", {
token: goldPile,
count: 3
});
// pieces[0]._splitFrom = goldPile.id
```

### State Syncs Automatically

```javascript
const host = new Engine();
host.connect("ws://relay.local:8080");

const client = new Engine();
client.connect("ws://relay.local:8080");

// Both make changes โ†’ CRDTs merge โ†’ identical final state
// No conflict resolution code. No server logic. It just works.
```

### Any Game Becomes a Training Environment

```typescript
class BlackjackEnv extends GymEnvironment {
get observationSpace() { return { shape: [6] }; }
get actionSpace() { return { n: 4 }; } // hit, stand, double, split

async step(action: number) {
const result = this.game.act(action);
return {
observation: this.encodeState(),
reward: result.reward,
terminated: result.done,
truncated: false,
info: {}
};
}
}
```

### Fork State for What-If Exploration

```javascript
const snapshot = engine.snapshot();

// Try option A
engine.dispatch('agent:cooperate');
const cooperateOutcome = engine.getState();

// Rewind, try option B
engine.restore(snapshot);
engine.dispatch('agent:defect');
const defectOutcome = engine.getState();

// Compare and decide
```

---

## ๐Ÿค– AI & ML Integration

**Gym/PettingZoo** โ€” Single-agent and multi-agent interfaces. Turn-based (AEC) and simultaneous (Parallel).

**ONNX Export** โ€” Train anywhere, deploy the policy in browser or Node.js.

**Python Bridge** โ€” Connect to the TypeScript engine from Python for training.

**MCP Server** โ€” Let LLMs play games via [Model Context Protocol](https://modelcontextprotocol.io/).

```bash
npm run mcp:server # LLM integration
npm run bridge:blackjack # Python bridge
```

---

## โš™๏ธ Architecture

```
hypertoken/
โ”œโ”€โ”€ core/ # CRDT state management
โ”‚ โ”œโ”€โ”€ Token.ts # Entities with provenance tracking
โ”‚ โ”œโ”€โ”€ Stack.ts # Ordered collections (decks, piles)
โ”‚ โ”œโ”€โ”€ Space.ts # Spatial zones (boards, hands)
โ”‚ โ”œโ”€โ”€ Chronicle.ts # Automerge CRDT wrapper
โ”‚ โ””โ”€โ”€ ConsensusCore.ts # P2P synchronization
โ”‚
โ”œโ”€โ”€ core-rs/ # Rust โ†’ WASM (67 typed actions)
โ”‚ โ””โ”€โ”€ src/
โ”‚ โ”œโ”€โ”€ stack.rs # 10 stack operations
โ”‚ โ”œโ”€โ”€ space.rs # 14 spatial operations
โ”‚ โ”œโ”€โ”€ agent.rs # 16 agent operations
โ”‚ โ”œโ”€โ”€ token_ops.rs # 7 token transformations
โ”‚ โ””โ”€โ”€ batch.rs # 8 batch operations
โ”‚
โ”œโ”€โ”€ engine/ # Game coordination
โ”‚ โ”œโ”€โ”€ Engine.ts # Action dispatch, WASM integration
โ”‚ โ”œโ”€โ”€ GameLoop.ts # Turn management
โ”‚ โ””โ”€โ”€ RuleEngine.ts # Condition-triggered actions
โ”‚
โ”œโ”€โ”€ network/ # P2P and server modes
โ”‚ โ”œโ”€โ”€ PeerConnection.ts
โ”‚ โ”œโ”€โ”€ AuthoritativeServer.ts
โ”‚ โ””โ”€โ”€ HybridPeerManager.ts
โ”‚
โ”œโ”€โ”€ interface/ # RL adapters
โ”‚ โ”œโ”€โ”€ Gym.ts # Single-agent
โ”‚ โ”œโ”€โ”€ PettingZoo.ts # Multi-agent (turn-based)
โ”‚ โ””โ”€โ”€ PettingZooParallel.ts # Multi-agent (simultaneous)
โ”‚
โ””โ”€โ”€ examples/ # Working games
โ”œโ”€โ”€ blackjack/
โ”œโ”€โ”€ poker/
โ”œโ”€โ”€ prisoners-dilemma/
โ””โ”€โ”€ hanabi/
```

---

## ๐Ÿ”ฎ The Philosophy

> "A token isn't valuable because of what it ISโ€”it's valuable because of its relationships"

Tokens derive meaning from context:
- **Who owns it** โ€” agents, players
- **What's attached** โ€” enchantments, modifiers
- **Where it is** โ€” zones, positions
- **What it came from** โ€” merge/split provenance
- **What rules govern it** โ€” constraints, triggers

This applies to cards in blackjack, strategies in game theory, shares in a market, or NPCs in a world. The same engine handles all of them because the abstraction is right.

---

## ๐ŸŒ Compared To

| System | HyperToken's Difference |
|--------|------------------------|
| **Unity/Godot** | Logic-first, no graphics dependency |
| **Colyseus** | P2P, no server required |
| **Blockchain games** | Same guarantees, zero gas fees |
| **Automerge/Yjs** | Game-aware abstractions (tokens, agents, rules) |
| **OpenAI Gym** | Built-in multiplayer, compositional tokens |

---

## ๐Ÿ“– Documentation

- [Action Reference](./engine/ACTIONS.md) โ€” All 67 actions
- [Architecture Guide](./docs/ARCHITECTURE.md) โ€” How components connect
- [Python Bridge](./docs/PYTHON_BRIDGE.md) โ€” PettingZoo integration
- [ONNX Export](./docs/ONNX_EXPORT.md) โ€” Deploy trained policies
- [Docker Guide](./DOCKER.md) โ€” Container deployment

---

## ๐Ÿณ Docker

```bash
docker build -t hypertoken:latest .
docker compose up relay
docker compose run --rm quickstart
```

---

## ๐Ÿ“œ License

Apache 2.0 โ€” Copyright ยฉ 2025 The Carpocratian Church of Commonality and Equality, Inc.

---

## ๐Ÿ‘ฅ Credits

**Created by Marcellina II (she/her)**

Inspired by Martin Kleppmann's work on CRDTs, Rich Hickey's philosophy on state and time, and the legacy of HyperCard.

---

## ๐Ÿœ Proemium to the Art of Tokens

*The All is number, and from number flow the forms of things.*
*For as the Monad abides in simplicity, so does it unfold the Dyad,*
*and from their tension spring the harmonies that sustain the world.*

*Among the arts that imitate the order of the heavens,*
*there now arises one most subtle and most justโ€”the Art of Tokens.*

*In this art, every being is rendered as a form in relation,*
*every action as a motion among forms,*
*and the laws that bind them are set forth as measure and correspondence.*

*Let none deem this art a toy of artifice.*
*It is the discipline by which the mind rehearses creation,*
*a mirror held to the pattern of the world-soul.*

*So may this art be given freely,*
*that all who love Wisdom may join the music of the spheres through understanding,*
*and that the harmony of minds may become the harmony of worlds.*

*For when reason is made common, the gods are near.*