forked from op7418/CodePilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageGenConfirmation.tsx
More file actions
346 lines (320 loc) · 11.8 KB
/
ImageGenConfirmation.tsx
File metadata and controls
346 lines (320 loc) · 11.8 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
'use client';
import { useState, useCallback, useEffect, useRef } from 'react';
import { cn } from '@/lib/utils';
import { Button } from '@/components/ui/button';
import { Textarea } from '@/components/ui/textarea';
import { ImageGenCard } from './ImageGenCard';
import { useTranslation } from '@/hooks/useTranslation';
import { usePanel } from '@/hooks/usePanel';
import type { TranslationKey } from '@/i18n';
import type { ReferenceImage } from '@/types';
import type { ImageGenResult } from '@/hooks/useImageGen';
const ASPECT_RATIOS = [
'1:1', '16:9', '9:16', '3:2', '2:3', '4:3', '3:4', '4:5', '5:4', '21:9',
] as const;
const RESOLUTIONS = ['1K', '2K', '4K'] as const;
interface ImageGenConfirmationProps {
messageId?: string;
sessionId?: string;
initialPrompt: string;
initialAspectRatio: string;
initialResolution: string;
/** The original raw ```image-gen-request...``` block — used for exact DB matching */
rawRequestBlock?: string;
referenceImages?: ReferenceImage[];
}
type Status = 'idle' | 'generating' | 'completed' | 'error';
export function ImageGenConfirmation({
messageId,
sessionId: sessionIdProp,
initialPrompt,
initialAspectRatio,
initialResolution,
rawRequestBlock,
referenceImages,
}: ImageGenConfirmationProps) {
const { t } = useTranslation();
const { sessionId: panelSessionId } = usePanel();
const sessionId = sessionIdProp || panelSessionId;
const [prompt, setPrompt] = useState(initialPrompt);
const [aspectRatio, setAspectRatio] = useState(
ASPECT_RATIOS.includes(initialAspectRatio as typeof ASPECT_RATIOS[number])
? initialAspectRatio
: '1:1'
);
const [resolution, setResolution] = useState(
RESOLUTIONS.includes(initialResolution as typeof RESOLUTIONS[number])
? initialResolution
: '1K'
);
const [status, setStatus] = useState<Status>('idle');
const [result, setResult] = useState<ImageGenResult | null>(null);
const [error, setError] = useState<string | null>(null);
const abortRef = useRef<AbortController | null>(null);
const handleStop = useCallback(() => {
if (abortRef.current) {
abortRef.current.abort();
abortRef.current = null;
}
setStatus('idle');
}, []);
const handleGenerate = useCallback(async () => {
const controller = new AbortController();
abortRef.current = controller;
setStatus('generating');
setError(null);
try {
// Split unified ReferenceImage[] back into base64 data vs file paths for the API
const refData = referenceImages?.filter(r => r.data).map(r => ({ mimeType: r.mimeType, data: r.data! }));
const refPaths = referenceImages?.filter(r => r.localPath).map(r => r.localPath!);
const res = await fetch('/api/media/generate', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
prompt,
aspectRatio,
imageSize: resolution,
sessionId,
...(refData && refData.length > 0
? { referenceImages: refData }
: {}),
...(refPaths && refPaths.length > 0
? { referenceImagePaths: refPaths }
: {}),
}),
signal: controller.signal,
});
if (!res.ok) {
const err = await res.json().catch(() => ({ error: 'Generation failed' }));
throw new Error(err.error || 'Generation failed');
}
const data = await res.json();
const genResult: ImageGenResult = {
id: data.id,
text: data.text,
images: data.images || [],
};
if (genResult.images.length > 0) {
setResult(genResult);
setStatus('completed');
// Persist result to DB by replacing image-gen-request with image-gen-result.
// During streaming the assistant message may not yet be in DB (no messageId),
// so retry once after a short delay to give the stream time to complete.
{
const resultBlock = JSON.stringify({
status: 'completed',
prompt,
aspectRatio,
resolution,
images: genResult.images.map(img => ({
mimeType: img.mimeType,
localPath: img.localPath,
})),
});
const persistBody = {
message_id: messageId || '',
content: '```image-gen-result\n' + resultBlock + '\n```',
session_id: sessionId,
prompt_hint: initialPrompt,
// Pass the raw block for exact content matching when messageId is unavailable
raw_request_block: rawRequestBlock,
};
const doPut = () => fetch('/api/chat/messages', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(persistBody),
});
doPut().then(r => {
if (!r.ok && !messageId) {
// Retry after 3s — message should be persisted by then
setTimeout(() => doPut().catch(() => {}), 3000);
}
}).catch(() => {
if (!messageId) {
setTimeout(() => doPut().catch(() => {}), 3000);
}
});
}
// Defer event dispatch so React commits setResult/setStatus before
// ChatView's handler calls sendMessage and triggers a re-render
setTimeout(() => {
window.dispatchEvent(new CustomEvent('image-gen-completed', {
detail: {
prompt,
aspectRatio,
resolution,
id: genResult.id,
images: genResult.images,
},
}));
}, 0);
} else {
setError('No images were generated');
setStatus('error');
}
} catch (err) {
if ((err as Error).name === 'AbortError') {
setStatus('idle');
return;
}
setError((err as Error).message || 'Generation failed');
setStatus('error');
} finally {
abortRef.current = null;
}
}, [prompt, aspectRatio, resolution, initialPrompt, sessionId, messageId, referenceImages]);
const handleRegenerate = useCallback(() => {
setResult(null);
setStatus('idle');
}, []);
// ── Completed: show result only ──
if (status === 'completed' && result && result.images.length > 0) {
return (
<div className="my-2">
<ImageGenCard
images={result.images}
prompt={prompt}
aspectRatio={aspectRatio}
imageSize={resolution}
onRegenerate={handleRegenerate}
referenceImages={referenceImages?.filter(r => r.data).map(r => ({ mimeType: r.mimeType, data: r.data! }))}
/>
</div>
);
}
// ── Idle / Generating / Error: show params card ──
return (
<div className="rounded-lg border border-border/50 bg-card overflow-hidden my-2">
{/* Header */}
<div className="flex items-center gap-2 px-4 py-2.5 border-b border-border/30 bg-muted/30">
<span className="text-sm font-medium">{t('imageGen.confirmTitle' as TranslationKey)}</span>
</div>
<div className="p-4 space-y-3">
{/* Reference images preview — unified loop over all reference images */}
{referenceImages && referenceImages.length > 0 && (
<div>
<label className="text-xs font-medium text-muted-foreground mb-1.5 block">
{t('imageGen.referenceImages' as TranslationKey)}
</label>
<div className="flex gap-2 flex-wrap">
{referenceImages.map((img, i) => (
<div key={i} className="w-16 h-16 rounded-md border border-border/30 overflow-hidden bg-muted/30">
{/* eslint-disable-next-line @next/next/no-img-element */}
<img
src={img.data
? `data:${img.mimeType};base64,${img.data}`
: `/api/uploads?path=${encodeURIComponent(img.localPath!)}`}
alt={`Reference ${i + 1}`}
className="w-full h-full object-cover"
/>
</div>
))}
</div>
</div>
)}
{/* Prompt textarea */}
<div>
<label className="text-xs font-medium text-muted-foreground mb-1 block">
{t('imageGen.prompt' as TranslationKey)}
</label>
<Textarea
value={prompt}
onChange={(e) => setPrompt(e.target.value)}
disabled={status === 'generating'}
rows={3}
className={cn(
'resize-none',
'disabled:opacity-60 disabled:cursor-not-allowed'
)}
/>
</div>
{/* Aspect Ratio */}
<div>
<label className="text-xs font-medium text-muted-foreground mb-1.5 block">
{t('imageGen.aspectRatio' as TranslationKey)}
</label>
<div className="flex flex-wrap gap-1.5">
{ASPECT_RATIOS.map((ratio) => (
<Button
key={ratio}
variant="outline"
size="xs"
disabled={status === 'generating'}
onClick={() => setAspectRatio(ratio)}
className={cn(
aspectRatio === ratio
? 'border-primary bg-primary/10 text-primary'
: 'border-border/60 text-muted-foreground hover:text-foreground hover:border-foreground/30'
)}
>
{ratio}
</Button>
))}
</div>
</div>
{/* Resolution */}
<div>
<label className="text-xs font-medium text-muted-foreground mb-1.5 block">
{t('imageGen.resolution' as TranslationKey)}
</label>
<div className="flex items-center gap-1.5">
{RESOLUTIONS.map((res) => (
<Button
key={res}
variant="outline"
size="xs"
disabled={status === 'generating'}
onClick={() => setResolution(res)}
className={cn(
resolution === res
? 'border-primary bg-primary/10 text-primary'
: 'border-border/60 text-muted-foreground hover:text-foreground hover:border-foreground/30'
)}
>
{res}
</Button>
))}
</div>
</div>
{/* Generate button */}
{status === 'idle' && (
<div className="pt-1">
<Button
onClick={handleGenerate}
disabled={!prompt.trim()}
size="sm"
className="gap-1.5"
>
{t('imageGen.generateButton' as TranslationKey)}
</Button>
</div>
)}
{/* Generating: spinner + stop */}
{status === 'generating' && (
<div className="pt-1">
<div className="flex items-center gap-3">
<div className="flex items-center gap-2">
<div className="h-4 w-4 animate-spin rounded-full border-2 border-primary border-t-transparent" />
<span className="text-sm text-muted-foreground">
{t('imageGen.generatingStatus' as TranslationKey)}
</span>
</div>
<Button onClick={handleStop} variant="outline" size="sm">
{t('imageGen.stopButton' as TranslationKey)}
</Button>
</div>
</div>
)}
{/* Error */}
{status === 'error' && error && (
<div className="space-y-2">
<p className="text-sm text-status-error-foreground">{error}</p>
<Button onClick={handleGenerate} variant="outline" size="sm">
{t('imageGen.retryButton' as TranslationKey)}
</Button>
</div>
)}
</div>
</div>
);
}