@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.
@@ -11,7 +11,7 @@ export const MCP_TOOLS = [
11
11
  {
12
12
  name: 'browse_navigate',
13
13
  title: 'Navigate tab',
14
- description: 'Navigate the active/focused tab to a URL. Returns as soon as the requested milestone is reached (default: DOM ready) — it does not wait for the load event, which SPAs often never fire.',
14
+ description: 'Navigate your tab to a URL. Returns at the requested milestone (default: DOM ready) — it does not wait for load, which SPAs often never fire.',
15
15
  annotations: { readOnlyHint: false, destructiveHint: false, openWorldHint: true },
16
16
  inputSchema: {
17
17
  type: 'object',
@@ -90,31 +90,31 @@ export const MCP_TOOLS = [
90
90
  },
91
91
  drag: {
92
92
  type: 'object',
93
- description: 'drag only. Give from/to for absolute points, dx/dy for a relative move, or target_to to drop onto another element.',
93
+ description: 'drag only. from/to = points, dx/dy = relative, target_to = drop onto an element.',
94
94
  properties: {
95
95
  from: {
96
96
  type: 'object',
97
- description: 'Start point. Omit to start from target.',
97
+ description: 'Start point (default: target).',
98
98
  properties: {
99
99
  x: { type: 'number' },
100
100
  y: { type: 'number' },
101
- anchor: { type: 'string', enum: ['center', 'left', 'right', 'top', 'bottom'], description: 'A side prefers that node\'s connection handle.' }
101
+ anchor: { type: 'string', enum: ['center', 'left', 'right', 'top', 'bottom'], description: 'A side prefers that handle.' }
102
102
  }
103
103
  },
104
104
  to: {
105
105
  type: 'object',
106
- description: 'End point in viewport coords.',
106
+ description: 'End point (viewport).',
107
107
  properties: {
108
108
  x: { type: 'number' },
109
109
  y: { type: 'number' },
110
110
  anchor: { type: 'string', enum: ['center', 'left', 'right', 'top', 'bottom'] }
111
111
  }
112
112
  },
113
- dx: { type: 'number', description: 'Horizontal distance instead of to.x.' },
114
- dy: { type: 'number', description: 'Vertical distance instead of to.y.' },
115
- target_to: { type: 'string', description: 'data-ai-id to drop onto (e.g. connect one node\'s output to another).' },
116
- hold: { type: 'array', items: { type: 'string' }, description: 'Modifiers held during the drag, e.g. ["Shift"].' },
117
- button: { type: 'string', enum: ['left', 'middle', 'right'], description: 'Canvas apps often pan on middle/right drag.' }
113
+ dx: { type: 'number', description: 'Instead of to.x.' },
114
+ dy: { type: 'number', description: 'Instead of to.y.' },
115
+ target_to: { type: 'string', description: 'data-ai-id to drop onto.' },
116
+ hold: { type: 'array', items: { type: 'string' }, description: 'Held modifiers, e.g. ["Shift"].' },
117
+ button: { type: 'string', enum: ['left', 'middle', 'right'], description: 'Canvas apps pan on middle/right drag.' }
118
118
  }
119
119
  },
120
120
  tab: { type: 'integer', description: 'Tab id (default: active tab).' }
@@ -184,7 +184,7 @@ export const MCP_TOOLS = [
184
184
  {
185
185
  name: 'browse_screenshot',
186
186
  title: 'Screenshot the viewport',
187
- description: 'Screenshot the viewport (base64 in data). Use FIRST when an action misbehaves: the element tree cannot show overlays, canvas, occlusion or glitches. Warns if a visible password input is on screen.',
187
+ description: 'Screenshot the viewport (base64). Use FIRST when an action misbehaves: the tree cannot show overlays, canvas, occlusion or glitches. Warns about visible password inputs.',
188
188
  annotations: { readOnlyHint: true, destructiveHint: false, openWorldHint: true },
189
189
  inputSchema: {
190
190
  type: 'object',
@@ -210,7 +210,7 @@ export const MCP_TOOLS = [
210
210
  {
211
211
  name: 'browse_tabs',
212
212
  title: 'Tabs',
213
- description: 'Tabs: op=list / new / close / switch. switch makes a tab the default target for later calls, so you rarely pass tab explicitly.',
213
+ description: 'Tabs: list/new/close/switch/window. switch = your default target (then omit tab). new never steals the user\'s view unless show:true. window = hide/show; work continues while hidden.',
214
214
  // 合并前是 4 个工具(list / new / close / set_active),每个只承载一个动词。
215
215
  // 合并后 destructiveHint 只能是 false —— 否则 op=list 也会被标成破坏性操作;
216
216
  // close 的破坏性写在 description 里。这是合并的唯一语义代价,接受。
@@ -218,9 +218,11 @@ export const MCP_TOOLS = [
218
218
  inputSchema: {
219
219
  type: 'object',
220
220
  properties: {
221
- op: { type: 'string', enum: ['list', 'new', 'close', 'switch'], description: 'list = id/url/title/active for every tab. switch = make `tab` the default target.' },
221
+ op: { type: 'string', enum: ['list', 'new', 'close', 'switch', 'window'], description: 'list = every tab; switch = set default; window = hide/show.' },
222
222
  tab: { type: 'integer', description: 'close / switch: the target tab id.' },
223
- url: { type: 'string', description: 'new: open at this URL (default about:blank).' }
223
+ url: { type: 'string', description: 'new: open at this URL (default about:blank).' },
224
+ show: { type: 'boolean', description: 'new: bring it to the front (default: background).' },
225
+ visible: { type: 'boolean', description: 'window: true = show, false = hide.' }
224
226
  },
225
227
  required: ['op']
226
228
  }
@@ -0,0 +1,104 @@
1
+ // main/media_inline.js — 让"能看的东西"直接显示,而不是掉进下载目录
2
+ //
3
+ // 判据在 shared/media_doc.cjs(那里写了为什么是 Content-Type 白名单)。
4
+ // 这个模块只负责把它接到 Electron 的响应链上,并且记住"哪个 tab 刚刚
5
+ // 被救回来一张可显示的主框架文档" —— 工具条据此显示「下载」按钮。
6
+ //
7
+ // # 为什么用 onHeadersReceived 而不是 will-download 里拦
8
+ //
9
+ // will-download 时下载**已经决定**了:只能整体取消(然后自己重新取一遍再塞进
10
+ // 页面),多一次往返、丢认证上下文的风险全靠自己兜。而 Content-Disposition
11
+ // 正是在 onHeadersReceived 之后才参与"MIME 嗅探 / 下载判定"的 ——
12
+ // 在这里把 attachment 摘掉,Chromium 就会按 image/png 正常显示,走的是浏览器
13
+ // 原本的路径,没有任何自行其是的地方。
14
+ //
15
+ // # 约束
16
+ // - 只处理 `resourceType === 'mainFrame'`:子资源本来就是按类型渲染的,
17
+ // 摘子资源的头纯属多余,还会把"另存为图片"的行为一起改掉。
18
+ // - 只摘 attachment,**不改 Content-Type**。站点把 png 说成 octet-stream 时,
19
+ // 改类型去强行渲染,坏了就是一片碎图;那种情况让它下载才是可靠的。
20
+ // - 处理不过来就原样放行:这个钩子在任何异常下都不能吞掉响应。
21
+ import mediaDoc from '../shared/media_doc.cjs';
22
+
23
+ const MAX_REMEMBER = 32;
24
+
25
+ export class MediaInline {
26
+ constructor(opts = {}) {
27
+ this.session = opts.session || null;
28
+ this._resolveTab = opts.resolveTab || (() => undefined);
29
+ this._onMedia = opts.onMedia || (() => {});
30
+ // tabId -> { url, mime, kind, at }:最近一次"被救回来"的主框架文档。
31
+ // 工具条只在**当前页就是它**时才显示下载按钮,所以这里存最近一次即可。
32
+ this._byTab = new Map();
33
+ this._installed = false;
34
+ this.stats = { seen: 0, inlined: 0 };
35
+ }
36
+
37
+ install(session) {
38
+ if (session) this.session = session;
39
+ if (this._installed || !this.session || typeof this.session.webRequest !== 'object') return;
40
+ this._installed = true;
41
+ try {
42
+ this.session.webRequest.onHeadersReceived(
43
+ { urls: ['*://*/*'], resourceType: ['mainFrame'] },
44
+ (details, callback) => {
45
+ let out = null;
46
+ try {
47
+ out = this._maybeInline(details);
48
+ } catch (_) {
49
+ out = null; // 判据出错就放行,绝不吞响应
50
+ }
51
+ callback(out || {});
52
+ }
53
+ );
54
+ } catch (_) {
55
+ // 装不上只是回到"图片被下载"的旧行为,不该让启动失败。
56
+ this._installed = false;
57
+ }
58
+ }
59
+
60
+ _maybeInline(details) {
61
+ this.stats.seen++;
62
+ const headers = details.responseHeaders;
63
+ if (!mediaDoc.shouldRenderInline(headers)) return null;
64
+
65
+ const stripped = mediaDoc.stripDisposition(headers);
66
+ if (!stripped.changed) return null;
67
+
68
+ const mime = mediaDoc.mimeOf(headers);
69
+ const tab = this._resolveTab(details.webContentsId);
70
+ if (tab !== undefined && tab !== null) {
71
+ const rec = {
72
+ tab,
73
+ url: details.url,
74
+ mime,
75
+ kind: mediaDoc.kindOf(mime),
76
+ at: Date.now(),
77
+ };
78
+ this._byTab.set(tab, rec);
79
+ if (this._byTab.size > MAX_REMEMBER) {
80
+ const oldest = this._byTab.keys().next();
81
+ if (!oldest.done) this._byTab.delete(oldest.value);
82
+ }
83
+ try { this._onMedia(rec); } catch (_) { /* UI 更新失败不影响这次响应 */ }
84
+ }
85
+ this.stats.inlined++;
86
+ return { responseHeaders: stripped.headers };
87
+ }
88
+
89
+ /**
90
+ * 这个 tab 当前显示的是不是一份"被救回来的"文档。
91
+ * 只在**地址也对得上**时才认:用户从图片页再往下点,旧的记录必须失效,
92
+ * 否则工具条会挂着一个对当前页毫无意义的下载按钮。
93
+ */
94
+ recordOf(tab, currentUrl) {
95
+ const rec = this._byTab.get(tab);
96
+ if (!rec) return null;
97
+ const cur = String(currentUrl || '');
98
+ if (cur && cur !== rec.url) return null;
99
+ return rec;
100
+ }
101
+
102
+ forget(tab) { this._byTab.delete(tab); }
103
+ clearAll() { this._byTab.clear(); }
104
+ }