runpod-sdk 1.0.1 → 1.0.3
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 +1 -1
- package/dist/index.d.ts +114 -0
- package/dist/index.js +61 -35
- package/examples/example.js +17 -0
- package/{test → examples}/test.js +7 -7
- package/package.json +2 -1
- package/src/index.ts +87 -50
- package/tsconfig.json +2 -1
package/README.md
CHANGED
|
@@ -37,7 +37,7 @@ const status = await endpoint.getStatus(requestId);
|
|
|
37
37
|
|
|
38
38
|
If the request has been completed, the status object returned will contain the `output` of the request.
|
|
39
39
|
|
|
40
|
-
If you don't want to manage polling for request completion yourself, you can simply call `runSync`, which will enqueue the request and then poll
|
|
40
|
+
If you don't want to manage polling for request completion yourself, you can simply call `runSync`, which will enqueue the request and then poll until the request completes, fails or times out.
|
|
41
41
|
|
|
42
42
|
```js
|
|
43
43
|
const result = await endpoint.runSync({
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
export type ExecutionPolicy = {
|
|
2
|
+
ttl?: number;
|
|
3
|
+
executionTimeout?: number;
|
|
4
|
+
};
|
|
5
|
+
export type S3Config = {
|
|
6
|
+
accessId: string;
|
|
7
|
+
accessSecret: string;
|
|
8
|
+
bucketName: string;
|
|
9
|
+
endpointUrl: string;
|
|
10
|
+
};
|
|
11
|
+
export type EndpointInputPayload = {
|
|
12
|
+
input: any;
|
|
13
|
+
webhook?: string;
|
|
14
|
+
s3Config?: S3Config;
|
|
15
|
+
policy?: ExecutionPolicy;
|
|
16
|
+
};
|
|
17
|
+
export type EndpointIncompleteOutput = {
|
|
18
|
+
status: string;
|
|
19
|
+
id: string;
|
|
20
|
+
};
|
|
21
|
+
export type EndpointCompletedOutput = {
|
|
22
|
+
status: string;
|
|
23
|
+
id: string;
|
|
24
|
+
output: any;
|
|
25
|
+
executionTime: number;
|
|
26
|
+
delayTime: number;
|
|
27
|
+
};
|
|
28
|
+
export type EndpointOutput = EndpointCompletedOutput | EndpointIncompleteOutput;
|
|
29
|
+
export type CancelOutput = {
|
|
30
|
+
status: string;
|
|
31
|
+
id: string;
|
|
32
|
+
executionTime: number;
|
|
33
|
+
delayTime: number;
|
|
34
|
+
};
|
|
35
|
+
export type HealthCheck = {
|
|
36
|
+
jobs: {
|
|
37
|
+
completed: number;
|
|
38
|
+
failed: number;
|
|
39
|
+
inProgress: number;
|
|
40
|
+
inQueue: number;
|
|
41
|
+
retried: number;
|
|
42
|
+
};
|
|
43
|
+
workers: {
|
|
44
|
+
idle: number;
|
|
45
|
+
initializing: number;
|
|
46
|
+
ready: number;
|
|
47
|
+
running: number;
|
|
48
|
+
throttled: number;
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
export type PurgeQueueOutput = {
|
|
52
|
+
removed: number;
|
|
53
|
+
status: string;
|
|
54
|
+
};
|
|
55
|
+
export type SdkOptions = {
|
|
56
|
+
baseUrl: string;
|
|
57
|
+
};
|
|
58
|
+
export declare const runpodServerlessBaseUrlProd = "https://api.runpod.ai/v2";
|
|
59
|
+
export declare const runpodServerlessBaseUrlDev = "https://dev-api.runpod.ai/v2";
|
|
60
|
+
export declare const runSync: import("ts-toolbelt/out/Function/Curry").Curry<(baseUrl: string, apiKey: string, endpointId: string, request: EndpointInputPayload) => Promise<{
|
|
61
|
+
started: boolean;
|
|
62
|
+
completed: boolean;
|
|
63
|
+
status: string;
|
|
64
|
+
id: string;
|
|
65
|
+
} | {
|
|
66
|
+
started: boolean;
|
|
67
|
+
completed: boolean;
|
|
68
|
+
status: string;
|
|
69
|
+
id: string;
|
|
70
|
+
output: any;
|
|
71
|
+
executionTime: number;
|
|
72
|
+
delayTime: number;
|
|
73
|
+
} | {
|
|
74
|
+
started: boolean;
|
|
75
|
+
completed: boolean;
|
|
76
|
+
succeeded: boolean;
|
|
77
|
+
status: string;
|
|
78
|
+
id: string;
|
|
79
|
+
} | {
|
|
80
|
+
started: boolean;
|
|
81
|
+
completed: boolean;
|
|
82
|
+
succeeded: boolean;
|
|
83
|
+
status: string;
|
|
84
|
+
id: string;
|
|
85
|
+
output: any;
|
|
86
|
+
executionTime: number;
|
|
87
|
+
delayTime: number;
|
|
88
|
+
}>>;
|
|
89
|
+
export declare const run: import("ts-toolbelt/out/Function/Curry").Curry<(baseUrl: string, apiKey: string, endpointId: string, request: EndpointInputPayload) => Promise<any>>;
|
|
90
|
+
export declare const getStatus: import("ts-toolbelt/out/Function/Curry").Curry<(baseUrl: string, apiKey: string, endpointId: string, requestId: string) => Promise<any>>;
|
|
91
|
+
export declare const stream: import("ts-toolbelt/out/Function/Curry").Curry<(baseUrl: string, apiKey: string, endpointId: string, requestId: string) => Promise<any>>;
|
|
92
|
+
export declare const getHealth: import("ts-toolbelt/out/Function/Curry").Curry<(baseUrl: string, apiKey: string, endpointId: string) => Promise<any>>;
|
|
93
|
+
export declare const purgeQueue: import("ts-toolbelt/out/Function/Curry").Curry<(baseUrl: string, apiKey: string, endpointId: string) => Promise<any>>;
|
|
94
|
+
declare class Endpoint {
|
|
95
|
+
endpointId: string;
|
|
96
|
+
baseUrl: string;
|
|
97
|
+
private apiKey;
|
|
98
|
+
constructor(baseUrl: string, apiKey: string, endpointId: string);
|
|
99
|
+
runSync(request: EndpointInputPayload): Promise<EndpointCompletedOutput>;
|
|
100
|
+
run(request: EndpointInputPayload): Promise<string>;
|
|
101
|
+
getStatus(requestId: string): Promise<EndpointOutput>;
|
|
102
|
+
stream(requestId: string): Promise<EndpointOutput>;
|
|
103
|
+
cancel(requestId: string): Promise<CancelOutput>;
|
|
104
|
+
getHealth(): Promise<HealthCheck>;
|
|
105
|
+
purgeQueue(): Promise<PurgeQueueOutput>;
|
|
106
|
+
}
|
|
107
|
+
declare class RunpodSdk {
|
|
108
|
+
private apiKey;
|
|
109
|
+
baseUrl: string;
|
|
110
|
+
constructor(apiKey: string, options: SdkOptions);
|
|
111
|
+
endpoint(endpointId: string): Endpoint | null;
|
|
112
|
+
}
|
|
113
|
+
declare const _default: (apiKey: string, options?: SdkOptions) => RunpodSdk;
|
|
114
|
+
export default _default;
|
package/dist/index.js
CHANGED
|
@@ -46,8 +46,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
46
46
|
}
|
|
47
47
|
};
|
|
48
48
|
import axios from "axios";
|
|
49
|
-
import { curry, clamp } from "ramda";
|
|
50
|
-
var runpodServerlessBaseUrl = "https://api.runpod.ai/v2";
|
|
49
|
+
import { curry, clamp, isNil } from "ramda";
|
|
51
50
|
var getAuthHeader = function (apiKey) { return ({
|
|
52
51
|
headers: {
|
|
53
52
|
"Authorization": "Bearer ".concat(apiKey),
|
|
@@ -86,9 +85,11 @@ var handleErrorsStatus = function (axiosRequest) { return __awaiter(void 0, void
|
|
|
86
85
|
}
|
|
87
86
|
});
|
|
88
87
|
}); };
|
|
89
|
-
var
|
|
88
|
+
export var runpodServerlessBaseUrlProd = "https://api.runpod.ai/v2";
|
|
89
|
+
export var runpodServerlessBaseUrlDev = "https://dev-api.runpod.ai/v2";
|
|
90
|
+
var getEndpointUrl = curry(function (baseUrl, endpointId) { return "".concat(baseUrl, "/").concat(endpointId); });
|
|
90
91
|
//run and then poll status
|
|
91
|
-
export var runSync = curry(function (apiKey, endpointId, request) { return __awaiter(void 0, void 0, void 0, function () {
|
|
92
|
+
export var runSync = curry(function (baseUrl, apiKey, endpointId, request) { return __awaiter(void 0, void 0, void 0, function () {
|
|
92
93
|
var maxWaitTimeMs, startTime, getRemainingTime, runResp, data, id, start;
|
|
93
94
|
var _a, _b;
|
|
94
95
|
return __generator(this, function (_c) {
|
|
@@ -97,7 +98,7 @@ export var runSync = curry(function (apiKey, endpointId, request) { return __awa
|
|
|
97
98
|
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
99
|
startTime = Date.now();
|
|
99
100
|
getRemainingTime = function () { return clamp(1000, 90000, maxWaitTimeMs - (Date.now() - startTime)); };
|
|
100
|
-
return [4 /*yield*/, runsync(apiKey, endpointId, request)];
|
|
101
|
+
return [4 /*yield*/, runsync(baseUrl, apiKey, endpointId, request)];
|
|
101
102
|
case 1:
|
|
102
103
|
runResp = _c.sent();
|
|
103
104
|
data = __assign({}, runResp);
|
|
@@ -110,7 +111,7 @@ export var runSync = curry(function (apiKey, endpointId, request) { return __awa
|
|
|
110
111
|
print("".concat(id, " timed out after ").concat(maxWaitTimeMs / 1000, " seconds"));
|
|
111
112
|
return [2 /*return*/, __assign(__assign({}, data), { started: true, completed: false })];
|
|
112
113
|
}
|
|
113
|
-
return [4 /*yield*/, statusSync(apiKey, endpointId, id, getRemainingTime())];
|
|
114
|
+
return [4 /*yield*/, statusSync(baseUrl, apiKey, endpointId, id, getRemainingTime())];
|
|
114
115
|
case 3:
|
|
115
116
|
data = _c.sent();
|
|
116
117
|
print("".concat(id, ": ").concat(data.status));
|
|
@@ -120,113 +121,138 @@ export var runSync = curry(function (apiKey, endpointId, request) { return __awa
|
|
|
120
121
|
});
|
|
121
122
|
}); });
|
|
122
123
|
//wrapper over /status-sync
|
|
123
|
-
var statusSync = curry(function (apiKey, endpointId, requestId, wait) {
|
|
124
|
-
var url = getEndpointUrl(endpointId) + "/status-sync/" + requestId + "?wait=".concat(wait);
|
|
124
|
+
var statusSync = curry(function (baseUrl, apiKey, endpointId, requestId, wait) {
|
|
125
|
+
var url = getEndpointUrl(baseUrl, endpointId) + "/status-sync/" + requestId + "?wait=".concat(wait);
|
|
125
126
|
var authHeader = getAuthHeader(apiKey);
|
|
126
127
|
return handleErrorsStatus(axios.get(url, authHeader));
|
|
127
128
|
});
|
|
128
129
|
//wrapper over /runsync
|
|
129
|
-
var runsync = curry(function (apiKey, endpointId, request) {
|
|
130
|
-
var url = getEndpointUrl(endpointId) + "/runsync";
|
|
130
|
+
var runsync = curry(function (baseUrl, apiKey, endpointId, request) {
|
|
131
|
+
var url = getEndpointUrl(baseUrl, endpointId) + "/runsync";
|
|
131
132
|
var authHeader = getAuthHeader(apiKey);
|
|
132
133
|
return handleErrorsStatus(axios.post(url, request, authHeader));
|
|
133
134
|
});
|
|
134
135
|
//wrapper over /run
|
|
135
|
-
export var run = curry(function (apiKey, endpointId, request) {
|
|
136
|
-
var url = getEndpointUrl(endpointId) + "/run";
|
|
136
|
+
export var run = curry(function (baseUrl, apiKey, endpointId, request) {
|
|
137
|
+
var url = getEndpointUrl(baseUrl, endpointId) + "/run";
|
|
137
138
|
var authHeader = getAuthHeader(apiKey);
|
|
138
139
|
return handleErrors(axios.post(url, request, authHeader));
|
|
139
140
|
});
|
|
140
141
|
//wrapper over /status
|
|
141
|
-
export var getStatus = curry(function (apiKey, endpointId, requestId) {
|
|
142
|
-
var url = getEndpointUrl(endpointId) + "/status/" + requestId;
|
|
142
|
+
export var getStatus = curry(function (baseUrl, apiKey, endpointId, requestId) {
|
|
143
|
+
var url = getEndpointUrl(baseUrl, endpointId) + "/status/" + requestId;
|
|
143
144
|
var authHeader = getAuthHeader(apiKey);
|
|
144
145
|
return handleErrorsStatus(axios.get(url, authHeader));
|
|
145
146
|
});
|
|
146
147
|
//wrapper over /stream
|
|
147
|
-
export var stream = curry(function (apiKey, endpointId, requestId) {
|
|
148
|
-
var url = getEndpointUrl(endpointId) + "/stream/" + requestId;
|
|
148
|
+
export var stream = curry(function (baseUrl, apiKey, endpointId, requestId) {
|
|
149
|
+
var url = getEndpointUrl(baseUrl, endpointId) + "/stream/" + requestId;
|
|
149
150
|
var authHeader = getAuthHeader(apiKey);
|
|
150
151
|
return handleErrors(axios.get(url, authHeader));
|
|
151
152
|
});
|
|
152
153
|
//wrapper over /cancel
|
|
153
|
-
var cancel = curry(function (apiKey, endpointId, requestId) {
|
|
154
|
-
var url = getEndpointUrl(endpointId) + "/cancel/" + requestId;
|
|
154
|
+
var cancel = curry(function (baseUrl, apiKey, endpointId, requestId) {
|
|
155
|
+
var url = getEndpointUrl(baseUrl, endpointId) + "/cancel/" + requestId;
|
|
155
156
|
var authHeader = getAuthHeader(apiKey);
|
|
156
157
|
return handleErrors(axios.post(url, {}, authHeader));
|
|
157
158
|
});
|
|
158
159
|
//wrapper over /health
|
|
159
|
-
export var getHealth = curry(function (apiKey, endpointId) {
|
|
160
|
-
var url = getEndpointUrl(endpointId) + "/health";
|
|
160
|
+
export var getHealth = curry(function (baseUrl, apiKey, endpointId) {
|
|
161
|
+
var url = getEndpointUrl(baseUrl, endpointId) + "/health";
|
|
161
162
|
var authHeader = getAuthHeader(apiKey);
|
|
162
163
|
return handleErrors(axios.get(url, authHeader));
|
|
163
164
|
});
|
|
164
165
|
//wrapper over /purge-queue
|
|
165
|
-
export var purgeQueue = curry(function (apiKey, endpointId) {
|
|
166
|
-
var url = getEndpointUrl(endpointId) + "/purge-queue";
|
|
166
|
+
export var purgeQueue = curry(function (baseUrl, apiKey, endpointId) {
|
|
167
|
+
var url = getEndpointUrl(baseUrl, endpointId) + "/purge-queue";
|
|
167
168
|
var authHeader = getAuthHeader(apiKey);
|
|
168
169
|
return handleErrors(axios.post(url, {}, authHeader));
|
|
169
170
|
});
|
|
170
171
|
var Endpoint = /** @class */ (function () {
|
|
171
|
-
function Endpoint(apiKey, endpointId) {
|
|
172
|
+
function Endpoint(baseUrl, apiKey, endpointId) {
|
|
172
173
|
this.endpointId = "";
|
|
174
|
+
this.baseUrl = runpodServerlessBaseUrlProd;
|
|
173
175
|
this.apiKey = "";
|
|
176
|
+
this.baseUrl = baseUrl;
|
|
174
177
|
this.apiKey = apiKey;
|
|
175
178
|
this.endpointId = endpointId;
|
|
176
179
|
}
|
|
177
180
|
Endpoint.prototype.runSync = function (request) {
|
|
178
181
|
return __awaiter(this, void 0, void 0, function () {
|
|
179
182
|
return __generator(this, function (_a) {
|
|
180
|
-
return [2 /*return*/, runSync(this.apiKey, this.endpointId, request)];
|
|
183
|
+
return [2 /*return*/, runSync(this.baseUrl, this.apiKey, this.endpointId, request)];
|
|
181
184
|
});
|
|
182
185
|
});
|
|
183
186
|
};
|
|
184
187
|
Endpoint.prototype.run = function (request) {
|
|
185
188
|
return __awaiter(this, void 0, void 0, function () {
|
|
186
189
|
return __generator(this, function (_a) {
|
|
187
|
-
return [2 /*return*/, run(this.apiKey, this.endpointId, request)];
|
|
190
|
+
return [2 /*return*/, run(this.baseUrl, this.apiKey, this.endpointId, request)];
|
|
188
191
|
});
|
|
189
192
|
});
|
|
190
193
|
};
|
|
191
194
|
Endpoint.prototype.getStatus = function (requestId) {
|
|
192
195
|
return __awaiter(this, void 0, void 0, function () {
|
|
193
196
|
return __generator(this, function (_a) {
|
|
194
|
-
return [2 /*return*/, getStatus(this.apiKey, this.endpointId, requestId)];
|
|
197
|
+
return [2 /*return*/, getStatus(this.baseUrl, this.apiKey, this.endpointId, requestId)];
|
|
195
198
|
});
|
|
196
199
|
});
|
|
197
200
|
};
|
|
198
201
|
Endpoint.prototype.stream = function (requestId) {
|
|
199
202
|
return __awaiter(this, void 0, void 0, function () {
|
|
200
203
|
return __generator(this, function (_a) {
|
|
201
|
-
return [2 /*return*/, stream(this.apiKey, this.endpointId, requestId)];
|
|
204
|
+
return [2 /*return*/, stream(this.baseUrl, this.apiKey, this.endpointId, requestId)];
|
|
202
205
|
});
|
|
203
206
|
});
|
|
204
207
|
};
|
|
205
208
|
Endpoint.prototype.cancel = function (requestId) {
|
|
206
209
|
return __awaiter(this, void 0, void 0, function () {
|
|
207
210
|
return __generator(this, function (_a) {
|
|
208
|
-
return [2 /*return*/, cancel(this.apiKey, this.endpointId, requestId)];
|
|
211
|
+
return [2 /*return*/, cancel(this.baseUrl, this.apiKey, this.endpointId, requestId)];
|
|
209
212
|
});
|
|
210
213
|
});
|
|
211
214
|
};
|
|
212
215
|
Endpoint.prototype.getHealth = function () {
|
|
213
216
|
return __awaiter(this, void 0, void 0, function () {
|
|
214
217
|
return __generator(this, function (_a) {
|
|
215
|
-
return [2 /*return*/, getHealth(this.apiKey, this.endpointId)];
|
|
218
|
+
return [2 /*return*/, getHealth(this.baseUrl, this.apiKey, this.endpointId)];
|
|
216
219
|
});
|
|
217
220
|
});
|
|
218
221
|
};
|
|
219
222
|
Endpoint.prototype.purgeQueue = function () {
|
|
220
223
|
return __awaiter(this, void 0, void 0, function () {
|
|
221
224
|
return __generator(this, function (_a) {
|
|
222
|
-
return [2 /*return*/, purgeQueue(this.apiKey, this.endpointId)];
|
|
225
|
+
return [2 /*return*/, purgeQueue(this.baseUrl, this.apiKey, this.endpointId)];
|
|
223
226
|
});
|
|
224
227
|
});
|
|
225
228
|
};
|
|
226
229
|
return Endpoint;
|
|
227
230
|
}());
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
231
|
+
var defaultSdkOptions = {
|
|
232
|
+
baseUrl: runpodServerlessBaseUrlProd,
|
|
233
|
+
};
|
|
234
|
+
var RunpodSdk = /** @class */ (function () {
|
|
235
|
+
function RunpodSdk(apiKey, options) {
|
|
236
|
+
var _a;
|
|
237
|
+
this.apiKey = "";
|
|
238
|
+
this.baseUrl = runpodServerlessBaseUrlProd;
|
|
239
|
+
if (isNil(apiKey)) {
|
|
240
|
+
print("Api key not supplied");
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
this.apiKey = apiKey;
|
|
244
|
+
this.baseUrl = (_a = options.baseUrl) !== null && _a !== void 0 ? _a : this.baseUrl;
|
|
245
|
+
}
|
|
246
|
+
RunpodSdk.prototype.endpoint = function (endpointId) {
|
|
247
|
+
if (isNil(endpointId)) {
|
|
248
|
+
print("Endpoint id not supplied");
|
|
249
|
+
return null;
|
|
250
|
+
}
|
|
251
|
+
return new Endpoint(this.baseUrl, this.apiKey, endpointId);
|
|
252
|
+
};
|
|
253
|
+
return RunpodSdk;
|
|
254
|
+
}());
|
|
255
|
+
export default (function (apiKey, options) {
|
|
256
|
+
if (options === void 0) { options = defaultSdkOptions; }
|
|
257
|
+
return new RunpodSdk(apiKey, options);
|
|
258
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
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: "photo of a horse",
|
|
14
|
+
},
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
console.log(result)
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import runpodSdk from "../dist/index.js"
|
|
1
|
+
import runpodSdk, { runpodServerlessBaseUrlDev } from "../dist/index.js"
|
|
2
2
|
|
|
3
|
-
const { RUNPOD_API_KEY
|
|
4
|
-
if (!
|
|
3
|
+
const { RUNPOD_API_KEY } = process.env
|
|
4
|
+
if (!RUNPOD_API_KEY) {
|
|
5
|
+
console.log("please supply RUNPOD_API_KEY as an environment variable")
|
|
5
6
|
process.exit()
|
|
6
7
|
}
|
|
7
8
|
//OOP
|
|
8
|
-
const runpod = runpodSdk(RUNPOD_API_KEY)
|
|
9
|
-
const endpoint = runpod.endpoint(
|
|
9
|
+
const runpod = runpodSdk(RUNPOD_API_KEY, { baseUrl: runpodServerlessBaseUrlDev })
|
|
10
|
+
const endpoint = runpod.endpoint("mock")
|
|
10
11
|
const request = {
|
|
11
12
|
input: {
|
|
12
|
-
|
|
13
|
-
name: "nate",
|
|
13
|
+
prompt: "photo of a horse",
|
|
14
14
|
},
|
|
15
15
|
policy: {
|
|
16
16
|
executionTimeout: 120 * 1000,
|
package/package.json
CHANGED
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
|
|
@@ -47,7 +47,10 @@ export type PurgeQueueOutput = {
|
|
|
47
47
|
status: string
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
|
|
50
|
+
export type SdkOptions = {
|
|
51
|
+
baseUrl: string
|
|
52
|
+
}
|
|
53
|
+
|
|
51
54
|
const getAuthHeader = (apiKey: string) => ({
|
|
52
55
|
headers: {
|
|
53
56
|
"Authorization": `Bearer ${apiKey}`,
|
|
@@ -78,16 +81,17 @@ const handleErrorsStatus = async (axiosRequest: Promise<AxiosResponse>) => {
|
|
|
78
81
|
succeeded: resp.data.status === "COMPLETED",
|
|
79
82
|
}
|
|
80
83
|
}
|
|
81
|
-
|
|
82
|
-
const
|
|
84
|
+
export const runpodServerlessBaseUrlProd = "https://api.runpod.ai/v2"
|
|
85
|
+
export const runpodServerlessBaseUrlDev = "https://dev-api.runpod.ai/v2"
|
|
86
|
+
const getEndpointUrl = curry((baseUrl, endpointId: string) => `${baseUrl}/${endpointId}`)
|
|
83
87
|
|
|
84
88
|
//run and then poll status
|
|
85
89
|
export const runSync = curry(
|
|
86
|
-
async (apiKey: string, endpointId: string, request: EndpointInputPayload) => {
|
|
90
|
+
async (baseUrl: string, apiKey: string, endpointId: string, request: EndpointInputPayload) => {
|
|
87
91
|
const maxWaitTimeMs = request?.policy?.executionTimeout ?? 300 * 1000
|
|
88
92
|
const startTime = Date.now()
|
|
89
93
|
const getRemainingTime = () => clamp(1000, 90000, maxWaitTimeMs - (Date.now() - startTime))
|
|
90
|
-
const runResp: any = await runsync(apiKey, endpointId, request)
|
|
94
|
+
const runResp: any = await runsync(baseUrl, apiKey, endpointId, request)
|
|
91
95
|
let data: EndpointOutput = { ...runResp }
|
|
92
96
|
const { id } = data
|
|
93
97
|
const start = Date.now()
|
|
@@ -96,7 +100,7 @@ export const runSync = curry(
|
|
|
96
100
|
print(`${id} timed out after ${maxWaitTimeMs / 1000} seconds`)
|
|
97
101
|
return { ...data, started: true, completed: false }
|
|
98
102
|
}
|
|
99
|
-
data = await statusSync(apiKey, endpointId, id, getRemainingTime())
|
|
103
|
+
data = await statusSync(baseUrl, apiKey, endpointId, id, getRemainingTime())
|
|
100
104
|
print(`${id}: ${data.status}`)
|
|
101
105
|
}
|
|
102
106
|
return { ...data, started: true, completed: true, succeeded: data.status === "COMPLETED" }
|
|
@@ -104,88 +108,121 @@ export const runSync = curry(
|
|
|
104
108
|
)
|
|
105
109
|
|
|
106
110
|
//wrapper over /status-sync
|
|
107
|
-
const statusSync = curry(
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
111
|
+
const statusSync = curry(
|
|
112
|
+
(baseUrl: string, apiKey: string, endpointId: string, requestId: String, wait: number) => {
|
|
113
|
+
const url = getEndpointUrl(baseUrl, endpointId) + "/status-sync/" + requestId + `?wait=${wait}`
|
|
114
|
+
const authHeader = getAuthHeader(apiKey)
|
|
115
|
+
return handleErrorsStatus(axios.get(url, authHeader))
|
|
116
|
+
}
|
|
117
|
+
)
|
|
112
118
|
|
|
113
119
|
//wrapper over /runsync
|
|
114
|
-
const runsync = curry(
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
120
|
+
const runsync = curry(
|
|
121
|
+
(baseUrl: string, apiKey: string, endpointId: string, request: EndpointInputPayload) => {
|
|
122
|
+
const url = getEndpointUrl(baseUrl, endpointId) + "/runsync"
|
|
123
|
+
const authHeader = getAuthHeader(apiKey)
|
|
124
|
+
return handleErrorsStatus(axios.post(url, request, authHeader))
|
|
125
|
+
}
|
|
126
|
+
)
|
|
119
127
|
|
|
120
128
|
//wrapper over /run
|
|
121
|
-
export const run = curry(
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
129
|
+
export const run = curry(
|
|
130
|
+
(baseUrl: string, apiKey: string, endpointId: string, request: EndpointInputPayload) => {
|
|
131
|
+
const url = getEndpointUrl(baseUrl, endpointId) + "/run"
|
|
132
|
+
const authHeader = getAuthHeader(apiKey)
|
|
133
|
+
return handleErrors(axios.post(url, request, authHeader))
|
|
134
|
+
}
|
|
135
|
+
)
|
|
126
136
|
//wrapper over /status
|
|
127
|
-
export const getStatus = curry(
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
137
|
+
export const getStatus = curry(
|
|
138
|
+
(baseUrl: string, apiKey: string, endpointId: string, requestId: string) => {
|
|
139
|
+
const url = getEndpointUrl(baseUrl, endpointId) + "/status/" + requestId
|
|
140
|
+
const authHeader = getAuthHeader(apiKey)
|
|
141
|
+
return handleErrorsStatus(axios.get(url, authHeader))
|
|
142
|
+
}
|
|
143
|
+
)
|
|
132
144
|
//wrapper over /stream
|
|
133
|
-
export const stream = curry(
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
145
|
+
export const stream = curry(
|
|
146
|
+
(baseUrl: string, apiKey: string, endpointId: string, requestId: string) => {
|
|
147
|
+
const url = getEndpointUrl(baseUrl, endpointId) + "/stream/" + requestId
|
|
148
|
+
const authHeader = getAuthHeader(apiKey)
|
|
149
|
+
return handleErrors(axios.get(url, authHeader))
|
|
150
|
+
}
|
|
151
|
+
)
|
|
138
152
|
//wrapper over /cancel
|
|
139
|
-
const cancel = curry((apiKey: string, endpointId: string, requestId: string) => {
|
|
140
|
-
const url = getEndpointUrl(endpointId) + "/cancel/" + requestId
|
|
153
|
+
const cancel = curry((baseUrl: string, apiKey: string, endpointId: string, requestId: string) => {
|
|
154
|
+
const url = getEndpointUrl(baseUrl, endpointId) + "/cancel/" + requestId
|
|
141
155
|
const authHeader = getAuthHeader(apiKey)
|
|
142
156
|
return handleErrors(axios.post(url, {}, authHeader))
|
|
143
157
|
})
|
|
144
158
|
//wrapper over /health
|
|
145
|
-
export const getHealth = curry((apiKey: string, endpointId: string) => {
|
|
146
|
-
const url = getEndpointUrl(endpointId) + "/health"
|
|
159
|
+
export const getHealth = curry((baseUrl: string, apiKey: string, endpointId: string) => {
|
|
160
|
+
const url = getEndpointUrl(baseUrl, endpointId) + "/health"
|
|
147
161
|
const authHeader = getAuthHeader(apiKey)
|
|
148
162
|
return handleErrors(axios.get(url, authHeader))
|
|
149
163
|
})
|
|
150
164
|
//wrapper over /purge-queue
|
|
151
|
-
export const purgeQueue = curry((apiKey: string, endpointId: string) => {
|
|
152
|
-
const url = getEndpointUrl(endpointId) + "/purge-queue"
|
|
165
|
+
export const purgeQueue = curry((baseUrl: string, apiKey: string, endpointId: string) => {
|
|
166
|
+
const url = getEndpointUrl(baseUrl, endpointId) + "/purge-queue"
|
|
153
167
|
const authHeader = getAuthHeader(apiKey)
|
|
154
168
|
return handleErrors(axios.post(url, {}, authHeader))
|
|
155
169
|
})
|
|
156
170
|
|
|
157
171
|
class Endpoint {
|
|
158
172
|
endpointId: string = ""
|
|
173
|
+
baseUrl: string = runpodServerlessBaseUrlProd
|
|
159
174
|
private apiKey: string = ""
|
|
160
|
-
constructor(apiKey: string, endpointId: string) {
|
|
175
|
+
constructor(baseUrl: string, apiKey: string, endpointId: string) {
|
|
176
|
+
this.baseUrl = baseUrl
|
|
161
177
|
this.apiKey = apiKey
|
|
162
178
|
this.endpointId = endpointId
|
|
163
179
|
}
|
|
164
180
|
async runSync(request: EndpointInputPayload): Promise<EndpointCompletedOutput> {
|
|
165
|
-
return runSync(this.apiKey, this.endpointId, request)
|
|
181
|
+
return runSync(this.baseUrl, this.apiKey, this.endpointId, request)
|
|
166
182
|
}
|
|
167
183
|
async run(request: EndpointInputPayload): Promise<string> {
|
|
168
|
-
return run(this.apiKey, this.endpointId, request)
|
|
184
|
+
return run(this.baseUrl, this.apiKey, this.endpointId, request)
|
|
169
185
|
}
|
|
170
186
|
async getStatus(requestId: string): Promise<EndpointOutput> {
|
|
171
|
-
return getStatus(this.apiKey, this.endpointId, requestId)
|
|
187
|
+
return getStatus(this.baseUrl, this.apiKey, this.endpointId, requestId)
|
|
172
188
|
}
|
|
173
189
|
async stream(requestId: string): Promise<EndpointOutput> {
|
|
174
|
-
return stream(this.apiKey, this.endpointId, requestId)
|
|
190
|
+
return stream(this.baseUrl, this.apiKey, this.endpointId, requestId)
|
|
175
191
|
}
|
|
176
192
|
async cancel(requestId: string): Promise<CancelOutput> {
|
|
177
|
-
return cancel(this.apiKey, this.endpointId, requestId)
|
|
193
|
+
return cancel(this.baseUrl, this.apiKey, this.endpointId, requestId)
|
|
178
194
|
}
|
|
179
195
|
async getHealth(): Promise<HealthCheck> {
|
|
180
|
-
return getHealth(this.apiKey, this.endpointId)
|
|
196
|
+
return getHealth(this.baseUrl, this.apiKey, this.endpointId)
|
|
181
197
|
}
|
|
182
198
|
async purgeQueue(): Promise<PurgeQueueOutput> {
|
|
183
|
-
return purgeQueue(this.apiKey, this.endpointId)
|
|
199
|
+
return purgeQueue(this.baseUrl, this.apiKey, this.endpointId)
|
|
184
200
|
}
|
|
185
201
|
}
|
|
186
202
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
203
|
+
const defaultSdkOptions = {
|
|
204
|
+
baseUrl: runpodServerlessBaseUrlProd,
|
|
205
|
+
}
|
|
206
|
+
class RunpodSdk {
|
|
207
|
+
private apiKey: string = ""
|
|
208
|
+
baseUrl: string = runpodServerlessBaseUrlProd
|
|
209
|
+
constructor(apiKey: string, options: SdkOptions) {
|
|
210
|
+
if (isNil(apiKey)) {
|
|
211
|
+
print("Api key not supplied")
|
|
212
|
+
return
|
|
213
|
+
}
|
|
214
|
+
this.apiKey = apiKey
|
|
215
|
+
this.baseUrl = options.baseUrl ?? this.baseUrl
|
|
216
|
+
}
|
|
217
|
+
endpoint(endpointId: string) {
|
|
218
|
+
if (isNil(endpointId)) {
|
|
219
|
+
print("Endpoint id not supplied")
|
|
220
|
+
return null
|
|
221
|
+
}
|
|
222
|
+
return new Endpoint(this.baseUrl, this.apiKey, endpointId)
|
|
223
|
+
}
|
|
190
224
|
//pod...
|
|
191
|
-
|
|
225
|
+
//template...
|
|
226
|
+
}
|
|
227
|
+
export default (apiKey: string, options: SdkOptions = defaultSdkOptions) =>
|
|
228
|
+
new RunpodSdk(apiKey, options)
|