How thomaslix7/satsback works
1. This repo is a monorepo centered on a Next.js 15 App Router application plus a reusable TypeScript SDK package. The app provides the demo UI, mock publisher endpoints, and API routes; the SDK package is what an external agent runtime would actually import to fetch sources and pay citations.
2. The architecture splits cleanly into three planes:
- Agent plane: the CLI demo in examples/research-agent/ plus packages/satsback-sdk/
- Publisher plane: app/sites/[slug]/ and app/sites/[slug]/.well-known/agent-pay.json
- Observability / control plane: app/dashboard/, app/api/*, and the lib/* data layer
3. The SDK package is the core reusable boundary. wrapWithSatsBack() builds two tool-call handlers: fetch_page(source) and cite(source, used_for). Internally it handles identity, request signing, payment settlement, and receipts, while keeping the agent runtime agnostic to the LLM provider.
4. Identity is Nostr-based. The README references NIP-98 signed requests, and the repo has lib/identity.ts plus packages/satsback-sdk/src/identity.ts, indicating that agent requests to publishers are authenticated and verified before a publisher returns the “clean” version of content.
5. Publisher behavior is modeled as a policy-and-content pair. Each mock publisher exposes a machine-readable payment policy at /.well-known/agent-pay.json, and the site route under /sites/[slug] serves the content itself. The publisher boundary is where the pay-if-cited model is enforced: verified traffic gets structured Markdown, anonymous traffic gets generic HTML.
6. Payment settlement is Lightning-native and intentionally off-app. The cite() tool ultimately pays a publisher via the MDK agent-wallet CLI, using a Lightning Address from publisher policy/config. This is the economic boundary of the system: the app does not implement a payments network itself, it orchestrates a real external wallet process.
7. Receipts are first-class artifacts. The README and route tree show app/api/receipts and packages/satsback-sdk/src/receipts.ts; receipts capture at least payment_hash, content_hash, and answer_hash. That makes the system auditable: a payment can be tied to both the fetched source and the final agent answer.
8. Persistence is lightweight and local. lib/db.ts suggests SQLite-backed storage, with scripts/reset-db.ts for wiping demo state. lib/dashboard-data.ts likely aggregates database rows into the dashboard’s run, publisher earnings, and agent spending views.
9. Reputation is a separate service boundary, not embedded inside the payment flow. lib/reputation.ts, lib/publishers.ts, and the API route app/api/agents/[pubkey]/status/route.ts indicate a reputation/status lookup for agents. The demo README says the sample agent is hardcoded as verified, implying the reputation system is intentionally simple in this repo but isolated for future expansion.
10. The dashboard is a read-only presentation layer over the stored demo artifacts. app/dashboard/page.tsx and app/dashboard/demo-agent-runner.tsx render the result of the agent run, aggregate publisher earnings, and show per-agent spending. It is not the system of record; it visualizes what the API and database captured.
11. There is a small set of supporting API endpoints for demo orchestration and health. The tree shows /api/health, /api/demo-agent, /api/receipts, and /api/agents/[pubkey]/status. These act as the operational surface for the demo rather than user-facing product logic.
12. The agent demo itself lives outside the app runtime in examples/research-agent/index.ts. The README says it uses any OpenAI-compatible LLM API and maps tools a, b, and c to local mock publishers. That makes the agent flow: LLM decides which sources matter, calls fetch_page() on candidates, then selectively calls cite() on the sources actually used.
13. Overall data flow: agent request → signed fetch to publisher → publisher returns content according to policy → agent composes answer → cite() triggers Lightning payment → receipt written via API/storage → dashboard reads from SQLite and shows earnings/spending/receipts. The key architectural idea is that citation and settlement are coupled, while retrieval and presentation remain separate.
14. The main external dependencies materially shaping the architecture are Next.js App Router, SQLite (better-sqlite3 implied by README), Nostr/NIP-98 for identity, Lightning via MDK agent-wallet, and an OpenAI-compatible LLM for the demo agent. These are not incidental tooling choices; they define the trust, settlement, and execution boundaries of the system.
Main entry points: fetch_page.ts Fetch page · db.ts Database · wrap.ts SDK wrapper · cite.ts Cite source
Components 22
Agent plane
Demo agent runtime and reusable SDK boundary
- Research agent · Example agent that runs the tool-calling flow against mock publishers
examples/research-agent/index.ts - SDK wrapper · wrapWithSatsBack() builds the agent-facing fetch and cite tools
packages/satsback-sdk/src/wrap.ts - Fetch page · Signed source fetch tool used by agents to read publisher content
packages/satsback-sdk/src/tools/fetch_page.ts - Cite source · Tool that settles payment for used sources and produces receipts
packages/satsback-sdk/src/tools/cite.ts - SDK identity · Agent-side request identity and signing utilities
packages/satsback-sdk/src/identity.ts - Payments · Lightning-native payment orchestration used by cite()
packages/satsback-sdk/src/payments.ts - Receipts · Receipt generation and payloads for payment and answer traceability
packages/satsback-sdk/src/receipts.ts - SDK types · Shared SDK interfaces and data shapes
packages/satsback-sdk/src/types.ts
Publisher plane
Mock publishers and paywall policy endpoints
- Publisher page · Serves the mock publisher content for each site slug
app/sites/[slug]/route.ts - Policy manifest · Machine-readable agent-pay policy exposed per publisher
app/sites/[slug]/.well-known/agent-pay.json/route.ts
Control plane
API, dashboard, and shared application logic
- Identity checks · App-side NIP-98 and request verification boundary
lib/identity.ts - Reputation · Agent verification and status lookup logic
lib/reputation.ts - Publishers · Publisher metadata and lookup helpers
lib/publishers.ts - Dashboard data · Aggregates runs, receipts, spending, and earnings for presentation
lib/dashboard-data.ts - Dashboard · Visualizes stored demo runs and payment activity
app/dashboard/page.tsx - Demo runner · Interactive dashboard component that replays the demo agent run
app/dashboard/demo-agent-runner.tsx - Demo API · Starts or serves the demo agent workflow
app/api/demo-agent/route.ts - Receipts API · Reads or exposes payment receipts for the demo
app/api/receipts/route.ts - Health API · Operational probe for the application
app/api/health/route.ts - Agent status API · Returns reputation or verification status for a pubkey
app/api/agents/[pubkey]/status/route.ts - Home page · Main application landing page
app/page.tsx
Storage plane
Local persistence and demo state
- Database · Local SQLite-backed persistence layer
lib/db.ts
Connections 22
- Research agent → SDK wrapper: uses · Example agent imports the reusable SDK boundary
- SDK wrapper → Fetch page: builds · Wrapper exposes retrieval tool
- SDK wrapper → Cite source: builds · Wrapper exposes settlement tool
- Fetch page → SDK identity: signs · Fetches are authenticated before contacting publishers
- Fetch page → Publisher page: reads · Retrieves publisher content from site routes
- Fetch page → Policy manifest: consults · Uses publisher policy and payment metadata
- Cite source → Payments: settles · Cite tool delegates Lightning payment execution
- Cite source → Receipts: writes · Cite tool produces auditable receipt artifacts
- Payments → Policy manifest: uses policy · Payment settlement relies on publisher Lightning Address and terms
- Publisher page → Identity checks: verifies · Publisher boundary checks signed agent requests
- Identity checks → Reputation: consults · Verification/status is backed by reputation lookup
- Agent status API → Reputation: serves · Status endpoint exposes agent reputation state
- Demo API → Dashboard data: orchestrates · Demo workflow feeds the dashboard-facing aggregates
- Receipts API → Database: reads · Receipts are persisted and retrieved from local storage
- Dashboard → Dashboard data: renders · Dashboard consumes aggregated demo data
- Demo runner → Demo API: calls · UI control triggers the demo run endpoint
- Dashboard data → Database: queries · Aggregations are built from stored demo artifacts
- Publishers → Database: loads · Publisher metadata is backed by local persistence
- Health API → Database: probes · Health surface can reflect storage availability
- Receipts → Receipts API: syncs · Receipt artifacts are surfaced through the app API
- Home page → Dashboard: links · Landing page leads into the read-only demo dashboard
- Research agent → Agent status API: checks · Demo agent can inspect verification status before acting