Deep Mind AI
  • Introduction
  • Examples
    • Getting Started with NextJS
    • Telegram Agent
    • Persistent Agent with PostgreSQL
    • AI Guided Market Making Agent
    • Discord Agent Integration
  • Guides
    • Add your own tool
    • Setup locally
    • Test it out
  • Features
    • Transfer Tokens
    • Stake SOL
    • Deploy SPL Token
    • Check Token Balances
    • Token Data Retrieval
    • Deploy NFT Collection
    • Mint NFT
    • Tensor NFT Marketplace
    • Jupiter Exchange Swaps
    • Solana Name Service (SNS)
    • Launch Token on Pump.fun
Powered by GitBook
On this page
  • Core Features
  • ​Quick Start
  • ​Implementation
  • ​Usage Examples
  • ​Memory Management
  • ​Modes of Operation
  • ​Best Practices
  • ​Common Issues
  • ​Monitoring
  • ​Development Tips
  • ​Resources
  • ​Support
  1. Examples

Persistent Agent with PostgreSQL

Build a Solana Agent with persistent memory using PostgreSQL

PreviousTelegram AgentNextAI Guided Market Making Agent

Last updated 3 months ago

Create a Solana Agent with persistent memory across sessions using PostgreSQL. This implementation enables the agent to maintain context and remember past interactions, providing a more personalized user experience.

Core Features

  1. Memory Persistence

    • Session history storage

    • Cross-session memory

    • Contextual awareness

    • Long-term retention

  2. Database Integration

    • PostgreSQL storage

    • Efficient querying

    • Scalable architecture

    • Data management

Quick Start

1. Setup Database

# Install PostgreSQL (if not already installed)
brew install postgresql

# Start PostgreSQL service
brew services start postgresql

# Create database
createdb agent_memory
# .env.local
DATABASE_URL=postgresql://user:password@localhost:5432/agent_memory
OPENAI_API_KEY=your_openai_key
RPC_URL=your_solana_rpc_url
SOLANA_PRIVATE_KEY=your_wallet_private_key
├── src/
│   ├── db/
│   │   ├── schema.sql
│   │   └── persistence.ts
│   ├── agent/
│   │   └── memory.ts
│   └── index.ts
└── package.json
-- schema.sql
CREATE TABLE IF NOT EXISTS memory (
  id SERIAL PRIMARY KEY,
  session_id TEXT NOT NULL,
  content JSONB NOT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
// memory.ts
import { PostgresSaver } from "langgraph/checkpoint_postgres";

const memorySaver = new PostgresSaver({
  sessionId: "unique-session-id",
  connectionString: process.env.DATABASE_URL,
  tableName: "memory"
});

export const agent = new SolanaAgentKit({
  memory: memorySaver,
  // other configurations
});
// Regular chat without memory
const agent = new SolanaAgentKit();

// First session
await agent.chat("I am Arpit");
// Response: "Hello Arpit! How can I assist you today?"

// New session (no memory)
await agent.chat("Do you know my name?");
// Response: "I don't know your name yet. If you'd like, you can share it."
// Chat with persistent memory
const agent = new SolanaAgentKit({
  memory: memorySaver
});

// First session
await agent.chat("I am Arpit");
// Response: "Hello Arpit! How can I assist you today?"

// New session (with memory)
await agent.chat("Do you know my name?");
// Response: "Yes, you mentioned that your name is Arpit. How can I help you today?"
interface Session {
  id: string;
  createdAt: Date;
  lastAccessed: Date;
  content: {
    messages: Message[];
    context: any;
  };
}

// Create new session
const session = await createSession();

// Restore existing session
const existingSession = await restoreSession(sessionId);
async function saveToMemory(content: any) {
  await db.query(`
    INSERT INTO memory (session_id, content)
    VALUES ($1, $2)
  `, [sessionId, content]);
}

async function retrieveFromMemory(sessionId: string) {
  const result = await db.query(`
    SELECT content FROM memory
    WHERE session_id = $1
    ORDER BY created_at DESC
    LIMIT 1
  `, [sessionId]);
  return result.rows[0]?.content;
}
  • Interactive conversations

  • Memory retention

  • Context awareness

  • Natural responses

  • Autonomous actions

  • Scheduled tasks

  • Event-driven responses

  • Background processing

  1. Database Management

    • Regular backups

    • Index optimization

    • Query efficiency

    • Connection pooling

  2. Memory Optimization

    • Relevant data storage

    • Memory cleanup

    • Session management

    • Data compression

  3. Error Handling

    • Database connectivity

    • Query failures

    • Session errors

    • Memory corruption

  1. Database Connection

    • Connection timeouts

    • Authentication errors

    • Pool exhaustion

    • Network issues

  2. Memory Management

    • Memory leaks

    • Large sessions

    • Slow queries

    • Data consistency

  3. Performance

    • Query optimization

    • Connection pooling

    • Cache usage

    • Resource management

interface MemoryMetrics {
  sessionCount: number;
  averageSessionSize: number;
  queryLatency: number;
  activeConnections: number;
}

async function getMetrics(): Promise<MemoryMetrics> {
  // Implementation
}
async function checkHealth() {
  try {
    // Check database connection
    await db.query('SELECT 1');
    
    // Check memory usage
    const metrics = await getMetrics();
    
    return {
      status: 'healthy',
      metrics
    };
  } catch (error) {
    return {
      status: 'unhealthy',
      error
    };
  }
}
  1. Local Development

    • Use local PostgreSQL

    • Test with sample data

    • Monitor performance

    • Profile queries

  2. Testing

    • Unit tests

    • Integration tests

    • Memory tests

    • Load testing

  3. Deployment

    • Database migration

    • Backup strategy

    • Monitoring setup

    • Scaling plan

For issues and questions:

  • GitHub Issues

  • Documentation

  • Community Forums

  • Development Team

2. Environment Configuration

3. Project Structure

Implementation

Database Schema

Memory Integration

Usage Examples

Without Persistence

With Persistence

Memory Management

Session Handling

Data Storage

Modes of Operation

1. Chat Mode

2. Auto Mode

Best Practices

Common Issues

Monitoring

Key Metrics

Health Checks

Development Tips

Resources

Support

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
PostgreSQL Documentation
LangGraph.js Documentation
Solana Agent Kit GitHub
Database Design Patterns
​