-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimplementation.js
More file actions
72 lines (61 loc) · 1.78 KB
/
implementation.js
File metadata and controls
72 lines (61 loc) · 1.78 KB
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
function escapeMarkdownAlt(text) {
if (!text) return "";
return text
.replace(/\\/g, "\\\\")
.replace(/\n/g, " ")
.replace(/\[/g, "\\[")
.replace(/\]/g, "\\]")
.replace(/</g, "<")
.replace(/>/g, ">");
}
async function flux_image_generator(params, userSettings) {
const { prompt, negative_prompt = "" } = params;
const falKey = userSettings.falKey;
const model = userSettings.model || "flux-dev";
const width = userSettings.width || 1024;
const height = userSettings.height || 1024;
const steps = userSettings.steps || 28;
const cfg = userSettings.cfg || 7;
if (!falKey) {
throw new Error("Missing FAL API key. Add it in plugin settings.");
}
const endpoints = {
"flux-dev": "https://fal.run/fal-ai/flux-dev",
"flux-pro": "https://fal.run/fal-ai/flux-pro",
"flux-schnell": "https://fal.run/fal-ai/flux-schnell"
};
const endpoint = endpoints[model];
if (!endpoint) {
throw new Error(`Unknown model: ${model}`);
}
const payload = {
prompt,
negative_prompt,
width,
height,
steps,
guidance_scale: cfg
};
const response = await fetch(endpoint, {
method: "POST",
headers: {
Authorization: `Key ${falKey}`,
"Content-Type": "application/json"
},
body: JSON.stringify(payload)
});
if (response.status === 401) {
throw new Error("Invalid FAL API key.");
}
if (!response.ok) {
const errorText = await response.text();
throw new Error(errorText);
}
const data = await response.json();
if (!data.images || data.images.length === 0) {
throw new Error("No image returned by the Flux API.");
}
const url = data.images[0].url;
const alt = escapeMarkdownAlt(prompt);
return `\n\n_Note: Save the image within 1 hour, or it will expire._`;
}