runpod-sdk 1.1.2 → 1.2.0-beta.1
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 +217 -32
- package/dist/src/graphql/client.d.ts +22 -0
- package/dist/src/graphql/client.js +75 -0
- package/dist/src/graphql/client.js.map +1 -0
- package/dist/src/graphql/generated/index.d.ts +742 -0
- package/dist/src/graphql/generated/index.js +347 -0
- package/dist/src/graphql/generated/index.js.map +1 -0
- package/dist/src/graphql/generated/operations.d.ts +428 -0
- package/dist/src/graphql/generated/operations.js +2 -0
- package/dist/src/graphql/generated/operations.js.map +1 -0
- package/dist/src/graphql/generated/sdk.d.ts +31 -0
- package/dist/src/graphql/generated/sdk.js +232 -0
- package/dist/src/graphql/generated/sdk.js.map +1 -0
- package/dist/src/graphql/generated/types.d.ts +4281 -0
- package/dist/src/graphql/generated/types.js +2 -0
- package/dist/src/graphql/generated/types.js.map +1 -0
- package/dist/src/index.d.ts +53 -8
- package/dist/src/index.js +73 -9
- package/dist/src/index.js.map +1 -1
- package/dist/src/pods/generated-gpu-types.d.ts +299 -0
- package/dist/src/pods/generated-gpu-types.js +148 -0
- package/dist/src/pods/generated-gpu-types.js.map +1 -0
- package/dist/src/pods/index.d.ts +122 -0
- package/dist/src/pods/index.js +242 -0
- package/dist/src/pods/index.js.map +1 -0
- package/package.json +16 -2
- package/schema.gql +161 -0
- package/.prettierrc +0 -12
- package/README-dev.md +0 -7
- package/examples/basic.js +0 -19
- package/examples/cancel.js +0 -27
- package/examples/devServerless.js +0 -17
- package/examples/errors.js +0 -11
- package/examples/health.js +0 -11
- package/examples/purge-queue.js +0 -11
- package/examples/run.js +0 -27
- package/examples/runSync.js +0 -17
- package/examples/stream.js +0 -48
- package/examples/test.js +0 -74
- package/src/index.ts +0 -330
- package/tsconfig.json +0 -17
package/src/index.ts
DELETED
|
@@ -1,330 +0,0 @@
|
|
|
1
|
-
import xior, { XiorResponse as AxiosResponse } from "xior"
|
|
2
|
-
import { curry, clamp, isNil } from "ramda"
|
|
3
|
-
import { createRequire } from 'module'
|
|
4
|
-
const require = createRequire(import.meta.url)
|
|
5
|
-
const pkg = require('../../package.json')
|
|
6
|
-
|
|
7
|
-
const axios = xior.create();
|
|
8
|
-
|
|
9
|
-
export type ExecutionPolicy = {
|
|
10
|
-
executionTimeout?: number
|
|
11
|
-
lowPriority?: boolean
|
|
12
|
-
ttl?: number
|
|
13
|
-
}
|
|
14
|
-
export type S3Config = {
|
|
15
|
-
accessId: string
|
|
16
|
-
accessSecret: string
|
|
17
|
-
bucketName: string
|
|
18
|
-
endpointUrl: string
|
|
19
|
-
objectPath?: string
|
|
20
|
-
}
|
|
21
|
-
export type EndpointInputPayload = {
|
|
22
|
-
input: any
|
|
23
|
-
webhook?: string
|
|
24
|
-
webhookV2?: string
|
|
25
|
-
s3Config?: S3Config
|
|
26
|
-
policy?: ExecutionPolicy
|
|
27
|
-
}
|
|
28
|
-
export type EndpointIncompleteOutput = {
|
|
29
|
-
delayTime?: number
|
|
30
|
-
status: string
|
|
31
|
-
id: string
|
|
32
|
-
}
|
|
33
|
-
export type EndpointCompletedOutput = {
|
|
34
|
-
status: string
|
|
35
|
-
id: string
|
|
36
|
-
output: any
|
|
37
|
-
executionTime: number
|
|
38
|
-
delayTime: number
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export type EndpointOutput = EndpointCompletedOutput | EndpointIncompleteOutput
|
|
42
|
-
|
|
43
|
-
export type EndpointStreamOutput = {
|
|
44
|
-
status: string
|
|
45
|
-
stream: [any]
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export type CancelOutput = {
|
|
49
|
-
status: string
|
|
50
|
-
id: string
|
|
51
|
-
executionTime: number
|
|
52
|
-
delayTime: number
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export type HealthCheck = {
|
|
56
|
-
jobs: { completed: number; failed: number; inProgress: number; inQueue: number; retried: number }
|
|
57
|
-
workers: { idle: number; initializing: number; ready: number; running: number; throttled: number }
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export type PurgeQueueOutput = {
|
|
61
|
-
removed: number
|
|
62
|
-
status: string
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export type SdkOptions = {
|
|
66
|
-
baseUrl: string
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function getUserAgent() {
|
|
70
|
-
const sdkVersion = pkg.version;
|
|
71
|
-
|
|
72
|
-
let environmentInfo = 'Unknown Environment';
|
|
73
|
-
|
|
74
|
-
if (typeof window !== 'undefined' && window.navigator) {
|
|
75
|
-
environmentInfo = `Browser/${window.navigator.userAgent}`;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
return `RunPod-JS-SDK/${sdkVersion} (${environmentInfo})`;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
const getAuthHeader = (apiKey: string) => ({
|
|
82
|
-
headers: {
|
|
83
|
-
"Authorization": `Bearer ${apiKey}`,
|
|
84
|
-
"content-type": "application/json",
|
|
85
|
-
"User-Agent": getUserAgent()
|
|
86
|
-
},
|
|
87
|
-
})
|
|
88
|
-
const print = console.log
|
|
89
|
-
|
|
90
|
-
const handleErrors = async (axiosRequest: Promise<AxiosResponse>) => {
|
|
91
|
-
const resp = await axiosRequest
|
|
92
|
-
const { status, statusText } = resp
|
|
93
|
-
if (status !== 200) {
|
|
94
|
-
return { status, statusText }
|
|
95
|
-
}
|
|
96
|
-
return resp.data
|
|
97
|
-
}
|
|
98
|
-
const handleErrorsStatus = async (axiosRequest: Promise<AxiosResponse>) => {
|
|
99
|
-
const resp = await axiosRequest
|
|
100
|
-
const { status, statusText } = resp
|
|
101
|
-
if (status !== 200) {
|
|
102
|
-
return { status, statusText, started: false }
|
|
103
|
-
}
|
|
104
|
-
return {
|
|
105
|
-
...resp.data,
|
|
106
|
-
started: true,
|
|
107
|
-
completed: isCompleted(resp.data.status),
|
|
108
|
-
succeeded: resp.data.status === "COMPLETED",
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
export const runpodServerlessBaseUrlProd = "https://api.runpod.ai/v2"
|
|
112
|
-
export const runpodServerlessBaseUrlDev = "https://dev-api.runpod.ai/v2"
|
|
113
|
-
const getEndpointUrl = curry((baseUrl, endpointId: string) => `${baseUrl}/${endpointId}`)
|
|
114
|
-
const isCompleted = (status: string) =>
|
|
115
|
-
["COMPLETED", "FAILED", "CANCELLED", "TIMED_OUT"].includes(status)
|
|
116
|
-
|
|
117
|
-
//run and then poll status
|
|
118
|
-
export const runSync = curry(
|
|
119
|
-
async (
|
|
120
|
-
baseUrl: string,
|
|
121
|
-
apiKey: string,
|
|
122
|
-
endpointId: string,
|
|
123
|
-
request: EndpointInputPayload,
|
|
124
|
-
timeout: number = 90000
|
|
125
|
-
) => {
|
|
126
|
-
const startTime = Date.now()
|
|
127
|
-
const getRemainingTime = () => clamp(1000, 90000, timeout - (Date.now() - startTime))
|
|
128
|
-
const runResp: any = await runsync(baseUrl, apiKey, endpointId, request, timeout)
|
|
129
|
-
let data: EndpointOutput = { ...runResp }
|
|
130
|
-
const { id } = data
|
|
131
|
-
const start = Date.now()
|
|
132
|
-
while (!isCompleted(data.status)) {
|
|
133
|
-
if (Date.now() - start > timeout) {
|
|
134
|
-
print(`${id} timed out after ${timeout / 1000} seconds`)
|
|
135
|
-
return { ...data, started: true, completed: false }
|
|
136
|
-
}
|
|
137
|
-
data = await statusSync(baseUrl, apiKey, endpointId, id, getRemainingTime())
|
|
138
|
-
print(`${id}: ${data.status}`)
|
|
139
|
-
}
|
|
140
|
-
return { ...data, started: true, completed: true, succeeded: data.status === "COMPLETED" }
|
|
141
|
-
}
|
|
142
|
-
)
|
|
143
|
-
|
|
144
|
-
//wrapper over /status-sync
|
|
145
|
-
const statusSync = curry(
|
|
146
|
-
(
|
|
147
|
-
baseUrl: string,
|
|
148
|
-
apiKey: string,
|
|
149
|
-
endpointId: string,
|
|
150
|
-
requestId: String,
|
|
151
|
-
timeout: number = 90000
|
|
152
|
-
) => {
|
|
153
|
-
const wait = clamp(1000, 90000, timeout)
|
|
154
|
-
const url = getEndpointUrl(baseUrl, endpointId) + "/status-sync/" + requestId + `?wait=${wait}`
|
|
155
|
-
const authHeader = getAuthHeader(apiKey)
|
|
156
|
-
return handleErrorsStatus(axios.get(url, { ...authHeader }))
|
|
157
|
-
}
|
|
158
|
-
)
|
|
159
|
-
|
|
160
|
-
//wrapper over /runsync
|
|
161
|
-
const runsync = curry(
|
|
162
|
-
(
|
|
163
|
-
baseUrl: string,
|
|
164
|
-
apiKey: string,
|
|
165
|
-
endpointId: string,
|
|
166
|
-
request: EndpointInputPayload,
|
|
167
|
-
timeout: number = 90000
|
|
168
|
-
) => {
|
|
169
|
-
const wait = clamp(1000, 90000, timeout)
|
|
170
|
-
const url = getEndpointUrl(baseUrl, endpointId) + "/runsync" + `?wait=${wait}`
|
|
171
|
-
const authHeader = getAuthHeader(apiKey)
|
|
172
|
-
return handleErrorsStatus(axios.post(url, request, { ...authHeader }))
|
|
173
|
-
}
|
|
174
|
-
)
|
|
175
|
-
|
|
176
|
-
//wrapper over /run
|
|
177
|
-
export const run = curry(
|
|
178
|
-
(
|
|
179
|
-
baseUrl: string,
|
|
180
|
-
apiKey: string,
|
|
181
|
-
endpointId: string,
|
|
182
|
-
request: EndpointInputPayload,
|
|
183
|
-
timeout: number = 3000
|
|
184
|
-
) => {
|
|
185
|
-
const url = getEndpointUrl(baseUrl, endpointId) + "/run"
|
|
186
|
-
const authHeader = getAuthHeader(apiKey)
|
|
187
|
-
return handleErrors(axios.post(url, request, { ...authHeader, timeout }))
|
|
188
|
-
}
|
|
189
|
-
)
|
|
190
|
-
//wrapper over /status
|
|
191
|
-
export const status = curry(
|
|
192
|
-
(
|
|
193
|
-
baseUrl: string,
|
|
194
|
-
apiKey: string,
|
|
195
|
-
endpointId: string,
|
|
196
|
-
requestId: string,
|
|
197
|
-
timeout: number = 3000
|
|
198
|
-
) => {
|
|
199
|
-
const url = getEndpointUrl(baseUrl, endpointId) + "/status/" + requestId
|
|
200
|
-
const authHeader = getAuthHeader(apiKey)
|
|
201
|
-
return handleErrorsStatus(axios.get(url, { ...authHeader, timeout }))
|
|
202
|
-
}
|
|
203
|
-
)
|
|
204
|
-
|
|
205
|
-
//generator yielding results of stream
|
|
206
|
-
export async function* stream(
|
|
207
|
-
baseUrl: string,
|
|
208
|
-
apiKey: string,
|
|
209
|
-
endpointId: string,
|
|
210
|
-
requestId: string,
|
|
211
|
-
timeout: number = 0
|
|
212
|
-
) {
|
|
213
|
-
let completed = false
|
|
214
|
-
const start = Date.now()
|
|
215
|
-
while (!completed) {
|
|
216
|
-
const url = getEndpointUrl(baseUrl, endpointId) + "/stream/" + requestId
|
|
217
|
-
const authHeader = getAuthHeader(apiKey)
|
|
218
|
-
const resp = await handleErrors(axios.get(url, authHeader))
|
|
219
|
-
if (timeout !== 0 && Date.now() - start > timeout) {
|
|
220
|
-
print(`stream timed out after ${timeout / 1000} seconds`)
|
|
221
|
-
completed = true
|
|
222
|
-
}
|
|
223
|
-
if (isCompleted(resp.status)) {
|
|
224
|
-
completed = true
|
|
225
|
-
}
|
|
226
|
-
for (const output of resp?.stream) {
|
|
227
|
-
yield output
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
//wrapper over /cancel
|
|
233
|
-
const cancel = curry(
|
|
234
|
-
(
|
|
235
|
-
baseUrl: string,
|
|
236
|
-
apiKey: string,
|
|
237
|
-
endpointId: string,
|
|
238
|
-
requestId: string,
|
|
239
|
-
timeout: number = 3000
|
|
240
|
-
) => {
|
|
241
|
-
const url = getEndpointUrl(baseUrl, endpointId) + "/cancel/" + requestId
|
|
242
|
-
const authHeader = getAuthHeader(apiKey)
|
|
243
|
-
return handleErrors(axios.post(url, {}, { ...authHeader, timeout }))
|
|
244
|
-
}
|
|
245
|
-
)
|
|
246
|
-
//wrapper over /health
|
|
247
|
-
export const health = curry(
|
|
248
|
-
(baseUrl: string, apiKey: string, endpointId: string, timeout: number = 3000) => {
|
|
249
|
-
const url = getEndpointUrl(baseUrl, endpointId) + "/health"
|
|
250
|
-
const authHeader = getAuthHeader(apiKey)
|
|
251
|
-
return handleErrors(axios.get(url, { ...authHeader, timeout }))
|
|
252
|
-
}
|
|
253
|
-
)
|
|
254
|
-
//wrapper over /purge-queue
|
|
255
|
-
export const purgeQueue = curry(
|
|
256
|
-
(baseUrl: string, apiKey: string, endpointId: string, timeout: number = 3000) => {
|
|
257
|
-
const url = getEndpointUrl(baseUrl, endpointId) + "/purge-queue"
|
|
258
|
-
const authHeader = getAuthHeader(apiKey)
|
|
259
|
-
return handleErrors(axios.post(url, {}, { ...authHeader, timeout }))
|
|
260
|
-
}
|
|
261
|
-
)
|
|
262
|
-
|
|
263
|
-
class Endpoint {
|
|
264
|
-
endpointId: string = ""
|
|
265
|
-
baseUrl: string = runpodServerlessBaseUrlProd
|
|
266
|
-
private apiKey: string = ""
|
|
267
|
-
constructor(baseUrl: string, apiKey: string, endpointId: string) {
|
|
268
|
-
this.baseUrl = baseUrl
|
|
269
|
-
this.apiKey = apiKey
|
|
270
|
-
this.endpointId = endpointId
|
|
271
|
-
}
|
|
272
|
-
async runSync(
|
|
273
|
-
request: EndpointInputPayload,
|
|
274
|
-
timeout: number = 90000
|
|
275
|
-
): Promise<EndpointCompletedOutput> {
|
|
276
|
-
return runSync(this.baseUrl, this.apiKey, this.endpointId, request, timeout)
|
|
277
|
-
}
|
|
278
|
-
async run(
|
|
279
|
-
request: EndpointInputPayload,
|
|
280
|
-
timeout: number = 3000
|
|
281
|
-
): Promise<EndpointIncompleteOutput> {
|
|
282
|
-
return run(this.baseUrl, this.apiKey, this.endpointId, request, timeout)
|
|
283
|
-
}
|
|
284
|
-
async status(requestId: string, timeout: number = 3000): Promise<EndpointOutput> {
|
|
285
|
-
return status(this.baseUrl, this.apiKey, this.endpointId, requestId, timeout)
|
|
286
|
-
}
|
|
287
|
-
async statusSync(requestId: string, timeout: number = 90000): Promise<EndpointOutput> {
|
|
288
|
-
return statusSync(this.baseUrl, this.apiKey, this.endpointId, requestId, timeout)
|
|
289
|
-
}
|
|
290
|
-
//default to no timeout
|
|
291
|
-
stream(requestId: string, timeout: number = 0): AsyncGenerator<any> {
|
|
292
|
-
return stream(this.baseUrl, this.apiKey, this.endpointId, requestId, timeout)
|
|
293
|
-
}
|
|
294
|
-
async cancel(requestId: string, timeout: number = 3000): Promise<CancelOutput> {
|
|
295
|
-
return cancel(this.baseUrl, this.apiKey, this.endpointId, requestId, timeout)
|
|
296
|
-
}
|
|
297
|
-
async health(timeout: number = 3000): Promise<HealthCheck> {
|
|
298
|
-
return health(this.baseUrl, this.apiKey, this.endpointId, timeout)
|
|
299
|
-
}
|
|
300
|
-
async purgeQueue(timeout: number = 3000): Promise<PurgeQueueOutput> {
|
|
301
|
-
return purgeQueue(this.baseUrl, this.apiKey, this.endpointId, timeout)
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
const defaultSdkOptions = {
|
|
306
|
-
baseUrl: runpodServerlessBaseUrlProd,
|
|
307
|
-
}
|
|
308
|
-
class RunpodSdk {
|
|
309
|
-
private apiKey: string = ""
|
|
310
|
-
baseUrl: string = runpodServerlessBaseUrlProd
|
|
311
|
-
constructor(apiKey: string, options: SdkOptions) {
|
|
312
|
-
if (isNil(apiKey)) {
|
|
313
|
-
print("Api key not supplied")
|
|
314
|
-
return
|
|
315
|
-
}
|
|
316
|
-
this.apiKey = apiKey
|
|
317
|
-
this.baseUrl = options.baseUrl ?? this.baseUrl
|
|
318
|
-
}
|
|
319
|
-
endpoint(endpointId: string) {
|
|
320
|
-
if (isNil(endpointId)) {
|
|
321
|
-
print("Endpoint id not supplied")
|
|
322
|
-
return null
|
|
323
|
-
}
|
|
324
|
-
return new Endpoint(this.baseUrl, this.apiKey, endpointId)
|
|
325
|
-
}
|
|
326
|
-
//pod...
|
|
327
|
-
//template...
|
|
328
|
-
}
|
|
329
|
-
export default (apiKey: string, options: SdkOptions = defaultSdkOptions) =>
|
|
330
|
-
new RunpodSdk(apiKey, options)
|
package/tsconfig.json
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2020",
|
|
4
|
-
"module": "ES2020",
|
|
5
|
-
"outDir": "./dist",
|
|
6
|
-
"rootDir": ".",
|
|
7
|
-
"moduleResolution": "node",
|
|
8
|
-
"lib": ["ES2020", "DOM"],
|
|
9
|
-
"types": ["node"],
|
|
10
|
-
"strict": true,
|
|
11
|
-
"esModuleInterop": true,
|
|
12
|
-
"declaration": true,
|
|
13
|
-
"sourceMap": true,
|
|
14
|
-
"resolveJsonModule": true
|
|
15
|
-
},
|
|
16
|
-
"include": ["src/**/*.ts"]
|
|
17
|
-
}
|