UNPKG

simstudio-ts-sdk

Version:

Sim Studio SDK - Execute workflows programmatically

120 lines 4.42 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = exports.SimStudioClient = exports.SimStudioError = void 0; const node_fetch_1 = __importDefault(require("node-fetch")); class SimStudioError extends Error { constructor(message, code, status) { super(message); this.name = 'SimStudioError'; this.code = code; this.status = status; } } exports.SimStudioError = SimStudioError; class SimStudioClient { constructor(config) { this.apiKey = config.apiKey; this.baseUrl = (config.baseUrl || 'https://simstudio.ai').replace(/\/+$/, ''); } /** * Execute a workflow with optional input data */ async executeWorkflow(workflowId, options = {}) { const url = `${this.baseUrl}/api/workflows/${workflowId}/execute`; const { input, timeout = 30000 } = options; try { // Create a timeout promise const timeoutPromise = new Promise((_, reject) => { setTimeout(() => reject(new Error('TIMEOUT')), timeout); }); const fetchPromise = (0, node_fetch_1.default)(url, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-API-Key': this.apiKey, }, body: JSON.stringify(input || {}), }); const response = await Promise.race([fetchPromise, timeoutPromise]); if (!response.ok) { const errorData = (await response.json().catch(() => ({}))); throw new SimStudioError(errorData.error || `HTTP ${response.status}: ${response.statusText}`, errorData.code, response.status); } const result = await response.json(); return result; } catch (error) { if (error instanceof SimStudioError) { throw error; } if (error.message === 'TIMEOUT') { throw new SimStudioError(`Workflow execution timed out after ${timeout}ms`, 'TIMEOUT'); } throw new SimStudioError(error?.message || 'Failed to execute workflow', 'EXECUTION_ERROR'); } } /** * Get the status of a workflow (deployment status, etc.) */ async getWorkflowStatus(workflowId) { const url = `${this.baseUrl}/api/workflows/${workflowId}/status`; try { const response = await (0, node_fetch_1.default)(url, { method: 'GET', headers: { 'X-API-Key': this.apiKey, }, }); if (!response.ok) { const errorData = (await response.json().catch(() => ({}))); throw new SimStudioError(errorData.error || `HTTP ${response.status}: ${response.statusText}`, errorData.code, response.status); } const result = await response.json(); return result; } catch (error) { if (error instanceof SimStudioError) { throw error; } throw new SimStudioError(error?.message || 'Failed to get workflow status', 'STATUS_ERROR'); } } /** * Execute a workflow and poll for completion (useful for long-running workflows) */ async executeWorkflowSync(workflowId, options = {}) { // For now, the API is synchronous, so we just execute directly // In the future, if async execution is added, this method can be enhanced return this.executeWorkflow(workflowId, options); } /** * Validate that a workflow is ready for execution */ async validateWorkflow(workflowId) { try { const status = await this.getWorkflowStatus(workflowId); return status.isDeployed; } catch (error) { return false; } } /** * Set a new API key */ setApiKey(apiKey) { this.apiKey = apiKey; } /** * Set a new base URL */ setBaseUrl(baseUrl) { this.baseUrl = baseUrl.replace(/\/+$/, ''); } } exports.SimStudioClient = SimStudioClient; exports.default = SimStudioClient; //# sourceMappingURL=index.js.map