illuma-agents 1.0.33 → 1.0.35

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.
@@ -37,12 +37,12 @@ function hasBrowserCapability(req) {
37
37
  const BrowserClickSchema = zod.z.object({
38
38
  index: zod.z
39
39
  .number()
40
- .describe('The index number [0], [1], etc. of the element to click from the page state element list'),
40
+ .describe('The [index] of the element to click. Use fieldLabel attribute to identify the correct element when multiple similar elements exist (e.g., fieldLabel="To" vs fieldLabel="Cc" for email fields).'),
41
41
  });
42
42
  const BrowserTypeSchema = zod.z.object({
43
43
  index: zod.z
44
44
  .number()
45
- .describe('The index number of the input element to type into'),
45
+ .describe('The [index] of the input element to type into. IMPORTANT: Check fieldLabel attribute to ensure you are typing into the correct field (e.g., fieldLabel="To" for recipient, fieldLabel="Subject" for subject line).'),
46
46
  text: zod.z.string().describe('The text to type into the element'),
47
47
  pressEnter: zod.z
48
48
  .boolean()
@@ -96,7 +96,8 @@ function formatResultForLLM(result, action) {
96
96
  parts.push(`**Page Title:** ${result.title}`);
97
97
  }
98
98
  if (result.elementList != null && result.elementList !== '') {
99
- parts.push(`\n**Interactive Elements:**\n${result.elementList}`);
99
+ // Add hint about fieldLabel for form interactions
100
+ parts.push(`\n**Interactive Elements** (use fieldLabel to identify form fields):\n${result.elementList}`);
100
101
  }
101
102
  if (result.screenshot != null && result.screenshot !== '') {
102
103
  parts.push('\n[Screenshot captured and displayed to user]');
@@ -157,79 +158,61 @@ function createBrowserTools(options) {
157
158
  // browser_click
158
159
  tools$1.push(tools.tool(createToolFunction('click'), {
159
160
  name: EBrowserTools.CLICK,
160
- description: `Click an element on the current web page by its index number.
161
- The element list shows clickable items like: [0]<button>Submit</button> [1]<a href="/home">Home</a>
162
- Use the index number in brackets to click that element.
163
- After clicking, you receive an updated element list showing the new page state.`,
161
+ description: 'Click element by [index]. Use fieldLabel attribute to identify correct element. Skip disabled elements.',
164
162
  schema: BrowserClickSchema,
165
163
  }));
166
164
  // browser_type
167
165
  tools$1.push(tools.tool(createToolFunction('type'), {
168
166
  name: EBrowserTools.TYPE,
169
- description: `Type text into an input element on the page.
170
- Find the input element in the list by its index (e.g., [5]<input placeholder="Search">).
171
- Set pressEnter: true to submit forms after typing.
172
- After typing, you receive an updated element list.`,
167
+ description: 'Type text into input by [index]. IMPORTANT: Use fieldLabel to identify the correct field (e.g., fieldLabel="To" vs "Cc"). Use pressEnter:true to submit.',
173
168
  schema: BrowserTypeSchema,
174
169
  }));
175
170
  // browser_navigate
176
171
  tools$1.push(tools.tool(createToolFunction('navigate'), {
177
172
  name: EBrowserTools.NAVIGATE,
178
- description: `Navigate to a URL. Always include the full URL with https://.
179
- After navigation, you receive the new page's element list.`,
173
+ description: 'Navigate to URL (include https://). Returns new page element list.',
180
174
  schema: BrowserNavigateSchema,
181
175
  }));
182
176
  // browser_scroll
183
177
  tools$1.push(tools.tool(createToolFunction('scroll'), {
184
178
  name: EBrowserTools.SCROLL,
185
- description: `Scroll the page to reveal more content.
186
- Use 'down' to scroll down, 'up' to scroll up.
187
- After scrolling, you receive an updated element list with newly visible elements.`,
179
+ description: 'Scroll page (up/down/left/right). Returns updated element list.',
188
180
  schema: BrowserScrollSchema,
189
181
  }));
190
182
  // browser_extract
191
183
  tools$1.push(tools.tool(createToolFunction('extract'), {
192
184
  name: EBrowserTools.EXTRACT,
193
- description: `Extract content from the current page.
194
- Returns page URL, title, and element list.`,
185
+ description: 'Extract page content. Returns URL, title, and element list.',
195
186
  schema: BrowserExtractSchema,
196
187
  }));
197
188
  // browser_hover
198
189
  tools$1.push(tools.tool(createToolFunction('hover'), {
199
190
  name: EBrowserTools.HOVER,
200
- description: `Hover over an element to reveal tooltips, dropdowns, or other hover-triggered content.
201
- After hovering, you receive an updated element list with any newly revealed elements.`,
191
+ description: 'Hover element by [index] to reveal menus/tooltips. Returns updated element list.',
202
192
  schema: BrowserHoverSchema,
203
193
  }));
204
194
  // browser_wait
205
195
  tools$1.push(tools.tool(createToolFunction('wait'), {
206
196
  name: EBrowserTools.WAIT,
207
- description: `Wait for a specified duration for page content to load.
208
- Use this after actions that trigger async content loading.
209
- After waiting, you receive an updated element list.`,
197
+ description: 'Wait for async content to load. Returns updated element list.',
210
198
  schema: BrowserWaitSchema,
211
199
  }));
212
200
  // browser_back
213
201
  tools$1.push(tools.tool(createToolFunction('back'), {
214
202
  name: EBrowserTools.BACK,
215
- description: `Go back to the previous page in browser history.
216
- After going back, you receive the previous page's element list.`,
203
+ description: 'Go back in browser history. Returns previous page element list.',
217
204
  schema: BrowserBackSchema,
218
205
  }));
219
206
  // browser_screenshot
220
207
  tools$1.push(tools.tool(createToolFunction('screenshot'), {
221
208
  name: EBrowserTools.SCREENSHOT,
222
- description: `Capture a screenshot of the current page.
223
- Returns the page state with a note that screenshot was displayed to the user.
224
- Use browser_get_page_state to get the element list for automation.`,
209
+ description: 'Capture screenshot. Displayed to user. Use get_page_state for automation.',
225
210
  schema: BrowserScreenshotSchema,
226
211
  }));
227
212
  // browser_get_page_state
228
213
  tools$1.push(tools.tool(createToolFunction('get_page_state'), {
229
214
  name: EBrowserTools.GET_PAGE_STATE,
230
- description: `Get the current page state including URL, title, and all interactive elements.
231
- Use this at the start of a task to see what elements are available.
232
- Returns a text list of elements with their index numbers for interaction.`,
215
+ description: 'Get page URL, title, and interactive elements with [index] for actions. Start here.',
233
216
  schema: BrowserGetPageStateSchema,
234
217
  }));
235
218
  return tools$1;
@@ -1 +1 @@
1
- {"version":3,"file":"BrowserTools.cjs","sources":["../../../src/tools/BrowserTools.ts"],"sourcesContent":["import { z } from 'zod';\nimport { tool, DynamicStructuredTool } from '@langchain/core/tools';\nimport type * as _t from '@/types';\n\n/**\n * Browser tool names - keep in sync with ranger-browser extension\n * These tools execute locally in the browser extension, NOT on the server\n */\nexport const EBrowserTools = {\n CLICK: 'browser_click',\n TYPE: 'browser_type',\n NAVIGATE: 'browser_navigate',\n SCROLL: 'browser_scroll',\n EXTRACT: 'browser_extract',\n HOVER: 'browser_hover',\n WAIT: 'browser_wait',\n BACK: 'browser_back',\n SCREENSHOT: 'browser_screenshot',\n GET_PAGE_STATE: 'browser_get_page_state',\n} as const;\n\nexport type BrowserToolName =\n (typeof EBrowserTools)[keyof typeof EBrowserTools];\n\n/**\n * Callback function type for waiting on browser action results\n * This allows the server (Ranger) to provide a callback that waits for the extension\n * to POST results back to the server before returning to the LLM.\n *\n * @param action - The browser action (click, type, navigate, etc.)\n * @param args - Arguments for the action\n * @param toolCallId - Unique ID for this tool call (from config.toolCall.id)\n * @returns Promise that resolves with the actual browser result (page state, etc.)\n */\nexport type BrowserToolCallback = (\n action: string,\n args: Record<string, unknown>,\n toolCallId: string\n) => Promise<BrowserActionResult>;\n\n/**\n * Result returned from browser action execution\n */\nexport interface BrowserActionResult {\n success: boolean;\n url?: string;\n title?: string;\n elementList?: string; // Text-based element list\n error?: string;\n screenshot?: string; // Base64 screenshot (if requested)\n}\n\n/**\n * Check if browser capability is available based on request headers or context\n * The browser extension sets these headers when connected:\n * - X-Ranger-Browser-Extension: true\n * - X-Ranger-Browser-Capable: true\n */\nexport function hasBrowserCapability(req?: {\n headers?: Record<string, string | string[] | undefined>;\n}): boolean {\n if (!req?.headers) {\n return false;\n }\n\n const browserExtension = req.headers['x-ranger-browser-extension'];\n const browserCapable = req.headers['x-ranger-browser-capable'];\n\n return browserExtension === 'true' || browserCapable === 'true';\n}\n\n// Tool schemas\nconst BrowserClickSchema = z.object({\n index: z\n .number()\n .describe(\n 'The index number [0], [1], etc. of the element to click from the page state element list'\n ),\n});\n\nconst BrowserTypeSchema = z.object({\n index: z\n .number()\n .describe('The index number of the input element to type into'),\n text: z.string().describe('The text to type into the element'),\n pressEnter: z\n .boolean()\n .optional()\n .describe('Whether to press Enter after typing (useful for search forms)'),\n});\n\nconst BrowserNavigateSchema = z.object({\n url: z\n .string()\n .describe('The full URL to navigate to (must include https://)'),\n});\n\nconst BrowserScrollSchema = z.object({\n direction: z\n .enum(['up', 'down', 'left', 'right'])\n .describe('Direction to scroll'),\n amount: z\n .number()\n .optional()\n .describe('Pixels to scroll (default: one viewport height)'),\n});\n\nconst BrowserExtractSchema = z.object({\n query: z\n .string()\n .optional()\n .describe('Optional: specific content to extract from the page'),\n});\n\nconst BrowserHoverSchema = z.object({\n index: z.number().describe('The index number of the element to hover over'),\n});\n\nconst BrowserWaitSchema = z.object({\n duration: z\n .number()\n .optional()\n .describe('Milliseconds to wait (default: 1000)'),\n});\n\nconst BrowserBackSchema = z.object({});\n\nconst BrowserScreenshotSchema = z.object({});\n\nconst BrowserGetPageStateSchema = z.object({});\n\n/**\n * Browser tool response interface\n * This is what the extension returns after executing the action\n */\nexport interface BrowserToolResponse {\n requiresBrowserExecution: true;\n action: string;\n args: Record<string, unknown>;\n toolCallId?: string; // Added to help extension correlate with callback\n}\n\n/**\n * Options for creating browser tools\n */\nexport interface CreateBrowserToolsOptions {\n /**\n * Optional callback that waits for browser action results.\n * When provided, tools will await this callback to get actual results from the extension.\n * When not provided, tools return markers immediately (for non-server contexts).\n */\n waitForResult?: BrowserToolCallback;\n}\n\n/**\n * Format browser action result for LLM consumption\n */\nfunction formatResultForLLM(\n result: BrowserActionResult,\n action: string\n): string {\n if (!result.success && result.error) {\n return `Browser action \"${action}\" failed: ${result.error}`;\n }\n\n const parts: string[] = [];\n\n if (result.url != null && result.url !== '') {\n parts.push(`**Current URL:** ${result.url}`);\n }\n if (result.title != null && result.title !== '') {\n parts.push(`**Page Title:** ${result.title}`);\n }\n if (result.elementList != null && result.elementList !== '') {\n parts.push(`\\n**Interactive Elements:**\\n${result.elementList}`);\n }\n if (result.screenshot != null && result.screenshot !== '') {\n parts.push('\\n[Screenshot captured and displayed to user]');\n }\n\n if (parts.length === 0) {\n return `Browser action \"${action}\" completed successfully.`;\n }\n\n return parts.join('\\n');\n}\n\n/**\n * Create browser tools with optional callback for waiting on results\n *\n * When waitForResult callback is provided:\n * 1. Tool returns marker that triggers extension\n * 2. Tool then awaits callback to get actual results\n * 3. Returns real page state to LLM\n *\n * When no callback:\n * 1. Tool returns marker only (for non-server contexts)\n *\n * NOTE: These tools use TEXT-BASED element lists, NOT screenshots\n * Screenshots would be 100K+ tokens each - element lists are ~100 tokens\n */\nexport function createBrowserTools(\n options?: CreateBrowserToolsOptions\n): DynamicStructuredTool[] {\n const { waitForResult } = options || {};\n const tools: DynamicStructuredTool[] = [];\n\n /**\n * Helper to create tool function that optionally waits for results\n * The toolCallId is extracted from the RunnableConfig passed by LangChain\n */\n const createToolFunction = (action: string) => {\n return async (\n args: Record<string, unknown>,\n config?: { toolCall?: { id?: string } }\n ): Promise<string> => {\n const toolCallId =\n config?.toolCall?.id ??\n `tool_${Date.now()}_${Math.random().toString(36).slice(2)}`;\n\n // Create marker for extension\n const marker: BrowserToolResponse = {\n requiresBrowserExecution: true,\n action,\n args,\n toolCallId,\n };\n\n // If no callback, return marker immediately (extension handles via SSE interception)\n if (!waitForResult) {\n return JSON.stringify(marker);\n }\n\n // With callback: wait for actual results from extension\n // The marker is still returned initially via SSE, but we wait for the callback\n try {\n const result = await waitForResult(action, args, toolCallId);\n return formatResultForLLM(result, action);\n } catch (error) {\n const errorMessage =\n error instanceof Error ? error.message : String(error);\n return `Browser action \"${action}\" failed: ${errorMessage}`;\n }\n };\n };\n\n // browser_click\n tools.push(\n tool(createToolFunction('click'), {\n name: EBrowserTools.CLICK,\n description: `Click an element on the current web page by its index number.\nThe element list shows clickable items like: [0]<button>Submit</button> [1]<a href=\"/home\">Home</a>\nUse the index number in brackets to click that element.\nAfter clicking, you receive an updated element list showing the new page state.`,\n schema: BrowserClickSchema,\n })\n );\n\n // browser_type\n tools.push(\n tool(createToolFunction('type'), {\n name: EBrowserTools.TYPE,\n description: `Type text into an input element on the page.\nFind the input element in the list by its index (e.g., [5]<input placeholder=\"Search\">).\nSet pressEnter: true to submit forms after typing.\nAfter typing, you receive an updated element list.`,\n schema: BrowserTypeSchema,\n })\n );\n\n // browser_navigate\n tools.push(\n tool(createToolFunction('navigate'), {\n name: EBrowserTools.NAVIGATE,\n description: `Navigate to a URL. Always include the full URL with https://.\nAfter navigation, you receive the new page's element list.`,\n schema: BrowserNavigateSchema,\n })\n );\n\n // browser_scroll\n tools.push(\n tool(createToolFunction('scroll'), {\n name: EBrowserTools.SCROLL,\n description: `Scroll the page to reveal more content.\nUse 'down' to scroll down, 'up' to scroll up.\nAfter scrolling, you receive an updated element list with newly visible elements.`,\n schema: BrowserScrollSchema,\n })\n );\n\n // browser_extract\n tools.push(\n tool(createToolFunction('extract'), {\n name: EBrowserTools.EXTRACT,\n description: `Extract content from the current page.\nReturns page URL, title, and element list.`,\n schema: BrowserExtractSchema,\n })\n );\n\n // browser_hover\n tools.push(\n tool(createToolFunction('hover'), {\n name: EBrowserTools.HOVER,\n description: `Hover over an element to reveal tooltips, dropdowns, or other hover-triggered content.\nAfter hovering, you receive an updated element list with any newly revealed elements.`,\n schema: BrowserHoverSchema,\n })\n );\n\n // browser_wait\n tools.push(\n tool(createToolFunction('wait'), {\n name: EBrowserTools.WAIT,\n description: `Wait for a specified duration for page content to load.\nUse this after actions that trigger async content loading.\nAfter waiting, you receive an updated element list.`,\n schema: BrowserWaitSchema,\n })\n );\n\n // browser_back\n tools.push(\n tool(createToolFunction('back'), {\n name: EBrowserTools.BACK,\n description: `Go back to the previous page in browser history.\nAfter going back, you receive the previous page's element list.`,\n schema: BrowserBackSchema,\n })\n );\n\n // browser_screenshot\n tools.push(\n tool(createToolFunction('screenshot'), {\n name: EBrowserTools.SCREENSHOT,\n description: `Capture a screenshot of the current page.\nReturns the page state with a note that screenshot was displayed to the user.\nUse browser_get_page_state to get the element list for automation.`,\n schema: BrowserScreenshotSchema,\n })\n );\n\n // browser_get_page_state\n tools.push(\n tool(createToolFunction('get_page_state'), {\n name: EBrowserTools.GET_PAGE_STATE,\n description: `Get the current page state including URL, title, and all interactive elements.\nUse this at the start of a task to see what elements are available.\nReturns a text list of elements with their index numbers for interaction.`,\n schema: BrowserGetPageStateSchema,\n })\n );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WAAW,EAAE,CAAA;AAC6C,+DAAA,CAAA;AAC1D,QAAA,MAAM,EAAE,iBAAiB;AAC1B,KAAA,CAAC,CACH;;IAGDD,OAAK,CAAC,IAAI,CACRC,UAAI,CAAC,kBAAkB,CAAC,YAAY,CAAC,EAAE;QACrC,IAAI,EAAE,aAAa,CAAC,UAAU;AAC9B,QAAA,WAAW,EAAE,CAAA;;AAEgD,kEAAA,CAAA;AAC7D,QAAA,MAAM,EAAE,uBAAuB;AAChC,KAAA,CAAC,CACH;;IAGDD,OAAK,CAAC,IAAI,CACRC,UAAI,CAAC,kBAAkB,CAAC,gBAAgB,CAAC,EAAE;QACzC,IAAI,EAAE,aAAa,CAAC,cAAc;AAClC,QAAA,WAAW,EAAE,CAAA;;AAEuD,yEAAA,CAAA;AACpE,QAAA,MAAM,EAAE,yBAAyB;AAClC,KAAA,CAAC,CACH;AAED,IAAA,OAAOD,OAAK;AACd;;;;;;"}
1
+ {"version":3,"file":"BrowserTools.cjs","sources":["../../../src/tools/BrowserTools.ts"],"sourcesContent":["import { z } from 'zod';\nimport { tool, DynamicStructuredTool } from '@langchain/core/tools';\nimport type * as _t from '@/types';\n\n/**\n * Browser tool names - keep in sync with ranger-browser extension\n * These tools execute locally in the browser extension, NOT on the server\n */\nexport const EBrowserTools = {\n CLICK: 'browser_click',\n TYPE: 'browser_type',\n NAVIGATE: 'browser_navigate',\n SCROLL: 'browser_scroll',\n EXTRACT: 'browser_extract',\n HOVER: 'browser_hover',\n WAIT: 'browser_wait',\n BACK: 'browser_back',\n SCREENSHOT: 'browser_screenshot',\n GET_PAGE_STATE: 'browser_get_page_state',\n} as const;\n\nexport type BrowserToolName =\n (typeof EBrowserTools)[keyof typeof EBrowserTools];\n\n/**\n * Callback function type for waiting on browser action results\n * This allows the server (Ranger) to provide a callback that waits for the extension\n * to POST results back to the server before returning to the LLM.\n *\n * @param action - The browser action (click, type, navigate, etc.)\n * @param args - Arguments for the action\n * @param toolCallId - Unique ID for this tool call (from config.toolCall.id)\n * @returns Promise that resolves with the actual browser result (page state, etc.)\n */\nexport type BrowserToolCallback = (\n action: string,\n args: Record<string, unknown>,\n toolCallId: string\n) => Promise<BrowserActionResult>;\n\n/**\n * Result returned from browser action execution\n */\nexport interface BrowserActionResult {\n success: boolean;\n url?: string;\n title?: string;\n elementList?: string; // Text-based element list\n error?: string;\n screenshot?: string; // Base64 screenshot (if requested)\n}\n\n/**\n * Check if browser capability is available based on request headers or context\n * The browser extension sets these headers when connected:\n * - X-Ranger-Browser-Extension: true\n * - X-Ranger-Browser-Capable: true\n */\nexport function hasBrowserCapability(req?: {\n headers?: Record<string, string | string[] | undefined>;\n}): boolean {\n if (!req?.headers) {\n return false;\n }\n\n const browserExtension = req.headers['x-ranger-browser-extension'];\n const browserCapable = req.headers['x-ranger-browser-capable'];\n\n return browserExtension === 'true' || browserCapable === 'true';\n}\n\n// Tool schemas\nconst BrowserClickSchema = z.object({\n index: z\n .number()\n .describe(\n 'The [index] of the element to click. Use fieldLabel attribute to identify the correct element when multiple similar elements exist (e.g., fieldLabel=\"To\" vs fieldLabel=\"Cc\" for email fields).'\n ),\n});\n\nconst BrowserTypeSchema = z.object({\n index: z\n .number()\n .describe('The [index] of the input element to type into. IMPORTANT: Check fieldLabel attribute to ensure you are typing into the correct field (e.g., fieldLabel=\"To\" for recipient, fieldLabel=\"Subject\" for subject line).'),\n text: z.string().describe('The text to type into the element'),\n pressEnter: z\n .boolean()\n .optional()\n .describe('Whether to press Enter after typing (useful for search forms)'),\n});\n\nconst BrowserNavigateSchema = z.object({\n url: z\n .string()\n .describe('The full URL to navigate to (must include https://)'),\n});\n\nconst BrowserScrollSchema = z.object({\n direction: z\n .enum(['up', 'down', 'left', 'right'])\n .describe('Direction to scroll'),\n amount: z\n .number()\n .optional()\n .describe('Pixels to scroll (default: one viewport height)'),\n});\n\nconst BrowserExtractSchema = z.object({\n query: z\n .string()\n .optional()\n .describe('Optional: specific content to extract from the page'),\n});\n\nconst BrowserHoverSchema = z.object({\n index: z.number().describe('The index number of the element to hover over'),\n});\n\nconst BrowserWaitSchema = z.object({\n duration: z\n .number()\n .optional()\n .describe('Milliseconds to wait (default: 1000)'),\n});\n\nconst BrowserBackSchema = z.object({});\n\nconst BrowserScreenshotSchema = z.object({});\n\nconst BrowserGetPageStateSchema = z.object({});\n\n/**\n * Browser tool response interface\n * This is what the extension returns after executing the action\n */\nexport interface BrowserToolResponse {\n requiresBrowserExecution: true;\n action: string;\n args: Record<string, unknown>;\n toolCallId?: string; // Added to help extension correlate with callback\n}\n\n/**\n * Options for creating browser tools\n */\nexport interface CreateBrowserToolsOptions {\n /**\n * Optional callback that waits for browser action results.\n * When provided, tools will await this callback to get actual results from the extension.\n * When not provided, tools return markers immediately (for non-server contexts).\n */\n waitForResult?: BrowserToolCallback;\n}\n\n/**\n * Format browser action result for LLM consumption\n */\nfunction formatResultForLLM(\n result: BrowserActionResult,\n action: string\n): string {\n if (!result.success && result.error) {\n return `Browser action \"${action}\" failed: ${result.error}`;\n }\n\n const parts: string[] = [];\n\n if (result.url != null && result.url !== '') {\n parts.push(`**Current URL:** ${result.url}`);\n }\n if (result.title != null && result.title !== '') {\n parts.push(`**Page Title:** ${result.title}`);\n }\n if (result.elementList != null && result.elementList !== '') {\n // Add hint about fieldLabel for form interactions\n parts.push(`\\n**Interactive Elements** (use fieldLabel to identify form fields):\\n${result.elementList}`);\n }\n if (result.screenshot != null && result.screenshot !== '') {\n parts.push('\\n[Screenshot captured and displayed to user]');\n }\n\n if (parts.length === 0) {\n return `Browser action \"${action}\" completed successfully.`;\n }\n\n return parts.join('\\n');\n}\n\n/**\n * Create browser tools with optional callback for waiting on results\n *\n * When waitForResult callback is provided:\n * 1. Tool returns marker that triggers extension\n * 2. Tool then awaits callback to get actual results\n * 3. Returns real page state to LLM\n *\n * When no callback:\n * 1. Tool returns marker only (for non-server contexts)\n *\n * NOTE: These tools use TEXT-BASED element lists, NOT screenshots\n * Screenshots would be 100K+ tokens each - element lists are ~100 tokens\n */\nexport function createBrowserTools(\n options?: CreateBrowserToolsOptions\n): DynamicStructuredTool[] {\n const { waitForResult } = options || {};\n const tools: DynamicStructuredTool[] = [];\n\n /**\n * Helper to create tool function that optionally waits for results\n * The toolCallId is extracted from the RunnableConfig passed by LangChain\n */\n const createToolFunction = (action: string) => {\n return async (\n args: Record<string, unknown>,\n config?: { toolCall?: { id?: string } }\n ): Promise<string> => {\n const toolCallId =\n config?.toolCall?.id ??\n `tool_${Date.now()}_${Math.random().toString(36).slice(2)}`;\n\n // Create marker for extension\n const marker: BrowserToolResponse = {\n requiresBrowserExecution: true,\n action,\n args,\n toolCallId,\n };\n\n // If no callback, return marker immediately (extension handles via SSE interception)\n if (!waitForResult) {\n return JSON.stringify(marker);\n }\n\n // With callback: wait for actual results from extension\n // The marker is still returned initially via SSE, but we wait for the callback\n try {\n const result = await waitForResult(action, args, toolCallId);\n return formatResultForLLM(result, action);\n } catch (error) {\n const errorMessage =\n error instanceof Error ? error.message : String(error);\n return `Browser action \"${action}\" failed: ${errorMessage}`;\n }\n };\n };\n\n // browser_click\n tools.push(\n tool(createToolFunction('click'), {\n name: EBrowserTools.CLICK,\n description: 'Click element by [index]. Use fieldLabel attribute to identify correct element. Skip disabled elements.',\n schema: BrowserClickSchema,\n })\n );\n\n // browser_type\n tools.push(\n tool(createToolFunction('type'), {\n name: EBrowserTools.TYPE,\n description: 'Type text into input by [index]. IMPORTANT: Use fieldLabel to identify the correct field (e.g., fieldLabel=\"To\" vs \"Cc\"). Use pressEnter:true to submit.',\n schema: BrowserTypeSchema,\n })\n );\n\n // browser_navigate\n tools.push(\n tool(createToolFunction('navigate'), {\n name: EBrowserTools.NAVIGATE,\n description: 'Navigate to URL (include https://). Returns new page element list.',\n schema: BrowserNavigateSchema,\n })\n );\n\n // browser_scroll\n tools.push(\n tool(createToolFunction('scroll'), {\n name: EBrowserTools.SCROLL,\n description: 'Scroll page (up/down/left/right). Returns updated element list.',\n schema: BrowserScrollSchema,\n })\n );\n\n // browser_extract\n tools.push(\n tool(createToolFunction('extract'), {\n name: EBrowserTools.EXTRACT,\n description: 'Extract page content. Returns URL, title, and element list.',\n schema: BrowserExtractSchema,\n })\n );\n\n // browser_hover\n tools.push(\n tool(createToolFunction('hover'), {\n name: EBrowserTools.HOVER,\n description: 'Hover element by [index] to reveal menus/tooltips. Returns updated element list.',\n schema: BrowserHoverSchema,\n })\n );\n\n // browser_wait\n tools.push(\n tool(createToolFunction('wait'), {\n name: EBrowserTools.WAIT,\n description: 'Wait for async content to load. Returns updated element list.',\n schema: BrowserWaitSchema,\n })\n );\n\n // browser_back\n tools.push(\n tool(createToolFunction('back'), {\n name: EBrowserTools.BACK,\n description: 'Go back in browser history. Returns previous page element list.',\n schema: BrowserBackSchema,\n })\n );\n\n // browser_screenshot\n tools.push(\n tool(createToolFunction('screenshot'), {\n name: EBrowserTools.SCREENSHOT,\n description: 'Capture screenshot. Displayed to user. Use get_page_state for automation.',\n schema: BrowserScreenshotSchema,\n })\n );\n\n // browser_get_page_state\n tools.push(\n tool(createToolFunction('get_page_state'), {\n name: EBrowserTools.GET_PAGE_STATE,\n description: 'Get page URL, title, and interactive elements with [index] for actions. Start here.',\n schema: BrowserGetPageStateSchema,\n })\n );\n\n return tools;\n}\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;;AAkC1C;;;;;AAKG;AACG,SAAU,oBAAoB,CAAC,GAEpC,EAAA;AACC,IAAA,IAAI,C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38
- .describe('The index number [0], [1], etc. of the element to click from the page state element list'),
38
+ .describe('The [index] of the element to click. Use fieldLabel attribute to identify the correct element when multiple similar elements exist (e.g., fieldLabel="To" vs fieldLabel="Cc" for email fields).'),
39
39
  });
40
40
  const BrowserTypeSchema = z.object({
41
41
  index: z
42
42
  .number()
43
- .describe('The index number of the input element to type into'),
43
+ .describe('The [index] of the input element to type into. IMPORTANT: Check fieldLabel attribute to ensure you are typing into the correct field (e.g., fieldLabel="To" for recipient, fieldLabel="Subject" for subject line).'),
44
44
  text: z.string().describe('The text to type into the element'),
45
45
  pressEnter: z
46
46
  .boolean()
@@ -94,7 +94,8 @@ function formatResultForLLM(result, action) {
94
94
  parts.push(`**Page Title:** ${result.title}`);
95
95
  }
96
96
  if (result.elementList != null && result.elementList !== '') {
97
- parts.push(`\n**Interactive Elements:**\n${result.elementList}`);
97
+ // Add hint about fieldLabel for form interactions
98
+ parts.push(`\n**Interactive Elements** (use fieldLabel to identify form fields):\n${result.elementList}`);
98
99
  }
99
100
  if (result.screenshot != null && result.screenshot !== '') {
100
101
  parts.push('\n[Screenshot captured and displayed to user]');
@@ -155,79 +156,61 @@ function createBrowserTools(options) {
155
156
  // browser_click
156
157
  tools.push(tool(createToolFunction('click'), {
157
158
  name: EBrowserTools.CLICK,
158
- description: `Click an element on the current web page by its index number.
159
- The element list shows clickable items like: [0]<button>Submit</button> [1]<a href="/home">Home</a>
160
- Use the index number in brackets to click that element.
161
- After clicking, you receive an updated element list showing the new page state.`,
159
+ description: 'Click element by [index]. Use fieldLabel attribute to identify correct element. Skip disabled elements.',
162
160
  schema: BrowserClickSchema,
163
161
  }));
164
162
  // browser_type
165
163
  tools.push(tool(createToolFunction('type'), {
166
164
  name: EBrowserTools.TYPE,
167
- description: `Type text into an input element on the page.
168
- Find the input element in the list by its index (e.g., [5]<input placeholder="Search">).
169
- Set pressEnter: true to submit forms after typing.
170
- After typing, you receive an updated element list.`,
165
+ description: 'Type text into input by [index]. IMPORTANT: Use fieldLabel to identify the correct field (e.g., fieldLabel="To" vs "Cc"). Use pressEnter:true to submit.',
171
166
  schema: BrowserTypeSchema,
172
167
  }));
173
168
  // browser_navigate
174
169
  tools.push(tool(createToolFunction('navigate'), {
175
170
  name: EBrowserTools.NAVIGATE,
176
- description: `Navigate to a URL. Always include the full URL with https://.
177
- After navigation, you receive the new page's element list.`,
171
+ description: 'Navigate to URL (include https://). Returns new page element list.',
178
172
  schema: BrowserNavigateSchema,
179
173
  }));
180
174
  // browser_scroll
181
175
  tools.push(tool(createToolFunction('scroll'), {
182
176
  name: EBrowserTools.SCROLL,
183
- description: `Scroll the page to reveal more content.
184
- Use 'down' to scroll down, 'up' to scroll up.
185
- After scrolling, you receive an updated element list with newly visible elements.`,
177
+ description: 'Scroll page (up/down/left/right). Returns updated element list.',
186
178
  schema: BrowserScrollSchema,
187
179
  }));
188
180
  // browser_extract
189
181
  tools.push(tool(createToolFunction('extract'), {
190
182
  name: EBrowserTools.EXTRACT,
191
- description: `Extract content from the current page.
192
- Returns page URL, title, and element list.`,
183
+ description: 'Extract page content. Returns URL, title, and element list.',
193
184
  schema: BrowserExtractSchema,
194
185
  }));
195
186
  // browser_hover
196
187
  tools.push(tool(createToolFunction('hover'), {
197
188
  name: EBrowserTools.HOVER,
198
- description: `Hover over an element to reveal tooltips, dropdowns, or other hover-triggered content.
199
- After hovering, you receive an updated element list with any newly revealed elements.`,
189
+ description: 'Hover element by [index] to reveal menus/tooltips. Returns updated element list.',
200
190
  schema: BrowserHoverSchema,
201
191
  }));
202
192
  // browser_wait
203
193
  tools.push(tool(createToolFunction('wait'), {
204
194
  name: EBrowserTools.WAIT,
205
- description: `Wait for a specified duration for page content to load.
206
- Use this after actions that trigger async content loading.
207
- After waiting, you receive an updated element list.`,
195
+ description: 'Wait for async content to load. Returns updated element list.',
208
196
  schema: BrowserWaitSchema,
209
197
  }));
210
198
  // browser_back
211
199
  tools.push(tool(createToolFunction('back'), {
212
200
  name: EBrowserTools.BACK,
213
- description: `Go back to the previous page in browser history.
214
- After going back, you receive the previous page's element list.`,
201
+ description: 'Go back in browser history. Returns previous page element list.',
215
202
  schema: BrowserBackSchema,
216
203
  }));
217
204
  // browser_screenshot
218
205
  tools.push(tool(createToolFunction('screenshot'), {
219
206
  name: EBrowserTools.SCREENSHOT,
220
- description: `Capture a screenshot of the current page.
221
- Returns the page state with a note that screenshot was displayed to the user.
222
- Use browser_get_page_state to get the element list for automation.`,
207
+ description: 'Capture screenshot. Displayed to user. Use get_page_state for automation.',
223
208
  schema: BrowserScreenshotSchema,
224
209
  }));
225
210
  // browser_get_page_state
226
211
  tools.push(tool(createToolFunction('get_page_state'), {
227
212
  name: EBrowserTools.GET_PAGE_STATE,
228
- description: `Get the current page state including URL, title, and all interactive elements.
229
- Use this at the start of a task to see what elements are available.
230
- Returns a text list of elements with their index numbers for interaction.`,
213
+ description: 'Get page URL, title, and interactive elements with [index] for actions. Start here.',
231
214
  schema: BrowserGetPageStateSchema,
232
215
  }));
233
216
  return tools;
@@ -1 +1 @@
1
- {"version":3,"file":"BrowserTools.mjs","sources":["../../../src/tools/BrowserTools.ts"],"sourcesContent":["import { z } from 'zod';\nimport { tool, DynamicStructuredTool } from '@langchain/core/tools';\nimport type * as _t from '@/types';\n\n/**\n * Browser tool names - keep in sync with ranger-browser extension\n * These tools execute locally in the browser extension, NOT on the server\n */\nexport const EBrowserTools = {\n CLICK: 'browser_click',\n TYPE: 'browser_type',\n NAVIGATE: 'browser_navigate',\n SCROLL: 'browser_scroll',\n EXTRACT: 'browser_extract',\n HOVER: 'browser_hover',\n WAIT: 'browser_wait',\n BACK: 'browser_back',\n SCREENSHOT: 'browser_screenshot',\n GET_PAGE_STATE: 'browser_get_page_state',\n} as const;\n\nexport type BrowserToolName =\n (typeof EBrowserTools)[keyof typeof EBrowserTools];\n\n/**\n * Callback function type for waiting on browser action results\n * This allows the server (Ranger) to provide a callback that waits for the extension\n * to POST results back to the server before returning to the LLM.\n *\n * @param action - The browser action (click, type, navigate, etc.)\n * @param args - Arguments for the action\n * @param toolCallId - Unique ID for this tool call (from config.toolCall.id)\n * @returns Promise that resolves with the actual browser result (page state, etc.)\n */\nexport type BrowserToolCallback = (\n action: string,\n args: Record<string, unknown>,\n toolCallId: string\n) => Promise<BrowserActionResult>;\n\n/**\n * Result returned from browser action execution\n */\nexport interface BrowserActionResult {\n success: boolean;\n url?: string;\n title?: string;\n elementList?: string; // Text-based element list\n error?: string;\n screenshot?: string; // Base64 screenshot (if requested)\n}\n\n/**\n * Check if browser capability is available based on request headers or context\n * The browser extension sets these headers when connected:\n * - X-Ranger-Browser-Extension: true\n * - X-Ranger-Browser-Capable: true\n */\nexport function hasBrowserCapability(req?: {\n headers?: Record<string, string | string[] | undefined>;\n}): boolean {\n if (!req?.headers) {\n return false;\n }\n\n const browserExtension = req.headers['x-ranger-browser-extension'];\n const browserCapable = req.headers['x-ranger-browser-capable'];\n\n return browserExtension === 'true' || browserCapable === 'true';\n}\n\n// Tool schemas\nconst BrowserClickSchema = z.object({\n index: z\n .number()\n .describe(\n 'The index number [0], [1], etc. of the element to click from the page state element list'\n ),\n});\n\nconst BrowserTypeSchema = z.object({\n index: z\n .number()\n .describe('The index number of the input element to type into'),\n text: z.string().describe('The text to type into the element'),\n pressEnter: z\n .boolean()\n .optional()\n .describe('Whether to press Enter after typing (useful for search forms)'),\n});\n\nconst BrowserNavigateSchema = z.object({\n url: z\n .string()\n .describe('The full URL to navigate to (must include https://)'),\n});\n\nconst BrowserScrollSchema = z.object({\n direction: z\n .enum(['up', 'down', 'left', 'right'])\n .describe('Direction to scroll'),\n amount: z\n .number()\n .optional()\n .describe('Pixels to scroll (default: one viewport height)'),\n});\n\nconst BrowserExtractSchema = z.object({\n query: z\n .string()\n .optional()\n .describe('Optional: specific content to extract from the page'),\n});\n\nconst BrowserHoverSchema = z.object({\n index: z.number().describe('The index number of the element to hover over'),\n});\n\nconst BrowserWaitSchema = z.object({\n duration: z\n .number()\n .optional()\n .describe('Milliseconds to wait (default: 1000)'),\n});\n\nconst BrowserBackSchema = z.object({});\n\nconst BrowserScreenshotSchema = z.object({});\n\nconst BrowserGetPageStateSchema = z.object({});\n\n/**\n * Browser tool response interface\n * This is what the extension returns after executing the action\n */\nexport interface BrowserToolResponse {\n requiresBrowserExecution: true;\n action: string;\n args: Record<string, unknown>;\n toolCallId?: string; // Added to help extension correlate with callback\n}\n\n/**\n * Options for creating browser tools\n */\nexport interface CreateBrowserToolsOptions {\n /**\n * Optional callback that waits for browser action results.\n * When provided, tools will await this callback to get actual results from the extension.\n * When not provided, tools return markers immediately (for non-server contexts).\n */\n waitForResult?: BrowserToolCallback;\n}\n\n/**\n * Format browser action result for LLM consumption\n */\nfunction formatResultForLLM(\n result: BrowserActionResult,\n action: string\n): string {\n if (!result.success && result.error) {\n return `Browser action \"${action}\" failed: ${result.error}`;\n }\n\n const parts: string[] = [];\n\n if (result.url != null && result.url !== '') {\n parts.push(`**Current URL:** ${result.url}`);\n }\n if (result.title != null && result.title !== '') {\n parts.push(`**Page Title:** ${result.title}`);\n }\n if (result.elementList != null && result.elementList !== '') {\n parts.push(`\\n**Interactive Elements:**\\n${result.elementList}`);\n }\n if (result.screenshot != null && result.screenshot !== '') {\n parts.push('\\n[Screenshot captured and displayed to user]');\n }\n\n if (parts.length === 0) {\n return `Browser action \"${action}\" completed successfully.`;\n }\n\n return parts.join('\\n');\n}\n\n/**\n * Create browser tools with optional callback for waiting on results\n *\n * When waitForResult callback is provided:\n * 1. Tool returns marker that triggers extension\n * 2. Tool then awaits callback to get actual results\n * 3. Returns real page state to LLM\n *\n * When no callback:\n * 1. Tool returns marker only (for non-server contexts)\n *\n * NOTE: These tools use TEXT-BASED element lists, NOT screenshots\n * Screenshots would be 100K+ tokens each - element lists are ~100 tokens\n */\nexport function createBrowserTools(\n options?: CreateBrowserToolsOptions\n): DynamicStructuredTool[] {\n const { waitForResult } = options || {};\n const tools: DynamicStructuredTool[] = [];\n\n /**\n * Helper to create tool function that optionally waits for results\n * The toolCallId is extracted from the RunnableConfig passed by LangChain\n */\n const createToolFunction = (action: string) => {\n return async (\n args: Record<string, unknown>,\n config?: { toolCall?: { id?: string } }\n ): Promise<string> => {\n const toolCallId =\n config?.toolCall?.id ??\n `tool_${Date.now()}_${Math.random().toString(36).slice(2)}`;\n\n // Create marker for extension\n const marker: BrowserToolResponse = {\n requiresBrowserExecution: true,\n action,\n args,\n toolCallId,\n };\n\n // If no callback, return marker immediately (extension handles via SSE interception)\n if (!waitForResult) {\n return JSON.stringify(marker);\n }\n\n // With callback: wait for actual results from extension\n // The marker is still returned initially via SSE, but we wait for the callback\n try {\n const result = await waitForResult(action, args, toolCallId);\n return formatResultForLLM(result, action);\n } catch (error) {\n const errorMessage =\n error instanceof Error ? error.message : String(error);\n return `Browser action \"${action}\" failed: ${errorMessage}`;\n }\n };\n };\n\n // browser_click\n tools.push(\n tool(createToolFunction('click'), {\n name: EBrowserTools.CLICK,\n description: `Click an element on the current web page by its index number.\nThe element list shows clickable items like: [0]<button>Submit</button> [1]<a href=\"/home\">Home</a>\nUse the index number in brackets to click that element.\nAfter clicking, you receive an updated element list showing the new page state.`,\n schema: BrowserClickSchema,\n })\n );\n\n // browser_type\n tools.push(\n tool(createToolFunction('type'), {\n name: EBrowserTools.TYPE,\n description: `Type text into an input element on the page.\nFind the input element in the list by its index (e.g., [5]<input placeholder=\"Search\">).\nSet pressEnter: true to submit forms after typing.\nAfter typing, you receive an updated element list.`,\n schema: BrowserTypeSchema,\n })\n );\n\n // browser_navigate\n tools.push(\n tool(createToolFunction('navigate'), {\n name: EBrowserTools.NAVIGATE,\n description: `Navigate to a URL. Always include the full URL with https://.\nAfter navigation, you receive the new page's element list.`,\n schema: BrowserNavigateSchema,\n })\n );\n\n // browser_scroll\n tools.push(\n tool(createToolFunction('scroll'), {\n name: EBrowserTools.SCROLL,\n description: `Scroll the page to reveal more content.\nUse 'down' to scroll down, 'up' to scroll up.\nAfter scrolling, you receive an updated element list with newly visible elements.`,\n schema: BrowserScrollSchema,\n })\n );\n\n // browser_extract\n tools.push(\n tool(createToolFunction('extract'), {\n name: EBrowserTools.EXTRACT,\n description: `Extract content from the current page.\nReturns page URL, title, and element list.`,\n schema: BrowserExtractSchema,\n })\n );\n\n // browser_hover\n tools.push(\n tool(createToolFunction('hover'), {\n name: EBrowserTools.HOVER,\n description: `Hover over an element to reveal tooltips, dropdowns, or other hover-triggered content.\nAfter hovering, you receive an updated element list with any newly revealed elements.`,\n schema: BrowserHoverSchema,\n })\n );\n\n // browser_wait\n tools.push(\n tool(createToolFunction('wait'), {\n name: EBrowserTools.WAIT,\n description: `Wait for a specified duration for page content to load.\nUse this after actions that trigger async content loading.\nAfter waiting, you receive an updated element list.`,\n schema: BrowserWaitSchema,\n })\n );\n\n // browser_back\n tools.push(\n tool(createToolFunction('back'), {\n name: EBrowserTools.BACK,\n description: `Go back to the previous page in browser history.\nAfter going back, you receive the previous page's element list.`,\n schema: BrowserBackSchema,\n })\n );\n\n // browser_screenshot\n tools.push(\n tool(createToolFunction('screenshot'), {\n name: EBrowserTools.SCREENSHOT,\n description: `Capture a screenshot of the current page.\nReturns the page state with a note that screenshot was displayed to the user.\nUse browser_get_page_state to get the element list for automation.`,\n schema: BrowserScreenshotSchema,\n })\n );\n\n // browser_get_page_state\n tools.push(\n tool(createToolFunction('get_page_state'), {\n name: EBrowserTools.GET_PAGE_STATE,\n description: `Get the current page state including URL, title, and all interactive elements.\nUse this at the start of a task to see what elements are available.\nReturns a text list of elements with their index numbers for interaction.`,\n schema: BrowserGetPageStateSchema,\n })\n );\n\n return tools;\n}\n"],"names":[],"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,I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C,IAAI,CACR,IAAI,CAAC,kBAAkB,CAAC,gBAAgB,CAAC,EAAE;QACzC,IAAI,EAAE,aAAa,CAAC,cAAc;AAClC,QAAA,WAAW,EAAE,CAAA;;AAEuD,yEAAA,CAAA;AACpE,QAAA,MAAM,EAAE,yBAAyB;AAClC,KAAA,CAAC,CACH;AAED,IAAA,OAAO,KAAK;AACd;;;;"}
1
+ {"version":3,"file":"BrowserTools.mjs","sources":["../../../src/tools/BrowserTools.ts"],"sourcesContent":["import { z } from 'zod';\nimport { tool, DynamicStructuredTool } from '@langchain/core/tools';\nimport type * as _t from '@/types';\n\n/**\n * Browser tool names - keep in sync with ranger-browser extension\n * These tools execute locally in the browser extension, NOT on the server\n */\nexport const EBrowserTools = {\n CLICK: 'browser_click',\n TYPE: 'browser_type',\n NAVIGATE: 'browser_navigate',\n SCROLL: 'browser_scroll',\n EXTRACT: 'browser_extract',\n HOVER: 'browser_hover',\n WAIT: 'browser_wait',\n BACK: 'browser_back',\n SCREENSHOT: 'browser_screenshot',\n GET_PAGE_STATE: 'browser_get_page_state',\n} as const;\n\nexport type BrowserToolName =\n (typeof EBrowserTools)[keyof typeof EBrowserTools];\n\n/**\n * Callback function type for waiting on browser action results\n * This allows the server (Ranger) to provide a callback that waits for the extension\n * to POST results back to the server before returning to the LLM.\n *\n * @param action - The browser action (click, type, navigate, etc.)\n * @param args - Arguments for the action\n * @param toolCallId - Unique ID for this tool call (from config.toolCall.id)\n * @returns Promise that resolves with the actual browser result (page state, etc.)\n */\nexport type BrowserToolCallback = (\n action: string,\n args: Record<string, unknown>,\n toolCallId: string\n) => Promise<BrowserActionResult>;\n\n/**\n * Result returned from browser action execution\n */\nexport interface BrowserActionResult {\n success: boolean;\n url?: string;\n title?: string;\n elementList?: string; // Text-based element list\n error?: string;\n screenshot?: string; // Base64 screenshot (if requested)\n}\n\n/**\n * Check if browser capability is available based on request headers or context\n * The browser extension sets these headers when connected:\n * - X-Ranger-Browser-Extension: true\n * - X-Ranger-Browser-Capable: true\n */\nexport function hasBrowserCapability(req?: {\n headers?: Record<string, string | string[] | undefined>;\n}): boolean {\n if (!req?.headers) {\n return false;\n }\n\n const browserExtension = req.headers['x-ranger-browser-extension'];\n const browserCapable = req.headers['x-ranger-browser-capable'];\n\n return browserExtension === 'true' || browserCapable === 'true';\n}\n\n// Tool schemas\nconst BrowserClickSchema = z.object({\n index: z\n .number()\n .describe(\n 'The [index] of the element to click. Use fieldLabel attribute to identify the correct element when multiple similar elements exist (e.g., fieldLabel=\"To\" vs fieldLabel=\"Cc\" for email fields).'\n ),\n});\n\nconst BrowserTypeSchema = z.object({\n index: z\n .number()\n .describe('The [index] of the input element to type into. IMPORTANT: Check fieldLabel attribute to ensure you are typing into the correct field (e.g., fieldLabel=\"To\" for recipient, fieldLabel=\"Subject\" for subject line).'),\n text: z.string().describe('The text to type into the element'),\n pressEnter: z\n .boolean()\n .optional()\n .describe('Whether to press Enter after typing (useful for search forms)'),\n});\n\nconst BrowserNavigateSchema = z.object({\n url: z\n .string()\n .describe('The full URL to navigate to (must include https://)'),\n});\n\nconst BrowserScrollSchema = z.object({\n direction: z\n .enum(['up', 'down', 'left', 'right'])\n .describe('Direction to scroll'),\n amount: z\n .number()\n .optional()\n .describe('Pixels to scroll (default: one viewport height)'),\n});\n\nconst BrowserExtractSchema = z.object({\n query: z\n .string()\n .optional()\n .describe('Optional: specific content to extract from the page'),\n});\n\nconst BrowserHoverSchema = z.object({\n index: z.number().describe('The index number of the element to hover over'),\n});\n\nconst BrowserWaitSchema = z.object({\n duration: z\n .number()\n .optional()\n .describe('Milliseconds to wait (default: 1000)'),\n});\n\nconst BrowserBackSchema = z.object({});\n\nconst BrowserScreenshotSchema = z.object({});\n\nconst BrowserGetPageStateSchema = z.object({});\n\n/**\n * Browser tool response interface\n * This is what the extension returns after executing the action\n */\nexport interface BrowserToolResponse {\n requiresBrowserExecution: true;\n action: string;\n args: Record<string, unknown>;\n toolCallId?: string; // Added to help extension correlate with callback\n}\n\n/**\n * Options for creating browser tools\n */\nexport interface CreateBrowserToolsOptions {\n /**\n * Optional callback that waits for browser action results.\n * When provided, tools will await this callback to get actual results from the extension.\n * When not provided, tools return markers immediately (for non-server contexts).\n */\n waitForResult?: BrowserToolCallback;\n}\n\n/**\n * Format browser action result for LLM consumption\n */\nfunction formatResultForLLM(\n result: BrowserActionResult,\n action: string\n): string {\n if (!result.success && result.error) {\n return `Browser action \"${action}\" failed: ${result.error}`;\n }\n\n const parts: string[] = [];\n\n if (result.url != null && result.url !== '') {\n parts.push(`**Current URL:** ${result.url}`);\n }\n if (result.title != null && result.title !== '') {\n parts.push(`**Page Title:** ${result.title}`);\n }\n if (result.elementList != null && result.elementList !== '') {\n // Add hint about fieldLabel for form interactions\n parts.push(`\\n**Interactive Elements** (use fieldLabel to identify form fields):\\n${result.elementList}`);\n }\n if (result.screenshot != null && result.screenshot !== '') {\n parts.push('\\n[Screenshot captured and displayed to user]');\n }\n\n if (parts.length === 0) {\n return `Browser action \"${action}\" completed successfully.`;\n }\n\n return parts.join('\\n');\n}\n\n/**\n * Create browser tools with optional callback for waiting on results\n *\n * When waitForResult callback is provided:\n * 1. Tool returns marker that triggers extension\n * 2. Tool then awaits callback to get actual results\n * 3. Returns real page state to LLM\n *\n * When no callback:\n * 1. Tool returns marker only (for non-server contexts)\n *\n * NOTE: These tools use TEXT-BASED element lists, NOT screenshots\n * Screenshots would be 100K+ tokens each - element lists are ~100 tokens\n */\nexport function createBrowserTools(\n options?: CreateBrowserToolsOptions\n): DynamicStructuredTool[] {\n const { waitForResult } = options || {};\n const tools: DynamicStructuredTool[] = [];\n\n /**\n * Helper to create tool function that optionally waits for results\n * The toolCallId is extracted from the RunnableConfig passed by LangChain\n */\n const createToolFunction = (action: string) => {\n return async (\n args: Record<string, unknown>,\n config?: { toolCall?: { id?: string } }\n ): Promise<string> => {\n const toolCallId =\n config?.toolCall?.id ??\n `tool_${Date.now()}_${Math.random().toString(36).slice(2)}`;\n\n // Create marker for extension\n const marker: BrowserToolResponse = {\n requiresBrowserExecution: true,\n action,\n args,\n toolCallId,\n };\n\n // If no callback, return marker immediately (extension handles via SSE interception)\n if (!waitForResult) {\n return JSON.stringify(marker);\n }\n\n // With callback: wait for actual results from extension\n // The marker is still returned initially via SSE, but we wait for the callback\n try {\n const result = await waitForResult(action, args, toolCallId);\n return formatResultForLLM(result, action);\n } catch (error) {\n const errorMessage =\n error instanceof Error ? error.message : String(error);\n return `Browser action \"${action}\" failed: ${errorMessage}`;\n }\n };\n };\n\n // browser_click\n tools.push(\n tool(createToolFunction('click'), {\n name: EBrowserTools.CLICK,\n description: 'Click element by [index]. Use fieldLabel attribute to identify correct element. Skip disabled elements.',\n schema: BrowserClickSchema,\n })\n );\n\n // browser_type\n tools.push(\n tool(createToolFunction('type'), {\n name: EBrowserTools.TYPE,\n description: 'Type text into input by [index]. IMPORTANT: Use fieldLabel to identify the correct field (e.g., fieldLabel=\"To\" vs \"Cc\"). Use pressEnter:true to submit.',\n schema: BrowserTypeSchema,\n })\n );\n\n // browser_navigate\n tools.push(\n tool(createToolFunction('navigate'), {\n name: EBrowserTools.NAVIGATE,\n description: 'Navigate to URL (include https://). Returns new page element list.',\n schema: BrowserNavigateSchema,\n })\n );\n\n // browser_scroll\n tools.push(\n tool(createToolFunction('scroll'), {\n name: EBrowserTools.SCROLL,\n description: 'Scroll page (up/down/left/right). Returns updated element list.',\n schema: BrowserScrollSchema,\n })\n );\n\n // browser_extract\n tools.push(\n tool(createToolFunction('extract'), {\n name: EBrowserTools.EXTRACT,\n description: 'Extract page content. Returns URL, title, and element list.',\n schema: BrowserExtractSchema,\n })\n );\n\n // browser_hover\n tools.push(\n tool(createToolFunction('hover'), {\n name: EBrowserTools.HOVER,\n description: 'Hover element by [index] to reveal menus/tooltips. Returns updated element list.',\n schema: BrowserHoverSchema,\n })\n );\n\n // browser_wait\n tools.push(\n tool(createToolFunction('wait'), {\n name: EBrowserTools.WAIT,\n description: 'Wait for async content to load. Returns updated element list.',\n schema: BrowserWaitSchema,\n })\n );\n\n // browser_back\n tools.push(\n tool(createToolFunction('back'), {\n name: EBrowserTools.BACK,\n description: 'Go back in browser history. Returns previous page element list.',\n schema: BrowserBackSchema,\n })\n );\n\n // browser_screenshot\n tools.push(\n tool(createToolFunction('screenshot'), {\n name: EBrowserTools.SCREENSHOT,\n description: 'Capture screenshot. Displayed to user. Use get_page_state for automation.',\n schema: BrowserScreenshotSchema,\n })\n );\n\n // browser_get_page_state\n tools.push(\n tool(createToolFunction('get_page_state'), {\n name: EBrowserTools.GET_PAGE_STATE,\n description: 'Get page URL, title, and interactive elements with [index] for actions. Start here.',\n schema: BrowserGetPageStateSchema,\n })\n );\n\n return tools;\n}\n"],"names":[],"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;;AAkC1C;;;;;AAKG;AACG,SAAU,oBAAoB,CAAC,GAEpC,EAAA;AACC,IAAA,IAAI,CAAC,GAAG,EAAE,OAAO,EAAE;AACjB,QAAA,OAAO,KAAK;;IAGd,MAAM,gBAAgB,GAAG,GAAG,CAAC,OAAO,CAAC,4BAA4B,CAAC;IAClE,MAAM,cAAc,GAAG,GAAG,CAAC,OAAO,CAAC,0BAA0B,CAAC;AAE9D,IAAA,OAAO,gBAAgB,KAAK,MAAM,IAAI,cAAc,KAAK,MAAM;AACjE;AAEA;AACA,MAAM,kBAAkB,GAAG,CAAC,CAAC,MAAM,CAAC;AAClC,IAAA,KAAK,EAAE;AACJ,SAAA,MAAM;SACN,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,CAAC,IAAI;AACxB,QAAA,WAAW,EAAE,+DAA+D;AAC5E,QAAA,MAAM,EAAE,iBAAiB;AAC1B,KAAA,CAAC,CACH;;IAGD,KAAK,CAAC,IAAI,CACR,IAAI,CAAC,kBAAkB,CAAC,MAAM,CAAC,EAAE;QAC/B,IAAI,EAAE,aAAa,CAAC,IAAI;AACxB,QAAA,WAAW,EAAE,iEAAiE;AAC9E,QAAA,MAAM,EAAE,iBAAiB;AAC1B,KAAA,CAAC,CACH;;IAGD,KAAK,CAAC,IAAI,CACR,IAAI,CAAC,kBAAkB,CAAC,YAAY,CAAC,EAAE;QACrC,IAAI,EAAE,aAAa,CAAC,UAAU;AAC9B,QAAA,WAAW,EAAE,2EAA2E;AACxF,QAAA,MAAM,EAAE,uBAAuB;AAChC,KAAA,CAAC,CACH;;IAGD,KAAK,CAAC,IAAI,CACR,IAAI,CAAC,kBAAkB,CAAC,gBAAgB,CAAC,EAAE;QACzC,IAAI,EAAE,aAAa,CAAC,cAAc;AAClC,QAAA,WAAW,EAAE,qFAAqF;AAClG,QAAA,MAAM,EAAE,yBAAyB;AAClC,KAAA,CAAC,CACH;AAED,IAAA,OAAO,KAAK;AACd;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "illuma-agents",
3
- "version": "1.0.33",
3
+ "version": "1.0.35",
4
4
  "main": "./dist/cjs/main.cjs",
5
5
  "module": "./dist/esm/main.mjs",
6
6
  "types": "./dist/types/index.d.ts",
@@ -74,14 +74,14 @@ const BrowserClickSchema = z.object({
74
74
  index: z
75
75
  .number()
76
76
  .describe(
77
- 'The index number [0], [1], etc. of the element to click from the page state element list'
77
+ 'The [index] of the element to click. Use fieldLabel attribute to identify the correct element when multiple similar elements exist (e.g., fieldLabel="To" vs fieldLabel="Cc" for email fields).'
78
78
  ),
79
79
  });
80
80
 
81
81
  const BrowserTypeSchema = z.object({
82
82
  index: z
83
83
  .number()
84
- .describe('The index number of the input element to type into'),
84
+ .describe('The [index] of the input element to type into. IMPORTANT: Check fieldLabel attribute to ensure you are typing into the correct field (e.g., fieldLabel="To" for recipient, fieldLabel="Subject" for subject line).'),
85
85
  text: z.string().describe('The text to type into the element'),
86
86
  pressEnter: z
87
87
  .boolean()
@@ -172,7 +172,8 @@ function formatResultForLLM(
172
172
  parts.push(`**Page Title:** ${result.title}`);
173
173
  }
174
174
  if (result.elementList != null && result.elementList !== '') {
175
- parts.push(`\n**Interactive Elements:**\n${result.elementList}`);
175
+ // Add hint about fieldLabel for form interactions
176
+ parts.push(`\n**Interactive Elements** (use fieldLabel to identify form fields):\n${result.elementList}`);
176
177
  }
177
178
  if (result.screenshot != null && result.screenshot !== '') {
178
179
  parts.push('\n[Screenshot captured and displayed to user]');
@@ -248,10 +249,7 @@ export function createBrowserTools(
248
249
  tools.push(
249
250
  tool(createToolFunction('click'), {
250
251
  name: EBrowserTools.CLICK,
251
- description: `Click an element on the current web page by its index number.
252
- The element list shows clickable items like: [0]<button>Submit</button> [1]<a href="/home">Home</a>
253
- Use the index number in brackets to click that element.
254
- After clicking, you receive an updated element list showing the new page state.`,
252
+ description: 'Click element by [index]. Use fieldLabel attribute to identify correct element. Skip disabled elements.',
255
253
  schema: BrowserClickSchema,
256
254
  })
257
255
  );
@@ -260,10 +258,7 @@ After clicking, you receive an updated element list showing the new page state.`
260
258
  tools.push(
261
259
  tool(createToolFunction('type'), {
262
260
  name: EBrowserTools.TYPE,
263
- description: `Type text into an input element on the page.
264
- Find the input element in the list by its index (e.g., [5]<input placeholder="Search">).
265
- Set pressEnter: true to submit forms after typing.
266
- After typing, you receive an updated element list.`,
261
+ description: 'Type text into input by [index]. IMPORTANT: Use fieldLabel to identify the correct field (e.g., fieldLabel="To" vs "Cc"). Use pressEnter:true to submit.',
267
262
  schema: BrowserTypeSchema,
268
263
  })
269
264
  );
@@ -272,8 +267,7 @@ After typing, you receive an updated element list.`,
272
267
  tools.push(
273
268
  tool(createToolFunction('navigate'), {
274
269
  name: EBrowserTools.NAVIGATE,
275
- description: `Navigate to a URL. Always include the full URL with https://.
276
- After navigation, you receive the new page's element list.`,
270
+ description: 'Navigate to URL (include https://). Returns new page element list.',
277
271
  schema: BrowserNavigateSchema,
278
272
  })
279
273
  );
@@ -282,9 +276,7 @@ After navigation, you receive the new page's element list.`,
282
276
  tools.push(
283
277
  tool(createToolFunction('scroll'), {
284
278
  name: EBrowserTools.SCROLL,
285
- description: `Scroll the page to reveal more content.
286
- Use 'down' to scroll down, 'up' to scroll up.
287
- After scrolling, you receive an updated element list with newly visible elements.`,
279
+ description: 'Scroll page (up/down/left/right). Returns updated element list.',
288
280
  schema: BrowserScrollSchema,
289
281
  })
290
282
  );
@@ -293,8 +285,7 @@ After scrolling, you receive an updated element list with newly visible elements
293
285
  tools.push(
294
286
  tool(createToolFunction('extract'), {
295
287
  name: EBrowserTools.EXTRACT,
296
- description: `Extract content from the current page.
297
- Returns page URL, title, and element list.`,
288
+ description: 'Extract page content. Returns URL, title, and element list.',
298
289
  schema: BrowserExtractSchema,
299
290
  })
300
291
  );
@@ -303,8 +294,7 @@ Returns page URL, title, and element list.`,
303
294
  tools.push(
304
295
  tool(createToolFunction('hover'), {
305
296
  name: EBrowserTools.HOVER,
306
- description: `Hover over an element to reveal tooltips, dropdowns, or other hover-triggered content.
307
- After hovering, you receive an updated element list with any newly revealed elements.`,
297
+ description: 'Hover element by [index] to reveal menus/tooltips. Returns updated element list.',
308
298
  schema: BrowserHoverSchema,
309
299
  })
310
300
  );
@@ -313,9 +303,7 @@ After hovering, you receive an updated element list with any newly revealed elem
313
303
  tools.push(
314
304
  tool(createToolFunction('wait'), {
315
305
  name: EBrowserTools.WAIT,
316
- description: `Wait for a specified duration for page content to load.
317
- Use this after actions that trigger async content loading.
318
- After waiting, you receive an updated element list.`,
306
+ description: 'Wait for async content to load. Returns updated element list.',
319
307
  schema: BrowserWaitSchema,
320
308
  })
321
309
  );
@@ -324,8 +312,7 @@ After waiting, you receive an updated element list.`,
324
312
  tools.push(
325
313
  tool(createToolFunction('back'), {
326
314
  name: EBrowserTools.BACK,
327
- description: `Go back to the previous page in browser history.
328
- After going back, you receive the previous page's element list.`,
315
+ description: 'Go back in browser history. Returns previous page element list.',
329
316
  schema: BrowserBackSchema,
330
317
  })
331
318
  );
@@ -334,9 +321,7 @@ After going back, you receive the previous page's element list.`,
334
321
  tools.push(
335
322
  tool(createToolFunction('screenshot'), {
336
323
  name: EBrowserTools.SCREENSHOT,
337
- description: `Capture a screenshot of the current page.
338
- Returns the page state with a note that screenshot was displayed to the user.
339
- Use browser_get_page_state to get the element list for automation.`,
324
+ description: 'Capture screenshot. Displayed to user. Use get_page_state for automation.',
340
325
  schema: BrowserScreenshotSchema,
341
326
  })
342
327
  );
@@ -345,9 +330,7 @@ Use browser_get_page_state to get the element list for automation.`,
345
330
  tools.push(
346
331
  tool(createToolFunction('get_page_state'), {
347
332
  name: EBrowserTools.GET_PAGE_STATE,
348
- description: `Get the current page state including URL, title, and all interactive elements.
349
- Use this at the start of a task to see what elements are available.
350
- Returns a text list of elements with their index numbers for interaction.`,
333
+ description: 'Get page URL, title, and interactive elements with [index] for actions. Start here.',
351
334
  schema: BrowserGetPageStateSchema,
352
335
  })
353
336
  );