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
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
/**
Get the status or result of a prediction on replicate.com
https://replicate.com/docs/reference/http#get-prediction
*/
export async function getReplicatePrediction({
/** https://replicate.com/account */
apiKey,
predictionId,
}: {
apiKey: string;
predictionId: string;
}) {
if (!apiKey) {
throw new Error("missing apiKey; visit https://replicate.com/account");
} else if (!predictionId) {
throw new Error("missing predictionId");
}
const result = await fetchJSON(
`https://api.replicate.com/v1/predictions/${encodeURIComponent(predictionId)}`,
{
headers: {
Authorization: `Token ${apiKey}`,
},
},
);
return result as Prediction;
}
interface Prediction {
id: string;
version: string;
urls: {
get: string;
cancel: string;
};
created_at: string;
started_at: string;
completed_at: string;
source: string;
status: "starting" | "processing" | "succeeded" | "failed" | "canceled";
input: {
prompt: string;
};
output: Array<string>;
error: any;
logs: string;
metrics: {
predict_time?: number;
};
}