@qbraid-core/compute
Version:
Client for the qBraid Compute service.
180 lines • 7.23 kB
JavaScript
;
// Copyright (c) 2025, qBraid Development Team
// All rights reserved.
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ComputeManagerClientV1 = exports.ComputeManagerClient = void 0;
const base_1 = require("@qbraid-core/base");
const base_2 = require("@qbraid-core/base");
const base_3 = require("@qbraid-core/base");
const axios_1 = __importDefault(require("axios"));
class ComputeManagerClient extends base_1.QbraidClient {
qBraidURLs;
constructor(session, hubURL = base_3.DEFAULT_HUB_URL, labURL = base_3.DEFAULT_LAB_URL) {
super(session);
this.qBraidURLs = {
hub: hubURL,
lab: labURL,
};
}
async getComputeManager(userEmail) {
const response = await this.session.client.get(`/api/lab/compute/get-profiles/${userEmail}`);
return response.data;
}
async getUserLabToken() {
const response = await this.session.client.get('/api/lab/compute/tokens');
const data = response.data;
const tokenData = data.token;
return tokenData.token;
}
async startServer(labToken, profileSlug = '2vCPU_4GB') {
const response = await this.session.client.post('/api/lab/compute/start/server', {
labToken: labToken,
labURL: this.qBraidURLs['lab'],
slug: profileSlug,
});
return response;
}
async stopServer(labToken, labUserName) {
const response = await axios_1.default.delete(`${this.qBraidURLs['hub']}/hub/api/users/${labUserName}/server`, { headers: { Authorization: `token ${labToken}` } });
return response;
}
async statusServer(labToken, labUserName) {
const response = await axios_1.default.get(`${this.qBraidURLs['hub']}/hub/api/users/${labUserName}`, {
headers: {
Authorization: `token ${labToken}`,
Accept: '*/*',
'Content-Type': 'application/json; charset=utf-8',
},
});
return response.data;
}
async getProfilePrice(profileSlug) {
const response = await this.session.client.get(`/api/lab/compute/get-profile-price/${profileSlug}`);
return response.data;
}
}
exports.ComputeManagerClient = ComputeManagerClient;
class ComputeManagerClientV1 extends base_2.QbraidClientV1 {
constructor(session) {
super(session);
}
/**
* Get compute profiles from the V1 API.
* @param params - Optional query parameters to filter profiles
* @returns Array of ComputeProfileV1 objects
* @throws Error if the API returns an error response
*/
async getProfiles(params) {
const response = await this.session.client?.get('/compute/profiles', { params });
if (response?.data && 'success' in response.data && !response.data.success) {
const error = response.data.error;
throw new Error(`${error.code}: ${error.message}`);
}
return response?.data && 'data' in response.data ? response.data.data : [];
}
/**
* Start a compute server with the specified profile.
* @param profileSlug - The slug of the profile to use
* @returns Server start response
* @throws Error if the API returns an error response
*/
async startServer(profileSlug) {
const response = await this.session.client?.post('/compute/servers/start', { profileSlug });
if (response?.data && 'success' in response.data && !response.data.success) {
const error = response.data.error;
throw new Error(`${error.code}: ${error.message}`);
}
return response?.data && 'data' in response.data
? response.data.data
: {
message: 'Unknown error',
clusterId: '',
profile: profileSlug,
status: 'starting',
};
}
/**
* Stop the current user's compute server.
* @returns Server stop response
* @throws Error if the API returns an error response
*/
async stopServer() {
const response = await this.session.client?.delete('/compute/servers/stop');
if (response?.data && 'success' in response.data && !response.data.success) {
const error = response.data.error;
throw new Error(`${error.code}: ${error.message}`);
}
return response?.data && 'data' in response.data
? response.data.data
: {
message: 'Unknown error',
clusterId: '',
status: 'stopping',
};
}
/**
* Get the status of the current user's compute server.
* @returns Server status response
* @throws Error if the API returns an error response
*/
async getServerStatus() {
const response = await this.session.client?.get('/compute/servers/status');
if (response?.data && 'success' in response.data && !response.data.success) {
const error = response.data.error;
throw new Error(`${error.code}: ${error.message}`);
}
return response?.data && 'data' in response.data
? response.data.data
: {
clusterId: '',
serverRunning: false,
currentProfile: null,
serverDetails: null,
userInfo: {
name: '',
admin: false,
groups: [],
},
};
}
/**
* Get a session token for the current user's compute server.
* @returns The session token string
* @throws Error if the API returns an error response
*/
async getUserLabToken() {
const response = await this.session.client?.get('/compute/servers/session-token');
if (response?.data && 'success' in response.data && !response.data.success) {
const error = response.data.error;
throw new Error(`${error.code}: ${error.message}`);
}
return response?.data && 'token' in response.data ? (response.data.token?.token ?? '') : '';
}
/**
* Get the price of a compute profile.
* @param profileSlug - The slug of the profile
* @returns Profile cost information
* @throws Error if the API returns an error response
*/
async getProfilePrice(profileSlug) {
const response = await this.session.client?.get(`/compute/profiles/${profileSlug}/price`);
if (response?.data && 'success' in response.data && !response.data.success) {
const error = response.data.error;
throw new Error(`${error.code}: ${error.message}`);
}
return response?.data && 'profileSlug' in response.data
? response.data
: {
profileSlug,
rateDollar: 0,
rateTimeFrame: 'hour',
estimatedHourlyCost: 0,
isFree: true,
};
}
}
exports.ComputeManagerClientV1 = ComputeManagerClientV1;
//# sourceMappingURL=client.js.map