Overview
Gambiarra uses a hub-and-spoke architecture where a central HTTP hub coordinates communication between multiple participants sharing their local LLM endpoints. The system is built on three core principles:- Local-first: All LLMs run locally on participant machines
- Zero-config networking: Optional mDNS discovery eliminates manual IP configuration
- Standard protocols: HTTP + SSE instead of WebSocket for simpler integration
Core Components
Hub (HTTP Server)
The hub is a lightweight HTTP server that acts as a coordinator and proxy:- Room management: Create/list rooms with optional password protection
- Participant registry: Track participants and their health status
- Request routing: Proxy OpenAI-compatible requests to appropriate participants
- Event broadcasting: Send real-time updates via Server-Sent Events (SSE)
packages/core/src/hub.ts
The hub stores all state in-memory. Restarting the hub clears all rooms and participants.
Rooms
Rooms are isolated workspaces where participants collaborate:- Short codes: Easy-to-share 6-character room codes
- Password protection: Rooms can require passwords (hashed with argon2id)
- Host tracking: First participant to create the room becomes the host
packages/core/src/room.ts:35-59
Participants
Participants are individual machines sharing their LLM endpoints:packages/core/src/participant.ts
SDK
The SDK provides programmatic access for JavaScript/TypeScript applications:packages/sdk/src/provider.ts
CLI
The CLI offers scripting and automation capabilities:packages/cli/src/cli.ts
TUI (Terminal UI)
The TUI provides interactive monitoring and management:- Real-time participant status via SSE
- Room creation and joining
- Visual health indicators
- Event log viewer
apps/tui/src/index.tsx
Communication Architecture
HTTP + SSE Model
Gambiarra replaced WebSocket with HTTP for requests and SSE for events:- Simpler SDK integration: Uses standard
@ai-sdk/openai-compatibleprovider - Better debugging: HTTP requests are easy to inspect with curl/Postman
- Standard API: Any OpenAI-compatible client works out of the box
- Unidirectional events: Hub broadcasts events; clients don’t need to send events back
docs/old/architecture-v1-websocket.md for the previous WebSocket design
Data Flow
1. Participant Registration
Implementation:packages/core/src/hub.ts:68-115
2. LLM Request Routing
Implementation:packages/core/src/hub.ts:249-306
3. Health Monitoring
Constants:HEALTH_CHECK_INTERVAL = 10_000ms (10 seconds)PARTICIPANT_TIMEOUT = 30_000ms (3 missed checks)
packages/core/src/hub.ts:380-388
SSE Event Types
The hub broadcasts these events to connected SSE clients:| Event | Data | Description |
|---|---|---|
connected | {clientId} | Client successfully connected |
room:created | {room} | New room was created |
participant:joined | {participant} | Participant joined a room |
participant:left | {participantId} | Participant left a room |
participant:offline | {participantId} | Participant timed out |
llm:request | {participantId, model} | LLM request routed to participant |
llm:error | {participantId, error} | LLM request failed |
packages/core/src/sse.ts
Package Structure
Gambiarra is a Bun + Turbo monorepo:Design Principles
1. Feature Parity
All three interfaces (SDK, CLI, TUI) provide the same core capabilities:| Feature | SDK | CLI | TUI |
|---|---|---|---|
| Create room | ✅ API | ✅ create | ✅ Dialog |
| Join room | ✅ API | ✅ join | ✅ Dialog |
| List rooms | ✅ API | ✅ list | ✅ Selector |
| Chat completion | ✅ AI SDK | ✅ Via SDK | ❌ (monitor only) |
| Real-time events | ✅ SSE API | ❌ | ✅ Built-in |
| Serve hub | ❌ | ✅ serve | ✅ Embedded |
2. OpenAI Compatibility
The hub exposes OpenAI-compatible endpoints, making it a drop-in replacement:3. Stateless Hub
The hub maintains no persistent storage:- All state is in-memory
- Restarting clears rooms and participants
- Participants must re-join after hub restart
- Suitable for development and local networks
Next Steps
Model Routing
Learn how to route requests to specific participants or models
Room Management
Master room lifecycle, passwords, and participant health checks
mDNS Discovery
Enable zero-config networking with Bonjour/Zeroconf
API Reference
Explore all HTTP endpoints and SDK methods