opencode-nostr-dm
v0.1.2
Published
OpenCode plugin for Nostr DM integration (NIP-04)
Downloads
261
Maintainers
Readme
OpenCode Nostr DM Plugin
Chat with OpenCode AI via Nostr encrypted DMs (NIP-04).
Quick Install (Ubuntu/Debian Server)
curl -fsSL https://raw.githubusercontent.com/happylemonprogramming/opencode-nostr-dm/main/install.sh | bashThis one command will:
- Install Node.js (for nostr-tools dependency)
- Install OpenCode
- Install nak (key generator)
- Prompt for your private key (or press Enter to generate a new one)
- Configure the plugin
- Set up systemd service (runs 24/7, survives reboots)
- Start the server automatically
- Print your npub
That's it! Send a DM to the npub shown at the end from any Nostr client (Primal, Damus, Amethyst, etc.) and get AI responses!
Useful commands:
systemctl status opencode # Check status
journalctl -u opencode -f # View logs
systemctl restart opencode # Restart serviceManual Installation
1. Install OpenCode
curl -fsSL https://opencode.ai/install | bash
source ~/.bashrc2. Install Node.js & nostr-tools
curl -fsSL https://deb.nodesource.com/setup_22.x | bash -
apt-get install -y nodejs
cd ~/.config/opencode
npm init -y
npm install nostr-tools3. Install nak & Generate Keypair
curl -sSL https://raw.githubusercontent.com/fiatjaf/nak/master/install.sh | sh
export PATH="$HOME/.local/bin:$PATH"
NSEC=$(nak key generate)
HEX_PUB=$(nak key public $NSEC)
NPUB=$(echo $HEX_PUB | nak encode npub)
echo "Your npub: $NPUB"4. Create Config Files
mkdir -p ~/.config/opencode/plugins
cat > ~/.config/opencode/opencode.json << 'EOF'
{
"$schema": "https://opencode.ai/config.json",
"model": "opencode/big-pickle"
}
EOF
cat > ~/.config/opencode/.env << EOF
NOSTR_PRIVATE_KEY=$NSEC
NOSTR_RELAYS=wss://relay.primal.net,wss://nos.lol,wss://relay.damus.io
NOSTR_DEBUG=true
EOF5. Download Plugin
curl -fsSL https://raw.githubusercontent.com/happylemonprogramming/opencode-nostr-dm/main/plugin.ts \
-o ~/.config/opencode/plugins/nostr-dm.ts6. Start & Test
opencode serve --print-logs
# In another terminal:
curl http://127.0.0.1:4096/eventConfiguration
| Variable | Required | Default | Description |
|----------|----------|---------|-------------|
| NOSTR_PRIVATE_KEY | Yes | - | Private key (nsec1... or hex) |
| NOSTR_RELAYS | No | 3 default relays | Comma-separated relay URLs |
| NOSTR_DEFAULT_MODEL | No | opencode/big-pickle | Default AI model (free) |
| NOSTR_DEBUG | No | false | Enable debug logging |
Edit config: nano ~/.config/opencode/.env
Systemd Service (24/7 Operation)
The quick install automatically sets up a systemd service. If you used manual installation, run:
cat > /etc/systemd/system/opencode.service << 'EOF'
[Unit]
Description=OpenCode Nostr DM Server
After=network.target
[Service]
Type=simple
User=root
Environment="PATH=/root/.local/bin:/usr/local/bin:/usr/bin:/bin"
WorkingDirectory=/root
ExecStart=/root/.local/bin/opencode serve
ExecStartPost=/bin/bash -c 'sleep 3 && curl -s http://127.0.0.1:4096/event > /dev/null'
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable opencode
systemctl start opencodeService commands:
systemctl status opencode # Check status
journalctl -u opencode -f # View logs (follow mode)
systemctl restart opencode # Restart after config changes
systemctl stop opencode # Stop the serviceHow It Works
- Plugin subscribes to NIP-04 DMs tagged to your server's pubkey
- When a DM arrives, it decrypts the message and sends it to OpenCode AI
- OpenCode AI generates a response
- Plugin encrypts and sends the response back via Nostr DM
Each sender gets their own persistent session (until server restart).
Features
- NIP-04 encrypted direct messages
- Automatic session management (one session per sender)
/newcommand support - send/newto clear context and start fresh- Automatic session recovery if session becomes invalid
- Multi-relay support
- Free AI models (no API keys needed)
- Debug logging
Commands
| Command | Description |
|---------|-------------|
| /new | Clear current session and start a fresh conversation |
| /models | Change AI model - prompts for model name, reverts if invalid |
Note: The default model (opencode/big-pickle) is free. To use paid models (e.g., anthropic/claude-sonnet-4-20250514), add the appropriate API key to your .env file (see .env.example).
Troubleshooting
Plugin doesn't load:
- Restart OpenCode and trigger with
curl http://127.0.0.1:4096/event - Check logs for
[NostrDM]messages
No DMs received:
- Verify
NOSTR_PRIVATE_KEYis set:cat ~/.config/opencode/.env - Enable debug:
NOSTR_DEBUG=true - Check you're DMing the correct npub
License
MIT
