btcp-browser-agent 0.1.7 → 0.1.8
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/LICENSE +21 -21
- package/README.md +338 -306
- package/package.json +69 -69
- package/packages/core/dist/actions.js +98 -67
- package/packages/core/dist/assertions.d.ts +118 -0
- package/packages/core/dist/assertions.js +230 -0
- package/packages/core/dist/errors.d.ts +23 -1
- package/packages/core/dist/errors.js +49 -0
- package/packages/core/dist/index.d.ts +1 -0
- package/packages/core/dist/index.js +1 -0
- package/packages/extension/dist/background.d.ts +8 -1
- package/packages/extension/dist/background.js +73 -14
- package/packages/extension/dist/content.js +114 -0
- package/packages/extension/dist/index.d.ts +43 -0
- package/packages/extension/dist/index.js +26 -0
- package/packages/extension/dist/remote.d.ts +133 -0
- package/packages/extension/dist/remote.js +668 -0
- package/packages/extension/dist/script-messenger.d.ts +132 -0
- package/packages/extension/dist/script-messenger.js +86 -0
- package/packages/extension/dist/types.d.ts +76 -2
|
@@ -42,6 +42,8 @@
|
|
|
42
42
|
import type { Command, Response, TabInfo } from './types.js';
|
|
43
43
|
import { BackgroundAgent as _BackgroundAgent, getBackgroundAgent as _getBackgroundAgent, setupMessageListener as _setupMessageListener, BrowserAgent as _BrowserAgent, getBrowserAgent as _getBrowserAgent } from './background.js';
|
|
44
44
|
export * from './types.js';
|
|
45
|
+
export { createScriptMessenger, createMethodMessenger, type MessageDefinitions, type ScriptMessenger, type MethodMessenger, type ScriptMessengerOptions, type PayloadOf, type ResultOf, } from './script-messenger.js';
|
|
46
|
+
export { createRemoteAgent, getBrowserToolDefinitions, mapToolToCommand, formatResponseForBTCP, type RemoteAgent, type RemoteAgentConfig, type RemoteAgentEvents, type BTCPToolDefinition, type BTCPContent, } from './remote.js';
|
|
45
47
|
export { _BackgroundAgent as BackgroundAgent, _getBackgroundAgent as getBackgroundAgent, _setupMessageListener as setupMessageListener, _BrowserAgent as BrowserAgent, _getBrowserAgent as getBrowserAgent, };
|
|
46
48
|
export { createContentAgent, type ContentAgent } from '../../core/dist/index.js';
|
|
47
49
|
export type { SnapshotData, BoundingBox, Modifier, } from '../../core/dist/index.js';
|
|
@@ -205,6 +207,47 @@ export interface Client {
|
|
|
205
207
|
initialized: boolean;
|
|
206
208
|
reconnected: boolean;
|
|
207
209
|
}>;
|
|
210
|
+
/**
|
|
211
|
+
* Inject a script into the page's main world
|
|
212
|
+
*
|
|
213
|
+
* The script runs in the page context (not the content script isolated world),
|
|
214
|
+
* allowing access to page-level APIs like window, fetch interceptors, etc.
|
|
215
|
+
*
|
|
216
|
+
* @example
|
|
217
|
+
* ```typescript
|
|
218
|
+
* await client.scriptInject(`
|
|
219
|
+
* window.addEventListener('message', (event) => {
|
|
220
|
+
* if (event.data?.type !== 'btcp:script-command') return;
|
|
221
|
+
* if (event.data.scriptId !== 'helper') return;
|
|
222
|
+
* const { commandId, payload } = event.data;
|
|
223
|
+
* // Handle and ack
|
|
224
|
+
* window.postMessage({ type: 'btcp:script-ack', commandId, result: { ok: true } }, '*');
|
|
225
|
+
* });
|
|
226
|
+
* `, { scriptId: 'helper' });
|
|
227
|
+
* ```
|
|
228
|
+
*/
|
|
229
|
+
scriptInject(code: string, options?: {
|
|
230
|
+
scriptId?: string;
|
|
231
|
+
}): Promise<{
|
|
232
|
+
scriptId: string;
|
|
233
|
+
injected: boolean;
|
|
234
|
+
}>;
|
|
235
|
+
/**
|
|
236
|
+
* Send a command to an injected script and wait for acknowledgment
|
|
237
|
+
*
|
|
238
|
+
* @example
|
|
239
|
+
* ```typescript
|
|
240
|
+
* const result = await client.scriptSend(
|
|
241
|
+
* { action: 'getData', selector: '.items' },
|
|
242
|
+
* { scriptId: 'helper', timeout: 5000 }
|
|
243
|
+
* );
|
|
244
|
+
* console.log(result); // { items: [...] }
|
|
245
|
+
* ```
|
|
246
|
+
*/
|
|
247
|
+
scriptSend(payload: unknown, options?: {
|
|
248
|
+
scriptId?: string;
|
|
249
|
+
timeout?: number;
|
|
250
|
+
}): Promise<unknown>;
|
|
208
251
|
}
|
|
209
252
|
/**
|
|
210
253
|
* Generate a unique command ID for BTCP commands
|
|
@@ -42,6 +42,10 @@
|
|
|
42
42
|
// Import for local use (and re-export below)
|
|
43
43
|
import { BackgroundAgent as _BackgroundAgent, getBackgroundAgent as _getBackgroundAgent, setupMessageListener as _setupMessageListener, BrowserAgent as _BrowserAgent, getBrowserAgent as _getBrowserAgent, } from './background.js';
|
|
44
44
|
export * from './types.js';
|
|
45
|
+
// Re-export script messenger types and functions
|
|
46
|
+
export { createScriptMessenger, createMethodMessenger, } from './script-messenger.js';
|
|
47
|
+
// Re-export remote agent for BTCP protocol control
|
|
48
|
+
export { createRemoteAgent, getBrowserToolDefinitions, mapToolToCommand, formatResponseForBTCP, } from './remote.js';
|
|
45
49
|
// Re-export BackgroundAgent for background script usage
|
|
46
50
|
export { _BackgroundAgent as BackgroundAgent, _getBackgroundAgent as getBackgroundAgent, _setupMessageListener as setupMessageListener,
|
|
47
51
|
// Deprecated aliases for backwards compatibility
|
|
@@ -353,6 +357,28 @@ export function createClient() {
|
|
|
353
357
|
assertSuccess(response);
|
|
354
358
|
return response.data;
|
|
355
359
|
},
|
|
360
|
+
// Script Injection
|
|
361
|
+
async scriptInject(code, options) {
|
|
362
|
+
const response = await sendCommand({
|
|
363
|
+
id: generateCommandId(),
|
|
364
|
+
action: 'scriptInject',
|
|
365
|
+
code,
|
|
366
|
+
scriptId: options?.scriptId,
|
|
367
|
+
});
|
|
368
|
+
assertSuccess(response);
|
|
369
|
+
return response.data;
|
|
370
|
+
},
|
|
371
|
+
async scriptSend(payload, options) {
|
|
372
|
+
const response = await sendCommand({
|
|
373
|
+
id: generateCommandId(),
|
|
374
|
+
action: 'scriptSend',
|
|
375
|
+
payload,
|
|
376
|
+
scriptId: options?.scriptId,
|
|
377
|
+
timeout: options?.timeout,
|
|
378
|
+
});
|
|
379
|
+
assertSuccess(response);
|
|
380
|
+
return response.data.result;
|
|
381
|
+
},
|
|
356
382
|
};
|
|
357
383
|
}
|
|
358
384
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @btcp/extension - Remote Control via BTCP Protocol
|
|
3
|
+
*
|
|
4
|
+
* Enables remote AI agents to control the browser via the Browser Tool Calling Protocol.
|
|
5
|
+
* Uses SSE for receiving commands and HTTP POST for sending results.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { createRemoteAgent } from '@btcp/browser-agent/extension';
|
|
10
|
+
*
|
|
11
|
+
* const remote = createRemoteAgent({
|
|
12
|
+
* serverUrl: 'http://localhost:8080',
|
|
13
|
+
* sessionId: 'my-session',
|
|
14
|
+
* });
|
|
15
|
+
*
|
|
16
|
+
* await remote.connect();
|
|
17
|
+
* // Browser is now controllable by the BTCP server
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
import type { Command, Response } from './types.js';
|
|
21
|
+
import { type BackgroundAgent } from './background.js';
|
|
22
|
+
/**
|
|
23
|
+
* BTCP tool definition schema
|
|
24
|
+
*/
|
|
25
|
+
export interface BTCPToolDefinition {
|
|
26
|
+
name: string;
|
|
27
|
+
description: string;
|
|
28
|
+
inputSchema: {
|
|
29
|
+
type: 'object';
|
|
30
|
+
properties: Record<string, unknown>;
|
|
31
|
+
required?: string[];
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* BTCP content types for tool responses
|
|
36
|
+
*/
|
|
37
|
+
export type BTCPContent = {
|
|
38
|
+
type: 'text';
|
|
39
|
+
text: string;
|
|
40
|
+
} | {
|
|
41
|
+
type: 'image';
|
|
42
|
+
data: string;
|
|
43
|
+
mimeType: string;
|
|
44
|
+
} | {
|
|
45
|
+
type: 'resource';
|
|
46
|
+
uri: string;
|
|
47
|
+
mimeType?: string;
|
|
48
|
+
text?: string;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* BTCP client configuration
|
|
52
|
+
*/
|
|
53
|
+
export interface RemoteAgentConfig {
|
|
54
|
+
/** BTCP server URL */
|
|
55
|
+
serverUrl: string;
|
|
56
|
+
/** Session ID for this browser instance */
|
|
57
|
+
sessionId?: string;
|
|
58
|
+
/** Enable auto-reconnect on disconnect (default: true) */
|
|
59
|
+
autoReconnect?: boolean;
|
|
60
|
+
/** Reconnect delay in ms (default: 1000) */
|
|
61
|
+
reconnectDelay?: number;
|
|
62
|
+
/** Max reconnect attempts (default: 10) */
|
|
63
|
+
maxReconnectAttempts?: number;
|
|
64
|
+
/** Connection timeout in ms (default: 30000) */
|
|
65
|
+
connectionTimeout?: number;
|
|
66
|
+
/** Enable debug logging (default: false) */
|
|
67
|
+
debug?: boolean;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* BTCP client events
|
|
71
|
+
*/
|
|
72
|
+
export interface RemoteAgentEvents {
|
|
73
|
+
connect: () => void;
|
|
74
|
+
disconnect: (code?: number, reason?: string) => void;
|
|
75
|
+
error: (error: Error) => void;
|
|
76
|
+
toolCall: (name: string, args: Record<string, unknown>) => void;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Get all browser tool definitions for BTCP registration
|
|
80
|
+
*/
|
|
81
|
+
export declare function getBrowserToolDefinitions(): BTCPToolDefinition[];
|
|
82
|
+
/**
|
|
83
|
+
* Map BTCP tool name and arguments to browser-agent Command
|
|
84
|
+
*/
|
|
85
|
+
export declare function mapToolToCommand(toolName: string, args: Record<string, unknown>): Command;
|
|
86
|
+
/**
|
|
87
|
+
* Format response for BTCP protocol
|
|
88
|
+
*/
|
|
89
|
+
export declare function formatResponseForBTCP(response: Response): BTCPContent[];
|
|
90
|
+
/**
|
|
91
|
+
* Remote agent state
|
|
92
|
+
*/
|
|
93
|
+
type ConnectionState = 'disconnected' | 'connecting' | 'connected';
|
|
94
|
+
/**
|
|
95
|
+
* Remote agent for BTCP protocol control
|
|
96
|
+
*/
|
|
97
|
+
export interface RemoteAgent {
|
|
98
|
+
/** Connect to the BTCP server */
|
|
99
|
+
connect(): Promise<void>;
|
|
100
|
+
/** Disconnect from the BTCP server */
|
|
101
|
+
disconnect(): void;
|
|
102
|
+
/** Check if connected */
|
|
103
|
+
isConnected(): boolean;
|
|
104
|
+
/** Get current connection state */
|
|
105
|
+
getState(): ConnectionState;
|
|
106
|
+
/** Add event listener */
|
|
107
|
+
on<K extends keyof RemoteAgentEvents>(event: K, handler: RemoteAgentEvents[K]): void;
|
|
108
|
+
/** Remove event listener */
|
|
109
|
+
off<K extends keyof RemoteAgentEvents>(event: K, handler: RemoteAgentEvents[K]): void;
|
|
110
|
+
/** Get the underlying BackgroundAgent */
|
|
111
|
+
getAgent(): BackgroundAgent;
|
|
112
|
+
/** Get registered tool definitions */
|
|
113
|
+
getTools(): BTCPToolDefinition[];
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Create a remote agent that connects to a BTCP server
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```typescript
|
|
120
|
+
* const remote = createRemoteAgent({
|
|
121
|
+
* serverUrl: 'http://localhost:8080',
|
|
122
|
+
* sessionId: 'browser-1',
|
|
123
|
+
* });
|
|
124
|
+
*
|
|
125
|
+
* remote.on('connect', () => console.log('Connected!'));
|
|
126
|
+
* remote.on('toolCall', (name, args) => console.log('Tool called:', name, args));
|
|
127
|
+
*
|
|
128
|
+
* await remote.connect();
|
|
129
|
+
* ```
|
|
130
|
+
*/
|
|
131
|
+
export declare function createRemoteAgent(config: RemoteAgentConfig): RemoteAgent;
|
|
132
|
+
export {};
|
|
133
|
+
//# sourceMappingURL=remote.d.ts.map
|