Skip to content

polkassembly/klara-frontend

Repository files navigation

Klara - AI-Powered Governance Assistant

Klara is an AI chatbot built for the Polkadot and Kusama governance ecosystem, integrated with Polkassembly. It helps users query on-chain data, explore governance insights, and understand the entire proposal and voting processβ€”all in natural language.

Features

  • 🧠 AI-Powered Governance Assistant: Natural language queries about Polkadot/Kusama governance
  • πŸ” Web3 Authentication: Secure wallet-based authentication (Polkadot.js, SubWallet, Talisman)
  • πŸ’¬ Real-time Chat: Token-by-token streaming responses with conversation management
  • πŸ—³οΈ Governance Data Querying: Access proposals, referenda, voting data, and treasury information
  • πŸ“Š Voting Analysis: Deep analysis of voter behavior and governance participation
  • πŸ’Ύ Dual Database Support: Firestore for conversations + PostgreSQL for analytics
  • 🎨 Beautiful UI: Polkassembly-inspired design with responsive layout
  • ☁️ Production Ready: Optimized for serverless deployment

Getting Started

Prerequisites

  • Node.js 18+
  • npm or yarn
  • Firebase project (for Firestore)
  • PostgreSQL database (optional, for analytics)

Installation

  1. Clone the repository:
git clone <your-repo-url>
cd polkassembly-frontend
  1. Install dependencies:
npm install
  1. Set up environment variables:
# Copy the example env file
cp .env.example .env.local

# Edit .env.local with your configuration
  1. Run the development server:
npm run dev
  1. Open http://localhost:3000 in your browser.

Configuration

Environment Variables

Create a .env.local file with the following variables:

# API Configuration
API_BASE_URL=https://your-klara-api-endpoint.com
POLKASSEMBLY_AI_TOKEN=your_api_token_here

# Firebase Configuration (Required)
NEXT_PUBLIC_FIREBASE_API_KEY=your_firebase_api_key
NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN=your_project.firebaseapp.com
NEXT_PUBLIC_FIREBASE_PROJECT_ID=your_project_id
NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET=your_project.appspot.com
NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID=your_sender_id
NEXT_PUBLIC_FIREBASE_APP_ID=your_app_id

# PostgreSQL Configuration (Optional - for analytics)
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_DATABASE=polkassembly
POSTGRES_USER=postgres
POSTGRES_PASSWORD=your_password
POSTGRES_SSL=false
DISABLE_POSTGRES=false

# Application Settings
NODE_ENV=development
CONVERSATION_HISTORY_LIMIT=5
JWT_SECRET=your_jwt_secret_key

Database Setup

Firestore (Required): Used for storing conversations and user data

  • Set up a Firebase project and enable Firestore
  • Configure the Firebase environment variables

PostgreSQL (Optional): Used for analytics and query logging

  • Set up PostgreSQL database
  • Tables are automatically created on first run
  • Set DISABLE_POSTGRES=true to disable PostgreSQL features

Usage

Getting Started with Klara

  1. Connect Wallet: Use Polkadot.js, SubWallet, or Talisman to authenticate
  2. Start Chatting: Ask questions about Polkadot/Kusama governance in natural language
  3. Manage Conversations: Create new conversations or continue existing ones
  4. Explore Data: Query proposals, referenda, voting patterns, and treasury information

Example Queries

  • "Show me all active referenda on Polkadot"
  • "Who voted 'nay' on referendum 300?"
  • "List treasury proposals above 100,000 DOT"
  • "Find all bounties created in 2024"
  • "Show voting power analysis for recent referenda"

Project Structure

src/
β”œβ”€β”€ app/                           # Next.js app directory
β”‚   β”œβ”€β”€ api/                      # API routes
β”‚   β”‚   β”œβ”€β”€ auth/wallet/          # Wallet authentication
β”‚   β”‚   β”œβ”€β”€ chat/                 # Chat endpoint with streaming
β”‚   β”‚   β”œβ”€β”€ chat-history/         # Chat history retrieval
β”‚   β”‚   β”œβ”€β”€ conversations/        # Conversation management
β”‚   β”‚   β”œβ”€β”€ conversation-messages/# Message retrieval
β”‚   β”‚   β”œβ”€β”€ feedback/             # User feedback collection
β”‚   β”‚   β”œβ”€β”€ health/               # Health check endpoint
β”‚   β”‚   β”œβ”€β”€ init-user/            # User initialization
β”‚   β”‚   └── stats/                # Statistics endpoint
β”‚   β”œβ”€β”€ feedback/                 # Feedback page
β”‚   β”œβ”€β”€ guide/                    # User guide page
β”‚   β”œβ”€β”€ globals.css               # Global styles
β”‚   β”œβ”€β”€ layout.tsx                # Root layout
β”‚   └── page.tsx                  # Main page
β”œβ”€β”€ components/                   # React components
β”‚   β”œβ”€β”€ ChatInterface.tsx         # Main chat interface
β”‚   β”œβ”€β”€ ConversationSidebar.tsx  # Conversation management
β”‚   β”œβ”€β”€ LoginForm.tsx             # Wallet login form
β”‚   β”œβ”€β”€ MessageBubble.tsx         # Individual message display
β”‚   β”œβ”€β”€ FeedbackForm.tsx          # User feedback form
β”‚   β”œβ”€β”€ Mascot.tsx               # AI mascot component
β”‚   └── WalletsUI/               # Wallet UI components
β”œβ”€β”€ lib/                          # Utility functions
β”‚   β”œβ”€β”€ authService.ts            # Authentication service
β”‚   β”œβ”€β”€ chatCache.ts              # Chat caching
β”‚   β”œβ”€β”€ database.ts               # Firestore operations
β”‚   β”œβ”€β”€ postgres.ts               # PostgreSQL operations
β”‚   β”œβ”€β”€ firebase.ts               # Firebase configuration
β”‚   └── walletService.ts          # Wallet integration
β”œβ”€β”€ hooks/                        # Custom React hooks
└── types/                        # TypeScript definitions
    β”œβ”€β”€ chat.ts                   # Chat-related types
    └── wallet.ts                 # Wallet-related types

Development

Scripts

  • npm run dev - Start development server
  • npm run build - Build for production
  • npm run start - Start production server
  • npm run lint - Run ESLint
  • npm run export-conversations - Export conversations from Firestore
  • npm run analyze-conversations - Analyze conversation data
  • npm run test-auth - Test wallet authentication
  • npm run create-feedback-table - Create PostgreSQL feedback table
  • npm run update-feedback-table - Update PostgreSQL feedback table

Adding New Features

  1. New API Endpoints: Add routes in src/app/api/
  2. New Components: Add in src/components/
  3. Database Operations: Extend src/lib/database.ts (Firestore) or src/lib/postgres.ts (PostgreSQL)
  4. Wallet Integration: Extend src/lib/walletService.ts
  5. Types: Update src/types/ files

Deployment

Vercel (Recommended)

npm run build

Then deploy to Vercel. The application is pre-configured for serverless deployment.

Environment Setup for Production

  1. Set up Firebase project with Firestore
  2. Configure PostgreSQL database (optional)
  3. Set all required environment variables
  4. Deploy to your preferred platform

Other Platforms

The application works on any platform that supports Next.js. Ensure all environment variables are properly configured.

Core Capabilities

Governance Data Querying

  • Proposal Lookup: Fetch proposals or referenda by ID or title
  • Filter by Type: Supports ReferendumV2, Treasury, Fellowship, Bounty, ChildBounty
  • Filter by Network: Switch between Polkadot and Kusama networks
  • Status Filtering: Retrieve proposals by status (Deciding, DecisionDepositPlaced, etc.)
  • Date-Based Search: Query by specific dates, months, or years
  • Text Search: Search in titles or content

Voting Data Analysis

  • Voter Information: Retrieve details of individual voters and their activity
  • Voting Power: Analyze balance and conviction using conviction_vote data
  • Delegation Tracking: Identify delegated votes and delegation relationships
  • Vote Decisions: Filter by aye, nay, or abstain
  • Conviction Analysis: Analyze lock periods and conviction multipliers
  • Top Voters: Rank voters by voting power

License

This project is licensed under the MIT License.

Packages

 
 
 

Contributors