dsh-builtin-browser 0.4.1 → 0.4.5

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.
Files changed (57) hide show
  1. package/CHANGELOG.md +1699 -0
  2. package/README.en.md +114 -33
  3. package/README.md +108 -32
  4. package/client.js +478 -29
  5. package/cordis.patch.yml +21 -0
  6. package/desktop-bridge/install.mjs +66 -6
  7. package/desktop-bridge/plugin-browser-bridge.js +426 -167
  8. package/docs/architecture.md +14 -4
  9. package/docs/tool-reference.md +13 -13
  10. package/docs/user-guide.md +58 -3
  11. package/docs/why-browser.md +1 -1
  12. package/lib/browser/types.d.ts +34 -3
  13. package/lib/browser-electron/bridge-connection.d.ts +49 -26
  14. package/lib/browser-electron/bridge-connection.js +185 -22
  15. package/lib/browser-electron/desktop-bridge-host.d.ts +69 -36
  16. package/lib/browser-electron/desktop-bridge-host.js +151 -86
  17. package/lib/browser-electron/entry.d.ts +1 -1
  18. package/lib/browser-electron/entry.js +133 -40
  19. package/lib/browser-electron/ephemeral-profile.d.ts +37 -0
  20. package/lib/browser-electron/ephemeral-profile.js +146 -0
  21. package/lib/browser-electron/history-store.d.ts +36 -2
  22. package/lib/browser-electron/history-store.js +76 -7
  23. package/lib/browser-electron/host-main.js +86 -28
  24. package/lib/browser-electron/provider.d.ts +106 -3
  25. package/lib/browser-electron/provider.js +407 -60
  26. package/lib/browser-electron/remote-host.d.ts +52 -3
  27. package/lib/browser-electron/remote-host.js +259 -25
  28. package/lib/browser-electron/rpc-ops.d.ts +18 -0
  29. package/lib/browser-electron/rpc-ops.js +36 -0
  30. package/lib/browser-electron/settings-route.d.ts +28 -0
  31. package/lib/browser-electron/settings-route.js +68 -0
  32. package/lib/browser-electron/settings-store.d.ts +40 -0
  33. package/lib/browser-electron/settings-store.js +160 -23
  34. package/lib/browser-electron/system-browser.d.ts +102 -12
  35. package/lib/browser-electron/system-browser.js +411 -51
  36. package/lib/index.js +0 -1
  37. package/lib/tool-browser/element-fields.d.ts +145 -0
  38. package/lib/tool-browser/element-fields.js +152 -0
  39. package/lib/tool-browser/index.js +149 -74
  40. package/package.json +10 -4
  41. package/scripts/verify-host-compat.mjs +195 -0
  42. package/src/browser/types.ts +34 -3
  43. package/src/browser-electron/bridge-connection.ts +179 -22
  44. package/src/browser-electron/desktop-bridge-host.ts +146 -84
  45. package/src/browser-electron/entry.ts +132 -39
  46. package/src/browser-electron/ephemeral-profile.ts +152 -0
  47. package/src/browser-electron/history-store.ts +81 -9
  48. package/src/browser-electron/host-main.ts +115 -36
  49. package/src/browser-electron/provider.ts +492 -69
  50. package/src/browser-electron/remote-host.ts +276 -38
  51. package/src/browser-electron/rpc-ops.ts +41 -0
  52. package/src/browser-electron/settings-route.ts +87 -0
  53. package/src/browser-electron/settings-store.ts +173 -24
  54. package/src/browser-electron/system-browser.ts +408 -39
  55. package/src/index.ts +3 -2
  56. package/src/tool-browser/element-fields.ts +167 -0
  57. package/src/tool-browser/index.ts +155 -76
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- /**
1
+ /**
2
2
  * dsh-builtin-browser plugin entry: aggregates the shared-browser capability
3
3
  * pieces. The cordis.patch.yml rows reference subpath exports:
4
4
  * - `dsh-builtin-browser/browser` -> the ctx.browser seam (Service)
@@ -14,6 +14,7 @@
14
14
  */
15
15
 
16
16
  /** Plugin identity for the root row (the composition surface is the subpath rows). */
17
+
17
18
  export const name = 'dsh-builtin-browser'
18
19
 
19
20
  /**
@@ -24,9 +25,9 @@ export const name = 'dsh-builtin-browser'
24
25
  * never reaches the browser (the plugin works, its client half is invisible).
25
26
  */
26
27
  export function apply(): void {
27
- // Intentionally empty: see the module comment above.
28
28
  }
29
29
 
30
+
30
31
  export { BrowserError } from './browser/types.js'
31
32
  export type {
32
33
  BrowserA11yNode,
@@ -0,0 +1,167 @@
1
+ /**
2
+ * The element and node field sets, declared once.
3
+ *
4
+ * WHY THIS FILE EXISTS
5
+ *
6
+ * The same field set used to be written out in four places: the page script produced an
7
+ * object, `types.ts` declared its shape, a tool's `execute` rebuilt it as an explicit list,
8
+ * and the tool's `output.schema` declared the properties. Nothing connected them, and the
9
+ * result is this project's most repeatable bug:
10
+ *
11
+ * - `browser_a11y`'s `selector` — computed by the provider, declared by the schema,
12
+ * printed by the renderer, and dropped by the projection. 134 tests stayed green.
13
+ * - `browser_screenshot`'s `width`/`height` — measured by the provider, declared by the
14
+ * schema, and dropped by the projection. 172 tests stayed green. It was found a round
15
+ * later, by reading the three places that agree and noticing the fourth did not.
16
+ *
17
+ * A hand-written allow-list is invisible to the type checker (it is an object literal) and to
18
+ * the tests (nothing ran a tool). The fix is not a careful list; it is one list. Both the
19
+ * schema fragment and the projection are derived from the tables below, so a field cannot be
20
+ * present in one and absent from the other.
21
+ *
22
+ * The schema is the dangerous half: `additionalProperties: false` means a field the schema
23
+ * does NOT declare causes the ENTIRE result to be rejected at run time, while a field the
24
+ * projection forgets is silently dropped. One table cannot get either wrong on its own.
25
+ */
26
+
27
+ /** A single declared field: how the schema describes it, and whether callers always get it. */
28
+ interface FieldSpec {
29
+ /** The JSON Schema fragment for this property. */
30
+ readonly schema: Record<string, unknown>
31
+ /** True when the value is always present, so both the schema and the result promise it. */
32
+ readonly required?: boolean
33
+ }
34
+
35
+ /**
36
+ * The fields of one entry in `browser_snapshot` / `browser_open`'s `elements`.
37
+ *
38
+ * `selector` is the reason this file exists; see the header. It was added to the schema and
39
+ * the projection separately, and only one of them landed.
40
+ */
41
+ const ELEMENT_FIELDS = {
42
+ ref: { schema: { type: 'number' }, required: true },
43
+ kind: { schema: { type: 'string' }, required: true },
44
+ label: { schema: { type: 'string' }, required: true },
45
+ selector: { schema: { type: 'string' } },
46
+ x: { schema: { type: 'number' }, required: true },
47
+ y: { schema: { type: 'number' }, required: true },
48
+ frame: { schema: { type: 'boolean' } },
49
+ } as const satisfies Record<string, FieldSpec>
50
+
51
+ /**
52
+ * The fields of one entry in `browser_a11y`'s `nodes`.
53
+ *
54
+ * Different shape from an element on purpose: a role and an accessible name carry meaning a
55
+ * tag and a label string do not, and `depth` is what renders the tree.
56
+ */
57
+ const NODE_FIELDS = {
58
+ ref: { schema: { type: 'number' }, required: true },
59
+ role: { schema: { type: 'string' }, required: true },
60
+ name: { schema: { type: 'string' }, required: true },
61
+ value: { schema: { type: 'string' } },
62
+ states: { schema: { type: 'array', items: { type: 'string' } }, required: true },
63
+ depth: { schema: { type: 'number' }, required: true },
64
+ tag: { schema: { type: 'string' }, required: true },
65
+ // Always present: the provider derives it for every node and yields an empty string when the
66
+ // element has neither an id nor a name. Declaring it optional described a shape the provider
67
+ // never produced, and the type said required — the same disagreement, one level down.
68
+ selector: { schema: { type: 'string' }, required: true },
69
+ x: { schema: { type: 'number' }, required: true },
70
+ y: { schema: { type: 'number' }, required: true },
71
+ frame: { schema: { type: 'boolean' } },
72
+ } as const satisfies Record<string, FieldSpec>
73
+
74
+ /**
75
+ * Copy the declared fields out of a source value.
76
+ *
77
+ * Reads the table rather than naming fields, so anything the provider produces that the table
78
+ * declares is carried through, and anything the table does not declare is left out — which is
79
+ * what keeps the result and the schema in agreement in both directions.
80
+ * @param fields - the field table to project through.
81
+ * @param source - the value the provider produced.
82
+ * @returns a new object holding exactly the declared fields that are present.
83
+ */
84
+ function project(fields: Record<string, FieldSpec>, source: Record<string, unknown>): Record<string, unknown> {
85
+ const out: Record<string, unknown> = {}
86
+ for (const name of Object.keys(fields)) {
87
+ const value = source[name]
88
+ if (value !== undefined && value !== null) out[name] = value
89
+ }
90
+ return out
91
+ }
92
+
93
+ /**
94
+ * The `elements` item schema for browser_snapshot and browser_open.
95
+ *
96
+ * Written as a literal rather than generated, because `defineTool` validates the output schema
97
+ * against its own `ValueSchemaSpec` and a generated `Record<string, …>` is too wide to satisfy
98
+ * it. The field table above remains the single source of truth: `tests/element-fields.test.mjs`
99
+ * asserts that these properties and `projectElement`'s output are exactly the table's keys, so
100
+ * the two cannot drift even though one is typed by hand.
101
+ */
102
+ export const ELEMENT_ITEM_SCHEMA = {
103
+ type: 'object',
104
+ additionalProperties: false,
105
+ properties: {
106
+ ref: { type: 'number', required: true },
107
+ kind: { type: 'string', required: true },
108
+ label: { type: 'string', required: true },
109
+ selector: { type: 'string' },
110
+ x: { type: 'number', required: true },
111
+ y: { type: 'number', required: true },
112
+ frame: { type: 'boolean' },
113
+ },
114
+ } as const
115
+
116
+ /**
117
+ * The `nodes` item schema for browser_a11y.
118
+ *
119
+ * Same reasoning as {@link ELEMENT_ITEM_SCHEMA}: literal for the type checker, table-backed by
120
+ * test.
121
+ */
122
+ export const NODE_ITEM_SCHEMA = {
123
+ type: 'object',
124
+ additionalProperties: false,
125
+ properties: {
126
+ ref: { type: 'number', required: true },
127
+ role: { type: 'string', required: true },
128
+ name: { type: 'string', required: true },
129
+ value: { type: 'string' },
130
+ states: { type: 'array', required: true, items: { type: 'string' } },
131
+ depth: { type: 'number', required: true },
132
+ tag: { type: 'string', required: true },
133
+ selector: { type: 'string', required: true },
134
+ x: { type: 'number', required: true },
135
+ y: { type: 'number', required: true },
136
+ frame: { type: 'boolean' },
137
+ },
138
+ } as const
139
+
140
+ /**
141
+ * Project one snapshot element into the shape the schema declares.
142
+ *
143
+ * Generic in the source type so the caller keeps the precise shape `defineTool` inferred from
144
+ * the schema: the projection decides which fields survive at run time, and the field table is
145
+ * what guarantees that set matches the schema. Widening the return type here would only move
146
+ * the mismatch to the call site.
147
+ * @param element - the element as the provider produced it.
148
+ * @returns exactly the declared fields.
149
+ */
150
+ export function projectElement<T extends object>(element: T): T {
151
+ return project(ELEMENT_FIELDS as Record<string, FieldSpec>, element as Record<string, unknown>) as T
152
+ }
153
+
154
+ /**
155
+ * Project one accessibility node into the shape the schema declares.
156
+ * @param node - the node as the provider produced it.
157
+ * @returns exactly the declared fields.
158
+ */
159
+ export function projectNode<T extends object>(node: T): T {
160
+ return project(NODE_FIELDS as Record<string, FieldSpec>, node as Record<string, unknown>) as T
161
+ }
162
+
163
+ /** The field names each table declares, for tests that check the two stay in step. */
164
+ export const DECLARED_FIELDS = {
165
+ element: Object.keys(ELEMENT_FIELDS),
166
+ node: Object.keys(NODE_FIELDS),
167
+ } as const