replicate-api
Version:
A typed client library for the replicate.com API
76 lines (75 loc) • 2.68 kB
TypeScript
import { ReplicateRequestOptions } from "./makeApiRequest";
export type PredictionStatus = "starting" | "processing" | "succeeded" | "failed" | "canceled";
export type PredictionResponse = {
id: string;
version: string;
urls: {
get: string;
cancel: string;
};
created_at: string;
started_at: string | null;
completed_at: string | null;
status: PredictionStatus;
input: Record<string, unknown>;
output: unknown;
error: null;
logs: null | string;
metrics: {
/** In seconds */
predict_time?: number;
};
};
/** Status of a prediction
*
* The status is not automatically updated. You can use the `.get()` function on this object to get a new object with the current state on `replicate.com`. You can use `.poll()` to wait for the prediction to finish.
*/
export type PredictionState = {
/** The id of this prediction */
id: string;
/** The version of the model used to generate this prediction */
version: string;
/** Get the updated state of this prediction from replicate
*
* @returns A new `PredictionState` representing the current state.
*/
get: () => Promise<PredictionState>;
/** Cancel a running prediction.
*
* @returns A new `PredictionState` representing the updated state.
*/
cancel: () => Promise<PredictionState>;
/** Poll until the prediction is completed or failed
*
* If the timeout occurs an error is thrown.
*
* @param timeout The timeout in milliseconds
* @returns The `PredictionState` for the finished prediction. It has a status of either "succeeded", "failed" or "canceled".
*/
poll: (timeout?: number) => Promise<PredictionState>;
/** When the prediction was created */
createdAt?: Date;
/** When execution of the prediction was started */
startedAt?: Date;
/** When execution of the prediction was completed (or cancelled) */
completedAt?: Date;
/** The status of the prediction */
status: PredictionStatus;
/** The input parameters */
input: Record<string, unknown>;
/** The output parameters */
output: unknown;
error: null;
/** The logs of the prediction. A string seperated by newlines */
logs?: string;
/** Metrics about the prediction */
metrics: {
/** In seconds */
predictTime?: number;
};
};
/** Convert the result that we get from replicate to a more idiomatic TypeScript object.
*
* Also adds `get()`, `cancel()` and `poll()` methods.
*/
export declare const convertPrediction: (options: ReplicateRequestOptions, prediction: PredictionResponse) => PredictionState;