Skip to content

Commit dc3fdeb

Browse files
committed
feat(images): add local Stable Diffusion provider with A1111 and ComfyUI support
1 parent 9724377 commit dc3fdeb

6 files changed

Lines changed: 923 additions & 2 deletions

File tree

src/api/model.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const ENV_URL_MAP: Record<string, string> = {
5151
stability: 'STABILITY_BASE_URL',
5252
replicate: 'REPLICATE_BASE_URL',
5353
ollama: 'OLLAMA_BASE_URL',
54+
'stable-diffusion-local': 'STABLE_DIFFUSION_LOCAL_BASE_URL',
5455
};
5556

5657
/**
@@ -160,6 +161,15 @@ export function resolveMediaProvider(
160161
return { providerId, modelId, baseUrl };
161162
}
162163

164+
if (providerId === 'stable-diffusion-local') {
165+
if (!baseUrl) {
166+
throw new Error(
167+
`No base URL for stable-diffusion-local. Set STABLE_DIFFUSION_LOCAL_BASE_URL or pass baseUrl.`,
168+
);
169+
}
170+
return { providerId, modelId, baseUrl };
171+
}
172+
163173
const envVar = ENV_KEY_MAP[providerId];
164174
if (envVar && !apiKey) {
165175
throw new Error(`No API key for ${providerId}. Set ${envVar} or pass apiKey.`);

src/api/provider-defaults.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ export const PROVIDER_DEFAULTS: Record<string, ProviderDefaults> = {
5959
replicate: {
6060
image: 'black-forest-labs/flux-1.1-pro',
6161
},
62+
'stable-diffusion-local': {
63+
image: 'v1-5-pruned-emaonly',
64+
},
6265
};
6366

6467
/** Env var keys checked for auto-detection, in priority order */
@@ -70,6 +73,7 @@ const AUTO_DETECT_ORDER: Array<{ envKey: string; provider: string }> = [
7073
{ envKey: 'OLLAMA_BASE_URL', provider: 'ollama' },
7174
{ envKey: 'STABILITY_API_KEY', provider: 'stability' },
7275
{ envKey: 'REPLICATE_API_TOKEN', provider: 'replicate' },
76+
{ envKey: 'STABLE_DIFFUSION_LOCAL_BASE_URL', provider: 'stable-diffusion-local' },
7377
];
7478

7579
/**

src/core/images/IImageProvider.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export type ImageProviderId = 'openai' | 'openrouter' | 'stability' | 'replicate' | (string & {});
1+
export type ImageProviderId = 'openai' | 'openrouter' | 'stability' | 'replicate' | 'stable-diffusion-local' | (string & {});
22
export type ImageModality = 'image' | 'text';
33
export type ImageBackground = 'transparent' | 'opaque' | 'auto';
44
export type ImageOutputFormat = 'png' | 'jpeg' | 'jpg' | 'webp';
@@ -72,11 +72,39 @@ export interface ReplicateImageProviderOptions {
7272
extraBody?: Record<string, unknown>;
7373
}
7474

75+
export interface StableDiffusionLocalImageProviderOptions {
76+
/** Number of inference steps (default 25). */
77+
steps?: number;
78+
/** Classifier-free guidance scale (default 7.5). */
79+
cfgScale?: number;
80+
/** Random seed (-1 for random). */
81+
seed?: number;
82+
/** Sampler name (e.g. 'Euler a', 'DPM++ 2M Karras'). */
83+
sampler?: string;
84+
/** Negative prompt. */
85+
negativePrompt?: string;
86+
/** Image width in pixels (default 512). */
87+
width?: number;
88+
/** Image height in pixels (default 512). */
89+
height?: number;
90+
/** Number of images to generate (default 1). */
91+
batchSize?: number;
92+
/** ControlNet settings forwarded verbatim to the backend. */
93+
controlnet?: Record<string, unknown>;
94+
/** LoRA models to apply. Injected into the prompt as `<lora:name:weight>`. */
95+
loras?: Array<{ name: string; weight?: number }>;
96+
/** Enable high-resolution fix (A1111 only). */
97+
hrFix?: boolean;
98+
/** Denoising strength for high-res fix or img2img (default 0.7). */
99+
denoisingStrength?: number;
100+
}
101+
75102
export interface ImageProviderOptionBag {
76103
openai?: OpenAIImageProviderOptions;
77104
openrouter?: OpenRouterImageProviderOptions;
78105
stability?: StabilityImageProviderOptions;
79106
replicate?: ReplicateImageProviderOptions;
107+
'stable-diffusion-local'?: StableDiffusionLocalImageProviderOptions;
80108
[providerId: string]: unknown;
81109
}
82110

@@ -118,7 +146,7 @@ export interface IImageProvider {
118146
shutdown?(): Promise<void>;
119147
}
120148

121-
const BUILT_IN_IMAGE_PROVIDER_IDS = new Set(['openai', 'openrouter', 'stability', 'replicate']);
149+
const BUILT_IN_IMAGE_PROVIDER_IDS = new Set(['openai', 'openrouter', 'stability', 'replicate', 'stable-diffusion-local']);
122150

123151
export function getImageProviderOptions<T extends object>(
124152
providerId: string,

0 commit comments

Comments
 (0)