Files
Reims-Go/internal/plugin/builtin/computer_use.go
T

73 lines
3.0 KiB
Go

package builtin
import (
"context"
"time"
"github.com/Nwflower/reims-go/internal/console"
"github.com/Nwflower/reims-go/internal/plugin"
)
// ComputerUseService is the host-owned console surface exposed to in-process
// features and permission-checked Sidecar Host API handlers.
type ComputerUseService interface {
WithVM(ctx context.Context, vmDir string, operation func(context.Context) error) error
Screendump(ctx context.Context, vmDir, path string) (string, error)
ScreendumpDisplay(ctx context.Context, vmDir, path, device string, head int) (string, error)
TypeText(ctx context.Context, vmDir, text string, progress func(done, total int)) (int, error)
Forget(ctx context.Context, vmDir string) error
Close()
SendPasteShortcut(ctx context.Context, vmDir string) error
SendAbsoluteClickAndPaste(ctx context.Context, vmDir string, normX, normY float64, activateClick bool, commandKey string, focusWait time.Duration) error
PointerAvailable(ctx context.Context, vmDir string) (bool, error)
MouseMove(ctx context.Context, vmDir string, x, y, width, height int) error
MouseButton(ctx context.Context, vmDir, button string, down bool) error
MouseButtonAt(ctx context.Context, vmDir string, x, y, width, height int, button string, down bool) error
MouseClick(ctx context.Context, vmDir, button string) error
MouseScroll(ctx context.Context, vmDir string, delta int, horizontal bool) error
PressKey(ctx context.Context, vmDir, key string, modifiers ...string) error
SendKey(ctx context.Context, vmDir, key string, down bool) error
}
func defComputerUse(ver string, desiredFn func(string, bool) bool) plugin.PluginDef {
return plugin.PluginDef{
Manifest: plugin.Manifest{
Schema: 1,
ID: IDComputerUse,
Version: ver,
Name: plugin.LocalizedText{ZhCN: "虚拟机交互", EnUS: "Computer Use"},
Description: plugin.LocalizedText{
ZhCN: "通过宿主控制台串行访问虚拟机屏幕、鼠标和键盘",
EnUS: "Serializes host console access to VM screens, pointers, and keyboards",
},
Publisher: "Reims-Go",
License: "Apache-2.0",
Provides: []plugin.ServiceProvision{{Service: ServiceComputerUse, Version: "1.0.0"}},
Permissions: []string{"vm:screen", "qmp:write"},
DefaultEnabled: true,
},
Source: plugin.SourceBuiltin,
Trust: plugin.TrustOfficial,
Desired: desiredFn(IDComputerUse, true),
Activate: func(_ context.Context, sc *plugin.Scope) error {
manager := console.NewManager()
sc.Provide(ServiceComputerUse, "1.0.0", manager)
sc.OnDispose(manager.Close)
for _, event := range []string{plugin.EventVMBeforeStop, plugin.EventVMDeleted} {
sc.Registry().Hooks().Subscribe(sc, plugin.HookRegistration{
Event: event,
Builtin: true,
Handler: func(ctx context.Context, raw any) (any, error) {
payload, ok := raw.(plugin.VMEventPayload)
if !ok || payload.VMDir == "" {
return nil, nil
}
return nil, manager.Forget(ctx, payload.VMDir)
},
})
}
return nil
},
}
}