diff --git a/Runware/Runware-base.ts b/Runware/Runware-base.ts index 86f20f0..edddb53 100644 --- a/Runware/Runware-base.ts +++ b/Runware/Runware-base.ts @@ -33,6 +33,7 @@ import { TImageMasking, TModelSearchResponse, TServerError, + IOutputType, } from "./types"; import { BASE_RUNWARE_URLS, @@ -144,8 +145,8 @@ export class RunwareBase { const arrayErrors = (msg as any)?.[0]?.errors ? (msg as any)?.[0]?.errors : Array.isArray(msg?.errors) - ? msg.errors - : [msg.errors]; + ? msg.errors + : [msg.errors]; const filteredMessage = arrayMessage.filter( (v) => (v?.taskUUID || v?.taskType) === taskUUID @@ -372,7 +373,7 @@ export class RunwareBase { }); } - async requestImages( + async requestImages( { outputType, outputFormat, @@ -407,10 +408,10 @@ export class RunwareBase { ipAdapters, outpaint, }: // imageSize, - // gScale, - IRequestImage, + // gScale, + IRequestImage, moreOptions?: Record - ): Promise { + ): Promise[] | undefined> { let lis: any = undefined; let requestObject: Record | undefined = undefined; let taskUUIDs: string[] = []; @@ -661,12 +662,12 @@ export class RunwareBase { } }; - requestImageToText = async ({ + requestImageToText = async ({ inputImage, includeCost, customTaskUUID, retry, - }: IRequestImageToText): Promise => { + }: IRequestImageToText): Promise> => { const totalRetry = retry || this._globalMaxRetries; let lis: any = undefined; @@ -731,9 +732,9 @@ export class RunwareBase { } }; - removeImageBackground = async ( - payload: IRemoveImageBackground - ): Promise => { + removeImageBackground = async( + payload: IRemoveImageBackground + ): Promise> => { return this.baseSingleRequest({ payload: { ...payload, @@ -830,7 +831,7 @@ export class RunwareBase { // } }; - upscaleGan = async ({ + upscaleGan = async({ inputImage, upscaleFactor, outputType, @@ -839,7 +840,7 @@ export class RunwareBase { outputQuality, customTaskUUID, retry, - }: IUpscaleGan): Promise => { + }: IUpscaleGan): Promise> => { const totalRetry = retry || this._globalMaxRetries; let lis: any = undefined; @@ -903,14 +904,14 @@ export class RunwareBase { } }; - enhancePrompt = async ({ + enhancePrompt = async ({ prompt, promptMaxLength = 380, promptVersions = 1, includeCost, customTaskUUID, retry, - }: IPromptEnhancer): Promise => { + }: IPromptEnhancer): Promise[]> => { const totalRetry = retry || this._globalMaxRetries; let lis: any = undefined; @@ -1217,7 +1218,7 @@ export class RunwareBase { throw this._connectionError; } - return new Promise((resolve, reject) => { + return new Promise((resolve, reject) => { // const isConnected = let retry = 0; const MAX_RETRY = 30; @@ -1257,7 +1258,7 @@ export class RunwareBase { if (hasConnected) { clearAllIntervals(); - resolve(true); + resolve(); } else if (retry >= MAX_RETRY) { clearAllIntervals(); reject(new Error("Retry timed out")); @@ -1279,7 +1280,7 @@ export class RunwareBase { if (hasConnected) { clearAllIntervals(); - resolve(true); + resolve(); return; } if (!!this.isInvalidAPIKey()) { diff --git a/Runware/types.ts b/Runware/types.ts index 1a0b755..ad48249 100644 --- a/Runware/types.ts +++ b/Runware/types.ts @@ -34,23 +34,41 @@ export type RunwareBaseType = { export type IOutputType = "base64Data" | "dataURI" | "URL"; export type IOutputFormat = "JPG" | "PNG" | "WEBP"; -export interface IImage { + +interface IOutputTypeFieldBase64 { + imageBase64Data: string +} + +interface IOutputTypeFieldUrl { + imageURL: string; +} + +interface IOutputTypeFieldUri { + imageDataURI: string; +} + +type OutputTypeFieldMap = { + base64Data: IOutputTypeFieldBase64; + dataURI: IOutputTypeFieldUri; + URL: IOutputTypeFieldUrl; +}; + +type CostField = Value extends true ? { cost: number } : {} + +export type IImage = { taskType: ETaskType; imageUUID: string; inputImageUUID?: string; taskUUID: string; - imageURL?: string; - imageBase64Data?: string; - imageDataURI?: string; NSFWContent?: boolean; - cost?: number; seed: number; -} +} & OutputTypeFieldMap[TOutput] & CostField -export interface ITextToImage extends IImage { +export type ITextToImage = { positivePrompt?: string; negativePrompt?: string; -} +} & IImage & CostField + export interface IControlNetImage { taskUUID: string; inputImageUUID: string; @@ -131,8 +149,8 @@ export interface IError { export type TPromptWeighting = "compel" | "sdEmbeds"; -export interface IRequestImage { - outputType?: IOutputType; +export interface IRequestImage { + outputType?: TOutput; outputFormat?: IOutputFormat; uploadEndpoint?: string; checkNSFW?: boolean; @@ -156,7 +174,7 @@ export interface IRequestImage { usePromptWeighting?: boolean; promptWeighting?: TPromptWeighting; numberResults?: number; // default to 1 - includeCost?: boolean; + includeCost?: HasCost; outputQuality?: number; controlNet?: IControlNet[]; @@ -195,21 +213,20 @@ export interface IRefiner { startStep?: number; startStepPercentage?: number; } -export interface IRequestImageToText { +export interface IRequestImageToText { inputImage?: File | string; - includeCost?: boolean; + includeCost?: HasCost; customTaskUUID?: string; retry?: number; } -export interface IImageToText { +export type IImageToText = { taskType: ETaskType; taskUUID: string; text: string; - cost?: number; -} +} & CostField -export interface IRemoveImageBackground extends IRequestImageToText { - outputType?: IOutputType; +export type IRemoveImageBackground = { + outputType?: TOutput; outputFormat?: IOutputFormat; model: string; settings?: { @@ -221,41 +238,36 @@ export interface IRemoveImageBackground extends IRequestImageToText { alphaMattingBackgroundThreshold?: number; alphaMattingErodeSize?: number; }; - includeCost?: boolean; + includeCost?: HasCost; outputQuality?: number; retry?: number; -} +} & IRequestImageToText -export interface IRemoveImage { +export type IRemoveImage = { taskType: ETaskType; taskUUID: string; imageUUID: string; inputImageUUID: string; - imageURL?: string; - imageBase64Data?: string; - imageDataURI?: string; - cost?: number; -} +} & OutputTypeFieldMap[TOutput] & CostField -export interface IPromptEnhancer { +export interface IPromptEnhancer { promptMaxLength?: number; promptVersions?: number; prompt: string; - includeCost?: boolean; + includeCost?: HasCost; customTaskUUID?: string; retry?: number; } -export interface IEnhancedPrompt extends IImageToText {} +export type IEnhancedPrompt = IImageToText -export interface IUpscaleGan { +export interface IUpscaleGan { inputImage: File | string; upscaleFactor: number; - outputType?: IOutputType; + outputType?: TOuput; outputFormat?: IOutputFormat; - includeCost?: boolean; + includeCost?: HasCost; outputQuality?: number; - customTaskUUID?: string; retry?: number; } @@ -355,7 +367,7 @@ export type RequireOnlyOne = Pick< > & { [K in Keys]-?: Required> & - Partial, undefined>>; + Partial, undefined>>; }[Keys]; export type ListenerType = {