1
import { TriggerClient, eventTrigger } from "@trigger.dev/sdk";
2
import { z } from "zod";
5
const engineId = "stable-diffusion-xl-1024-v1-0";
6
const apiHost = process.env.API_HOST ?? "https://api.stability.ai";
7
const apiKey = process.env.STABILITY_API_KEY;
9
interface GenerationResponse {
18
id: "stability-ai-text-to-image",
19
name: "Stability AI Text to Image",
21
trigger: eventTrigger({
22
name: "stability.text.to.image",
40
text_prompts: z.array(
43
weight: z.number().optional(),
46
cfg_scale: z.number().optional(),
47
width: z.number().optional(),
48
height: z.number().optional(),
49
steps: z.number().optional(),
50
samples: z.number().optional(),
53
run: async (payload, io, ctx) => {
56
const response = (await io.backgroundFetch(
57
"create-image-from-text",
58
`${apiHost}/v1/generation/${engineId}/text-to-image`,
62
"Content-Type": "application/json",
63
Accept: "application/json",
64
Authorization: `Bearer ${apiKey}`,
66
body: JSON.stringify({
67
text_prompts: payload.text_prompts,
68
cfg_scale: payload.cfg_scale ?? 7,
69
height: payload.height ?? 1024,
70
width: payload.width ?? 1024,
71
steps: payload.steps ?? 50,
72
samples: payload.samples ?? 1,
80
maxTimeoutInMs: 60000,
85
)) as GenerationResponse;
90
for (const [index, artifact] of response.artifacts.entries()) {
92
`Image ${index + 1}/${payload.samples ?? 1}`,
94
const imageUrl = `data:image/png;base64,${artifact.base64}`;