Files
Reims-Go/internal/app/plugin_bridge_settings.go
T
NwflowerandClaude Opus 5.5 fa3e1e2889 feat: add plugin views and expand the sandboxed page bridge
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>
2026-12-02 23:11:23 -07:00

139 lines
3.9 KiB
Go

package app
import (
"fmt"
"math"
"slices"
"github.com/Nwflower/reims-go/internal/plugin"
)
func panelValidateSettingValue(prop plugin.SettingProperty, value any) error {
switch prop.Type {
case "boolean":
if _, ok := value.(bool); !ok {
return fmt.Errorf("expected boolean")
}
case "string":
text, ok := value.(string)
if !ok {
return fmt.Errorf("expected string")
}
if len(prop.Enum) > 0 && !slices.Contains(prop.Enum, text) {
return fmt.Errorf("value is not in enum")
}
case "number", "integer":
number, ok := value.(float64)
if !ok || math.IsNaN(number) || math.IsInf(number, 0) {
return fmt.Errorf("expected finite number")
}
if prop.Type == "integer" && math.Trunc(number) != number {
return fmt.Errorf("expected integer")
}
if prop.Minimum != nil && number < *prop.Minimum {
return fmt.Errorf("number is below minimum")
}
if prop.Maximum != nil && number > *prop.Maximum {
return fmt.Errorf("number is above maximum")
}
default:
return fmt.Errorf("unsupported setting type %q", prop.Type)
}
return nil
}
func panelSettingScope(schema *plugin.SettingsSchema, key string) string {
if prop, ok := schema.Properties[key]; ok && prop.Scope != "" {
return prop.Scope
}
if schema.Scope != "" {
return schema.Scope
}
return "app"
}
func (a *App) panelSettingsVM(vm string) (string, error) {
if vm == "" {
return "", nil
}
info, err := a.resolveComputerUseVMInfo(vm)
if err != nil {
return "", err
}
return info.Ref().Key(), nil
}
func (a *App) panelSettingsCall(mgr *plugin.Manager, pluginID, method string, params map[string]any) (any, error) {
if err := mgr.Broker().AllowRate(pluginID); err != nil {
return nil, err
}
vm, err := panelTextParam(params, "vm", false, 200, false)
if err != nil {
return nil, err
}
vm, err = a.panelSettingsVM(vm)
if err != nil {
return nil, err
}
switch method {
case "settings.get":
values := mgr.GetSettings(pluginID, vm, true)
key, err := panelTextParam(params, "key", false, 128, false)
if err != nil || key == "" {
return values, err
}
info, ok := mgr.Registry().Info(pluginID)
if !ok || info.Settings == nil {
return nil, fmt.Errorf("plugin %q has no settings schema", pluginID)
}
prop, ok := info.Settings.Properties[key]
if !ok {
return nil, fmt.Errorf("unknown setting %q", key)
}
value := values[key]
configured := mgr.SettingConfigured(pluginID, vm, key)
return map[string]any{"value": value, "configured": configured, "secret": prop.Secret}, nil
case "settings.set":
info, ok := mgr.Registry().Info(pluginID)
if !ok || info.Settings == nil {
return nil, fmt.Errorf("plugin %q has no settings schema", pluginID)
}
values, ok := params["values"].(map[string]any)
if !ok {
key, keyErr := panelTextParam(params, "key", true, 128, false)
if keyErr != nil {
return nil, keyErr
}
values = map[string]any{key: params["value"]}
}
if len(values) == 0 {
return nil, fmt.Errorf("values must be a non-empty object")
}
for key, value := range values {
if key == computerUseAccessSetting {
return nil, fmt.Errorf("setting %q can only be changed by the user", key)
}
prop, ok := info.Settings.Properties[key]
if !ok {
return nil, fmt.Errorf("unknown setting %q", key)
}
scope := panelSettingScope(info.Settings, key)
if (vm == "" && scope == "vm") || (vm != "" && scope == "app") {
return nil, fmt.Errorf("setting %q is not valid at this scope", key)
}
if prop.Required && (value == nil || value == "") {
return nil, fmt.Errorf("setting %q is required", key)
}
if err := panelValidateSettingValue(prop, value); err != nil {
return nil, fmt.Errorf("setting %q: %w", key, err)
}
}
if err := mgr.SetSettings(pluginID, vm, values); err != nil {
return nil, err
}
return map[string]any{"ok": true}, nil
default:
return nil, fmt.Errorf("unsupported plugin settings method %q", method)
}
}