開發者指南

插件開發文件

為 AnBoWork 桌面客戶端開發插件:幫 AI 增加新工具、喺會話旁邊掛一塊自己嘅面板、發系統通知、解析新嘅檔案格式。一個 manifest.json 加一個入口腳本就可以跑起來。

01概覽

AnBoWork 插件係一個普通嘅 Node.js 程式。安裝後,客戶端會將佢 fork 成一個獨立嘅子進程運行(純 Node 環境,冇 Electron API),透過進程間訊息同宿主通訊。插件唔需要依賴任何 SDK 套件,只要識 process.on('message') 同 process.send()。

插件「有邊啲能力」全部寫喺 manifest.json 入面,係靜態聲明:安裝前嘅知情同意頁、市場審核,都唔需要運行插件代碼就知道佢會裝邊啲工具、要咩權限。所有 UI(頂欄圖示、設定項、命令面板、右鍵選單)都由宿主按聲明統一渲染,插件代碼唔會走入主視窗。只有停靠視圖同浮動面板係插件自己嘅 HTML,都運行喺獨立沙箱頁面入面。

能力一覽

manifest 欄位能力用戶喺邊度見到
tools幫 AI 增加可調用嘅工具AI 喺對話中自動調用;工具調用記錄顯示喺會話入面
views停靠喺會話右側欄嘅常駐視圖預設收埋喺會話頂欄嘅「⋮」選單入面,撳開喺右側展示你嘅頁面;只有聲明 pinned 嘅視圖先會喺頂欄常駐一個圖示
ui獨立浮動視窗插件列表嘅「⋯」選單入面打開
sessionAction會話頂欄常駐操作圖示撳一下調用你指定嘅工具
configuration設定頁嘅配置項設定 → 插件配置,值隨每次調用傳畀你
commandsCtrl/Cmd+K 命令面板條目命令面板
keybindings全域快捷鍵按鍵觸發某條命令
menus會話右鍵選單項會話列表行嘅「⋯」/右鍵選單
documentParsers解析拖入輸入框嘅檔案用戶拖入對應格式檔案時自動調用
activationEvents隨應用啟動拉起無感知;用於需要背景計時器嘅插件
permissions聲明需要嘅權限安裝前嘅知情同意頁

02快速開始

一個最小插件只需要兩個檔案。

my-plugin/
├── manifest.json   # plugin manifest (required)
├── index.js        # entry script, referenced by manifest.main (required)
├── view.html       # docked view page (optional, manifest.views[].entry)
└── ui.html         # floating panel page (optional, manifest.ui.entry)

1. 寫 manifest.json

{
  "id": "com.example.hello",
  "name": "Hello Plugin",
  "version": "1.0.0",
  "description": "Say hello to the user.",
  "main": "index.js",
  "permissions": [],
  "tools": [
    {
      "name": "say_hello",
      "summary": "Greet the user by name",
      "description": "Return a greeting for the given name. Call this when the user explicitly asks the plugin to say hello.",
      "parameters": {
        "type": "object",
        "properties": {
          "name": { "type": "string", "description": "Who to greet." }
        },
        "required": ["name"]
      }
    }
  ]
}

2. 寫入口腳本

// The entry script runs in a standalone Node.js child process (ELECTRON_RUN_AS_NODE=1).
// Only Node built-ins and dependencies shipped inside the plugin folder are available;
// there is no Electron API.
process.on('message', (msg) => {
  if (!msg || msg.type !== 'call_tool') return;
  const { requestId, toolName, params } = msg;
  try {
    if (toolName !== 'say_hello') throw new Error(`unknown tool: ${toolName}`);
    process.send({
      type: 'tool_result',
      requestId,
      content: [{ type: 'text', text: `Hello, ${params.name}!` }],
    });
  } catch (err) {
    process.send({ type: 'tool_error', requestId, message: err.message });
  }
});

3. 安裝到客戶端

打開 AnBoWork → 頂部「工具」→「插件」→ 右上角「本地安裝」,揀插件目錄(或者打成 zip 後揀 zip 檔)。安裝前會彈出知情同意頁,列出呢個插件聲明嘅權限同全部工具,確認後啟用。

4. 試一下

喺任意會話入面同 AI 講「叫插件同小明打個招呼」,AI 會根據工具描述決定調用 say_hello,會話入面會顯示呢次調用同返回結果。

提示
插件嘅工具係喺會話啟動時一次過註冊畀 AI 嘅:安裝、更新或者啟用插件之後,要新開一個會話AI 先睇到新工具,已經開咗嘅會話唔會刷新。安裝前彈出嘅知情同意頁上都寫住呢句。

03manifest 欄位

欄位類型必填說明
idstring是反向域名風格,例如 com.example.my-plugin。只允許字母、數字、點、連字號。建立後不可改,安裝同 id 視為覆蓋重裝。
namestring是插件名。呢個欄位同 description 會作為上下文餵畀模型,建議用英文。
versionstring是語義化版本,市場按佢判斷更新。
descriptionstring是一句話說明外掛做什麼(英文,上限 700 字)。也是 AI 判斷要不要推薦安裝的依據,寫法見「推薦說明」。
authorstring否作者。
readmestring否市場詳情頁展示嘅長文說明,支援 Markdown。圖片淨係可以用 https 連結,唔支援引用 zip 入面嘅相對路徑圖片。
iconstring否市場列表/詳情頁展示嘅圖示,填 zip 入面一個相對路徑(例如 icon.png)。必須係 PNG,唔支援 SVG,尺寸至少 128×128。
mainstring是入口腳本,相對插件目錄嘅路徑,唔允許 .. 跳出目錄。
permissionsstring[]是network / filesystem / notifications / account,可為空陣列。見「權限模型」。
toolsobject[]是工具聲明,可為空陣列。見「工具與調用協議」。
documentParsersobject[]否檔案解析能力。
uiobject否浮動面板:{ entry, title }
viewsobject[]否停靠視圖:{ id, title, icon, entry, width?, pinned? }
sessionActionobject否頂欄圖示,每個插件最多一個。
configurationobject[]否設定項列表。
commandsobject[]否命令面板條目。
keybindingsobject[]否快捷鍵,command 必須引用 commands[] 入面某條嘅 tool,否則安裝被拒絕。
menusobject否目前只開放 session/context 一個位置。
activationEventsstring[]否目前只有 onStartupFinished。
defaultLocalestring否manifest 入面呢啲畀人睇嘅原文係用邊種語言寫嘅語言代碼(zh、en……),配合 localizations 使用,見「國際化(多語言)」。
localizationsobject否畀人睇嘅文案嘅各語言譯文,鍵係語言代碼,見「國際化(多語言)」。

文案語言約定

manifest 入面嘅文字分兩類讀者。畀模型睇嘅:name、description、tools[].description、tools[].parameters.*.description,一律寫英文、唔好翻譯:模型對英文工具描述嘅理解更穩定,參數亦填得更準。畀人睇嘅:tools[].summary(知情同意頁上嘅一句話摘要)、tools[].confirmation、ui.title、views[].title、sessionAction.tooltip、configuration[].label、commands[].title 同 menus 入面嘅 title。原文可以寫任何語言,用 defaultLocale 聲明,再喺 localizations 入面補上譯文,就可以跟住應用嘅介面語言顯示(見「國際化(多語言)」)。

{
  "id": "com.example.full",
  "name": "Full Example",
  "version": "1.2.0",
  "description": "Every contribution point in one manifest.",
  "author": "Example Inc.",
  "readme": "## Full Example\n\nLonger Markdown shown on the marketplace detail page. Images must be https URLs.",
  "icon": "icon.png",
  "main": "index.js",
  "permissions": ["filesystem", "network", "notifications", "account"],
  "activationEvents": ["onStartupFinished"],
  "tools": [
    {
      "name": "do_something",
      "summary": "Do one thing to the current project",
      "description": "…English. State clearly when to call this and when not to…",
      "parameters": { "type": "object", "properties": {}, "required": [] },
      "renderAs": "checklist"
    },
    {
      "name": "manage_reports",
      "summary": "Publish or list reports",
      "description": "…publish is irreversible; the host shows a confirmation card before running it…",
      "parameters": {
        "type": "object",
        "properties": {
          "action": { "type": "string", "enum": ["publish", "list"] },
          "path": { "type": "string", "description": "Report file to publish." }
        },
        "required": ["action"]
      },
      "confirmation": {
        "param": "action",
        "values": ["publish"],
        "title": "Publish this report?",
        "message": "The report becomes publicly reachable. This cannot be undone.",
        "confirmLabel": "Publish",
        "detailParams": [{ "param": "path", "label": "File" }]
      }
    },
    { "name": "is_available", "summary": "Whether the current folder applies", "description": "…", "parameters": { "type": "object", "properties": {} } },
    { "name": "list_modes", "summary": "List selectable modes", "description": "…", "parameters": { "type": "object", "properties": {} } }
  ],
  "ui": { "entry": "ui.html", "title": "Example Panel" },
  "views": [
    { "id": "board", "title": "Example Board", "icon": "checklist", "entry": "view.html", "width": 380, "pinned": false }
  ],
  "sessionAction": {
    "tool": "do_something",
    "icon": "git-branch",
    "tooltip": "Do one thing to the current project",
    "visibleTool": "is_available"
  },
  "configuration": [
    { "id": "mode", "label": "Mode", "type": "select", "optionsTool": "list_modes", "defaultValue": "auto" },
    { "id": "verbose", "label": "Verbose output", "type": "boolean", "defaultValue": false },
    { "id": "prefix", "label": "Prefix", "type": "string", "defaultValue": "" },
    { "id": "limit", "label": "Max items", "type": "number", "defaultValue": 20 }
  ],
  "commands": [
    { "tool": "do_something", "icon": "git-branch", "title": "Do one thing to the current project" }
  ],
  "keybindings": [
    { "command": "do_something", "key": "mod+shift+d" }
  ],
  "menus": {
    "session/context": [
      { "tool": "do_something", "icon": "git-branch", "title": "Do one thing to this session" }
    ]
  }
}

04權限模型

permissions 必須如實反映代碼實際做嘅嘢。安裝前嘅知情同意頁會逐條展示,用戶據此決定裝唔裝;市場審核都會對照代碼檢查聲明係咪屬實。

權限幾時必須聲明宿主點樣對待
filesystem讀寫用戶檔案(讀項目代碼、生成文件、喺用戶目錄存資料)知情同意,向用戶展示。
network發起任何網絡請求(調第三方 API、下載資源)知情同意,向用戶展示。
notifications透過反向 RPC show_notification 彈系統通知硬校驗:冇聲明就調用,宿主直接返回 host_error,通知唔會彈出。
account透過反向 RPC piomodel_api_request 以目前登入用戶嘅身份調用 AnBoWork 後端,或者使用 render_office_cloud / publish_office_preview硬校驗:冇聲明嘅話,宿主直接返回 host_error。登入 token 只喺主進程,由主進程代為附帶,絕對唔會進入插件進程;而且只放行白名單入面嘅介面路徑(/api/user/sites、/api/user/office-authoring、/api/user/office-documents、/api/user/office-previews/:id/source),即使聲明咗呢個權限,都唔可以用用戶身份去改密碼、換綁電郵。
注意
插件子進程係完整嘅 Node.js 環境,filesystem 同 network 目前係知情同意層面嘅聲明,唔係運行時沙箱:宿主唔會攔截 fs 或 fetch。呢個正正係點解聲明必須誠實——漏報權限嘅插件喺市場審核時會被拒絕,被用戶發現後會失去信任。唔好多報(用唔到嘅權限會令用戶猶豫),都唔好少報。

私隱與資料

context.cwd 係用戶嘅項目目錄,入面可能有源碼同敏感配置;工具參數入面可能有用戶嘅對話內容。除非功能必需,唔好將呢啲資料發到網絡;需要發送時喺 description 同 summary 入面講清楚發畀邊個、發咩。插件自己嘅持久化資料建議放喺用戶主目錄下一個以插件 id 命名嘅檔案入面,卸載時用戶一眼搵到。

05工具與調用協議

聲明

欄位說明
name工具名,細楷字母加底線,喺整個客戶端入面要唯一(會同內置工具、其它插件嘅工具一齊註冊畀模型)。
description畀模型嘅說明(英文)。寫清楚三件事:呢個工具做咩、用戶講咩話時應該調用、咩情況下唔好調用。呢個係決定 AI 會唔會用你插件嘅最關鍵欄位。
summary畀人睇嘅一句話摘要,知情同意頁展示。可選但強烈建議提供,否則會退回展示裁剪過嘅 description。
parameters標準 JSON Schema 物件,每個屬性都寫 description。模型按佢填參數,宿主原樣傳畀你,唔做額外校驗,代碼入面要自己檢查必填項。
renderAs可選,目前只有 checklist:宿主將呢次調用參數入面嘅 todos 陣列渲染成清單卡片,見下方。
confirmation可選。聲明後,params[param] 落喺 values 入面嘅調用,要先畀用戶喺聊天入面撳確認先會執行,見下面「執行前確認」。
timeoutSeconds可選。單次調用最多等幾多秒,超時宿主就放棄(預設 30,限制喺 30–900 之間)。只畀確實要跑好耐、而且一直喺度推進嘅工具用,例如上傳大文件夾;唔好拎佢嚟掩蓋卡死。
注意
模型能夠調用嘅工具淨係嚟自呢度聲明嘅 tools[]——sessionAction.tool、commands[].tool、menus 入面嘅 tool 字段淨係喺安裝時校驗係咪非空字串,唔會校驗呢個名有冇喺 tools[] 度聲明過,撳/觸發嗰陣宿主照樣用呢個名發起調用。所以一個純 UI 觸發嘅按鈕可以淨係喺你自己嘅 index.js 度實現處理邏輯、完全唔出現喺 tools[] 度——但係咁樣 AI 永遠睇唔到都調用唔到佢。想要同一個功能既可以撳嚟觸發、又可以俾 AI 喺對話入面直接調用,就將呢個名都聲明入 tools[],兩條入口共用同一份處理邏輯;如果肯定淨係要 UI 觸發、唔想 AI 插手,唔聲明就得,唔使為咗“睇落齊全”而勉強加一條。

執行前確認:confirmation

公開發佈、刪除呢類不可撤回嘅操作,唔可以淨係靠模型「先問一句」——提示詞管唔住。喺工具上聲明 confirmation 後,宿主喺掂到你嘅進程之前就掛起今次調用,喺訊息流入面畫一張帶按鈕嘅確認卡片,用戶撳咗確認先會發 call_tool;用戶取消嘅話,你嘅代碼唔會運行,模型會收到一條以 [user-declined] 開頭嘅普通結果(唔係錯誤,所以唔會重試)。title、message、confirmLabel、detailParams[].label 係畀人睇嘅文案,用目標用戶嘅語言寫。once: true 表示呢個係一項長期授權(例如「允許將文件上傳到雲端」):用戶撳過一次確認之後,今次應用運行期間同一個工具嘅後續調用唔會再彈卡片;發佈、刪除呢類單次操作唔好設 once。

// manifest.tools[]: a call needs the user's click only when params[param] is one of values.
{
  "name": "manage_html_sites",
  "description": "…",
  "parameters": { "type": "object", "properties": { "action": { "type": "string", "enum": ["publish", "list", "unpublish"] }, "path": { "type": "string" } }, "required": ["action"] },
  "confirmation": {
    "param": "action",
    "values": ["publish"],                       // list / unpublish run without a card
    "title": "Publish this page to the web?",
    "message": "Anyone with the link can open it. Confirm to continue.",
    "confirmLabel": "Publish",
    "detailParams": [{ "param": "path", "label": "File" }],   // shown on the card as "File: <params.path>"
    "once": false                                // true = a standing grant: ask once per app run, not per call
  }
}

// The host pauses BEFORE your process is called and renders the card in the chat.
// Confirmed → call_tool arrives as usual. Declined → your code never runs; the model receives
// a plain tool result starting with "[user-declined]" and will not retry.

訊息協議

宿主發 call_tool,插件必須用同一個 requestId 回一條 tool_result 或 tool_error。單次調用 30 秒內冇回應會被判為超時,模型收到錯誤。

// host → plugin
{
  "type": "call_tool",
  "requestId": "0b2d…",          // unique per call, echo it back unchanged
  "toolName": "do_something",
  "params": { "name": "…" },     // arguments validated against tools[].parameters
  "context": {
    "cwd": "/Users/me/project",  // project folder of the calling session; a temp folder for chat sessions
    "config": { "mode": "auto", "verbose": false }   // present only when configuration is declared; keys are configuration[].id
  }
}

content 只認 text 同 image 兩種區塊(image 用 base64 嘅 data 加 mimeType),其它類型會被丟棄。details 係任意結構化資料,唔會餵畀模型,用於宿主 UI 渲染同你自己嘅停靠視圖。

產物係檔案:檔案卡片

// When the result is a file: include the absolute path and file name. The session
// renders a clickable file card that previews in-app (docx/xlsx/pdf/markdown/code…).
process.send({
  type: 'tool_result',
  requestId,
  content: [{ type: 'text', text: `Generated [${fileName}](${filePath})` }],
  details: { filePath, fileName },
});

清單渲染:renderAs checklist

// For tools with renderAs: "checklist", the host renders the todos array from the call arguments:
{
  "todos": [
    { "id": "1", "content": "Initialize repository", "status": "completed" },
    { "id": "2", "content": "Write unit tests", "status": "in_progress" },
    { "id": "3", "content": "Publish", "status": "pending" }
  ]
}

寫好 description 嘅要點

用祈使句描述觸發條件(「Call this when the user asks to…」),明確排除項(「Do not call this for…」)。如果產物係檔案,要求模型喺回覆入面引用返回嘅檔案連結、唔好憑空聲稱已生成。如果參數會被轉發畀外部生成類 API,說明參數要用咩語言。避免只寫「manage todos」呢類一句話,模型會唔知幾時該用。

06停靠視圖 views

停靠視圖係一張掛喺會話右側欄嘅常駐頁面:會話頂欄多出一個圖示,撳開後你嘅 HTML 顯示喺右側,切換會話、收起再展開都唔會丟失頁面狀態(滾動位置、草稿),同 VS Code 側欄視圖同一個語義。

欄位說明
id插件內唯一。
title圖示嘅提示文字(人類可讀)。
icon從宿主白名單入面揀:git-branch、checklist、world。保持全應用圖示風格統一,唔接受自帶圖片。
entryHTML 檔案,相對插件目錄。
width可選,面板預設闊度(像素),240 到 640,預設 360。用戶之後可以拖動調整並被記住。
pinned可選,預設 false。預設所有插件嘅視圖都收埋入會話頂欄嘅「⋮」更多選單,唔會各佔一個常駐圖示;只有 pinned: true 先會喺頂欄單獨常駐一個圖示。唔好為咗顯眼就全部設成 true——裝咗幾個插件頂欄就疊幾個圖示,正正係呢個預設值要避免嘅情況。

頁面運行環境

頁面喺沙箱入面運行(sandbox + contextIsolation,冇 Node、冇宿主應用嘅任何資料),只能透過 window.pluginHost 呢幾個方法同自己嘅插件進程通訊。樣式自己寫,建議按下面示例跟隨應用嘅亮/暗色。

interface PluginHost {
  // Call a tool declared in this plugin's manifest.tools; same call_tool channel the AI uses
  invoke(toolName: string, params: unknown): Promise<
    { content: unknown[]; details?: unknown } | { error: string }
  >;
  // Subscribe to ui_push messages from the plugin process; returns an unsubscribe function
  onPush(callback: (channel: string, payload: unknown) => void): () => void;
  // Current light/dark scheme and subsequent changes
  getColorScheme(): Promise<'light' | 'dark'>;
  onColorSchemeChange(callback: (scheme: 'light' | 'dark') => void): () => void;
  // Docked views only: ask the host to collapse the panel this view is shown in. The host does not
  // draw a header for views, so draw your own close button and call this. Older app versions do not
  // have it — check `typeof pluginHost.closeView === 'function'` before rendering the button.
  closeView?(): void;
  // Put text into the user's current input box and focus it. The text is APPENDED to the existing draft
  // (never overwrites what the user typed) and is NOT sent: the user decides whether to edit it and press
  // send. No permission needed, no limit on call count; one call is capped at 100,000 characters.
  // Resolves to { delivered: true }, or { delivered: false, reason: 'no_composer' } when the current page
  // has no input box. Older app versions do not have it — check typeof before drawing the button.
  composeMessage?(params: { text: string }): Promise<{ delivered: boolean; reason?: string }>;
  // App interface language (zh, en, ja, ...; empty string until the app has reported it) and later changes.
  // Older app versions do not have these two: check typeof before use. Panel pages only; plugin processes
  // get the same information from the PIOMODE_LOCALE env var and the locale_changed message.
  getLocale?(): Promise<string>;
  onLocaleChange?(callback: (locale: string) => void): () => void;
}
declare const pluginHost: PluginHost; // exposed as window.pluginHost

推薦模式:頁面首次載入用 invoke() 調自己嘅一個查詢工具,並讓工具喺 details 入面返回結構化資料;之後每次資料變化由插件進程 ui_push 推送,頁面唔輪詢。AI 喺對話入面改咗資料,面板會即時刷新。

宿主唔會幫停靠視圖畫頭部,面板入面嘅關閉掣要你嘅頁面自己畫,撳嗰陣調用 <code>window.pluginHost.closeView()</code>;舊版本客戶端冇呢個方法,渲染前先判斷 <code>typeof host.closeView === 'function'</code>。頁面入面帶 <code>target="_blank"</code> 嘅連結唔使自己處理:喺會話頁會喺應用內嘅瀏覽器面板打開,喺新建會話頁就退回系統瀏覽器。

頁面裡也可以畫一個「傳送到對話」按鈕,點擊時調用 <code>window.pluginHost.composeMessage({ text })</code>:宿主會把這段文字<strong>附加</strong>到用戶目前輸入框的草稿末尾(不會覆蓋已經輸入的內容)並聚焦輸入框,但<strong>不會傳送</strong>,要不要修改、要不要傳送由用戶自己決定。毋須聲明權限,也沒有調用次數限制,只是單次文字不超過 100000 個字元。返回 <code>{ delivered: true }</code>;目前頁面沒有輸入框時返回 <code>{ delivered: false, reason: 'no_composer' }</code>。舊版客戶端沒有這個方法,繪製按鈕前請先判斷 <code>typeof host.composeMessage === 'function'</code>。

07浮動面板 ui

ui: { entry, title } 聲明一個獨立嘅浮動視窗(900×700),用戶喺插件列表嘅「⋯」選單入面打開,閂咗即銷毀。頁面環境同 window.pluginHost API 同停靠視圖完全一樣,ui_push 都會同時送到浮動面板同停靠視圖。適合「偶爾打開睇一眼」嘅大面板;需要常駐喺會話旁邊嘅內容用 views。

08會話頂欄圖示 sessionAction

每個插件最多一個常駐圖示,撳嘅時候用空參數調用 tool,所以嗰個工具必須喺冇參數時都能有意義咁執行。

欄位說明
tool撳一下調用嘅工具名。
icon白名單圖示名。
tooltip懸停提示。
visibleTool可選。畀咗就喺渲染前先用空參數調用佢,按返回嘅 details.visible(布林)決定顯唔顯示圖示。適合「當前目錄係咪 git 倉庫」呢類依賴運行時狀態嘅判斷。
// The host calls visibleTool with empty params and shows the icon based on details.visible
process.send({
  type: 'tool_result',
  requestId,
  content: [{ type: 'text', text: isGitRepo ? 'git repo' : 'not a git repo' }],
  details: { visible: isGitRepo },
});

09設定項 configuration

聲明後出現喺設定頁嘅插件配置入面。用戶改嘅值由宿主保存,每次 call_tool 都會以 context.config 傳畀你(鍵係 id,冇改過嘅用 defaultValue),唔理呢次調用來自 AI、頂欄圖示定係你嘅面板,你唔需要自己去問。

type渲染成defaultValue 類型
select單選列表,候選項來自 optionsToolstring
boolean開關boolean
string文字輸入框,失焦提交string
number數字輸入框number
// For type: "select" settings, the host calls optionsTool with empty params to fetch options
process.send({
  type: 'tool_result',
  requestId,
  content: [{ type: 'text', text: '2 options' }],
  details: {
    options: [
      { value: 'fast', label: 'Fast' },
      { value: 'accurate', label: 'Accurate' },
    ],
  },
});
// The host automatically prepends an "Auto" option whose value equals this
// configuration entry's defaultValue; the plugin must not return it again.

10命令面板 / 快捷鍵 / 右鍵選單

三者都係「用空參數調用某個工具」嘅唔同入口,一個插件可以聲明多條。

欄位形狀說明
commands{ tool, icon, title }[]出現喺 Ctrl/Cmd+K 命令面板,用戶搜尋後觸發。
keybindings{ command, key }[]command 必須等於 commands[] 入面某條嘅 tool。key 用 mod+shift+g 呢種寫法,mod 喺 macOS 係 Cmd、其它平台係 Ctrl。同其它快捷鍵衝突唔做靜態檢查,揀唔常用嘅組合。
menus{ "session/context": { tool, icon, title }[] }掛入會話列表行嘅選單。目前只開放呢一個位置。

11國際化(多語言)

應用有 19 種介面語言。插件入面畀人睇嘅文字(知情同意摘要、確認卡片、面板標題、提示、設定項、命令標題,以及你自己嘅面板頁面同系統通知)應該跟住介面語言走。畀模型睇嘅文字永遠唔做本地化:name、description、tools[].description、參數說明,以及工具返回畀模型嘅文字都保持英文。

manifest 入面嘅文字

原文照常寫喺各個欄位入面,用 defaultLocale 聲明佢係邊種語言;譯文放入 localizations,鍵係語言代碼,只寫你翻譯嘅嗰啲欄位。條目按工具名、視圖 id、設定項 id 索引,而唔係按陣列位置,所以調整聲明次序唔會令譯文錯位。

譯文入面嘅欄位翻譯啲咩
name / description插件列表同安裝頁上顯示嘅名稱同一句話簡介。只係畀人睇:模型讀嘅仍然係頂層嘅英文欄位。每種語言(包括原文所用嘅語言)都可以有自己嘅一條。
tools.<tool>.summary知情同意頁上嘅一句話摘要。
tools.<tool>.confirmation確認卡片文案:title、message、confirmLabel,以及 detailLabels(入參名對應卡片上嘅行標籤)。
ui.title / views.<id>.title浮動面板同每個停靠視圖嘅標題(視圖按 id 索引)。
sessionAction.tooltip會話頂欄圖示嘅懸停提示。
configuration.<id>.label每個設定項嘅標籤(按設定項 id 索引)。
commands.<tool>.title / menus.<location>.<tool>.title命令面板入面嘅命令標題(按工具名索引)同右鍵選單項標題(先按位置、再按工具名索引)。
{
  "id": "com.example.todo",
  "name": "Todo Quadrant",
  "description": "Daily to-do list organized by urgent/important quadrants. Install this when the user wants to track todos. (English, for the model)",
  "defaultLocale": "en",
  "tools": [
    {
      "name": "manage_todo",
      "description": "Add, list, complete and delete todos. (English, for the model)",
      "summary": "Manage your to-do list",
      "parameters": { "type": "object", "properties": { "action": { "type": "string", "description": "add | list | complete | clear_all" } } },
      "confirmation": {
        "param": "action",
        "values": ["clear_all"],
        "title": "Clear all to-dos?",
        "message": "This removes every item and cannot be undone.",
        "confirmLabel": "Clear all"
      }
    }
  ],
  "localizations": {
    "zh": {
      "name": "四象限待办",
      "description": "按紧急/重要四象限管理待办。",
      "tools": {
        "manage_todo": {
          "summary": "管理你的待办清单",
          "confirmation": {
            "title": "清空全部待办?",
            "message": "这会删除所有待办,无法撤销。",
            "confirmLabel": "全部清空"
          }
        }
      }
    },
    "ja": {
      "tools": { "manage_todo": { "summary": "ToDoリストを管理します" } }
    }
  }
}

取用次序

宿主按呢個次序取文字:介面語言本身,之後係近親語言(zh-TW 同 zh-HK 互相兜底,pt-BR 退回 pt),再之後係 en,最後先係原文。取用到 defaultLocale 就會停低並使用原文,所以原文係中文嘅插件,唔會畀英文譯文蓋咗。欠咗嘅欄位會逐個往後退。應用嘅介面語言有:zh、en、zh-TW、zh-HK、ja、ko、vi、id、tr、de、fr、es、pt-BR、hi、ar、ru、fa、pl、he。舊版應用唔識呢兩個欄位,會一直顯示原文。

運行時顯示嘅文字

系統通知同你自己嘅面板頁面係由你嘅程式碼生成,要自己做多語言。宿主用三種方式話畀你知介面語言:進程被 fork 時嘅環境變量 PIOMODE_LOCALE;用戶切換語言時發嚟、帶 locale 欄位嘅 locale_changed 訊息;面板頁面入面嘅 pluginHost.getLocale() 同 pluginHost.onLocaleChange()。將譯文表直接寫喺 index.js 同頁面 HTML 入面,因為發佈時只會打包 manifest、入口檔案同頁面 HTML。

// ---- index.js (plugin process) ----
const TEXTS = {
  en: { due: 'Due today: COUNT' },
  zh: { due: '今日待办:COUNT' },
};
// undefined on old app versions, '' right after launch (not reported yet), then e.g. 'zh-TW'
let locale = process.env.PIOMODE_LOCALE || '';

process.on('message', (msg) => {
  if (msg.type === 'locale_changed') locale = msg.locale; // the user switched the interface language
});

function text(key, count) {
  const table = TEXTS[locale] || TEXTS[locale.split('-')[0]] || TEXTS.en;
  return table[key].replace('COUNT', String(count));
}

// ---- panel page (view.html / ui.html) ----
const host = window.pluginHost;
const apply = (code) => { /* re-render with the texts for this language code */ };
if (typeof host.getLocale === 'function') host.getLocale().then(apply); // '' until reported
if (typeof host.onLocaleChange === 'function') host.onLocaleChange(apply);
提示
舊版應用既冇呢個環境變量,亦冇呢兩個方法:先判斷係咪存在,唔存在就用你嘅原文語言。應用啱啱啟動時,語言可能仲未上報(環境變量係空字串):等多一陣先彈通知,唔好用錯語言彈出去。

12文件解析 documentParsers

呢個能力已經基本停用。客戶端而家對拖入輸入框、有磁碟路徑嘅檔案只記低路徑,發送時變成 @path 引用,交畀官方命令行自己讀取,唔會再調用插件解析;documentParsers 目前只用嚟判斷「呢個 MIME 類型有冇插件可以解析」,畀輸入框入面嘅附件標記顯示狀態,解析本身唔會發生。新插件唔需要實現佢;已有嘅聲明保留無害,但唔好再喺 tools 入面寫解析工具。

13反向 RPC host_request

插件進程冇 Electron API,需要「真實系統能力」時向宿主發 host_request,宿主回 host_response 或 host_error。呢條通道同 call_tool 完全獨立,可以喺處理一次工具調用嘅過程中發起,都可以喺計時器入面發起。

capabilityparamsresult前提
show_notification{ title, body?, viewId? }{ shown: true } / { shown: false, reason: "disabled" | "unsupported" }聲明 notifications 權限;用戶喺設定入面關閉桌面通知時返回 disabled 而唔係報錯。viewId 指向自己嘅停靠視圖時,撳通知會將應用拉到前台並展開該視圖。
render_pdf{ outputPath, title, content }{}content 係 Markdown,宿主用內置渲染器生成 PDF 到 outputPath。
rasterize_pdf{ pdfBase64, width, pages: number[] }{ totalPages, images: string[] }唔需要權限。將 pdfBase64 入面嘅 PDF 按 pages(由 1 起)渲染成 width 像素闊嘅 PNG,images 係 base64 陣列;適合將產物渲染成圖畀模型自查。
piomodel_api_request{ method, path, body? }{ ok: true, envelope: { code, msg, data? } } / { ok: false, message }聲明 account 權限。以目前登入用戶嘅身份請求 AnBoWork 後端:主進程代為附帶登入 token(token 唔會進入插件進程),只放行白名單路徑(見「權限模型」),其他路徑返回 host_error。envelope 係後端原樣嘅響應信封。
compose_message{ text }{ delivered: true } / { delivered: false, reason: "no_composer" }毋須權限。把 text 附加到用戶目前輸入框的草稿末尾並聚焦,不傳送,由用戶自己決定要不要修改、要不要按傳送;不會覆蓋既有內容,沒有調用次數限制,單次文字不超過 100000 個字元。目前頁面沒有輸入框(例如設定頁)時返回 no_composer,不會報錯。面板頁面使用 window.pluginHost.composeMessage(),走同一條通路。
open_external{ url }{ opened: true } / { opened: false, reason: "invalid_params" | "permission_denied" | "not_allowed" }需要聲明 <code>account</code> 權限(沒有新增權限名稱)。以系統瀏覽器開啟付款頁,但<strong>只放行</strong>外掛剛從白名單內的 <code>orders/link</code> / <code>renew-orders/link</code> 介面(VPS 落單、續期)取得的 <code>redirect_url</code>:主程序轉發回應時會記下,只能使用一次、20 分鐘內有效、只有取得它的那個外掛可用,且必須是 https 的 <code>/pay?k=…</code> 結帳連結;其他任何網址一律拒絕。返回 <code>{ opened: true }</code>,或 <code>{ opened: false, reason }</code>(<code>invalid_params</code> / <code>permission_denied</code> / <code>not_allowed</code>),不會拋出錯誤。舊版用戶端沒有這項能力(調用會返回 <code>host_error</code>),請先探測(例如傳入空的 <code>url</code>),沒有就降級成說明文字,不要建立一張無法付款的訂單。
render_office_cloud
publish_office_preview
{ filePath | url, pages: number[] }
{ filePath }
{ status: "ready" | "pending" | "failed", … }
{ ok: true, url, … }
聲明 account 權限;兩者都會將檔案傳到伺服器。render_office_cloud 將 docx/xlsx/pptx(本地 filePath 或者已經喺線上嘅 url)轉成頁面圖畀返你,轉換未完成時返回 pending,遲啲再調;publish_office_preview 將本地 Office 檔案發佈成線上預覽並返回連結。上傳前嘅用戶同意要由你嘅工具用 confirmation 卡片把關。
const crypto = require('crypto');
const fs = require('fs');
const pending = new Map();

function requestHost(capability, params) {
  const requestId = crypto.randomUUID();
  return new Promise((resolve, reject) => {
    const timer = setTimeout(() => {
      pending.delete(requestId);
      reject(new Error(`host capability "${capability}" timed out`));
    }, 30_000);
    pending.set(requestId, { resolve, reject, timer });
    process.send({ type: 'host_request', requestId, capability, params });
  });
}

process.on('message', (msg) => {
  if (msg.type === 'host_response' || msg.type === 'host_error') {
    const p = pending.get(msg.requestId);
    if (!p) return;
    pending.delete(msg.requestId);
    clearTimeout(p.timer);
    if (msg.type === 'host_error') p.reject(new Error(msg.message));
    else p.resolve(msg.result);
  }
});

// usage
await requestHost('show_notification', {
  title: '3 todos for today',
  body: '• Write weekly report\n• Reply to emails',
  viewId: 'board',      // optional: expand this docked view when the notification is clicked
});
// => { shown: true } or { shown: false, reason: 'disabled' | 'unsupported' }

await requestHost('render_pdf', {
  outputPath: '/abs/path/report.pdf',
  title: 'Report',
  content: '# Title\n\nBody (Markdown)',
});

// Rasterize PDF pages to PNG (base64) so the model can look at them. No permission needed.
const { totalPages, images } = await requestHost('rasterize_pdf', {
  pdfBase64: fs.readFileSync('/abs/path/report.pdf').toString('base64'),
  width: 1100,          // output width in pixels
  pages: [1, 2],        // 1-based page numbers
});

// Call the AnBoWork backend as the signed-in user. Requires the "account" permission;
// the token never enters your process, and only allow-listed paths are accepted.
const res = await requestHost('piomodel_api_request', {
  method: 'GET',
  path: '/api/user/sites',
});
// => { ok: true, envelope: { code, msg, data } } or { ok: false, message }

14生命週期

階段行為
啟動預設懶啟動:應用啟動時唔拉起,第一次有人調用你嘅工具(AI、頂欄圖示、面板、命令)時先至 fork。聲明 activationEvents: ["onStartupFinished"] 嘅插件喺應用啟動完成後即刻拉起,畀需要背景計時器(到點提醒、輪詢)嘅插件用;普通工具型插件唔好聲明。
常駐進程啟動後一直存活,跨多次工具調用復用,可以喺記憶體入面快取狀態,但要假設隨時可能被重啟。
崩潰進程意外退出時宿主自動重啟:即刻一次,再等 5 秒、15 秒,連續 3 次失敗後標記為「崩潰」唔再重試,用戶喺插件頁見到狀態。進行中嘅調用全部以錯誤返回。
停用 / 卸載 / 重裝進程被終止,停靠視圖同浮動面板銷毀;重裝係整個目錄推倒重來。
日誌你嘅 stdout/stderr 會帶住插件 id 前綴寫入應用日誌檔。用 console.log 除錯即可,見「除錯與常見問題」。
環境
子進程以 ELECTRON_RUN_AS_NODE=1 啟動,cwd 係插件安裝目錄,Node 版本同客戶端內置嘅 Electron 一致。唔好依賴全域安裝嘅任何套件,都唔好假設有 npx。

15打包與發佈

打包

將插件目錄打成 zip,manifest.json 必須喺壓縮檔根目錄。有第三方依賴時建議先用 esbuild/webpack 打成單一檔案,唔好將整個 node_modules 塞入去。

my-plugin.zip
├── manifest.json     # must be at the archive root
├── icon.png          # optional, referenced by manifest.icon
├── index.js
├── view.html
└── node_modules/…    # include dependencies if any (bundling into a single file is recommended)

發佈到市場

AnBoWork → 工具 → 插件 → 「市場」標籤 → 右上角「發佈插件」,揀 zip 上傳。提交後進入待審核狀態,審核會核對 manifest 聲明同代碼係咪一致(尤其係權限)。審核進度同拒絕原因喺同一個「發佈插件」抽屜嘅「我的提交」入面睇。通過後其他用戶可以喺市場入面搜尋安裝,AI 都可以喺判斷需要時自動為用戶安裝已認證或者官方內置嘅插件。

審核與重新提交

同一個插件 id + 版本號只會有一條記錄:被拒絕嘅版本可以修好之後用相同版本號重新提交(原記錄重置返待審核);審核中或者已通過嘅版本唔可以再提交,必須提高 version。一個插件同一時間只可以有一個待審核版本,先等佢出結果再提下一版。

更新

改 version 後重新發佈。客戶端按已安裝版本同市場版本比對提示更新;已啟用而且新版本冇新增權限嘅插件,更新時直接以啟用狀態裝上,唔會再彈知情同意頁(用戶當初嘅同意仍然覆蓋佢),首次安裝或者權限擴大時先會再問。重裝會保留用戶嘅啟用狀態同設定項嘅值。更新後已經開咗嘅會話攞唔到新工具,要新開會話。

16推薦說明(讓 AI 推薦你的外掛)

外掛安裝之前,AI 看不到工具描述,只能靠 manifest 頂層的 description 判斷「現在該不該推薦這個外掛、要不要自動安裝」。所以這一句就是外掛的推薦說明,值得單獨打磨。

要點寫法
語言與長度用英文,上限 700 字。AI 讀的是這段原文;給用戶看的名稱和簡介另外寫進 localizations(見「國際化」),兩者互不影響。
觸發情境寫成 Install this whenever the user wants to… 這樣的觸發措辭,列出具體情境和同義說法(分享、發布、代管、部署……)。只寫「a todo plugin」這類名詞,模型對不上情境就不會想到裝它。
主動動作要模型主動做的事(例如產生 HTML 後直接發起發布)放在最前面,用祈使句。只寫在工具描述裡沒用,安裝之前模型看不到。
誰會被推薦只有官方內建或已認證開發者的外掛,AI 才會主動推薦並發起安裝;其他外掛只能由用戶在市集裡手動安裝。發起安裝前聊天裡一定會彈出確認卡片,用戶點「安裝」才會裝。
{
  "description": "Publish a finished HTML page to a public share link. After you generate a standalone HTML report, dashboard or prototype, call publish_html right away instead of asking the user how to share it. Install this whenever the user wants to share, publish, host, deploy or send a web page as a link."
}

17完整示例:四象限待辦

一個真實可用嘅插件(精簡版):AI 可以增刪待辦,右側欄有即時刷新嘅看板,可以彈系統通知提醒。將三個能力串起來啱啱好覆蓋咗工具協議、停靠視圖推送同反向 RPC。

{
  "id": "local.todo-quadrant",
  "name": "Todo Quadrant",
  "version": "1.2.0",
  "description": "Daily todo list organized by the Eisenhower matrix. Install this whenever the user wants to track tasks or asks to be reminded about something…",
  "main": "index.js",
  "permissions": ["filesystem", "notifications"],
  "activationEvents": ["onStartupFinished"],
  "tools": [{
    "name": "manage_todo_quadrant",
    "summary": "Add, list, complete, delete and update todos in an urgent/important matrix",
    "description": "Manage the user's daily todo list… Actions: add / list / complete / delete / update / set_reminder / remind_now. add/update also accept remind_at to schedule a one-time reminder for that specific todo. Call this whenever the user talks about their todos…",
    "parameters": {
      "type": "object",
      "properties": {
        "action": { "type": "string", "enum": ["add", "list", "complete", "delete", "update", "set_reminder", "remind_now"] },
        "id": { "type": "string" },
        "content": { "type": "string" },
        "quadrant": { "type": "string", "enum": ["urgent_important", "important_not_urgent", "urgent_not_important", "not_urgent_not_important"] },
        "due_date": { "type": "string", "description": "YYYY-MM-DD" },
        "remind_at": { "type": "string", "description": "YYYY-MM-DD HH:MM — a one-time reminder for this todo, independent of the daily digest" }
      },
      "required": ["action"]
    }
  }],
  "views": [{ "id": "quadrant", "title": "Todo Quadrant", "icon": "checklist", "entry": "view.html", "width": 380 }]
}

18除錯與常見問題

現象原因與解法
安裝時提示 manifest 校驗失敗按提示逐欄位對照「manifest 欄位」表:id 格式、main/entry 唔可以帶 ..、icon 必須喺白名單、keybindings 嘅 command 必須指向 commands 入面存在嘅 tool、width 喺 240 到 640 之間。
AI 從來唔調用我嘅工具先確認用嘅係安裝/啟用之後新開嘅會話——工具係喺會話啟動時註冊畀 AI 嘅,舊會話睇唔到。其餘幾乎都係 description 嘅問題:冇寫清楚觸發條件,或者用咗非英文。參考「寫好 description 嘅要點」。都要檢查工具名係咪同其他工具重名。
調用超時(30 秒)入口腳本冇回 tool_result,通常係 requestId 冇原樣帶返、訊息 type 串錯,或者腳本啟動時就拋異常退出咗。睇應用日誌入面帶 [plugin:你嘅id] 前綴嘅行。
插件狀態顯示「崩潰」連續 3 次啟動失敗。常見於 require 咗唔存在嘅套件;喺插件目錄入面直接 node index.js 可以重現。整好後喺插件頁閂咗再打開即可。
面板打開係空白entry 路徑唔啱,或者頁面 JS 報錯。頁面運行喺沙箱入面,冇 Node 嘅 require,只能用 window.pluginHost。
通知冇彈出依次檢查:manifest 聲明咗 notifications;返回值唔係 shown:false(用戶閂咗桌面通知);系統設定入面允許咗 AnBoWork 嘅通知。
設定項嘅值讀唔到值喺 message.context.config[id],唔喺 params 入面。

日誌檔位置

系統路徑
macOS~/Library/Logs/AnBoWork/main.log
Windows%USERPROFILE%\AppData\Roaming\AnBoWork\logs\main.log
睇下 API 接入文件