Skip to content

Commit 887fdfb

Browse files
committed
fix(stagewise): scope browser tabs by agent
1 parent 7ebba5d commit 887fdfb

6 files changed

Lines changed: 366 additions & 20 deletions

File tree

apps/browser/src/backend/services/toolbox/index.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,13 +1198,22 @@ export class ToolboxService extends DisposableService {
11981198
this.shellService?.killSession(sessionId);
11991199
}
12001200

1201-
public getBrowserSnapshot(): BrowserSnapshot {
1201+
public getBrowserSnapshot(agentInstanceId?: string): BrowserSnapshot {
12021202
const browser = this.uiKarton.state.browser;
1203-
const activeTab = browser.activeTabId
1204-
? browser.tabs[browser.activeTabId]
1203+
const useAgentBrowserGroup =
1204+
!!agentInstanceId && agentInstanceId !== browser.activeAgentId;
1205+
const agentBrowserState = useAgentBrowserGroup
1206+
? browser.tabsByAgent[agentInstanceId]
12051207
: null;
1206-
1207-
const allTabs = Object.values(browser.tabs)
1208+
const browserTabs = useAgentBrowserGroup
1209+
? (agentBrowserState?.tabs ?? {})
1210+
: browser.tabs;
1211+
const activeTabId = useAgentBrowserGroup
1212+
? (agentBrowserState?.activeTabId ?? null)
1213+
: browser.activeTabId;
1214+
const activeTab = activeTabId ? browserTabs[activeTabId] : null;
1215+
1216+
const allTabs = Object.values(browserTabs)
12081217
.sort((a, b) => b.lastFocusedAt - a.lastFocusedAt)
12091218
.map((tab) => ({
12101219
id: tab.id,
@@ -1231,14 +1240,14 @@ export class ToolboxService extends DisposableService {
12311240
}
12321241
: null,
12331242
tabs: allTabs,
1234-
totalTabCount: Object.keys(browser.tabs).length,
1243+
totalTabCount: Object.keys(browserTabs).length,
12351244
};
12361245
}
12371246

12381247
public async captureEnvironmentSnapshot(
12391248
agentInstanceId: string,
12401249
): Promise<EnvironmentSnapshot> {
1241-
const browserState = this.getBrowserSnapshot();
1250+
const browserState = this.getBrowserSnapshot(agentInstanceId);
12421251
const workspaceState =
12431252
this.mountManagerService?.getWorkspaceSnapshot(agentInstanceId);
12441253
const toolboxState = this.uiKarton.state.toolbox[agentInstanceId];

apps/browser/src/backend/services/window-layout/chat-state-controller.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
import type { KartonService } from '../karton';
22
import type { TabController } from './tab-controller';
33
import type { SelectedElement } from '@shared/selected-elements';
4+
import type { AppState } from '@shared/karton-contracts/ui';
45

56
export class ChatStateController {
67
private uiKarton: KartonService;
78
private tabs: Record<string, TabController>;
9+
private getActiveBrowserGroupId: () => string;
810

9-
constructor(uiKarton: KartonService, tabs: Record<string, TabController>) {
11+
constructor(
12+
uiKarton: KartonService,
13+
tabs: Record<string, TabController>,
14+
getActiveBrowserGroupId: () => string,
15+
) {
1016
this.uiKarton = uiKarton;
1117
this.tabs = tabs;
18+
this.getActiveBrowserGroupId = getActiveBrowserGroupId;
1219
}
1320

1421
/**
@@ -32,6 +39,7 @@ export class ChatStateController {
3239
// Add if not exists
3340
if (!elements.some((e) => e.stagewiseId === element.stagewiseId))
3441
elements.push(element);
42+
this.syncGroupSelection(draft.browser.tabsByAgent, elements);
3543
});
3644
this.broadcastSelectionUpdate();
3745
}
@@ -45,6 +53,10 @@ export class ChatStateController {
4553
draft.browser.selectedElements = draft.browser.selectedElements.filter(
4654
(e) => e.stagewiseId !== elementId,
4755
);
56+
this.syncGroupSelection(
57+
draft.browser.tabsByAgent,
58+
draft.browser.selectedElements,
59+
);
4860
});
4961
this.broadcastSelectionUpdate();
5062
}
@@ -55,6 +67,10 @@ export class ChatStateController {
5567
public clearElements(): void {
5668
this.uiKarton.setState((draft) => {
5769
draft.browser.selectedElements = [];
70+
this.syncGroupSelection(
71+
draft.browser.tabsByAgent,
72+
draft.browser.selectedElements,
73+
);
5874
});
5975
this.broadcastSelectionUpdate();
6076
}
@@ -66,6 +82,10 @@ export class ChatStateController {
6682
public restoreElements(elements: SelectedElement[]): void {
6783
this.uiKarton.setState((draft) => {
6884
draft.browser.selectedElements = [...elements];
85+
this.syncGroupSelection(
86+
draft.browser.tabsByAgent,
87+
draft.browser.selectedElements,
88+
);
6989
});
7090
this.broadcastSelectionUpdate();
7191
}
@@ -90,4 +110,14 @@ export class ChatStateController {
90110
tab.updateContextSelection(allSelectedElements);
91111
});
92112
}
113+
114+
private syncGroupSelection(
115+
tabsByAgent: AppState['browser']['tabsByAgent'],
116+
selectedElements: SelectedElement[],
117+
): void {
118+
const group = tabsByAgent[this.getActiveBrowserGroupId()];
119+
if (group) {
120+
group.selectedElements = [...selectedElements];
121+
}
122+
}
93123
}

0 commit comments

Comments
 (0)