@luxorai/sandbox
Version:
TypeScript SDK for Luxor Sandbox: cloud sandboxes (microVMs) for AI agents on Luxor Agent Cloud
115 lines • 4.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PreviewUrlClient = void 0;
exports.previewUrlFromProto = previewUrlFromProto;
const protobuf_1 = require("@bufbuild/protobuf");
const wkt_1 = require("@bufbuild/protobuf/wkt");
const errors_js_1 = require("./errors.js");
const sandbox_pb_js_1 = require("./internal/proto/tenki/sandbox/v1/sandbox_pb.js");
const pagination_js_1 = require("./pagination.js");
const session_js_1 = require("./session.js");
function previewUrlFromProto(proto) {
if (!proto)
return undefined;
return {
id: proto.id,
workspaceId: proto.workspaceId,
ownerId: proto.ownerId,
slug: proto.slug,
token: proto.token,
previewUrl: proto.previewUrl,
sessionId: proto.sessionId || undefined,
port: proto.port,
createdAt: proto.createdAt ? (0, wkt_1.timestampDate)(proto.createdAt) : undefined,
updatedAt: proto.updatedAt ? (0, wkt_1.timestampDate)(proto.updatedAt) : undefined,
lastAccessedAt: proto.lastAccessedAt ? (0, wkt_1.timestampDate)(proto.lastAccessedAt) : undefined,
wildcard: proto.wildcard ?? false,
wildcardStatus: (0, session_js_1.wildcardStatusFromProto)(proto.wildcardStatus),
wildcardStatusReason: proto.wildcardStatusReason ?? "",
expiresAt: proto.expiresAt ? (0, wkt_1.timestampDate)(proto.expiresAt) : undefined,
};
}
class PreviewUrlClient {
rpcClient;
constructor(rpcClient) {
this.rpcClient = rpcClient;
}
/** Create a preview URL. `slug` is required and must be unique within the workspace. */
async create(slug, options) {
try {
const resp = await this.rpcClient.createPreviewUrl((0, protobuf_1.create)(sandbox_pb_js_1.CreatePreviewUrlRequestSchema, {
slug: slug.trim(),
expiresAt: options?.expiresAt ? (0, wkt_1.timestampFromDate)(options.expiresAt) : undefined,
workspaceId: options?.workspaceId,
sessionId: options?.sessionId,
port: options?.port,
}));
return previewUrlFromProto(resp.previewUrl);
}
catch (err) {
throw (0, errors_js_1.mapError)(err);
}
}
async delete(previewUrlId) {
try {
await this.rpcClient.deletePreviewUrl((0, protobuf_1.create)(sandbox_pb_js_1.DeletePreviewUrlRequestSchema, { previewUrlId }));
}
catch (err) {
throw (0, errors_js_1.mapError)(err);
}
}
async bind(previewUrlId, sessionId, port, expiresAt) {
try {
const resp = await this.rpcClient.bindPreviewUrl((0, protobuf_1.create)(sandbox_pb_js_1.BindPreviewUrlRequestSchema, {
previewUrlId,
sessionId,
port,
expiresAt: expiresAt ? (0, wkt_1.timestampFromDate)(expiresAt) : undefined,
}));
return previewUrlFromProto(resp.previewUrl);
}
catch (err) {
throw (0, errors_js_1.mapError)(err);
}
}
async unbind(previewUrlId) {
try {
const resp = await this.rpcClient.unbindPreviewUrl((0, protobuf_1.create)(sandbox_pb_js_1.UnbindPreviewUrlRequestSchema, { previewUrlId }));
return previewUrlFromProto(resp.previewUrl);
}
catch (err) {
throw (0, errors_js_1.mapError)(err);
}
}
async list(options) {
try {
const previewUrls = [];
let pageToken = "";
do {
const resp = await this.rpcClient.listPreviewUrls((0, protobuf_1.create)(sandbox_pb_js_1.ListPreviewUrlsRequestSchema, {
workspaceId: options?.workspaceId,
sessionId: options?.sessionId,
pageSize: (0, pagination_js_1.automaticPageSize)(),
pageToken,
}));
previewUrls.push(...(resp.previewUrls ?? []).map((item) => previewUrlFromProto(item)));
pageToken = (0, pagination_js_1.advancePageToken)(pageToken, resp.nextPageToken);
} while (pageToken);
return previewUrls;
}
catch (err) {
throw (0, errors_js_1.mapError)(err);
}
}
async get(previewUrlId) {
try {
const resp = await this.rpcClient.getPreviewUrl((0, protobuf_1.create)(sandbox_pb_js_1.GetPreviewUrlRequestSchema, { lookup: { case: "previewUrlId", value: previewUrlId } }));
return previewUrlFromProto(resp.previewUrl);
}
catch (err) {
throw (0, errors_js_1.mapError)(err);
}
}
}
exports.PreviewUrlClient = PreviewUrlClient;
//# sourceMappingURL=preview-url.js.map