UNPKG

@openai/agents-core

Version:

The OpenAI Agents SDK is a lightweight yet powerful framework for building multi-agent workflows.

166 lines • 6.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WorkspacePathPolicy = exports.SandboxWorkspaceScope = void 0; exports.normalizeSandboxCwd = normalizeSandboxCwd; const errors_1 = require("../errors.js"); const errors_2 = require("./errors.js"); const pathGrants_1 = require("./pathGrants.js"); const posixPath_1 = require("./shared/posixPath.js"); class SandboxWorkspaceScope { cwd; constructor(cwd) { this.cwd = cwd === undefined ? undefined : normalizeSandboxCwd(cwd); } static fromCwd(cwd) { return new SandboxWorkspaceScope(cwd); } anchor(path) { if (this.cwd === undefined) { return path; } const trimmed = path?.trim() ?? ''; if (!trimmed) { return this.cwd; } if (trimmed.startsWith('/')) { return trimmed; } return `${this.cwd}/${trimmed}`; } modelResourcePath(workspaceRoot, path) { if (this.cwd === undefined) { return path; } const normalizedRoot = normalizeWorkspaceRoot(workspaceRoot); const normalizedPath = normalizeWorkspaceRelativeResourcePath(path); return normalizedPath ? joinPosixPath(normalizedRoot, normalizedPath) : normalizedRoot; } absoluteCwd(workspaceRoot) { return this.cwd === undefined ? undefined : this.modelResourcePath(workspaceRoot, this.cwd); } } exports.SandboxWorkspaceScope = SandboxWorkspaceScope; function normalizeSandboxCwd(cwd) { if (typeof cwd !== 'string') { throw new errors_1.UserError('sandbox.cwd must be a string.'); } const trimmed = cwd.trim(); if (!trimmed) { throw new errors_1.UserError('sandbox.cwd must be non-empty.'); } if ((0, posixPath_1.hasBackslashPathSeparator)(cwd)) { throw new errors_1.UserError('sandbox.cwd must use POSIX path separators.'); } if (trimmed.startsWith('/')) { throw new errors_1.UserError('sandbox.cwd must be workspace-relative.'); } if ((0, posixPath_1.hasParentPathSegment)(cwd)) { throw new errors_1.UserError('sandbox.cwd must not contain parent segments.'); } const normalized = (0, posixPath_1.normalizePosixPath)(trimmed); if (normalized === '.') { throw new errors_1.UserError('sandbox.cwd must be non-empty.'); } return normalized; } class WorkspacePathPolicy { root; extraPathGrants; constructor(options) { if ((0, posixPath_1.hasParentPathSegment)(options.root)) { throw new errors_1.UserError('Sandbox workspace root must not contain parent segments.'); } if ((0, posixPath_1.hasBackslashPathSeparator)(options.root)) { throw new errors_1.UserError(`Sandbox path "${options.root}" must use "/" separators.`); } const root = (0, posixPath_1.normalizePosixPath)(options.root); if (!root.startsWith('/')) { throw new errors_1.UserError('Sandbox workspace root must be absolute.'); } this.root = root; this.extraPathGrants = (options.extraPathGrants ?? []) .map((grant) => (0, pathGrants_1.normalizePathGrant)(grant)) .sort((left, right) => right.path.length - left.path.length); } resolve(path, options = {}) { const originalPath = path ?? ''; const trimmed = originalPath.trim(); const normalizedPath = trimmed.length === 0 ? this.root : trimmed.startsWith('/') ? normalizeSandboxPath(trimmed, originalPath) : joinPosixPath(this.root, normalizeSandboxPath(trimmed, originalPath)); const workspaceRelativePath = (0, posixPath_1.relativePosixPathWithinRoot)(this.root, normalizedPath); if (workspaceRelativePath !== null) { return { path: normalizedPath, workspaceRelativePath, }; } const grant = this.matchingGrant(normalizedPath); if (grant) { if (options.forWrite && grant.readOnly) { throw new errors_1.UserError(`Sandbox path "${originalPath}" uses read-only extra path grant "${grant.path}".`); } return { path: normalizedPath, grant, }; } throw new errors_2.SandboxInvalidManifestPathError(`Sandbox path "${originalPath}" escapes the workspace root.`); } matchingGrant(path) { return this.extraPathGrants.find((grant) => (0, posixPath_1.isUnderPosixPath)(path, grant.path)); } } exports.WorkspacePathPolicy = WorkspacePathPolicy; function joinPosixPath(root, relativePath) { if (!relativePath) { return (0, posixPath_1.normalizePosixPath)(root); } return (0, posixPath_1.normalizePosixPath)(`${root.replace(/\/+$/u, '')}/${relativePath}`); } function normalizeWorkspaceRoot(root) { if ((0, posixPath_1.hasBackslashPathSeparator)(root)) { throw new errors_1.UserError('Sandbox workspace root must use POSIX path separators.'); } if ((0, posixPath_1.hasParentPathSegment)(root)) { throw new errors_1.UserError('Sandbox workspace root must not contain parent segments.'); } const normalized = (0, posixPath_1.normalizePosixPath)(root); if (!normalized.startsWith('/')) { throw new errors_1.UserError('Sandbox workspace root must be POSIX absolute.'); } return normalized; } function normalizeWorkspaceRelativeResourcePath(path) { const trimmed = path.trim(); if (!trimmed) { throw new errors_1.UserError('Sandbox resource path must be non-empty.'); } if ((0, posixPath_1.hasBackslashPathSeparator)(path)) { throw new errors_1.UserError('Sandbox resource path must use POSIX path separators.'); } if (trimmed.startsWith('/')) { throw new errors_1.UserError('Sandbox resource path must be workspace-relative.'); } if ((0, posixPath_1.hasParentPathSegment)(path)) { throw new errors_1.UserError('Sandbox resource path must not contain parent segments.'); } const normalized = (0, posixPath_1.normalizePosixPath)(trimmed); return normalized === '.' ? '' : normalized; } function normalizeSandboxPath(path, originalPath) { if ((0, posixPath_1.hasBackslashPathSeparator)(path)) { throw new errors_1.UserError(`Sandbox path "${originalPath}" must use "/" separators.`); } if ((0, posixPath_1.hasEscapingParentPathSegment)(path)) { throw new errors_2.SandboxInvalidManifestPathError(`Sandbox path "${originalPath}" must not escape root.`); } return (0, posixPath_1.normalizePosixPath)(path); } //# sourceMappingURL=workspacePaths.js.map