@midscene/core 1.4.3 → 1.4.4

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.
@@ -106,7 +106,7 @@ async function matchElementFromCache(context, cacheEntry, cachePrompt, cacheable
106
106
  return;
107
107
  }
108
108
  }
109
- const getMidsceneVersion = ()=>"1.4.3";
109
+ const getMidsceneVersion = ()=>"1.4.4";
110
110
  const parsePrompt = (prompt)=>{
111
111
  if ('string' == typeof prompt) return {
112
112
  textPrompt: prompt,
@@ -3,7 +3,7 @@ import { getDebug } from "@midscene/shared/logger";
3
3
  import { assert } from "@midscene/shared/utils";
4
4
  import { buildYamlFlowFromPlans, fillBboxParam, findAllMidsceneLocatorField } from "../common.mjs";
5
5
  import { systemPromptToTaskPlanning } from "./prompt/llm-planning.mjs";
6
- import { extractXMLTag, parseMarkFinishedIndexes, parseSubGoalsFromXML } from "./prompt/util.mjs";
6
+ import { extractXMLTag } from "./prompt/util.mjs";
7
7
  import { AIResponseParseError, callAI, safeParseJson } from "./service-caller/index.mjs";
8
8
  const debug = getDebug('planning');
9
9
  const warnLog = getDebug('planning', {
@@ -24,10 +24,6 @@ function parseXMLPlanningResponse(xmlString, modelFamily) {
24
24
  finalizeSuccess = 'true' === completeGoalMatch[1];
25
25
  finalizeMessage = completeGoalMatch[2]?.trim() || void 0;
26
26
  }
27
- const updatePlanContent = extractXMLTag(xmlString, 'update-plan-content');
28
- const markSubGoalDone = extractXMLTag(xmlString, 'mark-sub-goal-done');
29
- const updateSubGoals = updatePlanContent ? parseSubGoalsFromXML(updatePlanContent) : void 0;
30
- const markFinishedIndexes = markSubGoalDone ? parseMarkFinishedIndexes(markSubGoalDone) : void 0;
31
27
  let action = null;
32
28
  if (actionType && 'null' !== actionType.toLowerCase()) {
33
29
  const type = actionType.trim();
@@ -61,12 +57,6 @@ function parseXMLPlanningResponse(xmlString, modelFamily) {
61
57
  } : {},
62
58
  ...void 0 !== finalizeSuccess ? {
63
59
  finalizeSuccess
64
- } : {},
65
- ...updateSubGoals?.length ? {
66
- updateSubGoals
67
- } : {},
68
- ...markFinishedIndexes?.length ? {
69
- markFinishedIndexes
70
60
  } : {}
71
61
  };
72
62
  }
@@ -75,13 +65,12 @@ async function plan(userInstruction, opts) {
75
65
  const { size } = context;
76
66
  const screenshotBase64 = context.screenshot.base64;
77
67
  const { modelFamily } = modelConfig;
78
- const includeSubGoals = true === opts.deepThink;
79
68
  const systemPrompt = await systemPromptToTaskPlanning({
80
69
  actionSpace: opts.actionSpace,
81
70
  modelFamily,
82
71
  includeBbox: opts.includeBbox,
83
72
  includeThought: true,
84
- includeSubGoals
73
+ deepThink: true === opts.deepThink
85
74
  });
86
75
  let imagePayload = screenshotBase64;
87
76
  let imageWidth = size.width;
@@ -105,8 +94,8 @@ async function plan(userInstruction, opts) {
105
94
  }
106
95
  ];
107
96
  let latestFeedbackMessage;
108
- const subGoalsText = includeSubGoals ? conversationHistory.subGoalsToText() : conversationHistory.historicalLogsToText();
109
- const subGoalsSection = subGoalsText ? `\n\n${subGoalsText}` : '';
97
+ const historicalLogsText = conversationHistory.historicalLogsToText();
98
+ const historicalLogsSection = historicalLogsText ? `\n\n${historicalLogsText}` : '';
110
99
  const memoriesText = conversationHistory.memoriesToText();
111
100
  const memoriesSection = memoriesText ? `\n\n${memoriesText}` : '';
112
101
  if (conversationHistory.pendingFeedbackMessage) {
@@ -115,7 +104,7 @@ async function plan(userInstruction, opts) {
115
104
  content: [
116
105
  {
117
106
  type: 'text',
118
- text: `${conversationHistory.pendingFeedbackMessage}. The previous action has been executed, here is the latest screenshot. Please continue according to the instruction.${memoriesSection}${subGoalsSection}`
107
+ text: `${conversationHistory.pendingFeedbackMessage}. The previous action has been executed, here is the latest screenshot. Please continue according to the instruction.${memoriesSection}${historicalLogsSection}`
119
108
  },
120
109
  {
121
110
  type: 'image_url',
@@ -132,7 +121,7 @@ async function plan(userInstruction, opts) {
132
121
  content: [
133
122
  {
134
123
  type: 'text',
135
- text: `this is the latest screenshot${memoriesSection}${subGoalsSection}`
124
+ text: `this is the latest screenshot${memoriesSection}${historicalLogsSection}`
136
125
  },
137
126
  {
138
127
  type: 'image_url',
@@ -172,7 +161,6 @@ async function plan(userInstruction, opts) {
172
161
  if (void 0 !== planFromAI.finalizeSuccess) {
173
162
  debug('task completed via <complete> tag, stop planning');
174
163
  shouldContinuePlanning = false;
175
- if (includeSubGoals) conversationHistory.markAllSubGoalsFinished();
176
164
  }
177
165
  const returnValue = {
178
166
  ...planFromAI,
@@ -195,11 +183,7 @@ async function plan(userInstruction, opts) {
195
183
  if (locateResult && void 0 !== modelFamily) action.param[field] = fillBboxParam(locateResult, imageWidth, imageHeight, modelFamily);
196
184
  });
197
185
  });
198
- if (includeSubGoals) {
199
- if (planFromAI.updateSubGoals?.length) conversationHistory.setSubGoals(planFromAI.updateSubGoals);
200
- if (planFromAI.markFinishedIndexes?.length) for (const index of planFromAI.markFinishedIndexes)conversationHistory.markSubGoalFinished(index);
201
- if (planFromAI.log) conversationHistory.appendSubGoalLog(planFromAI.log);
202
- } else if (planFromAI.log) conversationHistory.appendHistoricalLog(planFromAI.log);
186
+ if (planFromAI.log) conversationHistory.appendHistoricalLog(planFromAI.log);
203
187
  if (planFromAI.memory) conversationHistory.appendMemory(planFromAI.memory);
204
188
  conversationHistory.append({
205
189
  role: 'assistant',
@@ -1 +1 @@
1
- {"version":3,"file":"ai-model/llm-planning.mjs","sources":["../../../src/ai-model/llm-planning.ts"],"sourcesContent":["import type {\n DeepThinkOption,\n DeviceAction,\n InterfaceType,\n PlanningAIResponse,\n RawResponsePlanningAIResponse,\n UIContext,\n} from '@/types';\nimport type { IModelConfig, TModelFamily } from '@midscene/shared/env';\nimport { paddingToMatchBlockByBase64 } from '@midscene/shared/img';\nimport { getDebug } from '@midscene/shared/logger';\nimport { assert } from '@midscene/shared/utils';\nimport type { ChatCompletionMessageParam } from 'openai/resources/index';\nimport {\n buildYamlFlowFromPlans,\n fillBboxParam,\n findAllMidsceneLocatorField,\n} from '../common';\nimport type { ConversationHistory } from './conversation-history';\nimport { systemPromptToTaskPlanning } from './prompt/llm-planning';\nimport {\n extractXMLTag,\n parseMarkFinishedIndexes,\n parseSubGoalsFromXML,\n} from './prompt/util';\nimport {\n AIResponseParseError,\n callAI,\n safeParseJson,\n} from './service-caller/index';\n\nconst debug = getDebug('planning');\nconst warnLog = getDebug('planning', { console: true });\n\n/**\n * Parse XML response from LLM and convert to RawResponsePlanningAIResponse\n */\nexport function parseXMLPlanningResponse(\n xmlString: string,\n modelFamily: TModelFamily | undefined,\n): RawResponsePlanningAIResponse {\n const thought = extractXMLTag(xmlString, 'thought');\n const memory = extractXMLTag(xmlString, 'memory');\n const log = extractXMLTag(xmlString, 'log') || '';\n const error = extractXMLTag(xmlString, 'error');\n const actionType = extractXMLTag(xmlString, 'action-type');\n const actionParamStr = extractXMLTag(xmlString, 'action-param-json');\n\n // Parse <complete> tag with success attribute\n const completeGoalRegex =\n /<complete\\s+success=\"(true|false)\">([\\s\\S]*?)<\\/complete>/i;\n const completeGoalMatch = xmlString.match(completeGoalRegex);\n let finalizeMessage: string | undefined;\n let finalizeSuccess: boolean | undefined;\n\n if (completeGoalMatch) {\n finalizeSuccess = completeGoalMatch[1] === 'true';\n finalizeMessage = completeGoalMatch[2]?.trim() || undefined;\n }\n\n // Parse sub-goal related tags\n const updatePlanContent = extractXMLTag(xmlString, 'update-plan-content');\n const markSubGoalDone = extractXMLTag(xmlString, 'mark-sub-goal-done');\n\n const updateSubGoals = updatePlanContent\n ? parseSubGoalsFromXML(updatePlanContent)\n : undefined;\n const markFinishedIndexes = markSubGoalDone\n ? parseMarkFinishedIndexes(markSubGoalDone)\n : undefined;\n\n // Parse action\n let action: any = null;\n if (actionType && actionType.toLowerCase() !== 'null') {\n const type = actionType.trim();\n let param: any = undefined;\n\n if (actionParamStr) {\n try {\n // Parse the JSON string in action-param-json\n param = safeParseJson(actionParamStr, modelFamily);\n } catch (e) {\n throw new Error(`Failed to parse action-param-json: ${e}`);\n }\n }\n\n action = {\n type,\n ...(param !== undefined ? { param } : {}),\n };\n }\n\n return {\n ...(thought ? { thought } : {}),\n ...(memory ? { memory } : {}),\n log,\n ...(error ? { error } : {}),\n action,\n ...(finalizeMessage !== undefined ? { finalizeMessage } : {}),\n ...(finalizeSuccess !== undefined ? { finalizeSuccess } : {}),\n ...(updateSubGoals?.length ? { updateSubGoals } : {}),\n ...(markFinishedIndexes?.length ? { markFinishedIndexes } : {}),\n };\n}\n\nexport async function plan(\n userInstruction: string,\n opts: {\n context: UIContext;\n interfaceType: InterfaceType;\n actionSpace: DeviceAction<any>[];\n actionContext?: string;\n modelConfig: IModelConfig;\n conversationHistory: ConversationHistory;\n includeBbox: boolean;\n imagesIncludeCount?: number;\n deepThink?: DeepThinkOption;\n },\n): Promise<PlanningAIResponse> {\n const { context, modelConfig, conversationHistory } = opts;\n const { size } = context;\n const screenshotBase64 = context.screenshot.base64;\n\n const { modelFamily } = modelConfig;\n\n // Only enable sub-goals when deepThink is true\n const includeSubGoals = opts.deepThink === true;\n\n const systemPrompt = await systemPromptToTaskPlanning({\n actionSpace: opts.actionSpace,\n modelFamily,\n includeBbox: opts.includeBbox,\n includeThought: true, // always include thought\n includeSubGoals,\n });\n\n let imagePayload = screenshotBase64;\n let imageWidth = size.width;\n let imageHeight = size.height;\n const rightLimit = imageWidth;\n const bottomLimit = imageHeight;\n\n // Process image based on VL mode requirements\n if (modelFamily === 'qwen2.5-vl') {\n const paddedResult = await paddingToMatchBlockByBase64(imagePayload);\n imageWidth = paddedResult.width;\n imageHeight = paddedResult.height;\n imagePayload = paddedResult.imageBase64;\n }\n\n const actionContext = opts.actionContext\n ? `<high_priority_knowledge>${opts.actionContext}</high_priority_knowledge>\\n`\n : '';\n\n const instruction: ChatCompletionMessageParam[] = [\n {\n role: 'user',\n content: [\n {\n type: 'text',\n text: `${actionContext}<user_instruction>${userInstruction}</user_instruction>`,\n },\n ],\n },\n ];\n\n let latestFeedbackMessage: ChatCompletionMessageParam;\n\n // Build sub-goal status text to include in the message\n // In deepThink mode: show full sub-goals with logs\n // In non-deepThink mode: show historical execution logs\n const subGoalsText = includeSubGoals\n ? conversationHistory.subGoalsToText()\n : conversationHistory.historicalLogsToText();\n const subGoalsSection = subGoalsText ? `\\n\\n${subGoalsText}` : '';\n\n // Build memories text to include in the message\n const memoriesText = conversationHistory.memoriesToText();\n const memoriesSection = memoriesText ? `\\n\\n${memoriesText}` : '';\n\n if (conversationHistory.pendingFeedbackMessage) {\n latestFeedbackMessage = {\n role: 'user',\n content: [\n {\n type: 'text',\n text: `${conversationHistory.pendingFeedbackMessage}. The previous action has been executed, here is the latest screenshot. Please continue according to the instruction.${memoriesSection}${subGoalsSection}`,\n },\n {\n type: 'image_url',\n image_url: {\n url: imagePayload,\n detail: 'high',\n },\n },\n ],\n };\n\n conversationHistory.resetPendingFeedbackMessageIfExists();\n } else {\n latestFeedbackMessage = {\n role: 'user',\n content: [\n {\n type: 'text',\n text: `this is the latest screenshot${memoriesSection}${subGoalsSection}`,\n },\n {\n type: 'image_url',\n image_url: {\n url: imagePayload,\n detail: 'high',\n },\n },\n ],\n };\n }\n conversationHistory.append(latestFeedbackMessage);\n\n // Compress history if it exceeds the threshold to avoid context overflow\n conversationHistory.compressHistory(50, 20);\n\n const historyLog = conversationHistory.snapshot(opts.imagesIncludeCount);\n\n const msgs: ChatCompletionMessageParam[] = [\n { role: 'system', content: systemPrompt },\n ...instruction,\n ...historyLog,\n ];\n\n const {\n content: rawResponse,\n usage,\n reasoning_content,\n } = await callAI(msgs, modelConfig, {\n deepThink: opts.deepThink === 'unset' ? undefined : opts.deepThink,\n });\n\n // Parse XML response to JSON object, capture parsing errors\n let planFromAI: RawResponsePlanningAIResponse;\n try {\n planFromAI = parseXMLPlanningResponse(rawResponse, modelFamily);\n\n if (planFromAI.action && planFromAI.finalizeSuccess !== undefined) {\n warnLog(\n 'Planning response included both an action and <complete>; ignoring <complete> output.',\n );\n planFromAI.finalizeMessage = undefined;\n planFromAI.finalizeSuccess = undefined;\n }\n\n const actions = planFromAI.action ? [planFromAI.action] : [];\n let shouldContinuePlanning = true;\n\n // Check if task is completed via <complete> tag\n if (planFromAI.finalizeSuccess !== undefined) {\n debug('task completed via <complete> tag, stop planning');\n shouldContinuePlanning = false;\n // Mark all sub-goals as finished when goal is completed (only when deepThink is enabled)\n if (includeSubGoals) {\n conversationHistory.markAllSubGoalsFinished();\n }\n }\n\n const returnValue: PlanningAIResponse = {\n ...planFromAI,\n actions,\n rawResponse,\n usage,\n reasoning_content,\n yamlFlow: buildYamlFlowFromPlans(actions, opts.actionSpace),\n shouldContinuePlanning,\n };\n\n assert(planFromAI, \"can't get plans from AI\");\n\n actions.forEach((action) => {\n const type = action.type;\n const actionInActionSpace = opts.actionSpace.find(\n (action) => action.name === type,\n );\n\n debug('actionInActionSpace matched', actionInActionSpace);\n const locateFields = actionInActionSpace\n ? findAllMidsceneLocatorField(actionInActionSpace.paramSchema)\n : [];\n\n debug('locateFields', locateFields);\n\n locateFields.forEach((field) => {\n const locateResult = action.param[field];\n if (locateResult && modelFamily !== undefined) {\n // Always use model family to fill bbox parameters\n action.param[field] = fillBboxParam(\n locateResult,\n imageWidth,\n imageHeight,\n modelFamily,\n );\n }\n });\n });\n\n // Update sub-goals in conversation history based on response (only when deepThink is enabled)\n if (includeSubGoals) {\n if (planFromAI.updateSubGoals?.length) {\n conversationHistory.setSubGoals(planFromAI.updateSubGoals);\n }\n if (planFromAI.markFinishedIndexes?.length) {\n for (const index of planFromAI.markFinishedIndexes) {\n conversationHistory.markSubGoalFinished(index);\n }\n }\n // Append the planning log to the currently running sub-goal\n if (planFromAI.log) {\n conversationHistory.appendSubGoalLog(planFromAI.log);\n }\n } else {\n // In non-deepThink mode, accumulate logs as historical execution steps\n if (planFromAI.log) {\n conversationHistory.appendHistoricalLog(planFromAI.log);\n }\n }\n\n // Append memory to conversation history if present\n if (planFromAI.memory) {\n conversationHistory.appendMemory(planFromAI.memory);\n }\n\n conversationHistory.append({\n role: 'assistant',\n content: [\n {\n type: 'text',\n text: rawResponse,\n },\n ],\n });\n\n return returnValue;\n } catch (parseError) {\n // Throw AIResponseParseError with usage and rawResponse preserved\n const errorMessage =\n parseError instanceof Error ? parseError.message : String(parseError);\n throw new AIResponseParseError(\n `XML parse error: ${errorMessage}`,\n rawResponse,\n usage,\n );\n }\n}\n"],"names":["debug","getDebug","warnLog","parseXMLPlanningResponse","xmlString","modelFamily","thought","extractXMLTag","memory","log","error","actionType","actionParamStr","completeGoalRegex","completeGoalMatch","finalizeMessage","finalizeSuccess","undefined","updatePlanContent","markSubGoalDone","updateSubGoals","parseSubGoalsFromXML","markFinishedIndexes","parseMarkFinishedIndexes","action","type","param","safeParseJson","e","Error","plan","userInstruction","opts","context","modelConfig","conversationHistory","size","screenshotBase64","includeSubGoals","systemPrompt","systemPromptToTaskPlanning","imagePayload","imageWidth","imageHeight","paddedResult","paddingToMatchBlockByBase64","actionContext","instruction","latestFeedbackMessage","subGoalsText","subGoalsSection","memoriesText","memoriesSection","historyLog","msgs","rawResponse","usage","reasoning_content","callAI","planFromAI","actions","shouldContinuePlanning","returnValue","buildYamlFlowFromPlans","assert","actionInActionSpace","locateFields","findAllMidsceneLocatorField","field","locateResult","fillBboxParam","index","parseError","errorMessage","String","AIResponseParseError"],"mappings":";;;;;;;AA+BA,MAAMA,QAAQC,SAAS;AACvB,MAAMC,UAAUD,SAAS,YAAY;IAAE,SAAS;AAAK;AAK9C,SAASE,yBACdC,SAAiB,EACjBC,WAAqC;IAErC,MAAMC,UAAUC,cAAcH,WAAW;IACzC,MAAMI,SAASD,cAAcH,WAAW;IACxC,MAAMK,MAAMF,cAAcH,WAAW,UAAU;IAC/C,MAAMM,QAAQH,cAAcH,WAAW;IACvC,MAAMO,aAAaJ,cAAcH,WAAW;IAC5C,MAAMQ,iBAAiBL,cAAcH,WAAW;IAGhD,MAAMS,oBACJ;IACF,MAAMC,oBAAoBV,UAAU,KAAK,CAACS;IAC1C,IAAI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version":3,"file":"ai-model/llm-planning.mjs","sources":["../../../src/ai-model/llm-planning.ts"],"sourcesContent":["import type {\n DeepThinkOption,\n DeviceAction,\n InterfaceType,\n PlanningAIResponse,\n RawResponsePlanningAIResponse,\n UIContext,\n} from '@/types';\nimport type { IModelConfig, TModelFamily } from '@midscene/shared/env';\nimport { paddingToMatchBlockByBase64 } from '@midscene/shared/img';\nimport { getDebug } from '@midscene/shared/logger';\nimport { assert } from '@midscene/shared/utils';\nimport type { ChatCompletionMessageParam } from 'openai/resources/index';\nimport {\n buildYamlFlowFromPlans,\n fillBboxParam,\n findAllMidsceneLocatorField,\n} from '../common';\nimport type { ConversationHistory } from './conversation-history';\nimport { systemPromptToTaskPlanning } from './prompt/llm-planning';\nimport { extractXMLTag } from './prompt/util';\nimport {\n AIResponseParseError,\n callAI,\n safeParseJson,\n} from './service-caller/index';\n\nconst debug = getDebug('planning');\nconst warnLog = getDebug('planning', { console: true });\n\n/**\n * Parse XML response from LLM and convert to RawResponsePlanningAIResponse\n */\nexport function parseXMLPlanningResponse(\n xmlString: string,\n modelFamily: TModelFamily | undefined,\n): RawResponsePlanningAIResponse {\n const thought = extractXMLTag(xmlString, 'thought');\n const memory = extractXMLTag(xmlString, 'memory');\n const log = extractXMLTag(xmlString, 'log') || '';\n const error = extractXMLTag(xmlString, 'error');\n const actionType = extractXMLTag(xmlString, 'action-type');\n const actionParamStr = extractXMLTag(xmlString, 'action-param-json');\n\n // Parse <complete> tag with success attribute\n const completeGoalRegex =\n /<complete\\s+success=\"(true|false)\">([\\s\\S]*?)<\\/complete>/i;\n const completeGoalMatch = xmlString.match(completeGoalRegex);\n let finalizeMessage: string | undefined;\n let finalizeSuccess: boolean | undefined;\n\n if (completeGoalMatch) {\n finalizeSuccess = completeGoalMatch[1] === 'true';\n finalizeMessage = completeGoalMatch[2]?.trim() || undefined;\n }\n\n // Parse action\n let action: any = null;\n if (actionType && actionType.toLowerCase() !== 'null') {\n const type = actionType.trim();\n let param: any = undefined;\n\n if (actionParamStr) {\n try {\n // Parse the JSON string in action-param-json\n param = safeParseJson(actionParamStr, modelFamily);\n } catch (e) {\n throw new Error(`Failed to parse action-param-json: ${e}`);\n }\n }\n\n action = {\n type,\n ...(param !== undefined ? { param } : {}),\n };\n }\n\n return {\n ...(thought ? { thought } : {}),\n ...(memory ? { memory } : {}),\n log,\n ...(error ? { error } : {}),\n action,\n ...(finalizeMessage !== undefined ? { finalizeMessage } : {}),\n ...(finalizeSuccess !== undefined ? { finalizeSuccess } : {}),\n };\n}\n\nexport async function plan(\n userInstruction: string,\n opts: {\n context: UIContext;\n interfaceType: InterfaceType;\n actionSpace: DeviceAction<any>[];\n actionContext?: string;\n modelConfig: IModelConfig;\n conversationHistory: ConversationHistory;\n includeBbox: boolean;\n imagesIncludeCount?: number;\n deepThink?: DeepThinkOption;\n },\n): Promise<PlanningAIResponse> {\n const { context, modelConfig, conversationHistory } = opts;\n const { size } = context;\n const screenshotBase64 = context.screenshot.base64;\n\n const { modelFamily } = modelConfig;\n\n const systemPrompt = await systemPromptToTaskPlanning({\n actionSpace: opts.actionSpace,\n modelFamily,\n includeBbox: opts.includeBbox,\n includeThought: true, // always include thought\n deepThink: opts.deepThink === true,\n });\n\n let imagePayload = screenshotBase64;\n let imageWidth = size.width;\n let imageHeight = size.height;\n const rightLimit = imageWidth;\n const bottomLimit = imageHeight;\n\n // Process image based on VL mode requirements\n if (modelFamily === 'qwen2.5-vl') {\n const paddedResult = await paddingToMatchBlockByBase64(imagePayload);\n imageWidth = paddedResult.width;\n imageHeight = paddedResult.height;\n imagePayload = paddedResult.imageBase64;\n }\n\n const actionContext = opts.actionContext\n ? `<high_priority_knowledge>${opts.actionContext}</high_priority_knowledge>\\n`\n : '';\n\n const instruction: ChatCompletionMessageParam[] = [\n {\n role: 'user',\n content: [\n {\n type: 'text',\n text: `${actionContext}<user_instruction>${userInstruction}</user_instruction>`,\n },\n ],\n },\n ];\n\n let latestFeedbackMessage: ChatCompletionMessageParam;\n\n // Build historical execution logs text to include in the message\n const historicalLogsText = conversationHistory.historicalLogsToText();\n const historicalLogsSection = historicalLogsText\n ? `\\n\\n${historicalLogsText}`\n : '';\n\n // Build memories text to include in the message\n const memoriesText = conversationHistory.memoriesToText();\n const memoriesSection = memoriesText ? `\\n\\n${memoriesText}` : '';\n\n if (conversationHistory.pendingFeedbackMessage) {\n latestFeedbackMessage = {\n role: 'user',\n content: [\n {\n type: 'text',\n text: `${conversationHistory.pendingFeedbackMessage}. The previous action has been executed, here is the latest screenshot. Please continue according to the instruction.${memoriesSection}${historicalLogsSection}`,\n },\n {\n type: 'image_url',\n image_url: {\n url: imagePayload,\n detail: 'high',\n },\n },\n ],\n };\n\n conversationHistory.resetPendingFeedbackMessageIfExists();\n } else {\n latestFeedbackMessage = {\n role: 'user',\n content: [\n {\n type: 'text',\n text: `this is the latest screenshot${memoriesSection}${historicalLogsSection}`,\n },\n {\n type: 'image_url',\n image_url: {\n url: imagePayload,\n detail: 'high',\n },\n },\n ],\n };\n }\n conversationHistory.append(latestFeedbackMessage);\n\n // Compress history if it exceeds the threshold to avoid context overflow\n conversationHistory.compressHistory(50, 20);\n\n const historyLog = conversationHistory.snapshot(opts.imagesIncludeCount);\n\n const msgs: ChatCompletionMessageParam[] = [\n { role: 'system', content: systemPrompt },\n ...instruction,\n ...historyLog,\n ];\n\n const {\n content: rawResponse,\n usage,\n reasoning_content,\n } = await callAI(msgs, modelConfig, {\n deepThink: opts.deepThink === 'unset' ? undefined : opts.deepThink,\n });\n\n // Parse XML response to JSON object, capture parsing errors\n let planFromAI: RawResponsePlanningAIResponse;\n try {\n planFromAI = parseXMLPlanningResponse(rawResponse, modelFamily);\n\n if (planFromAI.action && planFromAI.finalizeSuccess !== undefined) {\n warnLog(\n 'Planning response included both an action and <complete>; ignoring <complete> output.',\n );\n planFromAI.finalizeMessage = undefined;\n planFromAI.finalizeSuccess = undefined;\n }\n\n const actions = planFromAI.action ? [planFromAI.action] : [];\n let shouldContinuePlanning = true;\n\n // Check if task is completed via <complete> tag\n if (planFromAI.finalizeSuccess !== undefined) {\n debug('task completed via <complete> tag, stop planning');\n shouldContinuePlanning = false;\n }\n\n const returnValue: PlanningAIResponse = {\n ...planFromAI,\n actions,\n rawResponse,\n usage,\n reasoning_content,\n yamlFlow: buildYamlFlowFromPlans(actions, opts.actionSpace),\n shouldContinuePlanning,\n };\n\n assert(planFromAI, \"can't get plans from AI\");\n\n actions.forEach((action) => {\n const type = action.type;\n const actionInActionSpace = opts.actionSpace.find(\n (action) => action.name === type,\n );\n\n debug('actionInActionSpace matched', actionInActionSpace);\n const locateFields = actionInActionSpace\n ? findAllMidsceneLocatorField(actionInActionSpace.paramSchema)\n : [];\n\n debug('locateFields', locateFields);\n\n locateFields.forEach((field) => {\n const locateResult = action.param[field];\n if (locateResult && modelFamily !== undefined) {\n // Always use model family to fill bbox parameters\n action.param[field] = fillBboxParam(\n locateResult,\n imageWidth,\n imageHeight,\n modelFamily,\n );\n }\n });\n });\n\n // Accumulate logs as historical execution steps\n if (planFromAI.log) {\n conversationHistory.appendHistoricalLog(planFromAI.log);\n }\n\n // Append memory to conversation history if present\n if (planFromAI.memory) {\n conversationHistory.appendMemory(planFromAI.memory);\n }\n\n conversationHistory.append({\n role: 'assistant',\n content: [\n {\n type: 'text',\n text: rawResponse,\n },\n ],\n });\n\n return returnValue;\n } catch (parseError) {\n // Throw AIResponseParseError with usage and rawResponse preserved\n const errorMessage =\n parseError instanceof Error ? parseError.message : String(parseError);\n throw new AIResponseParseError(\n `XML parse error: ${errorMessage}`,\n rawResponse,\n usage,\n );\n }\n}\n"],"names":["debug","getDebug","warnLog","parseXMLPlanningResponse","xmlString","modelFamily","thought","extractXMLTag","memory","log","error","actionType","actionParamStr","completeGoalRegex","completeGoalMatch","finalizeMessage","finalizeSuccess","undefined","action","type","param","safeParseJson","e","Error","plan","userInstruction","opts","context","modelConfig","conversationHistory","size","screenshotBase64","systemPrompt","systemPromptToTaskPlanning","imagePayload","imageWidth","imageHeight","paddedResult","paddingToMatchBlockByBase64","actionContext","instruction","latestFeedbackMessage","historicalLogsText","historicalLogsSection","memoriesText","memoriesSection","historyLog","msgs","rawResponse","usage","reasoning_content","callAI","planFromAI","actions","shouldContinuePlanning","returnValue","buildYamlFlowFromPlans","assert","actionInActionSpace","locateFields","findAllMidsceneLocatorField","field","locateResult","fillBboxParam","parseError","errorMessage","String","AIResponseParseError"],"mappings":";;;;;;;AA2BA,MAAMA,QAAQC,SAAS;AACvB,MAAMC,UAAUD,SAAS,YAAY;IAAE,SAAS;AAAK;AAK9C,SAASE,yBACdC,SAAiB,EACjBC,WAAqC;IAErC,MAAMC,UAAUC,cAAcH,WAAW;IACzC,MAAMI,SAASD,cAAcH,WAAW;IACxC,MAAMK,MAAMF,cAAcH,WAAW,UAAU;IAC/C,MAAMM,QAAQH,cAAcH,WAAW;IACvC,MAAMO,aAAaJ,cAAcH,WAAW;IAC5C,MAAMQ,iBAAiBL,cAAcH,WAAW;IAGhD,MAAMS,oBACJ;IACF,MAAMC,oBAAoBV,UAAU,KAAK,CAACS;IAC1C,IAAIE;IACJ,IAAIC;IAEJ,IAAIF,mBAAmB;QACrBE,kBAAkBF,AAAyB,WAAzBA,iBAAiB,CAAC,EAAE;QACtCC,kBAAkBD,iBAAiB,CAAC,EAAE,EAAE,UAAUG;IACpD;IAGA,IAAIC,SAAc;IAClB,IAAIP,cAAcA,AAA6B,WAA7BA,WAAW,WAAW,IAAe;QACrD,MAAMQ,OAAOR,WAAW,IAAI;QAC5B,IAAIS;QAEJ,IAAIR,gBACF,IAAI;YAEFQ,QAAQC,cAAcT,gBAAgBP;QACxC,EAAE,OAAOiB,GAAG;YACV,MAAM,IAAIC,MAAM,CAAC,mCAAmC,EAAED,GAAG;QAC3D;QAGFJ,SAAS;YACPC;YACA,GAAIC,AAAUH,WAAVG,QAAsB;gBAAEA;YAAM,IAAI,CAAC,CAAC;QAC1C;IACF;IAEA,OAAO;QACL,GAAId,UAAU;YAAEA;QAAQ,IAAI,CAA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-63,12 +63,11 @@ const descriptionForAction = (action, locatorSchemaTypeDescription)=>{
63
63
  ${tab}${fields.join(`\n${tab}`)}
64
64
  `.trim();
65
65
  };
66
- async function systemPromptToTaskPlanning({ actionSpace, modelFamily, includeBbox, includeThought, includeSubGoals }) {
66
+ async function systemPromptToTaskPlanning({ actionSpace, modelFamily, includeBbox, includeThought, deepThink }) {
67
67
  const preferredLanguage = getPreferredLanguage();
68
68
  if (includeBbox && !modelFamily) throw new Error('modelFamily cannot be undefined when includeBbox is true. A valid modelFamily is required for bbox-based location.');
69
69
  const actionDescriptionList = actionSpace.map((action)=>descriptionForAction(action, vlLocateParam(includeBbox ? modelFamily : void 0)));
70
70
  const actionList = actionDescriptionList.join('\n');
71
- const shouldIncludeSubGoals = includeSubGoals ?? false;
72
71
  const locateExample1 = includeBbox ? `{
73
72
  "prompt": "Add to cart button for Sauce Labs Backpack",
74
73
  "bbox": [345, 442, 458, 483]
@@ -87,98 +86,26 @@ async function systemPromptToTaskPlanning({ actionSpace, modelFamily, includeBbo
87
86
  }` : `{
88
87
  "prompt": "Email input field in the registration form"
89
88
  }`;
90
- const step1Title = shouldIncludeSubGoals ? '## Step 1: Observe and Plan (related tags: <thought>, <update-plan-content>, <mark-sub-goal-done>)' : '## Step 1: Observe (related tags: <thought>)';
91
- const step1Description = shouldIncludeSubGoals ? "First, observe the current screenshot and previous logs, then break down the user's instruction into multiple high-level sub-goals. Update the status of sub-goals based on what you see in the current screenshot." : 'First, observe the current screenshot and previous logs to understand the current state.';
92
89
  const explicitInstructionRule = 'CRITICAL - Following Explicit Instructions: When the user gives you specific operation steps (not high-level goals), you MUST execute ONLY those exact steps - nothing more, nothing less. Do NOT add extra actions even if they seem logical. For example: "fill out the form" means only fill fields, do NOT submit; "click the button" means only click, do NOT wait for page load or verify results; "type \'hello\'" means only type, do NOT press Enter.';
93
- const thoughtTagDescription = shouldIncludeSubGoals ? `REQUIRED: You MUST always output the <thought> tag. Never skip it.
90
+ const thoughtTagDescription = `REQUIRED: You MUST always output the <thought> tag. Never skip it.
94
91
 
95
- Include your thought process in the <thought> tag. It should answer: What is the user's requirement? What is the current state based on the screenshot? Are all sub-goals completed? If not, what should be the next action? Write your thoughts naturally without numbering or section headers.
96
-
97
- ${explicitInstructionRule}` : `REQUIRED: You MUST always output the <thought> tag. Never skip it.
98
-
99
- Include your thought process in the <thought> tag. It should answer: What is the current state based on the screenshot? What should be the next action? Write your thoughts naturally without numbering or section headers.
92
+ Include your thought process in the <thought> tag. It should answer: What is the user's requirement? What is the current state based on the screenshot? What should be the next action? Write your thoughts naturally without numbering or section headers.
100
93
 
101
94
  ${explicitInstructionRule}`;
102
- const subGoalTags = shouldIncludeSubGoals ? `
103
-
104
- * <update-plan-content> tag
105
-
106
- Use this structure to give or update your plan:
107
-
108
- <update-plan-content>
109
- <sub-goal index="1" status="finished|pending">sub goal description</sub-goal>
110
- <sub-goal index="2" status="finished|pending">sub goal description</sub-goal>
111
- ...
112
- </update-plan-content>
113
-
114
- * <mark-sub-goal-done> tag
115
-
116
- Use this structure to mark a sub-goal as done:
117
-
118
- <mark-sub-goal-done>
119
- <sub-goal index="1" status="finished" />
120
- </mark-sub-goal-done>
121
-
122
- IMPORTANT: You MUST only mark a sub-goal as "finished" AFTER you have confirmed the task is actually completed by observing the result in the screenshot. Do NOT mark a sub-goal as done just because you expect the next action will complete it. Wait until you see visual confirmation in the screenshot that the sub-goal has been achieved.
123
-
124
- * Note
125
-
126
- During execution, you can call <update-plan-content> at any time to update the plan based on the latest screenshot and completed sub-goals.
127
-
128
- ### Example
129
-
130
- If the user wants to "log in to a system using username and password, complete all to-do items, and submit a registration form", you can break it down into the following sub-goals:
131
-
132
- <thought>...</thought>
133
- <update-plan-content>
134
- <sub-goal index="1" status="pending">Log in to the system</sub-goal>
135
- <sub-goal index="2" status="pending">Complete all to-do items</sub-goal>
136
- <sub-goal index="3" status="pending">Submit the registration form</sub-goal>
137
- </update-plan-content>
138
-
139
- After logging in and seeing the to-do items, you can mark the sub-goal as done:
140
-
141
- <mark-sub-goal-done>
142
- <sub-goal index="1" status="finished" />
143
- </mark-sub-goal-done>
144
-
145
- At this point, the status of all sub-goals is:
146
-
147
- <update-plan-content>
148
- <sub-goal index="1" status="finished" />
149
- <sub-goal index="2" status="pending" />
150
- <sub-goal index="3" status="pending" />
151
- </update-plan-content>
152
-
153
- After some time, when the last sub-goal is also completed, you can mark it as done as well:
154
-
155
- <mark-sub-goal-done>
156
- <sub-goal index="3" status="finished" />
157
- </mark-sub-goal-done>` : '';
158
- const memoryStepNumber = 2;
159
- const checkGoalStepNumber = shouldIncludeSubGoals ? 3 : 2;
160
- const actionStepNumber = shouldIncludeSubGoals ? 4 : 3;
161
95
  return `
162
96
  Target: You are an expert to manipulate the UI to accomplish the user's instruction. User will give you an instruction, some screenshots, background knowledge and previous logs indicating what have been done. Your task is to accomplish the instruction by thinking through the path to complete the task and give the next action to execute.
163
97
 
164
- ${step1Title}
98
+ ## Step 1: Observe (related tags: <thought>)
165
99
 
166
- ${step1Description}
100
+ First, observe the current screenshot and previous logs to understand the current state.
167
101
 
168
102
  * <thought> tag (REQUIRED)
169
103
 
170
104
  ${thoughtTagDescription}
171
- ${subGoalTags}
172
- ${shouldIncludeSubGoals ? `
173
- ## Step ${memoryStepNumber}: Memory Data from Current Screenshot (related tags: <memory>)
174
105
 
175
- While observing the current screenshot, if you notice any information that might be needed in follow-up actions, record it here. The current screenshot will NOT be available in subsequent steps, so this memory is your only way to preserve essential information. Examples: extracted data, element states, content that needs to be referenced.
106
+ ## Step 2: Check if the Instruction is Fulfilled (related tags: <complete>)
176
107
 
177
- Don't use this tag if no information needs to be preserved.
178
- ` : ''}
179
- ## Step ${checkGoalStepNumber}: ${shouldIncludeSubGoals ? 'Check if Goal is Accomplished' : 'Check if the Instruction is Fulfilled'} (related tags: <complete>)
180
-
181
- ${shouldIncludeSubGoals ? 'Based on the current screenshot and the status of all sub-goals, determine' : 'Determine'} if the entire task is completed.
108
+ Determine if the entire task is completed.
182
109
 
183
110
  ### CRITICAL: The User's Instruction is the Supreme Authority
184
111
 
@@ -188,36 +115,36 @@ The user's instruction defines the EXACT scope of what you must accomplish. You
188
115
  - If the user gives you **explicit operation steps** (e.g., "click X", "type Y", "fill out the form"), treat them as exact commands. Execute ONLY those steps, nothing more.
189
116
  - If the user gives you a **high-level goal** (e.g., "log in to the system", "complete the purchase"), you may determine the necessary steps to achieve it.
190
117
 
191
- **What "${shouldIncludeSubGoals ? 'goal accomplished' : 'instruction fulfilled'}" means:**
192
- - The ${shouldIncludeSubGoals ? 'goal is accomplished' : 'instruction is fulfilled'} when you have done EXACTLY what the user asked - no extra steps, no assumptions.
118
+ **What "instruction fulfilled" means:**
119
+ - The instruction is fulfilled when you have done EXACTLY what the user asked - no extra steps, no assumptions.
193
120
  - Do NOT perform any action beyond the explicit instruction, even if it seems logical or helpful.
194
121
 
195
122
  **Examples - Explicit instructions (execute exactly, no extra steps):**
196
- - "fill out the form" → ${shouldIncludeSubGoals ? 'Goal accomplished' : 'Instruction fulfilled'} when all fields are filled. Do NOT submit the form.
197
- - "click the login button" → ${shouldIncludeSubGoals ? 'Goal accomplished' : 'Instruction fulfilled'} once clicked. Do NOT wait for page load or verify login success.
198
- - "type 'hello' in the search box" → ${shouldIncludeSubGoals ? 'Goal accomplished' : 'Instruction fulfilled'} when 'hello' is typed. Do NOT press Enter or trigger search.
199
- - "select the first item" → ${shouldIncludeSubGoals ? 'Goal accomplished' : 'Instruction fulfilled'} when selected. Do NOT proceed to checkout.
123
+ - "fill out the form" → Instruction fulfilled when all fields are filled. Do NOT submit the form.
124
+ - "click the login button" → Instruction fulfilled once clicked. Do NOT wait for page load or verify login success.
125
+ - "type 'hello' in the search box" → Instruction fulfilled when 'hello' is typed. Do NOT press Enter or trigger search.
126
+ - "select the first item" → Instruction fulfilled when selected. Do NOT proceed to checkout.
200
127
 
201
128
  **Special case - Assertion instructions:**
202
- - If the user's instruction includes an assertion (e.g., "verify that...", "check that...", "assert..."), and you observe from the screenshot that the assertion condition is NOT satisfied and cannot be satisfied, mark ${shouldIncludeSubGoals ? 'the goal' : 'it'} as failed (success="false").
129
+ - If the user's instruction includes an assertion (e.g., "verify that...", "check that...", "assert..."), and you observe from the screenshot that the assertion condition is NOT satisfied and cannot be satisfied, mark it as failed (success="false").
203
130
  - If the page is still loading (e.g., you see a loading spinner, skeleton screen, or progress bar), do NOT assert yet. Wait for the page to finish loading before evaluating the assertion.
204
- ${!shouldIncludeSubGoals ? `
131
+
205
132
  **Page navigation restriction:**
206
133
  - Unless the user's instruction explicitly asks you to click a link, jump to another page, or navigate to a URL, you MUST complete the task on the current page only.
207
134
  - Do NOT navigate away from the current page on your own initiative (e.g., do not click links that lead to other pages, do not use browser back/forward, do not open new URLs).
208
135
  - If the task cannot be accomplished on the current page and the user has not instructed you to navigate, report it as a failure (success="false") instead of attempting to navigate to other pages.
209
- ` : ''}
136
+
210
137
  ### Output Rules
211
138
 
212
- - If the task is NOT complete, skip this section and continue to Step ${actionStepNumber}.
139
+ - If the task is NOT complete, skip this section and continue to Step 3.
213
140
  - Use the <complete success="true|false">message</complete> tag to output the result if the goal is accomplished or failed.
214
- - the 'success' attribute is required. ${shouldIncludeSubGoals ? 'It means whether the expected goal is accomplished based on what you observe in the current screenshot. ' : ''}No matter what actions were executed or what errors occurred during execution, if the ${shouldIncludeSubGoals ? 'expected goal is accomplished' : 'instruction is fulfilled'}, set success="true". If the ${shouldIncludeSubGoals ? 'expected goal is not accomplished and cannot be accomplished' : 'instruction is not fulfilled and cannot be fulfilled'}, set success="false".
141
+ - the 'success' attribute is required. No matter what actions were executed or what errors occurred during execution, if the instruction is fulfilled, set success="true". If the instruction is not fulfilled and cannot be fulfilled, set success="false".
215
142
  - the 'message' is the information that will be provided to the user. If the user asks for a specific format, strictly follow that.
216
143
  - If you output <complete>, do NOT output <action-type> or <action-param-json>. The task ends here.
217
144
 
218
- ## Step ${actionStepNumber}: Determine Next Action (related tags: <log>, <action-type>, <action-param-json>, <error>)
145
+ ## Step 3: Determine Next Action (related tags: <log>, <action-type>, <action-param-json>, <error>)
219
146
 
220
- ONLY if the task is not complete: Think what the next action is according to the current screenshot${shouldIncludeSubGoals ? ' and the plan' : ''}.
147
+ ONLY if the task is not complete: Think what the next action is according to the current screenshot.
221
148
 
222
149
  - Don't give extra actions or plans beyond the instruction or the plan. For example, don't try to submit the form if the instruction is only to fill something.
223
150
  - Consider the current screenshot and give the action that is most likely to accomplish the instruction. For example, if the next step is to click a button but it's not visible in the screenshot, you should try to find it first instead of give a click action.
@@ -272,26 +199,15 @@ For example:
272
199
  Return in XML format following this decision flow:
273
200
 
274
201
  **Always include (REQUIRED):**
275
- <!-- Step 1: Observe${shouldIncludeSubGoals ? ' and Plan' : ''} -->
202
+ <!-- Step 1: Observe -->
276
203
  <thought>Your thought process here. NEVER skip this tag.</thought>
277
- ${shouldIncludeSubGoals ? `
278
- <!-- required when no update-plan-content is provided in the previous response -->
279
- <update-plan-content>...</update-plan-content>
280
-
281
- <!-- required when any sub-goal is completed -->
282
- <mark-sub-goal-done>
283
- <sub-goal index="1" status="finished" />
284
- </mark-sub-goal-done>
285
- ` : ''}${shouldIncludeSubGoals ? `
286
- <!-- Step ${memoryStepNumber}: Memory data from current screenshot if needed -->
287
- <memory>...</memory>
288
- ` : ''}
204
+
289
205
  **Then choose ONE of the following paths:**
290
206
 
291
- **Path A: If the ${shouldIncludeSubGoals ? 'goal is accomplished' : 'instruction is fulfilled'} or failed (Step ${checkGoalStepNumber})**
207
+ **Path A: If the instruction is fulfilled or failed (Step 2)**
292
208
  <complete success="true|false">...</complete>
293
209
 
294
- **Path B: If the ${shouldIncludeSubGoals ? 'goal is NOT complete' : 'instruction is NOT fulfilled'} yet (Step ${actionStepNumber})**
210
+ **Path B: If the instruction is NOT fulfilled yet (Step 3)**
295
211
  <!-- Determine next action -->
296
212
  <log>...</log>
297
213
  <action-type>...</action-type>
@@ -299,137 +215,7 @@ ${shouldIncludeSubGoals ? `
299
215
 
300
216
  <!-- OR if there's an error -->
301
217
  <error>...</error>
302
- ${shouldIncludeSubGoals ? `
303
- ## Multi-turn Conversation Example
304
-
305
- Below is an example of a multi-turn conversation for "fill out the registration form with name 'John' and email 'john@example.com', then return the filled email address":
306
-
307
- ### Turn 1 - Initial instruction
308
-
309
- **User message:**
310
- <user_instruction>fill out the registration form with name 'John' and email 'john@example.com', then return the filled email address</user_instruction>
311
-
312
- **Screenshot:** [Shows a registration form with empty Name and Email fields]
313
-
314
- **Your response:**
315
- <thought>The user wants me to fill out the registration form with specific values and return the email address. I can see the form has two fields: Name and Email. Both are currently empty. I'll break this down into sub-goals and start with the Name field. Note: The instruction is to fill the form only (not submit), and return the email at the end.</thought>
316
- <update-plan-content>
317
- <sub-goal index="1" status="pending">Fill in the Name field with 'John'</sub-goal>
318
- <sub-goal index="2" status="pending">Fill in the Email field with 'john@example.com'</sub-goal>
319
- <sub-goal index="3" status="pending">Return the filled email address</sub-goal>
320
- </update-plan-content>
321
- <log>Click on the Name field to start filling the form</log>
322
- <action-type>Tap</action-type>
323
- <action-param-json>
324
- {
325
- "locate": ${locateNameField}
326
- }
327
- </action-param-json>
328
218
 
329
- ### Turn 2 - After clicking Name field
330
-
331
- **User message:**
332
- The previous action has been executed, here is the latest screenshot. Please continue according to the instruction.
333
-
334
- Sub-goals:
335
- 1. Fill in the Name field with 'John' (running)
336
- 2. Fill in the Email field with 'john@example.com' (pending)
337
- 3. Return the filled email address (pending)
338
- Current sub-goal is: Fill in the Name field with 'John'
339
- Actions performed for current sub-goal:
340
- - Click on the Name field to start filling the form
341
-
342
- **Screenshot:** [Shows the form with Name field now focused/active]
343
-
344
- **Your response:**
345
- <thought>The Name field is now focused. I need to type 'John' into this field. Current sub-goal is running, will be completed after input.</thought>
346
- <log>Typing 'John' into the Name field</log>
347
- <action-type>Input</action-type>
348
- <action-param-json>
349
- {
350
- "value": "John"
351
- }
352
- </action-param-json>
353
-
354
- ### Turn 3 - After entering name
355
-
356
- **User message:**
357
- The previous action has been executed, here is the latest screenshot. Please continue according to the instruction.
358
-
359
- Sub-goals:
360
- 1. Fill in the Name field with 'John' (running)
361
- 2. Fill in the Email field with 'john@example.com' (pending)
362
- 3. Return the filled email address (pending)
363
- Current sub-goal is: Fill in the Name field with 'John'
364
- Actions performed for current sub-goal:
365
- - Click on the Name field to start filling the form
366
- - Typing 'John' into the Name field
367
-
368
- **Screenshot:** [Shows the form with Name field containing 'John']
369
-
370
- **Your response:**
371
- <thought>The Name field now shows 'John'. Sub-goal 1 is complete. Next, I need to fill the Email field for sub-goal 2.</thought>
372
- <mark-sub-goal-done>
373
- <sub-goal index="1" status="finished" />
374
- </mark-sub-goal-done>
375
- <memory>Name field has been filled with 'John'</memory>
376
- <log>Moving to the Email field</log>
377
- <action-type>Tap</action-type>
378
- <action-param-json>
379
- {
380
- "locate": ${locateEmailField}
381
- }
382
- </action-param-json>
383
-
384
- ### Turn 4 - After clicking Email field
385
-
386
- **User message:**
387
- The previous action has been executed, here is the latest screenshot. Please continue according to the instruction.
388
-
389
- Sub-goals:
390
- 1. Fill in the Name field with 'John' (finished)
391
- 2. Fill in the Email field with 'john@example.com' (running)
392
- 3. Return the filled email address (pending)
393
- Current sub-goal is: Fill in the Email field with 'john@example.com'
394
- Actions performed for current sub-goal:
395
- - Moving to the Email field
396
-
397
- **Screenshot:** [Shows the form with Name='John' and Email field focused]
398
-
399
- **Your response:**
400
- <thought>The Email field is now focused. I'll enter 'john@example.com'. After this, sub-goal 2 will be complete and my task will be done.</thought>
401
- <log>Typing email address into the Email field</log>
402
- <action-type>Input</action-type>
403
- <action-param-json>
404
- {
405
- "value": "john@example.com"
406
- }
407
- </action-param-json>
408
-
409
- ### Turn 5 - After entering email (Goal accomplished)
410
-
411
- **User message:**
412
- The previous action has been executed, here is the latest screenshot. Please continue according to the instruction.
413
-
414
- Sub-goals:
415
- 1. Fill in the Name field with 'John' (finished)
416
- 2. Fill in the Email field with 'john@example.com' (running)
417
- 3. Return the filled email address (pending)
418
- Current sub-goal is: Fill in the Email field with 'john@example.com'
419
- Actions performed for current sub-goal:
420
- - Moving to the Email field
421
- - Typing email address into the Email field
422
-
423
- **Screenshot:** [Shows the form with Name='John' and Email='john@example.com']
424
-
425
- **Your response:**
426
- <thought>Both fields are now filled: Name shows 'John' and Email shows 'john@example.com'. Sub-goal 2 is complete. The user asked me to return the filled email address, so I need to include 'john@example.com' in my response. All sub-goals are now finished.</thought>
427
- <mark-sub-goal-done>
428
- <sub-goal index="2" status="finished" />
429
- <sub-goal index="3" status="finished" />
430
- </mark-sub-goal-done>
431
- <complete success="true">john@example.com</complete>
432
- ` : `
433
219
  ## Multi-turn Conversation Example
434
220
 
435
221
  Below is an example of a multi-turn conversation for "fill out the registration form with name 'John' and email 'john@example.com', then return the filled email address":
@@ -512,7 +298,7 @@ The previous action has been executed, here is the latest screenshot. Please con
512
298
  **Your response:**
513
299
  <thought>Both fields are now filled: Name shows 'John' and Email shows 'john@example.com'. The user asked me to return the filled email address, so I should include 'john@example.com' in my response. The instruction has been fulfilled.</thought>
514
300
  <complete success="true">john@example.com</complete>
515
- `}`;
301
+ `;
516
302
  }
517
303
  export { descriptionForAction, systemPromptToTaskPlanning };
518
304