App tools
Let the agent change your app's data through your own code.
What it is
appTools is an optional prop on embedded chat. Each tool is a function in your app that the
agent may call, such as “add a todo” or “mark these todos as done”. The tool runs in the browser, with the access your
app already has, so the agent changes your data through the same code as your UI. The agent gets no new permissions.
Import
import { EmbeddedChat, type AppTools } from "@manta-eu/react/components/embedded-chat";
Example
import { useMemo } from "react";
function useTodoTools(todos: TodoActions): AppTools {
return useMemo(
() => ({
addTodos: {
description: "Add todos to the list, one per title.",
parameters: {
type: "object",
properties: { titles: { type: "array", items: { type: "string" } } },
required: ["titles"],
},
execute: ({ titles }: { titles: string[] }) => {
for (const title of titles) todos.add(title);
return { added: titles.length };
},
},
}),
[todos],
);
}
export function TodoCopilot({ todos }: { todos: TodoActions }) {
return (
<EmbeddedChat
user={{ id: "…", email: "you@example.com", name: "You" }}
isOperator={false}
appTools={useTodoTools(todos)}
/>
);
}
Request
Pass an object whose keys are the tool names. Each tool has:
description— what the tool does. The agent reads it to decide when to call the tool.parameters— a JSON Schema for the tool’s input.execute— the function that runs the tool. It gets the input the agent wrote, and may return a promise. The browser does not check that input againstparameters, so check it as you would a form’s.
Build the object with useMemo, as in the example, so the chat gets the same tools on every render.
A tool name is 1 to 64 letters, digits, underscores or dashes. The chat throws an error for any other name, and for
requestUserInput, which is the agent’s own tool. Don’t give an app tool the name of any other tool the agent has:
the chat runs your execute whenever the agent calls a tool with that name.
The chat sends each tool’s name, description and schema with every message. The functions stay in the browser.
Response
When the agent calls a tool, the chat runs execute and gives its return value back to the agent, which then
continues its answer. If execute throws or rejects, the agent gets { error: "<the error message>" } as the output
instead, continues its answer, and can tell the user or try again.
A tool runs only while the chat is open in the browser. Keep each tool to a single change your UI could also make, and make it safe to run twice: if the tab closes before the output reaches the agent, the agent may call it again.
Returning files
A tool can return a file for the agent to look at, such as a screenshot of what it just changed. Put a Blob or a
File anywhere in the plain objects and arrays execute returns:
const screenshot: AppTool<{ part: string }> = {
description: "Take a picture of the stage as it looks now, to check a change. Call it after each change.",
parameters: { type: "object", properties: { part: { type: "string" } }, required: ["part"] },
execute: async ({ part }) => ({ part, picture: await stageAsPng() }),
};
The chat uploads each file to the datastore, without asset analysis, and gives the agent a reference in its place:
{ type: "manta-file", fileIri, mediaType, fileName }. The agent sees the file’s contents in its next step, as if it
had read it with readUploadedFile, and can read it again by that IRI in a later message.
- Uploading needs a person with a Manta account, signed in to Manta or through a custom app. A custom app user’s or a
visitor’s chat cannot upload, so the agent gets the tool’s
{ error }instead. - Each file is kept in the datastore like any other upload. Everyone with access to the datastore can see it on its Uploads page. A tool runs without asking the person first, so return only what they would show their team.
- The agent reads a picture’s text the way it reads a message, and that text can steer what it does next. Don’t return pictures of content you don’t control, such as ads, reviews or emails.
- The agent is shown at most eight files, 20 MiB together, each time it picks up after your tools answer. A file past
that, or one the model cannot read, reaches the agent as a note saying so. A tool that returns more than eight files
gets an
{ error }instead, and nothing is uploaded. - A file that fails to upload is replaced with
{ error }. The files that uploaded still reach the agent. - A csv, parquet, ttl or nt file is refused with an
{ error }, because the datastore would load it into the knowledge graph. Return its text instead. - The file’s type decides how the agent sees it. Give each
Blobatype; a file name without a matching extension gets one. - A file inside a
Map, aSetor a class instance is not found, and reaches the agent as{}. - A file returned in several places in one output is uploaded once. Each call uploads its files again, so return only the files the agent needs to see.