btcp-browser-agent 0.1.0 → 0.1.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.
Files changed (136) hide show
  1. package/package.json +8 -9
  2. package/packages/core/dist/actions.d.ts +97 -0
  3. package/packages/core/dist/actions.js +940 -0
  4. package/packages/core/dist/errors.d.ts +138 -0
  5. package/packages/core/dist/errors.js +157 -0
  6. package/packages/core/dist/index.d.ts +120 -0
  7. package/packages/core/dist/index.js +134 -0
  8. package/packages/core/dist/ref-map.d.ts +16 -0
  9. package/packages/core/dist/ref-map.js +91 -0
  10. package/packages/core/dist/snapshot.d.ts +37 -0
  11. package/packages/core/dist/snapshot.js +751 -0
  12. package/packages/core/dist/types.d.ts +396 -0
  13. package/packages/core/dist/types.js +7 -0
  14. package/packages/extension/dist/background.d.ts +227 -0
  15. package/packages/extension/dist/background.js +737 -0
  16. package/packages/extension/dist/content.d.ts +18 -0
  17. package/packages/extension/dist/content.js +149 -0
  18. package/packages/extension/dist/index.d.ts +228 -0
  19. package/packages/extension/dist/index.js +350 -0
  20. package/packages/extension/dist/session-manager.d.ts +87 -0
  21. package/packages/extension/dist/session-manager.js +322 -0
  22. package/packages/extension/{src/session-types.ts → dist/session-types.d.ts} +113 -144
  23. package/packages/extension/dist/session-types.js +5 -0
  24. package/packages/extension/dist/types.d.ts +88 -0
  25. package/packages/extension/dist/types.js +7 -0
  26. package/CLAUDE.md +0 -230
  27. package/SKILL.md +0 -143
  28. package/SNAPSHOT_IMPROVEMENTS.md +0 -302
  29. package/USAGE.md +0 -146
  30. package/dist/index.d.ts.map +0 -1
  31. package/dist/index.js.map +0 -1
  32. package/docs/browser-cli-design.md +0 -500
  33. package/examples/chrome-extension/CHANGELOG.md +0 -210
  34. package/examples/chrome-extension/DEBUG.md +0 -231
  35. package/examples/chrome-extension/ERROR_FIXED.md +0 -147
  36. package/examples/chrome-extension/QUICK_TEST.md +0 -189
  37. package/examples/chrome-extension/README.md +0 -149
  38. package/examples/chrome-extension/SESSION_ONLY_MODE.md +0 -305
  39. package/examples/chrome-extension/TEST_WITH_YOUR_TABS.md +0 -97
  40. package/examples/chrome-extension/build.js +0 -43
  41. package/examples/chrome-extension/manifest.json +0 -37
  42. package/examples/chrome-extension/package-lock.json +0 -1063
  43. package/examples/chrome-extension/package.json +0 -21
  44. package/examples/chrome-extension/popup.html +0 -195
  45. package/examples/chrome-extension/src/background.ts +0 -12
  46. package/examples/chrome-extension/src/content.ts +0 -7
  47. package/examples/chrome-extension/src/popup.ts +0 -303
  48. package/examples/chrome-extension/src/scenario-google-github.ts +0 -389
  49. package/examples/chrome-extension/test-page.html +0 -127
  50. package/examples/chrome-extension/tests/README.md +0 -206
  51. package/examples/chrome-extension/tests/scenario-google-to-github-star.ts +0 -380
  52. package/examples/chrome-extension/tsconfig.json +0 -14
  53. package/examples/snapshots/README.md +0 -207
  54. package/examples/snapshots/amazon-com-detail.html +0 -9528
  55. package/examples/snapshots/amazon-com-detail.snapshot.txt +0 -997
  56. package/examples/snapshots/convert-snapshots.ts +0 -97
  57. package/examples/snapshots/edition-cnn-com.html +0 -13292
  58. package/examples/snapshots/edition-cnn-com.snapshot.txt +0 -562
  59. package/examples/snapshots/github-com-microsoft-vscode.html +0 -2916
  60. package/examples/snapshots/github-com-microsoft-vscode.snapshot.txt +0 -455
  61. package/examples/snapshots/google-search.html +0 -20012
  62. package/examples/snapshots/google-search.snapshot.txt +0 -195
  63. package/examples/snapshots/metadata.json +0 -86
  64. package/examples/snapshots/npr-org-templates.html +0 -2031
  65. package/examples/snapshots/npr-org-templates.snapshot.txt +0 -224
  66. package/examples/snapshots/stackoverflow-com.html +0 -5216
  67. package/examples/snapshots/stackoverflow-com.snapshot.txt +0 -2404
  68. package/examples/snapshots/test-all-mode.html +0 -46
  69. package/examples/snapshots/test-all-mode.snapshot.txt +0 -5
  70. package/examples/snapshots/validate.test.ts +0 -296
  71. package/packages/cli/package.json +0 -42
  72. package/packages/cli/src/__tests__/cli.test.ts +0 -434
  73. package/packages/cli/src/__tests__/errors.test.ts +0 -226
  74. package/packages/cli/src/__tests__/executor.test.ts +0 -275
  75. package/packages/cli/src/__tests__/formatter.test.ts +0 -260
  76. package/packages/cli/src/__tests__/parser.test.ts +0 -288
  77. package/packages/cli/src/__tests__/suggestions.test.ts +0 -255
  78. package/packages/cli/src/commands/back.ts +0 -22
  79. package/packages/cli/src/commands/check.ts +0 -33
  80. package/packages/cli/src/commands/clear.ts +0 -33
  81. package/packages/cli/src/commands/click.ts +0 -32
  82. package/packages/cli/src/commands/closetab.ts +0 -31
  83. package/packages/cli/src/commands/eval.ts +0 -41
  84. package/packages/cli/src/commands/fill.ts +0 -30
  85. package/packages/cli/src/commands/focus.ts +0 -33
  86. package/packages/cli/src/commands/forward.ts +0 -22
  87. package/packages/cli/src/commands/goto.ts +0 -34
  88. package/packages/cli/src/commands/help.ts +0 -162
  89. package/packages/cli/src/commands/hover.ts +0 -34
  90. package/packages/cli/src/commands/index.ts +0 -129
  91. package/packages/cli/src/commands/newtab.ts +0 -35
  92. package/packages/cli/src/commands/press.ts +0 -40
  93. package/packages/cli/src/commands/reload.ts +0 -25
  94. package/packages/cli/src/commands/screenshot.ts +0 -27
  95. package/packages/cli/src/commands/scroll.ts +0 -64
  96. package/packages/cli/src/commands/select.ts +0 -35
  97. package/packages/cli/src/commands/snapshot.ts +0 -21
  98. package/packages/cli/src/commands/tab.ts +0 -32
  99. package/packages/cli/src/commands/tabs.ts +0 -26
  100. package/packages/cli/src/commands/text.ts +0 -27
  101. package/packages/cli/src/commands/title.ts +0 -17
  102. package/packages/cli/src/commands/type.ts +0 -38
  103. package/packages/cli/src/commands/uncheck.ts +0 -33
  104. package/packages/cli/src/commands/url.ts +0 -17
  105. package/packages/cli/src/commands/wait.ts +0 -54
  106. package/packages/cli/src/errors.ts +0 -164
  107. package/packages/cli/src/executor.ts +0 -68
  108. package/packages/cli/src/formatter.ts +0 -215
  109. package/packages/cli/src/index.ts +0 -257
  110. package/packages/cli/src/parser.ts +0 -195
  111. package/packages/cli/src/suggestions.ts +0 -207
  112. package/packages/cli/src/terminal/Terminal.ts +0 -365
  113. package/packages/cli/src/terminal/index.ts +0 -5
  114. package/packages/cli/src/types.ts +0 -155
  115. package/packages/cli/tsconfig.json +0 -20
  116. package/packages/core/package.json +0 -35
  117. package/packages/core/src/actions.ts +0 -1210
  118. package/packages/core/src/errors.ts +0 -296
  119. package/packages/core/src/index.test.ts +0 -638
  120. package/packages/core/src/index.ts +0 -220
  121. package/packages/core/src/ref-map.ts +0 -107
  122. package/packages/core/src/snapshot.ts +0 -873
  123. package/packages/core/src/types.ts +0 -536
  124. package/packages/core/tsconfig.json +0 -23
  125. package/packages/extension/README.md +0 -129
  126. package/packages/extension/package.json +0 -43
  127. package/packages/extension/src/background.ts +0 -888
  128. package/packages/extension/src/content.ts +0 -172
  129. package/packages/extension/src/index.ts +0 -579
  130. package/packages/extension/src/session-manager.ts +0 -385
  131. package/packages/extension/src/types.ts +0 -162
  132. package/packages/extension/tsconfig.json +0 -28
  133. package/src/index.ts +0 -64
  134. package/tsconfig.build.json +0 -12
  135. package/tsconfig.json +0 -26
  136. package/vitest.config.ts +0 -13
@@ -0,0 +1,18 @@
1
+ /**
2
+ * @btcp/extension - Content Script
3
+ *
4
+ * Runs in web pages, handles DOM commands from the background script.
5
+ * Uses ContentAgent from @btcp/core for DOM operations.
6
+ */
7
+ import { type ContentAgent, type Response } from '@btcp/core';
8
+ import type { Command } from './types.js';
9
+ /**
10
+ * Get or create the ContentAgent instance for this page
11
+ */
12
+ declare function getContentAgent(): ContentAgent;
13
+ /**
14
+ * Handle a command from the background script
15
+ */
16
+ declare function handleCommand(command: Command): Promise<Response>;
17
+ export { getContentAgent, handleCommand };
18
+ //# sourceMappingURL=content.d.ts.map
@@ -0,0 +1,149 @@
1
+ /**
2
+ * @btcp/extension - Content Script
3
+ *
4
+ * Runs in web pages, handles DOM commands from the background script.
5
+ * Uses ContentAgent from @btcp/core for DOM operations.
6
+ */
7
+ import { createContentAgent } from '@btcp/core';
8
+ let agent = null;
9
+ let isContentScriptReady = false;
10
+ /**
11
+ * Get or create the ContentAgent instance for this page
12
+ */
13
+ function getContentAgent() {
14
+ if (!agent) {
15
+ agent = createContentAgent(document, window);
16
+ isContentScriptReady = true;
17
+ console.log('[ContentScript] Agent initialized');
18
+ }
19
+ return agent;
20
+ }
21
+ // Initialize agent immediately
22
+ getContentAgent();
23
+ /**
24
+ * Check if a command is a core DOM command
25
+ */
26
+ function isCoreCommand(command) {
27
+ const extensionActions = [
28
+ 'navigate', 'back', 'forward', 'reload',
29
+ 'getUrl', 'getTitle', 'screenshot',
30
+ 'tabNew', 'tabClose', 'tabSwitch', 'tabList',
31
+ ];
32
+ return !extensionActions.includes(command.action);
33
+ }
34
+ /**
35
+ * Handle a command from the background script
36
+ */
37
+ async function handleCommand(command) {
38
+ // Core DOM commands are handled by ContentAgent
39
+ if (isCoreCommand(command)) {
40
+ return getContentAgent().execute(command);
41
+ }
42
+ // Extension commands that need content script execution
43
+ switch (command.action) {
44
+ case 'getUrl':
45
+ return {
46
+ id: command.id,
47
+ success: true,
48
+ data: { url: window.location.href },
49
+ };
50
+ case 'getTitle':
51
+ return {
52
+ id: command.id,
53
+ success: true,
54
+ data: { title: document.title },
55
+ };
56
+ default:
57
+ // Forward to background script
58
+ return {
59
+ id: command.id,
60
+ success: false,
61
+ error: `Command ${command.action} must be handled by background script`,
62
+ };
63
+ }
64
+ }
65
+ /**
66
+ * Listen for messages from background script
67
+ */
68
+ chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
69
+ const msg = message;
70
+ // Handle ping messages for heartbeat
71
+ if (msg.type === 'btcp:ping') {
72
+ sendResponse({ type: 'btcp:pong', ready: isContentScriptReady });
73
+ return true;
74
+ }
75
+ if (msg.type !== 'btcp:command') {
76
+ return false;
77
+ }
78
+ handleCommand(msg.command)
79
+ .then((response) => {
80
+ sendResponse({ type: 'btcp:response', response });
81
+ })
82
+ .catch((error) => {
83
+ sendResponse({
84
+ type: 'btcp:response',
85
+ response: {
86
+ id: msg.command.id,
87
+ success: false,
88
+ error: error instanceof Error ? error.message : String(error),
89
+ },
90
+ });
91
+ });
92
+ // Return true to indicate async response
93
+ return true;
94
+ });
95
+ /**
96
+ * Also listen for postMessage from page scripts
97
+ * This allows scripts injected into the page to use the agent
98
+ */
99
+ window.addEventListener('message', async (event) => {
100
+ if (event.source !== window)
101
+ return;
102
+ const msg = event.data;
103
+ if (msg?.type !== 'btcp:command')
104
+ return;
105
+ const response = await handleCommand(msg.command);
106
+ window.postMessage({
107
+ type: 'btcp:response',
108
+ response,
109
+ }, '*');
110
+ });
111
+ /**
112
+ * Lifecycle event listeners for session keep-alive
113
+ */
114
+ // Detect page visibility changes
115
+ document.addEventListener('visibilitychange', () => {
116
+ if (!document.hidden) {
117
+ console.log('[ContentScript] Page became visible, checking connection...');
118
+ }
119
+ });
120
+ // Detect freeze/resume events (Chrome 68+)
121
+ document.addEventListener('freeze', () => {
122
+ console.log('[ContentScript] Page frozen');
123
+ });
124
+ document.addEventListener('resume', () => {
125
+ console.log('[ContentScript] Page resumed, re-initializing agent...');
126
+ // Re-initialize agent to ensure fresh state
127
+ agent = null;
128
+ getContentAgent();
129
+ });
130
+ // Clear refs on navigation (full page navigation)
131
+ window.addEventListener('beforeunload', () => {
132
+ console.log('[ContentScript] Page navigating, clearing refs...');
133
+ const currentAgent = getContentAgent();
134
+ currentAgent.clearRefs();
135
+ });
136
+ // Detect SPA navigation (URL changes without full page reload)
137
+ let lastUrl = window.location.href;
138
+ setInterval(() => {
139
+ const currentUrl = window.location.href;
140
+ if (currentUrl !== lastUrl) {
141
+ console.log('[ContentScript] URL changed, clearing refs...', { from: lastUrl, to: currentUrl });
142
+ lastUrl = currentUrl;
143
+ const currentAgent = getContentAgent();
144
+ currentAgent.clearRefs();
145
+ }
146
+ }, 1000);
147
+ // Export for programmatic use
148
+ export { getContentAgent, handleCommand };
149
+ //# sourceMappingURL=content.js.map
@@ -0,0 +1,228 @@
1
+ /**
2
+ * @btcp/extension
3
+ *
4
+ * Chrome extension bridge for browser automation.
5
+ *
6
+ * Architecture:
7
+ * - BackgroundAgent: Runs in background script, manages tabs/navigation/screenshots
8
+ * - ContentAgent: Runs in content scripts, handles DOM operations (from @btcp/core)
9
+ * - Client: API for sending commands from popup or external scripts
10
+ *
11
+ * @example Background script setup:
12
+ * ```typescript
13
+ * import { BackgroundAgent, setupMessageListener } from '@btcp/extension';
14
+ *
15
+ * // Set up message routing
16
+ * setupMessageListener();
17
+ *
18
+ * // Or use BackgroundAgent directly
19
+ * const agent = new BackgroundAgent();
20
+ * await agent.navigate('https://example.com');
21
+ * await agent.screenshot();
22
+ * ```
23
+ *
24
+ * @example Content script setup:
25
+ * ```typescript
26
+ * import { createContentAgent } from '@btcp/core';
27
+ *
28
+ * const agent = createContentAgent();
29
+ * await agent.execute({ id: '1', action: 'snapshot' });
30
+ * ```
31
+ *
32
+ * @example Popup/external usage:
33
+ * ```typescript
34
+ * import { createClient } from '@btcp/extension';
35
+ *
36
+ * const client = createClient();
37
+ * await client.navigate('https://example.com');
38
+ * const snapshot = await client.snapshot();
39
+ * await client.click('@ref:5');
40
+ * ```
41
+ */
42
+ import type { Command, Response, TabInfo } from './types.js';
43
+ import { BackgroundAgent as _BackgroundAgent, getBackgroundAgent as _getBackgroundAgent, setupMessageListener as _setupMessageListener, BrowserAgent as _BrowserAgent, getBrowserAgent as _getBrowserAgent } from './background.js';
44
+ export * from './types.js';
45
+ export { _BackgroundAgent as BackgroundAgent, _getBackgroundAgent as getBackgroundAgent, _setupMessageListener as setupMessageListener, _BrowserAgent as BrowserAgent, _getBrowserAgent as getBrowserAgent, };
46
+ export { createContentAgent, type ContentAgent } from '@btcp/core';
47
+ export type { SnapshotData, BoundingBox, Modifier, } from '@btcp/core';
48
+ /**
49
+ * Client for sending commands to the extension background script
50
+ */
51
+ export interface Client {
52
+ /**
53
+ * Execute a raw command
54
+ */
55
+ execute(command: Command): Promise<Response>;
56
+ /**
57
+ * Navigate to a URL
58
+ */
59
+ navigate(url: string, options?: {
60
+ waitUntil?: 'load' | 'domcontentloaded';
61
+ }): Promise<Response>;
62
+ /**
63
+ * Go back in history
64
+ */
65
+ back(): Promise<Response>;
66
+ /**
67
+ * Go forward in history
68
+ */
69
+ forward(): Promise<Response>;
70
+ /**
71
+ * Reload the page
72
+ */
73
+ reload(options?: {
74
+ bypassCache?: boolean;
75
+ }): Promise<Response>;
76
+ /**
77
+ * Get the current URL
78
+ */
79
+ getUrl(): Promise<string>;
80
+ /**
81
+ * Get the page title
82
+ */
83
+ getTitle(): Promise<string>;
84
+ /**
85
+ * Take a snapshot of the page
86
+ */
87
+ snapshot(options?: {
88
+ selector?: string;
89
+ maxDepth?: number;
90
+ interactive?: boolean;
91
+ compact?: boolean;
92
+ format?: 'tree' | 'html';
93
+ }): Promise<string>;
94
+ /**
95
+ * Click an element
96
+ */
97
+ click(selector: string, options?: {
98
+ button?: 'left' | 'right' | 'middle';
99
+ }): Promise<Response>;
100
+ /**
101
+ * Type text into an element
102
+ */
103
+ type(selector: string, text: string, options?: {
104
+ delay?: number;
105
+ clear?: boolean;
106
+ }): Promise<Response>;
107
+ /**
108
+ * Fill an input with a value
109
+ */
110
+ fill(selector: string, value: string): Promise<Response>;
111
+ /**
112
+ * Get text content of an element
113
+ */
114
+ getText(selector: string): Promise<string | null>;
115
+ /**
116
+ * Check if element is visible
117
+ */
118
+ isVisible(selector: string): Promise<boolean>;
119
+ /**
120
+ * Take a screenshot
121
+ */
122
+ screenshot(options?: {
123
+ format?: 'png' | 'jpeg';
124
+ quality?: number;
125
+ }): Promise<string>;
126
+ /**
127
+ * Open a new tab
128
+ */
129
+ tabNew(options?: {
130
+ url?: string;
131
+ active?: boolean;
132
+ }): Promise<{
133
+ tabId: number;
134
+ url?: string;
135
+ }>;
136
+ /**
137
+ * Close a tab
138
+ */
139
+ tabClose(tabId?: number): Promise<Response>;
140
+ /**
141
+ * Switch to a tab
142
+ */
143
+ tabSwitch(tabId: number): Promise<Response>;
144
+ /**
145
+ * List all tabs
146
+ */
147
+ tabList(): Promise<TabInfo[]>;
148
+ /**
149
+ * Create a new tab group
150
+ */
151
+ groupCreate(options?: {
152
+ tabIds?: number[];
153
+ title?: string;
154
+ color?: string;
155
+ collapsed?: boolean;
156
+ }): Promise<{
157
+ group: import('./session-types.js').GroupInfo;
158
+ }>;
159
+ /**
160
+ * Update a tab group
161
+ */
162
+ groupUpdate(groupId: number, options: {
163
+ title?: string;
164
+ color?: string;
165
+ collapsed?: boolean;
166
+ }): Promise<{
167
+ group: import('./session-types.js').GroupInfo;
168
+ }>;
169
+ /**
170
+ * Delete a tab group (closes all tabs)
171
+ */
172
+ groupDelete(groupId: number): Promise<Response>;
173
+ /**
174
+ * List all tab groups
175
+ */
176
+ groupList(): Promise<import('./session-types.js').GroupInfo[]>;
177
+ /**
178
+ * Add tabs to a group
179
+ */
180
+ groupAddTabs(groupId: number, tabIds: number[]): Promise<Response>;
181
+ /**
182
+ * Remove tabs from their group
183
+ */
184
+ groupRemoveTabs(tabIds: number[]): Promise<Response>;
185
+ /**
186
+ * Get a specific tab group
187
+ */
188
+ groupGet(groupId: number): Promise<{
189
+ group: import('./session-types.js').GroupInfo;
190
+ }>;
191
+ /**
192
+ * Get current active session
193
+ */
194
+ sessionGetCurrent(): Promise<{
195
+ session: import('./session-types.js').SessionInfo | null;
196
+ }>;
197
+ /**
198
+ * Initialize popup (triggers session reconnection check)
199
+ */
200
+ popupInitialize(): Promise<{
201
+ initialized: boolean;
202
+ reconnected: boolean;
203
+ }>;
204
+ }
205
+ /**
206
+ * Create a client for communicating with the extension
207
+ *
208
+ * This function works in both popup/content scripts and background scripts:
209
+ * - In popup/content scripts: Uses chrome.runtime.sendMessage to communicate with background
210
+ * - In background scripts: Uses BackgroundAgent directly for better performance
211
+ *
212
+ * @example Popup usage:
213
+ * ```typescript
214
+ * import { createClient } from '@btcp/browser-agent/extension';
215
+ * const client = createClient();
216
+ * await client.navigate('https://example.com');
217
+ * ```
218
+ *
219
+ * @example Background script usage:
220
+ * ```typescript
221
+ * import { createClient } from '@btcp/browser-agent/extension';
222
+ * const client = createClient();
223
+ * // Works the same way - commands go directly to BackgroundAgent
224
+ * await client.navigate('https://example.com');
225
+ * ```
226
+ */
227
+ export declare function createClient(): Client;
228
+ //# sourceMappingURL=index.d.ts.map