replicate-api
Version:
A typed client library for the replicate.com API
29 lines (28 loc) • 1.34 kB
JavaScript
import { cancelPrediction } from "../cancelPrediction.js";
import { getPrediction } from "../getPrediction.js";
import { pollPrediction } from "../pollPrediction.js";
/** Convert the result that we get from replicate to a more idiomatic TypeScript object.
*
* Also adds `get()`, `cancel()` and `poll()` methods.
*/
export const convertPrediction = (options, prediction) => {
const PredictionState = {
id: prediction.id,
version: prediction.version,
get: async () => await getPrediction({ ...options, id: prediction.id }),
cancel: async () => await cancelPrediction({ ...options, id: prediction.id }),
poll: async (timeout) => await pollPrediction({ ...options, id: prediction.id, timeout: timeout }, PredictionState),
createdAt: prediction.created_at ? new Date(prediction.created_at) : undefined,
startedAt: prediction.started_at ? new Date(prediction.started_at) : undefined,
completedAt: prediction.completed_at ? new Date(prediction.completed_at) : undefined,
status: prediction.status,
input: prediction.input,
output: prediction.output,
error: prediction.error,
logs: prediction.logs ?? undefined,
metrics: {
predictTime: prediction.metrics?.predict_time,
},
};
return PredictionState;
};