@luxorai/sandbox
Version:
TypeScript SDK for Luxor Sandbox: cloud sandboxes (microVMs) for AI agents on Luxor Agent Cloud
110 lines • 4.38 kB
JavaScript
import { create } from "@bufbuild/protobuf";
import { timestampDate, timestampFromDate } from "@bufbuild/protobuf/wkt";
import { mapError } from "./errors.js";
import { BindPreviewUrlRequestSchema, CreatePreviewUrlRequestSchema, DeletePreviewUrlRequestSchema, GetPreviewUrlRequestSchema, ListPreviewUrlsRequestSchema, UnbindPreviewUrlRequestSchema, } from "./internal/proto/tenki/sandbox/v1/sandbox_pb.js";
import { advancePageToken, automaticPageSize } from "./pagination.js";
import { wildcardStatusFromProto } from "./session.js";
export 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 ? timestampDate(proto.createdAt) : undefined,
updatedAt: proto.updatedAt ? timestampDate(proto.updatedAt) : undefined,
lastAccessedAt: proto.lastAccessedAt ? timestampDate(proto.lastAccessedAt) : undefined,
wildcard: proto.wildcard ?? false,
wildcardStatus: wildcardStatusFromProto(proto.wildcardStatus),
wildcardStatusReason: proto.wildcardStatusReason ?? "",
expiresAt: proto.expiresAt ? timestampDate(proto.expiresAt) : undefined,
};
}
export 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(create(CreatePreviewUrlRequestSchema, {
slug: slug.trim(),
expiresAt: options?.expiresAt ? timestampFromDate(options.expiresAt) : undefined,
workspaceId: options?.workspaceId,
sessionId: options?.sessionId,
port: options?.port,
}));
return previewUrlFromProto(resp.previewUrl);
}
catch (err) {
throw mapError(err);
}
}
async delete(previewUrlId) {
try {
await this.rpcClient.deletePreviewUrl(create(DeletePreviewUrlRequestSchema, { previewUrlId }));
}
catch (err) {
throw mapError(err);
}
}
async bind(previewUrlId, sessionId, port, expiresAt) {
try {
const resp = await this.rpcClient.bindPreviewUrl(create(BindPreviewUrlRequestSchema, {
previewUrlId,
sessionId,
port,
expiresAt: expiresAt ? timestampFromDate(expiresAt) : undefined,
}));
return previewUrlFromProto(resp.previewUrl);
}
catch (err) {
throw mapError(err);
}
}
async unbind(previewUrlId) {
try {
const resp = await this.rpcClient.unbindPreviewUrl(create(UnbindPreviewUrlRequestSchema, { previewUrlId }));
return previewUrlFromProto(resp.previewUrl);
}
catch (err) {
throw mapError(err);
}
}
async list(options) {
try {
const previewUrls = [];
let pageToken = "";
do {
const resp = await this.rpcClient.listPreviewUrls(create(ListPreviewUrlsRequestSchema, {
workspaceId: options?.workspaceId,
sessionId: options?.sessionId,
pageSize: automaticPageSize(),
pageToken,
}));
previewUrls.push(...(resp.previewUrls ?? []).map((item) => previewUrlFromProto(item)));
pageToken = advancePageToken(pageToken, resp.nextPageToken);
} while (pageToken);
return previewUrls;
}
catch (err) {
throw mapError(err);
}
}
async get(previewUrlId) {
try {
const resp = await this.rpcClient.getPreviewUrl(create(GetPreviewUrlRequestSchema, { lookup: { case: "previewUrlId", value: previewUrlId } }));
return previewUrlFromProto(resp.previewUrl);
}
catch (err) {
throw mapError(err);
}
}
}
//# sourceMappingURL=preview-url.js.map