Repository navigation
Expand file tree
/
Copy pathmachine.ts
More file actions
731 lines (617 loc) · 25.8 KB
/
Copy pathmachine.ts
File metadata and controls
731 lines (617 loc) · 25.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
export * as BrowserMachine from "./machine.js"
import type { Permission } from "@opencode/schema/permission"
import type { Session } from "@opencode/schema/session"
import { Tool } from "@opencode/schema/tool"
import type { Browser } from "./rpc.js"
import { BrowserSkill } from "./skill.js"
import { Wildcard } from "./wildcard.js"
/** How long one permission sync (`session.get` and, when needed, `session.update`) may take. The desktop waits 15 s. */
export const PERMISSION_SYNC_TIMEOUT_MS = 10_000
/** How long a dispatched browser command may run before it times out and is cancelled on the desktop. */
export const REQUEST_TIMEOUT_MS = 60_000
/** The browser tools and their skill, which an attachment allows together. */
export const ACCESS = [
{ action: "browser", resource: "*" },
{ action: "skill", resource: BrowserSkill.id },
] as const
/** The one `session.permissions` sync in flight for a session: grant (`allow`) or revoke the browser rules. */
export type PermissionSync = { readonly attempt: number; readonly allow: boolean }
/**
* One session's attachment. A session absent from `sessions` has no attachment and no sync in flight.
*
* - `attaching`: the desktop called `attach` as `attempt`. Its grant runs after any revoke in flight (`sync`); until it
* succeeds, no `attached` handshake is sent and no command is dispatched.
* - `attached`: the session allows the browser and the desktop received `attached`; commands run.
* - `revoking`: no attachment; `sync` settles, then a revoke follows unless the session was `deleted`.
*/
export type SessionState =
| {
readonly status: "attaching"
readonly attempt: number
readonly connectionID: string
readonly sync: PermissionSync
}
| {
readonly status: "attached"
readonly attempt: number
readonly connectionID: string
readonly browserState: Browser.State
readonly pending: ReadonlyMap<string, Browser.Command>
}
| { readonly status: "revoking"; readonly sync: PermissionSync; readonly deleted: boolean }
export type RevokingSession = Extract<SessionState, { readonly status: "revoking" }>
type AttachingSession = Extract<SessionState, { readonly status: "attaching" }>
type AttachedSession = Extract<SessionState, { readonly status: "attached" }>
/** Every attachment of the plugin. Once `disposed`, only revokes still draining remain. */
export type ConnectionState =
| {
readonly status: "active"
readonly nextAttempt: number
readonly sessions: ReadonlyMap<Session.ID, SessionState>
}
| {
readonly status: "disposed"
readonly nextAttempt: number
readonly sessions: ReadonlyMap<Session.ID, RevokingSession>
}
/** How an `attach` call ends: its result, or `unavailable` when the grant failed. */
export type AttachmentOutcome = "closed" | "replaced" | "unavailable"
export type RequestSettlement =
| { readonly ok: true; readonly result: Browser.Result }
| { readonly ok: false; readonly error: Tool.Error }
export type ConnectionEvent =
/** `attempt` is the state's `nextAttempt`, read when the driver registered the attachment's handle. */
| {
readonly type: "attach.started"
readonly sessionID: Session.ID
readonly attempt: number
readonly connectionID: string
}
/** The `attach` call ended on the desktop's side (disconnect or interrupt). */
| { readonly type: "attach.closed"; readonly sessionID: Session.ID; readonly attempt: number }
| { readonly type: "permissions.synced"; readonly sessionID: Session.ID; readonly attempt: number }
| {
readonly type: "permissions.failed"
readonly sessionID: Session.ID
readonly attempt: number
readonly error: unknown
}
| { readonly type: "permissions.timed-out"; readonly sessionID: Session.ID; readonly attempt: number }
| {
readonly type: "state.updated"
readonly sessionID: Session.ID
readonly connectionID: string
readonly browserState: Browser.State
}
| {
readonly type: "request.started"
readonly sessionID: Session.ID
readonly connectionID: string
readonly requestID: string
readonly command: Browser.Command
}
| {
readonly type: "request.dispatch-failed"
readonly sessionID: Session.ID
readonly connectionID: string
readonly requestID: string
readonly error: unknown
}
| {
readonly type: "request.settled"
readonly sessionID: Session.ID
readonly connectionID: string
readonly requestID: string
readonly outcome: Browser.Outcome
}
| {
readonly type: "request.timed-out"
readonly sessionID: Session.ID
readonly connectionID: string
readonly requestID: string
}
/** The tool call that started the request was interrupted. */
| {
readonly type: "request.cancelled"
readonly sessionID: Session.ID
readonly connectionID: string
readonly requestID: string
}
| { readonly type: "session.moved"; readonly sessionID: Session.ID }
| { readonly type: "session.deleted"; readonly sessionID: Session.ID }
| { readonly type: "disposed" }
export type ConnectionEffect =
| {
readonly type: "sync-permissions"
readonly sessionID: Session.ID
readonly attempt: number
readonly allow: boolean
}
/** Disposes the attachment's tunnels and ends its `attach` call with `outcome`. */
| { readonly type: "close-attachment"; readonly attempt: number; readonly outcome: AttachmentOutcome }
| { readonly type: "emit-control"; readonly control: Browser.Control }
/** Emits the `command` control; a failure comes back as `request.dispatch-failed`. */
| {
readonly type: "dispatch-command"
readonly sessionID: Session.ID
readonly connectionID: string
readonly requestID: string
}
| { readonly type: "settle-request"; readonly requestID: string; readonly settlement: RequestSettlement }
| { readonly type: "warn"; readonly message: string; readonly error: unknown }
/** The disposed plugin has no revoke left in flight. */
| { readonly type: "notify-drained" }
export type ConnectionTransition = { readonly state: ConnectionState; readonly effects: readonly ConnectionEffect[] }
type SessionEvent = Exclude<ConnectionEvent, { readonly type: "disposed" }>
type Step<S extends SessionState> = {
readonly entry: S | undefined
readonly nextAttempt: number
readonly effects: readonly ConnectionEffect[]
}
export function createConnectionState(): ConnectionState {
return { status: "active", nextAttempt: 1, sessions: new Map() }
}
export function transitionConnection(state: ConnectionState, event: ConnectionEvent): ConnectionTransition {
if (event.type === "disposed") return dispose(state)
// A request whose attachment closed or was replaced after the tool picked it was never sent.
if (event.type === "request.started" && !isAttachedConnection(state, event.sessionID, event.connectionID))
return {
state,
effects: [
{
type: "settle-request",
requestID: event.requestID,
settlement: { ok: false, error: disconnectedBeforeDispatch() },
},
],
}
const entry = state.sessions.get(event.sessionID)
if (state.status === "disposed") {
if (!entry || entry.status !== "revoking") return { state, effects: [] }
const step = transitionRevoking(entry, event, state.nextAttempt)
if (!step) return { state, effects: [] }
const sessions = new Map(state.sessions)
if (step.entry) sessions.set(event.sessionID, step.entry)
if (!step.entry) sessions.delete(event.sessionID)
return {
state: { status: "disposed", nextAttempt: step.nextAttempt, sessions },
effects: sessions.size ? step.effects : [...step.effects, { type: "notify-drained" }],
}
}
const step = transitionSession(entry, event, state.nextAttempt)
if (!step) return { state, effects: [] }
const sessions = new Map(state.sessions)
if (step.entry) sessions.set(event.sessionID, step.entry)
if (!step.entry) sessions.delete(event.sessionID)
return { state: { status: "active", nextAttempt: step.nextAttempt, sessions }, effects: step.effects }
}
function transitionSession(
entry: SessionState | undefined,
event: SessionEvent,
nextAttempt: number,
): Step<SessionState> | undefined {
if (event.type === "attach.started") return attach(entry, event, nextAttempt)
if (!entry) return undefined
if (entry.status === "revoking") return transitionRevoking(entry, event, nextAttempt)
if (entry.status === "attaching") return transitionAttaching(entry, event, nextAttempt)
return transitionAttached(entry, event, nextAttempt)
}
function attach(
entry: SessionState | undefined,
event: Extract<SessionEvent, { readonly type: "attach.started" }>,
nextAttempt: number,
): Step<SessionState> | undefined {
if (event.attempt !== nextAttempt) return undefined
const attaching = (sync: PermissionSync): AttachingSession => ({
status: "attaching",
attempt: event.attempt,
connectionID: event.connectionID,
sync,
})
// A sync already in flight finishes first; the grant waits for it, so two writes never interleave.
if (entry?.status === "attaching")
return {
entry: attaching(entry.sync),
nextAttempt: nextAttempt + 1,
effects: [{ type: "close-attachment", attempt: entry.attempt, outcome: "replaced" }],
}
if (entry?.status === "revoking") return { entry: attaching(entry.sync), nextAttempt: nextAttempt + 1, effects: [] }
const sync = { attempt: nextAttempt + 1, allow: true }
const effects: ConnectionEffect[] =
entry?.status === "attached"
? [...failPending(entry), { type: "close-attachment", attempt: entry.attempt, outcome: "replaced" }]
: []
return {
entry: attaching(sync),
nextAttempt: nextAttempt + 2,
effects: [...effects, { type: "sync-permissions", sessionID: event.sessionID, ...sync }],
}
}
function transitionAttaching(
entry: AttachingSession,
event: SessionEvent,
nextAttempt: number,
): Step<SessionState> | undefined {
if (
event.type === "permissions.synced" ||
event.type === "permissions.failed" ||
event.type === "permissions.timed-out"
) {
if (event.attempt !== entry.sync.attempt) return undefined
const warning = syncWarning(event, entry.sync)
// The revoke that ran before this attachment settled; now grant.
if (!entry.sync.allow) {
const sync = { attempt: nextAttempt, allow: true }
return {
entry: { ...entry, sync },
nextAttempt: nextAttempt + 1,
effects: [...warning, { type: "sync-permissions", sessionID: event.sessionID, ...sync }],
}
}
if (event.type === "permissions.synced")
return {
entry: {
status: "attached",
attempt: entry.attempt,
connectionID: entry.connectionID,
browserState: { tabs: [], focusedTabID: null },
pending: new Map(),
},
nextAttempt,
effects: [
{ type: "emit-control", control: { type: "attached", connectionID: entry.connectionID, version: 4 } },
],
}
// A grant that timed out may still have written its rules; revoke them.
const sync = { attempt: nextAttempt, allow: false }
return {
entry: { status: "revoking", sync, deleted: false },
nextAttempt: nextAttempt + 1,
effects: [
...warning,
{ type: "close-attachment", attempt: entry.attempt, outcome: "unavailable" },
{ type: "sync-permissions", sessionID: event.sessionID, ...sync },
],
}
}
if (
(event.type === "attach.closed" && event.attempt === entry.attempt) ||
event.type === "session.moved" ||
event.type === "session.deleted"
)
return {
entry: { status: "revoking", sync: entry.sync, deleted: event.type === "session.deleted" },
nextAttempt,
effects: [{ type: "close-attachment", attempt: entry.attempt, outcome: "closed" }],
}
return undefined
}
function transitionAttached(
entry: AttachedSession,
event: SessionEvent,
nextAttempt: number,
): Step<SessionState> | undefined {
if (event.type === "state.updated") return updateBrowserState(entry, event, nextAttempt)
if (event.type === "request.started") {
if (entry.pending.has(event.requestID)) return undefined
return {
entry: { ...entry, pending: new Map(entry.pending).set(event.requestID, event.command) },
nextAttempt,
effects: [
{
type: "dispatch-command",
sessionID: event.sessionID,
connectionID: entry.connectionID,
requestID: event.requestID,
},
],
}
}
if (
event.type === "request.dispatch-failed" ||
event.type === "request.settled" ||
event.type === "request.timed-out" ||
event.type === "request.cancelled"
) {
const command = entry.connectionID === event.connectionID ? entry.pending.get(event.requestID) : undefined
if (!command) return undefined
const pending = new Map(entry.pending)
pending.delete(event.requestID)
return { entry: { ...entry, pending }, nextAttempt, effects: settleRequest(entry, event, command) }
}
if (event.type === "session.deleted")
return {
entry: undefined,
nextAttempt,
effects: [...failPending(entry), { type: "close-attachment", attempt: entry.attempt, outcome: "closed" }],
}
if ((event.type === "attach.closed" && event.attempt === entry.attempt) || event.type === "session.moved")
return revokeAttached(entry, event.sessionID, nextAttempt)
return undefined
}
function transitionRevoking(
entry: RevokingSession,
event: SessionEvent,
nextAttempt: number,
): Step<RevokingSession> | undefined {
if (event.type === "session.deleted")
return entry.deleted ? undefined : { entry: { ...entry, deleted: true }, nextAttempt, effects: [] }
if (
event.type !== "permissions.synced" &&
event.type !== "permissions.failed" &&
event.type !== "permissions.timed-out"
)
return undefined
if (event.attempt !== entry.sync.attempt) return undefined
const warning = syncWarning(event, entry.sync)
// A grant settled after its attachment closed, or timed out and may still have written: revoke it.
if (entry.sync.allow && !entry.deleted) {
const sync = { attempt: nextAttempt, allow: false }
return {
entry: { status: "revoking", sync, deleted: false },
nextAttempt: nextAttempt + 1,
effects: [...warning, { type: "sync-permissions", sessionID: event.sessionID, ...sync }],
}
}
return { entry: undefined, nextAttempt, effects: warning }
}
function dispose(state: ConnectionState): ConnectionTransition {
if (state.status === "disposed") return { state, effects: [] }
const effects: ConnectionEffect[] = []
const sessions = new Map<Session.ID, RevokingSession>()
const nextAttempt = Array.from(state.sessions).reduce((attempt, [sessionID, entry]) => {
if (entry.status === "revoking") {
sessions.set(sessionID, entry)
return attempt
}
if (entry.status === "attaching") {
sessions.set(sessionID, { status: "revoking", sync: entry.sync, deleted: false })
effects.push({ type: "close-attachment", attempt: entry.attempt, outcome: "closed" })
return attempt
}
const step = revokeAttached(entry, sessionID, attempt)
if (step.entry) sessions.set(sessionID, step.entry)
effects.push(...step.effects)
return step.nextAttempt
}, state.nextAttempt)
if (!sessions.size) effects.push({ type: "notify-drained" })
return { state: { status: "disposed", nextAttempt, sessions }, effects }
}
function revokeAttached(entry: AttachedSession, sessionID: Session.ID, nextAttempt: number): Step<RevokingSession> {
const sync = { attempt: nextAttempt, allow: false }
return {
entry: { status: "revoking", sync, deleted: false },
nextAttempt: nextAttempt + 1,
effects: [
...failPending(entry),
{ type: "close-attachment", attempt: entry.attempt, outcome: "closed" },
{ type: "sync-permissions", sessionID, ...sync },
],
}
}
function updateBrowserState(
entry: AttachedSession,
event: Extract<SessionEvent, { readonly type: "state.updated" }>,
nextAttempt: number,
): Step<SessionState> | undefined {
if (entry.connectionID !== event.connectionID || entry.browserState === event.browserState) return undefined
return { entry: { ...entry, browserState: event.browserState }, nextAttempt, effects: [] }
}
function settleRequest(
entry: AttachedSession,
event: Extract<
SessionEvent,
{ readonly type: "request.dispatch-failed" | "request.settled" | "request.timed-out" | "request.cancelled" }
>,
command: Browser.Command,
): ConnectionEffect[] {
const cancel: ConnectionEffect = {
type: "emit-control",
control: { type: "cancel", connectionID: entry.connectionID, requestID: event.requestID },
}
// The interrupted caller no longer waits; only the desktop needs to stop.
if (event.type === "request.cancelled") return [cancel]
const settle = (settlement: RequestSettlement): ConnectionEffect => ({
type: "settle-request",
requestID: event.requestID,
settlement,
})
if (event.type === "request.timed-out") return [settle({ ok: false, error: timedOut(command.action.type) }), cancel]
if (event.type === "request.dispatch-failed")
return [settle({ ok: false, error: dispatchFailed(command.action.type, event.error) })]
if (event.outcome.type === "success") return [settle({ ok: true, result: event.outcome.result })]
return [
settle({
ok: false,
// The code stays readable apart from the message, so the server acts on it without parsing text.
error: new Tool.Error({
message: `[browser.${event.outcome.code}] ${event.outcome.message}`,
metadata: { code: event.outcome.code },
}),
}),
]
}
function failPending(entry: AttachedSession): ConnectionEffect[] {
return Array.from(entry.pending.keys(), (requestID) => ({
type: "settle-request",
requestID,
settlement: { ok: false, error: disconnectedAfterDispatch() },
}))
}
function syncWarning(
event: Extract<
SessionEvent,
{ readonly type: "permissions.synced" | "permissions.failed" | "permissions.timed-out" }
>,
sync: PermissionSync,
): ConnectionEffect[] {
const action = sync.allow ? "grant" : "revoke"
if (event.type === "permissions.failed")
return [{ type: "warn", message: `Browser permission ${action} failed`, error: event.error }]
if (event.type === "permissions.timed-out")
return [
{
type: "warn",
message: `Browser permission ${action} timed out after ${PERMISSION_SYNC_TIMEOUT_MS} ms`,
error: undefined,
},
]
return []
}
/**
* The session's permissions with exactly one set of browser allows (`allow`) or none, every other rule kept in order.
* Undefined when they already are, so a replace writes nothing.
*/
export function reconcileBrowserPermissions(
current: readonly Permission.Rule[] | undefined,
allow: boolean,
): Permission.Rule[] | undefined {
const rules = current ?? []
const kept = rules.filter((rule) => !isGrant(rule))
const next = allow ? [...kept, ...ACCESS.map((rule) => ({ ...rule, effect: "allow" as const }))] : kept
const unchanged =
next.length === rules.length &&
next.every(
(rule, index) =>
rule.action === rules[index]?.action &&
rule.resource === rules[index]?.resource &&
rule.effect === rules[index]?.effect,
)
return unchanged ? undefined : next
}
/**
* One of the allows an attachment grants. A grant that holds only the browser allow, as some versions wrote it, is
* recognised too, so a revoke or the startup sweep removes it. A user rule of the same shape cannot be told apart
* from it.
*/
export function isGrant(rule: Permission.Rule) {
return rule.effect === "allow" && ACCESS.some((access) => access.action === rule.action && access.resource === rule.resource)
}
/**
* The rule that decides `browser` on `operation`, picked as core's `Permission.evaluate` picks it: the last rule whose
* action and resource patterns both match. Undefined when none matches.
*/
export function selectBrowserRule(rules: readonly Permission.Rule[], operation: string) {
const ignoreCase = process.platform === "win32"
return rules.findLast(
(rule) =>
Wildcard.match("browser", rule.action, ignoreCase) && Wildcard.match(operation, rule.resource, ignoreCase),
)
}
export function isConnectionDisposed(state: ConnectionState) {
return state.status === "disposed"
}
/** Whether the session has an attachment or a permission sync in flight, either of which owns its browser rules. */
export function hasSession(state: ConnectionState, sessionID: Session.ID) {
return state.sessions.has(sessionID)
}
/** The attempt the next `attach` gets; the driver keys its handle by it before dispatching `attach.started`. */
export function selectNextAttempt(state: ConnectionState) {
return state.nextAttempt
}
/** The attempt of the session's attachment with `connectionID`, attaching or attached. */
export function selectAttachment(state: ConnectionState, sessionID: Session.ID, connectionID: string) {
if (state.status === "disposed") return undefined
const entry = state.sessions.get(sessionID)
if (entry?.status === "revoking" || entry?.connectionID !== connectionID) return undefined
return entry.attempt
}
export function isAttachedConnection(state: ConnectionState, sessionID: Session.ID, connectionID: string) {
if (state.status === "disposed") return false
const entry = state.sessions.get(sessionID)
return entry?.status === "attached" && entry.connectionID === connectionID
}
export function selectPendingCommand(
state: ConnectionState,
sessionID: Session.ID,
connectionID: string,
requestID: string,
) {
if (state.status === "disposed") return undefined
const entry = state.sessions.get(sessionID)
if (entry?.status !== "attached" || entry.connectionID !== connectionID) return undefined
return entry.pending.get(requestID)
}
/**
* The session's attached connection, or the error a tool call returns while it has none. Reads nothing about its tabs,
* so it may answer before the call is authorized.
*/
export function selectConnection(
state: ConnectionState,
sessionID: Session.ID,
):
| { readonly ok: true; readonly connectionID: string; readonly tabs: readonly Browser.Tab[] }
| { readonly ok: false; readonly error: Tool.Error } {
const entry = state.status === "active" ? state.sessions.get(sessionID) : undefined
if (!entry || entry.status === "revoking")
return {
ok: false,
error: new Tool.Error({
message:
"No browser is attached to this session. It attaches when the session is open in the OpenCode desktop app. No action ran. Continue without the browser; retry only after it attaches.",
}),
}
if (entry.status === "attaching")
return {
ok: false,
error: new Tool.Error({
message:
"The desktop browser is still connecting to this session. No action ran. Retry only after it attaches.",
}),
}
return { ok: true, connectionID: entry.connectionID, tabs: entry.browserState.tabs }
}
/**
* The attachment and tab a browser tool call targets, or the error the tool returns. The error for an unknown tab lists
* the session's tabs, so this runs only once the call is authorized.
*/
export function selectTarget(
state: ConnectionState,
sessionID: Session.ID,
action: Browser.Action,
):
| { readonly ok: true; readonly connectionID: string; readonly tab: Browser.Tab | undefined }
| { readonly ok: false; readonly error: Tool.Error } {
const connection = selectConnection(state, sessionID)
if (!connection.ok) return connection
const tab = "tabID" in action ? connection.tabs.find((tab) => tab.id === action.tabID) : undefined
if ("tabID" in action && !tab) return { ok: false, error: tabUnavailable(action.tabID, connection.tabs) }
return { ok: true, connectionID: connection.connectionID, tab }
}
// Lists the session's open tabs, bounded, so the next call can use one directly.
function tabUnavailable(tabID: Browser.TabID, tabs: readonly Browser.Tab[]) {
const listed = tabs
.slice(0, 10)
.map((tab) => `${tab.id} (${tab.url.length > 100 ? `${tab.url.slice(0, 100)}…` : tab.url})`)
const more = tabs.length > listed.length ? `, and ${tabs.length - listed.length} more from browser.tabs.list({})` : ""
return new Tool.Error({
message: `[browser.tab_unavailable] No open tab in this session has the ID ${tabID}; it was closed or came from another session. ${
listed.length
? `Open tabs: ${listed.join(", ")}${more}. Use one of these IDs`
: "This session has no open tabs. Open one with browser.tabs.open({ url })"
}. Never use a request ID, file ID, or element ref as a tabID.`,
})
}
function disconnectedBeforeDispatch() {
return new Tool.Error({
message:
"[browser.disconnected] The desktop app disconnected before this action was sent; no action ran. Call browser.tabs.list({}) to check whether it has reconnected before you try again.",
})
}
function disconnectedAfterDispatch() {
return new Tool.Error({
message:
"[browser.disconnected] The desktop app disconnected while this action ran; the action may already have run. Once browser.tabs.list({}) succeeds again, inspect the tab with browser.snapshot({tabID}) before you repeat a click, submission, upload, or evaluation.",
})
}
function dispatchFailed(action: Browser.Method, error: unknown) {
return new Tool.Error({
message: `Could not dispatch browser.${action}. Check the desktop connection and call browser.tabs.list({}) before deciding whether to retry.`,
error,
})
}
function timedOut(action: Browser.Method) {
return new Tool.Error({
message: `[browser.timeout] browser.${action} did not finish within ${REQUEST_TIMEOUT_MS / 1000} seconds; its outcome is unknown. Check the desktop connection, call browser.tabs.list({}), and inspect the tab or browser.files.list({tabID}) for completed work. Do not blindly repeat a mutating action or start another recording.`,
})
}