Skip to content

Commit 6050dbd

Browse files
feat: add listRunning method to sandboxes namespace (#145)
Expose the vmListRunningVms endpoint as sdk.sandboxes.listRunning() to list currently running VMs with their specs, activity timestamps, and concurrent VM limits. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-authored-by: Claude <noreply@anthropic.com>
1 parent 3a6f9ea commit 6050dbd

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

src/Sandboxes.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,45 @@ export class Sandboxes {
209209
},
210210
};
211211
}
212+
213+
/**
214+
* List information about currently running VMs.
215+
*
216+
* This information is updated roughly every 30 seconds, so this data is not
217+
* guaranteed to be perfectly up-to-date.
218+
*/
219+
async listRunning() {
220+
const data = await this.api.listRunningVms();
221+
222+
return {
223+
concurrentVmCount: data.concurrent_vm_count,
224+
concurrentVmLimit: data.concurrent_vm_limit,
225+
vms: data.vms.map(vm => ({
226+
id: vm.id,
227+
creditBasis: vm.credit_basis,
228+
lastActiveAt: vm.last_active_at ? parseTimestamp(vm.last_active_at) : undefined,
229+
sessionStartedAt: vm.session_started_at ? parseTimestamp(vm.session_started_at) : undefined,
230+
specs: vm.specs ? {
231+
cpu: vm.specs.cpu,
232+
memory: vm.specs.memory,
233+
storage: vm.specs.storage,
234+
} : undefined,
235+
})),
236+
};
237+
}
238+
}
239+
240+
function parseTimestamp(timestamp: number): Date | undefined {
241+
if (!timestamp || timestamp === 0) {
242+
return undefined;
243+
}
244+
245+
// Handle both seconds and milliseconds timestamps
246+
const ts = timestamp < 10000000000 ? timestamp * 1000 : timestamp;
247+
const date = new Date(ts);
248+
249+
// Return undefined if the date is invalid
250+
return isNaN(date.getTime()) ? undefined : date;
212251
}
213252

214253
function privacyToNumber(privacy: SandboxPrivacy): number {

0 commit comments

Comments
 (0)