@browserbasehq/orca 3.2.0-preview.4 → 3.2.0-preview.5

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 (195) hide show
  1. package/dist/cjs/lib/inference.d.ts +3 -1
  2. package/dist/cjs/lib/inference.js +2 -2
  3. package/dist/cjs/lib/inference.js.map +1 -1
  4. package/dist/cjs/lib/prompt.d.ts +1 -1
  5. package/dist/cjs/lib/prompt.js +11 -2
  6. package/dist/cjs/lib/prompt.js.map +1 -1
  7. package/dist/cjs/lib/v3/agent/AgentClient.d.ts +8 -0
  8. package/dist/cjs/lib/v3/agent/AgentClient.js +13 -0
  9. package/dist/cjs/lib/v3/agent/AgentClient.js.map +1 -1
  10. package/dist/cjs/lib/v3/agent/AnthropicCUAClient.js +1 -0
  11. package/dist/cjs/lib/v3/agent/AnthropicCUAClient.js.map +1 -1
  12. package/dist/cjs/lib/v3/agent/GoogleCUAClient.js +1 -0
  13. package/dist/cjs/lib/v3/agent/GoogleCUAClient.js.map +1 -1
  14. package/dist/cjs/lib/v3/agent/MicrosoftCUAClient.js +1 -0
  15. package/dist/cjs/lib/v3/agent/MicrosoftCUAClient.js.map +1 -1
  16. package/dist/cjs/lib/v3/agent/OpenAICUAClient.d.ts +4 -4
  17. package/dist/cjs/lib/v3/agent/OpenAICUAClient.js +62 -1
  18. package/dist/cjs/lib/v3/agent/OpenAICUAClient.js.map +1 -1
  19. package/dist/cjs/lib/v3/agent/prompts/agentSystemPrompt.d.ts +2 -2
  20. package/dist/cjs/lib/v3/agent/prompts/agentSystemPrompt.js +10 -11
  21. package/dist/cjs/lib/v3/agent/prompts/agentSystemPrompt.js.map +1 -1
  22. package/dist/cjs/lib/v3/agent/tools/fillform.d.ts +0 -1
  23. package/dist/cjs/lib/v3/agent/tools/fillform.js +7 -10
  24. package/dist/cjs/lib/v3/agent/tools/fillform.js.map +1 -1
  25. package/dist/cjs/lib/v3/agent/utils/captchaSolver.d.ts +76 -0
  26. package/dist/cjs/lib/v3/agent/utils/captchaSolver.js +175 -0
  27. package/dist/cjs/lib/v3/agent/utils/captchaSolver.js.map +1 -0
  28. package/dist/cjs/lib/v3/agent/utils/variables.d.ts +5 -0
  29. package/dist/cjs/lib/v3/agent/utils/variables.js +9 -0
  30. package/dist/cjs/lib/v3/agent/utils/variables.js.map +1 -1
  31. package/dist/cjs/lib/v3/api.js +2 -9
  32. package/dist/cjs/lib/v3/api.js.map +1 -1
  33. package/dist/cjs/lib/v3/flowlogger/EventStore.js +1 -1
  34. package/dist/cjs/lib/v3/flowlogger/EventStore.js.map +1 -1
  35. package/dist/cjs/lib/v3/handlers/observeHandler.js +2 -1
  36. package/dist/cjs/lib/v3/handlers/observeHandler.js.map +1 -1
  37. package/dist/cjs/lib/v3/handlers/v3AgentHandler.d.ts +2 -1
  38. package/dist/cjs/lib/v3/handlers/v3AgentHandler.js +110 -46
  39. package/dist/cjs/lib/v3/handlers/v3AgentHandler.js.map +1 -1
  40. package/dist/cjs/lib/v3/handlers/v3CuaAgentHandler.d.ts +5 -0
  41. package/dist/cjs/lib/v3/handlers/v3CuaAgentHandler.js +125 -1
  42. package/dist/cjs/lib/v3/handlers/v3CuaAgentHandler.js.map +1 -1
  43. package/dist/cjs/lib/v3/types/private/cache.d.ts +0 -1
  44. package/dist/cjs/lib/v3/types/private/cache.js.map +1 -1
  45. package/dist/cjs/lib/v3/types/private/handlers.d.ts +1 -0
  46. package/dist/cjs/lib/v3/types/private/handlers.js.map +1 -1
  47. package/dist/cjs/lib/v3/types/public/api.d.ts +24 -7
  48. package/dist/cjs/lib/v3/types/public/api.js +41 -14
  49. package/dist/cjs/lib/v3/types/public/api.js.map +1 -1
  50. package/dist/cjs/lib/v3/types/public/methods.d.ts +1 -0
  51. package/dist/cjs/lib/v3/types/public/methods.js.map +1 -1
  52. package/dist/cjs/lib/v3/types/public/variables.d.ts +7 -0
  53. package/dist/cjs/lib/v3/types/public/variables.js +22 -0
  54. package/dist/cjs/lib/v3/types/public/variables.js.map +1 -0
  55. package/dist/cjs/lib/v3/v3.d.ts +5 -0
  56. package/dist/cjs/lib/v3/v3.js +19 -4
  57. package/dist/cjs/lib/v3/v3.js.map +1 -1
  58. package/dist/cjs/lib/version.d.ts +1 -1
  59. package/dist/cjs/lib/version.js +1 -1
  60. package/dist/cjs/lib/version.js.map +1 -1
  61. package/dist/cjs/tests/integration/agent-captcha-autosolve.spec.d.ts +1 -0
  62. package/dist/cjs/tests/integration/agent-captcha-autosolve.spec.js +56 -0
  63. package/dist/cjs/tests/integration/agent-captcha-autosolve.spec.js.map +1 -0
  64. package/dist/cjs/tests/integration/agent-hybrid-mode.spec.js +6 -6
  65. package/dist/cjs/tests/integration/agent-hybrid-mode.spec.js.map +1 -1
  66. package/dist/cjs/tests/integration/timeouts.spec.js +1 -1
  67. package/dist/cjs/tests/integration/timeouts.spec.js.map +1 -1
  68. package/dist/cjs/tests/unit/agent-captcha-hooks.test.d.ts +1 -0
  69. package/dist/cjs/tests/unit/agent-captcha-hooks.test.js +285 -0
  70. package/dist/cjs/tests/unit/agent-captcha-hooks.test.js.map +1 -0
  71. package/dist/cjs/tests/unit/agent-execution-model.test.js +25 -3
  72. package/dist/cjs/tests/unit/agent-execution-model.test.js.map +1 -1
  73. package/dist/cjs/tests/unit/agent-system-prompt-variables.test.d.ts +1 -0
  74. package/dist/cjs/tests/unit/agent-system-prompt-variables.test.js +23 -0
  75. package/dist/cjs/tests/unit/agent-system-prompt-variables.test.js.map +1 -0
  76. package/dist/cjs/tests/unit/api-client-observe-variables.test.d.ts +1 -0
  77. package/dist/cjs/tests/unit/api-client-observe-variables.test.js +86 -0
  78. package/dist/cjs/tests/unit/api-client-observe-variables.test.js.map +1 -0
  79. package/dist/cjs/tests/unit/api-variables-schema.test.d.ts +1 -0
  80. package/dist/cjs/tests/unit/api-variables-schema.test.js +37 -0
  81. package/dist/cjs/tests/unit/api-variables-schema.test.js.map +1 -0
  82. package/dist/cjs/tests/unit/browserbase-session-accessors.test.js +20 -0
  83. package/dist/cjs/tests/unit/browserbase-session-accessors.test.js.map +1 -1
  84. package/dist/cjs/tests/unit/captcha-solver.test.d.ts +1 -0
  85. package/dist/cjs/tests/unit/captcha-solver.test.js +154 -0
  86. package/dist/cjs/tests/unit/captcha-solver.test.js.map +1 -0
  87. package/dist/cjs/tests/unit/flowlogger-eventstore.test.js +1 -1
  88. package/dist/cjs/tests/unit/flowlogger-eventstore.test.js.map +1 -1
  89. package/dist/cjs/tests/unit/openai-cua-client.test.d.ts +1 -0
  90. package/dist/cjs/tests/unit/openai-cua-client.test.js +71 -0
  91. package/dist/cjs/tests/unit/openai-cua-client.test.js.map +1 -0
  92. package/dist/cjs/tests/unit/prompt-observe-variables.test.d.ts +1 -0
  93. package/dist/cjs/tests/unit/prompt-observe-variables.test.js +19 -0
  94. package/dist/cjs/tests/unit/prompt-observe-variables.test.js.map +1 -0
  95. package/dist/cjs/tests/unit/public-api/public-types.test.js.map +1 -1
  96. package/dist/cjs/tests/unit/timeout-handlers.test.js +50 -0
  97. package/dist/cjs/tests/unit/timeout-handlers.test.js.map +1 -1
  98. package/dist/esm/lib/inference.d.ts +3 -1
  99. package/dist/esm/lib/inference.js +2 -2
  100. package/dist/esm/lib/inference.js.map +1 -1
  101. package/dist/esm/lib/prompt.d.ts +1 -1
  102. package/dist/esm/lib/prompt.js +11 -2
  103. package/dist/esm/lib/prompt.js.map +1 -1
  104. package/dist/esm/lib/v3/agent/AgentClient.d.ts +8 -0
  105. package/dist/esm/lib/v3/agent/AgentClient.js +13 -0
  106. package/dist/esm/lib/v3/agent/AgentClient.js.map +1 -1
  107. package/dist/esm/lib/v3/agent/AnthropicCUAClient.js +1 -0
  108. package/dist/esm/lib/v3/agent/AnthropicCUAClient.js.map +1 -1
  109. package/dist/esm/lib/v3/agent/GoogleCUAClient.js +1 -0
  110. package/dist/esm/lib/v3/agent/GoogleCUAClient.js.map +1 -1
  111. package/dist/esm/lib/v3/agent/MicrosoftCUAClient.js +1 -0
  112. package/dist/esm/lib/v3/agent/MicrosoftCUAClient.js.map +1 -1
  113. package/dist/esm/lib/v3/agent/OpenAICUAClient.d.ts +4 -4
  114. package/dist/esm/lib/v3/agent/OpenAICUAClient.js +62 -1
  115. package/dist/esm/lib/v3/agent/OpenAICUAClient.js.map +1 -1
  116. package/dist/esm/lib/v3/agent/prompts/agentSystemPrompt.d.ts +2 -2
  117. package/dist/esm/lib/v3/agent/prompts/agentSystemPrompt.js +10 -11
  118. package/dist/esm/lib/v3/agent/prompts/agentSystemPrompt.js.map +1 -1
  119. package/dist/esm/lib/v3/agent/tools/fillform.d.ts +0 -1
  120. package/dist/esm/lib/v3/agent/tools/fillform.js +7 -10
  121. package/dist/esm/lib/v3/agent/tools/fillform.js.map +1 -1
  122. package/dist/esm/lib/v3/agent/utils/captchaSolver.d.ts +76 -0
  123. package/dist/esm/lib/v3/agent/utils/captchaSolver.js +171 -0
  124. package/dist/esm/lib/v3/agent/utils/captchaSolver.js.map +1 -0
  125. package/dist/esm/lib/v3/agent/utils/variables.d.ts +5 -0
  126. package/dist/esm/lib/v3/agent/utils/variables.js +8 -0
  127. package/dist/esm/lib/v3/agent/utils/variables.js.map +1 -1
  128. package/dist/esm/lib/v3/api.js +2 -9
  129. package/dist/esm/lib/v3/api.js.map +1 -1
  130. package/dist/esm/lib/v3/flowlogger/EventStore.js +1 -1
  131. package/dist/esm/lib/v3/flowlogger/EventStore.js.map +1 -1
  132. package/dist/esm/lib/v3/handlers/observeHandler.js +2 -1
  133. package/dist/esm/lib/v3/handlers/observeHandler.js.map +1 -1
  134. package/dist/esm/lib/v3/handlers/v3AgentHandler.d.ts +2 -1
  135. package/dist/esm/lib/v3/handlers/v3AgentHandler.js +110 -46
  136. package/dist/esm/lib/v3/handlers/v3AgentHandler.js.map +1 -1
  137. package/dist/esm/lib/v3/handlers/v3CuaAgentHandler.d.ts +5 -0
  138. package/dist/esm/lib/v3/handlers/v3CuaAgentHandler.js +125 -1
  139. package/dist/esm/lib/v3/handlers/v3CuaAgentHandler.js.map +1 -1
  140. package/dist/esm/lib/v3/types/private/cache.d.ts +0 -1
  141. package/dist/esm/lib/v3/types/private/cache.js.map +1 -1
  142. package/dist/esm/lib/v3/types/private/handlers.d.ts +1 -0
  143. package/dist/esm/lib/v3/types/private/handlers.js.map +1 -1
  144. package/dist/esm/lib/v3/types/public/api.d.ts +24 -7
  145. package/dist/esm/lib/v3/types/public/api.js +36 -12
  146. package/dist/esm/lib/v3/types/public/api.js.map +1 -1
  147. package/dist/esm/lib/v3/types/public/methods.d.ts +1 -0
  148. package/dist/esm/lib/v3/types/public/methods.js.map +1 -1
  149. package/dist/esm/lib/v3/types/public/variables.d.ts +7 -0
  150. package/dist/esm/lib/v3/types/public/variables.js +19 -0
  151. package/dist/esm/lib/v3/types/public/variables.js.map +1 -0
  152. package/dist/esm/lib/v3/v3.d.ts +5 -0
  153. package/dist/esm/lib/v3/v3.js +19 -4
  154. package/dist/esm/lib/v3/v3.js.map +1 -1
  155. package/dist/esm/lib/version.d.ts +1 -1
  156. package/dist/esm/lib/version.js +1 -1
  157. package/dist/esm/lib/version.js.map +1 -1
  158. package/dist/esm/tests/integration/agent-captcha-autosolve.spec.d.ts +1 -0
  159. package/dist/esm/tests/integration/agent-captcha-autosolve.spec.js +54 -0
  160. package/dist/esm/tests/integration/agent-captcha-autosolve.spec.js.map +1 -0
  161. package/dist/esm/tests/integration/agent-hybrid-mode.spec.js +6 -6
  162. package/dist/esm/tests/integration/agent-hybrid-mode.spec.js.map +1 -1
  163. package/dist/esm/tests/integration/timeouts.spec.js +1 -1
  164. package/dist/esm/tests/integration/timeouts.spec.js.map +1 -1
  165. package/dist/esm/tests/unit/agent-captcha-hooks.test.d.ts +1 -0
  166. package/dist/esm/tests/unit/agent-captcha-hooks.test.js +283 -0
  167. package/dist/esm/tests/unit/agent-captcha-hooks.test.js.map +1 -0
  168. package/dist/esm/tests/unit/agent-execution-model.test.js +25 -3
  169. package/dist/esm/tests/unit/agent-execution-model.test.js.map +1 -1
  170. package/dist/esm/tests/unit/agent-system-prompt-variables.test.d.ts +1 -0
  171. package/dist/esm/tests/unit/agent-system-prompt-variables.test.js +21 -0
  172. package/dist/esm/tests/unit/agent-system-prompt-variables.test.js.map +1 -0
  173. package/dist/esm/tests/unit/api-client-observe-variables.test.d.ts +1 -0
  174. package/dist/esm/tests/unit/api-client-observe-variables.test.js +84 -0
  175. package/dist/esm/tests/unit/api-client-observe-variables.test.js.map +1 -0
  176. package/dist/esm/tests/unit/api-variables-schema.test.d.ts +1 -0
  177. package/dist/esm/tests/unit/api-variables-schema.test.js +35 -0
  178. package/dist/esm/tests/unit/api-variables-schema.test.js.map +1 -0
  179. package/dist/esm/tests/unit/browserbase-session-accessors.test.js +20 -0
  180. package/dist/esm/tests/unit/browserbase-session-accessors.test.js.map +1 -1
  181. package/dist/esm/tests/unit/captcha-solver.test.d.ts +1 -0
  182. package/dist/esm/tests/unit/captcha-solver.test.js +152 -0
  183. package/dist/esm/tests/unit/captcha-solver.test.js.map +1 -0
  184. package/dist/esm/tests/unit/flowlogger-eventstore.test.js +1 -1
  185. package/dist/esm/tests/unit/flowlogger-eventstore.test.js.map +1 -1
  186. package/dist/esm/tests/unit/openai-cua-client.test.d.ts +1 -0
  187. package/dist/esm/tests/unit/openai-cua-client.test.js +69 -0
  188. package/dist/esm/tests/unit/openai-cua-client.test.js.map +1 -0
  189. package/dist/esm/tests/unit/prompt-observe-variables.test.d.ts +1 -0
  190. package/dist/esm/tests/unit/prompt-observe-variables.test.js +17 -0
  191. package/dist/esm/tests/unit/prompt-observe-variables.test.js.map +1 -0
  192. package/dist/esm/tests/unit/public-api/public-types.test.js.map +1 -1
  193. package/dist/esm/tests/unit/timeout-handlers.test.js +50 -0
  194. package/dist/esm/tests/unit/timeout-handlers.test.js.map +1 -1
  195. package/package.json +2 -2
@@ -1,6 +1,7 @@
1
1
  import { LogLine } from "./v3/types/public/logs.js";
2
2
  import { LLMClient } from "./v3/llm/LLMClient.js";
3
3
  import type { InferStagehandSchema, StagehandZodObject } from "./v3/zodCompat.js";
4
+ import type { Variables } from "./v3/types/public/agent.js";
4
5
  export type { LLMParsedResponse, LLMUsage } from "./v3/llm/LLMClient.js";
5
6
  export declare function extract<T extends StagehandZodObject>({ instruction, domElements, schema, llmClient, logger, userProvidedInstructions, logInferenceToFile, }: {
6
7
  instruction: string;
@@ -21,7 +22,7 @@ export declare function extract<T extends StagehandZodObject>({ instruction, dom
21
22
  cached_input_tokens: number;
22
23
  inference_time_ms: number;
23
24
  }>;
24
- export declare function observe({ instruction, domElements, llmClient, userProvidedInstructions, logger, logInferenceToFile, supportedActions, }: {
25
+ export declare function observe({ instruction, domElements, llmClient, userProvidedInstructions, logger, logInferenceToFile, supportedActions, variables, }: {
25
26
  instruction: string;
26
27
  domElements: string;
27
28
  llmClient: LLMClient;
@@ -29,6 +30,7 @@ export declare function observe({ instruction, domElements, llmClient, userProvi
29
30
  logger: (message: LogLine) => void;
30
31
  logInferenceToFile?: boolean;
31
32
  supportedActions?: string[];
33
+ variables?: Variables;
32
34
  }): Promise<{
33
35
  elements: {
34
36
  elementId: string;
@@ -145,7 +145,7 @@ async function extract({ instruction, domElements, schema, llmClient, logger, us
145
145
  inference_time_ms: totalInferenceTimeMs,
146
146
  };
147
147
  }
148
- async function observe({ instruction, domElements, llmClient, userProvidedInstructions, logger, logInferenceToFile = false, supportedActions, }) {
148
+ async function observe({ instruction, domElements, llmClient, userProvidedInstructions, logger, logInferenceToFile = false, supportedActions, variables, }) {
149
149
  const isGPT5 = llmClient.modelName.includes("gpt-5"); // TODO: remove this as we update support for gpt-5 configuration options
150
150
  const observeSchema = zod_1.z.object({
151
151
  elements: zod_1.z
@@ -169,7 +169,7 @@ async function observe({ instruction, domElements, llmClient, userProvidedInstru
169
169
  .describe("an array of accessible elements that match the instruction"),
170
170
  });
171
171
  const messages = [
172
- (0, prompt_js_1.buildObserveSystemPrompt)(userProvidedInstructions, supportedActions),
172
+ (0, prompt_js_1.buildObserveSystemPrompt)(userProvidedInstructions, supportedActions, variables),
173
173
  (0, prompt_js_1.buildObserveUserMessage)(instruction, domElements),
174
174
  ];
175
175
  let callTimestamp = "";
@@ -1 +1 @@
1
- {"version":3,"file":"inference.js","sourceRoot":"","sources":["../../../lib/inference.ts"],"names":[],"mappings":";;AA+BA,0BA8MC;AAED,0BAmJC;AAED,kBAuIC;AA3gBD,6BAAwB;AAGxB,4DAAqE;AACrE,2CAQqB;AACrB,iEAAgF;AAKhF,gEAA2E;AAK3E,SAAS,cAAc,CAAI,OAAmB,EAAE,SAAiB;IAC/D,OAAO,IAAA,8BAAW,EAChB,OAAO,EACP,IAAA,kCAAe,EAAC,YAAY,CAAC,EAC7B,OAAO,SAAS,EAAE,CACnB,CAAC;AACJ,CAAC;AAEM,KAAK,UAAU,OAAO,CAA+B,EAC1D,WAAW,EACX,WAAW,EACX,MAAM,EACN,SAAS,EACT,MAAM,EACN,wBAAwB,EACxB,kBAAkB,GAAG,KAAK,GAS3B;IACC,MAAM,cAAc,GAAG,OAAC,CAAC,MAAM,CAAC;QAC9B,QAAQ,EAAE,OAAC;aACR,MAAM,EAAE;aACR,QAAQ,CACP,oEAAoE,CACrE;QACH,SAAS,EAAE,OAAC;aACT,OAAO,EAAE;aACT,QAAQ,CACP,iHAAiH,CAClH;KACJ,CAAC,CAAC;IAKH,MAAM,gBAAgB,GAAG,SAAS,CAAC,IAAI,KAAK,WAAW,CAAC;IACxD,MAAM,MAAM,GAAG,SAAS,CAAC,SAAS,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,yEAAyE;IAE/H,MAAM,mBAAmB,GAAkB;QACzC,IAAA,oCAAwB,EAAC,gBAAgB,EAAE,wBAAwB,CAAC;QACpE,IAAA,kCAAsB,EAAC,WAAW,EAAE,WAAW,EAAE,gBAAgB,CAAC;KACnE,CAAC;IAEF,IAAI,eAAe,GAAG,EAAE,CAAC;IACzB,IAAI,oBAAoB,GAAG,EAAE,CAAC;IAC9B,IAAI,kBA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xB,cAAc,EAAE,QAAQ;YACxB,eAAe,EAAE,YAAY;YAC7B,aAAa,EAAE,YAAY;YAC3B,iBAAiB,EAAE,gBAAgB;YACnC,gBAAgB,EAAE,eAAe;YACjC,mBAAmB,EAAE,iBAAiB;YACtC,iBAAiB,EAAE,WAAW;SAC/B,CAAC,CAAC;IACL,CAAC;IAED,MAAM,aAAa,GAAG;QACpB,SAAS,EAAE,OAAO,CAAC,SAAS;QAC5B,WAAW,EAAE,MAAM,CAAC,OAAO,CAAC,WAAW,CAAC;QACxC,MAAM,EAAE,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC;QAC9B,SAAS,EAAE,OAAO,CAAC,SAAS;KAC7B,CAAC;IAEF,OAAO;QACL,OAAO,EAAE,aAAa;QACtB,aAAa,EAAE,YAAY;QAC3B,iBAAiB,EAAE,gBAAgB;QACnC,gBAAgB,EAAE,eAAe;QACjC,mBAAmB,EAAE,iBAAiB;QACtC,iBAAiB,EAAE,WAAW;QAC9B,OAAO,EAAE,OAAO,CAAC,OAAO;KACzB,CAAC;AACJ,CAAC","sourcesContent":["import { z } from \"zod\";\nimport { LogLine } from \"./v3/types/public/logs.js\";\nimport { ChatMessage, LLMClient } from \"./v3/llm/LLMClient.js\";\nimport { getEnvTimeoutMs, withTimeout } from \"./v3/timeoutConfig.js\";\nimport {\n buildActSystemPrompt,\n buildExtractSystemPrompt,\n buildExtractUserPrompt,\n buildMetadataPrompt,\n buildMetadataSystemPrompt,\n buildObserveSystemPrompt,\n buildObserveUserMessage,\n} from \"./prompt.js\";\nimport { appendSummary, writeTimestampedTxtFile } from \"./inferenceLogUtils.js\";\nimport type {\n InferStagehandSchema,\n StagehandZodObject,\n} from \"./v3/zodCompat.js\";\nimport { SupportedUnderstudyAction } from \"./v3/types/private/handlers.js\";\n\n// Re-export for backward compatibility\nexport type { LLMParsedResponse, LLMUsage } from \"./v3/llm/LLMClient.js\";\n\nfunction withLlmTimeout<T>(promise: Promise<T>, operation: string): Promise<T> {\n return withTimeout(\n promise,\n getEnvTimeoutMs(\"LLM_MAX_MS\"),\n `LLM ${operation}`,\n );\n}\n\nexport async function extract<T extends StagehandZodObject>({\n instruction,\n domElements,\n schema,\n llmClient,\n logger,\n userProvidedInstructions,\n logInferenceToFile = false,\n}: {\n instruction: string;\n domElements: string;\n schema: T;\n llmClient: LLMClient;\n userProvidedInstructions?: string;\n logger: (message: LogLine) => void;\n logInferenceToFile?: boolean;\n}) {\n const metadataSchema = z.object({\n progress: z\n .string()\n .describe(\n \"progress of what has been extracted so far, as concise as possible\",\n ),\n completed: z\n .boolean()\n .describe(\n \"true if the goal is now accomplished. Use this conservatively, only when sure that the goal has been completed.\",\n ),\n });\n\n type ExtractionResponse = InferStagehandSchema<T>;\n type MetadataResponse = z.infer<typeof metadataSchema>;\n\n const isUsingAnthropic = llmClient.type === \"anthropic\";\n const isGPT5 = llmClient.modelName.includes(\"gpt-5\"); // TODO: remove this as we update support for gpt-5 configuration options\n\n const extractCallMessages: ChatMessage[] = [\n buildExtractSystemPrompt(isUsingAnthropic, userProvidedInstructions),\n buildExtractUserPrompt(instruction, domElements, isUsingAnthropic),\n ];\n\n let extractCallFile = \"\";\n let extractCallTimestamp = \"\";\n if (logInferenceToFile) {\n const { fileName, timestamp } = writeTimestampedTxtFile(\n \"extract_summary\",\n \"extract_call\",\n {\n modelCall: \"extract\",\n messages: extractCallMessages,\n },\n );\n extractCallFile = fileName;\n extractCallTimestamp = timestamp;\n }\n\n const extractStartTime = Date.now();\n const extractionResponse = await withLlmTimeout(\n llmClient.createChatCompletion<ExtractionResponse>({\n options: {\n messages: extractCallMessages,\n response_model: {\n schema,\n name: \"Extraction\",\n },\n temperature: isGPT5 ? 1 : 0.1,\n top_p: 1,\n frequency_penalty: 0,\n presence_penalty: 0,\n },\n logger,\n }),\n \"extract\",\n );\n const extractEndTime = Date.now();\n\n const { data: extractedData, usage: extractUsage } = extractionResponse;\n\n let extractResponseFile: string;\n if (logInferenceToFile) {\n const { fileName } = writeTimestampedTxtFile(\n \"extract_summary\",\n \"extract_response\",\n {\n modelResponse: \"extract\",\n rawResponse: extractedData,\n },\n );\n extractResponseFile = fileName;\n\n appendSummary(\"extract\", {\n extract_inference_type: \"extract\",\n timestamp: extractCallTimestamp,\n LLM_input_file: extractCallFile,\n LLM_output_file: extractResponseFile,\n prompt_tokens: extractUsage?.prompt_tokens ?? 0,\n completion_tokens: extractUsage?.completion_tokens ?? 0,\n reasoning_tokens: extractUsage?.reasoning_tokens ?? 0,\n cached_input_tokens: extractUsage?.cached_input_tokens ?? 0,\n inference_time_ms: extractEndTime - extractStartTime,\n });\n }\n\n const metadataCallMessages: ChatMessage[] = [\n buildMetadataSystemPrompt(),\n buildMetadataPrompt(instruction, extractedData),\n ];\n\n let metadataCallFile = \"\";\n let metadataCallTimestamp = \"\";\n if (logInferenceToFile) {\n const { fileName, timestamp } = writeTimestampedTxtFile(\n \"extract_summary\",\n \"metadata_call\",\n {\n modelCall: \"metadata\",\n messages: metadataCallMessages,\n },\n );\n metadataCallFile = fileName;\n metadataCallTimestamp = timestamp;\n }\n\n const metadataStartTime = Date.now();\n const metadataResponse = await withLlmTimeout(\n llmClient.createChatCompletion<MetadataResponse>({\n options: {\n messages: metadataCallMessages,\n response_model: {\n name: \"Metadata\",\n schema: metadataSchema,\n },\n temperature: isGPT5 ? 1 : 0.1,\n top_p: 1,\n frequency_penalty: 0,\n presence_penalty: 0,\n },\n logger,\n }),\n \"extract metadata\",\n );\n const metadataEndTime = Date.now();\n\n const {\n data: {\n completed: metadataResponseCompleted,\n progress: metadataResponseProgress,\n },\n usage: metadataResponseUsage,\n } = metadataResponse;\n\n let metadataResponseFile: string;\n if (logInferenceToFile) {\n const { fileName } = writeTimestampedTxtFile(\n \"extract_summary\",\n \"metadata_response\",\n {\n modelResponse: \"metadata\",\n completed: metadataResponseCompleted,\n progress: metadataResponseProgress,\n },\n );\n metadataResponseFile = fileName;\n\n appendSummary(\"extract\", {\n extract_inference_type: \"metadata\",\n timestamp: metadataCallTimestamp,\n LLM_input_file: metadataCallFile,\n LLM_output_file: metadataResponseFile,\n prompt_tokens: metadataResponseUsage?.prompt_tokens ?? 0,\n completion_tokens: metadataResponseUsage?.completion_tokens ?? 0,\n reasoning_tokens: metadataResponseUsage?.reasoning_tokens ?? 0,\n cached_input_tokens: metadataResponseUsage?.cached_input_tokens ?? 0,\n inference_time_ms: metadataEndTime - metadataStartTime,\n });\n }\n\n const totalPromptTokens =\n (extractUsage?.prompt_tokens ?? 0) +\n (metadataResponseUsage?.prompt_tokens ?? 0);\n\n const totalCompletionTokens =\n (extractUsage?.completion_tokens ?? 0) +\n (metadataResponseUsage?.completion_tokens ?? 0);\n\n const totalInferenceTimeMs =\n extractEndTime - extractStartTime + (metadataEndTime - metadataStartTime);\n const totalReasoningTokens =\n (extractUsage?.reasoning_tokens ?? 0) +\n (metadataResponseUsage?.reasoning_tokens ?? 0);\n const totalCachedInputTokens =\n (extractUsage?.cached_input_tokens ?? 0) +\n (metadataResponseUsage?.cached_input_tokens ?? 0);\n\n return {\n ...extractedData,\n metadata: {\n completed: metadataResponseCompleted,\n progress: metadataResponseProgress,\n },\n prompt_tokens: totalPromptTokens,\n completion_tokens: totalCompletionTokens,\n reasoning_tokens: totalReasoningTokens,\n cached_input_tokens: totalCachedInputTokens,\n inference_time_ms: totalInferenceTimeMs,\n };\n}\n\nexport async function observe({\n instruction,\n domElements,\n llmClient,\n userProvidedInstructions,\n logger,\n logInferenceToFile = false,\n supportedActions,\n}: {\n instruction: string;\n domElements: string;\n llmClient: LLMClient;\n userProvidedInstructions?: string;\n logger: (message: LogLine) => void;\n logInferenceToFile?: boolean;\n supportedActions?: string[];\n}) {\n const isGPT5 = llmClient.modelName.includes(\"gpt-5\"); // TODO: remove this as we update support for gpt-5 configuration options\n\n const observeSchema = z.object({\n elements: z\n .array(\n z.object({\n elementId: z\n .string()\n .regex(/^\\d+-\\d+$/)\n .describe(\n \"the ID string associated with the element. Never include surrounding square brackets. This field must follow the format of 'number-number'.\",\n ),\n description: z\n .string()\n .describe(\n \"a description of the accessible element and its purpose\",\n ),\n method: z\n .enum(\n // Use Object.values() for Zod v3 compatibility - z.enum() in v3 doesn't accept TypeScript enums directly\n Object.values(SupportedUnderstudyAction) as unknown as readonly [\n string,\n ...string[],\n ],\n )\n .describe(\n `the candidate method/action to interact with the element. Select one of the available Understudy interaction methods.`,\n ),\n arguments: z.array(\n z\n .string()\n .describe(\n \"the arguments to pass to the method. For example, for a click, the arguments are empty, but for a fill, the arguments are the value to fill in.\",\n ),\n ),\n }),\n )\n .describe(\"an array of accessible elements that match the instruction\"),\n });\n\n type ObserveResponse = z.infer<typeof observeSchema>;\n\n const messages: ChatMessage[] = [\n buildObserveSystemPrompt(userProvidedInstructions, supportedActions),\n buildObserveUserMessage(instruction, domElements),\n ];\n\n let callTimestamp = \"\";\n let callFile = \"\";\n if (logInferenceToFile) {\n const { fileName, timestamp } = writeTimestampedTxtFile(\n `observe_summary`,\n `observe_call`,\n {\n modelCall: \"observe\",\n messages,\n },\n );\n callFile = fileName;\n callTimestamp = timestamp;\n }\n\n const start = Date.now();\n const rawResponse = await llmClient.createChatCompletion<ObserveResponse>({\n options: {\n messages,\n response_model: {\n schema: observeSchema,\n name: \"Observation\",\n },\n temperature: isGPT5 ? 1 : 0.1,\n top_p: 1,\n frequency_penalty: 0,\n presence_penalty: 0,\n },\n logger,\n });\n const end = Date.now();\n const usageTimeMs = end - start;\n\n const { data: observeData, usage: observeUsage } = rawResponse;\n const promptTokens = observeUsage?.prompt_tokens ?? 0;\n const completionTokens = observeUsage?.completion_tokens ?? 0;\n const reasoningTokens = observeUsage?.reasoning_tokens ?? 0;\n const cachedInputTokens = observeUsage?.cached_input_tokens ?? 0;\n\n let responseFile: string;\n if (logInferenceToFile) {\n const { fileName: responseFileName } = writeTimestampedTxtFile(\n `observe_summary`,\n `observe_response`,\n {\n modelResponse: \"observe\",\n rawResponse: observeData,\n },\n );\n responseFile = responseFileName;\n\n appendSummary(\"observe\", {\n [`observe_inference_type`]: \"observe\",\n timestamp: callTimestamp,\n LLM_input_file: callFile,\n LLM_output_file: responseFile,\n prompt_tokens: promptTokens,\n completion_tokens: completionTokens,\n reasoning_tokens: reasoningTokens,\n cached_input_tokens: cachedInputTokens,\n inference_time_ms: usageTimeMs,\n });\n }\n\n const parsedElements =\n observeData.elements?.map((el) => {\n const base = {\n elementId: el.elementId,\n description: String(el.description),\n method: String(el.method),\n arguments: el.arguments,\n };\n return base;\n }) ?? [];\n\n return {\n elements: parsedElements,\n prompt_tokens: promptTokens,\n completion_tokens: completionTokens,\n reasoning_tokens: reasoningTokens,\n cached_input_tokens: cachedInputTokens,\n inference_time_ms: usageTimeMs,\n };\n}\n\nexport async function act({\n instruction,\n domElements,\n llmClient,\n userProvidedInstructions,\n logger,\n logInferenceToFile = false,\n}: {\n instruction: string;\n domElements: string;\n llmClient: LLMClient;\n userProvidedInstructions?: string;\n logger: (message: LogLine) => void;\n logInferenceToFile?: boolean;\n}) {\n const isGPT5 = llmClient.modelName.includes(\"gpt-5\"); // TODO: remove this as we update support for gpt-5 configuration options\n\n const actSchema = z.object({\n elementId: z\n .string()\n .regex(/^\\d+-\\d+$/)\n .describe(\n \"the ID string associated with the element. Never include surrounding square brackets. This field must follow the format of 'number-number'.\",\n ),\n description: z\n .string()\n .describe(\"a description of the accessible element and its purpose\"),\n method: z\n .enum(\n // Use Object.values() for Zod v3 compatibility - z.enum() in v3 doesn't accept TypeScript enums directly\n Object.values(SupportedUnderstudyAction) as unknown as readonly [\n string,\n ...string[],\n ],\n )\n .describe(\n \"the candidate method/action to interact with the element. Select one of the available Understudy interaction methods.\",\n ),\n arguments: z.array(\n z\n .string()\n .describe(\n \"the arguments to pass to the method. For example, for a click, the arguments are empty, but for a fill, the arguments are the value to fill in.\",\n ),\n ),\n twoStep: z.boolean(),\n });\n\n type ActResponse = z.infer<typeof actSchema>;\n\n const messages: ChatMessage[] = [\n buildActSystemPrompt(userProvidedInstructions),\n buildObserveUserMessage(instruction, domElements),\n ];\n\n let callTimestamp = \"\";\n let callFile = \"\";\n if (logInferenceToFile) {\n const { fileName, timestamp } = writeTimestampedTxtFile(\n `act_summary`,\n `act_call`,\n {\n modelCall: \"act\",\n messages,\n },\n );\n callFile = fileName;\n callTimestamp = timestamp;\n }\n\n const start = Date.now();\n const rawResponse = await llmClient.createChatCompletion<ActResponse>({\n options: {\n messages,\n response_model: {\n schema: actSchema,\n name: \"act\",\n },\n temperature: isGPT5 ? 1 : 0.1,\n top_p: 1,\n frequency_penalty: 0,\n presence_penalty: 0,\n },\n logger,\n });\n const end = Date.now();\n const usageTimeMs = end - start;\n\n const { data: actData, usage: actUsage } = rawResponse;\n const promptTokens = actUsage?.prompt_tokens ?? 0;\n const completionTokens = actUsage?.completion_tokens ?? 0;\n const reasoningTokens = actUsage?.reasoning_tokens ?? 0;\n const cachedInputTokens = actUsage?.cached_input_tokens ?? 0;\n\n let responseFile: string;\n if (logInferenceToFile) {\n const { fileName: responseFileName } = writeTimestampedTxtFile(\n `act_summary`,\n `act_response`,\n {\n modelResponse: \"act\",\n rawResponse: actData,\n },\n );\n responseFile = responseFileName;\n\n appendSummary(\"act\", {\n [`act_inference_type`]: \"act\",\n timestamp: callTimestamp,\n LLM_input_file: callFile,\n LLM_output_file: responseFile,\n prompt_tokens: promptTokens,\n completion_tokens: completionTokens,\n reasoning_tokens: reasoningTokens,\n cached_input_tokens: cachedInputTokens,\n inference_time_ms: usageTimeMs,\n });\n }\n\n const parsedElement = {\n elementId: actData.elementId,\n description: String(actData.description),\n method: String(actData.method),\n arguments: actData.arguments,\n };\n\n return {\n element: parsedElement,\n prompt_tokens: promptTokens,\n completion_tokens: completionTokens,\n reasoning_tokens: reasoningTokens,\n cached_input_tokens: cachedInputTokens,\n inference_time_ms: usageTimeMs,\n twoStep: actData.twoStep,\n };\n}\n"]}
1
+ {"version":3,"file":"inference.js","sourceRoot":"","sources":["../../../lib/inference.ts"],"names":[],"mappings":";;AAgCA,0BA8MC;AAED,0BA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CAAC,CAAC;IACL,CAAC;IAED,MAAM,aAAa,GAAG;QACpB,SAAS,EAAE,OAAO,CAAC,SAAS;QAC5B,WAAW,EAAE,MAAM,CAAC,OAAO,CAAC,WAAW,CAAC;QACxC,MAAM,EAAE,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC;QAC9B,SAAS,EAAE,OAAO,CAAC,SAAS;KAC7B,CAAC;IAEF,OAAO;QACL,OAAO,EAAE,aAAa;QACtB,aAAa,EAAE,YAAY;QAC3B,iBAAiB,EAAE,gBAAgB;QACnC,gBAAgB,EAAE,eAAe;QACjC,mBAAmB,EAAE,iBAAiB;QACtC,iBAAiB,EAAE,WAAW;QAC9B,OAAO,EAAE,OAAO,CAAC,OAAO;KACzB,CAAC;AACJ,CAAC","sourcesContent":["import { z } from \"zod\";\nimport { LogLine } from \"./v3/types/public/logs.js\";\nimport { ChatMessage, LLMClient } from \"./v3/llm/LLMClient.js\";\nimport { getEnvTimeoutMs, withTimeout } from \"./v3/timeoutConfig.js\";\nimport {\n buildActSystemPrompt,\n buildExtractSystemPrompt,\n buildExtractUserPrompt,\n buildMetadataPrompt,\n buildMetadataSystemPrompt,\n buildObserveSystemPrompt,\n buildObserveUserMessage,\n} from \"./prompt.js\";\nimport { appendSummary, writeTimestampedTxtFile } from \"./inferenceLogUtils.js\";\nimport type {\n InferStagehandSchema,\n StagehandZodObject,\n} from \"./v3/zodCompat.js\";\nimport { SupportedUnderstudyAction } from \"./v3/types/private/handlers.js\";\nimport type { Variables } from \"./v3/types/public/agent.js\";\n\n// Re-export for backward compatibility\nexport type { LLMParsedResponse, LLMUsage } from \"./v3/llm/LLMClient.js\";\n\nfunction withLlmTimeout<T>(promise: Promise<T>, operation: string): Promise<T> {\n return withTimeout(\n promise,\n getEnvTimeoutMs(\"LLM_MAX_MS\"),\n `LLM ${operation}`,\n );\n}\n\nexport async function extract<T extends StagehandZodObject>({\n instruction,\n domElements,\n schema,\n llmClient,\n logger,\n userProvidedInstructions,\n logInferenceToFile = false,\n}: {\n instruction: string;\n domElements: string;\n schema: T;\n llmClient: LLMClient;\n userProvidedInstructions?: string;\n logger: (message: LogLine) => void;\n logInferenceToFile?: boolean;\n}) {\n const metadataSchema = z.object({\n progress: z\n .string()\n .describe(\n \"progress of what has been extracted so far, as concise as possible\",\n ),\n completed: z\n .boolean()\n .describe(\n \"true if the goal is now accomplished. Use this conservatively, only when sure that the goal has been completed.\",\n ),\n });\n\n type ExtractionResponse = InferStagehandSchema<T>;\n type MetadataResponse = z.infer<typeof metadataSchema>;\n\n const isUsingAnthropic = llmClient.type === \"anthropic\";\n const isGPT5 = llmClient.modelName.includes(\"gpt-5\"); // TODO: remove this as we update support for gpt-5 configuration options\n\n const extractCallMessages: ChatMessage[] = [\n buildExtractSystemPrompt(isUsingAnthropic, userProvidedInstructions),\n buildExtractUserPrompt(instruction, domElements, isUsingAnthropic),\n ];\n\n let extractCallFile = \"\";\n let extractCallTimestamp = \"\";\n if (logInferenceToFile) {\n const { fileName, timestamp } = writeTimestampedTxtFile(\n \"extract_summary\",\n \"extract_call\",\n {\n modelCall: \"extract\",\n messages: extractCallMessages,\n },\n );\n extractCallFile = fileName;\n extractCallTimestamp = timestamp;\n }\n\n const extractStartTime = Date.now();\n const extractionResponse = await withLlmTimeout(\n llmClient.createChatCompletion<ExtractionResponse>({\n options: {\n messages: extractCallMessages,\n response_model: {\n schema,\n name: \"Extraction\",\n },\n temperature: isGPT5 ? 1 : 0.1,\n top_p: 1,\n frequency_penalty: 0,\n presence_penalty: 0,\n },\n logger,\n }),\n \"extract\",\n );\n const extractEndTime = Date.now();\n\n const { data: extractedData, usage: extractUsage } = extractionResponse;\n\n let extractResponseFile: string;\n if (logInferenceToFile) {\n const { fileName } = writeTimestampedTxtFile(\n \"extract_summary\",\n \"extract_response\",\n {\n modelResponse: \"extract\",\n rawResponse: extractedData,\n },\n );\n extractResponseFile = fileName;\n\n appendSummary(\"extract\", {\n extract_inference_type: \"extract\",\n timestamp: extractCallTimestamp,\n LLM_input_file: extractCallFile,\n LLM_output_file: extractResponseFile,\n prompt_tokens: extractUsage?.prompt_tokens ?? 0,\n completion_tokens: extractUsage?.completion_tokens ?? 0,\n reasoning_tokens: extractUsage?.reasoning_tokens ?? 0,\n cached_input_tokens: extractUsage?.cached_input_tokens ?? 0,\n inference_time_ms: extractEndTime - extractStartTime,\n });\n }\n\n const metadataCallMessages: ChatMessage[] = [\n buildMetadataSystemPrompt(),\n buildMetadataPrompt(instruction, extractedData),\n ];\n\n let metadataCallFile = \"\";\n let metadataCallTimestamp = \"\";\n if (logInferenceToFile) {\n const { fileName, timestamp } = writeTimestampedTxtFile(\n \"extract_summary\",\n \"metadata_call\",\n {\n modelCall: \"metadata\",\n messages: metadataCallMessages,\n },\n );\n metadataCallFile = fileName;\n metadataCallTimestamp = timestamp;\n }\n\n const metadataStartTime = Date.now();\n const metadataResponse = await withLlmTimeout(\n llmClient.createChatCompletion<MetadataResponse>({\n options: {\n messages: metadataCallMessages,\n response_model: {\n name: \"Metadata\",\n schema: metadataSchema,\n },\n temperature: isGPT5 ? 1 : 0.1,\n top_p: 1,\n frequency_penalty: 0,\n presence_penalty: 0,\n },\n logger,\n }),\n \"extract metadata\",\n );\n const metadataEndTime = Date.now();\n\n const {\n data: {\n completed: metadataResponseCompleted,\n progress: metadataResponseProgress,\n },\n usage: metadataResponseUsage,\n } = metadataResponse;\n\n let metadataResponseFile: string;\n if (logInferenceToFile) {\n const { fileName } = writeTimestampedTxtFile(\n \"extract_summary\",\n \"metadata_response\",\n {\n modelResponse: \"metadata\",\n completed: metadataResponseCompleted,\n progress: metadataResponseProgress,\n },\n );\n metadataResponseFile = fileName;\n\n appendSummary(\"extract\", {\n extract_inference_type: \"metadata\",\n timestamp: metadataCallTimestamp,\n LLM_input_file: metadataCallFile,\n LLM_output_file: metadataResponseFile,\n prompt_tokens: metadataResponseUsage?.prompt_tokens ?? 0,\n completion_tokens: metadataResponseUsage?.completion_tokens ?? 0,\n reasoning_tokens: metadataResponseUsage?.reasoning_tokens ?? 0,\n cached_input_tokens: metadataResponseUsage?.cached_input_tokens ?? 0,\n inference_time_ms: metadataEndTime - metadataStartTime,\n });\n }\n\n const totalPromptTokens =\n (extractUsage?.prompt_tokens ?? 0) +\n (metadataResponseUsage?.prompt_tokens ?? 0);\n\n const totalCompletionTokens =\n (extractUsage?.completion_tokens ?? 0) +\n (metadataResponseUsage?.completion_tokens ?? 0);\n\n const totalInferenceTimeMs =\n extractEndTime - extractStartTime + (metadataEndTime - metadataStartTime);\n const totalReasoningTokens =\n (extractUsage?.reasoning_tokens ?? 0) +\n (metadataResponseUsage?.reasoning_tokens ?? 0);\n const totalCachedInputTokens =\n (extractUsage?.cached_input_tokens ?? 0) +\n (metadataResponseUsage?.cached_input_tokens ?? 0);\n\n return {\n ...extractedData,\n metadata: {\n completed: metadataResponseCompleted,\n progress: metadataResponseProgress,\n },\n prompt_tokens: totalPromptTokens,\n completion_tokens: totalCompletionTokens,\n reasoning_tokens: totalReasoningTokens,\n cached_input_tokens: totalCachedInputTokens,\n inference_time_ms: totalInferenceTimeMs,\n };\n}\n\nexport async function observe({\n instruction,\n domElements,\n llmClient,\n userProvidedInstructions,\n logger,\n logInferenceToFile = false,\n supportedActions,\n variables,\n}: {\n instruction: string;\n domElements: string;\n llmClient: LLMClient;\n userProvidedInstructions?: string;\n logger: (message: LogLine) => void;\n logInferenceToFile?: boolean;\n supportedActions?: string[];\n variables?: Variables;\n}) {\n const isGPT5 = llmClient.modelName.includes(\"gpt-5\"); // TODO: remove this as we update support for gpt-5 configuration options\n\n const observeSchema = z.object({\n elements: z\n .array(\n z.object({\n elementId: z\n .string()\n .regex(/^\\d+-\\d+$/)\n .describe(\n \"the ID string associated with the element. Never include surrounding square brackets. This field must follow the format of 'number-number'.\",\n ),\n description: z\n .string()\n .describe(\n \"a description of the accessible element and its purpose\",\n ),\n method: z\n .enum(\n // Use Object.values() for Zod v3 compatibility - z.enum() in v3 doesn't accept TypeScript enums directly\n Object.values(SupportedUnderstudyAction) as unknown as readonly [\n string,\n ...string[],\n ],\n )\n .describe(\n `the candidate method/action to interact with the element. Select one of the available Understudy interaction methods.`,\n ),\n arguments: z.array(\n z\n .string()\n .describe(\n \"the arguments to pass to the method. For example, for a click, the arguments are empty, but for a fill, the arguments are the value to fill in.\",\n ),\n ),\n }),\n )\n .describe(\"an array of accessible elements that match the instruction\"),\n });\n\n type ObserveResponse = z.infer<typeof observeSchema>;\n\n const messages: ChatMessage[] = [\n buildObserveSystemPrompt(\n userProvidedInstructions,\n supportedActions,\n variables,\n ),\n buildObserveUserMessage(instruction, domElements),\n ];\n\n let callTimestamp = \"\";\n let callFile = \"\";\n if (logInferenceToFile) {\n const { fileName, timestamp } = writeTimestampedTxtFile(\n `observe_summary`,\n `observe_call`,\n {\n modelCall: \"observe\",\n messages,\n },\n );\n callFile = fileName;\n callTimestamp = timestamp;\n }\n\n const start = Date.now();\n const rawResponse = await llmClient.createChatCompletion<ObserveResponse>({\n options: {\n messages,\n response_model: {\n schema: observeSchema,\n name: \"Observation\",\n },\n temperature: isGPT5 ? 1 : 0.1,\n top_p: 1,\n frequency_penalty: 0,\n presence_penalty: 0,\n },\n logger,\n });\n const end = Date.now();\n const usageTimeMs = end - start;\n\n const { data: observeData, usage: observeUsage } = rawResponse;\n const promptTokens = observeUsage?.prompt_tokens ?? 0;\n const completionTokens = observeUsage?.completion_tokens ?? 0;\n const reasoningTokens = observeUsage?.reasoning_tokens ?? 0;\n const cachedInputTokens = observeUsage?.cached_input_tokens ?? 0;\n\n let responseFile: string;\n if (logInferenceToFile) {\n const { fileName: responseFileName } = writeTimestampedTxtFile(\n `observe_summary`,\n `observe_response`,\n {\n modelResponse: \"observe\",\n rawResponse: observeData,\n },\n );\n responseFile = responseFileName;\n\n appendSummary(\"observe\", {\n [`observe_inference_type`]: \"observe\",\n timestamp: callTimestamp,\n LLM_input_file: callFile,\n LLM_output_file: responseFile,\n prompt_tokens: promptTokens,\n completion_tokens: completionTokens,\n reasoning_tokens: reasoningTokens,\n cached_input_tokens: cachedInputTokens,\n inference_time_ms: usageTimeMs,\n });\n }\n\n const parsedElements =\n observeData.elements?.map((el) => {\n const base = {\n elementId: el.elementId,\n description: String(el.description),\n method: String(el.method),\n arguments: el.arguments,\n };\n return base;\n }) ?? [];\n\n return {\n elements: parsedElements,\n prompt_tokens: promptTokens,\n completion_tokens: completionTokens,\n reasoning_tokens: reasoningTokens,\n cached_input_tokens: cachedInputTokens,\n inference_time_ms: usageTimeMs,\n };\n}\n\nexport async function act({\n instruction,\n domElements,\n llmClient,\n userProvidedInstructions,\n logger,\n logInferenceToFile = false,\n}: {\n instruction: string;\n domElements: string;\n llmClient: LLMClient;\n userProvidedInstructions?: string;\n logger: (message: LogLine) => void;\n logInferenceToFile?: boolean;\n}) {\n const isGPT5 = llmClient.modelName.includes(\"gpt-5\"); // TODO: remove this as we update support for gpt-5 configuration options\n\n const actSchema = z.object({\n elementId: z\n .string()\n .regex(/^\\d+-\\d+$/)\n .describe(\n \"the ID string associated with the element. Never include surrounding square brackets. This field must follow the format of 'number-number'.\",\n ),\n description: z\n .string()\n .describe(\"a description of the accessible element and its purpose\"),\n method: z\n .enum(\n // Use Object.values() for Zod v3 compatibility - z.enum() in v3 doesn't accept TypeScript enums directly\n Object.values(SupportedUnderstudyAction) as unknown as readonly [\n string,\n ...string[],\n ],\n )\n .describe(\n \"the candidate method/action to interact with the element. Select one of the available Understudy interaction methods.\",\n ),\n arguments: z.array(\n z\n .string()\n .describe(\n \"the arguments to pass to the method. For example, for a click, the arguments are empty, but for a fill, the arguments are the value to fill in.\",\n ),\n ),\n twoStep: z.boolean(),\n });\n\n type ActResponse = z.infer<typeof actSchema>;\n\n const messages: ChatMessage[] = [\n buildActSystemPrompt(userProvidedInstructions),\n buildObserveUserMessage(instruction, domElements),\n ];\n\n let callTimestamp = \"\";\n let callFile = \"\";\n if (logInferenceToFile) {\n const { fileName, timestamp } = writeTimestampedTxtFile(\n `act_summary`,\n `act_call`,\n {\n modelCall: \"act\",\n messages,\n },\n );\n callFile = fileName;\n callTimestamp = timestamp;\n }\n\n const start = Date.now();\n const rawResponse = await llmClient.createChatCompletion<ActResponse>({\n options: {\n messages,\n response_model: {\n schema: actSchema,\n name: \"act\",\n },\n temperature: isGPT5 ? 1 : 0.1,\n top_p: 1,\n frequency_penalty: 0,\n presence_penalty: 0,\n },\n logger,\n });\n const end = Date.now();\n const usageTimeMs = end - start;\n\n const { data: actData, usage: actUsage } = rawResponse;\n const promptTokens = actUsage?.prompt_tokens ?? 0;\n const completionTokens = actUsage?.completion_tokens ?? 0;\n const reasoningTokens = actUsage?.reasoning_tokens ?? 0;\n const cachedInputTokens = actUsage?.cached_input_tokens ?? 0;\n\n let responseFile: string;\n if (logInferenceToFile) {\n const { fileName: responseFileName } = writeTimestampedTxtFile(\n `act_summary`,\n `act_response`,\n {\n modelResponse: \"act\",\n rawResponse: actData,\n },\n );\n responseFile = responseFileName;\n\n appendSummary(\"act\", {\n [`act_inference_type`]: \"act\",\n timestamp: callTimestamp,\n LLM_input_file: callFile,\n LLM_output_file: responseFile,\n prompt_tokens: promptTokens,\n completion_tokens: completionTokens,\n reasoning_tokens: reasoningTokens,\n cached_input_tokens: cachedInputTokens,\n inference_time_ms: usageTimeMs,\n });\n }\n\n const parsedElement = {\n elementId: actData.elementId,\n description: String(actData.description),\n method: String(actData.method),\n arguments: actData.arguments,\n };\n\n return {\n element: parsedElement,\n prompt_tokens: promptTokens,\n completion_tokens: completionTokens,\n reasoning_tokens: reasoningTokens,\n cached_input_tokens: cachedInputTokens,\n inference_time_ms: usageTimeMs,\n twoStep: actData.twoStep,\n };\n}\n"]}
@@ -5,7 +5,7 @@ export declare function buildExtractSystemPrompt(isUsingPrintExtractedDataTool?:
5
5
  export declare function buildExtractUserPrompt(instruction: string, domElements: string, isUsingPrintExtractedDataTool?: boolean): ChatMessage;
6
6
  export declare function buildMetadataSystemPrompt(): ChatMessage;
7
7
  export declare function buildMetadataPrompt(instruction: string, extractionResponse: object): ChatMessage;
8
- export declare function buildObserveSystemPrompt(userProvidedInstructions?: string, supportedActions?: string[]): ChatMessage;
8
+ export declare function buildObserveSystemPrompt(userProvidedInstructions?: string, supportedActions?: string[], variables?: Variables): ChatMessage;
9
9
  export declare function buildObserveUserMessage(instruction: string, domElements: string): ChatMessage;
10
10
  export declare function buildActSystemPrompt(userProvidedInstructions?: string): ChatMessage;
11
11
  export declare function buildActPrompt(action: string, supportedActions: string[], variables?: Variables): string;
@@ -13,6 +13,7 @@ exports.buildStepTwoPrompt = buildStepTwoPrompt;
13
13
  exports.buildOperatorSystemPrompt = buildOperatorSystemPrompt;
14
14
  exports.buildCuaDefaultSystemPrompt = buildCuaDefaultSystemPrompt;
15
15
  exports.buildGoogleCUASystemPrompt = buildGoogleCUASystemPrompt;
16
+ const variables_js_1 = require("./v3/agent/utils/variables.js");
16
17
  function buildUserInstructionsString(userProvidedInstructions) {
17
18
  if (!userProvidedInstructions) {
18
19
  return "";
@@ -87,10 +88,18 @@ Extracted content: ${JSON.stringify(extractionResponse, null, 2)}`,
87
88
  };
88
89
  }
89
90
  // observe
90
- function buildObserveSystemPrompt(userProvidedInstructions, supportedActions) {
91
+ function buildObserveSystemPrompt(userProvidedInstructions, supportedActions, variables) {
91
92
  const actionsString = supportedActions?.length
92
93
  ? `\n\nSupported actions: ${supportedActions.join(", ")}`
93
94
  : "";
95
+ const variableEntries = (0, variables_js_1.getVariablePromptEntries)(variables);
96
+ const variablesString = variableEntries.length
97
+ ? `\n\nAvailable variables: ${variableEntries
98
+ .map(({ name, description }) => {
99
+ return description ? `%${name}% (${description})` : `%${name}%`;
100
+ })
101
+ .join(", ")}. When an action needs a dynamic or sensitive value, return the matching %variableName% placeholder in the action arguments instead of a literal value`
102
+ : "";
94
103
  const observeSystemPrompt = `
95
104
  You are helping the user automate the browser by finding elements based on what the user wants to observe in the page.
96
105
 
@@ -99,7 +108,7 @@ You will be given:
99
108
  2. a hierarchical accessibility tree showing the semantic structure of the page. The tree is a hybrid of the DOM and the accessibility tree.
100
109
 
101
110
  Return an array of elements that match the instruction if they exist, otherwise return an empty array.
102
- When returning elements, include the appropriate method from the supported actions list.${actionsString}. When choosing non-left click actions, provide right or middle as the argument.`;
111
+ When returning elements, include the appropriate method from the supported actions list.${actionsString}${variablesString}. When choosing non-left click actions, provide right or middle as the argument.`;
103
112
  const content = observeSystemPrompt.replace(/\s+/g, " ");
104
113
  return {
105
114
  role: "system",
@@ -1 +1 @@
1
- {"version":3,"file":"prompt.js","sourceRoot":"","sources":["../../../lib/prompt.ts"],"names":[],"mappings":";;AAGA,kEAaC;AAGD,4DA2CC;AAED,wDAkBC;AAUD,8DAKC;AAED,kDASC;AAGD,4DAyBC;AAED,0DASC;AAED,oDAmBC;AAED,wCA0CC;AAED,gDAgCC;AAED,8DAiCC;AAED,kEAEC;AAED,gEASC;AArSD,SAAgB,2BAA2B,CACzC,wBAAiC;IAEjC,IAAI,CAAC,wBAAwB,EAAE,CAAC;QAC9B,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,OAAO;;;;;EAKP,wBAAwB,EAAE,CAAC;AAC7B,CAAC;AAED,UAAU;AACV,SAAgB,wBAAwB,CACtC,gCAAyC,KAAK,EAC9C,wBAAiC;IAEjC,MAAM,WAAW,GAAG;;;;;;IAMlB,CAAC;IAEH,MAAM,aAAa,GAAG,yCAAyC,CAAC;IAEhE,MAAM,YAAY,GAAG;;;GAGpB,CAAC,IAAI,EAAE,CAAC;IAET,MAAM,gBAAgB,GAAG,6BAA6B;QACpD,CAAC,CAAC;;;GAGH,CAAC,IAAI,EAAE;QACN,CAAC,CAAC,EAAE,CAAC;IAEP,MAAM,sBAAsB,GAC1B,+GAA+G;QAC/G,sFAAsF,CAAC;IAEzF,MAAM,gBAAgB,GAAG,2BAA2B,CAClD,wBAAwB,CACzB,CAAC;IAEF,MAAM,OAAO,GACX,GAAG,WAAW,GAAG,aAAa,OAAO,YAAY,KAAK,gBAAgB,GACpE,sBAAsB,CAAC,CAAC,CAAC,O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ACE,CAAC;AACJ,CAAC","sourcesContent":["import { ChatMessage } from \"./v3/llm/LLMClient.js\";\nimport type { Variables } from \"./v3/types/public/agent.js\";\n\nexport function buildUserInstructionsString(\n userProvidedInstructions?: string,\n): string {\n if (!userProvidedInstructions) {\n return \"\";\n }\n\n return `\\n\\n# Custom Instructions Provided by the User\n \nPlease keep the user's instructions in mind when performing actions. If the user's instructions are not relevant to the current task, ignore them.\n\nUser Instructions:\n${userProvidedInstructions}`;\n}\n\n// extract\nexport function buildExtractSystemPrompt(\n isUsingPrintExtractedDataTool: boolean = false,\n userProvidedInstructions?: string,\n): ChatMessage {\n const baseContent = `You are extracting content on behalf of a user.\n If a user asks you to extract a 'list' of information, or 'all' information, \n YOU MUST EXTRACT ALL OF THE INFORMATION THAT THE USER REQUESTS.\n \n You will be given:\n1. An instruction\n2. `;\n\n const contentDetail = `A list of DOM elements to extract from.`;\n\n const instructions = `\nPrint the exact text from the DOM elements with all symbols, characters, and endlines as is.\nPrint null or an empty string if no new information is found.\n `.trim();\n\n const toolInstructions = isUsingPrintExtractedDataTool\n ? `\nONLY print the content using the print_extracted_data tool provided.\nONLY print the content using the print_extracted_data tool provided.\n `.trim()\n : \"\";\n\n const additionalInstructions =\n \"If a user is attempting to extract links or URLs, you MUST respond with ONLY the IDs of the link elements. \\n\" +\n \"Do not attempt to extract links directly from the text unless absolutely necessary. \";\n\n const userInstructions = buildUserInstructionsString(\n userProvidedInstructions,\n );\n\n const content =\n `${baseContent}${contentDetail}\\n\\n${instructions}\\n${toolInstructions}${\n additionalInstructions ? `\\n\\n${additionalInstructions}` : \"\"\n }${userInstructions ? `\\n\\n${userInstructions}` : \"\"}`.replace(/\\s+/g, \" \");\n\n return {\n role: \"system\",\n content,\n };\n}\n\nexport function buildExtractUserPrompt(\n instruction: string,\n domElements: string,\n isUsingPrintExtractedDataTool: boolean = false,\n): ChatMessage {\n let content = `Instruction: ${instruction}\nDOM: ${domElements}`;\n\n if (isUsingPrintExtractedDataTool) {\n content += `\nONLY print the content using the print_extracted_data tool provided.\nONLY print the content using the print_extracted_data tool provided.`;\n }\n\n return {\n role: \"user\",\n content,\n };\n}\n\nconst metadataSystemPrompt = `You are an AI assistant tasked with evaluating the progress and completion status of an extraction task.\nAnalyze the extraction response and determine if the task is completed or if more information is needed.\nStrictly abide by the following criteria:\n1. Once the instruction has been satisfied by the current extraction response, ALWAYS set completion status to true and stop processing, regardless of remaining chunks.\n2. Only set completion status to false if BOTH of these conditions are true:\n - The instruction has not been satisfied yet\n - There are still chunks left to process (chunksTotal > chunksSeen)`;\n\nexport function buildMetadataSystemPrompt(): ChatMessage {\n return {\n role: \"system\",\n content: metadataSystemPrompt,\n };\n}\n\nexport function buildMetadataPrompt(\n instruction: string,\n extractionResponse: object,\n): ChatMessage {\n return {\n role: \"user\",\n content: `Instruction: ${instruction}\nExtracted content: ${JSON.stringify(extractionResponse, null, 2)}`,\n };\n}\n\n// observe\nexport function buildObserveSystemPrompt(\n userProvidedInstructions?: string,\n supportedActions?: string[],\n): ChatMessage {\n const actionsString = supportedActions?.length\n ? `\\n\\nSupported actions: ${supportedActions.join(\", \")}`\n : \"\";\n\n const observeSystemPrompt = `\nYou are helping the user automate the browser by finding elements based on what the user wants to observe in the page.\n\nYou will be given:\n1. a instruction of elements to observe\n2. a hierarchical accessibility tree showing the semantic structure of the page. The tree is a hybrid of the DOM and the accessibility tree.\n\nReturn an array of elements that match the instruction if they exist, otherwise return an empty array.\nWhen returning elements, include the appropriate method from the supported actions list.${actionsString}. When choosing non-left click actions, provide right or middle as the argument.`;\n const content = observeSystemPrompt.replace(/\\s+/g, \" \");\n\n return {\n role: \"system\",\n content: [content, buildUserInstructionsString(userProvidedInstructions)]\n .filter(Boolean)\n .join(\"\\n\\n\"),\n };\n}\n\nexport function buildObserveUserMessage(\n instruction: string,\n domElements: string,\n): ChatMessage {\n return {\n role: \"user\",\n content: `instruction: ${instruction}\nAccessibility Tree: \\n${domElements}\\n`,\n };\n}\n\nexport function buildActSystemPrompt(\n userProvidedInstructions?: string,\n): ChatMessage {\n const actSystemPrompt = `\nYou are helping the user automate the browser by finding elements based on what action the user wants to take on the page\n\nYou will be given:\n1. a user defined instruction about what action to take\n2. a hierarchical accessibility tree showing the semantic structure of the page. The tree is a hybrid of the DOM and the accessibility tree.\n\nReturn the element that matches the instruction if it exists. Otherwise, return an empty object.`;\n const content = actSystemPrompt.replace(/\\s+/g, \" \");\n\n return {\n role: \"system\",\n content: [content, buildUserInstructionsString(userProvidedInstructions)]\n .filter(Boolean)\n .join(\"\\n\\n\"),\n };\n}\n\nexport function buildActPrompt(\n action: string,\n supportedActions: string[],\n variables?: Variables,\n): string {\n // Base instruction\n let instruction = `Find the most relevant element to perform an action on given the following action: ${action}. \n IF AND ONLY IF the action EXPLICITLY includes the word 'dropdown' and implies choosing/selecting an option from a dropdown, ignore the 'General Instructions' section, and follow the 'Dropdown Specific Instructions' section carefully.\n \n General Instructions: \n Provide an action for this element such as ${supportedActions.join(\", \")}. Remember that to users, buttons and links look the same in most cases.\n When choosing non-left click actions, provide right or middle as the argument\n If the action is completely unrelated to a potential action to be taken on the page, return an empty object. \n ONLY return one action. If multiple actions are relevant, return the most relevant one. \n If the user is asking to scroll to a position on the page, e.g., 'halfway' or 0.75, etc, you must return the argument formatted as the correct percentage, e.g., '50%' or '75%', etc.\n If the user is asking to scroll to the next chunk/previous chunk, choose the nextChunk/prevChunk method. No arguments are required here.\n If the action implies a key press, e.g., 'press enter', 'press a', 'press space', etc., always choose the press method with the appropriate key as argument — e.g. 'a', 'Enter', 'Space'. Do not choose a click action on an on-screen keyboard. Capitalize the first character like 'Enter', 'Tab', 'Escape' only for special keys. \n \n Dropdown Specific Instructions:\n For interacting with dropdowns, there are two specific cases that you need to handle. \n \n CASE 1: the element is a 'select' element. \n - choose the selectOptionFromDropdown method,\n - set the argument to the exact text of the option that should be selected,\n - set twoStep to false.\n CASE 2: the element is NOT a 'select' element:\n - do not attempt to directly choose the element from the dropdown. You will need to click to expand the dropdown first. You will achieve this by following these instructions:\n - choose the node that most closely corresponds to the given instruction EVEN if it is a 'StaticText' element, or otherwise does not appear to be interactable. \n - choose the 'click' method\n - set twoStep to true.\n `;\n\n // Add variable names (not values) to the instruction if any\n if (variables && Object.keys(variables).length > 0) {\n const variableNames = Object.keys(variables)\n .map((key) => `%${key}%`)\n .join(\", \");\n const variablesPrompt = `The following variables are available to use in the action: ${variableNames}. Fill the argument variables with the variable name.`;\n instruction += ` ${variablesPrompt}`;\n }\n\n return instruction;\n}\n\nexport function buildStepTwoPrompt(\n originalUserAction: string,\n previousAction: string,\n supportedActions: string[],\n variables?: Variables,\n): string {\n // Base instruction\n let instruction = `\n The original user action was: ${originalUserAction}.\n You have just taken the following action which completed step 1 of 2: ${previousAction}.\n \n Now, you must find the most relevant element to perform an action on in order to complete step 2 of 2. \n \n General Instructions: \n Provide an action for this element such as ${supportedActions.join(\", \")}. Remember that to users, buttons and links look the same in most cases.\n If the action is completely unrelated to a potential action to be taken on the page, return an empty object. \n ONLY return one action. If multiple actions are relevant, return the most relevant one. \n If the user is asking to scroll to a position on the page, e.g., 'halfway' or 0.75, etc, you must return the argument formatted as the correct percentage, e.g., '50%' or '75%', etc.\n If the user is asking to scroll to the next chunk/previous chunk, choose the nextChunk/prevChunk method. No arguments are required here.\n If the action implies a key press, e.g., 'press enter', 'press a', 'press space', etc., always choose the press method with the appropriate key as argument — e.g. 'a', 'Enter', 'Space'. Do not choose a click action on an on-screen keyboard. Capitalize the first character like 'Enter', 'Tab', 'Escape' only for special keys. \n `;\n\n // Add variable names (not values) to the instruction if any\n if (variables && Object.keys(variables).length > 0) {\n const variableNames = Object.keys(variables)\n .map((key) => `%${key}%`)\n .join(\", \");\n const variablesPrompt = `The following variables are available to use in the action: ${variableNames}. Fill the argument variables with the variable name.`;\n instruction += ` ${variablesPrompt}`;\n }\n\n return instruction;\n}\n\nexport function buildOperatorSystemPrompt(goal: string): ChatMessage {\n return {\n role: \"system\",\n content: `You are a general-purpose agent whose job is to accomplish the user's goal across multiple model calls by running actions on the page.\n\nYou will be given a goal and a list of steps that have been taken so far. Your job is to determine if either the user's goal has been completed or if there are still steps that need to be taken.\n\n# Your current goal\n${goal}\n\n# CRITICAL: You MUST use the provided tools to take actions. Do not just describe what you want to do - actually call the appropriate tools.\n\n# Available tools and when to use them:\n- \\`act\\`: Use this to interact with the page (click, type, navigate, etc.)\n- \\`extract\\`: Use this to get information from the page\n- \\`goto\\`: Use this to navigate to a specific URL\n- \\`wait\\`: Use this to wait for a period of time\n- \\`navback\\`: Use this to go back to the previous page\n- \\`refresh\\`: Use this to refresh the current page\n- \\`close\\`: Use this ONLY when the task is complete or cannot be achieved\n- External tools: Use any additional tools (like search tools) as needed for your goal\n\n# Important guidelines\n1. ALWAYS use tools - never just provide text responses about what you plan to do\n2. Break down complex actions into individual atomic steps\n3. For \\`act\\` commands, use only one action at a time, such as:\n - Single click on a specific element\n - Type into a single input field\n - Select a single option\n4. Avoid combining multiple actions in one instruction\n5. If multiple actions are needed, they should be separate steps\n6. Only use \\`close\\` when the task is genuinely complete or impossible to achieve`,\n };\n}\n\nexport function buildCuaDefaultSystemPrompt(): string {\n return `You are a helpful assistant that can use a web browser.\\nDo not ask follow up questions, the user will trust your judgement. Today's date is ${new Date().toISOString().split(\"T\")[0]}.`;\n}\n\nexport function buildGoogleCUASystemPrompt(): ChatMessage {\n return {\n role: \"system\",\n content: `You are a general-purpose browser agent whose job is to accomplish the user's goal.\nToday's date is ${new Date().toISOString().split(\"T\")[0]}.\nYou have access to a search tool; however, in most cases you should operate within the page/url the user has provided. ONLY use the search tool if you're stuck or the task is impossible to complete within the current page.\nYou will be given a goal and a list of steps that have been taken so far. Avoid requesting the user for input as much as possible. Good luck!\n`,\n };\n}\n"]}
1
+ {"version":3,"file":"prompt.js","sourceRoot":"","sources":["../../../lib/prompt.ts"],"names":[],"mappings":";;AAIA,kEAaC;AAGD,4DA2CC;AAED,wDAkBC;AAUD,8DAKC;AAED,kDASC;AAGD,4DAoCC;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;CAGvD;KACE,CAAC;AACJ,CAAC","sourcesContent":["import { ChatMessage } from \"./v3/llm/LLMClient.js\";\nimport type { Variables } from \"./v3/types/public/agent.js\";\nimport { getVariablePromptEntries } from \"./v3/agent/utils/variables.js\";\n\nexport function buildUserInstructionsString(\n userProvidedInstructions?: string,\n): string {\n if (!userProvidedInstructions) {\n return \"\";\n }\n\n return `\\n\\n# Custom Instructions Provided by the User\n \nPlease keep the user's instructions in mind when performing actions. If the user's instructions are not relevant to the current task, ignore them.\n\nUser Instructions:\n${userProvidedInstructions}`;\n}\n\n// extract\nexport function buildExtractSystemPrompt(\n isUsingPrintExtractedDataTool: boolean = false,\n userProvidedInstructions?: string,\n): ChatMessage {\n const baseContent = `You are extracting content on behalf of a user.\n If a user asks you to extract a 'list' of information, or 'all' information, \n YOU MUST EXTRACT ALL OF THE INFORMATION THAT THE USER REQUESTS.\n \n You will be given:\n1. An instruction\n2. `;\n\n const contentDetail = `A list of DOM elements to extract from.`;\n\n const instructions = `\nPrint the exact text from the DOM elements with all symbols, characters, and endlines as is.\nPrint null or an empty string if no new information is found.\n `.trim();\n\n const toolInstructions = isUsingPrintExtractedDataTool\n ? `\nONLY print the content using the print_extracted_data tool provided.\nONLY print the content using the print_extracted_data tool provided.\n `.trim()\n : \"\";\n\n const additionalInstructions =\n \"If a user is attempting to extract links or URLs, you MUST respond with ONLY the IDs of the link elements. \\n\" +\n \"Do not attempt to extract links directly from the text unless absolutely necessary. \";\n\n const userInstructions = buildUserInstructionsString(\n userProvidedInstructions,\n );\n\n const content =\n `${baseContent}${contentDetail}\\n\\n${instructions}\\n${toolInstructions}${\n additionalInstructions ? `\\n\\n${additionalInstructions}` : \"\"\n }${userInstructions ? `\\n\\n${userInstructions}` : \"\"}`.replace(/\\s+/g, \" \");\n\n return {\n role: \"system\",\n content,\n };\n}\n\nexport function buildExtractUserPrompt(\n instruction: string,\n domElements: string,\n isUsingPrintExtractedDataTool: boolean = false,\n): ChatMessage {\n let content = `Instruction: ${instruction}\nDOM: ${domElements}`;\n\n if (isUsingPrintExtractedDataTool) {\n content += `\nONLY print the content using the print_extracted_data tool provided.\nONLY print the content using the print_extracted_data tool provided.`;\n }\n\n return {\n role: \"user\",\n content,\n };\n}\n\nconst metadataSystemPrompt = `You are an AI assistant tasked with evaluating the progress and completion status of an extraction task.\nAnalyze the extraction response and determine if the task is completed or if more information is needed.\nStrictly abide by the following criteria:\n1. Once the instruction has been satisfied by the current extraction response, ALWAYS set completion status to true and stop processing, regardless of remaining chunks.\n2. Only set completion status to false if BOTH of these conditions are true:\n - The instruction has not been satisfied yet\n - There are still chunks left to process (chunksTotal > chunksSeen)`;\n\nexport function buildMetadataSystemPrompt(): ChatMessage {\n return {\n role: \"system\",\n content: metadataSystemPrompt,\n };\n}\n\nexport function buildMetadataPrompt(\n instruction: string,\n extractionResponse: object,\n): ChatMessage {\n return {\n role: \"user\",\n content: `Instruction: ${instruction}\nExtracted content: ${JSON.stringify(extractionResponse, null, 2)}`,\n };\n}\n\n// observe\nexport function buildObserveSystemPrompt(\n userProvidedInstructions?: string,\n supportedActions?: string[],\n variables?: Variables,\n): ChatMessage {\n const actionsString = supportedActions?.length\n ? `\\n\\nSupported actions: ${supportedActions.join(\", \")}`\n : \"\";\n const variableEntries = getVariablePromptEntries(variables);\n const variablesString = variableEntries.length\n ? `\\n\\nAvailable variables: ${variableEntries\n .map(({ name, description }) => {\n return description ? `%${name}% (${description})` : `%${name}%`;\n })\n .join(\n \", \",\n )}. When an action needs a dynamic or sensitive value, return the matching %variableName% placeholder in the action arguments instead of a literal value`\n : \"\";\n\n const observeSystemPrompt = `\nYou are helping the user automate the browser by finding elements based on what the user wants to observe in the page.\n\nYou will be given:\n1. a instruction of elements to observe\n2. a hierarchical accessibility tree showing the semantic structure of the page. The tree is a hybrid of the DOM and the accessibility tree.\n\nReturn an array of elements that match the instruction if they exist, otherwise return an empty array.\nWhen returning elements, include the appropriate method from the supported actions list.${actionsString}${variablesString}. When choosing non-left click actions, provide right or middle as the argument.`;\n const content = observeSystemPrompt.replace(/\\s+/g, \" \");\n\n return {\n role: \"system\",\n content: [content, buildUserInstructionsString(userProvidedInstructions)]\n .filter(Boolean)\n .join(\"\\n\\n\"),\n };\n}\n\nexport function buildObserveUserMessage(\n instruction: string,\n domElements: string,\n): ChatMessage {\n return {\n role: \"user\",\n content: `instruction: ${instruction}\nAccessibility Tree: \\n${domElements}\\n`,\n };\n}\n\nexport function buildActSystemPrompt(\n userProvidedInstructions?: string,\n): ChatMessage {\n const actSystemPrompt = `\nYou are helping the user automate the browser by finding elements based on what action the user wants to take on the page\n\nYou will be given:\n1. a user defined instruction about what action to take\n2. a hierarchical accessibility tree showing the semantic structure of the page. The tree is a hybrid of the DOM and the accessibility tree.\n\nReturn the element that matches the instruction if it exists. Otherwise, return an empty object.`;\n const content = actSystemPrompt.replace(/\\s+/g, \" \");\n\n return {\n role: \"system\",\n content: [content, buildUserInstructionsString(userProvidedInstructions)]\n .filter(Boolean)\n .join(\"\\n\\n\"),\n };\n}\n\nexport function buildActPrompt(\n action: string,\n supportedActions: string[],\n variables?: Variables,\n): string {\n // Base instruction\n let instruction = `Find the most relevant element to perform an action on given the following action: ${action}. \n IF AND ONLY IF the action EXPLICITLY includes the word 'dropdown' and implies choosing/selecting an option from a dropdown, ignore the 'General Instructions' section, and follow the 'Dropdown Specific Instructions' section carefully.\n \n General Instructions: \n Provide an action for this element such as ${supportedActions.join(\", \")}. Remember that to users, buttons and links look the same in most cases.\n When choosing non-left click actions, provide right or middle as the argument\n If the action is completely unrelated to a potential action to be taken on the page, return an empty object. \n ONLY return one action. If multiple actions are relevant, return the most relevant one. \n If the user is asking to scroll to a position on the page, e.g., 'halfway' or 0.75, etc, you must return the argument formatted as the correct percentage, e.g., '50%' or '75%', etc.\n If the user is asking to scroll to the next chunk/previous chunk, choose the nextChunk/prevChunk method. No arguments are required here.\n If the action implies a key press, e.g., 'press enter', 'press a', 'press space', etc., always choose the press method with the appropriate key as argument — e.g. 'a', 'Enter', 'Space'. Do not choose a click action on an on-screen keyboard. Capitalize the first character like 'Enter', 'Tab', 'Escape' only for special keys. \n \n Dropdown Specific Instructions:\n For interacting with dropdowns, there are two specific cases that you need to handle. \n \n CASE 1: the element is a 'select' element. \n - choose the selectOptionFromDropdown method,\n - set the argument to the exact text of the option that should be selected,\n - set twoStep to false.\n CASE 2: the element is NOT a 'select' element:\n - do not attempt to directly choose the element from the dropdown. You will need to click to expand the dropdown first. You will achieve this by following these instructions:\n - choose the node that most closely corresponds to the given instruction EVEN if it is a 'StaticText' element, or otherwise does not appear to be interactable. \n - choose the 'click' method\n - set twoStep to true.\n `;\n\n // Add variable names (not values) to the instruction if any\n if (variables && Object.keys(variables).length > 0) {\n const variableNames = Object.keys(variables)\n .map((key) => `%${key}%`)\n .join(\", \");\n const variablesPrompt = `The following variables are available to use in the action: ${variableNames}. Fill the argument variables with the variable name.`;\n instruction += ` ${variablesPrompt}`;\n }\n\n return instruction;\n}\n\nexport function buildStepTwoPrompt(\n originalUserAction: string,\n previousAction: string,\n supportedActions: string[],\n variables?: Variables,\n): string {\n // Base instruction\n let instruction = `\n The original user action was: ${originalUserAction}.\n You have just taken the following action which completed step 1 of 2: ${previousAction}.\n \n Now, you must find the most relevant element to perform an action on in order to complete step 2 of 2. \n \n General Instructions: \n Provide an action for this element such as ${supportedActions.join(\", \")}. Remember that to users, buttons and links look the same in most cases.\n If the action is completely unrelated to a potential action to be taken on the page, return an empty object. \n ONLY return one action. If multiple actions are relevant, return the most relevant one. \n If the user is asking to scroll to a position on the page, e.g., 'halfway' or 0.75, etc, you must return the argument formatted as the correct percentage, e.g., '50%' or '75%', etc.\n If the user is asking to scroll to the next chunk/previous chunk, choose the nextChunk/prevChunk method. No arguments are required here.\n If the action implies a key press, e.g., 'press enter', 'press a', 'press space', etc., always choose the press method with the appropriate key as argument — e.g. 'a', 'Enter', 'Space'. Do not choose a click action on an on-screen keyboard. Capitalize the first character like 'Enter', 'Tab', 'Escape' only for special keys. \n `;\n\n // Add variable names (not values) to the instruction if any\n if (variables && Object.keys(variables).length > 0) {\n const variableNames = Object.keys(variables)\n .map((key) => `%${key}%`)\n .join(\", \");\n const variablesPrompt = `The following variables are available to use in the action: ${variableNames}. Fill the argument variables with the variable name.`;\n instruction += ` ${variablesPrompt}`;\n }\n\n return instruction;\n}\n\nexport function buildOperatorSystemPrompt(goal: string): ChatMessage {\n return {\n role: \"system\",\n content: `You are a general-purpose agent whose job is to accomplish the user's goal across multiple model calls by running actions on the page.\n\nYou will be given a goal and a list of steps that have been taken so far. Your job is to determine if either the user's goal has been completed or if there are still steps that need to be taken.\n\n# Your current goal\n${goal}\n\n# CRITICAL: You MUST use the provided tools to take actions. Do not just describe what you want to do - actually call the appropriate tools.\n\n# Available tools and when to use them:\n- \\`act\\`: Use this to interact with the page (click, type, navigate, etc.)\n- \\`extract\\`: Use this to get information from the page\n- \\`goto\\`: Use this to navigate to a specific URL\n- \\`wait\\`: Use this to wait for a period of time\n- \\`navback\\`: Use this to go back to the previous page\n- \\`refresh\\`: Use this to refresh the current page\n- \\`close\\`: Use this ONLY when the task is complete or cannot be achieved\n- External tools: Use any additional tools (like search tools) as needed for your goal\n\n# Important guidelines\n1. ALWAYS use tools - never just provide text responses about what you plan to do\n2. Break down complex actions into individual atomic steps\n3. For \\`act\\` commands, use only one action at a time, such as:\n - Single click on a specific element\n - Type into a single input field\n - Select a single option\n4. Avoid combining multiple actions in one instruction\n5. If multiple actions are needed, they should be separate steps\n6. Only use \\`close\\` when the task is genuinely complete or impossible to achieve`,\n };\n}\n\nexport function buildCuaDefaultSystemPrompt(): string {\n return `You are a helpful assistant that can use a web browser.\\nDo not ask follow up questions, the user will trust your judgement. Today's date is ${new Date().toISOString().split(\"T\")[0]}.`;\n}\n\nexport function buildGoogleCUASystemPrompt(): ChatMessage {\n return {\n role: \"system\",\n content: `You are a general-purpose browser agent whose job is to accomplish the user's goal.\nToday's date is ${new Date().toISOString().split(\"T\")[0]}.\nYou have access to a search tool; however, in most cases you should operate within the page/url the user has provided. ONLY use the search tool if you're stuck or the task is impossible to complete within the current page.\nYou will be given a goal and a list of steps that have been taken so far. Avoid requesting the user for input as much as possible. Good luck!\n`,\n };\n}\n"]}
@@ -16,4 +16,12 @@ export declare abstract class AgentClient {
16
16
  abstract setCurrentUrl(url: string): void;
17
17
  abstract setScreenshotProvider(provider: () => Promise<string>): void;
18
18
  abstract setActionHandler(handler: (action: AgentAction) => Promise<void>): void;
19
+ /** Optional hook called at the top of every step in the agent loop. */
20
+ protected preStepHook?: () => Promise<void>;
21
+ setPreStepHook(handler: () => Promise<void>): void;
22
+ /**
23
+ * Optional ephemeral context note that should be sent to the next model turn.
24
+ * Clients that do not support this can ignore it.
25
+ */
26
+ addContextNote(note: string): void;
19
27
  }
@@ -16,6 +16,19 @@ class AgentClient {
16
16
  this.userProvidedInstructions = userProvidedInstructions;
17
17
  this.clientOptions = {};
18
18
  }
19
+ /** Optional hook called at the top of every step in the agent loop. */
20
+ preStepHook;
21
+ setPreStepHook(handler) {
22
+ this.preStepHook = handler;
23
+ }
24
+ /**
25
+ * Optional ephemeral context note that should be sent to the next model turn.
26
+ * Clients that do not support this can ignore it.
27
+ */
28
+ addContextNote(note) {
29
+ void note;
30
+ // no-op by default
31
+ }
19
32
  }
20
33
  exports.AgentClient = AgentClient;
21
34
  //# sourceMappingURL=AgentClient.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"AgentClient.js","sourceRoot":"","sources":["../../../../../lib/v3/agent/AgentClient.ts"],"names":[],"mappings":";;;AAQA;;;GAGG;AACH,MAAsB,WAAW;IACxB,IAAI,CAAY;IAChB,SAAS,CAAS;IAClB,aAAa,CAAgB;IAC7B,wBAAwB,CAAU;IAEzC,YACE,IAAe,EACf,SAAiB,EACjB,wBAAiC;QAEjC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,wBAAwB,GAAG,wBAAwB,CAAC;QACzD,IAAI,CAAC,aAAa,GAAG,EAAE,CAAC;IAC1B,CAAC;CAiBF;AAhCD,kCAgCC","sourcesContent":["import {\n AgentAction,\n AgentResult,\n AgentType,\n AgentExecutionOptions,\n} from \"../types/public/agent.js\";\nimport { ClientOptions } from \"../types/public/model.js\";\n\n/**\n * Abstract base class for agent clients\n * This provides a common interface for all agent implementations\n */\nexport abstract class AgentClient {\n public type: AgentType;\n public modelName: string;\n public clientOptions: ClientOptions;\n public userProvidedInstructions?: string;\n\n constructor(\n type: AgentType,\n modelName: string,\n userProvidedInstructions?: string,\n ) {\n this.type = type;\n this.modelName = modelName;\n this.userProvidedInstructions = userProvidedInstructions;\n this.clientOptions = {};\n }\n\n abstract execute(options: AgentExecutionOptions): Promise<AgentResult>;\n\n abstract captureScreenshot(\n options?: Record<string, unknown>,\n ): Promise<unknown>;\n\n abstract setViewport(width: number, height: number): void;\n\n abstract setCurrentUrl(url: string): void;\n\n abstract setScreenshotProvider(provider: () => Promise<string>): void;\n\n abstract setActionHandler(\n handler: (action: AgentAction) => Promise<void>,\n ): void;\n}\n"]}
1
+ {"version":3,"file":"AgentClient.js","sourceRoot":"","sources":["../../../../../lib/v3/agent/AgentClient.ts"],"names":[],"mappings":";;;AAQA;;;GAGG;AACH,MAAsB,WAAW;IACxB,IAAI,CAAY;IAChB,SAAS,CAAS;IAClB,aAAa,CAAgB;IAC7B,wBAAwB,CAAU;IAEzC,YACE,IAAe,EACf,SAAiB,EACjB,wBAAiC;QAEjC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,wBAAwB,GAAG,wBAAwB,CAAC;QACzD,IAAI,CAAC,aAAa,GAAG,EAAE,CAAC;IAC1B,CAAC;IAkBD,uEAAuE;IAC7D,WAAW,CAAuB;IAE5C,cAAc,CAAC,OAA4B;QACzC,IAAI,CAAC,WAAW,GAAG,OAAO,CAAC;IAC7B,CAAC;IAED;;;OAGG;IACH,cAAc,CAAC,IAAY;QACzB,KAAK,IAAI,CAAC;QACV,mBAAmB;IACrB,CAAC;CACF;AAhDD,kCAgDC","sourcesContent":["import {\n AgentAction,\n AgentResult,\n AgentType,\n AgentExecutionOptions,\n} from \"../types/public/agent.js\";\nimport { ClientOptions } from \"../types/public/model.js\";\n\n/**\n * Abstract base class for agent clients\n * This provides a common interface for all agent implementations\n */\nexport abstract class AgentClient {\n public type: AgentType;\n public modelName: string;\n public clientOptions: ClientOptions;\n public userProvidedInstructions?: string;\n\n constructor(\n type: AgentType,\n modelName: string,\n userProvidedInstructions?: string,\n ) {\n this.type = type;\n this.modelName = modelName;\n this.userProvidedInstructions = userProvidedInstructions;\n this.clientOptions = {};\n }\n\n abstract execute(options: AgentExecutionOptions): Promise<AgentResult>;\n\n abstract captureScreenshot(\n options?: Record<string, unknown>,\n ): Promise<unknown>;\n\n abstract setViewport(width: number, height: number): void;\n\n abstract setCurrentUrl(url: string): void;\n\n abstract setScreenshotProvider(provider: () => Promise<string>): void;\n\n abstract setActionHandler(\n handler: (action: AgentAction) => Promise<void>,\n ): void;\n\n /** Optional hook called at the top of every step in the agent loop. */\n protected preStepHook?: () => Promise<void>;\n\n setPreStepHook(handler: () => Promise<void>): void {\n this.preStepHook = handler;\n }\n\n /**\n * Optional ephemeral context note that should be sent to the next model turn.\n * Clients that do not support this can ignore it.\n */\n addContextNote(note: string): void {\n void note;\n // no-op by default\n }\n}\n"]}
@@ -90,6 +90,7 @@ class AnthropicCUAClient extends AgentClient_js_1.AgentClient {
90
90
  try {
91
91
  // Execute steps until completion or max steps reached
92
92
  while (!completed && currentStep < maxSteps) {
93
+ await this.preStepHook?.();
93
94
  logger({
94
95
  category: "agent",
95
96
  message: `Executing step ${currentStep + 1}/${maxSteps}`,