Assistant
The admin app can show an assistant: an Assistant button at the end of the top bar opens a chat beside the page. The app only draws what a backend sends and sends back what the user types and clicks; the backend decides everything else. Protobase ships one backend, built in, which answers questions about the app, reads its data and proposes changes the user approves. Another backend can take its place by URL.
Who sees it
Section titled “Who sees it”/api/meta names the backend as assistant: { url }, and only for a caller with the admin or ai role. The app shows the button when /meta has it, so without a backend, or for anyone else, nothing shows. Give someone the assistant without making them an admin by adding ai to the project’s roles (auth: { roles: ['admin', 'ai', ...] } in protobase.config.ts, see Roles) and to their user (protobase users set-role [email protected] sales,ai).
The built-in backend checks the role again on every request.
Which backend
Section titled “Which backend”| Set | /meta names |
|---|---|
PROTOBASE_ASSISTANT_URL |
that URL: a backend elsewhere, such as a hosted one |
otherwise PROTOBASE_ASSISTANT_API_KEY |
/api/assistant, the built-in backend |
| neither | no assistant |
Each variable can also be set in assistant of protobase.config.ts (assistant: { model: '...' }), which wins over the variable. assistant: false turns the assistant off whatever the environment says. A value that is no http(s) URL stops the server at startup. The model settings also serve AI fields and actions, even with a backend elsewhere or assistant: false.
| Variable | Option | |
|---|---|---|
PROTOBASE_ASSISTANT_URL |
url |
an external backend |
PROTOBASE_ASSISTANT_API_KEY |
apiKey |
the model endpoint’s API key; turns on the built-in backend |
PROTOBASE_ASSISTANT_MODEL |
model |
the model; default openrouter/auto on OpenRouter, required elsewhere |
PROTOBASE_ASSISTANT_BASE_URL |
baseUrl |
an OpenAI-compatible endpoint; default https://openrouter.ai/api/v1 |
With OpenRouter
Section titled “With OpenRouter”Create a key at openrouter.ai and set it, with a model of your choice:
PROTOBASE_ASSISTANT_API_KEY=sk-or-v1-...PROTOBASE_ASSISTANT_MODEL=anthropic/claude-sonnet-5.5 # optional; OpenRouter picks one without itOn OpenRouter the built-in backend asks for low reasoning effort and marks the system prompt and the earlier chat for prompt caching, so models that cache, such as Anthropic’s, bill the repeated part of each request as a cheaper cache read.
With another OpenAI-compatible endpoint
Section titled “With another OpenAI-compatible endpoint”Any endpoint that serves POST <base URL>/chat/completions with streaming and tool calls works: OpenAI itself, a gateway, or a local server such as Ollama or vLLM. Name the model:
PROTOBASE_ASSISTANT_BASE_URL=http://localhost:11434/v1PROTOBASE_ASSISTANT_API_KEY=ollama # sent as the bearer token; a local server may ignore itPROTOBASE_ASSISTANT_MODEL=llama3.3The model needs tool calling for the queries. A refused key, an exhausted account or a wrong model shows in the chat as a red card with the endpoint’s own message, such as The model endpoint https://openrouter.ai/api/v1 answered 401: No auth credentials found (check the API key).
The built-in backend
Section titled “The built-in backend”It lives at /api/assistant on the app’s own origin and answers with the user’s token like the rest of the API. Its system prompt describes the resources, tables and fields the user can see in /meta. It has two tools of its own, and the app’s:
run_read_only_queryruns oneSELECT(orVALUES, or aWITHquery) and shows its rows as a table in the chat. It runs in aREAD ONLYtransaction that is always rolled back, with a statement timeout of 5 seconds and at most 200 rows. Postgres refuses every write inside (SET TRANSACTION READ WRITE, writable CTEs,nextval,DOblocks, functions that write), and settings it changes go with the rollback. The statement runs through the functionquery.run_read_onlyas the role<database>_query, whichprotobase migrationmakes: that role may read every column of the resources’ tables,.sensitive()fields and the webhook subscriptions’ signing secrets included, and nothing else: not the auth store, nor a table without a resource. The function opens the statement as a cursor, so only one statement runs, and Postgres refuses a change of role inside it. Numbers other than integers of up to 15 digits come back as their exact text, such as"12.50".propose_write_querynever runs on its own. It shows the statement and a summary on a card with Approve and Reject. Only Approve runs it, as one statement in a read-write transaction; Reject, an answer after 10 minutes, or a new message sent instead of an answer, runs nothing. The model gets the returned rows, or that nothing ran. Statements areINSERT,UPDATEorDELETEwith aRETURNINGclause.
Both run outside Protobase’s access rules and tenant scoping: whoever has the admin or ai role can read every resource’s table, and with approval change everything the app’s database role can, since propose_write_query still runs as that role. Give the app a database role without superuser rights, and those roles only to people who may see all of the data.
Making the query role needs CREATEROLE once, for the role that applies the migrations; that role becomes a member of it and may run the function. The query role bypasses row-level security only when the migrating role may grant that, a superuser or, on Postgres 16 and later, a role with BYPASSRLS; otherwise it reads what the tables’ policies give it. When the app serves as another database role than the one that migrates, grant that role usage on schema query and execute on function query.run_read_only(text, integer). protobase migration writes the role when it finds the built-in assistant’s key, PROTOBASE_ASSISTANT_API_KEY in the .env it reads or assistant.apiKey of the config, so run it with the key set even when only production has the real one, or when mcpEndpoint.sql is on, whose run_read_only_query runs as the same role. The server does not start while the record of migrations lacks the role, its function or a resource’s grants, while the role can read an auth table, or while it may call a SECURITY DEFINER function, other than a trigger function, whose owner can read one: the statement would read the auth store through it, so revoke execute on such a function from PUBLIC, or keep it in a schema without a resource. Adding a resource writes its grants into the next migration, and removing one takes them back.
The app’s own tools
Section titled “The app’s own tools”assistant/tools.ts adds tools of the app’s own, which the built-in backend offers after its two: its default export is an array of tools, found by that path when the app starts. A tool is defineTool({ name, description, parameters, run }) from @protobase/server: parameters is the JSON Schema of its arguments, and run(args, context) returns the text the model reads. A name must be 1 to 64 letters, digits, _ or -, and a name taken twice stops the server at startup. A backend elsewhere has its own tools, so the file does nothing there, nor with assistant: false. A tool’s annotations are MCP’s tool hints (readOnlyHint, destructiveHint, …), which the dock ignores. The file serves the dock only, not the app’s MCP endpoint: tools for AI clients over MCP go in mcp/tools.ts, which can export these too (see The app’s own tools on MCP).
context holds the user’s session, locale (the language they read, 'en' or 'nl', for the texts a tool shows them such as its approvals; set over MCP, absent in the dock), show(part) to add a part to the answer, approve(request) for an approval card (in the dock, the turn stops at the card and goes on once the user answers: the tool then runs again from the start and approve gives the answer, as over MCP, so a tool calls it once, before it changes anything), conversation (the dock’s, absent over MCP), resources() (the resources the user may read, restricted to their fields, with their views), and records: list(resource, params) with the REST API’s list parameters, aggregate(resource, { group_by, filter?, limit?, label? }) with its counts per value as :aggregate gives them, get(resource, key), create(resource, data), update(resource, key, data, { etag? }) (without an ETag it updates any version), delete(resource, key, { etag? }) (soft on a resource with .softDelete()) and softDeleteField(resource) (the field a delete sets, or undefined when a delete removes the record) on the app’s resources as the user, through their access rules, tenant scope, validation and write hooks, as in the REST API. get, create, update and delete each answer { record, etag }, and list a page of records. batch(operations) runs the operations of a REST batch write in one transaction and answers its results; a refusal or a missing record rejects with an HttpProblem, whose detail a tool can return to the model as text, such as with .catch(refused): refused from @protobase/server turns a refusal into { "error": <slug>, "message": <detail> } and throws anything else again.
A tool with a widget creates or finds what it is about, shows a widget with its id, and returns the id and the state at that moment:
import { defineTool, widget } from '@protobase/server'
export const proposeTask = defineTool({ name: 'propose_task', description: 'Proposes a task; someone approves or declines it on its card, now or later. Read it again before relying on its status.', parameters: { type: 'object', properties: { title: { type: 'string' } }, required: ['title'] }, run: async (args, { records, show }) => { const { record } = await records.create('tasks', { title: args.title, status: 'proposed' }) show(widget('TaskProposal', { taskId: record.id }, { fallback: { title: `Task proposal: ${String(record.title)}` } })) return JSON.stringify({ taskId: record.id, status: 'proposed' }) },})
// assistant/tools.ts: import { proposeTask } from './propose-task'; export default [proposeTask]// components/task-proposal.tsx, exported from protobase.ui.tsx: export { TaskProposal } from './components/task-proposal'export const TaskProposal = ({ taskId }: { taskId: number }) => { const task = useRecord('tasks', taskId) const update = useUpdateRecord('tasks', taskId) if (!task.data) return <ActionCard title="Task proposal" note={task.error ? 'Could not load the task' : 'Loading'} /> const { record, etag } = task.data return ( <ActionCard title={String(record.title)} badge={String(record.status)} note={record.status === 'proposed' ? undefined : `${String(record.status)} by ${String(record.decidedBy)}`} actions={record.status === 'proposed' ? [{ id: 'approved', label: 'Approve', style: 'primary' }, { id: 'declined', label: 'Decline' }] : []} onAction={(status) => update.mutate({ patch: { status }, etag })} /> )}The tool and its component live apart, joined by the name, since the server config cannot import @protobase/ui and the browser must not get server code; put shared prop types in a file both import with import type. Whoever may update the task decides on it, now or next month, from the chat or the task’s own page, and a write hook can stamp who did from event.user; when two decide at once, the ETag makes the second write fail and the widget shows the first decision. The chat is not told: the model learns of the change by reading the record again, which the description asks it to do.
Each message carries the page the user is on, such as /orders/42, which the model reads with it. A message is at most 4,000 characters, and a turn takes at most 8 steps of tool calls, after which the answer ends with Stopped after 8 steps.
assistant.conversation_records, with the chat and everything the model read, so a restart keeps it and the app only ever sends the new message. protobase migration writes the assistant schema when it finds the built-in assistant’s key, as it does the query role, and protobase migrate applies it before the new version serves; the server does not start while the record of migrations lacks it.
Every instance serving the app shares the table, so a conversation carries on whichever instance a request reaches. Each request reads the conversation from the database, unless a turn of it is running on that instance. A card waiting for approval is part of the conversation: any instance takes the answer, and after a restart too, and only the first answer to a card counts. The instance that takes the answer runs the rest of the turn; for 10 minutes after the answer, the others show the answer on the card and refuse a new message with 409 until that turn is saved. A reply streams to the tabs connected to the instance that runs it. A tab connected to another instance gets the reply whole once it is saved: while a tab is open and no turn runs there, its instance checks the conversation’s number of records every 2 seconds (assistant.checkEveryMs) and reads it again when another instance added any. A turn claims its place in the conversation before any tool runs: when two instances start a turn on one conversation at once, the second runs nothing and shows a card that asks to send the message again, and so does a turn whose end loses to a message sent on another instance meanwhile, which then keeps nothing of its turn. If an instance stops after an approval and before it saved the result, the call does not run again: once those 10 minutes passed, the model is told that it is not known whether the call ran.
Another backend
Section titled “Another backend”A backend elsewhere replaces the built-in one through PROTOBASE_ASSISTANT_URL. It receives the user’s API token as Authorization: Bearer <token>, verifies it against the app’s key set (/api/auth/jwks), and allows the app’s origin with CORS. It can be built from the building blocks @protobase/server exports (the protocol’s routes, assistantProtocolRoutes, the turn loop with tool calling, runTurn, and resumeTurn for the turn whose card was answered, tools, approvals, the query tools, withCacheBreakpoints, which marks a chat’s messages for prompt caching, and createConversations over a conversation store: databaseConversationStore on a database with the assistant schema, fileConversationStore for a backend without a database on one instance, or one of your own) with its own prompt and tools; the built-in backend’s source, packages/server/src/assistant/built-in-assistant.ts, is the reference.
The protocol
Section titled “The protocol”The dock and a backend speak three requests on the backend’s URL, each with the user’s bearer token:
| Request | What it does |
|---|---|
GET <url>/events |
Server-sent events, each a JSON AssistantEvent: state replaces the whole chat and always comes first on a connection, message adds a message or replaces the one with its id, part adds a part to a message or replaces the one with its id, and patch changes replying, status or the composer’s placeholder. |
POST <url>/messages |
{ text, page? }: what the user wrote, and the page they are on. The built-in backend answers 202 and replies with events, or 409 while it still answers the last message. |
POST <url>/actions |
{ partId, actionId }: the user clicked a button of a card. The built-in backend answers 202 and goes on with the turn that waited for the card, 404 when no card waits for that button, or 409 while it still answers, or when another request answered or changed the conversation first. |
The types are exported from @protobase/schema (AssistantEvent, AssistantState, AssistantMessage, AssistantPart and its text, table, card and widget parts, AssistantMessageRequest, AssistantActionRequest). createAssistantClient({ url, token }) from @protobase/client, where token is a function that gives the user’s token, follows the event stream, reconnecting by itself, and sends the two requests; the dock uses it.
Widget parts
Section titled “Widget parts”Besides text, tables and cards, a backend can show a widget: a part that one of the app’s own React components draws. The chat keeps only the component’s name and a small JSON of props, such as { "taskId": 12 }, and the component fetches what it shows when it is drawn, so a chat from last month shows the record as it is today, including a decision someone else made since.
import { widget } from '@protobase/server'
show(widget('TaskProposal', { taskId: record.id }, { fallback: { title: `Task proposal: ${record.title}` } }))widget(name, props, { id?, fallback? }) builds the part { type: 'widget', id, name, props, fallback? }. The name must be PascalCase, and the props a JSON object of at most 2 KB, or it throws: pass ids and settings, not the state the widget shows. Without an id the part gets a new one; showing a part with the same id again replaces it. fallback is drawn as a plain card where the app has no component by that name. The part never reaches the model, which reads only the tool’s result.
How the dock draws them
Section titled “How the dock draws them”The dock draws a widget part with the component the app’s protobase.ui.tsx exports under that name, the same components composed pages use, giving it the part’s props. The component reads what it shows when it is drawn, as the signed-in user: the dock is mounted only while it is open, so each opening, and each page load, reads it again. Its buttons call the app’s API or the backend directly; they need no turn of the chat, so they work after a restart, from any tab and for any user who may make the call.
- No component by that name, such as in an app built before the component was added: the dock draws the fallback as a plain card noting “This version of the app cannot show it live”, or, without a fallback, a muted line naming the component.
- A component that throws is caught on its own: the dock draws the fallback noting “It could not be drawn”, without the error, and the rest of the chat stays.
Docks from before widget parts skip them; rebuild the app on a release that has them.
Reading the backend from a widget
Section titled “Reading the backend from a widget”A widget about the app’s own data uses the usual hooks (useRecord, useList, useUpdateRecord, useClient). A widget about the backend’s own data, such as a hosted backend’s tasks, uses useBackendData from @protobase/ui:
const Task = ({ taskId }: { taskId: string }) => { const path = `/api/assistant/tasks/${encodeURIComponent(taskId)}` const task = useBackendData<{ title: string; status: string }>(path, { refreshMs: (data) => (data?.status === 'running' ? 5000 : false) }) if (!task.data) return <ActionCard title="Task" note={task.error ? 'Could not load the task' : 'Loading'} /> return <ActionCard title={task.data.title} badge={task.data.status} note={task.error?.message} actions={[{ id: 'decline', label: 'Decline' }]} onAction={(action) => void task.post(`${path}/actions`, { action })} />}It returns { data, error, reload, post(path, body) }. It reads path when the widget mounts, and again every refreshMs while that is a number; post sends JSON and its answer replaces data. A failed read or post sets error, an ApiError carrying the backend’s problem detail, and keeps the last data. The path resolves against the backend’s URL from /meta and goes through the dock’s client (AssistantClient.fetch), with the same bearer token as the chat; a path on any other origin is refused before a request is made, so props a model wrote cannot send the user’s token elsewhere. It works only in a widget the dock draws.
Breaking change: AssistantPart now includes AssistantWidgetPart, so code that switches over part types must handle widget, for instance by drawing its fallback.
In the UI
Section titled “In the UI”The dock is AssistantDock from @protobase/ui, built from primitives that custom components and shell slots can use too: SidePanel, ChatThread, ChatMessage, Composer, ActionCard, FieldList, StepList, DiffView and CompactTable. ActionCard takes frameless to draw its content, note, buttons, hints and confirm dialog without the bordered card and title row, for a page that is already framed; the title still labels the section for screen readers. useApiToken() returns the signed-in user’s API token, renewed before it expires, for calling another service as them.