Panels and the aistudio bridge
A panel is an HTML page in a side panel next to the chat. The user can dock it left or right, float it, widen it or expand it. Your page talks to AI Studio only through the aistudio global.
The panel
"pane": { "title": "Tickets", "entry": "pane/index.html", "side": "right" }
<link rel="stylesheet" href="/aistudio/plugin.css">
<script src="/aistudio/plugin.js"></script>
The page runs in a sandboxed iframe on its own origin (http://<id with dots as -->.localhost:<port>): scripts, forms, downloads and its own localStorage work; it cannot open popups or navigate the app. Use relative paths for your own files.
Call await aistudio.ready() first. It resolves with { api, theme, project, connected } and applies the user's theme to the page.
Looking like AI Studio
plugin.css sets the app's colours as CSS variables and follows the user's theme and dark mode: --text, --muted, --panel, --line, --field, --accent, --accent-text, --tool, --ok, --font. It styles .btn (.primary, .small), inputs, label, .list / .row, .note and .empty, and makes [hidden] always hide. Use it, or only the variables, and the panel fits every theme.
The bridge
Every call returns a promise. Errors are Errors with a code: permission, auth, project (no project open), args, size, off.
| Call | Needs | What it does |
|---|---|---|
ready() | Handshake; { api, theme, project, connected }. | |
project.get() | { name, root } of the open project, or null. | |
project.listFiles(glob?) | project:read | The project's files (paths relative to its root). |
project.readFile(path) | project:read | A text file, up to 1 MB. |
chat.draft(text) | chat:send | Puts text in the chat box; the user sends it. |
chat.attach({ name, blob }) | chat:send | Adds an image or PDF to the chat box. |
chat.send(text, opts?) | chat:send | Sends a message. opts.mode: a chat mode key. opts.attachments: [{ name, blob }], images or PDFs, 20 MB each. opts.label: a short action the chat shows instead of the message, like 'Redesign the screens', with your plugin's name; the AI still gets the whole message (not with attachments). opts.wait: resolve when the AI is done, with { chatId, reply, changedFiles }. The promise's .started resolves with { chatId } once the message is in the chat. |
mcp.tools(), mcp.call(tool, args) | mcp:call | The plugin's own MCP tools. |
preview.screenshot({ scale }) | preview:read | A PNG Blob of the app in the preview. scale (optional, API 1.2): the page's own size times 1 to 4, by default 2; 3 gives a phone's real sharpness (a 390 point wide phone comes out 1170 wide). |
preview.navigate(path) | preview:control | Opens a page of the app. |
assets.add({ name, blob, request? }) | assets:write | Adds a file to the project's Assets (200 MB max). |
storage.get(key), storage.set(key, value) | JSON values for this plugin in this project (5 MB in all), in .aistudio/plugins/<id>.json. | |
fetch(url, init) | auth.relay | A request to one of your hosts with the user's token added. See Sign-in. |
connection(), connect(), auth.renew(params) | Sign-in state, start the sign-in, renew the token with extra fields. See Sign-in. | |
ui.badge(n) | A number on the panel's tab (0 clears it). | |
ui.toast(text) | A short message in the app. | |
ui.show() | Opens the panel. | |
ui.undock({ width, height }), ui.dock() | Floats the panel at that size, or docks it back. | |
ui.openExternal(url) | Opens an https link in the browser. | |
tools.handle(name, fn) | panel:tools | Answers the AI's calls of a tool in panelTools. Returns a function that stops answering. See Panel tools. |
log(...values) | Writes to the plugin's Log. Uncaught errors go there too. |
Events
const off = aistudio.on('chat:turn-end', ({ chatId, changedFiles }) => refresh());
| Event | Data |
|---|---|
visible | { visible }: the panel was opened or closed. |
project | The new project ({ name, root }) or null. |
theme | Already applied to the page; listen to redraw a canvas. |
connection | { connected }, after a sign-in or sign-out. |
chat:turn-start | { chatId, text, mode }. |
chat:turn-end | { chatId, changedFiles }. |
publish | With "hooks": { "publish": true }: { commit, branch, message, pushed, files, chatIds } after the user publishes. |
Panel tools: the AI using your panel
An MCP server gives the AI tools that run on their own. Panel tools run in your panel's page, so the AI can read and change what the user sees there: the shapes on a board, the rows of a table, the fields of a form. You list them in plugin.json and answer them in the page.
"aistudio": "^1.1",
"pane": { "title": "Board", "entry": "pane/index.html", "side": "right" },
"permissions": ["panel:tools"],
"panelTools": [
{
"name": "board_look",
"description": "See the board in the Board panel: every note with its id and text, and a picture of it.",
"inputSchema": { "type": "object", "properties": {} }
},
{
"name": "board_add_note",
"description": "Add a sticky note to the Board panel. Returns its id.",
"inputSchema": { "type": "object", "properties": { "text": { "type": "string" }, "x": { "type": "number" }, "y": { "type": "number" } }, "required": ["text"] }
}
]
aistudio.tools.handle('board_look', async () => [
JSON.stringify({ notes }), // text for the AI
await boardPicture(), // a PNG Blob: the AI sees it
]);
aistudio.tools.handle('board_add_note', ({ text, x = 0, y = 0 }) => {
if (!text.trim()) throw new Error('text is empty.'); // the AI is told it failed, and why
const note = addNote({ text, x, y }); // change the page like the user would
return { id: note.id }; // JSON for the AI
});
await aistudio.ready();
- Names and descriptions.
name: lowercase letters, digits and_, 40 characters at most; start it with your plugin's name (board_...) so the AI and the chat tell your tools apart.description(up to 2000 characters) is all the AI knows about the tool: say what it does, which panel it acts in, and the units of any position.inputSchemais a JSON schema of"type": "object". - What a handler returns is what the AI gets: a string as text, any other value as JSON, a
Blobas a picture (PNG, JPEG, GIF or WebP, 5 MB at most), or a list mixing text and pictures (20 parts at most). Throw anErrorto tell the AI the call failed, with your message. - When they run. The AI has your tools during each chat message while the plugin is on and the user accepted
panel:tools. On each call AI Studio opens your panel (the user watches what the AI does), waits forready(), and gives the call to your handler. Register handlers before callingready(). An answer must come within 2 minutes. - The user stays in charge. Make every change the AI makes one the user can undo, the same way as their own, and show it in the page. The chat shows each call as "Plugin name: tool".
- Older AI Studio. Panel tools need plugin API 1.1. With
"aistudio": "^1.1", an AI Studio that does not have it waits to update the plugin until it is updated itself.
Example: capture, then ask
await aistudio.ready();
document.querySelector('#fix').onclick = async () => {
const shot = await aistudio.preview.screenshot(); // preview:read
const done = await aistudio.chat.send('Make this screen match our brand colours.', {
attachments: [{ name: 'screen.png', blob: shot }], // chat:send
wait: true,
});
aistudio.ui.toast(`${done.changedFiles.length} files changed`);
};