Plugins can add pages to the sidebar, VM tabs, Settings and dialogs. Their pages can now use host forms and navigation, VM and settings access, authorized file I/O, task progress, controlled HTTP requests, events and private storage. Plugin pages cannot change allow_ai_access, which stays the user's per-VM consent. Closing a page cancels its unfinished tasks, and running plugin tasks no longer block changing the data folder. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
63 lines
1.8 KiB
Go
63 lines
1.8 KiB
Go
package app
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
|
|
"github.com/Nwflower/reims-go/internal/plugin"
|
|
)
|
|
|
|
// panelEventPermissions covers every BridgeEventCatalog entry. The theme and
|
|
// locale events are always delivered, so subscribing to them changes nothing.
|
|
var panelEventPermissions = map[string]string{
|
|
"theme.changed": "",
|
|
"locale.changed": "",
|
|
"vm.stateChanged": "vm:read",
|
|
"view.visibilityChanged": "",
|
|
"task.updated": "",
|
|
}
|
|
|
|
func panelEventNames(params map[string]any, allowEmpty bool) ([]string, error) {
|
|
raw, ok := params["events"].([]any)
|
|
if !ok || (!allowEmpty && len(raw) == 0) || len(raw) > len(panelEventPermissions) {
|
|
return nil, fmt.Errorf("events must be a non-empty array")
|
|
}
|
|
seen := make(map[string]bool, len(raw))
|
|
names := make([]string, 0, len(raw))
|
|
for _, item := range raw {
|
|
name, ok := item.(string)
|
|
if !ok || seen[name] {
|
|
return nil, fmt.Errorf("invalid event list")
|
|
}
|
|
if _, ok := panelEventPermissions[name]; !ok {
|
|
return nil, fmt.Errorf("unknown event %q", name)
|
|
}
|
|
seen[name] = true
|
|
names = append(names, name)
|
|
}
|
|
sort.Strings(names)
|
|
return names, nil
|
|
}
|
|
|
|
func panelEventsCall(mgr *plugin.Manager, pluginID, method string, params map[string]any) (any, error) {
|
|
names, err := panelEventNames(params, method == "events.unsubscribe")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if method == "events.subscribe" {
|
|
for _, name := range names {
|
|
permission := panelEventPermissions[name]
|
|
if permission != "" {
|
|
if err := mgr.Broker().Authorize(pluginID, permission); err != nil {
|
|
return nil, err
|
|
}
|
|
} else if err := mgr.Broker().AllowRate(pluginID); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
} else if err := mgr.Broker().AllowRate(pluginID); err != nil {
|
|
return nil, err
|
|
}
|
|
return map[string]any{"events": names}, nil
|
|
}
|