@midscene/core 0.27.0 → 0.27.1-beta-20250822094725.0

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 (64) hide show
  1. package/dist/es/agent/agent.mjs +518 -0
  2. package/dist/es/agent/agent.mjs.map +1 -0
  3. package/dist/es/agent/index.mjs +6 -0
  4. package/dist/es/agent/task-cache.mjs +149 -0
  5. package/dist/es/agent/task-cache.mjs.map +1 -0
  6. package/dist/es/agent/tasks.mjs +767 -0
  7. package/dist/es/agent/tasks.mjs.map +1 -0
  8. package/dist/es/agent/ui-utils.mjs +89 -0
  9. package/dist/es/agent/ui-utils.mjs.map +1 -0
  10. package/dist/es/agent/utils.mjs +337 -0
  11. package/dist/es/agent/utils.mjs.map +1 -0
  12. package/dist/es/device/index.mjs +20 -0
  13. package/dist/es/device/index.mjs.map +1 -0
  14. package/dist/es/index.mjs +2 -1
  15. package/dist/es/index.mjs.map +1 -1
  16. package/dist/es/types.mjs.map +1 -1
  17. package/dist/es/utils.mjs +2 -2
  18. package/dist/es/yaml/builder.mjs +13 -0
  19. package/dist/es/yaml/builder.mjs.map +1 -0
  20. package/dist/es/yaml/index.mjs +3 -0
  21. package/dist/es/yaml/player.mjs +375 -0
  22. package/dist/es/yaml/player.mjs.map +1 -0
  23. package/dist/es/yaml/utils.mjs +39 -0
  24. package/dist/es/yaml/utils.mjs.map +1 -0
  25. package/dist/lib/agent/agent.js +562 -0
  26. package/dist/lib/agent/agent.js.map +1 -0
  27. package/dist/lib/agent/index.js +90 -0
  28. package/dist/lib/agent/index.js.map +1 -0
  29. package/dist/lib/agent/task-cache.js +201 -0
  30. package/dist/lib/agent/task-cache.js.map +1 -0
  31. package/dist/lib/agent/tasks.js +804 -0
  32. package/dist/lib/agent/tasks.js.map +1 -0
  33. package/dist/lib/agent/ui-utils.js +141 -0
  34. package/dist/lib/agent/ui-utils.js.map +1 -0
  35. package/dist/lib/agent/utils.js +417 -0
  36. package/dist/lib/agent/utils.js.map +1 -0
  37. package/dist/lib/device/index.js +54 -0
  38. package/dist/lib/device/index.js.map +1 -0
  39. package/dist/lib/index.js +5 -1
  40. package/dist/lib/index.js.map +1 -1
  41. package/dist/lib/types.js.map +1 -1
  42. package/dist/lib/utils.js +2 -2
  43. package/dist/lib/yaml/builder.js +57 -0
  44. package/dist/lib/yaml/builder.js.map +1 -0
  45. package/dist/lib/yaml/index.js +80 -0
  46. package/dist/lib/yaml/index.js.map +1 -0
  47. package/dist/lib/yaml/player.js +409 -0
  48. package/dist/lib/yaml/player.js.map +1 -0
  49. package/dist/lib/yaml/utils.js +86 -0
  50. package/dist/lib/yaml/utils.js.map +1 -0
  51. package/dist/types/agent/agent.d.ts +123 -0
  52. package/dist/types/agent/index.d.ts +9 -0
  53. package/dist/types/agent/task-cache.d.ts +38 -0
  54. package/dist/types/agent/tasks.d.ts +56 -0
  55. package/dist/types/agent/ui-utils.d.ts +11 -0
  56. package/dist/types/agent/utils.d.ts +54 -0
  57. package/dist/types/device/index.d.ts +55 -0
  58. package/dist/types/index.d.ts +1 -0
  59. package/dist/types/types.d.ts +2 -0
  60. package/dist/types/yaml/builder.d.ts +2 -0
  61. package/dist/types/yaml/index.d.ts +3 -0
  62. package/dist/types/yaml/player.d.ts +34 -0
  63. package/dist/types/yaml/utils.d.ts +3 -0
  64. package/package.json +31 -3
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent/tasks.mjs","sources":["webpack://@midscene/core/./src/agent/tasks.ts"],"sourcesContent":["import {\n type ChatCompletionMessageParam,\n elementByPositionWithElementInfo,\n findAllMidsceneLocatorField,\n resizeImageForUiTars,\n vlmPlanning,\n} from '@/ai-model';\nimport type { AbstractPage } from '@/device';\nimport {\n type AIUsageInfo,\n type BaseElement,\n type DetailedLocateParam,\n type DumpSubscriber,\n type ExecutionRecorderItem,\n type ExecutionTaskActionApply,\n type ExecutionTaskApply,\n type ExecutionTaskHitBy,\n type ExecutionTaskInsightLocateApply,\n type ExecutionTaskInsightQueryApply,\n type ExecutionTaskPlanning,\n type ExecutionTaskPlanningApply,\n type ExecutionTaskProgressOptions,\n Executor,\n type ExecutorContext,\n type Insight,\n type InsightDump,\n type InsightExtractOption,\n type InsightExtractParam,\n type LocateResultElement,\n type MidsceneYamlFlowItem,\n type PageType,\n type PlanningAIResponse,\n type PlanningAction,\n type PlanningActionParamError,\n type PlanningActionParamSleep,\n type PlanningActionParamWaitFor,\n type PlanningLocateParam,\n type TMultimodalPrompt,\n type TUserPrompt,\n type UIContext,\n plan,\n} from '@/index';\nimport { sleep } from '@/utils';\nimport { NodeType } from '@midscene/shared/constants';\nimport {\n type IModelPreferences,\n MIDSCENE_REPLANNING_CYCLE_LIMIT,\n getAIConfigInNumber,\n} from '@midscene/shared/env';\nimport { getDebug } from '@midscene/shared/logger';\nimport { assert } from '@midscene/shared/utils';\nimport type { TaskCache } from './task-cache';\nimport { taskTitleStr } from './ui-utils';\nimport {\n matchElementFromCache,\n matchElementFromPlan,\n parsePrompt,\n} from './utils';\n\ninterface ExecutionResult<OutputType = any> {\n output: OutputType;\n thought?: string;\n executor: Executor;\n}\n\nconst debug = getDebug('device-task-executor');\nconst defaultReplanningCycleLimit = 10;\n\nexport function locatePlanForLocate(param: string | DetailedLocateParam) {\n const locate = typeof param === 'string' ? { prompt: param } : param;\n const locatePlan: PlanningAction<PlanningLocateParam> = {\n type: 'Locate',\n locate,\n param: locate,\n thought: '',\n };\n return locatePlan;\n}\n\nexport class PageTaskExecutor {\n page: AbstractPage;\n\n insight: Insight;\n\n taskCache?: TaskCache;\n\n conversationHistory: ChatCompletionMessageParam[] = [];\n\n onTaskStartCallback?: ExecutionTaskProgressOptions['onTaskStart'];\n\n constructor(\n page: AbstractPage,\n insight: Insight,\n opts: {\n taskCache?: TaskCache;\n onTaskStart?: ExecutionTaskProgressOptions['onTaskStart'];\n },\n ) {\n this.page = page;\n this.insight = insight;\n this.taskCache = opts.taskCache;\n this.onTaskStartCallback = opts?.onTaskStart;\n }\n\n private async recordScreenshot(timing: ExecutionRecorderItem['timing']) {\n const base64 = await this.page.screenshotBase64();\n const item: ExecutionRecorderItem = {\n type: 'screenshot',\n ts: Date.now(),\n screenshot: base64,\n timing,\n };\n return item;\n }\n\n private async getElementXpath(\n pageContext: UIContext<BaseElement>,\n element: LocateResultElement,\n ): Promise<string[] | undefined> {\n if (!(this.page as any).getXpathsByPoint) {\n debug('getXpathsByPoint is not supported for this page');\n return undefined;\n }\n\n let elementId = element?.id;\n if (element?.isOrderSensitive !== undefined) {\n const xpaths = await (this.page as any).getXpathsByPoint(\n {\n left: element.center[0],\n top: element.center[1],\n },\n element?.isOrderSensitive,\n );\n\n return xpaths;\n }\n\n // find the nearest xpath for the element\n if (element?.attributes?.nodeType === NodeType.POSITION) {\n await this.insight.contextRetrieverFn('locate');\n const info = elementByPositionWithElementInfo(\n pageContext.tree,\n {\n x: element.center[0],\n y: element.center[1],\n },\n {\n requireStrictDistance: false,\n filterPositionElements: true,\n },\n );\n if (info?.id) {\n elementId = info.id;\n } else {\n debug(\n 'no element id found for position node, will not update cache',\n element,\n );\n }\n }\n\n if (!elementId) {\n return undefined;\n }\n try {\n const result = await (this.page as any).getXpathsById(elementId);\n return result;\n } catch (error) {\n debug('getXpathsById error: ', error);\n }\n }\n\n private prependExecutorWithScreenshot(\n taskApply: ExecutionTaskApply,\n appendAfterExecution = false,\n ): ExecutionTaskApply {\n const taskWithScreenshot: ExecutionTaskApply = {\n ...taskApply,\n executor: async (param, context, ...args) => {\n const recorder: ExecutionRecorderItem[] = [];\n const { task } = context;\n // set the recorder before executor in case of error\n task.recorder = recorder;\n const shot = await this.recordScreenshot(`before ${task.type}`);\n recorder.push(shot);\n const result = await taskApply.executor(param, context, ...args);\n if (taskApply.type === 'Action') {\n await Promise.all([\n (async () => {\n await sleep(100);\n if (this.page.beforeAction) {\n debug('will call \"beforeAction\" for page');\n await this.page.beforeAction();\n }\n })(),\n sleep(200),\n ]);\n }\n if (appendAfterExecution) {\n const shot2 = await this.recordScreenshot('after Action');\n recorder.push(shot2);\n }\n return result;\n },\n };\n return taskWithScreenshot;\n }\n\n public async convertPlanToExecutable(plans: PlanningAction[]) {\n const tasks: ExecutionTaskApply[] = [];\n\n const taskForLocatePlan = (\n plan: PlanningAction<PlanningLocateParam>,\n detailedLocateParam: DetailedLocateParam | string,\n onResult?: (result: LocateResultElement) => void,\n ): ExecutionTaskInsightLocateApply => {\n if (typeof detailedLocateParam === 'string') {\n detailedLocateParam = {\n prompt: detailedLocateParam,\n };\n }\n const taskFind: ExecutionTaskInsightLocateApply = {\n type: 'Insight',\n subType: 'Locate',\n param: detailedLocateParam,\n thought: plan.thought,\n executor: async (param, taskContext) => {\n const { task } = taskContext;\n assert(\n param?.prompt || param?.id || param?.bbox,\n `No prompt or id or position or bbox to locate, param=${JSON.stringify(\n param,\n )}`,\n );\n let insightDump: InsightDump | undefined;\n let usage: AIUsageInfo | undefined;\n const dumpCollector: DumpSubscriber = (dump) => {\n insightDump = dump;\n usage = dump?.taskInfo?.usage;\n\n task.log = {\n dump: insightDump,\n };\n\n task.usage = usage;\n };\n this.insight.onceDumpUpdatedFn = dumpCollector;\n const shotTime = Date.now();\n\n // Get context through contextRetrieverFn which handles frozen context\n const pageContext = await this.insight.contextRetrieverFn('locate');\n task.pageContext = pageContext;\n\n const recordItem: ExecutionRecorderItem = {\n type: 'screenshot',\n ts: shotTime,\n screenshot: pageContext.screenshotBase64,\n timing: 'before Insight',\n };\n task.recorder = [recordItem];\n\n // try matching xpath\n const elementFromXpath =\n param.xpath && (this.page as any).getElementInfoByXpath\n ? await (this.page as any).getElementInfoByXpath(param.xpath)\n : undefined;\n const userExpectedPathHitFlag = !!elementFromXpath;\n\n // try matching cache\n const cachePrompt = param.prompt;\n const locateCacheRecord =\n this.taskCache?.matchLocateCache(cachePrompt);\n const xpaths = locateCacheRecord?.cacheContent?.xpaths;\n const elementFromCache = userExpectedPathHitFlag\n ? null\n : await matchElementFromCache(\n this,\n xpaths,\n cachePrompt,\n param.cacheable,\n );\n const cacheHitFlag = !!elementFromCache;\n\n // try matching plan\n const elementFromPlan =\n !userExpectedPathHitFlag && !cacheHitFlag\n ? matchElementFromPlan(param, pageContext.tree)\n : undefined;\n const planHitFlag = !!elementFromPlan;\n\n // try ai locate\n const elementFromAiLocate =\n !userExpectedPathHitFlag && !cacheHitFlag && !planHitFlag\n ? (\n await this.insight.locate(param, {\n // fallback to ai locate\n context: pageContext,\n })\n ).element\n : undefined;\n const aiLocateHitFlag = !!elementFromAiLocate;\n\n const element =\n elementFromXpath || // highest priority\n elementFromCache || // second priority\n elementFromPlan || // third priority\n elementFromAiLocate;\n\n // update cache\n let currentXpaths: string[] | undefined;\n if (\n element &&\n this.taskCache &&\n !cacheHitFlag &&\n param?.cacheable !== false\n ) {\n const elementXpaths = await this.getElementXpath(\n pageContext,\n element,\n );\n if (elementXpaths?.length) {\n currentXpaths = elementXpaths;\n this.taskCache.updateOrAppendCacheRecord(\n {\n type: 'locate',\n prompt: cachePrompt,\n xpaths: elementXpaths,\n },\n locateCacheRecord,\n );\n } else {\n debug(\n 'no xpaths found, will not update cache',\n cachePrompt,\n elementXpaths,\n );\n }\n }\n if (!element) {\n throw new Error(`Element not found: ${param.prompt}`);\n }\n\n let hitBy: ExecutionTaskHitBy | undefined;\n\n if (userExpectedPathHitFlag) {\n hitBy = {\n from: 'User expected path',\n context: {\n xpath: param.xpath,\n },\n };\n } else if (cacheHitFlag) {\n hitBy = {\n from: 'Cache',\n context: {\n xpathsFromCache: xpaths,\n xpathsToSave: currentXpaths,\n },\n };\n } else if (planHitFlag) {\n hitBy = {\n from: 'Planning',\n context: {\n id: elementFromPlan?.id,\n bbox: elementFromPlan?.bbox,\n },\n };\n } else if (aiLocateHitFlag) {\n hitBy = {\n from: 'AI model',\n context: {\n prompt: param.prompt,\n },\n };\n }\n\n onResult?.(element);\n\n return {\n output: {\n element,\n },\n pageContext,\n hitBy,\n };\n },\n };\n return taskFind;\n };\n\n for (const plan of plans) {\n if (plan.type === 'Locate') {\n if (\n !plan.locate ||\n plan.locate === null ||\n plan.locate?.id === null ||\n plan.locate?.id === 'null'\n ) {\n debug('Locate action with id is null, will be ignored', plan);\n continue;\n }\n const taskLocate = taskForLocatePlan(plan, plan.locate);\n\n tasks.push(taskLocate);\n } else if (plan.type === 'Error') {\n const taskActionError: ExecutionTaskActionApply<PlanningActionParamError> =\n {\n type: 'Action',\n subType: 'Error',\n param: plan.param,\n thought: plan.thought || plan.param?.thought,\n locate: plan.locate,\n executor: async () => {\n throw new Error(\n plan?.thought || plan.param?.thought || 'error without thought',\n );\n },\n };\n tasks.push(taskActionError);\n } else if (plan.type === 'Finished') {\n const taskActionFinished: ExecutionTaskActionApply<null> = {\n type: 'Action',\n subType: 'Finished',\n param: null,\n thought: plan.thought,\n locate: plan.locate,\n executor: async (param) => {},\n };\n tasks.push(taskActionFinished);\n } else if (plan.type === 'Sleep') {\n const taskActionSleep: ExecutionTaskActionApply<PlanningActionParamSleep> =\n {\n type: 'Action',\n subType: 'Sleep',\n param: plan.param,\n thought: plan.thought,\n locate: plan.locate,\n executor: async (taskParam) => {\n await sleep(taskParam?.timeMs || 3000);\n },\n };\n tasks.push(taskActionSleep);\n } else if (plan.type === 'Drag') {\n const taskActionDrag: ExecutionTaskActionApply<{\n start_box: { x: number; y: number };\n end_box: { x: number; y: number };\n }> = {\n type: 'Action',\n subType: 'Drag',\n param: plan.param,\n thought: plan.thought,\n locate: plan.locate,\n executor: async (taskParam) => {\n assert(\n taskParam?.start_box && taskParam?.end_box,\n 'No start_box or end_box to drag',\n );\n await this.page.mouse.drag(taskParam.start_box, taskParam.end_box);\n },\n };\n tasks.push(taskActionDrag);\n } else {\n // action in action space\n const planType = plan.type;\n const actionSpace = await this.page.actionSpace();\n const action = actionSpace.find((action) => action.name === planType);\n const param = plan.param;\n\n if (!action) {\n throw new Error(`Action type '${planType}' not found`);\n }\n\n // find all params that needs location\n const locateFields = action\n ? findAllMidsceneLocatorField(action.paramSchema)\n : [];\n\n const requiredLocateFields = action\n ? findAllMidsceneLocatorField(action.paramSchema, true)\n : [];\n\n locateFields.forEach((field) => {\n if (param[field]) {\n const locatePlan = locatePlanForLocate(param[field]);\n debug(\n 'will prepend locate param for field',\n `action.type=${planType}`,\n `param=${JSON.stringify(param[field])}`,\n `locatePlan=${JSON.stringify(locatePlan)}`,\n );\n const locateTask = taskForLocatePlan(\n locatePlan,\n param[field],\n (result) => {\n param[field] = result;\n },\n );\n tasks.push(locateTask);\n } else {\n assert(\n !requiredLocateFields.includes(field),\n `Required locate field '${field}' is not provided for action ${planType}`,\n );\n debug(`field '${field}' is not provided for action ${planType}`);\n }\n });\n\n const task: ExecutionTaskApply<\n 'Action',\n any,\n { success: boolean; action: string; param: any },\n void\n > = {\n type: 'Action',\n subType: planType,\n thought: plan.thought,\n param: plan.param,\n executor: async (param, context) => {\n debug(\n 'executing action',\n planType,\n param,\n `context.element.center: ${context.element?.center}`,\n );\n\n // Get page context for actionSpace operations to ensure size info is available\n const pageContext = await this.insight.contextRetrieverFn('locate');\n context.task.pageContext = pageContext;\n\n requiredLocateFields.forEach((field) => {\n assert(\n param[field],\n `field '${field}' is required for action ${planType} but not provided. Cannot execute action ${planType}.`,\n );\n });\n\n const actionFn = action.call.bind(this.page);\n await actionFn(param, context);\n // Return a proper result for report generation\n return {\n output: {\n success: true,\n action: planType,\n param: param,\n },\n };\n },\n };\n tasks.push(task);\n }\n }\n\n const wrappedTasks = tasks.map(\n (task: ExecutionTaskApply, index: number) => {\n if (task.type === 'Action') {\n return this.prependExecutorWithScreenshot(\n task,\n index === tasks.length - 1,\n );\n }\n return task;\n },\n );\n\n return {\n tasks: wrappedTasks,\n };\n }\n\n private async setupPlanningContext(executorContext: ExecutorContext) {\n const shotTime = Date.now();\n const pageContext = await this.insight.contextRetrieverFn('locate');\n const recordItem: ExecutionRecorderItem = {\n type: 'screenshot',\n ts: shotTime,\n screenshot: pageContext.screenshotBase64,\n timing: 'before Planning',\n };\n\n executorContext.task.recorder = [recordItem];\n (executorContext.task as ExecutionTaskPlanning).pageContext = pageContext;\n\n return {\n pageContext,\n };\n }\n\n async loadYamlFlowAsPlanning(userInstruction: string, yamlString: string) {\n const taskExecutor = new Executor(taskTitleStr('Action', userInstruction), {\n onTaskStart: this.onTaskStartCallback,\n });\n\n const task: ExecutionTaskPlanningApply = {\n type: 'Planning',\n subType: 'LoadYaml',\n locate: null,\n param: {\n userInstruction,\n },\n executor: async (param, executorContext) => {\n await this.setupPlanningContext(executorContext);\n return {\n output: {\n actions: [],\n more_actions_needed_by_instruction: false,\n log: '',\n yamlString,\n },\n cache: {\n hit: true,\n },\n hitBy: {\n from: 'Cache',\n context: {\n yamlString,\n },\n },\n };\n },\n };\n\n await taskExecutor.append(task);\n await taskExecutor.flush();\n\n return {\n executor: taskExecutor,\n };\n }\n\n private planningTaskFromPrompt(\n userInstruction: string,\n log?: string,\n actionContext?: string,\n ) {\n const task: ExecutionTaskPlanningApply = {\n type: 'Planning',\n subType: 'Plan',\n locate: null,\n param: {\n userInstruction,\n log,\n },\n executor: async (param, executorContext) => {\n const startTime = Date.now();\n const { pageContext } =\n await this.setupPlanningContext(executorContext);\n\n assert(\n this.page.actionSpace,\n 'actionSpace for device is not implemented',\n );\n const actionSpace = await this.page.actionSpace();\n debug(\n 'actionSpace for page is:',\n actionSpace.map((action) => action.name).join(', '),\n );\n assert(Array.isArray(actionSpace), 'actionSpace must be an array');\n if (actionSpace.length === 0) {\n console.warn(\n `ActionSpace for ${this.page.pageType} is empty. This may lead to unexpected behavior.`,\n );\n }\n\n const planResult = await plan(param.userInstruction, {\n context: pageContext,\n log: param.log,\n actionContext,\n pageType: this.page.pageType as PageType,\n actionSpace,\n });\n\n const {\n actions,\n log,\n more_actions_needed_by_instruction,\n error,\n usage,\n rawResponse,\n sleep,\n } = planResult;\n\n executorContext.task.log = {\n ...(executorContext.task.log || {}),\n rawResponse,\n };\n executorContext.task.usage = usage;\n\n const finalActions = actions || [];\n\n // TODO: check locate result\n // let bboxCollected = false;\n // (actions || []).reduce<PlanningAction[]>(\n // (acc, planningAction) => {\n // // TODO: magic field \"locate\" is used to indicate the action requires a locate\n // if (planningAction.locate) {\n // // we only collect bbox once, let qwen re-locate in the following steps\n // if (bboxCollected && planningAction.locate.bbox) {\n // // biome-ignore lint/performance/noDelete: <explanation>\n // delete planningAction.locate.bbox;\n // }\n\n // if (planningAction.locate.bbox) {\n // bboxCollected = true;\n // }\n\n // acc.push({\n // type: 'Locate',\n // locate: planningAction.locate,\n // param: null,\n // // thought is prompt created by ai, always a string\n // thought: planningAction.locate.prompt as string,\n // });\n // }\n // acc.push(planningAction);\n // return acc;\n // },\n // [],\n // );\n\n if (sleep) {\n const timeNow = Date.now();\n const timeRemaining = sleep - (timeNow - startTime);\n if (timeRemaining > 0) {\n finalActions.push({\n type: 'Sleep',\n param: {\n timeMs: timeRemaining,\n },\n locate: null,\n } as PlanningAction<PlanningActionParamSleep>);\n }\n }\n\n if (finalActions.length === 0) {\n assert(\n !more_actions_needed_by_instruction || sleep,\n error ? `Failed to plan: ${error}` : 'No plan found',\n );\n }\n\n return {\n output: {\n actions: finalActions,\n more_actions_needed_by_instruction,\n log,\n yamlFlow: planResult.yamlFlow,\n },\n cache: {\n hit: false,\n },\n pageContext,\n };\n },\n };\n\n return task;\n }\n\n private planningTaskToGoal(\n userInstruction: string,\n modelPreferences: IModelPreferences,\n ) {\n const task: ExecutionTaskPlanningApply = {\n type: 'Planning',\n subType: 'Plan',\n locate: null,\n param: {\n userInstruction,\n },\n executor: async (param, executorContext) => {\n const { pageContext } =\n await this.setupPlanningContext(executorContext);\n\n const imagePayload = await resizeImageForUiTars(\n pageContext.screenshotBase64,\n pageContext.size,\n modelPreferences,\n );\n\n this.appendConversationHistory({\n role: 'user',\n content: [\n {\n type: 'image_url',\n image_url: {\n url: imagePayload,\n },\n },\n ],\n });\n const planResult: {\n actions: PlanningAction<any>[];\n action_summary: string;\n usage?: AIUsageInfo;\n yamlFlow?: MidsceneYamlFlowItem[];\n rawResponse?: string;\n } = await vlmPlanning({\n userInstruction: param.userInstruction,\n conversationHistory: this.conversationHistory,\n size: pageContext.size,\n modelPreferences,\n });\n\n const { actions, action_summary, usage } = planResult;\n executorContext.task.log = {\n ...(executorContext.task.log || {}),\n rawResponse: planResult.rawResponse,\n };\n executorContext.task.usage = usage;\n this.appendConversationHistory({\n role: 'assistant',\n content: action_summary,\n });\n return {\n output: {\n actions,\n thought: actions[0]?.thought,\n actionType: actions[0].type,\n more_actions_needed_by_instruction: true,\n log: '',\n yamlFlow: planResult.yamlFlow,\n },\n cache: {\n hit: false,\n },\n };\n },\n };\n\n return task;\n }\n\n async runPlans(\n title: string,\n plans: PlanningAction[],\n ): Promise<ExecutionResult> {\n const taskExecutor = new Executor(title, {\n onTaskStart: this.onTaskStartCallback,\n });\n const { tasks } = await this.convertPlanToExecutable(plans);\n await taskExecutor.append(tasks);\n const result = await taskExecutor.flush();\n const { output } = result!;\n return {\n output,\n executor: taskExecutor,\n };\n }\n\n async action(\n userPrompt: string,\n actionContext?: string,\n ): Promise<\n ExecutionResult<\n | {\n yamlFlow?: MidsceneYamlFlowItem[]; // for cache use\n }\n | undefined\n >\n > {\n const taskExecutor = new Executor(taskTitleStr('Action', userPrompt), {\n onTaskStart: this.onTaskStartCallback,\n });\n\n let planningTask: ExecutionTaskPlanningApply | null =\n this.planningTaskFromPrompt(userPrompt, undefined, actionContext);\n let replanCount = 0;\n const logList: string[] = [];\n\n const yamlFlow: MidsceneYamlFlowItem[] = [];\n const replanningCycleLimit =\n getAIConfigInNumber(MIDSCENE_REPLANNING_CYCLE_LIMIT) ||\n defaultReplanningCycleLimit;\n while (planningTask) {\n if (replanCount > replanningCycleLimit) {\n const errorMsg =\n 'Replanning too many times, please split the task into multiple steps';\n\n return this.appendErrorPlan(taskExecutor, errorMsg);\n }\n\n // plan\n await taskExecutor.append(planningTask);\n const result = await taskExecutor.flush();\n const planResult: PlanningAIResponse = result?.output;\n if (taskExecutor.isInErrorState()) {\n return {\n output: planResult,\n executor: taskExecutor,\n };\n }\n\n const plans = planResult.actions || [];\n yamlFlow.push(...(planResult.yamlFlow || []));\n\n let executables: Awaited<ReturnType<typeof this.convertPlanToExecutable>>;\n try {\n executables = await this.convertPlanToExecutable(plans);\n taskExecutor.append(executables.tasks);\n } catch (error) {\n return this.appendErrorPlan(\n taskExecutor,\n `Error converting plans to executable tasks: ${error}, plans: ${JSON.stringify(\n plans,\n )}`,\n );\n }\n\n await taskExecutor.flush();\n if (taskExecutor.isInErrorState()) {\n return {\n output: undefined,\n executor: taskExecutor,\n };\n }\n if (planResult?.log) {\n logList.push(planResult.log);\n }\n\n if (!planResult.more_actions_needed_by_instruction) {\n planningTask = null;\n break;\n }\n planningTask = this.planningTaskFromPrompt(\n userPrompt,\n logList.length > 0 ? `- ${logList.join('\\n- ')}` : undefined,\n actionContext,\n );\n replanCount++;\n }\n\n return {\n output: {\n yamlFlow,\n },\n executor: taskExecutor,\n };\n }\n\n async actionToGoal(userPrompt: string): Promise<\n ExecutionResult<\n | {\n yamlFlow?: MidsceneYamlFlowItem[]; // for cache use\n }\n | undefined\n >\n > {\n const taskExecutor = new Executor(taskTitleStr('Action', userPrompt), {\n onTaskStart: this.onTaskStartCallback,\n });\n this.conversationHistory = [];\n const isCompleted = false;\n let currentActionCount = 0;\n const maxActionNumber = 40;\n\n const yamlFlow: MidsceneYamlFlowItem[] = [];\n while (!isCompleted && currentActionCount < maxActionNumber) {\n currentActionCount++;\n debug(\n 'actionToGoal, currentActionCount:',\n currentActionCount,\n 'userPrompt:',\n userPrompt,\n );\n const planningTask: ExecutionTaskPlanningApply = this.planningTaskToGoal(\n userPrompt,\n {\n intent: 'planning',\n },\n );\n await taskExecutor.append(planningTask);\n const result = await taskExecutor.flush();\n if (taskExecutor.isInErrorState()) {\n return {\n output: undefined,\n executor: taskExecutor,\n };\n }\n if (!result) {\n throw new Error(\n 'result of taskExecutor.flush() is undefined in function actionToGoal',\n );\n }\n const { output } = result;\n const plans = output.actions;\n yamlFlow.push(...(output.yamlFlow || []));\n let executables: Awaited<ReturnType<typeof this.convertPlanToExecutable>>;\n try {\n executables = await this.convertPlanToExecutable(plans);\n taskExecutor.append(executables.tasks);\n } catch (error) {\n return this.appendErrorPlan(\n taskExecutor,\n `Error converting plans to executable tasks: ${error}, plans: ${JSON.stringify(\n plans,\n )}`,\n );\n }\n\n await taskExecutor.flush();\n\n if (taskExecutor.isInErrorState()) {\n return {\n output: undefined,\n executor: taskExecutor,\n };\n }\n\n if (plans[0].type === 'Finished') {\n break;\n }\n }\n return {\n output: {\n yamlFlow,\n },\n executor: taskExecutor,\n };\n }\n\n private createTypeQueryTask(\n type: 'Query' | 'Boolean' | 'Number' | 'String' | 'Assert',\n demand: InsightExtractParam,\n opt?: InsightExtractOption,\n multimodalPrompt?: TMultimodalPrompt,\n ) {\n const queryTask: ExecutionTaskInsightQueryApply = {\n type: 'Insight',\n subType: type,\n locate: null,\n param: {\n // TODO: display image thumbnail in report\n dataDemand: multimodalPrompt\n ? ({\n demand,\n multimodalPrompt,\n } as never)\n : demand, // for user param presentation in report right sidebar\n },\n executor: async (param, taskContext) => {\n const { task } = taskContext;\n let insightDump: InsightDump | undefined;\n const dumpCollector: DumpSubscriber = (dump) => {\n insightDump = dump;\n };\n this.insight.onceDumpUpdatedFn = dumpCollector;\n\n // Get page context for query operations\n const shotTime = Date.now();\n const pageContext = await this.insight.contextRetrieverFn('extract');\n task.pageContext = pageContext;\n\n const recordItem: ExecutionRecorderItem = {\n type: 'screenshot',\n ts: shotTime,\n screenshot: pageContext.screenshotBase64,\n timing: 'before Extract',\n };\n task.recorder = [recordItem];\n\n const ifTypeRestricted = type !== 'Query';\n let demandInput = demand;\n if (ifTypeRestricted) {\n const returnType = type === 'Assert' ? 'Boolean' : type;\n demandInput = {\n result: `${returnType}, ${demand}`,\n };\n }\n\n const { data, usage, thought } = await this.insight.extract<any>(\n demandInput,\n opt,\n multimodalPrompt,\n );\n\n let outputResult = data;\n if (ifTypeRestricted) {\n assert(data?.result !== undefined, 'No result in query data');\n outputResult = (data as any).result;\n }\n\n return {\n output: outputResult,\n log: { dump: insightDump, isWaitForAssert: opt?.isWaitForAssert },\n usage,\n thought,\n };\n },\n };\n\n return queryTask;\n }\n async createTypeQueryExecution<T>(\n type: 'Query' | 'Boolean' | 'Number' | 'String' | 'Assert',\n demand: InsightExtractParam,\n opt?: InsightExtractOption,\n multimodalPrompt?: TMultimodalPrompt,\n ): Promise<ExecutionResult<T>> {\n const taskExecutor = new Executor(\n taskTitleStr(\n type,\n typeof demand === 'string' ? demand : JSON.stringify(demand),\n ),\n {\n onTaskStart: this.onTaskStartCallback,\n },\n );\n\n const queryTask = await this.createTypeQueryTask(\n type,\n demand,\n opt,\n multimodalPrompt,\n );\n\n await taskExecutor.append(this.prependExecutorWithScreenshot(queryTask));\n const result = await taskExecutor.flush();\n\n if (!result) {\n throw new Error(\n 'result of taskExecutor.flush() is undefined in function createTypeQueryTask',\n );\n }\n\n const { output, thought } = result;\n\n return {\n output,\n thought,\n executor: taskExecutor,\n };\n }\n\n async assert(\n assertion: TUserPrompt,\n opt?: InsightExtractOption,\n ): Promise<ExecutionResult<boolean>> {\n const { textPrompt, multimodalPrompt } = parsePrompt(assertion);\n return await this.createTypeQueryExecution<boolean>(\n 'Assert',\n textPrompt,\n opt,\n multimodalPrompt,\n );\n }\n\n /**\n * Append a message to the conversation history\n * For user messages with images:\n * - Keep max 4 user image messages in history\n * - Remove oldest user image message when limit reached\n * For assistant messages:\n * - Simply append to history\n * @param conversationHistory Message to append\n */\n private appendConversationHistory(\n conversationHistory: ChatCompletionMessageParam,\n ) {\n if (conversationHistory.role === 'user') {\n // Get all existing user messages with images\n const userImgItems = this.conversationHistory.filter(\n (item) => item.role === 'user',\n );\n\n // If we already have 4 user image messages\n if (userImgItems.length >= 4 && conversationHistory.role === 'user') {\n // Remove first user image message when we already have 4, before adding new one\n const firstUserImgIndex = this.conversationHistory.findIndex(\n (item) => item.role === 'user',\n );\n if (firstUserImgIndex >= 0) {\n this.conversationHistory.splice(firstUserImgIndex, 1);\n }\n }\n }\n // For non-user messages, simply append to history\n this.conversationHistory.push(conversationHistory);\n }\n\n private async appendErrorPlan(taskExecutor: Executor, errorMsg: string) {\n const errorPlan: PlanningAction<PlanningActionParamError> = {\n type: 'Error',\n param: {\n thought: errorMsg,\n },\n locate: null,\n };\n const { tasks } = await this.convertPlanToExecutable([errorPlan]);\n await taskExecutor.append(this.prependExecutorWithScreenshot(tasks[0]));\n await taskExecutor.flush();\n\n return {\n output: undefined,\n executor: taskExecutor,\n };\n }\n\n async waitFor(\n assertion: TUserPrompt,\n opt: PlanningActionParamWaitFor,\n ): Promise<ExecutionResult<void>> {\n const { textPrompt, multimodalPrompt } = parsePrompt(assertion);\n\n const description = `waitFor: ${textPrompt}`;\n const taskExecutor = new Executor(taskTitleStr('WaitFor', description), {\n onTaskStart: this.onTaskStartCallback,\n });\n const { timeoutMs, checkIntervalMs } = opt;\n\n assert(assertion, 'No assertion for waitFor');\n assert(timeoutMs, 'No timeoutMs for waitFor');\n assert(checkIntervalMs, 'No checkIntervalMs for waitFor');\n\n assert(\n checkIntervalMs <= timeoutMs,\n `wrong config for waitFor: checkIntervalMs must be less than timeoutMs, config: {checkIntervalMs: ${checkIntervalMs}, timeoutMs: ${timeoutMs}}`,\n );\n\n const overallStartTime = Date.now();\n let startTime = Date.now();\n let errorThought = '';\n while (Date.now() - overallStartTime < timeoutMs) {\n startTime = Date.now();\n const queryTask = await this.createTypeQueryTask(\n 'Assert',\n textPrompt,\n {\n isWaitForAssert: true,\n returnThought: true,\n doNotThrowError: true,\n },\n multimodalPrompt,\n );\n\n await taskExecutor.append(this.prependExecutorWithScreenshot(queryTask));\n const result = (await taskExecutor.flush()) as {\n output: boolean;\n thought?: string;\n };\n\n if (!result) {\n throw new Error(\n 'result of taskExecutor.flush() is undefined in function waitFor',\n );\n }\n\n if (result?.output) {\n return {\n output: undefined,\n executor: taskExecutor,\n };\n }\n\n errorThought =\n result?.thought ||\n `unknown error when waiting for assertion: ${textPrompt}`;\n const now = Date.now();\n if (now - startTime < checkIntervalMs) {\n const timeRemaining = checkIntervalMs - (now - startTime);\n const sleepPlan: PlanningAction<PlanningActionParamSleep> = {\n type: 'Sleep',\n param: {\n timeMs: timeRemaining,\n },\n locate: null,\n };\n const { tasks: sleepTasks } = await this.convertPlanToExecutable([\n sleepPlan,\n ]);\n await taskExecutor.append(\n this.prependExecutorWithScreenshot(sleepTasks[0]),\n );\n await taskExecutor.flush();\n }\n }\n\n return this.appendErrorPlan(\n taskExecutor,\n `waitFor timeout: ${errorThought}`,\n );\n }\n}\n"],"names":["debug","getDebug","defaultReplanningCycleLimit","locatePlanForLocate","param","locate","locatePlan","PageTaskExecutor","timing","base64","item","Date","pageContext","element","_element_attributes","elementId","undefined","xpaths","NodeType","info","elementByPositionWithElementInfo","result","error","taskApply","appendAfterExecution","taskWithScreenshot","context","args","recorder","task","shot","Promise","sleep","shot2","plans","tasks","taskForLocatePlan","plan","detailedLocateParam","onResult","taskFind","taskContext","_this_taskCache","_locateCacheRecord_cacheContent","assert","JSON","insightDump","usage","dumpCollector","dump","_dump_taskInfo","shotTime","recordItem","elementFromXpath","userExpectedPathHitFlag","cachePrompt","locateCacheRecord","elementFromCache","matchElementFromCache","cacheHitFlag","elementFromPlan","matchElementFromPlan","planHitFlag","elementFromAiLocate","aiLocateHitFlag","currentXpaths","elementXpaths","Error","hitBy","_plan_locate","_plan_locate1","taskLocate","_plan_param","taskActionError","taskActionFinished","taskActionSleep","taskParam","taskActionDrag","planType","actionSpace","action","locateFields","findAllMidsceneLocatorField","requiredLocateFields","field","locateTask","_context_element","actionFn","wrappedTasks","index","executorContext","userInstruction","yamlString","taskExecutor","Executor","taskTitleStr","log","actionContext","startTime","Array","console","planResult","actions","more_actions_needed_by_instruction","rawResponse","finalActions","timeNow","timeRemaining","modelPreferences","_actions_","imagePayload","resizeImageForUiTars","vlmPlanning","action_summary","title","output","userPrompt","planningTask","replanCount","logList","yamlFlow","replanningCycleLimit","getAIConfigInNumber","MIDSCENE_REPLANNING_CYCLE_LIMIT","errorMsg","executables","isCompleted","currentActionCount","maxActionNumber","type","demand","opt","multimodalPrompt","queryTask","ifTypeRestricted","demandInput","returnType","data","thought","outputResult","assertion","textPrompt","parsePrompt","conversationHistory","userImgItems","firstUserImgIndex","errorPlan","description","timeoutMs","checkIntervalMs","overallStartTime","errorThought","now","sleepPlan","sleepTasks","page","insight","opts"],"mappings":";;;;;;;;;;;;;;;;;;;AAiEA,MAAMA,QAAQC,SAAS;AACvB,MAAMC,8BAA8B;AAE7B,SAASC,oBAAoBC,KAAmC;IACrE,MAAMC,SAAS,AAAiB,YAAjB,OAAOD,QAAqB;QAAE,QAAQA;IAAM,IAAIA;IAC/D,MAAME,aAAkD;QACtD,MAAM;QACND;QACA,OAAOA;QACP,SAAS;IACX;IACA,OAAOC;AACT;AAEO,MAAMC;IAyBX,MAAc,iBAAiBC,MAAuC,EAAE;QACtE,MAAMC,SAAS,MAAM,IAAI,CAAC,IAAI,CAAC,gBAAgB;QAC/C,MAAMC,OAA8B;YAClC,MAAM;YACN,IAAIC,KAAK,GAAG;YACZ,YAAYF;YACZD;QACF;QACA,OAAOE;IACT;IAEA,MAAc,gBACZE,WAAmC,EACnCC,OAA4B,EACG;YAoB3BC;QAnBJ,IAAI,CAAE,IAAI,CAAC,IAAI,CAAS,gBAAgB,EAAE,YACxCd,MAAM;QAIR,IAAIe,YAAYF,QAAAA,UAAAA,KAAAA,IAAAA,QAAS,EAAE;QAC3B,IAAIA,AAAAA,CAAAA,QAAAA,UAAAA,KAAAA,IAAAA,QAAS,gBAAgB,AAAD,MAAMG,QAAW;YAC3C,MAAMC,SAAS,MAAO,IAAI,CAAC,IAAI,CAAS,gBAAgB,CACtD;gBACE,MAAMJ,QAAQ,MAAM,CAAC,EAAE;gBACvB,KAAKA,QAAQ,MAAM,CAAC,EAAE;YACxB,GACAA,QAAAA,UAAAA,KAAAA,IAAAA,QAAS,g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wH,YAAY;YAC3D,MAAMW,MAAM7I,KAAK,GAAG;YACpB,IAAI6I,MAAMtD,YAAYmD,iBAAiB;gBACrC,MAAM1C,gBAAgB0C,kBAAmBG,CAAAA,MAAMtD,SAAQ;gBACvD,MAAMuD,YAAsD;oBAC1D,MAAM;oBACN,OAAO;wBACL,QAAQ9C;oBACV;oBACA,QAAQ;gBACV;gBACA,MAAM,EAAE,OAAO+C,UAAU,EAAE,GAAG,MAAM,IAAI,CAAC,uBAAuB,CAAC;oBAC/DD;iBACD;gBACD,MAAM5D,aAAa,MAAM,CACvB,IAAI,CAAC,6BAA6B,CAAC6D,UAAU,CAAC,EAAE;gBAElD,MAAM7D,aAAa,KAAK;YAC1B;QACF;QAEA,OAAO,IAAI,CAAC,eAAe,CACzBA,cACA,CAAC,iBAAiB,EAAE0D,cAAc;IAEtC;IAtqCA,YACEI,IAAkB,EAClBC,OAAgB,EAChBC,IAGC,CACD;QAjBF;QAEA;QAEA;QAEA,8CAAoD,EAAE;QAEtD;QAUE,IAAI,CAAC,IAAI,GAAGF;QACZ,IAAI,CAAC,OAAO,GAAGC;QACf,IAAI,CAAC,SAAS,GAAGC,KAAK,SAAS;QAC/B,IAAI,CAAC,mBAAmB,GAAGA,QAAAA,OAAAA,KAAAA,IAAAA,KAAM,WAAW;IAC9C;AA2pCF"}
@@ -0,0 +1,89 @@
1
+ function typeStr(task) {
2
+ return task.subType && 'Plan' !== task.subType ? `${task.type} / ${task.subType || ''}` : task.type;
3
+ }
4
+ function getKeyCommands(value) {
5
+ const keys = Array.isArray(value) ? value : [
6
+ value
7
+ ];
8
+ return keys.reduce((acc, k)=>{
9
+ const includeMeta = keys.includes('Meta') || keys.includes('Control');
10
+ if (includeMeta && ('a' === k || 'A' === k)) return acc.concat([
11
+ {
12
+ key: k,
13
+ command: 'SelectAll'
14
+ }
15
+ ]);
16
+ if (includeMeta && ('c' === k || 'C' === k)) return acc.concat([
17
+ {
18
+ key: k,
19
+ command: 'Copy'
20
+ }
21
+ ]);
22
+ if (includeMeta && ('v' === k || 'V' === k)) return acc.concat([
23
+ {
24
+ key: k,
25
+ command: 'Paste'
26
+ }
27
+ ]);
28
+ return acc.concat([
29
+ {
30
+ key: k
31
+ }
32
+ ]);
33
+ }, []);
34
+ }
35
+ function locateParamStr(locate) {
36
+ if (!locate) return '';
37
+ if ('string' == typeof locate) return locate;
38
+ if ('string' == typeof locate.prompt) return locate.prompt;
39
+ if ('object' == typeof locate.prompt && locate.prompt.prompt) return locate.prompt.prompt;
40
+ return '';
41
+ }
42
+ function scrollParamStr(scrollParam) {
43
+ if (!scrollParam) return '';
44
+ return `${scrollParam.direction || 'down'}, ${scrollParam.scrollType || 'once'}, ${scrollParam.distance || 'distance-not-set'}`;
45
+ }
46
+ function pullParamStr(pullParam) {
47
+ if (!pullParam) return '';
48
+ const parts = [];
49
+ parts.push(`direction: ${pullParam.direction || 'down'}`);
50
+ if (pullParam.distance) parts.push(`distance: ${pullParam.distance}`);
51
+ if (pullParam.duration) parts.push(`duration: ${pullParam.duration}ms`);
52
+ return parts.join(', ');
53
+ }
54
+ function taskTitleStr(type, prompt) {
55
+ if (prompt) return `${type} - ${prompt}`;
56
+ return type;
57
+ }
58
+ function paramStr(task) {
59
+ let value;
60
+ if ('Planning' === task.type) {
61
+ var _task_param;
62
+ value = null == task ? void 0 : null == (_task_param = task.param) ? void 0 : _task_param.userInstruction;
63
+ }
64
+ if ('Insight' === task.type) {
65
+ var _task_param1, _task_param2, _task_param3, _task_param4;
66
+ value = (null == task ? void 0 : null == (_task_param1 = task.param) ? void 0 : _task_param1.prompt) || (null == task ? void 0 : null == (_task_param2 = task.param) ? void 0 : _task_param2.id) || (null == task ? void 0 : null == (_task_param3 = task.param) ? void 0 : _task_param3.dataDemand) || (null == task ? void 0 : null == (_task_param4 = task.param) ? void 0 : _task_param4.assertion);
67
+ }
68
+ if ('Action' === task.type) {
69
+ var _task_param5, _task_param6, _task_param7, _task_param8;
70
+ const locate = null == task ? void 0 : task.locate;
71
+ const locateStr = locate ? locateParamStr(locate) : '';
72
+ value = task.thought || '';
73
+ if ('number' == typeof (null == task ? void 0 : null == (_task_param5 = task.param) ? void 0 : _task_param5.timeMs)) {
74
+ var _task_param9;
75
+ value = `${null == task ? void 0 : null == (_task_param9 = task.param) ? void 0 : _task_param9.timeMs}ms`;
76
+ } else if ('string' == typeof (null == task ? void 0 : null == (_task_param6 = task.param) ? void 0 : _task_param6.scrollType)) value = scrollParamStr(null == task ? void 0 : task.param);
77
+ else if ('string' == typeof (null == task ? void 0 : null == (_task_param7 = task.param) ? void 0 : _task_param7.direction) && (null == task ? void 0 : task.subType) === 'AndroidPull') value = pullParamStr(null == task ? void 0 : task.param);
78
+ else if (void 0 !== (null == task ? void 0 : null == (_task_param8 = task.param) ? void 0 : _task_param8.value)) {
79
+ var _task_param10;
80
+ value = null == task ? void 0 : null == (_task_param10 = task.param) ? void 0 : _task_param10.value;
81
+ }
82
+ if (locateStr) value = value ? `${locateStr} - ${value}` : locateStr;
83
+ }
84
+ if (void 0 === value) return '';
85
+ return 'string' == typeof value ? value : JSON.stringify(value, void 0, 2);
86
+ }
87
+ export { getKeyCommands, locateParamStr, paramStr, pullParamStr, scrollParamStr, taskTitleStr, typeStr };
88
+
89
+ //# sourceMappingURL=ui-utils.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent/ui-utils.mjs","sources":["webpack://@midscene/core/./src/agent/ui-utils.ts"],"sourcesContent":["import type {\n AndroidPullParam,\n DetailedLocateParam,\n ExecutionTask,\n ExecutionTaskAction,\n ExecutionTaskInsightAssertion,\n ExecutionTaskInsightLocate,\n ExecutionTaskInsightQuery,\n ExecutionTaskPlanning,\n ScrollParam,\n} from '@/index';\n\nexport function typeStr(task: ExecutionTask) {\n return task.subType && task.subType !== 'Plan'\n ? `${task.type} / ${task.subType || ''}`\n : task.type;\n}\n\nexport function getKeyCommands(\n value: string | string[],\n): Array<{ key: string; command?: string }> {\n // Ensure value is an array of keys\n const keys = Array.isArray(value) ? value : [value];\n\n // Process each key to attach a corresponding command if needed, based on the presence of 'Meta' or 'Control' in the keys array.\n // ref: https://github.com/puppeteer/puppeteer/pull/9357/files#diff-32cf475237b000f980eb214a0a823e45a902bddb7d2426d677cae96397aa0ae4R94\n return keys.reduce((acc: Array<{ key: string; command?: string }>, k) => {\n const includeMeta = keys.includes('Meta') || keys.includes('Control');\n if (includeMeta && (k === 'a' || k === 'A')) {\n return acc.concat([{ key: k, command: 'SelectAll' }]);\n }\n if (includeMeta && (k === 'c' || k === 'C')) {\n return acc.concat([{ key: k, command: 'Copy' }]);\n }\n if (includeMeta && (k === 'v' || k === 'V')) {\n return acc.concat([{ key: k, command: 'Paste' }]);\n }\n return acc.concat([{ key: k }]);\n }, []);\n}\n\nexport function locateParamStr(locate?: DetailedLocateParam) {\n if (!locate) {\n return '';\n }\n\n if (typeof locate === 'string') {\n return locate;\n }\n\n if (typeof locate.prompt === 'string') {\n return locate.prompt;\n }\n\n if (typeof locate.prompt === 'object' && locate.prompt.prompt) {\n return locate.prompt.prompt;\n }\n\n return '';\n}\n\nexport function scrollParamStr(scrollParam?: ScrollParam) {\n if (!scrollParam) {\n return '';\n }\n return `${scrollParam.direction || 'down'}, ${scrollParam.scrollType || 'once'}, ${scrollParam.distance || 'distance-not-set'}`;\n}\n\nexport function pullParamStr(pullParam?: AndroidPullParam) {\n if (!pullParam) {\n return '';\n }\n const parts: string[] = [];\n parts.push(`direction: ${pullParam.direction || 'down'}`);\n if (pullParam.distance) {\n parts.push(`distance: ${pullParam.distance}`);\n }\n if (pullParam.duration) {\n parts.push(`duration: ${pullParam.duration}ms`);\n }\n return parts.join(', ');\n}\n\nexport function taskTitleStr(\n type:\n | 'Tap'\n | 'Hover'\n | 'Input'\n | 'RightClick'\n | 'KeyboardPress'\n | 'Scroll'\n | 'Action'\n | 'Query'\n | 'Assert'\n | 'WaitFor'\n | 'Locate'\n | 'Boolean'\n | 'Number'\n | 'String',\n prompt: string,\n) {\n if (prompt) {\n return `${type} - ${prompt}`;\n }\n return type;\n}\n\nexport function paramStr(task: ExecutionTask) {\n let value: string | undefined | object;\n if (task.type === 'Planning') {\n value = (task as ExecutionTaskPlanning)?.param?.userInstruction;\n }\n\n if (task.type === 'Insight') {\n value =\n (task as ExecutionTaskInsightLocate)?.param?.prompt ||\n (task as ExecutionTaskInsightLocate)?.param?.id ||\n (task as ExecutionTaskInsightQuery)?.param?.dataDemand ||\n (task as ExecutionTaskInsightAssertion)?.param?.assertion;\n }\n\n if (task.type === 'Action') {\n const locate = (task as ExecutionTaskAction)?.locate;\n const locateStr = locate ? locateParamStr(locate) : '';\n\n value = task.thought || '';\n if (typeof (task as ExecutionTaskAction)?.param?.timeMs === 'number') {\n value = `${(task as ExecutionTaskAction)?.param?.timeMs}ms`;\n } else if (\n typeof (task as ExecutionTaskAction)?.param?.scrollType === 'string'\n ) {\n value = scrollParamStr((task as ExecutionTaskAction)?.param);\n } else if (\n typeof (task as ExecutionTaskAction)?.param?.direction === 'string' &&\n (task as ExecutionTaskAction)?.subType === 'AndroidPull'\n ) {\n value = pullParamStr((task as ExecutionTaskAction)?.param);\n } else if (\n typeof (task as ExecutionTaskAction)?.param?.value !== 'undefined'\n ) {\n value = (task as ExecutionTaskAction)?.param?.value;\n }\n\n if (locateStr) {\n if (value) {\n value = `${locateStr} - ${value}`;\n } else {\n value = locateStr;\n }\n }\n }\n\n if (typeof value === 'undefined') return '';\n return typeof value === 'string'\n ? value\n : JSON.stringify(value, undefined, 2);\n}\n"],"names":["typeStr","task","getKeyCommands","value","keys","Array","acc","k","includeMeta","locateParamStr","locate","scrollParamStr","scrollParam","pullParamStr","pullParam","parts","taskTitleStr","type","prompt","paramStr","_task_param","_task_param1","_task_param2","_task_param3","_task_param4","_task_param5","_task_param6","_task_param7","_task_param8","locateStr","_task_param9","_task_param10","JSON","undefined"],"mappings":"AAYO,SAASA,QAAQC,IAAmB;IACzC,OAAOA,KAAK,OAAO,IAAIA,AAAiB,WAAjBA,KAAK,OAAO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AAK;aACpD,IACL,AAAuD,WAA/CA,CAAAA,QAAAA,OAAAA,KAAAA,IAAAA,QAAD2B,CAAAA,eAAC3B,KAA8B,KAAK,AAAD,IAAnC2B,KAAAA,IAAAA,aAAsC,KAAK,AAAD,GACjD;gBACQG;YAAR5B,QAASF,QAAAA,OAAAA,KAAAA,IAAAA,QAAD8B,CAAAA,gBAAC9B,KAA8B,KAAK,AAAD,IAAnC8B,KAAAA,IAAAA,cAAsC,KAAK;QACrD;QAEA,IAAIF,WAEA1B,QADEA,QACM,GAAG0B,UAAU,GAAG,EAAE1B,OAAO,GAEzB0B;IAGd;IAEA,IAAI,AAAiB,WAAV1B,OAAuB,OAAO;IACzC,OAAO,AAAiB,YAAjB,OAAOA,QACVA,QACA6B,KAAK,SAAS,CAAC7B,OAAO8B,QAAW;AACvC"}
@@ -0,0 +1,337 @@
1
+ import { elementByPositionWithElementInfo } from "../ai-model/index.mjs";
2
+ import { getMidsceneLocationSchema, z } from "../index.mjs";
3
+ import { sleep, uploadTestInfoToServer } from "../utils.mjs";
4
+ import { MIDSCENE_REPORT_TAG_NAME, getAIConfig } from "@midscene/shared/env";
5
+ import { generateElementByPosition, getNodeFromCacheList } from "@midscene/shared/extractor";
6
+ import { resizeImgBase64 } from "@midscene/shared/img";
7
+ import { getDebug } from "@midscene/shared/logger";
8
+ import { assert, logMsg, uuid } from "@midscene/shared/utils";
9
+ import dayjs from "dayjs";
10
+ import { debug } from "./task-cache.mjs";
11
+ import { getKeyCommands } from "./ui-utils.mjs";
12
+ const debugProfile = getDebug('web:tool:profile');
13
+ const debugUtils = getDebug('web:tool:utils');
14
+ async function commonContextParser(page) {
15
+ assert(page, 'page is required');
16
+ debugProfile('Getting page URL');
17
+ const url = await page.url();
18
+ debugProfile('URL end');
19
+ debugProfile('Uploading test info to server');
20
+ uploadTestInfoToServer({
21
+ testUrl: url
22
+ });
23
+ debugProfile('UploadTestInfoToServer end');
24
+ let screenshotBase64 = await page.screenshotBase64();
25
+ assert(screenshotBase64, 'screenshotBase64 is required');
26
+ const size = await page.size();
27
+ debugProfile(`size: ${size.width}x${size.height} dpr: ${size.dpr}`);
28
+ if (size.dpr && size.dpr > 1) {
29
+ debugProfile('Resizing screenshot for high DPR display');
30
+ screenshotBase64 = await resizeImgBase64(screenshotBase64, {
31
+ width: size.width,
32
+ height: size.height
33
+ });
34
+ debugProfile('ResizeImgBase64 end');
35
+ }
36
+ return {
37
+ tree: {
38
+ node: null,
39
+ children: []
40
+ },
41
+ size,
42
+ screenshotBase64: screenshotBase64,
43
+ url
44
+ };
45
+ }
46
+ function getReportFileName(tag = 'web') {
47
+ const reportTagName = getAIConfig(MIDSCENE_REPORT_TAG_NAME);
48
+ const dateTimeInFileName = dayjs().format('YYYY-MM-DD_HH-mm-ss');
49
+ const uniqueId = uuid().substring(0, 8);
50
+ return `${reportTagName || tag}-${dateTimeInFileName}-${uniqueId}`;
51
+ }
52
+ function printReportMsg(filepath) {
53
+ logMsg(`Midscene - report file updated: ${filepath}`);
54
+ }
55
+ function getCurrentExecutionFile(trace) {
56
+ const error = new Error();
57
+ const stackTrace = trace || error.stack;
58
+ const pkgDir = process.cwd() || '';
59
+ if (stackTrace) {
60
+ const stackLines = stackTrace.split('\n');
61
+ for (const line of stackLines)if (line.includes('.spec.') || line.includes('.test.') || line.includes('.ts') || line.includes('.js')) {
62
+ const match = line.match(/(?:at\s+)?(.*?\.(?:spec|test)\.[jt]s)/);
63
+ if (null == match ? void 0 : match[1]) {
64
+ const targetFileName = match[1].replace(pkgDir, '').trim().replace('at ', '');
65
+ return targetFileName;
66
+ }
67
+ }
68
+ }
69
+ return false;
70
+ }
71
+ const testFileIndex = new Map();
72
+ function generateCacheId(fileName) {
73
+ let taskFile = fileName || getCurrentExecutionFile();
74
+ if (!taskFile) {
75
+ taskFile = uuid();
76
+ console.warn('Midscene - using random UUID for cache id. Cache may be invalid.');
77
+ }
78
+ if (testFileIndex.has(taskFile)) {
79
+ const currentIndex = testFileIndex.get(taskFile);
80
+ if (void 0 !== currentIndex) testFileIndex.set(taskFile, currentIndex + 1);
81
+ } else testFileIndex.set(taskFile, 1);
82
+ return `${taskFile}-${testFileIndex.get(taskFile)}`;
83
+ }
84
+ function matchElementFromPlan(planLocateParam, tree) {
85
+ if (!planLocateParam) return;
86
+ if (planLocateParam.id) return getNodeFromCacheList(planLocateParam.id);
87
+ if (planLocateParam.bbox) {
88
+ const centerPosition = {
89
+ x: Math.floor((planLocateParam.bbox[0] + planLocateParam.bbox[2]) / 2),
90
+ y: Math.floor((planLocateParam.bbox[1] + planLocateParam.bbox[3]) / 2)
91
+ };
92
+ let element = elementByPositionWithElementInfo(tree, centerPosition);
93
+ if (!element) element = generateElementByPosition(centerPosition);
94
+ return element;
95
+ }
96
+ }
97
+ async function matchElementFromCache(taskExecutor, xpaths, cachePrompt, cacheable) {
98
+ try {
99
+ var _taskExecutor_taskCache;
100
+ if ((null == xpaths ? void 0 : xpaths.length) && (null == (_taskExecutor_taskCache = taskExecutor.taskCache) ? void 0 : _taskExecutor_taskCache.isCacheResultUsed) && false !== cacheable && taskExecutor.page.getElementInfoByXpath) for(let i = 0; i < xpaths.length; i++){
101
+ const element = await taskExecutor.page.getElementInfoByXpath(xpaths[i]);
102
+ if (null == element ? void 0 : element.id) {
103
+ debug('cache hit, prompt: %s', cachePrompt);
104
+ debug('found a new element with same xpath, xpath: %s, id: %s', xpaths[i], null == element ? void 0 : element.id);
105
+ return element;
106
+ }
107
+ }
108
+ } catch (error) {
109
+ debug('get element info by xpath error: ', error);
110
+ }
111
+ }
112
+ function trimContextByViewport(execution) {
113
+ function filterVisibleTree(node) {
114
+ if (!node) return null;
115
+ const filteredChildren = Array.isArray(node.children) ? node.children.map(filterVisibleTree).filter((child)=>null !== child) : [];
116
+ if (node.node && true === node.node.isVisible) return {
117
+ ...node,
118
+ children: filteredChildren
119
+ };
120
+ if (filteredChildren.length > 0) return {
121
+ node: null,
122
+ children: filteredChildren
123
+ };
124
+ return null;
125
+ }
126
+ return {
127
+ ...execution,
128
+ tasks: Array.isArray(execution.tasks) ? execution.tasks.map((task)=>{
129
+ var _task_pageContext;
130
+ const newTask = {
131
+ ...task
132
+ };
133
+ if (null == (_task_pageContext = task.pageContext) ? void 0 : _task_pageContext.tree) newTask.pageContext = {
134
+ ...task.pageContext,
135
+ tree: filterVisibleTree(task.pageContext.tree) || {
136
+ node: null,
137
+ children: []
138
+ }
139
+ };
140
+ return newTask;
141
+ }) : execution.tasks
142
+ };
143
+ }
144
+ function buildDetailedLocateParam(locatePrompt, opt) {
145
+ debugUtils('will call buildDetailedLocateParam', locatePrompt, opt);
146
+ let prompt = locatePrompt || (null == opt ? void 0 : opt.prompt) || (null == opt ? void 0 : opt.locate);
147
+ let deepThink = false;
148
+ let cacheable = true;
149
+ let xpath;
150
+ if ('object' == typeof opt && null !== opt) {
151
+ deepThink = opt.deepThink ?? false;
152
+ cacheable = opt.cacheable ?? true;
153
+ xpath = opt.xpath;
154
+ if (locatePrompt && opt.prompt && locatePrompt !== opt.prompt) console.warn('conflict prompt for item', locatePrompt, opt, 'maybe you put the prompt in the wrong place');
155
+ prompt = prompt || opt.prompt;
156
+ }
157
+ if (!prompt) return void debugUtils('no prompt, will return undefined in buildDetailedLocateParam', opt);
158
+ return {
159
+ prompt,
160
+ deepThink,
161
+ cacheable,
162
+ xpath
163
+ };
164
+ }
165
+ function buildDetailedLocateParamAndRestParams(locatePrompt, opt, excludeKeys = []) {
166
+ const locateParam = buildDetailedLocateParam(locatePrompt, opt);
167
+ const restParams = {};
168
+ if ('object' == typeof opt && null !== opt) {
169
+ const allKeys = Object.keys(opt);
170
+ const locateParamKeys = Object.keys(locateParam || {});
171
+ for (const key of allKeys)if (!locateParamKeys.includes(key) && !excludeKeys.includes(key) && 'locate' !== key) restParams[key] = opt[key];
172
+ }
173
+ return {
174
+ locateParam,
175
+ restParams
176
+ };
177
+ }
178
+ const getMidsceneVersion = ()=>"0.27.1-beta-20250822094725.0";
179
+ const parsePrompt = (prompt)=>{
180
+ if ('string' == typeof prompt) return {
181
+ textPrompt: prompt,
182
+ multimodalPrompt: void 0
183
+ };
184
+ return {
185
+ textPrompt: prompt.prompt,
186
+ multimodalPrompt: prompt.images ? {
187
+ images: prompt.images,
188
+ convertHttpImage2Base64: !!prompt.convertHttpImage2Base64
189
+ } : void 0
190
+ };
191
+ };
192
+ const commonWebActionsForWebPage = (page)=>[
193
+ {
194
+ name: 'Tap',
195
+ description: 'Tap the element',
196
+ interfaceAlias: 'aiTap',
197
+ paramSchema: z.object({
198
+ locate: getMidsceneLocationSchema().describe('The element to be tapped')
199
+ }),
200
+ call: async (param)=>{
201
+ const element = param.locate;
202
+ assert(element, 'Element not found, cannot tap');
203
+ await page.mouse.click(element.center[0], element.center[1], {
204
+ button: 'left'
205
+ });
206
+ }
207
+ },
208
+ {
209
+ name: 'RightClick',
210
+ description: 'Right click the element',
211
+ interfaceAlias: 'aiRightClick',
212
+ paramSchema: z.object({
213
+ locate: getMidsceneLocationSchema().describe('The element to be right clicked')
214
+ }),
215
+ call: async (param)=>{
216
+ const element = param.locate;
217
+ assert(element, 'Element not found, cannot right click');
218
+ await page.mouse.click(element.center[0], element.center[1], {
219
+ button: 'right'
220
+ });
221
+ }
222
+ },
223
+ {
224
+ name: 'Hover',
225
+ description: 'Move the mouse to the element',
226
+ interfaceAlias: 'aiHover',
227
+ paramSchema: z.object({
228
+ locate: getMidsceneLocationSchema().describe('The element to be hovered')
229
+ }),
230
+ call: async (param)=>{
231
+ const element = param.locate;
232
+ assert(element, 'Element not found, cannot hover');
233
+ await page.mouse.move(element.center[0], element.center[1]);
234
+ }
235
+ },
236
+ {
237
+ name: 'Input',
238
+ description: 'Replace the input field with a new value. `value` is the final that should be filled in the input box. No matter what modifications are required, just provide the final value to replace the existing input value. Giving a blank string means clear the input field.',
239
+ interfaceAlias: 'aiInput',
240
+ paramSchema: z.object({
241
+ value: z.string().describe('The final value that should be filled in the input box'),
242
+ locate: getMidsceneLocationSchema().describe('The input field to be filled').optional()
243
+ }),
244
+ call: async (param)=>{
245
+ const element = param.locate;
246
+ if (element) {
247
+ await page.clearInput(element);
248
+ if (!param || !param.value) return;
249
+ }
250
+ await page.keyboard.type(param.value);
251
+ }
252
+ },
253
+ {
254
+ name: 'KeyboardPress',
255
+ description: 'Press a function key, like "Enter", "Tab", "Escape". Do not use this to type text.',
256
+ interfaceAlias: 'aiKeyboardPress',
257
+ paramSchema: z.object({
258
+ locate: getMidsceneLocationSchema().describe('The element to be clicked before pressing the key').optional(),
259
+ keyName: z.string().describe('The key to be pressed')
260
+ }),
261
+ call: async (param)=>{
262
+ const element = param.locate;
263
+ if (element) await page.mouse.click(element.center[0], element.center[1], {
264
+ button: 'left'
265
+ });
266
+ const keys = getKeyCommands(param.keyName);
267
+ await page.keyboard.press(keys);
268
+ }
269
+ },
270
+ {
271
+ name: 'Scroll',
272
+ description: 'Scroll the page or an element. The direction to scroll, the scroll type, and the distance to scroll. The distance is the number of pixels to scroll. If not specified, use `down` direction, `once` scroll type, and `null` distance.',
273
+ interfaceAlias: 'aiScroll',
274
+ paramSchema: z.object({
275
+ direction: z["enum"]([
276
+ 'down',
277
+ 'up',
278
+ 'right',
279
+ 'left'
280
+ ]).default('down').describe('The direction to scroll'),
281
+ scrollType: z["enum"]([
282
+ 'once',
283
+ 'untilBottom',
284
+ 'untilTop',
285
+ 'untilRight',
286
+ 'untilLeft'
287
+ ]).default('once').describe('The scroll type'),
288
+ distance: z.number().nullable().optional().describe('The distance in pixels to scroll'),
289
+ locate: getMidsceneLocationSchema().optional().describe('The element to be scrolled')
290
+ }),
291
+ call: async (param)=>{
292
+ const element = param.locate;
293
+ const startingPoint = element ? {
294
+ left: element.center[0],
295
+ top: element.center[1]
296
+ } : void 0;
297
+ const scrollToEventName = null == param ? void 0 : param.scrollType;
298
+ if ('untilTop' === scrollToEventName) await page.scrollUntilTop(startingPoint);
299
+ else if ('untilBottom' === scrollToEventName) await page.scrollUntilBottom(startingPoint);
300
+ else if ('untilRight' === scrollToEventName) await page.scrollUntilRight(startingPoint);
301
+ else if ('untilLeft' === scrollToEventName) await page.scrollUntilLeft(startingPoint);
302
+ else if ('once' !== scrollToEventName && scrollToEventName) throw new Error(`Unknown scroll event type: ${scrollToEventName}, param: ${JSON.stringify(param)}`);
303
+ else {
304
+ if ((null == param ? void 0 : param.direction) !== 'down' && param && param.direction) if ('up' === param.direction) await page.scrollUp(param.distance || void 0, startingPoint);
305
+ else if ('left' === param.direction) await page.scrollLeft(param.distance || void 0, startingPoint);
306
+ else if ('right' === param.direction) await page.scrollRight(param.distance || void 0, startingPoint);
307
+ else throw new Error(`Unknown scroll direction: ${param.direction}`);
308
+ else await page.scrollDown((null == param ? void 0 : param.distance) || void 0, startingPoint);
309
+ await sleep(500);
310
+ }
311
+ }
312
+ },
313
+ {
314
+ name: 'DragAndDrop',
315
+ description: 'Drag and drop the element',
316
+ paramSchema: z.object({
317
+ from: getMidsceneLocationSchema().describe('The position to be dragged'),
318
+ to: getMidsceneLocationSchema().describe('The position to be dropped')
319
+ }),
320
+ call: async (param)=>{
321
+ const from = param.from;
322
+ const to = param.to;
323
+ assert(from, 'missing "from" param for drag and drop');
324
+ assert(to, 'missing "to" param for drag and drop');
325
+ await page.mouse.drag({
326
+ x: from.center[0],
327
+ y: from.center[1]
328
+ }, {
329
+ x: to.center[0],
330
+ y: to.center[1]
331
+ });
332
+ }
333
+ }
334
+ ];
335
+ export { buildDetailedLocateParam, buildDetailedLocateParamAndRestParams, commonContextParser, commonWebActionsForWebPage, generateCacheId, getCurrentExecutionFile, getMidsceneVersion, getReportFileName, matchElementFromCache, matchElementFromPlan, parsePrompt, printReportMsg, trimContextByViewport };
336
+
337
+ //# sourceMappingURL=utils.mjs.map