1
import { TriggerClient, eventTrigger } from "@trigger.dev/sdk";
2
import { z } from "zod";
6
const engineId = "stable-diffusion-xl-1024-v1-0";
7
const apiHost = process.env.API_HOST ?? "https://api.stability.ai";
8
const apiKey = process.env.STABILITY_API_KEY;
10
interface GenerationResponse {
19
id: "stability-ai-text-to-image",
20
name: "Stability AI Text to Image",
22
trigger: eventTrigger({
23
name: "stability.text.to.image",
41
text_prompts: z.array(
44
weight: z.number().optional(),
47
cfg_scale: z.number().optional(),
48
width: z.number().optional(),
49
height: z.number().optional(),
50
steps: z.number().optional(),
51
samples: z.number().optional(),
54
run: async (payload, io, ctx) => {
57
const response = (await io.backgroundFetch(
58
"create-image-from-text",
59
`${apiHost}/v1/generation/${engineId}/text-to-image`,
63
"Content-Type": "application/json",
64
Accept: "application/json",
65
Authorization: `Bearer ${apiKey}`,
67
body: JSON.stringify({
68
text_prompts: payload.text_prompts,
69
cfg_scale: payload.cfg_scale ?? 7,
70
height: payload.height ?? 1024,
71
width: payload.width ?? 1024,
72
steps: payload.steps ?? 50,
73
samples: payload.samples ?? 1,
81
maxTimeoutInMs: 60000,
86
)) as GenerationResponse;
91
for (const [index, artifact] of response.artifacts.entries()) {
93
`Image ${index + 1}/${payload.samples ?? 1}`,
95
const imageUrl = `data:image/png;base64,${artifact.base64}`;