Identities
Run bot jobs as the chat author's linked user, decide approval buttons through the authorization provider, and link approval prompts to the app.
A chat message names a platform user, not an app user. These helpers resolve the author through principalByIdentity, usually the identities block, and act only for a linked user. An unlinked author stays anonymous.
Each helper takes principalByIdentity directly or reads it from bsb.authorization.identities, where bsb is the createServer(betterSupabase, { authorization }) instance; pass it as server: bsb. The option passed to the helper wins.
Bot jobs as the linked user
botJobs resolves the author, then enqueues the job with a ticket for a linked user. The worker's forContext(job.context) turns the ticket back into that user's context through the provider's tickets.subject, so the bot reads and writes with the user's row level security.
import { botJobs, chatIdentityOf } from "better-supabase/chat-sdk";
const botQueue = botJobs({
server: bsb,
enqueue: (payload, options) => jobs.enqueue("bot_reply", payload, options),
ticketFor: (userId) => tickets.issue(userId),
});
bot.onNewMention(async (thread, message) => {
await botQueue.enqueue(chatIdentityOf(message.author, thread.adapter), {
threadId: thread.id,
text: message.text,
});
});ticketFor is the app's: the authorization provider issues the opaque ticket its tickets.subject accepts. An author with no link, or one linked to a contact, gets no ticket and no context, so the job runs anonymous. It never falls back to the bot's service role. When the resolver or ticketFor throws, nothing is enqueued and the promise rejects.
Pass the options argument to jobs.enqueue as it is. Adding a context there would give an unlinked author's job a user.
Approval buttons
chatActions decides a button press through the provider's chat.onAction hook. It resolves the author, then calls onAction(action, author).
import { chatActionOf, chatActions } from "better-supabase/chat-sdk";
const decide = chatActions({ server: bsb });
bot.onAction("approve", async (event) => {
const decision = await decide(chatActionOf(event));
if (!decision.allowed) {
await event.thread?.post("You can't approve this request here.");
return;
}
await approvals.approve(event.value, { as: decision.userId });
});Everything except allowed: true refuses, with a reason:
reason | When |
|---|---|
unlinked | The author has no link. The hook never runs. |
unhandled | There is no onAction hook, or it returned undefined. |
refused | The hook returned allowed: false, or a userId other than the linked author's. |
error | The resolver or the hook threw or rejected. |
For a user author, decision.userId is always the linked user. For a contact, it is the user the hook named, if any.
Approval prompts
An approval sent by chat or email links to the app's approval page. Opening the link approves nothing: the approver signs in, and the page's server action or POST route decides. better-supabase has no GET handler that approves.
import { approvalLink, approvalPrompt } from "better-supabase/chat-sdk";
const request = { kind: "tool", subject: approvalId, requester: userId };
await thread.post(
approvalPrompt({
page: "https://app.example.com/approvals",
request,
text: "The agent wants to send invoice 1042.",
}),
);
const emailUrl = approvalLink("https://app.example.com/approvals", request);approvalLink adds kind and subject as query parameters. approvalPrompt returns { markdown } with the link, labelled Review the request unless you pass label.
History keyed by the linked user
Chat SDK 4.45 keeps a per-user message history across platforms and threads,
under a key your identity resolver returns. chatHistory builds that
history option from the same principalByIdentity the other helpers use, so
the key is the linked app user (user:<id>), not an email address or a
platform id, and the same person on Slack and Teams shares one list. An author
without a link gets no key and nothing is stored. A contact's history is kept
only with contacts: true, under contact:<id>.
import { Chat } from "chat";
import { chatHistory } from "better-supabase/chat-sdk";
export const bot = new Chat({
// adapters, state ...
history: chatHistory({
server,
retention: "30d",
maxPerUser: 100,
workspaceOf: ({ message }) => teamOf(message),
}),
});workspaceOf is for an app that serves several Slack workspaces: return the
same value you pass to chatIdentityOf, or the link lookup misses. Set
retention, since the SDK keeps history forever when you don't.
eraseChatHistory(bot.history, { userId }) deletes everything stored for a
user and returns the count. Call it from your account deletion and data
subject request flows ({ contactId } does the same for a contact). It covers
the user scope only: the Chat SDK has no index from a user to the thread
caches that adapters write, so those expire with history.thread.ttlMs.
Last updated on
Channels
Record Slack, WhatsApp, Messenger and SMS conversations in the inbox with Chat SDK adapters, deliver staff replies on their channel and keep the event store clean.
eve on Supabase
Run eve agents on Supabase with the Workflow World on Postgres, Supabase sign-in on eve routes, tools that run as the user, approvals, queue schedules, MCP and OpenAPI connections, memory, sessions in the chat tables and the inbox as a channel.