runpod-sdk 1.0.0
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/.prettierrc +12 -0
- package/README.md +60 -0
- package/dist/index.js +232 -0
- package/package.json +25 -0
- package/src/index.ts +191 -0
- package/test/test.js +50 -0
- package/tsconfig.json +13 -0
package/.prettierrc
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# js-sdk
|
|
2
|
+
|
|
3
|
+
JavaScript client sdk for runpod
|
|
4
|
+
|
|
5
|
+
# Example Usage
|
|
6
|
+
|
|
7
|
+
```js
|
|
8
|
+
const { RUNPOD_API_KEY, ENDPOINT_ID } = process.env;
|
|
9
|
+
import runpodSdk from "@nathaniel-runpod-org/sdk";
|
|
10
|
+
|
|
11
|
+
const runpod = runpodSdk(RUNPOD_API_KEY);
|
|
12
|
+
const endpoint = runpod.endpoint(ENDPOINT_ID);
|
|
13
|
+
const result = await endpoint.runsync({
|
|
14
|
+
input: {
|
|
15
|
+
prompt: "a photo of a horse the size of a Boeing 787",
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
# Using Endpoints
|
|
21
|
+
|
|
22
|
+
Once an endpoint has been created, you can send requests to the queue:
|
|
23
|
+
|
|
24
|
+
```js
|
|
25
|
+
const requestId = await endpoint.run({
|
|
26
|
+
input: {
|
|
27
|
+
prompt: "a photo of a horse the size of a Boeing 787",
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
You can check on the status of this request once you have the id:
|
|
33
|
+
|
|
34
|
+
```js
|
|
35
|
+
const status = await endpoint.getStatus(requestId);
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
If the request has been completed, the status object returned will contain the `output` of the request.
|
|
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 in a loop (by default, once every 10 seconds) until the request completes, fails or times out.
|
|
41
|
+
|
|
42
|
+
```js
|
|
43
|
+
const result = await endpoint.runsync({
|
|
44
|
+
input: {
|
|
45
|
+
prompt: "a photo of a horse the size of a Boeing 787",
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
If you have the id of a request, you can cancel it if it's taking too long or no longer necessary:
|
|
51
|
+
|
|
52
|
+
```js
|
|
53
|
+
await endpoint.cancel(requestId);
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
For long running applications or troubleshooting, you may want to check the health of the endpoint workers:
|
|
57
|
+
|
|
58
|
+
```js
|
|
59
|
+
const health = await endpoint.getHealth();
|
|
60
|
+
```
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
var __assign = (this && this.__assign) || function () {
|
|
2
|
+
__assign = Object.assign || function(t) {
|
|
3
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
+
s = arguments[i];
|
|
5
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
+
t[p] = s[p];
|
|
7
|
+
}
|
|
8
|
+
return t;
|
|
9
|
+
};
|
|
10
|
+
return __assign.apply(this, arguments);
|
|
11
|
+
};
|
|
12
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
13
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
14
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
15
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
16
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
17
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
18
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
22
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
23
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
24
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
25
|
+
function step(op) {
|
|
26
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
27
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
28
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
29
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
30
|
+
switch (op[0]) {
|
|
31
|
+
case 0: case 1: t = op; break;
|
|
32
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
33
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
34
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
35
|
+
default:
|
|
36
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
37
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
38
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
39
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
40
|
+
if (t[2]) _.ops.pop();
|
|
41
|
+
_.trys.pop(); continue;
|
|
42
|
+
}
|
|
43
|
+
op = body.call(thisArg, _);
|
|
44
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
45
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
import axios from "axios";
|
|
49
|
+
import { curry, clamp } from "ramda";
|
|
50
|
+
var runpodServerlessBaseUrl = "https://api.runpod.ai/v2";
|
|
51
|
+
var getAuthHeader = function (apiKey) { return ({
|
|
52
|
+
headers: {
|
|
53
|
+
"Authorization": "Bearer ".concat(apiKey),
|
|
54
|
+
"content-type": "application/json",
|
|
55
|
+
},
|
|
56
|
+
}); };
|
|
57
|
+
var print = console.log;
|
|
58
|
+
var sleep = function (ms) { return new Promise(function (resolve) { return setTimeout(resolve, ms); }); };
|
|
59
|
+
var handleErrors = function (axiosRequest) { return __awaiter(void 0, void 0, void 0, function () {
|
|
60
|
+
var resp, status, statusText;
|
|
61
|
+
return __generator(this, function (_a) {
|
|
62
|
+
switch (_a.label) {
|
|
63
|
+
case 0: return [4 /*yield*/, axiosRequest];
|
|
64
|
+
case 1:
|
|
65
|
+
resp = _a.sent();
|
|
66
|
+
status = resp.status, statusText = resp.statusText;
|
|
67
|
+
if (status !== 200) {
|
|
68
|
+
return [2 /*return*/, { status: status, statusText: statusText }];
|
|
69
|
+
}
|
|
70
|
+
return [2 /*return*/, resp.data];
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}); };
|
|
74
|
+
var handleErrorsStatus = function (axiosRequest) { return __awaiter(void 0, void 0, void 0, function () {
|
|
75
|
+
var resp, status, statusText;
|
|
76
|
+
return __generator(this, function (_a) {
|
|
77
|
+
switch (_a.label) {
|
|
78
|
+
case 0: return [4 /*yield*/, axiosRequest];
|
|
79
|
+
case 1:
|
|
80
|
+
resp = _a.sent();
|
|
81
|
+
status = resp.status, statusText = resp.statusText;
|
|
82
|
+
if (status !== 200) {
|
|
83
|
+
return [2 /*return*/, { status: status, statusText: statusText, started: false }];
|
|
84
|
+
}
|
|
85
|
+
return [2 /*return*/, __assign(__assign({}, resp.data), { started: true, completed: ["COMPLETED", "FAILED"].includes(resp.data.status), succeeded: resp.data.status === "COMPLETED" })];
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
}); };
|
|
89
|
+
var getEndpointUrl = function (endpointId) { return "".concat(runpodServerlessBaseUrl, "/").concat(endpointId); };
|
|
90
|
+
//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
|
+
}
|
|
120
|
+
});
|
|
121
|
+
}); });
|
|
122
|
+
//wrapper over /status-sync
|
|
123
|
+
var statusSync = curry(function (apiKey, endpointId, requestId, wait) {
|
|
124
|
+
var url = getEndpointUrl(endpointId) + "/status-sync/" + requestId + "?wait=".concat(wait);
|
|
125
|
+
var authHeader = getAuthHeader(apiKey);
|
|
126
|
+
return handleErrorsStatus(axios.get(url, authHeader));
|
|
127
|
+
});
|
|
128
|
+
//wrapper over /runsync
|
|
129
|
+
var runsync = curry(function (apiKey, endpointId, request) {
|
|
130
|
+
var url = getEndpointUrl(endpointId) + "/runsync";
|
|
131
|
+
var authHeader = getAuthHeader(apiKey);
|
|
132
|
+
return handleErrorsStatus(axios.post(url, request, authHeader));
|
|
133
|
+
});
|
|
134
|
+
//wrapper over /run
|
|
135
|
+
export var run = curry(function (apiKey, endpointId, request) {
|
|
136
|
+
var url = getEndpointUrl(endpointId) + "/run";
|
|
137
|
+
var authHeader = getAuthHeader(apiKey);
|
|
138
|
+
return handleErrors(axios.post(url, request, authHeader));
|
|
139
|
+
});
|
|
140
|
+
//wrapper over /status
|
|
141
|
+
export var getStatus = curry(function (apiKey, endpointId, requestId) {
|
|
142
|
+
var url = getEndpointUrl(endpointId) + "/status/" + requestId;
|
|
143
|
+
var authHeader = getAuthHeader(apiKey);
|
|
144
|
+
return handleErrorsStatus(axios.get(url, authHeader));
|
|
145
|
+
});
|
|
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
|
+
});
|
|
152
|
+
//wrapper over /cancel
|
|
153
|
+
var cancel = curry(function (apiKey, endpointId, requestId) {
|
|
154
|
+
var url = getEndpointUrl(endpointId) + "/cancel/" + requestId;
|
|
155
|
+
var authHeader = getAuthHeader(apiKey);
|
|
156
|
+
return handleErrors(axios.post(url, {}, authHeader));
|
|
157
|
+
});
|
|
158
|
+
//wrapper over /health
|
|
159
|
+
export var getHealth = curry(function (apiKey, endpointId) {
|
|
160
|
+
var url = getEndpointUrl(endpointId) + "/health";
|
|
161
|
+
var authHeader = getAuthHeader(apiKey);
|
|
162
|
+
return handleErrors(axios.get(url, authHeader));
|
|
163
|
+
});
|
|
164
|
+
//wrapper over /purge-queue
|
|
165
|
+
export var purgeQueue = curry(function (apiKey, endpointId) {
|
|
166
|
+
var url = getEndpointUrl(endpointId) + "/purge-queue";
|
|
167
|
+
var authHeader = getAuthHeader(apiKey);
|
|
168
|
+
return handleErrors(axios.post(url, {}, authHeader));
|
|
169
|
+
});
|
|
170
|
+
var Endpoint = /** @class */ (function () {
|
|
171
|
+
function Endpoint(apiKey, endpointId) {
|
|
172
|
+
this.endpointId = "";
|
|
173
|
+
this.apiKey = "";
|
|
174
|
+
this.apiKey = apiKey;
|
|
175
|
+
this.endpointId = endpointId;
|
|
176
|
+
}
|
|
177
|
+
Endpoint.prototype.runSync = function (request) {
|
|
178
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
179
|
+
return __generator(this, function (_a) {
|
|
180
|
+
return [2 /*return*/, runSync(this.apiKey, this.endpointId, request)];
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
};
|
|
184
|
+
Endpoint.prototype.run = function (request) {
|
|
185
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
186
|
+
return __generator(this, function (_a) {
|
|
187
|
+
return [2 /*return*/, run(this.apiKey, this.endpointId, request)];
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
};
|
|
191
|
+
Endpoint.prototype.getStatus = function (requestId) {
|
|
192
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
193
|
+
return __generator(this, function (_a) {
|
|
194
|
+
return [2 /*return*/, getStatus(this.apiKey, this.endpointId, requestId)];
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
};
|
|
198
|
+
Endpoint.prototype.stream = function (requestId) {
|
|
199
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
200
|
+
return __generator(this, function (_a) {
|
|
201
|
+
return [2 /*return*/, stream(this.apiKey, this.endpointId, requestId)];
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
};
|
|
205
|
+
Endpoint.prototype.cancel = function (requestId) {
|
|
206
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
207
|
+
return __generator(this, function (_a) {
|
|
208
|
+
return [2 /*return*/, cancel(this.apiKey, this.endpointId, requestId)];
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
};
|
|
212
|
+
Endpoint.prototype.getHealth = function () {
|
|
213
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
214
|
+
return __generator(this, function (_a) {
|
|
215
|
+
return [2 /*return*/, getHealth(this.apiKey, this.endpointId)];
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
};
|
|
219
|
+
Endpoint.prototype.purgeQueue = function () {
|
|
220
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
221
|
+
return __generator(this, function (_a) {
|
|
222
|
+
return [2 /*return*/, purgeQueue(this.apiKey, this.endpointId)];
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
};
|
|
226
|
+
return Endpoint;
|
|
227
|
+
}());
|
|
228
|
+
export default (function (apiKey) { return ({
|
|
229
|
+
endpoint: function (endpointId) { return new Endpoint(apiKey, endpointId); },
|
|
230
|
+
//template...
|
|
231
|
+
//pod...
|
|
232
|
+
}); });
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "runpod-sdk",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "JavaScript SDK for Runpod",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"author": "Runpod",
|
|
8
|
+
"dependencies": {
|
|
9
|
+
"@actions/core": "^1.10.0",
|
|
10
|
+
"@actions/github": "^5.1.1",
|
|
11
|
+
"axios": "^1.4.0",
|
|
12
|
+
"dotenv": "^16.3.1",
|
|
13
|
+
"graphql": "^16.8.1",
|
|
14
|
+
"ramda": "^0.29.0"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@types/axios": "^0.14.0",
|
|
18
|
+
"@types/ramda": "^0.29.7",
|
|
19
|
+
"esm": "^3.2.25",
|
|
20
|
+
"typescript": "^5.2.2"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "tsc"
|
|
24
|
+
}
|
|
25
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import axios, { AxiosResponse } from "axios"
|
|
2
|
+
import { curry, clamp } from "ramda"
|
|
3
|
+
export type ExecutionPolicy = {
|
|
4
|
+
ttl?: number
|
|
5
|
+
executionTimeout?: number
|
|
6
|
+
}
|
|
7
|
+
export type S3Config = {
|
|
8
|
+
accessId: string
|
|
9
|
+
accessSecret: string
|
|
10
|
+
bucketName: string
|
|
11
|
+
endpointUrl: string
|
|
12
|
+
}
|
|
13
|
+
export type EndpointInputPayload = {
|
|
14
|
+
input: any
|
|
15
|
+
webhook?: string
|
|
16
|
+
s3Config?: S3Config
|
|
17
|
+
policy?: ExecutionPolicy
|
|
18
|
+
}
|
|
19
|
+
export type EndpointIncompleteOutput = {
|
|
20
|
+
status: string
|
|
21
|
+
id: string
|
|
22
|
+
}
|
|
23
|
+
export type EndpointCompletedOutput = {
|
|
24
|
+
status: string
|
|
25
|
+
id: string
|
|
26
|
+
output: any
|
|
27
|
+
executionTime: number
|
|
28
|
+
delayTime: number
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export type EndpointOutput = EndpointCompletedOutput | EndpointIncompleteOutput
|
|
32
|
+
|
|
33
|
+
export type CancelOutput = {
|
|
34
|
+
status: string
|
|
35
|
+
id: string
|
|
36
|
+
executionTime: number
|
|
37
|
+
delayTime: number
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export type HealthCheck = {
|
|
41
|
+
jobs: { completed: number; failed: number; inProgress: number; inQueue: number; retried: number }
|
|
42
|
+
workers: { idle: number; initializing: number; ready: number; running: number; throttled: number }
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export type PurgeQueueOutput = {
|
|
46
|
+
removed: number
|
|
47
|
+
status: string
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const runpodServerlessBaseUrl = "https://api.runpod.ai/v2"
|
|
51
|
+
const getAuthHeader = (apiKey: string) => ({
|
|
52
|
+
headers: {
|
|
53
|
+
"Authorization": `Bearer ${apiKey}`,
|
|
54
|
+
"content-type": "application/json",
|
|
55
|
+
},
|
|
56
|
+
})
|
|
57
|
+
const print = console.log
|
|
58
|
+
const sleep = (ms: any) => new Promise((resolve: any) => setTimeout(resolve, ms))
|
|
59
|
+
|
|
60
|
+
const handleErrors = async (axiosRequest: Promise<AxiosResponse>) => {
|
|
61
|
+
const resp = await axiosRequest
|
|
62
|
+
const { status, statusText } = resp
|
|
63
|
+
if (status !== 200) {
|
|
64
|
+
return { status, statusText }
|
|
65
|
+
}
|
|
66
|
+
return resp.data
|
|
67
|
+
}
|
|
68
|
+
const handleErrorsStatus = async (axiosRequest: Promise<AxiosResponse>) => {
|
|
69
|
+
const resp = await axiosRequest
|
|
70
|
+
const { status, statusText } = resp
|
|
71
|
+
if (status !== 200) {
|
|
72
|
+
return { status, statusText, started: false }
|
|
73
|
+
}
|
|
74
|
+
return {
|
|
75
|
+
...resp.data,
|
|
76
|
+
started: true,
|
|
77
|
+
completed: ["COMPLETED", "FAILED"].includes(resp.data.status),
|
|
78
|
+
succeeded: resp.data.status === "COMPLETED",
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const getEndpointUrl = (endpointId: string) => `${runpodServerlessBaseUrl}/${endpointId}`
|
|
83
|
+
|
|
84
|
+
//run and then poll status
|
|
85
|
+
export const runSync = curry(
|
|
86
|
+
async (apiKey: string, endpointId: string, request: EndpointInputPayload) => {
|
|
87
|
+
const maxWaitTimeMs = request?.policy?.executionTimeout ?? 300 * 1000
|
|
88
|
+
const startTime = Date.now()
|
|
89
|
+
const getRemainingTime = () => clamp(1000, 90000, maxWaitTimeMs - (Date.now() - startTime))
|
|
90
|
+
const runResp: any = await runsync(apiKey, endpointId, request)
|
|
91
|
+
let data: EndpointOutput = { ...runResp }
|
|
92
|
+
const { id } = data
|
|
93
|
+
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`)
|
|
97
|
+
return { ...data, started: true, completed: false }
|
|
98
|
+
}
|
|
99
|
+
data = await statusSync(apiKey, endpointId, id, getRemainingTime())
|
|
100
|
+
print(`${id}: ${data.status}`)
|
|
101
|
+
}
|
|
102
|
+
return { ...data, started: true, completed: true, succeeded: data.status === "COMPLETED" }
|
|
103
|
+
}
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
//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
|
+
})
|
|
112
|
+
|
|
113
|
+
//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
|
+
})
|
|
119
|
+
|
|
120
|
+
//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
|
+
})
|
|
126
|
+
//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
|
+
})
|
|
138
|
+
//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
|
+
})
|
|
144
|
+
//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
|
+
})
|
|
150
|
+
//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
|
+
})
|
|
156
|
+
|
|
157
|
+
class Endpoint {
|
|
158
|
+
endpointId: string = ""
|
|
159
|
+
private apiKey: string = ""
|
|
160
|
+
constructor(apiKey: string, endpointId: string) {
|
|
161
|
+
this.apiKey = apiKey
|
|
162
|
+
this.endpointId = endpointId
|
|
163
|
+
}
|
|
164
|
+
async runSync(request: EndpointInputPayload): Promise<EndpointCompletedOutput> {
|
|
165
|
+
return runSync(this.apiKey, this.endpointId, request)
|
|
166
|
+
}
|
|
167
|
+
async run(request: EndpointInputPayload): Promise<string> {
|
|
168
|
+
return run(this.apiKey, this.endpointId, request)
|
|
169
|
+
}
|
|
170
|
+
async getStatus(requestId: string): Promise<EndpointOutput> {
|
|
171
|
+
return getStatus(this.apiKey, this.endpointId, requestId)
|
|
172
|
+
}
|
|
173
|
+
async stream(requestId: string): Promise<EndpointOutput> {
|
|
174
|
+
return stream(this.apiKey, this.endpointId, requestId)
|
|
175
|
+
}
|
|
176
|
+
async cancel(requestId: string): Promise<CancelOutput> {
|
|
177
|
+
return cancel(this.apiKey, this.endpointId, requestId)
|
|
178
|
+
}
|
|
179
|
+
async getHealth(): Promise<HealthCheck> {
|
|
180
|
+
return getHealth(this.apiKey, this.endpointId)
|
|
181
|
+
}
|
|
182
|
+
async purgeQueue(): Promise<PurgeQueueOutput> {
|
|
183
|
+
return purgeQueue(this.apiKey, this.endpointId)
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export default (apiKey: string) => ({
|
|
188
|
+
endpoint: (endpointId: string) => new Endpoint(apiKey, endpointId),
|
|
189
|
+
//template...
|
|
190
|
+
//pod...
|
|
191
|
+
})
|
package/test/test.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import runpodSdk, { runSync } from "../dist/index.js"
|
|
2
|
+
|
|
3
|
+
const { RUNPOD_API_KEY, ENDPOINT_ID } = process.env
|
|
4
|
+
if (!(RUNPOD_API_KEY && ENDPOINT_ID)) {
|
|
5
|
+
process.exit()
|
|
6
|
+
}
|
|
7
|
+
//OOP
|
|
8
|
+
const runpod = runpodSdk(RUNPOD_API_KEY)
|
|
9
|
+
const endpoint = runpod.endpoint(ENDPOINT_ID)
|
|
10
|
+
const request = {
|
|
11
|
+
input: {
|
|
12
|
+
delay: 100,
|
|
13
|
+
name: "nate",
|
|
14
|
+
},
|
|
15
|
+
policy: {
|
|
16
|
+
executionTimeout: 120 * 1000,
|
|
17
|
+
},
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
console.log("\nrun")
|
|
21
|
+
const runResp = await endpoint.run(request)
|
|
22
|
+
console.log(runResp)
|
|
23
|
+
const { id: requestId } = runResp
|
|
24
|
+
console.log("\nstatus")
|
|
25
|
+
const statusResp = await endpoint.getStatus(requestId)
|
|
26
|
+
console.log(statusResp)
|
|
27
|
+
console.log("\ncancel")
|
|
28
|
+
const cancelResp = await endpoint.cancel(requestId)
|
|
29
|
+
console.log(cancelResp)
|
|
30
|
+
console.log("\nhealth")
|
|
31
|
+
const healthResp = await endpoint.getHealth()
|
|
32
|
+
console.log(healthResp)
|
|
33
|
+
console.log("\npurge queue")
|
|
34
|
+
const purgeResp = await endpoint.purgeQueue()
|
|
35
|
+
console.log(purgeResp)
|
|
36
|
+
console.log("\nafter purge")
|
|
37
|
+
console.log(await endpoint.getHealth())
|
|
38
|
+
|
|
39
|
+
console.log("runSync")
|
|
40
|
+
const result = await endpoint.runSync(request)
|
|
41
|
+
console.log(result)
|
|
42
|
+
//functional
|
|
43
|
+
// const run = runSync(RUNPOD_API_KEY, ENDPOINT_ID)
|
|
44
|
+
// const res = await run({
|
|
45
|
+
// input: {
|
|
46
|
+
// prompt: "a photo of a horse the size of a Boeing 787",
|
|
47
|
+
// },
|
|
48
|
+
// })
|
|
49
|
+
|
|
50
|
+
// console.log(res)
|
package/tsconfig.json
ADDED