@midscene/core 1.0.1-beta-20251021060907.0 → 1.0.1-beta-20251022061922.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.
- package/dist/es/agent/agent.mjs.map +1 -1
- package/dist/es/agent/execution-session.mjs.map +1 -1
- package/dist/es/agent/task-builder.mjs +258 -246
- package/dist/es/agent/task-builder.mjs.map +1 -1
- package/dist/es/agent/tasks.mjs +17 -19
- package/dist/es/agent/tasks.mjs.map +1 -1
- package/dist/es/agent/utils.mjs +1 -1
- package/dist/es/ai-model/llm-planning.mjs +5 -6
- package/dist/es/ai-model/llm-planning.mjs.map +1 -1
- package/dist/es/ai-model/prompt/llm-planning.mjs +15 -0
- package/dist/es/ai-model/prompt/llm-planning.mjs.map +1 -1
- package/dist/es/index.mjs +1 -1
- package/dist/es/index.mjs.map +1 -1
- package/dist/es/task-runner.mjs +13 -1
- package/dist/es/task-runner.mjs.map +1 -1
- package/dist/es/types.mjs.map +1 -1
- package/dist/es/utils.mjs +2 -2
- package/dist/es/yaml/player.mjs.map +1 -1
- package/dist/lib/agent/agent.js.map +1 -1
- package/dist/lib/agent/execution-session.js.map +1 -1
- package/dist/lib/agent/task-builder.js +258 -246
- package/dist/lib/agent/task-builder.js.map +1 -1
- package/dist/lib/agent/tasks.js +17 -19
- package/dist/lib/agent/tasks.js.map +1 -1
- package/dist/lib/agent/utils.js +1 -1
- package/dist/lib/ai-model/llm-planning.js +5 -6
- package/dist/lib/ai-model/llm-planning.js.map +1 -1
- package/dist/lib/ai-model/prompt/llm-planning.js +15 -0
- package/dist/lib/ai-model/prompt/llm-planning.js.map +1 -1
- package/dist/lib/index.js +1 -1
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/task-runner.js +13 -1
- package/dist/lib/task-runner.js.map +1 -1
- package/dist/lib/types.js.map +1 -1
- package/dist/lib/utils.js +2 -2
- package/dist/lib/yaml/player.js.map +1 -1
- package/dist/types/agent/task-builder.d.ts +11 -1
- package/dist/types/agent/tasks.d.ts +8 -4
- package/dist/types/agent/utils.d.ts +1 -0
- package/dist/types/device/index.d.ts +20 -20
- package/dist/types/index.d.ts +1 -1
- package/dist/types/task-runner.d.ts +1 -0
- package/dist/types/types.d.ts +6 -10
- package/package.json +3 -3
|
@@ -27,11 +27,11 @@ __webpack_require__.d(__webpack_exports__, {
|
|
|
27
27
|
locatePlanForLocate: ()=>locatePlanForLocate,
|
|
28
28
|
TaskBuilder: ()=>TaskBuilder
|
|
29
29
|
});
|
|
30
|
+
const index_js_namespaceObject = require("../ai-model/index.js");
|
|
30
31
|
const external_types_js_namespaceObject = require("../types.js");
|
|
31
32
|
const external_utils_js_namespaceObject = require("../utils.js");
|
|
32
33
|
const logger_namespaceObject = require("@midscene/shared/logger");
|
|
33
34
|
const utils_namespaceObject = require("@midscene/shared/utils");
|
|
34
|
-
const index_js_namespaceObject = require("../ai-model/index.js");
|
|
35
35
|
const external_utils_js_namespaceObject_1 = require("./utils.js");
|
|
36
36
|
function _define_property(obj, key, value) {
|
|
37
37
|
if (key in obj) Object.defineProperty(obj, key, {
|
|
@@ -60,263 +60,275 @@ class TaskBuilder {
|
|
|
60
60
|
async build(plans, modelConfig, options) {
|
|
61
61
|
const tasks = [];
|
|
62
62
|
const cacheable = null == options ? void 0 : options.cacheable;
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
...detailedLocateParam,
|
|
69
|
-
cacheable
|
|
70
|
-
};
|
|
71
|
-
const taskFind = {
|
|
72
|
-
type: 'Insight',
|
|
73
|
-
subType: 'Locate',
|
|
74
|
-
param: detailedLocateParam,
|
|
75
|
-
thought: plan.thought,
|
|
76
|
-
executor: async (param, taskContext)=>{
|
|
77
|
-
var _this_taskCache, _locateCacheRecord_cacheContent;
|
|
78
|
-
const { task } = taskContext;
|
|
79
|
-
let { uiContext } = taskContext;
|
|
80
|
-
(0, utils_namespaceObject.assert)((null == param ? void 0 : param.prompt) || (null == param ? void 0 : param.id) || (null == param ? void 0 : param.bbox), `No prompt or id or position or bbox to locate, param=${JSON.stringify(param)}`);
|
|
81
|
-
if (!uiContext) uiContext = await this.insight.contextRetrieverFn();
|
|
82
|
-
(0, utils_namespaceObject.assert)(uiContext, 'uiContext is required for Insight task');
|
|
83
|
-
let locateDump;
|
|
84
|
-
let locateResult;
|
|
85
|
-
const applyDump = (dump)=>{
|
|
86
|
-
var _dump_taskInfo, _dump_taskInfo1;
|
|
87
|
-
if (!dump) return;
|
|
88
|
-
locateDump = dump;
|
|
89
|
-
task.log = {
|
|
90
|
-
dump
|
|
91
|
-
};
|
|
92
|
-
task.usage = null == (_dump_taskInfo = dump.taskInfo) ? void 0 : _dump_taskInfo.usage;
|
|
93
|
-
if (null == (_dump_taskInfo1 = dump.taskInfo) ? void 0 : _dump_taskInfo1.searchAreaUsage) task.searchAreaUsage = dump.taskInfo.searchAreaUsage;
|
|
94
|
-
};
|
|
95
|
-
const elementFromXpath = param.xpath && this.interface.getElementInfoByXpath ? await this.interface.getElementInfoByXpath(param.xpath) : void 0;
|
|
96
|
-
const userExpectedPathHitFlag = !!elementFromXpath;
|
|
97
|
-
const cachePrompt = param.prompt;
|
|
98
|
-
const locateCacheRecord = null == (_this_taskCache = this.taskCache) ? void 0 : _this_taskCache.matchLocateCache(cachePrompt);
|
|
99
|
-
const cacheEntry = null == locateCacheRecord ? void 0 : null == (_locateCacheRecord_cacheContent = locateCacheRecord.cacheContent) ? void 0 : _locateCacheRecord_cacheContent.cache;
|
|
100
|
-
const elementFromCache = userExpectedPathHitFlag ? null : await (0, external_utils_js_namespaceObject_1.matchElementFromCache)({
|
|
101
|
-
taskCache: this.taskCache,
|
|
102
|
-
interfaceInstance: this.interface
|
|
103
|
-
}, cacheEntry, cachePrompt, param.cacheable);
|
|
104
|
-
const cacheHitFlag = !!elementFromCache;
|
|
105
|
-
const elementFromPlan = userExpectedPathHitFlag || cacheHitFlag ? void 0 : (0, external_utils_js_namespaceObject_1.matchElementFromPlan)(param, uiContext.tree);
|
|
106
|
-
const planHitFlag = !!elementFromPlan;
|
|
107
|
-
let elementFromAiLocate;
|
|
108
|
-
if (!userExpectedPathHitFlag && !cacheHitFlag && !planHitFlag) try {
|
|
109
|
-
locateResult = await this.insight.locate(param, {
|
|
110
|
-
context: uiContext
|
|
111
|
-
}, modelConfig);
|
|
112
|
-
applyDump(locateResult.dump);
|
|
113
|
-
elementFromAiLocate = locateResult.element;
|
|
114
|
-
} catch (error) {
|
|
115
|
-
if (error instanceof external_types_js_namespaceObject.InsightError) applyDump(error.dump);
|
|
116
|
-
throw error;
|
|
117
|
-
}
|
|
118
|
-
const aiLocateHitFlag = !!elementFromAiLocate;
|
|
119
|
-
const element = elementFromXpath || elementFromCache || elementFromPlan || elementFromAiLocate;
|
|
120
|
-
let currentCacheEntry;
|
|
121
|
-
if (element && this.taskCache && !cacheHitFlag && (null == param ? void 0 : param.cacheable) !== false) if (this.interface.cacheFeatureForRect) try {
|
|
122
|
-
const feature = await this.interface.cacheFeatureForRect(element.rect, void 0 !== element.isOrderSensitive ? {
|
|
123
|
-
_orderSensitive: element.isOrderSensitive
|
|
124
|
-
} : void 0);
|
|
125
|
-
if (feature && Object.keys(feature).length > 0) {
|
|
126
|
-
debug('update cache, prompt: %s, cache: %o', cachePrompt, feature);
|
|
127
|
-
currentCacheEntry = feature;
|
|
128
|
-
this.taskCache.updateOrAppendCacheRecord({
|
|
129
|
-
type: 'locate',
|
|
130
|
-
prompt: cachePrompt,
|
|
131
|
-
cache: feature
|
|
132
|
-
}, locateCacheRecord);
|
|
133
|
-
} else debug('no cache data returned, skip cache update, prompt: %s', cachePrompt);
|
|
134
|
-
} catch (error) {
|
|
135
|
-
debug('cacheFeatureForRect failed: %s', error);
|
|
136
|
-
}
|
|
137
|
-
else debug('cacheFeatureForRect is not supported, skip cache update');
|
|
138
|
-
if (!element) {
|
|
139
|
-
if (locateDump) throw new external_types_js_namespaceObject.InsightError(`Element not found: ${param.prompt}`, locateDump);
|
|
140
|
-
throw new Error(`Element not found: ${param.prompt}`);
|
|
141
|
-
}
|
|
142
|
-
let hitBy;
|
|
143
|
-
if (userExpectedPathHitFlag) hitBy = {
|
|
144
|
-
from: 'User expected path',
|
|
145
|
-
context: {
|
|
146
|
-
xpath: param.xpath
|
|
147
|
-
}
|
|
148
|
-
};
|
|
149
|
-
else if (cacheHitFlag) hitBy = {
|
|
150
|
-
from: 'Cache',
|
|
151
|
-
context: {
|
|
152
|
-
cacheEntry,
|
|
153
|
-
cacheToSave: currentCacheEntry
|
|
154
|
-
}
|
|
155
|
-
};
|
|
156
|
-
else if (planHitFlag) hitBy = {
|
|
157
|
-
from: 'Planning',
|
|
158
|
-
context: {
|
|
159
|
-
id: null == elementFromPlan ? void 0 : elementFromPlan.id,
|
|
160
|
-
bbox: null == elementFromPlan ? void 0 : elementFromPlan.bbox
|
|
161
|
-
}
|
|
162
|
-
};
|
|
163
|
-
else if (aiLocateHitFlag) hitBy = {
|
|
164
|
-
from: 'AI model',
|
|
165
|
-
context: {
|
|
166
|
-
prompt: param.prompt
|
|
167
|
-
}
|
|
168
|
-
};
|
|
169
|
-
null == onResult || onResult(element);
|
|
170
|
-
return {
|
|
171
|
-
output: {
|
|
172
|
-
element
|
|
173
|
-
},
|
|
174
|
-
uiContext,
|
|
175
|
-
hitBy
|
|
176
|
-
};
|
|
177
|
-
}
|
|
178
|
-
};
|
|
179
|
-
return taskFind;
|
|
63
|
+
const context = {
|
|
64
|
+
tasks,
|
|
65
|
+
modelConfig,
|
|
66
|
+
cacheable,
|
|
67
|
+
subTask: !!(null == options ? void 0 : options.subTask)
|
|
180
68
|
};
|
|
69
|
+
const planHandlers = new Map([
|
|
70
|
+
[
|
|
71
|
+
'Locate',
|
|
72
|
+
(plan)=>this.handleLocatePlan(plan, context)
|
|
73
|
+
],
|
|
74
|
+
[
|
|
75
|
+
'Finished',
|
|
76
|
+
(plan)=>this.handleFinishedPlan(plan, context)
|
|
77
|
+
],
|
|
78
|
+
[
|
|
79
|
+
'Sleep',
|
|
80
|
+
(plan)=>this.handleSleepPlan(plan, context)
|
|
81
|
+
]
|
|
82
|
+
]);
|
|
83
|
+
const defaultHandler = (plan)=>this.handleActionPlan(plan, context);
|
|
181
84
|
for (const plan of plans){
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
85
|
+
const handler = planHandlers.get(plan.type) ?? defaultHandler;
|
|
86
|
+
await handler(plan);
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
tasks
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
handleFinishedPlan(plan, context) {
|
|
93
|
+
const taskActionFinished = {
|
|
94
|
+
type: 'Action',
|
|
95
|
+
subType: 'Finished',
|
|
96
|
+
param: null,
|
|
97
|
+
thought: plan.thought,
|
|
98
|
+
locate: plan.locate,
|
|
99
|
+
subTask: context.subTask || void 0,
|
|
100
|
+
executor: async ()=>{}
|
|
101
|
+
};
|
|
102
|
+
context.tasks.push(taskActionFinished);
|
|
103
|
+
}
|
|
104
|
+
handleSleepPlan(plan, context) {
|
|
105
|
+
const sleepTask = this.createSleepTask(plan.param, {
|
|
106
|
+
thought: plan.thought,
|
|
107
|
+
locate: plan.locate
|
|
108
|
+
});
|
|
109
|
+
if (context.subTask) sleepTask.subTask = true;
|
|
110
|
+
context.tasks.push(sleepTask);
|
|
111
|
+
}
|
|
112
|
+
createSleepTask(param, meta) {
|
|
113
|
+
return {
|
|
114
|
+
type: 'Action',
|
|
115
|
+
subType: 'Sleep',
|
|
116
|
+
param,
|
|
117
|
+
thought: null == meta ? void 0 : meta.thought,
|
|
118
|
+
locate: (null == meta ? void 0 : meta.locate) ?? null,
|
|
119
|
+
executor: async (taskParam)=>{
|
|
120
|
+
await (0, external_utils_js_namespaceObject.sleep)((null == taskParam ? void 0 : taskParam.timeMs) || 3000);
|
|
218
121
|
}
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
async handleLocatePlan(plan, context) {
|
|
125
|
+
if (!plan.locate || null === plan.locate) return void debug('Locate action with id is null, will be ignored', plan);
|
|
126
|
+
const taskLocate = this.createLocateTask(plan, plan.locate, context);
|
|
127
|
+
context.tasks.push(taskLocate);
|
|
128
|
+
}
|
|
129
|
+
async handleActionPlan(plan, context) {
|
|
130
|
+
const planType = plan.type;
|
|
131
|
+
const actionSpace = await this.interface.actionSpace();
|
|
132
|
+
const action = actionSpace.find((item)=>item.name === planType);
|
|
133
|
+
const param = plan.param;
|
|
134
|
+
if (!action) throw new Error(`Action type '${planType}' not found`);
|
|
135
|
+
const locateFields = action ? (0, index_js_namespaceObject.findAllMidsceneLocatorField)(action.paramSchema) : [];
|
|
136
|
+
const requiredLocateFields = action ? (0, index_js_namespaceObject.findAllMidsceneLocatorField)(action.paramSchema, true) : [];
|
|
137
|
+
locateFields.forEach((field)=>{
|
|
138
|
+
if (param[field]) {
|
|
139
|
+
const locatePlan = locatePlanForLocate(param[field]);
|
|
140
|
+
debug('will prepend locate param for field', `action.type=${planType}`, `param=${JSON.stringify(param[field])}`, `locatePlan=${JSON.stringify(locatePlan)}`);
|
|
141
|
+
const locateTask = this.createLocateTask(locatePlan, param[field], context, (result)=>{
|
|
142
|
+
param[field] = result;
|
|
143
|
+
});
|
|
144
|
+
context.tasks.push(locateTask);
|
|
145
|
+
} else {
|
|
146
|
+
(0, utils_namespaceObject.assert)(!requiredLocateFields.includes(field), `Required locate field '${field}' is not provided for action ${planType}`);
|
|
147
|
+
debug(`field '${field}' is not provided for action ${planType}`);
|
|
232
148
|
}
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
149
|
+
});
|
|
150
|
+
const task = {
|
|
151
|
+
type: 'Action',
|
|
152
|
+
subType: planType,
|
|
153
|
+
thought: plan.thought,
|
|
154
|
+
param: plan.param,
|
|
155
|
+
subTask: context.subTask || void 0,
|
|
156
|
+
executor: async (param, taskContext)=>{
|
|
157
|
+
var _taskContext_element;
|
|
158
|
+
debug('executing action', planType, param, `taskContext.element.center: ${null == (_taskContext_element = taskContext.element) ? void 0 : _taskContext_element.center}`);
|
|
159
|
+
const uiContext = taskContext.uiContext;
|
|
160
|
+
(0, utils_namespaceObject.assert)(uiContext, 'uiContext is required for Action task');
|
|
161
|
+
requiredLocateFields.forEach((field)=>{
|
|
162
|
+
(0, utils_namespaceObject.assert)(param[field], `field '${field}' is required for action ${planType} but not provided. Cannot execute action ${planType}.`);
|
|
163
|
+
});
|
|
164
|
+
try {
|
|
165
|
+
await Promise.all([
|
|
166
|
+
(async ()=>{
|
|
167
|
+
if (this.interface.beforeInvokeAction) {
|
|
168
|
+
debug('will call "beforeInvokeAction" for interface');
|
|
169
|
+
await this.interface.beforeInvokeAction(action.name, param);
|
|
170
|
+
debug('called "beforeInvokeAction" for interface');
|
|
171
|
+
}
|
|
172
|
+
})(),
|
|
173
|
+
(0, external_utils_js_namespaceObject.sleep)(200)
|
|
174
|
+
]);
|
|
175
|
+
} catch (originalError) {
|
|
176
|
+
const originalMessage = (null == originalError ? void 0 : originalError.message) || String(originalError);
|
|
177
|
+
throw new Error(`error in running beforeInvokeAction for ${action.name}: ${originalMessage}`, {
|
|
178
|
+
cause: originalError
|
|
246
179
|
});
|
|
247
|
-
tasks.push(locateTask);
|
|
248
|
-
} else {
|
|
249
|
-
(0, utils_namespaceObject.assert)(!requiredLocateFields.includes(field), `Required locate field '${field}' is not provided for action ${planType}`);
|
|
250
|
-
debug(`field '${field}' is not provided for action ${planType}`);
|
|
251
180
|
}
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
param: plan.param,
|
|
258
|
-
executor: async (param, taskContext)=>{
|
|
259
|
-
var _taskContext_element;
|
|
260
|
-
debug('executing action', planType, param, `taskContext.element.center: ${null == (_taskContext_element = taskContext.element) ? void 0 : _taskContext_element.center}`);
|
|
261
|
-
const uiContext = taskContext.uiContext;
|
|
262
|
-
(0, utils_namespaceObject.assert)(uiContext, 'uiContext is required for Action task');
|
|
263
|
-
requiredLocateFields.forEach((field)=>{
|
|
264
|
-
(0, utils_namespaceObject.assert)(param[field], `field '${field}' is required for action ${planType} but not provided. Cannot execute action ${planType}.`);
|
|
181
|
+
if (action.paramSchema) try {
|
|
182
|
+
param = (0, index_js_namespaceObject.parseActionParam)(param, action.paramSchema);
|
|
183
|
+
} catch (error) {
|
|
184
|
+
throw new Error(`Invalid parameters for action ${action.name}: ${error.message}\nParameters: ${JSON.stringify(param)}`, {
|
|
185
|
+
cause: error
|
|
265
186
|
});
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
]);
|
|
277
|
-
} catch (originalError) {
|
|
278
|
-
const originalMessage = (null == originalError ? void 0 : originalError.message) || String(originalError);
|
|
279
|
-
throw new Error(`error in running beforeInvokeAction for ${action.name}: ${originalMessage}`, {
|
|
280
|
-
cause: originalError
|
|
281
|
-
});
|
|
282
|
-
}
|
|
283
|
-
if (action.paramSchema) try {
|
|
284
|
-
param = (0, index_js_namespaceObject.parseActionParam)(param, action.paramSchema);
|
|
285
|
-
} catch (error) {
|
|
286
|
-
throw new Error(`Invalid parameters for action ${action.name}: ${error.message}\nParameters: ${JSON.stringify(param)}`, {
|
|
287
|
-
cause: error
|
|
288
|
-
});
|
|
187
|
+
}
|
|
188
|
+
debug('calling action', action.name);
|
|
189
|
+
const actionFn = action.call.bind(this.interface);
|
|
190
|
+
await actionFn(param, taskContext);
|
|
191
|
+
debug('called action', action.name);
|
|
192
|
+
try {
|
|
193
|
+
if (this.interface.afterInvokeAction) {
|
|
194
|
+
debug('will call "afterInvokeAction" for interface');
|
|
195
|
+
await this.interface.afterInvokeAction(action.name, param);
|
|
196
|
+
debug('called "afterInvokeAction" for interface');
|
|
289
197
|
}
|
|
290
|
-
|
|
291
|
-
const
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
const originalMessage = (null == originalError ? void 0 : originalError.message) || String(originalError);
|
|
302
|
-
throw new Error(`error in running afterInvokeAction for ${action.name}: ${originalMessage}`, {
|
|
303
|
-
cause: originalError
|
|
304
|
-
});
|
|
198
|
+
} catch (originalError) {
|
|
199
|
+
const originalMessage = (null == originalError ? void 0 : originalError.message) || String(originalError);
|
|
200
|
+
throw new Error(`error in running afterInvokeAction for ${action.name}: ${originalMessage}`, {
|
|
201
|
+
cause: originalError
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
return {
|
|
205
|
+
output: {
|
|
206
|
+
success: true,
|
|
207
|
+
action: planType,
|
|
208
|
+
param: param
|
|
305
209
|
}
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
context.tasks.push(task);
|
|
214
|
+
}
|
|
215
|
+
createLocateTask(plan, detailedLocateParam, context, onResult) {
|
|
216
|
+
const { cacheable, modelConfig } = context;
|
|
217
|
+
let locateParam = detailedLocateParam;
|
|
218
|
+
if ('string' == typeof locateParam) locateParam = {
|
|
219
|
+
prompt: locateParam
|
|
220
|
+
};
|
|
221
|
+
if (void 0 !== cacheable) locateParam = {
|
|
222
|
+
...locateParam,
|
|
223
|
+
cacheable
|
|
224
|
+
};
|
|
225
|
+
const taskFind = {
|
|
226
|
+
type: 'Insight',
|
|
227
|
+
subType: 'Locate',
|
|
228
|
+
subTask: context.subTask || void 0,
|
|
229
|
+
param: locateParam,
|
|
230
|
+
thought: plan.thought,
|
|
231
|
+
executor: async (param, taskContext)=>{
|
|
232
|
+
var _this_taskCache, _locateCacheRecord_cacheContent;
|
|
233
|
+
const { task } = taskContext;
|
|
234
|
+
let { uiContext } = taskContext;
|
|
235
|
+
(0, utils_namespaceObject.assert)((null == param ? void 0 : param.prompt) || (null == param ? void 0 : param.id) || (null == param ? void 0 : param.bbox), `No prompt or id or position or bbox to locate, param=${JSON.stringify(param)}`);
|
|
236
|
+
if (!uiContext) uiContext = await this.insight.contextRetrieverFn();
|
|
237
|
+
(0, utils_namespaceObject.assert)(uiContext, 'uiContext is required for Insight task');
|
|
238
|
+
let locateDump;
|
|
239
|
+
let locateResult;
|
|
240
|
+
const applyDump = (dump)=>{
|
|
241
|
+
var _dump_taskInfo, _dump_taskInfo1;
|
|
242
|
+
if (!dump) return;
|
|
243
|
+
locateDump = dump;
|
|
244
|
+
task.log = {
|
|
245
|
+
dump
|
|
312
246
|
};
|
|
247
|
+
task.usage = null == (_dump_taskInfo = dump.taskInfo) ? void 0 : _dump_taskInfo.usage;
|
|
248
|
+
if (null == (_dump_taskInfo1 = dump.taskInfo) ? void 0 : _dump_taskInfo1.searchAreaUsage) task.searchAreaUsage = dump.taskInfo.searchAreaUsage;
|
|
249
|
+
};
|
|
250
|
+
let elementFromXpath;
|
|
251
|
+
if (param.xpath && this.interface.rectMatchesCacheFeature) elementFromXpath = await this.interface.rectMatchesCacheFeature({
|
|
252
|
+
xpaths: [
|
|
253
|
+
param.xpath
|
|
254
|
+
]
|
|
255
|
+
});
|
|
256
|
+
const userExpectedPathHitFlag = !!elementFromXpath;
|
|
257
|
+
const cachePrompt = param.prompt;
|
|
258
|
+
const locateCacheRecord = null == (_this_taskCache = this.taskCache) ? void 0 : _this_taskCache.matchLocateCache(cachePrompt);
|
|
259
|
+
const cacheEntry = null == locateCacheRecord ? void 0 : null == (_locateCacheRecord_cacheContent = locateCacheRecord.cacheContent) ? void 0 : _locateCacheRecord_cacheContent.cache;
|
|
260
|
+
const elementFromCache = userExpectedPathHitFlag ? null : await (0, external_utils_js_namespaceObject_1.matchElementFromCache)({
|
|
261
|
+
taskCache: this.taskCache,
|
|
262
|
+
interfaceInstance: this.interface
|
|
263
|
+
}, cacheEntry, cachePrompt, param.cacheable);
|
|
264
|
+
const cacheHitFlag = !!elementFromCache;
|
|
265
|
+
const elementFromPlan = userExpectedPathHitFlag || cacheHitFlag ? void 0 : (0, external_utils_js_namespaceObject_1.matchElementFromPlan)(param, uiContext.tree);
|
|
266
|
+
const planHitFlag = !!elementFromPlan;
|
|
267
|
+
let elementFromAiLocate;
|
|
268
|
+
if (!userExpectedPathHitFlag && !cacheHitFlag && !planHitFlag) try {
|
|
269
|
+
locateResult = await this.insight.locate(param, {
|
|
270
|
+
context: uiContext
|
|
271
|
+
}, modelConfig);
|
|
272
|
+
applyDump(locateResult.dump);
|
|
273
|
+
elementFromAiLocate = locateResult.element;
|
|
274
|
+
} catch (error) {
|
|
275
|
+
if (error instanceof external_types_js_namespaceObject.InsightError) applyDump(error.dump);
|
|
276
|
+
throw error;
|
|
313
277
|
}
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
278
|
+
const element = elementFromXpath || elementFromCache || elementFromPlan || elementFromAiLocate;
|
|
279
|
+
let currentCacheEntry;
|
|
280
|
+
if (element && this.taskCache && !cacheHitFlag && (null == param ? void 0 : param.cacheable) !== false) if (this.interface.cacheFeatureForRect) try {
|
|
281
|
+
const feature = await this.interface.cacheFeatureForRect(element.rect, void 0 !== element.isOrderSensitive ? {
|
|
282
|
+
_orderSensitive: element.isOrderSensitive
|
|
283
|
+
} : void 0);
|
|
284
|
+
if (feature && Object.keys(feature).length > 0) {
|
|
285
|
+
debug('update cache, prompt: %s, cache: %o', cachePrompt, feature);
|
|
286
|
+
currentCacheEntry = feature;
|
|
287
|
+
this.taskCache.updateOrAppendCacheRecord({
|
|
288
|
+
type: 'locate',
|
|
289
|
+
prompt: cachePrompt,
|
|
290
|
+
cache: feature
|
|
291
|
+
}, locateCacheRecord);
|
|
292
|
+
} else debug('no cache data returned, skip cache update, prompt: %s', cachePrompt);
|
|
293
|
+
} catch (error) {
|
|
294
|
+
debug('cacheFeatureForRect failed: %s', error);
|
|
295
|
+
}
|
|
296
|
+
else debug('cacheFeatureForRect is not supported, skip cache update');
|
|
297
|
+
if (!element) {
|
|
298
|
+
if (locateDump) throw new external_types_js_namespaceObject.InsightError(`Element not found: ${param.prompt}`, locateDump);
|
|
299
|
+
throw new Error(`Element not found: ${param.prompt}`);
|
|
300
|
+
}
|
|
301
|
+
let hitBy;
|
|
302
|
+
if (userExpectedPathHitFlag) hitBy = {
|
|
303
|
+
from: 'User expected path',
|
|
304
|
+
context: {
|
|
305
|
+
xpath: param.xpath
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
else if (cacheHitFlag) hitBy = {
|
|
309
|
+
from: 'Cache',
|
|
310
|
+
context: {
|
|
311
|
+
cacheEntry,
|
|
312
|
+
cacheToSave: currentCacheEntry
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
else if (planHitFlag) hitBy = {
|
|
316
|
+
from: 'Planning',
|
|
317
|
+
context: {
|
|
318
|
+
id: null == elementFromPlan ? void 0 : elementFromPlan.id,
|
|
319
|
+
bbox: null == elementFromPlan ? void 0 : elementFromPlan.bbox
|
|
320
|
+
}
|
|
321
|
+
};
|
|
322
|
+
null == onResult || onResult(element);
|
|
323
|
+
return {
|
|
324
|
+
output: {
|
|
325
|
+
element
|
|
326
|
+
},
|
|
327
|
+
hitBy
|
|
328
|
+
};
|
|
329
|
+
}
|
|
319
330
|
};
|
|
331
|
+
return taskFind;
|
|
320
332
|
}
|
|
321
333
|
constructor({ interfaceInstance, insight, taskCache }){
|
|
322
334
|
_define_property(this, "interface", void 0);
|