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>
163 lines
4.8 KiB
Go
163 lines
4.8 KiB
Go
package app
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/Nwflower/reims-go/internal/plugin"
|
|
)
|
|
|
|
type panelFormRequest struct {
|
|
Title string `json:"title"`
|
|
Message string `json:"message,omitempty"`
|
|
Schema plugin.SettingsSchema `json:"schema"`
|
|
Values map[string]any `json:"values"`
|
|
ConfirmLabel string `json:"confirmLabel,omitempty"`
|
|
CancelLabel string `json:"cancelLabel,omitempty"`
|
|
Danger bool `json:"danger,omitempty"`
|
|
}
|
|
|
|
func panelFormPayload(params map[string]any) (panelFormRequest, error) {
|
|
req := panelFormRequest{Values: map[string]any{}}
|
|
var err error
|
|
if req.Title, err = panelTextParam(params, "title", true, 200, false); err != nil {
|
|
return req, err
|
|
}
|
|
if req.Message, err = panelTextParam(params, "message", false, 4000, true); err != nil {
|
|
return req, err
|
|
}
|
|
if req.ConfirmLabel, err = panelTextParam(params, "confirmLabel", false, 80, false); err != nil {
|
|
return req, err
|
|
}
|
|
if req.ConfirmLabel == "" {
|
|
if req.ConfirmLabel, err = panelTextParam(params, "submitLabel", false, 80, false); err != nil {
|
|
return req, err
|
|
}
|
|
}
|
|
if req.CancelLabel, err = panelTextParam(params, "cancelLabel", false, 80, false); err != nil {
|
|
return req, err
|
|
}
|
|
if rawDanger, ok := params["danger"]; ok && rawDanger != nil {
|
|
danger, ok := rawDanger.(bool)
|
|
if !ok {
|
|
return req, errors.New("invalid danger")
|
|
}
|
|
req.Danger = danger
|
|
}
|
|
rawSchema, ok := params["schema"]
|
|
if !ok {
|
|
return req, errors.New("schema is required")
|
|
}
|
|
schemaJSON, err := json.Marshal(rawSchema)
|
|
if err != nil {
|
|
return req, err
|
|
}
|
|
if err := json.Unmarshal(schemaJSON, &req.Schema); err != nil {
|
|
return req, fmt.Errorf("invalid form schema: %w", err)
|
|
}
|
|
if len(req.Schema.Properties) == 0 || len(req.Schema.Properties) > 32 {
|
|
return req, errors.New("form schema must contain between 1 and 32 properties")
|
|
}
|
|
for key, prop := range req.Schema.Properties {
|
|
if key == "" || len(key) > 128 {
|
|
return req, fmt.Errorf("invalid form field name")
|
|
}
|
|
switch prop.Type {
|
|
case "boolean", "string", "number", "integer":
|
|
default:
|
|
return req, fmt.Errorf("form field %q has unsupported type %q", key, prop.Type)
|
|
}
|
|
if prop.Default != nil {
|
|
if err := panelValidateSettingValue(prop, prop.Default); err != nil {
|
|
return req, fmt.Errorf("form field %q default: %w", key, err)
|
|
}
|
|
}
|
|
}
|
|
if rawValues, exists := params["values"]; exists && rawValues != nil {
|
|
values, ok := rawValues.(map[string]any)
|
|
if !ok {
|
|
return req, errors.New("form values must be an object")
|
|
}
|
|
req.Values = values
|
|
}
|
|
for key, value := range req.Values {
|
|
prop, ok := req.Schema.Properties[key]
|
|
if !ok {
|
|
return req, fmt.Errorf("unknown form field %q", key)
|
|
}
|
|
if err := panelValidateSettingValue(prop, value); err != nil {
|
|
return req, fmt.Errorf("form field %q: %w", key, err)
|
|
}
|
|
}
|
|
return req, nil
|
|
}
|
|
|
|
func panelUICall(mgr *plugin.Manager, pluginID, method string, params map[string]any) (any, error) {
|
|
if err := mgr.Broker().Authorize(pluginID, "ui:slot"); err != nil {
|
|
return nil, err
|
|
}
|
|
switch method {
|
|
case "ui.showForm":
|
|
return panelFormPayload(params)
|
|
case "ui.setTitle":
|
|
title, err := panelTextParam(params, "title", true, 200, false)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return map[string]any{"title": title}, nil
|
|
case "ui.setBadge":
|
|
if _, ok := params["text"]; !ok {
|
|
return nil, errors.New("text is required")
|
|
}
|
|
text, err := panelTextParam(params, "text", false, 80, false)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
tone, err := panelTextParam(params, "tone", false, 20, false)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
switch tone {
|
|
case "", "neutral", "ok", "warn", "error", "busy":
|
|
default:
|
|
return nil, fmt.Errorf("invalid badge tone %q", tone)
|
|
}
|
|
return map[string]any{"text": text, "tone": tone}, nil
|
|
case "ui.openView":
|
|
viewID, err := panelTextParam(params, "view", true, 200, false)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
view, ok := mgr.Registry().View(pluginID, viewID)
|
|
if !ok {
|
|
return nil, fmt.Errorf("plugin %q has no active view %q", pluginID, viewID)
|
|
}
|
|
return view, nil
|
|
case "ui.close":
|
|
return map[string]any{"ok": true}, nil
|
|
case "ui.navigate":
|
|
page, err := panelTextParam(params, "page", false, 40, false)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
vm, err := panelTextParam(params, "vm", false, 200, false)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if (page == "") == (vm == "") {
|
|
return nil, errors.New("exactly one of page or vm is required")
|
|
}
|
|
if page != "" {
|
|
switch page {
|
|
case "plugins", "settings", "images", "host", "about":
|
|
default:
|
|
return nil, fmt.Errorf("unknown page %q", page)
|
|
}
|
|
}
|
|
return map[string]any{"page": page, "vm": vm}, nil
|
|
default:
|
|
return nil, fmt.Errorf("unsupported plugin UI method %q", method)
|
|
}
|
|
}
|