-
Notifications
You must be signed in to change notification settings - Fork 738
feat(agents): record the active tab on named browser sessions #2370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ben-reitz
wants to merge
2
commits into
refactor/browser-connector-shared-helpers
from
feat/browser-session-active-tab
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ import type { | |
| BrowserSessionStore, | ||
| StoredBrowserSession | ||
| } from "./session-manager"; | ||
| import { loadCdpSpec, type SearchableCdpSpec } from "./spec"; | ||
|
|
||
| /** | ||
| * Browser Run's server-side `keep_alive` maximum (600 seconds). Named | ||
|
|
@@ -98,14 +99,38 @@ export interface ResolvedBrowserSession { | |
| restarted: boolean; | ||
| createdAt: number; | ||
| updatedAt: number; | ||
| /** | ||
| * The tab the agent last worked in, when one was recorded for this | ||
| * browser. Absent on a browser this resolution created; a replacement a | ||
| * concurrent caller already created and used may carry one. | ||
| */ | ||
| activeTargetId?: string; | ||
| } | ||
|
|
||
| export interface ConnectedBrowserSession { | ||
| name: string; | ||
| sessionId: string; | ||
| /** | ||
| * `true` when the browser this caller resolved is gone, even if a | ||
| * concurrent caller already replaced it. See | ||
| * {@link ResolvedBrowserSession.restarted}. | ||
| */ | ||
| restarted: boolean; | ||
| /** See {@link ResolvedBrowserSession.activeTargetId}. */ | ||
| activeTargetId?: string; | ||
| /** Closing this socket does NOT delete the named session. */ | ||
| cdp: CdpSession; | ||
| /** | ||
| * Record the tab the agent is working in (or clear it with `undefined`) | ||
| * on this session's record. Never resurrects: returns `false` when the | ||
| * session was closed or replaced since this connection resolved it. | ||
| */ | ||
| setActiveTarget(targetId: string | undefined): Promise<boolean>; | ||
| /** | ||
| * The Chrome DevTools Protocol description this browser serves, read from | ||
| * the browser itself (cached per binding). | ||
| */ | ||
| spec(): Promise<SearchableCdpSpec>; | ||
| } | ||
|
|
||
| /** One-shot session options for the default Chromium engine. */ | ||
|
|
@@ -280,7 +305,16 @@ export class NamedBrowserSessions { | |
| name, | ||
| sessionId: resolved.sessionId, | ||
| restarted: resolved.restarted, | ||
| cdp | ||
| activeTargetId: resolved.activeTargetId, | ||
| cdp, | ||
| setActiveTarget: (targetId) => | ||
| this.#update(key, resolved.sessionId, (current) => ({ | ||
| ...current, | ||
| activeTargetId: targetId, | ||
| updatedAt: Date.now() | ||
| })), | ||
| spec: () => | ||
| loadCdpSpec({ browser: this.#browser, sessionId: resolved.sessionId }) | ||
|
Comment on lines
+316
to
+317
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| }; | ||
| } | ||
|
|
||
|
|
@@ -331,14 +365,29 @@ export class NamedBrowserSessions { | |
| } | ||
|
|
||
| /** Refresh `updatedAt` for an actively used session — never resurrects. */ | ||
| async #touch(key: string, sessionId: string): Promise<boolean> { | ||
| #touch(key: string, sessionId: string): Promise<boolean> { | ||
| return this.#update(key, sessionId, (current) => ({ | ||
| ...current, | ||
| updatedAt: Date.now() | ||
| })); | ||
| } | ||
|
|
||
| /** | ||
| * Rewrite the record only while it still holds `sessionId`: a replaced or | ||
| * retired entry is never resurrected. | ||
| */ | ||
| async #update( | ||
| key: string, | ||
| sessionId: string, | ||
| change: (current: StoredBrowserSession) => StoredBrowserSession | ||
| ): Promise<boolean> { | ||
| const lock = await this.#store.acquireLock(key); | ||
| try { | ||
| const current = await this.#store.get(key); | ||
| if (current?.sessionId !== sessionId) { | ||
| return false; // replaced or gone — activity no longer counts | ||
| return false; // replaced or gone — the caller's view is stale | ||
| } | ||
| await this.#store.set(key, { ...current, updatedAt: Date.now() }); | ||
| await this.#store.set(key, change(current)); | ||
| return true; | ||
| } finally { | ||
| await lock.release(); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.