> ## 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.

# Turns from your code

> Ask an agent from your own mutation with send, continue on the turn's result, and run turns on a schedule.

`send` from `@bijectionhq/app-agents` asks an agent in your own commit. It
submits the installation's turn target, `sent`, atomically with your writes,
and returns the `Task`. The task's result is a `TurnResult`, answered when the
turn completes, fails or is stopped.

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { mutation, t, table } from "@bijectionhq/bijection";
import { send } from "@bijectionhq/app-agents";
import { clerk, desk } from "./access";
import { support } from "./agents";

export const tickets = table({ subject: t.string, reply: t.maybe(t.string) }, { label: desk });

export const triage = mutation(
  { under: desk, allow: [clerk], args: { id: t.key(tickets) }, refuses: { NoTicket: null } },
  async (ctx, { id }) => {
    const ticket = await ctx.get(tickets(id));
    if (ticket === null) return ctx.refuse("NoTicket", "No such ticket");
    await send(ctx, support, { agent: "support", participant: ctx.frame.as.id, text: ticket.subject }, {
      once: ["ticket", id],
      within: { minutes: 10 },
      then: replied.with({ id }),
    });
    return null;
  },
);

export const replied = mutation(
  { under: desk, args: { id: t.key(tickets), result: t.result(support.sent) } },
  async (ctx, { id, result }) => {
    const reply = result.ok && result.value.status === "completed" ? result.value.text : null;
    await ctx.patch(tickets(id), { reply });
    return null;
  },
);
```

`send` runs under the
transcript label, so call it from a mutation under that label.

| Argument | Meaning |
| - | - |
| `agent` | The agent's name in the settings. |
| `participant` | The principal you vouch for. It owns the conversation. |
| `text` | The message. |
| `conversation` | A conversation key to post in. |
| `thread` | Your own key for an open conversation: the first message opens it, and later messages with the same `participant` and `thread` post in it. |
| `title` | A title for a newly opened conversation. |
| `once` | The effect's identity. The same `once` and args return the original task. |
| `within` | How long you wait for the result, at most a day. Past it the task settles `Expired`. |
| `then` | The continuation that receives `result: t.result(support.sent)`. |

`TurnResult` is `{ status, text, conversation, turn, error }`, with `status`
one of `completed`, `failed` and `stopped`. `conversation` is `null` when
the message was refused before a conversation was named or opened, and
`turn` is `null` when no turn was admitted.

## On a schedule

A reaction runs on a standing source. It declares the roles the installation
holds while it runs:

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { every, mutation, type PrincipalId } from "@bijectionhq/bijection";

/** The desk lead the daily digest is written for. */
const owner: PrincipalId = "person:example|lead";

export const digest = mutation(
  { under: desk, on: every({ hours: 24 }), roles: [clerk] },
  async (ctx) => {
    const day = ctx.frame.now.entry.toString().slice(0, 10);
    await send(ctx, support, { agent: "support", participant: owner, text: "Summarize today's open tickets.", thread: "daily" }, {
      once: ["digest", day],
      within: { hours: 1 },
    });
    return null;
  },
);
```

## Usage

Each model call reserves `MODEL_RESERVE` (16,000) tokens on the model
receiver's meter, and settles with the count the provider reports. A turn's
`tokens` field totals that usage. `maxRounds` bounds the model calls of one
turn. A lost reply from either receiver settles as `Unknown`, which
is final, and fails the turn rather than paying twice.


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