@glittering0717/ai-browser 2.1.21 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,136 @@
1
+ // renderer/tab_bar_layout.js — 标签栏的分组布局(纯函数,无 DOM)。
2
+ //
3
+ // 为什么单独一个文件:把"哪些标签页属于哪个组、哪个组展开着、展开后长什么样"
4
+ // 从 DOM 里抽出来,才能在 vitest 里钉住行为(tests/test_tab_bar_layout.js)。
5
+ // 靠人肉把鼠标移上去验证一个折叠交互,等于没验证 —— 它挂了也只会在某个没人
6
+ // 注意的下午被发现。
7
+ //
8
+ // 两种加载方式:<script src> 挂到 window.TabBarLayout(标签栏),
9
+ // require() 拿到同一份对象(单测)。逻辑只有一处。
10
+ (function (root, factory) {
11
+ const api = factory();
12
+ if (typeof module === 'object' && module.exports) module.exports = api;
13
+ if (root) root.TabBarLayout = api;
14
+ })(typeof globalThis !== 'undefined' ? globalThis : this, function () {
15
+ const HUMAN = 'human';
16
+
17
+ /** 初始状态。用数组而不是 Set:状态要能比较、能打印、能断言。 */
18
+ function empty() {
19
+ return { expanded: [], pinned: [] };
20
+ }
21
+
22
+ function norm(s) {
23
+ s = s || {};
24
+ return { expanded: new Set(s.expanded || []), pinned: new Set(s.pinned || []) };
25
+ }
26
+
27
+ function dump(s) {
28
+ return { expanded: Array.from(s.expanded).sort(), pinned: Array.from(s.pinned).sort() };
29
+ }
30
+
31
+ /** 鼠标悬停到某个组:展开它。人的组没有可展开的东西。 */
32
+ function hover(state, group) {
33
+ const s = norm(state);
34
+ if (group && group !== HUMAN) s.expanded.add(group);
35
+ return dump(s);
36
+ }
37
+
38
+ /** 点击 pill:钉住(鼠标移开也不收起) / 取消钉住。 */
39
+ function toggle(state, group) {
40
+ const s = norm(state);
41
+ if (!group || group === HUMAN) return dump(s);
42
+ if (s.pinned.has(group)) {
43
+ s.pinned.delete(group);
44
+ s.expanded.delete(group);
45
+ } else {
46
+ s.pinned.add(group);
47
+ s.expanded.add(group);
48
+ }
49
+ return dump(s);
50
+ }
51
+
52
+ /**
53
+ * 鼠标离开标签条:收起没被钉住的组 —— 但**正在看的那一组不收起**。
54
+ * 否则人切到某个 agent 的页面后,鼠标一移开那一页就从标签条上消失了,
55
+ * 只能对着地址栏猜"这是谁的页面"。
56
+ */
57
+ function leave(state, activeGroup) {
58
+ const s = norm(state);
59
+ for (const g of Array.from(s.expanded)) {
60
+ if (s.pinned.has(g) || g === activeGroup) continue;
61
+ s.expanded.delete(g);
62
+ }
63
+ return dump(s);
64
+ }
65
+
66
+ /**
67
+ * 渲染清单:把标签页数组摊平成"先人的页、再每个组一个 pill(展开时后面跟它的页)"。
68
+ *
69
+ * 返回 { items, state, activeGroup }:
70
+ * items: {type:'tab'|'group', ...} 顺序即标签条上的顺序
71
+ * state: 归一化后的展开/钉住状态("正在看的那一组"会被强制展开)
72
+ */
73
+ function build(tabs, state) {
74
+ const s = norm(state);
75
+ const list0 = Array.isArray(tabs) ? tabs : [];
76
+ const active = list0.find((t) => t.active);
77
+ const activeGroup = active ? (active.group || HUMAN) : null;
78
+ // 正在看的那一页所在的组必须展开,理由见 leave()。
79
+ if (activeGroup && activeGroup !== HUMAN) s.expanded.add(activeGroup);
80
+
81
+ // 按组归拢,组的顺序 = 每组第一页出现的顺序(人的页在它自己的位置上)。
82
+ const order = [];
83
+ const byGroup = new Map();
84
+ for (const t of list0) {
85
+ const g = t.group || HUMAN;
86
+ if (!byGroup.has(g)) {
87
+ byGroup.set(g, []);
88
+ order.push(g);
89
+ }
90
+ byGroup.get(g).push(t);
91
+ }
92
+
93
+ const items = [];
94
+ for (const g of order) {
95
+ const list = byGroup.get(g);
96
+ if (g === HUMAN) {
97
+ for (const tab of list) items.push({ type: 'tab', tab: tab });
98
+ continue;
99
+ }
100
+ const head = list[0];
101
+ const open = s.expanded.has(g);
102
+ items.push({
103
+ type: 'group',
104
+ id: g,
105
+ label: head.group_label || g,
106
+ count: list.length,
107
+ limit: head.group_limit || null,
108
+ color: head.group_color || null,
109
+ open: open,
110
+ });
111
+ if (open) {
112
+ for (const tab of list) {
113
+ items.push({ type: 'tab', tab: tab, inGroup: true, color: head.group_color || null });
114
+ }
115
+ }
116
+ }
117
+ return { items: items, state: dump(s), activeGroup: activeGroup };
118
+ }
119
+
120
+ /** 结构签名:只有它变了才重建 DOM(每 1.5s 重画会把滚动位置冲掉)。 */
121
+ function signature(tabs, state) {
122
+ const list = Array.isArray(tabs) ? tabs : [];
123
+ const exp = state && state.expanded ? Array.from(state.expanded).slice().sort().join(',') : '';
124
+ return list.map((t) => t.id + ':' + (t.group || HUMAN)).join(',') + '#' + exp;
125
+ }
126
+
127
+ return {
128
+ HUMAN: HUMAN,
129
+ empty: empty,
130
+ hover: hover,
131
+ toggle: toggle,
132
+ leave: leave,
133
+ build: build,
134
+ signature: signature,
135
+ };
136
+ });
@@ -71,6 +71,9 @@ export const VALID_METHODS = [
71
71
  'ui.paint_check',
72
72
  // 窗口/视图几何(读 + 改)。布局类缺陷需要它才能复现与回归。
73
73
  'ui.window_bounds',
74
+ // 窗口可见性 / 最小化(读 + 改)。后台运行(无头式无人值守)的开关:
75
+ // 隐藏是一个**稳定状态**,不会因为 agent 的下一次操作自己弹回来。
76
+ 'ui.window',
74
77
  'ui.snapshot',
75
78
  'ui.diff',
76
79
  'ui.capabilities',
@@ -101,6 +104,9 @@ export const VALID_METHODS = [
101
104
  // 服务端进程身份(pid + 启动时刻)。调用方据此核对"我连上的是不是我拉起的
102
105
  // 那个实例" —— 连错实例时结果毫无意义但不会报错,是最难查的一类。
103
106
  'ui.server_info',
107
+ // 连接自报家门(label / auto_workspace)。声明自己是 agent 的连接会拿到一块
108
+ // 自己的标签组与自动工作区 —— 这是"agent 的操作与人的操作隔离"的入口。
109
+ 'ui.hello',
104
110
  'ui.canvas_configure',
105
111
  'ui.canvas_list',
106
112
  'ui.canvas_read',
@@ -0,0 +1,112 @@
1
+ // shared/tab_group.cjs — 标签页归属:人的一组,每个 agent 一组。
2
+ //
3
+ // 解决的是"多 agent 同时用同一个浏览器"的最后一公里:v2.2.0 已经把**默认目标**
4
+ // 从全局 activeTab 改成会话级,但 agent 的页仍然混在人的标签条里,看不出谁是谁的;
5
+ // 更麻烦的是没有上限 —— 一个跑长任务的 agent 能开二十个标签页,内存被无声吃掉。
6
+ //
7
+ // 这里只有纯函数,不碰 Electron:判据能被单测钉住(tests/test_tab_group.js)。
8
+ //
9
+ // 三个概念分清楚,不要混:
10
+ // group —— 归属(人的 / 某个 agent 的),决定标签栏怎么归拢、上限算谁的
11
+ // active —— 人眼前看的是哪一页。**看**不等于**拥有**:人点开 agent 的页看它
12
+ // 干活,那一页仍然属于 agent,agent 的目标不受影响
13
+ // 目标 —— 某次调用作用在哪一页(shared/tab_route.cjs 的 explicit>session>…)
14
+ 'use strict';
15
+
16
+ /** 人的标签页统一归这一组。它不是"某个会话",而是"没有会话"。 */
17
+ const HUMAN_GROUP = 'human';
18
+
19
+ /** 默认每组上限。8 是权衡:一个正常流程(登录页 + 列表 + 详情 + 后台)留得下,
20
+ * 再多的多半是没关的中间页。环境变量 AI_BROWSER_TAB_LIMIT 可调。 */
21
+ const DEFAULT_LIMIT = 8;
22
+
23
+ /** 组色板。只用于标签栏上那个小圆点,不承载语义。 */
24
+ const COLORS = ['#7c6cf0', '#3fb6a8', '#e0a33e', '#e06c75', '#5aa9e6', '#c678dd'];
25
+
26
+ function groupIdForSession(sessionId) {
27
+ return sessionId ? 'agent:' + sessionId : HUMAN_GROUP;
28
+ }
29
+
30
+ function isAgentGroup(group) {
31
+ return typeof group === 'string' && group.indexOf('agent:') === 0;
32
+ }
33
+
34
+ function isHumanGroup(group) {
35
+ return !isAgentGroup(group);
36
+ }
37
+
38
+ /**
39
+ * 每 agent 的标签页上限。
40
+ *
41
+ * 为什么要上限:这是唯一能**自动**约束 agent 的机制。一个跑长任务的 agent 会
42
+ * 用"再开一个标签页"来回避"这一页怎么办"的决定(人也是这样),几十个标签页在
43
+ * 无头式后台运行里完全不可见,直到内存告急。硬上限把"要不要留这一页"提前变成
44
+ * 一次必须做的决定。
45
+ */
46
+ function limitFromEnv(env) {
47
+ const src = env || (typeof process !== 'undefined' ? process.env : {}) || {};
48
+ const raw = Number(src.AI_BROWSER_TAB_LIMIT);
49
+ if (!Number.isFinite(raw) || raw < 1) return DEFAULT_LIMIT;
50
+ return Math.min(Math.floor(raw), 64);
51
+ }
52
+
53
+ function colorFor(index) {
54
+ const n = COLORS.length;
55
+ return COLORS[((index % n) + n) % n];
56
+ }
57
+
58
+ /**
59
+ * 还能不能再开一页。
60
+ *
61
+ * 只对 agent 组生效:人的标签页不受任何限制(限制人会让产品变得没法用,而且
62
+ * 人要开多少页是他自己的事)。limit<=0 视为不限。
63
+ */
64
+ function decideOpen({ group, count, limit }) {
65
+ if (!isAgentGroup(group)) return { allowed: true, count, limit: null, remaining: null };
66
+ const cap = Number.isFinite(limit) && limit > 0 ? Math.floor(limit) : DEFAULT_LIMIT;
67
+ const used = Number.isFinite(count) && count > 0 ? count : 0;
68
+ return {
69
+ allowed: used < cap,
70
+ count: used,
71
+ limit: cap,
72
+ remaining: Math.max(0, cap - used),
73
+ };
74
+ }
75
+
76
+ /**
77
+ * 弹窗(页面里 window.open / target=_blank)把组撑满时,牺牲哪一页。
78
+ *
79
+ * 为什么弹窗不能像 ui.new_tab 一样直接拒绝:这个弹窗是**页面脚本要的**,拒绝
80
+ * 等于让站点的流程断在半路(OAuth 回调、打印预览、编辑器子窗口)。所以这里退一步,
81
+ * 挑一页最不心疼的关掉。三条排除:
82
+ * - 正在被打开的那个(except,关了自己等于什么都没发生)
83
+ * - 人眼前那一页(activeTab,人正在看)
84
+ * - 有对话框挂着的(那是"等人点"的状态,关掉就把人的操作吞了)
85
+ * 剩下的按"最久没用"排序(usedAt 由 page_manager 在导航/动作时刷新)。
86
+ * 没有候选就返回 null,调用方只好允许超限 —— 宁可多一页,不要吞掉正在用的页。
87
+ */
88
+ function pickEvictable({ tabs, except, activeTab }) {
89
+ const list = Array.isArray(tabs) ? tabs : [];
90
+ let best = null;
91
+ for (const t of list) {
92
+ if (!t || t.id === undefined || t.id === null) continue;
93
+ if (t.id === except || t.id === activeTab) continue;
94
+ if (t.dialog) continue;
95
+ const at = Number(t.usedAt) || 0;
96
+ if (!best || at < best.usedAt) best = { id: t.id, usedAt: at, url: t.url || '' };
97
+ }
98
+ return best ? best.id : null;
99
+ }
100
+
101
+ module.exports = {
102
+ HUMAN_GROUP,
103
+ DEFAULT_LIMIT,
104
+ COLORS,
105
+ groupIdForSession,
106
+ isAgentGroup,
107
+ isHumanGroup,
108
+ limitFromEnv,
109
+ colorFor,
110
+ decideOpen,
111
+ pickEvictable,
112
+ };
@@ -0,0 +1,99 @@
1
+ // shared/tab_route.cjs — 「这次调用到底操作哪个标签页」的唯一判据
2
+ //
3
+ // # 为什么需要它(2026-10-27,多 agent 并用)
4
+ //
5
+ // 以前默认目标是**全局** `activeTab` —— 也就是"人眼看的那一个"。单 agent 时它
6
+ // 恰好等于"agent 正在干的活",所以看不出问题。多个 agent 同时用的时候它立刻
7
+ // 崩掉:
8
+ // - A 开一个标签页 → 所有人的默认目标都变成它;
9
+ // - B 只说了"点一下提交",结果点在 A 的页面上;
10
+ // - 人切到别的标签页看热闹 → 把所有 agent 的目标一起切走。
11
+ // 这三件事的共同点:**动作落在哪个页面,取决于别人做了什么**。这是最难查的
12
+ // 一类 bug(回执 success、页面确实变了,但不是该变的那个)。
13
+ //
14
+ // 所以默认目标的优先级写在这一处,且只有这一处:
15
+ //
16
+ // explicit(调用方点名) > session(这个连接自己的标签页) > active(人眼看的) > 第一个
17
+ //
18
+ // 两条不可退让的约束:
19
+ // 1. **点名不存在的 tab 必须报错**,绝不"就近挑一个"。静默改目标 = 在错的页面上
20
+ // 执行成功 = 比失败更糟。
21
+ // 2. **会话标签页被关掉也要报错**(而不是退回 active)。owner 自己关的会清掉绑定,
22
+ // 所以走到这条分支意味着"别人动了你的页面" —— 调用方需要知道,而不是继续
23
+ // 在另一个页面上操作。
24
+
25
+ /**
26
+ * @param {object} o
27
+ * @param {number|null|undefined} o.explicit 调用方显式传的 tab
28
+ * @param {number|null|undefined} o.session 这个连接绑定的标签页
29
+ * @param {number|null|undefined} o.active 人眼看的那一个
30
+ * @param {number[]} o.tabs 当前存在的标签页
31
+ * @returns {{tab:number|null, source:string, error?:string}}
32
+ * source: explicit | session | active | first | none | gone | session-gone
33
+ */
34
+ function resolveTarget(o) {
35
+ const opts = o || {};
36
+ const known = Array.isArray(opts.tabs) ? opts.tabs : [];
37
+ const has = (id) => id !== undefined && id !== null && known.indexOf(id) >= 0;
38
+ const explicit = opts.explicit;
39
+ const session = opts.session;
40
+ const active = opts.active;
41
+
42
+ if (explicit !== undefined && explicit !== null) {
43
+ if (!has(explicit)) {
44
+ return {
45
+ tab: explicit,
46
+ source: 'gone',
47
+ error: 'tab ' + explicit + ' does not exist (open one with ui.new_tab, or check ui.list_tabs)',
48
+ };
49
+ }
50
+ return { tab: explicit, source: 'explicit' };
51
+ }
52
+
53
+ if (session !== undefined && session !== null) {
54
+ if (!has(session)) {
55
+ return {
56
+ tab: null,
57
+ source: 'session-gone',
58
+ error: 'your tab ' + session + ' is gone (closed); call ui.new_tab to open a new one, or ui.set_active_tab to pick one that still exists. Refusing to fall back to another tab — that would run this call on someone else\'s page.',
59
+ };
60
+ }
61
+ return { tab: session, source: 'session' };
62
+ }
63
+
64
+ if (has(active)) return { tab: active, source: 'active' };
65
+ if (known.length) return { tab: known[0], source: 'first' };
66
+ return { tab: null, source: 'none', error: 'no tabs open' };
67
+ }
68
+
69
+ /**
70
+ * 新建标签页要不要**切到人眼前**。
71
+ *
72
+ * 默认 **否**:人正在看的那一页是人的东西,agent 开一个新页面不该把它顶掉。
73
+ * 三种情况例外:
74
+ * - 调用方显式要求(show:true)——"这一步需要你看一眼",比如扫码登录;
75
+ * - 人自己点的「+」(UI 层用 defaultShow:true 调);
76
+ * - 当前根本没有可见标签页,或者可见的还是**启动页** —— 那种时候没有"人的页面"
77
+ * 可保护,不看新页反而像什么都没发生(agent 干活,用户盯着百度首页)。
78
+ */
79
+ function shouldShowNewTab(o) {
80
+ const opts = o || {};
81
+ if (opts.requested === true) return true;
82
+ if (opts.requested === false) return false;
83
+ if (opts.defaultShow === true) return true;
84
+ if (!opts.hasVisible) return true;
85
+ return opts.visibleIsStartup === true;
86
+ }
87
+
88
+ /**
89
+ * 站点 window.open / target=_blank 派生出来的标签页要不要显示:
90
+ * **跟着打开它的那一个走**。后台标签页里弹出来的窗口,也不该蹦到人眼前 ——
91
+ * agent 的流程里这种弹窗极多(登录、下载、预览),每弹一个就抢一次视野。
92
+ */
93
+ function shouldShowChildTab(o) {
94
+ const opts = o || {};
95
+ if (opts.openerIsVisible === true) return true;
96
+ return false;
97
+ }
98
+
99
+ module.exports = { resolveTarget, shouldShowNewTab, shouldShowChildTab };
@@ -2,4 +2,4 @@
2
2
  // Kept in JS (not imported from package.json) to avoid Node import-attribute
3
3
  // (JSON module) compatibility issues inside Electron. package.json "version"
4
4
  // is manually synced to this value; tests assert they match.
5
- export const VERSION = '2.1.21';
5
+ export const VERSION = '2.3.0';