Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions Runware/Runware-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
TImageMasking,
TModelSearchResponse,
TServerError,
IOutputType,
} from "./types";
import {
BASE_RUNWARE_URLS,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -372,7 +373,7 @@ export class RunwareBase {
});
}

async requestImages(
async requestImages<TOutput extends IOutputType = any, HasCost extends boolean = false>(
{
outputType,
outputFormat,
Expand Down Expand Up @@ -407,10 +408,10 @@ export class RunwareBase {
ipAdapters,
outpaint,
}: // imageSize,
// gScale,
IRequestImage,
// gScale,
IRequestImage<TOutput, HasCost>,
moreOptions?: Record<string, any>
): Promise<ITextToImage[] | undefined> {
): Promise<ITextToImage<TOutput, HasCost>[] | undefined> {
let lis: any = undefined;
let requestObject: Record<string, any> | undefined = undefined;
let taskUUIDs: string[] = [];
Expand Down Expand Up @@ -661,12 +662,12 @@ export class RunwareBase {
}
};

requestImageToText = async ({
requestImageToText = async <HasCost extends boolean = false>({
inputImage,
includeCost,
customTaskUUID,
retry,
}: IRequestImageToText): Promise<IImageToText> => {
}: IRequestImageToText<HasCost>): Promise<IImageToText<HasCost>> => {
const totalRetry = retry || this._globalMaxRetries;
let lis: any = undefined;

Expand Down Expand Up @@ -731,9 +732,9 @@ export class RunwareBase {
}
};

removeImageBackground = async (
payload: IRemoveImageBackground
): Promise<IRemoveImage> => {
removeImageBackground = async<TOutput extends IOutputType = any, HasCost extends boolean = false>(
payload: IRemoveImageBackground<TOutput, HasCost>
): Promise<IRemoveImage<TOutput, HasCost>> => {
return this.baseSingleRequest({
payload: {
...payload,
Expand Down Expand Up @@ -830,7 +831,7 @@ export class RunwareBase {
// }
};

upscaleGan = async ({
upscaleGan = async<TOutput extends IOutputType = any, HasCost extends boolean = false>({
inputImage,
upscaleFactor,
outputType,
Expand All @@ -839,7 +840,7 @@ export class RunwareBase {
outputQuality,
customTaskUUID,
retry,
}: IUpscaleGan): Promise<IImage> => {
}: IUpscaleGan<TOutput, HasCost>): Promise<IImage<TOutput, HasCost>> => {
const totalRetry = retry || this._globalMaxRetries;
let lis: any = undefined;

Expand Down Expand Up @@ -903,14 +904,14 @@ export class RunwareBase {
}
};

enhancePrompt = async ({
enhancePrompt = async<HasCost extends boolean = false> ({
prompt,
promptMaxLength = 380,
promptVersions = 1,
includeCost,
customTaskUUID,
retry,
}: IPromptEnhancer): Promise<IEnhancedPrompt[]> => {
}: IPromptEnhancer<HasCost>): Promise<IEnhancedPrompt<HasCost>[]> => {
const totalRetry = retry || this._globalMaxRetries;
let lis: any = undefined;

Expand Down Expand Up @@ -1217,7 +1218,7 @@ export class RunwareBase {
throw this._connectionError;
}

return new Promise((resolve, reject) => {
return new Promise<void>((resolve, reject) => {
// const isConnected =
let retry = 0;
const MAX_RETRY = 30;
Expand Down Expand Up @@ -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"));
Expand All @@ -1279,7 +1280,7 @@ export class RunwareBase {

if (hasConnected) {
clearAllIntervals();
resolve(true);
resolve();
return;
}
if (!!this.isInvalidAPIKey()) {
Expand Down
80 changes: 46 additions & 34 deletions Runware/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 boolean> = Value extends true ? { cost: number } : {}

export type IImage<TOutput extends IOutputType = any, HasCost extends boolean = false> = {
taskType: ETaskType;
imageUUID: string;
inputImageUUID?: string;
taskUUID: string;
imageURL?: string;
imageBase64Data?: string;
imageDataURI?: string;
NSFWContent?: boolean;
cost?: number;
seed: number;
}
} & OutputTypeFieldMap[TOutput] & CostField<HasCost>

export interface ITextToImage extends IImage {
export type ITextToImage<TOutput extends IOutputType = any, HasCost extends boolean = false> = {
positivePrompt?: string;
negativePrompt?: string;
}
} & IImage<TOutput> & CostField<HasCost>

export interface IControlNetImage {
taskUUID: string;
inputImageUUID: string;
Expand Down Expand Up @@ -131,8 +149,8 @@ export interface IError {

export type TPromptWeighting = "compel" | "sdEmbeds";

export interface IRequestImage {
outputType?: IOutputType;
export interface IRequestImage<TOutput extends IOutputType = any, HasCost extends boolean = false> {
outputType?: TOutput;
outputFormat?: IOutputFormat;
uploadEndpoint?: string;
checkNSFW?: boolean;
Expand All @@ -156,7 +174,7 @@ export interface IRequestImage {
usePromptWeighting?: boolean;
promptWeighting?: TPromptWeighting;
numberResults?: number; // default to 1
includeCost?: boolean;
includeCost?: HasCost;
outputQuality?: number;

controlNet?: IControlNet[];
Expand Down Expand Up @@ -195,21 +213,20 @@ export interface IRefiner {
startStep?: number;
startStepPercentage?: number;
}
export interface IRequestImageToText {
export interface IRequestImageToText<HasCost extends boolean = false> {
inputImage?: File | string;
includeCost?: boolean;
includeCost?: HasCost;
customTaskUUID?: string;
retry?: number;
}
export interface IImageToText {
export type IImageToText<HasCost extends boolean = false> = {
taskType: ETaskType;
taskUUID: string;
text: string;
cost?: number;
}
} & CostField<HasCost>

export interface IRemoveImageBackground extends IRequestImageToText {
outputType?: IOutputType;
export type IRemoveImageBackground<TOutput extends IOutputType = any, HasCost extends boolean = false> = {
outputType?: TOutput;
outputFormat?: IOutputFormat;
model: string;
settings?: {
Expand All @@ -221,41 +238,36 @@ export interface IRemoveImageBackground extends IRequestImageToText {
alphaMattingBackgroundThreshold?: number;
alphaMattingErodeSize?: number;
};
includeCost?: boolean;
includeCost?: HasCost;
outputQuality?: number;
retry?: number;
}
} & IRequestImageToText<HasCost>

export interface IRemoveImage {
export type IRemoveImage<TOutput extends IOutputType = any, HasCost extends boolean = false> = {
taskType: ETaskType;
taskUUID: string;
imageUUID: string;
inputImageUUID: string;
imageURL?: string;
imageBase64Data?: string;
imageDataURI?: string;
cost?: number;
}
} & OutputTypeFieldMap[TOutput] & CostField<HasCost>

export interface IPromptEnhancer {
export interface IPromptEnhancer<HasCost extends boolean = false> {
promptMaxLength?: number;
promptVersions?: number;
prompt: string;
includeCost?: boolean;
includeCost?: HasCost;
customTaskUUID?: string;
retry?: number;
}

export interface IEnhancedPrompt extends IImageToText {}
export type IEnhancedPrompt<HasCost extends boolean = false> = IImageToText<HasCost>

export interface IUpscaleGan {
export interface IUpscaleGan<TOuput extends IOutputType = any, HasCost extends boolean = false> {
inputImage: File | string;
upscaleFactor: number;
outputType?: IOutputType;
outputType?: TOuput;
outputFormat?: IOutputFormat;
includeCost?: boolean;
includeCost?: HasCost;
outputQuality?: number;

customTaskUUID?: string;
retry?: number;
}
Expand Down Expand Up @@ -355,7 +367,7 @@ export type RequireOnlyOne<T, Keys extends keyof T = keyof T> = Pick<
> &
{
[K in Keys]-?: Required<Pick<T, K>> &
Partial<Record<Exclude<Keys, K>, undefined>>;
Partial<Record<Exclude<Keys, K>, undefined>>;
}[Keys];

export type ListenerType = {
Expand Down