@silbercue/chrome 0.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.
Files changed (129) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +229 -0
  3. package/build/cache/a11y-tree.d.ts +252 -0
  4. package/build/cache/a11y-tree.js +1956 -0
  5. package/build/cache/index.d.ts +8 -0
  6. package/build/cache/index.js +4 -0
  7. package/build/cache/selector-cache.d.ts +47 -0
  8. package/build/cache/selector-cache.js +119 -0
  9. package/build/cache/session-defaults.d.ts +27 -0
  10. package/build/cache/session-defaults.js +130 -0
  11. package/build/cache/tab-state-cache.d.ts +39 -0
  12. package/build/cache/tab-state-cache.js +171 -0
  13. package/build/cdp/cdp-client.d.ts +25 -0
  14. package/build/cdp/cdp-client.js +146 -0
  15. package/build/cdp/chrome-launcher.d.ts +85 -0
  16. package/build/cdp/chrome-launcher.js +502 -0
  17. package/build/cdp/console-collector.d.ts +53 -0
  18. package/build/cdp/console-collector.js +147 -0
  19. package/build/cdp/debug.d.ts +1 -0
  20. package/build/cdp/debug.js +6 -0
  21. package/build/cdp/dialog-handler.d.ts +54 -0
  22. package/build/cdp/dialog-handler.js +129 -0
  23. package/build/cdp/dom-watcher.d.ts +45 -0
  24. package/build/cdp/dom-watcher.js +195 -0
  25. package/build/cdp/emulation.d.ts +12 -0
  26. package/build/cdp/emulation.js +17 -0
  27. package/build/cdp/index.d.ts +11 -0
  28. package/build/cdp/index.js +6 -0
  29. package/build/cdp/network-collector.d.ts +77 -0
  30. package/build/cdp/network-collector.js +257 -0
  31. package/build/cdp/protocol.d.ts +20 -0
  32. package/build/cdp/protocol.js +1 -0
  33. package/build/cdp/session-manager.d.ts +62 -0
  34. package/build/cdp/session-manager.js +205 -0
  35. package/build/cdp/settle.d.ts +16 -0
  36. package/build/cdp/settle.js +71 -0
  37. package/build/cli/license-commands.d.ts +19 -0
  38. package/build/cli/license-commands.js +199 -0
  39. package/build/cli/top-level-commands.d.ts +49 -0
  40. package/build/cli/top-level-commands.js +222 -0
  41. package/build/hooks/index.d.ts +2 -0
  42. package/build/hooks/index.js +1 -0
  43. package/build/hooks/pro-hooks.d.ts +126 -0
  44. package/build/hooks/pro-hooks.js +17 -0
  45. package/build/index.d.ts +4 -0
  46. package/build/index.js +86 -0
  47. package/build/license/free-tier-config.d.ts +14 -0
  48. package/build/license/free-tier-config.js +18 -0
  49. package/build/license/index.d.ts +4 -0
  50. package/build/license/index.js +2 -0
  51. package/build/license/license-status.d.ts +15 -0
  52. package/build/license/license-status.js +9 -0
  53. package/build/overlay/session-overlay.d.ts +22 -0
  54. package/build/overlay/session-overlay.js +372 -0
  55. package/build/plan/index.d.ts +7 -0
  56. package/build/plan/index.js +4 -0
  57. package/build/plan/plan-conditions.d.ts +12 -0
  58. package/build/plan/plan-conditions.js +242 -0
  59. package/build/plan/plan-executor.d.ts +49 -0
  60. package/build/plan/plan-executor.js +259 -0
  61. package/build/plan/plan-state-store.d.ts +24 -0
  62. package/build/plan/plan-state-store.js +43 -0
  63. package/build/plan/plan-variables.d.ts +16 -0
  64. package/build/plan/plan-variables.js +71 -0
  65. package/build/registry.d.ts +124 -0
  66. package/build/registry.js +884 -0
  67. package/build/server.d.ts +1 -0
  68. package/build/server.js +245 -0
  69. package/build/tools/click.d.ts +34 -0
  70. package/build/tools/click.js +293 -0
  71. package/build/tools/configure-session.d.ts +15 -0
  72. package/build/tools/configure-session.js +45 -0
  73. package/build/tools/console-logs.d.ts +18 -0
  74. package/build/tools/console-logs.js +44 -0
  75. package/build/tools/dom-snapshot.d.ts +13 -0
  76. package/build/tools/dom-snapshot.js +259 -0
  77. package/build/tools/element-utils.d.ts +23 -0
  78. package/build/tools/element-utils.js +133 -0
  79. package/build/tools/error-utils.d.ts +8 -0
  80. package/build/tools/error-utils.js +27 -0
  81. package/build/tools/evaluate.d.ts +34 -0
  82. package/build/tools/evaluate.js +217 -0
  83. package/build/tools/file-upload.d.ts +20 -0
  84. package/build/tools/file-upload.js +174 -0
  85. package/build/tools/fill-form.d.ts +39 -0
  86. package/build/tools/fill-form.js +256 -0
  87. package/build/tools/handle-dialog.d.ts +15 -0
  88. package/build/tools/handle-dialog.js +48 -0
  89. package/build/tools/index.d.ts +35 -0
  90. package/build/tools/index.js +18 -0
  91. package/build/tools/navigate.d.ts +18 -0
  92. package/build/tools/navigate.js +111 -0
  93. package/build/tools/network-monitor.d.ts +18 -0
  94. package/build/tools/network-monitor.js +66 -0
  95. package/build/tools/observe.d.ts +44 -0
  96. package/build/tools/observe.js +339 -0
  97. package/build/tools/press-key.d.ts +33 -0
  98. package/build/tools/press-key.js +155 -0
  99. package/build/tools/read-page.d.ts +22 -0
  100. package/build/tools/read-page.js +100 -0
  101. package/build/tools/run-plan.d.ts +205 -0
  102. package/build/tools/run-plan.js +215 -0
  103. package/build/tools/screenshot.d.ts +16 -0
  104. package/build/tools/screenshot.js +283 -0
  105. package/build/tools/scroll.d.ts +28 -0
  106. package/build/tools/scroll.js +143 -0
  107. package/build/tools/switch-tab.d.ts +26 -0
  108. package/build/tools/switch-tab.js +355 -0
  109. package/build/tools/tab-status.d.ts +7 -0
  110. package/build/tools/tab-status.js +50 -0
  111. package/build/tools/type.d.ts +31 -0
  112. package/build/tools/type.js +247 -0
  113. package/build/tools/virtual-desk.d.ts +7 -0
  114. package/build/tools/virtual-desk.js +108 -0
  115. package/build/tools/visual-constants.d.ts +3 -0
  116. package/build/tools/visual-constants.js +10 -0
  117. package/build/tools/wait-for.d.ts +26 -0
  118. package/build/tools/wait-for.js +323 -0
  119. package/build/transport/index.d.ts +3 -0
  120. package/build/transport/index.js +2 -0
  121. package/build/transport/pipe-transport.d.ts +18 -0
  122. package/build/transport/pipe-transport.js +63 -0
  123. package/build/transport/transport.d.ts +8 -0
  124. package/build/transport/transport.js +1 -0
  125. package/build/transport/websocket-transport.d.ts +22 -0
  126. package/build/transport/websocket-transport.js +200 -0
  127. package/build/types.d.ts +21 -0
  128. package/build/types.js +1 -0
  129. package/package.json +62 -0
@@ -0,0 +1,71 @@
1
+ const VAR_PATTERN = /\$(\w+)/g;
2
+ const EXACT_VAR_PATTERN = /^\$(\w+)$/;
3
+ /**
4
+ * Substitute $var references in a params object.
5
+ * Replaces string values matching "$varName" pattern with the value from vars.
6
+ * Supports nested objects and arrays.
7
+ * Unresolved $var references remain as-is (no error — the tool will handle invalid params).
8
+ */
9
+ export function substituteVars(params, vars) {
10
+ return substituteObject(params, vars);
11
+ }
12
+ function substituteValue(value, vars) {
13
+ if (typeof value === "string") {
14
+ // Whole-string replacement: preserves type (number, boolean, object, etc.)
15
+ const exactMatch = value.match(EXACT_VAR_PATTERN);
16
+ if (exactMatch) {
17
+ const varName = exactMatch[1];
18
+ if (varName in vars) {
19
+ return vars[varName];
20
+ }
21
+ return value; // unresolved — keep as-is
22
+ }
23
+ // Inline replacement: string interpolation
24
+ if (VAR_PATTERN.test(value)) {
25
+ // Reset lastIndex since we used .test()
26
+ VAR_PATTERN.lastIndex = 0;
27
+ return value.replace(VAR_PATTERN, (match, varName) => {
28
+ if (varName in vars) {
29
+ return String(vars[varName]);
30
+ }
31
+ return match; // unresolved — keep as-is
32
+ });
33
+ }
34
+ return value;
35
+ }
36
+ if (Array.isArray(value)) {
37
+ return value.map((item) => substituteValue(item, vars));
38
+ }
39
+ if (value !== null && typeof value === "object") {
40
+ return substituteObject(value, vars);
41
+ }
42
+ return value;
43
+ }
44
+ function substituteObject(obj, vars) {
45
+ const result = {};
46
+ for (const key of Object.keys(obj)) {
47
+ result[key] = substituteValue(obj[key], vars);
48
+ }
49
+ return result;
50
+ }
51
+ /**
52
+ * Extract the text content from a ToolResponse for saveAs.
53
+ * Concatenates all text content blocks into a single string.
54
+ * If the text is valid JSON, parse it and return the parsed value.
55
+ * Otherwise return the raw text string.
56
+ */
57
+ export function extractResultValue(result) {
58
+ const textParts = result.content
59
+ .filter((c) => c.type === "text")
60
+ .map((c) => c.text);
61
+ if (textParts.length === 0) {
62
+ return "";
63
+ }
64
+ const text = textParts.join("\n");
65
+ try {
66
+ return JSON.parse(text);
67
+ }
68
+ catch {
69
+ return text;
70
+ }
71
+ }
@@ -0,0 +1,124 @@
1
+ import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
2
+ import type { CdpClient } from "./cdp/cdp-client.js";
3
+ import type { SessionManager } from "./cdp/session-manager.js";
4
+ import type { DialogHandler } from "./cdp/dialog-handler.js";
5
+ import type { TabStateCache } from "./cache/tab-state-cache.js";
6
+ import type { ToolResponse, ConnectionStatus } from "./types.js";
7
+ import type { ConsoleCollector } from "./cdp/console-collector.js";
8
+ import type { NetworkCollector } from "./cdp/network-collector.js";
9
+ import type { SessionDefaults } from "./cache/session-defaults.js";
10
+ import { PlanStateStore } from "./plan/plan-state-store.js";
11
+ import type { LicenseStatus } from "./license/license-status.js";
12
+ import type { FreeTierConfig } from "./license/free-tier-config.js";
13
+ import { z } from "zod";
14
+ import { getProHooks } from "./hooks/pro-hooks.js";
15
+ import type { ToolRegistryPublic } from "./hooks/pro-hooks.js";
16
+ /**
17
+ * Story 16.4: Konvertiert ein JSON-Schema-Literal in eine Zod Raw Shape,
18
+ * damit der MCP-SDK `server.tool()` Aufruf den Schema-Check besteht.
19
+ *
20
+ * Unterstuetzte Typen:
21
+ * - `string` (inkl. `enum` → `z.enum`)
22
+ * - `boolean`
23
+ * - `number`
24
+ * - `integer` → `z.number().int()`
25
+ * - `array` (mit `items.type` string|number|boolean|object — verschachtelt)
26
+ * - `object` (rekursiv ueber `properties`/`required`)
27
+ * - Type-Arrays wie `["string", "null"]` → `z.union([...])`
28
+ * - Unbekannte Typen → `z.unknown()` als Fallback
29
+ *
30
+ * Default-Handling: Wenn ein Feld einen `default` hat, wird ausschliesslich
31
+ * `.default(value)` angewendet. Zod behandelt Felder mit Default in einem
32
+ * `z.object()` automatisch als optional (wenn der Input `undefined` ist,
33
+ * wird der Default eingesetzt). Erst wenn KEIN Default existiert UND das
34
+ * Feld nicht in `required` steht, wird `.optional()` angehaengt.
35
+ */
36
+ export declare function jsonSchemaToZodShape(schema: Record<string, unknown>): Record<string, z.ZodTypeAny>;
37
+ export declare class ToolRegistry implements ToolRegistryPublic {
38
+ private server;
39
+ cdpClient: CdpClient;
40
+ private _tabStateCache;
41
+ private _sessionId;
42
+ private _handlers;
43
+ readonly planStateStore: PlanStateStore;
44
+ /**
45
+ * Story 15.2: Delegate for `registerTool()` — set during `registerAll()`
46
+ * so it has access to the wrap() closure (dialog injection, response_bytes,
47
+ * session defaults). Pro-Repo calls `registerTool()` from within
48
+ * `registerProTools`, which runs inside `registerAll()` after this delegate
49
+ * is installed.
50
+ */
51
+ private _registerProToolDelegate;
52
+ /**
53
+ * Story 15.2: Public method exposed via `ToolRegistryPublic`. The Pro-Repo
54
+ * uses this from within the `registerProTools` hook to register extra
55
+ * MCP tools (e.g. inspect_element).
56
+ *
57
+ * Lifecycle: The delegate is installed at the start of `registerAll()`
58
+ * (before `registerProTools` is invoked) and cleared at the end of
59
+ * `registerAll()`. Calling `registerTool()` outside this window throws.
60
+ */
61
+ registerTool(name: string, description: string, schema: Record<string, unknown>, handler: (params: Record<string, unknown>, sessionIdOverride?: string) => Promise<ToolResponse>): void;
62
+ private _getConnectionStatus;
63
+ private _sessionManager;
64
+ private _dialogHandler;
65
+ private _licenseStatus;
66
+ private _freeTierConfig;
67
+ private _consoleCollector;
68
+ private _networkCollector;
69
+ private _sessionDefaults;
70
+ private _waitForAXChange;
71
+ private _contextChecked;
72
+ constructor(server: McpServer, cdpClient: CdpClient, sessionId: string, _tabStateCache: TabStateCache, getConnectionStatus?: () => ConnectionStatus, sessionManager?: SessionManager, dialogHandler?: DialogHandler, licenseStatus?: LicenseStatus, freeTierConfig?: FreeTierConfig, consoleCollector?: ConsoleCollector, networkCollector?: NetworkCollector, sessionDefaults?: SessionDefaults, waitForAXChange?: (timeoutMs: number) => Promise<boolean>);
73
+ get sessionId(): string;
74
+ /** Story 16.4: Public getter fuer OOPIF SessionManager (ToolRegistryPublic). */
75
+ get sessionManager(): SessionManager | undefined;
76
+ updateSession(sessionId: string): void;
77
+ /** Swap the CDP client and session after a successful reconnect */
78
+ updateClient(cdpClient: CdpClient, sessionId: string): void;
79
+ /** Get current connection status (connected, reconnecting, disconnected) */
80
+ get connectionStatus(): ConnectionStatus;
81
+ executeTool(name: string, params: Record<string, unknown>, sessionIdOverride?: string): Promise<ToolResponse>;
82
+ /**
83
+ * Story 15.3: Invokes the `onToolResult` Pro-Hook (if registered) to enrich
84
+ * the tool response with ambient context (DOM diffs, compact snapshots).
85
+ *
86
+ * The Free-Repo itself no longer contains any ambient-context orchestration —
87
+ * the 3-stage click analysis (classifyRef → waitForAXChange → diffSnapshots →
88
+ * formatDomDiff) now lives in the Pro-Repo hook implementation.
89
+ *
90
+ * Responsibilities that stay in the Free-Repo:
91
+ * - FR-007: `a11yTree.reset()` on navigate — called BEFORE the hook so
92
+ * stale refs never leak to the next tool call, even if the hook is
93
+ * not registered.
94
+ * - Error-guard: the hook is NOT invoked when `result.isError` is true,
95
+ * preserving the pre-15.3 semantics.
96
+ *
97
+ * After the hook returns, the enhanced response is merged via
98
+ * `Object.assign(result, enhanced)` so the original `_meta` reference
99
+ * remains stable — later code-paths (response_bytes, estimated_tokens,
100
+ * suggestion injection) mutate `result._meta` in place. If the hook
101
+ * returns a new `_meta` object, the original reference is restored to
102
+ * prevent downstream mutations from writing to a detached object.
103
+ */
104
+ private _runOnToolResultHook;
105
+ /**
106
+ * Story 6.1: Inject pending dialog notifications into any tool response.
107
+ * Called from both executeTool() (run_plan path) and server.tool() callbacks (direct MCP path).
108
+ */
109
+ private _injectDialogNotifications;
110
+ /**
111
+ * Wrap a tool handler so dialog notifications are injected into its response.
112
+ * This ensures notifications reach the LLM regardless of whether the tool
113
+ * is called via the direct MCP path (server.tool) or via executeTool (run_plan).
114
+ */
115
+ private _wrapWithDialogInjection;
116
+ /**
117
+ * Story 9.5: Wrap a tool handler with a Pro feature gate check.
118
+ * If a featureGate hook is registered and returns { allowed: false },
119
+ * the tool is blocked with an isError response.
120
+ * When no hook is registered, the tool executes normally.
121
+ */
122
+ wrapWithGate<T>(toolName: string, fn: (params: T) => Promise<ToolResponse>, hooks: ReturnType<typeof getProHooks>): (params: T) => Promise<ToolResponse>;
123
+ registerAll(): void;
124
+ }