@linxin666/dsh-client-ui-browser-view 0.4.13 → 0.4.14
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.i18n.yaml +2 -2
- package/README.md +10 -0
- package/README.zh.md +10 -0
- package/lib/index.js +238 -18
- package/lib/types/core/screenshot.d.ts +94 -0
- package/lib/types/core/tabs.d.ts +27 -3
- package/lib/types/host/service.d.ts +18 -3
- package/lib/types/host/tools.d.ts +4 -4
- package/package.json +1 -1
package/README.i18n.yaml
CHANGED
|
@@ -2,5 +2,5 @@
|
|
|
2
2
|
# side as of the last confirmed-consistent state. Both languages carry equal authority;
|
|
3
3
|
# after editing either side, bring the other along and re-record with:
|
|
4
4
|
# node scripts/verify-docs.mjs --write <dir>
|
|
5
|
-
README.md:
|
|
6
|
-
README.zh.md:
|
|
5
|
+
README.md: c36310fc8838ff5d6421e5c1e1d437b61bc13df4
|
|
6
|
+
README.zh.md: 4b4daa790ee6047b4173776cde36598c10f2651e
|
package/README.md
CHANGED
|
@@ -32,6 +32,15 @@ agent tool call ──► host half ──► CDP ──► its own Chromium, or
|
|
|
32
32
|
|
|
33
33
|
In the default isolated mode the host half starts Chrome lazily on the first tool call, pins a viewport, and keeps a ring of console entries and network requests per tab so a verification can be read back without re-running the page. In CDP mode a tab listing or a panel attach opens the same kind of session against the configured endpoint and starts no process of its own. A top-level document replacement clears both rings; a same-document navigation (hash, query, SPA pushState) keeps them.
|
|
34
34
|
|
|
35
|
+
## Screenshot fidelity
|
|
36
|
+
|
|
37
|
+
`browser_screenshot` captures a lossless PNG by default: a screenshot is usually looked at more than once, and annotation, thin borders and small labels stay legible instead of turning into JPEG ringing. `format` switches to `jpeg` when a smaller result matters more than exact pixels.
|
|
38
|
+
|
|
39
|
+
- **Capture a region, not the page**: `clip` takes `{ x, y, width, height }` in CSS pixels, the same coordinate space as a `browser_snapshot` box and the `x`/`y` of `browser_click`. A clipped capture is rendered at 2x unless `scale` says otherwise.
|
|
40
|
+
- **`scale` is 1, 2 or 3**: an explicit value always wins over the clip default.
|
|
41
|
+
- **Why the extra pixels**: on the model side one visual token cell covers roughly 42x42 px of the image, so a whole-viewport capture is already coarse. To read detail, take the box from `browser_snapshot` and capture only that region; at 2x the same element occupies several times the tokens it did in the full frame.
|
|
42
|
+
- **Out of bounds fails loudly**: a `clip` that leaves the pinned viewport (isolated mode, 1280x800) or the tab's real layout viewport (real-browser mode), or an encoded image over the host's attachment budget, is an error naming the reason. No image comes back for a region that was never rendered.
|
|
43
|
+
|
|
35
44
|
## Interface seats
|
|
36
45
|
|
|
37
46
|
| Seat | Kind | What lands there |
|
|
@@ -91,6 +100,7 @@ Sign in to the sites the agent should see inside that profile once; the plugin d
|
|
|
91
100
|
- The token top-up only covers the site root, because DSH answers the challenge with a 303 to `./` for `GET /` alone.
|
|
92
101
|
- One browser per host; tabs are isolated CDP targets, not separate profiles.
|
|
93
102
|
- Chrome must be installed at a standard location (or named by `CHROME_PATH`) for the isolated mode, which is the only mode that launches a browser.
|
|
103
|
+
- In the default isolated mode the viewport is pinned at 1280x800, so a `clip` must fall inside it: a region outside it is refused rather than scrolled into place. In real-browser mode the bound is the tab's own layout viewport.
|
|
94
104
|
|
|
95
105
|
## Install
|
|
96
106
|
|
package/README.zh.md
CHANGED
|
@@ -32,6 +32,15 @@ agent 工具调用 ──► 宿主半区 ──► CDP ──► 它自己的 C
|
|
|
32
32
|
|
|
33
33
|
默认的隔离模式下,宿主半区在第一次工具调用时才懒启动 Chrome,钉住视口尺寸,并按标签页保留控制台与网络请求的环形记录,因此一次验证不必重跑页面就能回读。接管模式下,列出标签页或挂载面板会对着配置的端点开同样形态的会话,自己不启动任何进程。顶层文档被替换时两份记录都会清空;同文档导航(hash、query、SPA pushState)则保留。
|
|
34
34
|
|
|
35
|
+
## 截图保真
|
|
36
|
+
|
|
37
|
+
`browser_screenshot` 默认截无损 PNG:截图往往要被再看一遍,标注、细边框与小字号标签保持清晰,不会糊成 JPEG 的振铃。体积比像素更重要时用 `format` 切到 `jpeg`。
|
|
38
|
+
|
|
39
|
+
- **截一块,而不是整页**:`clip` 收 `{ x, y, width, height }`(CSS 像素),与 `browser_snapshot` 的 box、`browser_click` 的 `x`/`y` 是同一坐标系。带 clip 时默认按 2 倍渲染,`scale` 可以改。
|
|
40
|
+
- **`scale` 只取 1、2、3**:显式给出的值总是优先于 clip 的默认值。
|
|
41
|
+
- **为什么要这些额外像素**:模型侧一个视觉 token 格大致覆盖 42x42 像素,整屏截图本来就偏粗。要看细节,先从 `browser_snapshot` 取 box,再只截那一块;2 倍下同一个元素占用的 token 是整屏里的数倍。
|
|
42
|
+
- **越界会明确报错**:`clip` 落到被钉住的视口(隔离模式,1280x800)或标签页真实视口(接管模式)之外,或编码后超出宿主的附件体积预算,都会作为错误报出原因。没有渲染出来的区域不会返回图片。
|
|
43
|
+
|
|
35
44
|
## 界面席位
|
|
36
45
|
|
|
37
46
|
| 席位 | 类型 | 放什么 |
|
|
@@ -91,6 +100,7 @@ agent 工具调用 ──► 宿主半区 ──► CDP ──► 它自己的 C
|
|
|
91
100
|
- 补票只覆盖站点根地址,因为 DSH 只在 `GET /` 时以 303 回应鉴权提示。
|
|
92
101
|
- 一个宿主一个浏览器;标签页是相互隔离的 CDP target,不是独立 profile。
|
|
93
102
|
- 隔离模式需要 Chrome 装在标准位置(或用 `CHROME_PATH` 指定);它也是唯一会拉起浏览器的模式。
|
|
103
|
+
- 默认的隔离模式下视口被钉在 1280x800,因此 `clip` 必须落在视口内;视口外的区域直接报错,而不是滚过去再截。接管模式下边界是该标签页自己的布局视口。
|
|
94
104
|
|
|
95
105
|
## 安装
|
|
96
106
|
|
package/lib/index.js
CHANGED
|
@@ -919,17 +919,25 @@ var TabManager = class {
|
|
|
919
919
|
await this.transport.send("Page.stopScreencast", {}, record.sessionId);
|
|
920
920
|
}
|
|
921
921
|
/**
|
|
922
|
-
* Capture one
|
|
922
|
+
* Capture one screenshot on demand.
|
|
923
|
+
*
|
|
924
|
+
* Without explicit parameters this is exactly the frame the screencast path
|
|
925
|
+
* has always asked for: a JPEG at the configured quality, bounded to the
|
|
926
|
+
* viewport. A caller that wants a clip or a scale plans it with
|
|
927
|
+
* `planScreenshot` and passes those parameters, so this method stays the
|
|
928
|
+
* single send point without deciding any of it.
|
|
923
929
|
* @param tabId - target tab.
|
|
924
|
-
* @
|
|
930
|
+
* @param params - CDP parameters from `planScreenshot`; omitted keeps the plain JPEG capture.
|
|
931
|
+
* @returns base64 payload without the `data:` prefix.
|
|
925
932
|
*/
|
|
926
|
-
async captureScreenshot(tabId) {
|
|
933
|
+
async captureScreenshot(tabId, params) {
|
|
927
934
|
const record = this.requireRecord(tabId);
|
|
928
|
-
const
|
|
935
|
+
const request = params ?? {
|
|
929
936
|
format: "jpeg",
|
|
930
937
|
quality: this.jpegQuality,
|
|
931
938
|
captureBeyondViewport: false
|
|
932
|
-
}
|
|
939
|
+
};
|
|
940
|
+
const result = await this.transport.send("Page.captureScreenshot", { ...request }, record.sessionId);
|
|
933
941
|
if (typeof result.data !== "string" || result.data === "") throw new Error("the browser returned an empty screenshot");
|
|
934
942
|
return result.data;
|
|
935
943
|
}
|
|
@@ -956,6 +964,22 @@ var TabManager = class {
|
|
|
956
964
|
this.onFrame?.(frame);
|
|
957
965
|
return frame;
|
|
958
966
|
}
|
|
967
|
+
/**
|
|
968
|
+
* Report the CSS viewport a capture of this tab renders.
|
|
969
|
+
*
|
|
970
|
+
* A pinned tab answers from configuration, so this costs no protocol
|
|
971
|
+
* round-trip. A tab this host adopted from a real browser is never pinned —
|
|
972
|
+
* the page keeps whatever size its window has — so the real CSS layout
|
|
973
|
+
* viewport is read from the page instead. Screenshot planning needs exactly
|
|
974
|
+
* this number: clip bounds and the output budget are both expressed in the
|
|
975
|
+
* viewport the pixels are measured against.
|
|
976
|
+
* @param tabId - target tab.
|
|
977
|
+
* @returns the CSS viewport size in pixels.
|
|
978
|
+
*/
|
|
979
|
+
async viewportOf(tabId) {
|
|
980
|
+
const record = this.requireRecord(tabId);
|
|
981
|
+
return await this.frameViewport(record);
|
|
982
|
+
}
|
|
959
983
|
/** Read the real CSS viewport without changing it in external-browser mode. */
|
|
960
984
|
async frameViewport(record) {
|
|
961
985
|
if (this.pinViewports) return this.viewport;
|
|
@@ -1385,6 +1409,136 @@ async function buildSnapshot(transport, tabId, sessionId, options = {}) {
|
|
|
1385
1409
|
return assembled;
|
|
1386
1410
|
}
|
|
1387
1411
|
//#endregion
|
|
1412
|
+
//#region src/core/screenshot.ts
|
|
1413
|
+
/** Matches the attachment service's `normalizedImageMaxPixels`: 2048*2048. */
|
|
1414
|
+
const SCREENSHOT_MAX_OUTPUT_PIXELS = 2048 * 2048;
|
|
1415
|
+
/** Longest encoded side CDP renders in one capture. */
|
|
1416
|
+
const SCREENSHOT_MAX_SIDE = 8192;
|
|
1417
|
+
/** Scale a clip-only request renders at: a crop is a detail view. */
|
|
1418
|
+
const DEFAULT_CLIP_SCALE = 2;
|
|
1419
|
+
/**
|
|
1420
|
+
* Describe one requested rectangle for an error message.
|
|
1421
|
+
* @param clip - the rectangle as the caller asked for it.
|
|
1422
|
+
* @returns a compact `width x height at x,y` reading.
|
|
1423
|
+
*/
|
|
1424
|
+
function describeClip(clip) {
|
|
1425
|
+
return `${clip.width}x${clip.height} at (${clip.x}, ${clip.y})`;
|
|
1426
|
+
}
|
|
1427
|
+
/**
|
|
1428
|
+
* Reject a format outside png/jpeg.
|
|
1429
|
+
*
|
|
1430
|
+
* The tool schema already constrains this; a second guard here is what keeps a
|
|
1431
|
+
* caller that reaches the core directly from asking CDP for an unknown encoder
|
|
1432
|
+
* and receiving an opaque protocol error.
|
|
1433
|
+
* @param format - the requested format.
|
|
1434
|
+
* @throws when the format is neither `png` nor `jpeg`.
|
|
1435
|
+
*/
|
|
1436
|
+
function assertFormat(format) {
|
|
1437
|
+
if (format !== "png" && format !== "jpeg") throw new Error(`screenshot format must be "png" or "jpeg"; received ${String(format)}`);
|
|
1438
|
+
}
|
|
1439
|
+
/**
|
|
1440
|
+
* Reject a scale outside the allowed range.
|
|
1441
|
+
* @param scale - the requested scale.
|
|
1442
|
+
* @throws when the scale is not a whole number from 1 to {@link SCREENSHOT_MAX_SCALE}.
|
|
1443
|
+
*/
|
|
1444
|
+
function assertScale(scale) {
|
|
1445
|
+
if (!Number.isInteger(scale) || scale < 1 || scale > 3) throw new Error(`screenshot scale must be an integer from 1 to 3; received ${scale}`);
|
|
1446
|
+
}
|
|
1447
|
+
/**
|
|
1448
|
+
* Reject a clip CDP could not honour.
|
|
1449
|
+
*
|
|
1450
|
+
* Fractional edges make the encoded size disagree with the reported size, a
|
|
1451
|
+
* zero or negative extent has no pixels, and a negative origin addresses
|
|
1452
|
+
* something outside the page.
|
|
1453
|
+
* @param clip - the requested rectangle.
|
|
1454
|
+
* @throws when any edge is fractional, the origin is negative, or an extent is below one pixel.
|
|
1455
|
+
*/
|
|
1456
|
+
function assertClip(clip) {
|
|
1457
|
+
if (![
|
|
1458
|
+
clip.x,
|
|
1459
|
+
clip.y,
|
|
1460
|
+
clip.width,
|
|
1461
|
+
clip.height
|
|
1462
|
+
].every((edge) => Number.isInteger(edge))) throw new Error(`screenshot clip must use whole pixels; received ${describeClip(clip)}`);
|
|
1463
|
+
if (clip.x < 0 || clip.y < 0) throw new Error(`screenshot clip must not start outside the viewport; received ${describeClip(clip)}`);
|
|
1464
|
+
if (clip.width < 1 || clip.height < 1) throw new Error(`screenshot clip must be at least 1x1 pixel; received ${describeClip(clip)}`);
|
|
1465
|
+
}
|
|
1466
|
+
/**
|
|
1467
|
+
* Reject a clip that reaches past the viewport.
|
|
1468
|
+
*
|
|
1469
|
+
* Reporting the real viewport is what lets the caller correct the rectangle
|
|
1470
|
+
* without a second round-trip to find out how large the page actually is.
|
|
1471
|
+
* @param clip - the requested rectangle.
|
|
1472
|
+
* @param viewport - the viewport the capture renders.
|
|
1473
|
+
* @throws when the clip crosses the right or bottom edge.
|
|
1474
|
+
*/
|
|
1475
|
+
function assertInside(clip, viewport) {
|
|
1476
|
+
if (clip.x + clip.width > viewport.width || clip.y + clip.height > viewport.height) throw new Error(`screenshot clip must stay inside the ${viewport.width}x${viewport.height} viewport; received ${describeClip(clip)}`);
|
|
1477
|
+
}
|
|
1478
|
+
/**
|
|
1479
|
+
* Reject a capture whose encoded size would be unusable.
|
|
1480
|
+
* @param width - base width in CSS pixels, before scaling.
|
|
1481
|
+
* @param height - base height in CSS pixels, before scaling.
|
|
1482
|
+
* @param scale - scale the capture renders at.
|
|
1483
|
+
* @throws when a side exceeds {@link SCREENSHOT_MAX_SIDE} or the total exceeds {@link SCREENSHOT_MAX_OUTPUT_PIXELS}.
|
|
1484
|
+
*/
|
|
1485
|
+
function assertOutputSize(width, height, scale) {
|
|
1486
|
+
const encodedWidth = width * scale;
|
|
1487
|
+
const encodedHeight = height * scale;
|
|
1488
|
+
if (encodedWidth > SCREENSHOT_MAX_SIDE || encodedHeight > SCREENSHOT_MAX_SIDE) throw new Error(`a ${width}x${height} screenshot at scale ${scale} would be ${encodedWidth}x${encodedHeight} pixels, over the ${SCREENSHOT_MAX_SIDE}-pixel side limit; lower the scale or shrink the clip`);
|
|
1489
|
+
const pixels = encodedWidth * encodedHeight;
|
|
1490
|
+
if (pixels > 4194304) throw new Error(`a ${width}x${height} screenshot at scale ${scale} would be ${pixels} pixels, over the ${SCREENSHOT_MAX_OUTPUT_PIXELS}-pixel output limit; lower the scale or shrink the clip`);
|
|
1491
|
+
}
|
|
1492
|
+
/**
|
|
1493
|
+
* Plan one screenshot capture.
|
|
1494
|
+
*
|
|
1495
|
+
* Pure: the same three arguments always produce the same plan, and nothing here
|
|
1496
|
+
* reads configuration, the clock, or the environment.
|
|
1497
|
+
* @param viewport - the CSS-pixel viewport the page is pinned to.
|
|
1498
|
+
* @param request - what the caller asked for; every field is optional.
|
|
1499
|
+
* @param jpegQuality - quality to request for a JPEG capture, 0-100.
|
|
1500
|
+
* @returns the CDP parameters, the media type, and the encoded pixel size.
|
|
1501
|
+
* @throws when the format, scale, clip, or resulting size cannot be honoured.
|
|
1502
|
+
*/
|
|
1503
|
+
function planScreenshot(viewport, request, jpegQuality) {
|
|
1504
|
+
const format = request.format ?? "png";
|
|
1505
|
+
assertFormat(format);
|
|
1506
|
+
const clip = request.clip;
|
|
1507
|
+
if (clip !== void 0) assertClip(clip);
|
|
1508
|
+
if (request.scale !== void 0) assertScale(request.scale);
|
|
1509
|
+
const scale = request.scale ?? (clip === void 0 ? 1 : DEFAULT_CLIP_SCALE);
|
|
1510
|
+
const width = clip === void 0 ? viewport.width : clip.width;
|
|
1511
|
+
const height = clip === void 0 ? viewport.height : clip.height;
|
|
1512
|
+
if (clip !== void 0) assertInside(clip, viewport);
|
|
1513
|
+
assertOutputSize(width, height, scale);
|
|
1514
|
+
const params = {
|
|
1515
|
+
format,
|
|
1516
|
+
captureBeyondViewport: false
|
|
1517
|
+
};
|
|
1518
|
+
if (format === "jpeg") params.quality = jpegQuality;
|
|
1519
|
+
if (clip !== void 0) params.clip = {
|
|
1520
|
+
x: clip.x,
|
|
1521
|
+
y: clip.y,
|
|
1522
|
+
width: clip.width,
|
|
1523
|
+
height: clip.height,
|
|
1524
|
+
scale
|
|
1525
|
+
};
|
|
1526
|
+
else if (scale > 1) params.clip = {
|
|
1527
|
+
x: 0,
|
|
1528
|
+
y: 0,
|
|
1529
|
+
width: viewport.width,
|
|
1530
|
+
height: viewport.height,
|
|
1531
|
+
scale
|
|
1532
|
+
};
|
|
1533
|
+
return {
|
|
1534
|
+
params,
|
|
1535
|
+
mediaType: format === "jpeg" ? "image/jpeg" : "image/png",
|
|
1536
|
+
width: width * scale,
|
|
1537
|
+
height: height * scale,
|
|
1538
|
+
scale
|
|
1539
|
+
};
|
|
1540
|
+
}
|
|
1541
|
+
//#endregion
|
|
1388
1542
|
//#region src/host/loopback.ts
|
|
1389
1543
|
/** IPv4 127/8 predicate (four decimal octets, first == 127). */
|
|
1390
1544
|
function isIPv4Loopback(v4) {
|
|
@@ -2435,21 +2589,36 @@ var BrowserViewService = class {
|
|
|
2435
2589
|
return result.snapshot;
|
|
2436
2590
|
}
|
|
2437
2591
|
/**
|
|
2438
|
-
* Capture one
|
|
2592
|
+
* Capture one image of the tab.
|
|
2593
|
+
*
|
|
2594
|
+
* The plan decides everything the caller is told about the bytes: the format
|
|
2595
|
+
* becomes the media type the attachment service stores, and the scale turns
|
|
2596
|
+
* one capture into a cropped render whose encoded size has to be reported
|
|
2597
|
+
* rather than guessed from the viewport. Planning before the capture is what
|
|
2598
|
+
* makes an impossible request fail here, with the numbers, instead of
|
|
2599
|
+
* producing a truncated or silently downscaled image. The viewport it plans
|
|
2600
|
+
* against is the tab's own: a pinned tab answers from configuration, and an
|
|
2601
|
+
* adopted real-browser tab reports the page's real layout viewport.
|
|
2439
2602
|
* @param sessionId - calling agent session.
|
|
2440
2603
|
* @param tabId - explicit tab.
|
|
2441
|
-
* @
|
|
2604
|
+
* @param request - format, clip, and scale; every field is optional.
|
|
2605
|
+
* @returns the tab, the base64 payload, and the media type and encoded size the plan produced.
|
|
2606
|
+
* @throws when the request cannot be honoured by this viewport or the size limits.
|
|
2442
2607
|
*/
|
|
2443
|
-
async screenshot(sessionId, tabId) {
|
|
2608
|
+
async screenshot(sessionId, tabId, request = {}) {
|
|
2444
2609
|
const tab = await this.tabFor(sessionId, tabId);
|
|
2445
2610
|
const tabs = await this.requireTabs();
|
|
2446
|
-
const
|
|
2611
|
+
const plan = planScreenshot(await tabs.viewportOf(tab.id), request, this.jpegQuality);
|
|
2612
|
+
const data = await tabs.captureScreenshot(tab.id, plan.params);
|
|
2447
2613
|
const current = tabs.list().find((candidate) => candidate.id === tab.id) ?? tab;
|
|
2448
2614
|
return {
|
|
2449
2615
|
tabId: current.id,
|
|
2450
2616
|
url: current.url,
|
|
2451
2617
|
title: current.title,
|
|
2452
|
-
data
|
|
2618
|
+
data,
|
|
2619
|
+
mediaType: plan.mediaType,
|
|
2620
|
+
width: plan.width,
|
|
2621
|
+
height: plan.height
|
|
2453
2622
|
};
|
|
2454
2623
|
}
|
|
2455
2624
|
/**
|
|
@@ -10145,8 +10314,8 @@ function toolAbortedBeforeDispatchResult(prior) {
|
|
|
10145
10314
|
* JSON, rendered as one pretty-printed text block: the model reads fields, not
|
|
10146
10315
|
* prose, and the GUI can present the same value later. `browser_screenshot`
|
|
10147
10316
|
* returns a real image part — an `ImageBlock` carrying a durable attachment
|
|
10148
|
-
* reference — because a
|
|
10149
|
-
* model that can see one.
|
|
10317
|
+
* reference — because a screenshot smuggled in as base64 text is not an image to
|
|
10318
|
+
* any model that can see one.
|
|
10150
10319
|
*
|
|
10151
10320
|
* The screenshot path therefore needs the host's attachment service: the bytes
|
|
10152
10321
|
* are committed through it, and the block only carries the reference. The
|
|
@@ -10160,6 +10329,10 @@ function toolAbortedBeforeDispatchResult(prior) {
|
|
|
10160
10329
|
*
|
|
10161
10330
|
* @module @linxin666/dsh-client-ui-browser-view/host/tools
|
|
10162
10331
|
*/
|
|
10332
|
+
/** File extension matching one screenshot media type. */
|
|
10333
|
+
function extensionOf(mediaType) {
|
|
10334
|
+
return mediaType === "image/png" ? "png" : "jpg";
|
|
10335
|
+
}
|
|
10163
10336
|
/** One text block. */
|
|
10164
10337
|
function text(value) {
|
|
10165
10338
|
return [{
|
|
@@ -10539,8 +10712,51 @@ function createBrowserTools(deps) {
|
|
|
10539
10712
|
}),
|
|
10540
10713
|
defineTool({
|
|
10541
10714
|
name: "browser_screenshot",
|
|
10542
|
-
description: "Capture the current tab as
|
|
10543
|
-
parameters: {
|
|
10715
|
+
description: "Capture the current tab as an image and return the image itself, not a description of it. Use it when the user should see the page, when layout or styling matters more than the DOM, or to check that something rendered correctly. The default is a lossless PNG of the whole visible viewport; scroll first if the region you need is off-screen. The full viewport is coarse to read: one visual token cell on the model side covers roughly 42x42 px of the image. To read detail, call browser_snapshot first, take the box of the node you care about, and capture just that region with clip (rendered at 2x unless scale says otherwise). 中文触发词:截图、看一下现在的页面、画面、放大看细节。",
|
|
10716
|
+
parameters: {
|
|
10717
|
+
clip: {
|
|
10718
|
+
type: "object",
|
|
10719
|
+
additionalProperties: false,
|
|
10720
|
+
description: "Region to capture, in CSS pixels inside the fixed viewport: the same coordinate space as a browser_snapshot box and the x/y of browser_click. Omit to capture the whole visible viewport.",
|
|
10721
|
+
properties: {
|
|
10722
|
+
x: {
|
|
10723
|
+
type: "number",
|
|
10724
|
+
required: true,
|
|
10725
|
+
description: "Left edge in CSS pixels."
|
|
10726
|
+
},
|
|
10727
|
+
y: {
|
|
10728
|
+
type: "number",
|
|
10729
|
+
required: true,
|
|
10730
|
+
description: "Top edge in CSS pixels."
|
|
10731
|
+
},
|
|
10732
|
+
width: {
|
|
10733
|
+
type: "number",
|
|
10734
|
+
required: true,
|
|
10735
|
+
description: "Region width in CSS pixels."
|
|
10736
|
+
},
|
|
10737
|
+
height: {
|
|
10738
|
+
type: "number",
|
|
10739
|
+
required: true,
|
|
10740
|
+
description: "Region height in CSS pixels."
|
|
10741
|
+
}
|
|
10742
|
+
}
|
|
10743
|
+
},
|
|
10744
|
+
scale: {
|
|
10745
|
+
type: "integer",
|
|
10746
|
+
enum: [
|
|
10747
|
+
1,
|
|
10748
|
+
2,
|
|
10749
|
+
3
|
|
10750
|
+
],
|
|
10751
|
+
description: "Render multiplier. Defaults to 2 when clip is given and 1 otherwise."
|
|
10752
|
+
},
|
|
10753
|
+
format: {
|
|
10754
|
+
type: "string",
|
|
10755
|
+
enum: ["png", "jpeg"],
|
|
10756
|
+
description: "Encoded image format; 'png' is lossless and the default."
|
|
10757
|
+
},
|
|
10758
|
+
tab_id: TAB_ID_PARAM
|
|
10759
|
+
},
|
|
10544
10760
|
output: {
|
|
10545
10761
|
schema: {
|
|
10546
10762
|
type: "object",
|
|
@@ -10586,7 +10802,7 @@ function createBrowserTools(deps) {
|
|
|
10586
10802
|
}
|
|
10587
10803
|
},
|
|
10588
10804
|
render: (_args, value) => {
|
|
10589
|
-
const envelope = `<path>browser-view:screenshot/${value.tabId}.
|
|
10805
|
+
const envelope = `<path>browser-view:screenshot/${value.tabId}.${extensionOf(value.image.mediaType)}</path>
|
|
10590
10806
|
<type>image</type>
|
|
10591
10807
|
<content>
|
|
10592
10808
|
${value.image.mediaType} screenshot, ${value.image.width}x${value.image.height} px, ${value.image.bytes} bytes
|
|
@@ -10603,11 +10819,15 @@ title: ${value.title}
|
|
|
10603
10819
|
execute: async (args, exec) => {
|
|
10604
10820
|
const saveImage = deps.saveImage;
|
|
10605
10821
|
if (saveImage === void 0) throw new Error("browser_screenshot needs the host attachment service to store the image; this host mounts none");
|
|
10606
|
-
const
|
|
10822
|
+
const request = {};
|
|
10823
|
+
if (args.format !== void 0) request.format = args.format;
|
|
10824
|
+
if (args.clip !== void 0) request.clip = args.clip;
|
|
10825
|
+
if (args.scale !== void 0) request.scale = args.scale;
|
|
10826
|
+
const shot = await service.screenshot(sessionIdOf(exec), args.tab_id, request);
|
|
10607
10827
|
const ref = await saveImage({
|
|
10608
10828
|
data: Buffer.from(shot.data, "base64"),
|
|
10609
|
-
mediaType:
|
|
10610
|
-
name: `browser-view-${shot.tabId}.
|
|
10829
|
+
mediaType: shot.mediaType,
|
|
10830
|
+
name: `browser-view-${shot.tabId}.${extensionOf(shot.mediaType)}`
|
|
10611
10831
|
});
|
|
10612
10832
|
return {
|
|
10613
10833
|
tabId: shot.tabId,
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Screenshot planning: the one place that turns a screenshot request into
|
|
3
|
+
* `Page.captureScreenshot` parameters and the pixel size the encoded image will
|
|
4
|
+
* have.
|
|
5
|
+
*
|
|
6
|
+
* Two things are decided here rather than at the call site. The format decides
|
|
7
|
+
* the media type the attachment service is asked to store, and a clip with a
|
|
8
|
+
* scale turns one capture into a cropped render, so both the tool result and the
|
|
9
|
+
* attachment metadata come from the same plan instead of being re-derived from
|
|
10
|
+
* each other.
|
|
11
|
+
*
|
|
12
|
+
* The limits are hard because every pixel is encoded, base64-encoded again, and
|
|
13
|
+
* pushed through the protocol: a full-viewport capture at scale 3 is 9.2 million
|
|
14
|
+
* pixels of JPEG on a socket, and a caller that asked for it by accident would
|
|
15
|
+
* stall the host rather than produce a screenshot anyone can read. The output
|
|
16
|
+
* budget matches the attachment service's own downscale threshold, so a plan
|
|
17
|
+
* that passes here never gets silently resized afterwards.
|
|
18
|
+
*
|
|
19
|
+
* @module @linxin666/dsh-client-ui-browser-view/core/screenshot
|
|
20
|
+
*/
|
|
21
|
+
/** Encoded image formats this package requests from CDP. */
|
|
22
|
+
export type ScreenshotFormat = 'png' | 'jpeg';
|
|
23
|
+
/** A rectangular crop in CSS pixels, relative to the viewport origin. */
|
|
24
|
+
export interface ScreenshotClip {
|
|
25
|
+
/** Left edge in CSS pixels. */
|
|
26
|
+
x: number;
|
|
27
|
+
/** Top edge in CSS pixels. */
|
|
28
|
+
y: number;
|
|
29
|
+
/** Crop width in CSS pixels. */
|
|
30
|
+
width: number;
|
|
31
|
+
/** Crop height in CSS pixels. */
|
|
32
|
+
height: number;
|
|
33
|
+
}
|
|
34
|
+
/** The tool-level screenshot request, before any planning. */
|
|
35
|
+
export interface ScreenshotRequest {
|
|
36
|
+
/** Encoded format. @default 'png' */
|
|
37
|
+
format?: ScreenshotFormat;
|
|
38
|
+
/** Crop to this rectangle instead of the whole viewport. */
|
|
39
|
+
clip?: ScreenshotClip;
|
|
40
|
+
/**
|
|
41
|
+
* Device pixels per CSS pixel. Defaults to 1 without a clip and to 2 with one,
|
|
42
|
+
* because a cropped capture is a detail view.
|
|
43
|
+
*/
|
|
44
|
+
scale?: number;
|
|
45
|
+
}
|
|
46
|
+
/** CDP `Page.captureScreenshot` parameters; the only place this package builds them. */
|
|
47
|
+
export interface CdpScreenshotParams {
|
|
48
|
+
/** Encoded format. */
|
|
49
|
+
format: ScreenshotFormat;
|
|
50
|
+
/** JPEG quality, 0-100; CDP ignores it for PNG. */
|
|
51
|
+
quality?: number;
|
|
52
|
+
/** Always false: capture only what the pinned viewport shows. */
|
|
53
|
+
captureBeyondViewport: false;
|
|
54
|
+
/** Optional crop plus the scale it renders at. */
|
|
55
|
+
clip?: {
|
|
56
|
+
x: number;
|
|
57
|
+
y: number;
|
|
58
|
+
width: number;
|
|
59
|
+
height: number;
|
|
60
|
+
scale: number;
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/** Everything the caller and the attachment need to know about one capture. */
|
|
64
|
+
export interface ScreenshotPlan {
|
|
65
|
+
/** Parameters to hand to `Page.captureScreenshot`. */
|
|
66
|
+
params: CdpScreenshotParams;
|
|
67
|
+
/** Media type of the encoded bytes. */
|
|
68
|
+
mediaType: 'image/png' | 'image/jpeg';
|
|
69
|
+
/** Encoded width in device pixels: the viewport or clip width times the scale. */
|
|
70
|
+
width: number;
|
|
71
|
+
/** Encoded height in device pixels: the viewport or clip height times the scale. */
|
|
72
|
+
height: number;
|
|
73
|
+
/** Scale the capture renders at. */
|
|
74
|
+
scale: number;
|
|
75
|
+
}
|
|
76
|
+
/** Matches the attachment service's `normalizedImageMaxPixels`: 2048*2048. */
|
|
77
|
+
export declare const SCREENSHOT_MAX_OUTPUT_PIXELS: number;
|
|
78
|
+
/** Largest scale a request may ask for. */
|
|
79
|
+
export declare const SCREENSHOT_MAX_SCALE = 3;
|
|
80
|
+
/**
|
|
81
|
+
* Plan one screenshot capture.
|
|
82
|
+
*
|
|
83
|
+
* Pure: the same three arguments always produce the same plan, and nothing here
|
|
84
|
+
* reads configuration, the clock, or the environment.
|
|
85
|
+
* @param viewport - the CSS-pixel viewport the page is pinned to.
|
|
86
|
+
* @param request - what the caller asked for; every field is optional.
|
|
87
|
+
* @param jpegQuality - quality to request for a JPEG capture, 0-100.
|
|
88
|
+
* @returns the CDP parameters, the media type, and the encoded pixel size.
|
|
89
|
+
* @throws when the format, scale, clip, or resulting size cannot be honoured.
|
|
90
|
+
*/
|
|
91
|
+
export declare function planScreenshot(viewport: {
|
|
92
|
+
width: number;
|
|
93
|
+
height: number;
|
|
94
|
+
}, request: ScreenshotRequest, jpegQuality: number): ScreenshotPlan;
|
package/lib/types/core/tabs.d.ts
CHANGED
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
*/
|
|
20
20
|
import type { CdpTransport } from './cdp.ts';
|
|
21
21
|
import { type BrowserConsoleEntry, type BrowserNetworkEntry, type BrowserTabView } from './protocol.ts';
|
|
22
|
+
import type { CdpScreenshotParams } from './screenshot.ts';
|
|
22
23
|
/** One JPEG frame as the panel receives it. */
|
|
23
24
|
export interface ScreencastFrame {
|
|
24
25
|
/** Tab the frame belongs to. */
|
|
@@ -188,11 +189,18 @@ export declare class TabManager {
|
|
|
188
189
|
*/
|
|
189
190
|
stopScreencast(tabId: string): Promise<void>;
|
|
190
191
|
/**
|
|
191
|
-
* Capture one
|
|
192
|
+
* Capture one screenshot on demand.
|
|
193
|
+
*
|
|
194
|
+
* Without explicit parameters this is exactly the frame the screencast path
|
|
195
|
+
* has always asked for: a JPEG at the configured quality, bounded to the
|
|
196
|
+
* viewport. A caller that wants a clip or a scale plans it with
|
|
197
|
+
* `planScreenshot` and passes those parameters, so this method stays the
|
|
198
|
+
* single send point without deciding any of it.
|
|
192
199
|
* @param tabId - target tab.
|
|
193
|
-
* @
|
|
200
|
+
* @param params - CDP parameters from `planScreenshot`; omitted keeps the plain JPEG capture.
|
|
201
|
+
* @returns base64 payload without the `data:` prefix.
|
|
194
202
|
*/
|
|
195
|
-
captureScreenshot(tabId: string): Promise<string>;
|
|
203
|
+
captureScreenshot(tabId: string, params?: CdpScreenshotParams): Promise<string>;
|
|
196
204
|
/**
|
|
197
205
|
* Capture a frame and hand it to the panel. An action calls this because a
|
|
198
206
|
* screencast only emits when the compositor sees a change: a click that
|
|
@@ -202,6 +210,22 @@ export declare class TabManager {
|
|
|
202
210
|
* @returns the emitted frame.
|
|
203
211
|
*/
|
|
204
212
|
refreshFrame(tabId: string): Promise<ScreencastFrame>;
|
|
213
|
+
/**
|
|
214
|
+
* Report the CSS viewport a capture of this tab renders.
|
|
215
|
+
*
|
|
216
|
+
* A pinned tab answers from configuration, so this costs no protocol
|
|
217
|
+
* round-trip. A tab this host adopted from a real browser is never pinned —
|
|
218
|
+
* the page keeps whatever size its window has — so the real CSS layout
|
|
219
|
+
* viewport is read from the page instead. Screenshot planning needs exactly
|
|
220
|
+
* this number: clip bounds and the output budget are both expressed in the
|
|
221
|
+
* viewport the pixels are measured against.
|
|
222
|
+
* @param tabId - target tab.
|
|
223
|
+
* @returns the CSS viewport size in pixels.
|
|
224
|
+
*/
|
|
225
|
+
viewportOf(tabId: string): Promise<{
|
|
226
|
+
width: number;
|
|
227
|
+
height: number;
|
|
228
|
+
}>;
|
|
205
229
|
/** Read the real CSS viewport without changing it in external-browser mode. */
|
|
206
230
|
private frameViewport;
|
|
207
231
|
/**
|
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
import type { CdpTransport } from '../core/cdp.ts';
|
|
23
23
|
import { ChromeProcess } from '../core/chrome.ts';
|
|
24
24
|
import { TabManager, type ScreencastFrame } from '../core/tabs.ts';
|
|
25
|
+
import { type ScreenshotPlan, type ScreenshotRequest } from '../core/screenshot.ts';
|
|
25
26
|
import { type ActionOutcome, type ElementTarget } from '../core/actions.ts';
|
|
26
27
|
import { type BrowserConsoleEntry, type BrowserNetworkEntry, type BrowserPanelState, type BrowserSnapshot, type BrowserTabView, type BrowserViewCommand, type BrowserViewEvent } from '../core/protocol.ts';
|
|
27
28
|
/** One attached panel socket as the service sees it. */
|
|
@@ -315,16 +316,30 @@ export declare class BrowserViewService implements BrowserViewBackend {
|
|
|
315
316
|
*/
|
|
316
317
|
snapshot(sessionId: string | undefined, tabId?: string): Promise<BrowserSnapshot>;
|
|
317
318
|
/**
|
|
318
|
-
* Capture one
|
|
319
|
+
* Capture one image of the tab.
|
|
320
|
+
*
|
|
321
|
+
* The plan decides everything the caller is told about the bytes: the format
|
|
322
|
+
* becomes the media type the attachment service stores, and the scale turns
|
|
323
|
+
* one capture into a cropped render whose encoded size has to be reported
|
|
324
|
+
* rather than guessed from the viewport. Planning before the capture is what
|
|
325
|
+
* makes an impossible request fail here, with the numbers, instead of
|
|
326
|
+
* producing a truncated or silently downscaled image. The viewport it plans
|
|
327
|
+
* against is the tab's own: a pinned tab answers from configuration, and an
|
|
328
|
+
* adopted real-browser tab reports the page's real layout viewport.
|
|
319
329
|
* @param sessionId - calling agent session.
|
|
320
330
|
* @param tabId - explicit tab.
|
|
321
|
-
* @
|
|
331
|
+
* @param request - format, clip, and scale; every field is optional.
|
|
332
|
+
* @returns the tab, the base64 payload, and the media type and encoded size the plan produced.
|
|
333
|
+
* @throws when the request cannot be honoured by this viewport or the size limits.
|
|
322
334
|
*/
|
|
323
|
-
screenshot(sessionId: string | undefined, tabId?: string): Promise<{
|
|
335
|
+
screenshot(sessionId: string | undefined, tabId?: string, request?: ScreenshotRequest): Promise<{
|
|
324
336
|
tabId: string;
|
|
325
337
|
url: string;
|
|
326
338
|
title: string;
|
|
327
339
|
data: string;
|
|
340
|
+
mediaType: ScreenshotPlan['mediaType'];
|
|
341
|
+
width: number;
|
|
342
|
+
height: number;
|
|
328
343
|
}>;
|
|
329
344
|
/**
|
|
330
345
|
* Console entries of one tab, newest last.
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
* JSON, rendered as one pretty-printed text block: the model reads fields, not
|
|
6
6
|
* prose, and the GUI can present the same value later. `browser_screenshot`
|
|
7
7
|
* returns a real image part — an `ImageBlock` carrying a durable attachment
|
|
8
|
-
* reference — because a
|
|
9
|
-
* model that can see one.
|
|
8
|
+
* reference — because a screenshot smuggled in as base64 text is not an image to
|
|
9
|
+
* any model that can see one.
|
|
10
10
|
*
|
|
11
11
|
* The screenshot path therefore needs the host's attachment service: the bytes
|
|
12
12
|
* are committed through it, and the block only carries the reference. The
|
|
@@ -42,7 +42,7 @@ export interface BrowserToolDeps {
|
|
|
42
42
|
/** The browser service every tool drives. */
|
|
43
43
|
service: BrowserViewService;
|
|
44
44
|
/**
|
|
45
|
-
* Commit one JPEG to the host's durable attachment store.
|
|
45
|
+
* Commit one screenshot (PNG or JPEG) to the host's durable attachment store.
|
|
46
46
|
*
|
|
47
47
|
* Absent when the composing host mounts no attachment service; the screenshot
|
|
48
48
|
* tool then refuses with an explanation instead of returning text pretending
|
|
@@ -50,7 +50,7 @@ export interface BrowserToolDeps {
|
|
|
50
50
|
*/
|
|
51
51
|
saveImage?: (input: {
|
|
52
52
|
data: Uint8Array;
|
|
53
|
-
mediaType: 'image/jpeg';
|
|
53
|
+
mediaType: 'image/png' | 'image/jpeg';
|
|
54
54
|
name?: string;
|
|
55
55
|
}) => Promise<DurableImageRef>;
|
|
56
56
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@linxin666/dsh-client-ui-browser-view",
|
|
3
3
|
"description": "Right-docked live browser view for the DSH Web GUI: a CDP-driven Chromium the agent operates, a screencast panel the user watches, and screenshots attached to every tool result.",
|
|
4
|
-
"version": "0.4.
|
|
4
|
+
"version": "0.4.14",
|
|
5
5
|
"icon": "icon.svg",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "lib/index.js",
|