illuma-agents 1.0.25 → 1.0.27
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/dist/cjs/main.cjs +4 -12
- package/dist/cjs/main.cjs.map +1 -1
- package/dist/cjs/tools/BrowserTools.cjs +220 -0
- package/dist/cjs/tools/BrowserTools.cjs.map +1 -0
- package/dist/esm/main.mjs +2 -4
- package/dist/esm/main.mjs.map +1 -1
- package/dist/esm/tools/BrowserTools.mjs +216 -0
- package/dist/esm/tools/BrowserTools.mjs.map +1 -0
- package/dist/types/index.d.ts +2 -4
- package/dist/types/tools/BrowserTools.d.ts +85 -0
- package/package.json +1 -1
- package/src/index.ts +3 -5
- package/src/tools/BrowserTools.ts +350 -0
- package/src/tools/__tests__/BrowserTools.test.ts +257 -0
package/dist/cjs/main.cjs
CHANGED
|
@@ -15,6 +15,7 @@ var Graph = require('./graphs/Graph.cjs');
|
|
|
15
15
|
var MultiAgentGraph = require('./graphs/MultiAgentGraph.cjs');
|
|
16
16
|
var Calculator = require('./tools/Calculator.cjs');
|
|
17
17
|
var CodeExecutor = require('./tools/CodeExecutor.cjs');
|
|
18
|
+
var BrowserTools = require('./tools/BrowserTools.cjs');
|
|
18
19
|
var ProgrammaticToolCalling = require('./tools/ProgrammaticToolCalling.cjs');
|
|
19
20
|
var ToolSearchRegex = require('./tools/ToolSearchRegex.cjs');
|
|
20
21
|
var handlers = require('./tools/handlers.cjs');
|
|
@@ -29,9 +30,6 @@ var tokens = require('./utils/tokens.cjs');
|
|
|
29
30
|
var toonFormat = require('./utils/toonFormat.cjs');
|
|
30
31
|
var contextAnalytics = require('./utils/contextAnalytics.cjs');
|
|
31
32
|
var index = require('./llm/openai/index.cjs');
|
|
32
|
-
var index$1 = require('./llm/anthropic/index.cjs');
|
|
33
|
-
var index$2 = require('./llm/bedrock/index.cjs');
|
|
34
|
-
var providers = require('./llm/providers.cjs');
|
|
35
33
|
|
|
36
34
|
|
|
37
35
|
|
|
@@ -83,6 +81,9 @@ exports.Calculator = Calculator.Calculator;
|
|
|
83
81
|
exports.createCodeExecutionTool = CodeExecutor.createCodeExecutionTool;
|
|
84
82
|
exports.getCodeBaseURL = CodeExecutor.getCodeBaseURL;
|
|
85
83
|
exports.imageExtRegex = CodeExecutor.imageExtRegex;
|
|
84
|
+
exports.EBrowserTools = BrowserTools.EBrowserTools;
|
|
85
|
+
exports.createBrowserTools = BrowserTools.createBrowserTools;
|
|
86
|
+
exports.hasBrowserCapability = BrowserTools.hasBrowserCapability;
|
|
86
87
|
exports.createProgrammaticToolCallingTool = ProgrammaticToolCalling.createProgrammaticToolCallingTool;
|
|
87
88
|
exports.executeTools = ProgrammaticToolCalling.executeTools;
|
|
88
89
|
exports.extractUsedToolNames = ProgrammaticToolCalling.extractUsedToolNames;
|
|
@@ -174,14 +175,5 @@ exports.isToonFormat = toonFormat.isToonFormat;
|
|
|
174
175
|
exports.jsonToToon = toonFormat.jsonToToon;
|
|
175
176
|
exports.processToolOutput = toonFormat.processToolOutput;
|
|
176
177
|
exports.buildContextAnalytics = contextAnalytics.buildContextAnalytics;
|
|
177
|
-
exports.AzureChatOpenAI = index.AzureChatOpenAI;
|
|
178
|
-
exports.ChatDeepSeek = index.ChatDeepSeek;
|
|
179
|
-
exports.ChatOpenAI = index.ChatOpenAI;
|
|
180
|
-
exports.ChatXAI = index.ChatXAI;
|
|
181
178
|
exports.CustomOpenAIClient = index.CustomOpenAIClient;
|
|
182
|
-
exports.CustomAnthropic = index$1.CustomAnthropic;
|
|
183
|
-
exports.CustomChatBedrockConverse = index$2.CustomChatBedrockConverse;
|
|
184
|
-
exports.getChatModelClass = providers.getChatModelClass;
|
|
185
|
-
exports.llmProviders = providers.llmProviders;
|
|
186
|
-
exports.manualToolStreamProviders = providers.manualToolStreamProviders;
|
|
187
179
|
//# sourceMappingURL=main.cjs.map
|
package/dist/cjs/main.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"main.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"main.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var zod = require('zod');
|
|
4
|
+
var tools = require('@langchain/core/tools');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Browser tool names - keep in sync with ranger-browser extension
|
|
8
|
+
* These tools execute locally in the browser extension, NOT on the server
|
|
9
|
+
*/
|
|
10
|
+
const EBrowserTools = {
|
|
11
|
+
CLICK: 'browser_click',
|
|
12
|
+
TYPE: 'browser_type',
|
|
13
|
+
NAVIGATE: 'browser_navigate',
|
|
14
|
+
SCROLL: 'browser_scroll',
|
|
15
|
+
EXTRACT: 'browser_extract',
|
|
16
|
+
HOVER: 'browser_hover',
|
|
17
|
+
WAIT: 'browser_wait',
|
|
18
|
+
BACK: 'browser_back',
|
|
19
|
+
SCREENSHOT: 'browser_screenshot',
|
|
20
|
+
GET_PAGE_STATE: 'browser_get_page_state',
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Check if browser capability is available based on request headers or context
|
|
24
|
+
* The browser extension sets these headers when connected:
|
|
25
|
+
* - X-Ranger-Browser-Extension: true
|
|
26
|
+
* - X-Ranger-Browser-Capable: true
|
|
27
|
+
*/
|
|
28
|
+
function hasBrowserCapability(req) {
|
|
29
|
+
if (!req?.headers) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
const browserExtension = req.headers['x-ranger-browser-extension'];
|
|
33
|
+
const browserCapable = req.headers['x-ranger-browser-capable'];
|
|
34
|
+
return browserExtension === 'true' || browserCapable === 'true';
|
|
35
|
+
}
|
|
36
|
+
// Tool schemas
|
|
37
|
+
const BrowserClickSchema = zod.z.object({
|
|
38
|
+
index: zod.z.number().describe('The index number [0], [1], etc. of the element to click from the page state element list'),
|
|
39
|
+
});
|
|
40
|
+
const BrowserTypeSchema = zod.z.object({
|
|
41
|
+
index: zod.z.number().describe('The index number of the input element to type into'),
|
|
42
|
+
text: zod.z.string().describe('The text to type into the element'),
|
|
43
|
+
pressEnter: zod.z.boolean().optional().describe('Whether to press Enter after typing (useful for search forms)'),
|
|
44
|
+
});
|
|
45
|
+
const BrowserNavigateSchema = zod.z.object({
|
|
46
|
+
url: zod.z.string().describe('The full URL to navigate to (must include https://)'),
|
|
47
|
+
});
|
|
48
|
+
const BrowserScrollSchema = zod.z.object({
|
|
49
|
+
direction: zod.z.enum(['up', 'down', 'left', 'right']).describe('Direction to scroll'),
|
|
50
|
+
amount: zod.z.number().optional().describe('Pixels to scroll (default: one viewport height)'),
|
|
51
|
+
});
|
|
52
|
+
const BrowserExtractSchema = zod.z.object({
|
|
53
|
+
query: zod.z.string().optional().describe('Optional: specific content to extract from the page'),
|
|
54
|
+
});
|
|
55
|
+
const BrowserHoverSchema = zod.z.object({
|
|
56
|
+
index: zod.z.number().describe('The index number of the element to hover over'),
|
|
57
|
+
});
|
|
58
|
+
const BrowserWaitSchema = zod.z.object({
|
|
59
|
+
duration: zod.z.number().optional().describe('Milliseconds to wait (default: 1000)'),
|
|
60
|
+
});
|
|
61
|
+
const BrowserBackSchema = zod.z.object({});
|
|
62
|
+
const BrowserScreenshotSchema = zod.z.object({});
|
|
63
|
+
const BrowserGetPageStateSchema = zod.z.object({});
|
|
64
|
+
/**
|
|
65
|
+
* Format browser action result for LLM consumption
|
|
66
|
+
*/
|
|
67
|
+
function formatResultForLLM(result, action) {
|
|
68
|
+
if (!result.success && result.error) {
|
|
69
|
+
return `Browser action "${action}" failed: ${result.error}`;
|
|
70
|
+
}
|
|
71
|
+
const parts = [];
|
|
72
|
+
if (result.url) {
|
|
73
|
+
parts.push(`**Current URL:** ${result.url}`);
|
|
74
|
+
}
|
|
75
|
+
if (result.title) {
|
|
76
|
+
parts.push(`**Page Title:** ${result.title}`);
|
|
77
|
+
}
|
|
78
|
+
if (result.elementList) {
|
|
79
|
+
parts.push(`\n**Interactive Elements:**\n${result.elementList}`);
|
|
80
|
+
}
|
|
81
|
+
if (result.screenshot) {
|
|
82
|
+
parts.push(`\n[Screenshot captured and displayed to user]`);
|
|
83
|
+
}
|
|
84
|
+
if (parts.length === 0) {
|
|
85
|
+
return `Browser action "${action}" completed successfully.`;
|
|
86
|
+
}
|
|
87
|
+
return parts.join('\n');
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Create browser tools with optional callback for waiting on results
|
|
91
|
+
*
|
|
92
|
+
* When waitForResult callback is provided:
|
|
93
|
+
* 1. Tool returns marker that triggers extension
|
|
94
|
+
* 2. Tool then awaits callback to get actual results
|
|
95
|
+
* 3. Returns real page state to LLM
|
|
96
|
+
*
|
|
97
|
+
* When no callback:
|
|
98
|
+
* 1. Tool returns marker only (for non-server contexts)
|
|
99
|
+
*
|
|
100
|
+
* NOTE: These tools use TEXT-BASED element lists, NOT screenshots
|
|
101
|
+
* Screenshots would be 100K+ tokens each - element lists are ~100 tokens
|
|
102
|
+
*/
|
|
103
|
+
function createBrowserTools(options) {
|
|
104
|
+
const { waitForResult } = options || {};
|
|
105
|
+
const tools$1 = [];
|
|
106
|
+
/**
|
|
107
|
+
* Helper to create tool function that optionally waits for results
|
|
108
|
+
* The toolCallId is extracted from the RunnableConfig passed by LangChain
|
|
109
|
+
*/
|
|
110
|
+
const createToolFunction = (action) => {
|
|
111
|
+
return async (args, config) => {
|
|
112
|
+
const toolCallId = config?.toolCall?.id || `tool_${Date.now()}_${Math.random().toString(36).slice(2)}`;
|
|
113
|
+
// Create marker for extension
|
|
114
|
+
const marker = {
|
|
115
|
+
requiresBrowserExecution: true,
|
|
116
|
+
action,
|
|
117
|
+
args,
|
|
118
|
+
toolCallId,
|
|
119
|
+
};
|
|
120
|
+
// If no callback, return marker immediately (extension handles via SSE interception)
|
|
121
|
+
if (!waitForResult) {
|
|
122
|
+
return JSON.stringify(marker);
|
|
123
|
+
}
|
|
124
|
+
// With callback: wait for actual results from extension
|
|
125
|
+
// The marker is still returned initially via SSE, but we wait for the callback
|
|
126
|
+
try {
|
|
127
|
+
const result = await waitForResult(action, args, toolCallId);
|
|
128
|
+
return formatResultForLLM(result, action);
|
|
129
|
+
}
|
|
130
|
+
catch (error) {
|
|
131
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
132
|
+
return `Browser action "${action}" failed: ${errorMessage}`;
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
};
|
|
136
|
+
// browser_click
|
|
137
|
+
tools$1.push(tools.tool(createToolFunction('click'), {
|
|
138
|
+
name: EBrowserTools.CLICK,
|
|
139
|
+
description: `Click an element on the current web page by its index number.
|
|
140
|
+
The element list shows clickable items like: [0]<button>Submit</button> [1]<a href="/home">Home</a>
|
|
141
|
+
Use the index number in brackets to click that element.
|
|
142
|
+
After clicking, you receive an updated element list showing the new page state.`,
|
|
143
|
+
schema: BrowserClickSchema,
|
|
144
|
+
}));
|
|
145
|
+
// browser_type
|
|
146
|
+
tools$1.push(tools.tool(createToolFunction('type'), {
|
|
147
|
+
name: EBrowserTools.TYPE,
|
|
148
|
+
description: `Type text into an input element on the page.
|
|
149
|
+
Find the input element in the list by its index (e.g., [5]<input placeholder="Search">).
|
|
150
|
+
Set pressEnter: true to submit forms after typing.
|
|
151
|
+
After typing, you receive an updated element list.`,
|
|
152
|
+
schema: BrowserTypeSchema,
|
|
153
|
+
}));
|
|
154
|
+
// browser_navigate
|
|
155
|
+
tools$1.push(tools.tool(createToolFunction('navigate'), {
|
|
156
|
+
name: EBrowserTools.NAVIGATE,
|
|
157
|
+
description: `Navigate to a URL. Always include the full URL with https://.
|
|
158
|
+
After navigation, you receive the new page's element list.`,
|
|
159
|
+
schema: BrowserNavigateSchema,
|
|
160
|
+
}));
|
|
161
|
+
// browser_scroll
|
|
162
|
+
tools$1.push(tools.tool(createToolFunction('scroll'), {
|
|
163
|
+
name: EBrowserTools.SCROLL,
|
|
164
|
+
description: `Scroll the page to reveal more content.
|
|
165
|
+
Use 'down' to scroll down, 'up' to scroll up.
|
|
166
|
+
After scrolling, you receive an updated element list with newly visible elements.`,
|
|
167
|
+
schema: BrowserScrollSchema,
|
|
168
|
+
}));
|
|
169
|
+
// browser_extract
|
|
170
|
+
tools$1.push(tools.tool(createToolFunction('extract'), {
|
|
171
|
+
name: EBrowserTools.EXTRACT,
|
|
172
|
+
description: `Extract content from the current page.
|
|
173
|
+
Returns page URL, title, and element list.`,
|
|
174
|
+
schema: BrowserExtractSchema,
|
|
175
|
+
}));
|
|
176
|
+
// browser_hover
|
|
177
|
+
tools$1.push(tools.tool(createToolFunction('hover'), {
|
|
178
|
+
name: EBrowserTools.HOVER,
|
|
179
|
+
description: `Hover over an element to reveal tooltips, dropdowns, or other hover-triggered content.
|
|
180
|
+
After hovering, you receive an updated element list with any newly revealed elements.`,
|
|
181
|
+
schema: BrowserHoverSchema,
|
|
182
|
+
}));
|
|
183
|
+
// browser_wait
|
|
184
|
+
tools$1.push(tools.tool(createToolFunction('wait'), {
|
|
185
|
+
name: EBrowserTools.WAIT,
|
|
186
|
+
description: `Wait for a specified duration for page content to load.
|
|
187
|
+
Use this after actions that trigger async content loading.
|
|
188
|
+
After waiting, you receive an updated element list.`,
|
|
189
|
+
schema: BrowserWaitSchema,
|
|
190
|
+
}));
|
|
191
|
+
// browser_back
|
|
192
|
+
tools$1.push(tools.tool(createToolFunction('back'), {
|
|
193
|
+
name: EBrowserTools.BACK,
|
|
194
|
+
description: `Go back to the previous page in browser history.
|
|
195
|
+
After going back, you receive the previous page's element list.`,
|
|
196
|
+
schema: BrowserBackSchema,
|
|
197
|
+
}));
|
|
198
|
+
// browser_screenshot
|
|
199
|
+
tools$1.push(tools.tool(createToolFunction('screenshot'), {
|
|
200
|
+
name: EBrowserTools.SCREENSHOT,
|
|
201
|
+
description: `Capture a screenshot of the current page.
|
|
202
|
+
Returns the page state with a note that screenshot was displayed to the user.
|
|
203
|
+
Use browser_get_page_state to get the element list for automation.`,
|
|
204
|
+
schema: BrowserScreenshotSchema,
|
|
205
|
+
}));
|
|
206
|
+
// browser_get_page_state
|
|
207
|
+
tools$1.push(tools.tool(createToolFunction('get_page_state'), {
|
|
208
|
+
name: EBrowserTools.GET_PAGE_STATE,
|
|
209
|
+
description: `Get the current page state including URL, title, and all interactive elements.
|
|
210
|
+
Use this at the start of a task to see what elements are available.
|
|
211
|
+
Returns a text list of elements with their index numbers for interaction.`,
|
|
212
|
+
schema: BrowserGetPageStateSchema,
|
|
213
|
+
}));
|
|
214
|
+
return tools$1;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
exports.EBrowserTools = EBrowserTools;
|
|
218
|
+
exports.createBrowserTools = createBrowserTools;
|
|
219
|
+
exports.hasBrowserCapability = hasBrowserCapability;
|
|
220
|
+
//# sourceMappingURL=BrowserTools.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BrowserTools.cjs","sources":["../../../src/tools/BrowserTools.ts"],"sourcesContent":["import { z } from 'zod';\r\nimport { tool, DynamicStructuredTool } from '@langchain/core/tools';\r\nimport type * as t from '@/types';\r\n\r\n/**\r\n * Browser tool names - keep in sync with ranger-browser extension\r\n * These tools execute locally in the browser extension, NOT on the server\r\n */\r\nexport const EBrowserTools = {\r\n CLICK: 'browser_click',\r\n TYPE: 'browser_type',\r\n NAVIGATE: 'browser_navigate',\r\n SCROLL: 'browser_scroll',\r\n EXTRACT: 'browser_extract',\r\n HOVER: 'browser_hover',\r\n WAIT: 'browser_wait',\r\n BACK: 'browser_back',\r\n SCREENSHOT: 'browser_screenshot',\r\n GET_PAGE_STATE: 'browser_get_page_state',\r\n} as const;\r\n\r\nexport type BrowserToolName = typeof EBrowserTools[keyof typeof EBrowserTools];\r\n\r\n/**\r\n * Callback function type for waiting on browser action results\r\n * This allows the server (Ranger) to provide a callback that waits for the extension\r\n * to POST results back to the server before returning to the LLM.\r\n * \r\n * @param action - The browser action (click, type, navigate, etc.)\r\n * @param args - Arguments for the action\r\n * @param toolCallId - Unique ID for this tool call (from config.toolCall.id)\r\n * @returns Promise that resolves with the actual browser result (page state, etc.)\r\n */\r\nexport type BrowserToolCallback = (\r\n action: string,\r\n args: Record<string, unknown>,\r\n toolCallId: string\r\n) => Promise<BrowserActionResult>;\r\n\r\n/**\r\n * Result returned from browser action execution\r\n */\r\nexport interface BrowserActionResult {\r\n success: boolean;\r\n url?: string;\r\n title?: string;\r\n elementList?: string; // Text-based element list\r\n error?: string;\r\n screenshot?: string; // Base64 screenshot (if requested)\r\n}\r\n\r\n/**\r\n * Check if browser capability is available based on request headers or context\r\n * The browser extension sets these headers when connected:\r\n * - X-Ranger-Browser-Extension: true\r\n * - X-Ranger-Browser-Capable: true\r\n */\r\nexport function hasBrowserCapability(req?: { headers?: Record<string, string | string[] | undefined> }): boolean {\r\n if (!req?.headers) {\r\n return false;\r\n }\r\n \r\n const browserExtension = req.headers['x-ranger-browser-extension'];\r\n const browserCapable = req.headers['x-ranger-browser-capable'];\r\n \r\n return browserExtension === 'true' || browserCapable === 'true';\r\n}\r\n\r\n// Tool schemas\r\nconst BrowserClickSchema = z.object({\r\n index: z.number().describe('The index number [0], [1], etc. of the element to click from the page state element list'),\r\n});\r\n\r\nconst BrowserTypeSchema = z.object({\r\n index: z.number().describe('The index number of the input element to type into'),\r\n text: z.string().describe('The text to type into the element'),\r\n pressEnter: z.boolean().optional().describe('Whether to press Enter after typing (useful for search forms)'),\r\n});\r\n\r\nconst BrowserNavigateSchema = z.object({\r\n url: z.string().describe('The full URL to navigate to (must include https://)'),\r\n});\r\n\r\nconst BrowserScrollSchema = z.object({\r\n direction: z.enum(['up', 'down', 'left', 'right']).describe('Direction to scroll'),\r\n amount: z.number().optional().describe('Pixels to scroll (default: one viewport height)'),\r\n});\r\n\r\nconst BrowserExtractSchema = z.object({\r\n query: z.string().optional().describe('Optional: specific content to extract from the page'),\r\n});\r\n\r\nconst BrowserHoverSchema = z.object({\r\n index: z.number().describe('The index number of the element to hover over'),\r\n});\r\n\r\nconst BrowserWaitSchema = z.object({\r\n duration: z.number().optional().describe('Milliseconds to wait (default: 1000)'),\r\n});\r\n\r\nconst BrowserBackSchema = z.object({});\r\n\r\nconst BrowserScreenshotSchema = z.object({});\r\n\r\nconst BrowserGetPageStateSchema = z.object({});\r\n\r\n/**\r\n * Browser tool response interface\r\n * This is what the extension returns after executing the action\r\n */\r\nexport interface BrowserToolResponse {\r\n requiresBrowserExecution: true;\r\n action: string;\r\n args: Record<string, unknown>;\r\n toolCallId?: string; // Added to help extension correlate with callback\r\n}\r\n\r\n/**\r\n * Options for creating browser tools\r\n */\r\nexport interface CreateBrowserToolsOptions {\r\n /**\r\n * Optional callback that waits for browser action results.\r\n * When provided, tools will await this callback to get actual results from the extension.\r\n * When not provided, tools return markers immediately (for non-server contexts).\r\n */\r\n waitForResult?: BrowserToolCallback;\r\n}\r\n\r\n/**\r\n * Format browser action result for LLM consumption\r\n */\r\nfunction formatResultForLLM(result: BrowserActionResult, action: string): string {\r\n if (!result.success && result.error) {\r\n return `Browser action \"${action}\" failed: ${result.error}`;\r\n }\r\n\r\n const parts: string[] = [];\r\n \r\n if (result.url) {\r\n parts.push(`**Current URL:** ${result.url}`);\r\n }\r\n if (result.title) {\r\n parts.push(`**Page Title:** ${result.title}`);\r\n }\r\n if (result.elementList) {\r\n parts.push(`\\n**Interactive Elements:**\\n${result.elementList}`);\r\n }\r\n if (result.screenshot) {\r\n parts.push(`\\n[Screenshot captured and displayed to user]`);\r\n }\r\n \r\n if (parts.length === 0) {\r\n return `Browser action \"${action}\" completed successfully.`;\r\n }\r\n \r\n return parts.join('\\n');\r\n}\r\n\r\n/**\r\n * Create browser tools with optional callback for waiting on results\r\n * \r\n * When waitForResult callback is provided:\r\n * 1. Tool returns marker that triggers extension\r\n * 2. Tool then awaits callback to get actual results\r\n * 3. Returns real page state to LLM\r\n * \r\n * When no callback:\r\n * 1. Tool returns marker only (for non-server contexts)\r\n * \r\n * NOTE: These tools use TEXT-BASED element lists, NOT screenshots\r\n * Screenshots would be 100K+ tokens each - element lists are ~100 tokens\r\n */\r\nexport function createBrowserTools(options?: CreateBrowserToolsOptions): DynamicStructuredTool[] {\r\n const { waitForResult } = options || {};\r\n const tools: DynamicStructuredTool[] = [];\r\n\r\n /**\r\n * Helper to create tool function that optionally waits for results\r\n * The toolCallId is extracted from the RunnableConfig passed by LangChain\r\n */\r\n const createToolFunction = (action: string) => {\r\n return async (args: Record<string, unknown>, config?: { toolCall?: { id?: string } }): Promise<string> => {\r\n const toolCallId = config?.toolCall?.id || `tool_${Date.now()}_${Math.random().toString(36).slice(2)}`;\r\n \r\n // Create marker for extension\r\n const marker: BrowserToolResponse = {\r\n requiresBrowserExecution: true,\r\n action,\r\n args,\r\n toolCallId,\r\n };\r\n \r\n // If no callback, return marker immediately (extension handles via SSE interception)\r\n if (!waitForResult) {\r\n return JSON.stringify(marker);\r\n }\r\n \r\n // With callback: wait for actual results from extension\r\n // The marker is still returned initially via SSE, but we wait for the callback\r\n try {\r\n const result = await waitForResult(action, args, toolCallId);\r\n return formatResultForLLM(result, action);\r\n } catch (error) {\r\n const errorMessage = error instanceof Error ? error.message : String(error);\r\n return `Browser action \"${action}\" failed: ${errorMessage}`;\r\n }\r\n };\r\n };\r\n\r\n // browser_click\r\n tools.push(\r\n tool(\r\n createToolFunction('click'),\r\n {\r\n name: EBrowserTools.CLICK,\r\n description: `Click an element on the current web page by its index number.\r\nThe element list shows clickable items like: [0]<button>Submit</button> [1]<a href=\"/home\">Home</a>\r\nUse the index number in brackets to click that element.\r\nAfter clicking, you receive an updated element list showing the new page state.`,\r\n schema: BrowserClickSchema,\r\n }\r\n )\r\n );\r\n\r\n // browser_type\r\n tools.push(\r\n tool(\r\n createToolFunction('type'),\r\n {\r\n name: EBrowserTools.TYPE,\r\n description: `Type text into an input element on the page.\r\nFind the input element in the list by its index (e.g., [5]<input placeholder=\"Search\">).\r\nSet pressEnter: true to submit forms after typing.\r\nAfter typing, you receive an updated element list.`,\r\n schema: BrowserTypeSchema,\r\n }\r\n )\r\n );\r\n\r\n // browser_navigate\r\n tools.push(\r\n tool(\r\n createToolFunction('navigate'),\r\n {\r\n name: EBrowserTools.NAVIGATE,\r\n description: `Navigate to a URL. Always include the full URL with https://.\r\nAfter navigation, you receive the new page's element list.`,\r\n schema: BrowserNavigateSchema,\r\n }\r\n )\r\n );\r\n\r\n // browser_scroll\r\n tools.push(\r\n tool(\r\n createToolFunction('scroll'),\r\n {\r\n name: EBrowserTools.SCROLL,\r\n description: `Scroll the page to reveal more content.\r\nUse 'down' to scroll down, 'up' to scroll up.\r\nAfter scrolling, you receive an updated element list with newly visible elements.`,\r\n schema: BrowserScrollSchema,\r\n }\r\n )\r\n );\r\n\r\n // browser_extract\r\n tools.push(\r\n tool(\r\n createToolFunction('extract'),\r\n {\r\n name: EBrowserTools.EXTRACT,\r\n description: `Extract content from the current page.\r\nReturns page URL, title, and element list.`,\r\n schema: BrowserExtractSchema,\r\n }\r\n )\r\n );\r\n\r\n // browser_hover\r\n tools.push(\r\n tool(\r\n createToolFunction('hover'),\r\n {\r\n name: EBrowserTools.HOVER,\r\n description: `Hover over an element to reveal tooltips, dropdowns, or other hover-triggered content.\r\nAfter hovering, you receive an updated element list with any newly revealed elements.`,\r\n schema: BrowserHoverSchema,\r\n }\r\n )\r\n );\r\n\r\n // browser_wait\r\n tools.push(\r\n tool(\r\n createToolFunction('wait'),\r\n {\r\n name: EBrowserTools.WAIT,\r\n description: `Wait for a specified duration for page content to load.\r\nUse this after actions that trigger async content loading.\r\nAfter waiting, you receive an updated element list.`,\r\n schema: BrowserWaitSchema,\r\n }\r\n )\r\n );\r\n\r\n // browser_back\r\n tools.push(\r\n tool(\r\n createToolFunction('back'),\r\n {\r\n name: EBrowserTools.BACK,\r\n description: `Go back to the previous page in browser history.\r\nAfter going back, you receive the previous page's element list.`,\r\n schema: BrowserBackSchema,\r\n }\r\n )\r\n );\r\n\r\n // browser_screenshot\r\n tools.push(\r\n tool(\r\n createToolFunction('screenshot'),\r\n {\r\n name: EBrowserTools.SCREENSHOT,\r\n description: `Capture a screenshot of the current page.\r\nReturns the page state with a note that screenshot was displayed to the user.\r\nUse browser_get_page_state to get the element list for automation.`,\r\n schema: BrowserScreenshotSchema,\r\n }\r\n )\r\n );\r\n\r\n // browser_get_page_state\r\n tools.push(\r\n tool(\r\n createToolFunction('get_page_state'),\r\n {\r\n name: EBrowserTools.GET_PAGE_STATE,\r\n description: `Get the current page state including URL, title, and all interactive elements.\r\nUse this at the start of a task to see what elements are available.\r\nReturns a text list of elements with their index numbers for interaction.`,\r\n schema: BrowserGetPageStateSchema,\r\n }\r\n )\r\n );\r\n\r\n return tools;\r\n}\r\n"],"names":["z","tools","tool"],"mappings":";;;;;AAIA;;;AAGG;AACU,MAAA,aAAa,GAAG;AAC3B,IAAA,KAAK,EAAE,eAAe;AACtB,IAAA,IAAI,EAAE,cAAc;AACpB,IAAA,QAAQ,EAAE,kBAAkB;AAC5B,IAAA,MAAM,EAAE,gBAAgB;AACxB,IAAA,OAAO,EAAE,iBAAiB;AAC1B,IAAA,KAAK,EAAE,eAAe;AACtB,IAAA,IAAI,EAAE,cAAc;AACpB,IAAA,IAAI,EAAE,cAAc;AACpB,IAAA,UAAU,EAAE,oBAAoB;AAChC,IAAA,cAAc,EAAE,wBAAwB;;AAiC1C;;;;;AAKG;AACG,SAAU,oBAAoB,CAAC,GAAiE,EAAA;AACpG,IAAA,IAAI,CAAC,GAAG,EAAE,OAAO,EAAE;AACjB,QAAA,OAAO,KAAK;;IAGd,MAAM,gBAAgB,GAAG,GAAG,CAAC,OAAO,CAAC,4BAA4B,CAAC;IAClE,MAAM,cAAc,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AAI,EAAE,aAAa,CAAC,KAAK;AACzB,QAAA,WAAW,EAAE,CAAA;AACiE,qFAAA,CAAA;AAC9E,QAAA,MAAM,EAAE,kBAAkB;AAC3B,KAAA,CACF,CACF;;IAGDD,OAAK,CAAC,IAAI,CACRC,UAAI,CACF,kBAAkB,CAAC,MAAM,CAAC,EAC1B;QACE,IAAI,EAAE,aAAa,CAAC,IAAI;AACxB,QAAA,WAAW,EAAE,CAAA;;AAE+B,mDAAA,CAAA;AAC5C,QAAA,MAAM,EAAE,iBAAiB;AAC1B,KAAA,CACF,CACF;;IAGDD,OAAK,CAAC,IAAI,CACRC,UAAI,CACF,kBAAkB,CAAC,MAAM,CAAC,EAC1B;QACE,IAAI,EAAE,aAAa,CAAC,IAAI;AACxB,QAAA,WAAW,EAAE,CAAA;AAC2C,+DAAA,CAAA;AACxD,QAAA,MAAM,EAAE,iBAAiB;AAC1B,KAAA,CACF,CACF;;IAGDD,OAAK,CAAC,IAAI,CACRC,UAAI,CACF,kBAAkB,CAAC,YAAY,CAAC,EAChC;QACE,IAAI,EAAE,aAAa,CAAC,UAAU;AAC9B,QAAA,WAAW,EAAE,CAAA;;AAE8C,kEAAA,CAAA;AAC3D,QAAA,MAAM,EAAE,uBAAuB;AAChC,KAAA,CACF,CACF;;IAGDD,OAAK,CAAC,IAAI,CACRC,UAAI,CACF,kBAAkB,CAAC,gBAAgB,CAAC,EACpC;QACE,IAAI,EAAE,aAAa,CAAC,cAAc;AAClC,QAAA,WAAW,EAAE,CAAA;;AAEqD,yEAAA,CAAA;AAClE,QAAA,MAAM,EAAE,yBAAyB;AAClC,KAAA,CACF,CACF;AAED,IAAA,OAAOD,OAAK;AACd;;;;;;"}
|
package/dist/esm/main.mjs
CHANGED
|
@@ -13,6 +13,7 @@ export { Graph, StandardGraph } from './graphs/Graph.mjs';
|
|
|
13
13
|
export { MultiAgentGraph } from './graphs/MultiAgentGraph.mjs';
|
|
14
14
|
export { Calculator } from './tools/Calculator.mjs';
|
|
15
15
|
export { createCodeExecutionTool, getCodeBaseURL, imageExtRegex } from './tools/CodeExecutor.mjs';
|
|
16
|
+
export { EBrowserTools, createBrowserTools, hasBrowserCapability } from './tools/BrowserTools.mjs';
|
|
16
17
|
export { createProgrammaticToolCallingTool, executeTools, extractUsedToolNames, fetchSessionFiles, filterToolsByUsage, formatCompletedResponse, makeRequest, normalizeToPythonIdentifier, unwrapToolResponse } from './tools/ProgrammaticToolCalling.mjs';
|
|
17
18
|
export { countNestedGroups, createToolSearchRegexTool, escapeRegexSpecialChars, hasNestedQuantifiers, isDangerousPattern, sanitizeRegex } from './tools/ToolSearchRegex.mjs';
|
|
18
19
|
export { handleServerToolResult, handleToolCallChunks, handleToolCalls, toolResultTypes } from './tools/handlers.mjs';
|
|
@@ -26,8 +27,5 @@ export { RunnableCallable, sleep } from './utils/run.mjs';
|
|
|
26
27
|
export { TokenEncoderManager, createTokenCounter, getTokenCountForMessage } from './utils/tokens.mjs';
|
|
27
28
|
export { ensureToonLoaded, extractFirstJson, isToonAvailable, isToonFormat, jsonToToon, processToolOutput } from './utils/toonFormat.mjs';
|
|
28
29
|
export { buildContextAnalytics } from './utils/contextAnalytics.mjs';
|
|
29
|
-
export {
|
|
30
|
-
export { CustomAnthropic } from './llm/anthropic/index.mjs';
|
|
31
|
-
export { CustomChatBedrockConverse } from './llm/bedrock/index.mjs';
|
|
32
|
-
export { getChatModelClass, llmProviders, manualToolStreamProviders } from './llm/providers.mjs';
|
|
30
|
+
export { CustomOpenAIClient } from './llm/openai/index.mjs';
|
|
33
31
|
//# sourceMappingURL=main.mjs.map
|
package/dist/esm/main.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"main.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"main.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { tool } from '@langchain/core/tools';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Browser tool names - keep in sync with ranger-browser extension
|
|
6
|
+
* These tools execute locally in the browser extension, NOT on the server
|
|
7
|
+
*/
|
|
8
|
+
const EBrowserTools = {
|
|
9
|
+
CLICK: 'browser_click',
|
|
10
|
+
TYPE: 'browser_type',
|
|
11
|
+
NAVIGATE: 'browser_navigate',
|
|
12
|
+
SCROLL: 'browser_scroll',
|
|
13
|
+
EXTRACT: 'browser_extract',
|
|
14
|
+
HOVER: 'browser_hover',
|
|
15
|
+
WAIT: 'browser_wait',
|
|
16
|
+
BACK: 'browser_back',
|
|
17
|
+
SCREENSHOT: 'browser_screenshot',
|
|
18
|
+
GET_PAGE_STATE: 'browser_get_page_state',
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Check if browser capability is available based on request headers or context
|
|
22
|
+
* The browser extension sets these headers when connected:
|
|
23
|
+
* - X-Ranger-Browser-Extension: true
|
|
24
|
+
* - X-Ranger-Browser-Capable: true
|
|
25
|
+
*/
|
|
26
|
+
function hasBrowserCapability(req) {
|
|
27
|
+
if (!req?.headers) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
const browserExtension = req.headers['x-ranger-browser-extension'];
|
|
31
|
+
const browserCapable = req.headers['x-ranger-browser-capable'];
|
|
32
|
+
return browserExtension === 'true' || browserCapable === 'true';
|
|
33
|
+
}
|
|
34
|
+
// Tool schemas
|
|
35
|
+
const BrowserClickSchema = z.object({
|
|
36
|
+
index: z.number().describe('The index number [0], [1], etc. of the element to click from the page state element list'),
|
|
37
|
+
});
|
|
38
|
+
const BrowserTypeSchema = z.object({
|
|
39
|
+
index: z.number().describe('The index number of the input element to type into'),
|
|
40
|
+
text: z.string().describe('The text to type into the element'),
|
|
41
|
+
pressEnter: z.boolean().optional().describe('Whether to press Enter after typing (useful for search forms)'),
|
|
42
|
+
});
|
|
43
|
+
const BrowserNavigateSchema = z.object({
|
|
44
|
+
url: z.string().describe('The full URL to navigate to (must include https://)'),
|
|
45
|
+
});
|
|
46
|
+
const BrowserScrollSchema = z.object({
|
|
47
|
+
direction: z.enum(['up', 'down', 'left', 'right']).describe('Direction to scroll'),
|
|
48
|
+
amount: z.number().optional().describe('Pixels to scroll (default: one viewport height)'),
|
|
49
|
+
});
|
|
50
|
+
const BrowserExtractSchema = z.object({
|
|
51
|
+
query: z.string().optional().describe('Optional: specific content to extract from the page'),
|
|
52
|
+
});
|
|
53
|
+
const BrowserHoverSchema = z.object({
|
|
54
|
+
index: z.number().describe('The index number of the element to hover over'),
|
|
55
|
+
});
|
|
56
|
+
const BrowserWaitSchema = z.object({
|
|
57
|
+
duration: z.number().optional().describe('Milliseconds to wait (default: 1000)'),
|
|
58
|
+
});
|
|
59
|
+
const BrowserBackSchema = z.object({});
|
|
60
|
+
const BrowserScreenshotSchema = z.object({});
|
|
61
|
+
const BrowserGetPageStateSchema = z.object({});
|
|
62
|
+
/**
|
|
63
|
+
* Format browser action result for LLM consumption
|
|
64
|
+
*/
|
|
65
|
+
function formatResultForLLM(result, action) {
|
|
66
|
+
if (!result.success && result.error) {
|
|
67
|
+
return `Browser action "${action}" failed: ${result.error}`;
|
|
68
|
+
}
|
|
69
|
+
const parts = [];
|
|
70
|
+
if (result.url) {
|
|
71
|
+
parts.push(`**Current URL:** ${result.url}`);
|
|
72
|
+
}
|
|
73
|
+
if (result.title) {
|
|
74
|
+
parts.push(`**Page Title:** ${result.title}`);
|
|
75
|
+
}
|
|
76
|
+
if (result.elementList) {
|
|
77
|
+
parts.push(`\n**Interactive Elements:**\n${result.elementList}`);
|
|
78
|
+
}
|
|
79
|
+
if (result.screenshot) {
|
|
80
|
+
parts.push(`\n[Screenshot captured and displayed to user]`);
|
|
81
|
+
}
|
|
82
|
+
if (parts.length === 0) {
|
|
83
|
+
return `Browser action "${action}" completed successfully.`;
|
|
84
|
+
}
|
|
85
|
+
return parts.join('\n');
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Create browser tools with optional callback for waiting on results
|
|
89
|
+
*
|
|
90
|
+
* When waitForResult callback is provided:
|
|
91
|
+
* 1. Tool returns marker that triggers extension
|
|
92
|
+
* 2. Tool then awaits callback to get actual results
|
|
93
|
+
* 3. Returns real page state to LLM
|
|
94
|
+
*
|
|
95
|
+
* When no callback:
|
|
96
|
+
* 1. Tool returns marker only (for non-server contexts)
|
|
97
|
+
*
|
|
98
|
+
* NOTE: These tools use TEXT-BASED element lists, NOT screenshots
|
|
99
|
+
* Screenshots would be 100K+ tokens each - element lists are ~100 tokens
|
|
100
|
+
*/
|
|
101
|
+
function createBrowserTools(options) {
|
|
102
|
+
const { waitForResult } = options || {};
|
|
103
|
+
const tools = [];
|
|
104
|
+
/**
|
|
105
|
+
* Helper to create tool function that optionally waits for results
|
|
106
|
+
* The toolCallId is extracted from the RunnableConfig passed by LangChain
|
|
107
|
+
*/
|
|
108
|
+
const createToolFunction = (action) => {
|
|
109
|
+
return async (args, config) => {
|
|
110
|
+
const toolCallId = config?.toolCall?.id || `tool_${Date.now()}_${Math.random().toString(36).slice(2)}`;
|
|
111
|
+
// Create marker for extension
|
|
112
|
+
const marker = {
|
|
113
|
+
requiresBrowserExecution: true,
|
|
114
|
+
action,
|
|
115
|
+
args,
|
|
116
|
+
toolCallId,
|
|
117
|
+
};
|
|
118
|
+
// If no callback, return marker immediately (extension handles via SSE interception)
|
|
119
|
+
if (!waitForResult) {
|
|
120
|
+
return JSON.stringify(marker);
|
|
121
|
+
}
|
|
122
|
+
// With callback: wait for actual results from extension
|
|
123
|
+
// The marker is still returned initially via SSE, but we wait for the callback
|
|
124
|
+
try {
|
|
125
|
+
const result = await waitForResult(action, args, toolCallId);
|
|
126
|
+
return formatResultForLLM(result, action);
|
|
127
|
+
}
|
|
128
|
+
catch (error) {
|
|
129
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
130
|
+
return `Browser action "${action}" failed: ${errorMessage}`;
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
};
|
|
134
|
+
// browser_click
|
|
135
|
+
tools.push(tool(createToolFunction('click'), {
|
|
136
|
+
name: EBrowserTools.CLICK,
|
|
137
|
+
description: `Click an element on the current web page by its index number.
|
|
138
|
+
The element list shows clickable items like: [0]<button>Submit</button> [1]<a href="/home">Home</a>
|
|
139
|
+
Use the index number in brackets to click that element.
|
|
140
|
+
After clicking, you receive an updated element list showing the new page state.`,
|
|
141
|
+
schema: BrowserClickSchema,
|
|
142
|
+
}));
|
|
143
|
+
// browser_type
|
|
144
|
+
tools.push(tool(createToolFunction('type'), {
|
|
145
|
+
name: EBrowserTools.TYPE,
|
|
146
|
+
description: `Type text into an input element on the page.
|
|
147
|
+
Find the input element in the list by its index (e.g., [5]<input placeholder="Search">).
|
|
148
|
+
Set pressEnter: true to submit forms after typing.
|
|
149
|
+
After typing, you receive an updated element list.`,
|
|
150
|
+
schema: BrowserTypeSchema,
|
|
151
|
+
}));
|
|
152
|
+
// browser_navigate
|
|
153
|
+
tools.push(tool(createToolFunction('navigate'), {
|
|
154
|
+
name: EBrowserTools.NAVIGATE,
|
|
155
|
+
description: `Navigate to a URL. Always include the full URL with https://.
|
|
156
|
+
After navigation, you receive the new page's element list.`,
|
|
157
|
+
schema: BrowserNavigateSchema,
|
|
158
|
+
}));
|
|
159
|
+
// browser_scroll
|
|
160
|
+
tools.push(tool(createToolFunction('scroll'), {
|
|
161
|
+
name: EBrowserTools.SCROLL,
|
|
162
|
+
description: `Scroll the page to reveal more content.
|
|
163
|
+
Use 'down' to scroll down, 'up' to scroll up.
|
|
164
|
+
After scrolling, you receive an updated element list with newly visible elements.`,
|
|
165
|
+
schema: BrowserScrollSchema,
|
|
166
|
+
}));
|
|
167
|
+
// browser_extract
|
|
168
|
+
tools.push(tool(createToolFunction('extract'), {
|
|
169
|
+
name: EBrowserTools.EXTRACT,
|
|
170
|
+
description: `Extract content from the current page.
|
|
171
|
+
Returns page URL, title, and element list.`,
|
|
172
|
+
schema: BrowserExtractSchema,
|
|
173
|
+
}));
|
|
174
|
+
// browser_hover
|
|
175
|
+
tools.push(tool(createToolFunction('hover'), {
|
|
176
|
+
name: EBrowserTools.HOVER,
|
|
177
|
+
description: `Hover over an element to reveal tooltips, dropdowns, or other hover-triggered content.
|
|
178
|
+
After hovering, you receive an updated element list with any newly revealed elements.`,
|
|
179
|
+
schema: BrowserHoverSchema,
|
|
180
|
+
}));
|
|
181
|
+
// browser_wait
|
|
182
|
+
tools.push(tool(createToolFunction('wait'), {
|
|
183
|
+
name: EBrowserTools.WAIT,
|
|
184
|
+
description: `Wait for a specified duration for page content to load.
|
|
185
|
+
Use this after actions that trigger async content loading.
|
|
186
|
+
After waiting, you receive an updated element list.`,
|
|
187
|
+
schema: BrowserWaitSchema,
|
|
188
|
+
}));
|
|
189
|
+
// browser_back
|
|
190
|
+
tools.push(tool(createToolFunction('back'), {
|
|
191
|
+
name: EBrowserTools.BACK,
|
|
192
|
+
description: `Go back to the previous page in browser history.
|
|
193
|
+
After going back, you receive the previous page's element list.`,
|
|
194
|
+
schema: BrowserBackSchema,
|
|
195
|
+
}));
|
|
196
|
+
// browser_screenshot
|
|
197
|
+
tools.push(tool(createToolFunction('screenshot'), {
|
|
198
|
+
name: EBrowserTools.SCREENSHOT,
|
|
199
|
+
description: `Capture a screenshot of the current page.
|
|
200
|
+
Returns the page state with a note that screenshot was displayed to the user.
|
|
201
|
+
Use browser_get_page_state to get the element list for automation.`,
|
|
202
|
+
schema: BrowserScreenshotSchema,
|
|
203
|
+
}));
|
|
204
|
+
// browser_get_page_state
|
|
205
|
+
tools.push(tool(createToolFunction('get_page_state'), {
|
|
206
|
+
name: EBrowserTools.GET_PAGE_STATE,
|
|
207
|
+
description: `Get the current page state including URL, title, and all interactive elements.
|
|
208
|
+
Use this at the start of a task to see what elements are available.
|
|
209
|
+
Returns a text list of elements with their index numbers for interaction.`,
|
|
210
|
+
schema: BrowserGetPageStateSchema,
|
|
211
|
+
}));
|
|
212
|
+
return tools;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export { EBrowserTools, createBrowserTools, hasBrowserCapability };
|
|
216
|
+
//# sourceMappingURL=BrowserTools.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BrowserTools.mjs","sources":["../../../src/tools/BrowserTools.ts"],"sourcesContent":["import { z } from 'zod';\r\nimport { tool, DynamicStructuredTool } from '@langchain/core/tools';\r\nimport type * as t from '@/types';\r\n\r\n/**\r\n * Browser tool names - keep in sync with ranger-browser extension\r\n * These tools execute locally in the browser extension, NOT on the server\r\n */\r\nexport const EBrowserTools = {\r\n CLICK: 'browser_click',\r\n TYPE: 'browser_type',\r\n NAVIGATE: 'browser_navigate',\r\n SCROLL: 'browser_scroll',\r\n EXTRACT: 'browser_extract',\r\n HOVER: 'browser_hover',\r\n WAIT: 'browser_wait',\r\n BACK: 'browser_back',\r\n SCREENSHOT: 'browser_screenshot',\r\n GET_PAGE_STATE: 'browser_get_page_state',\r\n} as const;\r\n\r\nexport type BrowserToolName = typeof EBrowserTools[keyof typeof EBrowserTools];\r\n\r\n/**\r\n * Callback function type for waiting on browser action results\r\n * This allows the server (Ranger) to provide a callback that waits for the extension\r\n * to POST results back to the server before returning to the LLM.\r\n * \r\n * @param action - The browser action (click, type, navigate, etc.)\r\n * @param args - Arguments for the action\r\n * @param toolCallId - Unique ID for this tool call (from config.toolCall.id)\r\n * @returns Promise that resolves with the actual browser result (page state, etc.)\r\n */\r\nexport type BrowserToolCallback = (\r\n action: string,\r\n args: Record<string, unknown>,\r\n toolCallId: string\r\n) => Promise<BrowserActionResult>;\r\n\r\n/**\r\n * Result returned from browser action execution\r\n */\r\nexport interface BrowserActionResult {\r\n success: boolean;\r\n url?: string;\r\n title?: string;\r\n elementList?: string; // Text-based element list\r\n error?: string;\r\n screenshot?: string; // Base64 screenshot (if requested)\r\n}\r\n\r\n/**\r\n * Check if browser capability is available based on request headers or context\r\n * The browser extension sets these headers when connected:\r\n * - X-Ranger-Browser-Extension: true\r\n * - X-Ranger-Browser-Capable: true\r\n */\r\nexport function hasBrowserCapability(req?: { headers?: Record<string, string | string[] | undefined> }): boolean {\r\n if (!req?.headers) {\r\n return false;\r\n }\r\n \r\n const browserExtension = req.headers['x-ranger-browser-extension'];\r\n const browserCapable = req.headers['x-ranger-browser-capable'];\r\n \r\n return browserExtension === 'true' || browserCapable === 'true';\r\n}\r\n\r\n// Tool schemas\r\nconst BrowserClickSchema = z.object({\r\n index: z.number().describe('The index number [0], [1], etc. of the element to click from the page state element list'),\r\n});\r\n\r\nconst BrowserTypeSchema = z.object({\r\n index: z.number().describe('The index number of the input element to type into'),\r\n text: z.string().describe('The text to type into the element'),\r\n pressEnter: z.boolean().optional().describe('Whether to press Enter after typing (useful for search forms)'),\r\n});\r\n\r\nconst BrowserNavigateSchema = z.object({\r\n url: z.string().describe('The full URL to navigate to (must include https://)'),\r\n});\r\n\r\nconst BrowserScrollSchema = z.object({\r\n direction: z.enum(['up', 'down', 'left', 'right']).describe('Direction to scroll'),\r\n amount: z.number().optional().describe('Pixels to scroll (default: one viewport height)'),\r\n});\r\n\r\nconst BrowserExtractSchema = z.object({\r\n query: z.string().optional().describe('Optional: specific content to extract from the page'),\r\n});\r\n\r\nconst BrowserHoverSchema = z.object({\r\n index: z.number().describe('The index number of the element to hover over'),\r\n});\r\n\r\nconst BrowserWaitSchema = z.object({\r\n duration: z.number().optional().describe('Milliseconds to wait (default: 1000)'),\r\n});\r\n\r\nconst BrowserBackSchema = z.object({});\r\n\r\nconst BrowserScreenshotSchema = z.object({});\r\n\r\nconst BrowserGetPageStateSchema = z.object({});\r\n\r\n/**\r\n * Browser tool response interface\r\n * This is what the extension returns after executing the action\r\n */\r\nexport interface BrowserToolResponse {\r\n requiresBrowserExecution: true;\r\n action: string;\r\n args: Record<string, unknown>;\r\n toolCallId?: string; // Added to help extension correlate with callback\r\n}\r\n\r\n/**\r\n * Options for creating browser tools\r\n */\r\nexport interface CreateBrowserToolsOptions {\r\n /**\r\n * Optional callback that waits for browser action results.\r\n * When provided, tools will await this callback to get actual results from the extension.\r\n * When not provided, tools return markers immediately (for non-server contexts).\r\n */\r\n waitForResult?: BrowserToolCallback;\r\n}\r\n\r\n/**\r\n * Format browser action result for LLM consumption\r\n */\r\nfunction formatResultForLLM(result: BrowserActionResult, action: string): string {\r\n if (!result.success && result.error) {\r\n return `Browser action \"${action}\" failed: ${result.error}`;\r\n }\r\n\r\n const parts: string[] = [];\r\n \r\n if (result.url) {\r\n parts.push(`**Current URL:** ${result.url}`);\r\n }\r\n if (result.title) {\r\n parts.push(`**Page Title:** ${result.title}`);\r\n }\r\n if (result.elementList) {\r\n parts.push(`\\n**Interactive Elements:**\\n${result.elementList}`);\r\n }\r\n if (result.screenshot) {\r\n parts.push(`\\n[Screenshot captured and displayed to user]`);\r\n }\r\n \r\n if (parts.length === 0) {\r\n return `Browser action \"${action}\" completed successfully.`;\r\n }\r\n \r\n return parts.join('\\n');\r\n}\r\n\r\n/**\r\n * Create browser tools with optional callback for waiting on results\r\n * \r\n * When waitForResult callback is provided:\r\n * 1. Tool returns marker that triggers extension\r\n * 2. Tool then awaits callback to get actual results\r\n * 3. Returns real page state to LLM\r\n * \r\n * When no callback:\r\n * 1. Tool returns marker only (for non-server contexts)\r\n * \r\n * NOTE: These tools use TEXT-BASED element lists, NOT screenshots\r\n * Screenshots would be 100K+ tokens each - element lists are ~100 tokens\r\n */\r\nexport function createBrowserTools(options?: CreateBrowserToolsOptions): DynamicStructuredTool[] {\r\n const { waitForResult } = options || {};\r\n const tools: DynamicStructuredTool[] = [];\r\n\r\n /**\r\n * Helper to create tool function that optionally waits for results\r\n * The toolCallId is extracted from the RunnableConfig passed by LangChain\r\n */\r\n const createToolFunction = (action: string) => {\r\n return async (args: Record<string, unknown>, config?: { toolCall?: { id?: string } }): Promise<string> => {\r\n const toolCallId = config?.toolCall?.id || `tool_${Date.now()}_${Math.random().toString(36).slice(2)}`;\r\n \r\n // Create marker for extension\r\n const marker: BrowserToolResponse = {\r\n requiresBrowserExecution: true,\r\n action,\r\n args,\r\n toolCallId,\r\n };\r\n \r\n // If no callback, return marker immediately (extension handles via SSE interception)\r\n if (!waitForResult) {\r\n return JSON.stringify(marker);\r\n }\r\n \r\n // With callback: wait for actual results from extension\r\n // The marker is still returned initially via SSE, but we wait for the callback\r\n try {\r\n const result = await waitForResult(action, args, toolCallId);\r\n return formatResultForLLM(result, action);\r\n } catch (error) {\r\n const errorMessage = error instanceof Error ? error.message : String(error);\r\n return `Browser action \"${action}\" failed: ${errorMessage}`;\r\n }\r\n };\r\n };\r\n\r\n // browser_click\r\n tools.push(\r\n tool(\r\n createToolFunction('click'),\r\n {\r\n name: EBrowserTools.CLICK,\r\n description: `Click an element on the current web page by its index number.\r\nThe element list shows clickable items like: [0]<button>Submit</button> [1]<a href=\"/home\">Home</a>\r\nUse the index number in brackets to click that element.\r\nAfter clicking, you receive an updated element list showing the new page state.`,\r\n schema: BrowserClickSchema,\r\n }\r\n )\r\n );\r\n\r\n // browser_type\r\n tools.push(\r\n tool(\r\n createToolFunction('type'),\r\n {\r\n name: EBrowserTools.TYPE,\r\n description: `Type text into an input element on the page.\r\nFind the input element in the list by its index (e.g., [5]<input placeholder=\"Search\">).\r\nSet pressEnter: true to submit forms after typing.\r\nAfter typing, you receive an updated element list.`,\r\n schema: BrowserTypeSchema,\r\n }\r\n )\r\n );\r\n\r\n // browser_navigate\r\n tools.push(\r\n tool(\r\n createToolFunction('navigate'),\r\n {\r\n name: EBrowserTools.NAVIGATE,\r\n description: `Navigate to a URL. Always include the full URL with https://.\r\nAfter navigation, you receive the new page's element list.`,\r\n schema: BrowserNavigateSchema,\r\n }\r\n )\r\n );\r\n\r\n // browser_scroll\r\n tools.push(\r\n tool(\r\n createToolFunction('scroll'),\r\n {\r\n name: EBrowserTools.SCROLL,\r\n description: `Scroll the page to reveal more content.\r\nUse 'down' to scroll down, 'up' to scroll up.\r\nAfter scrolling, you receive an updated element list with newly visible elements.`,\r\n schema: BrowserScrollSchema,\r\n }\r\n )\r\n );\r\n\r\n // browser_extract\r\n tools.push(\r\n tool(\r\n createToolFunction('extract'),\r\n {\r\n name: EBrowserTools.EXTRACT,\r\n description: `Extract content from the current page.\r\nReturns page URL, title, and element list.`,\r\n schema: BrowserExtractSchema,\r\n }\r\n )\r\n );\r\n\r\n // browser_hover\r\n tools.push(\r\n tool(\r\n createToolFunction('hover'),\r\n {\r\n name: EBrowserTools.HOVER,\r\n description: `Hover over an element to reveal tooltips, dropdowns, or other hover-triggered content.\r\nAfter hovering, you receive an updated element list with any newly revealed elements.`,\r\n schema: BrowserHoverSchema,\r\n }\r\n )\r\n );\r\n\r\n // browser_wait\r\n tools.push(\r\n tool(\r\n createToolFunction('wait'),\r\n {\r\n name: EBrowserTools.WAIT,\r\n description: `Wait for a specified duration for page content to load.\r\nUse this after actions that trigger async content loading.\r\nAfter waiting, you receive an updated element list.`,\r\n schema: BrowserWaitSchema,\r\n }\r\n )\r\n );\r\n\r\n // browser_back\r\n tools.push(\r\n tool(\r\n createToolFunction('back'),\r\n {\r\n name: EBrowserTools.BACK,\r\n description: `Go back to the previous page in browser history.\r\nAfter going back, you receive the previous page's element list.`,\r\n schema: BrowserBackSchema,\r\n }\r\n )\r\n );\r\n\r\n // browser_screenshot\r\n tools.push(\r\n tool(\r\n createToolFunction('screenshot'),\r\n {\r\n name: EBrowserTools.SCREENSHOT,\r\n description: `Capture a screenshot of the current page.\r\nReturns the page state with a note that screenshot was displayed to the user.\r\nUse browser_get_page_state to get the element list for automation.`,\r\n schema: BrowserScreenshotSchema,\r\n }\r\n )\r\n );\r\n\r\n // browser_get_page_state\r\n tools.push(\r\n tool(\r\n createToolFunction('get_page_state'),\r\n {\r\n name: EBrowserTools.GET_PAGE_STATE,\r\n description: `Get the current page state including URL, title, and all interactive elements.\r\nUse this at the start of a task to see what elements are available.\r\nReturns a text list of elements with their index numbers for interaction.`,\r\n schema: BrowserGetPageStateSchema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C;QACE,IAAI,EAAE,aAAa,CAAC,UAAU;AAC9B,QAAA,WAAW,EAAE,CAAA;;AAE8C,kEAAA,CAAA;AAC3D,QAAA,MAAM,EAAE,uBAAuB;AAChC,KAAA,CACF,CACF;;IAGD,KAAK,CAAC,IAAI,CACR,IAAI,CACF,kBAAkB,CAAC,gBAAgB,CAAC,EACpC;QACE,IAAI,EAAE,aAAa,CAAC,cAAc;AAClC,QAAA,WAAW,EAAE,CAAA;;AAEqD,yEAAA,CAAA;AAClE,QAAA,MAAM,EAAE,yBAAyB;AAClC,KAAA,CACF,CACF;AAED,IAAA,OAAO,KAAK;AACd;;;;"}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ export * from './messages';
|
|
|
6
6
|
export * from './graphs';
|
|
7
7
|
export * from './tools/Calculator';
|
|
8
8
|
export * from './tools/CodeExecutor';
|
|
9
|
+
export * from './tools/BrowserTools';
|
|
9
10
|
export * from './tools/ProgrammaticToolCalling';
|
|
10
11
|
export * from './tools/ToolSearchRegex';
|
|
11
12
|
export * from './tools/handlers';
|
|
@@ -13,7 +14,4 @@ export * from './tools/search';
|
|
|
13
14
|
export * from './common';
|
|
14
15
|
export * from './utils';
|
|
15
16
|
export type * from './types';
|
|
16
|
-
export { CustomOpenAIClient
|
|
17
|
-
export { CustomAnthropic } from './llm/anthropic';
|
|
18
|
-
export { CustomChatBedrockConverse } from './llm/bedrock';
|
|
19
|
-
export { llmProviders, getChatModelClass, manualToolStreamProviders } from './llm/providers';
|
|
17
|
+
export { CustomOpenAIClient } from './llm/openai';
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { DynamicStructuredTool } from '@langchain/core/tools';
|
|
2
|
+
/**
|
|
3
|
+
* Browser tool names - keep in sync with ranger-browser extension
|
|
4
|
+
* These tools execute locally in the browser extension, NOT on the server
|
|
5
|
+
*/
|
|
6
|
+
export declare const EBrowserTools: {
|
|
7
|
+
readonly CLICK: "browser_click";
|
|
8
|
+
readonly TYPE: "browser_type";
|
|
9
|
+
readonly NAVIGATE: "browser_navigate";
|
|
10
|
+
readonly SCROLL: "browser_scroll";
|
|
11
|
+
readonly EXTRACT: "browser_extract";
|
|
12
|
+
readonly HOVER: "browser_hover";
|
|
13
|
+
readonly WAIT: "browser_wait";
|
|
14
|
+
readonly BACK: "browser_back";
|
|
15
|
+
readonly SCREENSHOT: "browser_screenshot";
|
|
16
|
+
readonly GET_PAGE_STATE: "browser_get_page_state";
|
|
17
|
+
};
|
|
18
|
+
export type BrowserToolName = typeof EBrowserTools[keyof typeof EBrowserTools];
|
|
19
|
+
/**
|
|
20
|
+
* Callback function type for waiting on browser action results
|
|
21
|
+
* This allows the server (Ranger) to provide a callback that waits for the extension
|
|
22
|
+
* to POST results back to the server before returning to the LLM.
|
|
23
|
+
*
|
|
24
|
+
* @param action - The browser action (click, type, navigate, etc.)
|
|
25
|
+
* @param args - Arguments for the action
|
|
26
|
+
* @param toolCallId - Unique ID for this tool call (from config.toolCall.id)
|
|
27
|
+
* @returns Promise that resolves with the actual browser result (page state, etc.)
|
|
28
|
+
*/
|
|
29
|
+
export type BrowserToolCallback = (action: string, args: Record<string, unknown>, toolCallId: string) => Promise<BrowserActionResult>;
|
|
30
|
+
/**
|
|
31
|
+
* Result returned from browser action execution
|
|
32
|
+
*/
|
|
33
|
+
export interface BrowserActionResult {
|
|
34
|
+
success: boolean;
|
|
35
|
+
url?: string;
|
|
36
|
+
title?: string;
|
|
37
|
+
elementList?: string;
|
|
38
|
+
error?: string;
|
|
39
|
+
screenshot?: string;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Check if browser capability is available based on request headers or context
|
|
43
|
+
* The browser extension sets these headers when connected:
|
|
44
|
+
* - X-Ranger-Browser-Extension: true
|
|
45
|
+
* - X-Ranger-Browser-Capable: true
|
|
46
|
+
*/
|
|
47
|
+
export declare function hasBrowserCapability(req?: {
|
|
48
|
+
headers?: Record<string, string | string[] | undefined>;
|
|
49
|
+
}): boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Browser tool response interface
|
|
52
|
+
* This is what the extension returns after executing the action
|
|
53
|
+
*/
|
|
54
|
+
export interface BrowserToolResponse {
|
|
55
|
+
requiresBrowserExecution: true;
|
|
56
|
+
action: string;
|
|
57
|
+
args: Record<string, unknown>;
|
|
58
|
+
toolCallId?: string;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Options for creating browser tools
|
|
62
|
+
*/
|
|
63
|
+
export interface CreateBrowserToolsOptions {
|
|
64
|
+
/**
|
|
65
|
+
* Optional callback that waits for browser action results.
|
|
66
|
+
* When provided, tools will await this callback to get actual results from the extension.
|
|
67
|
+
* When not provided, tools return markers immediately (for non-server contexts).
|
|
68
|
+
*/
|
|
69
|
+
waitForResult?: BrowserToolCallback;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Create browser tools with optional callback for waiting on results
|
|
73
|
+
*
|
|
74
|
+
* When waitForResult callback is provided:
|
|
75
|
+
* 1. Tool returns marker that triggers extension
|
|
76
|
+
* 2. Tool then awaits callback to get actual results
|
|
77
|
+
* 3. Returns real page state to LLM
|
|
78
|
+
*
|
|
79
|
+
* When no callback:
|
|
80
|
+
* 1. Tool returns marker only (for non-server contexts)
|
|
81
|
+
*
|
|
82
|
+
* NOTE: These tools use TEXT-BASED element lists, NOT screenshots
|
|
83
|
+
* Screenshots would be 100K+ tokens each - element lists are ~100 tokens
|
|
84
|
+
*/
|
|
85
|
+
export declare function createBrowserTools(options?: CreateBrowserToolsOptions): DynamicStructuredTool[];
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -11,6 +11,7 @@ export * from './graphs';
|
|
|
11
11
|
/* Tools */
|
|
12
12
|
export * from './tools/Calculator';
|
|
13
13
|
export * from './tools/CodeExecutor';
|
|
14
|
+
export * from './tools/BrowserTools';
|
|
14
15
|
export * from './tools/ProgrammaticToolCalling';
|
|
15
16
|
export * from './tools/ToolSearchRegex';
|
|
16
17
|
export * from './tools/handlers';
|
|
@@ -23,8 +24,5 @@ export * from './utils';
|
|
|
23
24
|
/* Types */
|
|
24
25
|
export type * from './types';
|
|
25
26
|
|
|
26
|
-
/* LLM
|
|
27
|
-
export { CustomOpenAIClient
|
|
28
|
-
export { CustomAnthropic } from './llm/anthropic';
|
|
29
|
-
export { CustomChatBedrockConverse } from './llm/bedrock';
|
|
30
|
-
export { llmProviders, getChatModelClass, manualToolStreamProviders } from './llm/providers';
|
|
27
|
+
/* LLM */
|
|
28
|
+
export { CustomOpenAIClient } from './llm/openai';
|
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { tool, DynamicStructuredTool } from '@langchain/core/tools';
|
|
3
|
+
import type * as t from '@/types';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Browser tool names - keep in sync with ranger-browser extension
|
|
7
|
+
* These tools execute locally in the browser extension, NOT on the server
|
|
8
|
+
*/
|
|
9
|
+
export const EBrowserTools = {
|
|
10
|
+
CLICK: 'browser_click',
|
|
11
|
+
TYPE: 'browser_type',
|
|
12
|
+
NAVIGATE: 'browser_navigate',
|
|
13
|
+
SCROLL: 'browser_scroll',
|
|
14
|
+
EXTRACT: 'browser_extract',
|
|
15
|
+
HOVER: 'browser_hover',
|
|
16
|
+
WAIT: 'browser_wait',
|
|
17
|
+
BACK: 'browser_back',
|
|
18
|
+
SCREENSHOT: 'browser_screenshot',
|
|
19
|
+
GET_PAGE_STATE: 'browser_get_page_state',
|
|
20
|
+
} as const;
|
|
21
|
+
|
|
22
|
+
export type BrowserToolName = typeof EBrowserTools[keyof typeof EBrowserTools];
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Callback function type for waiting on browser action results
|
|
26
|
+
* This allows the server (Ranger) to provide a callback that waits for the extension
|
|
27
|
+
* to POST results back to the server before returning to the LLM.
|
|
28
|
+
*
|
|
29
|
+
* @param action - The browser action (click, type, navigate, etc.)
|
|
30
|
+
* @param args - Arguments for the action
|
|
31
|
+
* @param toolCallId - Unique ID for this tool call (from config.toolCall.id)
|
|
32
|
+
* @returns Promise that resolves with the actual browser result (page state, etc.)
|
|
33
|
+
*/
|
|
34
|
+
export type BrowserToolCallback = (
|
|
35
|
+
action: string,
|
|
36
|
+
args: Record<string, unknown>,
|
|
37
|
+
toolCallId: string
|
|
38
|
+
) => Promise<BrowserActionResult>;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Result returned from browser action execution
|
|
42
|
+
*/
|
|
43
|
+
export interface BrowserActionResult {
|
|
44
|
+
success: boolean;
|
|
45
|
+
url?: string;
|
|
46
|
+
title?: string;
|
|
47
|
+
elementList?: string; // Text-based element list
|
|
48
|
+
error?: string;
|
|
49
|
+
screenshot?: string; // Base64 screenshot (if requested)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Check if browser capability is available based on request headers or context
|
|
54
|
+
* The browser extension sets these headers when connected:
|
|
55
|
+
* - X-Ranger-Browser-Extension: true
|
|
56
|
+
* - X-Ranger-Browser-Capable: true
|
|
57
|
+
*/
|
|
58
|
+
export function hasBrowserCapability(req?: { headers?: Record<string, string | string[] | undefined> }): boolean {
|
|
59
|
+
if (!req?.headers) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const browserExtension = req.headers['x-ranger-browser-extension'];
|
|
64
|
+
const browserCapable = req.headers['x-ranger-browser-capable'];
|
|
65
|
+
|
|
66
|
+
return browserExtension === 'true' || browserCapable === 'true';
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Tool schemas
|
|
70
|
+
const BrowserClickSchema = z.object({
|
|
71
|
+
index: z.number().describe('The index number [0], [1], etc. of the element to click from the page state element list'),
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const BrowserTypeSchema = z.object({
|
|
75
|
+
index: z.number().describe('The index number of the input element to type into'),
|
|
76
|
+
text: z.string().describe('The text to type into the element'),
|
|
77
|
+
pressEnter: z.boolean().optional().describe('Whether to press Enter after typing (useful for search forms)'),
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const BrowserNavigateSchema = z.object({
|
|
81
|
+
url: z.string().describe('The full URL to navigate to (must include https://)'),
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
const BrowserScrollSchema = z.object({
|
|
85
|
+
direction: z.enum(['up', 'down', 'left', 'right']).describe('Direction to scroll'),
|
|
86
|
+
amount: z.number().optional().describe('Pixels to scroll (default: one viewport height)'),
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
const BrowserExtractSchema = z.object({
|
|
90
|
+
query: z.string().optional().describe('Optional: specific content to extract from the page'),
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
const BrowserHoverSchema = z.object({
|
|
94
|
+
index: z.number().describe('The index number of the element to hover over'),
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
const BrowserWaitSchema = z.object({
|
|
98
|
+
duration: z.number().optional().describe('Milliseconds to wait (default: 1000)'),
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
const BrowserBackSchema = z.object({});
|
|
102
|
+
|
|
103
|
+
const BrowserScreenshotSchema = z.object({});
|
|
104
|
+
|
|
105
|
+
const BrowserGetPageStateSchema = z.object({});
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Browser tool response interface
|
|
109
|
+
* This is what the extension returns after executing the action
|
|
110
|
+
*/
|
|
111
|
+
export interface BrowserToolResponse {
|
|
112
|
+
requiresBrowserExecution: true;
|
|
113
|
+
action: string;
|
|
114
|
+
args: Record<string, unknown>;
|
|
115
|
+
toolCallId?: string; // Added to help extension correlate with callback
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Options for creating browser tools
|
|
120
|
+
*/
|
|
121
|
+
export interface CreateBrowserToolsOptions {
|
|
122
|
+
/**
|
|
123
|
+
* Optional callback that waits for browser action results.
|
|
124
|
+
* When provided, tools will await this callback to get actual results from the extension.
|
|
125
|
+
* When not provided, tools return markers immediately (for non-server contexts).
|
|
126
|
+
*/
|
|
127
|
+
waitForResult?: BrowserToolCallback;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Format browser action result for LLM consumption
|
|
132
|
+
*/
|
|
133
|
+
function formatResultForLLM(result: BrowserActionResult, action: string): string {
|
|
134
|
+
if (!result.success && result.error) {
|
|
135
|
+
return `Browser action "${action}" failed: ${result.error}`;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const parts: string[] = [];
|
|
139
|
+
|
|
140
|
+
if (result.url) {
|
|
141
|
+
parts.push(`**Current URL:** ${result.url}`);
|
|
142
|
+
}
|
|
143
|
+
if (result.title) {
|
|
144
|
+
parts.push(`**Page Title:** ${result.title}`);
|
|
145
|
+
}
|
|
146
|
+
if (result.elementList) {
|
|
147
|
+
parts.push(`\n**Interactive Elements:**\n${result.elementList}`);
|
|
148
|
+
}
|
|
149
|
+
if (result.screenshot) {
|
|
150
|
+
parts.push(`\n[Screenshot captured and displayed to user]`);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (parts.length === 0) {
|
|
154
|
+
return `Browser action "${action}" completed successfully.`;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return parts.join('\n');
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Create browser tools with optional callback for waiting on results
|
|
162
|
+
*
|
|
163
|
+
* When waitForResult callback is provided:
|
|
164
|
+
* 1. Tool returns marker that triggers extension
|
|
165
|
+
* 2. Tool then awaits callback to get actual results
|
|
166
|
+
* 3. Returns real page state to LLM
|
|
167
|
+
*
|
|
168
|
+
* When no callback:
|
|
169
|
+
* 1. Tool returns marker only (for non-server contexts)
|
|
170
|
+
*
|
|
171
|
+
* NOTE: These tools use TEXT-BASED element lists, NOT screenshots
|
|
172
|
+
* Screenshots would be 100K+ tokens each - element lists are ~100 tokens
|
|
173
|
+
*/
|
|
174
|
+
export function createBrowserTools(options?: CreateBrowserToolsOptions): DynamicStructuredTool[] {
|
|
175
|
+
const { waitForResult } = options || {};
|
|
176
|
+
const tools: DynamicStructuredTool[] = [];
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Helper to create tool function that optionally waits for results
|
|
180
|
+
* The toolCallId is extracted from the RunnableConfig passed by LangChain
|
|
181
|
+
*/
|
|
182
|
+
const createToolFunction = (action: string) => {
|
|
183
|
+
return async (args: Record<string, unknown>, config?: { toolCall?: { id?: string } }): Promise<string> => {
|
|
184
|
+
const toolCallId = config?.toolCall?.id || `tool_${Date.now()}_${Math.random().toString(36).slice(2)}`;
|
|
185
|
+
|
|
186
|
+
// Create marker for extension
|
|
187
|
+
const marker: BrowserToolResponse = {
|
|
188
|
+
requiresBrowserExecution: true,
|
|
189
|
+
action,
|
|
190
|
+
args,
|
|
191
|
+
toolCallId,
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
// If no callback, return marker immediately (extension handles via SSE interception)
|
|
195
|
+
if (!waitForResult) {
|
|
196
|
+
return JSON.stringify(marker);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// With callback: wait for actual results from extension
|
|
200
|
+
// The marker is still returned initially via SSE, but we wait for the callback
|
|
201
|
+
try {
|
|
202
|
+
const result = await waitForResult(action, args, toolCallId);
|
|
203
|
+
return formatResultForLLM(result, action);
|
|
204
|
+
} catch (error) {
|
|
205
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
206
|
+
return `Browser action "${action}" failed: ${errorMessage}`;
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
// browser_click
|
|
212
|
+
tools.push(
|
|
213
|
+
tool(
|
|
214
|
+
createToolFunction('click'),
|
|
215
|
+
{
|
|
216
|
+
name: EBrowserTools.CLICK,
|
|
217
|
+
description: `Click an element on the current web page by its index number.
|
|
218
|
+
The element list shows clickable items like: [0]<button>Submit</button> [1]<a href="/home">Home</a>
|
|
219
|
+
Use the index number in brackets to click that element.
|
|
220
|
+
After clicking, you receive an updated element list showing the new page state.`,
|
|
221
|
+
schema: BrowserClickSchema,
|
|
222
|
+
}
|
|
223
|
+
)
|
|
224
|
+
);
|
|
225
|
+
|
|
226
|
+
// browser_type
|
|
227
|
+
tools.push(
|
|
228
|
+
tool(
|
|
229
|
+
createToolFunction('type'),
|
|
230
|
+
{
|
|
231
|
+
name: EBrowserTools.TYPE,
|
|
232
|
+
description: `Type text into an input element on the page.
|
|
233
|
+
Find the input element in the list by its index (e.g., [5]<input placeholder="Search">).
|
|
234
|
+
Set pressEnter: true to submit forms after typing.
|
|
235
|
+
After typing, you receive an updated element list.`,
|
|
236
|
+
schema: BrowserTypeSchema,
|
|
237
|
+
}
|
|
238
|
+
)
|
|
239
|
+
);
|
|
240
|
+
|
|
241
|
+
// browser_navigate
|
|
242
|
+
tools.push(
|
|
243
|
+
tool(
|
|
244
|
+
createToolFunction('navigate'),
|
|
245
|
+
{
|
|
246
|
+
name: EBrowserTools.NAVIGATE,
|
|
247
|
+
description: `Navigate to a URL. Always include the full URL with https://.
|
|
248
|
+
After navigation, you receive the new page's element list.`,
|
|
249
|
+
schema: BrowserNavigateSchema,
|
|
250
|
+
}
|
|
251
|
+
)
|
|
252
|
+
);
|
|
253
|
+
|
|
254
|
+
// browser_scroll
|
|
255
|
+
tools.push(
|
|
256
|
+
tool(
|
|
257
|
+
createToolFunction('scroll'),
|
|
258
|
+
{
|
|
259
|
+
name: EBrowserTools.SCROLL,
|
|
260
|
+
description: `Scroll the page to reveal more content.
|
|
261
|
+
Use 'down' to scroll down, 'up' to scroll up.
|
|
262
|
+
After scrolling, you receive an updated element list with newly visible elements.`,
|
|
263
|
+
schema: BrowserScrollSchema,
|
|
264
|
+
}
|
|
265
|
+
)
|
|
266
|
+
);
|
|
267
|
+
|
|
268
|
+
// browser_extract
|
|
269
|
+
tools.push(
|
|
270
|
+
tool(
|
|
271
|
+
createToolFunction('extract'),
|
|
272
|
+
{
|
|
273
|
+
name: EBrowserTools.EXTRACT,
|
|
274
|
+
description: `Extract content from the current page.
|
|
275
|
+
Returns page URL, title, and element list.`,
|
|
276
|
+
schema: BrowserExtractSchema,
|
|
277
|
+
}
|
|
278
|
+
)
|
|
279
|
+
);
|
|
280
|
+
|
|
281
|
+
// browser_hover
|
|
282
|
+
tools.push(
|
|
283
|
+
tool(
|
|
284
|
+
createToolFunction('hover'),
|
|
285
|
+
{
|
|
286
|
+
name: EBrowserTools.HOVER,
|
|
287
|
+
description: `Hover over an element to reveal tooltips, dropdowns, or other hover-triggered content.
|
|
288
|
+
After hovering, you receive an updated element list with any newly revealed elements.`,
|
|
289
|
+
schema: BrowserHoverSchema,
|
|
290
|
+
}
|
|
291
|
+
)
|
|
292
|
+
);
|
|
293
|
+
|
|
294
|
+
// browser_wait
|
|
295
|
+
tools.push(
|
|
296
|
+
tool(
|
|
297
|
+
createToolFunction('wait'),
|
|
298
|
+
{
|
|
299
|
+
name: EBrowserTools.WAIT,
|
|
300
|
+
description: `Wait for a specified duration for page content to load.
|
|
301
|
+
Use this after actions that trigger async content loading.
|
|
302
|
+
After waiting, you receive an updated element list.`,
|
|
303
|
+
schema: BrowserWaitSchema,
|
|
304
|
+
}
|
|
305
|
+
)
|
|
306
|
+
);
|
|
307
|
+
|
|
308
|
+
// browser_back
|
|
309
|
+
tools.push(
|
|
310
|
+
tool(
|
|
311
|
+
createToolFunction('back'),
|
|
312
|
+
{
|
|
313
|
+
name: EBrowserTools.BACK,
|
|
314
|
+
description: `Go back to the previous page in browser history.
|
|
315
|
+
After going back, you receive the previous page's element list.`,
|
|
316
|
+
schema: BrowserBackSchema,
|
|
317
|
+
}
|
|
318
|
+
)
|
|
319
|
+
);
|
|
320
|
+
|
|
321
|
+
// browser_screenshot
|
|
322
|
+
tools.push(
|
|
323
|
+
tool(
|
|
324
|
+
createToolFunction('screenshot'),
|
|
325
|
+
{
|
|
326
|
+
name: EBrowserTools.SCREENSHOT,
|
|
327
|
+
description: `Capture a screenshot of the current page.
|
|
328
|
+
Returns the page state with a note that screenshot was displayed to the user.
|
|
329
|
+
Use browser_get_page_state to get the element list for automation.`,
|
|
330
|
+
schema: BrowserScreenshotSchema,
|
|
331
|
+
}
|
|
332
|
+
)
|
|
333
|
+
);
|
|
334
|
+
|
|
335
|
+
// browser_get_page_state
|
|
336
|
+
tools.push(
|
|
337
|
+
tool(
|
|
338
|
+
createToolFunction('get_page_state'),
|
|
339
|
+
{
|
|
340
|
+
name: EBrowserTools.GET_PAGE_STATE,
|
|
341
|
+
description: `Get the current page state including URL, title, and all interactive elements.
|
|
342
|
+
Use this at the start of a task to see what elements are available.
|
|
343
|
+
Returns a text list of elements with their index numbers for interaction.`,
|
|
344
|
+
schema: BrowserGetPageStateSchema,
|
|
345
|
+
}
|
|
346
|
+
)
|
|
347
|
+
);
|
|
348
|
+
|
|
349
|
+
return tools;
|
|
350
|
+
}
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, jest } from '@jest/globals';
|
|
2
|
+
import {
|
|
3
|
+
createBrowserTools,
|
|
4
|
+
hasBrowserCapability,
|
|
5
|
+
EBrowserTools,
|
|
6
|
+
type BrowserToolResponse,
|
|
7
|
+
} from '../BrowserTools';
|
|
8
|
+
|
|
9
|
+
describe('BrowserTools', () => {
|
|
10
|
+
describe('EBrowserTools enum', () => {
|
|
11
|
+
it('should have all expected browser tool names', () => {
|
|
12
|
+
expect(EBrowserTools.CLICK).toBe('browser_click');
|
|
13
|
+
expect(EBrowserTools.TYPE).toBe('browser_type');
|
|
14
|
+
expect(EBrowserTools.NAVIGATE).toBe('browser_navigate');
|
|
15
|
+
expect(EBrowserTools.SCROLL).toBe('browser_scroll');
|
|
16
|
+
expect(EBrowserTools.EXTRACT).toBe('browser_extract');
|
|
17
|
+
expect(EBrowserTools.HOVER).toBe('browser_hover');
|
|
18
|
+
expect(EBrowserTools.WAIT).toBe('browser_wait');
|
|
19
|
+
expect(EBrowserTools.BACK).toBe('browser_back');
|
|
20
|
+
expect(EBrowserTools.SCREENSHOT).toBe('browser_screenshot');
|
|
21
|
+
expect(EBrowserTools.GET_PAGE_STATE).toBe('browser_get_page_state');
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('should have exactly 10 browser tools', () => {
|
|
25
|
+
expect(Object.keys(EBrowserTools).length).toBe(10);
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe('hasBrowserCapability', () => {
|
|
30
|
+
it('should return false when no request is provided', () => {
|
|
31
|
+
expect(hasBrowserCapability()).toBe(false);
|
|
32
|
+
expect(hasBrowserCapability(undefined)).toBe(false);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('should return false when request has no headers', () => {
|
|
36
|
+
expect(hasBrowserCapability({})).toBe(false);
|
|
37
|
+
expect(hasBrowserCapability({ headers: undefined })).toBe(false);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('should return true when X-Ranger-Browser-Extension header is true', () => {
|
|
41
|
+
const req = {
|
|
42
|
+
headers: {
|
|
43
|
+
'x-ranger-browser-extension': 'true',
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
expect(hasBrowserCapability(req)).toBe(true);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('should return true when X-Ranger-Browser-Capable header is true', () => {
|
|
50
|
+
const req = {
|
|
51
|
+
headers: {
|
|
52
|
+
'x-ranger-browser-capable': 'true',
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
expect(hasBrowserCapability(req)).toBe(true);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('should return false when browser headers are not true', () => {
|
|
59
|
+
const req = {
|
|
60
|
+
headers: {
|
|
61
|
+
'x-ranger-browser-extension': 'false',
|
|
62
|
+
'x-ranger-browser-capable': 'false',
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
expect(hasBrowserCapability(req)).toBe(false);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('should return false when browser headers are missing', () => {
|
|
69
|
+
const req = {
|
|
70
|
+
headers: {
|
|
71
|
+
'content-type': 'application/json',
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
expect(hasBrowserCapability(req)).toBe(false);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
describe('createBrowserTools', () => {
|
|
79
|
+
let tools: ReturnType<typeof createBrowserTools>;
|
|
80
|
+
|
|
81
|
+
beforeEach(() => {
|
|
82
|
+
tools = createBrowserTools();
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('should create exactly 10 browser tools', () => {
|
|
86
|
+
expect(tools.length).toBe(10);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('should create tools with correct names', () => {
|
|
90
|
+
const toolNames = tools.map(t => t.name);
|
|
91
|
+
expect(toolNames).toContain('browser_click');
|
|
92
|
+
expect(toolNames).toContain('browser_type');
|
|
93
|
+
expect(toolNames).toContain('browser_navigate');
|
|
94
|
+
expect(toolNames).toContain('browser_scroll');
|
|
95
|
+
expect(toolNames).toContain('browser_extract');
|
|
96
|
+
expect(toolNames).toContain('browser_hover');
|
|
97
|
+
expect(toolNames).toContain('browser_wait');
|
|
98
|
+
expect(toolNames).toContain('browser_back');
|
|
99
|
+
expect(toolNames).toContain('browser_screenshot');
|
|
100
|
+
expect(toolNames).toContain('browser_get_page_state');
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('should have descriptions for all tools', () => {
|
|
104
|
+
for (const tool of tools) {
|
|
105
|
+
expect(tool.description).toBeDefined();
|
|
106
|
+
expect(tool.description.length).toBeGreaterThan(0);
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
describe('browser_click tool', () => {
|
|
111
|
+
it('should return requiresBrowserExecution marker', async () => {
|
|
112
|
+
const clickTool = tools.find(t => t.name === 'browser_click');
|
|
113
|
+
expect(clickTool).toBeDefined();
|
|
114
|
+
|
|
115
|
+
const result = await clickTool!.invoke({ index: 5 });
|
|
116
|
+
const parsed: BrowserToolResponse = JSON.parse(result);
|
|
117
|
+
|
|
118
|
+
expect(parsed.requiresBrowserExecution).toBe(true);
|
|
119
|
+
expect(parsed.action).toBe('click');
|
|
120
|
+
expect(parsed.args).toEqual({ index: 5 });
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
describe('browser_type tool', () => {
|
|
125
|
+
it('should return requiresBrowserExecution marker with all args', async () => {
|
|
126
|
+
const typeTool = tools.find(t => t.name === 'browser_type');
|
|
127
|
+
expect(typeTool).toBeDefined();
|
|
128
|
+
|
|
129
|
+
const result = await typeTool!.invoke({
|
|
130
|
+
index: 3,
|
|
131
|
+
text: 'Hello World',
|
|
132
|
+
pressEnter: true
|
|
133
|
+
});
|
|
134
|
+
const parsed: BrowserToolResponse = JSON.parse(result);
|
|
135
|
+
|
|
136
|
+
expect(parsed.requiresBrowserExecution).toBe(true);
|
|
137
|
+
expect(parsed.action).toBe('type');
|
|
138
|
+
expect(parsed.args.index).toBe(3);
|
|
139
|
+
expect(parsed.args.text).toBe('Hello World');
|
|
140
|
+
expect(parsed.args.pressEnter).toBe(true);
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
describe('browser_navigate tool', () => {
|
|
145
|
+
it('should return requiresBrowserExecution marker with URL', async () => {
|
|
146
|
+
const navigateTool = tools.find(t => t.name === 'browser_navigate');
|
|
147
|
+
expect(navigateTool).toBeDefined();
|
|
148
|
+
|
|
149
|
+
const result = await navigateTool!.invoke({ url: 'https://example.com' });
|
|
150
|
+
const parsed: BrowserToolResponse = JSON.parse(result);
|
|
151
|
+
|
|
152
|
+
expect(parsed.requiresBrowserExecution).toBe(true);
|
|
153
|
+
expect(parsed.action).toBe('navigate');
|
|
154
|
+
expect(parsed.args.url).toBe('https://example.com');
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
describe('browser_scroll tool', () => {
|
|
159
|
+
it('should return requiresBrowserExecution marker with direction', async () => {
|
|
160
|
+
const scrollTool = tools.find(t => t.name === 'browser_scroll');
|
|
161
|
+
expect(scrollTool).toBeDefined();
|
|
162
|
+
|
|
163
|
+
const result = await scrollTool!.invoke({
|
|
164
|
+
direction: 'down',
|
|
165
|
+
amount: 500
|
|
166
|
+
});
|
|
167
|
+
const parsed: BrowserToolResponse = JSON.parse(result);
|
|
168
|
+
|
|
169
|
+
expect(parsed.requiresBrowserExecution).toBe(true);
|
|
170
|
+
expect(parsed.action).toBe('scroll');
|
|
171
|
+
expect(parsed.args.direction).toBe('down');
|
|
172
|
+
expect(parsed.args.amount).toBe(500);
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
describe('browser_hover tool', () => {
|
|
177
|
+
it('should return requiresBrowserExecution marker with index', async () => {
|
|
178
|
+
const hoverTool = tools.find(t => t.name === 'browser_hover');
|
|
179
|
+
expect(hoverTool).toBeDefined();
|
|
180
|
+
|
|
181
|
+
const result = await hoverTool!.invoke({ index: 2 });
|
|
182
|
+
const parsed: BrowserToolResponse = JSON.parse(result);
|
|
183
|
+
|
|
184
|
+
expect(parsed.requiresBrowserExecution).toBe(true);
|
|
185
|
+
expect(parsed.action).toBe('hover');
|
|
186
|
+
expect(parsed.args.index).toBe(2);
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
describe('browser_wait tool', () => {
|
|
191
|
+
it('should return requiresBrowserExecution marker with duration', async () => {
|
|
192
|
+
const waitTool = tools.find(t => t.name === 'browser_wait');
|
|
193
|
+
expect(waitTool).toBeDefined();
|
|
194
|
+
|
|
195
|
+
const result = await waitTool!.invoke({ duration: 2000 });
|
|
196
|
+
const parsed: BrowserToolResponse = JSON.parse(result);
|
|
197
|
+
|
|
198
|
+
expect(parsed.requiresBrowserExecution).toBe(true);
|
|
199
|
+
expect(parsed.action).toBe('wait');
|
|
200
|
+
expect(parsed.args.duration).toBe(2000);
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
describe('browser_back tool', () => {
|
|
205
|
+
it('should return requiresBrowserExecution marker', async () => {
|
|
206
|
+
const backTool = tools.find(t => t.name === 'browser_back');
|
|
207
|
+
expect(backTool).toBeDefined();
|
|
208
|
+
|
|
209
|
+
const result = await backTool!.invoke({});
|
|
210
|
+
const parsed: BrowserToolResponse = JSON.parse(result);
|
|
211
|
+
|
|
212
|
+
expect(parsed.requiresBrowserExecution).toBe(true);
|
|
213
|
+
expect(parsed.action).toBe('back');
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
describe('browser_screenshot tool', () => {
|
|
218
|
+
it('should return requiresBrowserExecution marker', async () => {
|
|
219
|
+
const screenshotTool = tools.find(t => t.name === 'browser_screenshot');
|
|
220
|
+
expect(screenshotTool).toBeDefined();
|
|
221
|
+
|
|
222
|
+
const result = await screenshotTool!.invoke({});
|
|
223
|
+
const parsed: BrowserToolResponse = JSON.parse(result);
|
|
224
|
+
|
|
225
|
+
expect(parsed.requiresBrowserExecution).toBe(true);
|
|
226
|
+
expect(parsed.action).toBe('screenshot');
|
|
227
|
+
});
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
describe('browser_get_page_state tool', () => {
|
|
231
|
+
it('should return requiresBrowserExecution marker', async () => {
|
|
232
|
+
const pageStateTool = tools.find(t => t.name === 'browser_get_page_state');
|
|
233
|
+
expect(pageStateTool).toBeDefined();
|
|
234
|
+
|
|
235
|
+
const result = await pageStateTool!.invoke({});
|
|
236
|
+
const parsed: BrowserToolResponse = JSON.parse(result);
|
|
237
|
+
|
|
238
|
+
expect(parsed.requiresBrowserExecution).toBe(true);
|
|
239
|
+
expect(parsed.action).toBe('get_page_state');
|
|
240
|
+
});
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
describe('browser_extract tool', () => {
|
|
244
|
+
it('should return requiresBrowserExecution marker', async () => {
|
|
245
|
+
const extractTool = tools.find(t => t.name === 'browser_extract');
|
|
246
|
+
expect(extractTool).toBeDefined();
|
|
247
|
+
|
|
248
|
+
const result = await extractTool!.invoke({ query: 'main content' });
|
|
249
|
+
const parsed: BrowserToolResponse = JSON.parse(result);
|
|
250
|
+
|
|
251
|
+
expect(parsed.requiresBrowserExecution).toBe(true);
|
|
252
|
+
expect(parsed.action).toBe('extract');
|
|
253
|
+
expect(parsed.args.query).toBe('main content');
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
});
|