test-wuying-agentbay-sdk 0.15.0-beta.20260114082807 → 0.15.0-beta.20260114164932

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/index.cjs CHANGED
@@ -12805,6 +12805,87 @@ var _Computer = class _Computer {
12805
12805
  constructor(session) {
12806
12806
  this.session = session;
12807
12807
  }
12808
+ static normalizeImageFormat(format, defaultValue) {
12809
+ const f = String(format || "").trim().toLowerCase();
12810
+ if (!f) {
12811
+ return defaultValue;
12812
+ }
12813
+ if (f === "jpg") {
12814
+ return "jpeg";
12815
+ }
12816
+ return f;
12817
+ }
12818
+ static validateBase64String(base64String) {
12819
+ const s = String(base64String || "");
12820
+ if (!s) {
12821
+ throw new Error("Empty base64 string");
12822
+ }
12823
+ if (s.length % 4 !== 0) {
12824
+ throw new Error("Invalid base64 string length");
12825
+ }
12826
+ const base64WithoutPadding = s.replace(/=+$/, "");
12827
+ if (!/^[A-Za-z0-9+/]+$/.test(base64WithoutPadding)) {
12828
+ throw new Error("Invalid base64 string format");
12829
+ }
12830
+ const paddingMatch = s.match(/=+$/);
12831
+ if (paddingMatch && paddingMatch[0].length > 2) {
12832
+ throw new Error("Invalid base64 padding format");
12833
+ }
12834
+ }
12835
+ static decodeScreenshotJsonToBytesStrict(jsonText, expectedFormat) {
12836
+ const s = String(jsonText || "").trim();
12837
+ if (!s) {
12838
+ throw new Error("Empty image data");
12839
+ }
12840
+ if (!s.startsWith("{")) {
12841
+ throw new Error("Screenshot tool returned non-JSON data");
12842
+ }
12843
+ let obj;
12844
+ try {
12845
+ obj = JSON.parse(s);
12846
+ } catch (e) {
12847
+ throw new Error(`Invalid screenshot JSON: ${e instanceof Error ? e.message : String(e)}`);
12848
+ }
12849
+ if (!obj || typeof obj !== "object" || Array.isArray(obj)) {
12850
+ throw new Error("Invalid screenshot JSON: expected object");
12851
+ }
12852
+ const b64 = obj.data;
12853
+ if (typeof b64 !== "string" || !b64.trim()) {
12854
+ throw new Error("Screenshot JSON missing base64 field");
12855
+ }
12856
+ _Computer.validateBase64String(b64);
12857
+ let bytes;
12858
+ if (typeof Buffer !== "undefined") {
12859
+ bytes = new Uint8Array(Buffer.from(b64, "base64"));
12860
+ } else {
12861
+ const binary = atob(b64);
12862
+ const out = new Uint8Array(binary.length);
12863
+ for (let i = 0; i < binary.length; i++) {
12864
+ out[i] = binary.charCodeAt(i);
12865
+ }
12866
+ bytes = out;
12867
+ }
12868
+ const fmt = _Computer.normalizeImageFormat(expectedFormat, "png");
12869
+ if (fmt === "png") {
12870
+ const pngMagic = new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10]);
12871
+ for (let i = 0; i < pngMagic.length; i++) {
12872
+ if (bytes[i] !== pngMagic[i]) {
12873
+ throw new Error("Screenshot data does not match expected format 'png'");
12874
+ }
12875
+ }
12876
+ return bytes;
12877
+ }
12878
+ if (fmt === "jpeg") {
12879
+ const jpgMagic = new Uint8Array([255, 216, 255]);
12880
+ for (let i = 0; i < jpgMagic.length; i++) {
12881
+ if (bytes[i] !== jpgMagic[i]) {
12882
+ throw new Error("Screenshot data does not match expected format 'jpeg'");
12883
+ }
12884
+ }
12885
+ return bytes;
12886
+ }
12887
+ throw new Error(`Unsupported format: ${JSON.stringify(expectedFormat)}`);
12888
+ }
12808
12889
  /**
12809
12890
  * Click mouse at specified coordinates.
12810
12891
  *
@@ -13270,6 +13351,55 @@ var _Computer = class _Computer {
13270
13351
  };
13271
13352
  }
13272
13353
  }
13354
+ /**
13355
+ * Capture the current screen and return raw image bytes (beta).
13356
+ *
13357
+ * This API uses the MCP tool `screenshot` (wuying_capture) and expects the backend to return
13358
+ * a JSON string with top-level field `data` containing base64.
13359
+ *
13360
+ * @param format - Output image format ("png", "jpeg", or "jpg"). Default is "png"
13361
+ */
13362
+ async betaTakeScreenshot(format = "png") {
13363
+ const fmt = _Computer.normalizeImageFormat(format, "png");
13364
+ if (fmt !== "png" && fmt !== "jpeg") {
13365
+ return {
13366
+ success: false,
13367
+ requestId: "",
13368
+ errorMessage: `Unsupported format: ${JSON.stringify(format)}. Supported values: "png", "jpeg".`,
13369
+ data: new Uint8Array(),
13370
+ format: fmt
13371
+ };
13372
+ }
13373
+ try {
13374
+ const result = await this.session.callMcpTool("screenshot", { format: fmt }, false, "wuying_capture");
13375
+ const requestId = result.requestId || "";
13376
+ if (!result.success) {
13377
+ return {
13378
+ success: false,
13379
+ requestId,
13380
+ errorMessage: result.errorMessage || "Failed to take screenshot",
13381
+ data: new Uint8Array(),
13382
+ format: fmt
13383
+ };
13384
+ }
13385
+ const bytes = _Computer.decodeScreenshotJsonToBytesStrict(String(result.data || ""), fmt);
13386
+ return {
13387
+ success: true,
13388
+ requestId,
13389
+ errorMessage: "",
13390
+ data: bytes,
13391
+ format: fmt
13392
+ };
13393
+ } catch (error) {
13394
+ return {
13395
+ success: false,
13396
+ requestId: "",
13397
+ errorMessage: `Failed to take screenshot: ${error instanceof Error ? error.message : String(error)}`,
13398
+ data: new Uint8Array(),
13399
+ format: fmt
13400
+ };
13401
+ }
13402
+ }
13273
13403
  /**
13274
13404
  * Lists all root windows.
13275
13405
  *
@@ -16164,41 +16294,27 @@ function normalizeImageFormat(format, defaultValue) {
16164
16294
  return f;
16165
16295
  }
16166
16296
  _chunkJB6CNGN4cjs.__name.call(void 0, normalizeImageFormat, "normalizeImageFormat");
16167
- function stripDataUrlPrefix(b64) {
16168
- const s = String(b64 || "").trim();
16169
- const idx = s.indexOf("base64,");
16170
- if (idx >= 0) {
16171
- return s.slice(idx + "base64,".length).trim();
16297
+ function validateBase64String(base64String) {
16298
+ const s = String(base64String || "");
16299
+ if (!s) {
16300
+ throw new Error("Empty base64 string");
16172
16301
  }
16173
- return s;
16174
- }
16175
- _chunkJB6CNGN4cjs.__name.call(void 0, stripDataUrlPrefix, "stripDataUrlPrefix");
16176
- function indexOfSubarray(haystack, needle, maxSearch) {
16177
- if (needle.length === 0 || haystack.length < needle.length) {
16178
- return -1;
16179
- }
16180
- const limit = Math.min(haystack.length, Math.max(0, maxSearch));
16181
- for (let i = 0; i + needle.length <= limit; i++) {
16182
- let ok = true;
16183
- for (let j = 0; j < needle.length; j++) {
16184
- if (haystack[i + j] !== needle[j]) {
16185
- ok = false;
16186
- break;
16187
- }
16188
- }
16189
- if (ok) {
16190
- return i;
16191
- }
16302
+ const base64WithoutPadding = s.replace(/=+$/, "");
16303
+ if (!/^[A-Za-z0-9+/]+$/.test(base64WithoutPadding)) {
16304
+ throw new Error("Invalid base64 string format");
16192
16305
  }
16193
- return -1;
16194
- }
16195
- _chunkJB6CNGN4cjs.__name.call(void 0, indexOfSubarray, "indexOfSubarray");
16196
- function base64ToUint8Array(input) {
16197
- let s = stripDataUrlPrefix(input).replace(/[\r\n\t ]+/g, "").replace(/-/g, "+").replace(/_/g, "/");
16198
- const mod = s.length % 4;
16199
- if (mod !== 0) {
16200
- s += "=".repeat(4 - mod);
16306
+ if (s.length % 4 !== 0) {
16307
+ throw new Error("Invalid base64 string length");
16308
+ }
16309
+ const paddingMatch = s.match(/=+$/);
16310
+ if (paddingMatch && paddingMatch[0].length > 2) {
16311
+ throw new Error("Invalid base64 padding format");
16201
16312
  }
16313
+ }
16314
+ _chunkJB6CNGN4cjs.__name.call(void 0, validateBase64String, "validateBase64String");
16315
+ function base64ToUint8ArrayStrict(input) {
16316
+ const s = String(input || "").trim();
16317
+ validateBase64String(s);
16202
16318
  if (typeof Buffer !== "undefined") {
16203
16319
  return new Uint8Array(Buffer.from(s, "base64"));
16204
16320
  }
@@ -16209,82 +16325,55 @@ function base64ToUint8Array(input) {
16209
16325
  }
16210
16326
  return out;
16211
16327
  }
16212
- _chunkJB6CNGN4cjs.__name.call(void 0, base64ToUint8Array, "base64ToUint8Array");
16213
- function decodeBase64Image(input, expectedFormat) {
16214
- const b64 = extractBase64FromMcpPayload(input);
16215
- const raw = base64ToUint8Array(b64);
16216
- let b = raw;
16217
- const pngIdx = indexOfSubarray(b, PNG_MAGIC, 128);
16218
- if (pngIdx > 0) {
16219
- b = b.slice(pngIdx);
16220
- }
16221
- const jpgIdx = indexOfSubarray(b, JPEG_MAGIC, 128);
16222
- if (jpgIdx > 0) {
16223
- b = b.slice(jpgIdx);
16224
- }
16225
- const fmt = indexOfSubarray(b, PNG_MAGIC, PNG_MAGIC.length) === 0 ? "png" : indexOfSubarray(b, JPEG_MAGIC, JPEG_MAGIC.length) === 0 ? "jpeg" : expectedFormat;
16226
- return { bytes: b, format: fmt };
16328
+ _chunkJB6CNGN4cjs.__name.call(void 0, base64ToUint8ArrayStrict, "base64ToUint8ArrayStrict");
16329
+ function startsWithMagic(bytes, magic) {
16330
+ if (bytes.length < magic.length) {
16331
+ return false;
16332
+ }
16333
+ for (let i = 0; i < magic.length; i++) {
16334
+ if (bytes[i] !== magic[i]) {
16335
+ return false;
16336
+ }
16337
+ }
16338
+ return true;
16227
16339
  }
16228
- _chunkJB6CNGN4cjs.__name.call(void 0, decodeBase64Image, "decodeBase64Image");
16229
- function extractBase64FromMcpPayload(input) {
16230
- const s = String(input || "").trim();
16231
- if (!(s.startsWith("{") || s.startsWith("["))) {
16232
- return s;
16340
+ _chunkJB6CNGN4cjs.__name.call(void 0, startsWithMagic, "startsWithMagic");
16341
+ function detectImageFormat(bytes) {
16342
+ if (startsWithMagic(bytes, PNG_MAGIC)) {
16343
+ return "png";
16233
16344
  }
16234
- try {
16235
- const obj = JSON.parse(s);
16236
- const b64 = extractBase64FromAny(obj);
16237
- return b64 || s;
16238
- } catch (e12) {
16239
- return s;
16345
+ if (startsWithMagic(bytes, JPEG_MAGIC)) {
16346
+ return "jpeg";
16240
16347
  }
16348
+ return "";
16241
16349
  }
16242
- _chunkJB6CNGN4cjs.__name.call(void 0, extractBase64FromMcpPayload, "extractBase64FromMcpPayload");
16243
- function extractBase64FromAny(v) {
16244
- if (!v) {
16245
- return "";
16350
+ _chunkJB6CNGN4cjs.__name.call(void 0, detectImageFormat, "detectImageFormat");
16351
+ function decodeBase64Image(input, expectedFormat) {
16352
+ const s = String(input || "").trim();
16353
+ if (!s) {
16354
+ throw new Error("Empty image data");
16246
16355
  }
16247
- if (typeof v === "string") {
16248
- return v;
16356
+ if (!s.startsWith("{")) {
16357
+ throw new Error("Screenshot tool returned non-JSON data");
16249
16358
  }
16250
- if (Array.isArray(v)) {
16251
- return v.length > 0 ? extractBase64FromAny(v[0]) : "";
16359
+ let obj;
16360
+ try {
16361
+ obj = JSON.parse(s);
16362
+ } catch (e) {
16363
+ throw new Error(`Invalid screenshot JSON: ${e instanceof Error ? e.message : String(e)}`);
16252
16364
  }
16253
- if (typeof v === "object") {
16254
- if (typeof v.data === "string" && v.data) {
16255
- return v.data;
16256
- }
16257
- const content = v.content;
16258
- if (Array.isArray(content) && content.length > 0 && typeof content[0] === "object" && content[0]) {
16259
- const c0 = content[0];
16260
- if (typeof c0.blob === "string" && c0.blob) {
16261
- return c0.blob;
16262
- }
16263
- if (typeof c0.data === "string" && c0.data) {
16264
- return c0.data;
16265
- }
16266
- if (typeof c0.text === "string" && c0.text) {
16267
- return c0.text;
16268
- }
16269
- if (c0.blob && typeof c0.blob === "object") {
16270
- if (typeof c0.blob.data === "string" && c0.blob.data) {
16271
- return c0.blob.data;
16272
- }
16273
- }
16274
- if (c0.image && typeof c0.image === "object" && typeof c0.image.data === "string" && c0.image.data) {
16275
- return c0.image.data;
16276
- }
16277
- if (c0.source && typeof c0.source === "object" && typeof c0.source.data === "string" && c0.source.data) {
16278
- return c0.source.data;
16279
- }
16280
- }
16281
- if (v.result) {
16282
- return extractBase64FromAny(v.result);
16283
- }
16365
+ if (!obj || typeof obj !== "object" || Array.isArray(obj)) {
16366
+ throw new Error("Invalid screenshot JSON: expected object");
16284
16367
  }
16285
- return "";
16368
+ const b64 = obj.data;
16369
+ if (typeof b64 !== "string" || !b64.trim()) {
16370
+ throw new Error("Screenshot JSON missing base64 field");
16371
+ }
16372
+ const bytes = base64ToUint8ArrayStrict(b64);
16373
+ const detected = detectImageFormat(bytes);
16374
+ return { bytes, format: detected || expectedFormat };
16286
16375
  }
16287
- _chunkJB6CNGN4cjs.__name.call(void 0, extractBase64FromAny, "extractBase64FromAny");
16376
+ _chunkJB6CNGN4cjs.__name.call(void 0, decodeBase64Image, "decodeBase64Image");
16288
16377
  var _Mobile = class _Mobile {
16289
16378
  constructor(session) {
16290
16379
  this.session = session;
@@ -18488,6 +18577,7 @@ var _Session = class _Session {
18488
18577
  url.searchParams.append("token", this.getToken());
18489
18578
  const requestId = `vpc-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
18490
18579
  url.searchParams.append("requestId", requestId);
18580
+ setRequestId(requestId);
18491
18581
  const response = await fetch(url.toString(), {
18492
18582
  method: "GET",
18493
18583
  headers: {
@@ -18499,7 +18589,7 @@ var _Session = class _Session {
18499
18589
  success: false,
18500
18590
  data: "",
18501
18591
  errorMessage: `VPC request failed: ${response.statusText}`,
18502
- requestId: ""
18592
+ requestId
18503
18593
  };
18504
18594
  }
18505
18595
  const responseData = await response.json();
@@ -18535,14 +18625,14 @@ var _Session = class _Session {
18535
18625
  success: true,
18536
18626
  data: dataStr,
18537
18627
  errorMessage: "",
18538
- requestId: ""
18628
+ requestId
18539
18629
  };
18540
18630
  }
18541
18631
  return {
18542
18632
  success: true,
18543
18633
  data: textContent || JSON.stringify(actualResult),
18544
18634
  errorMessage: "",
18545
- requestId: ""
18635
+ requestId
18546
18636
  };
18547
18637
  } else {
18548
18638
  const callToolRequest = new (0, _chunkQGXJA3GTcjs.CallMcpToolRequest)({
@@ -18572,6 +18662,9 @@ var _Session = class _Session {
18572
18662
  }
18573
18663
  const data = response.body.data;
18574
18664
  const reqId = extractRequestId(response) || "";
18665
+ if (reqId) {
18666
+ setRequestId(reqId);
18667
+ }
18575
18668
  if (toolName === "run_code") {
18576
18669
  let dataStr = "";
18577
18670
  if (data && Array.isArray(data.content) && data.content.length > 0 && data.content[0].text) {