Chat

Your app’s people chat with a declared agent: threads, each turn run as that person and held to their rows, streamed over SSE.

Let the people who use your app talk to a declared agent. Each person has their own threads; each message they send is one turn of the agent, run as that person and held to their rows. What the agent changes goes through the agent's merge policy like any run, so a turn that deletes or changes too much waits for a reviewer, and the person's reply says so.

Make an agent you can chat with

Add chat: to the agent's file:

Markdown
---
name: notes_helper
model: claude-haiku-5-5
tools:
  - sql: { read: [notes, tags], write: [notes] }
chat:
  owner_column: user_id     # the column holding a row's owner (default user_id)
  history: 20               # earlier messages each turn sends the model (default 20, at most 200)
---
You help a person keep their notes tidy. ...

chat: true takes both defaults. An agent people chat with answers in text, so chat: and output.schema can't be used together. Deploy it as any declared agent (forkbase deploy notes_helper.md).

What a turn may see and do

Each turn is a job of the agent's, in a run of its own:

  • Held to the person's rows. In its world the run reads and changes only rows whose owner_column holds the person's user id, in every table that has that column. A table without the column reads whole, so give the agent only tables that are either owned (have the column) or shared on purpose (a price list, tags).
  • Judged as the person. The run acts as the person (role authenticated, their claims, so auth.uid() is theirs): its merge holds only if they could make those writes under your row-level security and grants. See Acting as a person.
  • Under the agent's policy. The run belongs to the agent's identity; its policy decides what merges at once and what is held for review.
  • Their token is never kept. The run keeps the claims the request carried (or, with the service key, the user_id given), not the access token.

The model reads the thread so far: the person's messages and the agent's replies that stood (done or held), the last history of them.

The API: /chat/v1

Every call is either a person (their access token as Authorization: Bearer, with the anon key as apikey) or your server speaking for one (the service key, with user_id: in the body, or ?user_id= on GET and DELETE). A person can't pass another's user_id. The anon key alone is refused.

MethodPath
POST/threads{agent, title?}: a new thread (201). 400 if the agent has no chat:
GET/threads?agent=the person's threads, latest first
GET/threads/{id}the thread and its messages
DELETE/threads/{id}the thread and its messages (204); its runs and jobs stay, as history
POST/threads/{id}/messages{content}: sends a message (see below)
GET/threads/{id}/messages/{reply}/streama reply's turn as a stream again (after a dropped connection)

Another person's thread answers 404. A thread whose last reply is still working refuses a new message (409 busy).

Sending a message

Without streaming, POST /threads/{id}/messages answers 202 with {message, reply}: the person's message, and the agent's reply, working. Read the reply from the thread later, or listen on realtime (below).

With Accept: text/event-stream (or ?stream=true), the same call answers with the turn as server-sent events:

EventData
message{message, reply}, first
text{step, text}: what the model said at a step (a step's whole text, not token by token)
tool{step, name, is_error}: a tool it called
donethe reply as the turn left it, last
error{message}, if the stream can't go on

A stream follows a turn for up to 10 minutes, then sends done with the reply as it is then.

A reply's status

Status
workingits job runs
doneit answered, and its changes merged (or it changed nothing)
heldit answered; its changes wait for a reviewer (run_id is the held run). Turns done, rejected or expired once decided
rejecteda reviewer rejected its changes
expiredits run ended undecided
failedits job failed, or its changes conflicted (error says why)

Each change of a reply's status is broadcast on the realtime topic thread:<thread id>, event message, with {id, thread_id, status} only (never the text): read the thread to get the rest.

JavaScript
supabase.channel(`thread:${threadId}`).on("broadcast", { event: "message" }, () => reload()).subscribe();

From the SDKs

TypeScript
import { createClient } from "@chronosdb/forkbase";

// in the browser, as the signed-in person
const fb = createClient(url, anonKey);
const t = await fb.chat.createThread("notes_helper", { token: session.access_token });
const reply = await fb.chat.send(t.id, "Archive my notes from last year", {
  token: session.access_token,
  onEvent: (name, data) => name === "text" && show(data.text),
});
// reply.status: "done", "held", "failed", ...

// on your server, for one of your people
const server = createClient(url, serviceKey);
await server.chat.send(threadId, "What changed today?", { userId });
Python
fb = create_client(url, service_key)
t = fb.chat.create_thread("notes_helper", user_id=user_id)
reply = fb.chat.send(t["id"], "What changed today?", user_id=user_id, on_event=lambda n, d: print(n, d))

threads, thread, deleteThread (delete_thread) take the same token or userId (user_id).

In the dashboard

Agents ▸ Chat talks to an agent as one of your people (by their user id), through the service key: a way to try an agent before your app does.

Something wrong or missing? Edit this page on GitHub or open an issue.

Chat · Forkbase