mirror of
https://github.com/Tencent/BrowserSkill.git
synced 2026-09-10 13:01:23 +08:00
Tool cancellation is now cooperative end-to-end: the queue stays busy until compensation completes, cancellation is checked cooperatively across the pipeline, the fallback window is compensated too, and cleanup_failed is surfaced explicitly. Includes a gated delayed-compensation E2E test.
64 lines
2.3 KiB
TypeScript
64 lines
2.3 KiB
TypeScript
/**
|
|
* Wrapper around `chrome.windows.*` for creating / closing the
|
|
* dedicated Agent Window each session owns.
|
|
*
|
|
* Kept behind an interface so the SessionManager can be unit-tested
|
|
* with a fake chrome.windows implementation (see
|
|
* `__tests__/manager.test.ts`).
|
|
*/
|
|
|
|
export interface AgentWindowApi {
|
|
create(url: string, opts?: AgentWindowCreateOptions): Promise<number>;
|
|
remove(windowId: number): Promise<void>;
|
|
/**
|
|
* Guarantee the Agent Window has an active, CDP-navigable tab.
|
|
* `chrome://` pages (including the New Tab page) reject `Page.navigate`,
|
|
* so sessions bootstrap with `about:blank` instead.
|
|
*/
|
|
ensureActiveTab(windowId: number, url: string): Promise<void>;
|
|
}
|
|
|
|
/** Creation hints for a new Agent Window. */
|
|
export interface AgentWindowCreateOptions {
|
|
/** Optional outer size in CSS pixels. */
|
|
size?: { width: number; height: number };
|
|
/** Defaults to true so existing sessions keep visible Agent Windows. */
|
|
focused?: boolean;
|
|
}
|
|
|
|
/** Initial tab URL for every new session's Agent Window. */
|
|
export const AGENT_WINDOW_HOME = "about:blank";
|
|
|
|
export const chromeAgentWindowApi: AgentWindowApi = {
|
|
async create(url: string, opts: AgentWindowCreateOptions = {}): Promise<number> {
|
|
const win = await chrome.windows.create({
|
|
type: "normal",
|
|
focused: opts.focused ?? true,
|
|
url,
|
|
...(opts.size ? { width: opts.size.width, height: opts.size.height } : {}),
|
|
});
|
|
if (typeof win?.id !== "number") {
|
|
throw new Error("[bh] chrome.windows.create returned no window id");
|
|
}
|
|
return win.id;
|
|
},
|
|
async remove(windowId: number): Promise<void> {
|
|
// Callers decide whether a missing/failed removal is benign. In
|
|
// particular, transactional session-start cleanup must be able to
|
|
// surface a window it could not remove instead of reporting a false
|
|
// cancellation success while the Agent Window remains open.
|
|
await chrome.windows.remove(windowId);
|
|
},
|
|
async ensureActiveTab(windowId: number, url: string): Promise<void> {
|
|
const tabs = await chrome.tabs.query({ windowId });
|
|
const first = tabs.find((t) => typeof t.id === "number");
|
|
if (first?.id) {
|
|
if (!first.active) {
|
|
await chrome.tabs.update(first.id, { active: true });
|
|
}
|
|
return;
|
|
}
|
|
await chrome.tabs.create({ windowId, url, active: true });
|
|
},
|
|
};
|