-
Notifications
You must be signed in to change notification settings - Fork 882
/
Plugin.ts
309 lines (234 loc) · 9.69 KB
/
Plugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import { createStore, StoreApi } from "zustand";
/**
* Function used to create a `Plugin`
*
* ```ts
* import * as StableStudio from "@stability/stablestudio-plugin";
*
* // You need to export your plugin like this...
* export const createPlugin = StableStudio.createPlugin(() => ({}));
* ```
*
* It returns a function waiting for `PluginCreateContext` as an argument, which the UI provides
*/
export const createPlugin =
<P extends PluginTypeHelper>(
createPlugin: (options: {
context: PluginCreateContext;
set: StoreApi<Plugin<P>>["setState"];
get: StoreApi<Plugin<P>>["getState"];
}) => Plugin<P>
) =>
(context: PluginCreateContext) =>
createStore<Plugin<P>>((set, get) => createPlugin({ set, get, context }));
/** `PluginCreateContext` is passed to the `createPlugin` function and provides some useful functions */
type PluginCreateContext = {
/** Get the git hash of the repository */
getGitHash: () => string;
/** Get a random prompt for image generation */
getStableDiffusionRandomPrompt: () => string;
};
/** `PluginManifest` is used to describe your plugin on the settings page */
export type PluginManifest = {
/** What is your plugin called? */
name?: string;
/** A link to your plugin's icon */
icon?: URLString;
/** A link to your plugin's website */
link?: URLString;
/** Your plugin version, no versioning schema is currently enforced */
version?: string;
/** What's the license, no licensing schema is currently enforced */
license?: string;
/** Who made this plugin? */
author?: string;
/** What does your plugin do? */
description?: Markdown;
};
export type PluginSettingCommon = {
/** What is your plugin called? */
title?: string;
/** What is your plugin called? */
description?: Markdown;
/** Is this setting required? If so, users will be forced to provide an input */
required?: boolean;
};
/** `PluginSetting` allows for `string`, `number`, and `boolean` input types */
export type PluginSetting<T> =
| PluginSettingString<T>
| PluginSettingNumber<T>
| PluginSettingBoolean<T>;
/** `PluginSetting` which is displayed and input as a `string` */
export type PluginSettingString<T = string> = PluginSettingCommon & {
type: "string";
/** The current `value` */
value?: T;
/** If you provide `options`, this will become a dropdown */
options?: { label: string; value: T }[];
/** The `placeholder` input `string` */
placeholder?: string;
/** Given a `value`, how should it be displayed? Useful if your value is a `Date` for example */
formatter?: (value: T) => string;
/** Given a `string`, how should it be saved as a `value`? */
parser?: (string: string) => T;
/** Determines whether this input is displayed as a `password` type */
password?: boolean;
};
/** `PluginSetting` which is displayed and input as a `number` */
export type PluginSettingNumber<T = number> = PluginSettingCommon & {
type: "number";
/** The current `value` */
value?: T;
/** The `placeholder` input `number` */
placeholder?: number;
/** Enforces an inclusive `min` allowed `value` */
min?: number;
/** Enforces an inclusive `max` allowed `value` */
max?: number;
/** Enforces a `step` size between each `value` */
step?: number;
/** Given a `value`, how can it formatted as a `number`? */
formatter?: (value: T) => number;
/** Given a `number`, how should it be saved as a `value`? */
parser?: (value: number) => T;
/** Determines whether to use a slider or input. Uses `input` by default */
variant?: "slider" | "input";
};
/** `PluginSetting` which is displayed and input as a `boolean` */
export type PluginSettingBoolean<T = boolean> = PluginSettingCommon & {
type: "boolean";
/** The current `value` */
value?: T;
};
/** `PluginStatus` is displayed via an indicator on the `/settings` page */
export type PluginStatus = {
/** Determines the text displayed next to the `indicator` icon */
text?: string;
/** Determines the `indicator` color and icon */
indicator?: "success" | "error" | "warning" | "info" | "loading";
};
/** `PluginSettings` is a map of the settings your plugin will show on the `/settings` page */
export type PluginSettings = {
/** Each setting needs a unique `key` */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[settingKey: string]: PluginSetting<any>;
};
/** Useful for strictly-typing things */
export type PluginTypeHelper = { settings?: PluginSettings };
export type PluginTypeHelperDefault = { settings: undefined };
/** The `Plugin` type defines the API which StableStudio uses to provide functionality via plugins */
export type Plugin<P extends PluginTypeHelper = PluginTypeHelperDefault> = {
/** You should provide a `PluginManifest`, otherwise the UI shows a warning */
manifest?: PluginManifest;
/** The most fundamental function needed for stable diffusion generations */
createStableDiffusionImages?: (options?: {
/** The `StableDiffusionInput` you've been asked to generate, if empty, you could still return a random image */
input?: StableDiffusionInput;
/** Determines how many images will be created using the given `StableDiffusionInput` */
count?: number;
}) => MaybePromise<StableDiffusionImages | undefined>;
/** This function provides historical access to generated images. If omitted, the app will lose its history on refresh */
getStableDiffusionExistingImages?: (options?: {
/** How many images the UI is requesting */
limit?: number;
/** The `ID` of last image the UI has access to, it should be omitted from the response */
exclusiveStartImageID?: ID;
}) => MaybePromise<StableDiffusionImages[] | undefined>;
/** If more than one `StableDiffusionModel`, you can return them via this function and they will be presented as a dropdown in the UI */
getStableDiffusionModels?: () => MaybePromise<
StableDiffusionModel[] | undefined
>;
/** If more than one `StableDiffusionSampler`, you can return them via this function and they will be presented as a dropdown in the UI */
getStableDiffusionSamplers?: () => MaybePromise<
StableDiffusionSampler[] | undefined
>;
/** If more than one `StableDiffusionStyle`, you can return them via this function and they will be presented as a dropdown in the UI */
getStableDiffusionStyles?: () => MaybePromise<
StableDiffusionStyle[] | undefined
>;
/** Determines the default count passed to `createStableDiffusionImages` */
getStableDiffusionDefaultCount?: () => number | undefined;
/** Determines the default input passed to `createStableDiffusionImages` and is also used when setting up a new generation */
getStableDiffusionDefaultInput?: () => StableDiffusionInput | undefined;
/** If you support deleting existing images by `ID`, this function will enable a deletion UI */
deleteStableDiffusionImages?: (options?: {
imageIDs?: ID[];
}) => MaybePromise<void>;
/** Determines whether to display a `PluginStatus` and sets its contents */
getStatus?: () => MaybePromise<PluginStatus | undefined>;
/** If your plugin has `settings` you want to make available on the `/settings page, you can declare them here` */
settings?: P["settings"];
/** Handles changes in `settings`, you'll need to actually use Zustand's `set` to persist the change */
setSetting?: (
settingKey: keyof P["settings"],
value: string | number | boolean
) => void;
} & P;
export type StableDiffusionInput = {
prompts?: StableDiffusionPrompt[];
/** If `getStableDiffusionModels` is available and a `StableDiffusionModel` is selected, this is its `ID` */
model?: ID;
/** If `getStableDiffusionStyles` is available and a `StableDiffusionStyle` is selected, this is its `ID` */
style?: ID;
width?: number;
height?: number;
sampler?: StableDiffusionSampler;
cfgScale?: number;
steps?: number;
seed?: number;
/** `StableDiffusionInputImage` in the form of a black and white mask used for in-painting and out-painting */
maskImage?: StableDiffusionInputImage;
/** `StableDiffusionInputImage` which is used for image-to-image generations such as creating variations */
initialImage?: StableDiffusionInputImage;
};
export type StableDiffusionPrompt = {
text?: string;
/** Value between `-1` and `1` */
weight?: number;
};
/** This controls how a `StableDiffusionModel` is displayed in the UI */
export type StableDiffusionModel = {
id: ID;
name?: string;
description?: string;
image?: URLString;
};
/** This controls how a `StableDiffusionStyle` is displayed in the UI */
export type StableDiffusionStyle = {
id: ID;
name?: string;
description?: string;
image?: URLString;
};
/** This controls how a `StableDiffusionSampler` is displayed in the UI */
export declare type StableDiffusionSampler = {
id: ID;
name?: string;
};
export type StableDiffusionInputImage = {
blob?: Blob;
/** Value between `0` and `1` */
weight?: number;
};
/** Since multiple images can be generated at once, this is how they are grouped */
export type StableDiffusionImages = {
id: ID;
/** If `exclusiveStartImageID` is set, this means more images exist for this group and the `ID` is passed to `getStableDiffusionExistingImages` */
exclusiveStartImageID?: ID;
images?: StableDiffusionImage[];
};
export type StableDiffusionImage = {
id: ID;
createdAt?: Date;
/** The `StableDiffusionInput` used to create this image */
input?: StableDiffusionInput;
blob?: Blob;
};
/** Allows both asynchronous and synchronous return types */
type MaybePromise<T> = T | Promise<T>;
/** In this context, an `ID` represents a unique-within-this-plugin `ID`, no schema is enforced */
export type ID = string;
/** A valid URL */
export type URLString = string;
/** Markdown string which is rendered as markdown in the UI */
export type Markdown = string;