@midscene/core 0.3.1-beta-20240821105917.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4440,7 +4440,7 @@ import {
4440
4440
  writeFileSync
4441
4441
  } from "fs";
4442
4442
  import { tmpdir } from "os";
4443
- import path, { basename, join } from "path";
4443
+ import path, { basename, dirname, join } from "path";
4444
4444
  var logDir = join(process.cwd(), "./midscene_run/");
4445
4445
 
4446
4446
  // src/image/visualization.ts
@@ -4954,11 +4954,6 @@ async function plan(userPrompt, opts, useModel) {
4954
4954
  if (planFromAI.error) {
4955
4955
  throw new Error(planFromAI.error);
4956
4956
  }
4957
- actions.forEach((task) => {
4958
- if (task.type === "Error") {
4959
- throw new Error(task.thought);
4960
- }
4961
- });
4962
4957
  return { plans: actions };
4963
4958
  }
4964
4959
  export {
package/dist/es/image.js CHANGED
@@ -1,23 +1,6 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
3
- var __hasOwnProp = Object.prototype.hasOwnProperty;
4
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
5
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
- var __spreadValues = (a, b) => {
7
- for (var prop in b || (b = {}))
8
- if (__hasOwnProp.call(b, prop))
9
- __defNormalProp(a, prop, b[prop]);
10
- if (__getOwnPropSymbols)
11
- for (var prop of __getOwnPropSymbols(b)) {
12
- if (__propIsEnum.call(b, prop))
13
- __defNormalProp(a, prop, b[prop]);
14
- }
15
- return a;
16
- };
17
-
18
1
  // src/image/info.ts
19
2
  import assert from "assert";
20
- import { Buffer as Buffer2 } from "buffer";
3
+ import { Buffer } from "buffer";
21
4
  import { readFileSync } from "fs";
22
5
  import Sharp from "sharp";
23
6
  async function imageInfo(image) {
@@ -27,7 +10,7 @@ async function imageInfo(image) {
27
10
  }
28
11
  async function imageInfoOfBase64(imageBase64) {
29
12
  const base64Data = imageBase64.replace(/^data:image\/\w+;base64,/, "");
30
- return imageInfo(Buffer2.from(base64Data, "base64"));
13
+ return imageInfo(Buffer.from(base64Data, "base64"));
31
14
  }
32
15
  function base64Encoded(image, withHeader = true) {
33
16
  const imageBuffer = readFileSync(image);
@@ -44,12 +27,12 @@ function base64Encoded(image, withHeader = true) {
44
27
  }
45
28
 
46
29
  // src/image/transform.ts
47
- import { Buffer as Buffer3 } from "buffer";
30
+ import { Buffer as Buffer2 } from "buffer";
48
31
  import Sharp2 from "sharp";
49
32
  async function saveBase64Image(options) {
50
33
  const { base64Data, outputPath } = options;
51
34
  const base64Image = base64Data.split(";base64,").pop() || base64Data;
52
- const imageBuffer = Buffer3.from(base64Image, "base64");
35
+ const imageBuffer = Buffer2.from(base64Image, "base64");
53
36
  await Sharp2(imageBuffer).toFile(outputPath);
54
37
  console.log("Image successfully written to file.");
55
38
  }
@@ -61,7 +44,7 @@ async function transformImgPathToBase64(inputPath) {
61
44
  }
62
45
  async function resizeImg(base64Data) {
63
46
  const base64Image = base64Data.split(";base64,").pop() || base64Data;
64
- const imageBuffer = Buffer3.from(base64Image, "base64");
47
+ const imageBuffer = Buffer2.from(base64Image, "base64");
65
48
  const metadata = await Sharp2(imageBuffer).metadata();
66
49
  const { width, height } = metadata;
67
50
  if (!width || !height) {
@@ -116,42 +99,26 @@ async function alignCoordByTrim(image, centerRect) {
116
99
  if (!(imgInfo == null ? void 0 : imgInfo.width) || !imgInfo.height || imgInfo.width <= 3 || imgInfo.height <= 3) {
117
100
  return centerRect;
118
101
  }
119
- const zeroSize = {
120
- left: 0,
121
- top: 0,
122
- width: -1,
123
- height: -1
124
- };
125
- const finalCenterRect = __spreadValues({}, centerRect);
126
- if (centerRect.left > imgInfo.width || centerRect.top > imgInfo.height) {
127
- return zeroSize;
128
- }
129
- if (centerRect.left + centerRect.width > imgInfo.width) {
130
- finalCenterRect.width = imgInfo.width - centerRect.left;
131
- }
132
- if (centerRect.top + centerRect.height > imgInfo.height) {
133
- finalCenterRect.height = imgInfo.height - centerRect.top;
134
- }
135
102
  try {
136
- const img = await Sharp2(image).extract(finalCenterRect).toBuffer();
103
+ const img = await Sharp2(image).extract(centerRect).toBuffer();
137
104
  const trimInfo = await trimImage(img);
138
105
  if (!trimInfo) {
139
- return finalCenterRect;
106
+ return centerRect;
140
107
  }
141
108
  return {
142
- left: finalCenterRect.left - trimInfo.trimOffsetLeft,
143
- top: finalCenterRect.top - trimInfo.trimOffsetTop,
109
+ left: centerRect.left - trimInfo.trimOffsetLeft,
110
+ top: centerRect.top - trimInfo.trimOffsetTop,
144
111
  width: trimInfo.width,
145
112
  height: trimInfo.height
146
113
  };
147
114
  } catch (e) {
148
- console.warn(imgInfo, finalCenterRect);
115
+ console.log(imgInfo);
149
116
  throw e;
150
117
  }
151
118
  }
152
119
 
153
120
  // src/image/visualization.ts
154
- import { Buffer as Buffer4 } from "buffer";
121
+ import { Buffer as Buffer3 } from "buffer";
155
122
 
156
123
  // src/utils.ts
157
124
  import assert2 from "assert";
@@ -163,7 +130,7 @@ import {
163
130
  writeFileSync
164
131
  } from "fs";
165
132
  import { tmpdir } from "os";
166
- import path, { basename, join } from "path";
133
+ import path, { basename, dirname, join } from "path";
167
134
  var pkg;
168
135
  function getPkgInfo() {
169
136
  if (pkg) {
@@ -293,7 +260,7 @@ async function composeSectionDiagram(sections, context) {
293
260
  ${rects.join("\n")}
294
261
  </svg>
295
262
  `;
296
- const svgBuffer = Buffer4.from(rectangles);
263
+ const svgBuffer = Buffer3.from(rectangles);
297
264
  const file = getTmpFile("png");
298
265
  await Sharp3({
299
266
  create: {
package/dist/es/index.js CHANGED
@@ -1021,7 +1021,7 @@ import {
1021
1021
  writeFileSync
1022
1022
  } from "fs";
1023
1023
  import { tmpdir } from "os";
1024
- import path, { basename, join } from "path";
1024
+ import path, { basename, dirname, join } from "path";
1025
1025
  var pkg;
1026
1026
  function getPkgInfo() {
1027
1027
  if (pkg) {
@@ -1106,6 +1106,10 @@ ${logDirName}/dump
1106
1106
  logEnvReady = true;
1107
1107
  }
1108
1108
  const filePath = join(targetDir, `${fileName}.${fileExt}`);
1109
+ const outputResourceDir = dirname(filePath);
1110
+ if (!existsSync(outputResourceDir)) {
1111
+ mkdirSync(outputResourceDir, { recursive: true });
1112
+ }
1109
1113
  writeFileSync(filePath, fileContent);
1110
1114
  if (opts == null ? void 0 : opts.generateReport) {
1111
1115
  return writeDumpReport(fileName, fileContent);
@@ -1247,13 +1251,12 @@ var Executor = class {
1247
1251
  }
1248
1252
  if (successfullyCompleted) {
1249
1253
  this.status = "completed";
1254
+ if (this.tasks.length) {
1255
+ return this.tasks[this.tasks.length - 1].output;
1256
+ }
1250
1257
  } else {
1251
1258
  this.status = "error";
1252
1259
  }
1253
- if (this.tasks.length) {
1254
- const outputIndex = Math.min(taskIndex, this.tasks.length - 1);
1255
- return this.tasks[outputIndex].output;
1256
- }
1257
1260
  }
1258
1261
  isInErrorState() {
1259
1262
  return this.status === "error";
@@ -5266,11 +5269,6 @@ async function plan(userPrompt, opts, useModel) {
5266
5269
  if (planFromAI.error) {
5267
5270
  throw new Error(planFromAI.error);
5268
5271
  }
5269
- actions.forEach((task) => {
5270
- if (task.type === "Error") {
5271
- throw new Error(task.thought);
5272
- }
5273
- });
5274
5272
  return { plans: actions };
5275
5273
  }
5276
5274
 
package/dist/es/utils.js CHANGED
@@ -8,7 +8,7 @@ import {
8
8
  writeFileSync
9
9
  } from "fs";
10
10
  import { tmpdir } from "os";
11
- import path, { basename, join } from "path";
11
+ import path, { basename, dirname, join } from "path";
12
12
  var pkg;
13
13
  function getPkgInfo() {
14
14
  if (pkg) {
@@ -94,6 +94,10 @@ ${logDirName}/dump
94
94
  logEnvReady = true;
95
95
  }
96
96
  const filePath = join(targetDir, `${fileName}.${fileExt}`);
97
+ const outputResourceDir = dirname(filePath);
98
+ if (!existsSync(outputResourceDir)) {
99
+ mkdirSync(outputResourceDir, { recursive: true });
100
+ }
97
101
  writeFileSync(filePath, fileContent);
98
102
  if (opts == null ? void 0 : opts.generateReport) {
99
103
  return writeDumpReport(fileName, fileContent);
@@ -4969,11 +4969,6 @@ async function plan(userPrompt, opts, useModel) {
4969
4969
  if (planFromAI.error) {
4970
4970
  throw new Error(planFromAI.error);
4971
4971
  }
4972
- actions.forEach((task) => {
4973
- if (task.type === "Error") {
4974
- throw new Error(task.thought);
4975
- }
4976
- });
4977
4972
  return { plans: actions };
4978
4973
  }
4979
4974
  // Annotate the CommonJS export names for ESM import in node:
package/dist/lib/image.js CHANGED
@@ -3,22 +3,8 @@ var __create = Object.create;
3
3
  var __defProp = Object.defineProperty;
4
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
7
6
  var __getProtoOf = Object.getPrototypeOf;
8
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
9
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
10
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
11
- var __spreadValues = (a, b) => {
12
- for (var prop in b || (b = {}))
13
- if (__hasOwnProp.call(b, prop))
14
- __defNormalProp(a, prop, b[prop]);
15
- if (__getOwnPropSymbols)
16
- for (var prop of __getOwnPropSymbols(b)) {
17
- if (__propIsEnum.call(b, prop))
18
- __defNormalProp(a, prop, b[prop]);
19
- }
20
- return a;
21
- };
22
8
  var __export = (target, all) => {
23
9
  for (var name in all)
24
10
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -158,36 +144,20 @@ async function alignCoordByTrim(image, centerRect) {
158
144
  if (!(imgInfo == null ? void 0 : imgInfo.width) || !imgInfo.height || imgInfo.width <= 3 || imgInfo.height <= 3) {
159
145
  return centerRect;
160
146
  }
161
- const zeroSize = {
162
- left: 0,
163
- top: 0,
164
- width: -1,
165
- height: -1
166
- };
167
- const finalCenterRect = __spreadValues({}, centerRect);
168
- if (centerRect.left > imgInfo.width || centerRect.top > imgInfo.height) {
169
- return zeroSize;
170
- }
171
- if (centerRect.left + centerRect.width > imgInfo.width) {
172
- finalCenterRect.width = imgInfo.width - centerRect.left;
173
- }
174
- if (centerRect.top + centerRect.height > imgInfo.height) {
175
- finalCenterRect.height = imgInfo.height - centerRect.top;
176
- }
177
147
  try {
178
- const img = await (0, import_sharp2.default)(image).extract(finalCenterRect).toBuffer();
148
+ const img = await (0, import_sharp2.default)(image).extract(centerRect).toBuffer();
179
149
  const trimInfo = await trimImage(img);
180
150
  if (!trimInfo) {
181
- return finalCenterRect;
151
+ return centerRect;
182
152
  }
183
153
  return {
184
- left: finalCenterRect.left - trimInfo.trimOffsetLeft,
185
- top: finalCenterRect.top - trimInfo.trimOffsetTop,
154
+ left: centerRect.left - trimInfo.trimOffsetLeft,
155
+ top: centerRect.top - trimInfo.trimOffsetTop,
186
156
  width: trimInfo.width,
187
157
  height: trimInfo.height
188
158
  };
189
159
  } catch (e) {
190
- console.warn(imgInfo, finalCenterRect);
160
+ console.log(imgInfo);
191
161
  throw e;
192
162
  }
193
163
  }
package/dist/lib/index.js CHANGED
@@ -1122,6 +1122,10 @@ ${logDirName}/dump
1122
1122
  logEnvReady = true;
1123
1123
  }
1124
1124
  const filePath = (0, import_node_path.join)(targetDir, `${fileName}.${fileExt}`);
1125
+ const outputResourceDir = (0, import_node_path.dirname)(filePath);
1126
+ if (!(0, import_node_fs.existsSync)(outputResourceDir)) {
1127
+ (0, import_node_fs.mkdirSync)(outputResourceDir, { recursive: true });
1128
+ }
1125
1129
  (0, import_node_fs.writeFileSync)(filePath, fileContent);
1126
1130
  if (opts == null ? void 0 : opts.generateReport) {
1127
1131
  return writeDumpReport(fileName, fileContent);
@@ -1263,13 +1267,12 @@ var Executor = class {
1263
1267
  }
1264
1268
  if (successfullyCompleted) {
1265
1269
  this.status = "completed";
1270
+ if (this.tasks.length) {
1271
+ return this.tasks[this.tasks.length - 1].output;
1272
+ }
1266
1273
  } else {
1267
1274
  this.status = "error";
1268
1275
  }
1269
- if (this.tasks.length) {
1270
- const outputIndex = Math.min(taskIndex, this.tasks.length - 1);
1271
- return this.tasks[outputIndex].output;
1272
- }
1273
1276
  }
1274
1277
  isInErrorState() {
1275
1278
  return this.status === "error";
@@ -5282,11 +5285,6 @@ async function plan(userPrompt, opts, useModel) {
5282
5285
  if (planFromAI.error) {
5283
5286
  throw new Error(planFromAI.error);
5284
5287
  }
5285
- actions.forEach((task) => {
5286
- if (task.type === "Error") {
5287
- throw new Error(task.thought);
5288
- }
5289
- });
5290
5288
  return { plans: actions };
5291
5289
  }
5292
5290
 
package/dist/lib/utils.js CHANGED
@@ -138,6 +138,10 @@ ${logDirName}/dump
138
138
  logEnvReady = true;
139
139
  }
140
140
  const filePath = (0, import_node_path.join)(targetDir, `${fileName}.${fileExt}`);
141
+ const outputResourceDir = (0, import_node_path.dirname)(filePath);
142
+ if (!(0, import_node_fs.existsSync)(outputResourceDir)) {
143
+ (0, import_node_fs.mkdirSync)(outputResourceDir, { recursive: true });
144
+ }
141
145
  (0, import_node_fs.writeFileSync)(filePath, fileContent);
142
146
  if (opts == null ? void 0 : opts.generateReport) {
143
147
  return writeDumpReport(fileName, fileContent);
@@ -1,8 +1,8 @@
1
1
  import { ChatCompletionMessageParam } from 'openai/resources';
2
2
  export { ChatCompletionMessageParam } from 'openai/resources';
3
- import { c as callAiFn } from './index-0479d487.js';
4
- export { d as describeUserPage, p as plan } from './index-0479d487.js';
5
- import { B as BaseElement, U as UIContext, e as AIElementParseResponse, f as AISectionParseResponse, g as AIAssertionResponse } from './types-3eb61b5c.js';
3
+ import { c as callAiFn } from './index-7a9ec3e1.js';
4
+ export { d as describeUserPage, p as plan } from './index-7a9ec3e1.js';
5
+ import { B as BaseElement, U as UIContext, e as AIElementParseResponse, f as AISectionParseResponse, g as AIAssertionResponse } from './types-ed68710b.js';
6
6
 
7
7
  declare function AiInspectElement<ElementType extends BaseElement = BaseElement>(options: {
8
8
  context: UIContext<ElementType>;
@@ -1,5 +1,5 @@
1
1
  import { Buffer } from 'node:buffer';
2
- import { S as Size, R as Rect, h as UISection, U as UIContext, G as Color } from './types-3eb61b5c.js';
2
+ import { S as Size, R as Rect, h as UISection, U as UIContext, G as Color } from './types-ed68710b.js';
3
3
  import 'openai/resources';
4
4
 
5
5
  /**
@@ -1,4 +1,4 @@
1
- import { B as BaseElement, U as UIContext, r as PlanningAction } from './types-3eb61b5c.js';
1
+ import { B as BaseElement, U as UIContext, r as PlanningAction } from './types-ed68710b.js';
2
2
  import { ChatCompletionSystemMessageParam, ChatCompletionUserMessageParam } from 'openai/resources';
3
3
 
4
4
  type AIArgs = [
@@ -1,7 +1,7 @@
1
- import { E as ExecutionTask, a as ExecutionTaskApply, b as ExecutionDump, B as BaseElement, U as UIContext, D as DumpSubscriber, I as InsightTaskInfo, c as InsightOptions, d as InsightAssertionResponse } from './types-3eb61b5c.js';
2
- export { g as AIAssertionResponse, e as AIElementParseResponse, A as AIResponseFormat, f as AISectionParseResponse, q as AgentWaitForOpt, H as BaseAgentParserOpt, j as BasicSectionQuery, C as CallAIFn, G as Color, l as DumpMeta, p as ElementById, i as EnsureObject, M as ExecutionRecorderItem, a6 as ExecutionTaskAction, a5 as ExecutionTaskActionApply, a4 as ExecutionTaskInsightAssertion, a3 as ExecutionTaskInsightAssertionApply, a2 as ExecutionTaskInsightAssertionParam, X as ExecutionTaskInsightDumpLog, Z as ExecutionTaskInsightLocate, Y as ExecutionTaskInsightLocateApply, W as ExecutionTaskInsightLocateOutput, V as ExecutionTaskInsightLocateParam, a1 as ExecutionTaskInsightQuery, a0 as ExecutionTaskInsightQueryApply, $ as ExecutionTaskInsightQueryOutput, _ as ExecutionTaskInsightQueryParam, a8 as ExecutionTaskPlanning, a7 as ExecutionTaskPlanningApply, Q as ExecutionTaskReturn, N as ExecutionTaskType, O as ExecutorContext, a9 as GroupedActionDump, n as InsightDump, k as InsightExtractParam, L as LiteUISection, o as PartialInsightDumpFromSDK, s as PlanningAIResponse, r as PlanningAction, x as PlanningActionParamAssert, z as PlanningActionParamError, u as PlanningActionParamHover, v as PlanningActionParamInputOrKeyPress, w as PlanningActionParamScroll, y as PlanningActionParamSleep, t as PlanningActionParamTap, F as PlanningActionParamWaitFor, K as PlaywrightParserOpt, P as Point, J as PuppeteerParserOpt, R as Rect, m as ReportDumpWithAttributes, S as Size, T as TaskCacheInfo, h as UISection } from './types-3eb61b5c.js';
3
- import { c as callAiFn, r as retrieveElement, a as retrieveSection } from './index-0479d487.js';
4
- export { p as plan } from './index-0479d487.js';
1
+ import { E as ExecutionTask, a as ExecutionTaskApply, b as ExecutionDump, B as BaseElement, U as UIContext, D as DumpSubscriber, I as InsightTaskInfo, c as InsightOptions, d as InsightAssertionResponse } from './types-ed68710b.js';
2
+ export { g as AIAssertionResponse, e as AIElementParseResponse, A as AIResponseFormat, f as AISectionParseResponse, q as AgentWaitForOpt, H as BaseAgentParserOpt, j as BasicSectionQuery, C as CallAIFn, G as Color, l as DumpMeta, p as ElementById, i as EnsureObject, M as ExecutionRecorderItem, a6 as ExecutionTaskAction, a5 as ExecutionTaskActionApply, a4 as ExecutionTaskInsightAssertion, a3 as ExecutionTaskInsightAssertionApply, a2 as ExecutionTaskInsightAssertionParam, X as ExecutionTaskInsightDumpLog, Z as ExecutionTaskInsightLocate, Y as ExecutionTaskInsightLocateApply, W as ExecutionTaskInsightLocateOutput, V as ExecutionTaskInsightLocateParam, a1 as ExecutionTaskInsightQuery, a0 as ExecutionTaskInsightQueryApply, $ as ExecutionTaskInsightQueryOutput, _ as ExecutionTaskInsightQueryParam, a8 as ExecutionTaskPlanning, a7 as ExecutionTaskPlanningApply, Q as ExecutionTaskReturn, N as ExecutionTaskType, O as ExecutorContext, a9 as GroupedActionDump, n as InsightDump, k as InsightExtractParam, L as LiteUISection, o as PartialInsightDumpFromSDK, s as PlanningAIResponse, r as PlanningAction, x as PlanningActionParamAssert, z as PlanningActionParamError, u as PlanningActionParamHover, v as PlanningActionParamInputOrKeyPress, w as PlanningActionParamScroll, y as PlanningActionParamSleep, t as PlanningActionParamTap, F as PlanningActionParamWaitFor, K as PlaywrightParserOpt, P as Point, J as PuppeteerParserOpt, R as Rect, m as ReportDumpWithAttributes, S as Size, T as TaskCacheInfo, h as UISection } from './types-ed68710b.js';
3
+ import { c as callAiFn, r as retrieveElement, a as retrieveSection } from './index-7a9ec3e1.js';
4
+ export { p as plan } from './index-7a9ec3e1.js';
5
5
  export { setLogDir } from './utils.js';
6
6
  import 'openai/resources';
7
7
 
@@ -10,7 +10,7 @@ interface Size {
10
10
  }
11
11
  type Rect = Point & Size;
12
12
  declare enum NodeType {
13
- FORM_ITEM = "FORM_ITEM Node",
13
+ INPUT = "INPUT Node",
14
14
  BUTTON = "BUTTON Node",
15
15
  IMG = "IMG Node",
16
16
  TEXT = "TEXT Node"
@@ -1,4 +1,4 @@
1
- import { m as ReportDumpWithAttributes, R as Rect } from './types-3eb61b5c.js';
1
+ import { m as ReportDumpWithAttributes, R as Rect } from './types-ed68710b.js';
2
2
  import 'openai/resources';
3
3
 
4
4
  interface PkgInfo {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@midscene/core",
3
3
  "description": "Hello, It's Midscene",
4
- "version": "0.3.1-beta-20240821105917.0",
4
+ "version": "0.3.1",
5
5
  "jsnext:source": "./src/index.ts",
6
6
  "main": "./dist/lib/index.js",
7
7
  "module": "./dist/es/index.js",