lightning-memory 0.4.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- lightning_memory-0.4.0/.gitignore +15 -0
- lightning_memory-0.4.0/LICENSE +21 -0
- lightning_memory-0.4.0/PKG-INFO +304 -0
- lightning_memory-0.4.0/README.md +267 -0
- lightning_memory-0.4.0/SKILL.md +100 -0
- lightning_memory-0.4.0/lightning_memory/__init__.py +3 -0
- lightning_memory-0.4.0/lightning_memory/config.py +101 -0
- lightning_memory-0.4.0/lightning_memory/db.py +226 -0
- lightning_memory-0.4.0/lightning_memory/gateway.py +340 -0
- lightning_memory-0.4.0/lightning_memory/intelligence.py +162 -0
- lightning_memory-0.4.0/lightning_memory/l402.py +81 -0
- lightning_memory-0.4.0/lightning_memory/lightning.py +82 -0
- lightning_memory-0.4.0/lightning_memory/macaroon.py +141 -0
- lightning_memory-0.4.0/lightning_memory/memory.py +151 -0
- lightning_memory-0.4.0/lightning_memory/nostr.py +206 -0
- lightning_memory-0.4.0/lightning_memory/phoenixd.py +132 -0
- lightning_memory-0.4.0/lightning_memory/relay.py +141 -0
- lightning_memory-0.4.0/lightning_memory/server.py +307 -0
- lightning_memory-0.4.0/lightning_memory/sync.py +268 -0
- lightning_memory-0.4.0/pyproject.toml +50 -0
- lightning_memory-0.4.0/server.json +33 -0
- lightning_memory-0.4.0/tests/__init__.py +0 -0
- lightning_memory-0.4.0/tests/conftest.py +29 -0
- lightning_memory-0.4.0/tests/test_config.py +67 -0
- lightning_memory-0.4.0/tests/test_db.py +155 -0
- lightning_memory-0.4.0/tests/test_gateway.py +337 -0
- lightning_memory-0.4.0/tests/test_intelligence.py +165 -0
- lightning_memory-0.4.0/tests/test_l402.py +141 -0
- lightning_memory-0.4.0/tests/test_macaroon.py +155 -0
- lightning_memory-0.4.0/tests/test_memory.py +99 -0
- lightning_memory-0.4.0/tests/test_nostr.py +111 -0
- lightning_memory-0.4.0/tests/test_relay.py +148 -0
- lightning_memory-0.4.0/tests/test_server.py +64 -0
- lightning_memory-0.4.0/tests/test_signing.py +91 -0
- lightning_memory-0.4.0/tests/test_sync.py +202 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jason Sosa
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: lightning-memory
|
|
3
|
+
Version: 0.4.0
|
|
4
|
+
Summary: Decentralized agent memory for the Lightning economy. Nostr identity, L402 payments, semantic search.
|
|
5
|
+
Project-URL: Homepage, https://github.com/singularityjason/lightning-memory
|
|
6
|
+
Project-URL: Repository, https://github.com/singularityjason/lightning-memory
|
|
7
|
+
Project-URL: Issues, https://github.com/singularityjason/lightning-memory/issues
|
|
8
|
+
Author: Jason Sosa
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: agent-memory,ai-agents,l402,lightning,mcp,nostr
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
17
|
+
Requires-Python: >=3.10
|
|
18
|
+
Requires-Dist: mcp>=1.0.0
|
|
19
|
+
Provides-Extra: crypto
|
|
20
|
+
Requires-Dist: secp256k1>=0.14.0; extra == 'crypto'
|
|
21
|
+
Provides-Extra: dev
|
|
22
|
+
Requires-Dist: httpx>=0.27; extra == 'dev'
|
|
23
|
+
Requires-Dist: pytest; extra == 'dev'
|
|
24
|
+
Requires-Dist: pytest-asyncio; extra == 'dev'
|
|
25
|
+
Requires-Dist: secp256k1>=0.14.0; extra == 'dev'
|
|
26
|
+
Requires-Dist: starlette>=0.40; extra == 'dev'
|
|
27
|
+
Requires-Dist: uvicorn>=0.30; extra == 'dev'
|
|
28
|
+
Requires-Dist: websockets>=12.0; extra == 'dev'
|
|
29
|
+
Provides-Extra: gateway
|
|
30
|
+
Requires-Dist: httpx>=0.27; extra == 'gateway'
|
|
31
|
+
Requires-Dist: starlette>=0.40; extra == 'gateway'
|
|
32
|
+
Requires-Dist: uvicorn>=0.30; extra == 'gateway'
|
|
33
|
+
Provides-Extra: sync
|
|
34
|
+
Requires-Dist: secp256k1>=0.14.0; extra == 'sync'
|
|
35
|
+
Requires-Dist: websockets>=12.0; extra == 'sync'
|
|
36
|
+
Description-Content-Type: text/markdown
|
|
37
|
+
|
|
38
|
+
# Lightning Memory
|
|
39
|
+
|
|
40
|
+
Decentralized agent memory for the Lightning economy. Store, query, and recall memories with cryptographic identity (Nostr) and micropayments (Lightning/L402).
|
|
41
|
+
|
|
42
|
+
**The problem:** AI agents can transact via Lightning (L402) but can't remember what they paid for, which vendors are reliable, or their spending patterns. Lightning Memory fixes this.
|
|
43
|
+
|
|
44
|
+
## Architecture
|
|
45
|
+
|
|
46
|
+
```
|
|
47
|
+
L1: Bitcoin (settlement)
|
|
48
|
+
L2: Lightning Network (payments, L402)
|
|
49
|
+
L3: Lightning Memory (agent memory protocol)
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
- **Nostr identity**: Agent identity = Nostr keypair. No accounts, no API keys.
|
|
53
|
+
- **Local-first**: SQLite with FTS5 full-text search. Works offline, zero dependencies.
|
|
54
|
+
- **Nostr sync** (Phase 2): Memories written as NIP-78 events to relays. Portable, tamper-proof.
|
|
55
|
+
- **L402 payments** (Phase 3): Pay-per-query hosted service. 1-5 sats per operation.
|
|
56
|
+
|
|
57
|
+
## Quick Start
|
|
58
|
+
|
|
59
|
+
### Install
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
pip install lightning-memory
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Or from source:
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
git clone https://github.com/singularityjason/lightning-memory
|
|
69
|
+
cd lightning-memory
|
|
70
|
+
pip install -e .
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Run as MCP Server
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
lightning-memory
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Configure in Claude Code
|
|
80
|
+
|
|
81
|
+
Add to your MCP config (`~/.claude/config.json`):
|
|
82
|
+
|
|
83
|
+
```json
|
|
84
|
+
{
|
|
85
|
+
"mcpServers": {
|
|
86
|
+
"lightning-memory": {
|
|
87
|
+
"command": "lightning-memory"
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Configure in Claude Desktop
|
|
94
|
+
|
|
95
|
+
Add to `claude_desktop_config.json`:
|
|
96
|
+
|
|
97
|
+
```json
|
|
98
|
+
{
|
|
99
|
+
"mcpServers": {
|
|
100
|
+
"lightning-memory": {
|
|
101
|
+
"command": "python",
|
|
102
|
+
"args": ["-m", "lightning_memory.server"]
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## Tools
|
|
109
|
+
|
|
110
|
+
### `memory_store`
|
|
111
|
+
|
|
112
|
+
Store a memory for later retrieval.
|
|
113
|
+
|
|
114
|
+
```
|
|
115
|
+
memory_store(
|
|
116
|
+
content="Paid 500 sats to bitrefill.com for a $5 Amazon gift card via L402. Fast, reliable.",
|
|
117
|
+
type="transaction",
|
|
118
|
+
metadata='{"vendor": "bitrefill.com", "amount_sats": 500}'
|
|
119
|
+
)
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
**Types:** `general`, `transaction`, `vendor`, `preference`, `error`, `decision`
|
|
123
|
+
|
|
124
|
+
### `memory_query`
|
|
125
|
+
|
|
126
|
+
Search memories by relevance.
|
|
127
|
+
|
|
128
|
+
```
|
|
129
|
+
memory_query(query="bitrefill payment history", limit=5)
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### `memory_list`
|
|
133
|
+
|
|
134
|
+
List memories with optional filters.
|
|
135
|
+
|
|
136
|
+
```
|
|
137
|
+
memory_list(type="transaction", since="24h", limit=20)
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### `ln_vendor_reputation`
|
|
141
|
+
|
|
142
|
+
Check a vendor's reputation based on transaction history.
|
|
143
|
+
|
|
144
|
+
```
|
|
145
|
+
ln_vendor_reputation(vendor="bitrefill.com")
|
|
146
|
+
# → {reputation: {total_txns: 12, success_rate: 0.92, avg_sats: 450}, recommendation: "reliable"}
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### `ln_spending_summary`
|
|
150
|
+
|
|
151
|
+
Get a spending breakdown for budget awareness.
|
|
152
|
+
|
|
153
|
+
```
|
|
154
|
+
ln_spending_summary(since="30d")
|
|
155
|
+
# → {summary: {total_sats: 15000, by_vendor: {"bitrefill.com": 9000, ...}, txn_count: 25}}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### `ln_anomaly_check`
|
|
159
|
+
|
|
160
|
+
Check if a proposed payment looks normal compared to history.
|
|
161
|
+
|
|
162
|
+
```
|
|
163
|
+
ln_anomaly_check(vendor="bitrefill.com", amount_sats=5000)
|
|
164
|
+
# → {anomaly: {verdict: "high", context: "5000 sats is 11.1x the historical average..."}}
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### `memory_sync`
|
|
168
|
+
|
|
169
|
+
Sync memories with Nostr relays (push and/or pull).
|
|
170
|
+
|
|
171
|
+
```
|
|
172
|
+
memory_sync(direction="both") # "push", "pull", or "both"
|
|
173
|
+
# → {pushed: 5, pulled: 3, errors: []}
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
Requires `pip install lightning-memory[sync]` for relay support.
|
|
177
|
+
|
|
178
|
+
### `memory_export`
|
|
179
|
+
|
|
180
|
+
Export memories as portable NIP-78 Nostr events.
|
|
181
|
+
|
|
182
|
+
```
|
|
183
|
+
memory_export(limit=50)
|
|
184
|
+
# → {count: 50, signed: true, events: [...]}
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### `ln_budget_status`
|
|
188
|
+
|
|
189
|
+
Check L402 gateway earnings and payment stats.
|
|
190
|
+
|
|
191
|
+
```
|
|
192
|
+
ln_budget_status()
|
|
193
|
+
# → {total_earned_sats: 150, total_payments: 42, by_operation: {"memory_query": 80, ...}}
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
## L402 Gateway
|
|
197
|
+
|
|
198
|
+
Lightning Memory includes an L402 pay-per-query HTTP gateway. Remote agents pay Lightning micropayments to query your memory engine — no API keys, no accounts.
|
|
199
|
+
|
|
200
|
+
### Install
|
|
201
|
+
|
|
202
|
+
```bash
|
|
203
|
+
pip install lightning-memory[gateway]
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
### Start
|
|
207
|
+
|
|
208
|
+
```bash
|
|
209
|
+
lightning-memory-gateway
|
|
210
|
+
# Listening on 0.0.0.0:8402
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
### How L402 Works
|
|
214
|
+
|
|
215
|
+
```
|
|
216
|
+
Agent Gateway Phoenixd
|
|
217
|
+
| | |
|
|
218
|
+
|-- GET /memory/query?q=... --->| |
|
|
219
|
+
|<-- 402 + Lightning invoice ---|--- create_invoice -------->|
|
|
220
|
+
| |<-- bolt11 + payment_hash --|
|
|
221
|
+
| | |
|
|
222
|
+
| [pay invoice via Lightning] | |
|
|
223
|
+
| | |
|
|
224
|
+
|-- GET + L402 token ---------->| |
|
|
225
|
+
| (macaroon:preimage) |--- verify preimage ------->|
|
|
226
|
+
|<-- 200 + query results -------| |
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
### Endpoints
|
|
230
|
+
|
|
231
|
+
| Endpoint | Method | Price | Description |
|
|
232
|
+
|----------|--------|-------|-------------|
|
|
233
|
+
| `/info` | GET | Free | Gateway status, pricing, node info |
|
|
234
|
+
| `/health` | GET | Free | Health check |
|
|
235
|
+
| `/memory/store` | POST | 3 sats | Store a memory |
|
|
236
|
+
| `/memory/query` | GET | 2 sats | Search memories by relevance |
|
|
237
|
+
| `/memory/list` | GET | 1 sat | List memories with filters |
|
|
238
|
+
| `/ln/vendor/{name}` | GET | 3 sats | Vendor reputation report |
|
|
239
|
+
| `/ln/spending` | GET | 2 sats | Spending summary |
|
|
240
|
+
| `/ln/anomaly-check` | POST | 3 sats | Payment anomaly detection |
|
|
241
|
+
|
|
242
|
+
### Phoenixd Setup
|
|
243
|
+
|
|
244
|
+
The gateway needs a Lightning node to create invoices. [Phoenixd](https://phoenix.acinq.co/server) is the simplest option — zero config, auto channel management.
|
|
245
|
+
|
|
246
|
+
1. Download and run Phoenixd (listens on `localhost:9740`)
|
|
247
|
+
2. Fund it with ~10,000 sats for initial channel opening
|
|
248
|
+
3. Configure the gateway:
|
|
249
|
+
|
|
250
|
+
```bash
|
|
251
|
+
mkdir -p ~/.lightning-memory
|
|
252
|
+
cat > ~/.lightning-memory/config.json << 'EOF'
|
|
253
|
+
{
|
|
254
|
+
"phoenixd_password": "<from ~/.phoenix/phoenix.conf>"
|
|
255
|
+
}
|
|
256
|
+
EOF
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
4. Start: `lightning-memory-gateway`
|
|
260
|
+
|
|
261
|
+
### Client Example
|
|
262
|
+
|
|
263
|
+
```bash
|
|
264
|
+
# Using lnget (auto-pays Lightning invoices):
|
|
265
|
+
lnget https://your-server.com/ln/vendor/bitrefill
|
|
266
|
+
|
|
267
|
+
# Manual flow with curl:
|
|
268
|
+
curl https://your-server.com/memory/query?q=openai+rate+limits
|
|
269
|
+
# → 402 + invoice in WWW-Authenticate header
|
|
270
|
+
# Pay the invoice, extract preimage
|
|
271
|
+
curl -H "Authorization: L402 <macaroon>:<preimage>" \
|
|
272
|
+
https://your-server.com/memory/query?q=openai+rate+limits
|
|
273
|
+
# → 200 + relevant memories
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
## How It Works
|
|
277
|
+
|
|
278
|
+
1. **First run**: A Nostr keypair is generated and stored at `~/.lightning-memory/keys/`
|
|
279
|
+
2. **Storing**: Memories go to local SQLite with FTS5 indexing. Each memory is tagged with your agent's public key.
|
|
280
|
+
3. **Querying**: Full-text search with BM25 ranking returns the most relevant memories.
|
|
281
|
+
4. **Identity**: Your agent's public key is a globally unique, cryptographically verifiable identifier. No accounts needed.
|
|
282
|
+
|
|
283
|
+
## Data Storage
|
|
284
|
+
|
|
285
|
+
All data is stored locally:
|
|
286
|
+
|
|
287
|
+
```
|
|
288
|
+
~/.lightning-memory/
|
|
289
|
+
memories.db # SQLite database
|
|
290
|
+
keys/
|
|
291
|
+
private.key # Nostr private key (chmod 600)
|
|
292
|
+
public.key # Nostr public key (your agent identity)
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
## Roadmap
|
|
296
|
+
|
|
297
|
+
- [x] Phase 1: MCP server with local SQLite storage
|
|
298
|
+
- [x] Phase 2: Lightning intelligence layer (vendor reputation, spending summary, anomaly detection)
|
|
299
|
+
- [x] Phase 3: Nostr relay sync (NIP-78 events, Schnorr signing, bidirectional sync)
|
|
300
|
+
- [x] Phase 4: L402 payment gateway (macaroons, Phoenixd, Starlette HTTP gateway)
|
|
301
|
+
|
|
302
|
+
## License
|
|
303
|
+
|
|
304
|
+
MIT
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
# Lightning Memory
|
|
2
|
+
|
|
3
|
+
Decentralized agent memory for the Lightning economy. Store, query, and recall memories with cryptographic identity (Nostr) and micropayments (Lightning/L402).
|
|
4
|
+
|
|
5
|
+
**The problem:** AI agents can transact via Lightning (L402) but can't remember what they paid for, which vendors are reliable, or their spending patterns. Lightning Memory fixes this.
|
|
6
|
+
|
|
7
|
+
## Architecture
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
L1: Bitcoin (settlement)
|
|
11
|
+
L2: Lightning Network (payments, L402)
|
|
12
|
+
L3: Lightning Memory (agent memory protocol)
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
- **Nostr identity**: Agent identity = Nostr keypair. No accounts, no API keys.
|
|
16
|
+
- **Local-first**: SQLite with FTS5 full-text search. Works offline, zero dependencies.
|
|
17
|
+
- **Nostr sync** (Phase 2): Memories written as NIP-78 events to relays. Portable, tamper-proof.
|
|
18
|
+
- **L402 payments** (Phase 3): Pay-per-query hosted service. 1-5 sats per operation.
|
|
19
|
+
|
|
20
|
+
## Quick Start
|
|
21
|
+
|
|
22
|
+
### Install
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pip install lightning-memory
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Or from source:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
git clone https://github.com/singularityjason/lightning-memory
|
|
32
|
+
cd lightning-memory
|
|
33
|
+
pip install -e .
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### Run as MCP Server
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
lightning-memory
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Configure in Claude Code
|
|
43
|
+
|
|
44
|
+
Add to your MCP config (`~/.claude/config.json`):
|
|
45
|
+
|
|
46
|
+
```json
|
|
47
|
+
{
|
|
48
|
+
"mcpServers": {
|
|
49
|
+
"lightning-memory": {
|
|
50
|
+
"command": "lightning-memory"
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Configure in Claude Desktop
|
|
57
|
+
|
|
58
|
+
Add to `claude_desktop_config.json`:
|
|
59
|
+
|
|
60
|
+
```json
|
|
61
|
+
{
|
|
62
|
+
"mcpServers": {
|
|
63
|
+
"lightning-memory": {
|
|
64
|
+
"command": "python",
|
|
65
|
+
"args": ["-m", "lightning_memory.server"]
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Tools
|
|
72
|
+
|
|
73
|
+
### `memory_store`
|
|
74
|
+
|
|
75
|
+
Store a memory for later retrieval.
|
|
76
|
+
|
|
77
|
+
```
|
|
78
|
+
memory_store(
|
|
79
|
+
content="Paid 500 sats to bitrefill.com for a $5 Amazon gift card via L402. Fast, reliable.",
|
|
80
|
+
type="transaction",
|
|
81
|
+
metadata='{"vendor": "bitrefill.com", "amount_sats": 500}'
|
|
82
|
+
)
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
**Types:** `general`, `transaction`, `vendor`, `preference`, `error`, `decision`
|
|
86
|
+
|
|
87
|
+
### `memory_query`
|
|
88
|
+
|
|
89
|
+
Search memories by relevance.
|
|
90
|
+
|
|
91
|
+
```
|
|
92
|
+
memory_query(query="bitrefill payment history", limit=5)
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### `memory_list`
|
|
96
|
+
|
|
97
|
+
List memories with optional filters.
|
|
98
|
+
|
|
99
|
+
```
|
|
100
|
+
memory_list(type="transaction", since="24h", limit=20)
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### `ln_vendor_reputation`
|
|
104
|
+
|
|
105
|
+
Check a vendor's reputation based on transaction history.
|
|
106
|
+
|
|
107
|
+
```
|
|
108
|
+
ln_vendor_reputation(vendor="bitrefill.com")
|
|
109
|
+
# → {reputation: {total_txns: 12, success_rate: 0.92, avg_sats: 450}, recommendation: "reliable"}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### `ln_spending_summary`
|
|
113
|
+
|
|
114
|
+
Get a spending breakdown for budget awareness.
|
|
115
|
+
|
|
116
|
+
```
|
|
117
|
+
ln_spending_summary(since="30d")
|
|
118
|
+
# → {summary: {total_sats: 15000, by_vendor: {"bitrefill.com": 9000, ...}, txn_count: 25}}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### `ln_anomaly_check`
|
|
122
|
+
|
|
123
|
+
Check if a proposed payment looks normal compared to history.
|
|
124
|
+
|
|
125
|
+
```
|
|
126
|
+
ln_anomaly_check(vendor="bitrefill.com", amount_sats=5000)
|
|
127
|
+
# → {anomaly: {verdict: "high", context: "5000 sats is 11.1x the historical average..."}}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### `memory_sync`
|
|
131
|
+
|
|
132
|
+
Sync memories with Nostr relays (push and/or pull).
|
|
133
|
+
|
|
134
|
+
```
|
|
135
|
+
memory_sync(direction="both") # "push", "pull", or "both"
|
|
136
|
+
# → {pushed: 5, pulled: 3, errors: []}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Requires `pip install lightning-memory[sync]` for relay support.
|
|
140
|
+
|
|
141
|
+
### `memory_export`
|
|
142
|
+
|
|
143
|
+
Export memories as portable NIP-78 Nostr events.
|
|
144
|
+
|
|
145
|
+
```
|
|
146
|
+
memory_export(limit=50)
|
|
147
|
+
# → {count: 50, signed: true, events: [...]}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### `ln_budget_status`
|
|
151
|
+
|
|
152
|
+
Check L402 gateway earnings and payment stats.
|
|
153
|
+
|
|
154
|
+
```
|
|
155
|
+
ln_budget_status()
|
|
156
|
+
# → {total_earned_sats: 150, total_payments: 42, by_operation: {"memory_query": 80, ...}}
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## L402 Gateway
|
|
160
|
+
|
|
161
|
+
Lightning Memory includes an L402 pay-per-query HTTP gateway. Remote agents pay Lightning micropayments to query your memory engine — no API keys, no accounts.
|
|
162
|
+
|
|
163
|
+
### Install
|
|
164
|
+
|
|
165
|
+
```bash
|
|
166
|
+
pip install lightning-memory[gateway]
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### Start
|
|
170
|
+
|
|
171
|
+
```bash
|
|
172
|
+
lightning-memory-gateway
|
|
173
|
+
# Listening on 0.0.0.0:8402
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### How L402 Works
|
|
177
|
+
|
|
178
|
+
```
|
|
179
|
+
Agent Gateway Phoenixd
|
|
180
|
+
| | |
|
|
181
|
+
|-- GET /memory/query?q=... --->| |
|
|
182
|
+
|<-- 402 + Lightning invoice ---|--- create_invoice -------->|
|
|
183
|
+
| |<-- bolt11 + payment_hash --|
|
|
184
|
+
| | |
|
|
185
|
+
| [pay invoice via Lightning] | |
|
|
186
|
+
| | |
|
|
187
|
+
|-- GET + L402 token ---------->| |
|
|
188
|
+
| (macaroon:preimage) |--- verify preimage ------->|
|
|
189
|
+
|<-- 200 + query results -------| |
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### Endpoints
|
|
193
|
+
|
|
194
|
+
| Endpoint | Method | Price | Description |
|
|
195
|
+
|----------|--------|-------|-------------|
|
|
196
|
+
| `/info` | GET | Free | Gateway status, pricing, node info |
|
|
197
|
+
| `/health` | GET | Free | Health check |
|
|
198
|
+
| `/memory/store` | POST | 3 sats | Store a memory |
|
|
199
|
+
| `/memory/query` | GET | 2 sats | Search memories by relevance |
|
|
200
|
+
| `/memory/list` | GET | 1 sat | List memories with filters |
|
|
201
|
+
| `/ln/vendor/{name}` | GET | 3 sats | Vendor reputation report |
|
|
202
|
+
| `/ln/spending` | GET | 2 sats | Spending summary |
|
|
203
|
+
| `/ln/anomaly-check` | POST | 3 sats | Payment anomaly detection |
|
|
204
|
+
|
|
205
|
+
### Phoenixd Setup
|
|
206
|
+
|
|
207
|
+
The gateway needs a Lightning node to create invoices. [Phoenixd](https://phoenix.acinq.co/server) is the simplest option — zero config, auto channel management.
|
|
208
|
+
|
|
209
|
+
1. Download and run Phoenixd (listens on `localhost:9740`)
|
|
210
|
+
2. Fund it with ~10,000 sats for initial channel opening
|
|
211
|
+
3. Configure the gateway:
|
|
212
|
+
|
|
213
|
+
```bash
|
|
214
|
+
mkdir -p ~/.lightning-memory
|
|
215
|
+
cat > ~/.lightning-memory/config.json << 'EOF'
|
|
216
|
+
{
|
|
217
|
+
"phoenixd_password": "<from ~/.phoenix/phoenix.conf>"
|
|
218
|
+
}
|
|
219
|
+
EOF
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
4. Start: `lightning-memory-gateway`
|
|
223
|
+
|
|
224
|
+
### Client Example
|
|
225
|
+
|
|
226
|
+
```bash
|
|
227
|
+
# Using lnget (auto-pays Lightning invoices):
|
|
228
|
+
lnget https://your-server.com/ln/vendor/bitrefill
|
|
229
|
+
|
|
230
|
+
# Manual flow with curl:
|
|
231
|
+
curl https://your-server.com/memory/query?q=openai+rate+limits
|
|
232
|
+
# → 402 + invoice in WWW-Authenticate header
|
|
233
|
+
# Pay the invoice, extract preimage
|
|
234
|
+
curl -H "Authorization: L402 <macaroon>:<preimage>" \
|
|
235
|
+
https://your-server.com/memory/query?q=openai+rate+limits
|
|
236
|
+
# → 200 + relevant memories
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
## How It Works
|
|
240
|
+
|
|
241
|
+
1. **First run**: A Nostr keypair is generated and stored at `~/.lightning-memory/keys/`
|
|
242
|
+
2. **Storing**: Memories go to local SQLite with FTS5 indexing. Each memory is tagged with your agent's public key.
|
|
243
|
+
3. **Querying**: Full-text search with BM25 ranking returns the most relevant memories.
|
|
244
|
+
4. **Identity**: Your agent's public key is a globally unique, cryptographically verifiable identifier. No accounts needed.
|
|
245
|
+
|
|
246
|
+
## Data Storage
|
|
247
|
+
|
|
248
|
+
All data is stored locally:
|
|
249
|
+
|
|
250
|
+
```
|
|
251
|
+
~/.lightning-memory/
|
|
252
|
+
memories.db # SQLite database
|
|
253
|
+
keys/
|
|
254
|
+
private.key # Nostr private key (chmod 600)
|
|
255
|
+
public.key # Nostr public key (your agent identity)
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
## Roadmap
|
|
259
|
+
|
|
260
|
+
- [x] Phase 1: MCP server with local SQLite storage
|
|
261
|
+
- [x] Phase 2: Lightning intelligence layer (vendor reputation, spending summary, anomaly detection)
|
|
262
|
+
- [x] Phase 3: Nostr relay sync (NIP-78 events, Schnorr signing, bidirectional sync)
|
|
263
|
+
- [x] Phase 4: L402 payment gateway (macaroons, Phoenixd, Starlette HTTP gateway)
|
|
264
|
+
|
|
265
|
+
## License
|
|
266
|
+
|
|
267
|
+
MIT
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: lightning-memory
|
|
3
|
+
description: Agent memory + intelligence for the Lightning economy. Remember transactions, check vendor reputations, track spending, detect anomalies.
|
|
4
|
+
category: memory
|
|
5
|
+
tags: [lightning, nostr, l402, memory, agent]
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
# Lightning Memory
|
|
9
|
+
|
|
10
|
+
You have access to persistent memory via the Lightning Memory MCP server.
|
|
11
|
+
|
|
12
|
+
## When to Use
|
|
13
|
+
|
|
14
|
+
- **After transactions**: Store payment details, amounts, vendors, and outcomes
|
|
15
|
+
- **After API calls**: Record rate limits, errors, response quality
|
|
16
|
+
- **Before purchases**: Check vendor reputation and detect price anomalies
|
|
17
|
+
- **For budgeting**: Review spending summaries by vendor and protocol
|
|
18
|
+
- **For decisions**: Store and recall reasoning for spending decisions
|
|
19
|
+
- **For patterns**: Track recurring errors, preferences, or behaviors
|
|
20
|
+
|
|
21
|
+
## Core Tools (Memory)
|
|
22
|
+
|
|
23
|
+
### Store a memory
|
|
24
|
+
```
|
|
25
|
+
memory_store(content="...", type="transaction|vendor|preference|error|decision|general")
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### Search memories
|
|
29
|
+
```
|
|
30
|
+
memory_query(query="...", limit=10)
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Browse memories
|
|
34
|
+
```
|
|
35
|
+
memory_list(type="transaction", since="24h")
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Intelligence Tools (Lightning)
|
|
39
|
+
|
|
40
|
+
### Check vendor reputation
|
|
41
|
+
```
|
|
42
|
+
ln_vendor_reputation(vendor="bitrefill.com")
|
|
43
|
+
```
|
|
44
|
+
Returns: transaction count, total sats, success rate, recommendation.
|
|
45
|
+
|
|
46
|
+
### Get spending summary
|
|
47
|
+
```
|
|
48
|
+
ln_spending_summary(since="30d")
|
|
49
|
+
```
|
|
50
|
+
Returns: total sats, breakdown by vendor and protocol.
|
|
51
|
+
|
|
52
|
+
### Check for price anomalies
|
|
53
|
+
```
|
|
54
|
+
ln_anomaly_check(vendor="bitrefill.com", amount_sats=5000)
|
|
55
|
+
```
|
|
56
|
+
Returns: verdict (normal/high/first_time), historical average, context.
|
|
57
|
+
|
|
58
|
+
### Check gateway earnings
|
|
59
|
+
```
|
|
60
|
+
ln_budget_status()
|
|
61
|
+
```
|
|
62
|
+
Returns: total sats earned from L402 payments, payment count, breakdown by operation.
|
|
63
|
+
|
|
64
|
+
## Sync Tools (Nostr)
|
|
65
|
+
|
|
66
|
+
### Sync with relays
|
|
67
|
+
```
|
|
68
|
+
memory_sync(direction="both")
|
|
69
|
+
```
|
|
70
|
+
Push local memories to Nostr relays and/or pull remote events. Requires `lightning-memory[sync]`.
|
|
71
|
+
|
|
72
|
+
### Export as Nostr events
|
|
73
|
+
```
|
|
74
|
+
memory_export(limit=100)
|
|
75
|
+
```
|
|
76
|
+
Export memories as portable NIP-78 events (signed if secp256k1 available).
|
|
77
|
+
|
|
78
|
+
## Memory Types
|
|
79
|
+
|
|
80
|
+
| Type | Use for |
|
|
81
|
+
|------|---------|
|
|
82
|
+
| `transaction` | Payment records, invoices, L402 purchases |
|
|
83
|
+
| `vendor` | Service/API reputation, reliability notes |
|
|
84
|
+
| `preference` | User/agent settings, spending caps |
|
|
85
|
+
| `error` | Error patterns, rate limits, failures |
|
|
86
|
+
| `decision` | Key decisions and reasoning |
|
|
87
|
+
| `general` | Everything else |
|
|
88
|
+
|
|
89
|
+
## L402 Gateway
|
|
90
|
+
|
|
91
|
+
Lightning Memory also runs as a paid HTTP gateway (`lightning-memory-gateway`) on port 8402. Remote agents pay Lightning micropayments to query your memory engine. Use `ln_budget_status` to check earnings.
|
|
92
|
+
|
|
93
|
+
## Best Practices
|
|
94
|
+
|
|
95
|
+
1. Be descriptive in content. Include amounts, vendor names, outcomes.
|
|
96
|
+
2. Use the right type. It helps with filtering and recall.
|
|
97
|
+
3. Include `vendor` and `amount_sats` in metadata for transactions — the intelligence tools use these fields.
|
|
98
|
+
4. Query before acting. Check if you've dealt with a vendor or API before.
|
|
99
|
+
5. Use `ln_anomaly_check` before large payments. It catches price spikes.
|
|
100
|
+
6. Store errors. Pattern recognition across sessions catches reliability issues early.
|