Files
Nwflower d55241cba2 feat(plugin): catalog the Host API, open secret typing and extend the SDK
Give the Sidecar Host API one authoritative catalog in pkg/pluginproto: every
host/* method's status, required permissions, handshake feature and request and
result shapes. The host, pkg/pluginsdk and plugins share those types, so a field
can no longer exist on one side only, and permissions come from the catalog
instead of from each handler. Tests gate that every served method is
catalogued, every stable catalogued method is served, and every catalogued
permission is one the host recognizes.

Open the interface up instead of adding consent gates. Typing a secret setting
is no longer reserved for reims.agent: any plugin that holds qmp:write and that
the user authorized for the VM can have the host type the plugin's own setting,
which the plugin never reads back. Restoring a snapshot works on any authorized
VM, a plugin names its own clones, a held key may carry modifiers, and a
screenshot width outside the supported range is brought into range rather than
refused. Deleting a VM stays limited to the temporary VMs a plugin created.

Record the keys and pointer buttons each plugin holds per VM and lift them when
the plugin's process exits, its scope is disposed, the VM stops or it stops
calling, so a guest never stays stuck with a modifier down. Requests from
plugins that declare pluginApi 1.3 or newer are decoded strictly and answer an
undefined field with JSON-RPC -32602, while older plugins keep tolerant
decoding.

Add typed VM and console methods to pkg/pluginsdk so a plugin no longer needs
its own JSON-RPC client. A method the contract does not catalog, or one the
host did not advertise in the handshake, is refused before it is sent, and the
host's JSON-RPC error code reaches the caller.
2026-12-05 01:43:28 -07:00

80 lines
3.1 KiB
Go

package pluginsdk
import (
"context"
"github.com/Nwflower/reims-go/pkg/pluginproto"
)
// The VM methods a plugin may call on VMs the user authorized it for. Every
// call is refused before it is sent unless the host advertised the method in
// the handshake, so a plugin never depends on a host that cannot serve it.
// ListVMs returns the VMs the host shows this plugin.
func (s *Server) ListVMs(ctx context.Context) ([]pluginproto.VMView, error) {
var res pluginproto.VMListResult
if err := s.callFeature(ctx, "host/vm.list", nil, &res); err != nil {
return nil, err
}
return res.VMs, nil
}
// GetVM reads one VM by name or by canonical driverID:name reference.
func (s *Server) GetVM(ctx context.Context, vm string) (pluginproto.VMView, error) {
var view pluginproto.VMView
err := s.callFeature(ctx, "host/vm.get", pluginproto.VMParams{VM: vm}, &view)
return view, err
}
// StartVM starts a VM.
func (s *Server) StartVM(ctx context.Context, vm string) error {
return s.callFeature(ctx, "host/vm.start", pluginproto.VMParams{VM: vm}, nil)
}
// ShutdownVM shuts a VM down, or stops it outright when force is set.
func (s *Server) ShutdownVM(ctx context.Context, vm string, force bool) error {
return s.callFeature(ctx, "host/vm.shutdown", pluginproto.VMShutdownParams{VM: vm, Force: force}, nil)
}
// SuspendVM suspends a VM.
func (s *Server) SuspendVM(ctx context.Context, vm string) error {
return s.callFeature(ctx, "host/vm.suspend", pluginproto.VMParams{VM: vm}, nil)
}
// ResumeVM resumes a VM.
func (s *Server) ResumeVM(ctx context.Context, vm string) error {
return s.callFeature(ctx, "host/vm.resume", pluginproto.VMParams{VM: vm}, nil)
}
// CloneVM clones a VM into a temporary VM this plugin owns. cloneName must
// start with __agent- so the host can recognize and clean up the clone.
func (s *Server) CloneVM(ctx context.Context, vm, cloneName string) (pluginproto.VMCloneResult, error) {
var res pluginproto.VMCloneResult
err := s.callFeature(ctx, "host/vm.clone", pluginproto.VMCloneParams{VM: vm, CloneName: cloneName}, &res)
return res, err
}
// DeleteVM deletes a temporary VM this plugin owns.
func (s *Server) DeleteVM(ctx context.Context, vm string) error {
return s.callFeature(ctx, "host/vm.delete", pluginproto.VMParams{VM: vm}, nil)
}
// ListSnapshots lists a VM's snapshots.
func (s *Server) ListSnapshots(ctx context.Context, vm string) ([]pluginproto.Snapshot, error) {
var res pluginproto.SnapshotListResult
if err := s.callFeature(ctx, "host/vm.snapshot.list", pluginproto.VMParams{VM: vm}, &res); err != nil {
return nil, err
}
return res.Snapshots, nil
}
// CreateSnapshot creates a snapshot.
func (s *Server) CreateSnapshot(ctx context.Context, vm, name string) error {
return s.callFeature(ctx, "host/vm.snapshot.create", pluginproto.SnapshotNameParams{VM: vm, Name: name}, nil)
}
// RestoreSnapshot restores a snapshot of a temporary VM this plugin owns.
func (s *Server) RestoreSnapshot(ctx context.Context, vm, name string) error {
return s.callFeature(ctx, "host/vm.snapshot.restore", pluginproto.SnapshotNameParams{VM: vm, Name: name}, nil)
}