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>
173 lines
4.6 KiB
Go
173 lines
4.6 KiB
Go
package plugin
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
"unicode"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
const (
|
|
viewStorageVersion = 1
|
|
viewStorageFileName = "view-storage.json"
|
|
maxViewStorageKeys = 256
|
|
maxViewStorageKeyBytes = 128
|
|
maxViewStorageValueBytes = 256 << 10
|
|
maxViewStorageFileBytes = 1 << 20
|
|
)
|
|
|
|
type viewStorageFile struct {
|
|
Version int `json:"version"`
|
|
Values map[string]json.RawMessage `json:"values"`
|
|
}
|
|
|
|
func validateViewStorageKey(key string) error {
|
|
if key == "" || key != strings.TrimSpace(key) || !utf8.ValidString(key) || len(key) > maxViewStorageKeyBytes {
|
|
return fmt.Errorf("invalid storage key")
|
|
}
|
|
for _, r := range key {
|
|
if unicode.IsControl(r) {
|
|
return fmt.Errorf("invalid storage key")
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m *Manager) viewStoragePath(pluginID string) string {
|
|
return filepath.Join(m.layout.DataDir(pluginID), viewStorageFileName)
|
|
}
|
|
|
|
func (m *Manager) loadViewStorage(pluginID string) (*viewStorageFile, error) {
|
|
st := &viewStorageFile{Version: viewStorageVersion, Values: make(map[string]json.RawMessage)}
|
|
raw, err := os.ReadFile(m.viewStoragePath(pluginID))
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
return st, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(raw) > maxViewStorageFileBytes {
|
|
return nil, fmt.Errorf("plugin view storage exceeds %d bytes", maxViewStorageFileBytes)
|
|
}
|
|
if err := json.Unmarshal(raw, st); err != nil {
|
|
return nil, fmt.Errorf("decode plugin view storage: %w", err)
|
|
}
|
|
if st.Version != viewStorageVersion {
|
|
return nil, fmt.Errorf("unsupported plugin view storage version %d", st.Version)
|
|
}
|
|
if st.Values == nil {
|
|
st.Values = make(map[string]json.RawMessage)
|
|
}
|
|
return st, nil
|
|
}
|
|
|
|
func (m *Manager) saveViewStorage(pluginID string, st *viewStorageFile) error {
|
|
raw, err := json.MarshalIndent(st, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
raw = append(raw, '\n')
|
|
if len(raw) > maxViewStorageFileBytes {
|
|
return fmt.Errorf("plugin view storage exceeds %d bytes", maxViewStorageFileBytes)
|
|
}
|
|
dir := m.layout.DataDir(pluginID)
|
|
if err := os.MkdirAll(dir, 0o700); err != nil {
|
|
return err
|
|
}
|
|
target := m.viewStoragePath(pluginID)
|
|
tmp := target + ".tmp"
|
|
if err := os.WriteFile(tmp, raw, 0o600); err != nil {
|
|
return err
|
|
}
|
|
if err := os.Rename(tmp, target); err != nil {
|
|
_ = os.Remove(tmp)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ViewStorageGet returns one value from a plugin's persistent private view store.
|
|
func (m *Manager) ViewStorageGet(pluginID, key string) (json.RawMessage, bool, error) {
|
|
if err := validateViewStorageKey(key); err != nil {
|
|
return nil, false, err
|
|
}
|
|
m.viewStorageMu.Lock()
|
|
defer m.viewStorageMu.Unlock()
|
|
st, err := m.loadViewStorage(pluginID)
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
value, ok := st.Values[key]
|
|
if !ok {
|
|
return nil, false, nil
|
|
}
|
|
return append(json.RawMessage(nil), value...), true, nil
|
|
}
|
|
|
|
// ViewStorageSet persists one JSON value in a plugin's private view store.
|
|
func (m *Manager) ViewStorageSet(pluginID, key string, value any) error {
|
|
if err := validateViewStorageKey(key); err != nil {
|
|
return err
|
|
}
|
|
raw, err := json.Marshal(value)
|
|
if err != nil {
|
|
return fmt.Errorf("encode storage value: %w", err)
|
|
}
|
|
if len(raw) > maxViewStorageValueBytes {
|
|
return fmt.Errorf("storage value exceeds %d bytes", maxViewStorageValueBytes)
|
|
}
|
|
m.viewStorageMu.Lock()
|
|
defer m.viewStorageMu.Unlock()
|
|
st, err := m.loadViewStorage(pluginID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if _, exists := st.Values[key]; !exists && len(st.Values) >= maxViewStorageKeys {
|
|
return fmt.Errorf("plugin view storage exceeds %d keys", maxViewStorageKeys)
|
|
}
|
|
st.Values[key] = append(json.RawMessage(nil), raw...)
|
|
return m.saveViewStorage(pluginID, st)
|
|
}
|
|
|
|
// ViewStorageList returns sorted keys from a plugin's private view store.
|
|
func (m *Manager) ViewStorageList(pluginID string) ([]string, error) {
|
|
m.viewStorageMu.Lock()
|
|
defer m.viewStorageMu.Unlock()
|
|
st, err := m.loadViewStorage(pluginID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
keys := make([]string, 0, len(st.Values))
|
|
for key := range st.Values {
|
|
keys = append(keys, key)
|
|
}
|
|
sort.Strings(keys)
|
|
return keys, nil
|
|
}
|
|
|
|
// ViewStorageDelete removes one key and reports whether it existed.
|
|
func (m *Manager) ViewStorageDelete(pluginID, key string) (bool, error) {
|
|
if err := validateViewStorageKey(key); err != nil {
|
|
return false, err
|
|
}
|
|
m.viewStorageMu.Lock()
|
|
defer m.viewStorageMu.Unlock()
|
|
st, err := m.loadViewStorage(pluginID)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if _, ok := st.Values[key]; !ok {
|
|
return false, nil
|
|
}
|
|
delete(st.Values, key)
|
|
if err := m.saveViewStorage(pluginID, st); err != nil {
|
|
return false, err
|
|
}
|
|
return true, nil
|
|
}
|