@midscene/core 1.9.6 → 1.9.7

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 (69) hide show
  1. package/dist/es/agent/agent.mjs +40 -8
  2. package/dist/es/agent/agent.mjs.map +1 -1
  3. package/dist/es/agent/tasks.mjs +3 -3
  4. package/dist/es/agent/tasks.mjs.map +1 -1
  5. package/dist/es/agent/utils.mjs +18 -3
  6. package/dist/es/agent/utils.mjs.map +1 -1
  7. package/dist/es/ai-model/prompt/describe.mjs +10 -2
  8. package/dist/es/ai-model/prompt/describe.mjs.map +1 -1
  9. package/dist/es/ai-model/prompt/markdown-generator.mjs +150 -40
  10. package/dist/es/ai-model/prompt/markdown-generator.mjs.map +1 -1
  11. package/dist/es/ai-model/prompt/recorder-generation-common.mjs +74 -14
  12. package/dist/es/ai-model/prompt/recorder-generation-common.mjs.map +1 -1
  13. package/dist/es/ai-model/prompt/recorder-metadata-generator.mjs +3 -5
  14. package/dist/es/ai-model/prompt/recorder-metadata-generator.mjs.map +1 -1
  15. package/dist/es/ai-model/prompt/recorder-ui-describer.mjs +10 -6
  16. package/dist/es/ai-model/prompt/recorder-ui-describer.mjs.map +1 -1
  17. package/dist/es/ai-model/prompt/yaml-generator.mjs +2 -2
  18. package/dist/es/ai-model/prompt/yaml-generator.mjs.map +1 -1
  19. package/dist/es/ai-model/service-caller/index.mjs +33 -3
  20. package/dist/es/ai-model/service-caller/index.mjs.map +1 -1
  21. package/dist/es/device/index.mjs.map +1 -1
  22. package/dist/es/recorder-ui-describer.mjs +33 -84
  23. package/dist/es/recorder-ui-describer.mjs.map +1 -1
  24. package/dist/es/service/index.mjs +11 -3
  25. package/dist/es/service/index.mjs.map +1 -1
  26. package/dist/es/service/utils.mjs +50 -1
  27. package/dist/es/service/utils.mjs.map +1 -1
  28. package/dist/es/types.mjs.map +1 -1
  29. package/dist/es/utils.mjs +2 -2
  30. package/dist/lib/agent/agent.js +39 -7
  31. package/dist/lib/agent/agent.js.map +1 -1
  32. package/dist/lib/agent/tasks.js +3 -3
  33. package/dist/lib/agent/tasks.js.map +1 -1
  34. package/dist/lib/agent/utils.js +20 -2
  35. package/dist/lib/agent/utils.js.map +1 -1
  36. package/dist/lib/ai-model/prompt/describe.js +10 -2
  37. package/dist/lib/ai-model/prompt/describe.js.map +1 -1
  38. package/dist/lib/ai-model/prompt/markdown-generator.js +150 -40
  39. package/dist/lib/ai-model/prompt/markdown-generator.js.map +1 -1
  40. package/dist/lib/ai-model/prompt/recorder-generation-common.js +75 -12
  41. package/dist/lib/ai-model/prompt/recorder-generation-common.js.map +1 -1
  42. package/dist/lib/ai-model/prompt/recorder-metadata-generator.js +2 -4
  43. package/dist/lib/ai-model/prompt/recorder-metadata-generator.js.map +1 -1
  44. package/dist/lib/ai-model/prompt/recorder-ui-describer.js +10 -6
  45. package/dist/lib/ai-model/prompt/recorder-ui-describer.js.map +1 -1
  46. package/dist/lib/ai-model/prompt/yaml-generator.js +2 -2
  47. package/dist/lib/ai-model/prompt/yaml-generator.js.map +1 -1
  48. package/dist/lib/ai-model/service-caller/index.js +33 -3
  49. package/dist/lib/ai-model/service-caller/index.js.map +1 -1
  50. package/dist/lib/device/index.js.map +1 -1
  51. package/dist/lib/recorder-ui-describer.js +33 -84
  52. package/dist/lib/recorder-ui-describer.js.map +1 -1
  53. package/dist/lib/service/index.js +10 -2
  54. package/dist/lib/service/index.js.map +1 -1
  55. package/dist/lib/service/utils.js +53 -1
  56. package/dist/lib/service/utils.js.map +1 -1
  57. package/dist/lib/types.js.map +1 -1
  58. package/dist/lib/utils.js +2 -2
  59. package/dist/types/agent/agent.d.ts +17 -6
  60. package/dist/types/agent/index.d.ts +1 -1
  61. package/dist/types/agent/tasks.d.ts +4 -2
  62. package/dist/types/agent/utils.d.ts +4 -1
  63. package/dist/types/ai-model/prompt/recorder-generation-common.d.ts +11 -7
  64. package/dist/types/ai-model/prompt/recorder-ui-describer.d.ts +1 -1
  65. package/dist/types/device/index.d.ts +6 -0
  66. package/dist/types/service/index.d.ts +1 -0
  67. package/dist/types/service/utils.d.ts +2 -0
  68. package/dist/types/types.d.ts +1 -0
  69. package/package.json +2 -2
@@ -1 +1 @@
1
- {"version":3,"file":"device/index.mjs","sources":["../../../src/device/index.ts"],"sourcesContent":["import type { ModelRuntime } from '@/ai-model/models';\nimport { getMidsceneLocationSchema } from '@/common';\nimport type {\n ActionScrollParam,\n DeviceAction,\n ExecutorContext,\n LocateResultElement,\n} from '@/types';\nimport type { ElementNode } from '@midscene/shared/extractor';\nimport { getDebug } from '@midscene/shared/logger';\nimport { _keyDefinitions } from '@midscene/shared/us-keyboard-layout';\nimport { z } from 'zod';\nimport type { ElementCacheFeature, Rect, Size, UIContext } from '../types';\n\nexport interface FileChooserHandler {\n accept(files: string[]): Promise<void>;\n}\n\nexport interface FileChooserRegistration {\n dispose: () => void;\n getError: () => Error | undefined | Promise<Error | undefined>;\n}\n\nexport interface MjpegStreamFrame {\n /** Raw base64-encoded image bytes WITHOUT a `data:image/...;base64,` prefix. */\n data: string;\n contentType?: string;\n}\n\nexport interface MjpegStreamHandle {\n stop(): void | Promise<void>;\n}\n\nexport interface MjpegStreamOptions {\n signal?: AbortSignal;\n onFrame(frame: MjpegStreamFrame): void;\n onError?(error: unknown): void;\n}\n\n/** A point in device-pixel coordinates on the screen. */\nexport interface PointerPoint {\n x: number;\n y: number;\n}\n\nexport interface PointerInputPrimitives {\n tap(p: PointerPoint, opts?: { duration?: number }): Promise<void>;\n doubleClick?(p: PointerPoint): Promise<void>;\n rightClick?(p: PointerPoint): Promise<void>;\n hover?(p: PointerPoint): Promise<void>;\n longPress?(p: PointerPoint, opts?: { duration?: number }): Promise<void>;\n dragAndDrop?(from: PointerPoint, to: PointerPoint): Promise<void>;\n}\n\nexport interface TouchInputPrimitives {\n swipe(\n start: PointerPoint,\n end: PointerPoint,\n opts?: { duration?: number; repeat?: number },\n ): Promise<void>;\n pinch?(\n center: PointerPoint,\n opts: { startDistance: number; endDistance: number; duration: number },\n ): Promise<void>;\n}\n\nexport interface KeyboardInputPrimitives {\n keyboardPress(keyName: string, opts?: { target?: unknown }): Promise<void>;\n cursorMove?(direction: 'left' | 'right', times?: number): Promise<void>;\n typeText(\n value: string,\n opts?: {\n autoDismissKeyboard?: boolean;\n keyboardDismissStrategy?: 'esc-first' | 'back-first';\n target?: unknown;\n replace?: boolean;\n focusOnly?: boolean;\n },\n ): Promise<void>;\n clearInput(target?: unknown): Promise<void>;\n}\n\nexport interface ScrollInputPrimitives {\n scroll(param: ActionScrollParam): Promise<void>;\n}\n\nexport interface SystemInputPrimitives {\n backButton?(): Promise<void>;\n homeButton?(): Promise<void>;\n recentAppsButton?(): Promise<void>;\n}\n\nexport interface InputPrimitives {\n pointer?: PointerInputPrimitives;\n keyboard?: KeyboardInputPrimitives;\n touch?: TouchInputPrimitives;\n scroll?: ScrollInputPrimitives;\n system?: SystemInputPrimitives;\n}\n\nexport interface MobileInputPrimitives extends InputPrimitives {\n pointer: PointerInputPrimitives & {\n doubleClick(p: PointerPoint): Promise<void>;\n longPress(p: PointerPoint, opts?: { duration?: number }): Promise<void>;\n dragAndDrop(from: PointerPoint, to: PointerPoint): Promise<void>;\n };\n keyboard: KeyboardInputPrimitives;\n touch: TouchInputPrimitives;\n}\n\nexport interface BrowserInputPrimitives extends InputPrimitives {\n pointer: PointerInputPrimitives & {\n doubleClick(p: PointerPoint): Promise<void>;\n rightClick(p: PointerPoint): Promise<void>;\n hover(p: PointerPoint): Promise<void>;\n dragAndDrop(from: PointerPoint, to: PointerPoint): Promise<void>;\n longPress(p: PointerPoint, opts?: { duration?: number }): Promise<void>;\n };\n keyboard: KeyboardInputPrimitives;\n scroll: ScrollInputPrimitives;\n touch: TouchInputPrimitives;\n}\n\nexport interface ComputerInputPrimitives extends InputPrimitives {\n pointer: PointerInputPrimitives & {\n doubleClick(p: PointerPoint): Promise<void>;\n rightClick(p: PointerPoint): Promise<void>;\n hover(p: PointerPoint): Promise<void>;\n dragAndDrop(from: PointerPoint, to: PointerPoint): Promise<void>;\n };\n keyboard: KeyboardInputPrimitives;\n scroll: ScrollInputPrimitives;\n}\n\nexport abstract class AbstractInterface {\n abstract interfaceType: string;\n\n abstract screenshotBase64(): Promise<string>;\n abstract size(): Promise<Size>;\n abstract actionSpace(): DeviceAction[];\n\n abstract cacheFeatureForPoint?(\n center: [number, number],\n options?: {\n targetDescription?: string;\n modelRuntime?: ModelRuntime;\n },\n ): Promise<ElementCacheFeature>;\n abstract rectMatchesCacheFeature?(\n feature: ElementCacheFeature,\n ): Promise<Rect>;\n\n abstract destroy?(): Promise<void>;\n\n abstract describe?(): string;\n abstract beforeInvokeAction?(actionName: string, param: any): Promise<void>;\n abstract afterInvokeAction?(actionName: string, param: any): Promise<void>;\n\n // for web only\n registerFileChooserListener?(\n handler: (chooser: FileChooserHandler) => Promise<void>,\n ): Promise<FileChooserRegistration>;\n\n // @deprecated do NOT extend this method\n abstract getElementsNodeTree?: () => Promise<ElementNode>;\n\n // @deprecated do NOT extend this method\n abstract url?: () => string | Promise<string>;\n\n // @deprecated do NOT extend this method\n abstract evaluateJavaScript?<T = any>(script: string): Promise<T>;\n\n /**\n * Get the current device-local time as a formatted string.\n * Prefer this for user-visible time because timestamps alone do not preserve\n * the target device's timezone when formatted on the host machine.\n */\n getDeviceLocalTimeString?(format?: string): Promise<string>;\n\n /** URL of native MJPEG stream for real-time screen preview (e.g. WDA MJPEG server) */\n mjpegStreamUrl?: string;\n\n /**\n * Optional in-process MJPEG frame producer. Implementations can push raw\n * base64 frames here when there is no standalone native MJPEG URL, e.g.\n * Chromium CDP Page.startScreencast for web previews.\n */\n startMjpegStream?(\n options: MjpegStreamOptions,\n ): MjpegStreamHandle | undefined | Promise<MjpegStreamHandle | undefined>;\n\n /**\n * Optional hook used after keyboard-only actions to force a fresh frame on\n * the active MJPEG stream. Implementations should be a no-op when no stream\n * is active.\n */\n flushPendingVisualUpdate?(): Promise<void>;\n\n /**\n * Optional navigation state probe for browser-like interfaces, used to drive\n * loading indicators in playground UIs. Returning `undefined` means the\n * interface does not expose this concept.\n */\n navigationState?(): Promise<{ isLoading: boolean }>;\n\n /**\n * Low-level device input surface. Platform implementations expose transport\n * primitives here; higher-level AI actions and manual pointer dispatch should\n * adapt to this instead of duplicating platform gesture logic.\n */\n inputPrimitives?: InputPrimitives;\n}\n\n// Generic function to define actions with proper type inference\n// TRuntime allows specifying a different type for the runtime parameter (after location resolution)\n// TReturn allows specifying the return type of the action\nexport const defineAction = <\n TSchema extends z.ZodType | undefined = undefined,\n TRuntime = TSchema extends z.ZodType ? z.infer<TSchema> : undefined,\n TReturn = any,\n>(\n config: {\n name: string;\n description: string;\n interfaceAlias?: string;\n paramSchema?: TSchema;\n call: (\n param: TRuntime,\n context?: ExecutorContext,\n ) => Promise<TReturn> | TReturn;\n } & Partial<\n Omit<\n DeviceAction<TRuntime, TReturn>,\n 'name' | 'description' | 'interfaceAlias' | 'paramSchema' | 'call'\n >\n >,\n): DeviceAction<TRuntime, TReturn> => {\n return config as any; // Type assertion needed because schema validation type differs from runtime type\n};\n\nfunction pointFromLocate(\n locate: LocateResultElement | undefined,\n missingMessage: string,\n): PointerPoint {\n if (!locate) {\n throw new Error(missingMessage);\n }\n return { x: locate.center[0], y: locate.center[1] };\n}\n\nfunction defineLocatedPointAction<\n TSchema extends z.ZodType,\n TParam extends { locate: LocateResultElement },\n>(config: {\n name: string;\n description: string;\n interfaceAlias?: string;\n paramSchema: TSchema;\n sample: DeviceAction<TParam>['sample'];\n missingLocateMessage: string;\n call: (point: PointerPoint, param: TParam) => Promise<void>;\n}): DeviceAction<TParam> {\n return defineAction<TSchema, TParam>({\n name: config.name,\n description: config.description,\n interfaceAlias: config.interfaceAlias,\n paramSchema: config.paramSchema,\n sample: config.sample,\n call: async (param) => {\n await config.call(\n pointFromLocate(param.locate, config.missingLocateMessage),\n param,\n );\n },\n });\n}\n\n// Tap\nexport const actionTapParamSchema = z.object({\n locate: getMidsceneLocationSchema().describe('The element to be tapped'),\n});\nexport type ActionTapParam = {\n locate: LocateResultElement;\n};\n\nexport const defineActionTap = (\n tap: PointerInputPrimitives['tap'],\n): DeviceAction<ActionTapParam> => {\n return defineLocatedPointAction<typeof actionTapParamSchema, ActionTapParam>({\n name: 'Tap',\n description: 'Tap the element',\n interfaceAlias: 'aiTap',\n paramSchema: actionTapParamSchema,\n sample: {\n locate: { prompt: 'the \"Submit\" button' },\n },\n missingLocateMessage: 'Element not found, cannot tap',\n call: async (point) => {\n await tap(point);\n },\n });\n};\n\n// RightClick\nexport const actionRightClickParamSchema = z.object({\n locate: getMidsceneLocationSchema().describe(\n 'The element to be right clicked',\n ),\n});\nexport type ActionRightClickParam = {\n locate: LocateResultElement;\n};\n\nexport const defineActionRightClick = (\n rightClick: NonNullable<PointerInputPrimitives['rightClick']>,\n): DeviceAction<ActionRightClickParam> => {\n return defineLocatedPointAction<\n typeof actionRightClickParamSchema,\n ActionRightClickParam\n >({\n name: 'RightClick',\n description: 'Right click the element',\n interfaceAlias: 'aiRightClick',\n paramSchema: actionRightClickParamSchema,\n sample: {\n locate: { prompt: 'the file icon on the desktop' },\n },\n missingLocateMessage: 'Element not found, cannot right click',\n call: async (point) => {\n await rightClick(point);\n },\n });\n};\n\n// DoubleClick\nexport const actionDoubleClickParamSchema = z.object({\n locate: getMidsceneLocationSchema().describe(\n 'The element to be double clicked',\n ),\n});\nexport type ActionDoubleClickParam = {\n locate: LocateResultElement;\n};\n\nexport const defineActionDoubleClick = (\n doubleClick: NonNullable<PointerInputPrimitives['doubleClick']>,\n): DeviceAction<ActionDoubleClickParam> => {\n return defineLocatedPointAction<\n typeof actionDoubleClickParamSchema,\n ActionDoubleClickParam\n >({\n name: 'DoubleClick',\n description: 'Double click the element',\n interfaceAlias: 'aiDoubleClick',\n paramSchema: actionDoubleClickParamSchema,\n sample: {\n locate: { prompt: 'the folder icon' },\n },\n missingLocateMessage: 'Element not found, cannot double click',\n call: async (point) => {\n await doubleClick(point);\n },\n });\n};\n\n// Hover\nexport const actionHoverParamSchema = z.object({\n locate: getMidsceneLocationSchema().describe('The element to be hovered'),\n});\nexport type ActionHoverParam = {\n locate: LocateResultElement;\n};\n\nexport const defineActionHover = (\n hover: NonNullable<PointerInputPrimitives['hover']>,\n): DeviceAction<ActionHoverParam> => {\n return defineLocatedPointAction<\n typeof actionHoverParamSchema,\n ActionHoverParam\n >({\n name: 'Hover',\n description: 'Move the mouse to the element',\n interfaceAlias: 'aiHover',\n paramSchema: actionHoverParamSchema,\n sample: {\n locate: { prompt: 'the navigation menu item \"Products\"' },\n },\n missingLocateMessage: 'Element not found, cannot hover',\n call: async (point) => {\n await hover(point);\n },\n });\n};\n\n// Input\nconst inputLocateDescription =\n 'the position of the placeholder or text content in the target input field. If there is no content, locate the center of the input field.';\nexport const actionInputParamSchema = z.object({\n value: z\n .union([z.string(), z.number()])\n .transform((val) => String(val))\n .describe(\n 'The text to input. Provide the final content for replace mode, only the inserted characters for typeOnly mode, or an empty string when using clear mode to remove existing text.',\n ),\n locate: getMidsceneLocationSchema()\n .describe(inputLocateDescription)\n .optional(),\n mode: z\n .enum(['replace', 'clear', 'typeOnly'])\n .default('replace')\n .describe(\n 'Input mode: \"replace\" (default) - clear the field and input the value; \"typeOnly\" - type the value directly without clearing the field first, and should be set explicitly for incremental edits after moving the cursor; \"clear\" - clear the field without inputting new text.',\n ),\n autoDismissKeyboard: z\n .boolean()\n .optional()\n .describe(\n 'If true, the keyboard will be dismissed after the input is completed. Do not set it unless the user asks you to do so.',\n ),\n});\nexport type ActionInputParam = {\n value: string;\n locate?: LocateResultElement;\n mode?: 'replace' | 'clear' | 'typeOnly' | 'append';\n autoDismissKeyboard?: boolean;\n};\n\nexport const defineActionInput = (\n keyboard: KeyboardInputPrimitives,\n): DeviceAction<ActionInputParam> => {\n return defineAction<typeof actionInputParamSchema, ActionInputParam>({\n name: 'Input',\n description: 'Input the value into the element',\n interfaceAlias: 'aiInput',\n paramSchema: actionInputParamSchema,\n sample: {\n value: 'test@example.com',\n locate: { prompt: 'the email input field' },\n },\n call: async (param) => {\n // backward compat: convert deprecated 'append' to 'typeOnly'\n if ((param.mode as string) === 'append') {\n param.mode = 'typeOnly';\n }\n\n if (param.mode === 'clear') {\n await keyboard.clearInput(param.locate);\n return;\n }\n\n if (!param || !param.value) {\n return;\n }\n\n await keyboard.typeText(param.value, {\n target: param.locate,\n replace: param.mode !== 'typeOnly',\n autoDismissKeyboard: param.autoDismissKeyboard,\n });\n },\n });\n};\n\n// KeyboardPress\nexport const actionKeyboardPressParamSchema = z.object({\n locate: getMidsceneLocationSchema()\n .describe('The element to be clicked before pressing the key')\n .optional(),\n keyName: z\n .string()\n .describe(\n \"The key to be pressed. Use '+' for key combinations, e.g., 'Control+A', 'Shift+Enter'\",\n ),\n});\nexport type ActionKeyboardPressParam = {\n locate?: LocateResultElement;\n keyName: string;\n};\n\nexport const defineActionKeyboardPress = (\n keyboardPress: KeyboardInputPrimitives['keyboardPress'],\n): DeviceAction<ActionKeyboardPressParam> => {\n return defineAction<\n typeof actionKeyboardPressParamSchema,\n ActionKeyboardPressParam\n >({\n name: 'KeyboardPress',\n description:\n 'Press a key or key combination, like \"Enter\", \"Tab\", \"Escape\", or \"Control+A\", \"Shift+Enter\". Do not use this to type text.',\n interfaceAlias: 'aiKeyboardPress',\n paramSchema: actionKeyboardPressParamSchema,\n sample: {\n keyName: 'Enter',\n },\n call: async (param) => {\n await keyboardPress(param.keyName, {\n target: param.locate,\n });\n },\n });\n};\n\n// Scroll\nexport const actionScrollParamSchema = z.object({\n scrollType: z\n .enum([\n 'singleAction',\n 'scrollToBottom',\n 'scrollToTop',\n 'scrollToRight',\n 'scrollToLeft',\n ])\n .default('singleAction')\n .describe(\n 'The scroll behavior: \"singleAction\" for a single scroll action, \"scrollToBottom\" for scrolling all the way to the bottom by rapidly scrolling 5-10 times (skipping intermediate content until reaching the bottom), \"scrollToTop\" for scrolling all the way to the top by rapidly scrolling 5-10 times (skipping intermediate content until reaching the top), \"scrollToRight\" for scrolling all the way to the right by rapidly scrolling multiple times, \"scrollToLeft\" for scrolling all the way to the left by rapidly scrolling multiple times',\n ),\n direction: z\n .enum(['down', 'up', 'right', 'left'])\n .default('down')\n .describe(\n 'The direction to scroll. Only effective when scrollType is \"singleAction\".',\n ),\n distance: z\n .number()\n .nullable()\n .optional()\n .describe('The distance in pixels to scroll'),\n locate: getMidsceneLocationSchema()\n .optional()\n .describe(\n 'Describe the target element to be scrolled on, like \"the table\" or \"the list\" or \"the content area\" or \"the scrollable area\". Do NOT provide a general intent like \"scroll to find some element\"',\n ),\n});\n\nexport const defineActionScroll = (\n scroll: ScrollInputPrimitives['scroll'],\n): DeviceAction<ActionScrollParam> => {\n return defineAction<typeof actionScrollParamSchema, ActionScrollParam>({\n name: 'Scroll',\n description:\n 'Scroll the page or a scrollable element to browse content. This is the preferred way to scroll on all platforms, including mobile. Supports scrollToBottom/scrollToTop for boundary navigation. Default: direction `down`, scrollType `singleAction`, distance `null`.',\n interfaceAlias: 'aiScroll',\n paramSchema: actionScrollParamSchema,\n sample: {\n direction: 'down',\n scrollType: 'singleAction',\n locate: { prompt: 'the center of the product list area' },\n },\n call: async (param) => {\n await scroll(param);\n },\n });\n};\n\n// DragAndDrop\nexport const actionDragAndDropParamSchema = z.object({\n from: getMidsceneLocationSchema().describe('The position to be dragged'),\n to: getMidsceneLocationSchema().describe('The position to be dropped'),\n});\nexport type ActionDragAndDropParam = {\n from: LocateResultElement;\n to: LocateResultElement;\n};\n\nexport const defineActionDragAndDrop = (\n dragAndDrop: NonNullable<PointerInputPrimitives['dragAndDrop']>,\n): DeviceAction<ActionDragAndDropParam> => {\n return defineAction<\n typeof actionDragAndDropParamSchema,\n ActionDragAndDropParam\n >({\n name: 'DragAndDrop',\n description:\n 'Pick up a specific UI element and move it to a new position (e.g., reorder a card, move a file into a folder, sort list items). The element itself moves with your finger/mouse.',\n interfaceAlias: 'aiDragAndDrop',\n paramSchema: actionDragAndDropParamSchema,\n sample: {\n from: { prompt: 'the \"report.pdf\" file icon' },\n to: { prompt: 'the upload drop zone' },\n },\n call: async (param) => {\n const from = param.from;\n const to = param.to;\n if (!from) {\n throw new Error('missing \"from\" param for drag and drop');\n }\n if (!to) {\n throw new Error('missing \"to\" param for drag and drop');\n }\n await dragAndDrop(\n { x: from.center[0], y: from.center[1] },\n { x: to.center[0], y: to.center[1] },\n );\n },\n });\n};\n\nexport const ActionLongPressParamSchema = z.object({\n locate: getMidsceneLocationSchema().describe(\n 'The element to be long pressed',\n ),\n duration: z\n .number()\n .optional()\n .describe('Long press duration in milliseconds'),\n});\n\nexport type ActionLongPressParam = {\n locate: LocateResultElement;\n duration?: number;\n};\nexport const defineActionLongPress = (\n longPress: NonNullable<PointerInputPrimitives['longPress']>,\n): DeviceAction<ActionLongPressParam> => {\n return defineLocatedPointAction<\n typeof ActionLongPressParamSchema,\n ActionLongPressParam\n >({\n name: 'LongPress',\n description: 'Long press the element',\n interfaceAlias: 'aiLongPress',\n paramSchema: ActionLongPressParamSchema,\n sample: {\n locate: { prompt: 'the message bubble' },\n },\n missingLocateMessage: 'LongPress requires an element to be located',\n call: async (point, param) => {\n await longPress(point, { duration: param.duration });\n },\n });\n};\n\nexport const ActionSwipeParamSchema = z.object({\n start: getMidsceneLocationSchema()\n .optional()\n .describe(\n 'Starting point of the swipe gesture, if not specified, the center of the page will be used',\n ),\n direction: z\n .enum(['up', 'down', 'left', 'right'])\n .optional()\n .describe(\n 'The direction to swipe (required when using distance). The direction means the direction of the finger swipe.',\n ),\n distance: z\n .number()\n .optional()\n .describe('The distance in pixels to swipe (mutually exclusive with end)'),\n end: getMidsceneLocationSchema()\n .optional()\n .describe(\n 'Ending point of the swipe gesture (mutually exclusive with distance)',\n ),\n duration: z\n .number()\n .default(300)\n .describe('Duration of the swipe gesture in milliseconds'),\n repeat: z\n .number()\n .optional()\n .describe(\n 'The number of times to repeat the swipe gesture. 1 for default, 0 for infinite (e.g. endless swipe until the end of the page)',\n ),\n});\n\nexport type ActionSwipeParam = {\n start?: LocateResultElement;\n direction?: 'up' | 'down' | 'left' | 'right';\n distance?: number;\n end?: LocateResultElement;\n duration?: number;\n repeat?: number;\n};\n\nexport function normalizeMobileSwipeParam(\n param: ActionSwipeParam,\n screenSize: { width: number; height: number },\n): {\n startPoint: { x: number; y: number };\n endPoint: { x: number; y: number };\n duration: number;\n repeatCount: number;\n} {\n const { width, height } = screenSize;\n const { start, end } = param;\n\n const startPoint = start\n ? { x: start.center[0], y: start.center[1] }\n : { x: width / 2, y: height / 2 };\n\n let endPoint: { x: number; y: number };\n\n if (end) {\n endPoint = { x: end.center[0], y: end.center[1] };\n } else if (param.distance) {\n const direction = param.direction;\n if (!direction) {\n throw new Error('direction is required for swipe gesture');\n }\n endPoint = {\n x:\n startPoint.x +\n (direction === 'right'\n ? param.distance\n : direction === 'left'\n ? -param.distance\n : 0),\n y:\n startPoint.y +\n (direction === 'down'\n ? param.distance\n : direction === 'up'\n ? -param.distance\n : 0),\n };\n } else {\n throw new Error(\n 'Either end or distance must be specified for swipe gesture',\n );\n }\n\n endPoint.x = Math.max(0, Math.min(endPoint.x, width));\n endPoint.y = Math.max(0, Math.min(endPoint.y, height));\n\n const duration = param.duration ?? 300;\n\n let repeatCount = typeof param.repeat === 'number' ? param.repeat : 1;\n if (repeatCount === 0) {\n repeatCount = 10;\n }\n\n return { startPoint, endPoint, duration, repeatCount };\n}\n\nexport const defineActionSwipe = (config: {\n swipe: TouchInputPrimitives['swipe'];\n size(): Promise<Size>;\n}): DeviceAction<ActionSwipeParam> => {\n return defineAction<typeof ActionSwipeParamSchema, ActionSwipeParam>({\n name: 'Swipe',\n description:\n 'Perform a touch gesture for interactions beyond regular scrolling (e.g., adjust a continuous control such as a slider, flip pages in a carousel, dismiss a notification, swipe-to-delete a list item). For regular content scrolling, use Scroll instead. Use \"distance\" + \"direction\" for relative movement, or \"start\" + \"end\" for precise endpoint movement.',\n paramSchema: ActionSwipeParamSchema,\n sample: {\n start: { prompt: 'center of the notification' },\n end: { prompt: 'upper edge of the screen' },\n },\n call: async (param) => {\n const { startPoint, endPoint, duration, repeatCount } =\n normalizeMobileSwipeParam(param, await config.size());\n for (let i = 0; i < repeatCount; i++) {\n await config.swipe(startPoint, endPoint, { duration });\n }\n },\n });\n};\n\n// ClearInput\nexport const actionClearInputParamSchema = z.object({\n locate: getMidsceneLocationSchema()\n .describe('The input field to be cleared')\n .optional(),\n});\nexport type ActionClearInputParam = {\n locate?: LocateResultElement;\n};\n\nexport const defineActionClearInput = (\n clearInput: KeyboardInputPrimitives['clearInput'],\n): DeviceAction<ActionClearInputParam> => {\n return defineAction<\n typeof actionClearInputParamSchema,\n ActionClearInputParam\n >({\n name: 'ClearInput',\n description: inputLocateDescription,\n interfaceAlias: 'aiClearInput',\n paramSchema: actionClearInputParamSchema,\n sample: {\n locate: { prompt: 'the search input field' },\n },\n call: async (param) => {\n await clearInput(param.locate);\n },\n });\n};\n\n// CursorMove\nexport const actionCursorMoveParamSchema = z.object({\n direction: z\n .enum(['left', 'right'])\n .describe('The direction to move the cursor'),\n times: z\n .number()\n .int()\n .min(1)\n .default(1)\n .describe(\n 'The number of times to move the cursor in the specified direction',\n ),\n});\nexport type ActionCursorMoveParam = {\n direction: 'left' | 'right';\n times?: number;\n};\n\nexport const defineActionCursorMove = (config: {\n keyboard: Pick<KeyboardInputPrimitives, 'keyboardPress' | 'cursorMove'>;\n sleep?(timeMs: number): Promise<void>;\n}): DeviceAction<ActionCursorMoveParam> => {\n return defineAction<\n typeof actionCursorMoveParamSchema,\n ActionCursorMoveParam\n >({\n name: 'CursorMove',\n description:\n 'Move the text cursor (caret) left or right within an input field or text area. Use this to reposition the cursor without selecting text.',\n paramSchema: actionCursorMoveParamSchema,\n sample: {\n direction: 'left',\n times: 3,\n },\n call: async (param) => {\n const times = param.times ?? 1;\n if (config.keyboard.cursorMove) {\n await config.keyboard.cursorMove(param.direction, times);\n return;\n }\n\n const wait =\n config.sleep ??\n ((timeMs: number) =>\n new Promise<void>((resolve) => setTimeout(resolve, timeMs)));\n const arrowKey = param.direction === 'left' ? 'ArrowLeft' : 'ArrowRight';\n for (let i = 0; i < times; i++) {\n await config.keyboard.keyboardPress(arrowKey);\n await wait(100);\n }\n },\n });\n};\n\n// Pinch\nexport const ActionPinchParamSchema = z.object({\n locate: getMidsceneLocationSchema()\n .optional()\n .describe(\n 'The element to pinch on. If not specified, the center of the screen will be used',\n ),\n direction: z\n .enum(['in', 'out'])\n .describe(\n 'Pinch direction. \"in\" = pinch fingers together (zoom out / shrink), \"out\" = spread fingers apart (zoom in / enlarge).',\n ),\n distance: z\n .number()\n .positive()\n .optional()\n .describe(\n 'How far each finger moves in pixels. Defaults to a quarter of the shorter screen dimension.',\n ),\n duration: z\n .number()\n .default(500)\n .optional()\n .describe('Duration of the pinch gesture in milliseconds'),\n});\n\nexport type ActionPinchParam = {\n locate?: LocateResultElement;\n direction: 'in' | 'out';\n distance?: number;\n duration?: number;\n};\n\nexport const defineActionPinch = (config: {\n pinch: TouchInputPrimitives['pinch'];\n size(): Promise<Size>;\n}): DeviceAction<ActionPinchParam> | undefined => {\n if (!config.pinch) {\n return undefined;\n }\n\n return defineAction<typeof ActionPinchParamSchema, ActionPinchParam>({\n name: 'Pinch',\n description:\n 'Perform a two-finger pinch gesture. Use direction \"in\" to pinch fingers together (zoom out), or \"out\" to spread fingers apart (zoom in). Optionally specify distance for how far each finger moves.',\n interfaceAlias: 'aiPinch',\n paramSchema: ActionPinchParamSchema,\n sample: {\n locate: { prompt: 'the map area' },\n direction: 'out',\n distance: 200,\n },\n call: async (param) => {\n const { centerX, centerY, startDistance, endDistance, duration } =\n normalizePinchParam(param, await config.size());\n await config.pinch?.(\n { x: centerX, y: centerY },\n { startDistance, endDistance, duration },\n );\n },\n });\n};\n\nexport function normalizePinchParam(\n param: ActionPinchParam,\n screenSize: { width: number; height: number },\n): {\n centerX: number;\n centerY: number;\n startDistance: number;\n endDistance: number;\n duration: number;\n} {\n const { width, height } = screenSize;\n const element = param.locate;\n const centerX = element\n ? Math.round(element.center[0])\n : Math.round(width / 2);\n const centerY = element\n ? Math.round(element.center[1])\n : Math.round(height / 2);\n const duration = param.duration ?? 500;\n\n const baseDistance = Math.round(Math.min(width, height) / 4);\n const fingerDistance = param.distance ?? baseDistance;\n\n const startDistance = baseDistance;\n const endDistance =\n param.direction === 'out'\n ? baseDistance + fingerDistance\n : Math.max(10, baseDistance - fingerDistance);\n\n return { centerX, centerY, startDistance, endDistance, duration };\n}\n\nexport interface MobileInputActionContext {\n input: MobileInputPrimitives;\n size(): Promise<Size>;\n sleep?(timeMs: number): Promise<void>;\n getDefaultAutoDismissKeyboard?(): boolean | undefined;\n systemActions?: SystemInputActionOptions;\n}\n\nexport interface SystemInputActionConfig {\n name: string;\n description: string;\n interfaceAlias?: string;\n delayBeforeRunner?: number;\n delayAfterRunner?: number;\n}\n\nexport interface SystemInputActionOptions {\n backButton?: SystemInputActionConfig;\n homeButton?: SystemInputActionConfig;\n recentAppsButton?: SystemInputActionConfig;\n}\n\nexport interface InputPrimitiveActionOptions {\n size?: () => Promise<Size>;\n sleep?: (timeMs: number) => Promise<void>;\n includeSwipe?: boolean;\n includePinch?: boolean;\n systemActions?: SystemInputActionOptions;\n}\n\nfunction defineSystemInputAction(\n config: SystemInputActionConfig,\n call: () => Promise<void>,\n): DeviceAction<undefined, void> {\n return defineAction<undefined, undefined, void>({\n name: config.name,\n description: config.description,\n interfaceAlias: config.interfaceAlias,\n delayBeforeRunner: config.delayBeforeRunner,\n delayAfterRunner: config.delayAfterRunner,\n call,\n });\n}\n\nexport function defineActionsFromInputPrimitives(\n input: InputPrimitives,\n options: InputPrimitiveActionOptions = {},\n): DeviceAction<any>[] {\n const actions: Array<DeviceAction<any> | undefined> = [];\n const { pointer, keyboard, scroll, touch, system } = input;\n\n if (pointer) {\n actions.push(defineActionTap(pointer.tap));\n if (pointer.doubleClick) {\n actions.push(defineActionDoubleClick(pointer.doubleClick));\n }\n if (pointer.rightClick) {\n actions.push(defineActionRightClick(pointer.rightClick));\n }\n if (pointer.hover) {\n actions.push(defineActionHover(pointer.hover));\n }\n if (pointer.dragAndDrop) {\n actions.push(defineActionDragAndDrop(pointer.dragAndDrop));\n }\n if (pointer.longPress) {\n actions.push(defineActionLongPress(pointer.longPress));\n }\n }\n\n if (keyboard) {\n actions.push(\n defineActionInput(keyboard),\n defineActionClearInput(keyboard.clearInput),\n defineActionKeyboardPress(keyboard.keyboardPress),\n defineActionCursorMove({ keyboard, sleep: options.sleep }),\n );\n }\n\n if (scroll) {\n actions.push(defineActionScroll(scroll.scroll));\n }\n\n if (touch?.swipe && options.size && options.includeSwipe !== false) {\n actions.push(defineActionSwipe({ swipe: touch.swipe, size: options.size }));\n }\n\n if (touch?.pinch && options.size && options.includePinch !== false) {\n actions.push(defineActionPinch({ pinch: touch.pinch, size: options.size }));\n }\n\n if (system && options.systemActions) {\n const { systemActions } = options;\n if (system.backButton && systemActions.backButton) {\n actions.push(\n defineSystemInputAction(systemActions.backButton, system.backButton),\n );\n }\n if (system.homeButton && systemActions.homeButton) {\n actions.push(\n defineSystemInputAction(systemActions.homeButton, system.homeButton),\n );\n }\n if (system.recentAppsButton && systemActions.recentAppsButton) {\n actions.push(\n defineSystemInputAction(\n systemActions.recentAppsButton,\n system.recentAppsButton,\n ),\n );\n }\n }\n\n return actions.filter((action): action is DeviceAction<any> =>\n Boolean(action),\n );\n}\n\nexport function createDefaultMobileActions(\n context: MobileInputActionContext,\n): DeviceAction<any>[] {\n return defineActionsFromInputPrimitives(context.input, {\n size: context.size,\n sleep: context.sleep,\n systemActions: context.systemActions,\n });\n}\n\n// Sleep\nexport const ActionSleepParamSchema = z.object({\n timeMs: z\n .number()\n .default(1000)\n .optional()\n .describe('Sleep duration in milliseconds, defaults to 1000ms (1 second)'),\n});\n\nexport type ActionSleepParam = {\n timeMs?: number;\n};\n\nexport const defineActionSleep = (): DeviceAction<ActionSleepParam> => {\n return defineAction<typeof ActionSleepParamSchema, ActionSleepParam>({\n name: 'Sleep',\n description:\n 'Wait for a specified duration before continuing. Defaults to 1 second (1000ms) if not specified.',\n paramSchema: ActionSleepParamSchema,\n sample: {\n timeMs: 2000,\n },\n call: async (param) => {\n const duration = param?.timeMs ?? 1000;\n getDebug('device:common-action')(`Sleeping for ${duration}ms`);\n await new Promise((resolve) => setTimeout(resolve, duration));\n },\n });\n};\n\nexport type { DeviceAction } from '../types';\nexport type {\n AndroidDeviceOpt,\n AndroidDeviceInputOpt,\n IOSDeviceOpt,\n IOSDeviceInputOpt,\n HarmonyDeviceOpt,\n HarmonyDeviceInputOpt,\n} from './device-options';\n"],"names":["AbstractInterface","defineAction","config","pointFromLocate","locate","missingMessage","Error","defineLocatedPointAction","param","actionTapParamSchema","z","getMidsceneLocationSchema","defineActionTap","tap","point","actionRightClickParamSchema","defineActionRightClick","rightClick","actionDoubleClickParamSchema","defineActionDoubleClick","doubleClick","actionHoverParamSchema","defineActionHover","hover","inputLocateDescription","actionInputParamSchema","val","String","defineActionInput","keyboard","actionKeyboardPressParamSchema","defineActionKeyboardPress","keyboardPress","actionScrollParamSchema","defineActionScroll","scroll","actionDragAndDropParamSchema","defineActionDragAndDrop","dragAndDrop","from","to","ActionLongPressParamSchema","defineActionLongPress","longPress","ActionSwipeParamSchema","normalizeMobileSwipeParam","screenSize","width","height","start","end","startPoint","endPoint","direction","Math","duration","repeatCount","defineActionSwipe","i","actionClearInputParamSchema","defineActionClearInput","clearInput","actionCursorMoveParamSchema","defineActionCursorMove","times","wait","timeMs","Promise","resolve","setTimeout","arrowKey","ActionPinchParamSchema","defineActionPinch","centerX","centerY","startDistance","endDistance","normalizePinchParam","element","baseDistance","fingerDistance","defineSystemInputAction","call","defineActionsFromInputPrimitives","input","options","actions","pointer","touch","system","systemActions","action","Boolean","createDefaultMobileActions","context","ActionSleepParamSchema","defineActionSleep","getDebug"],"mappings":";;;;;;;;;;;;;AAsIO,MAAeA;;QA8CpB;QA8BA;;AACF;AAKO,MAAMC,eAAe,CAK1BC,SAgBOA;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,cAAc,gBAAgB,EAC3DJ,QAAQ,IAAI,CACVL,wBACES,cAAc,gBAAgB,EAC9BD,OAAO,gBAAgB;IAI/B;IAEA,OAAOH,QAAQ,MAAM,CAAC,CAACK,SACrBC,QAAQD;AAEZ;AAEO,SAASE,2BACdC,OAAiC;IAEjC,OAAOX,iCAAiCW,QAAQ,KAAK,EAAE;QACrD,MAAMA,QAAQ,IAAI;QAClB,OAAOA,QAAQ,KAAK;QACpB,eAAeA,QAAQ,aAAa;IACtC;AACF;AAGO,MAAMC,yBAAyBrF,EAAE,MAAM,CAAC;IAC7C,QAAQA,EAAAA,MACC,GACN,OAAO,CAAC,MACR,QAAQ,GACR,QAAQ,CAAC;AACd;AAMO,MAAMsF,oBAAoB,IACxB/F,aAA8D;QACnE,MAAM;QACN,aACE;QACF,aAAa8F;QACb,QAAQ;YACN,QAAQ;QACV;QACA,MAAM,OAAOvF;YACX,MAAM+C,WAAW/C,OAAO,UAAU;YAClCyF,SAAS,wBAAwB,CAAC,aAAa,EAAE1C,SAAS,EAAE,CAAC;YAC7D,MAAM,IAAIY,QAAQ,CAACC,UAAYC,WAAWD,SAASb;QACrD;IACF"}
1
+ {"version":3,"file":"device/index.mjs","sources":["../../../src/device/index.ts"],"sourcesContent":["import type { ModelRuntime } from '@/ai-model/models';\nimport { getMidsceneLocationSchema } from '@/common';\nimport type {\n ActionScrollParam,\n DeviceAction,\n ExecutorContext,\n LocateResultElement,\n} from '@/types';\nimport type { ElementNode } from '@midscene/shared/extractor';\nimport { getDebug } from '@midscene/shared/logger';\nimport { _keyDefinitions } from '@midscene/shared/us-keyboard-layout';\nimport { z } from 'zod';\nimport type { ElementCacheFeature, Rect, Size, UIContext } from '../types';\n\nexport interface FileChooserHandler {\n accept(files: string[]): Promise<void>;\n}\n\nexport interface FileChooserRegistration {\n dispose: () => void;\n getError: () => Error | undefined | Promise<Error | undefined>;\n}\n\nexport interface MjpegStreamFrame {\n /** Raw base64-encoded image bytes WITHOUT a `data:image/...;base64,` prefix. */\n data: string;\n contentType?: string;\n}\n\nexport interface MjpegStreamHandle {\n stop(): void | Promise<void>;\n}\n\nexport interface MjpegStreamOptions {\n signal?: AbortSignal;\n onFrame(frame: MjpegStreamFrame): void;\n onError?(error: unknown): void;\n}\n\n/** A point in device-pixel coordinates on the screen. */\nexport interface PointerPoint {\n x: number;\n y: number;\n}\n\nexport interface PointerInputPrimitives {\n tap(p: PointerPoint, opts?: { duration?: number }): Promise<void>;\n doubleClick?(p: PointerPoint): Promise<void>;\n rightClick?(p: PointerPoint): Promise<void>;\n hover?(p: PointerPoint): Promise<void>;\n longPress?(p: PointerPoint, opts?: { duration?: number }): Promise<void>;\n dragAndDrop?(from: PointerPoint, to: PointerPoint): Promise<void>;\n}\n\nexport interface TouchInputPrimitives {\n swipe(\n start: PointerPoint,\n end: PointerPoint,\n opts?: { duration?: number; repeat?: number },\n ): Promise<void>;\n pinch?(\n center: PointerPoint,\n opts: { startDistance: number; endDistance: number; duration: number },\n ): Promise<void>;\n}\n\nexport interface KeyboardInputPrimitives {\n keyboardPress(keyName: string, opts?: { target?: unknown }): Promise<void>;\n cursorMove?(direction: 'left' | 'right', times?: number): Promise<void>;\n typeText(\n value: string,\n opts?: {\n autoDismissKeyboard?: boolean;\n keyboardDismissStrategy?: 'esc-first' | 'back-first';\n target?: unknown;\n replace?: boolean;\n focusOnly?: boolean;\n },\n ): Promise<void>;\n clearInput(target?: unknown): Promise<void>;\n}\n\nexport interface ScrollInputPrimitives {\n scroll(param: ActionScrollParam): Promise<void>;\n}\n\nexport interface SystemInputPrimitives {\n backButton?(): Promise<void>;\n homeButton?(): Promise<void>;\n recentAppsButton?(): Promise<void>;\n}\n\nexport interface InputPrimitives {\n pointer?: PointerInputPrimitives;\n keyboard?: KeyboardInputPrimitives;\n touch?: TouchInputPrimitives;\n scroll?: ScrollInputPrimitives;\n system?: SystemInputPrimitives;\n}\n\nexport interface MobileInputPrimitives extends InputPrimitives {\n pointer: PointerInputPrimitives & {\n doubleClick(p: PointerPoint): Promise<void>;\n longPress(p: PointerPoint, opts?: { duration?: number }): Promise<void>;\n dragAndDrop(from: PointerPoint, to: PointerPoint): Promise<void>;\n };\n keyboard: KeyboardInputPrimitives;\n touch: TouchInputPrimitives;\n}\n\nexport interface BrowserInputPrimitives extends InputPrimitives {\n pointer: PointerInputPrimitives & {\n doubleClick(p: PointerPoint): Promise<void>;\n rightClick(p: PointerPoint): Promise<void>;\n hover(p: PointerPoint): Promise<void>;\n dragAndDrop(from: PointerPoint, to: PointerPoint): Promise<void>;\n longPress(p: PointerPoint, opts?: { duration?: number }): Promise<void>;\n };\n keyboard: KeyboardInputPrimitives;\n scroll: ScrollInputPrimitives;\n touch: TouchInputPrimitives;\n}\n\nexport interface ComputerInputPrimitives extends InputPrimitives {\n pointer: PointerInputPrimitives & {\n doubleClick(p: PointerPoint): Promise<void>;\n rightClick(p: PointerPoint): Promise<void>;\n hover(p: PointerPoint): Promise<void>;\n dragAndDrop(from: PointerPoint, to: PointerPoint): Promise<void>;\n };\n keyboard: KeyboardInputPrimitives;\n scroll: ScrollInputPrimitives;\n}\n\nexport abstract class AbstractInterface {\n abstract interfaceType: string;\n\n abstract screenshotBase64(): Promise<string>;\n abstract size(): Promise<Size>;\n abstract actionSpace(): DeviceAction[];\n\n abstract cacheFeatureForPoint?(\n center: [number, number],\n options?: {\n targetDescription?: string;\n modelRuntime?: ModelRuntime;\n },\n ): Promise<ElementCacheFeature>;\n abstract rectMatchesCacheFeature?(\n feature: ElementCacheFeature,\n ): Promise<Rect>;\n\n abstract destroy?(): Promise<void>;\n\n abstract describe?(): string;\n abstract beforeInvokeAction?(actionName: string, param: any): Promise<void>;\n abstract afterInvokeAction?(actionName: string, param: any): Promise<void>;\n\n // for web only\n registerFileChooserListener?(\n handler: (chooser: FileChooserHandler) => Promise<void>,\n ): Promise<FileChooserRegistration>;\n\n // @deprecated do NOT extend this method\n abstract getElementsNodeTree?: () => Promise<ElementNode>;\n\n // @deprecated do NOT extend this method\n abstract url?: () => string | Promise<string>;\n\n // @deprecated do NOT extend this method\n abstract evaluateJavaScript?<T = any>(script: string): Promise<T>;\n\n /**\n * Get the current device-local time as a formatted string.\n * Prefer this for user-visible time because timestamps alone do not preserve\n * the target device's timezone when formatted on the host machine.\n */\n getDeviceLocalTimeString?(format?: string): Promise<string>;\n\n /** URL of native MJPEG stream for real-time screen preview (e.g. WDA MJPEG server) */\n mjpegStreamUrl?: string;\n\n /**\n * Optional in-process MJPEG frame producer. Implementations can push raw\n * base64 frames here when there is no standalone native MJPEG URL, e.g.\n * Chromium CDP Page.startScreencast for web previews.\n */\n startMjpegStream?(\n options: MjpegStreamOptions,\n ): MjpegStreamHandle | undefined | Promise<MjpegStreamHandle | undefined>;\n\n /**\n * Optional hook used after keyboard-only actions to force a fresh frame on\n * the active MJPEG stream. Implementations should be a no-op when no stream\n * is active.\n */\n flushPendingVisualUpdate?(): Promise<void>;\n\n /**\n * Optional non-blocking variant of `flushPendingVisualUpdate`. Keyboard-\n * heavy preview interactions can schedule a coalesced refresh here without\n * stalling the input hot path.\n */\n schedulePendingVisualUpdate?(): void;\n\n /**\n * Optional navigation state probe for browser-like interfaces, used to drive\n * loading indicators in playground UIs. Returning `undefined` means the\n * interface does not expose this concept.\n */\n navigationState?(): Promise<{ isLoading: boolean }>;\n\n /**\n * Low-level device input surface. Platform implementations expose transport\n * primitives here; higher-level AI actions and manual pointer dispatch should\n * adapt to this instead of duplicating platform gesture logic.\n */\n inputPrimitives?: InputPrimitives;\n}\n\n// Generic function to define actions with proper type inference\n// TRuntime allows specifying a different type for the runtime parameter (after location resolution)\n// TReturn allows specifying the return type of the action\nexport const defineAction = <\n TSchema extends z.ZodType | undefined = undefined,\n TRuntime = TSchema extends z.ZodType ? z.infer<TSchema> : undefined,\n TReturn = any,\n>(\n config: {\n name: string;\n description: string;\n interfaceAlias?: string;\n paramSchema?: TSchema;\n call: (\n param: TRuntime,\n context?: ExecutorContext,\n ) => Promise<TReturn> | TReturn;\n } & Partial<\n Omit<\n DeviceAction<TRuntime, TReturn>,\n 'name' | 'description' | 'interfaceAlias' | 'paramSchema' | 'call'\n >\n >,\n): DeviceAction<TRuntime, TReturn> => {\n return config as any; // Type assertion needed because schema validation type differs from runtime type\n};\n\nfunction pointFromLocate(\n locate: LocateResultElement | undefined,\n missingMessage: string,\n): PointerPoint {\n if (!locate) {\n throw new Error(missingMessage);\n }\n return { x: locate.center[0], y: locate.center[1] };\n}\n\nfunction defineLocatedPointAction<\n TSchema extends z.ZodType,\n TParam extends { locate: LocateResultElement },\n>(config: {\n name: string;\n description: string;\n interfaceAlias?: string;\n paramSchema: TSchema;\n sample: DeviceAction<TParam>['sample'];\n missingLocateMessage: string;\n call: (point: PointerPoint, param: TParam) => Promise<void>;\n}): DeviceAction<TParam> {\n return defineAction<TSchema, TParam>({\n name: config.name,\n description: config.description,\n interfaceAlias: config.interfaceAlias,\n paramSchema: config.paramSchema,\n sample: config.sample,\n call: async (param) => {\n await config.call(\n pointFromLocate(param.locate, config.missingLocateMessage),\n param,\n );\n },\n });\n}\n\n// Tap\nexport const actionTapParamSchema = z.object({\n locate: getMidsceneLocationSchema().describe('The element to be tapped'),\n});\nexport type ActionTapParam = {\n locate: LocateResultElement;\n};\n\nexport const defineActionTap = (\n tap: PointerInputPrimitives['tap'],\n): DeviceAction<ActionTapParam> => {\n return defineLocatedPointAction<typeof actionTapParamSchema, ActionTapParam>({\n name: 'Tap',\n description: 'Tap the element',\n interfaceAlias: 'aiTap',\n paramSchema: actionTapParamSchema,\n sample: {\n locate: { prompt: 'the \"Submit\" button' },\n },\n missingLocateMessage: 'Element not found, cannot tap',\n call: async (point) => {\n await tap(point);\n },\n });\n};\n\n// RightClick\nexport const actionRightClickParamSchema = z.object({\n locate: getMidsceneLocationSchema().describe(\n 'The element to be right clicked',\n ),\n});\nexport type ActionRightClickParam = {\n locate: LocateResultElement;\n};\n\nexport const defineActionRightClick = (\n rightClick: NonNullable<PointerInputPrimitives['rightClick']>,\n): DeviceAction<ActionRightClickParam> => {\n return defineLocatedPointAction<\n typeof actionRightClickParamSchema,\n ActionRightClickParam\n >({\n name: 'RightClick',\n description: 'Right click the element',\n interfaceAlias: 'aiRightClick',\n paramSchema: actionRightClickParamSchema,\n sample: {\n locate: { prompt: 'the file icon on the desktop' },\n },\n missingLocateMessage: 'Element not found, cannot right click',\n call: async (point) => {\n await rightClick(point);\n },\n });\n};\n\n// DoubleClick\nexport const actionDoubleClickParamSchema = z.object({\n locate: getMidsceneLocationSchema().describe(\n 'The element to be double clicked',\n ),\n});\nexport type ActionDoubleClickParam = {\n locate: LocateResultElement;\n};\n\nexport const defineActionDoubleClick = (\n doubleClick: NonNullable<PointerInputPrimitives['doubleClick']>,\n): DeviceAction<ActionDoubleClickParam> => {\n return defineLocatedPointAction<\n typeof actionDoubleClickParamSchema,\n ActionDoubleClickParam\n >({\n name: 'DoubleClick',\n description: 'Double click the element',\n interfaceAlias: 'aiDoubleClick',\n paramSchema: actionDoubleClickParamSchema,\n sample: {\n locate: { prompt: 'the folder icon' },\n },\n missingLocateMessage: 'Element not found, cannot double click',\n call: async (point) => {\n await doubleClick(point);\n },\n });\n};\n\n// Hover\nexport const actionHoverParamSchema = z.object({\n locate: getMidsceneLocationSchema().describe('The element to be hovered'),\n});\nexport type ActionHoverParam = {\n locate: LocateResultElement;\n};\n\nexport const defineActionHover = (\n hover: NonNullable<PointerInputPrimitives['hover']>,\n): DeviceAction<ActionHoverParam> => {\n return defineLocatedPointAction<\n typeof actionHoverParamSchema,\n ActionHoverParam\n >({\n name: 'Hover',\n description: 'Move the mouse to the element',\n interfaceAlias: 'aiHover',\n paramSchema: actionHoverParamSchema,\n sample: {\n locate: { prompt: 'the navigation menu item \"Products\"' },\n },\n missingLocateMessage: 'Element not found, cannot hover',\n call: async (point) => {\n await hover(point);\n },\n });\n};\n\n// Input\nconst inputLocateDescription =\n 'the position of the placeholder or text content in the target input field. If there is no content, locate the center of the input field.';\nexport const actionInputParamSchema = z.object({\n value: z\n .union([z.string(), z.number()])\n .transform((val) => String(val))\n .describe(\n 'The text to input. Provide the final content for replace mode, only the inserted characters for typeOnly mode, or an empty string when using clear mode to remove existing text.',\n ),\n locate: getMidsceneLocationSchema()\n .describe(inputLocateDescription)\n .optional(),\n mode: z\n .enum(['replace', 'clear', 'typeOnly'])\n .default('replace')\n .describe(\n 'Input mode: \"replace\" (default) - clear the field and input the value; \"typeOnly\" - type the value directly without clearing the field first, and should be set explicitly for incremental edits after moving the cursor; \"clear\" - clear the field without inputting new text.',\n ),\n autoDismissKeyboard: z\n .boolean()\n .optional()\n .describe(\n 'If true, the keyboard will be dismissed after the input is completed. Do not set it unless the user asks you to do so.',\n ),\n});\nexport type ActionInputParam = {\n value: string;\n locate?: LocateResultElement;\n mode?: 'replace' | 'clear' | 'typeOnly' | 'append';\n autoDismissKeyboard?: boolean;\n};\n\nexport const defineActionInput = (\n keyboard: KeyboardInputPrimitives,\n): DeviceAction<ActionInputParam> => {\n return defineAction<typeof actionInputParamSchema, ActionInputParam>({\n name: 'Input',\n description: 'Input the value into the element',\n interfaceAlias: 'aiInput',\n paramSchema: actionInputParamSchema,\n sample: {\n value: 'test@example.com',\n locate: { prompt: 'the email input field' },\n },\n call: async (param) => {\n // backward compat: convert deprecated 'append' to 'typeOnly'\n if ((param.mode as string) === 'append') {\n param.mode = 'typeOnly';\n }\n\n if (param.mode === 'clear') {\n await keyboard.clearInput(param.locate);\n return;\n }\n\n if (!param || !param.value) {\n return;\n }\n\n await keyboard.typeText(param.value, {\n target: param.locate,\n replace: param.mode !== 'typeOnly',\n autoDismissKeyboard: param.autoDismissKeyboard,\n });\n },\n });\n};\n\n// KeyboardPress\nexport const actionKeyboardPressParamSchema = z.object({\n locate: getMidsceneLocationSchema()\n .describe('The element to be clicked before pressing the key')\n .optional(),\n keyName: z\n .string()\n .describe(\n \"The key to be pressed. Use '+' for key combinations, e.g., 'Control+A', 'Shift+Enter'\",\n ),\n});\nexport type ActionKeyboardPressParam = {\n locate?: LocateResultElement;\n keyName: string;\n};\n\nexport const defineActionKeyboardPress = (\n keyboardPress: KeyboardInputPrimitives['keyboardPress'],\n): DeviceAction<ActionKeyboardPressParam> => {\n return defineAction<\n typeof actionKeyboardPressParamSchema,\n ActionKeyboardPressParam\n >({\n name: 'KeyboardPress',\n description:\n 'Press a key or key combination, like \"Enter\", \"Tab\", \"Escape\", or \"Control+A\", \"Shift+Enter\". Do not use this to type text.',\n interfaceAlias: 'aiKeyboardPress',\n paramSchema: actionKeyboardPressParamSchema,\n sample: {\n keyName: 'Enter',\n },\n call: async (param) => {\n await keyboardPress(param.keyName, {\n target: param.locate,\n });\n },\n });\n};\n\n// Scroll\nexport const actionScrollParamSchema = z.object({\n scrollType: z\n .enum([\n 'singleAction',\n 'scrollToBottom',\n 'scrollToTop',\n 'scrollToRight',\n 'scrollToLeft',\n ])\n .default('singleAction')\n .describe(\n 'The scroll behavior: \"singleAction\" for a single scroll action, \"scrollToBottom\" for scrolling all the way to the bottom by rapidly scrolling 5-10 times (skipping intermediate content until reaching the bottom), \"scrollToTop\" for scrolling all the way to the top by rapidly scrolling 5-10 times (skipping intermediate content until reaching the top), \"scrollToRight\" for scrolling all the way to the right by rapidly scrolling multiple times, \"scrollToLeft\" for scrolling all the way to the left by rapidly scrolling multiple times',\n ),\n direction: z\n .enum(['down', 'up', 'right', 'left'])\n .default('down')\n .describe(\n 'The direction to scroll. Only effective when scrollType is \"singleAction\".',\n ),\n distance: z\n .number()\n .nullable()\n .optional()\n .describe('The distance in pixels to scroll'),\n locate: getMidsceneLocationSchema()\n .optional()\n .describe(\n 'Describe the target element to be scrolled on, like \"the table\" or \"the list\" or \"the content area\" or \"the scrollable area\". Do NOT provide a general intent like \"scroll to find some element\"',\n ),\n});\n\nexport const defineActionScroll = (\n scroll: ScrollInputPrimitives['scroll'],\n): DeviceAction<ActionScrollParam> => {\n return defineAction<typeof actionScrollParamSchema, ActionScrollParam>({\n name: 'Scroll',\n description:\n 'Scroll the page or a scrollable element to browse content. This is the preferred way to scroll on all platforms, including mobile. Supports scrollToBottom/scrollToTop for boundary navigation. Default: direction `down`, scrollType `singleAction`, distance `null`.',\n interfaceAlias: 'aiScroll',\n paramSchema: actionScrollParamSchema,\n sample: {\n direction: 'down',\n scrollType: 'singleAction',\n locate: { prompt: 'the center of the product list area' },\n },\n call: async (param) => {\n await scroll(param);\n },\n });\n};\n\n// DragAndDrop\nexport const actionDragAndDropParamSchema = z.object({\n from: getMidsceneLocationSchema().describe('The position to be dragged'),\n to: getMidsceneLocationSchema().describe('The position to be dropped'),\n});\nexport type ActionDragAndDropParam = {\n from: LocateResultElement;\n to: LocateResultElement;\n};\n\nexport const defineActionDragAndDrop = (\n dragAndDrop: NonNullable<PointerInputPrimitives['dragAndDrop']>,\n): DeviceAction<ActionDragAndDropParam> => {\n return defineAction<\n typeof actionDragAndDropParamSchema,\n ActionDragAndDropParam\n >({\n name: 'DragAndDrop',\n description:\n 'Pick up a specific UI element and move it to a new position (e.g., reorder a card, move a file into a folder, sort list items). The element itself moves with your finger/mouse.',\n interfaceAlias: 'aiDragAndDrop',\n paramSchema: actionDragAndDropParamSchema,\n sample: {\n from: { prompt: 'the \"report.pdf\" file icon' },\n to: { prompt: 'the upload drop zone' },\n },\n call: async (param) => {\n const from = param.from;\n const to = param.to;\n if (!from) {\n throw new Error('missing \"from\" param for drag and drop');\n }\n if (!to) {\n throw new Error('missing \"to\" param for drag and drop');\n }\n await dragAndDrop(\n { x: from.center[0], y: from.center[1] },\n { x: to.center[0], y: to.center[1] },\n );\n },\n });\n};\n\nexport const ActionLongPressParamSchema = z.object({\n locate: getMidsceneLocationSchema().describe(\n 'The element to be long pressed',\n ),\n duration: z\n .number()\n .optional()\n .describe('Long press duration in milliseconds'),\n});\n\nexport type ActionLongPressParam = {\n locate: LocateResultElement;\n duration?: number;\n};\nexport const defineActionLongPress = (\n longPress: NonNullable<PointerInputPrimitives['longPress']>,\n): DeviceAction<ActionLongPressParam> => {\n return defineLocatedPointAction<\n typeof ActionLongPressParamSchema,\n ActionLongPressParam\n >({\n name: 'LongPress',\n description: 'Long press the element',\n interfaceAlias: 'aiLongPress',\n paramSchema: ActionLongPressParamSchema,\n sample: {\n locate: { prompt: 'the message bubble' },\n },\n missingLocateMessage: 'LongPress requires an element to be located',\n call: async (point, param) => {\n await longPress(point, { duration: param.duration });\n },\n });\n};\n\nexport const ActionSwipeParamSchema = z.object({\n start: getMidsceneLocationSchema()\n .optional()\n .describe(\n 'Starting point of the swipe gesture, if not specified, the center of the page will be used',\n ),\n direction: z\n .enum(['up', 'down', 'left', 'right'])\n .optional()\n .describe(\n 'The direction to swipe (required when using distance). The direction means the direction of the finger swipe.',\n ),\n distance: z\n .number()\n .optional()\n .describe('The distance in pixels to swipe (mutually exclusive with end)'),\n end: getMidsceneLocationSchema()\n .optional()\n .describe(\n 'Ending point of the swipe gesture (mutually exclusive with distance)',\n ),\n duration: z\n .number()\n .default(300)\n .describe('Duration of the swipe gesture in milliseconds'),\n repeat: z\n .number()\n .optional()\n .describe(\n 'The number of times to repeat the swipe gesture. 1 for default, 0 for infinite (e.g. endless swipe until the end of the page)',\n ),\n});\n\nexport type ActionSwipeParam = {\n start?: LocateResultElement;\n direction?: 'up' | 'down' | 'left' | 'right';\n distance?: number;\n end?: LocateResultElement;\n duration?: number;\n repeat?: number;\n};\n\nexport function normalizeMobileSwipeParam(\n param: ActionSwipeParam,\n screenSize: { width: number; height: number },\n): {\n startPoint: { x: number; y: number };\n endPoint: { x: number; y: number };\n duration: number;\n repeatCount: number;\n} {\n const { width, height } = screenSize;\n const { start, end } = param;\n\n const startPoint = start\n ? { x: start.center[0], y: start.center[1] }\n : { x: width / 2, y: height / 2 };\n\n let endPoint: { x: number; y: number };\n\n if (end) {\n endPoint = { x: end.center[0], y: end.center[1] };\n } else if (param.distance) {\n const direction = param.direction;\n if (!direction) {\n throw new Error('direction is required for swipe gesture');\n }\n endPoint = {\n x:\n startPoint.x +\n (direction === 'right'\n ? param.distance\n : direction === 'left'\n ? -param.distance\n : 0),\n y:\n startPoint.y +\n (direction === 'down'\n ? param.distance\n : direction === 'up'\n ? -param.distance\n : 0),\n };\n } else {\n throw new Error(\n 'Either end or distance must be specified for swipe gesture',\n );\n }\n\n endPoint.x = Math.max(0, Math.min(endPoint.x, width));\n endPoint.y = Math.max(0, Math.min(endPoint.y, height));\n\n const duration = param.duration ?? 300;\n\n let repeatCount = typeof param.repeat === 'number' ? param.repeat : 1;\n if (repeatCount === 0) {\n repeatCount = 10;\n }\n\n return { startPoint, endPoint, duration, repeatCount };\n}\n\nexport const defineActionSwipe = (config: {\n swipe: TouchInputPrimitives['swipe'];\n size(): Promise<Size>;\n}): DeviceAction<ActionSwipeParam> => {\n return defineAction<typeof ActionSwipeParamSchema, ActionSwipeParam>({\n name: 'Swipe',\n description:\n 'Perform a touch gesture for interactions beyond regular scrolling (e.g., adjust a continuous control such as a slider, flip pages in a carousel, dismiss a notification, swipe-to-delete a list item). For regular content scrolling, use Scroll instead. Use \"distance\" + \"direction\" for relative movement, or \"start\" + \"end\" for precise endpoint movement.',\n paramSchema: ActionSwipeParamSchema,\n sample: {\n start: { prompt: 'center of the notification' },\n end: { prompt: 'upper edge of the screen' },\n },\n call: async (param) => {\n const { startPoint, endPoint, duration, repeatCount } =\n normalizeMobileSwipeParam(param, await config.size());\n for (let i = 0; i < repeatCount; i++) {\n await config.swipe(startPoint, endPoint, { duration });\n }\n },\n });\n};\n\n// ClearInput\nexport const actionClearInputParamSchema = z.object({\n locate: getMidsceneLocationSchema()\n .describe('The input field to be cleared')\n .optional(),\n});\nexport type ActionClearInputParam = {\n locate?: LocateResultElement;\n};\n\nexport const defineActionClearInput = (\n clearInput: KeyboardInputPrimitives['clearInput'],\n): DeviceAction<ActionClearInputParam> => {\n return defineAction<\n typeof actionClearInputParamSchema,\n ActionClearInputParam\n >({\n name: 'ClearInput',\n description: inputLocateDescription,\n interfaceAlias: 'aiClearInput',\n paramSchema: actionClearInputParamSchema,\n sample: {\n locate: { prompt: 'the search input field' },\n },\n call: async (param) => {\n await clearInput(param.locate);\n },\n });\n};\n\n// CursorMove\nexport const actionCursorMoveParamSchema = z.object({\n direction: z\n .enum(['left', 'right'])\n .describe('The direction to move the cursor'),\n times: z\n .number()\n .int()\n .min(1)\n .default(1)\n .describe(\n 'The number of times to move the cursor in the specified direction',\n ),\n});\nexport type ActionCursorMoveParam = {\n direction: 'left' | 'right';\n times?: number;\n};\n\nexport const defineActionCursorMove = (config: {\n keyboard: Pick<KeyboardInputPrimitives, 'keyboardPress' | 'cursorMove'>;\n sleep?(timeMs: number): Promise<void>;\n}): DeviceAction<ActionCursorMoveParam> => {\n return defineAction<\n typeof actionCursorMoveParamSchema,\n ActionCursorMoveParam\n >({\n name: 'CursorMove',\n description:\n 'Move the text cursor (caret) left or right within an input field or text area. Use this to reposition the cursor without selecting text.',\n paramSchema: actionCursorMoveParamSchema,\n sample: {\n direction: 'left',\n times: 3,\n },\n call: async (param) => {\n const times = param.times ?? 1;\n if (config.keyboard.cursorMove) {\n await config.keyboard.cursorMove(param.direction, times);\n return;\n }\n\n const wait =\n config.sleep ??\n ((timeMs: number) =>\n new Promise<void>((resolve) => setTimeout(resolve, timeMs)));\n const arrowKey = param.direction === 'left' ? 'ArrowLeft' : 'ArrowRight';\n for (let i = 0; i < times; i++) {\n await config.keyboard.keyboardPress(arrowKey);\n await wait(100);\n }\n },\n });\n};\n\n// Pinch\nexport const ActionPinchParamSchema = z.object({\n locate: getMidsceneLocationSchema()\n .optional()\n .describe(\n 'The element to pinch on. If not specified, the center of the screen will be used',\n ),\n direction: z\n .enum(['in', 'out'])\n .describe(\n 'Pinch direction. \"in\" = pinch fingers together (zoom out / shrink), \"out\" = spread fingers apart (zoom in / enlarge).',\n ),\n distance: z\n .number()\n .positive()\n .optional()\n .describe(\n 'How far each finger moves in pixels. Defaults to a quarter of the shorter screen dimension.',\n ),\n duration: z\n .number()\n .default(500)\n .optional()\n .describe('Duration of the pinch gesture in milliseconds'),\n});\n\nexport type ActionPinchParam = {\n locate?: LocateResultElement;\n direction: 'in' | 'out';\n distance?: number;\n duration?: number;\n};\n\nexport const defineActionPinch = (config: {\n pinch: TouchInputPrimitives['pinch'];\n size(): Promise<Size>;\n}): DeviceAction<ActionPinchParam> | undefined => {\n if (!config.pinch) {\n return undefined;\n }\n\n return defineAction<typeof ActionPinchParamSchema, ActionPinchParam>({\n name: 'Pinch',\n description:\n 'Perform a two-finger pinch gesture. Use direction \"in\" to pinch fingers together (zoom out), or \"out\" to spread fingers apart (zoom in). Optionally specify distance for how far each finger moves.',\n interfaceAlias: 'aiPinch',\n paramSchema: ActionPinchParamSchema,\n sample: {\n locate: { prompt: 'the map area' },\n direction: 'out',\n distance: 200,\n },\n call: async (param) => {\n const { centerX, centerY, startDistance, endDistance, duration } =\n normalizePinchParam(param, await config.size());\n await config.pinch?.(\n { x: centerX, y: centerY },\n { startDistance, endDistance, duration },\n );\n },\n });\n};\n\nexport function normalizePinchParam(\n param: ActionPinchParam,\n screenSize: { width: number; height: number },\n): {\n centerX: number;\n centerY: number;\n startDistance: number;\n endDistance: number;\n duration: number;\n} {\n const { width, height } = screenSize;\n const element = param.locate;\n const centerX = element\n ? Math.round(element.center[0])\n : Math.round(width / 2);\n const centerY = element\n ? Math.round(element.center[1])\n : Math.round(height / 2);\n const duration = param.duration ?? 500;\n\n const baseDistance = Math.round(Math.min(width, height) / 4);\n const fingerDistance = param.distance ?? baseDistance;\n\n const startDistance = baseDistance;\n const endDistance =\n param.direction === 'out'\n ? baseDistance + fingerDistance\n : Math.max(10, baseDistance - fingerDistance);\n\n return { centerX, centerY, startDistance, endDistance, duration };\n}\n\nexport interface MobileInputActionContext {\n input: MobileInputPrimitives;\n size(): Promise<Size>;\n sleep?(timeMs: number): Promise<void>;\n getDefaultAutoDismissKeyboard?(): boolean | undefined;\n systemActions?: SystemInputActionOptions;\n}\n\nexport interface SystemInputActionConfig {\n name: string;\n description: string;\n interfaceAlias?: string;\n delayBeforeRunner?: number;\n delayAfterRunner?: number;\n}\n\nexport interface SystemInputActionOptions {\n backButton?: SystemInputActionConfig;\n homeButton?: SystemInputActionConfig;\n recentAppsButton?: SystemInputActionConfig;\n}\n\nexport interface InputPrimitiveActionOptions {\n size?: () => Promise<Size>;\n sleep?: (timeMs: number) => Promise<void>;\n includeSwipe?: boolean;\n includePinch?: boolean;\n systemActions?: SystemInputActionOptions;\n}\n\nfunction defineSystemInputAction(\n config: SystemInputActionConfig,\n call: () => Promise<void>,\n): DeviceAction<undefined, void> {\n return defineAction<undefined, undefined, void>({\n name: config.name,\n description: config.description,\n interfaceAlias: config.interfaceAlias,\n delayBeforeRunner: config.delayBeforeRunner,\n delayAfterRunner: config.delayAfterRunner,\n call,\n });\n}\n\nexport function defineActionsFromInputPrimitives(\n input: InputPrimitives,\n options: InputPrimitiveActionOptions = {},\n): DeviceAction<any>[] {\n const actions: Array<DeviceAction<any> | undefined> = [];\n const { pointer, keyboard, scroll, touch, system } = input;\n\n if (pointer) {\n actions.push(defineActionTap(pointer.tap));\n if (pointer.doubleClick) {\n actions.push(defineActionDoubleClick(pointer.doubleClick));\n }\n if (pointer.rightClick) {\n actions.push(defineActionRightClick(pointer.rightClick));\n }\n if (pointer.hover) {\n actions.push(defineActionHover(pointer.hover));\n }\n if (pointer.dragAndDrop) {\n actions.push(defineActionDragAndDrop(pointer.dragAndDrop));\n }\n if (pointer.longPress) {\n actions.push(defineActionLongPress(pointer.longPress));\n }\n }\n\n if (keyboard) {\n actions.push(\n defineActionInput(keyboard),\n defineActionClearInput(keyboard.clearInput),\n defineActionKeyboardPress(keyboard.keyboardPress),\n defineActionCursorMove({ keyboard, sleep: options.sleep }),\n );\n }\n\n if (scroll) {\n actions.push(defineActionScroll(scroll.scroll));\n }\n\n if (touch?.swipe && options.size && options.includeSwipe !== false) {\n actions.push(defineActionSwipe({ swipe: touch.swipe, size: options.size }));\n }\n\n if (touch?.pinch && options.size && options.includePinch !== false) {\n actions.push(defineActionPinch({ pinch: touch.pinch, size: options.size }));\n }\n\n if (system && options.systemActions) {\n const { systemActions } = options;\n if (system.backButton && systemActions.backButton) {\n actions.push(\n defineSystemInputAction(systemActions.backButton, system.backButton),\n );\n }\n if (system.homeButton && systemActions.homeButton) {\n actions.push(\n defineSystemInputAction(systemActions.homeButton, system.homeButton),\n );\n }\n if (system.recentAppsButton && systemActions.recentAppsButton) {\n actions.push(\n defineSystemInputAction(\n systemActions.recentAppsButton,\n system.recentAppsButton,\n ),\n );\n }\n }\n\n return actions.filter((action): action is DeviceAction<any> =>\n Boolean(action),\n );\n}\n\nexport function createDefaultMobileActions(\n context: MobileInputActionContext,\n): DeviceAction<any>[] {\n return defineActionsFromInputPrimitives(context.input, {\n size: context.size,\n sleep: context.sleep,\n systemActions: context.systemActions,\n });\n}\n\n// Sleep\nexport const ActionSleepParamSchema = z.object({\n timeMs: z\n .number()\n .default(1000)\n .optional()\n .describe('Sleep duration in milliseconds, defaults to 1000ms (1 second)'),\n});\n\nexport type ActionSleepParam = {\n timeMs?: number;\n};\n\nexport const defineActionSleep = (): DeviceAction<ActionSleepParam> => {\n return defineAction<typeof ActionSleepParamSchema, ActionSleepParam>({\n name: 'Sleep',\n description:\n 'Wait for a specified duration before continuing. Defaults to 1 second (1000ms) if not specified.',\n paramSchema: ActionSleepParamSchema,\n sample: {\n timeMs: 2000,\n },\n call: async (param) => {\n const duration = param?.timeMs ?? 1000;\n getDebug('device:common-action')(`Sleeping for ${duration}ms`);\n await new Promise((resolve) => setTimeout(resolve, duration));\n },\n });\n};\n\nexport type { DeviceAction } from '../types';\nexport type {\n AndroidDeviceOpt,\n AndroidDeviceInputOpt,\n IOSDeviceOpt,\n IOSDeviceInputOpt,\n HarmonyDeviceOpt,\n HarmonyDeviceInputOpt,\n} from './device-options';\n"],"names":["AbstractInterface","defineAction","config","pointFromLocate","locate","missingMessage","Error","defineLocatedPointAction","param","actionTapParamSchema","z","getMidsceneLocationSchema","defineActionTap","tap","point","actionRightClickParamSchema","defineActionRightClick","rightClick","actionDoubleClickParamSchema","defineActionDoubleClick","doubleClick","actionHoverParamSchema","defineActionHover","hover","inputLocateDescription","actionInputParamSchema","val","String","defineActionInput","keyboard","actionKeyboardPressParamSchema","defineActionKeyboardPress","keyboardPress","actionScrollParamSchema","defineActionScroll","scroll","actionDragAndDropParamSchema","defineActionDragAndDrop","dragAndDrop","from","to","ActionLongPressParamSchema","defineActionLongPress","longPress","ActionSwipeParamSchema","normalizeMobileSwipeParam","screenSize","width","height","start","end","startPoint","endPoint","direction","Math","duration","repeatCount","defineActionSwipe","i","actionClearInputParamSchema","defineActionClearInput","clearInput","actionCursorMoveParamSchema","defineActionCursorMove","times","wait","timeMs","Promise","resolve","setTimeout","arrowKey","ActionPinchParamSchema","defineActionPinch","centerX","centerY","startDistance","endDistance","normalizePinchParam","element","baseDistance","fingerDistance","defineSystemInputAction","call","defineActionsFromInputPrimitives","input","options","actions","pointer","touch","system","systemActions","action","Boolean","createDefaultMobileActions","context","ActionSleepParamSchema","defineActionSleep","getDebug"],"mappings":";;;;;;;;;;;;;AAsIO,MAAeA;;QA8CpB;QAqCA;;AACF;AAKO,MAAMC,eAAe,CAK1BC,SAgBOA;AAGT,SAASC,gBACPC,MAAuC,EACvCC,cAAsB;IAEtB,IAAI,CAACD,QACH,MAAM,IAAIE,MAAMD;IAElB,OAAO;QAAE,GAAGD,OAAO,MAAM,CAAC,EAAE;QAAE,GAAGA,OAAO,MAAM,CAAC,EAAE;IAAC;AACpD;AAEA,SAASG,yBAGPL,MAQD;IACC,OAAOD,aAA8B;QACnC,MAAMC,OAAO,IAAI;QACjB,aAAaA,OAAO,WAAW;QAC/B,gBAAgBA,OAAO,cAAc;QACrC,aAAaA,OAAO,WAAW;QAC/B,QAAQA,OAAO,MAAM;QACrB,MAAM,OAAOM;YACX,MAAMN,OAAO,IAAI,CACfC,gBAAgBK,MAAM,MAAM,EAAEN,OAAO,oBAAoB,GACzDM;QAEJ;IACF;AACF;AAGO,MAAMC,uBAAuBC,EAAE,MAAM,CAAC;IAC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ACN,QAAQ;QACV;QACA,MAAM,OAAOvF;YACX,MAAM+C,WAAW/C,OAAO,UAAU;YAClCyF,SAAS,wBAAwB,CAAC,aAAa,EAAE1C,SAAS,EAAE,CAAC;YAC7D,MAAM,IAAIY,QAAQ,CAACC,UAAYC,WAAWD,SAASb;QACrD;IACF"}
@@ -1,4 +1,5 @@
1
1
  import { compositeElementInfoImg } from "@midscene/shared/img";
2
+ import { buildMidsceneRecorderActionSummary, buildMidsceneRecorderReplayInstruction, getMidsceneRecorderSemantic } from "@midscene/shared/recorder";
2
3
  import { RECORDER_UI_DESCRIBER_SYSTEM_PROMPT } from "./ai-model/prompt/recorder-ui-describer.mjs";
3
4
  import { callAIWithObjectResponse } from "./ai-model/service-caller/index.mjs";
4
5
  const RECORDER_UI_DESCRIBER_DEFAULT_RETRIES = 2;
@@ -80,44 +81,6 @@ function getDragActionVerb(event) {
80
81
  return 'Drag';
81
82
  }
82
83
  }
83
- function getActionReplayInstruction(event, elementDescription) {
84
- if ('click' === event.type) {
85
- const verb = getPointerActionVerb(event);
86
- if ('Long press' === verb) return `${verb} the element described as "${elementDescription}".`;
87
- return `${verb} on the element described as "${elementDescription}".`;
88
- }
89
- if ('drag' === event.type) {
90
- const verb = getDragActionVerb(event);
91
- return `${verb} through the area described as "${elementDescription}".`;
92
- }
93
- }
94
- function getActionSummaryVerb(event) {
95
- if ('click' === event.type) return getPointerActionVerb(event);
96
- if ('drag' === event.type) return getDragActionVerb(event);
97
- if ('keydown' === event.type) return 'Press';
98
- }
99
- function getCanonicalReplayInstruction(event, elementDescription) {
100
- switch(event.type){
101
- case 'click':
102
- case 'drag':
103
- case 'input':
104
- case 'keydown':
105
- return getFallbackReplayInstruction(event, elementDescription);
106
- default:
107
- return;
108
- }
109
- }
110
- function getCanonicalActionSummary(event, elementDescription) {
111
- switch(event.type){
112
- case 'click':
113
- case 'drag':
114
- case 'input':
115
- case 'keydown':
116
- return getFallbackActionSummary(event, elementDescription);
117
- default:
118
- return;
119
- }
120
- }
121
84
  function pointToRect(x, y, size, pageInfo) {
122
85
  const width = pageInfo.width || size;
123
86
  const height = pageInfo.height || size;
@@ -163,44 +126,27 @@ function getFallbackDescription(event, target) {
163
126
  case 'drag':
164
127
  return pageContext ? `gesture area in ${pageContext}` : `gesture area on the ${surface}`;
165
128
  case 'input':
166
- return pageContext ? `input field in ${pageContext}` : `input field on the ${surface}`;
129
+ return `unresolved input field on the ${surface}`;
167
130
  case 'keydown':
168
131
  return pageContext ? `focused control in ${pageContext}` : `focused control on the ${surface}`;
169
132
  default:
170
133
  return pageContext ? `control in ${pageContext}` : `control on the ${surface}`;
171
134
  }
172
135
  }
136
+ function buildSemanticAction(event, scrollDestinationDescription) {
137
+ return {
138
+ type: event.type,
139
+ actionType: event.actionType,
140
+ value: event.value,
141
+ url: event.url,
142
+ scrollDestinationDescription
143
+ };
144
+ }
173
145
  function getFallbackReplayInstruction(event, elementDescription) {
174
- switch(event.type){
175
- case 'navigation':
176
- return event.url ? `Navigate to \`${event.url}\`.` : `Navigate using ${elementDescription}.`;
177
- case 'scroll':
178
- return `Scroll the page/region with description "${elementDescription}" by value "${event.value || 'down'}".`;
179
- case 'drag':
180
- return getActionReplayInstruction(event, elementDescription) || `Drag through the area described as "${elementDescription}".`;
181
- case 'input':
182
- return `Input "${event.value || ''}" into the element described as "${elementDescription}".`;
183
- case 'keydown':
184
- return `Press "${event.value || 'the recorded key'}" on the element described as "${elementDescription}".`;
185
- default:
186
- return getActionReplayInstruction(event, elementDescription) || `Click on the element described as "${elementDescription}".`;
187
- }
146
+ return buildMidsceneRecorderReplayInstruction(buildSemanticAction(event), elementDescription);
188
147
  }
189
148
  function getFallbackActionSummary(event, elementDescription) {
190
- switch(event.type){
191
- case 'navigation':
192
- return event.url ? `Navigate to ${event.url}` : 'Navigate';
193
- case 'scroll':
194
- return `Scroll ${elementDescription}`;
195
- case 'drag':
196
- return `${getActionSummaryVerb(event) || 'Drag'} ${elementDescription}`;
197
- case 'input':
198
- return `Input into ${elementDescription}`;
199
- case 'keydown':
200
- return `Press ${event.value || 'key'} on ${elementDescription}`;
201
- default:
202
- return `${getActionSummaryVerb(event) || 'Click'} ${elementDescription}`;
203
- }
149
+ return buildMidsceneRecorderActionSummary(buildSemanticAction(event), elementDescription);
204
150
  }
205
151
  function getActionGuidance(event, target) {
206
152
  const platformGuidance = getPlatformGuidance(target);
@@ -208,9 +154,9 @@ function getActionGuidance(event, target) {
208
154
  case 'click':
209
155
  return `${platformGuidance} Identify the ${getPointerActionVerb(event).toLowerCase()} target by exact visible text first, then label/placeholder, then role plus stable surrounding context, then icon purpose, then visual position. Never describe it by coordinates, marker location, or as a nearby element.`;
210
156
  case 'input':
211
- return `${platformGuidance} Identify the exact input field at the marker before text entry. Use visible label, placeholder, field name, or stable surrounding section. Preserve the recorded input value only in replayInstruction; never describe the typed value as the field.`;
157
+ return `${platformGuidance} Identify the exact input field at the marker before text entry. Use stable visible label, field role, field name, surrounding section, or sequence intent. Treat hint text that can change by user, time, data, or context as secondary evidence. Preserve the recorded input value only in replayInstruction; never describe the typed value or page title alone as the field.`;
212
158
  case 'scroll':
213
- return `${platformGuidance} Identify the scrollable page/region and concrete destination content revealed after scrolling. Use newly visible headings, section titles, list/table names, list items, or stable region labels; never say only "more content" or "current page".`;
159
+ return `${platformGuidance} Identify the scrollable page/region at the highlighted scroll point and concrete destination content revealed after scrolling. If multiple scrollable regions are visible, preserve the specific region where the scroll happened, such as left/right/top/bottom panel, navigation area, content pane, dialog body, table, list, or menu; do not generalize a panel/list scroll into the whole page. Use newly visible headings, section titles, list/table names, list items, or stable region labels; never say only "more content" or "current page".`;
214
160
  case 'drag':
215
161
  return `${platformGuidance} Identify the ${getDragActionVerb(event).toLowerCase()} start/end regions or the dragged UI control. Do not describe only the gesture path or coordinates.`;
216
162
  case 'keydown':
@@ -326,14 +272,17 @@ The target or region is highlighted in the screenshot below. Convert this event
326
272
  if ('scroll' === event.type && !hasScrollDestination('', scrollDestinationDescription)) throw new Error("AI returned a scroll description without a destination.");
327
273
  const aiReplayInstruction = content.replayInstruction?.trim();
328
274
  if (aiReplayInstruction && isWeakReplayInstruction(aiReplayInstruction)) throw new Error('AI returned a weak recorder replay instruction.');
329
- const replayInstruction = 'scroll' === event.type && scrollDestinationDescription ? `Scroll the page/region with description "${elementDescription}" by value "${event.value || 'down'}" until "${scrollDestinationDescription}" is visible.` : getCanonicalReplayInstruction(event, elementDescription) || aiReplayInstruction || getFallbackReplayInstruction(event, elementDescription);
275
+ const semanticAction = buildSemanticAction(event, scrollDestinationDescription);
276
+ const replayInstruction = buildMidsceneRecorderReplayInstruction(semanticAction, elementDescription);
330
277
  if (isWeakReplayInstruction(replayInstruction)) throw new Error('AI returned a weak recorder replay instruction.');
331
- const actionSummary = 'scroll' === event.type && scrollDestinationDescription ? `Scroll ${elementDescription} toward ${scrollDestinationDescription}` : getCanonicalActionSummary(event, elementDescription) || content.actionSummary?.trim() || getFallbackActionSummary(event, elementDescription);
278
+ const actionSummary = buildMidsceneRecorderActionSummary(semanticAction, elementDescription);
332
279
  return {
280
+ source: 'recorderAI',
281
+ status: 'ready',
333
282
  elementDescription,
334
283
  replayInstruction,
335
284
  actionSummary,
336
- semanticConfidence: content.confidence || 'medium'
285
+ confidence: content.confidence || 'medium'
337
286
  };
338
287
  } catch (error) {
339
288
  lastError = error;
@@ -358,16 +307,19 @@ async function createScreenshotWithBox(event, rect) {
358
307
  });
359
308
  }
360
309
  function createFallbackEvent(event, error, screenshotWithBox, target) {
361
- const elementDescription = event.elementDescription && !isWeakDescription(event.elementDescription) ? event.elementDescription : getFallbackDescription(event, target);
310
+ const semantic = getMidsceneRecorderSemantic(event);
311
+ const elementDescription = semantic?.elementDescription && !isWeakDescription(semantic.elementDescription) ? semantic.elementDescription : getFallbackDescription(event, target);
362
312
  return {
363
313
  ...event,
364
- elementDescription,
365
- replayInstruction: event.replayInstruction || getFallbackReplayInstruction(event, elementDescription),
366
- actionSummary: event.actionSummary || getFallbackActionSummary(event, elementDescription),
367
- semanticConfidence: 'low',
368
- descriptionLoading: false,
369
- descriptionSource: 'fallback',
370
- descriptionError: error,
314
+ semantic: {
315
+ source: 'heuristic',
316
+ status: 'ready',
317
+ elementDescription,
318
+ replayInstruction: getFallbackReplayInstruction(event, elementDescription),
319
+ actionSummary: getFallbackActionSummary(event, elementDescription),
320
+ confidence: 'low',
321
+ error
322
+ },
371
323
  screenshotWithBox: screenshotWithBox || event.screenshotWithBox
372
324
  };
373
325
  }
@@ -393,10 +345,7 @@ async function describeRecorderUIEvent(input, modelConfig, options = {}) {
393
345
  usedFallback: false,
394
346
  event: {
395
347
  ...event,
396
- ...semanticFields,
397
- descriptionLoading: false,
398
- descriptionSource: 'ai',
399
- descriptionError: void 0,
348
+ semantic: semanticFields,
400
349
  screenshotWithBox: screenshotWithBox || event.screenshotWithBox
401
350
  }
402
351
  };
@@ -1 +1 @@
1
- {"version":3,"file":"recorder-ui-describer.mjs","sources":["../../src/recorder-ui-describer.ts"],"sourcesContent":["import type { IModelConfig } from '@midscene/shared/env';\nimport { compositeElementInfoImg } from '@midscene/shared/img';\nimport type {\n MidsceneRecorderEvent,\n MidsceneRecorderPageInfo,\n MidsceneRecorderTarget,\n} from '@midscene/shared/recorder';\nimport { RECORDER_UI_DESCRIBER_SYSTEM_PROMPT } from './ai-model/prompt/recorder-ui-describer';\nimport { callAIWithObjectResponse } from './ai-model/service-caller';\nimport type { Rect } from './types';\n\nexport interface DescribeRecorderUIEventInput {\n event: MidsceneRecorderEvent;\n target?: MidsceneRecorderTarget;\n}\n\nexport interface DescribeRecorderUIEventOptions {\n maxRetries?: number;\n retryDelayMs?: number;\n concurrency?: number;\n}\n\nexport interface DescribeRecorderUIEventResult {\n event: MidsceneRecorderEvent;\n usedFallback: boolean;\n error?: string;\n}\n\ninterface RecorderUIEventAIResponse {\n elementDescription?: string;\n replayInstruction?: string;\n actionSummary?: string;\n scrollDestinationDescription?: string;\n confidence?: 'high' | 'medium' | 'low';\n error?: string;\n}\n\nconst RECORDER_UI_DESCRIBER_DEFAULT_RETRIES = 2;\nconst RECORDER_UI_DESCRIBER_DEFAULT_RETRY_DELAY_MS = 200;\nconst RECORDER_UI_DESCRIBER_DEFAULT_CONCURRENCY = 2;\n\nfunction delay(ms: number) {\n return new Promise((resolve) => setTimeout(resolve, ms));\n}\n\nfunction clamp(value: number, min: number, max: number) {\n return Math.min(Math.max(value, min), max);\n}\n\nfunction isFiniteNumber(value: unknown): value is number {\n return typeof value === 'number' && Number.isFinite(value);\n}\n\nfunction isPendingDescription(value?: string) {\n return value?.trim() === 'AI is analyzing element...';\n}\n\nfunction getRecorderEventScreenshot(event: MidsceneRecorderEvent) {\n return (\n event.screenshotWithBox || event.screenshotBefore || event.screenshotAfter\n );\n}\n\nfunction getRecorderEventAfterScreenshot(event: MidsceneRecorderEvent) {\n return event.screenshotAfter || event.screenshotWithBox;\n}\n\nfunction normalizeActionType(event: MidsceneRecorderEvent) {\n return event.actionType?.trim();\n}\n\nfunction getPlatformId(target?: MidsceneRecorderTarget) {\n return target?.platformId?.toLowerCase();\n}\n\nfunction getPlatformSurface(target?: MidsceneRecorderTarget) {\n switch (getPlatformId(target)) {\n case 'web':\n return 'current web page';\n case 'android':\n case 'ios':\n case 'harmony':\n return 'current mobile screen';\n case 'computer':\n return 'current desktop screen';\n default:\n return 'current UI';\n }\n}\n\nfunction getPlatformGuidance(target?: MidsceneRecorderTarget) {\n switch (getPlatformId(target)) {\n case 'web':\n return 'For web targets, use web UI terms such as button, input, link, menu item, tab, dialog, aria-label, placeholder, and form section when visible or inferable.';\n case 'android':\n case 'ios':\n case 'harmony':\n return 'For mobile targets, use mobile UI terms such as tab, list item, text field, icon button, navigation bar, bottom bar, sheet, card, and screen section.';\n case 'computer':\n return 'For desktop/computer targets, use desktop UI terms such as menu item, toolbar button, dialog field, sidebar item, window control, file row, and application region.';\n default:\n return 'Use platform-neutral UI terms such as control, field, item, icon button, list item, region, panel, and page section.';\n }\n}\n\nfunction getPointerActionVerb(event: MidsceneRecorderEvent) {\n switch (normalizeActionType(event)) {\n case 'Tap':\n return 'Tap';\n case 'DoubleClick':\n return 'Double click';\n case 'LongPress':\n return 'Long press';\n case 'RightClick':\n return 'Right click';\n default:\n return 'Click';\n }\n}\n\nfunction getDragActionVerb(event: MidsceneRecorderEvent) {\n switch (normalizeActionType(event)) {\n case 'Swipe':\n return 'Swipe';\n case 'DragAndDrop':\n return 'Drag';\n default:\n return 'Drag';\n }\n}\n\nfunction getActionReplayInstruction(\n event: MidsceneRecorderEvent,\n elementDescription: string,\n) {\n if (event.type === 'click') {\n const verb = getPointerActionVerb(event);\n if (verb === 'Long press') {\n return `${verb} the element described as \"${elementDescription}\".`;\n }\n return `${verb} on the element described as \"${elementDescription}\".`;\n }\n\n if (event.type === 'drag') {\n const verb = getDragActionVerb(event);\n return `${verb} through the area described as \"${elementDescription}\".`;\n }\n\n return undefined;\n}\n\nfunction getActionSummaryVerb(event: MidsceneRecorderEvent) {\n if (event.type === 'click') {\n return getPointerActionVerb(event);\n }\n if (event.type === 'drag') {\n return getDragActionVerb(event);\n }\n if (event.type === 'keydown') {\n return 'Press';\n }\n return undefined;\n}\n\nfunction getCanonicalReplayInstruction(\n event: MidsceneRecorderEvent,\n elementDescription: string,\n) {\n switch (event.type) {\n case 'click':\n case 'drag':\n case 'input':\n case 'keydown':\n return getFallbackReplayInstruction(event, elementDescription);\n default:\n return undefined;\n }\n}\n\nfunction getCanonicalActionSummary(\n event: MidsceneRecorderEvent,\n elementDescription: string,\n) {\n switch (event.type) {\n case 'click':\n case 'drag':\n case 'input':\n case 'keydown':\n return getFallbackActionSummary(event, elementDescription);\n default:\n return undefined;\n }\n}\n\nfunction pointToRect(\n x: number,\n y: number,\n size: number,\n pageInfo: MidsceneRecorderPageInfo,\n): Rect {\n const width = pageInfo.width || size;\n const height = pageInfo.height || size;\n const left = clamp(Math.floor(x - size / 2), 0, Math.max(width - 1, 0));\n const top = clamp(Math.floor(y - size / 2), 0, Math.max(height - 1, 0));\n return {\n left,\n top,\n width: Math.min(size, Math.max(width - left, 1)),\n height: Math.min(size, Math.max(height - top, 1)),\n };\n}\n\nfunction getPointRectSize(event: MidsceneRecorderEvent) {\n switch (event.type) {\n case 'scroll':\n return 96;\n case 'drag':\n return 64;\n default:\n return 36;\n }\n}\n\nexport function getRecorderUIEventTargetRect(\n event: MidsceneRecorderEvent,\n): Rect | null {\n const rect = event.elementRect;\n if (!rect) {\n return null;\n }\n\n if (\n isFiniteNumber(rect.width) &&\n rect.width > 0 &&\n isFiniteNumber(rect.height) &&\n rect.height > 0 &&\n (isFiniteNumber(rect.left) || isFiniteNumber(rect.top))\n ) {\n return {\n left: rect.left || 0,\n top: rect.top || 0,\n width: rect.width,\n height: rect.height,\n };\n }\n\n if (isFiniteNumber(rect.x) && isFiniteNumber(rect.y)) {\n return pointToRect(rect.x, rect.y, getPointRectSize(event), event.pageInfo);\n }\n\n return null;\n}\n\nfunction getFallbackDescription(\n event: MidsceneRecorderEvent,\n target?: MidsceneRecorderTarget,\n) {\n const pageContext = getPageSemanticContext(event);\n const surface = getPlatformSurface(target);\n\n switch (event.type) {\n case 'navigation':\n return event.url || event.value || event.actionType || 'navigation';\n case 'scroll':\n return pageContext\n ? `${pageContext} scrollable content`\n : `scrollable content on the ${surface}`;\n case 'drag':\n return pageContext\n ? `gesture area in ${pageContext}`\n : `gesture area on the ${surface}`;\n case 'input':\n return pageContext\n ? `input field in ${pageContext}`\n : `input field on the ${surface}`;\n case 'keydown':\n return pageContext\n ? `focused control in ${pageContext}`\n : `focused control on the ${surface}`;\n default:\n return pageContext\n ? `control in ${pageContext}`\n : `control on the ${surface}`;\n }\n}\n\nfunction getFallbackReplayInstruction(\n event: MidsceneRecorderEvent,\n elementDescription: string,\n) {\n switch (event.type) {\n case 'navigation':\n return event.url\n ? `Navigate to \\`${event.url}\\`.`\n : `Navigate using ${elementDescription}.`;\n case 'scroll':\n return `Scroll the page/region with description \"${elementDescription}\" by value \"${event.value || 'down'}\".`;\n case 'drag':\n return (\n getActionReplayInstruction(event, elementDescription) ||\n `Drag through the area described as \"${elementDescription}\".`\n );\n case 'input':\n return `Input \"${event.value || ''}\" into the element described as \"${elementDescription}\".`;\n case 'keydown':\n return `Press \"${event.value || 'the recorded key'}\" on the element described as \"${elementDescription}\".`;\n default:\n return (\n getActionReplayInstruction(event, elementDescription) ||\n `Click on the element described as \"${elementDescription}\".`\n );\n }\n}\n\nfunction getFallbackActionSummary(\n event: MidsceneRecorderEvent,\n elementDescription: string,\n) {\n switch (event.type) {\n case 'navigation':\n return event.url ? `Navigate to ${event.url}` : 'Navigate';\n case 'scroll':\n return `Scroll ${elementDescription}`;\n case 'drag':\n return `${getActionSummaryVerb(event) || 'Drag'} ${elementDescription}`;\n case 'input':\n return `Input into ${elementDescription}`;\n case 'keydown':\n return `Press ${event.value || 'key'} on ${elementDescription}`;\n default:\n return `${getActionSummaryVerb(event) || 'Click'} ${elementDescription}`;\n }\n}\n\nfunction getActionGuidance(\n event: MidsceneRecorderEvent,\n target?: MidsceneRecorderTarget,\n) {\n const platformGuidance = getPlatformGuidance(target);\n\n switch (event.type) {\n case 'click':\n return `${platformGuidance} Identify the ${getPointerActionVerb(event).toLowerCase()} target by exact visible text first, then label/placeholder, then role plus stable surrounding context, then icon purpose, then visual position. Never describe it by coordinates, marker location, or as a nearby element.`;\n case 'input':\n return `${platformGuidance} Identify the exact input field at the marker before text entry. Use visible label, placeholder, field name, or stable surrounding section. Preserve the recorded input value only in replayInstruction; never describe the typed value as the field.`;\n case 'scroll':\n return `${platformGuidance} Identify the scrollable page/region and concrete destination content revealed after scrolling. Use newly visible headings, section titles, list/table names, list items, or stable region labels; never say only \"more content\" or \"current page\".`;\n case 'drag':\n return `${platformGuidance} Identify the ${getDragActionVerb(event).toLowerCase()} start/end regions or the dragged UI control. Do not describe only the gesture path or coordinates.`;\n case 'keydown':\n return `${platformGuidance} Identify the focused element or keyboard target if visible, and preserve the recorded key in the replay instruction.`;\n default:\n return `${platformGuidance} Identify the UI target involved in this event using the most stable visible text or surrounding context.`;\n }\n}\n\nfunction getEventRawCoordinates(event: MidsceneRecorderEvent) {\n const x = event.elementRect?.x;\n const y = event.elementRect?.y;\n if (isFiniteNumber(x) && isFiniteNumber(y)) {\n return { x, y };\n }\n return undefined;\n}\n\nfunction getPageSemanticContext(event: MidsceneRecorderEvent) {\n const candidates = [event.title, event.url]\n .map((item) => item?.trim())\n .filter(Boolean) as string[];\n return candidates[0];\n}\n\nfunction isWeakDescription(value?: string) {\n if (!value) {\n return true;\n }\n if (isPendingDescription(value)) {\n return true;\n }\n const normalized = value.trim().toLowerCase();\n const compact = normalized.replace(/\\s+/g, '');\n return (\n normalized.length === 0 ||\n /^\\(?\\d+(?:\\.\\d+)?,\\s*\\d+(?:\\.\\d+)?\\)?$/.test(normalized) ||\n normalized === 'target' ||\n normalized === 'element' ||\n normalized === 'target element' ||\n normalized === 'the element' ||\n normalized === 'page element' ||\n normalized === 'input field' ||\n normalized === 'text input' ||\n normalized === 'text field' ||\n normalized === 'search box' ||\n normalized === 'more content' ||\n normalized === 'the page' ||\n normalized === 'current page' ||\n normalized === 'current screen' ||\n normalized === 'the screen' ||\n normalized === 'current ui' ||\n normalized === 'current visible ui' ||\n normalized === 'current visible page' ||\n normalized === 'current visible screen' ||\n normalized === 'main area' ||\n normalized === 'main scrollable area' ||\n normalized === 'scrollable area' ||\n normalized === 'highlighted element' ||\n normalized === 'highlighted item' ||\n normalized === 'marked element' ||\n normalized === 'marked item' ||\n normalized.includes('ai is analyzing element') ||\n compact.includes('坐标') ||\n compact.includes('附近') ||\n compact.includes('附近的元素') ||\n normalized.includes('coordinate') ||\n normalized.includes('near the coordinate') ||\n normalized.includes('near coordinates') ||\n normalized.includes('nearby element') ||\n normalized.includes('nearby item') ||\n normalized.includes('near the marker') ||\n normalized.includes('near marker') ||\n normalized.includes('near the point') ||\n normalized.includes('near point') ||\n normalized.includes('at the point') ||\n normalized.includes('button near point') ||\n normalized.includes('shown in the screenshot') ||\n normalized.includes('red rectangle') ||\n normalized.includes('red marker') ||\n normalized.includes('red box') ||\n normalized.includes('highlighted element') ||\n normalized.includes('highlighted item') ||\n normalized.includes('highlighted screenshot')\n );\n}\n\nfunction isWeakReplayInstruction(value?: string) {\n if (!value) {\n return true;\n }\n if (isPendingDescription(value)) {\n return true;\n }\n const normalized = value.trim().toLowerCase();\n const compact = normalized.replace(/\\s+/g, '');\n return (\n compact.includes('坐标') ||\n compact.includes('附近') ||\n normalized.includes('coordinate') ||\n normalized.includes('near the coordinate') ||\n normalized.includes('nearby element') ||\n normalized.includes('nearby item') ||\n normalized.includes('near the marker') ||\n normalized.includes('near marker') ||\n normalized.includes('near the point') ||\n normalized.includes('near point') ||\n normalized.includes('at the point') ||\n normalized.includes('ai is analyzing element') ||\n normalized.includes('more content') ||\n normalized.includes('current page') ||\n normalized.includes('current screen') ||\n normalized.includes('highlighted element') ||\n normalized.includes('highlighted item') ||\n normalized.includes('red marker') ||\n normalized.includes('red box') ||\n normalized.includes('shown in the screenshot') ||\n normalized.includes('highlighted screenshot')\n );\n}\n\nfunction normalizeForComparison(value: string) {\n return value.trim().toLowerCase().replace(/[\"'`]/g, '').replace(/\\s+/g, ' ');\n}\n\nfunction isInputValueUsedAsFieldDescription(\n event: MidsceneRecorderEvent,\n elementDescription?: string,\n) {\n if (event.type !== 'input' || !event.value || !elementDescription) {\n return false;\n }\n\n const typedValue = normalizeForComparison(event.value);\n if (!typedValue) {\n return false;\n }\n const description = normalizeForComparison(elementDescription);\n\n return (\n description === typedValue ||\n description === `${typedValue} input` ||\n description === `${typedValue} field` ||\n description === `${typedValue} text field` ||\n description === `input ${typedValue}` ||\n description === `field ${typedValue}` ||\n description.includes(`typed value ${typedValue}`) ||\n description.includes(`value ${typedValue}`)\n );\n}\n\nfunction hasScrollDestination(\n replayInstruction: string,\n scrollDestinationDescription?: string,\n) {\n if (\n scrollDestinationDescription &&\n !isWeakDescription(scrollDestinationDescription)\n ) {\n return true;\n }\n const normalized = replayInstruction.toLowerCase();\n return (\n normalized.includes(' until ') ||\n normalized.includes(' visible') ||\n normalized.includes(' reveal') ||\n normalized.includes(' to the ') ||\n normalized.includes(' toward ')\n );\n}\n\nasync function describeWithRetry(\n event: MidsceneRecorderEvent,\n target: MidsceneRecorderTarget | undefined,\n highlightedScreenshot: string,\n modelConfig: IModelConfig,\n options: Required<\n Pick<DescribeRecorderUIEventOptions, 'maxRetries' | 'retryDelayMs'>\n >,\n) {\n let lastError: unknown;\n for (let attempt = 1; attempt <= options.maxRetries; attempt += 1) {\n try {\n const afterScreenshot = getRecorderEventAfterScreenshot(event);\n const pageContext = getPageSemanticContext(event);\n const platformGuidance = getPlatformGuidance(target);\n const userContent: any[] = [\n {\n type: 'text',\n text: `Recorder event:\n${JSON.stringify(\n {\n type: event.type,\n actionType: event.actionType,\n value: event.value,\n rawCoordinates: getEventRawCoordinates(event),\n url: event.url,\n title: event.title,\n pageContext,\n pageInfo: event.pageInfo,\n target,\n platformGuidance,\n guidance: getActionGuidance(event, target),\n },\n null,\n 2,\n)}\n\nThe target or region is highlighted in the screenshot below. Convert this event into semantic replay fields.`,\n },\n {\n type: 'image_url',\n image_url: {\n url: highlightedScreenshot,\n detail: 'high',\n },\n },\n ];\n if (afterScreenshot) {\n userContent.push(\n {\n type: 'text',\n text: 'Screenshot after the recorded action, for context only:',\n },\n {\n type: 'image_url',\n image_url: {\n url: afterScreenshot,\n detail: 'high',\n },\n },\n );\n }\n const response =\n await callAIWithObjectResponse<RecorderUIEventAIResponse>(\n [\n {\n role: 'system',\n content: RECORDER_UI_DESCRIBER_SYSTEM_PROMPT,\n },\n {\n role: 'user',\n content: userContent,\n },\n ],\n modelConfig,\n );\n\n const content = response.content;\n if (content.error) {\n throw new Error(content.error);\n }\n if (isWeakDescription(content.elementDescription)) {\n throw new Error('AI returned a weak recorder event description.');\n }\n if (\n isInputValueUsedAsFieldDescription(event, content.elementDescription)\n ) {\n throw new Error(\n 'AI used the recorded input value as the field description.',\n );\n }\n const elementDescription = content.elementDescription!.trim();\n const scrollDestinationDescription =\n event.type === 'scroll'\n ? content.scrollDestinationDescription?.trim()\n : undefined;\n if (\n event.type === 'scroll' &&\n !hasScrollDestination('', scrollDestinationDescription)\n ) {\n throw new Error(\n 'AI returned a scroll description without a destination.',\n );\n }\n const aiReplayInstruction = content.replayInstruction?.trim();\n if (aiReplayInstruction && isWeakReplayInstruction(aiReplayInstruction)) {\n throw new Error('AI returned a weak recorder replay instruction.');\n }\n const replayInstruction =\n event.type === 'scroll' && scrollDestinationDescription\n ? `Scroll the page/region with description \"${elementDescription}\" by value \"${event.value || 'down'}\" until \"${scrollDestinationDescription}\" is visible.`\n : getCanonicalReplayInstruction(event, elementDescription) ||\n aiReplayInstruction ||\n getFallbackReplayInstruction(event, elementDescription);\n if (isWeakReplayInstruction(replayInstruction)) {\n throw new Error('AI returned a weak recorder replay instruction.');\n }\n const actionSummary =\n event.type === 'scroll' && scrollDestinationDescription\n ? `Scroll ${elementDescription} toward ${scrollDestinationDescription}`\n : getCanonicalActionSummary(event, elementDescription) ||\n content.actionSummary?.trim() ||\n getFallbackActionSummary(event, elementDescription);\n\n return {\n elementDescription,\n replayInstruction,\n actionSummary,\n semanticConfidence: content.confidence || 'medium',\n };\n } catch (error) {\n lastError = error;\n if (attempt < options.maxRetries) {\n await delay(options.retryDelayMs);\n }\n }\n }\n throw lastError;\n}\n\nasync function createScreenshotWithBox(\n event: MidsceneRecorderEvent,\n rect: Rect,\n) {\n if (event.screenshotWithBox) {\n return event.screenshotWithBox;\n }\n const screenshot = getRecorderEventScreenshot(event);\n if (!screenshot) {\n return undefined;\n }\n return compositeElementInfoImg({\n inputImgBase64: screenshot,\n size: event.pageInfo,\n elementsPositionInfo: [{ rect }],\n borderThickness: 3,\n annotationPadding: 2,\n });\n}\n\nfunction createFallbackEvent(\n event: MidsceneRecorderEvent,\n error: string,\n screenshotWithBox?: string,\n target?: MidsceneRecorderTarget,\n): MidsceneRecorderEvent {\n const elementDescription =\n event.elementDescription && !isWeakDescription(event.elementDescription)\n ? event.elementDescription\n : getFallbackDescription(event, target);\n return {\n ...event,\n elementDescription,\n replayInstruction:\n event.replayInstruction ||\n getFallbackReplayInstruction(event, elementDescription),\n actionSummary:\n event.actionSummary ||\n getFallbackActionSummary(event, elementDescription),\n semanticConfidence: 'low',\n descriptionLoading: false,\n descriptionSource: 'fallback',\n descriptionError: error,\n screenshotWithBox: screenshotWithBox || event.screenshotWithBox,\n };\n}\n\nexport async function describeRecorderUIEvent(\n input: DescribeRecorderUIEventInput,\n modelConfig: IModelConfig,\n options: DescribeRecorderUIEventOptions = {},\n): Promise<DescribeRecorderUIEventResult> {\n const event = input.event;\n const rect = getRecorderUIEventTargetRect(event);\n const screenshot = getRecorderEventScreenshot(event);\n\n if (!rect || !screenshot) {\n const error = !rect\n ? 'Recorder event has no target rectangle.'\n : 'Recorder event has no screenshot.';\n return {\n usedFallback: true,\n event: createFallbackEvent(event, error, undefined, input.target),\n };\n }\n\n let screenshotWithBox: string | undefined;\n try {\n screenshotWithBox = await createScreenshotWithBox(event, rect);\n const semanticFields = await describeWithRetry(\n event,\n input.target,\n screenshotWithBox || screenshot,\n modelConfig,\n {\n maxRetries: options.maxRetries ?? RECORDER_UI_DESCRIBER_DEFAULT_RETRIES,\n retryDelayMs:\n options.retryDelayMs ?? RECORDER_UI_DESCRIBER_DEFAULT_RETRY_DELAY_MS,\n },\n );\n return {\n usedFallback: false,\n event: {\n ...event,\n ...semanticFields,\n descriptionLoading: false,\n descriptionSource: 'ai',\n descriptionError: undefined,\n screenshotWithBox: screenshotWithBox || event.screenshotWithBox,\n },\n };\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error);\n return {\n usedFallback: true,\n error: message,\n event: createFallbackEvent(\n event,\n message,\n screenshotWithBox,\n input.target,\n ),\n };\n }\n}\n\nexport async function describeRecorderUIEvents(\n inputs: DescribeRecorderUIEventInput[],\n modelConfig: IModelConfig,\n options: DescribeRecorderUIEventOptions = {},\n): Promise<DescribeRecorderUIEventResult[]> {\n const concurrency = Math.max(\n 1,\n options.concurrency ?? RECORDER_UI_DESCRIBER_DEFAULT_CONCURRENCY,\n );\n const results: DescribeRecorderUIEventResult[] = new Array(inputs.length);\n let cursor = 0;\n\n async function worker() {\n while (cursor < inputs.length) {\n const index = cursor;\n cursor += 1;\n results[index] = await describeRecorderUIEvent(\n inputs[index],\n modelConfig,\n options,\n );\n }\n }\n\n await Promise.all(\n Array.from({ length: Math.min(concurrency, inputs.length) }, () =>\n worker(),\n ),\n );\n return results;\n}\n"],"names":["RECORDER_UI_DESCRIBER_DEFAULT_RETRIES","RECORDER_UI_DESCRIBER_DEFAULT_RETRY_DELAY_MS","RECORDER_UI_DESCRIBER_DEFAULT_CONCURRENCY","delay","ms","Promise","resolve","setTimeout","clamp","value","min","max","Math","isFiniteNumber","Number","isPendingDescription","getRecorderEventScreenshot","event","getRecorderEventAfterScreenshot","normalizeActionType","getPlatformId","target","getPlatformSurface","getPlatformGuidance","getPointerActionVerb","getDragActionVerb","getActionReplayInstruction","elementDescription","verb","getActionSummaryVerb","getCanonicalReplayInstruction","getFallbackReplayInstruction","getCanonicalActionSummary","getFallbackActionSummary","pointToRect","x","y","size","pageInfo","width","height","left","top","getPointRectSize","getRecorderUIEventTargetRect","rect","getFallbackDescription","pageContext","getPageSemanticContext","surface","getActionGuidance","platformGuidance","getEventRawCoordinates","candidates","item","Boolean","isWeakDescription","normalized","compact","isWeakReplayInstruction","normalizeForComparison","isInputValueUsedAsFieldDescription","typedValue","description","hasScrollDestination","replayInstruction","scrollDestinationDescription","describeWithRetry","highlightedScreenshot","modelConfig","options","lastError","attempt","afterScreenshot","userContent","JSON","response","callAIWithObjectResponse","RECORDER_UI_DESCRIBER_SYSTEM_PROMPT","content","Error","undefined","aiReplayInstruction","actionSummary","error","createScreenshotWithBox","screenshot","compositeElementInfoImg","createFallbackEvent","screenshotWithBox","describeRecorderUIEvent","input","semanticFields","message","String","describeRecorderUIEvents","inputs","concurrency","results","Array","cursor","worker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,QAAQ,WAAW,IAAIpE;IAEzB,MAAMgG,UAA2C,IAAIC,MAAMH,OAAO,MAAM;IACxE,IAAII,SAAS;IAEb,eAAeC;QACb,MAAOD,SAASJ,OAAO,MAAM,CAAE;YAC7B,MAAMM,QAAQF;YACdA,UAAU;YACVF,OAAO,CAACI,MAAM,GAAG,MAAMZ,wBACrBM,MAAM,CAACM,MAAM,EACbjC,aACAC;QAEJ;IACF;IAEA,MAAMjE,QAAQ,GAAG,CACf8F,MAAM,IAAI,CAAC;QAAE,QAAQvF,KAAK,GAAG,CAACqF,aAAaD,OAAO,MAAM;IAAE,GAAG,IAC3DK;IAGJ,OAAOH;AACT"}
1
+ {"version":3,"file":"recorder-ui-describer.mjs","sources":["../../src/recorder-ui-describer.ts"],"sourcesContent":["import type { IModelConfig } from '@midscene/shared/env';\nimport { compositeElementInfoImg } from '@midscene/shared/img';\nimport type {\n MidsceneRecorderEvent,\n MidsceneRecorderPageInfo,\n MidsceneRecorderSemanticAction,\n MidsceneRecorderTarget,\n} from '@midscene/shared/recorder';\nimport {\n buildMidsceneRecorderActionSummary,\n buildMidsceneRecorderReplayInstruction,\n getMidsceneRecorderSemantic,\n} from '@midscene/shared/recorder';\nimport { RECORDER_UI_DESCRIBER_SYSTEM_PROMPT } from './ai-model/prompt/recorder-ui-describer';\nimport { callAIWithObjectResponse } from './ai-model/service-caller';\nimport type { Rect } from './types';\n\nexport interface DescribeRecorderUIEventInput {\n event: MidsceneRecorderEvent;\n target?: MidsceneRecorderTarget;\n}\n\nexport interface DescribeRecorderUIEventOptions {\n maxRetries?: number;\n retryDelayMs?: number;\n concurrency?: number;\n}\n\nexport interface DescribeRecorderUIEventResult {\n event: MidsceneRecorderEvent;\n usedFallback: boolean;\n error?: string;\n}\n\ninterface RecorderUIEventAIResponse {\n elementDescription?: string;\n replayInstruction?: string;\n actionSummary?: string;\n scrollDestinationDescription?: string;\n confidence?: 'high' | 'medium' | 'low';\n error?: string;\n}\n\nconst RECORDER_UI_DESCRIBER_DEFAULT_RETRIES = 2;\nconst RECORDER_UI_DESCRIBER_DEFAULT_RETRY_DELAY_MS = 200;\nconst RECORDER_UI_DESCRIBER_DEFAULT_CONCURRENCY = 2;\n\nfunction delay(ms: number) {\n return new Promise((resolve) => setTimeout(resolve, ms));\n}\n\nfunction clamp(value: number, min: number, max: number) {\n return Math.min(Math.max(value, min), max);\n}\n\nfunction isFiniteNumber(value: unknown): value is number {\n return typeof value === 'number' && Number.isFinite(value);\n}\n\nfunction isPendingDescription(value?: string) {\n return value?.trim() === 'AI is analyzing element...';\n}\n\nfunction getRecorderEventScreenshot(event: MidsceneRecorderEvent) {\n return (\n event.screenshotWithBox || event.screenshotBefore || event.screenshotAfter\n );\n}\n\nfunction getRecorderEventAfterScreenshot(event: MidsceneRecorderEvent) {\n return event.screenshotAfter || event.screenshotWithBox;\n}\n\nfunction normalizeActionType(event: MidsceneRecorderEvent) {\n return event.actionType?.trim();\n}\n\nfunction getPlatformId(target?: MidsceneRecorderTarget) {\n return target?.platformId?.toLowerCase();\n}\n\nfunction getPlatformSurface(target?: MidsceneRecorderTarget) {\n switch (getPlatformId(target)) {\n case 'web':\n return 'current web page';\n case 'android':\n case 'ios':\n case 'harmony':\n return 'current mobile screen';\n case 'computer':\n return 'current desktop screen';\n default:\n return 'current UI';\n }\n}\n\nfunction getPlatformGuidance(target?: MidsceneRecorderTarget) {\n switch (getPlatformId(target)) {\n case 'web':\n return 'For web targets, use web UI terms such as button, input, link, menu item, tab, dialog, aria-label, placeholder, and form section when visible or inferable.';\n case 'android':\n case 'ios':\n case 'harmony':\n return 'For mobile targets, use mobile UI terms such as tab, list item, text field, icon button, navigation bar, bottom bar, sheet, card, and screen section.';\n case 'computer':\n return 'For desktop/computer targets, use desktop UI terms such as menu item, toolbar button, dialog field, sidebar item, window control, file row, and application region.';\n default:\n return 'Use platform-neutral UI terms such as control, field, item, icon button, list item, region, panel, and page section.';\n }\n}\n\nfunction getPointerActionVerb(event: MidsceneRecorderEvent) {\n switch (normalizeActionType(event)) {\n case 'Tap':\n return 'Tap';\n case 'DoubleClick':\n return 'Double click';\n case 'LongPress':\n return 'Long press';\n case 'RightClick':\n return 'Right click';\n default:\n return 'Click';\n }\n}\n\nfunction getDragActionVerb(event: MidsceneRecorderEvent) {\n switch (normalizeActionType(event)) {\n case 'Swipe':\n return 'Swipe';\n case 'DragAndDrop':\n return 'Drag';\n default:\n return 'Drag';\n }\n}\n\nfunction pointToRect(\n x: number,\n y: number,\n size: number,\n pageInfo: MidsceneRecorderPageInfo,\n): Rect {\n const width = pageInfo.width || size;\n const height = pageInfo.height || size;\n const left = clamp(Math.floor(x - size / 2), 0, Math.max(width - 1, 0));\n const top = clamp(Math.floor(y - size / 2), 0, Math.max(height - 1, 0));\n return {\n left,\n top,\n width: Math.min(size, Math.max(width - left, 1)),\n height: Math.min(size, Math.max(height - top, 1)),\n };\n}\n\nfunction getPointRectSize(event: MidsceneRecorderEvent) {\n switch (event.type) {\n case 'scroll':\n return 96;\n case 'drag':\n return 64;\n default:\n return 36;\n }\n}\n\nexport function getRecorderUIEventTargetRect(\n event: MidsceneRecorderEvent,\n): Rect | null {\n const rect = event.elementRect;\n if (!rect) {\n return null;\n }\n\n if (\n isFiniteNumber(rect.width) &&\n rect.width > 0 &&\n isFiniteNumber(rect.height) &&\n rect.height > 0 &&\n (isFiniteNumber(rect.left) || isFiniteNumber(rect.top))\n ) {\n return {\n left: rect.left || 0,\n top: rect.top || 0,\n width: rect.width,\n height: rect.height,\n };\n }\n\n if (isFiniteNumber(rect.x) && isFiniteNumber(rect.y)) {\n return pointToRect(rect.x, rect.y, getPointRectSize(event), event.pageInfo);\n }\n\n return null;\n}\n\nfunction getFallbackDescription(\n event: MidsceneRecorderEvent,\n target?: MidsceneRecorderTarget,\n) {\n const pageContext = getPageSemanticContext(event);\n const surface = getPlatformSurface(target);\n\n switch (event.type) {\n case 'navigation':\n return event.url || event.value || event.actionType || 'navigation';\n case 'scroll':\n return pageContext\n ? `${pageContext} scrollable content`\n : `scrollable content on the ${surface}`;\n case 'drag':\n return pageContext\n ? `gesture area in ${pageContext}`\n : `gesture area on the ${surface}`;\n case 'input':\n return `unresolved input field on the ${surface}`;\n case 'keydown':\n return pageContext\n ? `focused control in ${pageContext}`\n : `focused control on the ${surface}`;\n default:\n return pageContext\n ? `control in ${pageContext}`\n : `control on the ${surface}`;\n }\n}\n\nfunction buildSemanticAction(\n event: MidsceneRecorderEvent,\n scrollDestinationDescription?: string,\n): MidsceneRecorderSemanticAction {\n return {\n type: event.type,\n actionType: event.actionType,\n value: event.value,\n url: event.url,\n scrollDestinationDescription,\n };\n}\n\nfunction getFallbackReplayInstruction(\n event: MidsceneRecorderEvent,\n elementDescription: string,\n) {\n return buildMidsceneRecorderReplayInstruction(\n buildSemanticAction(event),\n elementDescription,\n );\n}\n\nfunction getFallbackActionSummary(\n event: MidsceneRecorderEvent,\n elementDescription: string,\n) {\n return buildMidsceneRecorderActionSummary(\n buildSemanticAction(event),\n elementDescription,\n );\n}\n\nfunction getActionGuidance(\n event: MidsceneRecorderEvent,\n target?: MidsceneRecorderTarget,\n) {\n const platformGuidance = getPlatformGuidance(target);\n\n switch (event.type) {\n case 'click':\n return `${platformGuidance} Identify the ${getPointerActionVerb(event).toLowerCase()} target by exact visible text first, then label/placeholder, then role plus stable surrounding context, then icon purpose, then visual position. Never describe it by coordinates, marker location, or as a nearby element.`;\n case 'input':\n return `${platformGuidance} Identify the exact input field at the marker before text entry. Use stable visible label, field role, field name, surrounding section, or sequence intent. Treat hint text that can change by user, time, data, or context as secondary evidence. Preserve the recorded input value only in replayInstruction; never describe the typed value or page title alone as the field.`;\n case 'scroll':\n return `${platformGuidance} Identify the scrollable page/region at the highlighted scroll point and concrete destination content revealed after scrolling. If multiple scrollable regions are visible, preserve the specific region where the scroll happened, such as left/right/top/bottom panel, navigation area, content pane, dialog body, table, list, or menu; do not generalize a panel/list scroll into the whole page. Use newly visible headings, section titles, list/table names, list items, or stable region labels; never say only \"more content\" or \"current page\".`;\n case 'drag':\n return `${platformGuidance} Identify the ${getDragActionVerb(event).toLowerCase()} start/end regions or the dragged UI control. Do not describe only the gesture path or coordinates.`;\n case 'keydown':\n return `${platformGuidance} Identify the focused element or keyboard target if visible, and preserve the recorded key in the replay instruction.`;\n default:\n return `${platformGuidance} Identify the UI target involved in this event using the most stable visible text or surrounding context.`;\n }\n}\n\nfunction getEventRawCoordinates(event: MidsceneRecorderEvent) {\n const x = event.elementRect?.x;\n const y = event.elementRect?.y;\n if (isFiniteNumber(x) && isFiniteNumber(y)) {\n return { x, y };\n }\n return undefined;\n}\n\nfunction getPageSemanticContext(event: MidsceneRecorderEvent) {\n const candidates = [event.title, event.url]\n .map((item) => item?.trim())\n .filter(Boolean) as string[];\n return candidates[0];\n}\n\nfunction isWeakDescription(value?: string) {\n if (!value) {\n return true;\n }\n if (isPendingDescription(value)) {\n return true;\n }\n const normalized = value.trim().toLowerCase();\n const compact = normalized.replace(/\\s+/g, '');\n return (\n normalized.length === 0 ||\n /^\\(?\\d+(?:\\.\\d+)?,\\s*\\d+(?:\\.\\d+)?\\)?$/.test(normalized) ||\n normalized === 'target' ||\n normalized === 'element' ||\n normalized === 'target element' ||\n normalized === 'the element' ||\n normalized === 'page element' ||\n normalized === 'input field' ||\n normalized === 'text input' ||\n normalized === 'text field' ||\n normalized === 'search box' ||\n normalized === 'more content' ||\n normalized === 'the page' ||\n normalized === 'current page' ||\n normalized === 'current screen' ||\n normalized === 'the screen' ||\n normalized === 'current ui' ||\n normalized === 'current visible ui' ||\n normalized === 'current visible page' ||\n normalized === 'current visible screen' ||\n normalized === 'main area' ||\n normalized === 'main scrollable area' ||\n normalized === 'scrollable area' ||\n normalized === 'highlighted element' ||\n normalized === 'highlighted item' ||\n normalized === 'marked element' ||\n normalized === 'marked item' ||\n normalized.includes('ai is analyzing element') ||\n compact.includes('坐标') ||\n compact.includes('附近') ||\n compact.includes('附近的元素') ||\n normalized.includes('coordinate') ||\n normalized.includes('near the coordinate') ||\n normalized.includes('near coordinates') ||\n normalized.includes('nearby element') ||\n normalized.includes('nearby item') ||\n normalized.includes('near the marker') ||\n normalized.includes('near marker') ||\n normalized.includes('near the point') ||\n normalized.includes('near point') ||\n normalized.includes('at the point') ||\n normalized.includes('button near point') ||\n normalized.includes('shown in the screenshot') ||\n normalized.includes('red rectangle') ||\n normalized.includes('red marker') ||\n normalized.includes('red box') ||\n normalized.includes('highlighted element') ||\n normalized.includes('highlighted item') ||\n normalized.includes('highlighted screenshot')\n );\n}\n\nfunction isWeakReplayInstruction(value?: string) {\n if (!value) {\n return true;\n }\n if (isPendingDescription(value)) {\n return true;\n }\n const normalized = value.trim().toLowerCase();\n const compact = normalized.replace(/\\s+/g, '');\n return (\n compact.includes('坐标') ||\n compact.includes('附近') ||\n normalized.includes('coordinate') ||\n normalized.includes('near the coordinate') ||\n normalized.includes('nearby element') ||\n normalized.includes('nearby item') ||\n normalized.includes('near the marker') ||\n normalized.includes('near marker') ||\n normalized.includes('near the point') ||\n normalized.includes('near point') ||\n normalized.includes('at the point') ||\n normalized.includes('ai is analyzing element') ||\n normalized.includes('more content') ||\n normalized.includes('current page') ||\n normalized.includes('current screen') ||\n normalized.includes('highlighted element') ||\n normalized.includes('highlighted item') ||\n normalized.includes('red marker') ||\n normalized.includes('red box') ||\n normalized.includes('shown in the screenshot') ||\n normalized.includes('highlighted screenshot')\n );\n}\n\nfunction normalizeForComparison(value: string) {\n return value.trim().toLowerCase().replace(/[\"'`]/g, '').replace(/\\s+/g, ' ');\n}\n\nfunction isInputValueUsedAsFieldDescription(\n event: MidsceneRecorderEvent,\n elementDescription?: string,\n) {\n if (event.type !== 'input' || !event.value || !elementDescription) {\n return false;\n }\n\n const typedValue = normalizeForComparison(event.value);\n if (!typedValue) {\n return false;\n }\n const description = normalizeForComparison(elementDescription);\n\n return (\n description === typedValue ||\n description === `${typedValue} input` ||\n description === `${typedValue} field` ||\n description === `${typedValue} text field` ||\n description === `input ${typedValue}` ||\n description === `field ${typedValue}` ||\n description.includes(`typed value ${typedValue}`) ||\n description.includes(`value ${typedValue}`)\n );\n}\n\nfunction hasScrollDestination(\n replayInstruction: string,\n scrollDestinationDescription?: string,\n) {\n if (\n scrollDestinationDescription &&\n !isWeakDescription(scrollDestinationDescription)\n ) {\n return true;\n }\n const normalized = replayInstruction.toLowerCase();\n return (\n normalized.includes(' until ') ||\n normalized.includes(' visible') ||\n normalized.includes(' reveal') ||\n normalized.includes(' to the ') ||\n normalized.includes(' toward ')\n );\n}\n\nasync function describeWithRetry(\n event: MidsceneRecorderEvent,\n target: MidsceneRecorderTarget | undefined,\n highlightedScreenshot: string,\n modelConfig: IModelConfig,\n options: Required<\n Pick<DescribeRecorderUIEventOptions, 'maxRetries' | 'retryDelayMs'>\n >,\n) {\n let lastError: unknown;\n for (let attempt = 1; attempt <= options.maxRetries; attempt += 1) {\n try {\n const afterScreenshot = getRecorderEventAfterScreenshot(event);\n const pageContext = getPageSemanticContext(event);\n const platformGuidance = getPlatformGuidance(target);\n const userContent: any[] = [\n {\n type: 'text',\n text: `Recorder event:\n${JSON.stringify(\n {\n type: event.type,\n actionType: event.actionType,\n value: event.value,\n rawCoordinates: getEventRawCoordinates(event),\n url: event.url,\n title: event.title,\n pageContext,\n pageInfo: event.pageInfo,\n target,\n platformGuidance,\n guidance: getActionGuidance(event, target),\n },\n null,\n 2,\n)}\n\nThe target or region is highlighted in the screenshot below. Convert this event into semantic replay fields.`,\n },\n {\n type: 'image_url',\n image_url: {\n url: highlightedScreenshot,\n detail: 'high',\n },\n },\n ];\n if (afterScreenshot) {\n userContent.push(\n {\n type: 'text',\n text: 'Screenshot after the recorded action, for context only:',\n },\n {\n type: 'image_url',\n image_url: {\n url: afterScreenshot,\n detail: 'high',\n },\n },\n );\n }\n const response =\n await callAIWithObjectResponse<RecorderUIEventAIResponse>(\n [\n {\n role: 'system',\n content: RECORDER_UI_DESCRIBER_SYSTEM_PROMPT,\n },\n {\n role: 'user',\n content: userContent,\n },\n ],\n modelConfig,\n );\n\n const content = response.content;\n if (content.error) {\n throw new Error(content.error);\n }\n if (isWeakDescription(content.elementDescription)) {\n throw new Error('AI returned a weak recorder event description.');\n }\n if (\n isInputValueUsedAsFieldDescription(event, content.elementDescription)\n ) {\n throw new Error(\n 'AI used the recorded input value as the field description.',\n );\n }\n const elementDescription = content.elementDescription!.trim();\n const scrollDestinationDescription =\n event.type === 'scroll'\n ? content.scrollDestinationDescription?.trim()\n : undefined;\n if (\n event.type === 'scroll' &&\n !hasScrollDestination('', scrollDestinationDescription)\n ) {\n throw new Error(\n 'AI returned a scroll description without a destination.',\n );\n }\n const aiReplayInstruction = content.replayInstruction?.trim();\n if (aiReplayInstruction && isWeakReplayInstruction(aiReplayInstruction)) {\n throw new Error('AI returned a weak recorder replay instruction.');\n }\n const semanticAction = buildSemanticAction(\n event,\n scrollDestinationDescription,\n );\n const replayInstruction = buildMidsceneRecorderReplayInstruction(\n semanticAction,\n elementDescription,\n );\n if (isWeakReplayInstruction(replayInstruction)) {\n throw new Error('AI returned a weak recorder replay instruction.');\n }\n const actionSummary = buildMidsceneRecorderActionSummary(\n semanticAction,\n elementDescription,\n );\n\n return {\n source: 'recorderAI' as const,\n status: 'ready' as const,\n elementDescription,\n replayInstruction,\n actionSummary,\n confidence: content.confidence || 'medium',\n };\n } catch (error) {\n lastError = error;\n if (attempt < options.maxRetries) {\n await delay(options.retryDelayMs);\n }\n }\n }\n throw lastError;\n}\n\nasync function createScreenshotWithBox(\n event: MidsceneRecorderEvent,\n rect: Rect,\n) {\n if (event.screenshotWithBox) {\n return event.screenshotWithBox;\n }\n const screenshot = getRecorderEventScreenshot(event);\n if (!screenshot) {\n return undefined;\n }\n return compositeElementInfoImg({\n inputImgBase64: screenshot,\n size: event.pageInfo,\n elementsPositionInfo: [{ rect }],\n borderThickness: 3,\n annotationPadding: 2,\n });\n}\n\nfunction createFallbackEvent(\n event: MidsceneRecorderEvent,\n error: string,\n screenshotWithBox?: string,\n target?: MidsceneRecorderTarget,\n): MidsceneRecorderEvent {\n const semantic = getMidsceneRecorderSemantic(event);\n const elementDescription =\n semantic?.elementDescription &&\n !isWeakDescription(semantic.elementDescription)\n ? semantic.elementDescription\n : getFallbackDescription(event, target);\n return {\n ...event,\n semantic: {\n source: 'heuristic',\n status: 'ready',\n elementDescription,\n replayInstruction: getFallbackReplayInstruction(\n event,\n elementDescription,\n ),\n actionSummary: getFallbackActionSummary(event, elementDescription),\n confidence: 'low',\n error,\n },\n screenshotWithBox: screenshotWithBox || event.screenshotWithBox,\n };\n}\n\nexport async function describeRecorderUIEvent(\n input: DescribeRecorderUIEventInput,\n modelConfig: IModelConfig,\n options: DescribeRecorderUIEventOptions = {},\n): Promise<DescribeRecorderUIEventResult> {\n const event = input.event;\n const rect = getRecorderUIEventTargetRect(event);\n const screenshot = getRecorderEventScreenshot(event);\n\n if (!rect || !screenshot) {\n const error = !rect\n ? 'Recorder event has no target rectangle.'\n : 'Recorder event has no screenshot.';\n return {\n usedFallback: true,\n event: createFallbackEvent(event, error, undefined, input.target),\n };\n }\n\n let screenshotWithBox: string | undefined;\n try {\n screenshotWithBox = await createScreenshotWithBox(event, rect);\n const semanticFields = await describeWithRetry(\n event,\n input.target,\n screenshotWithBox || screenshot,\n modelConfig,\n {\n maxRetries: options.maxRetries ?? RECORDER_UI_DESCRIBER_DEFAULT_RETRIES,\n retryDelayMs:\n options.retryDelayMs ?? RECORDER_UI_DESCRIBER_DEFAULT_RETRY_DELAY_MS,\n },\n );\n return {\n usedFallback: false,\n event: {\n ...event,\n semantic: semanticFields,\n screenshotWithBox: screenshotWithBox || event.screenshotWithBox,\n },\n };\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error);\n return {\n usedFallback: true,\n error: message,\n event: createFallbackEvent(\n event,\n message,\n screenshotWithBox,\n input.target,\n ),\n };\n }\n}\n\nexport async function describeRecorderUIEvents(\n inputs: DescribeRecorderUIEventInput[],\n modelConfig: IModelConfig,\n options: DescribeRecorderUIEventOptions = {},\n): Promise<DescribeRecorderUIEventResult[]> {\n const concurrency = Math.max(\n 1,\n options.concurrency ?? RECORDER_UI_DESCRIBER_DEFAULT_CONCURRENCY,\n );\n const results: DescribeRecorderUIEventResult[] = new Array(inputs.length);\n let cursor = 0;\n\n async function worker() {\n while (cursor < inputs.length) {\n const index = cursor;\n cursor += 1;\n results[index] = await describeRecorderUIEvent(\n inputs[index],\n modelConfig,\n options,\n );\n }\n }\n\n await Promise.all(\n Array.from({ length: Math.min(concurrency, inputs.length) }, () =>\n worker(),\n ),\n );\n return results;\n}\n"],"names":["RECORDER_UI_DESCRIBER_DEFAULT_RETRIES","RECORDER_UI_DESCRIBER_DEFAULT_RETRY_DELAY_MS","RECORDER_UI_DESCRIBER_DEFAULT_CONCURRENCY","delay","ms","Promise","resolve","setTimeout","clamp","value","min","max","Math","isFiniteNumber","Number","isPendingDescription","getRecorderEventScreenshot","event","getRecorderEventAfterScreenshot","normalizeActionType","getPlatformId","target","getPlatformSurface","getPlatformGuidance","getPointerActionVerb","getDragActionVerb","pointToRect","x","y","size","pageInfo","width","height","left","top","getPointRectSize","getRecorderUIEventTargetRect","rect","getFallbackDescription","pageContext","getPageSemanticContext","surface","buildSemanticAction","scrollDestinationDescription","getFallbackReplayInstruction","elementDescription","buildMidsceneRecorderReplayInstruction","getFallbackActionSummary","buildMidsceneRecorderActionSummary","getActionGuidance","platformGuidance","getEventRawCoordinates","candidates","item","Boolean","isWeakDescription","normalized","compact","isWeakReplayInstruction","normalizeForComparison","isInputValueUsedAsFieldDescription","typedValue","description","hasScrollDestination","replayInstruction","describeWithRetry","highlightedScreenshot","modelConfig","options","lastError","attempt","afterScreenshot","userContent","JSON","response","callAIWithObjectResponse","RECORDER_UI_DESCRIBER_SYSTEM_PROMPT","content","Error","undefined","aiReplayInstruction","semanticAction","actionSummary","error","createScreenshotWithBox","screenshot","compositeElementInfoImg","createFallbackEvent","screenshotWithBox","semantic","getMidsceneRecorderSemantic","describeRecorderUIEvent","input","semanticFields","message","String","describeRecorderUIEvents","inputs","concurrency","results","Array","cursor","worker","index"],"mappings":";;;;AA2CA,MAAMA,wCAAwC;AAC9C,MAAMC,+CAA+C;AACrD,MAAMC,4CAA4C;AAElD,SAASC,MAAMC,EAAU;IACvB,OAAO,IAAIC,QAAQ,CAACC,UAAYC,WAAWD,SAASF;AACtD;AAEA,SAASI,MAAMC,KAAa,EAAEC,GAAW,EAAEC,GAAW;IACpD,OAAOC,KAAK,GAAG,CAACA,KAAK,GAAG,CAACH,OAAOC,MAAMC;AACxC;AAEA,SAASE,eAAeJ,KAAc;IACpC,OAAO,AAAiB,YAAjB,OAAOA,SAAsBK,OAAO,QAAQ,CAACL;AACtD;AAEA,SAASM,qBAAqBN,KAAc;IAC1C,OAAOA,OAAO,WAAW;AAC3B;AAEA,SAASO,2BAA2BC,KAA4B;IAC9D,OACEA,MAAM,iBAAiB,IAAIA,MAAM,gBAAgB,IAAIA,MAAM,eAAe;AAE9E;AAEA,SAASC,gCAAgCD,KAA4B;IACnE,OAAOA,MAAM,eAAe,IAAIA,MAAM,iBAAiB;AACzD;AAEA,SAASE,oBAAoBF,KAA4B;IACvD,OAAOA,MAAM,UAAU,EAAE;AAC3B;AAEA,SAASG,cAAcC,MAA+B;IACpD,OAAOA,QAAQ,YAAY;AAC7B;AAEA,SAASC,mBAAmBD,MAA+B;IACzD,OAAQD,cAAcC;QACpB,KAAK;YACH,OAAO;QACT,KAAK;QACL,KAAK;QACL,KAAK;YACH,OAAO;QACT,KAAK;YACH,OAAO;QACT;YACE,OAAO;IACX;AACF;AAEA,SAASE,oBAAoBF,MAA+B;IAC1D,OAAQD,cAAcC;QACpB,KAAK;YACH,OAAO;QACT,KAAK;QACL,KAAK;QACL,KAAK;YACH,OAAO;QACT,KAAK;YACH,OAAO;QACT;YACE,OAAO;IACX;AACF;AAEA,SAASG,qBAAqBP,KAA4B;IACxD,OAAQE,oBAAoBF;QAC1B,KAAK;YACH,OAAO;QACT,KAAK;YACH,OAAO;QACT,KAAK;YACH,OAAO;QAC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