dsh-browser-firefox 0.3.3 → 0.4.1
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/lib/index.js +396 -220
- package/lib/protocol.js +42 -13
- package/lib/types/events.d.ts +59 -0
- package/lib/types/gateway.d.ts +87 -0
- package/lib/types/index.d.ts +7 -5
- package/lib/types/protocol.d.ts +52 -20
- package/lib/types/server.d.ts +49 -19
- package/lib/types/session-deferral.d.ts +12 -13
- package/lib/types/session-workspace.d.ts +14 -9
- package/lib/types/tools.d.ts +1 -1
- package/package.json +29 -25
- package/src/index.ts +37 -18
- package/src/protocol.ts +75 -22
- package/src/server.ts +239 -79
- package/src/session-deferral.ts +61 -66
- package/src/session-workspace.ts +46 -39
- package/src/tools.ts +52 -3
package/src/session-workspace.ts
CHANGED
|
@@ -1,34 +1,47 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Best-effort workspace grouping for sessions created through the browser
|
|
3
|
-
* bridge. The
|
|
4
|
-
* explicit workspace choices and every other
|
|
3
|
+
* bridge. The interceptor changes only implicit `session/create` requests;
|
|
4
|
+
* explicit workspace choices and every other Remote endpoint pass through.
|
|
5
5
|
* @module @deepseek-ai/dsh-bridge-browser/src/session-workspace
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import { randomUUID } from 'node:crypto'
|
|
9
8
|
import { mkdir } from 'node:fs/promises'
|
|
10
|
-
import type {
|
|
11
|
-
import {
|
|
9
|
+
import type { WorkspaceId } from '@deepseek-ai/dsh-api-workspace-controller/types'
|
|
10
|
+
import type { RemoteWireResult } from './protocol.ts'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Unary Remote dispatch seam: slash-joined endpoint, native args object,
|
|
14
|
+
* RemoteResult wire form. The bridge server and the interceptors share it.
|
|
15
|
+
*/
|
|
16
|
+
export type RpcDispatch = (method: string, args: Record<string, unknown>, signal: AbortSignal) => Promise<RemoteWireResult>
|
|
12
17
|
|
|
13
18
|
type Warn = (message: string) => void
|
|
14
19
|
|
|
20
|
+
/** Extract the `request` arg object from one dispatch's native args, when present. */
|
|
21
|
+
function requestArg(args: Record<string, unknown>): Record<string, unknown> {
|
|
22
|
+
const request = args.request
|
|
23
|
+
return typeof request === 'object' && request !== null && !Array.isArray(request)
|
|
24
|
+
? request as Record<string, unknown>
|
|
25
|
+
: {}
|
|
26
|
+
}
|
|
27
|
+
|
|
15
28
|
/**
|
|
16
|
-
* Add
|
|
29
|
+
* Add the dedicated Workspace to implicit session creation without making
|
|
17
30
|
* grouping a session-creation dependency. The first implicit create mkdirs
|
|
18
|
-
* and registers the configured path; that result,
|
|
19
|
-
* cached for the
|
|
31
|
+
* and registers the configured path (`workspace/create`); that result,
|
|
32
|
+
* including failure, is cached for the interceptor lifetime.
|
|
20
33
|
*
|
|
21
|
-
* @param
|
|
34
|
+
* @param dispatch - Inner Remote dispatch.
|
|
22
35
|
* @param workspacePath - Dedicated directory, or an empty string to opt out.
|
|
23
36
|
* @param warn - Logger called once when grouping cannot be established.
|
|
24
|
-
* @returns the original
|
|
37
|
+
* @returns the original dispatch for opt-out, otherwise a dispatch with intercepted session creation.
|
|
25
38
|
*/
|
|
26
39
|
export function withSessionWorkspace(
|
|
27
|
-
|
|
40
|
+
dispatch: RpcDispatch,
|
|
28
41
|
workspacePath: string,
|
|
29
42
|
warn: Warn,
|
|
30
|
-
):
|
|
31
|
-
if (workspacePath === '') return
|
|
43
|
+
): RpcDispatch {
|
|
44
|
+
if (workspacePath === '') return dispatch
|
|
32
45
|
|
|
33
46
|
let workspacePromise: Promise<WorkspaceId | undefined> | undefined
|
|
34
47
|
const ensureWorkspace = (): Promise<WorkspaceId | undefined> => {
|
|
@@ -36,23 +49,21 @@ export function withSessionWorkspace(
|
|
|
36
49
|
workspacePromise = (async () => {
|
|
37
50
|
try {
|
|
38
51
|
await mkdir(workspacePath, { recursive: true })
|
|
39
|
-
const
|
|
40
|
-
if (
|
|
41
|
-
warn(`browser bridge: workspace API is unavailable; sessions will remain ungrouped`)
|
|
42
|
-
return undefined
|
|
43
|
-
}
|
|
44
|
-
const response = await workspaceApi.create({
|
|
45
|
-
rpcId: RpcId(randomUUID()),
|
|
46
|
-
payload: { path: workspacePath },
|
|
47
|
-
})
|
|
48
|
-
if (!response.result.ok) {
|
|
52
|
+
const result = await dispatch('workspace/create', { request: { path: workspacePath } }, new AbortController().signal)
|
|
53
|
+
if (!result.ok) {
|
|
49
54
|
warn(
|
|
50
|
-
`browser bridge: workspace
|
|
51
|
-
+ `(${
|
|
55
|
+
`browser bridge: workspace/create failed for "${workspacePath}" `
|
|
56
|
+
+ `(${result.error.code}: ${result.error.message}); sessions will remain ungrouped`,
|
|
52
57
|
)
|
|
53
58
|
return undefined
|
|
54
59
|
}
|
|
55
|
-
|
|
60
|
+
const workspace = (result.value as { workspace?: { workspaceId?: unknown } }).workspace
|
|
61
|
+
const workspaceId = workspace?.workspaceId
|
|
62
|
+
if (typeof workspaceId !== 'string') {
|
|
63
|
+
warn(`browser bridge: workspace/create for "${workspacePath}" returned no workspace id; sessions will remain ungrouped`)
|
|
64
|
+
return undefined
|
|
65
|
+
}
|
|
66
|
+
return workspaceId as WorkspaceId
|
|
56
67
|
} catch (error: unknown) {
|
|
57
68
|
warn(
|
|
58
69
|
`browser bridge: could not prepare session workspace "${workspacePath}": `
|
|
@@ -64,18 +75,14 @@ export function withSessionWorkspace(
|
|
|
64
75
|
return workspacePromise
|
|
65
76
|
}
|
|
66
77
|
|
|
67
|
-
return {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
delete payload.cwd
|
|
77
|
-
return api.sessions.create({ ...request, payload })
|
|
78
|
-
},
|
|
79
|
-
},
|
|
78
|
+
return async (method, args, signal) => {
|
|
79
|
+
if (method !== 'session/create') return dispatch(method, args, signal)
|
|
80
|
+
const request = requestArg(args)
|
|
81
|
+
if (request.workspaceId !== undefined) return dispatch(method, args, signal)
|
|
82
|
+
const workspaceId = await ensureWorkspace()
|
|
83
|
+
if (workspaceId === undefined) return dispatch(method, args, signal)
|
|
84
|
+
const next: Record<string, unknown> = { ...request, workspaceId }
|
|
85
|
+
delete next.cwd
|
|
86
|
+
return dispatch(method, { ...args, request: next }, signal)
|
|
80
87
|
}
|
|
81
88
|
}
|
package/src/tools.ts
CHANGED
|
@@ -58,7 +58,7 @@ async function saveScreenshot(dataUrl: string, workspacePath: string): Promise<s
|
|
|
58
58
|
if (encoded === undefined) throw new Error('data URL 缺少 base64 内容')
|
|
59
59
|
const dir = screenshotDir(workspacePath)
|
|
60
60
|
await mkdir(dir, { recursive: true })
|
|
61
|
-
const file = join(dir, `screenshot-${Date.now()}.png`)
|
|
61
|
+
const file = join(dir, `screenshot-${Date.now()}-${Math.random().toString(36).slice(2, 8)}.png`)
|
|
62
62
|
await writeFile(file, Buffer.from(encoded, 'base64'))
|
|
63
63
|
await pruneScreenshots(dir)
|
|
64
64
|
return file
|
|
@@ -68,7 +68,7 @@ async function saveScreenshot(dataUrl: string, workspacePath: string): Promise<s
|
|
|
68
68
|
async function pruneScreenshots(dir: string): Promise<void> {
|
|
69
69
|
let files: string[]
|
|
70
70
|
try {
|
|
71
|
-
files = (await readdir(dir)).filter((f) => /^screenshot-\d
|
|
71
|
+
files = (await readdir(dir)).filter((f) => /^screenshot-\d+(?:-[a-z0-9]+)?\.png$/.test(f)).sort()
|
|
72
72
|
} catch {
|
|
73
73
|
return
|
|
74
74
|
}
|
|
@@ -132,6 +132,9 @@ export const BROWSER_TOOL_NAMES = [
|
|
|
132
132
|
'browser_evaluate',
|
|
133
133
|
'browser_screenshot',
|
|
134
134
|
'browser_list_tabs',
|
|
135
|
+
'browser_open_tab',
|
|
136
|
+
'browser_follow_tab',
|
|
137
|
+
'browser_close_tab',
|
|
135
138
|
] as const
|
|
136
139
|
|
|
137
140
|
/**
|
|
@@ -236,6 +239,7 @@ function defineTools(call: Call, options: BrowserToolsOptions): ToolDefinition[]
|
|
|
236
239
|
name: 'browser_snapshot',
|
|
237
240
|
description: '读取当前浏览器页面的结构化文本快照(无截图):标题、URL、正文摘要、带编号的可交互元素清单、表单字段。'
|
|
238
241
|
+ '编号是后续 browser_click/browser_type 等操作的定位依据。页面未变化时设置 delta=true 只返回变化部分,节省上下文。'
|
|
242
|
+
+ '页面含 iframe 时自动聚合:主帧占 80% 预算,各 iframe 小节附在后面(其编号配合 frame 参数使用)。'
|
|
239
243
|
+ UNTRUSTED_CONTENT_WARNING,
|
|
240
244
|
parameters: argsSchema({
|
|
241
245
|
delta: { type: 'boolean', description: 'true 时只返回相对上次快照的变化(编号、URL、标题)。默认 false 返回完整快照。' },
|
|
@@ -258,6 +262,7 @@ function defineTools(call: Call, options: BrowserToolsOptions): ToolDefinition[]
|
|
|
258
262
|
parameters: argsSchema({
|
|
259
263
|
index: { type: 'number', description: 'browser_snapshot 清单中的元素编号(与 selector 二选一)。' },
|
|
260
264
|
selector: { type: 'string', description: 'CSS 选择器(与 index 二选一)。' },
|
|
265
|
+
frame: { type: 'number', description: '目标 iframe 的 frameId(默认 0=主帧;iframe 内元素编号见快照的 iframe 小节)。' },
|
|
261
266
|
}),
|
|
262
267
|
timeoutMs: options.toolTimeoutMs,
|
|
263
268
|
output: TEXT_OUTPUT,
|
|
@@ -272,15 +277,17 @@ function defineTools(call: Call, options: BrowserToolsOptions): ToolDefinition[]
|
|
|
272
277
|
index: { type: 'number', description: '表单字段编号(来自 browser_snapshot 的 forms 清单)。' },
|
|
273
278
|
text: { type: 'string', description: '要输入的文本。' },
|
|
274
279
|
replace: { type: 'boolean', description: 'true 时清空现有值后输入。默认追加。' },
|
|
280
|
+
frame: { type: 'number', description: '目标 iframe 的 frameId(默认 0=主帧)。' },
|
|
275
281
|
}, ['index', 'text']),
|
|
276
282
|
timeoutMs: options.toolTimeoutMs,
|
|
277
283
|
output: TEXT_OUTPUT,
|
|
278
284
|
execute: (args, exec) => {
|
|
279
|
-
const a = args as { index: number; text: string; replace?: boolean }
|
|
285
|
+
const a = args as { index: number; text: string; replace?: boolean; frame?: number }
|
|
280
286
|
return call(exec, 'browser_type', {
|
|
281
287
|
index: a.index,
|
|
282
288
|
text: a.text,
|
|
283
289
|
...a.replace !== undefined ? { replace: a.replace } : {},
|
|
290
|
+
...a.frame !== undefined ? { frame: a.frame } : {},
|
|
284
291
|
})
|
|
285
292
|
},
|
|
286
293
|
})
|
|
@@ -372,6 +379,7 @@ function defineTools(call: Call, options: BrowserToolsOptions): ToolDefinition[]
|
|
|
372
379
|
action: { type: 'string', enum: ['count', 'getText', 'click', 'setValue', 'querySelectorAll'], description: '操作类型。' },
|
|
373
380
|
selector: { type: 'string', description: 'CSS 选择器。' },
|
|
374
381
|
index: { type: 'number', description: '匹配元素的序号(默认 0)。' },
|
|
382
|
+
frame: { type: 'number', description: '目标 iframe 的 frameId(默认 0=主帧)。' },
|
|
375
383
|
value: { type: 'string', description: 'setValue 时设置的值。' },
|
|
376
384
|
}, ['action', 'selector']),
|
|
377
385
|
timeoutMs: options.toolTimeoutMs,
|
|
@@ -393,5 +401,46 @@ function defineTools(call: Call, options: BrowserToolsOptions): ToolDefinition[]
|
|
|
393
401
|
wait(),
|
|
394
402
|
evaluate(),
|
|
395
403
|
simple('browser_list_tabs', '列出当前会话标签页组中的所有标签页(id、标题、URL,当前工作的用 * 标记)。'),
|
|
404
|
+
...tabOps(call, options),
|
|
405
|
+
]
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/** 标签页管理三件套:open_tab 开新页、follow_tab 切换工作页、close_tab 关闭本会话的页。 */
|
|
409
|
+
function tabOps(call: Call, options: BrowserToolsOptions): ToolDefinition[] {
|
|
410
|
+
return [
|
|
411
|
+
{
|
|
412
|
+
name: 'browser_open_tab',
|
|
413
|
+
description: '在后台新标签页打开一个 URL(归入当前会话的标签页组),返回标签页 id。'
|
|
414
|
+
+ '要操作其中内容需先用 browser_follow_tab 或直接 browser_navigate(自带快照)。',
|
|
415
|
+
parameters: argsSchema({
|
|
416
|
+
url: { type: 'string', description: '完整 http/https URL。' },
|
|
417
|
+
title: { type: 'string', description: '任务标题(用于标签页组命名,可选)。' },
|
|
418
|
+
}, ['url']),
|
|
419
|
+
timeoutMs: options.toolTimeoutMs,
|
|
420
|
+
output: TEXT_OUTPUT,
|
|
421
|
+
execute: (args, exec) => call(exec, 'browser_open_tab', args as Record<string, unknown>),
|
|
422
|
+
},
|
|
423
|
+
{
|
|
424
|
+
name: 'browser_follow_tab',
|
|
425
|
+
description: '把当前会话的工作标签页切换到指定 id(后续 snapshot/click 等操作都作用于它)。'
|
|
426
|
+
+ '用 browser_list_tabs 查看可用 id。',
|
|
427
|
+
parameters: argsSchema({
|
|
428
|
+
tabId: { type: 'number', description: '目标标签页 id。' },
|
|
429
|
+
}, ['tabId']),
|
|
430
|
+
timeoutMs: options.toolTimeoutMs,
|
|
431
|
+
output: TEXT_OUTPUT,
|
|
432
|
+
execute: (args, exec) => call(exec, 'browser_follow_tab', args as Record<string, unknown>),
|
|
433
|
+
},
|
|
434
|
+
{
|
|
435
|
+
name: 'browser_close_tab',
|
|
436
|
+
description: '关闭一个标签页(默认当前工作页)。只能关闭本会话打开/归入本组的标签页,'
|
|
437
|
+
+ '用户自己的标签页会被拒绝。',
|
|
438
|
+
parameters: argsSchema({
|
|
439
|
+
tabId: { type: 'number', description: '要关闭的标签页 id(缺省为当前工作页)。' },
|
|
440
|
+
}, []),
|
|
441
|
+
timeoutMs: options.toolTimeoutMs,
|
|
442
|
+
output: TEXT_OUTPUT,
|
|
443
|
+
execute: (args, exec) => call(exec, 'browser_close_tab', args as Record<string, unknown>),
|
|
444
|
+
},
|
|
396
445
|
]
|
|
397
446
|
}
|