dsh-embedded-browser-next 0.0.0-stage → 1.0.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,73 @@
1
+ /**
2
+ * How much of a page inventory one snapshot may return.
3
+ *
4
+ * The number used to be a flat 80, which reads as "this page has almost nothing"
5
+ * on any site with a real navigation bar — and the first 80 entries of a long page
6
+ * are exactly the ones a person is *not* looking at. The policy here is:
7
+ *
8
+ * - **80 as a floor.** A small page never gets worse than it was.
9
+ * - **240 as a cap.** Beyond that the answer costs more context than it saves.
10
+ * - **12 000 characters as the real ceiling.** Entries are prose, not numbers;
11
+ * the budget is what keeps a dense page from turning one call into a context
12
+ * dump, and it is what the callers actually feel.
13
+ *
14
+ * A caller can still ask for an exact count (`maxElements`), which is how the
15
+ * numbers stay predictable for a script; and `offset` walks the list, so "there
16
+ * are 200 of these" is a paging instruction rather than a dead end.
17
+ *
18
+ * @module dsh-embedded-browser/snapshot-window
19
+ */
20
+
21
+ /** Fewest entries a snapshot returns when the caller names no count. */
22
+ export const SNAPSHOT_FLOOR = 80
23
+
24
+ /** Most entries a snapshot returns, whatever the page's size. */
25
+ export const SNAPSHOT_CAP = 240
26
+
27
+ /** Character budget for the whole inventory of one snapshot. */
28
+ export const SNAPSHOT_BUDGET_CHARS = 12_000
29
+
30
+ /**
31
+ * How many entries to take for a page with `total` interactive elements.
32
+ *
33
+ * @param total - how many the page has (visible, actionable).
34
+ * @param requested - explicit `maxElements`; `undefined`/`0` means "decide".
35
+ * @returns a positive integer.
36
+ */
37
+ export function snapshotLimit(total, requested, { floor = SNAPSHOT_FLOOR, cap = SNAPSHOT_CAP } = {}) {
38
+ const asked = Number(requested)
39
+ if (Number.isFinite(asked) && asked > 0) return Math.floor(asked)
40
+ const size = Math.max(0, Math.floor(Number(total) || 0))
41
+ return Math.min(cap, Math.max(floor, size))
42
+ }
43
+
44
+ /**
45
+ * Which slice of the order a snapshot returns, and whether anything follows.
46
+ *
47
+ * @param total - entries in the whole list.
48
+ * @param offset - first entry to return (0-based).
49
+ * @param limit - how many to take at most.
50
+ * @returns `{ start, end, count, total, more }`.
51
+ */
52
+ export function snapshotWindow(total, offset = 0, limit = SNAPSHOT_FLOOR) {
53
+ const size = Math.max(0, Math.floor(Number(total) || 0))
54
+ const start = Math.min(Math.max(0, Math.floor(Number(offset) || 0)), size)
55
+ const count = Math.max(0, Math.min(Math.floor(Number(limit) || 0), size - start))
56
+ return { start, end: start + count, count, total: size, more: start + count < size }
57
+ }
58
+
59
+ /**
60
+ * What one inventory line is expected to cost in the answer.
61
+ *
62
+ * Deliberately generous: a wrong guess must not be *too low*, or the budget stops
63
+ * protecting anything. The tag, type and punctuation add a fixed overhead.
64
+ *
65
+ * @param label - the element's visible text.
66
+ * @param loc - the stable handle, when there is one.
67
+ * @returns an estimate in characters.
68
+ */
69
+ export function estimateEntryChars(label, loc) {
70
+ const text = typeof label === 'string' ? label.length : 0
71
+ const handle = typeof loc === 'string' ? loc.length : 0
72
+ return text + handle + 40
73
+ }