Back to Eliza

Supabase Examples

packages/docs/examples-gallery/supabase.mdx

1.7.23.3 KB
Original Source

Deploy AI agents to Supabase's Deno-based edge functions.

Quick Start

bash
cd examples/supabase
supabase start
supabase functions deploy eliza-chat

Available Implementations

LanguageDirectoryFeatures
TypeScript (Deno)examples/supabase/functions/eliza-chat/Native Deno
Rust WASMexamples/supabase/functions/eliza-chat-wasm/WASM runtime

Edge Function

typescript
// functions/eliza-chat/index.ts
import { serve } from "https://deno.land/[email protected]/http/server.ts";
import { AgentRuntime, ModelType } from "@elizaos/core";
import { openaiPlugin } from "@elizaos/plugin-openai";

let runtime: AgentRuntime | null = null;

async function getRuntime() {
  if (runtime) return runtime;

  runtime = new AgentRuntime({
    character: { name: "Eliza", bio: "A helpful AI assistant." },
    plugins: [openaiPlugin],
  });

  await runtime.initialize();
  return runtime;
}

serve(async (req) => {
  // Handle CORS
  if (req.method === "OPTIONS") {
    return new Response(null, {
      headers: {
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Methods": "POST",
        "Access-Control-Allow-Headers": "Content-Type",
      },
    });
  }

  const runtime = await getRuntime();
  const { message } = await req.json();

  const response = await runtime.useModel(ModelType.TEXT_LARGE, {
    prompt: message,
  });

  return new Response(JSON.stringify({ response: String(response) }), {
    headers: {
      "Content-Type": "application/json",
      "Access-Control-Allow-Origin": "*",
    },
  });
});

Testing Locally

bash
supabase functions serve eliza-chat --env-file .env.local
curl -X POST http://localhost:54321/functions/v1/eliza-chat \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_ANON_KEY" \
  -d '{"message": "Hello!"}'

Configuration

Create .env.local:

bash
OPENAI_API_KEY=your-key

Set secrets for production:

bash
supabase secrets set OPENAI_API_KEY=your-key

WASM Version

For better performance, use the Rust WASM version:

typescript
// functions/eliza-chat-wasm/index.ts
import { serve } from "https://deno.land/[email protected]/http/server.ts";
import init, { WasmAgentRuntime } from "./lib/elizaos_bg.wasm";

await init();

const runtime = await new WasmAgentRuntime(
  JSON.stringify({
    name: "Eliza",
    bio: "A helpful AI assistant.",
  }),
);

await runtime.initialize();

serve(async (req) => {
  const { message } = await req.json();
  const response = await runtime.use_model("TEXT_LARGE", message);

  return new Response(JSON.stringify({ response }));
});

Integration with Supabase Database

typescript
import { createClient } from "https://esm.sh/@supabase/supabase-js@2";

const supabase = createClient(
  Deno.env.get("SUPABASE_URL")!,
  Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!,
);

// Store conversation
await supabase.from("conversations").insert({
  user_id: userId,
  message: userMessage,
  response: aiResponse,
});

Benefits

  • <100ms cold start
  • Integrated with Supabase Auth, Database, Storage
  • Deno runtime for modern JavaScript
  • 500K free invocations/month