replicate-client.ts implements a client for interacting with the Replicate API to generate images using AI models. It provides methods for generating images, processing them for Printify, and cleaning up temporary files.
fs: File system module for reading and writing filespath: Path manipulation modulesharp: Image processing libraryreplicate: Official Replicate SDKfs/promises: Promise-based file system module
The main class for interacting with the Replicate API.
constructor(apiToken: string)apiToken: The Replicate API token
The constructor:
- Initializes the Replicate client with the API token
- Creates a temporary directory for downloaded images if it doesn't exist
private client: The Replicate clientprivate tempDir: The path to the temporary directory
Generates an image using the Flux 1.1 Pro model and saves it to a file.
async generateImage(prompt: string, options: any = {}): Promise<string>prompt: The text prompt to generate an image fromoptions: Additional options for the model- Returns: A promise that resolves to the path of the generated image file
This method:
- Sets up the input parameters for the model, including:
prompt: The text promptprompt_upsampling: Always set to trueaspect_ratio: Optional aspect ratiowidthandheight: Optional dimensionsseed: Optional seed for reproducible generationnum_inference_steps: Optional number of inference stepsguidance_scale: Optional guidance scalenegative_prompt: Optional negative prompt
- Generates a temporary file path for the output image
- Runs the Flux 1.1 Pro model using the Replicate client
- Saves the output directly to a file
- Returns the path to the generated image file
Processes an image with Sharp to ensure it's a valid PNG for Printify.
async processImageForPrintify(inputPath: string, outputFilename: string): Promise<string>inputPath: The path to the input image from ReplicateoutputFilename: The filename to save the processed image as- Returns: A promise that resolves to the path of the processed image ready for Printify upload
This method:
- Generates a temporary file path for the output
- Processes the image with Sharp to ensure it's a valid PNG for Printify
- Returns the path to the processed image
Cleans up temporary files.
async cleanupTempFiles(filePaths: string[]): Promise<void>filePaths: Array of file paths to delete- Returns: A promise that resolves when all files are deleted
This method:
- Iterates through the file paths
- Deletes each file if it exists
- Logs errors but continues with other files even if one fails
The class includes robust error handling for all methods, with detailed error messages to help troubleshoot issues. For example:
catch (error: any) {
// Provide detailed error information
const errorDetails = {
message: error.message,
prompt: prompt,
options: JSON.stringify(options)
};
throw new Error(`Replicate API error: ${error.message}\nDetails: ${JSON.stringify(errorDetails, null, 2)}`);
}replicate-client.ts provides a client for generating images using the Replicate API, specifically the Flux 1.1 Pro model. It includes methods for processing the generated images for Printify and cleaning up temporary files. The class includes robust error handling and detailed logging to help troubleshoot issues.