- Each plugin connect connection gets a session id that is forwarded with channel/<id>/message; when the connection ends the host notifies the sidecar with channel/<id>/closed so it can cancel calls and release input. - host/console.screenshot crops a region from the full-resolution frame, optionally measured in a reference image size, and full frames now honor max_width up to 2560. - host/console.key can hold or release one key, and host/console.release lifts keys and pointer buttons in one call. Driver consoles stay press-only. - host/vm.shutdown accepts force, and host/vm.snapshot.list needs only vm:read. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
250 lines
7.9 KiB
Go
250 lines
7.9 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/Nwflower/reims-go/internal/console"
|
|
"github.com/Nwflower/reims-go/internal/driver"
|
|
)
|
|
|
|
const (
|
|
consoleRouteQMP = "qmp"
|
|
consoleRouteDriver = "driver"
|
|
)
|
|
|
|
// consoleRoute says how the host reaches one VM's screen and input: through
|
|
// its own QMP console, or through the console its driver serves.
|
|
type consoleRoute struct {
|
|
kind string
|
|
vm driver.VMInfo
|
|
console driver.ConsoleDriver // set for consoleRouteDriver
|
|
}
|
|
|
|
// driverFor returns the registered driver that owns vm, or nil.
|
|
func (a *App) driverFor(vm driver.VMInfo) driver.Driver {
|
|
id := driver.CanonicalDriverID(vm.DriverID)
|
|
if id == "" {
|
|
id = driver.BuiltinReimsMacOSDriverID
|
|
}
|
|
a.mu.Lock()
|
|
router := a.driverRouter
|
|
a.mu.Unlock()
|
|
if router == nil {
|
|
return nil
|
|
}
|
|
drv, err := router.DriverByID(id)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
return drv
|
|
}
|
|
|
|
// resolveConsoleRoute picks the console of vm. A driver that declares
|
|
// `consoleEndpoint: "driver"` and the `console` capability serves its own;
|
|
// every other VM needs a QMP console, which a hosted VM does not have.
|
|
func (a *App) resolveConsoleRoute(vm driver.VMInfo) (consoleRoute, error) {
|
|
if cd, ok := driver.DriverServesConsole(a.driverFor(vm)); ok && driver.HasCapability(vm.Capabilities, driver.CapConsole) {
|
|
return consoleRoute{kind: consoleRouteDriver, vm: vm, console: cd}, nil
|
|
}
|
|
if !driver.IsHosted(vm.Capabilities) && driver.SupportsCapability(vm.Capabilities, driver.CapQMP) {
|
|
return consoleRoute{kind: consoleRouteQMP, vm: vm}, nil
|
|
}
|
|
return consoleRoute{}, fmt.Errorf("computer_use_unavailable: driver %q has no console the host can reach", vm.DriverID)
|
|
}
|
|
|
|
// consoleCapture writes one full frame of the guest screen to a fresh temp
|
|
// file and returns its path and format. cleanup removes the file. External
|
|
// agent calls use this so disabling builtin.computer-use blocks QMP access.
|
|
func (a *App) consoleCapture(ctx context.Context, route consoleRoute, maxEdge int) (path, format string, cleanup func(), err error) {
|
|
return a.consoleCaptureWithMode(ctx, route, maxEdge, false)
|
|
}
|
|
|
|
// consoleCaptureInternal captures a frame for desktop-owned features
|
|
// (thumbnails and screenshot copy) using the internal QMP fallback when
|
|
// builtin.computer-use is disabled.
|
|
func (a *App) consoleCaptureInternal(ctx context.Context, route consoleRoute, maxEdge int) (path, format string, cleanup func(), err error) {
|
|
return a.consoleCaptureWithMode(ctx, route, maxEdge, true)
|
|
}
|
|
|
|
func (a *App) consoleCaptureWithMode(ctx context.Context, route consoleRoute, maxEdge int, internal bool) (path, format string, cleanup func(), err error) {
|
|
a.mu.Lock()
|
|
if a.cfg == nil {
|
|
a.mu.Unlock()
|
|
return "", "", nil, errors.New("application configuration is unavailable")
|
|
}
|
|
tempDir := a.cfg.TempDir()
|
|
a.mu.Unlock()
|
|
if err := os.MkdirAll(tempDir, 0o755); err != nil {
|
|
return "", "", nil, err
|
|
}
|
|
file, err := os.CreateTemp(tempDir, "console-*.frame")
|
|
if err != nil {
|
|
return "", "", nil, err
|
|
}
|
|
path = file.Name()
|
|
if err := file.Close(); err != nil {
|
|
_ = os.Remove(path)
|
|
return "", "", nil, err
|
|
}
|
|
cleanup = func() { _ = os.Remove(path) }
|
|
switch route.kind {
|
|
case consoleRouteDriver:
|
|
res, err := route.console.ConsoleScreenshot(ctx, route.vm.Name, driver.ConsoleScreenshotRequest{Path: path, MaxEdge: maxEdge})
|
|
if err != nil {
|
|
cleanup()
|
|
return "", "", nil, err
|
|
}
|
|
if res.Path != "" && !samePath(res.Path, path) {
|
|
cleanup()
|
|
return "", "", nil, fmt.Errorf("driver %q did not write its frame to the requested path", route.vm.DriverID)
|
|
}
|
|
format = strings.ToLower(strings.TrimSpace(res.Format))
|
|
switch format {
|
|
case "":
|
|
format = "png"
|
|
case "png", "ppm":
|
|
default:
|
|
cleanup()
|
|
return "", "", nil, fmt.Errorf("driver %q returned an unsupported frame format %q", route.vm.DriverID, res.Format)
|
|
}
|
|
if st, err := os.Lstat(path); err != nil || !st.Mode().IsRegular() || st.Size() == 0 {
|
|
cleanup()
|
|
return "", "", nil, fmt.Errorf("driver %q wrote no frame", route.vm.DriverID)
|
|
}
|
|
return path, format, cleanup, nil
|
|
default:
|
|
service := a.computerUseInternalService()
|
|
if !internal {
|
|
var svcErr error
|
|
service, svcErr = a.computerUseService()
|
|
if svcErr != nil {
|
|
cleanup()
|
|
return "", "", nil, svcErr
|
|
}
|
|
}
|
|
if route.vm.Directory == "" {
|
|
cleanup()
|
|
return "", "", nil, fmt.Errorf("VM %q has no console directory", route.vm.Name)
|
|
}
|
|
format, err = service.Screendump(ctx, route.vm.Directory, path)
|
|
if err != nil {
|
|
cleanup()
|
|
return "", "", nil, err
|
|
}
|
|
return path, format, cleanup, nil
|
|
}
|
|
}
|
|
|
|
// consolePointer runs one pointer action on the VM's console.
|
|
func (a *App) consolePointer(ctx context.Context, route consoleRoute, req driver.ConsolePointerRequest) error {
|
|
if route.kind == consoleRouteDriver {
|
|
return route.console.ConsolePointer(ctx, route.vm.Name, req)
|
|
}
|
|
service, err := a.computerUseService()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
dir := route.vm.Directory
|
|
switch strings.ToLower(strings.TrimSpace(req.Action)) {
|
|
case "move":
|
|
available, err := service.PointerAvailable(ctx, dir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !available {
|
|
return errors.New("VM has no active absolute pointer")
|
|
}
|
|
return service.MouseMove(ctx, dir, req.X, req.Y, req.Width, req.Height)
|
|
case "button":
|
|
if req.Down == nil {
|
|
return errors.New("button action requires down")
|
|
}
|
|
return service.MouseButton(ctx, dir, req.Button, *req.Down)
|
|
case "click":
|
|
return service.MouseClick(ctx, dir, req.Button)
|
|
case "scroll":
|
|
return service.MouseScroll(ctx, dir, req.Delta, req.Horizontal)
|
|
}
|
|
return fmt.Errorf("unsupported pointer action %q", req.Action)
|
|
}
|
|
|
|
// consoleKeyState holds or releases one key. Only the host's QMP console can
|
|
// hold a key; a driver console exposes presses alone.
|
|
func (a *App) consoleKeyState(ctx context.Context, route consoleRoute, key string, down bool) error {
|
|
if route.kind == consoleRouteDriver {
|
|
return fmt.Errorf("computer_use_unsupported: driver %q cannot hold a key", route.vm.DriverID)
|
|
}
|
|
code, err := console.NormalizeHeldKey(key)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
service, err := a.computerUseService()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return service.SendKey(ctx, route.vm.Directory, code, down)
|
|
}
|
|
|
|
// consoleRelease lifts held keys and pointer buttons on the VM's console.
|
|
func (a *App) consoleRelease(ctx context.Context, route consoleRoute, keys, buttons []string) error {
|
|
buttons, err := normalizeReleaseButtons(buttons)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if route.kind == consoleRouteDriver {
|
|
// A driver console cannot hold keys, so only its buttons need lifting.
|
|
up := false
|
|
for _, button := range buttons {
|
|
if err := route.console.ConsolePointer(ctx, route.vm.Name, driver.ConsolePointerRequest{Action: "button", Button: button, Down: &up}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
codes := make([]string, 0, len(keys))
|
|
for _, key := range keys {
|
|
code, err := console.NormalizeHeldKey(key)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
codes = append(codes, code)
|
|
}
|
|
if len(codes)+len(buttons) == 0 {
|
|
return nil
|
|
}
|
|
service, err := a.computerUseService()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return service.ReleaseInputs(ctx, route.vm.Directory, codes, buttons)
|
|
}
|
|
|
|
func normalizeReleaseButtons(buttons []string) ([]string, error) {
|
|
out := make([]string, 0, len(buttons))
|
|
for _, button := range buttons {
|
|
switch name := strings.ToLower(strings.TrimSpace(button)); name {
|
|
case "left", "right", "middle":
|
|
out = append(out, name)
|
|
default:
|
|
return nil, fmt.Errorf("unsupported pointer button %q", button)
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// consoleKey presses one key on the VM's console.
|
|
func (a *App) consoleKey(ctx context.Context, route consoleRoute, key string, modifiers []string) error {
|
|
if route.kind == consoleRouteDriver {
|
|
return route.console.ConsoleKey(ctx, route.vm.Name, driver.ConsoleKeyRequest{Key: key, Modifiers: modifiers})
|
|
}
|
|
service, err := a.computerUseService()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return service.PressKey(ctx, route.vm.Directory, key, modifiers...)
|
|
}
|