Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(browser): 按实际可见状态激活配方页签

Dash61921c422c28
diff --git a/packages/lizi-mcps/src/browser/opencli-page.ts b/packages/lizi-mcps/src/browser/opencli-page.ts
index 57c036a65b..00f956d67c 100644
--- a/packages/lizi-mcps/src/browser/opencli-page.ts
+++ b/packages/lizi-mcps/src/browser/opencli-page.ts
@@ -194,14 +194,18 @@ export class OpenCliPage extends CDPBasePage {
async tabs(): Promise<
Array<{ page: string; url?: string; active: boolean }>
> {
- const data = await this.request({ action: "tabs" });
- return ((data.tabs ?? []) as Array<{ targetId: string; url?: string }>).map(
- (tab) => ({
- page: tab.targetId,
- url: tab.url,
- active: tab.targetId === this.targetId,
- }),
- );
+ const data = await this.request({ action: "tabs" }, false);
+ const tabs = [];
+ for (const tab of (data.tabs ?? []) as Array<{ targetId: string; url?: string }>) {
+ // Binding is not visibility. Probe without adopting another target;
+ // a tab that disappeared during enumeration is not known to be visible.
+ const visibility = await this.request({
+ action: 'act', targetId: tab.targetId,
+ request: { kind: 'evaluate', fn: "() => document.visibilityState === 'visible'" },
+ }, false).catch(() => ({ result: false }));
+ tabs.push({ page: tab.targetId, url: tab.url, active: visibility.result === true });
+ }
+ return tabs;
}
async selectTab(target: number | string): Promise<void> {
@@ -211,6 +215,7 @@ export class OpenCliPage extends CDPBasePage {
? tabs[target]
: tabs.find((item) => item.page === target);
if (!tab) throw new Error(`Browser tab not found: ${target}`);
+ await this.request({ action: 'focus', targetId: tab.page }, false);
this.targetId = tab.page;
this._lastUrl = tab.url ?? null;
}
diff --git a/packages/lizi-mcps/src/browser/opencli-recipes.test.ts b/packages/lizi-mcps/src/browser/opencli-recipes.test.ts
index b784031ebb..152fd57b72 100644
--- a/packages/lizi-mcps/src/browser/opencli-recipes.test.ts
+++ b/packages/lizi-mcps/src/browser/opencli-recipes.test.ts
@@ -61,6 +61,52 @@ afterEach(() => {
});
describe("bundled OpenCLI recipes", () => {
+ // #6826 / review 4353609017, unit: the real Gemini activation helper and
+ // host page must resume a hidden response, including a reused bound tab.
+ // Earlier foreground-login tests never exercised tab discovery/selection.
+ it.each(['gemini-target', 'other-target'])('activates the hidden Gemini response bound to %s', async targetId => {
+ let visible = false;
+ const page = new OpenCliPage({ targetId, call: async request => {
+ if (request.action === 'tabs') return { ok: true, action: request.action, data: { tabs: [
+ { targetId: 'gemini-target', url: 'https://gemini.google.com/app' },
+ ] } };
+ if (request.action === 'focus') {
+ expect(request.targetId).toBe('gemini-target');
+ visible = true;
+ }
+ const js = request.request?.fn ?? '';
+ return { ok: true, action: request.action, data: { result: js.includes('visibilityState') ? visible
+ : js.includes('fixture-response') ? visible ? 'ready' : 'pending' : 'https://gemini.google.com/app' } };
+ } });
+ await gemini.ensureGeminiPage(page);
+ expect(await page.evaluate('fixture-response')).toBe('ready');
+ expect(page.getActivePage()).toBe('gemini-target');
+ });
+
+ it.each(['target-id', 1])('preserves tab binding if explicit selection %s cannot focus', async target => {
+ const page = new OpenCliPage({ targetId: 'original', call: async request => {
+ if (request.action === 'focus') return { ok: false, action: request.action, message: 'fixture focus failed' };
+ return { ok: true, action: request.action, data: request.action === 'tabs'
+ ? { tabs: [{ targetId: 'original' }, { targetId: 'target-id' }] }
+ : { result: false } };
+ } });
+ await expect(page.selectTab(target)).rejects.toThrow('fixture focus failed');
+ expect(page.getActivePage()).toBe('original');
+ });
+
+ it('reports current visibility without changing the bound target', async () => {
+ let visible = true;
+ const page = new OpenCliPage({ targetId: 'bound', call: async request => ({
+ ok: true, action: request.action, data: request.action === 'tabs'
+ ? { targetId: 'unrelated-response-target', tabs: [{ targetId: 'bound' }, { targetId: 'other' }] }
+ : { targetId: request.targetId, result: request.targetId === 'bound' && visible },
+ }) });
+ expect((await page.tabs()).map(tab => tab.active)).toEqual([true, false]);
+ visible = false;
+ expect((await page.tabs()).map(tab => tab.active)).toEqual([false, false]);
+ expect(page.getActivePage()).toBe('bound');
+ });
+
// #6826 / review 4353330401, unit: real Instagram adapter + host page.
// Earlier tests did not enter the ten-item pre-share recovery branch. A
// failed composer must be dismissed before the next navigation, without