@empiricalrun/test-gen 0.42.4 → 0.42.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/CHANGELOG.md +39 -0
  2. package/dist/agent/browsing/utils.d.ts.map +1 -1
  3. package/dist/agent/browsing/utils.js +1 -0
  4. package/dist/agent/codegen/create-test-block.d.ts.map +1 -1
  5. package/dist/agent/codegen/create-test-block.js +4 -2
  6. package/dist/agent/codegen/lexical-scoped-vars.d.ts.map +1 -1
  7. package/dist/agent/codegen/lexical-scoped-vars.js +4 -6
  8. package/dist/agent/codegen/promptBuilder.d.ts +3 -0
  9. package/dist/agent/codegen/promptBuilder.d.ts.map +1 -0
  10. package/dist/agent/codegen/promptBuilder.js +44 -0
  11. package/dist/agent/codegen/repo-edit.js +1 -1
  12. package/dist/agent/master/action-tool-calls.d.ts +40 -0
  13. package/dist/agent/master/action-tool-calls.d.ts.map +1 -0
  14. package/dist/agent/master/action-tool-calls.js +83 -0
  15. package/dist/agent/master/element-annotation.d.ts +7 -2
  16. package/dist/agent/master/element-annotation.d.ts.map +1 -1
  17. package/dist/agent/master/element-annotation.js +13 -3
  18. package/dist/agent/master/next-action.d.ts +12 -14
  19. package/dist/agent/master/next-action.d.ts.map +1 -1
  20. package/dist/agent/master/next-action.js +62 -63
  21. package/dist/agent/master/run.d.ts.map +1 -1
  22. package/dist/agent/master/run.js +68 -51
  23. package/dist/agent/master/scroller.d.ts +15 -0
  24. package/dist/agent/master/scroller.d.ts.map +1 -0
  25. package/dist/agent/master/scroller.js +371 -0
  26. package/dist/agent/master/with-hints.d.ts.map +1 -1
  27. package/dist/agent/master/with-hints.js +4 -1
  28. package/dist/agent/utils.d.ts +2 -0
  29. package/dist/agent/utils.d.ts.map +1 -0
  30. package/dist/agent/utils.js +12 -0
  31. package/dist/bin/utils/platform/web/index.d.ts.map +1 -1
  32. package/dist/bin/utils/platform/web/index.js +2 -0
  33. package/dist/browser-injected-scripts/annotate-elements.js +122 -74
  34. package/dist/browser-injected-scripts/annotate-elements.spec.d.ts +2 -0
  35. package/dist/browser-injected-scripts/annotate-elements.spec.d.ts.map +1 -0
  36. package/dist/browser-injected-scripts/annotate-elements.spec.js +186 -0
  37. package/dist/browser-injected-scripts/annotate-elements.spec.ts +52 -26
  38. package/dist/evals/master-agent.evals.d.ts.map +1 -1
  39. package/dist/evals/master-agent.evals.js +5 -4
  40. package/dist/prompts/lib/ts-transformer.d.ts +4 -0
  41. package/dist/prompts/lib/ts-transformer.d.ts.map +1 -0
  42. package/dist/prompts/lib/ts-transformer.js +90 -0
  43. package/dist/prompts/lib/vitest-plugin.d.ts +8 -0
  44. package/dist/prompts/lib/vitest-plugin.d.ts.map +1 -0
  45. package/dist/prompts/lib/vitest-plugin.js +20 -0
  46. package/dist/session/index.d.ts.map +1 -1
  47. package/dist/session/index.js +4 -0
  48. package/package.json +10 -8
  49. package/playwright.config.ts +1 -1
  50. package/vitest.config.ts +5 -0
  51. package/browser-injected-scripts/annotate-elements.js +0 -491
  52. package/browser-injected-scripts/annotate-elements.spec.ts +0 -277
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createTestUsingMasterAgent = void 0;
4
4
  const llm_1 = require("@empiricalrun/llm");
5
5
  const actions_1 = require("../../actions");
6
- const fill_1 = require("../../actions/fill");
7
6
  const skill_1 = require("../../actions/skill");
8
7
  const utils_1 = require("../../actions/utils");
9
8
  const logger_1 = require("../../bin/logger");
@@ -15,6 +14,8 @@ const browsing_1 = require("../browsing");
15
14
  const utils_2 = require("../browsing/utils");
16
15
  const skills_retriever_1 = require("../codegen/skills-retriever");
17
16
  const run_time_planner_1 = require("../planner/run-time-planner");
17
+ const utils_3 = require("../utils");
18
+ const action_tool_calls_1 = require("./action-tool-calls");
18
19
  const element_annotation_1 = require("./element-annotation");
19
20
  const next_action_1 = require("./next-action");
20
21
  const with_hints_1 = require("./with-hints");
@@ -134,28 +135,30 @@ async function createTestUsingMasterAgent({ task, page, testCase, options, scope
134
135
  trace: masterAgentSpan,
135
136
  llm,
136
137
  options,
137
- pageScreenshot,
138
+ pageScreenshot: [pageScreenshot],
138
139
  actions,
140
+ actionTypes: action_tool_calls_1.ActionType,
139
141
  disableSkills,
142
+ page,
143
+ logger,
140
144
  });
141
145
  if (await (0, session_1.shouldStopSession)()) {
142
146
  break;
143
147
  }
144
148
  if (toolCall) {
145
- const args = JSON.parse(toolCall.function.arguments);
149
+ const args = (0, utils_3.parseJson)(toolCall.toolCallArgs);
146
150
  const masterAgentActionSpan = masterAgentSpan?.span({
147
151
  name: "master-agent-execute-action",
148
152
  });
149
153
  try {
150
154
  output = {
151
- action: JSON.parse(toolCall.function.arguments).action ||
152
- JSON.parse(toolCall.function.arguments).skill,
153
- reason: JSON.parse(toolCall.function.arguments).reason,
155
+ action: args.action || args.skill,
156
+ reason: args.reason,
154
157
  };
155
158
  void testGenReporter.sendMessage(output.reason);
156
159
  logger.log(`Next Action: ${output.action}`);
157
- if (toolCall.function.name === skill_1.SKILL_USAGE) {
158
- const code = await actions.executeAction(toolCall.function.name, args, masterAgentActionSpan);
160
+ if (toolCall.actionType === skill_1.SKILL_USAGE) {
161
+ const code = await actions.executeAction(toolCall.actionType, args, masterAgentActionSpan);
159
162
  if (code) {
160
163
  generatedCodeSteps.push(code);
161
164
  }
@@ -165,15 +168,27 @@ async function createTestUsingMasterAgent({ task, page, testCase, options, scope
165
168
  let hintsExecutionCompletion;
166
169
  let elementAnnotation;
167
170
  await page.waitForTimeout(2000);
168
- const actionType = JSON.parse(toolCall.function.arguments).action_type;
169
- let preference = "all";
170
- if (useActionSpecificAnnotations && (0, next_action_1.isValidActionType)(actionType)) {
171
+ const actionType = toolCall.actionType;
172
+ let preference = {
173
+ actionType: "all",
174
+ };
175
+ if (useActionSpecificAnnotations && (0, action_tool_calls_1.isValidActionType)(actionType)) {
171
176
  switch (actionType) {
172
- case fill_1.PLAYWRIGHT_FILL_ACTION_NAME:
173
- preference = "fill";
177
+ case action_tool_calls_1.ActionType.FILL:
178
+ preference = {
179
+ actionType: action_tool_calls_1.ActionType.FILL,
180
+ };
181
+ break;
182
+ case action_tool_calls_1.ActionType.ASSERT_TEXT:
183
+ preference = {
184
+ actionType: action_tool_calls_1.ActionType.ASSERT_TEXT,
185
+ assertionText: args.assertion_text,
186
+ };
174
187
  break;
175
188
  default:
176
- preference = "all";
189
+ preference = {
190
+ actionType: "all",
191
+ };
177
192
  }
178
193
  }
179
194
  let { annotationKeys, annotatedPageScreenshot, annotationBuffer } = await (0, element_annotation_1.getAnnotationKeys)({
@@ -181,42 +196,44 @@ async function createTestUsingMasterAgent({ task, page, testCase, options, scope
181
196
  preference,
182
197
  options,
183
198
  });
184
- const annotationMapString = annotationKeys
185
- ?.map((a) => `${a.elementID}:${a.text}`)
186
- .join("\n");
187
- // Provides the annotations for all the element present on screen
188
- // Also provides the annotation of element on which the action needs to be taken
189
- elementAnnotation = await (0, element_annotation_1.getElementAnnotation)({
190
- elementDescription: JSON.parse(toolCall.function.arguments)
191
- .element_description,
192
- annotations: annotationMapString,
193
- annotatedScreenshot: annotatedPageScreenshot,
194
- trace: masterAgentActionSpan,
195
- llm,
196
- });
197
- output.elementAnnotation = elementAnnotation;
198
- console.log("Output: ", output);
199
- await testGenReporter.sendCurrentView(annotationBuffer);
200
- const triggerHintsFlowSpan = masterAgentActionSpan?.span({
201
- name: "trigger-hints-flow",
202
- input: {
199
+ if (annotationKeys.length > 0) {
200
+ const annotationMapString = annotationKeys
201
+ ?.map((a) => `${a.elementID}:${a.text}`)
202
+ .join("\n");
203
+ // Provides the annotations for all the element present on screen
204
+ // Also provides the annotation of element on which the action needs to be taken
205
+ elementAnnotation = await (0, element_annotation_1.getElementAnnotation)({
206
+ elementDescription: args.element_description,
207
+ annotations: annotationMapString,
208
+ annotatedScreenshot: annotatedPageScreenshot,
209
+ trace: masterAgentActionSpan,
210
+ llm,
211
+ preference,
212
+ });
213
+ output.elementAnnotation = elementAnnotation;
214
+ console.log("Output: ", output);
215
+ await testGenReporter.sendCurrentView(annotationBuffer);
216
+ const triggerHintsFlowSpan = masterAgentActionSpan?.span({
217
+ name: "trigger-hints-flow",
218
+ input: {
219
+ outputFromGetNextAction: output,
220
+ generatedAnnotations: annotationKeys,
221
+ },
222
+ });
223
+ // Provides the action whether its a click, fill etc.
224
+ const result = await (0, with_hints_1.triggerHintsFlow)({
203
225
  outputFromGetNextAction: output,
204
226
  generatedAnnotations: annotationKeys,
205
- },
206
- });
207
- // Provides the action whether its a click, fill etc.
208
- const result = await (0, with_hints_1.triggerHintsFlow)({
209
- outputFromGetNextAction: output,
210
- generatedAnnotations: annotationKeys,
211
- actions,
212
- llm,
213
- trace: triggerHintsFlowSpan,
214
- });
215
- shouldTriggerHintsFlow = result.shouldTriggerHintsFlow;
216
- hintsExecutionCompletion = result.hintsExecutionCompletion;
217
- triggerHintsFlowSpan?.end({
218
- output: result,
219
- });
227
+ actions,
228
+ llm,
229
+ trace: triggerHintsFlowSpan,
230
+ });
231
+ shouldTriggerHintsFlow = result.shouldTriggerHintsFlow;
232
+ hintsExecutionCompletion = result.hintsExecutionCompletion;
233
+ triggerHintsFlowSpan?.end({
234
+ output: result,
235
+ });
236
+ }
220
237
  if (shouldTriggerHintsFlow && hintsExecutionCompletion) {
221
238
  const toolCalls = hintsExecutionCompletion?.tool_calls || [];
222
239
  for (const i in toolCalls) {
@@ -252,7 +269,7 @@ async function createTestUsingMasterAgent({ task, page, testCase, options, scope
252
269
  input: {
253
270
  action: output.action,
254
271
  reason: output.reason,
255
- type: toolCall.function.name,
272
+ type: toolCall.actionType,
256
273
  },
257
274
  output: {
258
275
  success: true,
@@ -262,7 +279,7 @@ async function createTestUsingMasterAgent({ task, page, testCase, options, scope
262
279
  disableSkills = false;
263
280
  }
264
281
  catch (e) {
265
- if (toolCall.function.name === skill_1.SKILL_USAGE) {
282
+ if (toolCall.actionType === skill_1.SKILL_USAGE) {
266
283
  logger.log("Disabling skill usage for next retry");
267
284
  disableSkills = true;
268
285
  }
@@ -270,7 +287,7 @@ async function createTestUsingMasterAgent({ task, page, testCase, options, scope
270
287
  input: {
271
288
  action: output.action,
272
289
  reason: output.reason,
273
- type: toolCall.function.name,
290
+ type: toolCall.actionType,
274
291
  },
275
292
  output: {
276
293
  error: true,
@@ -0,0 +1,15 @@
1
+ import { TraceClient } from "@empiricalrun/llm";
2
+ import { Page } from "playwright";
3
+ import { CustomLogger } from "../../bin/logger";
4
+ export type FrameReference = {
5
+ scrollPosition: number;
6
+ frameScreenshot: string;
7
+ };
8
+ export declare function scroller({ elementDescription, page, trace, frameReference, logger, }: {
9
+ elementDescription: string;
10
+ page: Page;
11
+ trace?: TraceClient;
12
+ frameReference?: FrameReference;
13
+ logger?: CustomLogger;
14
+ }): Promise<FrameReference[]>;
15
+ //# sourceMappingURL=scroller.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scroller.d.ts","sourceRoot":"","sources":["../../../src/agent/master/scroller.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAGrE,OAAO,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAElC,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAahD,MAAM,MAAM,cAAc,GAAG;IAC3B,cAAc,EAAE,MAAM,CAAC;IACvB,eAAe,EAAE,MAAM,CAAC;CACzB,CAAC;AAoZF,wBAAsB,QAAQ,CAAC,EAC7B,kBAAkB,EAClB,IAAI,EACJ,KAAK,EACL,cAAc,EACd,MAAM,GACP,EAAE;IACD,kBAAkB,EAAE,MAAM,CAAC;IAC3B,IAAI,EAAE,IAAI,CAAC;IACX,KAAK,CAAC,EAAE,WAAW,CAAC;IACpB,cAAc,CAAC,EAAE,cAAc,CAAC;IAChC,MAAM,CAAC,EAAE,YAAY,CAAC;CACvB,GAAG,OAAO,CAAC,cAAc,EAAE,CAAC,CA6D5B"}
@@ -0,0 +1,371 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.scroller = void 0;
4
+ const llm_1 = require("@empiricalrun/llm");
5
+ const vision_1 = require("@empiricalrun/llm/vision");
6
+ const constants_1 = require("../../constants");
7
+ const utils_1 = require("../utils");
8
+ const action_tool_calls_1 = require("./action-tool-calls");
9
+ const element_annotation_1 = require("./element-annotation");
10
+ let usedAnnotations = [];
11
+ // This checks whether scroll is possible or not
12
+ // If the div annotation is undefined, we check the scrollability of the page
13
+ // else we check for the element
14
+ async function isScrollable({ scrollDirection, page, divAnnotation, }) {
15
+ if (divAnnotation) {
16
+ return await page.evaluate(({ scrollDirection, divAnnotation }) => {
17
+ if (
18
+ // @ts-ignore
19
+ window?.annotationInstance?.annotations?.[divAnnotation]) {
20
+ let element = // @ts-ignore
21
+ window?.annotationInstance?.annotations?.[divAnnotation]?.node;
22
+ if (scrollDirection === "up") {
23
+ return element.scrollTop > 0;
24
+ }
25
+ else {
26
+ return (element.scrollHeight > element.clientHeight + element.scrollTop);
27
+ }
28
+ }
29
+ return false;
30
+ }, { scrollDirection, divAnnotation });
31
+ }
32
+ return await page.evaluate(({ scrollDirection }) => {
33
+ if (scrollDirection === "up") {
34
+ return window.scrollY > 0;
35
+ }
36
+ else {
37
+ return (document.documentElement.scrollHeight >
38
+ window.innerHeight + window.scrollY);
39
+ }
40
+ }, { scrollDirection });
41
+ }
42
+ // Performs scroll on page or the element
43
+ // If the div annotation is undefined, we scroll the page
44
+ // else we scroll the element
45
+ async function scroll({ scrollBy, page, direction, divAnnotation, logger, }) {
46
+ if (divAnnotation) {
47
+ logger?.log("Scrolling the div since element is not in view");
48
+ return await page.evaluate(({ scrollBy, direction, divAnnotation }) => {
49
+ if (
50
+ // @ts-ignore
51
+ window?.annotationInstance?.annotations?.[divAnnotation]) {
52
+ let element = // @ts-ignore
53
+ window?.annotationInstance?.annotations?.[divAnnotation]?.node;
54
+ let scrollHeight = scrollBy || element.clientHeight;
55
+ element.scrollBy(0, scrollHeight * (direction === "up" ? -1 : 1));
56
+ return scrollHeight;
57
+ }
58
+ return 0;
59
+ }, { scrollBy, direction, divAnnotation });
60
+ }
61
+ logger?.log("Scrolling the page since element is not in view");
62
+ return await page.evaluate(({ scrollBy, direction }) => {
63
+ let scrollHeight = scrollBy || window.innerHeight;
64
+ window.scrollBy(0, scrollHeight * (direction === "up" ? -1 : 1));
65
+ return scrollHeight;
66
+ }, { scrollBy, direction });
67
+ }
68
+ // Scrolls the page to top
69
+ async function scrollToTop(page) {
70
+ await page.evaluate(() => {
71
+ window.scrollTo({ top: 0 });
72
+ });
73
+ }
74
+ // Checks if the element is visible in the current frame
75
+ async function isElementVisibleInFrame({ elementDescription, page, trace, logger, }) {
76
+ const buffer = await page.screenshot();
77
+ const frameScreenshot = buffer.toString("base64");
78
+ const systemMessage = {
79
+ role: "system",
80
+ content: `
81
+ You are a web automation tool having extraordinary capabilities of going through the webpage screenshots. You are given a task to identify whether the element with given element description is present on the screen.
82
+ You need to make a decision whether the element is visible or not. Only consider an element to be visible if it's completely visible, otherwise respond false.`,
83
+ };
84
+ const userMessage = {
85
+ role: "user",
86
+ content: [
87
+ {
88
+ type: "text",
89
+ text: `
90
+ Element description:
91
+ ${elementDescription}
92
+
93
+ ----
94
+
95
+ Follow the instructions before responding:
96
+ - Scan through the content in the screenshot
97
+ - While scanning check whether there is any element for which the given element description matches.
98
+ - If it matches set is_visible as true
99
+ - else set is_visible as false
100
+ `,
101
+ },
102
+ {
103
+ type: "text",
104
+ text: "Screenshot",
105
+ },
106
+ {
107
+ type: "image_url",
108
+ image_url: {
109
+ url: (0, vision_1.imageFormatForProvider)(constants_1.DEFAULT_MODEL_PROVIDER, frameScreenshot),
110
+ },
111
+ },
112
+ ],
113
+ };
114
+ const tool = {
115
+ type: "function",
116
+ function: {
117
+ name: "is-element-visible",
118
+ description: "Is the element with given element description present in the screenshot",
119
+ parameters: {
120
+ type: "object",
121
+ properties: {
122
+ reason: {
123
+ type: "string",
124
+ description: "Explain why the element is marked as visible and its location. The reason should be clear and concise.",
125
+ },
126
+ is_visible: {
127
+ type: "boolean",
128
+ description: "Boolean value for whether the element is completely visible in the screenshot.",
129
+ },
130
+ },
131
+ required: ["reason", "is_visible"],
132
+ },
133
+ },
134
+ };
135
+ const messages = [
136
+ systemMessage,
137
+ userMessage,
138
+ ];
139
+ const scrollSpan = trace?.span({
140
+ name: "is-element-visible-after-scroll",
141
+ input: {
142
+ elementDescription,
143
+ messages,
144
+ },
145
+ });
146
+ const llm = new llm_1.LLM({
147
+ provider: constants_1.DEFAULT_MODEL_PROVIDER,
148
+ defaultModel: constants_1.DEFAULT_MODEL,
149
+ });
150
+ const completion = await llm.createChatCompletion({
151
+ messages,
152
+ modelParameters: {
153
+ ...constants_1.DEFAULT_MODEL_PARAMETERS,
154
+ tool_choice: "required",
155
+ temperature: 1,
156
+ },
157
+ trace: scrollSpan,
158
+ tools: [tool],
159
+ });
160
+ let isVisible = false;
161
+ const toolCall = completion?.tool_calls?.[0];
162
+ scrollSpan?.end({ output: toolCall });
163
+ if (toolCall) {
164
+ const args = (0, utils_1.parseJson)(toolCall.function.arguments);
165
+ isVisible = args.is_visible || false;
166
+ }
167
+ else {
168
+ logger?.error(`No tool call found in completion. [Trace](${trace?.getTraceUrl()})`);
169
+ }
170
+ return {
171
+ isVisible,
172
+ frameScreenshot,
173
+ };
174
+ }
175
+ // Returns the element annotation to scroll on
176
+ // if there is no element matching the description we return "NA"
177
+ async function getDivAnnotationToScrollOn({ elementDescription, page, trace, logger, }) {
178
+ const preference = {
179
+ actionType: action_tool_calls_1.ActionType.SCROLL,
180
+ };
181
+ let { annotationKeys, annotatedPageScreenshot } = await (0, element_annotation_1.getAnnotationKeys)({
182
+ page,
183
+ preference: {
184
+ actionType: action_tool_calls_1.ActionType.SCROLL,
185
+ },
186
+ options: {},
187
+ });
188
+ // Remove the used annotations from the list
189
+ annotationKeys = annotationKeys.filter((key) => !usedAnnotations.includes(key.elementID));
190
+ if (annotationKeys.length === 0) {
191
+ return;
192
+ }
193
+ const annotationKeysString = annotationKeys
194
+ ?.map((a) => `${a.elementID}:${a.text}`)
195
+ .join("\n");
196
+ const annotationsSpan = trace?.span({
197
+ name: "get-div-annotation",
198
+ input: {
199
+ elementDescription,
200
+ annotationKeys,
201
+ annotatedPageScreenshot,
202
+ preference,
203
+ },
204
+ });
205
+ const systemMessage = {
206
+ role: "system",
207
+ content: `
208
+ You are an expert in describing the images and it's content. You will be provided with an annotated screenshot where scrollable divs are annotated.
209
+
210
+ The annotation is done by drawing a red box around the element and a small yellow box on it which contains unique element id.
211
+
212
+ You are given a "Annotations" which contains list of unique element Id and description of the element separated by ":".
213
+
214
+ You are also given the description of the element which can be present inside the annotated element. The description includes information about how the element looks, it's position etc.
215
+
216
+ Your task is to provide the annotation of the div on which the scroll action needs to be performed based on whether the provided element is inside the div. You can use the description of the annotated element as well. E.g. text in the div contains the text as described in the element description.
217
+
218
+ You also need to extract the relevant information like the element text or position from the provided element description, this can be used to match the div content with the element.
219
+
220
+ If there is a match provide the annotation for the div else respond with "NA".
221
+
222
+ Follow steps to fulfil your task:
223
+ - If the provided "Annotations" is empty, respond with "NA"
224
+ - For each element Id, read the description for the div element
225
+ - If the description contains the element description or anything on similar lines
226
+ - Respond with the element Id
227
+ - If none of the description contains the element description respond with "NA"
228
+ - If the specified element Id is not found in the "Annotations" section, the response is invalid.
229
+ `,
230
+ };
231
+ const userMessage = {
232
+ role: "user",
233
+ content: [
234
+ {
235
+ type: "text",
236
+ text: `
237
+ Element description:
238
+ ${elementDescription}
239
+
240
+ Annotations:
241
+ ${annotationKeysString}`,
242
+ },
243
+ {
244
+ type: "image_url",
245
+ image_url: {
246
+ url: (0, vision_1.imageFormatForProvider)(constants_1.DEFAULT_MODEL_PROVIDER, annotatedPageScreenshot),
247
+ },
248
+ },
249
+ ],
250
+ };
251
+ const messages = [
252
+ systemMessage,
253
+ userMessage,
254
+ ];
255
+ const annotationToolAction = {
256
+ name: "element_annotation",
257
+ schema: {
258
+ type: "function",
259
+ function: {
260
+ name: "element_annotation",
261
+ description: "Handles annotations for elements",
262
+ parameters: {
263
+ type: "object",
264
+ properties: {
265
+ element: {
266
+ type: "string",
267
+ description: "Relevant information from the provided element description.",
268
+ },
269
+ reason: {
270
+ type: "string",
271
+ description: "Explain why this element is selected. The reason should be clear and align with the task or purpose.",
272
+ },
273
+ element_annotation: {
274
+ type: "string",
275
+ description: "Return the unique element ID for the element on which the action needs to be performed.",
276
+ },
277
+ },
278
+ required: ["element", "reason", "element_annotation"],
279
+ },
280
+ },
281
+ },
282
+ };
283
+ const llm = new llm_1.LLM({
284
+ provider: constants_1.DEFAULT_MODEL_PROVIDER,
285
+ defaultModel: constants_1.DEFAULT_MODEL,
286
+ });
287
+ const completion = await llm.createChatCompletion({
288
+ messages,
289
+ modelParameters: {
290
+ ...constants_1.DEFAULT_MODEL_PARAMETERS,
291
+ tool_choice: "required",
292
+ temperature: 1,
293
+ },
294
+ trace: annotationsSpan,
295
+ traceName: "get-element-from-action",
296
+ //@ts-ignore
297
+ tools: [annotationToolAction.schema],
298
+ });
299
+ const toolCall = completion?.tool_calls?.[0];
300
+ annotationsSpan?.end({ output: toolCall });
301
+ if (toolCall) {
302
+ const args = (0, utils_1.parseJson)(toolCall.function.arguments);
303
+ const isAnnotationPresentInKeys = annotationKeys.some((annotation) => annotation.elementID === args.element_annotation);
304
+ if (args.element_annotation !== "NA" && isAnnotationPresentInKeys) {
305
+ usedAnnotations.push(args.element_annotation);
306
+ return args.element_annotation;
307
+ }
308
+ }
309
+ else {
310
+ logger?.error(`No tool call found in completion. [Trace](${trace?.getTraceUrl()})`);
311
+ }
312
+ return;
313
+ }
314
+ // Scrolls the page and returns the reference to the frame in which the element is visible
315
+ async function scroller({ elementDescription, page, trace, frameReference, logger, }) {
316
+ await scrollToTop(page);
317
+ if (frameReference) {
318
+ await scroll({
319
+ scrollBy: frameReference.scrollPosition,
320
+ page,
321
+ direction: "down",
322
+ });
323
+ return [frameReference];
324
+ }
325
+ let referenceImages = [];
326
+ let scrolledHeight = 0;
327
+ let isScrollAvailable = true;
328
+ const divAnnotation = await getDivAnnotationToScrollOn({
329
+ elementDescription,
330
+ page,
331
+ trace,
332
+ logger,
333
+ });
334
+ while (isScrollAvailable) {
335
+ // Perform action to check for visibility
336
+ const { isVisible, frameScreenshot } = await isElementVisibleInFrame({
337
+ elementDescription,
338
+ page,
339
+ trace,
340
+ logger,
341
+ });
342
+ if (isVisible) {
343
+ usedAnnotations = [];
344
+ referenceImages.push({
345
+ scrollPosition: scrolledHeight,
346
+ frameScreenshot,
347
+ });
348
+ // TODO: remove this to support multiple images
349
+ break;
350
+ }
351
+ isScrollAvailable = await isScrollable({
352
+ scrollDirection: "down",
353
+ page,
354
+ divAnnotation,
355
+ });
356
+ if (isScrollAvailable) {
357
+ scrolledHeight += await scroll({
358
+ page,
359
+ divAnnotation,
360
+ direction: "down",
361
+ logger,
362
+ });
363
+ }
364
+ }
365
+ if (referenceImages.length === 0) {
366
+ logger?.log("Element not found in the current scroll");
367
+ }
368
+ await (0, llm_1.flushAllTraces)();
369
+ return referenceImages;
370
+ }
371
+ exports.scroller = scroller;
@@ -1 +1 @@
1
- {"version":3,"file":"with-hints.d.ts","sourceRoot":"","sources":["../../../src/agent/master/with-hints.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAErD,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAQlD,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAEnD,eAAO,MAAM,0BAA0B;iBAMxB,OAAO,8BAA8B;;oBAElC,MAAM;6BACG,MAAM;MAC7B,MAAM,GAAG,OAAO,yBAAyB,EAiC5C,CAAC;AAEF,eAAO,MAAM,gBAAgB;6BAOF;QACvB,MAAM,EAAE,MAAM,CAAC;QACf,iBAAiB,CAAC,EAAE,MAAM,CAAC;KAC5B;0BACqB,OAAO,MAAM,EAAE,GAAG,CAAC;aAChC,iBAAiB;SACrB,GAAG;;MAEN,QAAQ;IACV,sBAAsB,EAAE,OAAO,CAAC;IAChC,wBAAwB,EAAE,OAAO,qBAAqB,GAAG,SAAS,CAAC;CACpE,CAuGA,CAAC"}
1
+ {"version":3,"file":"with-hints.d.ts","sourceRoot":"","sources":["../../../src/agent/master/with-hints.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAErD,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AASlD,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAEnD,eAAO,MAAM,0BAA0B;iBAMxB,OAAO,8BAA8B;;oBAElC,MAAM;6BACG,MAAM;MAC7B,MAAM,GAAG,OAAO,yBAAyB,EAiC5C,CAAC;AAEF,eAAO,MAAM,gBAAgB;6BAOF;QACvB,MAAM,EAAE,MAAM,CAAC;QACf,iBAAiB,CAAC,EAAE,MAAM,CAAC;KAC5B;0BACqB,OAAO,MAAM,EAAE,GAAG,CAAC;aAChC,iBAAiB;SACrB,GAAG;;MAEN,QAAQ;IACV,sBAAsB,EAAE,OAAO,CAAC;IAChC,wBAAwB,EAAE,OAAO,qBAAqB,GAAG,SAAS,CAAC;CACpE,CAyGA,CAAC"}
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.triggerHintsFlow = exports.getUserMessageWithForHints = void 0;
4
4
  const vision_1 = require("@empiricalrun/llm/vision");
5
+ const assert_1 = require("../../actions/assert");
5
6
  const click_1 = require("../../actions/click");
6
7
  const fill_1 = require("../../actions/fill");
7
8
  const hover_1 = require("../../actions/hover");
@@ -92,7 +93,9 @@ const triggerHintsFlow = async ({ outputFromGetNextAction, generatedAnnotations,
92
93
  });
93
94
  const canTriggerHintsFlow = completion?.tool_calls?.some((currentToolCall) => currentToolCall.function.name === fill_1.PLAYWRIGHT_FILL_ACTION_NAME ||
94
95
  currentToolCall.function.name === click_1.PLAYWRIGHT_CLICK_ACTION_NAME ||
95
- currentToolCall.function.name === hover_1.PLAYWRIGHT_HOVER_ACTION_NAME);
96
+ currentToolCall.function.name === hover_1.PLAYWRIGHT_HOVER_ACTION_NAME ||
97
+ currentToolCall.function.name ===
98
+ assert_1.PLAYWRIGHT_ASSERT_TEXT_VISIBILITY_ACTION_NAME);
96
99
  trace?.event({
97
100
  name: "can-trigger-hints-flow",
98
101
  output: {
@@ -0,0 +1,2 @@
1
+ export declare function parseJson(args: string): any;
2
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/agent/utils.ts"],"names":[],"mappings":"AAAA,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,OAMrC"}
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseJson = void 0;
4
+ function parseJson(args) {
5
+ try {
6
+ return JSON.parse(args);
7
+ }
8
+ catch (e) {
9
+ console.error("Failed to parse JSON", e);
10
+ }
11
+ }
12
+ exports.parseJson = parseJson;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/bin/utils/platform/web/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAMhD,OAAO,EAGL,IAAI,EAEJ,UAAU,EAEX,MAAM,UAAU,CAAC;AAGlB,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAE7C,eAAO,MAAM,gCAAgC,eAC/B,UAAU,KACrB,MAgBF,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,sBAAsB,CAAC,EACrC,YAAY,EACZ,MAAM,EACN,OAAO,GACR,EAAE;IACD,YAAY,EAAE,M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O,MAAM,iCAAiC,cACjC,MAAM,EAAE,gBACL,MAAM,sBAyBrB,CAAC;AAEF,wBAAsB,qBAAqB,CAAC,EAC1C,YAAY,EACZ,QAAQ,EACR,MAAM,GACP,EAAE;IACD,YAAY,EAAE,MAAM,CAAC;IACrB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,EAAE,CAAC;CAClB,iBAgDA;AAED,wBAAsB,uBAAuB,CAC3C,QAAQ,EAAE,MAAM,EAChB,cAAc,EAAE,MAAM,EAAE,iBAsBzB;AAED,wBAAgB,aAAa,CAAC,EAC5B,QAAQ,EACR,QAAQ,GACT,EAAE;IACD,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,QAAQ,CAAC;CACpB,WAYA;AAED,wBAAgB,mBAAmB,CAAC,EAClC,QAAQ,EACR,MAAM,GACP,EAAE;IACD,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,EAAE,CAAC;CAClB,UAOA;AAED,wBAAgB,+BAA+B,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,EAAE,CA4B5E;AAED,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAQnD"}
@@ -229,6 +229,8 @@ exports.stripAndPrependImports = stripAndPrependImports;
229
229
  async function lintErrors(filePath) {
230
230
  const eslint = new eslint_1.ESLint({
231
231
  fix: true,
232
+ // useEslintrc: false,
233
+ // overrideConfigFile: path.join(process.cwd(), ".eslintrc.js"),
232
234
  useEslintrc: true,
233
235
  });
234
236
  const [result] = await eslint.lintFiles(filePath);