Skip to content

Add Supabase state adapter#253

Open
vincenzodomina wants to merge 7 commits intovercel:mainfrom
vincenzodomina:main
Open

Add Supabase state adapter#253
vincenzodomina wants to merge 7 commits intovercel:mainfrom
vincenzodomina:main

Conversation

@vincenzodomina
Copy link
Contributor

Summary

Closes #252

Adds @chat-adapter/state-supabase, a production state adapter that uses an existing Supabase client and RPCs for subscriptions, locks, cache, and list state—no Redis or separate Postgres URL.

Why

For apps already on Supabase, this keeps Chat SDK state on the same DB and client: reuse the app’s Supabase client, manage schema via migrations, and keep access and permissions under Supabase (e.g. service-role RPCs only). Avoids the mismatch of @chat-adapter/state-pg (direct pg / connection URL and runtime schema creation).

What’s in this PR

  • Package packages/state-supabase: createSupabaseState({ client, keyPrefix?, logger? }) using the shared StateAdapter contract.
  • SQL sql/chat_state.sql: copy-paste migration for a dedicated chat_state schema (tables + RPCs). Grants are RPC-only for service_role; schema is not created at runtime.
  • Behavior: All state goes through RPCs (e.g. chat_state_subscribe, chat_state_acquire_lock, chat_state_set, chat_state_append_to_list). TTL handling matches memory/Redis (0 or invalid = no expiry). List appends are serialized per key with pg_advisory_xact_lock so trim and TTL stay correct under concurrency.
  • Tests: 60 unit tests (mocked client) for lifecycle, subscriptions, locks, cache, lists, TTL normalization, lock normalization, RPC error propagation, and StateAdapter contract. Optional Testcontainers integration when RUN_INTEGRATION=1. 100% coverage on the adapter module.

Usage

import { createSupabaseState } from "@chat-adapter/state-supabase";

const bot = new Chat({
  userName: "mybot",
  adapters,
  state: createSupabaseState({ client: supabase }),
});
  1. Add sql/chat_state.sql to your Supabase schema/migrations.
  2. Expose the chat_state schema in the Supabase API.
  3. Pass a server-side (e.g. service-role) Supabase client.

Test plan

  • Changeset: Added .changeset/add-state-supabase-adapter.md with minor for @chat-adapter/state-supabase and a short changelog summary, per contributing guidelines and Changesets workflow.
  • Lint / types / build: Ran pnpm check, pnpm typecheck, and pnpm build at repo root (and/or pnpm validate) so the new package passes lint, typecheck, and build.
  • Unit tests: Ran pnpm --filter @chat-adapter/state-supabase test. All 60 unit tests pass with 100% coverage on the adapter; tests cover factory, connection lifecycle, ensureConnected for every public method, use-after-disconnect, RPC error propagation, subscriptions, locking (including lock normalization and extendLock false), cache (including TTL normalization and setIfNotExists false), lists (including partial options and ttlMs 0), and StateAdapter contract.
  • Optional integration: With RUN_INTEGRATION=1, the Testcontainers-based integration suite runs (Postgres + sql/chat_state.sql), including schema/RPC checks and the adapter against a real DB; this is skipped in default CI.

@vercel
Copy link
Contributor

vercel bot commented Mar 16, 2026

@vincenzodomina is attempting to deploy a commit to the Vercel Labs Team on Vercel.

A member of the Team first needs to authorize it.

@socket-security
Copy link

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Added@​supabase/​supabase-js@​2.99.174100100100100
Addedtestcontainers@​11.12.0961008294100

View full report

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Supabase State Adapter

1 participant