rl-rock 1.3.7 → 1.3.9

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
@@ -76,6 +76,7 @@ var ReadFileRequestSchema = z.object({
76
76
  errors: z.string().optional()
77
77
  });
78
78
  var UploadModeSchema = z.enum(["auto", "direct", "oss"]);
79
+ var DownloadModeSchema = z.enum(["auto", "direct", "oss"]);
79
80
  var UploadRequestSchema = z.object({
80
81
  sourcePath: z.string(),
81
82
  targetPath: z.string(),
@@ -597,6 +598,9 @@ var envVars = {
597
598
  get ROCK_OSS_BUCKET_REGION() {
598
599
  return getEnv("ROCK_OSS_BUCKET_REGION");
599
600
  },
601
+ get ROCK_OSS_TIMEOUT() {
602
+ return parseInt(getEnv("ROCK_OSS_TIMEOUT", "300000"), 10);
603
+ },
600
604
  // Pip
601
605
  get ROCK_PIP_INDEX_URL() {
602
606
  return getEnv("ROCK_PIP_INDEX_URL", "https://pypi.org/simple/");
@@ -2291,9 +2295,12 @@ var Sandbox = class extends AbstractSandbox {
2291
2295
  async upload(request) {
2292
2296
  return this.uploadByPath(request.sourcePath, request.targetPath);
2293
2297
  }
2294
- async uploadByPath(sourcePath, targetPath, uploadMode = "auto") {
2298
+ async uploadByPath(sourcePath, targetPath, options) {
2295
2299
  const url = `${this.url}/upload`;
2296
2300
  const headers = this.buildHeaders();
2301
+ const uploadMode = options?.uploadMode ?? "auto";
2302
+ const timeout = options?.timeout;
2303
+ const onProgress = options?.onProgress;
2297
2304
  try {
2298
2305
  const fs = await import('fs/promises');
2299
2306
  try {
@@ -2306,7 +2313,7 @@ var Sandbox = class extends AbstractSandbox {
2306
2313
  const ossEnabled = envVars.ROCK_OSS_ENABLE;
2307
2314
  const ossThreshold = 1024 * 1024;
2308
2315
  if (uploadMode === "oss" || uploadMode === "auto" && ossEnabled && fileSize > ossThreshold) {
2309
- return this.uploadViaOss(sourcePath, targetPath);
2316
+ return this.uploadViaOss(sourcePath, targetPath, timeout, onProgress);
2310
2317
  }
2311
2318
  const fileBuffer = await fs.readFile(sourcePath);
2312
2319
  const fileName = sourcePath.split("/").pop() ?? "file";
@@ -2355,9 +2362,61 @@ var Sandbox = class extends AbstractSandbox {
2355
2362
  }
2356
2363
  }
2357
2364
  /**
2358
- * Download file from sandbox via OSS
2365
+ * Download file from sandbox
2366
+ * @param remotePath - File path in sandbox
2367
+ * @param localPath - Local file path
2368
+ * @param downloadMode - Download mode: 'auto' (default), 'direct', or 'oss'
2369
+ * @param timeout - Optional timeout in milliseconds for OSS mode
2359
2370
  */
2360
- async downloadFile(remotePath, localPath) {
2371
+ async downloadFile(remotePath, localPath, options) {
2372
+ const downloadMode = options?.downloadMode ?? "auto";
2373
+ const timeout = options?.timeout;
2374
+ const onProgress = options?.onProgress;
2375
+ if (!remotePath || remotePath.trim() === "") {
2376
+ return { success: false, message: "Remote path is required" };
2377
+ }
2378
+ const checkResult = await this.execute({ command: ["test", "-f", remotePath], timeout: 60 });
2379
+ if (checkResult.exitCode !== 0) {
2380
+ return { success: false, message: `Remote file does not exist: ${remotePath}` };
2381
+ }
2382
+ if (downloadMode === "direct") {
2383
+ return this.downloadDirect(remotePath, localPath);
2384
+ }
2385
+ if (downloadMode === "oss") {
2386
+ return this.downloadViaOss(remotePath, localPath, timeout, onProgress);
2387
+ }
2388
+ const sizeResult = await this.execute({ command: ["stat", "-c", "%s", remotePath], timeout: 60 });
2389
+ if (sizeResult.exitCode !== 0) {
2390
+ return this.downloadDirect(remotePath, localPath);
2391
+ }
2392
+ const fileSize = parseInt(sizeResult.stdout.trim(), 10);
2393
+ const ossThreshold = 1024 * 1024;
2394
+ const ossEnabled = envVars.ROCK_OSS_ENABLE;
2395
+ if (ossEnabled && fileSize >= ossThreshold) {
2396
+ return this.downloadViaOss(remotePath, localPath, timeout, onProgress);
2397
+ }
2398
+ return this.downloadDirect(remotePath, localPath);
2399
+ }
2400
+ /**
2401
+ * Download file directly via readFile API
2402
+ */
2403
+ async downloadDirect(remotePath, localPath) {
2404
+ try {
2405
+ const fs = await import('fs/promises');
2406
+ const path2 = await import('path');
2407
+ const response = await this.readFile({ path: remotePath });
2408
+ const parentDir = path2.dirname(localPath);
2409
+ await fs.mkdir(parentDir, { recursive: true });
2410
+ await fs.writeFile(localPath, response.content, "utf-8");
2411
+ return { success: true, message: `Successfully downloaded ${remotePath} to ${localPath}` };
2412
+ } catch (e) {
2413
+ return { success: false, message: `Direct download failed: ${e}` };
2414
+ }
2415
+ }
2416
+ /**
2417
+ * Download file via OSS as intermediary
2418
+ */
2419
+ async downloadViaOss(remotePath, localPath, timeout, onProgress) {
2361
2420
  if (!envVars.ROCK_OSS_ENABLE) {
2362
2421
  return {
2363
2422
  success: false,
@@ -2365,15 +2424,8 @@ var Sandbox = class extends AbstractSandbox {
2365
2424
  };
2366
2425
  }
2367
2426
  try {
2368
- if (!remotePath || remotePath.trim() === "") {
2369
- return { success: false, message: "Remote path is required" };
2370
- }
2371
- const checkResult = await this.execute({ command: ["test", "-f", remotePath], timeout: 60 });
2372
- if (checkResult.exitCode !== 0) {
2373
- return { success: false, message: `Remote file does not exist: ${remotePath}` };
2374
- }
2375
2427
  if (this.ossBucket === null || this.isTokenExpired()) {
2376
- await this.setupOss();
2428
+ await this.setupOss(timeout);
2377
2429
  }
2378
2430
  if (!this.ossBucket) {
2379
2431
  return { success: false, message: "Failed to setup OSS bucket" };
@@ -2392,23 +2444,33 @@ var Sandbox = class extends AbstractSandbox {
2392
2444
  if (uploadResult.exitCode !== 0) {
2393
2445
  return { success: false, message: `Sandbox to OSS upload failed: ${uploadResult.output}` };
2394
2446
  }
2395
- const result = await this.ossBucket.get(objectName, localPath);
2447
+ const ossTimeout = timeout ?? envVars.ROCK_OSS_TIMEOUT;
2448
+ const result = await this.ossBucket.get(objectName, localPath, {
2449
+ timeout: ossTimeout,
2450
+ progress: (p) => {
2451
+ onProgress?.({
2452
+ phase: "download-to-local",
2453
+ percent: Math.round(p * 100)
2454
+ });
2455
+ }
2456
+ });
2396
2457
  try {
2397
2458
  await this.ossBucket.delete(objectName);
2398
2459
  } catch {
2399
2460
  }
2400
2461
  return { success: true, message: `Successfully downloaded ${remotePath} to ${localPath}` };
2401
2462
  } catch (e) {
2402
- return { success: false, message: `Download failed: ${e}` };
2463
+ return { success: false, message: `OSS download failed: ${e}` };
2403
2464
  }
2404
2465
  }
2405
2466
  /**
2406
2467
  * Upload file via OSS (internal method)
2468
+ * @param timeout - Optional timeout in milliseconds
2407
2469
  */
2408
- async uploadViaOss(sourcePath, targetPath) {
2470
+ async uploadViaOss(sourcePath, targetPath, timeout, onProgress) {
2409
2471
  try {
2410
2472
  if (this.ossBucket === null || this.isTokenExpired()) {
2411
- await this.setupOss();
2473
+ await this.setupOss(timeout);
2412
2474
  }
2413
2475
  if (!this.ossBucket) {
2414
2476
  return { success: false, message: "Failed to setup OSS bucket" };
@@ -2416,8 +2478,39 @@ var Sandbox = class extends AbstractSandbox {
2416
2478
  const timestamp = Date.now();
2417
2479
  const fileName = sourcePath.split("/").pop() ?? "file";
2418
2480
  const objectName = `${timestamp}-${fileName}`;
2419
- await this.ossBucket.put(objectName, sourcePath);
2481
+ const fs = await import('fs/promises');
2482
+ const stats = await fs.stat(sourcePath);
2483
+ const fileSize = stats.size;
2484
+ const multipartThreshold = 1024 * 1024;
2485
+ const ossTimeout = timeout ?? envVars.ROCK_OSS_TIMEOUT;
2486
+ if (fileSize >= multipartThreshold) {
2487
+ await this.ossBucket.multipartUpload(objectName, sourcePath, {
2488
+ timeout: ossTimeout,
2489
+ partSize: multipartThreshold,
2490
+ // 1MB per part
2491
+ progress: (p) => {
2492
+ onProgress?.({
2493
+ phase: "upload-to-oss",
2494
+ percent: Math.round(p * 100)
2495
+ });
2496
+ }
2497
+ });
2498
+ } else {
2499
+ await this.ossBucket.put(objectName, sourcePath, {
2500
+ timeout: ossTimeout,
2501
+ progress: (p) => {
2502
+ onProgress?.({
2503
+ phase: "upload-to-oss",
2504
+ percent: Math.round(p * 100)
2505
+ });
2506
+ }
2507
+ });
2508
+ }
2420
2509
  const signedUrl = this.ossBucket.signatureUrl(objectName, { expires: 600 });
2510
+ onProgress?.({
2511
+ phase: "download-to-sandbox",
2512
+ percent: -1
2513
+ });
2421
2514
  const downloadCmd = `wget -c -O '${targetPath}' '${signedUrl}'`;
2422
2515
  await this.arun(downloadCmd, { mode: "nohup", waitTimeout: 600 });
2423
2516
  const checkResult = await this.execute({ command: ["test", "-f", targetPath], timeout: 60 });
@@ -2431,13 +2524,16 @@ var Sandbox = class extends AbstractSandbox {
2431
2524
  }
2432
2525
  /**
2433
2526
  * Setup OSS bucket with STS credentials
2527
+ * @param timeout - Optional timeout in milliseconds (defaults to ROCK_OSS_TIMEOUT env var or 300000ms)
2434
2528
  */
2435
- async setupOss() {
2529
+ async setupOss(timeout) {
2436
2530
  const credentials = await this.getOssStsCredentials();
2437
2531
  const OSS = (await import('ali-oss')).default;
2532
+ const ossTimeout = timeout ?? envVars.ROCK_OSS_TIMEOUT;
2438
2533
  this.ossBucket = new OSS({
2439
2534
  secure: true,
2440
2535
  // Use HTTPS for OSS connections
2536
+ timeout: ossTimeout,
2441
2537
  region: envVars.ROCK_OSS_BUCKET_REGION ?? "",
2442
2538
  accessKeyId: credentials.accessKeyId,
2443
2539
  accessKeySecret: credentials.accessKeySecret,
@@ -3398,6 +3494,6 @@ function getVersion() {
3398
3494
  }
3399
3495
  var VERSION = getVersion();
3400
3496
 
3401
- export { Agent, AgentBashCommandSchema, AgentConfigSchema, BadRequestRockError, BashActionSchema, ChmodRequestSchema, ChmodResponseSchema, ChownRequestSchema, ChownResponseSchema, CloseResponseSchema, CloseSessionRequestSchema, CloseSessionResponseSchema, Codes, CommandResponseSchema, CommandRockError, CommandSchema, CreateBashSessionRequestSchema, CreateSessionResponseSchema, DefaultAgent, DefaultAgentConfigSchema, Deploy, DownloadFileResponseSchema, EnvHubClient, EnvHubClientConfigSchema, EnvHubError, ExecuteBashSessionResponseSchema, HttpUtils, InternalServerRockError, InvalidParameterRockException, IsAliveResponseSchema, LinuxFileSystem, LinuxRemoteUser, ModelClient, ModelService, ModelServiceConfigSchema, NODE_DEFAULT_VERSION, Network, NodeRuntimeEnv, NodeRuntimeEnvConfigSchema, ObservationSchema, OssCredentialsSchema, OssSetupResponseSchema, PID_PREFIX, PID_SUFFIX, Process, PythonRuntimeEnv, PythonRuntimeEnvConfigSchema, ReadFileRequestSchema, ReadFileResponseSchema, ReasonPhrases, RockAgentConfigSchema, RockEnv, RockEnvInfoSchema, RockException, RunMode, RuntimeEnv, RuntimeEnvConfigSchema, Sandbox, SandboxConfigSchema, SandboxGroup, SandboxGroupConfigSchema, SandboxResponseSchema, SandboxStatusResponseSchema, SpeedupType, UploadModeSchema, UploadRequestSchema, UploadResponseSchema, VERSION, WriteFileRequestSchema, WriteFileResponseSchema, arunWithRetry, createRockEnvInfo, createRuntimeEnvId, createSandboxConfig, createSandboxGroupConfig, deprecated, deprecatedClass, extractNohupPid as extractNohupPidFromSandbox, fromRockException, getDefaultPipIndexUrl, getEnv, getReasonPhrase, getRequiredEnv, isClientError, isCommandError, isEnvSet, isError, isNode, isServerError, isSuccess, make, raiseForCode, retryAsync, sleep, withRetry, withTimeLogging };
3497
+ export { Agent, AgentBashCommandSchema, AgentConfigSchema, BadRequestRockError, BashActionSchema, ChmodRequestSchema, ChmodResponseSchema, ChownRequestSchema, ChownResponseSchema, CloseResponseSchema, CloseSessionRequestSchema, CloseSessionResponseSchema, Codes, CommandResponseSchema, CommandRockError, CommandSchema, CreateBashSessionRequestSchema, CreateSessionResponseSchema, DefaultAgent, DefaultAgentConfigSchema, Deploy, DownloadFileResponseSchema, DownloadModeSchema, EnvHubClient, EnvHubClientConfigSchema, EnvHubError, ExecuteBashSessionResponseSchema, HttpUtils, InternalServerRockError, InvalidParameterRockException, IsAliveResponseSchema, LinuxFileSystem, LinuxRemoteUser, ModelClient, ModelService, ModelServiceConfigSchema, NODE_DEFAULT_VERSION, Network, NodeRuntimeEnv, NodeRuntimeEnvConfigSchema, ObservationSchema, OssCredentialsSchema, OssSetupResponseSchema, PID_PREFIX, PID_SUFFIX, Process, PythonRuntimeEnv, PythonRuntimeEnvConfigSchema, ReadFileRequestSchema, ReadFileResponseSchema, ReasonPhrases, RockAgentConfigSchema, RockEnv, RockEnvInfoSchema, RockException, RunMode, RuntimeEnv, RuntimeEnvConfigSchema, Sandbox, SandboxConfigSchema, SandboxGroup, SandboxGroupConfigSchema, SandboxResponseSchema, SandboxStatusResponseSchema, SpeedupType, UploadModeSchema, UploadRequestSchema, UploadResponseSchema, VERSION, WriteFileRequestSchema, WriteFileResponseSchema, arunWithRetry, createRockEnvInfo, createRuntimeEnvId, createSandboxConfig, createSandboxGroupConfig, deprecated, deprecatedClass, extractNohupPid as extractNohupPidFromSandbox, fromRockException, getDefaultPipIndexUrl, getEnv, getReasonPhrase, getRequiredEnv, isClientError, isCommandError, isEnvSet, isError, isNode, isServerError, isSuccess, make, raiseForCode, retryAsync, sleep, withRetry, withTimeLogging };
3402
3498
  //# sourceMappingURL=index.mjs.map
3403
3499
  //# sourceMappingURL=index.mjs.map