Skip to content

memodb-io/memobase-playground

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Memobase Playground

A playground project based on the open-source Memobase project, built with Supabase as the backend database.

Overview

This project is a playground environment for experimenting with and extending the Memobase functionality. It provides a simplified setup for development and testing purposes.

Database Setup

The project uses Supabase as its database backend. Below are the SQL statements to create the necessary tables:

Messages Table

CREATE TABLE "public"."messages" (
    "id" uuid NOT NULL DEFAULT gen_random_uuid(),
    "parent_id" uuid,
    "thread_id" uuid NOT NULL,
    "created_by" uuid NOT NULL,
    "updated_by" uuid NOT NULL,
    "format" text NOT NULL,
    "content" text NOT NULL,
    "height" integer,
    "created_at" timestamptz NOT NULL DEFAULT now(),
    "updated_at" timestamptz NOT NULL DEFAULT now()
);

Threads Table

CREATE TABLE "public"."threads" (
    "id" uuid NOT NULL DEFAULT gen_random_uuid(),
    "workspace_id" text NOT NULL,
    "updated_by" uuid NOT NULL,
    "title" text,
    "is_archived" boolean NOT NULL DEFAULT false,
    "external_id" text,
    "metadata" jsonb,
    "created_by" uuid NOT NULL,
    "created_at" timestamptz NOT NULL DEFAULT now(),
    "updated_at" timestamptz NOT NULL DEFAULT now(),
    "last_message_at" timestamptz
);

Database Functions

Click to expand
CREATE OR REPLACE FUNCTION public.get_threads_for_user(uid uuid)
 RETURNS json
 LANGUAGE plpgsql
 SECURITY DEFINER
AS $function$BEGIN
  RETURN (
    SELECT json_build_object(
      'threads', COALESCE(json_agg(t), '[]'::json)
    )
    FROM (
      SELECT *
      FROM threads
      WHERE created_by = uid AND is_archived = false
      ORDER BY created_at DESC
    ) t
  );
END;$function$

CREATE OR REPLACE FUNCTION public.create_thread(uid uuid, last_message_at timestamp with time zone)
 RETURNS json
 LANGUAGE plpgsql
 SECURITY DEFINER
AS $function$DECLARE
  thread_id UUID;
BEGIN
  INSERT INTO threads (
    id,
    workspace_id,
    created_by,
    updated_by,
    title,
    last_message_at
  )
  VALUES (
    uuid_generate_v4(),
    'workspace',
    uid,
    uid,
    '',
    last_message_at
  )
  RETURNING id INTO thread_id;

  RETURN json_build_object('thread_id', thread_id);
END;$function$

CREATE OR REPLACE FUNCTION public.update_thread_archived(uid uuid, thread_id uuid, archived boolean)
 RETURNS json
 LANGUAGE plpgsql
 SECURITY DEFINER
AS $function$BEGIN
  UPDATE threads
  SET
    is_archived = archived,
    updated_by = uid,
    updated_at = now()
  WHERE id = thread_id
    AND created_by = uid;

  RETURN json_build_object();
END;$function$

CREATE OR REPLACE FUNCTION public.get_messages_by_thread_and_user(uid uuid, tid uuid)
 RETURNS json
 LANGUAGE plpgsql
 SECURITY DEFINER
AS $function$BEGIN
  RETURN (
    WITH message_data AS (
      SELECT
        m.id,
        m.parent_id,
        m.thread_id,
        m.created_by,
        m.created_at,
        m.updated_by,
        m.updated_at,
        m.format,
        m.content,
        m.height
      FROM
        messages m
      WHERE
        m.thread_id = tid
        AND m.created_by = uid
      ORDER BY
        m.created_at DESC
    )
    SELECT json_build_object(
      'messages', COALESCE(json_agg(message_data), '[]'::json)
    )
    FROM message_data
  );
END;$function$

CREATE OR REPLACE FUNCTION public.create_message(uid uuid, tid uuid, pid uuid, fmt character varying, c json)
 RETURNS json
 LANGUAGE plpgsql
 SECURITY DEFINER
AS $function$DECLARE
  mid UUID;
BEGIN
  INSERT INTO messages (
    id,
    thread_id,
    parent_id,
    created_by,
    updated_by,
    content,
    format,
    created_at,
    updated_at
  )
  VALUES (
    uuid_generate_v4(),
    tid,
    pid,
    uid,
    uid,
    c,
    fmt,
    now(),
    now()
  )
  RETURNING id INTO mid;

  RETURN json_build_object(
    'message_id', mid
  );
END;$function$

CREATE OR REPLACE FUNCTION public.get_message_count_by_uid(uid uuid)
 RETURNS integer
 LANGUAGE plpgsql
 SECURITY DEFINER
AS $function$DECLARE
    message_count INTEGER;
BEGIN
    SELECT COUNT(*)
    INTO message_count
    FROM messages
    WHERE created_by = uid
        AND created_at >= date_trunc('day', now());

    RETURN message_count;
END;$function$

Getting Started

  1. Clone the repository
  2. Set up your Supabase project
  3. Run the database creation scripts
  4. Configure your environment variables
  5. Start the development server

Environment Setup

The project includes an .env.example file with the following variables that you should configure:

NEXT_PUBLIC_BASE_URL="http://localhost:3000"
NEXT_PUBLIC_BASE_PATH="/playground"

OPENAI_API_KEY=""          # Your OpenAI API key
OPENAI_BASE_URL=""         # OpenAI API base URL (optional)
OPENAI_MODEL=""            # OpenAI model to use (e.g., "gpt-3.5-turbo")

NEXT_PUBLIC_SUPABASE_URL=""        # Your Supabase project URL
NEXT_PUBLIC_SUPABASE_ANON_KEY=""   # Your Supabase anonymous key

NEXT_PUBLIC_MEMOBASE_PROJECT_URL="" # Memobase project URL (if connecting to Memobase)
NEXT_PUBLIC_MEMOBASE_API_KEY=""     # Memobase API key (if connecting to Memobase)

Copy the .env.example file to .env and fill in the required values for your environment.

Available Scripts

This project uses pnpm as the package manager. Here are the available scripts:

# Install dependencies
pnpm install

# Start development server with Turbopack
pnpm dev

# Build for production
pnpm build

# Start production server
pnpm start

# Run linting
pnpm lint

# Preview Cloudflare deployment locally
pnpm preview

# Deploy to Cloudflare
pnpm deploy

# Upload to Cloudflare
pnpm upload

# Generate Cloudflare environment types
pnpm cf-typegen

Development

This is a playground project, so feel free to experiment and modify the codebase as needed. The project structure follows the original Memobase architecture but with simplified components for easier development and testing.

Contributing

Since this is a playground project, contributions are welcome but not actively maintained. Feel free to fork and modify for your own needs.

License

This project is based on Memobase and follows its original license. Please refer to the original Memobase repository for license details.