Skip to main content
xbot
切换暗/亮/自动模式 切换暗/亮/自动模式 切换暗/亮/自动模式 返回首页

本文档的英文版本包含完整的代码示例和 API 参考。请参阅 English version 获取完整内容。

本文档提供 权限系统 的中文概览。详细的 API 参考和代码示例请参阅英文版本。

xbot’s plugin permission system provides fine-grained capability control. Plugins declare required permissions in plugin.json and can only access the APIs they declare.

How It Works

  1. Declaration: Plugins list required permissions in plugin.json:

    {
      "permissions": ["tools.register", "ui.contribute", "storage"]
    }
    
  2. Validation: During manifest loading, permissions are validated against the known list. Unknown permissions are rejected.

  3. Enforcement: At runtime, PluginContext wraps every method call with a PermissionChecker. Undeclared permissions return an error.

Permission List

Backend (Go) Permissions

PermissionDescriptionPluginContext Methods
tools.registerRegister tools for the LLMRegisterTool, RegisterTools, UseMiddleware
hooks.registerRegister lifecycle hooksOnPreToolUse, OnPostToolUse, OnUserPrompt, OnAgentStop, OnSessionStart, OnSessionEnd, OnEvent, OnAllToolUse, OnError
bus.readSubscribe to event busSubscribe
bus.writePublish to event busPublish
bus.pluginPlugin-to-plugin eventsRequires bus.read + bus.write
ui.contributeContribute UI widgetsContributeUI, UpdateWidget, RegisterWebActionHandler
ui.themesContribute themesContributeTheme
channels.registerRegister channel providersChannel provider registration
storageAccess per-plugin KV storageStorage, StorageInt, StorageBool, StorageJSON, StorageGetJSON
cronSchedule cron jobsScheduleCron

Frontend (Web) Permissions

PermissionDescriptionContext API
rpcMake RPC calls to the backendctx.rpc
uiAccess UI API (open views, tabs)ctx.ui
eventsAccess event busctx.events
commandsRegister and execute commandsctx.commands
stateAccess shared statectx.state
pluginsAccess plugin management APIctx.plugins
configAccess configuration APIctx.config

PermissionChecker

The PermissionChecker (plugin/permissions.go) validates permissions:

type PermissionChecker struct {
    permissions map[string]bool
}

func NewPermissionChecker(permissions []string) *PermissionChecker

func (pc *PermissionChecker) Has(permission string) bool
func (pc *PermissionChecker) HasAll(permissions ...string) bool
func (pc *PermissionChecker) HasAny(permissions ...string) bool

Validation

During manifest loading, validateManifest checks that all declared permissions are valid:

func IsValidPermission(perm string) bool
func AllPermissions() []string

Unknown permissions cause manifest validation to fail, preventing the plugin from loading.

Best Practices

  1. Declare only what you need: Minimize the permission surface
  2. Don’t request bus.plugin unless you need both read and write: Use bus.read or bus.write individually
  3. Frontend permissions are separate: Web plugins use a different permission set (rpc, ui, events, etc.)
  4. Permissions are not hierarchical: ui.contribute does not imply ui.themes

See Also