{"version":3,"file":"utils/workspace-path.mjs","sources":["webpack://@agent-tars/server/./src/utils/workspace-path.ts"],"sourcesContent":["/*\n * Copyright (c) 2025 Bytedance, Inc. and its affiliates.\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport fs from 'fs';\nimport path from 'path';\nimport os from 'os';\n\n/**\n * WorkspacePathManager provides utilities for handling workspace directory paths,\n * supporting various path formats (absolute, relative, home directory) and\n * ensuring the directories exist.\n */\nexport class WorkspacePathManager {\n  /**\n   * Default workspace directory name\n   */\n  private static readonly DEFAULT_WORKSPACE_DIR = 'agent-tars-workspace';\n\n  /**\n   * Resolve a workspace path, supporting various formats:\n   * - Relative paths: './workspace', 'workspace'\n   * - Home directory: '~/.agent-tars', '~/workspace'\n   * - Absolute paths: '/path/to/workspace'\n   * If no path is provided, uses the default path: 'cwd/agent-tars-workspace'\n   *\n   * @param baseDir The base directory to resolve relative paths from (usually cwd)\n   * @param workspacePath Optional workspace path specification\n   * @param namespace Optional workspace namespace for isolation (e.g. session ID)\n   * @param isolateSessions Whether to create isolated session directories\n   * @returns Resolved absolute path to the workspace directory\n   */\n  public static resolveWorkspacePath(\n    baseDir: string,\n    workspacePath?: string,\n    namespace?: string,\n    isolateSessions?: boolean,\n  ): string {\n    let resolvedPath: string;\n\n    // If no workspace path provided, use default\n    if (!workspacePath) {\n      resolvedPath = path.join(baseDir, this.DEFAULT_WORKSPACE_DIR);\n    }\n    // Handle home directory paths (starting with ~)\n    else if (workspacePath.startsWith('~')) {\n      resolvedPath = workspacePath.replace(/^~/, os.homedir());\n    }\n    // Handle absolute paths\n    else if (path.isAbsolute(workspacePath)) {\n      resolvedPath = workspacePath;\n    }\n    // Handle relative paths\n    else {\n      resolvedPath = path.resolve(baseDir, workspacePath);\n    }\n\n    // Add namespace subdirectory only if isolateSessions is true and namespace is provided\n    if (isolateSessions && namespace) {\n      resolvedPath = path.join(resolvedPath, namespace);\n    }\n\n    return resolvedPath;\n  }\n\n  /**\n   * Ensures the specified workspace directory exists\n   *\n   * @param workspacePath Path to workspace directory\n   * @returns The ensured workspace path\n   * @throws Error if directory creation fails\n   */\n  public static ensureWorkspaceDirectory(workspacePath: string): string {\n    try {\n      fs.mkdirSync(workspacePath, { recursive: true });\n      return workspacePath;\n    } catch (error) {\n      throw new Error(\n        `Failed to create workspace directory ${workspacePath}: ${\n          error instanceof Error ? error.message : String(error)\n        }`,\n      );\n    }\n  }\n}\n"],"names":["WorkspacePathManager","baseDir","workspacePath","namespace","isolateSessions","resolvedPath","os","path","fs","error","Error","String"],"mappings":";;;;;;;AAGC;;;;;;;;;;AAWM,MAAMA;IAmBX,OAAc,qBACZC,OAAe,EACfC,aAAsB,EACtBC,SAAkB,EAClBC,eAAyB,EACjB;QACR,IAAIC;QAQFA,eALGH,gBAIIA,cAAc,UAAU,CAAC,OACjBA,cAAc,OAAO,CAAC,MAAMI,oBAAAA,OAAU,MAG9CC,sBAAAA,UAAe,CAACL,iBACRA,gBAIAK,sBAAAA,OAAY,CAACN,SAASC,iBAZtBK,sBAAAA,IAAS,CAACN,SAAS,IAAI,CAAC,qBAAqB;QAgB9D,IAAIG,mBAAmBD,WACrBE,eAAeE,sBAAAA,IAAS,CAACF,cAAcF;QAGzC,OAAOE;IACT;IASA,OAAc,yBAAyBH,aAAqB,EAAU;QACpE,IAAI;YACFM,oBAAAA,SAAY,CAACN,eAAe;gBAAE,WAAW;YAAK;YAC9C,OAAOA;QACT,EAAE,OAAOO,OAAO;YACd,MAAM,IAAIC,MACR,CAAC,qCAAqC,EAAER,cAAc,EAAE,EACtDO,iBAAiBC,QAAQD,MAAM,OAAO,GAAGE,OAAOF,QAChD;QAEN;IACF;AACF;AAnEE,iBAJWT,sBAIa,yBAAwB"}