# thomaslix7/satsback architecture

> Architecture diagram of the GitHub repository thomaslix7/satsback, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/thomaslix7/satsback
- Repository: https://github.com/thomaslix7/satsback

## Overview

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: [`packages/satsback-sdk/src/tools/fetch_page.ts`](https://github.com/thomaslix7/satsback/blob/main/packages/satsback-sdk/src/tools/fetch_page.ts) (Fetch page), [`lib/db.ts`](https://github.com/thomaslix7/satsback/blob/main/lib/db.ts) (Database), [`packages/satsback-sdk/src/wrap.ts`](https://github.com/thomaslix7/satsback/blob/main/packages/satsback-sdk/src/wrap.ts) (SDK wrapper), [`packages/satsback-sdk/src/tools/cite.ts`](https://github.com/thomaslix7/satsback/blob/main/packages/satsback-sdk/src/tools/cite.ts) (Cite source)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_agent_plane["Agent plane"]
  node_examples_research_agent["Research agent<br/>demo CLI<br/>[index.ts]"]
  node_sdk_wrap["SDK wrapper<br/>tool orchestrator<br/>[wrap.ts]"]
  node_sdk_fetch_page["Fetch page<br/>retrieval tool<br/>[fetch_page.ts]"]
  node_sdk_cite["Cite source<br/>settlement tool<br/>[cite.ts]"]
  node_sdk_identity["SDK identity<br/>nostr auth<br/>[identity.ts]"]
  node_sdk_payments["Payments<br/>lightning settlement<br/>[payments.ts]"]
  node_sdk_receipts["Receipts<br/>audit records<br/>[receipts.ts]"]
  node_sdk_types["SDK types<br/>tool contracts<br/>[types.ts]"]
end

subgraph group_publisher_plane["Publisher plane"]
  node_publisher_route["Publisher page<br/>content route<br/>[route.ts]"]
  node_publisher_policy["Policy manifest<br/>payment policy<br/>[route.ts]"]
end

subgraph group_control_plane["Control plane"]
  node_identity_lib["Identity checks<br/>nostr verification<br/>[identity.ts]"]
  node_reputation_lib["Reputation<br/>agent status<br/>[reputation.ts]"]
  node_publishers_lib["Publishers<br/>publisher registry<br/>[publishers.ts]"]
  node_dashboard_data["Dashboard data<br/>aggregation<br/>[dashboard-data.ts]"]
  node_dashboard_page["Dashboard<br/>read-only UI<br/>[page.tsx]"]
  node_demo_runner["Demo runner<br/>ui harness"]
  node_api_demo_agent["Demo API<br/>orchestration api<br/>[route.ts]"]
  node_api_receipts["Receipts API<br/>receipt api<br/>[route.ts]"]
  node_api_health["Health API<br/>health check<br/>[route.ts]"]
  node_api_agent_status["Agent status API<br/>[route.ts]"]
  node_home_page["Home page<br/>entry page<br/>[page.tsx]"]
end

subgraph group_storage_plane["Storage plane"]
  node_db_lib[("Database<br/>sqlite store<br/>[db.ts]")]
end

node_examples_research_agent -->|"uses"| node_sdk_wrap
node_sdk_wrap -->|"builds"| node_sdk_fetch_page
node_sdk_wrap -->|"builds"| node_sdk_cite
node_sdk_fetch_page -->|"signs"| node_sdk_identity
node_sdk_fetch_page -->|"reads"| node_publisher_route
node_sdk_fetch_page -->|"consults"| node_publisher_policy
node_sdk_cite -->|"settles"| node_sdk_payments
node_sdk_cite -->|"writes"| node_sdk_receipts
node_sdk_payments -->|"uses policy"| node_publisher_policy
node_publisher_route -->|"verifies"| node_identity_lib
node_identity_lib -->|"consults"| node_reputation_lib
node_api_agent_status -->|"serves"| node_reputation_lib
node_api_demo_agent -->|"orchestrates"| node_dashboard_data
node_api_receipts -->|"reads"| node_db_lib
node_dashboard_page -->|"renders"| node_dashboard_data
node_demo_runner -->|"calls"| node_api_demo_agent
node_dashboard_data -->|"queries"| node_db_lib
node_publishers_lib -->|"loads"| node_db_lib
node_api_health -->|"probes"| node_db_lib
node_sdk_receipts -->|"syncs"| node_api_receipts
node_home_page -->|"links"| node_dashboard_page
node_examples_research_agent -->|"checks"| node_api_agent_status

click node_examples_research_agent "https://github.com/thomaslix7/satsback/blob/main/examples/research-agent/index.ts"
click node_sdk_wrap "https://github.com/thomaslix7/satsback/blob/main/packages/satsback-sdk/src/wrap.ts"
click node_sdk_fetch_page "https://github.com/thomaslix7/satsback/blob/main/packages/satsback-sdk/src/tools/fetch_page.ts"
click node_sdk_cite "https://github.com/thomaslix7/satsback/blob/main/packages/satsback-sdk/src/tools/cite.ts"
click node_sdk_identity "https://github.com/thomaslix7/satsback/blob/main/packages/satsback-sdk/src/identity.ts"
click node_sdk_payments "https://github.com/thomaslix7/satsback/blob/main/packages/satsback-sdk/src/payments.ts"
click node_sdk_receipts "https://github.com/thomaslix7/satsback/blob/main/packages/satsback-sdk/src/receipts.ts"
click node_sdk_types "https://github.com/thomaslix7/satsback/blob/main/packages/satsback-sdk/src/types.ts"
click node_publisher_route "https://github.com/thomaslix7/satsback/blob/main/app/sites/[slug]/route.ts"
click node_publisher_policy "https://github.com/thomaslix7/satsback/blob/main/app/sites/[slug]/.well-known/agent-pay.json/route.ts"
click node_identity_lib "https://github.com/thomaslix7/satsback/blob/main/lib/identity.ts"
click node_reputation_lib "https://github.com/thomaslix7/satsback/blob/main/lib/reputation.ts"
click node_publishers_lib "https://github.com/thomaslix7/satsback/blob/main/lib/publishers.ts"
click node_db_lib "https://github.com/thomaslix7/satsback/blob/main/lib/db.ts"
click node_dashboard_data "https://github.com/thomaslix7/satsback/blob/main/lib/dashboard-data.ts"
click node_dashboard_page "https://github.com/thomaslix7/satsback/blob/main/app/dashboard/page.tsx"
click node_demo_runner "https://github.com/thomaslix7/satsback/blob/main/app/dashboard/demo-agent-runner.tsx"
click node_api_demo_agent "https://github.com/thomaslix7/satsback/blob/main/app/api/demo-agent/route.ts"
click node_api_receipts "https://github.com/thomaslix7/satsback/blob/main/app/api/receipts/route.ts"
click node_api_health "https://github.com/thomaslix7/satsback/blob/main/app/api/health/route.ts"
click node_api_agent_status "https://github.com/thomaslix7/satsback/blob/main/app/api/agents/[pubkey]/status/route.ts"
click node_home_page "https://github.com/thomaslix7/satsback/blob/main/app/page.tsx"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_examples_research_agent,node_sdk_wrap,node_sdk_fetch_page,node_sdk_cite,node_sdk_identity,node_sdk_payments,node_sdk_receipts,node_sdk_types toneBlue
class node_publisher_route,node_publisher_policy toneAmber
class node_identity_lib,node_reputation_lib,node_publishers_lib,node_dashboard_data,node_dashboard_page,node_demo_runner,node_api_demo_agent,node_api_receipts,node_api_health,node_api_agent_status,node_home_page toneMint
class node_db_lib toneRose
```

## Components

### Agent plane

Demo agent runtime and reusable SDK boundary

- [Research agent](https://github.com/thomaslix7/satsback/blob/main/examples/research-agent/index.ts): demo CLI · Example agent that runs the tool-calling flow against mock publishers · `examples/research-agent/index.ts`
- [SDK wrapper](https://github.com/thomaslix7/satsback/blob/main/packages/satsback-sdk/src/wrap.ts): tool orchestrator · wrapWithSatsBack() builds the agent-facing fetch and cite tools · `packages/satsback-sdk/src/wrap.ts`
- [Fetch page](https://github.com/thomaslix7/satsback/blob/main/packages/satsback-sdk/src/tools/fetch_page.ts): retrieval tool · Signed source fetch tool used by agents to read publisher content · `packages/satsback-sdk/src/tools/fetch_page.ts`
- [Cite source](https://github.com/thomaslix7/satsback/blob/main/packages/satsback-sdk/src/tools/cite.ts): settlement tool · Tool that settles payment for used sources and produces receipts · `packages/satsback-sdk/src/tools/cite.ts`
- [SDK identity](https://github.com/thomaslix7/satsback/blob/main/packages/satsback-sdk/src/identity.ts): nostr auth · Agent-side request identity and signing utilities · `packages/satsback-sdk/src/identity.ts`
- [Payments](https://github.com/thomaslix7/satsback/blob/main/packages/satsback-sdk/src/payments.ts): lightning settlement · Lightning-native payment orchestration used by cite() · `packages/satsback-sdk/src/payments.ts`
- [Receipts](https://github.com/thomaslix7/satsback/blob/main/packages/satsback-sdk/src/receipts.ts): audit records · Receipt generation and payloads for payment and answer traceability · `packages/satsback-sdk/src/receipts.ts`
- [SDK types](https://github.com/thomaslix7/satsback/blob/main/packages/satsback-sdk/src/types.ts): tool contracts · Shared SDK interfaces and data shapes · `packages/satsback-sdk/src/types.ts`

### Publisher plane

Mock publishers and paywall policy endpoints

- [Publisher page](https://github.com/thomaslix7/satsback/blob/main/app/sites/[slug]/route.ts): content route · Serves the mock publisher content for each site slug · `app/sites/[slug]/route.ts`
- [Policy manifest](https://github.com/thomaslix7/satsback/blob/main/app/sites/[slug]/.well-known/agent-pay.json/route.ts): payment policy · 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](https://github.com/thomaslix7/satsback/blob/main/lib/identity.ts): nostr verification · App-side NIP-98 and request verification boundary · `lib/identity.ts`
- [Reputation](https://github.com/thomaslix7/satsback/blob/main/lib/reputation.ts): agent status · Agent verification and status lookup logic · `lib/reputation.ts`
- [Publishers](https://github.com/thomaslix7/satsback/blob/main/lib/publishers.ts): publisher registry · Publisher metadata and lookup helpers · `lib/publishers.ts`
- [Dashboard data](https://github.com/thomaslix7/satsback/blob/main/lib/dashboard-data.ts): aggregation · Aggregates runs, receipts, spending, and earnings for presentation · `lib/dashboard-data.ts`
- [Dashboard](https://github.com/thomaslix7/satsback/blob/main/app/dashboard/page.tsx): read-only UI · Visualizes stored demo runs and payment activity · `app/dashboard/page.tsx`
- [Demo runner](https://github.com/thomaslix7/satsback/blob/main/app/dashboard/demo-agent-runner.tsx): ui harness · Interactive dashboard component that replays the demo agent run · `app/dashboard/demo-agent-runner.tsx`
- [Demo API](https://github.com/thomaslix7/satsback/blob/main/app/api/demo-agent/route.ts): orchestration api · Starts or serves the demo agent workflow · `app/api/demo-agent/route.ts`
- [Receipts API](https://github.com/thomaslix7/satsback/blob/main/app/api/receipts/route.ts): receipt api · Reads or exposes payment receipts for the demo · `app/api/receipts/route.ts`
- [Health API](https://github.com/thomaslix7/satsback/blob/main/app/api/health/route.ts): health check · Operational probe for the application · `app/api/health/route.ts`
- [Agent status API](https://github.com/thomaslix7/satsback/blob/main/app/api/agents/[pubkey]/status/route.ts): status api · Returns reputation or verification status for a pubkey · `app/api/agents/[pubkey]/status/route.ts`
- [Home page](https://github.com/thomaslix7/satsback/blob/main/app/page.tsx): entry page · Main application landing page · `app/page.tsx`

### Storage plane

Local persistence and demo state

- [Database](https://github.com/thomaslix7/satsback/blob/main/lib/db.ts): sqlite store · Local SQLite-backed persistence layer · `lib/db.ts`

## Connections

- 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
