Skip to content

mbt1909432/Acontext-Agent-Playground

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

ACONTEXT Agent Playground

A production-ready starter template for building context-aware, autonomous AI agents

Powered by Acontext

Features Β· Getting Started Β· Deployment Β· Documentation

Next.js TypeScript Supabase Made with Acontext


ACONTEXT Agent Playground is a production-ready starter template for building context-aware, autonomous AI agents powered by Acontext. This template provides a complete foundation with authentication, chat interface, tool integration, and session management, allowing you to quickly build and customize your own AI agent applications.

πŸš€ What is This?

This is a start template that inherits the powerful capabilities of the Acontext platform, including:

  • Session Management: Persistent conversation sessions with automatic context management
  • Semantic Search: Intelligent retrieval of relevant conversation history
  • File & Artifact Management: Upload, store, and manage files through Acontext Disk
  • Experience Learning: Search and reuse learned skills from user Spaces
  • Context Compression: Automatic token management and context optimization
  • Tool Integration: Extensible tool system for custom functionality

✨ Features

Core Capabilities

  • πŸ” Authentication: Email/password authentication with Supabase
  • πŸ’¬ Chat Interface: Full-featured chat UI with message history, streaming responses, and tool call visualization
  • 🎨 Customizable UI: Built with Tailwind CSS and shadcn/ui components - fully customizable
  • πŸ€– Avatar Support: Easy to customize agent avatar and branding
  • πŸ› οΈ Extensible Tools: Add your own custom tools or modify existing ones
  • πŸ“ File Upload: Support for uploading PDFs, images, and documents
  • 🌐 Browser Automation: Integrated Browser Use SDK for web automation tasks
  • πŸŒ“ Theme Support: Dark/light mode with next-themes

Acontext Integration

  • Session Persistence: Each chat session maps to an Acontext Session
  • Semantic Context Search: Automatically retrieves relevant historical context before generating responses
  • Disk Storage: Files are stored in Acontext Disk with automatic tool access
  • Experience Search: Leverages learned skills from user Spaces to improve responses
  • Context Compression: Automatic token management with manual compression option

πŸ› οΈ Tech Stack

  • Framework: Next.js 15+ (App Router)
  • Authentication & Database: Supabase (@supabase/supabase-js, @supabase/ssr)
  • AI Platform: Acontext (@acontext/acontext)
  • LLM: OpenAI (openai SDK) - compatible with OpenAI API
  • Browser Agent: Browser Use SDK (browser-use-sdk)
  • UI Components: Tailwind CSS, shadcn/ui, Lucide icons
  • Language: TypeScript
  • Theming: next-themes

πŸ“¦ Getting Started

Prerequisites

  • Node.js 18+ and npm/yarn/pnpm
  • A Supabase account (create one here)
  • An Acontext account (sign up here)
  • An OpenAI API key (or compatible API endpoint)

Installation

  1. Clone the repository
git clone <your-repo-url> nextjs-with-supabase-acontext
cd nextjs-with-supabase-acontext
  1. Install dependencies
npm install
# or
yarn install
# or
pnpm install
  1. Set up Supabase

    • Create a new project at Supabase Dashboard
    • Note your Project URL and Anon (publishable) key
  2. Configure environment variables

    Create a .env.local file in the root directory:

# Supabase (required)
NEXT_PUBLIC_SUPABASE_URL=your-supabase-project-url
NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY=your-supabase-publishable-or-anon-key

# Acontext (required for full functionality)
ACONTEXT_API_KEY=your-acontext-api-key
ACONTEXT_BASE_URL=https://api.acontext.com/api/v1  # Optional, defaults to this

# OpenAI LLM (required)
OPENAI_LLM_ENDPOINT=https://api.openai.com/v1
OPENAI_LLM_API_KEY=your-openai-api-key
OPENAI_LLM_MODEL=gpt-4o-mini  # or gpt-3.5-turbo, gpt-4, etc.
OPENAI_LLM_TEMPERATURE=0.7
OPENAI_LLM_MAX_TOKENS=2000

# Browser Use Cloud (optional, for browser automation)
BROWSER_USE_API_KEY=your-browser-use-cloud-api-key
  1. Set up database schema

    • Open your Supabase project
    • Go to SQL Editor
    • Run the SQL from specs/001-chatbot-openai/schema.sql
    • This creates tables for chat_sessions and chat_messages
    • For Acontext integration, also run specs/001-chatbot-openai/migration-acontext.sql
  2. Run the development server

npm run dev
# or
yarn dev
# or
pnpm dev

Visit http://localhost:3000 to see your application.

🎨 Customization Guide

Customizing the UI

The UI is built with Tailwind CSS and shadcn/ui components, making it easy to customize:

  1. Theme Colors: Edit tailwind.config.ts to change color schemes
  2. Components: All UI components are in components/ui/ - modify as needed
  3. Layout: Main pages are in app/ directory
  4. Homepage: Customize app/page.tsx for your landing page
  5. Chat Interface: Modify components/chatbot-panel.tsx for chat UI changes

Customizing the Avatar

The agent avatar can be customized in several ways:

  1. Character Image: Replace components/parallax-character.tsx with your own character/logo
  2. Homepage Avatar: Update the parallax character component or homepage hero section
  3. Chat Avatar: Modify the avatar display in components/chatbot-panel.tsx

Example locations:

  • components/parallax-character.tsx - Homepage parallax character
  • public/acontext-character.png - Character assets
  • app/page.tsx - Homepage layout

Adding Custom Tools

Tools are defined as OpenAI function calling schemas. To add your own tool:

  1. Create a tool file in lib/ (e.g., lib/my-custom-tool.ts):
export function getMyToolSchema() {
  return {
    type: "function" as const,
    function: {
      name: "my_custom_tool",
      description: "Description of what your tool does",
      parameters: {
        type: "object",
        properties: {
          param1: {
            type: "string",
            description: "Parameter description",
          },
        },
        required: ["param1"],
      },
    },
  };
}

export async function executeMyTool(args: { param1: string }) {
  // Your tool logic here
  return {
    result: "Tool execution result",
  };
}
  1. Register the tool in lib/openai-client.ts:
// Add to getAvailableTools function
if (shouldIncludeMyTool) {
  tools.push(getMyToolSchema());
}

// Add to executeToolCall function
if (name === "my_custom_tool") {
  const args = JSON.parse(argsJson || "{}");
  return await executeMyTool(args);
}
  1. Wire it into the chat API in app/api/chatbot/route.ts:
// The tools are automatically included when calling chatCompletionStream

Existing Tool Examples:

  • lib/browser-use.ts - Browser automation
  • lib/acontext-disk-tools.ts - File system operations
  • lib/acontext-experience-search-tool.ts - Experience search
  • lib/acontext-todo-tool.ts - Todo management

Customizing Acontext Integration

The Acontext integration is modular and can be customized:

  • Session Management: lib/acontext-integration.ts - Modify session creation and management
  • Client Configuration: lib/acontext-client.ts - Adjust Acontext client settings
  • Tool Integration: lib/acontext-disk-tools.ts - Customize disk tool behavior
  • Experience Search: lib/acontext-experience-search-tool.ts - Modify search logic

πŸ“š Project Structure

nextjs-with-supabase-acontext/
β”œβ”€β”€ app/                    # Next.js app directory
β”‚   β”œβ”€β”€ api/               # API routes
β”‚   β”‚   β”œβ”€β”€ acontext/      # Acontext API endpoints
β”‚   β”‚   β”œβ”€β”€ chatbot/       # Chat API endpoint
β”‚   β”‚   └── tools/         # Tools API endpoint
β”‚   β”œβ”€β”€ auth/              # Authentication pages
β”‚   β”œβ”€β”€ protected/         # Protected chat page
β”‚   └── page.tsx           # Homepage
β”œβ”€β”€ components/             # React components
β”‚   β”œβ”€β”€ ui/                # shadcn/ui components
β”‚   β”œβ”€β”€ chatbot-panel.tsx  # Main chat interface
β”‚   └── parallax-character.tsx  # Homepage character
β”œβ”€β”€ lib/                   # Core libraries
β”‚   β”œβ”€β”€ acontext-*.ts      # Acontext integration
β”‚   β”œβ”€β”€ openai-client.ts   # OpenAI client wrapper
β”‚   β”œβ”€β”€ browser-use.ts    # Browser automation
β”‚   └── supabase/          # Supabase utilities
β”œβ”€β”€ specs/                 # Project specifications
└── types/                 # TypeScript types

πŸ”§ Available Tools

Built-in Tools

  1. Browser Use Task (browser_use_task)

    • Performs web automation tasks
    • Requires Browser Use Cloud API key
    • Location: lib/browser-use.ts
  2. Acontext Disk Tools

    • write_file, read_file, replace_string, list_artifacts, download_file
    • File system operations on Acontext Disk
    • Location: lib/acontext-disk-tools.ts
  3. Experience Search (experience_search)

    • Searches user's learned skills from Acontext Space
    • Location: lib/acontext-experience-search-tool.ts
  4. Todo Management (todo)

    • Create and manage todos within chat sessions
    • Location: lib/acontext-todo-tool.ts

Adding Your Own Tools

See the Customization Guide section above.

🌐 Environment Variables

Variable Description Required
NEXT_PUBLIC_SUPABASE_URL Supabase project URL Yes
NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY Supabase publishable/anon key Yes
ACONTEXT_API_KEY Acontext API key Yes (for full features)
ACONTEXT_BASE_URL Acontext API base URL No (has default)
OPENAI_LLM_ENDPOINT OpenAI-compatible API endpoint Yes
OPENAI_LLM_API_KEY OpenAI API key Yes
OPENAI_LLM_MODEL Model name (e.g., gpt-4o-mini) Yes
OPENAI_LLM_TEMPERATURE Temperature (0-2) No (default: 0.7)
OPENAI_LLM_MAX_TOKENS Max tokens in response No (default: 2000)
BROWSER_USE_API_KEY Browser Use Cloud API key No (optional)
IMAGE_GEN_API_KEY Upstream image generation API key (sent as x-goog-api-key) No (optional, required for image_generate)
IMAGE_GEN_BASE_URL Upstream image generation base URL No (default: https://api.openai-next.com)
IMAGE_GEN_DEFAULT_MODEL Image generation model name (server-controlled; not overridable by tool args) No (default: gemini-3-pro-image-preview)

🚒 Deployment

Deploy to Vercel

Vercel deployment will guide you through creating a Supabase account and project.

After installation of the Supabase integration, all relevant environment variables will be assigned to the project so the deployment is fully functioning.

Deploy with Vercel

The above will also clone the Starter kit to your GitHub, you can clone that locally and develop locally.

Vercel Configuration Explained

When deploying to Vercel, you have two options:

Option 1: One-Click Deploy (Recommended for First-Time Setup)

  1. Click the "Deploy with Vercel" button above
  2. Vercel will prompt you to:
    • Sign in to Vercel (or create an account)
    • Connect your GitHub account (if not already connected)
    • Create a new Supabase project (or connect existing one)
  3. During the Supabase integration setup:
    • Vercel will automatically create a Supabase project for you
    • All required Supabase environment variables will be automatically configured:
      • NEXT_PUBLIC_SUPABASE_URL
      • NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY
  4. You'll need to manually add the remaining environment variables in Vercel dashboard:
    • ACONTEXT_API_KEY - Your Acontext API key
    • OPENAI_LLM_ENDPOINT - OpenAI API endpoint (default: https://api.openai.com/v1)
    • OPENAI_LLM_API_KEY - Your OpenAI API key
    • OPENAI_LLM_MODEL - Model name (e.g., gpt-4o-mini)
    • OPENAI_LLM_TEMPERATURE - Temperature (default: 0.7)
    • OPENAI_LLM_MAX_TOKENS - Max tokens (default: 2000)
    • BROWSER_USE_API_KEY - Optional, for browser automation
    • ACONTEXT_BASE_URL - Optional, defaults to https://api.acontext.com/api/v1
  5. After deployment, run the database migrations:
    • Go to your Supabase project dashboard
    • Navigate to SQL Editor
    • Run the SQL from specs/001-chatbot-openai/schema.sql
    • Run the SQL from specs/001-chatbot-openai/migration-acontext.sql

Option 2: Manual Deployment (If You Already Have a GitHub Repository)

  1. Push your code to GitHub
  2. Go to Vercel Dashboard
  3. Click "Add New Project"
  4. Import your GitHub repository
  5. Configure environment variables:
    • Supabase Integration: Click "Add Integration" β†’ Search "Supabase" β†’ Connect your Supabase project
      • This automatically sets NEXT_PUBLIC_SUPABASE_URL and NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY
    • Manual Variables: Add the remaining environment variables listed above
  6. Click "Deploy"
  7. After deployment, run the database migrations in Supabase SQL Editor

Environment Variables in Vercel

To add environment variables in Vercel:

  1. Go to your project in Vercel Dashboard
  2. Navigate to Settings β†’ Environment Variables
  3. Add each variable with its value
  4. Select which environments to apply to (Production, Preview, Development)
  5. Click "Save"
  6. Redeploy your application for changes to take effect

Important Notes:

  • Environment variables starting with NEXT_PUBLIC_ are exposed to the browser
  • Keep sensitive keys (like OPENAI_LLM_API_KEY, ACONTEXT_API_KEY) secure and never commit them
  • Vercel automatically provides NEXT_PUBLIC_SUPABASE_URL and NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY when you use the Supabase integration

Local Development After Vercel Deployment

If you wish to just develop locally and not deploy to Vercel, follow the steps below:

  1. Clone the repository from GitHub (created during Vercel deployment)
  2. Install dependencies: npm install
  3. Create .env.local file with all environment variables (see Installation section)
  4. Run database migrations in Supabase SQL Editor
  5. Start development server: npm run dev

Other Platforms

This is a standard Next.js application and can be deployed to any platform that supports Next.js:

  • Netlify
  • Railway
  • Render
  • AWS Amplify
  • Self-hosted with Node.js

πŸ”Œ Acontext Integration Details

Implemented Features

1. Session Persistence βœ…

  • Each chat session maps to an Acontext Session (reused, not recreated each time)
  • Database schema includes acontext_session_id field
  • Sessions automatically create or reuse Acontext sessions
  • All messages are stored in the same Acontext session

2. Semantic Search βœ…

  • Retrieves relevant historical context from Acontext before generating responses
  • Searches Acontext session for relevant historical messages before calling LLM
  • Injects search results into system prompt to enhance context coherence
  • Enabled by default, can be disabled with enableSemanticSearch: false

3. File Upload & Artifacts βœ…

  • Support for uploading files (PDFs, images, documents) for AI analysis
  • Chat interface includes file upload button (πŸ“Ž icon)
  • Supports multiple file uploads
  • Files are uploaded to Acontext Disk via Artifacts API
  • Uploaded files are referenced in messages, AI can read file content
  • Image Support: Uses OpenAI Vision API, AI can directly view and analyze images
  • File List View: View all files stored in Acontext Disk

4. File System Tools (Function Calling) βœ…

  • LLM can autonomously read/write files on Acontext Disk through tool functions
  • Includes official DISK_TOOLS exposed via OpenAI Tool Schema: write_file, read_file, replace_string, list_artifacts, download_file
  • Chat API automatically provides these tools when Acontext is configured
  • Tools automatically select existing disk (or create one if needed), no frontend intervention required

File Upload Usage

Frontend (Chat Interface)

  1. Click attachment button: πŸ“Ž (paperclip) icon button next to input box
  2. Select files: Opens file picker, supports multiple files
  3. View preview: Selected files appear above input box with filename
  4. Remove attachment: Click ❌ button next to file to remove
  5. Send message: Enter message and send, files upload with message
  6. View file list: Click "Files" button to view all files in Acontext Disk

API Usage

Send a message with attachments:

const response = await fetch("/api/chatbot", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    message: "Analyze this document",
    sessionId: "your-session-id",
    attachments: [
      {
        filename: "document.pdf",
        content: "base64-encoded-content", // base64 encoded file content
        mimeType: "application/pdf",
      },
      {
        filename: "image.png",
        content: "base64-encoded-image",
        mimeType: "image/png",
      },
    ],
    enableSemanticSearch: true, // optional, default true
  }),
});

Attachment Format:

  • filename (string): File name
  • content (string): Base64 encoded file content (without data:image/png;base64, prefix)
  • mimeType (string): MIME type, e.g., application/pdf, image/png, text/plain

View File List

// List all artifacts
const response = await fetch("/api/acontext/artifacts");
const data = await response.json();

if (data.success) {
  console.log(`Found ${data.count} files:`, data.artifacts);
}

// List files from specific disk
const response = await fetch("/api/acontext/artifacts?diskId=your-disk-id");

Database Migration

Run the following SQL to add Acontext-related fields:

-- Run in Supabase SQL Editor
-- File location: specs/001-chatbot-openai/migration-acontext.sql

Environment Variables

Ensure Acontext is configured in .env.local:

ACONTEXT_API_KEY=your_api_key_here
ACONTEXT_BASE_URL=https://api.acontext.com/api/v1  # Optional, defaults to this

# Bypass proxy for Acontext API (local development only)
# When set to true or 1, Acontext API requests bypass HTTP_PROXY/HTTPS_PROXY
# Other service requests still use proxy
ACONTEXT_BYPASS_PROXY=true  # Optional, only needed for local development

Implementation Details

Key Files:

  • lib/acontext-integration.ts - Acontext integration utility functions
  • lib/acontext-disk-tools.ts - Acontext disk file system tools (Function Calling)
  • app/api/chatbot/route.ts - Updated chat API with Acontext integration
  • components/chatbot-panel.tsx - File upload UI
  • types/chat.ts - Updated type definitions

Key Functions:

  • getOrCreateAcontextSession() - Get or create Acontext session
  • searchRelevantContext() - Semantic search for relevant context
  • uploadFileToAcontext() - Upload file to Acontext
  • storeMessageInAcontext() - Store message in Acontext

Notes:

  • Optional Integration: If ACONTEXT_API_KEY is not configured, all Acontext features automatically skip, basic chat functionality remains unaffected
  • Error Handling: All Acontext operations have error handling, failures don't affect main flow
  • Performance: Semantic search retrieves recent messages as context, production may need more refined search strategy

πŸ“– Documentation

🀝 Contributing

This is a starter template - feel free to fork and customize it for your needs!

πŸ“„ License

Check the LICENSE file in the repository.

πŸ†˜ Support


Built with ❀️ using Acontext Platform

Releases

No releases published

Packages

 
 
 

Contributors

Languages