@luxorai/sandbox
Version:
TypeScript SDK for Luxor Sandbox: cloud sandboxes (microVMs) for AI agents on Luxor Agent Cloud
134 lines • 4.86 kB
JavaScript
import { create } from "@bufbuild/protobuf";
import { CapabilityUnavailableError, PrimitiveTimeoutError } from "./errors.js";
import { DialClosed_Reason, DialRequestSchema, SandboxSessionDataPlaneServiceDialRequestSchema, } from "./internal/proto/tenki/sandbox/v1/sandbox_pb.js";
export async function dial(session, unixSocketPath, options) {
for (let attempt = 0;; attempt++) {
const outbound = new AsyncQueue();
outbound.push(create(DialRequestSchema, {
payload: {
case: "open",
value: {
sessionId: session.id,
target: { case: "unixSocketPath", value: unixSocketPath },
connectTimeoutMs: options?.connectTimeoutMs ?? 0,
},
},
}));
const dataPlane = await session.requireDataPlane();
const responses = unwrapDataPlaneDial(dataPlane.dial(wrapDataPlaneDial(outbound)));
const responseIterator = responses[Symbol.asyncIterator]();
let first;
try {
first = await responseIterator.next();
}
catch (err) {
if (attempt === 0 && (await session.reauthOnUnauthenticated(err))) {
outbound.close();
continue;
}
throw err;
}
if (first.done)
throw new Error("sandbox dial closed before open");
if (first.value.payload.case === "closed") {
const closed = first.value.payload.value;
if (closed.reason === DialClosed_Reason.CAPABILITY_UNAVAILABLE) {
if (closed.detail?.startsWith("first_frame_timeout")) {
throw new PrimitiveTimeoutError("dial", closed.detail);
}
throw new CapabilityUnavailableError("dial", closed.detail || undefined);
}
throw new Error(closed.detail || `sandbox dial failed: ${DialClosed_Reason[closed.reason]}`);
}
if (first.value.payload.case !== "opened")
throw new Error("sandbox dial did not open");
const writable = new WritableStream({
write(chunk) {
outbound.push(create(DialRequestSchema, { payload: { case: "data", value: chunk } }));
},
close() {
outbound.push(create(DialRequestSchema, { payload: { case: "halfClose", value: true } }));
outbound.close();
},
abort(reason) {
outbound.fail(reason);
},
});
const readable = new ReadableStream({
async start(controller) {
try {
for (;;) {
const next = await responseIterator.next();
if (next.done)
break;
const frame = next.value;
if (frame.payload.case === "data")
controller.enqueue(frame.payload.value);
if (frame.payload.case === "closed") {
controller.close();
return;
}
}
controller.close();
}
catch (err) {
controller.error(err);
}
},
cancel() {
outbound.close();
},
});
return { readable, writable };
}
}
async function* wrapDataPlaneDial(outbound) {
for await (const frame of outbound) {
yield create(SandboxSessionDataPlaneServiceDialRequestSchema, { frame });
}
}
async function* unwrapDataPlaneDial(responses) {
for await (const response of responses) {
if (response.frame)
yield response.frame;
}
}
class AsyncQueue {
values = [];
resolvers = [];
done = false;
error;
push(value) {
const resolve = this.resolvers.shift();
if (resolve)
resolve({ done: false, value });
else
this.values.push(value);
}
close() {
this.done = true;
for (const resolve of this.resolvers.splice(0))
resolve({ done: true, value: undefined });
}
fail(error) {
this.error = error;
this.close();
}
async *[Symbol.asyncIterator]() {
for (;;) {
if (this.values.length > 0) {
yield this.values.shift();
continue;
}
if (this.error)
throw this.error;
if (this.done)
return;
const next = await new Promise((resolve) => this.resolvers.push(resolve));
if (next.done)
return;
yield next.value;
}
}
}
//# sourceMappingURL=dial.js.map