Chapter 16 of 18

The app's own assistant

Chapter 10 let an agent outside the app call its commands. Freddy's colleagues will not set up an MCP client; they want to type "pin my notes about trains" into the app itself. The app can have its own assistant, and it can do no more than the person asking.

Turn it on

App::new()
    .commands(resources::commands())
    .backend(Backend::new("https://notes.example.com").abilities().assistant())
    .assistant(
        Assistant::new()
            .allow_abilities(["notes.*", "team_notes.*"])
            .confirm("notes.delete")
            .instructions("You help Freddy's team find, write and tidy their notes."),
    )
<script>
  import { openAssistant } from "@elyra/runtime";
</script>

<Button onclick={() => openAssistant()}>Ask</Button>

The assistant's tools are the app's own commands, the ones whose #[command(can = "…")] its grant covers. The grant is its own, apart from the page's and from an MCP agent's, and a command without can is never a tool. Each tool is described as chapter 10's MCP server describes it, so the /// doc comment is what the model reads; make:resource writes them for this reader. A ⌘K query that matches no command offers Ask the assistant too.

Where the model is

Chapter 9 said a provider key does not belong in a desktop binary. With Backend::assistant() the app reaches Claude through Freddy's Laravel app, with the user's Sanctum token; Laravel adds its own key and decides what may be asked. On the server it is one route from elyracode/elyra-backend (chapter 12 says how to add the package), and a key in its .env:

// routes/api.php, inside auth:sanctum
Route::elyraAssistant();      // POST /api/elyra/assistant/v1/messages
ANTHROPIC_API_KEY=sk-ant-…
ELYRA_ASSISTANT_PER_MINUTE=30      # optional, per user

Laravel answers only for the models it allows, caps max_tokens, and refuses Claude's own server tools; the tool loop stays on the desktop, because the tools are the app's commands. Signed out, the panel says "Sign in to use the assistant." Without a backend, the assistant uses the user's own key from the keychain, which the panel asks for, or ANTHROPIC_API_KEY in the environment while Freddy develops.

What may run

A call runs only when every one of these holds: the assistant's grant covers the command's ability; with chapter 12's .abilities(), the signed-in user has it, checked again at each call; the tool's rate limit, 60 calls a minute by default, is not spent; and, for an ability confirm names, the user accepted this call. That last one is a themed dialog in the window with the command's description and arguments, Allow or Don't. The model has no tool for it, so it cannot answer for the user.

A refused call is told to the model in words it can act on: the validation errors per field, "The user may not do this", "The user didn't confirm". Commands run through the middleware with Origin::Assistant, which is how chapter 4's audit line can say who did it. Freddy checks what asks first:

rata assistant inspect

What it sees of the window

Freddy asks from a note: "pin this one". Each turn carries a short note of the window it was asked from, before his words: the route, and the window's live queries by command and arguments, the ones the user may run:

<window>
{"route":"/notes/12","showing":[{"command":"notes_show","arguments":{"id":12}}]}
</window>

Pin this one.

Names, not data: the rows the window shows are never in it, and to read one the model calls the command, held to the user's abilities like any call. A <Table select="notes"> also says what's selected, so "tag these as trains" knows which. Nothing in the app had to change for this: a screen that runs live.notes_show(id) already says which note it is on.

And the answer comes back with links. Chapter 6's link on the generated commands turns each step in the panel into a link to the note it touched: "notes update ✓" opens #/notes/12. The answer itself is written into the panel as Claude writes it, through the tool steps, and stopping it stops the model.

What to ask

Before the first message, the panel suggests what to ask: the app's Prompts, which the app's MCP server from chapter 10 offers too. A suggestion shows only when the user may do everything it needs:

let tidy = Prompt::new("tidy_notes", "Tidy my notes",
        "Find my notes without a notebook and suggest one for each.")
    .needs("notes.update");

Assistant::new().allow_abilities(["notes.*"]).prompts([&tidy])

Saying it instead of typing it

On the train Freddy would rather speak. The microphone feature (with ai, which transcription needs) records in the shell, not the page, which a webview could not do on every platform, and Laravel turns the recording into text with its own key, as it did for the assistant:

App::new()
    .microphone("Freddy Notes uses the microphone to let you dictate.")
    .backend(Backend::new(url).abilities().assistant().transcription())
// routes/api.php, inside auth:sanctum
Route::elyraTranscription();     // OPENAI_API_KEY in .env
<Microphone ontext={(text) => (body += ` ${text}`)} />

<Microphone> is a click to start and stop, or held down with the mouse, a finger or Space for push-to-talk. The sentence why is what macOS shows when it asks the user, and rata bundle writes it into the .app. One honest note from the guide: macOS does not refuse an app it won't let listen, it hands it silence, so a recording of exact silence is an error that says where to allow the microphone. While developing with cargo run, the terminal is the app macOS asks about.

The guides are The app's assistant, the assistant section of A Laravel backend, and The microphone.

What you learned

  • The assistant's tools are the app's commands its own grant covers, described by their doc comments.
  • Through Backend::assistant() the key stays on the server, and Laravel decides what may be asked.
  • A call needs the grant, the user's ability, the rate limit and, for what can't be undone, the user's own yes.
  • Each turn says which window it was asked from, by names, not data, and the answer links to what it touched.
  • <Microphone> records in the shell, and Laravel turns it into text.
Next: in Chapter 17 Freddy's colleagues ask for what a browser tab can't do.