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