-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
Expand file tree
/
Copy pathkie.ts
More file actions
109 lines (90 loc) · 2.71 KB
/
Copy pathkie.ts
File metadata and controls
109 lines (90 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { BaseExecutor } from "./base.ts";
import { sleep } from "../utils/sleep.ts";
import {
isJsonObject,
normalizeKieTaskState,
type JsonObject,
type KieTaskState,
} from "../utils/kieTask.ts";
export type { KieTaskState } from "../utils/kieTask.ts";
type KieTaskInput = {
baseUrl: string;
token: string;
payload: unknown;
endpoint?: string;
};
type KiePollInput = {
statusUrl: string;
taskId: string;
token: string;
timeoutMs: number;
pollIntervalMs: number;
};
export type KieTaskRecord = {
data: JsonObject;
state: KieTaskState;
};
function normalizeBaseUrl(baseUrl: string): string {
return baseUrl.replace(/\/$/, "");
}
export class KieExecutor extends BaseExecutor {
constructor() {
super("kie", { baseUrl: "https://api.kie.ai" });
}
getTaskCreateUrl(baseUrl: string, endpoint = "/api/v1/jobs/createTask"): string {
return `${normalizeBaseUrl(baseUrl)}${endpoint}`;
}
getTaskStatusUrl(baseUrl: string): string {
return `${normalizeBaseUrl(baseUrl)}/api/v1/jobs/recordInfo`;
}
async createTask({ baseUrl, token, payload, endpoint }: KieTaskInput): Promise<JsonObject> {
const res = await fetch(this.getTaskCreateUrl(baseUrl, endpoint), {
method: "POST",
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
});
if (!res.ok) {
const error = await res.text();
throw Object.assign(new Error(error || `Kie createTask failed with status ${res.status}`), {
status: res.status,
});
}
const data = (await res.json()) as unknown;
return isJsonObject(data) ? data : {};
}
async pollTask({
statusUrl,
taskId,
token,
timeoutMs,
pollIntervalMs,
}: KiePollInput): Promise<KieTaskRecord> {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
const pollUrl = new URL(statusUrl);
pollUrl.searchParams.set("taskId", String(taskId));
const res = await fetch(pollUrl.toString(), {
method: "GET",
headers: { Authorization: `Bearer ${token}` },
});
if (!res.ok) {
const error = await res.text();
throw Object.assign(new Error(error || `Kie poll failed with status ${res.status}`), {
status: res.status,
});
}
const data = (await res.json()) as unknown;
const recordData = isJsonObject(data) ? data : {};
const state = normalizeKieTaskState(recordData);
if (state !== "pending") {
return { data: recordData, state };
}
await sleep(pollIntervalMs);
}
throw Object.assign(new Error("Kie task timed out"), { status: 504 });
}
}
export const kieExecutor = new KieExecutor();