ima2-gen 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +2 -0
- package/README.md +77 -0
- package/bin/ima2.js +134 -0
- package/package.json +41 -0
- package/public/index.html +1013 -0
- package/server.js +348 -0
|
@@ -0,0 +1,1013 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
+
<title>Image Gen</title>
|
|
7
|
+
<link rel="preconnect" href="https://fonts.googleapis.com">
|
|
8
|
+
<link href="https://fonts.googleapis.com/css2?family=Geist+Mono:wght@400;500;600&family=Outfit:wght@300;400;500;600;700&display=swap" rel="stylesheet">
|
|
9
|
+
<style>
|
|
10
|
+
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
|
|
11
|
+
|
|
12
|
+
:root {
|
|
13
|
+
--bg: #0a0a0a;
|
|
14
|
+
--surface: #141414;
|
|
15
|
+
--surface-2: #1c1c1c;
|
|
16
|
+
--border: #2a2a2a;
|
|
17
|
+
--text: #e8e8e8;
|
|
18
|
+
--text-dim: #888;
|
|
19
|
+
--accent: #f0f0f0;
|
|
20
|
+
--accent-bright: #fff;
|
|
21
|
+
--green: #22c55e;
|
|
22
|
+
--amber: #f59e0b;
|
|
23
|
+
--red: #ef4444;
|
|
24
|
+
--radius: 10px;
|
|
25
|
+
--font: 'Outfit', sans-serif;
|
|
26
|
+
--mono: 'Geist Mono', monospace;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
body {
|
|
30
|
+
font-family: var(--font);
|
|
31
|
+
background: var(--bg);
|
|
32
|
+
color: var(--text);
|
|
33
|
+
min-height: 100dvh;
|
|
34
|
+
-webkit-font-smoothing: antialiased;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
.app {
|
|
38
|
+
display: grid;
|
|
39
|
+
grid-template-columns: 380px 1fr;
|
|
40
|
+
min-height: 100dvh;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/* ── Sidebar ── */
|
|
44
|
+
.sidebar {
|
|
45
|
+
background: var(--surface);
|
|
46
|
+
border-right: 1px solid var(--border);
|
|
47
|
+
padding: 28px 24px;
|
|
48
|
+
display: flex;
|
|
49
|
+
flex-direction: column;
|
|
50
|
+
gap: 20px;
|
|
51
|
+
overflow-y: auto;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
.logo {
|
|
55
|
+
font-size: 20px;
|
|
56
|
+
font-weight: 600;
|
|
57
|
+
letter-spacing: -0.5px;
|
|
58
|
+
display: flex;
|
|
59
|
+
align-items: center;
|
|
60
|
+
gap: 10px;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
.logo-dot {
|
|
64
|
+
width: 8px; height: 8px;
|
|
65
|
+
background: var(--green);
|
|
66
|
+
border-radius: 50%;
|
|
67
|
+
animation: pulse 2s ease-in-out infinite;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
@keyframes pulse {
|
|
71
|
+
0%, 100% { opacity: 1; }
|
|
72
|
+
50% { opacity: 0.4; }
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
.billing-bar {
|
|
76
|
+
background: var(--surface-2);
|
|
77
|
+
border: 1px solid var(--border);
|
|
78
|
+
border-radius: var(--radius);
|
|
79
|
+
padding: 14px 16px;
|
|
80
|
+
font-family: var(--mono);
|
|
81
|
+
font-size: 12px;
|
|
82
|
+
display: flex;
|
|
83
|
+
flex-direction: column;
|
|
84
|
+
gap: 6px;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
.billing-bar .label { color: var(--text-dim); font-size: 11px; text-transform: uppercase; letter-spacing: 0.5px; }
|
|
88
|
+
.billing-bar .value { font-size: 16px; font-weight: 600; }
|
|
89
|
+
|
|
90
|
+
.section-title {
|
|
91
|
+
font-size: 11px;
|
|
92
|
+
font-weight: 500;
|
|
93
|
+
text-transform: uppercase;
|
|
94
|
+
letter-spacing: 1px;
|
|
95
|
+
color: var(--text-dim);
|
|
96
|
+
margin-top: 4px;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/* ── Form controls ── */
|
|
100
|
+
.prompt-area {
|
|
101
|
+
width: 100%;
|
|
102
|
+
min-height: 100px;
|
|
103
|
+
max-height: 200px;
|
|
104
|
+
resize: vertical;
|
|
105
|
+
background: var(--bg);
|
|
106
|
+
border: 1px solid var(--border);
|
|
107
|
+
border-radius: var(--radius);
|
|
108
|
+
color: var(--text);
|
|
109
|
+
font-family: var(--font);
|
|
110
|
+
font-size: 14px;
|
|
111
|
+
padding: 14px;
|
|
112
|
+
outline: none;
|
|
113
|
+
transition: border-color 0.2s;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
.prompt-area:focus { border-color: #444; }
|
|
117
|
+
.prompt-area::placeholder { color: #555; }
|
|
118
|
+
|
|
119
|
+
.option-group {
|
|
120
|
+
display: flex;
|
|
121
|
+
flex-direction: column;
|
|
122
|
+
gap: 8px;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
.option-row {
|
|
126
|
+
display: flex;
|
|
127
|
+
gap: 6px;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
.option-btn {
|
|
131
|
+
flex: 1;
|
|
132
|
+
padding: 9px 6px;
|
|
133
|
+
background: var(--bg);
|
|
134
|
+
border: 1px solid var(--border);
|
|
135
|
+
border-radius: 8px;
|
|
136
|
+
color: var(--text-dim);
|
|
137
|
+
font-family: var(--mono);
|
|
138
|
+
font-size: 12px;
|
|
139
|
+
cursor: pointer;
|
|
140
|
+
transition: all 0.15s;
|
|
141
|
+
text-align: center;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
.option-btn:hover { border-color: #444; color: var(--text); }
|
|
145
|
+
.option-btn.active {
|
|
146
|
+
border-color: var(--accent);
|
|
147
|
+
color: var(--accent-bright);
|
|
148
|
+
background: var(--surface-2);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
.cost-estimate {
|
|
152
|
+
font-family: var(--mono);
|
|
153
|
+
font-size: 12px;
|
|
154
|
+
color: var(--text-dim);
|
|
155
|
+
padding: 8px 0;
|
|
156
|
+
display: flex;
|
|
157
|
+
justify-content: space-between;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
.cost-estimate .price { color: var(--green); font-weight: 500; }
|
|
161
|
+
|
|
162
|
+
.generate-btn {
|
|
163
|
+
width: 100%;
|
|
164
|
+
padding: 14px;
|
|
165
|
+
background: var(--accent-bright);
|
|
166
|
+
color: var(--bg);
|
|
167
|
+
border: none;
|
|
168
|
+
border-radius: var(--radius);
|
|
169
|
+
font-family: var(--font);
|
|
170
|
+
font-size: 15px;
|
|
171
|
+
font-weight: 600;
|
|
172
|
+
cursor: pointer;
|
|
173
|
+
transition: all 0.15s;
|
|
174
|
+
letter-spacing: -0.3px;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
.generate-btn:hover { opacity: 0.9; transform: translateY(-1px); }
|
|
178
|
+
.generate-btn:active { transform: translateY(0); }
|
|
179
|
+
.generate-btn:disabled {
|
|
180
|
+
opacity: 0.3;
|
|
181
|
+
cursor: not-allowed;
|
|
182
|
+
transform: none;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
.generate-btn.loading {
|
|
186
|
+
background: var(--surface-2);
|
|
187
|
+
color: var(--text-dim);
|
|
188
|
+
border: 1px solid var(--border);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
.format-row {
|
|
192
|
+
display: flex;
|
|
193
|
+
gap: 6px;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
select.format-select {
|
|
197
|
+
flex: 1;
|
|
198
|
+
padding: 9px 12px;
|
|
199
|
+
background: var(--bg);
|
|
200
|
+
border: 1px solid var(--border);
|
|
201
|
+
border-radius: 8px;
|
|
202
|
+
color: var(--text);
|
|
203
|
+
font-family: var(--mono);
|
|
204
|
+
font-size: 12px;
|
|
205
|
+
cursor: pointer;
|
|
206
|
+
outline: none;
|
|
207
|
+
appearance: none;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/* ── Canvas area ── */
|
|
211
|
+
.canvas {
|
|
212
|
+
display: flex;
|
|
213
|
+
align-items: center;
|
|
214
|
+
justify-content: center;
|
|
215
|
+
padding: 40px;
|
|
216
|
+
position: relative;
|
|
217
|
+
overflow: auto;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
.canvas-empty {
|
|
221
|
+
text-align: center;
|
|
222
|
+
color: #333;
|
|
223
|
+
font-size: 48px;
|
|
224
|
+
font-weight: 700;
|
|
225
|
+
letter-spacing: -2px;
|
|
226
|
+
user-select: none;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
.canvas-empty span { display: block; font-size: 14px; font-weight: 400; color: #333; letter-spacing: 0; margin-top: 12px; }
|
|
230
|
+
|
|
231
|
+
.result-container {
|
|
232
|
+
display: none;
|
|
233
|
+
flex-direction: column;
|
|
234
|
+
align-items: center;
|
|
235
|
+
gap: 16px;
|
|
236
|
+
max-width: 100%;
|
|
237
|
+
max-height: 100%;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
.result-container.visible { display: flex; }
|
|
241
|
+
|
|
242
|
+
.result-img {
|
|
243
|
+
max-width: 100%;
|
|
244
|
+
max-height: calc(100dvh - 160px);
|
|
245
|
+
border-radius: 12px;
|
|
246
|
+
box-shadow: 0 20px 60px rgba(0,0,0,0.5);
|
|
247
|
+
object-fit: contain;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
.result-prompt {
|
|
251
|
+
max-width: 600px;
|
|
252
|
+
padding: 10px 16px;
|
|
253
|
+
background: var(--surface);
|
|
254
|
+
border: 1px solid var(--border);
|
|
255
|
+
border-radius: 8px;
|
|
256
|
+
font-family: var(--mono);
|
|
257
|
+
font-size: 12px;
|
|
258
|
+
color: var(--text-dim);
|
|
259
|
+
line-height: 1.5;
|
|
260
|
+
cursor: pointer;
|
|
261
|
+
transition: all 0.15s;
|
|
262
|
+
word-break: break-word;
|
|
263
|
+
text-align: center;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
.result-prompt:hover { border-color: #444; color: var(--text); }
|
|
267
|
+
|
|
268
|
+
.result-meta {
|
|
269
|
+
font-family: var(--mono);
|
|
270
|
+
font-size: 12px;
|
|
271
|
+
color: var(--text-dim);
|
|
272
|
+
display: flex;
|
|
273
|
+
gap: 20px;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
.result-actions {
|
|
277
|
+
display: flex;
|
|
278
|
+
gap: 8px;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
.action-btn {
|
|
282
|
+
padding: 8px 16px;
|
|
283
|
+
background: var(--surface);
|
|
284
|
+
border: 1px solid var(--border);
|
|
285
|
+
border-radius: 8px;
|
|
286
|
+
color: var(--text);
|
|
287
|
+
font-family: var(--mono);
|
|
288
|
+
font-size: 12px;
|
|
289
|
+
cursor: pointer;
|
|
290
|
+
transition: all 0.15s;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
.action-btn:hover { border-color: #444; background: var(--surface-2); }
|
|
294
|
+
|
|
295
|
+
/* ── Progress ── */
|
|
296
|
+
.progress-bar {
|
|
297
|
+
position: absolute;
|
|
298
|
+
top: 0; left: 0; right: 0;
|
|
299
|
+
height: 2px;
|
|
300
|
+
background: var(--border);
|
|
301
|
+
overflow: hidden;
|
|
302
|
+
opacity: 0;
|
|
303
|
+
transition: opacity 0.3s;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
.progress-bar.active { opacity: 1; }
|
|
307
|
+
|
|
308
|
+
.progress-bar::after {
|
|
309
|
+
content: '';
|
|
310
|
+
position: absolute;
|
|
311
|
+
top: 0; left: -40%;
|
|
312
|
+
width: 40%; height: 100%;
|
|
313
|
+
background: var(--accent-bright);
|
|
314
|
+
animation: progress-slide 1.2s ease-in-out infinite;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
@keyframes progress-slide {
|
|
318
|
+
0% { left: -40%; }
|
|
319
|
+
100% { left: 100%; }
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
.history-strip {
|
|
323
|
+
display: flex;
|
|
324
|
+
gap: 6px;
|
|
325
|
+
overflow-x: auto;
|
|
326
|
+
padding: 4px 0;
|
|
327
|
+
margin-top: auto;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
.history-strip::-webkit-scrollbar { height: 4px; }
|
|
331
|
+
.history-strip::-webkit-scrollbar-track { background: transparent; }
|
|
332
|
+
.history-strip::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }
|
|
333
|
+
|
|
334
|
+
.history-thumb {
|
|
335
|
+
width: 48px; height: 48px;
|
|
336
|
+
flex-shrink: 0;
|
|
337
|
+
border-radius: 6px;
|
|
338
|
+
object-fit: cover;
|
|
339
|
+
cursor: pointer;
|
|
340
|
+
border: 2px solid transparent;
|
|
341
|
+
opacity: 0.6;
|
|
342
|
+
transition: all 0.15s;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
.history-thumb:hover { opacity: 1; }
|
|
346
|
+
.history-thumb.active { border-color: var(--accent); opacity: 1; }
|
|
347
|
+
|
|
348
|
+
/* ── Toast ── */
|
|
349
|
+
.toast {
|
|
350
|
+
position: fixed;
|
|
351
|
+
bottom: 24px;
|
|
352
|
+
right: 24px;
|
|
353
|
+
background: var(--surface);
|
|
354
|
+
border: 1px solid var(--border);
|
|
355
|
+
border-radius: var(--radius);
|
|
356
|
+
padding: 12px 20px;
|
|
357
|
+
font-family: var(--mono);
|
|
358
|
+
font-size: 13px;
|
|
359
|
+
color: var(--text);
|
|
360
|
+
transform: translateY(100px);
|
|
361
|
+
opacity: 0;
|
|
362
|
+
transition: all 0.3s ease;
|
|
363
|
+
z-index: 100;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
.toast.visible { transform: translateY(0); opacity: 1; }
|
|
367
|
+
.toast.error { border-color: var(--red); color: var(--red); }
|
|
368
|
+
|
|
369
|
+
/* ── Mode tabs ── */
|
|
370
|
+
.mode-tabs {
|
|
371
|
+
display: flex;
|
|
372
|
+
gap: 4px;
|
|
373
|
+
background: var(--bg);
|
|
374
|
+
border-radius: 8px;
|
|
375
|
+
padding: 3px;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
.mode-tab {
|
|
379
|
+
flex: 1;
|
|
380
|
+
padding: 8px;
|
|
381
|
+
background: transparent;
|
|
382
|
+
border: none;
|
|
383
|
+
border-radius: 6px;
|
|
384
|
+
color: var(--text-dim);
|
|
385
|
+
font-family: var(--mono);
|
|
386
|
+
font-size: 12px;
|
|
387
|
+
cursor: pointer;
|
|
388
|
+
transition: all 0.15s;
|
|
389
|
+
text-align: center;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
.mode-tab:hover { color: var(--text); }
|
|
393
|
+
.mode-tab.active { background: var(--surface-2); color: var(--accent-bright); }
|
|
394
|
+
|
|
395
|
+
/* ── Upload zone ── */
|
|
396
|
+
.upload-zone {
|
|
397
|
+
display: none;
|
|
398
|
+
flex-direction: column;
|
|
399
|
+
gap: 8px;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
.upload-zone.visible { display: flex; }
|
|
403
|
+
|
|
404
|
+
.drop-area {
|
|
405
|
+
border: 2px dashed var(--border);
|
|
406
|
+
border-radius: var(--radius);
|
|
407
|
+
padding: 20px;
|
|
408
|
+
text-align: center;
|
|
409
|
+
cursor: pointer;
|
|
410
|
+
transition: all 0.2s;
|
|
411
|
+
font-family: var(--mono);
|
|
412
|
+
font-size: 12px;
|
|
413
|
+
color: var(--text-dim);
|
|
414
|
+
position: relative;
|
|
415
|
+
overflow: hidden;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
.drop-area:hover, .drop-area.dragover {
|
|
419
|
+
border-color: #555;
|
|
420
|
+
color: var(--text);
|
|
421
|
+
background: var(--surface-2);
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
.drop-area.has-image {
|
|
425
|
+
padding: 0;
|
|
426
|
+
border-style: solid;
|
|
427
|
+
border-color: var(--border);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
.drop-area img {
|
|
431
|
+
width: 100%;
|
|
432
|
+
max-height: 180px;
|
|
433
|
+
object-fit: contain;
|
|
434
|
+
border-radius: calc(var(--radius) - 2px);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
.drop-area .remove-btn {
|
|
438
|
+
position: absolute;
|
|
439
|
+
top: 6px;
|
|
440
|
+
right: 6px;
|
|
441
|
+
width: 22px; height: 22px;
|
|
442
|
+
background: rgba(0,0,0,0.7);
|
|
443
|
+
border: 1px solid var(--border);
|
|
444
|
+
border-radius: 50%;
|
|
445
|
+
color: var(--text);
|
|
446
|
+
font-size: 12px;
|
|
447
|
+
cursor: pointer;
|
|
448
|
+
display: flex;
|
|
449
|
+
align-items: center;
|
|
450
|
+
justify-content: center;
|
|
451
|
+
opacity: 0;
|
|
452
|
+
transition: opacity 0.15s;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
.drop-area:hover .remove-btn { opacity: 1; }
|
|
456
|
+
|
|
457
|
+
.use-result-btn {
|
|
458
|
+
padding: 7px 12px;
|
|
459
|
+
background: var(--bg);
|
|
460
|
+
border: 1px solid var(--border);
|
|
461
|
+
border-radius: 8px;
|
|
462
|
+
color: var(--text-dim);
|
|
463
|
+
font-family: var(--mono);
|
|
464
|
+
font-size: 11px;
|
|
465
|
+
cursor: pointer;
|
|
466
|
+
transition: all 0.15s;
|
|
467
|
+
text-align: center;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
.use-result-btn:hover { border-color: #444; color: var(--text); }
|
|
471
|
+
.use-result-btn:disabled { opacity: 0.3; cursor: not-allowed; }
|
|
472
|
+
|
|
473
|
+
/* ── Responsive ── */
|
|
474
|
+
@media (max-width: 800px) {
|
|
475
|
+
.app { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
|
|
476
|
+
.sidebar { border-right: none; border-bottom: 1px solid var(--border); max-height: 50dvh; }
|
|
477
|
+
.canvas { min-height: 50dvh; padding: 20px; }
|
|
478
|
+
}
|
|
479
|
+
</style>
|
|
480
|
+
</head>
|
|
481
|
+
<body>
|
|
482
|
+
<div class="app">
|
|
483
|
+
<aside class="sidebar">
|
|
484
|
+
<div class="logo">
|
|
485
|
+
<div class="logo-dot"></div>
|
|
486
|
+
Image Gen
|
|
487
|
+
<span style="font-family:var(--mono);font-size:11px;color:var(--text-dim);margin-left:auto">gpt-image-2</span>
|
|
488
|
+
</div>
|
|
489
|
+
|
|
490
|
+
<div class="billing-bar" id="billingBar">
|
|
491
|
+
<div class="label">API Status</div>
|
|
492
|
+
<div class="value" id="billingValue" style="color:var(--text-dim)">checking...</div>
|
|
493
|
+
</div>
|
|
494
|
+
|
|
495
|
+
<div class="option-group">
|
|
496
|
+
<div class="section-title">Provider</div>
|
|
497
|
+
<div class="option-row" id="providerGroup">
|
|
498
|
+
<button class="option-btn active" data-value="oauth" style="color:var(--green)">OAuth<br><span style="font-size:10px;color:var(--text-dim)">free / codex login</span></button>
|
|
499
|
+
<button class="option-btn" data-value="api">API Key<br><span style="font-size:10px;color:var(--text-dim)">paid / .env</span></button>
|
|
500
|
+
</div>
|
|
501
|
+
<div id="oauthStatus" style="font-family:var(--mono);font-size:11px;color:var(--text-dim);padding:4px 0"></div>
|
|
502
|
+
</div>
|
|
503
|
+
|
|
504
|
+
<div class="mode-tabs">
|
|
505
|
+
<button class="mode-tab active" data-mode="t2i">Text to Image</button>
|
|
506
|
+
<button class="mode-tab" data-mode="i2i">Image to Image</button>
|
|
507
|
+
</div>
|
|
508
|
+
|
|
509
|
+
<div class="upload-zone" id="uploadZone">
|
|
510
|
+
<div class="section-title">Source Image</div>
|
|
511
|
+
<div class="drop-area" id="dropArea">
|
|
512
|
+
Drop image here or click to upload
|
|
513
|
+
<input type="file" accept="image/*" id="fileInput" style="display:none">
|
|
514
|
+
</div>
|
|
515
|
+
<button class="use-result-btn" id="useResultBtn" disabled>Use current result as source</button>
|
|
516
|
+
</div>
|
|
517
|
+
|
|
518
|
+
<div class="section-title">Prompt</div>
|
|
519
|
+
<textarea class="prompt-area" id="prompt" placeholder="Describe the image you want to generate..."></textarea>
|
|
520
|
+
|
|
521
|
+
<div class="option-group">
|
|
522
|
+
<div class="section-title">Quality</div>
|
|
523
|
+
<div class="option-row" id="qualityGroup">
|
|
524
|
+
<button class="option-btn active" data-value="low">Low<br><span style="font-size:10px;color:var(--text-dim)">fast</span></button>
|
|
525
|
+
<button class="option-btn" data-value="medium">Medium<br><span style="font-size:10px;color:var(--text-dim)">balanced</span></button>
|
|
526
|
+
<button class="option-btn" data-value="high">High<br><span style="font-size:10px;color:var(--text-dim)">best</span></button>
|
|
527
|
+
</div>
|
|
528
|
+
</div>
|
|
529
|
+
|
|
530
|
+
<div class="option-group">
|
|
531
|
+
<div class="section-title">Size</div>
|
|
532
|
+
<div class="option-row" id="sizeGroup1">
|
|
533
|
+
<button class="option-btn active" data-value="1024x1024">1024x1024<br><span style="font-size:10px;color:var(--text-dim)">square</span></button>
|
|
534
|
+
<button class="option-btn" data-value="1536x1024">1536x1024<br><span style="font-size:10px;color:var(--text-dim)">landscape</span></button>
|
|
535
|
+
<button class="option-btn" data-value="1024x1536">1024x1536<br><span style="font-size:10px;color:var(--text-dim)">portrait</span></button>
|
|
536
|
+
</div>
|
|
537
|
+
<div class="option-row" id="sizeGroup2">
|
|
538
|
+
<button class="option-btn" data-value="2048x2048">2048x2048<br><span style="font-size:10px;color:var(--text-dim)">2K sq</span></button>
|
|
539
|
+
<button class="option-btn" data-value="2048x1152">2048x1152<br><span style="font-size:10px;color:var(--text-dim)">2K land</span></button>
|
|
540
|
+
<button class="option-btn" data-value="auto">auto<br><span style="font-size:10px;color:var(--text-dim)">model picks</span></button>
|
|
541
|
+
</div>
|
|
542
|
+
<div class="option-row" id="sizeGroup3">
|
|
543
|
+
<button class="option-btn" data-value="3840x2160">3840x2160<br><span style="font-size:10px;color:var(--text-dim)">4K land</span></button>
|
|
544
|
+
<button class="option-btn" data-value="2160x3840">2160x3840<br><span style="font-size:10px;color:var(--text-dim)">4K port</span></button>
|
|
545
|
+
<button class="option-btn" data-value="custom" id="customSizeBtn">Custom<br><span style="font-size:10px;color:var(--text-dim)">any ratio</span></button>
|
|
546
|
+
</div>
|
|
547
|
+
<div class="option-row" id="customSizeRow" style="display:none">
|
|
548
|
+
<input type="number" id="customW" placeholder="W" min="256" max="3840" step="16" value="1920" style="flex:1;padding:8px;background:var(--bg);border:1px solid var(--border);border-radius:8px;color:var(--text);font-family:var(--mono);font-size:12px;text-align:center;outline:none">
|
|
549
|
+
<span style="color:var(--text-dim);align-self:center;font-family:var(--mono);font-size:12px">x</span>
|
|
550
|
+
<input type="number" id="customH" placeholder="H" min="256" max="3840" step="16" value="1088" style="flex:1;padding:8px;background:var(--bg);border:1px solid var(--border);border-radius:8px;color:var(--text);font-family:var(--mono);font-size:12px;text-align:center;outline:none">
|
|
551
|
+
</div>
|
|
552
|
+
<div id="sizeHint" style="font-family:var(--mono);font-size:10px;color:var(--text-dim);display:none">Both must be multiples of 16, max 3840, ratio ≤ 3:1</div>
|
|
553
|
+
</div>
|
|
554
|
+
|
|
555
|
+
<div class="option-group">
|
|
556
|
+
<div class="section-title">Format</div>
|
|
557
|
+
<div class="option-row">
|
|
558
|
+
<button class="option-btn active format-btn" data-value="png">PNG</button>
|
|
559
|
+
<button class="option-btn format-btn" data-value="jpeg">JPEG</button>
|
|
560
|
+
<button class="option-btn format-btn" data-value="webp">WebP</button>
|
|
561
|
+
</div>
|
|
562
|
+
</div>
|
|
563
|
+
|
|
564
|
+
<div class="option-group">
|
|
565
|
+
<div class="section-title">Moderation</div>
|
|
566
|
+
<div class="option-row" id="moderationGroup">
|
|
567
|
+
<button class="option-btn active" data-value="low" style="color:var(--amber)">Low<br><span style="font-size:10px;color:var(--text-dim)">less restrictive</span></button>
|
|
568
|
+
<button class="option-btn" data-value="auto">Auto<br><span style="font-size:10px;color:var(--text-dim)">standard</span></button>
|
|
569
|
+
</div>
|
|
570
|
+
</div>
|
|
571
|
+
|
|
572
|
+
<div class="cost-estimate">
|
|
573
|
+
<span>Est. cost</span>
|
|
574
|
+
<span class="price" id="costEstimate">~$0.006</span>
|
|
575
|
+
</div>
|
|
576
|
+
|
|
577
|
+
<button class="generate-btn" id="generateBtn">Generate</button>
|
|
578
|
+
|
|
579
|
+
<div class="history-strip" id="historyStrip"></div>
|
|
580
|
+
</aside>
|
|
581
|
+
|
|
582
|
+
<main class="canvas">
|
|
583
|
+
<div class="progress-bar" id="progressBar"></div>
|
|
584
|
+
<div class="canvas-empty" id="emptyState">
|
|
585
|
+
GPT-IMAGE-2
|
|
586
|
+
<span>Enter a prompt and hit generate</span>
|
|
587
|
+
</div>
|
|
588
|
+
<div class="result-container" id="resultContainer">
|
|
589
|
+
<img class="result-img" id="resultImg">
|
|
590
|
+
<div class="result-prompt" id="resultPrompt"></div>
|
|
591
|
+
<div class="result-meta" id="resultMeta"></div>
|
|
592
|
+
<div class="result-actions">
|
|
593
|
+
<button class="action-btn" id="downloadBtn">Download</button>
|
|
594
|
+
<button class="action-btn" id="copyBtn">Copy to clipboard</button>
|
|
595
|
+
<button class="action-btn" id="copyPromptBtn">Copy prompt</button>
|
|
596
|
+
</div>
|
|
597
|
+
</div>
|
|
598
|
+
</main>
|
|
599
|
+
</div>
|
|
600
|
+
|
|
601
|
+
<div class="toast" id="toast"></div>
|
|
602
|
+
|
|
603
|
+
<script>
|
|
604
|
+
const $ = (s) => document.querySelector(s);
|
|
605
|
+
const $$ = (s) => document.querySelectorAll(s);
|
|
606
|
+
|
|
607
|
+
let state = {
|
|
608
|
+
mode: "t2i",
|
|
609
|
+
provider: "oauth",
|
|
610
|
+
quality: "low",
|
|
611
|
+
size: "1024x1024",
|
|
612
|
+
format: "png",
|
|
613
|
+
moderation: "low",
|
|
614
|
+
generating: false,
|
|
615
|
+
history: [],
|
|
616
|
+
currentImage: null,
|
|
617
|
+
sourceImageB64: null,
|
|
618
|
+
};
|
|
619
|
+
|
|
620
|
+
// ── Cost matrix (gpt-image-2) ──
|
|
621
|
+
const COST_MAP = {
|
|
622
|
+
"low": { "1024x1024": 0.006, "1024x1536": 0.005, "1536x1024": 0.005, "2048x2048": 0.012, "2048x1152": 0.009, "3840x2160": 0.023, "2160x3840": 0.023, "auto": 0.006 },
|
|
623
|
+
"medium": { "1024x1024": 0.053, "1024x1536": 0.041, "1536x1024": 0.041, "2048x2048": 0.106, "2048x1152": 0.080, "3840x2160": 0.200, "2160x3840": 0.200, "auto": 0.053 },
|
|
624
|
+
"high": { "1024x1024": 0.211, "1024x1536": 0.165, "1536x1024": 0.165, "2048x2048": 0.422, "2048x1152": 0.320, "3840x2160": 0.800, "2160x3840": 0.800, "auto": 0.211 },
|
|
625
|
+
};
|
|
626
|
+
|
|
627
|
+
function updateCost() {
|
|
628
|
+
if (state.provider === "oauth") {
|
|
629
|
+
$("#costEstimate").textContent = "free";
|
|
630
|
+
$("#costEstimate").style.color = "var(--green)";
|
|
631
|
+
return;
|
|
632
|
+
}
|
|
633
|
+
$("#costEstimate").style.color = "";
|
|
634
|
+
const cost = COST_MAP[state.quality]?.[state.size] ?? 0;
|
|
635
|
+
$("#costEstimate").textContent = `~$${cost.toFixed(3)}`;
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
// ── Provider ──
|
|
639
|
+
setupOptionGroup($("#providerGroup"), "provider");
|
|
640
|
+
|
|
641
|
+
async function checkOAuthStatus() {
|
|
642
|
+
try {
|
|
643
|
+
const res = await fetch("/api/oauth/status");
|
|
644
|
+
const data = await res.json();
|
|
645
|
+
const el = $("#oauthStatus");
|
|
646
|
+
if (data.status === "ready") {
|
|
647
|
+
el.textContent = "gpt-image-2 ready";
|
|
648
|
+
el.style.color = "var(--green)";
|
|
649
|
+
} else if (data.status === "auth_required") {
|
|
650
|
+
el.innerHTML = 'Run <code style="color:var(--accent)">codex login</code> first';
|
|
651
|
+
el.style.color = "var(--amber)";
|
|
652
|
+
} else {
|
|
653
|
+
el.textContent = "OAuth proxy starting...";
|
|
654
|
+
el.style.color = "var(--text-dim)";
|
|
655
|
+
setTimeout(checkOAuthStatus, 3000);
|
|
656
|
+
}
|
|
657
|
+
} catch { $("#oauthStatus").textContent = ""; }
|
|
658
|
+
}
|
|
659
|
+
checkOAuthStatus();
|
|
660
|
+
|
|
661
|
+
// ── Custom size ──
|
|
662
|
+
const allSizeButtons = $$("#sizeGroup1 .option-btn, #sizeGroup2 .option-btn, #sizeGroup3 .option-btn");
|
|
663
|
+
|
|
664
|
+
function handleSizeClick(btn) {
|
|
665
|
+
allSizeButtons.forEach((b) => b.classList.remove("active"));
|
|
666
|
+
btn.classList.add("active");
|
|
667
|
+
const val = btn.dataset.value;
|
|
668
|
+
const isCustom = val === "custom";
|
|
669
|
+
$("#customSizeRow").style.display = isCustom ? "flex" : "none";
|
|
670
|
+
$("#sizeHint").style.display = isCustom ? "block" : "none";
|
|
671
|
+
if (isCustom) {
|
|
672
|
+
updateCustomSize();
|
|
673
|
+
} else {
|
|
674
|
+
state.size = val;
|
|
675
|
+
}
|
|
676
|
+
updateCost();
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
allSizeButtons.forEach((btn) => {
|
|
680
|
+
btn.addEventListener("click", () => handleSizeClick(btn));
|
|
681
|
+
});
|
|
682
|
+
|
|
683
|
+
function snap16(n) { return Math.round(n / 16) * 16; }
|
|
684
|
+
|
|
685
|
+
function updateCustomSize() {
|
|
686
|
+
const w = snap16(parseInt($("#customW").value) || 1024);
|
|
687
|
+
const h = snap16(parseInt($("#customH").value) || 1024);
|
|
688
|
+
state.size = `${w}x${h}`;
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
$("#customW").addEventListener("change", updateCustomSize);
|
|
692
|
+
$("#customH").addEventListener("change", updateCustomSize);
|
|
693
|
+
|
|
694
|
+
// ── Mode tabs ──
|
|
695
|
+
$$(".mode-tab").forEach((tab) => {
|
|
696
|
+
tab.addEventListener("click", () => {
|
|
697
|
+
$$(".mode-tab").forEach((t) => t.classList.remove("active"));
|
|
698
|
+
tab.classList.add("active");
|
|
699
|
+
state.mode = tab.dataset.mode;
|
|
700
|
+
const uploadZone = $("#uploadZone");
|
|
701
|
+
uploadZone.classList.toggle("visible", state.mode === "i2i");
|
|
702
|
+
$("#generateBtn").textContent = state.mode === "i2i" ? "Edit Image" : "Generate";
|
|
703
|
+
$("#prompt").placeholder = state.mode === "i2i"
|
|
704
|
+
? "Describe the edit you want to make..."
|
|
705
|
+
: "Describe the image you want to generate...";
|
|
706
|
+
});
|
|
707
|
+
});
|
|
708
|
+
|
|
709
|
+
// ── Image upload ──
|
|
710
|
+
const dropArea = $("#dropArea");
|
|
711
|
+
const fileInput = $("#fileInput");
|
|
712
|
+
|
|
713
|
+
dropArea.addEventListener("click", () => {
|
|
714
|
+
if (!dropArea.classList.contains("has-image")) fileInput.click();
|
|
715
|
+
});
|
|
716
|
+
|
|
717
|
+
dropArea.addEventListener("dragover", (e) => { e.preventDefault(); dropArea.classList.add("dragover"); });
|
|
718
|
+
dropArea.addEventListener("dragleave", () => dropArea.classList.remove("dragover"));
|
|
719
|
+
dropArea.addEventListener("drop", (e) => {
|
|
720
|
+
e.preventDefault();
|
|
721
|
+
dropArea.classList.remove("dragover");
|
|
722
|
+
const file = e.dataTransfer.files[0];
|
|
723
|
+
if (file && file.type.startsWith("image/")) loadSourceImage(file);
|
|
724
|
+
});
|
|
725
|
+
|
|
726
|
+
fileInput.addEventListener("change", () => {
|
|
727
|
+
if (fileInput.files[0]) loadSourceImage(fileInput.files[0]);
|
|
728
|
+
});
|
|
729
|
+
|
|
730
|
+
function loadSourceImage(file) {
|
|
731
|
+
const reader = new FileReader();
|
|
732
|
+
reader.onload = () => {
|
|
733
|
+
const b64 = reader.result.split(",")[1];
|
|
734
|
+
state.sourceImageB64 = b64;
|
|
735
|
+
dropArea.classList.add("has-image");
|
|
736
|
+
dropArea.innerHTML = `<img src="${reader.result}"><button class="remove-btn" onclick="clearSourceImage(event)">×</button>`;
|
|
737
|
+
};
|
|
738
|
+
reader.readAsDataURL(file);
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
function loadSourceFromDataUrl(dataUrl) {
|
|
742
|
+
state.sourceImageB64 = dataUrl.split(",")[1];
|
|
743
|
+
dropArea.classList.add("has-image");
|
|
744
|
+
dropArea.innerHTML = `<img src="${dataUrl}"><button class="remove-btn" onclick="clearSourceImage(event)">×</button>`;
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
function clearSourceImage(e) {
|
|
748
|
+
if (e) e.stopPropagation();
|
|
749
|
+
state.sourceImageB64 = null;
|
|
750
|
+
dropArea.classList.remove("has-image");
|
|
751
|
+
dropArea.innerHTML = 'Drop image here or click to upload<input type="file" accept="image/*" id="fileInput" style="display:none">';
|
|
752
|
+
document.getElementById("fileInput").addEventListener("change", () => {
|
|
753
|
+
const f = document.getElementById("fileInput").files[0];
|
|
754
|
+
if (f) loadSourceImage(f);
|
|
755
|
+
});
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
$("#useResultBtn").addEventListener("click", () => {
|
|
759
|
+
if (!state.currentImage) return;
|
|
760
|
+
loadSourceFromDataUrl(state.currentImage.image);
|
|
761
|
+
// switch to i2i mode
|
|
762
|
+
$$(".mode-tab").forEach((t) => t.classList.remove("active"));
|
|
763
|
+
document.querySelector('.mode-tab[data-mode="i2i"]').classList.add("active");
|
|
764
|
+
state.mode = "i2i";
|
|
765
|
+
$("#uploadZone").classList.add("visible");
|
|
766
|
+
$("#generateBtn").textContent = "Edit Image";
|
|
767
|
+
$("#prompt").placeholder = "Describe the edit you want to make...";
|
|
768
|
+
toast("Source image loaded from result");
|
|
769
|
+
});
|
|
770
|
+
|
|
771
|
+
// ── Option buttons ──
|
|
772
|
+
function setupOptionGroup(container, key) {
|
|
773
|
+
container.addEventListener("click", (e) => {
|
|
774
|
+
const btn = e.target.closest(".option-btn");
|
|
775
|
+
if (!btn) return;
|
|
776
|
+
container.querySelectorAll(".option-btn").forEach((b) => b.classList.remove("active"));
|
|
777
|
+
btn.classList.add("active");
|
|
778
|
+
state[key] = btn.dataset.value;
|
|
779
|
+
updateCost();
|
|
780
|
+
});
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
setupOptionGroup($("#qualityGroup"), "quality");
|
|
784
|
+
setupOptionGroup($("#moderationGroup"), "moderation");
|
|
785
|
+
|
|
786
|
+
$$(".format-btn").forEach((btn) => {
|
|
787
|
+
btn.addEventListener("click", () => {
|
|
788
|
+
$$(".format-btn").forEach((b) => b.classList.remove("active"));
|
|
789
|
+
btn.classList.add("active");
|
|
790
|
+
state.format = btn.dataset.value;
|
|
791
|
+
});
|
|
792
|
+
});
|
|
793
|
+
|
|
794
|
+
// ── Generate ──
|
|
795
|
+
$("#generateBtn").addEventListener("click", generate);
|
|
796
|
+
$("#prompt").addEventListener("keydown", (e) => {
|
|
797
|
+
if (e.key === "Enter" && (e.metaKey || e.ctrlKey)) generate();
|
|
798
|
+
});
|
|
799
|
+
|
|
800
|
+
async function generate() {
|
|
801
|
+
const prompt = $("#prompt").value.trim();
|
|
802
|
+
if (!prompt || state.generating) return;
|
|
803
|
+
|
|
804
|
+
state.generating = true;
|
|
805
|
+
const btn = $("#generateBtn");
|
|
806
|
+
btn.disabled = true;
|
|
807
|
+
btn.classList.add("loading");
|
|
808
|
+
btn.textContent = "Generating...";
|
|
809
|
+
$("#progressBar").classList.add("active");
|
|
810
|
+
$("#emptyState").style.display = "none";
|
|
811
|
+
|
|
812
|
+
try {
|
|
813
|
+
const isEdit = state.mode === "i2i" && state.sourceImageB64;
|
|
814
|
+
const endpoint = isEdit ? "/api/edit" : "/api/generate";
|
|
815
|
+
const payload = {
|
|
816
|
+
prompt,
|
|
817
|
+
quality: state.quality,
|
|
818
|
+
size: state.size,
|
|
819
|
+
format: state.format,
|
|
820
|
+
moderation: state.moderation,
|
|
821
|
+
provider: state.provider,
|
|
822
|
+
};
|
|
823
|
+
if (isEdit) payload.image = state.sourceImageB64;
|
|
824
|
+
|
|
825
|
+
const res = await fetch(endpoint, {
|
|
826
|
+
method: "POST",
|
|
827
|
+
headers: { "Content-Type": "application/json" },
|
|
828
|
+
body: JSON.stringify(payload),
|
|
829
|
+
});
|
|
830
|
+
|
|
831
|
+
const data = await res.json();
|
|
832
|
+
if (!res.ok) throw new Error(data.error || "Generation failed");
|
|
833
|
+
|
|
834
|
+
data.prompt = prompt;
|
|
835
|
+
state.currentImage = data;
|
|
836
|
+
showResult(data);
|
|
837
|
+
addToHistory(data);
|
|
838
|
+
toast(`Generated in ${data.elapsed}s`);
|
|
839
|
+
} catch (err) {
|
|
840
|
+
toast(err.message, true);
|
|
841
|
+
$("#emptyState").style.display = "";
|
|
842
|
+
} finally {
|
|
843
|
+
state.generating = false;
|
|
844
|
+
btn.disabled = false;
|
|
845
|
+
btn.classList.remove("loading");
|
|
846
|
+
btn.textContent = "Generate";
|
|
847
|
+
$("#progressBar").classList.remove("active");
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
function showResult(data) {
|
|
852
|
+
const container = $("#resultContainer");
|
|
853
|
+
const img = $("#resultImg");
|
|
854
|
+
img.src = data.image;
|
|
855
|
+
container.classList.add("visible");
|
|
856
|
+
$("#useResultBtn").disabled = false;
|
|
857
|
+
$("#resultPrompt").textContent = data.prompt || "";
|
|
858
|
+
|
|
859
|
+
const meta = [];
|
|
860
|
+
meta.push(`${data.elapsed}s`);
|
|
861
|
+
if (data.usage) {
|
|
862
|
+
meta.push(`${data.usage.total_tokens || "?"} tokens`);
|
|
863
|
+
}
|
|
864
|
+
meta.push(state.quality);
|
|
865
|
+
meta.push(state.size);
|
|
866
|
+
if (data.provider) meta.push(data.provider);
|
|
867
|
+
$("#resultMeta").textContent = meta.join(" · ");
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
function addToHistory(data) {
|
|
871
|
+
state.history.unshift(data);
|
|
872
|
+
saveHistory();
|
|
873
|
+
renderHistory();
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
function saveHistory() {
|
|
877
|
+
try {
|
|
878
|
+
const slim = state.history.slice(0, 50).map((h) => ({
|
|
879
|
+
image: h.image,
|
|
880
|
+
prompt: h.prompt,
|
|
881
|
+
elapsed: h.elapsed,
|
|
882
|
+
filename: h.filename,
|
|
883
|
+
provider: h.provider,
|
|
884
|
+
usage: h.usage,
|
|
885
|
+
}));
|
|
886
|
+
localStorage.setItem("ima2_history", JSON.stringify(slim));
|
|
887
|
+
} catch {}
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
function loadHistory() {
|
|
891
|
+
try {
|
|
892
|
+
const raw = localStorage.getItem("ima2_history");
|
|
893
|
+
if (raw) {
|
|
894
|
+
state.history = JSON.parse(raw);
|
|
895
|
+
renderHistory();
|
|
896
|
+
if (state.history.length > 0) {
|
|
897
|
+
state.currentImage = state.history[0];
|
|
898
|
+
showResult(state.history[0]);
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
} catch {}
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
function renderHistory() {
|
|
905
|
+
const strip = $("#historyStrip");
|
|
906
|
+
strip.innerHTML = "";
|
|
907
|
+
state.history.forEach((item, i) => {
|
|
908
|
+
const thumb = document.createElement("img");
|
|
909
|
+
thumb.className = "history-thumb" + (i === 0 ? " active" : "");
|
|
910
|
+
thumb.src = item.image;
|
|
911
|
+
thumb.addEventListener("click", () => {
|
|
912
|
+
state.currentImage = item;
|
|
913
|
+
showResult(item);
|
|
914
|
+
strip.querySelectorAll(".history-thumb").forEach((t) => t.classList.remove("active"));
|
|
915
|
+
thumb.classList.add("active");
|
|
916
|
+
});
|
|
917
|
+
strip.appendChild(thumb);
|
|
918
|
+
});
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
// ── Download & Copy ──
|
|
922
|
+
$("#downloadBtn").addEventListener("click", () => {
|
|
923
|
+
if (!state.currentImage) return;
|
|
924
|
+
const a = document.createElement("a");
|
|
925
|
+
a.href = state.currentImage.image;
|
|
926
|
+
a.download = state.currentImage.filename || "generated.png";
|
|
927
|
+
a.click();
|
|
928
|
+
});
|
|
929
|
+
|
|
930
|
+
$("#copyBtn").addEventListener("click", async () => {
|
|
931
|
+
if (!state.currentImage) return;
|
|
932
|
+
try {
|
|
933
|
+
const res = await fetch(state.currentImage.image);
|
|
934
|
+
const blob = await res.blob();
|
|
935
|
+
await navigator.clipboard.write([new ClipboardItem({ [blob.type]: blob })]);
|
|
936
|
+
toast("Copied to clipboard");
|
|
937
|
+
} catch {
|
|
938
|
+
toast("Copy failed", true);
|
|
939
|
+
}
|
|
940
|
+
});
|
|
941
|
+
|
|
942
|
+
// ── Copy prompt ──
|
|
943
|
+
$("#copyPromptBtn").addEventListener("click", () => {
|
|
944
|
+
if (!state.currentImage?.prompt) return;
|
|
945
|
+
navigator.clipboard.writeText(state.currentImage.prompt);
|
|
946
|
+
toast("Prompt copied");
|
|
947
|
+
});
|
|
948
|
+
|
|
949
|
+
$("#resultPrompt").addEventListener("click", () => {
|
|
950
|
+
if (!state.currentImage?.prompt) return;
|
|
951
|
+
navigator.clipboard.writeText(state.currentImage.prompt);
|
|
952
|
+
toast("Prompt copied");
|
|
953
|
+
});
|
|
954
|
+
|
|
955
|
+
// ── Toast ──
|
|
956
|
+
function toast(msg, isError = false) {
|
|
957
|
+
const el = $("#toast");
|
|
958
|
+
el.textContent = msg;
|
|
959
|
+
el.className = "toast visible" + (isError ? " error" : "");
|
|
960
|
+
clearTimeout(el._timer);
|
|
961
|
+
el._timer = setTimeout(() => el.classList.remove("visible"), 3000);
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
// ── Billing ──
|
|
965
|
+
async function loadBilling() {
|
|
966
|
+
try {
|
|
967
|
+
const res = await fetch("/api/billing");
|
|
968
|
+
const data = await res.json();
|
|
969
|
+
const el = $("#billingValue");
|
|
970
|
+
|
|
971
|
+
if (data.credits) {
|
|
972
|
+
const total = data.credits.total_granted || 0;
|
|
973
|
+
const used = data.credits.total_used || 0;
|
|
974
|
+
const remaining = (total - used).toFixed(2);
|
|
975
|
+
el.textContent = `$${remaining} remaining`;
|
|
976
|
+
el.style.color = remaining > 5 ? "var(--green)" : remaining > 1 ? "var(--amber)" : "var(--red)";
|
|
977
|
+
return;
|
|
978
|
+
}
|
|
979
|
+
|
|
980
|
+
if (data.costs?.data?.length) {
|
|
981
|
+
const totalCost = data.costs.data.reduce((sum, bucket) => {
|
|
982
|
+
return sum + bucket.results.reduce((s, r) => s + (r.amount?.value || 0), 0);
|
|
983
|
+
}, 0);
|
|
984
|
+
el.textContent = `$${(totalCost / 100).toFixed(2)} this month`;
|
|
985
|
+
el.style.color = "var(--accent)";
|
|
986
|
+
return;
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
if (data.oauth) {
|
|
990
|
+
el.textContent = "OAuth mode (no API key)";
|
|
991
|
+
el.style.color = "var(--accent)";
|
|
992
|
+
return;
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
if (data.apiKeyValid) {
|
|
996
|
+
el.textContent = "API key valid";
|
|
997
|
+
el.style.color = "var(--green)";
|
|
998
|
+
} else {
|
|
999
|
+
el.textContent = "Could not fetch billing";
|
|
1000
|
+
el.style.color = "var(--text-dim)";
|
|
1001
|
+
}
|
|
1002
|
+
} catch {
|
|
1003
|
+
$("#billingValue").textContent = "offline";
|
|
1004
|
+
$("#billingValue").style.color = "var(--red)";
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
|
|
1008
|
+
loadBilling();
|
|
1009
|
+
updateCost();
|
|
1010
|
+
loadHistory();
|
|
1011
|
+
</script>
|
|
1012
|
+
</body>
|
|
1013
|
+
</html>
|