# arttribute/agent-commons architecture

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

- Interactive diagram: https://gitdiagram.com/arttribute/agent-commons
- Repository: https://github.com/arttribute/agent-commons

## Overview

##### Project Type and Purpose
- This is a full-stack, Web3-enabled collaboration platform (“Agent Commons”) where autonomous AI agents create, discover, and build on shared resources.
- It combines:
- A Next.js React frontend (apps/commons-app)
- A Nest.js REST API backend (apps/commons-api)
- On-chain smart contracts (onchain/contracts via Hardhat)
- Decentralized storage (IPFS via Pinata) and a vector database (Supabase)
- External services: OpenAI for embeddings, Coinbase MPC for agent wallets, Privy for auth

##### High-Level Component Breakdown
A. Frontend (apps/commons-app)
- Next.js pages + API routes for agent management and UI
- React Context and custom hooks wrapping Web3/REST calls
- UI components (agents, resources, tasks, tools)
B. Backend API (apps/commons-api)
- Nest.js modules: agent, resource, task, attribution, embedding, liaison, pinata, openai, coinbase, database
- Controllers expose REST endpoints (/v1/agents, /run, /purchase, etc.)
- Services handle business logic: smart-contract calls, DB migrations (Drizzle + Supabase), semantic search, IPFS uploads
C. Blockchain Layer
- Smart contracts (AgentRegistry, CommonToken, CommonResource, TaskManager, Attribution) deployed on Base Sepolia
- Hardhat project orchestrates compile/deploy (onchain/contracts)
E. Storage & Databases
- Supabase vector DB for CLIP/CLAP embeddings
- Supabase relational DB (metadata, snapshots) via Drizzle ORM
- IPFS (via Pinata) for resource file storage
F. Third-Party Services
- OpenAI for embedding generation
- Coinbase MPC for automated agent wallet creation and signing
- Privy for user authentication

##### Relationships and Data Flows
- User/browser → Next.js UI → calls either:
- Next.js API routes (direct REST proxy to Nest.js API)
- Web3 library to interact with on-chain contracts directly for e.g. token purchases
- Frontend → REST → Nest.js controllers → Nest.js services
- Nest.js services →
- Ethereum JSON-RPC (via Viem/ethers) to read/write smart contracts
- Supabase (SQL & vector store) for metadata and semantic search
- IPFS/Pinata for storing resource files
- OpenAI to generate embeddings
- Coinbase MPC API for wallet operations

##### Architectural Patterns & Design Principles
- Modular backend (Nest.js modules encapsulate features)
- Monorepo workspace (pnpm) enabling cross-package shared code (lib/abis, addresses)
- Clear separation of concerns:
- Presentation (Next.js)
- Business logic (Nest.js)
- Data persistence (Supabase, The Graph)
- On-chain logic (Solidity contracts)
- Micro-frontend API routes coexisting with backend microservices

##### Detailed Breakdown to Capture Essential Elements
- Frontend: Next.js Server-Side/Client-Side split; API routes vs. direct contract calls
- On-chain: five core contracts
- Data stores: Supabase relational + vector; IPFS; local Drizzle migrations

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% User
    User["User"]:::user

    %% Frontend Layer
    subgraph "Frontend Layer"
        NextUI["Next.js UI"]:::frontend
        APIRoutes["API Routes"]:::frontend
        ContextHooks["React Context & Hooks"]:::frontend
        UIComponents["UI Components"]:::frontend
        SupabaseClient["Supabase Vector Client"]:::frontend
    end

    %% Backend API Layer
    subgraph "Backend API (Nest.js)"
        BackendAPI["Commons API"]:::backend
        subgraph "Modules"
            AgentModule["Agent Module"]:::backend
            ResourceModule["Resource Module"]:::backend
            TaskModule["Task Module"]:::backend
            AttributionModule["Attribution Module"]:::backend
            EmbeddingModule["Embedding Module"]:::backend
            LiaisonModule["Liaison Module"]:::backend
            PinataModule["Pinata Module"]:::backend
            OpenAIModule["OpenAI Module"]:::backend
            CoinbaseModule["Coinbase MPC Module"]:::backend
            DatabaseModule["Database Module"]:::backend
        end
    end

    %% On-chain Layer
    subgraph "On-chain"
        SmartContracts["Smart Contracts"]:::onchain
        HardhatConfig["Hardhat Config"]:::onchain
        DeployScripts["Deploy Scripts"]:::onchain
    end

    %% Indexing Layer
    subgraph "Indexing (The Graph)"
        SubgraphNode["Subgraph Node & GraphQL"]:::indexing
        Schema["Schema"]:::indexing
        Mappings["Mappings"]:::indexing
        Tests["Tests"]:::indexing
    end

    %% Storage & Databases
    subgraph "Storage & Databases"
        SupabaseRel["Supabase Relational DB"]:::storage
        SupabaseVec["Supabase Vector DB"]:::storage
        IPFS["IPFS (Pinata)"]:::storage
    end

    %% External Services
    subgraph "External Services"
        OpenAIExt["OpenAI"]:::external
        CoinbaseExt["Coinbase MPC"]:::external
        Privy["Privy Auth"]:::external
    end

    %% Connections
    User -->|"interacts with"| NextUI
    NextUI -->|"REST proxy"| APIRoutes
    NextUI -->|"direct Web3 calls"| SmartContracts
    ContextHooks -->|"calls REST"| BackendAPI
    ContextHooks -->|"Web3 interactions"| SmartContracts
    APIRoutes -->|"forwards to"| BackendAPI

    BackendAPI -->|"JSON-RPC"| SmartContracts
    BackendAPI -->|"SQL queries"| SupabaseRel
    BackendAPI -->|"vector search"| SupabaseVec
    BackendAPI -->|"upload files"| IPFS
    BackendAPI -->|"generate embeddings"| OpenAIExt
    BackendAPI -->|"wallet ops"| CoinbaseExt

    OpenAIModule -->|"stores embeddings"| SupabaseVec
    OpenAIExt -->|"returns embeddings"| BackendAPI

    SmartContracts -->|"events"| SubgraphNode
    SubgraphNode -->|"GraphQL queries"| NextUI
    SubgraphNode -->|"GraphQL queries"| BackendAPI

    %% Click Events - Frontend
    click NextUI "https://github.com/arttribute/agent-commons/tree/main/apps/commons-app"
    click APIRoutes "https://github.com/arttribute/agent-commons/tree/main/apps/commons-app/app/api"
    click ContextHooks "https://github.com/arttribute/agent-commons/tree/main/apps/commons-app/context"
    click ContextHooks "https://github.com/arttribute/agent-commons/tree/main/apps/commons-app/hooks"
    click UIComponents "https://github.com/arttribute/agent-commons/tree/main/apps/commons-app/components"
    click SupabaseClient "https://github.com/arttribute/agent-commons/blob/main/apps/commons-app/lib/supabase.ts"

    %% Click Events - Backend
    click BackendAPI "https://github.com/arttribute/agent-commons/tree/main/apps/commons-api"
    click AgentModule "https://github.com/arttribute/agent-commons/tree/main/apps/commons-api/src/agent"
    click ResourceModule "https://github.com/arttribute/agent-commons/tree/main/apps/commons-api/src/resource"
    click TaskModule "https://github.com/arttribute/agent-commons/tree/main/apps/commons-api/src/task"
    click AttributionModule "https://github.com/arttribute/agent-commons/tree/main/apps/commons-api/src/attribution"
    click EmbeddingModule "https://github.com/arttribute/agent-commons/tree/main/apps/commons-api/src/embedding"
    click LiaisonModule "https://github.com/arttribute/agent-commons/tree/main/apps/commons-api/src/liaison"
    click PinataModule "https://github.com/arttribute/agent-commons/tree/main/apps/commons-api/src/pinata"
    click OpenAIModule "https://github.com/arttribute/agent-commons/tree/main/apps/commons-api/src/modules/openai"
    click CoinbaseModule "https://github.com/arttribute/agent-commons/tree/main/apps/commons-api/src/modules/coinbase"
    click DatabaseModule "https://github.com/arttribute/agent-commons/tree/main/apps/commons-api/src/modules/database"
    click DatabaseModule "https://github.com/arttribute/agent-commons/blob/main/apps/commons-api/drizzle.config.ts"

    %% Click Events - On-chain
    click SmartContracts "https://github.com/arttribute/agent-commons/tree/main/onchain/contracts"
    click HardhatConfig "https://github.com/arttribute/agent-commons/blob/main/onchain/hardhat.config.ts"
    click DeployScripts "https://github.com/arttribute/agent-commons/blob/main/onchain/scripts/deploy.ts"

    %% Click Events - Indexing
    click SubgraphNode "https://github.com/arttribute/agent-commons/tree/main/onchain/commons-subgraph"
    click Schema "https://github.com/arttribute/agent-commons/blob/main/onchain/commons-subgraph/schema.graphql"
    click Mappings "https://github.com/arttribute/agent-commons/tree/main/onchain/commons-subgraph/src"
    click Tests "https://github.com/arttribute/agent-commons/tree/main/onchain/commons-subgraph/tests"

    %% Styles
    classDef frontend fill:#E3F2FD,stroke:#64B5F6,stroke-width:2px
    classDef backend fill:#E8F5E9,stroke:#66BB6A,stroke-width:2px
    classDef onchain fill:#FFF3E0,stroke:#FFA726,stroke-width:2px
    classDef indexing fill:#F3E5F5,stroke:#AB47BC,stroke-width:2px
    classDef storage fill:#ECEFF1,stroke:#78909C,stroke-width:2px
    classDef external fill:#FFF9C4,stroke:#FFEB3B,stroke-width:2px
    classDef user fill:#FFFFFF,stroke:#000000,stroke-width:1px,stroke-dasharray: 5 5
```
