@glittering0717/ai-browser 2.1.20 → 2.2.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.
- package/README.md +53 -32
- package/package.json +5 -2
- package/scripts/marketing/data_snapshot.sh +19 -1
- package/scripts/release.cjs +13 -4
- package/skills/ai-browser/SKILL.md +10 -9
- package/src/main/download_monitor.js +7 -0
- package/src/main/index.js +160 -27
- package/src/main/mcp_server.js +14 -3
- package/src/main/mcp_tools.js +16 -14
- package/src/main/media_inline.js +104 -0
- package/src/main/page_manager.js +378 -37
- package/src/main/ws_server.js +150 -7
- package/src/preload/bridge.cjs +10 -0
- package/src/renderer/download.html +217 -0
- package/src/renderer/tab_bar.html +45 -0
- package/src/shared/media_doc.cjs +154 -0
- package/src/shared/protocol.js +7 -0
- package/src/shared/tab_route.cjs +99 -0
- package/src/shared/version.js +1 -1
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
// shared/media_doc.cjs — 「这条响应到底该显示,还是该下载」
|
|
2
|
+
//
|
|
3
|
+
// # 为什么需要它(2026-10-27 用户报"点证书下载 → 正在打开… → 一坨 png 链接")
|
|
4
|
+
//
|
|
5
|
+
// 沧州教师继续教育网的证书地址回的是:
|
|
6
|
+
// Content-Type: image/png
|
|
7
|
+
// Content-Disposition: attachment;filename=<uuid>.png
|
|
8
|
+
// 站点用新标签打开它,于是 Electron 把它当下载处理 —— **导航永远不会提交**,
|
|
9
|
+
// 那个标签就停在 loading.html「正在打开…」上,把 URL 明晃晃地摆着;
|
|
10
|
+
// 文件本身倒是落盘了,但落在 ~/.ai-browser/downloads/<tab>/ 里,界面上没有任何回执。
|
|
11
|
+
//
|
|
12
|
+
// 唯一的"别扭"源头是:**能看的东西被当成文件丢进了黑洞**。所以这里给出
|
|
13
|
+
// 一个纯函数判据 —— 主框架响应只要(a)浏览器能直接渲染,(b)服务端挂了
|
|
14
|
+
// attachment,就把 attachment 摘掉,让它按内容类型正常显示。
|
|
15
|
+
//
|
|
16
|
+
// 为什么按**内容类型白名单**而不是按 URL 后缀:后缀是站点随口写的,
|
|
17
|
+
// `?id=123`、`.do`、`.ashx` 到处都是;Content-Type 才是浏览器的判据。
|
|
18
|
+
// 白名单只收"人眼能直接看"的四类,其余(zip/dmg/exe/docx/xlsx…)保持下载,
|
|
19
|
+
// 这是浏览器的既有语义,不该被我们改写。
|
|
20
|
+
//
|
|
21
|
+
// 刻意**不含** text/plain、application/json、text/csv:
|
|
22
|
+
// 它们技术上能内联,但实践中"点导出"的语义就是"给我文件",
|
|
23
|
+
// 把 CSV 铺满一屏反而是帮倒忙。
|
|
24
|
+
|
|
25
|
+
/** 可以直接在标签页里显示的主框架内容类型。 */
|
|
26
|
+
const RENDERABLE = [
|
|
27
|
+
/^image\/[a-z0-9.+-]+$/i,
|
|
28
|
+
/^video\/[a-z0-9.+-]+$/i,
|
|
29
|
+
/^audio\/[a-z0-9.+-]+$/i,
|
|
30
|
+
/^application\/pdf$/i,
|
|
31
|
+
];
|
|
32
|
+
|
|
33
|
+
// application/octet-stream 永不视为可显示:站点把 png 说成二进制流时,
|
|
34
|
+
// 改 Content-Type 去强行渲染,坏了就是一片碎图 —— 下载反而是可靠的那条路。
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* 在 Electron 的 responseHeaders(Record<string, string[]>)里按名字取值。
|
|
38
|
+
* 键名大小写由服务端决定(`Content-Disposition` / `content-disposition` 都见过),
|
|
39
|
+
* 所以必须不区分大小写地找。
|
|
40
|
+
*/
|
|
41
|
+
function headerValue(headers, name) {
|
|
42
|
+
if (!headers || typeof headers !== 'object') return '';
|
|
43
|
+
const want = String(name).toLowerCase();
|
|
44
|
+
for (const k of Object.keys(headers)) {
|
|
45
|
+
if (String(k).toLowerCase() !== want) continue;
|
|
46
|
+
const v = headers[k];
|
|
47
|
+
if (Array.isArray(v)) return v.length ? String(v[0]) : '';
|
|
48
|
+
return v === undefined || v === null ? '' : String(v);
|
|
49
|
+
}
|
|
50
|
+
return '';
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** `image/png;charset=UTF-8` → `image/png`。没有 Content-Type 时给空串。 */
|
|
54
|
+
function mimeOf(headers) {
|
|
55
|
+
const raw = headerValue(headers, 'content-type');
|
|
56
|
+
if (!raw) return '';
|
|
57
|
+
return raw.split(';')[0].trim().toLowerCase();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* 主框架内容的处置方式:'attachment' | 'inline' | 'none'。
|
|
62
|
+
* 没有 Content-Disposition 就是 'none'(浏览器按内容类型自己决定)。
|
|
63
|
+
*/
|
|
64
|
+
function dispositionOf(headers) {
|
|
65
|
+
const raw = headerValue(headers, 'content-disposition').toLowerCase();
|
|
66
|
+
if (!raw) return 'none';
|
|
67
|
+
if (raw.indexOf('attachment') === 0) return 'attachment';
|
|
68
|
+
if (raw.indexOf('inline') === 0) return 'inline';
|
|
69
|
+
// `form-data; name="file"` 这种出现在响应头里是没有意义的,别当成 attachment。
|
|
70
|
+
return 'none';
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function isRenderableMime(mime) {
|
|
74
|
+
if (!mime) return false;
|
|
75
|
+
return RENDERABLE.some((re) => re.test(mime));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** 这份主框架响应该不该被"救回来"直接显示。 */
|
|
79
|
+
function shouldRenderInline(headers) {
|
|
80
|
+
return isRenderableMime(mimeOf(headers)) && dispositionOf(headers) === 'attachment';
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* 去掉 Content-Disposition,返回**新对象**(不改入参:Electron 可能缓存,
|
|
85
|
+
* 原地改动在重试/缓存命中时表现不一致)。第二个返回值表示是否真的改了。
|
|
86
|
+
*/
|
|
87
|
+
function stripDisposition(headers) {
|
|
88
|
+
if (!headers || typeof headers !== 'object') return { headers, changed: false };
|
|
89
|
+
const out = {};
|
|
90
|
+
let changed = false;
|
|
91
|
+
for (const k of Object.keys(headers)) {
|
|
92
|
+
if (String(k).toLowerCase() === 'content-disposition') { changed = true; continue; }
|
|
93
|
+
out[k] = headers[k];
|
|
94
|
+
}
|
|
95
|
+
return { headers: changed ? out : headers, changed };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** 展示用分类,决定结果页画缩略图还是图标。 */
|
|
99
|
+
function kindOf(mime) {
|
|
100
|
+
const m = String(mime || '').toLowerCase();
|
|
101
|
+
if (m.indexOf('image/') === 0) return 'image';
|
|
102
|
+
if (m.indexOf('video/') === 0) return 'video';
|
|
103
|
+
if (m.indexOf('audio/') === 0) return 'audio';
|
|
104
|
+
if (m === 'application/pdf') return 'pdf';
|
|
105
|
+
return 'file';
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** 562KB / 1.2MB / 3.4GB —— 结果页和回执都只用到这一种写法。 */
|
|
109
|
+
function humanSize(bytes) {
|
|
110
|
+
const n = Number(bytes);
|
|
111
|
+
if (!isFinite(n) || n <= 0) return '大小未知';
|
|
112
|
+
const units = ['B', 'KB', 'MB', 'GB', 'TB'];
|
|
113
|
+
let v = n, i = 0;
|
|
114
|
+
while (v >= 1024 && i < units.length - 1) { v /= 1024; i++; }
|
|
115
|
+
const s = i === 0 ? String(Math.round(v)) : (v >= 100 ? v.toFixed(0) : v.toFixed(1));
|
|
116
|
+
return s + units[i];
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** 这个 URL 是不是我们自己的"正在打开…"占位页(file:///…/loading.html?url=…)。 */
|
|
120
|
+
function isPlaceholderUrl(url) {
|
|
121
|
+
return /\/renderer\/loading\.html(\?|$)/.test(String(url || ''));
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** 占位页 query 里带的目标地址(用于核对"这次下载是不是这个 tab 存在的理由")。 */
|
|
125
|
+
function placeholderTarget(url) {
|
|
126
|
+
const s = String(url || '');
|
|
127
|
+
const i = s.indexOf('?');
|
|
128
|
+
if (i < 0) return '';
|
|
129
|
+
try {
|
|
130
|
+
const q = new URLSearchParams(s.slice(i + 1));
|
|
131
|
+
return q.get('url') || '';
|
|
132
|
+
} catch (_) {
|
|
133
|
+
return '';
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** 这个 URL 是不是我们自己的界面页(download/loading/error_page)。 */
|
|
138
|
+
function isChromePageUrl(url) {
|
|
139
|
+
return /^file:\/\/.*\/renderer\/(download|loading|error_page|error_banner|tab_bar)\.html/.test(String(url || ''));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
module.exports = {
|
|
143
|
+
headerValue,
|
|
144
|
+
mimeOf,
|
|
145
|
+
dispositionOf,
|
|
146
|
+
isRenderableMime,
|
|
147
|
+
shouldRenderInline,
|
|
148
|
+
stripDisposition,
|
|
149
|
+
kindOf,
|
|
150
|
+
humanSize,
|
|
151
|
+
isPlaceholderUrl,
|
|
152
|
+
placeholderTarget,
|
|
153
|
+
isChromePageUrl,
|
|
154
|
+
};
|
package/src/shared/protocol.js
CHANGED
|
@@ -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',
|
|
@@ -78,6 +81,10 @@ export const VALID_METHODS = [
|
|
|
78
81
|
'ui.subscribe',
|
|
79
82
|
'ui.unsubscribe',
|
|
80
83
|
'ui.downloads',
|
|
84
|
+
// "把当前页存下来"。浏览器的显示/下载是两条路:图片、视频、PDF 被显示出来
|
|
85
|
+
// (见 shared/media_doc.cjs),所以要留下文件时得有一条明确的入口,
|
|
86
|
+
// 否则等于把"下载"这个能力从这类页面上拿掉了。
|
|
87
|
+
'ui.download',
|
|
81
88
|
// 主进程错误日志(只读)。界面弹过"js 崩溃"原生框、或某个动作莫名失败时,
|
|
82
89
|
// 这里是唯一能查到 stack 与"出错那一刻在调什么"的地方 —— 此前这类异常只活在
|
|
83
90
|
// 那个框里,点掉即永久丢失。
|
|
@@ -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 };
|
package/src/shared/version.js
CHANGED
|
@@ -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.
|
|
5
|
+
export const VERSION = '2.2.0';
|