> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bijection.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Conversations

> The Agents app's exposed conversation and turn functions, and how a client calls them.

A person holding the agents app's `participant` role chats through the
conversation functions. Each one is exposed at its path under the place you
mounted the app, and acts only on the caller's own conversations: a
conversation's owner is the principal that opened it.

| Function | Kind | What |
| - | - | - |
| `conversations.agents({})` | query | The configured agents as a chat offers them: `name`, `starters`, `placeholder`. Never instructions, model or tools. |
| `conversations.open({ agent, title })` | mutation | Open a conversation bound to one agent. Returns its key. Refuses `UnknownAgent`. |
| `conversations.list({})` | query | The caller's conversations, newest first (at most 50). |
| `conversations.get({ id })` | query | One of the caller's conversations, or `null`. |
| `conversations.messages({ id, size, cursor })` | query | A page of messages, newest first: `{ facts, next }`. Pass `next` as `cursor` for the page before. |
| `conversations.post({ id, text })` | mutation | Start a turn now, or queue it behind the running one (at most eight wait). Returns the turn's key. |
| `conversations.close({ id })` | mutation | Stop its queued and running turns. It takes no more messages. |
| `conversations.delete({ id })` | mutation | Close it, then delete its messages, turns and fact, a page at a time. |
| `turns.get({ id })` | query | A turn's `state`, `rounds`, `answer` message, `error`, `tokens`, `startedAt` and `settledAt`. |
| `turns.stop({ id })` | mutation | Stop a turn. The conversation's next queued turn starts. |

A conversation fact carries `id`, `agent`, `title`, `state`, `running` (the
active turn) and `createdAt`. A message carries `id`, `position`, `role`
(`user`, `assistant` or `tool`), `text`, `tools`, `turn`, `by` and `at`.

Every function refuses with a host code or one of the app's codes:
`NotFound`, `UnknownAgent`, `SignedOut`, `Unadmitted` and `InvalidInput`. A
caller without the participant role is refused `Disallowed`.

## From React

```tsx theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { useState } from "react";
import type { Agents } from "@bijectionhq/app-agents";
import type { KeyOf } from "@bijectionhq/bijection";
import { api } from "@bijectionhq/bijection/client";
import { useMutation, usePages, useWatch } from "@bijectionhq/bijection/react";

const { conversations, turns } = api<{ agents: Agents }>().agents;
type ConversationKey = KeyOf<Agents["store"]["conversations"]>;
type TurnKey = KeyOf<Agents["store"]["turns"]>;

export function Chat() {
  const [id, setId] = useState<ConversationKey | null>(null);
  const [turn, setTurn] = useState<TurnKey | null>(null);
  const open = useMutation(conversations.open);
  const post = useMutation(conversations.post);
  const current = useWatch(turns.get, turn === null ? null : { id: turn });

  async function send(text: string) {
    let conversation = id;
    if (conversation === null) {
      const opened = await open({ agent: "support", title: null });
      if (!opened.ok) return;
      conversation = opened.value;
      setId(conversation);
    }
    const posted = await post({ id: conversation, text });
    if (posted.ok) setTurn(posted.value);
  }

  // current?.ok && current.value?.state: "queued" | "running" | "awaiting" | "completed" | "failed" | "stopped"
  return id === null ? <Composer onSend={send} /> : <Thread id={id} onSend={send} />;
}

function Thread({ id, onSend }: { id: ConversationKey; onSend: (text: string) => void }) {
  const messages = usePages(conversations.messages, { id }, { size: 50 });
  return (
    <>
      {messages.continuation?.kind === "more" ? <button onClick={messages.loadMore}>Earlier messages</button> : null}
      <ol>{[...messages.facts].reverse().map((m) => <li key={m.id} data-role={m.role}>{m.text}</li>)}</ol>
      <Composer onSend={onSend} />
    </>
  );
}
```

To retry a post after `Unknown`, call the mutation with the original
arguments and the refusal's `once` to recover the original turn.
`useWatch` with `null` arguments holds no watch.

## Turn states

| `state` | Meaning |
| - | - |
| `queued` | Waiting behind the conversation's running turn. |
| `running` | A model call or tool calls are outstanding. |
| `awaiting` | A question to a person is outstanding. |
| `completed` | The model replied with no tool calls. |
| `failed` | A model call did not succeed, the turn reached `maxRounds`, or its agent is unknown. `error` says why. |
| `stopped` | `turns.stop` or `conversations.close` ended it. |

A stopped turn's outstanding model call, tool calls and questions run to
their own results, which the app ignores. `tokens` totals the usage the
provider reported for the turn's model calls.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.