computeruse

package
v2.25.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 4, 2026 License: GPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

Package computeruse lets a model operate a desktop through screenshots and pointer and keyboard actions.

A Session covers one task. The caller sends the current screen, the model answers with actions, the caller performs them and reports the results with the next screen, until the model reports the task done:

caller                              session (model)
  |-- Observation{Screen} ----------->|
  |<--------- Turn{Actions} ----------|
  |   perform actions in order        |
  |-- Observation{Results, Screen} -->|
  |<--------- Turn{Done} -------------|

Coordinates always refer to the pixel space of the screenshots the session was sent.

Index

Constants

View Source
const (
	ButtonLeft   = "left"
	ButtonRight  = "right"
	ButtonMiddle = "middle"
)

Pointer buttons.

View Source
const (
	DoneToolName        = "done"
	DoneToolDescription = "Report that the task is finished or cannot be finished."
	// DoneInstruction tells the model how to end a task.
	DoneInstruction = "When the task is complete, or cannot be completed, call done with success set accordingly and a short summary."
)

The done tool is how a model reports the task finished, in every mode.

View Source
const SkippedText = "Not executed: an earlier action in this turn failed."

SkippedText is how a skipped action is reported to a model.

Variables

View Source
var ErrModelNotSupported = errors.New("the model does not support the provider's native computer use")

ErrModelNotSupported reports a model that the provider's native computer-use tool does not support. In ModeAuto, New then uses the generic session.

Functions

func DoneParameters

func DoneParameters() map[string]any

DoneParameters returns the JSON schema of the done tool's arguments.

func HasNative

func HasNative(providerType llm.ProviderType) bool

HasNative reports whether a provider type has a native session factory.

func PixelsToNotches

func PixelsToNotches(pixels int) int

PixelsToNotches converts a scroll distance in pixels to wheel notches, keeping any nonzero distance at least one notch.

func RegisterNative

func RegisterNative(providerType llm.ProviderType, factory NativeFactory)

RegisterNative registers the native session factory of a provider type.

func ScrollDelta

func ScrollDelta(direction string, amount int) (x, y int, err error)

ScrollDelta turns a direction and an amount into scroll deltas.

func SplitKeys

func SplitKeys(combo string) []string

SplitKeys splits a key combination such as "ctrl+shift+t" into key names. A trailing "+" is the plus key itself, as in "ctrl++".

Types

type Action

type Action struct {
	// CallID identifies the model call the action belongs to; the result
	// of the action carries the same ID.
	CallID string `json:"call_id,omitempty"`
	Kind   Kind   `json:"kind"`
	Point  *Point `json:"point,omitempty"`
	// ElementID names an element from the observation to act on. The
	// executor resolves it to a point before the action runs.
	ElementID string        `json:"element_id,omitempty"`
	Path      []Point       `json:"path,omitempty"`
	Button    string        `json:"button,omitempty"`
	Count     int           `json:"count,omitempty"`
	Modifiers []string      `json:"modifiers,omitempty"`
	Text      string        `json:"text,omitempty"`
	Keys      []string      `json:"keys,omitempty"`
	Repeat    int           `json:"repeat,omitempty"`
	ScrollX   int           `json:"scroll_x,omitempty"`
	ScrollY   int           `json:"scroll_y,omitempty"`
	Duration  time.Duration `json:"duration,omitempty"`
	Region    *Rect         `json:"region,omitempty"`
}

Action is one thing the model asks the caller to do. Key names are the model's own spelling, such as "Return", "ctrl" or "PageDown".

type Done

type Done struct {
	Success bool   `json:"success"`
	Summary string `json:"summary"`
}

Done is the model's report that the task finished.

func ParseDone

func ParseDone(arguments []byte) (*Done, error)

ParseDone decodes the arguments of a done tool call.

type Element

type Element struct {
	ID   string
	Role string
	Name string
	// Value is what the element holds, such as a field's text or the option
	// a combo box shows, with the values the step types written as their
	// placeholders.
	Value string
	// Hint is the text an empty field shows in place of a value, such as a
	// grey "0", which the field does not hold. Empty when the field holds a
	// value or the desktop does not report one.
	Hint string
}

Element is an actionable control the model can target by id, resolved to its position by the executor.

type ImageLimit

type ImageLimit struct {
	LongEdge  int
	MaxPixels int
}

ImageLimit is the largest screenshot a session accepts. Callers scale screenshots down to fit before sending them.

type Kind

type Kind string

Kind names a desktop action.

const (
	// KindClick clicks Button Count times at Point, or at the cursor when
	// Point is nil, holding Modifiers.
	KindClick Kind = "click"
	// KindMove moves the pointer to Point.
	KindMove Kind = "move"
	// KindDrag presses the left button at the first point of Path, moves
	// through the rest and releases at the last, holding Modifiers.
	KindDrag Kind = "drag"
	// KindMouseDown presses Button at the cursor.
	KindMouseDown Kind = "mouse_down"
	// KindMouseUp releases Button at the cursor.
	KindMouseUp Kind = "mouse_up"
	// KindScroll scrolls ScrollX and ScrollY wheel notches at Point, or at
	// the cursor when Point is nil. Positive values scroll right and down.
	KindScroll Kind = "scroll"
	// KindType types Text at the keyboard focus.
	KindType Kind = "type"
	// KindKey presses Keys together Repeat times.
	KindKey Kind = "key"
	// KindHoldKey holds Keys down for Duration.
	KindHoldKey Kind = "hold_key"
	// KindWait pauses for Duration.
	KindWait Kind = "wait"
	// KindScreenshot reports the current screen.
	KindScreenshot Kind = "screenshot"
	// KindZoom reports Region of the screen at a higher resolution.
	KindZoom Kind = "zoom"
	// KindCursorPosition reports the pointer position.
	KindCursorPosition Kind = "cursor_position"
	// KindChoose sets the combo box or pop-up ElementID names to the option
	// Text, through the control itself.
	KindChoose Kind = "choose"
)

type Mode

type Mode string

Mode chooses how a session talks to the model.

const (
	// ModeAuto uses the provider's native computer-use tool when it has one.
	ModeAuto Mode = "auto"
	// ModeNative requires the provider's native computer-use tool.
	ModeNative Mode = "native"
	// ModeGeneric uses plain function tools, which works with any
	// tool-calling model that accepts images.
	ModeGeneric Mode = "generic"
)

type NativeFactory

type NativeFactory func(provider llm.Provider, opts Options) (Session, error)

NativeFactory starts a session with a provider's native computer-use tool. provider is the value llm.NewProvider returned for the type the factory was registered for.

type Observation

type Observation struct {
	// Screen is the current screen.
	Screen Screen
	// Results answer the previous turn's actions, one per action, in order.
	Results []Result
	// Acknowledged approves the previous turn's Confirmation.
	Acknowledged bool
	// Note is an instruction for the model, such as a reminder to report
	// the task done.
	Note string
	// ReportOnly asks the model for its report instead of more actions. A
	// session that can limit the model's tools offers it only done.
	ReportOnly bool
	// Elements are the actionable elements of the front window the model
	// may target by id, more reliable than aiming at a pixel. Empty when
	// the desktop exposes no elements.
	Elements []Element
}

Observation reports the desktop to the model.

type Options

type Options struct {
	// Model is the model name.
	Model string
	// Task is what the model should accomplish.
	Task string
	// System adds instructions about the environment, such as the
	// operating system.
	System string
	// MaxTokens caps each model response.
	MaxTokens *int
	// Temperature applies to models that accept it.
	Temperature *float64
}

Options configure a session.

type Point

type Point struct {
	X int `json:"x"`
	Y int `json:"y"`
}

Point is a screenshot pixel position.

type Rect

type Rect struct {
	Min Point `json:"min"`
	Max Point `json:"max"`
}

Rect is a screenshot region from its top-left to its bottom-right corner.

type Result

type Result struct {
	CallID string
	// Output is text the action produced, such as a cursor position.
	Output string
	// Image is the picture a screenshot or zoom action produced.
	Image *llm.Image
	// Error describes why the action failed.
	Error string
	// Skipped marks an action not performed because an earlier one failed.
	Skipped bool
}

Result reports how one action went.

func (Result) Failed

func (r Result) Failed() bool

Failed reports whether the action did not complete.

func (Result) Text

func (r Result) Text() string

Text returns the result as the text a model is sent.

type Screen

type Screen struct {
	Image  llm.Image
	Width  int
	Height int
}

Screen is a screenshot and its pixel size.

type Session

type Session interface {
	// ImageLimit reports the largest screenshot the session accepts.
	ImageLimit() ImageLimit
	// Next sends an observation and returns the model's next turn.
	Next(ctx context.Context, obs Observation) (*Turn, error)
}

Session drives one task.

func New

func New(providerType llm.ProviderType, provider llm.Provider, mode Mode, opts Options) (Session, error)

New starts a session for a provider of the given type.

type Turn

type Turn struct {
	// Actions are performed in order, stopping at the first failure.
	Actions []Action
	// Done is set when the model finished the task; any Actions of the
	// same turn are performed first.
	Done *Done
	// Confirmation, when set, is the model provider's explanation of why
	// Actions need a person's approval before they run.
	Confirmation string
	// Text is what the model wrote alongside its actions.
	Text string
	// Usage counts the tokens of this turn.
	Usage llm.Usage
}

Turn is the model's answer to an observation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL