Files
Reims-Go/pkg/pluginsdk/console.go
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

100 lines
4.0 KiB
Go

package pluginsdk
import (
"context"
"github.com/Nwflower/reims-go/pkg/pluginproto"
)
// The console methods a plugin may call on a VM the user authorized it for.
// A key or a pointer button sent with a press stays held in the guest until a
// release arrives, so a caller that holds input must pair it or call Release;
// the host lifts whatever is still held when the plugin's process goes away.
// Screenshot captures the guest screen. Region is measured in the coordinate
// space of the reference frame of size Width by Height, and MaxWidth caps the
// width of the returned image. A Blank frame is a normal state during boot,
// login and display switches, not a failure.
func (s *Server) Screenshot(ctx context.Context, req pluginproto.ScreenshotParams) (pluginproto.ScreenshotResult, error) {
var res pluginproto.ScreenshotResult
err := s.callFeature(ctx, "host/console.screenshot", req, &res)
return res, err
}
// MovePointer moves the pointer to x, y, given in a frame of size width by
// height.
func (s *Server) MovePointer(ctx context.Context, vm string, x, y, width, height int) error {
return s.callFeature(ctx, "host/console.pointer", pluginproto.PointerParams{
VM: vm, Action: "move", X: x, Y: y, Width: width, Height: height,
}, nil)
}
// PointerButton presses or releases one pointer button.
func (s *Server) PointerButton(ctx context.Context, vm, button string, down bool) error {
return s.callFeature(ctx, "host/console.pointer", pluginproto.PointerParams{
VM: vm, Action: "button", Button: button, Down: &down,
}, nil)
}
// Click presses and releases one pointer button.
func (s *Server) Click(ctx context.Context, vm, button string) error {
return s.callFeature(ctx, "host/console.pointer", pluginproto.PointerParams{
VM: vm, Action: "click", Button: button,
}, nil)
}
// Scroll sends wheel steps. A positive delta scrolls down, or right when
// horizontal is set.
func (s *Server) Scroll(ctx context.Context, vm string, delta int, horizontal bool) error {
return s.callFeature(ctx, "host/console.pointer", pluginproto.PointerParams{
VM: vm, Action: "scroll", Delta: delta, Horizontal: horizontal,
}, nil)
}
// Key presses and releases one key with its modifiers held.
func (s *Server) Key(ctx context.Context, vm, key string, modifiers ...string) error {
return s.callFeature(ctx, "host/console.key", pluginproto.KeyParams{
VM: vm, Key: key, Modifiers: modifiers,
}, nil)
}
// KeyDown presses one key and leaves it held in the guest.
func (s *Server) KeyDown(ctx context.Context, vm, key string) error {
down := true
return s.callFeature(ctx, "host/console.key", pluginproto.KeyParams{VM: vm, Key: key, Down: &down}, nil)
}
// KeyUp releases one key this plugin pressed with KeyDown.
func (s *Server) KeyUp(ctx context.Context, vm, key string) error {
down := false
return s.callFeature(ctx, "host/console.key", pluginproto.KeyParams{VM: vm, Key: key, Down: &down}, nil)
}
// Release lifts the keys and pointer buttons this plugin still holds.
func (s *Server) Release(ctx context.Context, vm string, keys, buttons []string) error {
return s.callFeature(ctx, "host/console.release", pluginproto.ReleaseParams{
VM: vm, Keys: keys, Buttons: buttons,
}, nil)
}
// Type types text as US-layout key strokes and returns how many characters
// reached the guest.
func (s *Server) Type(ctx context.Context, vm, text string) (int, error) {
var res pluginproto.CharactersResult
if err := s.callFeature(ctx, "host/console.type", pluginproto.TypeParams{VM: vm, Text: text}, &res); err != nil {
return 0, err
}
return res.Characters, nil
}
// TypeSecret has the host type one of this plugin's own secret settings. The
// plugin never sees the value, and the user must have enabled the plugin's
// secret-typing consent for that VM.
func (s *Server) TypeSecret(ctx context.Context, vm, setting string) (int, error) {
var res pluginproto.CharactersResult
if err := s.callFeature(ctx, "host/console.typeSecret", pluginproto.TypeSecretParams{VM: vm, Setting: setting}, &res); err != nil {
return 0, err
}
return res.Characters, nil
}