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
- Variables
- func DoneParameters() map[string]any
- func HasNative(providerType llm.ProviderType) bool
- func PixelsToNotches(pixels int) int
- func RegisterNative(providerType llm.ProviderType, factory NativeFactory)
- func ScrollDelta(direction string, amount int) (x, y int, err error)
- func SplitKeys(combo string) []string
- type Action
- type Done
- type ImageLimit
- type Kind
- type Mode
- type NativeFactory
- type Observation
- type Options
- type Point
- type Rect
- type Result
- type Screen
- type Session
- type Turn
Constants ¶
const ( ButtonLeft = "left" ButtonRight = "right" ButtonMiddle = "middle" )
Pointer buttons.
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.
const SkippedText = "Not executed: an earlier action in this turn failed."
SkippedText is how a skipped action is reported to a model.
Variables ¶
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 ¶
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 ¶
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 ¶
ScrollDelta turns a direction and an amount into scroll deltas.
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"`
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 ImageLimit ¶
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" )
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 ¶
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
}
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 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.
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.
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.