UNPKG

@luxorai/sandbox

Version:

TypeScript SDK for Luxor Sandbox: cloud sandboxes (microVMs) for AI agents on Luxor Agent Cloud

176 lines • 7.31 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.Variables = exports.WorkspaceVariableError = void 0; const wkt_1 = require("@bufbuild/protobuf/wkt"); const connect_1 = require("@connectrpc/connect"); const pb = __importStar(require("./internal/proto/tenki/cloud/workspace/v1beta1/variables_pb.js")); const workspace_secrets_js_1 = require("./workspace-secrets.js"); /** A failed variable RPC, with the server's message and status. */ class WorkspaceVariableError extends Error { code; constructor(message, code, options) { super(message, options); this.code = code; this.name = "WorkspaceVariableError"; } } exports.WorkspaceVariableError = WorkspaceVariableError; function throwVariableError(error) { if (error instanceof connect_1.ConnectError) throw new WorkspaceVariableError(error.rawMessage, error.code, { cause: error }); throw error; } /** protobuf-es would send a non-string as "" or "[object Object]" and replace lone surrogates with U+FFFD. */ function checkValue(value) { if (typeof value !== "string") throw new TypeError("variable value must be a string"); if (/\p{Cs}/u.test(value)) throw new Error("variable value must be valid UTF-8"); } function variable(v) { if (!v) throw new Error("Missing variable"); return { id: v.id, workspaceId: v.workspaceId, name: v.name, value: v.value, repository: v.repository || undefined, description: v.description || undefined, createdAt: v.createdAt && (0, wkt_1.timestampDate)(v.createdAt), updatedAt: v.updatedAt && (0, wkt_1.timestampDate)(v.updatedAt), }; } /** A repository without an availability means repository availability, and workspace refuses one, as for secrets. */ function variableAvailability(availability, repository) { if (repository) { if (availability === "workspace") throw new Error("availability workspace cannot be combined with a repository"); (0, workspace_secrets_js_1.checkRepository)(repository); } switch (availability) { case undefined: return repository ? pb.VariableAvailability.REPOSITORY : pb.VariableAvailability.UNSPECIFIED; case "workspace": return pb.VariableAvailability.WORKSPACE; case "repository": return pb.VariableAvailability.REPOSITORY; } throw new Error(`unknown variable availability ${JSON.stringify(availability)}`); } /** Access through WorkspaceClient.variables. Values are plain text and come back with every variable a method returns. */ class Variables { workspaceId; rpc; secrets; constructor(workspaceId, rpc, secrets) { this.workspaceId = workspaceId; this.rpc = rpc; this.secrets = secrets; } /** With `repository`, only CI jobs in that connected repository get it, in place of a workspace variable with the same name. */ async create(name, value, options = {}) { const { repository = "", description = "" } = options; if (repository) (0, workspace_secrets_js_1.checkRepository)(repository); checkValue(value); const response = await this.rpc .createVariable({ workspaceId: this.workspaceId, name, value, repository, description }) .catch(throwVariableError); return variable(response.variable); } async update(variableId, options) { const availability = variableAvailability(options.availability, options.repository); if (availability === pb.VariableAvailability.REPOSITORY) (0, workspace_secrets_js_1.checkRepository)(options.repository ?? ""); if (options.value !== undefined) checkValue(options.value); const response = await this.rpc .updateVariable({ workspaceId: this.workspaceId, variableId, value: options.value, description: options.description, availability, repository: options.repository, }) .catch(throwVariableError); return variable(response.variable); } async get(variableId) { const response = await this.rpc .getVariable({ workspaceId: this.workspaceId, variableId }) .catch(throwVariableError); return variable(response.variable); } /** Returns the variable as it was; it cannot be restored. */ async delete(variableId) { const response = await this.rpc .deleteVariable({ workspaceId: this.workspaceId, variableId }) .catch(throwVariableError); return variable(response.variable); } async list(options = {}) { const availability = variableAvailability(options.availability, options.repository); const response = await this.rpc .listVariables({ workspaceId: this.workspaceId, pageSize: options.pageSize, cursor: options.cursor, availability, repository: options.repository, }) .catch(throwVariableError); return { variables: response.variables.map(variable), nextCursor: response.nextCursor }; } /** Lists the connected repositories (owner/name) a variable can be limited to, ordered by name. */ async listRepositories(options = {}) { const response = await this.secrets .listSecretRepositories({ workspaceId: this.workspaceId, pageSize: options.pageSize, cursor: options.cursor, search: options.search, }) .catch(throwVariableError); return { repositories: response.repositories.map((repository) => repository.fullName), nextCursor: response.nextCursor, }; } } exports.Variables = Variables; //# sourceMappingURL=workspace-variables.js.map