Shared Bridge API
All views use window.AstrBotPluginView. The host injects the SDK automatically. Use an external type="module" script and await bridge.ready() before calling its methods.
Capabilities
| API | Standalone page | Message component / message-created workbench | Resident workbench |
|---|---|---|---|
ready, getContext, onContext | Supported | Supported | Supported |
| Theme CSS tokens and localization | Supported | Supported | Supported |
view.subscribe, view.action | — | Supported | — |
apiGet, apiPost, file transfers, SSE | Supported | — | Supported |
chat.fill, chat.send | — | — | Supported |
Unsupported operations return UNSUPPORTED_CAPABILITY. AstrBotPluginPage remains a compatibility alias; use AstrBotPluginView in new code.
TypeScript declarations
Download astrbot-plugin-view.d.ts into your frontend project's src/ directory, or another path covered by tsconfig.json's include. It adds types for window.AstrBotPluginView; the host still injects the runtime SDK, so no JavaScript package import is needed.
These are all public method signatures. The following sections define their data structures. Every method exists on the bridge object, but availability depends on host capabilities. __setInitialContext is internal to the host, not a plugin API.
export interface AstrBotPluginViewBridge<TViewData = JsonObject> {
ready(): Promise<BridgeContext<TViewData>>;
getContext(): BridgeContext<TViewData> | null;
getLocale(): string;
getI18n(): I18nResources;
t(key: string, fallback?: string): string;
onContext(handler: (context: BridgeContext<TViewData>) => void): Unsubscribe;
view: {
subscribe(
handler: (snapshot: ViewSnapshot<TViewData>) => void,
): Unsubscribe;
action(actionId: string, data?: JsonValue): Promise<void>;
};
chat: {
fill(text: string): Promise<ChatResult>;
send(text: string): Promise<ChatResult>;
};
apiGet<TResult = unknown>(
endpoint: string,
params?: QueryParams,
): Promise<TResult>;
apiPost<TResult = unknown>(
endpoint: string,
body?: JsonValue,
): Promise<TResult>;
upload<TResult = unknown>(
endpoint: string,
file: UploadFile,
): Promise<TResult>;
download(
endpoint: string,
params?: QueryParams,
filename?: string,
): Promise<DownloadResult>;
subscribeSSE<TData = unknown>(
endpoint: string,
handlers?: SSEHandlers<TData>,
params?: SSEParams,
): Promise<string>;
unsubscribeSSE(subscriptionId: string): Promise<UnsubscribeSSEResult>;
}Base types
export type JsonValue =
| string
| number
| boolean
| null
| JsonValue[]
| { [key: string]: JsonValue };
export type JsonObject = { [key: string]: JsonValue };
export type Unsubscribe = () => void;
export type QueryParams = Record<string, JsonValue | undefined>;
export type SSEParams = Record<
string,
string | number | boolean | null | undefined
>;
export type I18nResources = Record<string, JsonObject>;Initialize the bridge
The View iframe cannot directly access Dashboard cookies, LocalStorage, or the parent DOM. View scripts must use window.AstrBotPluginView to call backend APIs and read context.
const bridge = window.AstrBotPluginView;In TypeScript, specify your snapshot and business-response types:
import type { AstrBotPluginViewBridge } from "./astrbot-plugin-view";
type CounterData = { count: number };
const bridge = window.AstrBotPluginView as AstrBotPluginViewBridge<CounterData>;
const context = await bridge.ready();
if (context.view) {
bridge.view.subscribe(({ data }) => console.log(data.count));
} else {
const result = await bridge.apiGet<{ message: string }>("ping");
console.log(result.message);
}Context
ready() waits for the parent page to send the initial context and returns Promise<BridgeContext>. Wait for it during page initialization.
const context = await bridge.ready();export interface ThemeColors {
primary?: string;
secondary?: string;
"on-primary"?: string;
primaryText?: string;
"on-surface"?: string;
secondaryText?: string;
background?: string;
surface?: string;
border?: string;
error?: string;
[name: string]: string | undefined;
}
export interface BaseContext {
pluginName: string;
displayName: string;
pageName: string;
pageTitle: string;
locale: string;
i18n: I18nResources;
isDark: boolean;
themeColors: ThemeColors;
}
export interface ViewSnapshot<TData = JsonObject> {
id: string;
revision: number;
data: TData;
}
export interface StandalonePageContext extends BaseContext {
target: "plugin_page";
sessionId: null;
view?: never;
}
export interface MessageViewContext<TData = JsonObject> extends BaseContext {
target: "chat_message" | "chat_workbench" | "chat_workbench_with_launcher";
view: ViewSnapshot<TData>;
sessionId?: never;
}
export interface ResidentWorkbenchContext extends BaseContext {
target: "chat_workbench_with_launcher";
sessionId: string | null;
view?: never;
}
export type BridgeContext<TData = JsonObject> =
| StandalonePageContext
| MessageViewContext<TData>
| ResidentWorkbenchContext;Use both target and view to identify the host:
| Host | target | view | sessionId |
|---|---|---|---|
| Standalone page | plugin_page | Absent | null |
| Message component | chat_message | ViewSnapshot | Absent |
| Message-created workbench | chat_workbench / chat_workbench_with_launcher | ViewSnapshot | Absent |
| Resident workbench | chat_workbench_with_launcher | Absent | Active session ID, or null without a session |
view contains only id, revision, and data, not the name or version fields of Python's UIInteraction.view. Read the registered view name from context.pageName. i18n is keyed by locale, for example { "en-US": { "views": { "counter": { "title": "Counter" } } } }. Additional themeColors keys come from the host theme; prefer the CSS tokens listed below for styling.
Context APIs:
| API | Returns | Description |
|---|---|---|
ready() | Promise<BridgeContext> | Waits until the bridge is ready and returns the current context |
getContext() | BridgeContext | null | Synchronously reads the latest context |
getLocale() | string | Current WebUI locale, defaulting to zh-CN |
getI18n() | I18nResources | Current plugin i18n resources |
t(key, fallback) | string | Reads a dot-separated translation key, returning fallback when missing |
onContext(handler) | () => void | Listens for context changes and returns an unsubscribe function |
onContext() invokes the handler immediately when context is available, then on every context update. Its unsubscribe function removes only that subscription. getContext() returns null before initialization. t() searches the current locale, then zh-CN, then en-US; if no value exists, it returns fallback or an empty string.
Respond to locale or theme changes:
function render() {
document.title = bridge.t("views.bridge-demo.title", "Bridge Demo");
document.getElementById("locale").textContent = bridge.getLocale();
}
await bridge.ready();
render();
const off = bridge.onContext(render);
window.addEventListener("beforeunload", off);Plugin Web APIs
Request and return rules
The endpoint used by apiGet, apiPost, upload, download, and subscribeSSE is a plugin-local relative path, such as stats, settings/save, or files/export. Prefer not to start it with /; the current bridge strips leading / for compatibility.
endpoint must not be empty, contain \, contain a URL scheme, contain query strings or fragments, or contain empty, ., or .. path segments.
Do not append query strings to endpoint:
await bridge.apiGet("stats", { limit: 20 });Bridge JSON calls use this compatibility rule:
- For successful responses, a non-null
datafield is unwrapped, regardless of whetherstatusis"ok". For example,{ "data": 0 }resolves to0. - If
datais absent or null, the complete response is returned. Both{ "message": "pong" }and{ "data": null }are returned unchanged. - If the backend returns
{ "status": "error", "message": "..." }, or the HTTP request fails, the Promise rejects withError.
For View-only APIs, prefer returning plain business JSON:
return json_response({"message": "pong"})Use this for errors:
return error_response("missing file", status_code=400)Handle errors on the View:
try {
await bridge.apiPost("settings/save", { enabled: true });
} catch (error) {
console.error(error.message);
}apiGet(endpoint, params)
apiGet<TResult = unknown>(endpoint: string, params?: QueryParams): Promise<TResult>Sends a GET request. params are sent as query parameters.
const stats = await bridge.apiGet("stats", { limit: 20, tag: "today" });Backend:
async def stats(self):
limit = request.query.get("limit", 20, type=int)
tag = request.query.get("tag")
return json_response({"limit": limit, "tag": tag})apiPost(endpoint, body)
apiPost<TResult = unknown>(endpoint: string, body?: JsonValue): Promise<TResult>Sends a POST JSON request.
const result = await bridge.apiPost("settings/save", {
enabled: true,
threshold: 0.8,
});Backend:
async def save_settings(self):
payload = await request.json(default={})
return json_response({"saved": True, "enabled": payload.get("enabled")})upload(endpoint, file)
upload<TResult = unknown>(endpoint: string, file: UploadFile): Promise<TResult>Uploads one file as multipart/form-data. The field name is always file.
const input = document.querySelector("input[type=file]");
const file = input.files[0];
const result = await bridge.upload("files/import", file);Backend:
from astrbot.api.web import PluginUploadFile, error_response, json_response, request
async def import_file(self):
files = await request.files()
upload: PluginUploadFile | None = files.get("file")
if not isinstance(upload, PluginUploadFile):
return error_response("missing file", status_code=400)
return json_response({"filename": upload.filename})If you need extra structured fields, send them through a separate apiPost call or use query parameters to select import behavior. The current upload() bridge method sends one file.
download(endpoint, params, filename)
download(endpoint: string, params?: QueryParams, filename?: string): Promise<DownloadResult>Requests a plugin backend file endpoint and triggers a browser download. params are sent as query parameters. filename is optional; when omitted, the bridge tries to read it from response headers.
await bridge.download("files/export", { format: "json" }, "export.json");Backend:
async def export_file(self):
fmt = request.query.get("format", "json")
return file_response(
export_path,
filename=f"export.{fmt}",
content_type="application/json",
)download() resolves to:
{ "filename": "export.json" }subscribeSSE(endpoint, handlers, params)
subscribeSSE<TData = unknown>(
endpoint: string,
handlers?: SSEHandlers<TData>,
params?: SSEParams,
): Promise<string>export interface SSEMessage<TData = unknown> {
raw: string;
parsed: TData | string;
eventType: string;
lastEventId: string;
}
export interface SSEHandlers<TData = unknown> {
onOpen?: () => void;
onMessage?: (event: SSEMessage<TData>) => void;
onClose?: () => void;
onError?: () => void;
}Subscribes to plugin backend SSE and returns Promise<subscriptionId>. handlers may include onOpen, onMessage, onClose, and onError.
const subscriptionId = await bridge.subscribeSSE(
"events",
{
onOpen() {
console.log("SSE opened");
},
onMessage(event) {
console.log(event.raw, event.parsed, event.lastEventId);
},
onClose() {
console.log("SSE stream ended");
},
onError() {
console.warn("SSE error");
},
},
{ topic: "logs" },
);event.raw is the raw string. If the message is a JSON string, event.parsed is parsed automatically; otherwise it equals the raw string. event.eventType matches the SSE event: field and defaults to message.
onClose is called when the stream ends, and onError is called if reading it fails. Calling unsubscribeSSE triggers neither callback. The bridge does not reconnect automatically; subscribe again if needed. Initial subscription failures reject the returned promise.
The backend must return text/event-stream:
async def events(self):
async def stream():
yield 'data: {"message": "ready"}\n\n'
return stream_response(stream())Unsubscribe:
await bridge.unsubscribeSSE(subscriptionId);Clean up on unload:
window.addEventListener("beforeunload", () => {
bridge.unsubscribeSSE(subscriptionId);
});View actions and chat controls
view.subscribe(handler: (snapshot: ViewSnapshot<TViewData>) => void): Unsubscribe
view.action(actionId: string, data?: JsonValue): Promise<void>
chat.fill(text: string): Promise<ChatResult>
chat.send(text: string): Promise<ChatResult>Call view.subscribe() after ready(). It immediately supplies a copy of the current snapshot and runs again when a snapshot with the same or a higher revision arrives. Unsubscribing removes the listener without closing the view. Notifications do not necessarily indicate a business-data change; mutating the copy does not save data.
The data argument of view.action() defaults to null. Success resolves to undefined, not a snapshot; read updated data through view.subscribe() or getContext().view. The promise settles after the callback and state commit, without waiting for subsequent model generation. Declare each action in views[].actions in metadata.
chat.fill() appends to the draft and focuses the composer. chat.send() sends to the current session while preserving the draft and attachments. Both resolve to { sessionId: string | null }. Sending requires an open session with no running reply, and resolves when the request is accepted rather than when the answer finishes. Text must not be blank or exceed 100,000 UTF-16 code units. See Workbench page for host requirements and examples.
Result and error types
export interface UploadFile {
arrayBuffer(): Promise<ArrayBuffer>;
name?: string;
type?: string;
lastModified?: number;
}
export interface DownloadResult {
filename: string;
}
export interface ChatResult {
sessionId: string | null;
}
export interface UnsubscribeSSEResult {
subscriptionId: string;
}
export interface BridgeError extends Error {
code?: string;
}- Browser
FileandBlobobjects satisfyUploadFile. The default filename isupload.bin; the default MIME type isapplication/octet-stream. unsubscribeSSE(id)returnsPromise<UnsubscribeSSEResult>, resolving to{ subscriptionId: id }even if the connection has already ended.- Generics on
apiGet,apiPost, anduploaddescribe the unwrapped business response and default tounknown. They do not validate server data. - SSE
parsedcontains the JSON value when parsing succeeds, otherwise the original string.eventTypedefaults tomessage.lastEventIdstarts as an empty string and persists across events without anid:field.onOpen,onClose, andonErrorreceive no arguments. apiGet/downloaduse Axios query serialization.subscribeSSEcallsString(value)for each parameter and does not support repeated keys or arrays. Omit optional SSE parameters instead of passingundefinedornull.- With an omitted body,
null,false,0, or an empty string, the currentapiPosthost sends{}. Prefer JSON object request bodies.
Failures are generally Error objects; code is optional. Message views can report codes such as UNSUPPORTED_CAPABILITY, ACTION_IN_PROGRESS, and REVISION_CONFLICT. Host-forwarded plugin Web API and chat-control errors carry only a message, not an HTTP status or additional backend fields.
try {
await window.AstrBotPluginView.view.action("reading_view:set_read", {
book_id: "design",
read: true,
});
} catch (error: unknown) {
if (error instanceof Error) {
const code = (error as BridgeError).code;
console.error(code ?? "BRIDGE_ERROR", error.message);
}
}Import the type with import type { BridgeError } from "./astrbot-plugin-view". view.subscribe() can throw synchronously; request methods reject their promises. A REVISION_CONFLICT synchronizes the latest snapshot before rejecting; do not blindly retry actions with side effects.
Light and dark themes
AstrBot syncs the current theme to Plugin Views. The bridge SDK maintains a data-theme attribute on <html>:
- Light mode:
<html data-theme="light"> - Dark mode:
<html data-theme="dark">
The bridge also synchronizes CSS color-scheme to light or dark, keeping transparent iframe backgrounds consistent with the host theme.
When Follow System is selected, the View still receives either light or dark.
Following user-selected colors
All targets use the same AstrBotPluginView bridge. It injects these CSS tokens on <html> and updates them on initialization, light/dark changes, and palette changes. No manual listener or CSS assignment is needed.
| CSS token | Purpose |
|---|---|
--astrbot-color-primary | Primary |
--astrbot-color-secondary | Secondary |
--astrbot-color-on-primary | Text on primary |
--astrbot-color-text | Body text |
--astrbot-color-text-secondary | Secondary text |
--astrbot-color-background | Page background |
--astrbot-color-surface | Surface background |
--astrbot-color-border | Border |
--astrbot-color-error | Error |
body {
color: var(--astrbot-color-text);
background: var(--astrbot-color-background);
}
button {
color: var(--astrbot-color-on-primary);
background: var(--astrbot-color-primary);
border: 1px solid var(--astrbot-color-border);
}The bridge only injects variables; it does not force a page background. Chat cards can keep background: transparent. Existing themeColors.primary / secondary remain available. All targets use bridge.onContext(({ isDark, themeColors, locale }) => { ... }) when additional JavaScript behavior is needed.
View metadata and localization
Define default view titles and descriptions under views in metadata.yaml. Put translations in .astrbot-plugin/i18n/<locale>.json under views.<name>, alongside the top-level metadata and config keys.
For example, localized metadata for the bridge-demo View is stored as follows:
astrbot_plugin_view_demo/
├─ views/
│ └─ bridge-demo/
│ └─ index.html
└─ .astrbot-plugin/
└─ i18n/
├─ zh-CN.json
└─ en-US.json.astrbot-plugin/i18n/en-US.json:
{
"views": {
"bridge-demo": {
"title": "Bridge Demo",
"description": "Shows how a plugin page reads the WebUI locale and translations.",
"heading": "Plugin View",
"refresh": "Render again"
}
}
}Field reference:
| Field | Required | Purpose |
|---|---|---|
title | No | The View name in the sidebar, WebUI shell, and plugin detail page. |
description | No | The View description on the plugin detail page. |
| Other fields | No | View-specific translations, such as heading and refresh in this example; read them with bridge.t(). |
Add each language to its matching locale file, such as .astrbot-plugin/i18n/zh-CN.json for Chinese. Use views for new plugins; the older pages key remains only as a compatibility alias. Reload the plugin after editing locale files so the WebUI receives the latest metadata.
If the current locale has no title, the ungrouped sidebar displays the plugin name. In grouped mode it displays the View directory name because the group already shows the plugin name. Render other text inside the View with bridge.t() and react to locale changes with onContext():
function render() {
document.title = bridge.t("views.bridge-demo.title", "Bridge Demo");
document.getElementById("heading").textContent = bridge.t(
"views.bridge-demo.heading",
"Plugin View",
);
}
await bridge.ready();
render();
bridge.onContext(render);Static asset paths
Use normal relative paths:
<link rel="stylesheet" href="./style.css" />
<script type="module" src="./app.js"></script>
<img src="./assets/logo.svg" alt="" />AstrBot authenticates view assets with a path token: the content_path returned by the entry config endpoint looks like /api/v1/plugins/<plugin>/views/<view>/_t/<token>/, and relative URLs inside the page resolve under that prefix and inherit the token through normal URL resolution. Content is served as-is, without rewriting. The token lives as long as the dashboard session and is scoped to exactly that plugin and view.
Do not hardcode content URLs, append asset_token yourself, or rely on .. to escape the View root. The legacy /api/plugin/page/content/...?asset_token=... form has been removed.
If you build a SPA, prefer hash routing. The static asset server resolves real file paths; with history routing, refreshing a page requires a real file at that path.
Security constraints
Plugin Views run inside a restricted iframe:
allow-scripts allow-forms allow-downloadsThe View cannot directly access Dashboard cookies, LocalStorage, or the parent DOM, and it cannot bypass the bridge to reuse Dashboard auth. All operations that need Dashboard identity should go through the bridge.
Asset responses include security headers such as:
X-Frame-Options: SAMEORIGINContent-Security-Policy: frame-ancestors 'self'; object-src 'none'; base-uri 'self'Cache-Control: no-storeX-Content-Type-Options: nosniff
Backend handlers must still validate input. Do not trust paths, filenames, formats, or numeric ranges sent by the View. Store files only in safe directories and prefer whitelisted or regenerated filenames.
Troubleshooting
- View is missing: check that
views/<view_name>/index.htmlexists, the plugin is enabled, and the plugin detail page has been refreshed. - Bridge is missing: make sure your script runs after the bridge SDK is injected; external
type="module"scripts are recommended. - API is not matched: make sure the registered route includes the plugin name prefix, such as
/{PLUGIN_NAME}/stats, while the View endpoint isstats. - Query or JSON is empty: pass GET values through
apiGet(endpoint, params)and POST JSON throughapiPost(endpoint, body). - Upload is empty:
upload()always uses the field namefile; read it with(await request.files()).get("file"). - SSE has no messages: make sure the backend response is
text/event-streamand each message ends with a blank line, such asdata: ...\n\n. - SSE returns 401: do not call
new EventSource("/api/v1/...")directly from the View. NativeEventSourcecannot send theAuthorizationheader; call throughbridge.subscribeSSE()instead.
View labels
Resolve each field independently: check the current locale, then zh-CN, then en-US. Within each locale, prefer views.<name> and fall back to the legacy pages.<name> key. A valid translation overrides the metadata title or description. Without a translation, use metadata; ultimately titles fall back to name and descriptions to an empty string. Blank or non-string translations do not override defaults.
Existing i18n files do not need to move. Legacy pages translations remain valid. You may move default-language labels into metadata, remove duplicate entries for that locale, and keep translations for other languages. Translation files cannot change target, actions, or versions.
Standalone page entries, workbench entries, and tabs share this resolution. An instance's event.view_result(title=...) overrides static labels for dynamic titles such as filenames.