<p align="center">
<img src="public/images/logo.svg" width="120" alt="X Space Agent" />
</p>
<h1 align="center">X Space Agent</h1>
<p align="center">
<b>AI agents that join and talk in X/Twitter Spaces</b>
</p>
<p align="center">
<a href="#quick-start">Quick Start</a> โข
<a href="#features">Features</a> โข
<a href="#project-structure">Structure</a> โข
<a href="#architecture">Architecture</a> โข
<a href="#examples">Examples</a> โข
<a href="docs/">Docs</a> โข
<a href="CONTRIBUTING.md">Contributing</a>
</p>
---
<p align="center">
<img src="docs/assets/hero.svg" alt="X Space Agent โ Multi-agent AI voice conversations" width="800">
<br>
<em>Multi-agent AI voice conversations in X/Twitter Spaces โ real-time transcription, LLM responses, and voice synthesis</em>
</p>
## What is this?
X Space Agent is a TypeScript SDK that lets you build **AI agents that autonomously join, listen, and speak in X/Twitter Spaces**. Connect any LLM, any voice provider, and ship in minutes. No Twitter API approval needed.
```typescript
import { XSpaceAgent } from 'xspace-agent'
const agent = new XSpaceAgent({
auth: { token: process.env.X_AUTH_TOKEN!, ct0: process.env.X_CT0! },
ai: { provider: 'openai', apiKey: process.env.OPENAI_API_KEY! },
})
agent.on('transcription', ({ text }) => console.log('Heard:', text))
agent.on('response', ({ text }) => console.log('Said:', text))
await agent.join('https://x.com/i/spaces/YOUR_SPACE_ID')
```
Or skip the code entirely with the CLI:
```bash
node packages/cli/bin/xspace-agent join https://x.com/i/spaces/YOUR_SPACE_ID --provider openai
```
(Once the packages are published this becomes `npx xspace-agent join <url>`.)
## Features
<table>
<tr>
<td align="center">๐ค<br><b>Multi-Provider LLM</b><br>OpenAI, Claude, Groq,<br>or any custom API</td>
<td align="center">๐ฅ<br><b>Multi-Agent Teams</b><br>Run multiple personalities<br>with turn management</td>
<td align="center">๐ง<br><b>Middleware Pipeline</b><br>Hook into STT โ LLM โ TTS<br>at any stage</td>
</tr>
<tr>
<td align="center">๐ป<br><b>Zero-Code CLI</b><br><code>xspace-agent join <url></code><br>no SDK needed</td>
<td align="center">๐<br><b>Admin Dashboard</b><br>Web UI to monitor and<br>control live agents</td>
<td align="center">๐ท<br><b>TypeScript-First</b><br>Full type safety,<br>autocomplete included</td>
</tr>
</table>
## Requirements
- **Node.js** >= 18 (tested on 18, 20, 22)
- **pnpm** >= 9 (for monorepo development) or npm/yarn for consuming the SDK
- **Chromium** โ bundled with Puppeteer, or provide your own via `BROWSER_MODE=connect`
- **X (Twitter) account** โ cookie-based auth (`X_AUTH_TOKEN` + `X_CT0`) or username/password
- **At least one AI provider key** โ OpenAI, Anthropic, or Groq
## Quick Start
**1. Install**
```bash
npm install xspace-agent # the SDK
npm create xspace-agent@latest # or scaffold a new project
```
To work on the monorepo itself, install from source:
```bash
git clone https://github.com/nirholas/xspace-AI.git
cd xspace-AI
pnpm install
pnpm build
```
The CLI (`packages/cli`) is not on npm yet; run it from `packages/cli/bin/xspace-agent`.
**2. Set environment variables**
```bash
# .env
<!-- three.ws:badges -->
[](https://github.com/nirholas/xspace-AI/stargazers) [](https://github.com/nirholas/xspace-AI/commits) [](https://github.com/nirholas/xspace-AI/pulls) [](https://github.com/nirholas/xspace-AI/blob/HEAD/AGENTS.md)
<!-- /three.ws:badges -->
X_AUTH_TOKEN=your_x_auth_token
X_CT0=your_x_ct0_cookie
OPENAI_API_KEY=sk-...
```
> Get `X_AUTH_TOKEN` and `X_CT0` from your browser cookies after logging into X. [Guide โ](docs/architecture-overview.md)
**3. Run**
```typescript
import { XSpaceAgent } from 'xspace-agent'
const agent = new XSpaceAgent({
auth: { token: process.env.X_AUTH_TOKEN!, ct0: process.env.X_CT0! },
ai: {
provider: 'openai',
apiKey: process.env.OPENAI_API_KEY!,
model: 'gpt-4o',
systemPrompt: 'You are a helpful AI analyst. Be concise and data-driven.',
},
voice: {
sttProvider: 'deepgram',
ttsProvider: 'elevenlabs',
voiceId: 'rachel',
},
})
agent.on('transcription', ({ text, speaker }) => console.log(`${speaker}: ${text}`))
agent.on('response', ({ text }) => console.log(`Agent: ${text}`))
await agent.join('https://x.com/i/spaces/YOUR_SPACE_ID')
```
Or skip the code entirely with the CLI:
```bash
node packages/cli/bin/xspace-agent join https://x.com/i/spaces/YOUR_SPACE_ID --provider openai
```
(Once the packages are published this becomes `npx xspace-agent join <url>`.)
## Deploy
<p>
<a href="https://railway.app/new/template?template=https://github.com/nirholas/xspace-AI"><img src="https://railway.app/button.svg" alt="Deploy on Railway" height="32" /></a>
<a href="https://render.com/deploy?repo=https://github.com/nirholas/xspace-AI"><img src="https://render.com/images/deploy-to-render-button.svg" alt="Deploy to Render" height="32" /></a>
</p>
Or with Docker, using the `Dockerfile` in this repo (no image is published to a
registry yet):
```bash
npm run docker:build
docker run -e OPENAI_API_KEY=sk-... -p 3000:3000 --shm-size=2gb xspace-agent
```
## Documentation
Full docs live in [docs/](docs/). Key guides:
| Guide | Description |
|-------|-------------|
| [Architecture Overview](docs/architecture-overview.md) | How the system fits together |
| [Providers](docs/providers.md) | LLM, STT, and TTS provider setup |
| [Admin Panel](docs/admin-page.md) | Web dashboard guide |
| [Environment Variables](docs/env-vars-reference.md) | All config options |
| [Multi-Space Support](docs/internal/multi-space-support.md) | Run agents across multiple Spaces |
| [Agent Memory & RAG](docs/internal/agent-memory-rag.md) | Persistent memory and retrieval |
| [TypeScript Migration](docs/typescript-migration.md) | TypeScript usage guide |
| [x402 Pay-Per-Question](docs/x402-pay-per-question.md) | USDC micropayment API powering [three.ws](https://github.com/nirholas/three.ws) |
## Project Structure
This is a **pnpm monorepo** with six publishable packages, a standalone voice agent, and supporting infrastructure.
### Packages (workspace packages, not yet published to npm)
```
packages/
core/ โ xspace-agent The main SDK. Everything needed to build an AI agent
โโโ agent.ts Entry point โ orchestrates browser, audio, LLM, turns
โโโ team.ts Multi-agent coordination (multiple AIs, one Space)
โโโ audio/ PCM capture, VAD, silence detection, WAV encoding, TTS injection
โโโ browser/ Puppeteer lifecycle, self-healing selector engine, DOM interaction
โโโ fsm/ Finite state machine for agent & team lifecycles
โโโ intelligence/ Speaker ID, topic tracking, sentiment, context management
โโโ pipeline/ Provider factories โ createLLM(), createSTT(), createTTS()
โโโ turns/ Turn coordination, decision engine, interruption handling
โโโ plugins/ Plugin system with 6 middleware hooks (before/after stt/llm/tts)
โโโ providers/ Multi-provider router and cost tracking
โโโ db/ Drizzle ORM, migrations, repositories
โโโ auth/ X/Twitter login, token validation, OAuth, SAML
โโโ memory/ Conversation persistence, RAG, archiving
โโโ observability/ Structured logging (Pino), tracing, metrics
โโโ __tests__/ Unit & E2E test suites with fixtures
server/ โ @xspace/server Express + Socket.IO admin panel
โโโ routes/ REST API endpoints
โโโ events/ Socket.IO real-time event handlers
โโโ middleware/ Auth, validation, CORS, rate limiting
โโโ schemas/ Zod request/response validation
โโโ personalities/ Preset agent configurations
โโโ public/ Admin dashboard HTML/CSS/JS
cli/ โ @xspace/cli Command-line tool
โโโ commands/ init, auth, join, start, dashboard
widget/ โ @xspace/widget Embeddable voice chat widget (UMD + ESM builds)
โโโ connection.ts WebSocket connection handler
โโโ theme.ts Theme customization
โโโ ui/ UI components
create-xspace-agent/ โ create-xspace-agent Project scaffolding (like create-react-app)
โโโ templates/base/ Starter project template
web-client/ โ @xspace/web-client Browser SDK for x402 pay-per-question
โโโ client.ts Question/payment client
โโโ x402-fetch.ts x402 payment-aware fetch wrapper
โโโ wallet/ Browser wallet adapters
```
### Application Code
```
agent-voice-chat/ Standalone voice chat agent โ separate from the monorepo
โโโ server.js Express + Socket.IO server (38KB)
โโโ openapi.json Full REST API spec
โโโ agents.config.json Agent configurations
โโโ room-manager.js Multi-room coordination
โโโ knowledge/ Vector embeddings & RAG data
โโโ memory/ Persistent conversation storage
โโโ providers/ LLM, STT, TTS implementations
โโโ tests/ Own test suite (vitest)
src/ Legacy monolithic server โ functional via `npm run dev`, being migrated
โโโ server/ Express server, socket handlers, routes, metrics
โโโ browser/ Puppeteer auth, launcher, orchestrator, selectors
โโโ audio/ Audio stream bridge
โโโ client/ Frontend initialization & provider configs
x-spaces/ Low-level Puppeteer automation scripts (JavaScript)
โโโ index.js Orchestration entry point
โโโ audio-bridge.js Audio capture & injection via CDP
โโโ auth.js Browser cookie authentication
โโโ space-ui.js X Spaces DOM interaction & selectors
```
### Supporting Directories
```
examples/ 12 runnable projects โ basic-join, multi-agent-debate, discord-bridge,
custom-provider, middleware-pipeline, express-integration, scheduled-spaces,
chrome-connect, with-plugins, and more. All but `plugins/` (a set of
reusable plugin modules) have their own package.json.
docs/ 78 markdown files: architecture overview, API reference (REST + WebSocket),
provider guides, deployment (Docker, Railway, Render, VPS), troubleshooting,
plugin system, configuration, and internal design specs.
personalities/ Pre-built agent personalities with system prompts & voice preferences
โโโ presets/ agent-zero, comedian, crypto-degen, educator,
interviewer, tech-analyst, and more
providers/ AI provider wrappers (JS) โ Claude, Groq, OpenAI Chat, OpenAI Realtime, STT, TTS
public/ Frontend assets โ admin dashboard, agent builder, voice chat UI,
widget demos (React, Vue), landing pages
docker/ Monitoring stack โ Prometheus scrape configs + Grafana dashboards
tasks/ 43 implementation specs & roadmap items (landing page, design system,
docs site, onboarding flow, admin dashboard v2, auth, rate limiting, etc.)
tests/ Top-level integration & load tests
```
## Examples
| Example | Description |
|---------|-------------|
| [**basic-join**](examples/basic-join/) | Join a Space with an AI agent in ~15 lines |
| [**transcription-logger**](examples/transcription-logger/) | Listen-only โ save timestamped transcripts to file |
| [**multi-agent-debate**](examples/multi-agent-debate/) | Two AIs (Bull vs Bear) debate live with round-robin turns |
| [**multi-agent**](examples/multi-agent/) | Multiple AI agents sharing a single Space |
| [**custom-provider**](examples/custom-provider/) | Use a local LLM (Ollama) or any custom API backend |
| [**middleware-pipeline**](examples/middleware-pipeline/) | Content filtering, language detection, safety redaction, analytics hooks |
| [**express-integration**](examples/express-integration/) | Embed the agent in an existing Express app with admin panel |
| [**scheduled-spaces**](examples/scheduled-spaces/) | Join Spaces on a cron schedule with auto-leave timers |
| [**discord-bridge**](examples/discord-bridge/) | Control the agent from Discord โ join, leave, speak, stream transcriptions |
| [**chrome-connect**](examples/chrome-connect/) | Connect to an existing Chrome instance instead of launching one |
| [**with-plugins**](examples/with-plugins/) | Extend agent behavior with custom plugins |
| [**plugins**](examples/plugins/) | Reusable plugin modules โ analytics, moderation, webhooks |
```bash
cd examples/basic-join
npm install
cp .env.example .env # fill in your API keys
npm start
```
## Architecture
```
X Space (live audio)
โ
Puppeteer + Chrome DevTools Protocol
โ
โโโโโโโโโโโโโผโโโโโโโโโโโโโ
โ BrowserLifecycle โ Auth โ Join โ Request Speaker โ Speak
โ Self-healing CSS/ โ Retries selectors via CSS โ text โ aria
โ text/aria selectors โ
โโโโโโโโโโโโโฌโโโโโโโโโโโโโ
โ RTCPeerConnection audio hooks
โโโโโโโโโโโโโผโโโโโโโโโโโโโ
โ AudioPipeline โ PCM capture โ VAD โ silence detection
โ โ โ WAV encoding โ TTS injection
โโโโโโโโโโโโโฌโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโ
โ โ โ
โโโโโโโโผโโโโโโโ โโโโโโโโผโโโโโโโ โโโโโโโโผโโโโโโโ
โ STT โ โ LLM โ โ TTS โ
โ Deepgram โ โ OpenAI โ โ ElevenLabs โ
โ Whisper โ โ Claude โ โ OpenAI TTS โ
โ (Groq/OAI) โ โ Groq โ โ Browser โ
โโโโโโโโฌโโโโโโโ โ Custom โ โโโโโโโโฌโโโโโโโ
โ โโโโโโโโฌโโโโโโโ โ
โ before:stt โ before:llm โ before:tts
โ after:stt โ after:llm โ after:tts
โ โ middleware โ โ โ middleware โ โ โ middleware โ
โ โ โ
โโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโ
โ Intelligence Layer โ
โ Speaker ID ยท Topic tracking ยท Sentiment ยท Context mgmt โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Turn Management + FSM โ
โ Decision engine ยท Interruption handling ยท Response pace โ
โ โ
โ idle โ launching โ authenticating โ joining โ listening โ
โ โ โ
โ speaking โ leaving โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
```
The agent connects to X Spaces via a headless Chromium browser, hooks into the WebRTC audio stream, and routes it through a fully configurable **STT โ LLM โ TTS** pipeline. Every stage supports middleware for logging, filtering, translation, content moderation, and more. The intelligence layer attributes speech to speakers, tracks topics, and manages conversation context. A finite state machine governs the full agent lifecycle.
## Providers
| Category | Providers |
|----------|-----------|
| **LLM** | OpenAI (GPT-4o), Anthropic (Claude), Groq (Llama/Mixtral), any OpenAI-compatible API |
| **Speech-to-Text** | Deepgram (streaming), OpenAI Whisper, custom |
| **Text-to-Speech** | ElevenLabs, OpenAI TTS, custom |
## CLI Reference
```bash
xspace-agent init # Interactive setup wizard
xspace-agent auth # Authenticate with X
xspace-agent join <url> # Join a Space
xspace-agent start # Start agent with admin panel
xspace-agent dashboard # Launch web dashboard only
```
## Used By
<!-- Add your project here! Open a PR to be featured. -->
_Be the first! [Open a PR](CONTRIBUTING.md) to add your project._
## Community
- ๐ [GitHub Issues](https://github.com/nirholas/xspace-AI/issues): bug reports, feature requests, questions, and ideas
## Contributing
We welcome contributions! See [CONTRIBUTING.md](CONTRIBUTING.md) for setup instructions and guidelines.
**Good first contributions:**
- Add a new AI provider (Mistral, Cohere, Together)
- Add a new TTS provider (Cartesia, PlayHT)
- Build an example project
- Improve documentation
## License
All rights reserved. See [LICENSE](LICENSE).
## Star History
[](https://www.star-history.com/#nirholas/xspace-AI&Date)
<!-- three.ws:growth -->
## Support the project
If xspace-AI saves you time, **[star it on GitHub](https://github.com/nirholas/xspace-AI)**. Stars are how other developers and AI agents find the repositories worth trusting, and they cost you one click.
Know someone who would use it? [Post on X](https://twitter.com/intent/tweet?text=xspace-AI%3A%20TypeScript%20SDK%20and%20CLI%20for%20building%20AI%20agents%20that%20autonomously%20join%2C%20listen%2C%20and%20speak%20in&url=https%3A%2F%2Fgithub.com%2Fnirholas%2Fxspace-AI) ยท [Share on Bluesky](https://bsky.app/intent/compose?text=xspace-AI%3A%20TypeScript%20SDK%20and%20CLI%20for%20building%20AI%20agents%20that%20autonomously%20join%2C%20listen%2C%20and%20speak%20in%20https%3A%2F%2Fgithub.com%2Fnirholas%2Fxspace-AI) ยท [Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fgithub.com%2Fnirholas%2Fxspace-AI) ยท [Submit to Hacker News](https://news.ycombinator.com/submitlink?u=https%3A%2F%2Fgithub.com%2Fnirholas%2Fxspace-AI&t=xspace-AI%3A%20TypeScript%20SDK%20and%20CLI%20for%20building%20AI%20agents%20that%20autonomously%20join%2C%20listen%2C%20and%20speak%20in) ยท [Share on Reddit](https://www.reddit.com/submit?url=https%3A%2F%2Fgithub.com%2Fnirholas%2Fxspace-AI&title=xspace-AI%3A%20TypeScript%20SDK%20and%20CLI%20for%20building%20AI%20agents%20that%20autonomously%20join%2C%20listen%2C%20and%20speak%20in)
## Built for AI agents too
Coding agents and LLM tooling can read this repo directly: [AGENTS.md](./AGENTS.md), [llms.txt](./llms.txt), [llms-full.txt](./llms-full.txt). Point an agent at `https://github.com/nirholas/xspace-AI` and it has the context it needs.
## More from the same author
- [All repositories by nirholas](https://github.com/nirholas/nirholas#readme): the full catalog, grouped by topic
- [three.ws](https://three.ws): the platform for 3D AI agents with Solana wallets, a skill marketplace and x402 payments
- Questions or ideas: [open an issue](https://github.com/nirholas/xspace-AI/issues) or [start a discussion](https://github.com/nirholas/xspace-AI/discussions)
## Contributors
[](https://github.com/nirholas/xspace-AI/graphs/contributors)
<!-- /three.ws:growth -->
<!-- nirholas:watermark -->
---
<p align="center">
Built by <a href="https://github.com/nirholas"><b>nirholas</b></a> ยท <a href="https://x.com/nichxbt">@nichxbt</a> ยท <a href="https://github.com/nirholas">github.com/nirholas</a>
</p>
<!-- /nirholas:watermark -->