runpod-sdk 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -22,7 +22,7 @@ const result = await endpoint.runSync({
22
22
  Once an endpoint has been created, you can send requests to the queue:
23
23
 
24
24
  ```js
25
- const requestId = await endpoint.run({
25
+ const { id } = await endpoint.run({
26
26
  input: {
27
27
  prompt: "a photo of a horse the size of a Boeing 787",
28
28
  },
@@ -32,7 +32,7 @@ const requestId = await endpoint.run({
32
32
  You can check on the status of this request once you have the id:
33
33
 
34
34
  ```js
35
- const status = await endpoint.getStatus(requestId);
35
+ const status = await endpoint.getStatus(id);
36
36
  ```
37
37
 
38
38
  If the request has been completed, the status object returned will contain the `output` of the request.
@@ -58,3 +58,20 @@ For long running applications or troubleshooting, you may want to check the heal
58
58
  ```js
59
59
  const health = await endpoint.getHealth();
60
60
  ```
61
+
62
+ Some Runpod endpoints are configured to yield multiple results for a single input, which can be streamed locally:
63
+
64
+ ```js
65
+ const { id } = await endpoint.run({
66
+ input: {
67
+ prompt: "7 photos of a horse the size of a Boeing 787",
68
+ },
69
+ });
70
+
71
+ for await (const result of endpoint.stream(id)) {
72
+ console.log("Stream yielded another photo:");
73
+ console.log(result.output);
74
+ }
75
+ ```
76
+
77
+ (this horse photo endpoint is fictitious, but it shows why you might want a stream of results)
package/dist/index.d.ts CHANGED
@@ -15,6 +15,7 @@ export type EndpointInputPayload = {
15
15
  policy?: ExecutionPolicy;
16
16
  };
17
17
  export type EndpointIncompleteOutput = {
18
+ delayTime?: number;
18
19
  status: string;
19
20
  id: string;
20
21
  };
@@ -26,6 +27,10 @@ export type EndpointCompletedOutput = {
26
27
  delayTime: number;
27
28
  };
28
29
  export type EndpointOutput = EndpointCompletedOutput | EndpointIncompleteOutput;
30
+ export type EndpointStreamOutput = {
31
+ status: string;
32
+ stream: [any];
33
+ };
29
34
  export type CancelOutput = {
30
35
  status: string;
31
36
  id: string;
@@ -52,9 +57,15 @@ export type PurgeQueueOutput = {
52
57
  removed: number;
53
58
  status: string;
54
59
  };
55
- export declare const runSync: import("ts-toolbelt/out/Function/Curry").Curry<(apiKey: string, endpointId: string, request: EndpointInputPayload) => Promise<{
60
+ export type SdkOptions = {
61
+ baseUrl: string;
62
+ };
63
+ export declare const runpodServerlessBaseUrlProd = "https://api.runpod.ai/v2";
64
+ export declare const runpodServerlessBaseUrlDev = "https://dev-api.runpod.ai/v2";
65
+ export declare const runSync: import("ts-toolbelt/out/Function/Curry").Curry<(baseUrl: string, apiKey: string, endpointId: string, request: EndpointInputPayload, timeout?: number) => Promise<{
56
66
  started: boolean;
57
67
  completed: boolean;
68
+ delayTime?: number | undefined;
58
69
  status: string;
59
70
  id: string;
60
71
  } | {
@@ -69,6 +80,7 @@ export declare const runSync: import("ts-toolbelt/out/Function/Curry").Curry<(ap
69
80
  started: boolean;
70
81
  completed: boolean;
71
82
  succeeded: boolean;
83
+ delayTime?: number | undefined;
72
84
  status: string;
73
85
  id: string;
74
86
  } | {
@@ -81,24 +93,30 @@ export declare const runSync: import("ts-toolbelt/out/Function/Curry").Curry<(ap
81
93
  executionTime: number;
82
94
  delayTime: number;
83
95
  }>>;
84
- export declare const run: import("ts-toolbelt/out/Function/Curry").Curry<(apiKey: string, endpointId: string, request: EndpointInputPayload) => Promise<any>>;
85
- export declare const getStatus: import("ts-toolbelt/out/Function/Curry").Curry<(apiKey: string, endpointId: string, requestId: string) => Promise<any>>;
86
- export declare const stream: import("ts-toolbelt/out/Function/Curry").Curry<(apiKey: string, endpointId: string, requestId: string) => Promise<any>>;
87
- export declare const getHealth: import("ts-toolbelt/out/Function/Curry").Curry<(apiKey: string, endpointId: string) => Promise<any>>;
88
- export declare const purgeQueue: import("ts-toolbelt/out/Function/Curry").Curry<(apiKey: string, endpointId: string) => Promise<any>>;
96
+ export declare const run: import("ts-toolbelt/out/Function/Curry").Curry<(baseUrl: string, apiKey: string, endpointId: string, request: EndpointInputPayload, timeout?: number) => Promise<any>>;
97
+ export declare const status: import("ts-toolbelt/out/Function/Curry").Curry<(baseUrl: string, apiKey: string, endpointId: string, requestId: string, timeout?: number) => Promise<any>>;
98
+ export declare function stream(baseUrl: string, apiKey: string, endpointId: string, requestId: string): AsyncGenerator<any, void, unknown>;
99
+ export declare const health: import("ts-toolbelt/out/Function/Curry").Curry<(baseUrl: string, apiKey: string, endpointId: string, timeout?: number) => Promise<any>>;
100
+ export declare const purgeQueue: import("ts-toolbelt/out/Function/Curry").Curry<(baseUrl: string, apiKey: string, endpointId: string, timeout?: number) => Promise<any>>;
89
101
  declare class Endpoint {
90
102
  endpointId: string;
103
+ baseUrl: string;
91
104
  private apiKey;
92
- constructor(apiKey: string, endpointId: string);
93
- runSync(request: EndpointInputPayload): Promise<EndpointCompletedOutput>;
94
- run(request: EndpointInputPayload): Promise<string>;
95
- getStatus(requestId: string): Promise<EndpointOutput>;
96
- stream(requestId: string): Promise<EndpointOutput>;
97
- cancel(requestId: string): Promise<CancelOutput>;
98
- getHealth(): Promise<HealthCheck>;
99
- purgeQueue(): Promise<PurgeQueueOutput>;
105
+ constructor(baseUrl: string, apiKey: string, endpointId: string);
106
+ runSync(request: EndpointInputPayload, timeout?: number): Promise<EndpointCompletedOutput>;
107
+ run(request: EndpointInputPayload, timeout?: number): Promise<string>;
108
+ status(requestId: string, timeout?: number): Promise<EndpointOutput>;
109
+ statusSync(requestId: string, timeout?: number): Promise<EndpointOutput>;
110
+ stream(requestId: string): AsyncGenerator<any>;
111
+ cancel(requestId: string, timeout?: number): Promise<CancelOutput>;
112
+ health(timeout?: number): Promise<HealthCheck>;
113
+ purgeQueue(timeout?: number): Promise<PurgeQueueOutput>;
100
114
  }
101
- declare const _default: (apiKey: string) => {
102
- endpoint: (endpointId: string) => Endpoint;
103
- };
115
+ declare class RunpodSdk {
116
+ private apiKey;
117
+ baseUrl: string;
118
+ constructor(apiKey: string, options: SdkOptions);
119
+ endpoint(endpointId: string): Endpoint | null;
120
+ }
121
+ declare const _default: (apiKey: string, options?: SdkOptions) => RunpodSdk;
104
122
  export default _default;
package/dist/index.js CHANGED
@@ -45,9 +45,20 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
45
45
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
46
46
  }
47
47
  };
48
+ var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
49
+ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
50
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
51
+ var g = generator.apply(thisArg, _arguments || []), i, q = [];
52
+ return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
53
+ function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
54
+ function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
55
+ function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
56
+ function fulfill(value) { resume("next", value); }
57
+ function reject(value) { resume("throw", value); }
58
+ function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
59
+ };
48
60
  import axios from "axios";
49
- import { curry, clamp } from "ramda";
50
- var runpodServerlessBaseUrl = "https://api.runpod.ai/v2";
61
+ import { curry, clamp, isNil } from "ramda";
51
62
  var getAuthHeader = function (apiKey) { return ({
52
63
  headers: {
53
64
  "Authorization": "Bearer ".concat(apiKey),
@@ -55,7 +66,6 @@ var getAuthHeader = function (apiKey) { return ({
55
66
  },
56
67
  }); };
57
68
  var print = console.log;
58
- var sleep = function (ms) { return new Promise(function (resolve) { return setTimeout(resolve, ms); }); };
59
69
  var handleErrors = function (axiosRequest) { return __awaiter(void 0, void 0, void 0, function () {
60
70
  var resp, status, statusText;
61
71
  return __generator(this, function (_a) {
@@ -82,151 +92,236 @@ var handleErrorsStatus = function (axiosRequest) { return __awaiter(void 0, void
82
92
  if (status !== 200) {
83
93
  return [2 /*return*/, { status: status, statusText: statusText, started: false }];
84
94
  }
85
- return [2 /*return*/, __assign(__assign({}, resp.data), { started: true, completed: ["COMPLETED", "FAILED"].includes(resp.data.status), succeeded: resp.data.status === "COMPLETED" })];
95
+ return [2 /*return*/, __assign(__assign({}, resp.data), { started: true, completed: isCompleted(resp.data.status), succeeded: resp.data.status === "COMPLETED" })];
86
96
  }
87
97
  });
88
98
  }); };
89
- var getEndpointUrl = function (endpointId) { return "".concat(runpodServerlessBaseUrl, "/").concat(endpointId); };
99
+ export var runpodServerlessBaseUrlProd = "https://api.runpod.ai/v2";
100
+ export var runpodServerlessBaseUrlDev = "https://dev-api.runpod.ai/v2";
101
+ var getEndpointUrl = curry(function (baseUrl, endpointId) { return "".concat(baseUrl, "/").concat(endpointId); });
102
+ var isCompleted = function (status) {
103
+ return ["COMPLETED", "FAILED", "CANCELLED", "TIMED_OUT"].includes(status);
104
+ };
90
105
  //run and then poll status
91
- export var runSync = curry(function (apiKey, endpointId, request) { return __awaiter(void 0, void 0, void 0, function () {
92
- var maxWaitTimeMs, startTime, getRemainingTime, runResp, data, id, start;
93
- var _a, _b;
94
- return __generator(this, function (_c) {
95
- switch (_c.label) {
96
- case 0:
97
- maxWaitTimeMs = (_b = (_a = request === null || request === void 0 ? void 0 : request.policy) === null || _a === void 0 ? void 0 : _a.executionTimeout) !== null && _b !== void 0 ? _b : 300 * 1000;
98
- startTime = Date.now();
99
- getRemainingTime = function () { return clamp(1000, 90000, maxWaitTimeMs - (Date.now() - startTime)); };
100
- return [4 /*yield*/, runsync(apiKey, endpointId, request)];
101
- case 1:
102
- runResp = _c.sent();
103
- data = __assign({}, runResp);
104
- id = data.id;
105
- start = Date.now();
106
- _c.label = 2;
107
- case 2:
108
- if (!!["COMPLETED", "FAILED"].includes(data.status)) return [3 /*break*/, 4];
109
- if (Date.now() - start > maxWaitTimeMs) {
110
- print("".concat(id, " timed out after ").concat(maxWaitTimeMs / 1000, " seconds"));
111
- return [2 /*return*/, __assign(__assign({}, data), { started: true, completed: false })];
112
- }
113
- return [4 /*yield*/, statusSync(apiKey, endpointId, id, getRemainingTime())];
114
- case 3:
115
- data = _c.sent();
116
- print("".concat(id, ": ").concat(data.status));
117
- return [3 /*break*/, 2];
118
- case 4: return [2 /*return*/, __assign(__assign({}, data), { started: true, completed: true, succeeded: data.status === "COMPLETED" })];
119
- }
106
+ export var runSync = curry(function (baseUrl, apiKey, endpointId, request, timeout) {
107
+ if (timeout === void 0) { timeout = 90000; }
108
+ return __awaiter(void 0, void 0, void 0, function () {
109
+ var startTime, getRemainingTime, runResp, data, id, start;
110
+ return __generator(this, function (_a) {
111
+ switch (_a.label) {
112
+ case 0:
113
+ startTime = Date.now();
114
+ getRemainingTime = function () { return clamp(1000, 90000, timeout - (Date.now() - startTime)); };
115
+ return [4 /*yield*/, runsync(baseUrl, apiKey, endpointId, request, timeout)];
116
+ case 1:
117
+ runResp = _a.sent();
118
+ data = __assign({}, runResp);
119
+ id = data.id;
120
+ start = Date.now();
121
+ _a.label = 2;
122
+ case 2:
123
+ if (!!isCompleted(data.status)) return [3 /*break*/, 4];
124
+ if (Date.now() - start > timeout) {
125
+ print("".concat(id, " timed out after ").concat(timeout / 1000, " seconds"));
126
+ return [2 /*return*/, __assign(__assign({}, data), { started: true, completed: false })];
127
+ }
128
+ return [4 /*yield*/, statusSync(baseUrl, apiKey, endpointId, id, getRemainingTime())];
129
+ case 3:
130
+ data = _a.sent();
131
+ print("".concat(id, ": ").concat(data.status));
132
+ return [3 /*break*/, 2];
133
+ case 4: return [2 /*return*/, __assign(__assign({}, data), { started: true, completed: true, succeeded: data.status === "COMPLETED" })];
134
+ }
135
+ });
120
136
  });
121
- }); });
137
+ });
122
138
  //wrapper over /status-sync
123
- var statusSync = curry(function (apiKey, endpointId, requestId, wait) {
124
- var url = getEndpointUrl(endpointId) + "/status-sync/" + requestId + "?wait=".concat(wait);
139
+ var statusSync = curry(function (baseUrl, apiKey, endpointId, requestId, timeout) {
140
+ if (timeout === void 0) { timeout = 90000; }
141
+ var wait = clamp(1000, 90000, timeout);
142
+ console.log("statusSync wait", wait);
143
+ var url = getEndpointUrl(baseUrl, endpointId) + "/status-sync/" + requestId + "?wait=".concat(wait);
125
144
  var authHeader = getAuthHeader(apiKey);
126
- return handleErrorsStatus(axios.get(url, authHeader));
145
+ return handleErrorsStatus(axios.get(url, __assign({}, authHeader)));
127
146
  });
128
147
  //wrapper over /runsync
129
- var runsync = curry(function (apiKey, endpointId, request) {
130
- var url = getEndpointUrl(endpointId) + "/runsync";
148
+ var runsync = curry(function (baseUrl, apiKey, endpointId, request, timeout) {
149
+ if (timeout === void 0) { timeout = 90000; }
150
+ var wait = clamp(1000, 90000, timeout);
151
+ console.log("runsync wait:", wait);
152
+ var url = getEndpointUrl(baseUrl, endpointId) + "/runsync" + "?wait=".concat(wait);
131
153
  var authHeader = getAuthHeader(apiKey);
132
- return handleErrorsStatus(axios.post(url, request, authHeader));
154
+ return handleErrorsStatus(axios.post(url, request, __assign({}, authHeader)));
133
155
  });
134
156
  //wrapper over /run
135
- export var run = curry(function (apiKey, endpointId, request) {
136
- var url = getEndpointUrl(endpointId) + "/run";
157
+ export var run = curry(function (baseUrl, apiKey, endpointId, request, timeout) {
158
+ if (timeout === void 0) { timeout = 3000; }
159
+ var url = getEndpointUrl(baseUrl, endpointId) + "/run";
137
160
  var authHeader = getAuthHeader(apiKey);
138
- return handleErrors(axios.post(url, request, authHeader));
161
+ return handleErrors(axios.post(url, request, __assign(__assign({}, authHeader), { timeout: timeout })));
139
162
  });
140
163
  //wrapper over /status
141
- export var getStatus = curry(function (apiKey, endpointId, requestId) {
142
- var url = getEndpointUrl(endpointId) + "/status/" + requestId;
164
+ export var status = curry(function (baseUrl, apiKey, endpointId, requestId, timeout) {
165
+ if (timeout === void 0) { timeout = 3000; }
166
+ var url = getEndpointUrl(baseUrl, endpointId) + "/status/" + requestId;
143
167
  var authHeader = getAuthHeader(apiKey);
144
168
  return handleErrorsStatus(axios.get(url, authHeader));
145
169
  });
146
- //wrapper over /stream
147
- export var stream = curry(function (apiKey, endpointId, requestId) {
148
- var url = getEndpointUrl(endpointId) + "/stream/" + requestId;
149
- var authHeader = getAuthHeader(apiKey);
150
- return handleErrors(axios.get(url, authHeader));
151
- });
170
+ //generator yielding results of stream
171
+ export function stream(baseUrl, apiKey, endpointId, requestId) {
172
+ return __asyncGenerator(this, arguments, function stream_1() {
173
+ var completed, url, authHeader, resp, _i, _a, output;
174
+ return __generator(this, function (_b) {
175
+ switch (_b.label) {
176
+ case 0:
177
+ completed = false;
178
+ _b.label = 1;
179
+ case 1:
180
+ if (!!completed) return [3 /*break*/, 8];
181
+ url = getEndpointUrl(baseUrl, endpointId) + "/stream/" + requestId;
182
+ authHeader = getAuthHeader(apiKey);
183
+ return [4 /*yield*/, __await(handleErrors(axios.get(url, authHeader)))];
184
+ case 2:
185
+ resp = _b.sent();
186
+ if (isCompleted(resp.status)) {
187
+ completed = true;
188
+ }
189
+ _i = 0, _a = resp === null || resp === void 0 ? void 0 : resp.stream;
190
+ _b.label = 3;
191
+ case 3:
192
+ if (!(_i < _a.length)) return [3 /*break*/, 7];
193
+ output = _a[_i];
194
+ return [4 /*yield*/, __await(output)];
195
+ case 4: return [4 /*yield*/, _b.sent()];
196
+ case 5:
197
+ _b.sent();
198
+ _b.label = 6;
199
+ case 6:
200
+ _i++;
201
+ return [3 /*break*/, 3];
202
+ case 7: return [3 /*break*/, 1];
203
+ case 8: return [2 /*return*/];
204
+ }
205
+ });
206
+ });
207
+ }
152
208
  //wrapper over /cancel
153
- var cancel = curry(function (apiKey, endpointId, requestId) {
154
- var url = getEndpointUrl(endpointId) + "/cancel/" + requestId;
209
+ var cancel = curry(function (baseUrl, apiKey, endpointId, requestId, timeout) {
210
+ if (timeout === void 0) { timeout = 3000; }
211
+ var url = getEndpointUrl(baseUrl, endpointId) + "/cancel/" + requestId;
155
212
  var authHeader = getAuthHeader(apiKey);
156
- return handleErrors(axios.post(url, {}, authHeader));
213
+ return handleErrors(axios.post(url, {}, __assign(__assign({}, authHeader), { timeout: timeout })));
157
214
  });
158
215
  //wrapper over /health
159
- export var getHealth = curry(function (apiKey, endpointId) {
160
- var url = getEndpointUrl(endpointId) + "/health";
216
+ export var health = curry(function (baseUrl, apiKey, endpointId, timeout) {
217
+ if (timeout === void 0) { timeout = 3000; }
218
+ var url = getEndpointUrl(baseUrl, endpointId) + "/health";
161
219
  var authHeader = getAuthHeader(apiKey);
162
- return handleErrors(axios.get(url, authHeader));
220
+ return handleErrors(axios.get(url, __assign(__assign({}, authHeader), { timeout: timeout })));
163
221
  });
164
222
  //wrapper over /purge-queue
165
- export var purgeQueue = curry(function (apiKey, endpointId) {
166
- var url = getEndpointUrl(endpointId) + "/purge-queue";
223
+ export var purgeQueue = curry(function (baseUrl, apiKey, endpointId, timeout) {
224
+ if (timeout === void 0) { timeout = 3000; }
225
+ var url = getEndpointUrl(baseUrl, endpointId) + "/purge-queue";
167
226
  var authHeader = getAuthHeader(apiKey);
168
- return handleErrors(axios.post(url, {}, authHeader));
227
+ return handleErrors(axios.post(url, {}, __assign(__assign({}, authHeader), { timeout: timeout })));
169
228
  });
170
229
  var Endpoint = /** @class */ (function () {
171
- function Endpoint(apiKey, endpointId) {
230
+ function Endpoint(baseUrl, apiKey, endpointId) {
172
231
  this.endpointId = "";
232
+ this.baseUrl = runpodServerlessBaseUrlProd;
173
233
  this.apiKey = "";
234
+ this.baseUrl = baseUrl;
174
235
  this.apiKey = apiKey;
175
236
  this.endpointId = endpointId;
176
237
  }
177
- Endpoint.prototype.runSync = function (request) {
238
+ Endpoint.prototype.runSync = function (request, timeout) {
239
+ if (timeout === void 0) { timeout = 90000; }
178
240
  return __awaiter(this, void 0, void 0, function () {
179
241
  return __generator(this, function (_a) {
180
- return [2 /*return*/, runSync(this.apiKey, this.endpointId, request)];
242
+ return [2 /*return*/, runSync(this.baseUrl, this.apiKey, this.endpointId, request, timeout)];
181
243
  });
182
244
  });
183
245
  };
184
- Endpoint.prototype.run = function (request) {
246
+ Endpoint.prototype.run = function (request, timeout) {
247
+ if (timeout === void 0) { timeout = 3000; }
185
248
  return __awaiter(this, void 0, void 0, function () {
186
249
  return __generator(this, function (_a) {
187
- return [2 /*return*/, run(this.apiKey, this.endpointId, request)];
250
+ return [2 /*return*/, run(this.baseUrl, this.apiKey, this.endpointId, request, timeout)];
188
251
  });
189
252
  });
190
253
  };
191
- Endpoint.prototype.getStatus = function (requestId) {
254
+ Endpoint.prototype.status = function (requestId, timeout) {
255
+ if (timeout === void 0) { timeout = 3000; }
192
256
  return __awaiter(this, void 0, void 0, function () {
193
257
  return __generator(this, function (_a) {
194
- return [2 /*return*/, getStatus(this.apiKey, this.endpointId, requestId)];
258
+ return [2 /*return*/, status(this.baseUrl, this.apiKey, this.endpointId, requestId, timeout)];
195
259
  });
196
260
  });
197
261
  };
198
- Endpoint.prototype.stream = function (requestId) {
262
+ Endpoint.prototype.statusSync = function (requestId, timeout) {
263
+ if (timeout === void 0) { timeout = 90000; }
199
264
  return __awaiter(this, void 0, void 0, function () {
200
265
  return __generator(this, function (_a) {
201
- return [2 /*return*/, stream(this.apiKey, this.endpointId, requestId)];
266
+ return [2 /*return*/, statusSync(this.baseUrl, this.apiKey, this.endpointId, requestId, timeout)];
202
267
  });
203
268
  });
204
269
  };
205
- Endpoint.prototype.cancel = function (requestId) {
270
+ Endpoint.prototype.stream = function (requestId) {
271
+ return stream(this.baseUrl, this.apiKey, this.endpointId, requestId);
272
+ };
273
+ Endpoint.prototype.cancel = function (requestId, timeout) {
274
+ if (timeout === void 0) { timeout = 3000; }
206
275
  return __awaiter(this, void 0, void 0, function () {
207
276
  return __generator(this, function (_a) {
208
- return [2 /*return*/, cancel(this.apiKey, this.endpointId, requestId)];
277
+ return [2 /*return*/, cancel(this.baseUrl, this.apiKey, this.endpointId, requestId, timeout)];
209
278
  });
210
279
  });
211
280
  };
212
- Endpoint.prototype.getHealth = function () {
281
+ Endpoint.prototype.health = function (timeout) {
282
+ if (timeout === void 0) { timeout = 3000; }
213
283
  return __awaiter(this, void 0, void 0, function () {
214
284
  return __generator(this, function (_a) {
215
- return [2 /*return*/, getHealth(this.apiKey, this.endpointId)];
285
+ return [2 /*return*/, health(this.baseUrl, this.apiKey, this.endpointId, timeout)];
216
286
  });
217
287
  });
218
288
  };
219
- Endpoint.prototype.purgeQueue = function () {
289
+ Endpoint.prototype.purgeQueue = function (timeout) {
290
+ if (timeout === void 0) { timeout = 3000; }
220
291
  return __awaiter(this, void 0, void 0, function () {
221
292
  return __generator(this, function (_a) {
222
- return [2 /*return*/, purgeQueue(this.apiKey, this.endpointId)];
293
+ return [2 /*return*/, purgeQueue(this.baseUrl, this.apiKey, this.endpointId, timeout)];
223
294
  });
224
295
  });
225
296
  };
226
297
  return Endpoint;
227
298
  }());
228
- export default (function (apiKey) { return ({
229
- endpoint: function (endpointId) { return new Endpoint(apiKey, endpointId); },
230
- //template...
231
- //pod...
232
- }); });
299
+ var defaultSdkOptions = {
300
+ baseUrl: runpodServerlessBaseUrlProd,
301
+ };
302
+ var RunpodSdk = /** @class */ (function () {
303
+ function RunpodSdk(apiKey, options) {
304
+ var _a;
305
+ this.apiKey = "";
306
+ this.baseUrl = runpodServerlessBaseUrlProd;
307
+ if (isNil(apiKey)) {
308
+ print("Api key not supplied");
309
+ return;
310
+ }
311
+ this.apiKey = apiKey;
312
+ this.baseUrl = (_a = options.baseUrl) !== null && _a !== void 0 ? _a : this.baseUrl;
313
+ }
314
+ RunpodSdk.prototype.endpoint = function (endpointId) {
315
+ if (isNil(endpointId)) {
316
+ print("Endpoint id not supplied");
317
+ return null;
318
+ }
319
+ return new Endpoint(this.baseUrl, this.apiKey, endpointId);
320
+ };
321
+ return RunpodSdk;
322
+ }());
323
+ export default (function (apiKey, options) {
324
+ if (options === void 0) { options = defaultSdkOptions; }
325
+ return new RunpodSdk(apiKey, options);
326
+ });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AC,IAAI,CAAC,OAAO,EAAE,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,UAAU,EAAE,OAAO,CAAC,EAAA;;;KACvE;IACH,eAAC;AAAD,CAAC,AApCD,IAoCC;AAED,IAAM,iBAAiB,GAAG;IACxB,OAAO,EAAE,2BAA2B;CACrC,CAAA;AACD;IAGE,mBAAY,MAAc,EAAE,OAAmB;;QAFvC,WAAM,GAAW,EAAE,CAAA;QAC3B,YAAO,GAAW,2BAA2B,CAAA;QAE3C,IAAI,KAAK,CAAC,MAAM,CAAC,EAAE;YACjB,KAAK,CAAC,sBAAsB,CAAC,CAAA;YAC7B,OAAM;SACP;QACD,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;QACpB,IAAI,CAAC,OAAO,GAAG,MAAA,OAAO,CAAC,OAAO,mCAAI,IAAI,CAAC,OAAO,CAAA;IAChD,CAAC;IACD,4BAAQ,GAAR,UAAS,UAAkB;QACzB,IAAI,KAAK,CAAC,UAAU,CAAC,EAAE;YACrB,KAAK,CAAC,0BAA0B,CAAC,CAAA;YACjC,OAAO,IAAI,CAAA;SACZ;QACD,OAAO,IAAI,QAAQ,CAAC,IAAI,CAAC,OAAO,EAAE,IAAI,CAAC,MAAM,EAAE,UAAU,CAAC,CAAA;IAC5D,CAAC;IAGH,gBAAC;AAAD,CAAC,AApBD,IAoBC;AACD,gBAAe,UAAC,MAAc,EAAE,OAAuC;IAAvC,wBAAA,EAAA,2BAAuC;IACrE,OAAA,IAAI,SAAS,CAAC,MAAM,EAAE,OAAO,CAAC;AAA9B,CAA8B,EAAA"}
@@ -0,0 +1,19 @@
1
+ import runpodSdk from "../dist/index.js"
2
+ const { RUNPOD_API_KEY } = process.env
3
+ if (!RUNPOD_API_KEY) {
4
+ console.log("please supply RUNPOD_API_KEY as an environment variable")
5
+ process.exit()
6
+ }
7
+
8
+ const runpod = runpodSdk(RUNPOD_API_KEY)
9
+ const endpoint = runpod.endpoint("faster-whisper")
10
+
11
+ const input = {
12
+ input: {
13
+ audio: "https://github.com/runpod-workers/sample-inputs/raw/main/audio/gettysburg.wav",
14
+ model: "base",
15
+ },
16
+ }
17
+ const result = await endpoint.runSync(input)
18
+
19
+ console.log(result)
@@ -0,0 +1,27 @@
1
+ import runpodSdk from "../dist/index.js"
2
+ const { RUNPOD_API_KEY } = process.env
3
+ if (!RUNPOD_API_KEY) {
4
+ console.log("please supply RUNPOD_API_KEY as an environment variable")
5
+ process.exit()
6
+ }
7
+
8
+ const runpod = runpodSdk(RUNPOD_API_KEY)
9
+ const endpoint = runpod.endpoint("faster-whisper")
10
+ const input = {
11
+ input: {
12
+ audio: "https://github.com/runpod-workers/sample-inputs/raw/main/audio/gettysburg.wav",
13
+ model: "base",
14
+ },
15
+ }
16
+ const result = await endpoint.run(input)
17
+ console.log("run resp")
18
+ console.log(result)
19
+
20
+ const { id } = result
21
+ const cancelResp = await endpoint.cancel(id)
22
+ console.log("cancel resp")
23
+ console.log(cancelResp)
24
+
25
+ const statusResp = await endpoint.status(id)
26
+ console.log("status after cancellation")
27
+ console.log(statusResp)
@@ -0,0 +1,17 @@
1
+ import runpodSdk, { runpodServerlessBaseUrlDev } from "../dist/index.js"
2
+ const { RUNPOD_API_KEY } = process.env
3
+ if (!RUNPOD_API_KEY) {
4
+ console.log("please supply RUNPOD_API_KEY as an environment variable")
5
+ process.exit()
6
+ }
7
+
8
+ const runpod = runpodSdk(RUNPOD_API_KEY, { baseUrl: runpodServerlessBaseUrlDev })
9
+ const endpoint = runpod.endpoint("faster-whisper")
10
+ const input = {
11
+ input: {
12
+ audio: "https://github.com/runpod-workers/sample-inputs/raw/main/audio/gettysburg.wav",
13
+ model: "base",
14
+ },
15
+ }
16
+ const result = await endpoint.runSync(input)
17
+ console.log(result)
@@ -0,0 +1,11 @@
1
+ import runpodSdk from "../dist/index.js"
2
+ const { RUNPOD_API_KEY, ENDPOINT_ID } = process.env
3
+ if (!RUNPOD_API_KEY || !ENDPOINT_ID) {
4
+ console.log("please supply RUNPOD_API_KEY as an environment variable")
5
+ process.exit()
6
+ }
7
+
8
+ const runpod = runpodSdk(RUNPOD_API_KEY)
9
+ const endpoint = runpod.endpoint(ENDPOINT_ID)
10
+ const result = await endpoint.health()
11
+ console.log(result)
@@ -0,0 +1,11 @@
1
+ import runpodSdk from "../dist/index.js"
2
+ const { RUNPOD_API_KEY } = process.env
3
+ if (!RUNPOD_API_KEY) {
4
+ console.log("please supply RUNPOD_API_KEY as an environment variable")
5
+ process.exit()
6
+ }
7
+
8
+ const runpod = runpodSdk(RUNPOD_API_KEY)
9
+ const endpoint = runpod.endpoint("faster-whisper")
10
+ const result = await endpoint.health()
11
+ console.log(result)
@@ -0,0 +1,11 @@
1
+ import runpodSdk from "../dist/index.js"
2
+ const { RUNPOD_API_KEY, ENDPOINT_ID } = process.env
3
+ if (!RUNPOD_API_KEY || !ENDPOINT_ID) {
4
+ console.log("please supply RUNPOD_API_KEY as an environment variable")
5
+ process.exit()
6
+ }
7
+
8
+ const runpod = runpodSdk(RUNPOD_API_KEY)
9
+ const endpoint = runpod.endpoint(ENDPOINT_ID)
10
+ const result = await endpoint.purgeQueue()
11
+ console.log(result)
@@ -0,0 +1,27 @@
1
+ import runpodSdk from "../dist/index.js"
2
+ const { RUNPOD_API_KEY } = process.env
3
+ if (!RUNPOD_API_KEY) {
4
+ console.log("please supply RUNPOD_API_KEY as an environment variable")
5
+ process.exit()
6
+ }
7
+
8
+ const runpod = runpodSdk(RUNPOD_API_KEY)
9
+ const endpoint = runpod.endpoint("faster-whisper")
10
+ const input = {
11
+ input: {
12
+ audio: "https://github.com/runpod-workers/sample-inputs/raw/main/audio/gettysburg.wav",
13
+ model: "base",
14
+ },
15
+ }
16
+ const result = await endpoint.run(input)
17
+ console.log("run response")
18
+ console.log(result)
19
+
20
+ const { id } = result
21
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
22
+ for (let i = 0; i < 5; i++) {
23
+ const status = await endpoint.status(id)
24
+ console.log("status response")
25
+ console.log(status)
26
+ await sleep(1000)
27
+ }
@@ -0,0 +1,17 @@
1
+ import runpodSdk from "../dist/index.js"
2
+ const { RUNPOD_API_KEY } = process.env
3
+ if (!RUNPOD_API_KEY) {
4
+ console.log("please supply RUNPOD_API_KEY as an environment variable")
5
+ process.exit()
6
+ }
7
+
8
+ const runpod = runpodSdk(RUNPOD_API_KEY)
9
+ const endpoint = runpod.endpoint("faster-whisper")
10
+ const input = {
11
+ input: {
12
+ audio: "https://github.com/runpod-workers/sample-inputs/raw/main/audio/gettysburg.wav",
13
+ model: "base",
14
+ },
15
+ }
16
+ const result = await endpoint.runSync(input, 3000)
17
+ console.log(result)
@@ -0,0 +1,48 @@
1
+ import runpodSdk from "runpod-sdk"
2
+
3
+ const { RUNPOD_API_KEY } = process.env
4
+ if (!RUNPOD_API_KEY) {
5
+ console.log("please supply RUNPOD_API_KEY as an environment variable")
6
+ process.exit()
7
+ }
8
+ const runpod = runpodSdk(RUNPOD_API_KEY)
9
+ //mock endpoint which returns whatever you specify as a stream
10
+ //with a specified delay between inputs
11
+ const endpoint = runpod.endpoint("gwp4kx5yd3nur1")
12
+
13
+ const request = {
14
+ input: {
15
+ mock_return: ["a", "b", "c", "d", "e", "f", "g"],
16
+ mock_delay: 2,
17
+ },
18
+ }
19
+ const runResp = await endpoint.run(request)
20
+ const { id } = runResp
21
+ for await (const result of endpoint.stream(id)) {
22
+ console.log(`stream yielded ${JSON.stringify(result, null, 2)}`)
23
+ }
24
+
25
+ // Expected output
26
+ /*
27
+ stream yielded {
28
+ "output": "a",
29
+ }
30
+ stream yielded {
31
+ "output": "b"
32
+ }
33
+ stream yielded {
34
+ "output": "c"
35
+ }
36
+ stream yielded {
37
+ "output": "d"
38
+ }
39
+ stream yielded {
40
+ "output": "e"
41
+ }
42
+ stream yielded {
43
+ "output": "f"
44
+ }
45
+ stream yielded {
46
+ "output": "g"
47
+ }
48
+ */
package/examples/test.js CHANGED
@@ -1,20 +1,16 @@
1
1
  import runpodSdk from "../dist/index.js"
2
2
 
3
3
  const { RUNPOD_API_KEY, ENDPOINT_ID } = process.env
4
- if (!(RUNPOD_API_KEY && ENDPOINT_ID)) {
5
- console.log("please supply RUNPOD_API_KEY and ENDPOINT_ID as environment variables")
4
+ if (!RUNPOD_API_KEY || !ENDPOINT_ID) {
5
+ console.log("please supply RUNPOD_API_KEY and ENDPOINT_ID as an environment variable")
6
6
  process.exit()
7
7
  }
8
- //OOP
9
8
  const runpod = runpodSdk(RUNPOD_API_KEY)
10
9
  const endpoint = runpod.endpoint(ENDPOINT_ID)
10
+
11
11
  const request = {
12
12
  input: {
13
- delay: 100,
14
- name: "nate",
15
- },
16
- policy: {
17
- executionTimeout: 120 * 1000,
13
+ prompt: "photo of a horse",
18
14
  },
19
15
  }
20
16
 
@@ -23,20 +19,40 @@ const runResp = await endpoint.run(request)
23
19
  console.log(runResp)
24
20
  const { id: requestId } = runResp
25
21
  console.log("\nstatus")
26
- const statusResp = await endpoint.getStatus(requestId)
22
+ const statusResp = await endpoint.status(requestId)
27
23
  console.log(statusResp)
28
24
  console.log("\ncancel")
29
25
  const cancelResp = await endpoint.cancel(requestId)
30
26
  console.log(cancelResp)
31
27
  console.log("\nhealth")
32
- const healthResp = await endpoint.getHealth()
28
+ const healthResp = await endpoint.health()
33
29
  console.log(healthResp)
34
30
  console.log("\npurge queue")
35
31
  const purgeResp = await endpoint.purgeQueue()
36
32
  console.log(purgeResp)
37
33
  console.log("\nafter purge")
38
- console.log(await endpoint.getHealth())
34
+ console.log(await endpoint.health())
39
35
 
40
36
  console.log("runSync")
41
37
  const result = await endpoint.runSync(request)
42
38
  console.log(result)
39
+
40
+ const failingRequest = {
41
+ input: {
42
+ bogus_key: "photo of a horse",
43
+ },
44
+ }
45
+ console.log("runSync failing")
46
+ const failingResult = await endpoint.runSync(failingRequest)
47
+ console.log(failingResult)
48
+
49
+ const slowRequest = {
50
+ input: {
51
+ prompt: "photo of a horse",
52
+ num_inference_steps: 200,
53
+ },
54
+ }
55
+
56
+ console.log("runSync timed out")
57
+ const timedOutResult = await endpoint.runSync(slowRequest, 3000)
58
+ console.log(timedOutResult)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "runpod-sdk",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "JavaScript SDK for Runpod",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/index.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import axios, { AxiosResponse } from "axios"
2
- import { curry, clamp } from "ramda"
2
+ import { curry, clamp, isNil } from "ramda"
3
3
  export type ExecutionPolicy = {
4
4
  ttl?: number
5
5
  executionTimeout?: number
@@ -17,6 +17,7 @@ export type EndpointInputPayload = {
17
17
  policy?: ExecutionPolicy
18
18
  }
19
19
  export type EndpointIncompleteOutput = {
20
+ delayTime?: number
20
21
  status: string
21
22
  id: string
22
23
  }
@@ -30,6 +31,11 @@ export type EndpointCompletedOutput = {
30
31
 
31
32
  export type EndpointOutput = EndpointCompletedOutput | EndpointIncompleteOutput
32
33
 
34
+ export type EndpointStreamOutput = {
35
+ status: string
36
+ stream: [any]
37
+ }
38
+
33
39
  export type CancelOutput = {
34
40
  status: string
35
41
  id: string
@@ -47,7 +53,10 @@ export type PurgeQueueOutput = {
47
53
  status: string
48
54
  }
49
55
 
50
- const runpodServerlessBaseUrl = "https://api.runpod.ai/v2"
56
+ export type SdkOptions = {
57
+ baseUrl: string
58
+ }
59
+
51
60
  const getAuthHeader = (apiKey: string) => ({
52
61
  headers: {
53
62
  "Authorization": `Bearer ${apiKey}`,
@@ -55,7 +64,6 @@ const getAuthHeader = (apiKey: string) => ({
55
64
  },
56
65
  })
57
66
  const print = console.log
58
- const sleep = (ms: any) => new Promise((resolve: any) => setTimeout(resolve, ms))
59
67
 
60
68
  const handleErrors = async (axiosRequest: Promise<AxiosResponse>) => {
61
69
  const resp = await axiosRequest
@@ -74,29 +82,37 @@ const handleErrorsStatus = async (axiosRequest: Promise<AxiosResponse>) => {
74
82
  return {
75
83
  ...resp.data,
76
84
  started: true,
77
- completed: ["COMPLETED", "FAILED"].includes(resp.data.status),
85
+ completed: isCompleted(resp.data.status),
78
86
  succeeded: resp.data.status === "COMPLETED",
79
87
  }
80
88
  }
81
-
82
- const getEndpointUrl = (endpointId: string) => `${runpodServerlessBaseUrl}/${endpointId}`
89
+ export const runpodServerlessBaseUrlProd = "https://api.runpod.ai/v2"
90
+ export const runpodServerlessBaseUrlDev = "https://dev-api.runpod.ai/v2"
91
+ const getEndpointUrl = curry((baseUrl, endpointId: string) => `${baseUrl}/${endpointId}`)
92
+ const isCompleted = (status: string) =>
93
+ ["COMPLETED", "FAILED", "CANCELLED", "TIMED_OUT"].includes(status)
83
94
 
84
95
  //run and then poll status
85
96
  export const runSync = curry(
86
- async (apiKey: string, endpointId: string, request: EndpointInputPayload) => {
87
- const maxWaitTimeMs = request?.policy?.executionTimeout ?? 300 * 1000
97
+ async (
98
+ baseUrl: string,
99
+ apiKey: string,
100
+ endpointId: string,
101
+ request: EndpointInputPayload,
102
+ timeout: number = 90000
103
+ ) => {
88
104
  const startTime = Date.now()
89
- const getRemainingTime = () => clamp(1000, 90000, maxWaitTimeMs - (Date.now() - startTime))
90
- const runResp: any = await runsync(apiKey, endpointId, request)
105
+ const getRemainingTime = () => clamp(1000, 90000, timeout - (Date.now() - startTime))
106
+ const runResp: any = await runsync(baseUrl, apiKey, endpointId, request, timeout)
91
107
  let data: EndpointOutput = { ...runResp }
92
108
  const { id } = data
93
109
  const start = Date.now()
94
- while (!["COMPLETED", "FAILED"].includes(data.status)) {
95
- if (Date.now() - start > maxWaitTimeMs) {
96
- print(`${id} timed out after ${maxWaitTimeMs / 1000} seconds`)
110
+ while (!isCompleted(data.status)) {
111
+ if (Date.now() - start > timeout) {
112
+ print(`${id} timed out after ${timeout / 1000} seconds`)
97
113
  return { ...data, started: true, completed: false }
98
114
  }
99
- data = await statusSync(apiKey, endpointId, id, getRemainingTime())
115
+ data = await statusSync(baseUrl, apiKey, endpointId, id, getRemainingTime())
100
116
  print(`${id}: ${data.status}`)
101
117
  }
102
118
  return { ...data, started: true, completed: true, succeeded: data.status === "COMPLETED" }
@@ -104,88 +120,181 @@ export const runSync = curry(
104
120
  )
105
121
 
106
122
  //wrapper over /status-sync
107
- const statusSync = curry((apiKey: string, endpointId: string, requestId: String, wait: number) => {
108
- const url = getEndpointUrl(endpointId) + "/status-sync/" + requestId + `?wait=${wait}`
109
- const authHeader = getAuthHeader(apiKey)
110
- return handleErrorsStatus(axios.get(url, authHeader))
111
- })
123
+ const statusSync = curry(
124
+ (
125
+ baseUrl: string,
126
+ apiKey: string,
127
+ endpointId: string,
128
+ requestId: String,
129
+ timeout: number = 90000
130
+ ) => {
131
+ const wait = clamp(1000, 90000, timeout)
132
+ console.log("statusSync wait", wait)
133
+ const url = getEndpointUrl(baseUrl, endpointId) + "/status-sync/" + requestId + `?wait=${wait}`
134
+ const authHeader = getAuthHeader(apiKey)
135
+ return handleErrorsStatus(axios.get(url, { ...authHeader }))
136
+ }
137
+ )
112
138
 
113
139
  //wrapper over /runsync
114
- const runsync = curry((apiKey: string, endpointId: string, request: EndpointInputPayload) => {
115
- const url = getEndpointUrl(endpointId) + "/runsync"
116
- const authHeader = getAuthHeader(apiKey)
117
- return handleErrorsStatus(axios.post(url, request, authHeader))
118
- })
140
+ const runsync = curry(
141
+ (
142
+ baseUrl: string,
143
+ apiKey: string,
144
+ endpointId: string,
145
+ request: EndpointInputPayload,
146
+ timeout: number = 90000
147
+ ) => {
148
+ const wait = clamp(1000, 90000, timeout)
149
+ console.log("runsync wait:", wait)
150
+ const url = getEndpointUrl(baseUrl, endpointId) + "/runsync" + `?wait=${wait}`
151
+ const authHeader = getAuthHeader(apiKey)
152
+ return handleErrorsStatus(axios.post(url, request, { ...authHeader }))
153
+ }
154
+ )
119
155
 
120
156
  //wrapper over /run
121
- export const run = curry((apiKey: string, endpointId: string, request: EndpointInputPayload) => {
122
- const url = getEndpointUrl(endpointId) + "/run"
123
- const authHeader = getAuthHeader(apiKey)
124
- return handleErrors(axios.post(url, request, authHeader))
125
- })
157
+ export const run = curry(
158
+ (
159
+ baseUrl: string,
160
+ apiKey: string,
161
+ endpointId: string,
162
+ request: EndpointInputPayload,
163
+ timeout: number = 3000
164
+ ) => {
165
+ const url = getEndpointUrl(baseUrl, endpointId) + "/run"
166
+ const authHeader = getAuthHeader(apiKey)
167
+ return handleErrors(axios.post(url, request, { ...authHeader, timeout }))
168
+ }
169
+ )
126
170
  //wrapper over /status
127
- export const getStatus = curry((apiKey: string, endpointId: string, requestId: string) => {
128
- const url = getEndpointUrl(endpointId) + "/status/" + requestId
129
- const authHeader = getAuthHeader(apiKey)
130
- return handleErrorsStatus(axios.get(url, authHeader))
131
- })
132
- //wrapper over /stream
133
- export const stream = curry((apiKey: string, endpointId: string, requestId: string) => {
134
- const url = getEndpointUrl(endpointId) + "/stream/" + requestId
135
- const authHeader = getAuthHeader(apiKey)
136
- return handleErrors(axios.get(url, authHeader))
137
- })
171
+ export const status = curry(
172
+ (
173
+ baseUrl: string,
174
+ apiKey: string,
175
+ endpointId: string,
176
+ requestId: string,
177
+ timeout: number = 3000
178
+ ) => {
179
+ const url = getEndpointUrl(baseUrl, endpointId) + "/status/" + requestId
180
+ const authHeader = getAuthHeader(apiKey)
181
+ return handleErrorsStatus(axios.get(url, authHeader))
182
+ }
183
+ )
184
+
185
+ //generator yielding results of stream
186
+ export async function* stream(
187
+ baseUrl: string,
188
+ apiKey: string,
189
+ endpointId: string,
190
+ requestId: string
191
+ ) {
192
+ let completed = false
193
+ while (!completed) {
194
+ const url = getEndpointUrl(baseUrl, endpointId) + "/stream/" + requestId
195
+ const authHeader = getAuthHeader(apiKey)
196
+ const resp = await handleErrors(axios.get(url, authHeader))
197
+ if (isCompleted(resp.status)) {
198
+ completed = true
199
+ }
200
+ for (const output of resp?.stream) {
201
+ yield output
202
+ }
203
+ }
204
+ }
205
+
138
206
  //wrapper over /cancel
139
- const cancel = curry((apiKey: string, endpointId: string, requestId: string) => {
140
- const url = getEndpointUrl(endpointId) + "/cancel/" + requestId
141
- const authHeader = getAuthHeader(apiKey)
142
- return handleErrors(axios.post(url, {}, authHeader))
143
- })
207
+ const cancel = curry(
208
+ (
209
+ baseUrl: string,
210
+ apiKey: string,
211
+ endpointId: string,
212
+ requestId: string,
213
+ timeout: number = 3000
214
+ ) => {
215
+ const url = getEndpointUrl(baseUrl, endpointId) + "/cancel/" + requestId
216
+ const authHeader = getAuthHeader(apiKey)
217
+ return handleErrors(axios.post(url, {}, { ...authHeader, timeout }))
218
+ }
219
+ )
144
220
  //wrapper over /health
145
- export const getHealth = curry((apiKey: string, endpointId: string) => {
146
- const url = getEndpointUrl(endpointId) + "/health"
147
- const authHeader = getAuthHeader(apiKey)
148
- return handleErrors(axios.get(url, authHeader))
149
- })
221
+ export const health = curry(
222
+ (baseUrl: string, apiKey: string, endpointId: string, timeout: number = 3000) => {
223
+ const url = getEndpointUrl(baseUrl, endpointId) + "/health"
224
+ const authHeader = getAuthHeader(apiKey)
225
+ return handleErrors(axios.get(url, { ...authHeader, timeout }))
226
+ }
227
+ )
150
228
  //wrapper over /purge-queue
151
- export const purgeQueue = curry((apiKey: string, endpointId: string) => {
152
- const url = getEndpointUrl(endpointId) + "/purge-queue"
153
- const authHeader = getAuthHeader(apiKey)
154
- return handleErrors(axios.post(url, {}, authHeader))
155
- })
229
+ export const purgeQueue = curry(
230
+ (baseUrl: string, apiKey: string, endpointId: string, timeout: number = 3000) => {
231
+ const url = getEndpointUrl(baseUrl, endpointId) + "/purge-queue"
232
+ const authHeader = getAuthHeader(apiKey)
233
+ return handleErrors(axios.post(url, {}, { ...authHeader, timeout }))
234
+ }
235
+ )
156
236
 
157
237
  class Endpoint {
158
238
  endpointId: string = ""
239
+ baseUrl: string = runpodServerlessBaseUrlProd
159
240
  private apiKey: string = ""
160
- constructor(apiKey: string, endpointId: string) {
241
+ constructor(baseUrl: string, apiKey: string, endpointId: string) {
242
+ this.baseUrl = baseUrl
161
243
  this.apiKey = apiKey
162
244
  this.endpointId = endpointId
163
245
  }
164
- async runSync(request: EndpointInputPayload): Promise<EndpointCompletedOutput> {
165
- return runSync(this.apiKey, this.endpointId, request)
246
+ async runSync(
247
+ request: EndpointInputPayload,
248
+ timeout: number = 90000
249
+ ): Promise<EndpointCompletedOutput> {
250
+ return runSync(this.baseUrl, this.apiKey, this.endpointId, request, timeout)
251
+ }
252
+ async run(request: EndpointInputPayload, timeout: number = 3000): Promise<string> {
253
+ return run(this.baseUrl, this.apiKey, this.endpointId, request, timeout)
166
254
  }
167
- async run(request: EndpointInputPayload): Promise<string> {
168
- return run(this.apiKey, this.endpointId, request)
255
+ async status(requestId: string, timeout: number = 3000): Promise<EndpointOutput> {
256
+ return status(this.baseUrl, this.apiKey, this.endpointId, requestId, timeout)
169
257
  }
170
- async getStatus(requestId: string): Promise<EndpointOutput> {
171
- return getStatus(this.apiKey, this.endpointId, requestId)
258
+ async statusSync(requestId: string, timeout: number = 90000): Promise<EndpointOutput> {
259
+ return statusSync(this.baseUrl, this.apiKey, this.endpointId, requestId, timeout)
172
260
  }
173
- async stream(requestId: string): Promise<EndpointOutput> {
174
- return stream(this.apiKey, this.endpointId, requestId)
261
+ stream(requestId: string): AsyncGenerator<any> {
262
+ return stream(this.baseUrl, this.apiKey, this.endpointId, requestId)
175
263
  }
176
- async cancel(requestId: string): Promise<CancelOutput> {
177
- return cancel(this.apiKey, this.endpointId, requestId)
264
+ async cancel(requestId: string, timeout: number = 3000): Promise<CancelOutput> {
265
+ return cancel(this.baseUrl, this.apiKey, this.endpointId, requestId, timeout)
178
266
  }
179
- async getHealth(): Promise<HealthCheck> {
180
- return getHealth(this.apiKey, this.endpointId)
267
+ async health(timeout: number = 3000): Promise<HealthCheck> {
268
+ return health(this.baseUrl, this.apiKey, this.endpointId, timeout)
181
269
  }
182
- async purgeQueue(): Promise<PurgeQueueOutput> {
183
- return purgeQueue(this.apiKey, this.endpointId)
270
+ async purgeQueue(timeout: number = 3000): Promise<PurgeQueueOutput> {
271
+ return purgeQueue(this.baseUrl, this.apiKey, this.endpointId, timeout)
184
272
  }
185
273
  }
186
274
 
187
- export default (apiKey: string) => ({
188
- endpoint: (endpointId: string) => new Endpoint(apiKey, endpointId),
189
- //template...
275
+ const defaultSdkOptions = {
276
+ baseUrl: runpodServerlessBaseUrlProd,
277
+ }
278
+ class RunpodSdk {
279
+ private apiKey: string = ""
280
+ baseUrl: string = runpodServerlessBaseUrlProd
281
+ constructor(apiKey: string, options: SdkOptions) {
282
+ if (isNil(apiKey)) {
283
+ print("Api key not supplied")
284
+ return
285
+ }
286
+ this.apiKey = apiKey
287
+ this.baseUrl = options.baseUrl ?? this.baseUrl
288
+ }
289
+ endpoint(endpointId: string) {
290
+ if (isNil(endpointId)) {
291
+ print("Endpoint id not supplied")
292
+ return null
293
+ }
294
+ return new Endpoint(this.baseUrl, this.apiKey, endpointId)
295
+ }
190
296
  //pod...
191
- })
297
+ //template...
298
+ }
299
+ export default (apiKey: string, options: SdkOptions = defaultSdkOptions) =>
300
+ new RunpodSdk(apiKey, options)
package/tsconfig.json CHANGED
@@ -8,7 +8,8 @@
8
8
  "lib": ["ES2022", "DOM"],
9
9
  "strict": true,
10
10
  "esModuleInterop": true,
11
- "declaration": true
11
+ "declaration": true,
12
+ "sourceMap": true
12
13
  },
13
14
  "include": ["src/**/*.ts"]
14
15
  }
@@ -1,17 +0,0 @@
1
- import runpodSdk from "runpod-sdk"
2
-
3
- const { RUNPOD_API_KEY, ENDPOINT_ID } = process.env
4
- if (!(RUNPOD_API_KEY && ENDPOINT_ID)) {
5
- console.log("please supply RUNPOD_API_KEY and ENDPOINT_ID as environment variables")
6
- process.exit()
7
- }
8
- const runpod = runpodSdk(RUNPOD_API_KEY)
9
- const endpoint = runpod.endpoint(ENDPOINT_ID)
10
-
11
- const result = await endpoint.runSync({
12
- input: {
13
- prompt: "Hi, what's your name?",
14
- },
15
- })
16
-
17
- console.log(result)