@aiscene/core 1.1.1

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 (299) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +9 -0
  3. package/dist/es/agent/agent.mjs +753 -0
  4. package/dist/es/agent/agent.mjs.map +1 -0
  5. package/dist/es/agent/common.mjs +0 -0
  6. package/dist/es/agent/execution-session.mjs +41 -0
  7. package/dist/es/agent/execution-session.mjs.map +1 -0
  8. package/dist/es/agent/index.mjs +6 -0
  9. package/dist/es/agent/task-builder.mjs +332 -0
  10. package/dist/es/agent/task-builder.mjs.map +1 -0
  11. package/dist/es/agent/task-cache.mjs +214 -0
  12. package/dist/es/agent/task-cache.mjs.map +1 -0
  13. package/dist/es/agent/tasks.mjs +423 -0
  14. package/dist/es/agent/tasks.mjs.map +1 -0
  15. package/dist/es/agent/ui-utils.mjs +91 -0
  16. package/dist/es/agent/ui-utils.mjs.map +1 -0
  17. package/dist/es/agent/utils.mjs +169 -0
  18. package/dist/es/agent/utils.mjs.map +1 -0
  19. package/dist/es/ai-model/auto-glm/actions.mjs +239 -0
  20. package/dist/es/ai-model/auto-glm/actions.mjs.map +1 -0
  21. package/dist/es/ai-model/auto-glm/index.mjs +6 -0
  22. package/dist/es/ai-model/auto-glm/parser.mjs +239 -0
  23. package/dist/es/ai-model/auto-glm/parser.mjs.map +1 -0
  24. package/dist/es/ai-model/auto-glm/planning.mjs +71 -0
  25. package/dist/es/ai-model/auto-glm/planning.mjs.map +1 -0
  26. package/dist/es/ai-model/auto-glm/prompt.mjs +222 -0
  27. package/dist/es/ai-model/auto-glm/prompt.mjs.map +1 -0
  28. package/dist/es/ai-model/auto-glm/util.mjs +9 -0
  29. package/dist/es/ai-model/auto-glm/util.mjs.map +1 -0
  30. package/dist/es/ai-model/connectivity.mjs +138 -0
  31. package/dist/es/ai-model/connectivity.mjs.map +1 -0
  32. package/dist/es/ai-model/conversation-history.mjs +195 -0
  33. package/dist/es/ai-model/conversation-history.mjs.map +1 -0
  34. package/dist/es/ai-model/index.mjs +12 -0
  35. package/dist/es/ai-model/inspect.mjs +397 -0
  36. package/dist/es/ai-model/inspect.mjs.map +1 -0
  37. package/dist/es/ai-model/llm-planning.mjs +233 -0
  38. package/dist/es/ai-model/llm-planning.mjs.map +1 -0
  39. package/dist/es/ai-model/prompt/common.mjs +7 -0
  40. package/dist/es/ai-model/prompt/common.mjs.map +1 -0
  41. package/dist/es/ai-model/prompt/describe.mjs +66 -0
  42. package/dist/es/ai-model/prompt/describe.mjs.map +1 -0
  43. package/dist/es/ai-model/prompt/extraction.mjs +131 -0
  44. package/dist/es/ai-model/prompt/extraction.mjs.map +1 -0
  45. package/dist/es/ai-model/prompt/llm-locator.mjs +51 -0
  46. package/dist/es/ai-model/prompt/llm-locator.mjs.map +1 -0
  47. package/dist/es/ai-model/prompt/llm-planning.mjs +568 -0
  48. package/dist/es/ai-model/prompt/llm-planning.mjs.map +1 -0
  49. package/dist/es/ai-model/prompt/llm-section-locator.mjs +44 -0
  50. package/dist/es/ai-model/prompt/llm-section-locator.mjs.map +1 -0
  51. package/dist/es/ai-model/prompt/order-sensitive-judge.mjs +35 -0
  52. package/dist/es/ai-model/prompt/order-sensitive-judge.mjs.map +1 -0
  53. package/dist/es/ai-model/prompt/playwright-generator.mjs +117 -0
  54. package/dist/es/ai-model/prompt/playwright-generator.mjs.map +1 -0
  55. package/dist/es/ai-model/prompt/ui-tars-planning.mjs +36 -0
  56. package/dist/es/ai-model/prompt/ui-tars-planning.mjs.map +1 -0
  57. package/dist/es/ai-model/prompt/util.mjs +59 -0
  58. package/dist/es/ai-model/prompt/util.mjs.map +1 -0
  59. package/dist/es/ai-model/prompt/yaml-generator.mjs +203 -0
  60. package/dist/es/ai-model/prompt/yaml-generator.mjs.map +1 -0
  61. package/dist/es/ai-model/service-caller/codex-app-server.mjs +575 -0
  62. package/dist/es/ai-model/service-caller/codex-app-server.mjs.map +1 -0
  63. package/dist/es/ai-model/service-caller/image-detail.mjs +6 -0
  64. package/dist/es/ai-model/service-caller/image-detail.mjs.map +1 -0
  65. package/dist/es/ai-model/service-caller/index.mjs +475 -0
  66. package/dist/es/ai-model/service-caller/index.mjs.map +1 -0
  67. package/dist/es/ai-model/ui-tars-planning.mjs +249 -0
  68. package/dist/es/ai-model/ui-tars-planning.mjs.map +1 -0
  69. package/dist/es/common.mjs +371 -0
  70. package/dist/es/common.mjs.map +1 -0
  71. package/dist/es/device/device-options.mjs +0 -0
  72. package/dist/es/device/index.mjs +341 -0
  73. package/dist/es/device/index.mjs.map +1 -0
  74. package/dist/es/dump/html-utils.mjs +292 -0
  75. package/dist/es/dump/html-utils.mjs.map +1 -0
  76. package/dist/es/dump/index.mjs +3 -0
  77. package/dist/es/dump/screenshot-restoration.mjs +32 -0
  78. package/dist/es/dump/screenshot-restoration.mjs.map +1 -0
  79. package/dist/es/dump/screenshot-store.mjs +126 -0
  80. package/dist/es/dump/screenshot-store.mjs.map +1 -0
  81. package/dist/es/index.mjs +19 -0
  82. package/dist/es/index.mjs.map +1 -0
  83. package/dist/es/report-cli.mjs +151 -0
  84. package/dist/es/report-cli.mjs.map +1 -0
  85. package/dist/es/report-generator.mjs +205 -0
  86. package/dist/es/report-generator.mjs.map +1 -0
  87. package/dist/es/report-markdown.mjs +218 -0
  88. package/dist/es/report-markdown.mjs.map +1 -0
  89. package/dist/es/report.mjs +270 -0
  90. package/dist/es/report.mjs.map +1 -0
  91. package/dist/es/screenshot-item.mjs +122 -0
  92. package/dist/es/screenshot-item.mjs.map +1 -0
  93. package/dist/es/service/index.mjs +274 -0
  94. package/dist/es/service/index.mjs.map +1 -0
  95. package/dist/es/service/utils.mjs +15 -0
  96. package/dist/es/service/utils.mjs.map +1 -0
  97. package/dist/es/skill/index.mjs +38 -0
  98. package/dist/es/skill/index.mjs.map +1 -0
  99. package/dist/es/task-runner.mjs +263 -0
  100. package/dist/es/task-runner.mjs.map +1 -0
  101. package/dist/es/task-timing.mjs +12 -0
  102. package/dist/es/task-timing.mjs.map +1 -0
  103. package/dist/es/tree.mjs +13 -0
  104. package/dist/es/tree.mjs.map +1 -0
  105. package/dist/es/types.mjs +204 -0
  106. package/dist/es/types.mjs.map +1 -0
  107. package/dist/es/utils.mjs +234 -0
  108. package/dist/es/utils.mjs.map +1 -0
  109. package/dist/es/yaml/builder.mjs +13 -0
  110. package/dist/es/yaml/builder.mjs.map +1 -0
  111. package/dist/es/yaml/index.mjs +4 -0
  112. package/dist/es/yaml/player.mjs +442 -0
  113. package/dist/es/yaml/player.mjs.map +1 -0
  114. package/dist/es/yaml/utils.mjs +102 -0
  115. package/dist/es/yaml/utils.mjs.map +1 -0
  116. package/dist/es/yaml.mjs +0 -0
  117. package/dist/lib/agent/agent.js +801 -0
  118. package/dist/lib/agent/agent.js.map +1 -0
  119. package/dist/lib/agent/common.js +5 -0
  120. package/dist/lib/agent/execution-session.js +75 -0
  121. package/dist/lib/agent/execution-session.js.map +1 -0
  122. package/dist/lib/agent/index.js +78 -0
  123. package/dist/lib/agent/index.js.map +1 -0
  124. package/dist/lib/agent/task-builder.js +369 -0
  125. package/dist/lib/agent/task-builder.js.map +1 -0
  126. package/dist/lib/agent/task-cache.js +266 -0
  127. package/dist/lib/agent/task-cache.js.map +1 -0
  128. package/dist/lib/agent/tasks.js +466 -0
  129. package/dist/lib/agent/tasks.js.map +1 -0
  130. package/dist/lib/agent/ui-utils.js +143 -0
  131. package/dist/lib/agent/ui-utils.js.map +1 -0
  132. package/dist/lib/agent/utils.js +240 -0
  133. package/dist/lib/agent/utils.js.map +1 -0
  134. package/dist/lib/ai-model/auto-glm/actions.js +273 -0
  135. package/dist/lib/ai-model/auto-glm/actions.js.map +1 -0
  136. package/dist/lib/ai-model/auto-glm/index.js +66 -0
  137. package/dist/lib/ai-model/auto-glm/index.js.map +1 -0
  138. package/dist/lib/ai-model/auto-glm/parser.js +282 -0
  139. package/dist/lib/ai-model/auto-glm/parser.js.map +1 -0
  140. package/dist/lib/ai-model/auto-glm/planning.js +105 -0
  141. package/dist/lib/ai-model/auto-glm/planning.js.map +1 -0
  142. package/dist/lib/ai-model/auto-glm/prompt.js +259 -0
  143. package/dist/lib/ai-model/auto-glm/prompt.js.map +1 -0
  144. package/dist/lib/ai-model/auto-glm/util.js +46 -0
  145. package/dist/lib/ai-model/auto-glm/util.js.map +1 -0
  146. package/dist/lib/ai-model/connectivity.js +182 -0
  147. package/dist/lib/ai-model/connectivity.js.map +1 -0
  148. package/dist/lib/ai-model/conversation-history.js +229 -0
  149. package/dist/lib/ai-model/conversation-history.js.map +1 -0
  150. package/dist/lib/ai-model/index.js +129 -0
  151. package/dist/lib/ai-model/index.js.map +1 -0
  152. package/dist/lib/ai-model/inspect.js +443 -0
  153. package/dist/lib/ai-model/inspect.js.map +1 -0
  154. package/dist/lib/ai-model/llm-planning.js +270 -0
  155. package/dist/lib/ai-model/llm-planning.js.map +1 -0
  156. package/dist/lib/ai-model/prompt/common.js +41 -0
  157. package/dist/lib/ai-model/prompt/common.js.map +1 -0
  158. package/dist/lib/ai-model/prompt/describe.js +100 -0
  159. package/dist/lib/ai-model/prompt/describe.js.map +1 -0
  160. package/dist/lib/ai-model/prompt/extraction.js +171 -0
  161. package/dist/lib/ai-model/prompt/extraction.js.map +1 -0
  162. package/dist/lib/ai-model/prompt/llm-locator.js +88 -0
  163. package/dist/lib/ai-model/prompt/llm-locator.js.map +1 -0
  164. package/dist/lib/ai-model/prompt/llm-planning.js +605 -0
  165. package/dist/lib/ai-model/prompt/llm-planning.js.map +1 -0
  166. package/dist/lib/ai-model/prompt/llm-section-locator.js +81 -0
  167. package/dist/lib/ai-model/prompt/llm-section-locator.js.map +1 -0
  168. package/dist/lib/ai-model/prompt/order-sensitive-judge.js +72 -0
  169. package/dist/lib/ai-model/prompt/order-sensitive-judge.js.map +1 -0
  170. package/dist/lib/ai-model/prompt/playwright-generator.js +178 -0
  171. package/dist/lib/ai-model/prompt/playwright-generator.js.map +1 -0
  172. package/dist/lib/ai-model/prompt/ui-tars-planning.js +73 -0
  173. package/dist/lib/ai-model/prompt/ui-tars-planning.js.map +1 -0
  174. package/dist/lib/ai-model/prompt/util.js +105 -0
  175. package/dist/lib/ai-model/prompt/util.js.map +1 -0
  176. package/dist/lib/ai-model/prompt/yaml-generator.js +264 -0
  177. package/dist/lib/ai-model/prompt/yaml-generator.js.map +1 -0
  178. package/dist/lib/ai-model/service-caller/codex-app-server.js +624 -0
  179. package/dist/lib/ai-model/service-caller/codex-app-server.js.map +1 -0
  180. package/dist/lib/ai-model/service-caller/image-detail.js +40 -0
  181. package/dist/lib/ai-model/service-caller/image-detail.js.map +1 -0
  182. package/dist/lib/ai-model/service-caller/index.js +540 -0
  183. package/dist/lib/ai-model/service-caller/index.js.map +1 -0
  184. package/dist/lib/ai-model/ui-tars-planning.js +283 -0
  185. package/dist/lib/ai-model/ui-tars-planning.js.map +1 -0
  186. package/dist/lib/common.js +480 -0
  187. package/dist/lib/common.js.map +1 -0
  188. package/dist/lib/device/device-options.js +20 -0
  189. package/dist/lib/device/device-options.js.map +1 -0
  190. package/dist/lib/device/index.js +468 -0
  191. package/dist/lib/device/index.js.map +1 -0
  192. package/dist/lib/dump/html-utils.js +368 -0
  193. package/dist/lib/dump/html-utils.js.map +1 -0
  194. package/dist/lib/dump/index.js +60 -0
  195. package/dist/lib/dump/index.js.map +1 -0
  196. package/dist/lib/dump/screenshot-restoration.js +66 -0
  197. package/dist/lib/dump/screenshot-restoration.js.map +1 -0
  198. package/dist/lib/dump/screenshot-store.js +166 -0
  199. package/dist/lib/dump/screenshot-store.js.map +1 -0
  200. package/dist/lib/index.js +186 -0
  201. package/dist/lib/index.js.map +1 -0
  202. package/dist/lib/report-cli.js +191 -0
  203. package/dist/lib/report-cli.js.map +1 -0
  204. package/dist/lib/report-generator.js +246 -0
  205. package/dist/lib/report-generator.js.map +1 -0
  206. package/dist/lib/report-markdown.js +255 -0
  207. package/dist/lib/report-markdown.js.map +1 -0
  208. package/dist/lib/report.js +316 -0
  209. package/dist/lib/report.js.map +1 -0
  210. package/dist/lib/screenshot-item.js +156 -0
  211. package/dist/lib/screenshot-item.js.map +1 -0
  212. package/dist/lib/service/index.js +308 -0
  213. package/dist/lib/service/index.js.map +1 -0
  214. package/dist/lib/service/utils.js +49 -0
  215. package/dist/lib/service/utils.js.map +1 -0
  216. package/dist/lib/skill/index.js +72 -0
  217. package/dist/lib/skill/index.js.map +1 -0
  218. package/dist/lib/task-runner.js +300 -0
  219. package/dist/lib/task-runner.js.map +1 -0
  220. package/dist/lib/task-timing.js +46 -0
  221. package/dist/lib/task-timing.js.map +1 -0
  222. package/dist/lib/tree.js +53 -0
  223. package/dist/lib/tree.js.map +1 -0
  224. package/dist/lib/types.js +300 -0
  225. package/dist/lib/types.js.map +1 -0
  226. package/dist/lib/utils.js +316 -0
  227. package/dist/lib/utils.js.map +1 -0
  228. package/dist/lib/yaml/builder.js +57 -0
  229. package/dist/lib/yaml/builder.js.map +1 -0
  230. package/dist/lib/yaml/index.js +81 -0
  231. package/dist/lib/yaml/index.js.map +1 -0
  232. package/dist/lib/yaml/player.js +476 -0
  233. package/dist/lib/yaml/player.js.map +1 -0
  234. package/dist/lib/yaml/utils.js +155 -0
  235. package/dist/lib/yaml/utils.js.map +1 -0
  236. package/dist/lib/yaml.js +20 -0
  237. package/dist/lib/yaml.js.map +1 -0
  238. package/dist/types/agent/agent.d.ts +216 -0
  239. package/dist/types/agent/common.d.ts +0 -0
  240. package/dist/types/agent/execution-session.d.ts +36 -0
  241. package/dist/types/agent/index.d.ts +9 -0
  242. package/dist/types/agent/task-builder.d.ts +34 -0
  243. package/dist/types/agent/task-cache.d.ts +49 -0
  244. package/dist/types/agent/tasks.d.ts +69 -0
  245. package/dist/types/agent/ui-utils.d.ts +14 -0
  246. package/dist/types/agent/utils.d.ts +25 -0
  247. package/dist/types/ai-model/auto-glm/actions.d.ts +78 -0
  248. package/dist/types/ai-model/auto-glm/index.d.ts +6 -0
  249. package/dist/types/ai-model/auto-glm/parser.d.ts +18 -0
  250. package/dist/types/ai-model/auto-glm/planning.d.ts +12 -0
  251. package/dist/types/ai-model/auto-glm/prompt.d.ts +27 -0
  252. package/dist/types/ai-model/auto-glm/util.d.ts +13 -0
  253. package/dist/types/ai-model/connectivity.d.ts +20 -0
  254. package/dist/types/ai-model/conversation-history.d.ts +105 -0
  255. package/dist/types/ai-model/index.d.ts +16 -0
  256. package/dist/types/ai-model/inspect.d.ts +67 -0
  257. package/dist/types/ai-model/llm-planning.d.ts +19 -0
  258. package/dist/types/ai-model/prompt/common.d.ts +2 -0
  259. package/dist/types/ai-model/prompt/describe.d.ts +1 -0
  260. package/dist/types/ai-model/prompt/extraction.d.ts +7 -0
  261. package/dist/types/ai-model/prompt/llm-locator.d.ts +3 -0
  262. package/dist/types/ai-model/prompt/llm-planning.d.ts +10 -0
  263. package/dist/types/ai-model/prompt/llm-section-locator.d.ts +3 -0
  264. package/dist/types/ai-model/prompt/order-sensitive-judge.d.ts +2 -0
  265. package/dist/types/ai-model/prompt/playwright-generator.d.ts +26 -0
  266. package/dist/types/ai-model/prompt/ui-tars-planning.d.ts +2 -0
  267. package/dist/types/ai-model/prompt/util.d.ts +33 -0
  268. package/dist/types/ai-model/prompt/yaml-generator.d.ts +102 -0
  269. package/dist/types/ai-model/service-caller/codex-app-server.d.ts +42 -0
  270. package/dist/types/ai-model/service-caller/image-detail.d.ts +2 -0
  271. package/dist/types/ai-model/service-caller/index.d.ts +49 -0
  272. package/dist/types/ai-model/ui-tars-planning.d.ts +72 -0
  273. package/dist/types/common.d.ts +288 -0
  274. package/dist/types/device/device-options.d.ts +145 -0
  275. package/dist/types/device/index.d.ts +2528 -0
  276. package/dist/types/dump/html-utils.d.ts +75 -0
  277. package/dist/types/dump/index.d.ts +5 -0
  278. package/dist/types/dump/screenshot-restoration.d.ts +8 -0
  279. package/dist/types/dump/screenshot-store.d.ts +49 -0
  280. package/dist/types/index.d.ts +21 -0
  281. package/dist/types/report-cli.d.ts +36 -0
  282. package/dist/types/report-generator.d.ts +81 -0
  283. package/dist/types/report-markdown.d.ts +24 -0
  284. package/dist/types/report.d.ts +52 -0
  285. package/dist/types/screenshot-item.d.ts +67 -0
  286. package/dist/types/service/index.d.ts +24 -0
  287. package/dist/types/service/utils.d.ts +2 -0
  288. package/dist/types/skill/index.d.ts +25 -0
  289. package/dist/types/task-runner.d.ts +50 -0
  290. package/dist/types/task-timing.d.ts +8 -0
  291. package/dist/types/tree.d.ts +4 -0
  292. package/dist/types/types.d.ts +681 -0
  293. package/dist/types/utils.d.ts +45 -0
  294. package/dist/types/yaml/builder.d.ts +2 -0
  295. package/dist/types/yaml/index.d.ts +4 -0
  296. package/dist/types/yaml/player.d.ts +34 -0
  297. package/dist/types/yaml/utils.d.ts +9 -0
  298. package/dist/types/yaml.d.ts +215 -0
  299. package/package.json +111 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"task-runner.mjs","sources":["../../src/task-runner.ts"],"sourcesContent":["import type { ScreenshotItem } from '@/screenshot-item';\nimport { setTimingFieldOnce } from '@/task-timing';\nimport {\n ExecutionDump,\n type ExecutionRecorderItem,\n type ExecutionTask,\n type ExecutionTaskActionApply,\n type ExecutionTaskApply,\n type ExecutionTaskPlanningLocateOutput,\n type ExecutionTaskProgressOptions,\n type ExecutionTaskReturn,\n type ExecutorContext,\n type PlanningActionParamError,\n type UIContext,\n} from '@/types';\nimport { getDebug } from '@midscene/shared/logger';\nimport { assert, uuid } from '@midscene/shared/utils';\n\nconst debug = getDebug('task-runner');\nconst UI_CONTEXT_CACHE_TTL_MS = 300;\n\ntype TaskRunnerInitOptions = ExecutionTaskProgressOptions & {\n tasks?: ExecutionTaskApply[];\n onTaskUpdate?: (\n runner: TaskRunner,\n error?: TaskExecutionError,\n ) => Promise<void> | void;\n};\n\ntype TaskRunnerOperationOptions = {\n allowWhenError?: boolean;\n};\n\nexport class TaskRunner {\n readonly id: string;\n name: string;\n\n tasks: ExecutionTask[];\n\n // status of runner\n status: 'init' | 'pending' | 'running' | 'completed' | 'error';\n\n onTaskStart?: ExecutionTaskProgressOptions['onTaskStart'];\n\n private readonly uiContextBuilder: () => Promise<UIContext>;\n\n private readonly onTaskUpdate?:\n | ((runner: TaskRunner, error?: TaskExecutionError) => Promise<void> | void)\n | undefined;\n\n private readonly executionLogTime: number;\n\n constructor(\n name: string,\n uiContextBuilder: () => Promise<UIContext>,\n options?: TaskRunnerInitOptions,\n ) {\n this.id = uuid();\n this.status =\n options?.tasks && options.tasks.length > 0 ? 'pending' : 'init';\n this.name = name;\n this.tasks = (options?.tasks || []).map((item) =>\n this.markTaskAsPending(item),\n );\n this.onTaskStart = options?.onTaskStart;\n this.uiContextBuilder = uiContextBuilder;\n this.onTaskUpdate = options?.onTaskUpdate;\n this.executionLogTime = Date.now();\n }\n\n private async emitOnTaskUpdate(error?: TaskExecutionError): Promise<void> {\n if (!this.onTaskUpdate) {\n return;\n }\n await this.onTaskUpdate(this, error);\n }\n\n private lastUiContext?: {\n context: UIContext;\n capturedAt: number;\n };\n\n private async getUiContext(options?: { forceRefresh?: boolean }): Promise<\n UIContext | undefined\n > {\n const now = Date.now();\n const shouldReuse =\n !options?.forceRefresh &&\n this.lastUiContext &&\n now - this.lastUiContext.capturedAt <= UI_CONTEXT_CACHE_TTL_MS;\n\n if (shouldReuse && this.lastUiContext?.context) {\n debug(\n `reuse cached uiContext captured ${now - this.lastUiContext.capturedAt}ms ago`,\n );\n return this.lastUiContext?.context;\n }\n\n try {\n const uiContext = await this.uiContextBuilder();\n if (uiContext) {\n this.lastUiContext = {\n context: uiContext,\n capturedAt: Date.now(),\n };\n } else {\n this.lastUiContext = undefined;\n }\n return uiContext;\n } catch (error) {\n this.lastUiContext = undefined;\n throw error;\n }\n }\n\n private async captureScreenshot(): Promise<ScreenshotItem | undefined> {\n try {\n const uiContext = await this.getUiContext({ forceRefresh: true });\n return uiContext?.screenshot;\n } catch (error) {\n console.error('error while capturing screenshot', error);\n }\n return undefined;\n }\n\n private attachRecorderItem(\n task: ExecutionTask,\n screenshot: ScreenshotItem | undefined,\n phase: 'after-calling',\n ): void {\n if (!phase || !screenshot) {\n return;\n }\n\n const recorderItem: ExecutionRecorderItem = {\n type: 'screenshot',\n ts: Date.now(),\n screenshot,\n timing: phase,\n };\n\n if (!task.recorder) {\n task.recorder = [recorderItem];\n return;\n }\n task.recorder.push(recorderItem);\n }\n\n private markTaskAsPending(task: ExecutionTaskApply): ExecutionTask {\n return {\n taskId: uuid(),\n status: 'pending',\n ...task,\n };\n }\n\n private normalizeStatusFromError(\n options?: TaskRunnerOperationOptions,\n errorMessage?: string,\n ): void {\n if (this.status !== 'error') {\n return;\n }\n assert(\n options?.allowWhenError,\n errorMessage ||\n `task runner is in error state, cannot proceed\\nerror=${this.latestErrorTask()?.error}\\n${this.latestErrorTask()?.errorStack}`,\n );\n // reset runner state so new tasks can run\n this.status = this.tasks.length > 0 ? 'pending' : 'init';\n }\n\n async append(\n task: ExecutionTaskApply[] | ExecutionTaskApply,\n options?: TaskRunnerOperationOptions,\n ): Promise<void> {\n this.normalizeStatusFromError(\n options,\n `task runner is in error state, cannot append task\\nerror=${this.latestErrorTask()?.error}\\n${this.latestErrorTask()?.errorStack}`,\n );\n if (Array.isArray(task)) {\n this.tasks.push(...task.map((item) => this.markTaskAsPending(item)));\n } else {\n this.tasks.push(this.markTaskAsPending(task));\n }\n if (this.status !== 'running') {\n this.status = 'pending';\n }\n await this.emitOnTaskUpdate();\n }\n\n async appendAndFlush(\n task: ExecutionTaskApply[] | ExecutionTaskApply,\n options?: TaskRunnerOperationOptions,\n ): Promise<{ output: any; thought?: string } | undefined> {\n await this.append(task, options);\n return this.flush(options);\n }\n\n async flush(\n options?: TaskRunnerOperationOptions,\n ): Promise<{ output: any; thought?: string } | undefined> {\n if (this.status === 'init' && this.tasks.length > 0) {\n console.warn(\n 'illegal state for task runner, status is init but tasks are not empty',\n );\n }\n\n this.normalizeStatusFromError(options, 'task runner is in error state');\n assert(this.status !== 'running', 'task runner is already running');\n assert(this.status !== 'completed', 'task runner is already completed');\n\n const nextPendingIndex = this.tasks.findIndex(\n (task) => task.status === 'pending',\n );\n if (nextPendingIndex < 0) {\n // all tasks are completed\n return;\n }\n\n this.status = 'running';\n await this.emitOnTaskUpdate();\n let taskIndex = nextPendingIndex;\n let successfullyCompleted = true;\n\n let previousFindOutput: ExecutionTaskPlanningLocateOutput | undefined;\n\n while (taskIndex < this.tasks.length) {\n const task = this.tasks[taskIndex];\n assert(\n task.status === 'pending',\n `task status should be pending, but got: ${task.status}`,\n );\n task.timing = {\n start: Date.now(),\n };\n try {\n task.status = 'running';\n await this.emitOnTaskUpdate();\n try {\n if (this.onTaskStart) {\n await this.onTaskStart(task);\n }\n } catch (e) {\n console.error('error in onTaskStart', e);\n }\n assert(\n ['Insight', 'Action Space', 'Planning'].indexOf(task.type) >= 0,\n `unsupported task type: ${task.type}`,\n );\n\n const { executor, param } = task;\n assert(executor, `executor is required for task type: ${task.type}`);\n\n let returnValue;\n // For Insight tasks (Query/Assert/WaitFor), always get fresh context\n // to ensure we have the latest UI state after any preceding actions\n const forceRefresh = task.type === 'Insight';\n setTimingFieldOnce(task.timing, 'getUiContextStart');\n const uiContext = await this.getUiContext({ forceRefresh });\n setTimingFieldOnce(task.timing, 'getUiContextEnd');\n\n task.uiContext = uiContext;\n const executorContext: ExecutorContext = {\n task,\n element: previousFindOutput?.element,\n uiContext,\n };\n\n if (task.type === 'Insight') {\n assert(\n task.subType === 'Query' ||\n task.subType === 'Assert' ||\n task.subType === 'WaitFor' ||\n task.subType === 'Boolean' ||\n task.subType === 'Number' ||\n task.subType === 'String',\n `unsupported service subType: ${task.subType}`,\n );\n returnValue = await task.executor(param, executorContext);\n } else if (task.type === 'Planning') {\n returnValue = await task.executor(param, executorContext);\n if (task.subType === 'Locate') {\n previousFindOutput = (\n returnValue as ExecutionTaskReturn<ExecutionTaskPlanningLocateOutput>\n )?.output;\n }\n } else if (task.type === 'Action Space') {\n returnValue = await task.executor(param, executorContext);\n } else {\n console.warn(\n `unsupported task type: ${task.type}, will try to execute it directly`,\n );\n returnValue = await task.executor(param, executorContext);\n }\n\n const isLastTask = taskIndex === this.tasks.length - 1;\n\n if (isLastTask) {\n setTimingFieldOnce(task.timing, 'captureAfterCallingSnapshotStart');\n const screenshot = await this.captureScreenshot();\n this.attachRecorderItem(task, screenshot, 'after-calling');\n setTimingFieldOnce(task.timing, 'captureAfterCallingSnapshotEnd');\n }\n\n Object.assign(task, returnValue);\n task.status = 'finished';\n task.timing.end = Date.now();\n task.timing.cost = task.timing.end - task.timing.start;\n await this.emitOnTaskUpdate();\n taskIndex++;\n } catch (e: any) {\n successfullyCompleted = false;\n task.error = e;\n task.errorMessage =\n e?.message || (typeof e === 'string' ? e : 'error-without-message');\n task.errorStack = e.stack;\n\n task.status = 'failed';\n task.timing.end = Date.now();\n task.timing.cost = task.timing.end - task.timing.start;\n await this.emitOnTaskUpdate();\n break;\n }\n }\n\n // set all remaining tasks as cancelled\n for (let i = taskIndex + 1; i < this.tasks.length; i++) {\n this.tasks[i].status = 'cancelled';\n }\n if (taskIndex + 1 < this.tasks.length) {\n await this.emitOnTaskUpdate();\n }\n\n let finalizeError: TaskExecutionError | undefined;\n if (!successfullyCompleted) {\n this.status = 'error';\n const errorTask = this.latestErrorTask();\n const messageBase =\n errorTask?.errorMessage ||\n (errorTask?.error ? String(errorTask.error) : 'Task execution failed');\n const stack = errorTask?.errorStack;\n const message = stack ? `${messageBase}\\n${stack}` : messageBase;\n finalizeError = new TaskExecutionError(message, this, errorTask, {\n cause: errorTask?.error,\n });\n await this.emitOnTaskUpdate(finalizeError);\n } else {\n this.status = 'completed';\n await this.emitOnTaskUpdate();\n }\n\n if (finalizeError) {\n throw finalizeError;\n }\n\n if (this.tasks.length) {\n // return the last output\n const outputIndex = Math.min(taskIndex, this.tasks.length - 1);\n const { thought, output } = this.tasks[outputIndex];\n return {\n thought,\n output,\n };\n }\n }\n\n isInErrorState(): boolean {\n return this.status === 'error';\n }\n\n latestErrorTask(): ExecutionTask | null {\n if (this.status !== 'error') {\n return null;\n }\n // Find the LAST failed task (not the first one)\n // This is important when using allowWhenError to continue after errors\n for (let i = this.tasks.length - 1; i >= 0; i--) {\n if (this.tasks[i].status === 'failed') {\n return this.tasks[i];\n }\n }\n return null;\n }\n\n dump(): ExecutionDump {\n return new ExecutionDump({\n id: this.id,\n logTime: this.executionLogTime,\n name: this.name,\n tasks: this.tasks,\n });\n }\n\n async appendErrorPlan(errorMsg: string): Promise<{\n output: undefined;\n runner: TaskRunner;\n }> {\n const errorTask: ExecutionTaskActionApply<PlanningActionParamError> = {\n type: 'Action Space',\n subType: 'Error',\n param: {\n thought: errorMsg,\n },\n thought: errorMsg,\n executor: async () => {\n throw new Error(errorMsg || 'error without thought');\n },\n };\n await this.appendAndFlush(errorTask);\n\n return {\n output: undefined,\n runner: this,\n };\n }\n}\n\nexport class TaskExecutionError extends Error {\n runner: TaskRunner;\n\n errorTask: ExecutionTask | null;\n\n constructor(\n message: string,\n runner: TaskRunner,\n errorTask: ExecutionTask | null,\n options?: { cause?: unknown },\n ) {\n super(message, options);\n this.runner = runner;\n this.errorTask = errorTask;\n }\n}\n"],"names":["debug","getDebug","UI_CONTEXT_CACHE_TTL_MS","TaskRunner","error","options","now","Date","shouldReuse","uiContext","undefined","console","task","screenshot","phase","recorderItem","uuid","errorMessage","assert","Array","item","nextPendingIndex","taskIndex","successfullyCompleted","previousFindOutput","e","executor","param","returnValue","forceRefresh","setTimingFieldOnce","executorContext","isLastTask","Object","i","finalizeError","errorTask","messageBase","String","stack","message","TaskExecutionError","outputIndex","Math","thought","output","ExecutionDump","errorMsg","Error","name","uiContextBuilder","runner"],"mappings":";;;;;;;;;;;;;;AAkBA,MAAMA,QAAQC,SAAS;AACvB,MAAMC,0BAA0B;AAczB,MAAMC;IAqCX,MAAc,iBAAiBC,KAA0B,EAAiB;QACxE,IAAI,CAAC,IAAI,CAAC,YAAY,EACpB;QAEF,MAAM,IAAI,CAAC,YAAY,CAAC,IAA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ApBE,IAAI,CAAC,EAAE,GAAGW;QACV,IAAI,CAAC,MAAM,GACTX,SAAS,SAASA,QAAQ,KAAK,CAAC,MAAM,GAAG,IAAI,YAAY;QAC3D,IAAI,CAAC,IAAI,GAAG4C;QACZ,IAAI,CAAC,KAAK,GAAI5C,AAAAA,CAAAA,SAAS,SAAS,EAAC,EAAG,GAAG,CAAC,CAACe,OACvC,IAAI,CAAC,iBAAiB,CAACA;QAEzB,IAAI,CAAC,WAAW,GAAGf,SAAS;QAC5B,IAAI,CAAC,gBAAgB,GAAG6C;QACxB,IAAI,CAAC,YAAY,GAAG7C,SAAS;QAC7B,IAAI,CAAC,gBAAgB,GAAGE,KAAK,GAAG;IAClC;AA4VF;AAEO,MAAMkC,2BAA2BO;IAKtC,YACER,OAAe,EACfW,MAAkB,EAClBf,SAA+B,EAC/B/B,OAA6B,CAC7B;QACA,KAAK,CAACmC,SAASnC,UAVjB,0CAEA;QASE,IAAI,CAAC,MAAM,GAAG8C;QACd,IAAI,CAAC,SAAS,GAAGf;IACnB;AACF"}
@@ -0,0 +1,12 @@
1
+ import { getDebug } from "@midscene/shared/logger";
2
+ const debugTiming = getDebug('task-timing');
3
+ function setTimingFieldOnce(timing, field) {
4
+ if (!timing) return void debugTiming(`[warning] timing object missing, skip set. field=${field}`);
5
+ const value = Date.now();
6
+ const existingValue = timing[field];
7
+ if (void 0 !== existingValue) return void debugTiming(`[warning] duplicate timing field set ignored. field=${field}, existing=${existingValue}, incoming=${value}`);
8
+ timing[field] = value;
9
+ }
10
+ export { setTimingFieldOnce };
11
+
12
+ //# sourceMappingURL=task-timing.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"task-timing.mjs","sources":["../../src/task-timing.ts"],"sourcesContent":["import type { ExecutionTask } from '@/types';\nimport { getDebug } from '@midscene/shared/logger';\n\nconst debugTiming = getDebug('task-timing');\n\ntype ExecutionTaskTiming = NonNullable<ExecutionTask['timing']>;\n\ntype NumericTimingField = {\n [K in keyof ExecutionTaskTiming]-?: ExecutionTaskTiming[K] extends\n | number\n | undefined\n ? K\n : never;\n}[keyof ExecutionTaskTiming];\n\nexport type TimingSettableField = Exclude<\n NumericTimingField,\n 'start' | 'end' | 'cost'\n>;\n\nexport function setTimingFieldOnce(\n timing: ExecutionTaskTiming | undefined,\n field: TimingSettableField,\n): void {\n if (!timing) {\n debugTiming(`[warning] timing object missing, skip set. field=${field}`);\n return;\n }\n\n const value = Date.now();\n const existingValue = timing[field];\n if (existingValue !== undefined) {\n debugTiming(\n `[warning] duplicate timing field set ignored. field=${field}, existing=${existingValue}, incoming=${value}`,\n );\n return;\n }\n\n timing[field] = value;\n}\n"],"names":["debugTiming","getDebug","setTimingFieldOnce","timing","field","value","Date","existingValue","undefined"],"mappings":";AAGA,MAAMA,cAAcC,SAAS;AAiBtB,SAASC,mBACdC,MAAuC,EACvCC,KAA0B;IAE1B,IAAI,CAACD,QAAQ,YACXH,YAAY,CAAC,iDAAiD,EAAEI,OAAO;IAIzE,MAAMC,QAAQC,KAAK,GAAG;IACtB,MAAMC,gBAAgBJ,MAAM,CAACC,MAAM;IACnC,IAAIG,AAAkBC,WAAlBD,eAA6B,YAC/BP,YACE,CAAC,oDAAoD,EAAEI,MAAM,WAAW,EAAEG,cAAc,WAAW,EAAEF,OAAO;IAKhHF,MAAM,CAACC,MAAM,GAAGC;AAClB"}
@@ -0,0 +1,13 @@
1
+ import { descriptionOfTree, treeToList, trimAttributes, truncateText } from "@midscene/shared/extractor";
2
+ const ELEMENT_COUNT_WARNING_THRESHOLD = 5000;
3
+ const TREE_SIZE_WARNING_MESSAGE = 'The number of elements is too large, it may cause the prompt to be too long, please use domIncluded: "visible-only" to reduce the number of elements';
4
+ function tree_descriptionOfTree(tree, truncateTextLength, filterNonTextContent = false, visibleOnly = true) {
5
+ if (!visibleOnly) {
6
+ const flatElements = treeToList(tree);
7
+ if (flatElements.length >= ELEMENT_COUNT_WARNING_THRESHOLD) console.warn(TREE_SIZE_WARNING_MESSAGE);
8
+ }
9
+ return descriptionOfTree(tree, truncateTextLength, filterNonTextContent, visibleOnly);
10
+ }
11
+ export { tree_descriptionOfTree as descriptionOfTree, trimAttributes, truncateText };
12
+
13
+ //# sourceMappingURL=tree.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tree.mjs","sources":["../../src/tree.ts"],"sourcesContent":["import type { BaseElement, ElementTreeNode } from '@midscene/shared/types';\n\nimport {\n descriptionOfTree as sharedDescriptionOfTree,\n treeToList,\n trimAttributes,\n truncateText,\n} from '@midscene/shared/extractor';\n\nconst ELEMENT_COUNT_WARNING_THRESHOLD = 5000;\nconst TREE_SIZE_WARNING_MESSAGE =\n 'The number of elements is too large, it may cause the prompt to be too long, please use domIncluded: \"visible-only\" to reduce the number of elements';\n\nexport { trimAttributes, truncateText };\n\nexport function descriptionOfTree<\n ElementType extends BaseElement = BaseElement,\n>(\n tree: ElementTreeNode<ElementType>,\n truncateTextLength?: number,\n filterNonTextContent = false,\n visibleOnly = true,\n) {\n if (!visibleOnly) {\n const flatElements = treeToList(tree);\n if (flatElements.length >= ELEMENT_COUNT_WARNING_THRESHOLD) {\n console.warn(TREE_SIZE_WARNING_MESSAGE);\n }\n }\n\n return sharedDescriptionOfTree(\n tree,\n truncateTextLength,\n filterNonTextContent,\n visibleOnly,\n );\n}\n"],"names":["ELEMENT_COUNT_WARNING_THRESHOLD","TREE_SIZE_WARNING_MESSAGE","descriptionOfTree","tree","truncateTextLength","filterNonTextContent","visibleOnly","flatElements","treeToList","console","sharedDescriptionOfTree"],"mappings":";AASA,MAAMA,kCAAkC;AACxC,MAAMC,4BACJ;AAIK,SAASC,uBAGdC,IAAkC,EAClCC,kBAA2B,EAC3BC,uBAAuB,KAAK,EAC5BC,cAAc,IAAI;IAElB,IAAI,CAACA,aAAa;QAChB,MAAMC,eAAeC,WAAWL;QAChC,IAAII,aAAa,MAAM,IAAIP,iCACzBS,QAAQ,IAAI,CAACR;IAEjB;IAEA,OAAOS,kBACLP,MACAC,oBACAC,sBACAC;AAEJ"}
@@ -0,0 +1,204 @@
1
+ import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
2
+ import { join } from "node:path";
3
+ import { restoreImageReferences } from "./dump/screenshot-restoration.mjs";
4
+ import { ScreenshotStore } from "./dump/screenshot-store.mjs";
5
+ import { ScreenshotItem } from "./screenshot-item.mjs";
6
+ export * from "./yaml.mjs";
7
+ function _define_property(obj, key, value) {
8
+ if (key in obj) Object.defineProperty(obj, key, {
9
+ value: value,
10
+ enumerable: true,
11
+ configurable: true,
12
+ writable: true
13
+ });
14
+ else obj[key] = value;
15
+ return obj;
16
+ }
17
+ class UIContext {
18
+ }
19
+ class ServiceError extends Error {
20
+ constructor(message, dump){
21
+ super(message), _define_property(this, "dump", void 0);
22
+ this.name = 'ServiceError';
23
+ this.dump = dump;
24
+ }
25
+ }
26
+ function replacerForDumpSerialization(_key, value) {
27
+ if (value && value.constructor?.name === 'Page') return '[Page object]';
28
+ if (value && value.constructor?.name === 'Browser') return '[Browser object]';
29
+ if (value && 'function' == typeof value.toSerializable) return value.toSerializable();
30
+ return value;
31
+ }
32
+ function reviverForDumpDeserialization(key, value) {
33
+ if ('screenshot' !== key || 'object' != typeof value || null === value) return value;
34
+ ScreenshotItem.isSerialized(value);
35
+ return value;
36
+ }
37
+ class ExecutionDump {
38
+ serialize(indents) {
39
+ return JSON.stringify(this.toJSON(), replacerForDumpSerialization, indents);
40
+ }
41
+ toJSON() {
42
+ return {
43
+ id: this.id,
44
+ logTime: this.logTime,
45
+ name: this.name,
46
+ description: this.description,
47
+ tasks: this.tasks.map((task)=>({
48
+ ...task,
49
+ recorder: task.recorder || []
50
+ })),
51
+ aiActContext: this.aiActContext
52
+ };
53
+ }
54
+ static fromSerializedString(serialized) {
55
+ const parsed = JSON.parse(serialized, reviverForDumpDeserialization);
56
+ return new ExecutionDump(parsed);
57
+ }
58
+ static fromJSON(data) {
59
+ return new ExecutionDump(data);
60
+ }
61
+ collectScreenshots() {
62
+ const screenshots = [];
63
+ for (const task of this.tasks){
64
+ if (task.uiContext?.screenshot instanceof ScreenshotItem) screenshots.push(task.uiContext.screenshot);
65
+ if (task.recorder) {
66
+ for (const record of task.recorder)if (record.screenshot instanceof ScreenshotItem) screenshots.push(record.screenshot);
67
+ }
68
+ }
69
+ return screenshots;
70
+ }
71
+ constructor(data){
72
+ _define_property(this, "id", void 0);
73
+ _define_property(this, "logTime", void 0);
74
+ _define_property(this, "name", void 0);
75
+ _define_property(this, "description", void 0);
76
+ _define_property(this, "tasks", void 0);
77
+ _define_property(this, "aiActContext", void 0);
78
+ this.id = data.id;
79
+ this.logTime = data.logTime;
80
+ this.name = data.name;
81
+ this.description = data.description;
82
+ this.tasks = data.tasks;
83
+ this.aiActContext = data.aiActContext;
84
+ }
85
+ }
86
+ class ReportActionDump {
87
+ serialize(indents) {
88
+ return JSON.stringify(this.toJSON(), replacerForDumpSerialization, indents);
89
+ }
90
+ serializeWithInlineScreenshots(indents) {
91
+ const processValue = (obj)=>{
92
+ if (obj instanceof ScreenshotItem) return {
93
+ base64: obj.base64,
94
+ capturedAt: obj.capturedAt
95
+ };
96
+ if (Array.isArray(obj)) return obj.map(processValue);
97
+ if (obj && 'object' == typeof obj) {
98
+ const entries = Object.entries(obj).map(([key, value])=>[
99
+ key,
100
+ processValue(value)
101
+ ]);
102
+ return Object.fromEntries(entries);
103
+ }
104
+ return obj;
105
+ };
106
+ const data = processValue(this.toJSON());
107
+ return JSON.stringify(data, null, indents);
108
+ }
109
+ toJSON() {
110
+ return {
111
+ sdkVersion: this.sdkVersion,
112
+ groupName: this.groupName,
113
+ groupDescription: this.groupDescription,
114
+ modelBriefs: this.modelBriefs,
115
+ executions: this.executions.map((exec)=>exec.toJSON()),
116
+ deviceType: this.deviceType
117
+ };
118
+ }
119
+ static fromSerializedString(serialized) {
120
+ const parsed = JSON.parse(serialized, reviverForDumpDeserialization);
121
+ return new ReportActionDump(parsed);
122
+ }
123
+ static fromJSON(data) {
124
+ return new ReportActionDump(data);
125
+ }
126
+ collectAllScreenshots() {
127
+ const screenshots = [];
128
+ for (const execution of this.executions)screenshots.push(...execution.collectScreenshots());
129
+ return screenshots;
130
+ }
131
+ serializeToFiles(basePath) {
132
+ const screenshotsDir = `${basePath}.screenshots`;
133
+ if (!existsSync(screenshotsDir)) mkdirSync(screenshotsDir, {
134
+ recursive: true
135
+ });
136
+ const screenshots = this.collectAllScreenshots();
137
+ for (const screenshot of screenshots){
138
+ const imagePath = join(screenshotsDir, `${screenshot.id}.${screenshot.extension}`);
139
+ if (existsSync(imagePath)) continue;
140
+ const rawBase64 = screenshot.rawBase64;
141
+ writeFileSync(imagePath, Buffer.from(rawBase64, 'base64'));
142
+ }
143
+ writeFileSync(basePath, this.serialize(), 'utf-8');
144
+ }
145
+ static fromFilesAsInlineJson(basePath) {
146
+ const dumpString = readFileSync(basePath, 'utf-8');
147
+ const screenshotsDir = `${basePath}.screenshots`;
148
+ const loadFromExecutionScreenshotDir = (id, mimeType)=>{
149
+ const ext = 'image/jpeg' === mimeType ? 'jpeg' : 'png';
150
+ const filePath = join(screenshotsDir, `${id}.${ext}`);
151
+ if (!existsSync(filePath)) return '';
152
+ const data = readFileSync(filePath);
153
+ return `data:image/${ext};base64,${data.toString('base64')}`;
154
+ };
155
+ const dumpData = JSON.parse(dumpString);
156
+ const store = new ScreenshotStore({
157
+ mode: 'directory',
158
+ reportPath: basePath
159
+ });
160
+ const processedData = restoreImageReferences(dumpData, (ref)=>{
161
+ const executionFileImage = loadFromExecutionScreenshotDir(ref.id, ref.mimeType);
162
+ if (executionFileImage) return executionFileImage;
163
+ if ('inline' === ref.storage) return '';
164
+ return store.loadBase64(ref);
165
+ });
166
+ return JSON.stringify(processedData);
167
+ }
168
+ static cleanupFiles(basePath) {
169
+ const filesToClean = [
170
+ basePath,
171
+ `${basePath}.screenshots`
172
+ ];
173
+ for (const filePath of filesToClean)try {
174
+ rmSync(filePath, {
175
+ force: true,
176
+ recursive: true
177
+ });
178
+ } catch {}
179
+ }
180
+ static getFilePaths(basePath) {
181
+ return [
182
+ basePath,
183
+ `${basePath}.screenshots`
184
+ ];
185
+ }
186
+ constructor(data){
187
+ _define_property(this, "sdkVersion", void 0);
188
+ _define_property(this, "groupName", void 0);
189
+ _define_property(this, "groupDescription", void 0);
190
+ _define_property(this, "modelBriefs", void 0);
191
+ _define_property(this, "executions", void 0);
192
+ _define_property(this, "deviceType", void 0);
193
+ this.sdkVersion = data.sdkVersion;
194
+ this.groupName = data.groupName;
195
+ this.groupDescription = data.groupDescription;
196
+ this.modelBriefs = data.modelBriefs;
197
+ this.executions = data.executions.map((exec)=>exec instanceof ExecutionDump ? exec : ExecutionDump.fromJSON(exec));
198
+ this.deviceType = data.deviceType;
199
+ }
200
+ }
201
+ const GroupedActionDump = ReportActionDump;
202
+ export { ExecutionDump, GroupedActionDump, ReportActionDump, ServiceError, UIContext };
203
+
204
+ //# sourceMappingURL=types.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.mjs","sources":["../../src/types.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\n\nimport {\n existsSync,\n mkdirSync,\n readFileSync,\n rmSync,\n writeFileSync,\n} from 'node:fs';\nimport { join } from 'node:path';\nimport type { NodeType } from '@midscene/shared/constants';\nimport type { CreateOpenAIClientFn, TModelConfig } from '@midscene/shared/env';\nimport type {\n BaseElement,\n LocateResultElement,\n Rect,\n Size,\n} from '@midscene/shared/types';\nimport type { z } from 'zod';\nimport type { TUserPrompt } from './common';\nimport { restoreImageReferences } from './dump/screenshot-restoration';\nimport { ScreenshotStore } from './dump/screenshot-store';\nimport { ScreenshotItem } from './screenshot-item';\nimport type {\n DetailedLocateParam,\n MidsceneYamlFlowItem,\n ServiceExtractOption,\n} from './yaml';\n\nexport type {\n ElementTreeNode,\n BaseElement,\n Rect,\n Size,\n Point,\n} from '@midscene/shared/types';\nexport * from './yaml';\n\nexport type AIUsageInfo = Record<string, any> & {\n prompt_tokens: number | undefined;\n completion_tokens: number | undefined;\n total_tokens: number | undefined;\n cached_input: number | undefined;\n time_cost: number | undefined;\n model_name: string | undefined;\n model_description: string | undefined;\n intent: string | undefined;\n request_id: string | undefined;\n};\n\nexport type { LocateResultElement };\n\nexport type AISingleElementResponseByPosition = {\n position?: {\n x: number;\n y: number;\n };\n bbox?: [number, number, number, number];\n reason: string;\n text: string;\n};\n\nexport interface AIElementCoordinatesResponse {\n bbox: [number, number, number, number];\n errors?: string[];\n}\n\nexport type AIElementResponse = AIElementCoordinatesResponse;\n\nexport interface AIDataExtractionResponse<DataDemand> {\n data: DataDemand;\n errors?: string[];\n thought?: string;\n}\n\nexport interface AISectionLocatorResponse {\n bbox: [number, number, number, number];\n references_bbox?: [number, number, number, number][];\n error?: string;\n}\n\nexport interface AIAssertionResponse {\n pass: boolean;\n thought: string;\n}\n\nexport interface AIDescribeElementResponse {\n description: string;\n error?: string;\n}\n\nexport interface LocatorValidatorOption {\n centerDistanceThreshold?: number;\n}\n\nexport interface LocateValidatorResult {\n pass: boolean;\n rect: Rect;\n center: [number, number];\n centerDistance?: number;\n}\n\nexport interface AgentDescribeElementAtPointResult {\n prompt: string;\n deepLocate: boolean;\n verifyResult?: LocateValidatorResult;\n}\n\n/**\n * context\n */\n\nexport abstract class UIContext {\n /**\n * screenshot of the current UI state. which size is shotSize(be shrunk by screenshotShrinkFactor),\n */\n abstract screenshot: ScreenshotItem;\n\n /**\n * screenshot size after shrinking\n */\n abstract shotSize: Size;\n\n /**\n * The ratio for converting shrunk screenshot coordinates to logical coordinates.\n *\n * Example:\n * - Physical screen width: 3000px, dpr=6\n * - Logical width: 500px\n * - User-defined screenshotShrinkFactor: 2\n * - Actual shrunk screenshot width: 3000 / 2 = 1500px\n * - shrunkShotToLogicalRatio: dpr / screenshotShrinkFactor = 6 / 2 = 3\n * - To map back to logical coordinates: 1500 / shrunkShotToLogicalRatio = 500px\n */\n abstract shrunkShotToLogicalRatio: number;\n\n abstract _isFrozen?: boolean;\n\n // @deprecated - backward compatibility for aiLocate\n abstract deprecatedDpr?: number;\n}\n\nexport type EnsureObject<T> = { [K in keyof T]: any };\n\nexport type ServiceAction = 'locate' | 'extract' | 'assert' | 'describe';\n\nexport type ServiceExtractParam = string | Record<string, string>;\n\nexport type ElementCacheFeature = Record<string, unknown>;\n\nexport interface LocateResult {\n element: LocateResultElement | null;\n rect?: Rect;\n}\n\nexport type ThinkingLevel = 'off' | 'medium' | 'high';\n\nexport type DeepThinkOption = 'unset' | true | false;\n\nexport interface ServiceTaskInfo {\n durationMs: number;\n formatResponse?: string;\n rawResponse?: string;\n usage?: AIUsageInfo;\n searchArea?: Rect;\n searchAreaRawResponse?: string;\n searchAreaUsage?: AIUsageInfo;\n reasoning_content?: string;\n}\n\nexport interface DumpMeta {\n logTime: number;\n}\n\nexport type ReportAttributes = Record<\n string,\n string | number | boolean | null | undefined\n>;\n\nexport interface ReportDumpWithAttributes {\n dumpString: string;\n attributes?: ReportAttributes;\n}\n\nexport interface ServiceDump extends DumpMeta {\n type: 'locate' | 'extract' | 'assert';\n logId: string;\n userQuery: {\n element?: TUserPrompt;\n dataDemand?: ServiceExtractParam;\n assertion?: TUserPrompt;\n };\n matchedElement: LocateResultElement[];\n matchedRect?: Rect;\n deepLocate?: boolean;\n data: any;\n assertionPass?: boolean;\n assertionThought?: string;\n taskInfo: ServiceTaskInfo;\n error?: string;\n output?: any;\n}\n\nexport type PartialServiceDumpFromSDK = Omit<\n ServiceDump,\n 'logTime' | 'logId' | 'model_name'\n>;\n\nexport interface ServiceResultBase {\n dump: ServiceDump;\n}\n\nexport type LocateResultWithDump = LocateResult & ServiceResultBase;\n\nexport interface ServiceExtractResult<T> extends ServiceResultBase {\n data: T;\n thought?: string;\n usage?: AIUsageInfo;\n reasoning_content?: string;\n}\n\nexport class ServiceError extends Error {\n dump: ServiceDump;\n\n constructor(message: string, dump: ServiceDump) {\n super(message);\n this.name = 'ServiceError';\n this.dump = dump;\n }\n}\n\n// intermediate variables to optimize the return value by AI\nexport interface LiteUISection {\n name: string;\n description: string;\n sectionCharacteristics: string;\n textIds: string[];\n}\n\nexport type ElementById = (id: string) => BaseElement | null;\n\nexport type ServiceAssertionResponse = AIAssertionResponse & {\n usage?: AIUsageInfo;\n};\n\n/**\n * agent\n */\n\nexport type OnTaskStartTip = (tip: string) => Promise<void> | void;\n\nexport interface AgentWaitForOpt extends ServiceExtractOption {\n checkIntervalMs?: number;\n timeoutMs?: number;\n}\n\nexport interface AgentAssertOpt {\n keepRawResponse?: boolean;\n}\n\n/**\n * planning\n *\n */\n\nexport interface PlanningLocateParam extends DetailedLocateParam {\n bbox?: [number, number, number, number];\n}\n\nexport interface PlanningAction<ParamType = any> {\n thought?: string;\n log?: string; // a brief preamble to the user explaining what you’re about to do\n type: string;\n param: ParamType;\n}\n\nexport type SubGoalStatus = 'pending' | 'running' | 'finished';\n\nexport interface SubGoal {\n index: number;\n status: SubGoalStatus;\n description: string;\n logs?: string[];\n}\n\nexport interface RawResponsePlanningAIResponse {\n action: PlanningAction;\n thought?: string;\n log: string;\n memory?: string;\n error?: string;\n finalizeMessage?: string;\n finalizeSuccess?: boolean;\n updateSubGoals?: SubGoal[];\n markFinishedIndexes?: number[];\n}\n\nexport interface PlanningAIResponse\n extends Omit<RawResponsePlanningAIResponse, 'action'> {\n actions?: PlanningAction[];\n usage?: AIUsageInfo;\n rawResponse?: string;\n yamlFlow?: MidsceneYamlFlowItem[];\n yamlString?: string;\n error?: string;\n reasoning_content?: string;\n shouldContinuePlanning: boolean;\n output?: string; // Output message from <complete> tag (same as finalizeMessage)\n}\n\nexport interface PlanningActionParamSleep {\n timeMs: number;\n}\n\nexport interface PlanningActionParamError {\n thought: string;\n}\n\nexport type PlanningActionParamWaitFor = AgentWaitForOpt & {};\n\nexport interface LongPressParam {\n duration?: number;\n}\n\nexport interface PullParam {\n direction: 'up' | 'down';\n distance?: number;\n duration?: number;\n}\n/**\n * misc\n */\n\nexport interface Color {\n name: string;\n hex: string;\n}\n\nexport interface BaseAgentParserOpt {\n selector?: string;\n}\n// eslint-disable-next-line @typescript-eslint/no-empty-interface\nexport interface PuppeteerParserOpt extends BaseAgentParserOpt {}\n\n// eslint-disable-next-line @typescript-eslint/no-empty-interface\nexport interface PlaywrightParserOpt extends BaseAgentParserOpt {}\n\n/*\naction\n*/\nexport interface ExecutionTaskProgressOptions {\n onTaskStart?: (task: ExecutionTask) => Promise<void> | void;\n}\n\nexport interface ExecutionRecorderItem {\n type: 'screenshot';\n ts: number;\n screenshot?: ScreenshotItem;\n timing?: string;\n}\n\nexport type ExecutionTaskType = 'Planning' | 'Insight' | 'Action Space' | 'Log';\n\nexport interface ExecutorContext {\n task: ExecutionTask;\n element?: LocateResultElement | null;\n uiContext?: UIContext;\n}\n\nexport interface ExecutionTaskApply<\n Type extends ExecutionTaskType = any,\n TaskParam = any,\n TaskOutput = any,\n TaskLog = any,\n> {\n type: Type;\n subType?: string;\n param?: TaskParam;\n thought?: string;\n uiContext?: UIContext;\n executor: (\n param: TaskParam,\n context: ExecutorContext,\n ) => // biome-ignore lint/suspicious/noConfusingVoidType: void is intentionally allowed as some executors may not return a value\n | Promise<ExecutionTaskReturn<TaskOutput, TaskLog> | undefined | void>\n | undefined\n | void;\n}\n\nexport interface ExecutionTaskHitBy {\n from: string;\n context: Record<string, any>;\n}\n\nexport interface ExecutionTaskReturn<TaskOutput = unknown, TaskLog = unknown> {\n output?: TaskOutput;\n log?: TaskLog;\n recorder?: ExecutionRecorderItem[];\n hitBy?: ExecutionTaskHitBy;\n}\n\nexport type ExecutionTask<\n E extends ExecutionTaskApply<any, any, any> = ExecutionTaskApply<\n any,\n any,\n any\n >,\n> = E &\n ExecutionTaskReturn<\n E extends ExecutionTaskApply<any, any, infer TaskOutput, any>\n ? TaskOutput\n : unknown,\n E extends ExecutionTaskApply<any, any, any, infer TaskLog>\n ? TaskLog\n : unknown\n > & {\n taskId: string;\n status: 'pending' | 'running' | 'finished' | 'failed' | 'cancelled';\n error?: Error;\n errorMessage?: string;\n errorStack?: string;\n timing?: {\n start: number;\n getUiContextStart?: number;\n getUiContextEnd?: number;\n callAiStart?: number;\n callAiEnd?: number;\n beforeInvokeActionHookStart?: number;\n beforeInvokeActionHookEnd?: number;\n callActionStart?: number;\n callActionEnd?: number;\n afterInvokeActionHookStart?: number;\n afterInvokeActionHookEnd?: number;\n captureAfterCallingSnapshotStart?: number;\n captureAfterCallingSnapshotEnd?: number;\n end?: number;\n cost?: number;\n };\n usage?: AIUsageInfo;\n searchAreaUsage?: AIUsageInfo;\n reasoning_content?: string;\n };\n\nexport interface IExecutionDump extends DumpMeta {\n /** Stable unique identifier for this execution run */\n id?: string;\n name: string;\n description?: string;\n tasks: ExecutionTask[];\n aiActContext?: string;\n}\n\n/**\n * Replacer function for JSON serialization that handles Page, Browser objects and ScreenshotItem\n */\nfunction replacerForDumpSerialization(_key: string, value: any): any {\n if (value && value.constructor?.name === 'Page') {\n return '[Page object]';\n }\n if (value && value.constructor?.name === 'Browser') {\n return '[Browser object]';\n }\n // Handle ScreenshotItem serialization\n if (value && typeof value.toSerializable === 'function') {\n return value.toSerializable();\n }\n return value;\n}\n\n/**\n * Reviver function for JSON deserialization that keeps screenshot references\n * as plain objects. Resolution is handled lazily by restoreImageReferences.\n *\n * @param key - JSON key being processed\n * @param value - JSON value being processed\n * @returns Restored value\n */\nfunction reviverForDumpDeserialization(key: string, value: any): any {\n // Only process screenshot fields\n if (key !== 'screenshot' || typeof value !== 'object' || value === null) {\n return value;\n }\n\n if (ScreenshotItem.isSerialized(value)) {\n return value;\n }\n\n return value;\n}\n\n/**\n * ExecutionDump class for serializing and deserializing execution dumps\n */\nexport class ExecutionDump implements IExecutionDump {\n id?: string;\n logTime: number;\n name: string;\n description?: string;\n tasks: ExecutionTask[];\n aiActContext?: string;\n\n constructor(data: IExecutionDump) {\n this.id = data.id;\n this.logTime = data.logTime;\n this.name = data.name;\n this.description = data.description;\n this.tasks = data.tasks;\n this.aiActContext = data.aiActContext;\n }\n\n /**\n * Serialize the ExecutionDump to a JSON string\n */\n serialize(indents?: number): string {\n return JSON.stringify(this.toJSON(), replacerForDumpSerialization, indents);\n }\n\n /**\n * Convert to a plain object for JSON serialization\n */\n toJSON(): IExecutionDump {\n return {\n id: this.id,\n logTime: this.logTime,\n name: this.name,\n description: this.description,\n tasks: this.tasks.map((task) => ({\n ...task,\n recorder: task.recorder || [],\n })),\n aiActContext: this.aiActContext,\n };\n }\n\n /**\n * Create an ExecutionDump instance from a serialized JSON string\n */\n static fromSerializedString(serialized: string): ExecutionDump {\n const parsed = JSON.parse(\n serialized,\n reviverForDumpDeserialization,\n ) as IExecutionDump;\n return new ExecutionDump(parsed);\n }\n\n /**\n * Create an ExecutionDump instance from a plain object\n */\n static fromJSON(data: IExecutionDump): ExecutionDump {\n return new ExecutionDump(data);\n }\n\n /**\n * Collect all ScreenshotItem instances from tasks.\n * Scans through uiContext and recorder items to find screenshots.\n *\n * @returns Array of ScreenshotItem instances\n */\n collectScreenshots(): ScreenshotItem[] {\n const screenshots: ScreenshotItem[] = [];\n\n for (const task of this.tasks) {\n // Collect uiContext.screenshot if present\n if (task.uiContext?.screenshot instanceof ScreenshotItem) {\n screenshots.push(task.uiContext.screenshot);\n }\n\n // Collect recorder screenshots\n if (task.recorder) {\n for (const record of task.recorder) {\n if (record.screenshot instanceof ScreenshotItem) {\n screenshots.push(record.screenshot);\n }\n }\n }\n }\n\n return screenshots;\n }\n}\n\n/*\ntask - service-locate\n*/\nexport type ExecutionTaskInsightLocateParam = PlanningLocateParam;\n\nexport interface ExecutionTaskInsightLocateOutput {\n element: LocateResultElement | null;\n}\n\nexport type ExecutionTaskInsightDump = ServiceDump;\n\nexport type ExecutionTaskInsightLocateApply = ExecutionTaskApply<\n 'Insight',\n ExecutionTaskInsightLocateParam,\n ExecutionTaskInsightLocateOutput,\n ExecutionTaskInsightDump\n>;\n\nexport type ExecutionTaskInsightLocate =\n ExecutionTask<ExecutionTaskInsightLocateApply>;\n\n/*\ntask - service-query\n*/\nexport interface ExecutionTaskInsightQueryParam {\n dataDemand: ServiceExtractParam;\n domIncluded?: boolean | 'visible-only';\n}\n\nexport interface ExecutionTaskInsightQueryOutput {\n data: any;\n}\n\nexport type ExecutionTaskInsightQueryApply = ExecutionTaskApply<\n 'Insight',\n ExecutionTaskInsightQueryParam,\n any,\n ExecutionTaskInsightDump\n>;\n\nexport type ExecutionTaskInsightQuery =\n ExecutionTask<ExecutionTaskInsightQueryApply>;\n\n/*\ntask - assertion\n*/\nexport interface ExecutionTaskInsightAssertionParam {\n assertion: string;\n}\n\nexport type ExecutionTaskInsightAssertionApply = ExecutionTaskApply<\n 'Insight',\n ExecutionTaskInsightAssertionParam,\n ServiceAssertionResponse,\n ExecutionTaskInsightDump\n>;\n\nexport type ExecutionTaskInsightAssertion =\n ExecutionTask<ExecutionTaskInsightAssertionApply>;\n\n/*\ntask - action (i.e. interact) \n*/\nexport type ExecutionTaskActionApply<ActionParam = any> = ExecutionTaskApply<\n 'Action Space',\n ActionParam,\n void,\n void\n>;\n\nexport type ExecutionTaskAction = ExecutionTask<ExecutionTaskActionApply>;\n\n/*\ntask - Log\n*/\n\nexport type ExecutionTaskLogApply<\n LogParam = {\n content: string;\n },\n> = ExecutionTaskApply<'Log', LogParam, void, void>;\n\nexport type ExecutionTaskLog = ExecutionTask<ExecutionTaskLogApply>;\n\n/*\ntask - planning\n*/\n\nexport type ExecutionTaskPlanningApply = ExecutionTaskApply<\n 'Planning',\n {\n userInstruction: string;\n aiActContext?: string;\n },\n PlanningAIResponse\n>;\n\nexport type ExecutionTaskPlanning = ExecutionTask<ExecutionTaskPlanningApply>;\n\n/*\ntask - planning-locate\n*/\nexport type ExecutionTaskPlanningLocateParam = PlanningLocateParam;\n\nexport interface ExecutionTaskPlanningLocateOutput {\n element: LocateResultElement | null;\n}\n\nexport type ExecutionTaskPlanningDump = ServiceDump;\n\nexport type ExecutionTaskPlanningLocateApply = ExecutionTaskApply<\n 'Planning',\n ExecutionTaskPlanningLocateParam,\n ExecutionTaskPlanningLocateOutput,\n ExecutionTaskPlanningDump\n>;\n\nexport type ExecutionTaskPlanningLocate =\n ExecutionTask<ExecutionTaskPlanningLocateApply>;\n\n/*\nReport metadata - extracted from ReportActionDump for per-execution writes\n*/\nexport interface ReportMeta {\n groupName: string;\n groupDescription?: string;\n sdkVersion: string;\n modelBriefs: ModelBrief[];\n deviceType?: string;\n}\n\n// Backward-compatible aliases for existing external consumers.\nexport type GroupMeta = ReportMeta;\n\n/*\nReport dump\n*/\nexport interface IReportActionDump {\n sdkVersion: string;\n groupName: string;\n groupDescription?: string;\n modelBriefs: ModelBrief[];\n executions: IExecutionDump[];\n deviceType?: string;\n}\n\n// Backward-compatible aliases for existing external consumers.\nexport type IGroupedActionDump = IReportActionDump;\n\nexport interface ModelBrief {\n /**\n * The intent/category of the model call, for example \"planning\" or \"insight\".\n */\n intent?: string;\n\n /**\n * The model name returned by usage metadata, for example \"gpt-4o\".\n */\n name?: string;\n\n /**\n * Optional human-readable model description, for example \"qwen2.5-vl mode\".\n */\n modelDescription?: string;\n}\n\n/**\n * ReportActionDump class for serializing and deserializing report action dumps\n */\nexport class ReportActionDump implements IReportActionDump {\n sdkVersion: string;\n groupName: string;\n groupDescription?: string;\n modelBriefs: ModelBrief[];\n executions: ExecutionDump[];\n deviceType?: string;\n\n constructor(data: IReportActionDump) {\n this.sdkVersion = data.sdkVersion;\n this.groupName = data.groupName;\n this.groupDescription = data.groupDescription;\n this.modelBriefs = data.modelBriefs;\n this.executions = data.executions.map((exec) =>\n exec instanceof ExecutionDump ? exec : ExecutionDump.fromJSON(exec),\n );\n this.deviceType = data.deviceType;\n }\n\n /**\n * Serialize the ReportActionDump to a JSON string\n * Uses compact { $screenshot: id } format\n */\n serialize(indents?: number): string {\n return JSON.stringify(this.toJSON(), replacerForDumpSerialization, indents);\n }\n\n /**\n * Serialize the ReportActionDump with inline screenshots to a JSON string.\n * Each ScreenshotItem is replaced with { base64: \"...\", capturedAt }.\n */\n serializeWithInlineScreenshots(indents?: number): string {\n const processValue = (obj: unknown): unknown => {\n if (obj instanceof ScreenshotItem) {\n return { base64: obj.base64, capturedAt: obj.capturedAt };\n }\n if (Array.isArray(obj)) {\n return obj.map(processValue);\n }\n if (obj && typeof obj === 'object') {\n const entries = Object.entries(obj).map(([key, value]) => [\n key,\n processValue(value),\n ]);\n return Object.fromEntries(entries);\n }\n return obj;\n };\n\n const data = processValue(this.toJSON());\n return JSON.stringify(data, null, indents);\n }\n\n /**\n * Convert to a plain object for JSON serialization\n */\n toJSON(): IReportActionDump {\n return {\n sdkVersion: this.sdkVersion,\n groupName: this.groupName,\n groupDescription: this.groupDescription,\n modelBriefs: this.modelBriefs,\n executions: this.executions.map((exec) => exec.toJSON()),\n deviceType: this.deviceType,\n };\n }\n\n /**\n * Create a ReportActionDump instance from a serialized JSON string\n */\n static fromSerializedString(serialized: string): ReportActionDump {\n const parsed = JSON.parse(\n serialized,\n reviverForDumpDeserialization,\n ) as IReportActionDump;\n return new ReportActionDump(parsed);\n }\n\n /**\n * Create a ReportActionDump instance from a plain object\n */\n static fromJSON(data: IReportActionDump): ReportActionDump {\n return new ReportActionDump(data);\n }\n\n /**\n * Collect all ScreenshotItem instances from all executions.\n *\n * @returns Array of all ScreenshotItem instances across all executions\n */\n collectAllScreenshots(): ScreenshotItem[] {\n const screenshots: ScreenshotItem[] = [];\n for (const execution of this.executions) {\n screenshots.push(...execution.collectScreenshots());\n }\n return screenshots;\n }\n\n /**\n * Serialize the dump to files with screenshots as separate PNG files.\n * Creates:\n * - {basePath} - dump JSON with { $screenshot: id } references\n * - {basePath}.screenshots/ - PNG files\n *\n * @param basePath - Base path for the dump file\n */\n serializeToFiles(basePath: string): void {\n const screenshotsDir = `${basePath}.screenshots`;\n if (!existsSync(screenshotsDir)) {\n mkdirSync(screenshotsDir, { recursive: true });\n }\n\n const screenshots = this.collectAllScreenshots();\n\n for (const screenshot of screenshots) {\n const imagePath = join(\n screenshotsDir,\n `${screenshot.id}.${screenshot.extension}`,\n );\n if (existsSync(imagePath)) {\n continue;\n }\n\n const rawBase64 = screenshot.rawBase64;\n writeFileSync(imagePath, Buffer.from(rawBase64, 'base64'));\n }\n\n // Write dump JSON with references\n writeFileSync(basePath, this.serialize(), 'utf-8');\n }\n\n /**\n * Read dump from files and return JSON string with inline screenshots.\n * Reads the dump JSON and screenshot files, then inlines the base64 data.\n *\n * @param basePath - Base path for the dump file\n * @returns JSON string with inline screenshots ({ base64: \"...\" } format)\n */\n static fromFilesAsInlineJson(basePath: string): string {\n const dumpString = readFileSync(basePath, 'utf-8');\n const screenshotsDir = `${basePath}.screenshots`;\n\n const loadFromExecutionScreenshotDir = (id: string, mimeType: string) => {\n const ext = mimeType === 'image/jpeg' ? 'jpeg' : 'png';\n const filePath = join(screenshotsDir, `${id}.${ext}`);\n if (!existsSync(filePath)) {\n return '';\n }\n const data = readFileSync(filePath);\n return `data:image/${ext};base64,${data.toString('base64')}`;\n };\n\n // Restore image references\n const dumpData = JSON.parse(dumpString);\n const store = new ScreenshotStore({\n mode: 'directory',\n reportPath: basePath,\n });\n const processedData = restoreImageReferences(dumpData, (ref) => {\n const executionFileImage = loadFromExecutionScreenshotDir(\n ref.id,\n ref.mimeType,\n );\n if (executionFileImage) {\n return executionFileImage;\n }\n\n if (ref.storage === 'inline') {\n return '';\n }\n return store.loadBase64(ref);\n });\n return JSON.stringify(processedData);\n }\n\n /**\n * Clean up all files associated with a serialized dump.\n *\n * @param basePath - Base path for the dump file\n */\n static cleanupFiles(basePath: string): void {\n const filesToClean = [basePath, `${basePath}.screenshots`];\n\n for (const filePath of filesToClean) {\n try {\n rmSync(filePath, { force: true, recursive: true });\n } catch {\n // Ignore errors - file may already be deleted\n }\n }\n }\n\n /**\n * Get all file paths associated with a serialized dump.\n *\n * @param basePath - Base path for the dump file\n * @returns Array of all associated file paths\n */\n static getFilePaths(basePath: string): string[] {\n return [basePath, `${basePath}.screenshots`];\n }\n}\n\n// Backward-compatible aliases for existing external consumers.\nexport type GroupedActionDump = ReportActionDump;\nexport const GroupedActionDump = ReportActionDump;\n\nexport type InterfaceType =\n | 'puppeteer'\n | 'playwright'\n | 'static'\n | 'chrome-extension-proxy'\n | 'android'\n | string;\n\nexport interface StreamingCodeGenerationOptions {\n /** Whether to enable streaming output */\n stream?: boolean;\n /** Callback function to handle streaming chunks */\n onChunk?: StreamingCallback;\n /** Callback function to handle streaming completion */\n onComplete?: (finalCode: string) => void;\n /** Callback function to handle streaming errors */\n onError?: (error: Error) => void;\n}\n\nexport type StreamingCallback = (chunk: CodeGenerationChunk) => void;\n\nexport interface CodeGenerationChunk {\n /** The incremental content chunk */\n content: string;\n /** The reasoning content */\n reasoning_content: string;\n /** The accumulated content so far */\n accumulated: string;\n /** Whether this is the final chunk */\n isComplete: boolean;\n /** Token usage information if available */\n usage?: AIUsageInfo;\n}\n\nexport interface StreamingAIResponse {\n /** The final accumulated content */\n content: string;\n /** Token usage information */\n usage?: AIUsageInfo;\n /** Whether the response was streamed */\n isStreamed: boolean;\n}\n\nexport interface DeviceAction<TParam = any, TReturn = any> {\n name: string;\n description?: string;\n interfaceAlias?: string;\n paramSchema?: z.ZodType<TParam>;\n call: (param: TParam, context: ExecutorContext) => Promise<TReturn> | TReturn;\n delayAfterRunner?: number;\n /**\n * An example param object for this action.\n * Locate fields with { prompt } will automatically get bbox injected when needed.\n */\n sample?: { [K in keyof TParam]?: any };\n}\n\n/**\n * Type utilities for extracting types from DeviceAction definitions\n */\n\n/**\n * Extract parameter type from a DeviceAction\n */\nexport type ActionParam<Action extends DeviceAction<any, any>> =\n Action extends DeviceAction<infer P, any> ? P : never;\n\n/**\n * Extract return type from a DeviceAction\n */\nexport type ActionReturn<Action extends DeviceAction<any, any>> =\n Action extends DeviceAction<any, infer R> ? R : never;\n\n/**\n * Web-specific types\n */\nexport interface WebElementInfo extends BaseElement {\n id: string;\n attributes: {\n nodeType: NodeType;\n [key: string]: string;\n };\n}\n\n/**\n * Agent\n */\n\nexport type CacheConfig = {\n strategy?: 'read-only' | 'read-write' | 'write-only';\n id: string;\n};\n\nexport type Cache =\n | false // No read, no write\n | true // Will throw error at runtime - deprecated\n | CacheConfig; // Object configuration (requires explicit id)\n\nexport interface AgentOpt {\n // @deprecated Use `reportFileName` and `cache.id` instead.\n testId?: string;\n // @deprecated\n cacheId?: string; // Keep backward compatibility, but marked as deprecated\n groupName?: string;\n groupDescription?: string;\n /* if auto generate report, default true */\n generateReport?: boolean;\n /* if persist per-execution dump files next to the report, default false */\n persistExecutionDump?: boolean;\n /* if auto print report msg, default true */\n autoPrintReportMsg?: boolean;\n\n /**\n * Use directory-based report format with separate image files.\n *\n * When enabled:\n * - Screenshots are saved as PNG files in a `screenshots/` subdirectory\n * - Report is generated as `index.html` with relative image paths\n * - Reduces memory usage and report file size\n *\n * IMPORTANT: 'html-and-external-assets' reports must be served via HTTP server\n * (e.g., `npx serve ./report-dir`). The file:// protocol will not\n * work due to browser CORS restrictions.\n *\n * @default 'single-html'\n */\n outputFormat?: 'single-html' | 'html-and-external-assets';\n\n onTaskStartTip?: OnTaskStartTip;\n aiActContext?: string;\n aiActionContext?: string;\n /* custom report file name */\n reportFileName?: string;\n modelConfig?: TModelConfig;\n cache?: Cache;\n /**\n * Maximum number of replanning cycles for aiAct.\n * Defaults to 20 (40 for `vlm-ui-tars`) when not provided.\n * If omitted, the agent will also read `MIDSCENE_REPLANNING_CYCLE_LIMIT` for backward compatibility.\n */\n replanningCycleLimit?: number;\n\n /**\n * Wait time in milliseconds after each action execution.\n * This allows the UI to settle and stabilize before the next action.\n * Defaults to 300ms when not provided.\n */\n waitAfterAction?: number;\n\n /**\n * When set to true, Midscene will use the target device's time (Android/iOS)\n * instead of the system time. Useful when the device time differs from the\n * host machine. Default: false\n */\n useDeviceTimestamp?: boolean;\n\n /**\n * Custom screenshot shrink factor to reduce AI token usage.\n * When set, the screenshot will be scaled down by this factor from the physical resolution.\n *\n * Example:\n * - Physical screen width: 3000px, dpr=6\n * - Logical width: 500px\n * - screenshotShrinkFactor: 2\n * - Actual shrunk screenshot width: 3000 / 2 = 1500px\n * - AI analyzes the 1500px screenshot\n * - Coordinates are transformed back to logical (500px) before actions execute\n *\n * Benefits:\n * - Reduces token usage for high-resolution screenshots\n * - Maintains accuracy by scaling coordinates appropriately\n *\n * Must be >= 1 (shrinking only, enlarging is not supported).\n *\n * @default 1 (no shrinking, uses original physical screenshot)\n */\n screenshotShrinkFactor?: number;\n\n /**\n * Custom OpenAI client factory function\n *\n * If provided, this function will be called to create OpenAI client instances\n * for each AI call, allowing you to:\n * - Wrap clients with observability tools (langsmith, langfuse)\n * - Use custom OpenAI-compatible clients\n * - Apply different configurations based on intent\n *\n * @param config - Resolved model configuration\n * @returns OpenAI client instance (original or wrapped)\n *\n * @example\n * ```typescript\n * createOpenAIClient: async (openai, opts) => {\n * // Wrap with langsmith for planning tasks\n * if (opts.baseURL?.includes('planning')) {\n * return wrapOpenAI(openai, { metadata: { task: 'planning' } });\n * }\n *\n * return openai;\n * }\n * ```\n */\n createOpenAIClient?: CreateOpenAIClientFn;\n}\n\nexport type TestStatus =\n | 'passed'\n | 'failed'\n | 'timedOut'\n | 'skipped'\n | 'interrupted';\n\nexport interface ReportFileAttributes {\n testDuration: number;\n testStatus: TestStatus;\n testTitle: string;\n testId: string;\n testDescription: string;\n}\n\nexport type ReportFileWithAttributes =\n | {\n reportFilePath: string;\n reportAttributes: ReportFileAttributes;\n }\n | {\n reportFilePath?: string;\n reportAttributes: ReportFileAttributes & { testStatus: 'skipped' };\n };\n"],"names":["UIContext","ServiceError","Error","message","dump","replacerForDumpSerialization","_key","value","reviverForDumpDeserialization","key","ScreenshotItem","ExecutionDump","indents","JSON","task","serialized","parsed","data","screenshots","record","ReportActionDump","processValue","obj","Array","entries","Object","exec","execution","basePath","screenshotsDir","existsSync","mkdirSync","screenshot","imagePath","join","rawBase64","writeFileSync","Buffer","dumpString","readFileSync","loadFromExecutionScreenshotDir","id","mimeType","ext","filePath","dumpData","store","ScreenshotStore","processedData","restoreImageReferences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GA,KAAK,SAAS;QAC/B,IAAI,CAAC,gBAAgB,GAAGA,KAAK,gBAAgB;QAC7C,IAAI,CAAC,WAAW,GAAGA,KAAK,WAAW;QACnC,IAAI,CAAC,UAAU,GAAGA,KAAK,UAAU,CAAC,GAAG,CAAC,CAACS,OACrCA,gBAAgBf,gBAAgBe,OAAOf,cAAc,QAAQ,CAACe;QAEhE,IAAI,CAAC,UAAU,GAAGT,KAAK,UAAU;IACnC;AAwLF;AAIO,MAAMqC,oBAAoBlC"}
@@ -0,0 +1,234 @@
1
+ import { execFile } from "node:child_process";
2
+ import { appendFileSync, closeSync, existsSync, mkdirSync, openSync, readFileSync, readSync, statSync, truncateSync, writeFileSync } from "node:fs";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { promisify } from "node:util";
6
+ import { defaultRunDirName, getMidsceneRunSubDir } from "@midscene/shared/common";
7
+ import { MIDSCENE_CACHE, MIDSCENE_DEBUG_MODE, globalConfigManager } from "@midscene/shared/env";
8
+ import { getRunningPkgInfo } from "@midscene/shared/node";
9
+ import { assert, escapeScriptTag, ifInBrowser, ifInWorker, logMsg, uuid } from "@midscene/shared/utils";
10
+ let logEnvReady = false;
11
+ const groupedActionDumpFileExt = 'web-dump.json';
12
+ function processCacheConfig(cache, cacheId) {
13
+ if (void 0 !== cache) {
14
+ if (false === cache) return;
15
+ if (true === cache) return {
16
+ id: cacheId
17
+ };
18
+ if ('object' == typeof cache && null !== cache) {
19
+ if (!cache.id) return {
20
+ ...cache,
21
+ id: cacheId
22
+ };
23
+ return cache;
24
+ }
25
+ }
26
+ const envEnabled = globalConfigManager.getEnvConfigInBoolean(MIDSCENE_CACHE);
27
+ if (envEnabled && cacheId) return {
28
+ id: cacheId
29
+ };
30
+ }
31
+ const reportInitializedMap = new Map();
32
+ const reportGroupIdMap = new Map();
33
+ function getReportTpl() {
34
+ const reportTpl = 'REPLACE_ME_WITH_REPORT_HTML';
35
+ return reportTpl;
36
+ }
37
+ function insertContentBeforeClosingHtml(html, content) {
38
+ const htmlEndIdx = html.lastIndexOf('</html>');
39
+ if (-1 === htmlEndIdx) return html + content;
40
+ return `${html.slice(0, htmlEndIdx)}${content}\n${html.slice(htmlEndIdx)}`;
41
+ }
42
+ function insertScriptBeforeClosingHtml(filePath, scriptContent) {
43
+ const htmlEndTag = '</html>';
44
+ const stat = statSync(filePath);
45
+ const readSize = Math.min(stat.size, 4096);
46
+ const start = Math.max(0, stat.size - readSize);
47
+ const buffer = Buffer.alloc(stat.size - start);
48
+ const fd = openSync(filePath, 'r');
49
+ readSync(fd, buffer, 0, buffer.length, start);
50
+ closeSync(fd);
51
+ const tailStr = buffer.toString('utf8');
52
+ const htmlEndIdx = tailStr.lastIndexOf(htmlEndTag);
53
+ if (-1 === htmlEndIdx) throw new Error(`No </html> found in file:${filePath}`);
54
+ const beforeHtmlInTail = tailStr.slice(0, htmlEndIdx);
55
+ const htmlEndPos = start + Buffer.byteLength(beforeHtmlInTail, 'utf8');
56
+ truncateSync(filePath, htmlEndPos);
57
+ appendFileSync(filePath, `${scriptContent}\n${htmlEndTag}\n`);
58
+ }
59
+ function reportHTMLContent(dumpData, reportPath, appendReport, withTpl = true) {
60
+ let tpl = '';
61
+ if (withTpl) {
62
+ tpl = getReportTpl();
63
+ if (!tpl) {
64
+ console.warn('reportTpl is not set, will not write report');
65
+ return '';
66
+ }
67
+ }
68
+ const writeToFile = reportPath && !ifInBrowser;
69
+ let dumpContent = '';
70
+ const resolveAutoGroupId = ()=>{
71
+ if (!reportPath || !appendReport) return uuid();
72
+ const existingGroupId = reportGroupIdMap.get(reportPath);
73
+ if (existingGroupId) return existingGroupId;
74
+ const newGroupId = uuid();
75
+ reportGroupIdMap.set(reportPath, newGroupId);
76
+ return newGroupId;
77
+ };
78
+ if ('string' == typeof dumpData) {
79
+ const groupId = resolveAutoGroupId();
80
+ dumpContent = '<script type="midscene_web_dump" type="application/json" data-group-id="' + encodeURIComponent(groupId) + '">\n' + escapeScriptTag(dumpData) + "\n<\/script>";
81
+ } else {
82
+ const { dumpString, attributes } = dumpData;
83
+ const attributesArr = Object.entries(attributes || {}).filter((entry)=>void 0 !== entry[1] && null !== entry[1]).map(([key, value])=>`${key}="${encodeURIComponent(value)}"`);
84
+ dumpContent = '<script type="midscene_web_dump" type="application/json" ' + attributesArr.join(' ') + '>\n' + escapeScriptTag(dumpString) + "\n<\/script>";
85
+ }
86
+ if (writeToFile) {
87
+ if (!appendReport) {
88
+ writeFileSync(reportPath, insertContentBeforeClosingHtml(tpl, dumpContent), {
89
+ flag: 'w'
90
+ });
91
+ return reportPath;
92
+ }
93
+ if (!reportInitializedMap.get(reportPath)) {
94
+ writeFileSync(reportPath, tpl, {
95
+ flag: 'w'
96
+ });
97
+ reportInitializedMap.set(reportPath, true);
98
+ }
99
+ insertScriptBeforeClosingHtml(reportPath, dumpContent);
100
+ return reportPath;
101
+ }
102
+ return insertContentBeforeClosingHtml(tpl, dumpContent);
103
+ }
104
+ function writeDumpReport(fileName, dumpData, appendReport) {
105
+ if (ifInBrowser || ifInWorker) {
106
+ console.log('will not write report in browser');
107
+ return null;
108
+ }
109
+ const reportPath = join(getMidsceneRunSubDir('report'), `${fileName}.html`);
110
+ reportHTMLContent(dumpData, reportPath, appendReport);
111
+ if (process.env.MIDSCENE_DEBUG_LOG_JSON) {
112
+ const jsonPath = `${reportPath}.json`;
113
+ let data;
114
+ data = 'string' == typeof dumpData ? JSON.parse(dumpData) : dumpData;
115
+ writeFileSync(jsonPath, JSON.stringify(data, null, 2), {
116
+ flag: appendReport ? 'a' : 'w'
117
+ });
118
+ logMsg(`Midscene - dump file written: ${jsonPath}`);
119
+ }
120
+ return reportPath;
121
+ }
122
+ function writeLogFile(opts) {
123
+ if (ifInBrowser || ifInWorker) return '/mock/report.html';
124
+ const { fileName, fileExt, fileContent, type = 'dump' } = opts;
125
+ const targetDir = getMidsceneRunSubDir(type);
126
+ if (!logEnvReady) {
127
+ assert(targetDir, 'logDir should be set before writing dump file');
128
+ const gitIgnorePath = join(targetDir, '../../.gitignore');
129
+ const gitPath = join(targetDir, '../../.git');
130
+ let gitIgnoreContent = '';
131
+ if (existsSync(gitPath)) {
132
+ if (existsSync(gitIgnorePath)) gitIgnoreContent = readFileSync(gitIgnorePath, 'utf-8');
133
+ if (!gitIgnoreContent.includes(`${defaultRunDirName}/`)) writeFileSync(gitIgnorePath, `${gitIgnoreContent}\n# Midscene.js dump files\n${defaultRunDirName}/dump\n${defaultRunDirName}/report\n${defaultRunDirName}/tmp\n${defaultRunDirName}/log\n`, 'utf-8');
134
+ }
135
+ logEnvReady = true;
136
+ }
137
+ const filePath = join(targetDir, `${fileName}.${fileExt}`);
138
+ if ('dump' !== type) writeFileSync(filePath, JSON.stringify(fileContent));
139
+ if (opts?.generateReport) return writeDumpReport(fileName, fileContent, opts.appendReport);
140
+ return filePath;
141
+ }
142
+ function getTmpDir() {
143
+ try {
144
+ const runningPkgInfo = getRunningPkgInfo();
145
+ if (!runningPkgInfo) return null;
146
+ const { name } = runningPkgInfo;
147
+ const tmpPath = join(tmpdir(), name);
148
+ mkdirSync(tmpPath, {
149
+ recursive: true
150
+ });
151
+ return tmpPath;
152
+ } catch (e) {
153
+ return null;
154
+ }
155
+ }
156
+ function getTmpFile(fileExtWithoutDot) {
157
+ if (ifInBrowser || ifInWorker) return null;
158
+ const tmpDir = getTmpDir();
159
+ const filename = `${uuid()}.${fileExtWithoutDot}`;
160
+ return join(tmpDir, filename);
161
+ }
162
+ function overlapped(container, target) {
163
+ return container.left < target.left + target.width && container.left + container.width > target.left && container.top < target.top + target.height && container.top + container.height > target.top;
164
+ }
165
+ async function sleep(ms) {
166
+ return new Promise((resolve)=>setTimeout(resolve, ms));
167
+ }
168
+ function replacerForPageObject(_key, value) {
169
+ if (value && value.constructor?.name === 'Page') return '[Page object]';
170
+ if (value && value.constructor?.name === 'Browser') return '[Browser object]';
171
+ if (value && 'function' == typeof value.toSerializable) return value.toSerializable();
172
+ return value;
173
+ }
174
+ function stringifyDumpData(data, indents) {
175
+ return JSON.stringify(data, replacerForPageObject, indents);
176
+ }
177
+ function getVersion() {
178
+ return "1.1.1";
179
+ }
180
+ function debugLog(...message) {
181
+ const debugMode = process.env[MIDSCENE_DEBUG_MODE];
182
+ if (debugMode) console.log('[Midscene]', ...message);
183
+ }
184
+ let gitInfoPromise = null;
185
+ function getGitInfoAsync() {
186
+ if (gitInfoPromise) return gitInfoPromise;
187
+ const execFileAsync = promisify(execFile);
188
+ gitInfoPromise = Promise.all([
189
+ execFileAsync('git', [
190
+ 'config',
191
+ '--get',
192
+ 'remote.origin.url'
193
+ ]).then(({ stdout })=>stdout.trim(), ()=>''),
194
+ execFileAsync('git', [
195
+ 'config',
196
+ '--get',
197
+ 'user.email'
198
+ ]).then(({ stdout })=>stdout.trim(), ()=>'')
199
+ ]).then(([repoUrl, userEmail])=>({
200
+ repoUrl,
201
+ userEmail
202
+ }));
203
+ return gitInfoPromise;
204
+ }
205
+ let lastReportedRepoUrl = '';
206
+ async function uploadTestInfoToServer({ testUrl, serverUrl }) {
207
+ if (!serverUrl) return;
208
+ const { repoUrl, userEmail } = await getGitInfoAsync();
209
+ if (repoUrl ? repoUrl !== lastReportedRepoUrl : !!testUrl) {
210
+ debugLog('Uploading test info to server', {
211
+ serverUrl,
212
+ repoUrl,
213
+ testUrl,
214
+ userEmail
215
+ });
216
+ fetch(serverUrl, {
217
+ method: 'POST',
218
+ headers: {
219
+ 'Content-Type': 'application/json'
220
+ },
221
+ body: JSON.stringify({
222
+ repo_url: repoUrl,
223
+ test_url: testUrl,
224
+ user_email: userEmail
225
+ })
226
+ }).then((response)=>response.json()).then((data)=>{
227
+ debugLog('Successfully uploaded test info to server:', data);
228
+ }).catch((error)=>debugLog('Failed to upload test info to server:', error));
229
+ lastReportedRepoUrl = repoUrl;
230
+ }
231
+ }
232
+ export { appendFileSync, getReportTpl, getTmpDir, getTmpFile, getVersion, groupedActionDumpFileExt, insertContentBeforeClosingHtml, insertScriptBeforeClosingHtml, overlapped, processCacheConfig, replacerForPageObject, reportHTMLContent, sleep, stringifyDumpData, uploadTestInfoToServer, writeDumpReport, writeLogFile };
233
+
234
+ //# sourceMappingURL=utils.mjs.map