test-wuying-agentbay-sdk 0.13.1-beta.20251224094729 → 0.13.1-beta.20251224100120

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.mjs CHANGED
@@ -14322,43 +14322,107 @@ var _FileSystem = class _FileSystem {
14322
14322
  * @param path - Path to the file to read.
14323
14323
  * @param offset - Optional: Byte offset to start reading from (0-based).
14324
14324
  * @param length - Optional: Number of bytes to read. If 0, reads the entire file from offset.
14325
- * @returns FileContentResult with file content and requestId
14325
+ * @param formatType - Optional: Format to read the file in. "text" (default) or "binary".
14326
+ * @returns FileContentResult for text format, BinaryFileContentResult for binary format
14326
14327
  */
14327
- async readFileChunk(path6, offset = 0, length = 0) {
14328
+ async readFileChunk(path6, offset = 0, length = 0, formatType = "text") {
14328
14329
  try {
14329
14330
  const args = {
14330
14331
  path: path6
14331
14332
  };
14332
- if (offset > 0) {
14333
+ if (offset >= 0) {
14333
14334
  args.offset = offset;
14334
14335
  }
14335
- if (length > 0) {
14336
+ if (length >= 0) {
14336
14337
  args.length = length;
14337
14338
  }
14339
+ if (formatType === "binary") {
14340
+ args.format = "binary";
14341
+ }
14338
14342
  const result = await this.session.callMcpTool(
14339
14343
  "read_file",
14340
14344
  args
14341
14345
  );
14342
14346
  if (!result.success) {
14347
+ if (formatType === "binary") {
14348
+ return {
14349
+ requestId: result.requestId,
14350
+ success: false,
14351
+ content: new Uint8Array(0),
14352
+ errorMessage: result.errorMessage
14353
+ };
14354
+ } else {
14355
+ return {
14356
+ requestId: result.requestId,
14357
+ success: false,
14358
+ content: "",
14359
+ errorMessage: result.errorMessage
14360
+ };
14361
+ }
14362
+ }
14363
+ if (formatType === "binary") {
14364
+ try {
14365
+ if (typeof Buffer !== "undefined") {
14366
+ const base64String = result.data || "";
14367
+ if (base64String) {
14368
+ const base64WithoutPadding = base64String.replace(/=+$/, "");
14369
+ if (!/^[A-Za-z0-9+/]+$/.test(base64WithoutPadding)) {
14370
+ throw new Error("Invalid base64 string format");
14371
+ }
14372
+ const paddingMatch = base64String.match(/=+$/);
14373
+ if (paddingMatch && paddingMatch[0].length > 2) {
14374
+ throw new Error("Invalid base64 padding format");
14375
+ }
14376
+ }
14377
+ const binaryContent = Buffer.from(base64String, "base64");
14378
+ return {
14379
+ requestId: result.requestId,
14380
+ success: true,
14381
+ content: new Uint8Array(binaryContent)
14382
+ };
14383
+ } else {
14384
+ const binaryString = atob(result.data || "");
14385
+ const binaryContent = new Uint8Array(binaryString.length);
14386
+ for (let i = 0; i < binaryString.length; i++) {
14387
+ binaryContent[i] = binaryString.charCodeAt(i);
14388
+ }
14389
+ return {
14390
+ requestId: result.requestId,
14391
+ success: true,
14392
+ content: binaryContent
14393
+ };
14394
+ }
14395
+ } catch (e) {
14396
+ return {
14397
+ requestId: result.requestId,
14398
+ success: false,
14399
+ content: new Uint8Array(0),
14400
+ errorMessage: `Failed to decode base64: ${e}`
14401
+ };
14402
+ }
14403
+ } else {
14343
14404
  return {
14344
14405
  requestId: result.requestId,
14406
+ success: true,
14407
+ content: result.data || ""
14408
+ };
14409
+ }
14410
+ } catch (error) {
14411
+ if (formatType === "binary") {
14412
+ return {
14413
+ requestId: "",
14414
+ success: false,
14415
+ content: new Uint8Array(0),
14416
+ errorMessage: `Failed to read file: ${error}`
14417
+ };
14418
+ } else {
14419
+ return {
14420
+ requestId: "",
14345
14421
  success: false,
14346
14422
  content: "",
14347
- errorMessage: result.errorMessage
14423
+ errorMessage: `Failed to read file: ${error}`
14348
14424
  };
14349
14425
  }
14350
- return {
14351
- requestId: result.requestId,
14352
- success: true,
14353
- content: result.data || ""
14354
- };
14355
- } catch (error) {
14356
- return {
14357
- requestId: "",
14358
- success: false,
14359
- content: "",
14360
- errorMessage: `Failed to read file: ${error}`
14361
- };
14362
14426
  }
14363
14427
  }
14364
14428
  /**
@@ -14556,120 +14620,178 @@ var _FileSystem = class _FileSystem {
14556
14620
  };
14557
14621
  }
14558
14622
  }
14559
- /**
14560
- * Reads the contents of a file. Automatically handles large files by chunking.
14561
- *
14562
- * @param path - Path to the file to read.
14563
- * @returns FileContentResult with complete file content and requestId
14564
- */
14565
- /**
14566
- * Reads the entire content of a file.
14567
- *
14568
- * @param path - Absolute path to the file to read.
14569
- *
14570
- * @returns Promise resolving to FileContentResult containing:
14571
- * - success: Whether the read operation succeeded
14572
- * - content: String content of the file
14573
- * - requestId: Unique identifier for this API request
14574
- * - errorMessage: Error description if read failed
14575
- *
14576
- * @throws Error if the API call fails.
14577
- *
14578
- * @example
14579
- * ```typescript
14580
- * import { AgentBay } from 'wuying-agentbay-sdk';
14581
- *
14582
- * const agentBay = new AgentBay({ apiKey: 'your_api_key' });
14583
- * const result = await agentBay.create();
14584
- *
14585
- * if (result.success) {
14586
- * const session = result.session;
14587
- *
14588
- * // Read a text file
14589
- * const fileResult = await session.fileSystem.readFile('/etc/hostname');
14590
- * if (fileResult.success) {
14591
- * console.log(`Content: ${fileResult.content}`);
14592
- * // Output: Content: agentbay-session-xyz
14593
- * }
14594
- *
14595
- * await session.delete();
14596
- * }
14597
- * ```
14598
- *
14599
- * @remarks
14600
- * **Behavior:**
14601
- * - Automatically handles large files by reading in 60KB chunks
14602
- * - Returns empty string for empty files
14603
- * - Fails if path is a directory or doesn't exist
14604
- * - Content is returned as UTF-8 string
14605
- *
14606
- * @see {@link writeFile}, {@link listDirectory}
14607
- */
14608
- async readFile(path6) {
14623
+ async readFile(path6, opts) {
14624
+ const format = opts?.format || "text";
14609
14625
  const chunkSize = DEFAULT_CHUNK_SIZE;
14610
14626
  try {
14611
14627
  const fileInfoResult = await this.getFileInfo(path6);
14612
14628
  if (!fileInfoResult.success) {
14613
- return {
14614
- requestId: fileInfoResult.requestId,
14615
- success: false,
14616
- content: "",
14617
- errorMessage: fileInfoResult.errorMessage
14618
- };
14629
+ if (format === "bytes") {
14630
+ return {
14631
+ requestId: fileInfoResult.requestId,
14632
+ success: false,
14633
+ content: new Uint8Array(0),
14634
+ errorMessage: fileInfoResult.errorMessage
14635
+ };
14636
+ } else {
14637
+ return {
14638
+ requestId: fileInfoResult.requestId,
14639
+ success: false,
14640
+ content: "",
14641
+ errorMessage: fileInfoResult.errorMessage
14642
+ };
14643
+ }
14619
14644
  }
14620
14645
  if (!fileInfoResult.fileInfo || fileInfoResult.fileInfo.isDirectory) {
14621
- return {
14622
- requestId: fileInfoResult.requestId,
14623
- success: false,
14624
- content: "",
14625
- errorMessage: `Path does not exist or is a directory: ${path6}`
14626
- };
14646
+ const errorMsg = `Path does not exist or is a directory: ${path6}`;
14647
+ if (format === "bytes") {
14648
+ return {
14649
+ requestId: fileInfoResult.requestId,
14650
+ success: false,
14651
+ content: new Uint8Array(0),
14652
+ errorMessage: errorMsg
14653
+ };
14654
+ } else {
14655
+ return {
14656
+ requestId: fileInfoResult.requestId,
14657
+ success: false,
14658
+ content: "",
14659
+ errorMessage: errorMsg
14660
+ };
14661
+ }
14627
14662
  }
14628
14663
  const fileSize = fileInfoResult.fileInfo.size || 0;
14629
14664
  if (fileSize === 0) {
14665
+ if (format === "bytes") {
14666
+ return {
14667
+ requestId: fileInfoResult.requestId,
14668
+ success: true,
14669
+ content: new Uint8Array(0),
14670
+ size: 0
14671
+ };
14672
+ } else {
14673
+ return {
14674
+ requestId: fileInfoResult.requestId,
14675
+ success: true,
14676
+ content: ""
14677
+ };
14678
+ }
14679
+ }
14680
+ if (format === "bytes") {
14681
+ const contentChunks = [];
14682
+ let offset = 0;
14683
+ let chunkCount = 0;
14684
+ while (offset < fileSize) {
14685
+ let length = chunkSize;
14686
+ if (offset + length > fileSize) {
14687
+ length = fileSize - offset;
14688
+ }
14689
+ try {
14690
+ const chunkResult = await this.readFileChunk(
14691
+ path6,
14692
+ offset,
14693
+ length,
14694
+ "binary"
14695
+ );
14696
+ if (!chunkResult.success) {
14697
+ return chunkResult;
14698
+ }
14699
+ if ("content" in chunkResult && chunkResult.content instanceof Uint8Array) {
14700
+ contentChunks.push(chunkResult.content);
14701
+ } else {
14702
+ return {
14703
+ requestId: chunkResult.requestId,
14704
+ success: false,
14705
+ content: new Uint8Array(0),
14706
+ errorMessage: "Unexpected result type for binary format"
14707
+ };
14708
+ }
14709
+ offset += length;
14710
+ chunkCount++;
14711
+ } catch (error) {
14712
+ return {
14713
+ requestId: fileInfoResult.requestId,
14714
+ success: false,
14715
+ content: new Uint8Array(0),
14716
+ errorMessage: `Error reading chunk at offset ${offset}: ${error}`
14717
+ };
14718
+ }
14719
+ }
14720
+ const totalLength = contentChunks.reduce((sum, chunk) => sum + chunk.length, 0);
14721
+ const finalContent = new Uint8Array(totalLength);
14722
+ let position = 0;
14723
+ for (const chunk of contentChunks) {
14724
+ finalContent.set(chunk, position);
14725
+ position += chunk.length;
14726
+ }
14630
14727
  return {
14631
14728
  requestId: fileInfoResult.requestId,
14632
14729
  success: true,
14633
- content: ""
14730
+ content: finalContent,
14731
+ size: finalContent.length
14634
14732
  };
14635
- }
14636
- let result = "";
14637
- let offset = 0;
14638
- let chunkCount = 0;
14639
- while (offset < fileSize) {
14640
- let length = chunkSize;
14641
- if (offset + length > fileSize) {
14642
- length = fileSize - offset;
14643
- }
14644
- try {
14645
- const chunkResult = await this.readFileChunk(path6, offset, length);
14646
- if (!chunkResult.success) {
14647
- return chunkResult;
14733
+ } else {
14734
+ let result = "";
14735
+ let offset = 0;
14736
+ let chunkCount = 0;
14737
+ while (offset < fileSize) {
14738
+ let length = chunkSize;
14739
+ if (offset + length > fileSize) {
14740
+ length = fileSize - offset;
14741
+ }
14742
+ try {
14743
+ const chunkResult = await this.readFileChunk(
14744
+ path6,
14745
+ offset,
14746
+ length,
14747
+ "text"
14748
+ );
14749
+ if (!chunkResult.success) {
14750
+ return chunkResult;
14751
+ }
14752
+ if ("content" in chunkResult && typeof chunkResult.content === "string") {
14753
+ result += chunkResult.content;
14754
+ } else {
14755
+ return {
14756
+ requestId: chunkResult.requestId,
14757
+ success: false,
14758
+ content: "",
14759
+ errorMessage: "Unexpected result type for text format"
14760
+ };
14761
+ }
14762
+ offset += length;
14763
+ chunkCount++;
14764
+ } catch (error) {
14765
+ return {
14766
+ requestId: fileInfoResult.requestId,
14767
+ success: false,
14768
+ content: "",
14769
+ errorMessage: `Error reading chunk at offset ${offset}: ${error}`
14770
+ };
14648
14771
  }
14649
- result += chunkResult.content;
14650
- offset += length;
14651
- chunkCount++;
14652
- } catch (error) {
14653
- return {
14654
- requestId: fileInfoResult.requestId,
14655
- success: false,
14656
- content: "",
14657
- errorMessage: `Error reading chunk at offset ${offset}: ${error}`
14658
- };
14659
14772
  }
14773
+ return {
14774
+ requestId: fileInfoResult.requestId,
14775
+ success: true,
14776
+ content: result
14777
+ };
14660
14778
  }
14661
- return {
14662
- requestId: fileInfoResult.requestId,
14663
- success: true,
14664
- content: result
14665
- };
14666
14779
  } catch (error) {
14667
- return {
14668
- requestId: "",
14669
- success: false,
14670
- content: "",
14671
- errorMessage: `Failed to read large file: ${error}`
14672
- };
14780
+ if (format === "bytes") {
14781
+ return {
14782
+ requestId: "",
14783
+ success: false,
14784
+ content: new Uint8Array(0),
14785
+ errorMessage: `Failed to read large file: ${error}`
14786
+ };
14787
+ } else {
14788
+ return {
14789
+ requestId: "",
14790
+ success: false,
14791
+ content: "",
14792
+ errorMessage: `Failed to read large file: ${error}`
14793
+ };
14794
+ }
14673
14795
  }
14674
14796
  }
14675
14797
  /**