Memory
memoryMemory for AI assistants: core memory as files under /memories edited with view, create, str_replace, insert, delete and rename with version checks; archival facts with hybrid similarity search; message embeddings to recall earlier chats, skipped for temporary chats; and versioned documents under an opaque scope key for agent runtimes such as eve.
AImanaged
Which deals close this month?
AI
3 deals, €42k in total.
Install
The command also installs updated-at, memberships, vector-search, permissions, the modules it needs.
npx better-supabase add memoryConfigure
The blocks entries in your config, with the default mode.
import { defineConfig } from "better-supabase/config";
export default defineConfig({
blocks: {
// Required by memory; add installs them too.
"updated-at": {},
memberships: {},
"vector-search": {},
permissions: {},
// Modes: "managed". The default is "managed".
memory: { mode: "managed" },
},
});Migrations
The files
npx better-supabase add memory writes into your declarative schema with the default layout.11 files
supabase/schemas/900_better_supabase_050_memory.sqlschema1489 lines-- better-supabase module: memory
-- @bsb-block memory@5 managed
-- @bsb-hash sha256:174249039948f069e122b26278c3868c0de1b17802f6159324b1580b63b80ae4
-- Memory for AI assistants: core memory as files under /memories edited with view, create, str_replace, insert, delete and rename with version checks; archival facts with hybrid similarity search; message embeddings to recall earlier chats, skipped for temporary chats; and versioned documents under an opaque scope key for agent runtimes such as eve.
-- Managed by `better-supabase add`; re-running it rewrites this file unless it was edited by hand.
-- Change it through `blocks` in better-supabase.config.ts and the module's SQL hooks.
-- Memory for AI assistants. Core memory is a small set of files under
-- /memories that the model reads and edits with view, create, str_replace,
-- insert, delete and rename; archival memory is a list of facts found by
-- similarity. Each belongs to a user, an agent, a chat, a project, the
-- organization or a record.
create table "better_supabase"."memories" (
"id" uuid primary key default gen_random_uuid(),
"tenant_id" uuid not null,
"owner_id" uuid references auth.users (id) on delete cascade,
"scope" text not null default 'user' constraint bsb_memories_scope_check check ("scope" in ('user', 'agent', 'chat', 'project', 'organization', 'record')),
"agent_id" uuid,
"chat_id" uuid,
"project_id" uuid,
"kind" text not null constraint bsb_memories_kind_check check ("kind" in ('core', 'archival')),
"path" text constraint bsb_memories_path_check check ("path" ~ '^/memories(/[A-Za-z0-9._ -]+)*$' and "path" !~ '(^|/)[.]{1,2}(/|$)'),
"content" text not null constraint bsb_memories_content_check check (length("content") <= 100000),
"version" integer not null default 1,
"embedding" extensions.vector(1536),
"embedding_model" text,
"source_message_id" text,
"superseded_by" uuid references "better_supabase"."memories" ("id") on delete set null,
"subject_type" text,
"subject_id" text,
"category" text constraint bsb_memories_category_check check ("category" ~ '^[a-z0-9][a-z0-9_-]*$'),
"confidence" real constraint bsb_memories_confidence_check check ("confidence" >= 0 and "confidence" <= 1),
"expires_at" timestamptz,
"archived_at" timestamptz,
"use_count" integer not null default 0,
"last_used_at" timestamptz,
"tsv" tsvector generated always as (to_tsvector('simple'::regconfig, "content")) stored,
"created_at" timestamptz not null default now(),
"updated_at" timestamptz not null default now(),
constraint bsb_memories_check check (("kind" = 'core') = ("path" is not null)),
constraint bsb_memories_owner_check check ("scope" in ('organization', 'record') or "owner_id" is not null),
constraint bsb_memories_subject_check check (("scope" = 'record') = ("subject_id" is not null) and ("subject_id" is null) = ("subject_type" is null))
);
create unique index memories_path_idx on "better_supabase"."memories" ("tenant_id", "owner_id", "scope", "agent_id", "chat_id", "project_id", "subject_type", "subject_id", "path") nulls not distinct
where "path" is not null and "superseded_by" is null and "archived_at" is null;
create index memories_owner_idx on "better_supabase"."memories" ("owner_id", "tenant_id") where "owner_id" is not null;
create index memories_tenant_idx on "better_supabase"."memories" ("tenant_id", "scope");
create index memories_chat_idx on "better_supabase"."memories" ("chat_id") where "chat_id" is not null;
create index memories_project_idx on "better_supabase"."memories" ("project_id") where "project_id" is not null;
create index memories_subject_idx on "better_supabase"."memories" ("subject_type", "subject_id") where "subject_id" is not null;
create index memories_deleted_idx on "better_supabase"."memories" ("archived_at") where "archived_at" is not null;
create index memories_expires_idx on "better_supabase"."memories" ("expires_at") where "expires_at" is not null;
create index memories_superseded_idx on "better_supabase"."memories" ("superseded_by") where "superseded_by" is not null;
create index memories_embedding_idx on "better_supabase"."memories" using hnsw ("embedding" extensions.vector_cosine_ops);
create index memories_tsv_idx on "better_supabase"."memories" using gin ("tsv");
create index memories_pending_embedding_idx on "better_supabase"."memories" ("updated_at")
where "embedding" is null and "superseded_by" is null and "archived_at" is null;
alter table "better_supabase"."memories" enable row level security;
revoke all on "better_supabase"."memories" from public, anon, authenticated;
grant select on "better_supabase"."memories" to authenticated;
grant all on "better_supabase"."memories" to service_role;
create policy bsb_memories_read on "better_supabase"."memories"
for select to authenticated
using (("owner_id" = (select auth.uid()) or ("owner_id" is null and "tenant_id" in (select better_supabase.tenant_ids_with('memory.read')))) and "archived_at" is null);
-- One embedding per message, so an assistant can recall earlier chats.
-- Temporary chats get none.
create table "better_supabase"."memory_message_embeddings" (
"chat_id" uuid not null,
"message_id" text not null,
"user_id" uuid not null references auth.users (id) on delete cascade,
"tenant_id" uuid not null,
"embedding" extensions.vector(1536) not null,
"embedding_model" text,
"created_at" timestamptz not null default now(),
primary key ("chat_id", "message_id")
);
create index "memory_message_embeddings_user_idx" on "better_supabase"."memory_message_embeddings" ("user_id", "tenant_id");
create index "memory_message_embeddings_embedding_idx" on "better_supabase"."memory_message_embeddings" using hnsw ("embedding" extensions.vector_cosine_ops);
alter table "better_supabase"."memory_message_embeddings" enable row level security;
revoke all on "better_supabase"."memory_message_embeddings" from public, anon, authenticated;
grant select on "better_supabase"."memory_message_embeddings" to authenticated;
grant all on "better_supabase"."memory_message_embeddings" to service_role;
create policy bsb_memory_message_embeddings_read on "better_supabase"."memory_message_embeddings"
for select to authenticated
using ("user_id" = (select auth.uid()));
-- A core memory file, or the files under a directory, or null.
create or replace function "better_supabase"."memory_view"(tenant uuid, path text default '/memories', ns jsonb default '{}')
returns jsonb
language plpgsql
stable
security definer
set search_path = ''
as $$
declare
v_scope text;
v_agent uuid;
v_chat uuid;
v_project uuid;
v_subject_type text;
v_subject_id text;
v_private boolean;
v_owner uuid;
v_row "better_supabase"."memories"%rowtype;
v_entries jsonb;
begin
v_scope := coalesce(memory_view.ns ->> 'scope', 'user');
v_agent := (memory_view.ns ->> 'agent_id')::uuid;
v_chat := (memory_view.ns ->> 'chat_id')::uuid;
v_project := (memory_view.ns ->> 'project_id')::uuid;
v_subject_type := nullif(memory_view.ns ->> 'subject_type', '');
v_subject_id := nullif(memory_view.ns ->> 'subject_id', '');
v_private := coalesce((memory_view.ns ->> 'private')::boolean, false);
v_owner := case when v_scope in ('organization', 'record') and not v_private then null when coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin') then (memory_view.ns ->> 'owner_id')::uuid else auth.uid() end;
if not coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin') and (auth.uid() is null or not coalesce(better_supabase.can('tenant', memory_view.tenant, 'memory.read'), false)) then
raise exception 'you may not use memory here' using errcode = '42501', hint = 'MEMORY_FORBIDDEN';
end if;
if v_scope not in ('user', 'agent', 'chat', 'project', 'organization', 'record')
or (v_scope = 'agent') <> (v_agent is not null)
or (v_scope = 'chat') <> (v_chat is not null)
or (v_scope = 'project') <> (v_project is not null)
or (v_scope = 'record') <> (v_subject_id is not null)
or (v_subject_id is not null) <> (v_subject_type is not null)
or (v_owner is null and (v_scope not in ('organization', 'record') or v_private)) then
raise exception 'ns % is not a memory namespace', memory_view.ns using errcode = '22023', hint = 'MEMORY_NAMESPACE';
end if;
if memory_view.path is null or length(memory_view.path) > 500 or memory_view.path !~ '^/memories(/[A-Za-z0-9._ -]+)*$' or memory_view.path ~ '(^|/)[.]{1,2}(/|$)' then
raise exception '% is not a path under /memories', memory_view.path using errcode = '22023', hint = 'MEMORY_PATH';
end if;
select * into v_row from "better_supabase"."memories" x
where x."tenant_id" = memory_view.tenant and x."owner_id" is not distinct from v_owner and x."scope" = v_scope and x."agent_id" is not distinct from v_agent and x."chat_id" is not distinct from v_chat and x."project_id" is not distinct from v_project and x."subject_type" is not distinct from v_subject_type and x."subject_id" is not distinct from v_subject_id and x."superseded_by" is null and x."archived_at" is null and x."path" = memory_view.path;
if found then
return jsonb_build_object('type', 'file', 'path', v_row."path", 'content', v_row."content", 'version', v_row."version", 'updated_at', v_row."updated_at");
end if;
select jsonb_agg(jsonb_build_object('path', x."path", 'size', length(x."content"), 'updated_at', x."updated_at") order by x."path")
into v_entries
from "better_supabase"."memories" x
where x."tenant_id" = memory_view.tenant and x."owner_id" is not distinct from v_owner and x."scope" = v_scope and x."agent_id" is not distinct from v_agent and x."chat_id" is not distinct from v_chat and x."project_id" is not distinct from v_project and x."subject_type" is not distinct from v_subject_type and x."subject_id" is not distinct from v_subject_id and x."superseded_by" is null and x."archived_at" is null and x."path" like replace(replace(memory_view.path, '_', '\_'), '%', '\%') || '/%';
if v_entries is null and memory_view.path <> '/memories' then
return null;
end if;
return jsonb_build_object('type', 'directory', 'path', memory_view.path, 'entries', coalesce(v_entries, '[]'));
end;
$$;
revoke execute on function "better_supabase"."memory_view"(uuid, text, jsonb) from public, anon;
grant execute on function "better_supabase"."memory_view"(uuid, text, jsonb) to authenticated, service_role;
-- Creates or overwrites a core memory file. expected_version 0 means the
-- file must not exist yet; another number must match its version.
create or replace function "better_supabase"."memory_create"(tenant uuid, path text, content text, ns jsonb default '{}', expected_version integer default null)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_scope text;
v_agent uuid;
v_chat uuid;
v_project uuid;
v_subject_type text;
v_subject_id text;
v_private boolean;
v_owner uuid;
v_row "better_supabase"."memories"%rowtype;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
v_scope := coalesce(memory_create.ns ->> 'scope', 'user');
v_agent := (memory_create.ns ->> 'agent_id')::uuid;
v_chat := (memory_create.ns ->> 'chat_id')::uuid;
v_project := (memory_create.ns ->> 'project_id')::uuid;
v_subject_type := nullif(memory_create.ns ->> 'subject_type', '');
v_subject_id := nullif(memory_create.ns ->> 'subject_id', '');
v_private := coalesce((memory_create.ns ->> 'private')::boolean, false);
v_owner := case when v_scope in ('organization', 'record') and not v_private then null when coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin') then (memory_create.ns ->> 'owner_id')::uuid else auth.uid() end;
if not coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin') and (auth.uid() is null or not coalesce(better_supabase.can('tenant', memory_create.tenant, 'memory.read'), false)) then
raise exception 'you may not use memory here' using errcode = '42501', hint = 'MEMORY_FORBIDDEN';
end if;
if v_scope not in ('user', 'agent', 'chat', 'project', 'organization', 'record')
or (v_scope = 'agent') <> (v_agent is not null)
or (v_scope = 'chat') <> (v_chat is not null)
or (v_scope = 'project') <> (v_project is not null)
or (v_scope = 'record') <> (v_subject_id is not null)
or (v_subject_id is not null) <> (v_subject_type is not null)
or (v_owner is null and (v_scope not in ('organization', 'record') or v_private)) then
raise exception 'ns % is not a memory namespace', memory_create.ns using errcode = '22023', hint = 'MEMORY_NAMESPACE';
end if;
if v_owner is null and not (coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin') or coalesce(better_supabase.can('tenant', memory_create.tenant, 'memory.manage'), false)) then
raise exception 'only admins change organization and record memory' using errcode = '42501', hint = 'MEMORY_FORBIDDEN';
end if;
if memory_create.path is null or length(memory_create.path) > 500 or memory_create.path !~ '^/memories(/[A-Za-z0-9._ -]+)*$' or memory_create.path ~ '(^|/)[.]{1,2}(/|$)' then
raise exception '% is not a path under /memories', memory_create.path using errcode = '22023', hint = 'MEMORY_PATH';
end if;
if length(memory_create.content) > 100000 then
raise exception 'memory content is limited to % characters', 100000 using errcode = '22023', hint = 'MEMORY_TOO_LARGE';
end if;
select * into v_row from "better_supabase"."memories" x
where x."tenant_id" = memory_create.tenant and x."owner_id" is not distinct from v_owner and x."scope" = v_scope and x."agent_id" is not distinct from v_agent and x."chat_id" is not distinct from v_chat and x."project_id" is not distinct from v_project and x."subject_type" is not distinct from v_subject_type and x."subject_id" is not distinct from v_subject_id and x."superseded_by" is null and x."archived_at" is null and x."path" = memory_create.path
for update;
if found then
if memory_create.expected_version is not null and memory_create.expected_version <> v_row."version" then
raise exception '% changed since version %', memory_create.path, memory_create.expected_version using errcode = '40001', hint = 'MEMORY_CONFLICT';
end if;
update "better_supabase"."memories" x set "content" = memory_create.content, "version" = x."version" + 1, "embedding" = null, "updated_at" = now()
where x."id" = v_row."id"
returning * into v_row;
else
if coalesce(memory_create.expected_version, 0) <> 0 then
raise exception '% not found', memory_create.path using errcode = 'P0002', hint = 'MEMORY_NOT_FOUND';
end if;
insert into "better_supabase"."memories" ("tenant_id", "owner_id", "scope", "agent_id", "chat_id", "project_id", "subject_type", "subject_id", "kind", "path", "content")
values (memory_create.tenant, v_owner, v_scope, v_agent, v_chat, v_project, v_subject_type, v_subject_id, 'core', memory_create.path, memory_create.content)
returning * into v_row;
end if;
return jsonb_build_object('id', v_row."id", 'tenant_id', v_row."tenant_id", 'owner_id', v_row."owner_id", 'scope', v_row."scope", 'agent_id', v_row."agent_id", 'chat_id', v_row."chat_id", 'project_id', v_row."project_id", 'kind', v_row."kind", 'path', v_row."path", 'content', v_row."content", 'version', v_row."version", 'embedding_model', v_row."embedding_model", 'source_message_id', v_row."source_message_id", 'subject_type', v_row."subject_type", 'subject_id', v_row."subject_id", 'category', v_row."category", 'confidence', v_row."confidence", 'expires_at', v_row."expires_at", 'use_count', v_row."use_count", 'last_used_at', v_row."last_used_at", 'created_at', v_row."created_at", 'updated_at', v_row."updated_at");
end;
$$;
revoke execute on function "better_supabase"."memory_create"(uuid, text, text, jsonb, integer) from public, anon;
grant execute on function "better_supabase"."memory_create"(uuid, text, text, jsonb, integer) to authenticated, service_role;
-- Replaces old_text with new_text in a core memory file; old_text has to
-- appear exactly once.
create or replace function "better_supabase"."memory_str_replace"(tenant uuid, path text, old_text text, new_text text, ns jsonb default '{}')
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_scope text;
v_agent uuid;
v_chat uuid;
v_project uuid;
v_subject_type text;
v_subject_id text;
v_private boolean;
v_owner uuid;
v_row "better_supabase"."memories"%rowtype;
v_matches integer;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
v_scope := coalesce(memory_str_replace.ns ->> 'scope', 'user');
v_agent := (memory_str_replace.ns ->> 'agent_id')::uuid;
v_chat := (memory_str_replace.ns ->> 'chat_id')::uuid;
v_project := (memory_str_replace.ns ->> 'project_id')::uuid;
v_subject_type := nullif(memory_str_replace.ns ->> 'subject_type', '');
v_subject_id := nullif(memory_str_replace.ns ->> 'subject_id', '');
v_private := coalesce((memory_str_replace.ns ->> 'private')::boolean, false);
v_owner := case when v_scope in ('organization', 'record') and not v_private then null when coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin') then (memory_str_replace.ns ->> 'owner_id')::uuid else auth.uid() end;
if not coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin') and (auth.uid() is null or not coalesce(better_supabase.can('tenant', memory_str_replace.tenant, 'memory.read'), false)) then
raise exception 'you may not use memory here' using errcode = '42501', hint = 'MEMORY_FORBIDDEN';
end if;
if v_scope not in ('user', 'agent', 'chat', 'project', 'organization', 'record')
or (v_scope = 'agent') <> (v_agent is not null)
or (v_scope = 'chat') <> (v_chat is not null)
or (v_scope = 'project') <> (v_project is not null)
or (v_scope = 'record') <> (v_subject_id is not null)
or (v_subject_id is not null) <> (v_subject_type is not null)
or (v_owner is null and (v_scope not in ('organization', 'record') or v_private)) then
raise exception 'ns % is not a memory namespace', memory_str_replace.ns using errcode = '22023', hint = 'MEMORY_NAMESPACE';
end if;
if v_owner is null and not (coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin') or coalesce(better_supabase.can('tenant', memory_str_replace.tenant, 'memory.manage'), false)) then
raise exception 'only admins change organization and record memory' using errcode = '42501', hint = 'MEMORY_FORBIDDEN';
end if;
if memory_str_replace.path is null or length(memory_str_replace.path) > 500 or memory_str_replace.path !~ '^/memories(/[A-Za-z0-9._ -]+)*$' or memory_str_replace.path ~ '(^|/)[.]{1,2}(/|$)' then
raise exception '% is not a path under /memories', memory_str_replace.path using errcode = '22023', hint = 'MEMORY_PATH';
end if;
select * into v_row from "better_supabase"."memories" x
where x."tenant_id" = memory_str_replace.tenant and x."owner_id" is not distinct from v_owner and x."scope" = v_scope and x."agent_id" is not distinct from v_agent and x."chat_id" is not distinct from v_chat and x."project_id" is not distinct from v_project and x."subject_type" is not distinct from v_subject_type and x."subject_id" is not distinct from v_subject_id and x."superseded_by" is null and x."archived_at" is null and x."path" = memory_str_replace.path
for update;
if not found then
raise exception '% not found', memory_str_replace.path using errcode = 'P0002', hint = 'MEMORY_NOT_FOUND';
end if;
if coalesce(length(memory_str_replace.old_text), 0) = 0 then
raise exception 'old_text is empty' using errcode = '22023', hint = 'MEMORY_NO_MATCH';
end if;
v_matches := (length(v_row."content") - length(replace(v_row."content", memory_str_replace.old_text, ''))) / length(memory_str_replace.old_text);
if v_matches = 0 then
raise exception 'old_text does not appear in %', memory_str_replace.path using errcode = '22023', hint = 'MEMORY_NO_MATCH';
elsif v_matches > 1 then
raise exception 'old_text appears % times in %', v_matches, memory_str_replace.path using errcode = '22023', hint = 'MEMORY_AMBIGUOUS';
end if;
if length(replace(v_row."content", memory_str_replace.old_text, coalesce(memory_str_replace.new_text, ''))) > 100000 then
raise exception 'memory content is limited to % characters', 100000 using errcode = '22023', hint = 'MEMORY_TOO_LARGE';
end if;
update "better_supabase"."memories" x set
"content" = replace(x."content", memory_str_replace.old_text, coalesce(memory_str_replace.new_text, '')),
"version" = x."version" + 1, "embedding" = null, "updated_at" = now()
where x."id" = v_row."id"
returning * into v_row;
return jsonb_build_object('id', v_row."id", 'tenant_id', v_row."tenant_id", 'owner_id', v_row."owner_id", 'scope', v_row."scope", 'agent_id', v_row."agent_id", 'chat_id', v_row."chat_id", 'project_id', v_row."project_id", 'kind', v_row."kind", 'path', v_row."path", 'content', v_row."content", 'version', v_row."version", 'embedding_model', v_row."embedding_model", 'source_message_id', v_row."source_message_id", 'subject_type', v_row."subject_type", 'subject_id', v_row."subject_id", 'category', v_row."category", 'confidence', v_row."confidence", 'expires_at', v_row."expires_at", 'use_count', v_row."use_count", 'last_used_at', v_row."last_used_at", 'created_at', v_row."created_at", 'updated_at', v_row."updated_at");
end;
$$;
revoke execute on function "better_supabase"."memory_str_replace"(uuid, text, text, text, jsonb) from public, anon;
grant execute on function "better_supabase"."memory_str_replace"(uuid, text, text, text, jsonb) to authenticated, service_role;
-- Inserts text after line insert_line (0 inserts at the top).
create or replace function "better_supabase"."memory_insert"(tenant uuid, path text, insert_line integer, insert_text text, ns jsonb default '{}')
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_scope text;
v_agent uuid;
v_chat uuid;
v_project uuid;
v_subject_type text;
v_subject_id text;
v_private boolean;
v_owner uuid;
v_row "better_supabase"."memories"%rowtype;
v_lines text[];
v_content text;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
v_scope := coalesce(memory_insert.ns ->> 'scope', 'user');
v_agent := (memory_insert.ns ->> 'agent_id')::uuid;
v_chat := (memory_insert.ns ->> 'chat_id')::uuid;
v_project := (memory_insert.ns ->> 'project_id')::uuid;
v_subject_type := nullif(memory_insert.ns ->> 'subject_type', '');
v_subject_id := nullif(memory_insert.ns ->> 'subject_id', '');
v_private := coalesce((memory_insert.ns ->> 'private')::boolean, false);
v_owner := case when v_scope in ('organization', 'record') and not v_private then null when coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin') then (memory_insert.ns ->> 'owner_id')::uuid else auth.uid() end;
if not coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin') and (auth.uid() is null or not coalesce(better_supabase.can('tenant', memory_insert.tenant, 'memory.read'), false)) then
raise exception 'you may not use memory here' using errcode = '42501', hint = 'MEMORY_FORBIDDEN';
end if;
if v_scope not in ('user', 'agent', 'chat', 'project', 'organization', 'record')
or (v_scope = 'agent') <> (v_agent is not null)
or (v_scope = 'chat') <> (v_chat is not null)
or (v_scope = 'project') <> (v_project is not null)
or (v_scope = 'record') <> (v_subject_id is not null)
or (v_subject_id is not null) <> (v_subject_type is not null)
or (v_owner is null and (v_scope not in ('organization', 'record') or v_private)) then
raise exception 'ns % is not a memory namespace', memory_insert.ns using errcode = '22023', hint = 'MEMORY_NAMESPACE';
end if;
if v_owner is null and not (coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin') or coalesce(better_supabase.can('tenant', memory_insert.tenant, 'memory.manage'), false)) then
raise exception 'only admins change organization and record memory' using errcode = '42501', hint = 'MEMORY_FORBIDDEN';
end if;
if memory_insert.path is null or length(memory_insert.path) > 500 or memory_insert.path !~ '^/memories(/[A-Za-z0-9._ -]+)*$' or memory_insert.path ~ '(^|/)[.]{1,2}(/|$)' then
raise exception '% is not a path under /memories', memory_insert.path using errcode = '22023', hint = 'MEMORY_PATH';
end if;
select * into v_row from "better_supabase"."memories" x
where x."tenant_id" = memory_insert.tenant and x."owner_id" is not distinct from v_owner and x."scope" = v_scope and x."agent_id" is not distinct from v_agent and x."chat_id" is not distinct from v_chat and x."project_id" is not distinct from v_project and x."subject_type" is not distinct from v_subject_type and x."subject_id" is not distinct from v_subject_id and x."superseded_by" is null and x."archived_at" is null and x."path" = memory_insert.path
for update;
if not found then
raise exception '% not found', memory_insert.path using errcode = 'P0002', hint = 'MEMORY_NOT_FOUND';
end if;
v_lines := case when v_row."content" = '' then '{}'::text[] else string_to_array(v_row."content", E'\n') end;
if memory_insert.insert_line < 0 or memory_insert.insert_line > coalesce(array_length(v_lines, 1), 0) then
raise exception 'line % is outside the file', memory_insert.insert_line using errcode = '22023', hint = 'MEMORY_LINE';
end if;
v_content := array_to_string(v_lines[1:memory_insert.insert_line] || string_to_array(coalesce(memory_insert.insert_text, ''), E'\n') || v_lines[memory_insert.insert_line + 1:], E'\n');
if length(v_content) > 100000 then
raise exception 'memory content is limited to % characters', 100000 using errcode = '22023', hint = 'MEMORY_TOO_LARGE';
end if;
update "better_supabase"."memories" x set "content" = v_content, "version" = x."version" + 1, "embedding" = null, "updated_at" = now()
where x."id" = v_row."id"
returning * into v_row;
return jsonb_build_object('id', v_row."id", 'tenant_id', v_row."tenant_id", 'owner_id', v_row."owner_id", 'scope', v_row."scope", 'agent_id', v_row."agent_id", 'chat_id', v_row."chat_id", 'project_id', v_row."project_id", 'kind', v_row."kind", 'path', v_row."path", 'content', v_row."content", 'version', v_row."version", 'embedding_model', v_row."embedding_model", 'source_message_id', v_row."source_message_id", 'subject_type', v_row."subject_type", 'subject_id', v_row."subject_id", 'category', v_row."category", 'confidence', v_row."confidence", 'expires_at', v_row."expires_at", 'use_count', v_row."use_count", 'last_used_at', v_row."last_used_at", 'created_at', v_row."created_at", 'updated_at', v_row."updated_at");
end;
$$;
revoke execute on function "better_supabase"."memory_insert"(uuid, text, integer, text, jsonb) from public, anon;
grant execute on function "better_supabase"."memory_insert"(uuid, text, integer, text, jsonb) to authenticated, service_role;
-- Deletes a file, or a directory and everything under it, and returns the
-- count. A deleted file stays restorable until purge_memories removes it.
create or replace function "better_supabase"."memory_delete"(tenant uuid, path text, ns jsonb default '{}')
returns integer
language plpgsql
security definer
set search_path = ''
as $$
declare
v_scope text;
v_agent uuid;
v_chat uuid;
v_project uuid;
v_subject_type text;
v_subject_id text;
v_private boolean;
v_owner uuid;
v_count integer;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
v_scope := coalesce(memory_delete.ns ->> 'scope', 'user');
v_agent := (memory_delete.ns ->> 'agent_id')::uuid;
v_chat := (memory_delete.ns ->> 'chat_id')::uuid;
v_project := (memory_delete.ns ->> 'project_id')::uuid;
v_subject_type := nullif(memory_delete.ns ->> 'subject_type', '');
v_subject_id := nullif(memory_delete.ns ->> 'subject_id', '');
v_private := coalesce((memory_delete.ns ->> 'private')::boolean, false);
v_owner := case when v_scope in ('organization', 'record') and not v_private then null when coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin') then (memory_delete.ns ->> 'owner_id')::uuid else auth.uid() end;
if not coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin') and (auth.uid() is null or not coalesce(better_supabase.can('tenant', memory_delete.tenant, 'memory.read'), false)) then
raise exception 'you may not use memory here' using errcode = '42501', hint = 'MEMORY_FORBIDDEN';
end if;
if v_scope not in ('user', 'agent', 'chat', 'project', 'organization', 'record')
or (v_scope = 'agent') <> (v_agent is not null)
or (v_scope = 'chat') <> (v_chat is not null)
or (v_scope = 'project') <> (v_project is not null)
or (v_scope = 'record') <> (v_subject_id is not null)
or (v_subject_id is not null) <> (v_subject_type is not null)
or (v_owner is null and (v_scope not in ('organization', 'record') or v_private)) then
raise exception 'ns % is not a memory namespace', memory_delete.ns using errcode = '22023', hint = 'MEMORY_NAMESPACE';
end if;
if v_owner is null and not (coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin') or coalesce(better_supabase.can('tenant', memory_delete.tenant, 'memory.manage'), false)) then
raise exception 'only admins change organization and record memory' using errcode = '42501', hint = 'MEMORY_FORBIDDEN';
end if;
if memory_delete.path is null or length(memory_delete.path) > 500 or memory_delete.path !~ '^/memories(/[A-Za-z0-9._ -]+)*$' or memory_delete.path ~ '(^|/)[.]{1,2}(/|$)' then
raise exception '% is not a path under /memories', memory_delete.path using errcode = '22023', hint = 'MEMORY_PATH';
end if;
update "better_supabase"."memories" x set "archived_at" = now(), "updated_at" = now()
where x."tenant_id" = memory_delete.tenant and x."owner_id" is not distinct from v_owner and x."scope" = v_scope and x."agent_id" is not distinct from v_agent and x."chat_id" is not distinct from v_chat and x."project_id" is not distinct from v_project and x."subject_type" is not distinct from v_subject_type and x."subject_id" is not distinct from v_subject_id and x."superseded_by" is null and x."archived_at" is null
and (x."path" = memory_delete.path or x."path" like replace(replace(memory_delete.path, '_', '\_'), '%', '\%') || '/%');
get diagnostics v_count = row_count;
return v_count;
end;
$$;
revoke execute on function "better_supabase"."memory_delete"(uuid, text, jsonb) from public, anon;
grant execute on function "better_supabase"."memory_delete"(uuid, text, jsonb) to authenticated, service_role;
-- Moves a file or a directory. Fails when the target already exists.
create or replace function "better_supabase"."memory_rename"(tenant uuid, old_path text, new_path text, ns jsonb default '{}')
returns integer
language plpgsql
security definer
set search_path = ''
as $$
declare
v_scope text;
v_agent uuid;
v_chat uuid;
v_project uuid;
v_subject_type text;
v_subject_id text;
v_private boolean;
v_owner uuid;
v_count integer;
v_prefix text := replace(replace(memory_rename.old_path, '_', '\_'), '%', '\%') || '/%';
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
v_scope := coalesce(memory_rename.ns ->> 'scope', 'user');
v_agent := (memory_rename.ns ->> 'agent_id')::uuid;
v_chat := (memory_rename.ns ->> 'chat_id')::uuid;
v_project := (memory_rename.ns ->> 'project_id')::uuid;
v_subject_type := nullif(memory_rename.ns ->> 'subject_type', '');
v_subject_id := nullif(memory_rename.ns ->> 'subject_id', '');
v_private := coalesce((memory_rename.ns ->> 'private')::boolean, false);
v_owner := case when v_scope in ('organization', 'record') and not v_private then null when coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin') then (memory_rename.ns ->> 'owner_id')::uuid else auth.uid() end;
if not coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin') and (auth.uid() is null or not coalesce(better_supabase.can('tenant', memory_rename.tenant, 'memory.read'), false)) then
raise exception 'you may not use memory here' using errcode = '42501', hint = 'MEMORY_FORBIDDEN';
end if;
if v_scope not in ('user', 'agent', 'chat', 'project', 'organization', 'record')
or (v_scope = 'agent') <> (v_agent is not null)
or (v_scope = 'chat') <> (v_chat is not null)
or (v_scope = 'project') <> (v_project is not null)
or (v_scope = 'record') <> (v_subject_id is not null)
or (v_subject_id is not null) <> (v_subject_type is not null)
or (v_owner is null and (v_scope not in ('organization', 'record') or v_private)) then
raise exception 'ns % is not a memory namespace', memory_rename.ns using errcode = '22023', hint = 'MEMORY_NAMESPACE';
end if;
if v_owner is null and not (coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin') or coalesce(better_supabase.can('tenant', memory_rename.tenant, 'memory.manage'), false)) then
raise exception 'only admins change organization and record memory' using errcode = '42501', hint = 'MEMORY_FORBIDDEN';
end if;
if memory_rename.old_path is null or length(memory_rename.old_path) > 500 or memory_rename.old_path !~ '^/memories(/[A-Za-z0-9._ -]+)*$' or memory_rename.old_path ~ '(^|/)[.]{1,2}(/|$)' then
raise exception '% is not a path under /memories', memory_rename.old_path using errcode = '22023', hint = 'MEMORY_PATH';
end if;
if memory_rename.new_path is null or length(memory_rename.new_path) > 500 or memory_rename.new_path !~ '^/memories(/[A-Za-z0-9._ -]+)*$' or memory_rename.new_path ~ '(^|/)[.]{1,2}(/|$)' then
raise exception '% is not a path under /memories', memory_rename.new_path using errcode = '22023', hint = 'MEMORY_PATH';
end if;
if memory_rename.new_path = memory_rename.old_path or memory_rename.new_path like v_prefix then
raise exception 'cannot move % into itself', memory_rename.old_path using errcode = '22023', hint = 'MEMORY_PATH';
end if;
if exists (
select 1 from "better_supabase"."memories" x
where x."tenant_id" = memory_rename.tenant and x."owner_id" is not distinct from v_owner and x."scope" = v_scope and x."agent_id" is not distinct from v_agent and x."chat_id" is not distinct from v_chat and x."project_id" is not distinct from v_project and x."subject_type" is not distinct from v_subject_type and x."subject_id" is not distinct from v_subject_id and x."superseded_by" is null and x."archived_at" is null
and (x."path" = memory_rename.new_path or x."path" like replace(replace(memory_rename.new_path, '_', '\_'), '%', '\%') || '/%')
) then
raise exception '% already exists', memory_rename.new_path using errcode = '23505', hint = 'MEMORY_EXISTS';
end if;
update "better_supabase"."memories" x set
"path" = memory_rename.new_path || substr(x."path", length(memory_rename.old_path) + 1),
"version" = x."version" + 1, "updated_at" = now()
where x."tenant_id" = memory_rename.tenant and x."owner_id" is not distinct from v_owner and x."scope" = v_scope and x."agent_id" is not distinct from v_agent and x."chat_id" is not distinct from v_chat and x."project_id" is not distinct from v_project and x."subject_type" is not distinct from v_subject_type and x."subject_id" is not distinct from v_subject_id and x."superseded_by" is null and x."archived_at" is null
and (x."path" = memory_rename.old_path or x."path" like v_prefix);
get diagnostics v_count = row_count;
if v_count = 0 then
raise exception '% not found', memory_rename.old_path using errcode = 'P0002', hint = 'MEMORY_NOT_FOUND';
end if;
return v_count;
end;
$$;
revoke execute on function "better_supabase"."memory_rename"(uuid, text, text, jsonb) from public, anon;
grant execute on function "better_supabase"."memory_rename"(uuid, text, text, jsonb) to authenticated, service_role;
-- The memories of a namespace, core files by path or archival facts newest first.
create or replace function "better_supabase"."memory_list"(tenant uuid, ns jsonb default '{}', kind text default 'core', max_rows integer default 200)
returns jsonb
language plpgsql
stable
security definer
set search_path = ''
as $$
declare
v_scope text;
v_agent uuid;
v_chat uuid;
v_project uuid;
v_subject_type text;
v_subject_id text;
v_private boolean;
v_owner uuid;
v_rows jsonb;
begin
v_scope := coalesce(memory_list.ns ->> 'scope', 'user');
v_agent := (memory_list.ns ->> 'agent_id')::uuid;
v_chat := (memory_list.ns ->> 'chat_id')::uuid;
v_project := (memory_list.ns ->> 'project_id')::uuid;
v_subject_type := nullif(memory_list.ns ->> 'subject_type', '');
v_subject_id := nullif(memory_list.ns ->> 'subject_id', '');
v_private := coalesce((memory_list.ns ->> 'private')::boolean, false);
v_owner := case when v_scope in ('organization', 'record') and not v_private then null when coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin') then (memory_list.ns ->> 'owner_id')::uuid else auth.uid() end;
if not coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin') and (auth.uid() is null or not coalesce(better_supabase.can('tenant', memory_list.tenant, 'memory.read'), false)) then
raise exception 'you may not use memory here' using errcode = '42501', hint = 'MEMORY_FORBIDDEN';
end if;
if v_scope not in ('user', 'agent', 'chat', 'project', 'organization', 'record')
or (v_scope = 'agent') <> (v_agent is not null)
or (v_scope = 'chat') <> (v_chat is not null)
or (v_scope = 'project') <> (v_project is not null)
or (v_scope = 'record') <> (v_subject_id is not null)
or (v_subject_id is not null) <> (v_subject_type is not null)
or (v_owner is null and (v_scope not in ('organization', 'record') or v_private)) then
raise exception 'ns % is not a memory namespace', memory_list.ns using errcode = '22023', hint = 'MEMORY_NAMESPACE';
end if;
select coalesce(jsonb_agg(jsonb_build_object('id', x."id", 'tenant_id', x."tenant_id", 'owner_id', x."owner_id", 'scope', x."scope", 'agent_id', x."agent_id", 'chat_id', x."chat_id", 'project_id', x."project_id", 'kind', x."kind", 'path', x."path", 'content', x."content", 'version', x."version", 'embedding_model', x."embedding_model", 'source_message_id', x."source_message_id", 'subject_type', x."subject_type", 'subject_id', x."subject_id", 'category', x."category", 'confidence', x."confidence", 'expires_at', x."expires_at", 'use_count', x."use_count", 'last_used_at', x."last_used_at", 'created_at', x."created_at", 'updated_at', x."updated_at") order by x."path", x."created_at" desc), '[]') into v_rows
from (
select * from "better_supabase"."memories" y
where y."tenant_id" = memory_list.tenant and y."owner_id" is not distinct from v_owner and y."scope" = v_scope and y."agent_id" is not distinct from v_agent and y."chat_id" is not distinct from v_chat and y."project_id" is not distinct from v_project and y."subject_type" is not distinct from v_subject_type and y."subject_id" is not distinct from v_subject_id and y."superseded_by" is null and y."archived_at" is null and (y."expires_at" is null or y."expires_at" > now()) and y."kind" = memory_list.kind
order by y."path", y."created_at" desc
limit least(greatest(memory_list.max_rows, 1), 1000)
) x;
return v_rows;
end;
$$;
revoke execute on function "better_supabase"."memory_list"(uuid, jsonb, text, integer) from public, anon;
grant execute on function "better_supabase"."memory_list"(uuid, jsonb, text, integer) to authenticated, service_role;
-- The memories of one namespace as a keyset page (ADR 1077), archival facts
-- newest first. ns names the namespace as memory_list does. filters is { kind
-- (core, archival; archival by default), category (a value or a list), search
-- (content), sort (createdAt, updatedAt, path, useCount, lastUsedAt), direction, facet,
-- count, limit, after, offset, paged, total, totalCap }. It runs as the
-- caller: row level security decides what shows.
create or replace function "better_supabase"."page_memories"(tenant uuid, ns jsonb default '{}', filters jsonb default '{}')
returns jsonb
language plpgsql
stable
security invoker
set search_path = ''
as $$
#variable_conflict use_variable
declare
v_scope text;
v_agent uuid;
v_chat uuid;
v_project uuid;
v_subject_type text;
v_subject_id text;
v_private boolean;
v_owner uuid;
v_filters jsonb := coalesce(page_memories.filters, '{}'::jsonb);
v_search text := case when nullif(trim(v_filters ->> 'search'), '') is null then null
else '%' || replace(replace(replace(trim(v_filters ->> 'search'), '\', '\\'), '%', '\%'), '_', '\_') || '%' end;
v_limit integer := least(greatest(coalesce((v_filters ->> 'limit')::integer, 50), 1), 200);
v_offset integer := greatest(coalesce((v_filters ->> 'offset')::integer, 0), 0);
v_after jsonb := v_filters -> 'after';
v_paged boolean := coalesce((v_filters ->> 'paged')::boolean, false);
v_total_cap integer := least(greatest(coalesce((v_filters ->> 'totalCap')::integer, 10000), 1), 100000);
v_items jsonb;
v_more boolean;
v_next jsonb;
v_total integer;
v_sort text := nullif(v_filters ->> 'sort', '');
v_dir text := nullif(v_filters ->> 'direction', '');
v_facet text := nullif(v_filters ->> 'facet', '');
v_count boolean := coalesce((v_filters ->> 'count')::boolean, false);
v_result jsonb;
begin
v_scope := coalesce(page_memories.ns ->> 'scope', 'user');
v_agent := (page_memories.ns ->> 'agent_id')::uuid;
v_chat := (page_memories.ns ->> 'chat_id')::uuid;
v_project := (page_memories.ns ->> 'project_id')::uuid;
v_subject_type := nullif(page_memories.ns ->> 'subject_type', '');
v_subject_id := nullif(page_memories.ns ->> 'subject_id', '');
v_private := coalesce((page_memories.ns ->> 'private')::boolean, false);
v_owner := case when v_scope in ('organization', 'record') and not v_private then null when coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin') then (page_memories.ns ->> 'owner_id')::uuid else auth.uid() end;
if not coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin') and (auth.uid() is null or not coalesce(better_supabase.can('tenant', page_memories.tenant, 'memory.read'), false)) then
raise exception 'you may not use memory here' using errcode = '42501', hint = 'MEMORY_FORBIDDEN';
end if;
if v_scope not in ('user', 'agent', 'chat', 'project', 'organization', 'record')
or (v_scope = 'agent') <> (v_agent is not null)
or (v_scope = 'chat') <> (v_chat is not null)
or (v_scope = 'project') <> (v_project is not null)
or (v_scope = 'record') <> (v_subject_id is not null)
or (v_subject_id is not null) <> (v_subject_type is not null)
or (v_owner is null and (v_scope not in ('organization', 'record') or v_private)) then
raise exception 'ns % is not a memory namespace', page_memories.ns using errcode = '22023', hint = 'MEMORY_NAMESPACE';
end if;
if exists (select 1 from jsonb_array_elements_text(case when jsonb_typeof(v_filters -> 'kind') = 'array' then v_filters -> 'kind' when jsonb_typeof(v_filters -> 'kind') in ('string', 'number') then jsonb_build_array(v_filters -> 'kind') else '[]'::jsonb end) s where s not in ('core', 'archival')) then
raise exception 'kind is core or archival' using errcode = '22023', hint = 'MEMORY_INVALID';
end if;
if v_filters ? 'after' and v_filters ? 'offset' then
raise exception 'after and offset are two ways to page; pass one' using errcode = '22023', hint = 'MEMORY_INVALID';
end if;
if v_facet is not null then
if v_facet not in ('kind', 'category') then
raise exception 'facet is one of kind, category' using errcode = '22023', hint = 'MEMORY_INVALID';
end if;
with s as (
select (case v_facet when 'kind' then (x."kind")::text when 'category' then (coalesce(x."category", ''))::text end) as v
from "better_supabase"."memories" x
where x."tenant_id" = page_memories.tenant and x."owner_id" is not distinct from v_owner and x."scope" = v_scope and x."agent_id" is not distinct from v_agent and x."chat_id" is not distinct from v_chat and x."project_id" is not distinct from v_project and x."subject_type" is not distinct from v_subject_type and x."subject_id" is not distinct from v_subject_id and x."superseded_by" is null and x."archived_at" is null
and true and (x."expires_at" is null or x."expires_at" > now())
and (case when (v_filters -> 'kind' is not null and jsonb_typeof(v_filters -> 'kind') <> 'null' and not (jsonb_typeof(v_filters -> 'kind') = 'array' and jsonb_array_length(v_filters -> 'kind') = 0)) then (not (v_filters -> 'kind' is not null and jsonb_typeof(v_filters -> 'kind') <> 'null' and not (jsonb_typeof(v_filters -> 'kind') = 'array' and jsonb_array_length(v_filters -> 'kind') = 0)) or (x."kind")::text = any((case when jsonb_typeof(v_filters -> 'kind') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'kind')) else array[v_filters ->> 'kind'] end)::text[])) else x."kind" = 'archival' end)
and (not (v_filters -> 'category' is not null and jsonb_typeof(v_filters -> 'category') <> 'null' and not (jsonb_typeof(v_filters -> 'category') = 'array' and jsonb_array_length(v_filters -> 'category') = 0)) or (coalesce(x."category", ''))::text = any((case when jsonb_typeof(v_filters -> 'category') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'category')) else array[v_filters ->> 'category'] end)::text[]))
and (v_search is null or x."content" ilike v_search)
limit v_total_cap + 1
)
select jsonb_build_object('facet', v_facet, 'values', coalesce(jsonb_agg(jsonb_build_object('value', g.v, 'count', jsonb_build_object('value', g.n, 'exact', g.exact)) order by g.n desc, g.v), '[]'::jsonb))
into v_result
from (
select s.v, count(*) as n, (select count(*) from s) <= v_total_cap as exact
from s where s.v is not null group by s.v order by n desc, s.v limit 100
) g;
return v_result;
end if;
if v_count then
select count(*) into v_total from (
select 1 from "better_supabase"."memories" x
where x."tenant_id" = page_memories.tenant and x."owner_id" is not distinct from v_owner and x."scope" = v_scope and x."agent_id" is not distinct from v_agent and x."chat_id" is not distinct from v_chat and x."project_id" is not distinct from v_project and x."subject_type" is not distinct from v_subject_type and x."subject_id" is not distinct from v_subject_id and x."superseded_by" is null and x."archived_at" is null
and true and (x."expires_at" is null or x."expires_at" > now())
and (case when (v_filters -> 'kind' is not null and jsonb_typeof(v_filters -> 'kind') <> 'null' and not (jsonb_typeof(v_filters -> 'kind') = 'array' and jsonb_array_length(v_filters -> 'kind') = 0)) then (not (v_filters -> 'kind' is not null and jsonb_typeof(v_filters -> 'kind') <> 'null' and not (jsonb_typeof(v_filters -> 'kind') = 'array' and jsonb_array_length(v_filters -> 'kind') = 0)) or (x."kind")::text = any((case when jsonb_typeof(v_filters -> 'kind') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'kind')) else array[v_filters ->> 'kind'] end)::text[])) else x."kind" = 'archival' end)
and (not (v_filters -> 'category' is not null and jsonb_typeof(v_filters -> 'category') <> 'null' and not (jsonb_typeof(v_filters -> 'category') = 'array' and jsonb_array_length(v_filters -> 'category') = 0)) or (coalesce(x."category", ''))::text = any((case when jsonb_typeof(v_filters -> 'category') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'category')) else array[v_filters ->> 'category'] end)::text[]))
and (v_search is null or x."content" ilike v_search)
limit v_total_cap + 1
) c;
return jsonb_build_object('value', least(v_total, v_total_cap), 'exact', v_total <= v_total_cap);
end if;
if v_sort is not null and v_sort not in ('createdAt', 'updatedAt', 'path', 'useCount', 'lastUsedAt') then
raise exception 'sort is one of createdAt, updatedAt, path, useCount, lastUsedAt' using errcode = '22023', hint = 'MEMORY_INVALID';
end if;
if v_dir is not null and v_dir not in ('asc', 'desc') then
raise exception 'direction is asc or desc' using errcode = '22023', hint = 'MEMORY_INVALID';
end if;
if v_sort is null and coalesce(v_dir, 'desc') = 'asc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'MEMORY_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, jsonb_build_object('id', x."id", 'tenant_id', x."tenant_id", 'owner_id', x."owner_id", 'scope', x."scope", 'agent_id', x."agent_id", 'chat_id', x."chat_id", 'project_id', x."project_id", 'kind', x."kind", 'path', x."path", 'content', x."content", 'version', x."version", 'embedding_model', x."embedding_model", 'source_message_id', x."source_message_id", 'subject_type', x."subject_type", 'subject_id', x."subject_id", 'category', x."category", 'confidence', x."confidence", 'expires_at', x."expires_at", 'use_count', x."use_count", 'last_used_at', x."last_used_at", 'created_at', x."created_at", 'updated_at', x."updated_at") as j, jsonb_build_array((x."created_at")::text, (x."id")::text) as k
from (
select * from "better_supabase"."memories" x
where x."tenant_id" = page_memories.tenant and x."owner_id" is not distinct from v_owner and x."scope" = v_scope and x."agent_id" is not distinct from v_agent and x."chat_id" is not distinct from v_chat and x."project_id" is not distinct from v_project and x."subject_type" is not distinct from v_subject_type and x."subject_id" is not distinct from v_subject_id and x."superseded_by" is null and x."archived_at" is null
and true and (x."expires_at" is null or x."expires_at" > now())
and (case when (v_filters -> 'kind' is not null and jsonb_typeof(v_filters -> 'kind') <> 'null' and not (jsonb_typeof(v_filters -> 'kind') = 'array' and jsonb_array_length(v_filters -> 'kind') = 0)) then (not (v_filters -> 'kind' is not null and jsonb_typeof(v_filters -> 'kind') <> 'null' and not (jsonb_typeof(v_filters -> 'kind') = 'array' and jsonb_array_length(v_filters -> 'kind') = 0)) or (x."kind")::text = any((case when jsonb_typeof(v_filters -> 'kind') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'kind')) else array[v_filters ->> 'kind'] end)::text[])) else x."kind" = 'archival' end)
and (not (v_filters -> 'category' is not null and jsonb_typeof(v_filters -> 'category') <> 'null' and not (jsonb_typeof(v_filters -> 'category') = 'array' and jsonb_array_length(v_filters -> 'category') = 0)) or (coalesce(x."category", ''))::text = any((case when jsonb_typeof(v_filters -> 'category') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'category')) else array[v_filters ->> 'category'] end)::text[]))
and (v_search is null or x."content" ilike v_search)
and (v_after is null or (x."created_at" >= (v_after ->> 0)::timestamptz and ((x."created_at" > (v_after ->> 0)::timestamptz) or (x."created_at" = (v_after ->> 0)::timestamptz and x."id" < (v_after ->> 1)::uuid))))
order by x."created_at" asc, x."id" desc
limit v_limit + 1 offset v_offset
) x
) p;
elsif v_sort is null and coalesce(v_dir, 'desc') = 'desc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'MEMORY_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, jsonb_build_object('id', x."id", 'tenant_id', x."tenant_id", 'owner_id', x."owner_id", 'scope', x."scope", 'agent_id', x."agent_id", 'chat_id', x."chat_id", 'project_id', x."project_id", 'kind', x."kind", 'path', x."path", 'content', x."content", 'version', x."version", 'embedding_model', x."embedding_model", 'source_message_id', x."source_message_id", 'subject_type', x."subject_type", 'subject_id', x."subject_id", 'category', x."category", 'confidence', x."confidence", 'expires_at', x."expires_at", 'use_count', x."use_count", 'last_used_at', x."last_used_at", 'created_at', x."created_at", 'updated_at', x."updated_at") as j, jsonb_build_array((x."created_at")::text, (x."id")::text) as k
from (
select * from "better_supabase"."memories" x
where x."tenant_id" = page_memories.tenant and x."owner_id" is not distinct from v_owner and x."scope" = v_scope and x."agent_id" is not distinct from v_agent and x."chat_id" is not distinct from v_chat and x."project_id" is not distinct from v_project and x."subject_type" is not distinct from v_subject_type and x."subject_id" is not distinct from v_subject_id and x."superseded_by" is null and x."archived_at" is null
and true and (x."expires_at" is null or x."expires_at" > now())
and (case when (v_filters -> 'kind' is not null and jsonb_typeof(v_filters -> 'kind') <> 'null' and not (jsonb_typeof(v_filters -> 'kind') = 'array' and jsonb_array_length(v_filters -> 'kind') = 0)) then (not (v_filters -> 'kind' is not null and jsonb_typeof(v_filters -> 'kind') <> 'null' and not (jsonb_typeof(v_filters -> 'kind') = 'array' and jsonb_array_length(v_filters -> 'kind') = 0)) or (x."kind")::text = any((case when jsonb_typeof(v_filters -> 'kind') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'kind')) else array[v_filters ->> 'kind'] end)::text[])) else x."kind" = 'archival' end)
and (not (v_filters -> 'category' is not null and jsonb_typeof(v_filters -> 'category') <> 'null' and not (jsonb_typeof(v_filters -> 'category') = 'array' and jsonb_array_length(v_filters -> 'category') = 0)) or (coalesce(x."category", ''))::text = any((case when jsonb_typeof(v_filters -> 'category') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'category')) else array[v_filters ->> 'category'] end)::text[]))
and (v_search is null or x."content" ilike v_search)
and (v_after is null or (x."created_at", x."id") < ((v_after ->> 0)::timestamptz, (v_after ->> 1)::uuid))
order by x."created_at" desc, x."id" desc
limit v_limit + 1 offset v_offset
) x
) p;
elsif v_sort = 'createdAt' and coalesce(v_dir, 'desc') = 'asc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'MEMORY_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, jsonb_build_object('id', x."id", 'tenant_id', x."tenant_id", 'owner_id', x."owner_id", 'scope', x."scope", 'agent_id', x."agent_id", 'chat_id', x."chat_id", 'project_id', x."project_id", 'kind', x."kind", 'path', x."path", 'content', x."content", 'version', x."version", 'embedding_model', x."embedding_model", 'source_message_id', x."source_message_id", 'subject_type', x."subject_type", 'subject_id', x."subject_id", 'category', x."category", 'confidence', x."confidence", 'expires_at', x."expires_at", 'use_count', x."use_count", 'last_used_at', x."last_used_at", 'created_at', x."created_at", 'updated_at', x."updated_at") as j, jsonb_build_array((x."created_at")::text, (x."id")::text) as k
from (
select * from "better_supabase"."memories" x
where x."tenant_id" = page_memories.tenant and x."owner_id" is not distinct from v_owner and x."scope" = v_scope and x."agent_id" is not distinct from v_agent and x."chat_id" is not distinct from v_chat and x."project_id" is not distinct from v_project and x."subject_type" is not distinct from v_subject_type and x."subject_id" is not distinct from v_subject_id and x."superseded_by" is null and x."archived_at" is null
and true and (x."expires_at" is null or x."expires_at" > now())
and (case when (v_filters -> 'kind' is not null and jsonb_typeof(v_filters -> 'kind') <> 'null' and not (jsonb_typeof(v_filters -> 'kind') = 'array' and jsonb_array_length(v_filters -> 'kind') = 0)) then (not (v_filters -> 'kind' is not null and jsonb_typeof(v_filters -> 'kind') <> 'null' and not (jsonb_typeof(v_filters -> 'kind') = 'array' and jsonb_array_length(v_filters -> 'kind') = 0)) or (x."kind")::text = any((case when jsonb_typeof(v_filters -> 'kind') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'kind')) else array[v_filters ->> 'kind'] end)::text[])) else x."kind" = 'archival' end)
and (not (v_filters -> 'category' is not null and jsonb_typeof(v_filters -> 'category') <> 'null' and not (jsonb_typeof(v_filters -> 'category') = 'array' and jsonb_array_length(v_filters -> 'category') = 0)) or (coalesce(x."category", ''))::text = any((case when jsonb_typeof(v_filters -> 'category') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'category')) else array[v_filters ->> 'category'] end)::text[]))
and (v_search is null or x."content" ilike v_search)
and (v_after is null or (x."created_at" >= (v_after ->> 0)::timestamptz and ((x."created_at" > (v_after ->> 0)::timestamptz) or (x."created_at" = (v_after ->> 0)::timestamptz and x."id" < (v_after ->> 1)::uuid))))
order by x."created_at" asc, x."id" desc
limit v_limit + 1 offset v_offset
) x
) p;
elsif v_sort = 'createdAt' and coalesce(v_dir, 'desc') = 'desc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'MEMORY_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, jsonb_build_object('id', x."id", 'tenant_id', x."tenant_id", 'owner_id', x."owner_id", 'scope', x."scope", 'agent_id', x."agent_id", 'chat_id', x."chat_id", 'project_id', x."project_id", 'kind', x."kind", 'path', x."path", 'content', x."content", 'version', x."version", 'embedding_model', x."embedding_model", 'source_message_id', x."source_message_id", 'subject_type', x."subject_type", 'subject_id', x."subject_id", 'category', x."category", 'confidence', x."confidence", 'expires_at', x."expires_at", 'use_count', x."use_count", 'last_used_at', x."last_used_at", 'created_at', x."created_at", 'updated_at', x."updated_at") as j, jsonb_build_array((x."created_at")::text, (x."id")::text) as k
from (
select * from "better_supabase"."memories" x
where x."tenant_id" = page_memories.tenant and x."owner_id" is not distinct from v_owner and x."scope" = v_scope and x."agent_id" is not distinct from v_agent and x."chat_id" is not distinct from v_chat and x."project_id" is not distinct from v_project and x."subject_type" is not distinct from v_subject_type and x."subject_id" is not distinct from v_subject_id and x."superseded_by" is null and x."archived_at" is null
and true and (x."expires_at" is null or x."expires_at" > now())
and (case when (v_filters -> 'kind' is not null and jsonb_typeof(v_filters -> 'kind') <> 'null' and not (jsonb_typeof(v_filters -> 'kind') = 'array' and jsonb_array_length(v_filters -> 'kind') = 0)) then (not (v_filters -> 'kind' is not null and jsonb_typeof(v_filters -> 'kind') <> 'null' and not (jsonb_typeof(v_filters -> 'kind') = 'array' and jsonb_array_length(v_filters -> 'kind') = 0)) or (x."kind")::text = any((case when jsonb_typeof(v_filters -> 'kind') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'kind')) else array[v_filters ->> 'kind'] end)::text[])) else x."kind" = 'archival' end)
and (not (v_filters -> 'category' is not null and jsonb_typeof(v_filters -> 'category') <> 'null' and not (jsonb_typeof(v_filters -> 'category') = 'array' and jsonb_array_length(v_filters -> 'category') = 0)) or (coalesce(x."category", ''))::text = any((case when jsonb_typeof(v_filters -> 'category') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'category')) else array[v_filters ->> 'category'] end)::text[]))
and (v_search is null or x."content" ilike v_search)
and (v_after is null or (x."created_at", x."id") < ((v_after ->> 0)::timestamptz, (v_after ->> 1)::uuid))
order by x."created_at" desc, x."id" desc
limit v_limit + 1 offset v_offset
) x
) p;
elsif v_sort = 'updatedAt' and coalesce(v_dir, 'desc') = 'asc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'MEMORY_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, jsonb_build_object('id', x."id", 'tenant_id', x."tenant_id", 'owner_id', x."owner_id", 'scope', x."scope", 'agent_id', x."agent_id", 'chat_id', x."chat_id", 'project_id', x."project_id", 'kind', x."kind", 'path', x."path", 'content', x."content", 'version', x."version", 'embedding_model', x."embedding_model", 'source_message_id', x."source_message_id", 'subject_type', x."subject_type", 'subject_id', x."subject_id", 'category', x."category", 'confidence', x."confidence", 'expires_at', x."expires_at", 'use_count', x."use_count", 'last_used_at', x."last_used_at", 'created_at', x."created_at", 'updated_at', x."updated_at") as j, jsonb_build_array((x."updated_at")::text, (x."id")::text) as k
from (
select * from "better_supabase"."memories" x
where x."tenant_id" = page_memories.tenant and x."owner_id" is not distinct from v_owner and x."scope" = v_scope and x."agent_id" is not distinct from v_agent and x."chat_id" is not distinct from v_chat and x."project_id" is not distinct from v_project and x."subject_type" is not distinct from v_subject_type and x."subject_id" is not distinct from v_subject_id and x."superseded_by" is null and x."archived_at" is null
and true and (x."expires_at" is null or x."expires_at" > now())
and (case when (v_filters -> 'kind' is not null and jsonb_typeof(v_filters -> 'kind') <> 'null' and not (jsonb_typeof(v_filters -> 'kind') = 'array' and jsonb_array_length(v_filters -> 'kind') = 0)) then (not (v_filters -> 'kind' is not null and jsonb_typeof(v_filters -> 'kind') <> 'null' and not (jsonb_typeof(v_filters -> 'kind') = 'array' and jsonb_array_length(v_filters -> 'kind') = 0)) or (x."kind")::text = any((case when jsonb_typeof(v_filters -> 'kind') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'kind')) else array[v_filters ->> 'kind'] end)::text[])) else x."kind" = 'archival' end)
and (not (v_filters -> 'category' is not null and jsonb_typeof(v_filters -> 'category') <> 'null' and not (jsonb_typeof(v_filters -> 'category') = 'array' and jsonb_array_length(v_filters -> 'category') = 0)) or (coalesce(x."category", ''))::text = any((case when jsonb_typeof(v_filters -> 'category') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'category')) else array[v_filters ->> 'category'] end)::text[]))
and (v_search is null or x."content" ilike v_search)
and (v_after is null or (x."updated_at" >= (v_after ->> 0)::timestamptz and ((x."updated_at" > (v_after ->> 0)::timestamptz) or (x."updated_at" = (v_after ->> 0)::timestamptz and x."id" < (v_after ->> 1)::uuid))))
order by x."updated_at" asc, x."id" desc
limit v_limit + 1 offset v_offset
) x
) p;
elsif v_sort = 'updatedAt' and coalesce(v_dir, 'desc') = 'desc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'MEMORY_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, jsonb_build_object('id', x."id", 'tenant_id', x."tenant_id", 'owner_id', x."owner_id", 'scope', x."scope", 'agent_id', x."agent_id", 'chat_id', x."chat_id", 'project_id', x."project_id", 'kind', x."kind", 'path', x."path", 'content', x."content", 'version', x."version", 'embedding_model', x."embedding_model", 'source_message_id', x."source_message_id", 'subject_type', x."subject_type", 'subject_id', x."subject_id", 'category', x."category", 'confidence', x."confidence", 'expires_at', x."expires_at", 'use_count', x."use_count", 'last_used_at', x."last_used_at", 'created_at', x."created_at", 'updated_at', x."updated_at") as j, jsonb_build_array((x."updated_at")::text, (x."id")::text) as k
from (
select * from "better_supabase"."memories" x
where x."tenant_id" = page_memories.tenant and x."owner_id" is not distinct from v_owner and x."scope" = v_scope and x."agent_id" is not distinct from v_agent and x."chat_id" is not distinct from v_chat and x."project_id" is not distinct from v_project and x."subject_type" is not distinct from v_subject_type and x."subject_id" is not distinct from v_subject_id and x."superseded_by" is null and x."archived_at" is null
and true and (x."expires_at" is null or x."expires_at" > now())
and (case when (v_filters -> 'kind' is not null and jsonb_typeof(v_filters -> 'kind') <> 'null' and not (jsonb_typeof(v_filters -> 'kind') = 'array' and jsonb_array_length(v_filters -> 'kind') = 0)) then (not (v_filters -> 'kind' is not null and jsonb_typeof(v_filters -> 'kind') <> 'null' and not (jsonb_typeof(v_filters -> 'kind') = 'array' and jsonb_array_length(v_filters -> 'kind') = 0)) or (x."kind")::text = any((case when jsonb_typeof(v_filters -> 'kind') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'kind')) else array[v_filters ->> 'kind'] end)::text[])) else x."kind" = 'archival' end)
and (not (v_filters -> 'category' is not null and jsonb_typeof(v_filters -> 'category') <> 'null' and not (jsonb_typeof(v_filters -> 'category') = 'array' and jsonb_array_length(v_filters -> 'category') = 0)) or (coalesce(x."category", ''))::text = any((case when jsonb_typeof(v_filters -> 'category') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'category')) else array[v_filters ->> 'category'] end)::text[]))
and (v_search is null or x."content" ilike v_search)
and (v_after is null or (x."updated_at", x."id") < ((v_after ->> 0)::timestamptz, (v_after ->> 1)::uuid))
order by x."updated_at" desc, x."id" desc
limit v_limit + 1 offset v_offset
) x
) p;
elsif v_sort = 'path' and coalesce(v_dir, 'asc') = 'asc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'MEMORY_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, jsonb_build_object('id', x."id", 'tenant_id', x."tenant_id", 'owner_id', x."owner_id", 'scope', x."scope", 'agent_id', x."agent_id", 'chat_id', x."chat_id", 'project_id', x."project_id", 'kind', x."kind", 'path', x."path", 'content', x."content", 'version', x."version", 'embedding_model', x."embedding_model", 'source_message_id', x."source_message_id", 'subject_type', x."subject_type", 'subject_id', x."subject_id", 'category', x."category", 'confidence', x."confidence", 'expires_at', x."expires_at", 'use_count', x."use_count", 'last_used_at', x."last_used_at", 'created_at', x."created_at", 'updated_at', x."updated_at") as j, jsonb_build_array((coalesce(x."path", ''))::text, (x."id")::text) as k
from (
select * from "better_supabase"."memories" x
where x."tenant_id" = page_memories.tenant and x."owner_id" is not distinct from v_owner and x."scope" = v_scope and x."agent_id" is not distinct from v_agent and x."chat_id" is not distinct from v_chat and x."project_id" is not distinct from v_project and x."subject_type" is not distinct from v_subject_type and x."subject_id" is not distinct from v_subject_id and x."superseded_by" is null and x."archived_at" is null
and true and (x."expires_at" is null or x."expires_at" > now())
and (case when (v_filters -> 'kind' is not null and jsonb_typeof(v_filters -> 'kind') <> 'null' and not (jsonb_typeof(v_filters -> 'kind') = 'array' and jsonb_array_length(v_filters -> 'kind') = 0)) then (not (v_filters -> 'kind' is not null and jsonb_typeof(v_filters -> 'kind') <> 'null' and not (jsonb_typeof(v_filters -> 'kind') = 'array' and jsonb_array_length(v_filters -> 'kind') = 0)) or (x."kind")::text = any((case when jsonb_typeof(v_filters -> 'kind') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'kind')) else array[v_filters ->> 'kind'] end)::text[])) else x."kind" = 'archival' end)
and (not (v_filters -> 'category' is not null and jsonb_typeof(v_filters -> 'category') <> 'null' and not (jsonb_typeof(v_filters -> 'category') = 'array' and jsonb_array_length(v_filters -> 'category') = 0)) or (coalesce(x."category", ''))::text = any((case when jsonb_typeof(v_filters -> 'category') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'category')) else array[v_filters ->> 'category'] end)::text[]))
and (v_search is null or x."content" ilike v_search)
and (v_after is null or (coalesce(x."path", '') >= (v_after ->> 0)::text and ((coalesce(x."path", '') > (v_after ->> 0)::text) or (coalesce(x."path", '') = (v_after ->> 0)::text and x."id" < (v_after ->> 1)::uuid))))
order by coalesce(x."path", '') asc, x."id" desc
limit v_limit + 1 offset v_offset
) x
) p;
elsif v_sort = 'path' and coalesce(v_dir, 'asc') = 'desc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'MEMORY_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, jsonb_build_object('id', x."id", 'tenant_id', x."tenant_id", 'owner_id', x."owner_id", 'scope', x."scope", 'agent_id', x."agent_id", 'chat_id', x."chat_id", 'project_id', x."project_id", 'kind', x."kind", 'path', x."path", 'content', x."content", 'version', x."version", 'embedding_model', x."embedding_model", 'source_message_id', x."source_message_id", 'subject_type', x."subject_type", 'subject_id', x."subject_id", 'category', x."category", 'confidence', x."confidence", 'expires_at', x."expires_at", 'use_count', x."use_count", 'last_used_at', x."last_used_at", 'created_at', x."created_at", 'updated_at', x."updated_at") as j, jsonb_build_array((coalesce(x."path", ''))::text, (x."id")::text) as k
from (
select * from "better_supabase"."memories" x
where x."tenant_id" = page_memories.tenant and x."owner_id" is not distinct from v_owner and x."scope" = v_scope and x."agent_id" is not distinct from v_agent and x."chat_id" is not distinct from v_chat and x."project_id" is not distinct from v_project and x."subject_type" is not distinct from v_subject_type and x."subject_id" is not distinct from v_subject_id and x."superseded_by" is null and x."archived_at" is null
and true and (x."expires_at" is null or x."expires_at" > now())
and (case when (v_filters -> 'kind' is not null and jsonb_typeof(v_filters -> 'kind') <> 'null' and not (jsonb_typeof(v_filters -> 'kind') = 'array' and jsonb_array_length(v_filters -> 'kind') = 0)) then (not (v_filters -> 'kind' is not null and jsonb_typeof(v_filters -> 'kind') <> 'null' and not (jsonb_typeof(v_filters -> 'kind') = 'array' and jsonb_array_length(v_filters -> 'kind') = 0)) or (x."kind")::text = any((case when jsonb_typeof(v_filters -> 'kind') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'kind')) else array[v_filters ->> 'kind'] end)::text[])) else x."kind" = 'archival' end)
and (not (v_filters -> 'category' is not null and jsonb_typeof(v_filters -> 'category') <> 'null' and not (jsonb_typeof(v_filters -> 'category') = 'array' and jsonb_array_length(v_filters -> 'category') = 0)) or (coalesce(x."category", ''))::text = any((case when jsonb_typeof(v_filters -> 'category') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'category')) else array[v_filters ->> 'category'] end)::text[]))
and (v_search is null or x."content" ilike v_search)
and (v_after is null or (coalesce(x."path", ''), x."id") < ((v_after ->> 0)::text, (v_after ->> 1)::uuid))
order by coalesce(x."path", '') desc, x."id" desc
limit v_limit + 1 offset v_offset
) x
) p;
elsif v_sort = 'useCount' and coalesce(v_dir, 'desc') = 'asc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'MEMORY_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, jsonb_build_object('id', x."id", 'tenant_id', x."tenant_id", 'owner_id', x."owner_id", 'scope', x."scope", 'agent_id', x."agent_id", 'chat_id', x."chat_id", 'project_id', x."project_id", 'kind', x."kind", 'path', x."path", 'content', x."content", 'version', x."version", 'embedding_model', x."embedding_model", 'source_message_id', x."source_message_id", 'subject_type', x."subject_type", 'subject_id', x."subject_id", 'category', x."category", 'confidence', x."confidence", 'expires_at', x."expires_at", 'use_count', x."use_count", 'last_used_at', x."last_used_at", 'created_at', x."created_at", 'updated_at', x."updated_at") as j, jsonb_build_array((x."use_count")::text, (x."id")::text) as k
from (
select * from "better_supabase"."memories" x
where x."tenant_id" = page_memories.tenant and x."owner_id" is not distinct from v_owner and x."scope" = v_scope and x."agent_id" is not distinct from v_agent and x."chat_id" is not distinct from v_chat and x."project_id" is not distinct from v_project and x."subject_type" is not distinct from v_subject_type and x."subject_id" is not distinct from v_subject_id and x."superseded_by" is null and x."archived_at" is null
and true and (x."expires_at" is null or x."expires_at" > now())
and (case when (v_filters -> 'kind' is not null and jsonb_typeof(v_filters -> 'kind') <> 'null' and not (jsonb_typeof(v_filters -> 'kind') = 'array' and jsonb_array_length(v_filters -> 'kind') = 0)) then (not (v_filters -> 'kind' is not null and jsonb_typeof(v_filters -> 'kind') <> 'null' and not (jsonb_typeof(v_filters -> 'kind') = 'array' and jsonb_array_length(v_filters -> 'kind') = 0)) or (x."kind")::text = any((case when jsonb_typeof(v_filters -> 'kind') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'kind')) else array[v_filters ->> 'kind'] end)::text[])) else x."kind" = 'archival' end)
and (not (v_filters -> 'category' is not null and jsonb_typeof(v_filters -> 'category') <> 'null' and not (jsonb_typeof(v_filters -> 'category') = 'array' and jsonb_array_length(v_filters -> 'category') = 0)) or (coalesce(x."category", ''))::text = any((case when jsonb_typeof(v_filters -> 'category') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'category')) else array[v_filters ->> 'category'] end)::text[]))
and (v_search is null or x."content" ilike v_search)
and (v_after is null or (x."use_count" >= (v_after ->> 0)::integer and ((x."use_count" > (v_after ->> 0)::integer) or (x."use_count" = (v_after ->> 0)::integer and x."id" < (v_after ->> 1)::uuid))))
order by x."use_count" asc, x."id" desc
limit v_limit + 1 offset v_offset
) x
) p;
elsif v_sort = 'useCount' and coalesce(v_dir, 'desc') = 'desc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'MEMORY_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, jsonb_build_object('id', x."id", 'tenant_id', x."tenant_id", 'owner_id', x."owner_id", 'scope', x."scope", 'agent_id', x."agent_id", 'chat_id', x."chat_id", 'project_id', x."project_id", 'kind', x."kind", 'path', x."path", 'content', x."content", 'version', x."version", 'embedding_model', x."embedding_model", 'source_message_id', x."source_message_id", 'subject_type', x."subject_type", 'subject_id', x."subject_id", 'category', x."category", 'confidence', x."confidence", 'expires_at', x."expires_at", 'use_count', x."use_count", 'last_used_at', x."last_used_at", 'created_at', x."created_at", 'updated_at', x."updated_at") as j, jsonb_build_array((x."use_count")::text, (x."id")::text) as k
from (
select * from "better_supabase"."memories" x
where x."tenant_id" = page_memories.tenant and x."owner_id" is not distinct from v_owner and x."scope" = v_scope and x."agent_id" is not distinct from v_agent and x."chat_id" is not distinct from v_chat and x."project_id" is not distinct from v_project and x."subject_type" is not distinct from v_subject_type and x."subject_id" is not distinct from v_subject_id and x."superseded_by" is null and x."archived_at" is null
and true and (x."expires_at" is null or x."expires_at" > now())
and (case when (v_filters -> 'kind' is not null and jsonb_typeof(v_filters -> 'kind') <> 'null' and not (jsonb_typeof(v_filters -> 'kind') = 'array' and jsonb_array_length(v_filters -> 'kind') = 0)) then (not (v_filters -> 'kind' is not null and jsonb_typeof(v_filters -> 'kind') <> 'null' and not (jsonb_typeof(v_filters -> 'kind') = 'array' and jsonb_array_length(v_filters -> 'kind') = 0)) or (x."kind")::text = any((case when jsonb_typeof(v_filters -> 'kind') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'kind')) else array[v_filters ->> 'kind'] end)::text[])) else x."kind" = 'archival' end)
and (not (v_filters -> 'category' is not null and jsonb_typeof(v_filters -> 'category') <> 'null' and not (jsonb_typeof(v_filters -> 'category') = 'array' and jsonb_array_length(v_filters -> 'category') = 0)) or (coalesce(x."category", ''))::text = any((case when jsonb_typeof(v_filters -> 'category') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'category')) else array[v_filters ->> 'category'] end)::text[]))
and (v_search is null or x."content" ilike v_search)
and (v_after is null or (x."use_count", x."id") < ((v_after ->> 0)::integer, (v_after ->> 1)::uuid))
order by x."use_count" desc, x."id" desc
limit v_limit + 1 offset v_offset
) x
) p;
elsif v_sort = 'lastUsedAt' and coalesce(v_dir, 'desc') = 'asc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'MEMORY_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, jsonb_build_object('id', x."id", 'tenant_id', x."tenant_id", 'owner_id', x."owner_id", 'scope', x."scope", 'agent_id', x."agent_id", 'chat_id', x."chat_id", 'project_id', x."project_id", 'kind', x."kind", 'path', x."path", 'content', x."content", 'version', x."version", 'embedding_model', x."embedding_model", 'source_message_id', x."source_message_id", 'subject_type', x."subject_type", 'subject_id', x."subject_id", 'category', x."category", 'confidence', x."confidence", 'expires_at', x."expires_at", 'use_count', x."use_count", 'last_used_at', x."last_used_at", 'created_at', x."created_at", 'updated_at', x."updated_at") as j, jsonb_build_array((coalesce(x."last_used_at", 'epoch'::timestamptz))::text, (x."id")::text) as k
from (
select * from "better_supabase"."memories" x
where x."tenant_id" = page_memories.tenant and x."owner_id" is not distinct from v_owner and x."scope" = v_scope and x."agent_id" is not distinct from v_agent and x."chat_id" is not distinct from v_chat and x."project_id" is not distinct from v_project and x."subject_type" is not distinct from v_subject_type and x."subject_id" is not distinct from v_subject_id and x."superseded_by" is null and x."archived_at" is null
and true and (x."expires_at" is null or x."expires_at" > now())
and (case when (v_filters -> 'kind' is not null and jsonb_typeof(v_filters -> 'kind') <> 'null' and not (jsonb_typeof(v_filters -> 'kind') = 'array' and jsonb_array_length(v_filters -> 'kind') = 0)) then (not (v_filters -> 'kind' is not null and jsonb_typeof(v_filters -> 'kind') <> 'null' and not (jsonb_typeof(v_filters -> 'kind') = 'array' and jsonb_array_length(v_filters -> 'kind') = 0)) or (x."kind")::text = any((case when jsonb_typeof(v_filters -> 'kind') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'kind')) else array[v_filters ->> 'kind'] end)::text[])) else x."kind" = 'archival' end)
and (not (v_filters -> 'category' is not null and jsonb_typeof(v_filters -> 'category') <> 'null' and not (jsonb_typeof(v_filters -> 'category') = 'array' and jsonb_array_length(v_filters -> 'category') = 0)) or (coalesce(x."category", ''))::text = any((case when jsonb_typeof(v_filters -> 'category') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'category')) else array[v_filters ->> 'category'] end)::text[]))
and (v_search is null or x."content" ilike v_search)
and (v_after is null or (coalesce(x."last_used_at", 'epoch'::timestamptz) >= (v_after ->> 0)::timestamptz and ((coalesce(x."last_used_at", 'epoch'::timestamptz) > (v_after ->> 0)::timestamptz) or (coalesce(x."last_used_at", 'epoch'::timestamptz) = (v_after ->> 0)::timestamptz and x."id" < (v_after ->> 1)::uuid))))
order by coalesce(x."last_used_at", 'epoch'::timestamptz) asc, x."id" desc
limit v_limit + 1 offset v_offset
) x
) p;
elsif v_sort = 'lastUsedAt' and coalesce(v_dir, 'desc') = 'desc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'MEMORY_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, jsonb_build_object('id', x."id", 'tenant_id', x."tenant_id", 'owner_id', x."owner_id", 'scope', x."scope", 'agent_id', x."agent_id", 'chat_id', x."chat_id", 'project_id', x."project_id", 'kind', x."kind", 'path', x."path", 'content', x."content", 'version', x."version", 'embedding_model', x."embedding_model", 'source_message_id', x."source_message_id", 'subject_type', x."subject_type", 'subject_id', x."subject_id", 'category', x."category", 'confidence', x."confidence", 'expires_at', x."expires_at", 'use_count', x."use_count", 'last_used_at', x."last_used_at", 'created_at', x."created_at", 'updated_at', x."updated_at") as j, jsonb_build_array((coalesce(x."last_used_at", 'epoch'::timestamptz))::text, (x."id")::text) as k
from (
select * from "better_supabase"."memories" x
where x."tenant_id" = page_memories.tenant and x."owner_id" is not distinct from v_owner and x."scope" = v_scope and x."agent_id" is not distinct from v_agent and x."chat_id" is not distinct from v_chat and x."project_id" is not distinct from v_project and x."subject_type" is not distinct from v_subject_type and x."subject_id" is not distinct from v_subject_id and x."superseded_by" is null and x."archived_at" is null
and true and (x."expires_at" is null or x."expires_at" > now())
and (case when (v_filters -> 'kind' is not null and jsonb_typeof(v_filters -> 'kind') <> 'null' and not (jsonb_typeof(v_filters -> 'kind') = 'array' and jsonb_array_length(v_filters -> 'kind') = 0)) then (not (v_filters -> 'kind' is not null and jsonb_typeof(v_filters -> 'kind') <> 'null' and not (jsonb_typeof(v_filters -> 'kind') = 'array' and jsonb_array_length(v_filters -> 'kind') = 0)) or (x."kind")::text = any((case when jsonb_typeof(v_filters -> 'kind') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'kind')) else array[v_filters ->> 'kind'] end)::text[])) else x."kind" = 'archival' end)
and (not (v_filters -> 'category' is not null and jsonb_typeof(v_filters -> 'category') <> 'null' and not (jsonb_typeof(v_filters -> 'category') = 'array' and jsonb_array_length(v_filters -> 'category') = 0)) or (coalesce(x."category", ''))::text = any((case when jsonb_typeof(v_filters -> 'category') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'category')) else array[v_filters ->> 'category'] end)::text[]))
and (v_search is null or x."content" ilike v_search)
and (v_after is null or (coalesce(x."last_used_at", 'epoch'::timestamptz), x."id") < ((v_after ->> 0)::timestamptz, (v_after ->> 1)::uuid))
order by coalesce(x."last_used_at", 'epoch'::timestamptz) desc, x."id" desc
limit v_limit + 1 offset v_offset
) x
) p;
end if;
if not v_paged then
return v_items;
end if;
if coalesce((v_filters ->> 'total')::boolean, false) then
select count(*) into v_total from (
select 1 from "better_supabase"."memories" x
where x."tenant_id" = page_memories.tenant and x."owner_id" is not distinct from v_owner and x."scope" = v_scope and x."agent_id" is not distinct from v_agent and x."chat_id" is not distinct from v_chat and x."project_id" is not distinct from v_project and x."subject_type" is not distinct from v_subject_type and x."subject_id" is not distinct from v_subject_id and x."superseded_by" is null and x."archived_at" is null
and true and (x."expires_at" is null or x."expires_at" > now())
and (case when (v_filters -> 'kind' is not null and jsonb_typeof(v_filters -> 'kind') <> 'null' and not (jsonb_typeof(v_filters -> 'kind') = 'array' and jsonb_array_length(v_filters -> 'kind') = 0)) then (not (v_filters -> 'kind' is not null and jsonb_typeof(v_filters -> 'kind') <> 'null' and not (jsonb_typeof(v_filters -> 'kind') = 'array' and jsonb_array_length(v_filters -> 'kind') = 0)) or (x."kind")::text = any((case when jsonb_typeof(v_filters -> 'kind') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'kind')) else array[v_filters ->> 'kind'] end)::text[])) else x."kind" = 'archival' end)
and (not (v_filters -> 'category' is not null and jsonb_typeof(v_filters -> 'category') <> 'null' and not (jsonb_typeof(v_filters -> 'category') = 'array' and jsonb_array_length(v_filters -> 'category') = 0)) or (coalesce(x."category", ''))::text = any((case when jsonb_typeof(v_filters -> 'category') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'category')) else array[v_filters ->> 'category'] end)::text[]))
and (v_search is null or x."content" ilike v_search)
limit v_total_cap + 1
) c;
end if;
return jsonb_build_object('items', v_items, 'hasMore', v_more, 'nextCursor', case when v_more then v_next end)
|| case when v_total is null then '{}'::jsonb
else jsonb_build_object('total', least(v_total, v_total_cap), 'totalCapped', v_total > v_total_cap) end;
end;
$$;
revoke execute on function "better_supabase"."page_memories"(uuid, jsonb, jsonb) from public, anon;
grant execute on function "better_supabase"."page_memories"(uuid, jsonb, jsonb) to authenticated, service_role;
-- One memory the caller can read by id, or null.
create or replace function "better_supabase"."get_memory"(memory_id uuid)
returns jsonb
language sql
stable
security invoker
set search_path = ''
as $$
select jsonb_build_object('id', x."id", 'tenant_id', x."tenant_id", 'owner_id', x."owner_id", 'scope', x."scope", 'agent_id', x."agent_id", 'chat_id', x."chat_id", 'project_id', x."project_id", 'kind', x."kind", 'path', x."path", 'content', x."content", 'version', x."version", 'embedding_model', x."embedding_model", 'source_message_id', x."source_message_id", 'subject_type', x."subject_type", 'subject_id', x."subject_id", 'category', x."category", 'confidence', x."confidence", 'expires_at', x."expires_at", 'use_count', x."use_count", 'last_used_at', x."last_used_at", 'created_at', x."created_at", 'updated_at', x."updated_at") from "better_supabase"."memories" x
where x."id" = get_memory.memory_id and x."archived_at" is null
$$;
revoke execute on function "better_supabase"."get_memory"(uuid) from public, anon;
grant execute on function "better_supabase"."get_memory"(uuid) to authenticated, service_role;
-- Saves an archival fact, with its embedding when the caller has one, a
-- category, a confidence from 0 to 1 that weighs it in searches, and the time
-- it expires.
create or replace function "better_supabase"."memory_save"(
tenant uuid,
content text,
ns jsonb default '{}',
embedding extensions.vector default null,
model text default null,
source_message_id text default null,
category text default null,
confidence real default null,
expires_at timestamptz default null
)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_scope text;
v_agent uuid;
v_chat uuid;
v_project uuid;
v_subject_type text;
v_subject_id text;
v_private boolean;
v_owner uuid;
v_row "better_supabase"."memories"%rowtype;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
v_scope := coalesce(memory_save.ns ->> 'scope', 'user');
v_agent := (memory_save.ns ->> 'agent_id')::uuid;
v_chat := (memory_save.ns ->> 'chat_id')::uuid;
v_project := (memory_save.ns ->> 'project_id')::uuid;
v_subject_type := nullif(memory_save.ns ->> 'subject_type', '');
v_subject_id := nullif(memory_save.ns ->> 'subject_id', '');
v_private := coalesce((memory_save.ns ->> 'private')::boolean, false);
v_owner := case when v_scope in ('organization', 'record') and not v_private then null when coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin') then (memory_save.ns ->> 'owner_id')::uuid else auth.uid() end;
if not coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin') and (auth.uid() is null or not coalesce(better_supabase.can('tenant', memory_save.tenant, 'memory.read'), false)) then
raise exception 'you may not use memory here' using errcode = '42501', hint = 'MEMORY_FORBIDDEN';
end if;
if v_scope not in ('user', 'agent', 'chat', 'project', 'organization', 'record')
or (v_scope = 'agent') <> (v_agent is not null)
or (v_scope = 'chat') <> (v_chat is not null)
or (v_scope = 'project') <> (v_project is not null)
or (v_scope = 'record') <> (v_subject_id is not null)
or (v_subject_id is not null) <> (v_subject_type is not null)
or (v_owner is null and (v_scope not in ('organization', 'record') or v_private)) then
raise exception 'ns % is not a memory namespace', memory_save.ns using errcode = '22023', hint = 'MEMORY_NAMESPACE';
end if;
if v_owner is null and not (coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin') or coalesce(better_supabase.can('tenant', memory_save.tenant, 'memory.manage'), false)) then
raise exception 'only admins change organization and record memory' using errcode = '42501', hint = 'MEMORY_FORBIDDEN';
end if;
if coalesce(length(memory_save.content), 0) = 0 then
raise exception 'memory content is empty' using errcode = '22023', hint = 'MEMORY_EMPTY';
end if;
if length(memory_save.content) > 100000 then
raise exception 'memory content is limited to % characters', 100000 using errcode = '22023', hint = 'MEMORY_TOO_LARGE';
end if;
if memory_save.confidence is not null and not memory_save.confidence between 0 and 1 then
raise exception 'confidence % is not between 0 and 1', memory_save.confidence using errcode = '22023', hint = 'MEMORY_CONFIDENCE';
end if;
if memory_save.category is not null and memory_save.category !~ '^[a-z0-9][a-z0-9_-]*$' then
raise exception 'category % is not a lowercase slug', memory_save.category using errcode = '22023', hint = 'MEMORY_CATEGORY';
end if;
insert into "better_supabase"."memories" ("tenant_id", "owner_id", "scope", "agent_id", "chat_id", "project_id", "subject_type", "subject_id", "kind", "content", "embedding", "embedding_model", "source_message_id", "category", "confidence", "expires_at")
values (memory_save.tenant, v_owner, v_scope, v_agent, v_chat, v_project, v_subject_type, v_subject_id, 'archival', memory_save.content, memory_save.embedding, memory_save.model, memory_save.source_message_id, memory_save.category, memory_save.confidence, memory_save.expires_at)
returning * into v_row;
return jsonb_build_object('id', v_row."id", 'tenant_id', v_row."tenant_id", 'owner_id', v_row."owner_id", 'scope', v_row."scope", 'agent_id', v_row."agent_id", 'chat_id', v_row."chat_id", 'project_id', v_row."project_id", 'kind', v_row."kind", 'path', v_row."path", 'content', v_row."content", 'version', v_row."version", 'embedding_model', v_row."embedding_model", 'source_message_id', v_row."source_message_id", 'subject_type', v_row."subject_type", 'subject_id', v_row."subject_id", 'category', v_row."category", 'confidence', v_row."confidence", 'expires_at', v_row."expires_at", 'use_count', v_row."use_count", 'last_used_at', v_row."last_used_at", 'created_at', v_row."created_at", 'updated_at', v_row."updated_at");
end;
$$;
revoke execute on function "better_supabase"."memory_save"(uuid, text, jsonb, extensions.vector, text, text, text, real, timestamptz) from public, anon;
grant execute on function "better_supabase"."memory_save"(uuid, text, jsonb, extensions.vector, text, text, text, real, timestamptz) to authenticated, service_role;
-- Archival facts of a namespace ranked by reciprocal rank fusion of vector
-- and full-text search, weighed by confidence, each with its cosine
-- similarity when it has one. Counts each fact it returns as a use.
-- Volatile because it sets hnsw.iterative_scan, which it puts back before
-- returning so the rest of the caller's transaction keeps its own setting.
create or replace function "better_supabase"."memory_search"(
tenant uuid,
query_embedding extensions.vector default null,
query_text text default null,
ns jsonb default '{}',
k integer default 8
)
returns jsonb
language plpgsql
volatile
security definer
set search_path = ''
as $$
declare
v_scope text;
v_agent uuid;
v_chat uuid;
v_project uuid;
v_subject_type text;
v_subject_id text;
v_private boolean;
v_owner uuid;
v_rows jsonb;
v_candidates integer := least(greatest(memory_search.k, 1) * 4, 400);
v_scan text := current_setting('hnsw.iterative_scan', true);
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
v_scope := coalesce(memory_search.ns ->> 'scope', 'user');
v_agent := (memory_search.ns ->> 'agent_id')::uuid;
v_chat := (memory_search.ns ->> 'chat_id')::uuid;
v_project := (memory_search.ns ->> 'project_id')::uuid;
v_subject_type := nullif(memory_search.ns ->> 'subject_type', '');
v_subject_id := nullif(memory_search.ns ->> 'subject_id', '');
v_private := coalesce((memory_search.ns ->> 'private')::boolean, false);
v_owner := case when v_scope in ('organization', 'record') and not v_private then null when coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin') then (memory_search.ns ->> 'owner_id')::uuid else auth.uid() end;
if not coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin') and (auth.uid() is null or not coalesce(better_supabase.can('tenant', memory_search.tenant, 'memory.read'), false)) then
raise exception 'you may not use memory here' using errcode = '42501', hint = 'MEMORY_FORBIDDEN';
end if;
if v_scope not in ('user', 'agent', 'chat', 'project', 'organization', 'record')
or (v_scope = 'agent') <> (v_agent is not null)
or (v_scope = 'chat') <> (v_chat is not null)
or (v_scope = 'project') <> (v_project is not null)
or (v_scope = 'record') <> (v_subject_id is not null)
or (v_subject_id is not null) <> (v_subject_type is not null)
or (v_owner is null and (v_scope not in ('organization', 'record') or v_private)) then
raise exception 'ns % is not a memory namespace', memory_search.ns using errcode = '22023', hint = 'MEMORY_NAMESPACE';
end if;
perform set_config('hnsw.iterative_scan', 'relaxed_order', true);
with scoped as materialized (
select x."id" as id from "better_supabase"."memories" x
where x."tenant_id" = memory_search.tenant and x."owner_id" is not distinct from v_owner and x."scope" = v_scope and x."agent_id" is not distinct from v_agent and x."chat_id" is not distinct from v_chat and x."project_id" is not distinct from v_project and x."subject_type" is not distinct from v_subject_type and x."subject_id" is not distinct from v_subject_id and x."superseded_by" is null and x."archived_at" is null and (x."expires_at" is null or x."expires_at" > now()) and x."kind" = 'archival'
),
vector_hits as materialized (
select t."id" as id, t."embedding" operator(extensions.<=>) memory_search.query_embedding as distance
from "better_supabase"."memories" t
where memory_search.query_embedding is not null and t."embedding" is not null and t."id" in (select scoped.id from scoped)
order by t."embedding" operator(extensions.<=>) memory_search.query_embedding
limit v_candidates
),
vector_ranked as (
select h.id, h.distance, row_number() over (order by h.distance) as rank from vector_hits h
),
text_ranked as (
select t."id" as id, row_number() over (order by ts_rank_cd(t."tsv", q) desc) as rank
from "better_supabase"."memories" t, websearch_to_tsquery('simple'::regconfig, memory_search.query_text) q
where memory_search.query_text is not null and t."tsv" @@ q and t."id" in (select scoped.id from scoped)
limit v_candidates
),
fused as materialized (
select r.id, r.distance, r.score * coalesce(t."confidence", 1) as score
from (
select coalesce(v.id, x.id) as id, v.distance,
coalesce(1.0 / (60 + v.rank), 0) + coalesce(1.0 / (60 + x.rank), 0) as score
from vector_ranked v full join text_ranked x on x.id = v.id
) r join "better_supabase"."memories" t on t."id" = r.id
order by score desc
limit least(greatest(memory_search.k, 1), 100)
),
used as (
update "better_supabase"."memories" t set "use_count" = t."use_count" + 1, "last_used_at" = now()
from fused f where t."id" = f.id
)
select coalesce(jsonb_agg(jsonb_build_object('id', t."id", 'tenant_id', t."tenant_id", 'owner_id', t."owner_id", 'scope', t."scope", 'agent_id', t."agent_id", 'chat_id', t."chat_id", 'project_id', t."project_id", 'kind', t."kind", 'path', t."path", 'content', t."content", 'version', t."version", 'embedding_model', t."embedding_model", 'source_message_id', t."source_message_id", 'subject_type', t."subject_type", 'subject_id', t."subject_id", 'category', t."category", 'confidence', t."confidence", 'expires_at', t."expires_at", 'use_count', t."use_count", 'last_used_at', t."last_used_at", 'created_at', t."created_at", 'updated_at', t."updated_at") || jsonb_build_object('score', f.score, 'similarity', 1 - f.distance) order by f.score desc), '[]')
into v_rows
from fused f join "better_supabase"."memories" t on t."id" = f.id;
perform set_config('hnsw.iterative_scan', coalesce(nullif(v_scan, ''), 'off'), true);
return v_rows;
end;
$$;
revoke execute on function "better_supabase"."memory_search"(uuid, extensions.vector, text, jsonb, integer) from public, anon;
grant execute on function "better_supabase"."memory_search"(uuid, extensions.vector, text, jsonb, integer) to authenticated, service_role;
-- Forgets a memory: its owner, an admin for organization or record memory,
-- or the service role. superseded_by points a replaced fact at its
-- successor; otherwise the memory is deleted and stays restorable until
-- purge_memories removes it.
create or replace function "better_supabase"."memory_forget"(memory_id uuid, superseded_by uuid default null)
returns boolean
language plpgsql
security definer
set search_path = ''
as $$
declare
v_row "better_supabase"."memories"%rowtype;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select * into v_row from "better_supabase"."memories" x where x."id" = memory_forget.memory_id;
if not found or not (coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin') or coalesce(v_row."owner_id" = auth.uid(), false) or (v_row."owner_id" is null and coalesce(better_supabase.can('tenant', v_row."tenant_id", 'memory.manage'), false))) then
return false;
end if;
if memory_forget.superseded_by is null then
update "better_supabase"."memories" x set "archived_at" = coalesce(x."archived_at", now()), "updated_at" = now() where x."id" = v_row."id";
else
update "better_supabase"."memories" x set "superseded_by" = memory_forget.superseded_by, "updated_at" = now() where x."id" = v_row."id";
end if;
return true;
end;
$$;
revoke execute on function "better_supabase"."memory_forget"(uuid, uuid) from public, anon;
grant execute on function "better_supabase"."memory_forget"(uuid, uuid) to authenticated, service_role;
-- Restores a deleted memory, with the same rights as memory_forget. Fails
-- when a live file already has its path.
create or replace function "better_supabase"."memory_restore"(memory_id uuid)
returns boolean
language plpgsql
security definer
set search_path = ''
as $$
declare
v_row "better_supabase"."memories"%rowtype;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select * into v_row from "better_supabase"."memories" x where x."id" = memory_restore.memory_id and x."archived_at" is not null;
if not found or not (coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin') or coalesce(v_row."owner_id" = auth.uid(), false) or (v_row."owner_id" is null and coalesce(better_supabase.can('tenant', v_row."tenant_id", 'memory.manage'), false))) then
return false;
end if;
update "better_supabase"."memories" x set "archived_at" = null, "updated_at" = now() where x."id" = v_row."id";
return true;
exception when unique_violation then
raise exception '% already exists', v_row."path" using errcode = '23505', hint = 'MEMORY_EXISTS';
end;
$$;
revoke execute on function "better_supabase"."memory_restore"(uuid) from public, anon;
grant execute on function "better_supabase"."memory_restore"(uuid) to authenticated, service_role;
-- Removes memories deleted more than retention_days ago and expired ones, up to
-- batch rows, in every tenant or only in tenant, and returns how many it
-- removed.
create or replace function "better_supabase"."purge_memories"(retention_days integer default 30, batch integer default 1000, tenant uuid default null)
returns integer
language plpgsql
security definer
set search_path = ''
as $$
declare
v_count integer;
begin
delete from "better_supabase"."memories" x
where x."id" in (
select y."id" from "better_supabase"."memories" y
where (purge_memories.tenant is null or y."tenant_id" = purge_memories.tenant)
and (y."archived_at" < now() - make_interval(days => greatest(purge_memories.retention_days, 0)) or y."expires_at" < now())
limit least(greatest(purge_memories.batch, 1), 10000)
);
get diagnostics v_count = row_count;
return v_count;
end;
$$;
revoke execute on function "better_supabase"."purge_memories"(integer, integer, uuid) from public, anon, authenticated;
grant execute on function "better_supabase"."purge_memories"(integer, integer, uuid) to service_role;
-- content_hash is the md5 of the content that was embedded: a memory edited
-- since then keeps waiting instead of taking a vector of its old text.
create or replace function "better_supabase"."set_memory_embedding"(memory_id uuid, embedding extensions.vector, model text default null, content_hash text default null)
returns boolean
language sql
security definer
set search_path = ''
as $$
update "better_supabase"."memories" x set "embedding" = set_memory_embedding.embedding, "embedding_model" = set_memory_embedding.model
where x."id" = set_memory_embedding.memory_id
and (set_memory_embedding.content_hash is null or md5(x."content") = set_memory_embedding.content_hash)
returning true
$$;
revoke execute on function "better_supabase"."set_memory_embedding"(uuid, extensions.vector, text, text) from public, anon, authenticated;
grant execute on function "better_supabase"."set_memory_embedding"(uuid, extensions.vector, text, text) to service_role;
-- Stores embeddings for many memories ({"items": [{"id", "hash", "embedding"}]})
-- and returns how many it stored; one whose content no longer matches its
-- hash keeps waiting.
create or replace function "better_supabase"."set_memory_embeddings"(items jsonb, model text default null)
returns integer
language plpgsql
security definer
set search_path = ''
as $$
declare
v_items jsonb := case when jsonb_typeof(set_memory_embeddings.items) = 'object' then set_memory_embeddings.items -> 'items' else set_memory_embeddings.items end;
v_count integer;
begin
update "better_supabase"."memories" x set
"embedding" = (e.value ->> 'embedding')::extensions.vector(1536),
"embedding_model" = set_memory_embeddings.model
from jsonb_array_elements(case when jsonb_typeof(v_items) = 'array' then v_items else '[]' end) e
where x."id" = (e.value ->> 'id')::uuid and md5(x."content") = e.value ->> 'hash';
get diagnostics v_count = row_count;
return v_count;
end;
$$;
revoke execute on function "better_supabase"."set_memory_embeddings"(jsonb, text) from public, anon, authenticated;
grant execute on function "better_supabase"."set_memory_embeddings"(jsonb, text) to service_role;
-- Memories edited since they were embedded, for the embed worker, with the
-- md5 of each content it passes back to set_memory_embeddings.
create or replace function "better_supabase"."pending_memory_embeddings"(batch integer default 64)
returns jsonb
language sql
stable
security definer
set search_path = ''
as $$
select coalesce(jsonb_agg(jsonb_build_object('id', x."id", 'content', x."content", 'hash', md5(x."content"))), '[]')
from (
select * from "better_supabase"."memories" y where y."embedding" is null and y."superseded_by" is null and y."archived_at" is null and (y."expires_at" is null or y."expires_at" > now())
order by y."updated_at"
limit least(greatest(pending_memory_embeddings.batch, 1), 2048)
) x
$$;
revoke execute on function "better_supabase"."pending_memory_embeddings"(integer) from public, anon, authenticated;
grant execute on function "better_supabase"."pending_memory_embeddings"(integer) to service_role;
-- Stores a message's embedding. Returns false for temporary chats.
create or replace function "better_supabase"."set_ai_message_embedding"(
chat_id uuid,
message_id text,
user_id uuid,
tenant uuid,
embedding extensions.vector,
model text default null
)
returns boolean
language plpgsql
security definer
set search_path = ''
as $$
#variable_conflict use_column
begin
insert into "better_supabase"."memory_message_embeddings" ("chat_id", "message_id", "user_id", "tenant_id", "embedding", "embedding_model")
values (set_ai_message_embedding.chat_id, set_ai_message_embedding.message_id, set_ai_message_embedding.user_id, set_ai_message_embedding.tenant, set_ai_message_embedding.embedding, set_ai_message_embedding.model)
on conflict ("chat_id", "message_id") do update set "embedding" = excluded."embedding", "embedding_model" = excluded."embedding_model";
return true;
end;
$$;
revoke execute on function "better_supabase"."set_ai_message_embedding"(uuid, text, uuid, uuid, extensions.vector, text) from public, anon, authenticated;
grant execute on function "better_supabase"."set_ai_message_embedding"(uuid, text, uuid, uuid, extensions.vector, text) to service_role;
-- The caller's earlier messages closest to a query, other than in
-- exclude_chat. Volatile and restores hnsw.iterative_scan, as memory_search.
create or replace function "better_supabase"."recall_ai_messages"(
tenant uuid,
query_embedding extensions.vector,
k integer default 5,
exclude_chat uuid default null,
owner uuid default null
)
returns jsonb
language plpgsql
volatile
security definer
set search_path = ''
as $$
declare
v_user uuid := case when coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin') then recall_ai_messages.owner else auth.uid() end;
v_rows jsonb;
v_scan text := current_setting('hnsw.iterative_scan', true);
begin
if v_user is null then
return '[]';
end if;
perform set_config('hnsw.iterative_scan', 'relaxed_order', true);
select coalesce(jsonb_agg(jsonb_build_object('chat_id', r.chat_id, 'message_id', r.message_id, 'similarity', 1 - r.distance) order by r.distance), '[]')
into v_rows
from (
select x."chat_id" as chat_id, x."message_id" as message_id, x."embedding" operator(extensions.<=>) recall_ai_messages.query_embedding as distance
from "better_supabase"."memory_message_embeddings" x
where x."user_id" = v_user and x."tenant_id" = recall_ai_messages.tenant
and x."chat_id" is distinct from recall_ai_messages.exclude_chat
order by x."embedding" operator(extensions.<=>) recall_ai_messages.query_embedding
limit least(greatest(recall_ai_messages.k, 1), 50)
) r;
perform set_config('hnsw.iterative_scan', coalesce(nullif(v_scan, ''), 'off'), true);
return v_rows;
end;
$$;
revoke execute on function "better_supabase"."recall_ai_messages"(uuid, extensions.vector, integer, uuid, uuid) from public, anon;
grant execute on function "better_supabase"."recall_ai_messages"(uuid, extensions.vector, integer, uuid, uuid) to authenticated, service_role;
-- Documents an agent runtime keeps under an opaque scope key, such as eve's
-- file memory and its replay records. Only the service role reads or writes
-- them; a write names the version it read, so a stale write fails.
create table "better_supabase"."memory_documents" (
"scope_key" text not null constraint bsb_memory_documents_scope_key_check check (length("scope_key") <= 4096),
"path" text not null constraint bsb_memory_documents_path_check check (length("path") <= 1024),
"content" text not null constraint bsb_memory_documents_content_check check (length("content") <= 100000),
"version" bigint not null default 1,
"expires_at" timestamptz,
"created_at" timestamptz not null default now(),
"updated_at" timestamptz not null default now(),
primary key ("scope_key", "path")
);
create index memory_documents_expires_idx on "better_supabase"."memory_documents" ("expires_at") where "expires_at" is not null;
alter table "better_supabase"."memory_documents" enable row level security;
revoke all on "better_supabase"."memory_documents" from public, anon, authenticated;
grant all on "better_supabase"."memory_documents" to service_role;
-- A document and its version, or null.
create or replace function "better_supabase"."memory_document_read"(scope_key text, path text)
returns jsonb
language sql
stable
security definer
set search_path = ''
as $$
select jsonb_build_object('content', x."content", 'version', x."version"::text)
from "better_supabase"."memory_documents" x
where x."scope_key" = memory_document_read.scope_key and x."path" = memory_document_read.path
and (x."expires_at" is null or x."expires_at" > now())
$$;
revoke execute on function "better_supabase"."memory_document_read"(text, text) from public, anon, authenticated;
grant execute on function "better_supabase"."memory_document_read"(text, text) to service_role;
-- Writes a document when its version is still expected_version; null means
-- it must not exist yet. Otherwise raises MEMORY_DOCUMENT_CONFLICT.
create or replace function "better_supabase"."memory_document_write"(scope_key text, path text, content text, expected_version text default null, expires_in integer default null)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
#variable_conflict use_column
declare
v_row "better_supabase"."memory_documents"%rowtype;
v_expires timestamptz := case when memory_document_write.expires_in is null then null else now() + make_interval(secs => memory_document_write.expires_in) end;
begin
if length(memory_document_write.content) > 100000 then
raise exception 'memory content is limited to % characters', 100000 using errcode = '22023', hint = 'MEMORY_TOO_LARGE';
end if;
delete from "better_supabase"."memory_documents" x
where x."scope_key" = memory_document_write.scope_key and x."path" = memory_document_write.path
and x."expires_at" <= now();
if memory_document_write.expected_version is null then
insert into "better_supabase"."memory_documents" ("scope_key", "path", "content", "expires_at")
values (memory_document_write.scope_key, memory_document_write.path, memory_document_write.content, v_expires)
on conflict ("scope_key", "path") do nothing
returning * into v_row;
else
update "better_supabase"."memory_documents" x set "content" = memory_document_write.content, "version" = x."version" + 1,
"expires_at" = v_expires, "updated_at" = now()
where x."scope_key" = memory_document_write.scope_key and x."path" = memory_document_write.path
and x."version"::text = memory_document_write.expected_version
returning * into v_row;
end if;
if not found then
raise exception '% changed since version %', memory_document_write.path, coalesce(memory_document_write.expected_version, 'none') using errcode = '40001', hint = 'MEMORY_DOCUMENT_CONFLICT';
end if;
return jsonb_build_object('content', v_row."content", 'version', v_row."version"::text);
end;
$$;
revoke execute on function "better_supabase"."memory_document_write"(text, text, text, text, integer) from public, anon, authenticated;
grant execute on function "better_supabase"."memory_document_write"(text, text, text, text, integer) to service_role;
-- Deletes expired documents, at most batch. Returns how many.
create or replace function "better_supabase"."purge_memory_documents"(batch integer default 1000)
returns integer
language plpgsql
security definer
set search_path = ''
as $$
declare
v_count integer;
begin
delete from "better_supabase"."memory_documents" x
where (x."scope_key", x."path") in (
select y."scope_key", y."path" from "better_supabase"."memory_documents" y
where y."expires_at" <= now()
limit least(greatest(purge_memory_documents.batch, 1), 10000)
);
get diagnostics v_count = row_count;
return v_count;
end;
$$;
revoke execute on function "better_supabase"."purge_memory_documents"(integer) from public, anon, authenticated;
grant execute on function "better_supabase"."purge_memory_documents"(integer) to service_role;
-- Tenant isolation (ADR 0099): permissive policies combine with or, so one
-- the app adds would widen the block's. These restrictive policies keep each
-- table to the caller's organizations and what the block's policies grant.
create policy bsb_tenant_scope_select on "better_supabase"."memories" as restrictive
for select to public
using (
"tenant_id" in (select better_supabase.member_organization_ids())
or (("owner_id" = (select auth.uid()) or ("owner_id" is null and "tenant_id" in (select better_supabase.tenant_ids_with('memory.read')))) and "archived_at" is null)
);
create policy bsb_tenant_scope_insert on "better_supabase"."memories" as restrictive
for insert to public
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_update on "better_supabase"."memories" as restrictive
for update to public
using ("tenant_id" in (select better_supabase.member_organization_ids()))
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_delete on "better_supabase"."memories" as restrictive
for delete to public
using ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_select on "better_supabase"."memory_message_embeddings" as restrictive
for select to public
using (
"tenant_id" in (select better_supabase.member_organization_ids())
or ("user_id" = (select auth.uid()))
);
create policy bsb_tenant_scope_insert on "better_supabase"."memory_message_embeddings" as restrictive
for insert to public
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_update on "better_supabase"."memory_message_embeddings" as restrictive
for update to public
using ("tenant_id" in (select better_supabase.member_organization_ids()))
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_delete on "better_supabase"."memory_message_embeddings" as restrictive
for delete to public
using ("tenant_id" in (select better_supabase.member_organization_ids()));
Client
This block has no React entry yet. Its TypeScript side is in better-supabase/blocks/memory, which runs on the server.
Events
With the outbox installed, the module writes these in the same transaction as the change.
This module writes no outbox events.
Dependencies
- Required: the install command adds it too
- Works with: optional
Works with
Recipes
No UI recipe uses it yet. See all recipes.
More in AI
Vector searchvector-search search_<table>(query, k) for each table in vectorSearch: the k nearest rows the caller can read, with pgvector iterative index scans so RLS filters still return k rows. AI chatai-chat Chats, projects, participants and a message tree (or one linear list) in the canonical AI message format or the SDK's own parts, with runs (and the id of a durable engine's run), run steps for progress, a compare-and-set stream claim, harness sessions, tool approvals and policies, pending inputs, cited sources, feedback, hashed share links, a model catalog per plan, moderation events and private Realtime topics per chat and per user.React hooks AI filesai-files Files and documents for AI chats: a private bucket whose policies only accept an upload a reserved record expects, provider file references with expiry, versioned documents with suggested edits, and a purge that hands back the objects to remove. Knowledgeknowledge Documents and chunks for retrieval with an embedding and a tsvector each, scoped to an organization, agent, project, chat, user or record (subject) and grouped into collections with a visibility, a priority and grants, plus system documents shared with every tenant; hybrid search with reciprocal rank fusion, chunks that keep their embedding when unchanged, and an embed job per document when jobs is installed. With ai-files and jobs, queue_knowledge_file ingests a file through a job. Agentsagents Custom assistants with instructions, a model, tools, connectors, knowledge scopes and starters; private until published to the organization or the public store, with installs, ratings and provider skills. AI scheduled tasksai-schedules Prompts a user schedules on a cron, with a run per occurrence; the scheduler claims due tasks, queues an ai_task_run job when jobs is installed, and fails runs that stall.