-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
355 lines (300 loc) · 10.4 KB
/
script.js
File metadata and controls
355 lines (300 loc) · 10.4 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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
const output = document.getElementById("output");
const imgEl = document.getElementById("dishImage");
const cookBtn = document.getElementById("cook");
const worseBtn = document.getElementById("worse");
const loadingOverlay = document.getElementById("loadingOverlay");
const loadingText = document.getElementById("loadingText");
//Worker URL - replace with your own if self-hosting or using a different service
const WORKER_URL = "https://cooked-serverside.wangz9096z.workers.dev";
let lastDish = "";
const MIN_RECIPE_STEPS = 8;
const MAX_RECIPE_CONTINUATIONS = 2;
const BASE_PROMPT = `
Invent a completely original fictional dish.
Real ingredients only.
Fake but edible-sounding components.
Cursed but bizarre name.
Write a complete full-length recipe, not a short summary.
Use this exact structure:
Dish Name
Short pitch
Servings + prep time + cook time
Ingredients list (one per line)
Step-by-step method (at least 8 clear steps)
Fake nutrition stats
Chef warning
Explain preparation and fake stats in detail.
Unhinged but coherent.
Output plain text only.
No markdown or code blocks.
Make it sound as appetizing as possible while being very wrong and inedible.
Also make it like an actually possible recipe that someone could attempt if they were very reckless and didn't care about the consequences.
`;
//Halo
async function generateDish(prompt) {
let recipe = "";
let nextPrompt = prompt;
for (let attempt = 0; attempt <= MAX_RECIPE_CONTINUATIONS; attempt++) {
const { text, finishReason } = await requestDishCompletion(nextPrompt);
recipe = mergeRecipeText(recipe, text);
const needsContinuation = finishReason === "length" || isRecipeLikelyIncomplete(recipe);
if (!needsContinuation) {
return recipe;
}
if (attempt === MAX_RECIPE_CONTINUATIONS) {
return recipe;
}
nextPrompt = buildContinuationPrompt(recipe);
}
return recipe;
}
async function requestDishCompletion(prompt) {
const res = await fetch(WORKER_URL, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
model: "qwen/qwen3-32b",
messages: [
{ role: "system", content: "You are a chaotic experimental chef AI." },
{ role: "user", content: prompt }
],
temperature: 1.1,
max_tokens: 1100
})
});
const data = await readJsonResponse(res);
if (!res.ok) {
throw new Error(data?.error?.message || data?.error || `Request failed (${res.status})`);
}
const content = extractModelText(data);
if (!content) {
throw new Error("No recipe text returned by model");
}
return {
text: toPlainText(content),
finishReason: data?.choices?.[0]?.finish_reason || data?.finish_reason || ""
};
}
function buildContinuationPrompt(currentRecipe) {
return [
"Continue this exact recipe from where it stops.",
"Only output the missing remainder.",
"Do not restart or repeat previous sections.",
"If the steps are incomplete, continue from the next step number.",
"Output plain text only.",
"",
currentRecipe
].join("\n");
}
function isRecipeLikelyIncomplete(text) {
const cleaned = String(text || "").trim();
if (!cleaned) return true;
const stepCount = countRecipeSteps(cleaned);
if (stepCount < MIN_RECIPE_STEPS) return true;
// Heuristic for obvious truncation at the tail.
return /[:,;\-]$/.test(cleaned);
}
function countRecipeSteps(text) {
const matches = String(text).match(/^\s*(?:step\s*)?\d{1,2}[).:-]\s+/gim);
return matches ? matches.length : 0;
}
function mergeRecipeText(existing, addition) {
const left = String(existing || "").trim();
const right = String(addition || "").trim();
if (!left) return right;
if (!right) return left;
if (left.includes(right)) return left;
if (right.includes(left)) return right;
const leftLines = left.split(/\r?\n/).map((line) => line.trim()).filter(Boolean);
const rightLines = right.split(/\r?\n/).map((line) => line.trim()).filter(Boolean);
const maxOverlap = Math.min(12, leftLines.length, rightLines.length);
let overlap = 0;
for (let size = maxOverlap; size >= 1; size--) {
const leftTail = leftLines.slice(-size).join("\n");
const rightHead = rightLines.slice(0, size).join("\n");
if (leftTail === rightHead) {
overlap = size;
break;
}
}
const uniqueRight = overlap > 0 ? rightLines.slice(overlap).join("\n") : right;
return uniqueRight ? `${left}\n${uniqueRight}` : left;
}
function extractModelText(data) {
const choice = data?.choices?.[0];
// Chat-completions style: message.content can be a string or an array of parts.
const fromMessage = flattenText(choice?.message?.content);
if (fromMessage) return fromMessage;
// Some providers place text directly on the message.
const fromMessageText = flattenText(choice?.message?.text || choice?.message?.output_text);
if (fromMessageText) return fromMessageText;
// Text-completions style.
const fromChoiceText = flattenText(choice?.text || choice?.content);
if (fromChoiceText) return fromChoiceText;
// Responses API style.
const fromOutputText = flattenText(data?.output_text);
if (fromOutputText) return fromOutputText;
// Some APIs return nested output/content arrays with text fields.
const fromOutput = flattenText(data?.output);
if (fromOutput) return fromOutput;
// Gemini-style candidates payload.
const fromCandidates = flattenText(data?.candidates);
if (fromCandidates) return fromCandidates;
return "";
}
function flattenText(value) {
if (!value) return "";
if (typeof value === "string") return value.trim();
if (Array.isArray(value)) {
const text = value
.map((item) => flattenText(item))
.filter(Boolean)
.join("\n")
.trim();
return text;
}
if (typeof value === "object") {
const direct = [value.text, value.content, value.output_text, value.value]
.map((item) => flattenText(item))
.find(Boolean);
if (direct) return direct;
const nested = [value.message, value.delta, value.output, value.parts, value.candidates]
.map((item) => flattenText(item))
.find(Boolean);
if (nested) return nested;
}
return "";
}
async function generateImage(description) {
const res = await fetch(WORKER_URL + "/image", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
model: "google/gemini-2.5-flash-image",
prompt: `A cursed fictional food dish. Looks edible but wrong.
Dark humor.
Description: ${description}`,
size: "512x512"
})
});
const data = await readJsonResponse(res);
if (!res.ok) {
throw new Error(data?.error?.message || data?.error || `Image request failed (${res.status})`);
}
if (data.error) throw new Error(data.error.message || data.error);
const imageSrc = extractImageSrc(data);
if (!imageSrc) throw new Error("No image returned");
imgEl.src = imageSrc;
imgEl.style.display = "block";
}
function extractImageSrc(data) {
const candidates = [
data?.data?.[0]?.b64_json,
data?.data?.[0]?.base64,
data?.images?.[0]?.b64_json,
data?.images?.[0]?.base64,
data?.image?.b64_json,
data?.image?.base64,
data?.output?.[0]?.b64_json,
data?.output?.[0]?.base64,
data?.output?.[0]?.image?.b64_json,
data?.output?.[0]?.image?.base64,
data?.output?.[0]?.content?.[0]?.image_base64,
data?.candidates?.[0]?.content?.parts?.[0]?.inlineData?.data,
data?.data?.[0]?.url,
data?.images?.[0]?.url,
data?.image?.url,
data?.output?.[0]?.url
];
for (const value of candidates) {
const src = normalizeImageValue(value);
if (src) return src;
}
return "";
}
function normalizeImageValue(value) {
if (!value || typeof value !== "string") return "";
const trimmed = value.trim();
if (!trimmed) return "";
if (/^data:image\//i.test(trimmed)) return trimmed;
if (/^https?:\/\//i.test(trimmed)) return trimmed;
// Assume plain base64 payload if it's not a URL/data URI.
return "data:image/png;base64," + trimmed;
}
async function readJsonResponse(res) {
const text = await res.text();
try {
return text ? JSON.parse(text) : {};
} catch {
throw new Error(`Invalid server response: ${text.slice(0, 160)}`);
}
}
function formatError(err) {
if (!err) return "Unknown error";
if (typeof err === "string") return err;
if (err instanceof Error) return err.message;
if (typeof err === "object") {
return err.message || err.error || JSON.stringify(err, null, 2);
}
return String(err);
}
function toPlainText(text) {
return String(text)
// Keep fenced content but strip fence marker lines.
.replace(/^```[\w-]*\s*$/gm, "")
.replace(/^```\s*$/gm, "")
.replace(/\*\*(.*?)\*\*/g, "$1")
.replace(/__(.*?)__/g, "$1")
.replace(/`([^`]+)`/g, "$1")
.trim();
}
function setLoadingState(isLoading, message) {
loadingOverlay.style.display = isLoading ? "flex" : "none";
loadingOverlay.setAttribute("aria-hidden", isLoading ? "false" : "true");
if (isLoading && message) {
loadingText.textContent = message;
}
cookBtn.disabled = isLoading;
worseBtn.disabled = isLoading;
}
cookBtn.onclick = async () => {
setLoadingState(true, "Cooking something illegal...");
output.textContent = "Cooking something illegal...";
imgEl.style.display = "none";
try {
lastDish = await generateDish(BASE_PROMPT);
output.textContent = lastDish;
try {
await generateImage(lastDish);
} catch (imageError) {
// Keep recipe text visible even if image generation fails.
output.textContent = `${lastDish}\n\n[Image failed: ${formatError(imageError)}]`;
}
} catch (e) {
output.textContent = "Error: " + formatError(e);
} finally {
setLoadingState(false);
}
};
worseBtn.onclick = async () => {
if (!lastDish) return;
setLoadingState(true, "Making it worse...");
output.textContent = "Making it worse...";
imgEl.style.display = "none";
try {
lastDish = await generateDish(
"Make this dish worse, more cursed, and less edible. Keep it a complete full recipe with ingredients and at least 8 steps. Output plain text only.\n\n" + lastDish
);
output.textContent = lastDish;
try {
await generateImage(lastDish);
} catch (imageError) {
// Keep recipe text visible even if image generation fails.
output.textContent = `${lastDish}\n\n[Image failed: ${formatError(imageError)}]`;
}
} catch (e) {
output.textContent = "Error: " + formatError(e);
} finally {
setLoadingState(false);
}
};