import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
export async function getReplicatePrediction({
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;
};
}