| 1 | package browser |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "time" |
| 7 | ) |
| 8 | |
| 9 | // Executor is the host-neutral browser contract. The local Electron shell and |
| 10 | // the remote SSH broker both implement it; the tools in this package are its |
| 11 | // only callers and never learn which host answers. |
| 12 | type Executor interface { |
| 13 | Tabs(ctx context.Context) ([]Tab, error) |
| 14 | Open(ctx context.Context, req OpenRequest) (Tab, error) |
| 15 | Navigate(ctx context.Context, req NavigateRequest) (Tab, error) |
| 16 | Snapshot(ctx context.Context, req SnapshotRequest) (Snapshot, error) |
| 17 | Screenshot(ctx context.Context, req ScreenshotRequest) (Screenshot, error) |
| 18 | Act(ctx context.Context, req ActRequest) (ActResult, error) |
| 19 | Downloads(ctx context.Context, req DownloadsRequest) ([]Download, error) |
| 20 | Close(ctx context.Context, req CloseRequest) error |
| 21 | } |
| 22 | |
| 23 | // Availability is an optional Executor capability: a host whose grant can |
| 24 | // lapse (session switch, connection generation change) reports it here so the |
| 25 | // tools fail closed before any call is dispatched. |
| 26 | type Availability interface { |
| 27 | Available(ctx context.Context) bool |
| 28 | } |
| 29 | |
| 30 | var ( |
| 31 | // ErrStaleReference means the ref or documentToken predates a navigation, |
| 32 | // page replacement, or take-over; the caller must snapshot again. |
| 33 | ErrStaleReference = errors.New("browser: stale reference") |
| 34 | // ErrTakenOver means the user is operating the tab; agent access resumes |
| 35 | // only after a fresh snapshot once the tab leaves human mode. |
| 36 | ErrTakenOver = errors.New("browser: tab taken over by the user") |
| 37 | // ErrNoGrant means no current browser grant covers this task or tab. |
| 38 | ErrNoGrant = errors.New("browser: no browser grant") |
| 39 | // ErrUnknownOutcome means a write's receipt was lost: the action may or |
| 40 | // may not have run and must never be replayed. |
| 41 | ErrUnknownOutcome = errors.New("browser: operation outcome unknown") |
| 42 | ) |
| 43 | |
| 44 | // Navigate actions. |
| 45 | const ( |
| 46 | NavigateURL = "url" |
| 47 | NavigateBack = "back" |
| 48 | NavigateForward = "forward" |
| 49 | NavigateReload = "reload" |
| 50 | ) |
| 51 | |
| 52 | // Act actions. |
| 53 | const ( |
| 54 | ActionClick = "click" |
| 55 | ActionType = "type" |
| 56 | ActionPress = "press" |
| 57 | ActionScroll = "scroll" |
| 58 | ActionSelect = "select" |
| 59 | ActionUpload = "upload" |
| 60 | ) |
| 61 | |
| 62 | // Act outcomes. |
| 63 | const ( |
| 64 | OutcomeExecuted = "executed" |
| 65 | OutcomeNotExecuted = "not_executed" |
| 66 | OutcomeUnknown = "unknown" |
| 67 | ) |
| 68 | |
| 69 | // Tab is one browser tab owned by the current task. |
| 70 | type Tab struct { |
| 71 | ID string |
| 72 | URL string |
| 73 | Title string |
| 74 | Loading bool |
| 75 | Temporary bool |
| 76 | } |
| 77 | |
| 78 | type OpenRequest struct { |
| 79 | OperationID string |
| 80 | URL string |
| 81 | Temporary bool |
| 82 | } |
| 83 | |
| 84 | // NavigateRequest moves a bound tab; URL is consulted only for NavigateURL. |
| 85 | type NavigateRequest struct { |
| 86 | OperationID string |
| 87 | TabID string |
| 88 | URL string |
| 89 | Action string |
| 90 | } |
| 91 | |
| 92 | type CloseRequest struct { |
| 93 | OperationID string |
| 94 | TabID string |
| 95 | } |
| 96 | |
| 97 | type SnapshotRequest struct { |
| 98 | TabID string |
| 99 | Selector string |
| 100 | } |
| 101 | |
| 102 | // Snapshot is the accessibility-style tree of one document version; |
| 103 | // DocumentToken binds every ref in Tree to that version. |
| 104 | type Snapshot struct { |
| 105 | DocumentToken string |
| 106 | URL string |
| 107 | Title string |
| 108 | Tree string |
| 109 | Refs int |
| 110 | } |
| 111 | |
| 112 | type ScreenshotRequest struct { |
| 113 | TabID string |
| 114 | Ref string |
| 115 | FullPage bool |
| 116 | } |
| 117 | |
| 118 | // Screenshot names the task-owned file the host wrote; image bytes never |
| 119 | // travel through control frames. |
| 120 | type Screenshot struct { |
| 121 | Path string |
| 122 | MIME string |
| 123 | Width int |
| 124 | Height int |
| 125 | } |
| 126 | |
| 127 | // ActRequest is one reserved write. OperationID is minted by the model, |
| 128 | // unique per attempt, and rejected forever once used. |
| 129 | type ActRequest struct { |
| 130 | OperationID string |
| 131 | TabID string |
| 132 | DocumentToken string |
| 133 | Action string |
| 134 | Ref string |
| 135 | Text string |
| 136 | Keys string |
| 137 | Options []string |
| 138 | Files []string |
| 139 | Submit bool |
| 140 | DeltaX int |
| 141 | DeltaY int |
| 142 | } |
| 143 | |
| 144 | // ActResult is the host's receipt; Reason explains a not_executed Outcome. |
| 145 | type ActResult struct { |
| 146 | Executed bool |
| 147 | Reason string |
| 148 | DocumentToken string |
| 149 | Outcome string |
| 150 | } |
| 151 | |
| 152 | type DownloadsRequest struct { |
| 153 | TabID string |
| 154 | WaitFor time.Duration |
| 155 | } |
| 156 | |
| 157 | type Download struct { |
| 158 | ID string |
| 159 | URL string |
| 160 | Path string |
| 161 | State string |
| 162 | Bytes int64 |
| 163 | } |
| 164 |