feat(image): 实现图片比例选择并进行移动端响应式优化
Browse files- 后端:支持 1:1, 16:9, 9:16, 4:3, 3:4 比例控制
- 前端:添加比例选择下拉框,并实现 localStorage 持久化记忆
- 移动端适配:
- 在小屏幕下将侧边栏列表收纳进历史记录弹窗
- 优化顶部导航栏与底部工具栏,解决挤压和堆叠问题
- 体验:生图占位框支持比例自适应
- api/ai.py +3 -2
- services/chatgpt_service.py +10 -4
- services/openai_backend_api.py +5 -2
- web/package-lock.json +0 -0
- web/src/app/image/components/image-composer.tsx +49 -14
- web/src/app/image/components/image-results.tsx +21 -4
- web/src/app/image/components/image-sidebar.tsx +24 -20
- web/src/app/image/page.tsx +93 -14
- web/src/components/top-nav.tsx +12 -12
- web/src/lib/api.ts +4 -2
- web/src/store/image-conversations.ts +3 -0
api/ai.py
CHANGED
|
@@ -15,6 +15,7 @@ class ImageGenerationRequest(BaseModel):
|
|
| 15 |
prompt: str = Field(..., min_length=1)
|
| 16 |
model: str = "gpt-image-2"
|
| 17 |
n: int = Field(default=1, ge=1, le=4)
|
|
|
|
| 18 |
response_format: str = "b64_json"
|
| 19 |
history_disabled: bool = True
|
| 20 |
stream: bool | None = None
|
|
@@ -66,14 +67,14 @@ def create_router(chatgpt_service: ChatGPTService) -> APIRouter:
|
|
| 66 |
return StreamingResponse(
|
| 67 |
sse_json_stream(
|
| 68 |
chatgpt_service.stream_image_generation(
|
| 69 |
-
body.prompt, body.model, body.n, body.response_format, base_url
|
| 70 |
)
|
| 71 |
),
|
| 72 |
media_type="text/event-stream",
|
| 73 |
)
|
| 74 |
try:
|
| 75 |
return await run_in_threadpool(
|
| 76 |
-
chatgpt_service.generate_with_pool, body.prompt, body.model, body.n, body.response_format, base_url
|
| 77 |
)
|
| 78 |
except ImageGenerationError as exc:
|
| 79 |
raise_image_quota_error(exc)
|
|
|
|
| 15 |
prompt: str = Field(..., min_length=1)
|
| 16 |
model: str = "gpt-image-2"
|
| 17 |
n: int = Field(default=1, ge=1, le=4)
|
| 18 |
+
size: str = "1:1"
|
| 19 |
response_format: str = "b64_json"
|
| 20 |
history_disabled: bool = True
|
| 21 |
stream: bool | None = None
|
|
|
|
| 67 |
return StreamingResponse(
|
| 68 |
sse_json_stream(
|
| 69 |
chatgpt_service.stream_image_generation(
|
| 70 |
+
body.prompt, body.model, body.n, body.size, body.response_format, base_url
|
| 71 |
)
|
| 72 |
),
|
| 73 |
media_type="text/event-stream",
|
| 74 |
)
|
| 75 |
try:
|
| 76 |
return await run_in_threadpool(
|
| 77 |
+
chatgpt_service.generate_with_pool, body.prompt, body.model, body.n, body.size, body.response_format, base_url
|
| 78 |
)
|
| 79 |
except ImageGenerationError as exc:
|
| 80 |
raise_image_quota_error(exc)
|
services/chatgpt_service.py
CHANGED
|
@@ -332,7 +332,7 @@ class ChatGPTService:
|
|
| 332 |
image_data, mime_type = image_info
|
| 333 |
image_result = self.edit_with_pool(prompt, [(image_data, "image.png", mime_type)], model, 1)
|
| 334 |
else:
|
| 335 |
-
image_result = self.generate_with_pool(prompt, model, 1)
|
| 336 |
except ImageGenerationError as exc:
|
| 337 |
raise HTTPException(status_code=502, detail={"error": str(exc)}) from exc
|
| 338 |
|
|
@@ -385,7 +385,7 @@ class ChatGPTService:
|
|
| 385 |
image_data, mime_type = image_info
|
| 386 |
stream = self.stream_image_edit(prompt, [(image_data, "image.png", mime_type)], model, 1)
|
| 387 |
else:
|
| 388 |
-
stream = self.stream_image_generation(prompt, model, 1)
|
| 389 |
|
| 390 |
for chunk in stream:
|
| 391 |
data = chunk.get("data")
|
|
@@ -513,6 +513,7 @@ class ChatGPTService:
|
|
| 513 |
index: int,
|
| 514 |
total: int,
|
| 515 |
request_token: str,
|
|
|
|
| 516 |
response_format: str = "b64_json",
|
| 517 |
base_url: str | None = None,
|
| 518 |
images: list[str] | None = None,
|
|
@@ -520,6 +521,7 @@ class ChatGPTService:
|
|
| 520 |
stream = self._new_backend(request_token).stream_image_chat_completions(
|
| 521 |
prompt=prompt,
|
| 522 |
model=model,
|
|
|
|
| 523 |
images=images or None,
|
| 524 |
)
|
| 525 |
for chunk in stream:
|
|
@@ -566,6 +568,7 @@ class ChatGPTService:
|
|
| 566 |
prompt: str,
|
| 567 |
model: str,
|
| 568 |
n: int,
|
|
|
|
| 569 |
response_format: str = "b64_json",
|
| 570 |
base_url: str | None = None,
|
| 571 |
) -> Iterator[dict[str, object]]:
|
|
@@ -598,6 +601,7 @@ class ChatGPTService:
|
|
| 598 |
result = self._format_image_result(self._new_backend(request_token).images_generations(
|
| 599 |
prompt=prompt,
|
| 600 |
model=model,
|
|
|
|
| 601 |
response_format="b64_json",
|
| 602 |
), prompt, response_format, base_url)
|
| 603 |
account = self.account_service.mark_image_result(request_token, success=True)
|
|
@@ -639,11 +643,11 @@ class ChatGPTService:
|
|
| 639 |
if not emitted:
|
| 640 |
raise ImageGenerationError(last_error or "image generation failed")
|
| 641 |
|
| 642 |
-
def generate_with_pool(self, prompt: str, model: str, n: int, response_format: str = "b64_json",
|
| 643 |
base_url: str = None):
|
| 644 |
created = None
|
| 645 |
image_items: list[dict[str, object]] = []
|
| 646 |
-
for result in self._iter_generated_images_with_pool(prompt, model, n, response_format, base_url):
|
| 647 |
if created is None:
|
| 648 |
created = result.get("created")
|
| 649 |
data = result.get("data")
|
|
@@ -659,6 +663,7 @@ class ChatGPTService:
|
|
| 659 |
prompt: str,
|
| 660 |
model: str,
|
| 661 |
n: int,
|
|
|
|
| 662 |
response_format: str = "b64_json",
|
| 663 |
base_url: str | None = None,
|
| 664 |
) -> Iterator[dict[str, object]]:
|
|
@@ -696,6 +701,7 @@ class ChatGPTService:
|
|
| 696 |
index,
|
| 697 |
n,
|
| 698 |
request_token,
|
|
|
|
| 699 |
response_format,
|
| 700 |
base_url,
|
| 701 |
):
|
|
|
|
| 332 |
image_data, mime_type = image_info
|
| 333 |
image_result = self.edit_with_pool(prompt, [(image_data, "image.png", mime_type)], model, 1)
|
| 334 |
else:
|
| 335 |
+
image_result = self.generate_with_pool(prompt, model, 1, size="1:1")
|
| 336 |
except ImageGenerationError as exc:
|
| 337 |
raise HTTPException(status_code=502, detail={"error": str(exc)}) from exc
|
| 338 |
|
|
|
|
| 385 |
image_data, mime_type = image_info
|
| 386 |
stream = self.stream_image_edit(prompt, [(image_data, "image.png", mime_type)], model, 1)
|
| 387 |
else:
|
| 388 |
+
stream = self.stream_image_generation(prompt, model, 1, size="1:1")
|
| 389 |
|
| 390 |
for chunk in stream:
|
| 391 |
data = chunk.get("data")
|
|
|
|
| 513 |
index: int,
|
| 514 |
total: int,
|
| 515 |
request_token: str,
|
| 516 |
+
size: str = "1:1",
|
| 517 |
response_format: str = "b64_json",
|
| 518 |
base_url: str | None = None,
|
| 519 |
images: list[str] | None = None,
|
|
|
|
| 521 |
stream = self._new_backend(request_token).stream_image_chat_completions(
|
| 522 |
prompt=prompt,
|
| 523 |
model=model,
|
| 524 |
+
size=size,
|
| 525 |
images=images or None,
|
| 526 |
)
|
| 527 |
for chunk in stream:
|
|
|
|
| 568 |
prompt: str,
|
| 569 |
model: str,
|
| 570 |
n: int,
|
| 571 |
+
size: str = "1:1",
|
| 572 |
response_format: str = "b64_json",
|
| 573 |
base_url: str | None = None,
|
| 574 |
) -> Iterator[dict[str, object]]:
|
|
|
|
| 601 |
result = self._format_image_result(self._new_backend(request_token).images_generations(
|
| 602 |
prompt=prompt,
|
| 603 |
model=model,
|
| 604 |
+
size=size,
|
| 605 |
response_format="b64_json",
|
| 606 |
), prompt, response_format, base_url)
|
| 607 |
account = self.account_service.mark_image_result(request_token, success=True)
|
|
|
|
| 643 |
if not emitted:
|
| 644 |
raise ImageGenerationError(last_error or "image generation failed")
|
| 645 |
|
| 646 |
+
def generate_with_pool(self, prompt: str, model: str, n: int, size: str = "1:1", response_format: str = "b64_json",
|
| 647 |
base_url: str = None):
|
| 648 |
created = None
|
| 649 |
image_items: list[dict[str, object]] = []
|
| 650 |
+
for result in self._iter_generated_images_with_pool(prompt, model, n, size, response_format, base_url):
|
| 651 |
if created is None:
|
| 652 |
created = result.get("created")
|
| 653 |
data = result.get("data")
|
|
|
|
| 663 |
prompt: str,
|
| 664 |
model: str,
|
| 665 |
n: int,
|
| 666 |
+
size: str = "1:1",
|
| 667 |
response_format: str = "b64_json",
|
| 668 |
base_url: str | None = None,
|
| 669 |
) -> Iterator[dict[str, object]]:
|
|
|
|
| 701 |
index,
|
| 702 |
n,
|
| 703 |
request_token,
|
| 704 |
+
size,
|
| 705 |
response_format,
|
| 706 |
base_url,
|
| 707 |
):
|
services/openai_backend_api.py
CHANGED
|
@@ -297,12 +297,14 @@ class OpenAIBackendAPI:
|
|
| 297 |
"""把标准图片 prompt 和宽高比转成底层图片生成 prompt。"""
|
| 298 |
if not size:
|
| 299 |
return prompt
|
| 300 |
-
if size not in {"1:1", "16:9", "9:16"}:
|
| 301 |
return f"{prompt.strip()}\n\n输出图片,宽高比为 {size}。"
|
| 302 |
hint = {
|
| 303 |
"1:1": "输出为 1:1 正方形构图,主体居中,适合正方形画幅。",
|
| 304 |
"16:9": "输出为 16:9 横屏构图,适合宽画幅展示。",
|
| 305 |
"9:16": "输出为 9:16 竖屏构图,适合竖版画幅展示。",
|
|
|
|
|
|
|
| 306 |
}[size]
|
| 307 |
return f"{prompt.strip()}\n\n{hint}"
|
| 308 |
|
|
@@ -921,6 +923,7 @@ class OpenAIBackendAPI:
|
|
| 921 |
self,
|
| 922 |
prompt: str,
|
| 923 |
model: str = "gpt-image-2",
|
|
|
|
| 924 |
images: Optional[list[str]] = None,
|
| 925 |
) -> Iterator[Dict[str, Any]]:
|
| 926 |
if not self.access_token:
|
|
@@ -937,7 +940,7 @@ class OpenAIBackendAPI:
|
|
| 937 |
references = [self._upload_image(image, f"image_{idx}.png") for idx, image in enumerate(images or [], start=1)]
|
| 938 |
self._bootstrap()
|
| 939 |
requirements = self._get_auth_chat_requirements()
|
| 940 |
-
final_prompt = self._build_image_prompt(prompt,
|
| 941 |
conduit_token = self._prepare_image_conversation(final_prompt, requirements, model)
|
| 942 |
sse = self._start_image_generation(final_prompt, requirements, conduit_token, model, references)
|
| 943 |
try:
|
|
|
|
| 297 |
"""把标准图片 prompt 和宽高比转成底层图片生成 prompt。"""
|
| 298 |
if not size:
|
| 299 |
return prompt
|
| 300 |
+
if size not in {"1:1", "16:9", "9:16", "4:3", "3:4"}:
|
| 301 |
return f"{prompt.strip()}\n\n输出图片,宽高比为 {size}。"
|
| 302 |
hint = {
|
| 303 |
"1:1": "输出为 1:1 正方形构图,主体居中,适合正方形画幅。",
|
| 304 |
"16:9": "输出为 16:9 横屏构图,适合宽画幅展示。",
|
| 305 |
"9:16": "输出为 9:16 竖屏构图,适合竖版画幅展示。",
|
| 306 |
+
"4:3": "输出为 4:3 比例,兼顾宽度与高度,适合展示画面细节。",
|
| 307 |
+
"3:4": "输出为 3:4 比例,纵向构图,适合人物肖像或竖向场景。",
|
| 308 |
}[size]
|
| 309 |
return f"{prompt.strip()}\n\n{hint}"
|
| 310 |
|
|
|
|
| 923 |
self,
|
| 924 |
prompt: str,
|
| 925 |
model: str = "gpt-image-2",
|
| 926 |
+
size: str = "1:1",
|
| 927 |
images: Optional[list[str]] = None,
|
| 928 |
) -> Iterator[Dict[str, Any]]:
|
| 929 |
if not self.access_token:
|
|
|
|
| 940 |
references = [self._upload_image(image, f"image_{idx}.png") for idx, image in enumerate(images or [], start=1)]
|
| 941 |
self._bootstrap()
|
| 942 |
requirements = self._get_auth_chat_requirements()
|
| 943 |
+
final_prompt = self._build_image_prompt(prompt, size)
|
| 944 |
conduit_token = self._prepare_image_conversation(final_prompt, requirements, model)
|
| 945 |
sse = self._start_image_generation(final_prompt, requirements, conduit_token, model, references)
|
| 946 |
try:
|
web/package-lock.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
web/src/app/image/components/image-composer.tsx
CHANGED
|
@@ -8,11 +8,19 @@ import { Input } from "@/components/ui/input";
|
|
| 8 |
import { Textarea } from "@/components/ui/textarea";
|
| 9 |
import type { ImageConversationMode } from "@/store/image-conversations";
|
| 10 |
import { cn } from "@/lib/utils";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
type ImageComposerProps = {
|
| 13 |
mode: ImageConversationMode;
|
| 14 |
prompt: string;
|
| 15 |
imageCount: string;
|
|
|
|
| 16 |
availableQuota: string;
|
| 17 |
activeTaskCount: number;
|
| 18 |
referenceImages: Array<{ name: string; dataUrl: string }>;
|
|
@@ -21,6 +29,7 @@ type ImageComposerProps = {
|
|
| 21 |
onModeChange: (value: ImageConversationMode) => void;
|
| 22 |
onPromptChange: (value: string) => void;
|
| 23 |
onImageCountChange: (value: string) => void;
|
|
|
|
| 24 |
onSubmit: () => void | Promise<void>;
|
| 25 |
onPickReferenceImage: () => void;
|
| 26 |
onReferenceImageChange: (files: File[]) => void | Promise<void>;
|
|
@@ -31,6 +40,7 @@ export function ImageComposer({
|
|
| 31 |
mode,
|
| 32 |
prompt,
|
| 33 |
imageCount,
|
|
|
|
| 34 |
availableQuota,
|
| 35 |
activeTaskCount,
|
| 36 |
referenceImages,
|
|
@@ -39,6 +49,7 @@ export function ImageComposer({
|
|
| 39 |
onModeChange,
|
| 40 |
onPromptChange,
|
| 41 |
onImageCountChange,
|
|
|
|
| 42 |
onSubmit,
|
| 43 |
onPickReferenceImage,
|
| 44 |
onReferenceImageChange,
|
|
@@ -145,27 +156,30 @@ export function ImageComposer({
|
|
| 145 |
|
| 146 |
<div className="absolute inset-x-0 bottom-0 bg-gradient-to-t from-white via-white/95 to-transparent px-4 pb-4 pt-6 sm:px-6">
|
| 147 |
<div className="flex items-end justify-between gap-3">
|
| 148 |
-
<div className="flex min-w-0 flex-1 flex-wrap items-center gap-3">
|
| 149 |
{mode === "edit" && (
|
| 150 |
<Button
|
| 151 |
type="button"
|
| 152 |
variant="outline"
|
| 153 |
-
className="h-
|
| 154 |
onClick={onPickReferenceImage}
|
| 155 |
>
|
| 156 |
-
<ImagePlus className="size-4" />
|
| 157 |
-
{referenceImages.length > 0 ? "继续添加参考图" : "上传参考图"}
|
|
|
|
| 158 |
</Button>
|
| 159 |
)}
|
| 160 |
-
<div className="rounded-full bg-stone-100 px-
|
|
|
|
|
|
|
| 161 |
{activeTaskCount > 0 && (
|
| 162 |
-
<div className="flex items-center gap-1
|
| 163 |
<LoaderCircle className="size-3 animate-spin" />
|
| 164 |
-
{activeTaskCount} 个任务处理中
|
| 165 |
</div>
|
| 166 |
)}
|
| 167 |
-
<div className="flex items-center gap-
|
| 168 |
-
<span className="text-
|
| 169 |
<Input
|
| 170 |
type="number"
|
| 171 |
min="1"
|
|
@@ -173,10 +187,31 @@ export function ImageComposer({
|
|
| 173 |
step="1"
|
| 174 |
value={imageCount}
|
| 175 |
onChange={(event) => onImageCountChange(event.target.value)}
|
| 176 |
-
className="h-
|
| 177 |
/>
|
| 178 |
</div>
|
| 179 |
-
<div className="flex items-center gap-2">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
<ModeButton active={mode === "generate"} onClick={() => onModeChange("generate")}>
|
| 181 |
文生图
|
| 182 |
</ModeButton>
|
|
@@ -190,10 +225,10 @@ export function ImageComposer({
|
|
| 190 |
type="button"
|
| 191 |
onClick={() => void onSubmit()}
|
| 192 |
disabled={!prompt.trim() || (mode === "edit" && referenceImages.length === 0)}
|
| 193 |
-
className="inline-flex size-
|
| 194 |
aria-label={mode === "edit" ? "编辑图片" : "生成图片"}
|
| 195 |
>
|
| 196 |
-
<ArrowUp className="size-4" />
|
| 197 |
</button>
|
| 198 |
</div>
|
| 199 |
</div>
|
|
@@ -218,7 +253,7 @@ function ModeButton({
|
|
| 218 |
type="button"
|
| 219 |
onClick={onClick}
|
| 220 |
className={cn(
|
| 221 |
-
"rounded-full px-
|
| 222 |
active ? "bg-stone-950 text-white" : "bg-stone-100 text-stone-600 hover:bg-stone-200",
|
| 223 |
)}
|
| 224 |
>
|
|
|
|
| 8 |
import { Textarea } from "@/components/ui/textarea";
|
| 9 |
import type { ImageConversationMode } from "@/store/image-conversations";
|
| 10 |
import { cn } from "@/lib/utils";
|
| 11 |
+
import {
|
| 12 |
+
Select,
|
| 13 |
+
SelectContent,
|
| 14 |
+
SelectItem,
|
| 15 |
+
SelectTrigger,
|
| 16 |
+
SelectValue,
|
| 17 |
+
} from "@/components/ui/select";
|
| 18 |
|
| 19 |
type ImageComposerProps = {
|
| 20 |
mode: ImageConversationMode;
|
| 21 |
prompt: string;
|
| 22 |
imageCount: string;
|
| 23 |
+
imageSize: string;
|
| 24 |
availableQuota: string;
|
| 25 |
activeTaskCount: number;
|
| 26 |
referenceImages: Array<{ name: string; dataUrl: string }>;
|
|
|
|
| 29 |
onModeChange: (value: ImageConversationMode) => void;
|
| 30 |
onPromptChange: (value: string) => void;
|
| 31 |
onImageCountChange: (value: string) => void;
|
| 32 |
+
onImageSizeChange: (value: string) => void;
|
| 33 |
onSubmit: () => void | Promise<void>;
|
| 34 |
onPickReferenceImage: () => void;
|
| 35 |
onReferenceImageChange: (files: File[]) => void | Promise<void>;
|
|
|
|
| 40 |
mode,
|
| 41 |
prompt,
|
| 42 |
imageCount,
|
| 43 |
+
imageSize,
|
| 44 |
availableQuota,
|
| 45 |
activeTaskCount,
|
| 46 |
referenceImages,
|
|
|
|
| 49 |
onModeChange,
|
| 50 |
onPromptChange,
|
| 51 |
onImageCountChange,
|
| 52 |
+
onImageSizeChange,
|
| 53 |
onSubmit,
|
| 54 |
onPickReferenceImage,
|
| 55 |
onReferenceImageChange,
|
|
|
|
| 156 |
|
| 157 |
<div className="absolute inset-x-0 bottom-0 bg-gradient-to-t from-white via-white/95 to-transparent px-4 pb-4 pt-6 sm:px-6">
|
| 158 |
<div className="flex items-end justify-between gap-3">
|
| 159 |
+
<div className="flex min-w-0 flex-1 flex-wrap items-center gap-2 sm:gap-3">
|
| 160 |
{mode === "edit" && (
|
| 161 |
<Button
|
| 162 |
type="button"
|
| 163 |
variant="outline"
|
| 164 |
+
className="h-9 rounded-full border-stone-200 bg-white px-3 text-xs font-medium text-stone-700 shadow-none sm:h-10 sm:px-4 sm:text-sm"
|
| 165 |
onClick={onPickReferenceImage}
|
| 166 |
>
|
| 167 |
+
<ImagePlus className="size-3.5 sm:size-4" />
|
| 168 |
+
<span className="hidden sm:inline">{referenceImages.length > 0 ? "继续添加参考图" : "上传参考图"}</span>
|
| 169 |
+
<span className="sm:hidden">{referenceImages.length > 0 ? "继续" : "上传"}</span>
|
| 170 |
</Button>
|
| 171 |
)}
|
| 172 |
+
<div className="rounded-full bg-stone-100 px-2 py-1 text-[10px] font-medium text-stone-600 sm:px-3 sm:py-2 sm:text-xs">
|
| 173 |
+
<span className="hidden xs:inline">剩余额度 </span>{availableQuota}
|
| 174 |
+
</div>
|
| 175 |
{activeTaskCount > 0 && (
|
| 176 |
+
<div className="flex items-center gap-1 rounded-full bg-amber-50 px-2 py-1 text-[10px] font-medium text-amber-700 sm:gap-1.5 sm:px-3 sm:py-2 sm:text-xs">
|
| 177 |
<LoaderCircle className="size-3 animate-spin" />
|
| 178 |
+
{activeTaskCount}<span className="hidden sm:inline"> 个任务处理中</span>
|
| 179 |
</div>
|
| 180 |
)}
|
| 181 |
+
<div className="flex items-center gap-1.5 rounded-full border border-stone-200 bg-white px-2 py-0.5 sm:gap-2 sm:px-3 sm:py-1">
|
| 182 |
+
<span className="text-[11px] font-medium text-stone-700 sm:text-sm">张数</span>
|
| 183 |
<Input
|
| 184 |
type="number"
|
| 185 |
min="1"
|
|
|
|
| 187 |
step="1"
|
| 188 |
value={imageCount}
|
| 189 |
onChange={(event) => onImageCountChange(event.target.value)}
|
| 190 |
+
className="h-7 w-[40px] border-0 bg-transparent px-0 text-center text-xs font-medium text-stone-700 shadow-none focus-visible:ring-0 sm:h-8 sm:w-[64px] sm:text-sm"
|
| 191 |
/>
|
| 192 |
</div>
|
| 193 |
+
<div className="flex items-center gap-1.5 rounded-full border border-stone-200 bg-white px-2 py-0.5 text-[11px] sm:gap-2 sm:px-3 sm:py-1 sm:text-[13px]">
|
| 194 |
+
<span className="font-medium text-stone-700 sm:text-sm">比例</span>
|
| 195 |
+
<Select value={imageSize} onValueChange={onImageSizeChange}>
|
| 196 |
+
<SelectTrigger className="h-7 border-0 bg-transparent px-0 text-xs font-bold text-stone-700 shadow-none focus-visible:ring-0 w-auto gap-0 sm:h-8 sm:gap-1">
|
| 197 |
+
<div className="flex items-center">
|
| 198 |
+
<span>{imageSize}</span>
|
| 199 |
+
<span className="hidden sm:inline ml-1 font-medium">
|
| 200 |
+
({imageSize === "1:1" ? "正方形" : imageSize.includes("16:9") || imageSize.includes("4:3") ? "横版" : "竖版"})
|
| 201 |
+
</span>
|
| 202 |
+
</div>
|
| 203 |
+
</SelectTrigger>
|
| 204 |
+
<SelectContent>
|
| 205 |
+
<SelectItem value="1:1">1:1 (正方形)</SelectItem>
|
| 206 |
+
<SelectItem value="16:9">16:9 (横版)</SelectItem>
|
| 207 |
+
<SelectItem value="4:3">4:3 (横版)</SelectItem>
|
| 208 |
+
<SelectItem value="3:4">3:4 (竖版)</SelectItem>
|
| 209 |
+
<SelectItem value="9:16">9:16 (竖版)</SelectItem>
|
| 210 |
+
</SelectContent>
|
| 211 |
+
</Select>
|
| 212 |
+
</div>
|
| 213 |
+
|
| 214 |
+
<div className="flex items-center gap-1.5 sm:gap-2">
|
| 215 |
<ModeButton active={mode === "generate"} onClick={() => onModeChange("generate")}>
|
| 216 |
文生图
|
| 217 |
</ModeButton>
|
|
|
|
| 225 |
type="button"
|
| 226 |
onClick={() => void onSubmit()}
|
| 227 |
disabled={!prompt.trim() || (mode === "edit" && referenceImages.length === 0)}
|
| 228 |
+
className="inline-flex size-9 shrink-0 items-center justify-center rounded-full bg-stone-950 text-white transition hover:bg-stone-800 disabled:cursor-not-allowed disabled:bg-stone-300 sm:size-11"
|
| 229 |
aria-label={mode === "edit" ? "编辑图片" : "生成图片"}
|
| 230 |
>
|
| 231 |
+
<ArrowUp className="size-3.5 sm:size-4" />
|
| 232 |
</button>
|
| 233 |
</div>
|
| 234 |
</div>
|
|
|
|
| 253 |
type="button"
|
| 254 |
onClick={onClick}
|
| 255 |
className={cn(
|
| 256 |
+
"rounded-full px-2.5 py-1.5 text-xs font-medium transition sm:px-4 sm:py-2 sm:text-sm",
|
| 257 |
active ? "bg-stone-950 text-white" : "bg-stone-100 text-stone-600 hover:bg-stone-200",
|
| 258 |
)}
|
| 259 |
>
|
web/src/app/image/components/image-results.tsx
CHANGED
|
@@ -3,6 +3,7 @@
|
|
| 3 |
import { Clock3, LoaderCircle, Sparkles } from "lucide-react";
|
| 4 |
|
| 5 |
import { Button } from "@/components/ui/button";
|
|
|
|
| 6 |
import type { ImageConversation, ImageTurnStatus, StoredImage, StoredReferenceImage } from "@/store/image-conversations";
|
| 7 |
|
| 8 |
export type ImageLightboxItem = {
|
|
@@ -161,9 +162,17 @@ export function ImageResults({
|
|
| 161 |
return (
|
| 162 |
<div
|
| 163 |
key={image.id}
|
| 164 |
-
className=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
>
|
| 166 |
-
<div className="flex
|
| 167 |
{image.error || "生成失败"}
|
| 168 |
</div>
|
| 169 |
</div>
|
|
@@ -173,9 +182,17 @@ export function ImageResults({
|
|
| 173 |
return (
|
| 174 |
<div
|
| 175 |
key={image.id}
|
| 176 |
-
className=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
>
|
| 178 |
-
<div className="flex
|
| 179 |
<div className="rounded-full bg-white p-3 shadow-sm">
|
| 180 |
{turn.status === "queued" ? (
|
| 181 |
<Clock3 className="size-5" />
|
|
|
|
| 3 |
import { Clock3, LoaderCircle, Sparkles } from "lucide-react";
|
| 4 |
|
| 5 |
import { Button } from "@/components/ui/button";
|
| 6 |
+
import { cn } from "@/lib/utils";
|
| 7 |
import type { ImageConversation, ImageTurnStatus, StoredImage, StoredReferenceImage } from "@/store/image-conversations";
|
| 8 |
|
| 9 |
export type ImageLightboxItem = {
|
|
|
|
| 162 |
return (
|
| 163 |
<div
|
| 164 |
key={image.id}
|
| 165 |
+
className={cn(
|
| 166 |
+
"break-inside-avoid overflow-hidden border border-rose-200 bg-rose-50",
|
| 167 |
+
turn.size === "1:1" && "aspect-square",
|
| 168 |
+
turn.size === "16:9" && "aspect-video",
|
| 169 |
+
turn.size === "9:16" && "aspect-[9/16]",
|
| 170 |
+
turn.size === "4:3" && "aspect-[4/3]",
|
| 171 |
+
turn.size === "3:4" && "aspect-[3/4]",
|
| 172 |
+
!["1:1", "16:9", "9:16", "4:3", "3:4"].includes(turn.size) && "aspect-square",
|
| 173 |
+
)}
|
| 174 |
>
|
| 175 |
+
<div className="flex h-full items-center justify-center px-6 py-8 text-center text-sm leading-6 text-rose-600">
|
| 176 |
{image.error || "生成失败"}
|
| 177 |
</div>
|
| 178 |
</div>
|
|
|
|
| 182 |
return (
|
| 183 |
<div
|
| 184 |
key={image.id}
|
| 185 |
+
className={cn(
|
| 186 |
+
"break-inside-avoid overflow-hidden border border-stone-200/80 bg-stone-100/80",
|
| 187 |
+
turn.size === "1:1" && "aspect-square",
|
| 188 |
+
turn.size === "16:9" && "aspect-video",
|
| 189 |
+
turn.size === "9:16" && "aspect-[9/16]",
|
| 190 |
+
turn.size === "4:3" && "aspect-[4/3]",
|
| 191 |
+
turn.size === "3:4" && "aspect-[3/4]",
|
| 192 |
+
!["1:1", "16:9", "9:16", "4:3", "3:4"].includes(turn.size) && "aspect-square",
|
| 193 |
+
)}
|
| 194 |
>
|
| 195 |
+
<div className="flex h-full flex-col items-center justify-center gap-3 px-6 py-8 text-center text-stone-500">
|
| 196 |
<div className="rounded-full bg-white p-3 shadow-sm">
|
| 197 |
{turn.status === "queued" ? (
|
| 198 |
<Clock3 className="size-5" />
|
web/src/app/image/components/image-sidebar.tsx
CHANGED
|
@@ -15,6 +15,7 @@ type ImageSidebarProps = {
|
|
| 15 |
onSelectConversation: (id: string) => void;
|
| 16 |
onDeleteConversation: (id: string) => void | Promise<void>;
|
| 17 |
formatConversationTime: (value: string) => string;
|
|
|
|
| 18 |
};
|
| 19 |
|
| 20 |
export function ImageSidebar({
|
|
@@ -26,24 +27,27 @@ export function ImageSidebar({
|
|
| 26 |
onSelectConversation,
|
| 27 |
onDeleteConversation,
|
| 28 |
formatConversationTime,
|
|
|
|
| 29 |
}: ImageSidebarProps) {
|
| 30 |
return (
|
| 31 |
-
<aside className="min-h-0
|
| 32 |
-
<div className="flex h-full min-h-0 flex-col gap-3 py-2">
|
| 33 |
-
|
| 34 |
-
<
|
| 35 |
-
<
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
| 47 |
|
| 48 |
<div className="min-h-0 flex-1 space-y-2 overflow-y-auto pr-1">
|
| 49 |
{isLoadingHistory ? (
|
|
@@ -58,10 +62,10 @@ export function ImageSidebar({
|
|
| 58 |
const active = conversation.id === selectedConversationId;
|
| 59 |
const stats = getImageConversationStats(conversation);
|
| 60 |
return (
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
active
|
| 66 |
? "border-stone-900 bg-black/[0.03] text-stone-950"
|
| 67 |
: "border-transparent text-stone-700 hover:border-stone-300 hover:bg-white/40",
|
|
|
|
| 15 |
onSelectConversation: (id: string) => void;
|
| 16 |
onDeleteConversation: (id: string) => void | Promise<void>;
|
| 17 |
formatConversationTime: (value: string) => string;
|
| 18 |
+
hideActionButtons?: boolean;
|
| 19 |
};
|
| 20 |
|
| 21 |
export function ImageSidebar({
|
|
|
|
| 27 |
onSelectConversation,
|
| 28 |
onDeleteConversation,
|
| 29 |
formatConversationTime,
|
| 30 |
+
hideActionButtons = false,
|
| 31 |
}: ImageSidebarProps) {
|
| 32 |
return (
|
| 33 |
+
<aside className="min-h-0">
|
| 34 |
+
<div className="flex h-full min-h-0 flex-col gap-2 py-1 sm:gap-3 sm:py-2">
|
| 35 |
+
{!hideActionButtons && (
|
| 36 |
+
<div className="flex items-center gap-2">
|
| 37 |
+
<Button className="h-10 flex-1 rounded-xl bg-stone-950 text-white hover:bg-stone-800" onClick={onCreateDraft}>
|
| 38 |
+
<MessageSquarePlus className="size-4" />
|
| 39 |
+
新建对话
|
| 40 |
+
</Button>
|
| 41 |
+
<Button
|
| 42 |
+
variant="outline"
|
| 43 |
+
className="h-10 rounded-xl border-stone-200 bg-white/85 px-3 text-stone-600 hover:bg-white"
|
| 44 |
+
onClick={() => void onClearHistory()}
|
| 45 |
+
disabled={conversations.length === 0}
|
| 46 |
+
>
|
| 47 |
+
<Trash2 className="size-4" />
|
| 48 |
+
</Button>
|
| 49 |
+
</div>
|
| 50 |
+
)}
|
| 51 |
|
| 52 |
<div className="min-h-0 flex-1 space-y-2 overflow-y-auto pr-1">
|
| 53 |
{isLoadingHistory ? (
|
|
|
|
| 62 |
const active = conversation.id === selectedConversationId;
|
| 63 |
const stats = getImageConversationStats(conversation);
|
| 64 |
return (
|
| 65 |
+
<div
|
| 66 |
+
key={conversation.id}
|
| 67 |
+
className={cn(
|
| 68 |
+
"group relative w-full border-l-2 px-3 py-2 text-left transition sm:py-3",
|
| 69 |
active
|
| 70 |
? "border-stone-900 bg-black/[0.03] text-stone-950"
|
| 71 |
: "border-transparent text-stone-700 hover:border-stone-300 hover:bg-white/40",
|
web/src/app/image/page.tsx
CHANGED
|
@@ -1,12 +1,20 @@
|
|
| 1 |
"use client";
|
| 2 |
|
| 3 |
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
|
|
| 4 |
import { toast } from "sonner";
|
| 5 |
|
| 6 |
import { ImageComposer } from "@/app/image/components/image-composer";
|
| 7 |
import { ImageResults, type ImageLightboxItem } from "@/app/image/components/image-results";
|
| 8 |
import { ImageSidebar } from "@/app/image/components/image-sidebar";
|
| 9 |
import { ImageLightbox } from "@/components/image-lightbox";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
import { editImage, fetchAccounts, generateImage, type Account } from "@/lib/api";
|
| 11 |
import {
|
| 12 |
clearImageConversations,
|
|
@@ -23,6 +31,7 @@ import {
|
|
| 23 |
} from "@/store/image-conversations";
|
| 24 |
|
| 25 |
const ACTIVE_CONVERSATION_STORAGE_KEY = "chatgpt2api:image_active_conversation_id";
|
|
|
|
| 26 |
const activeConversationQueueIds = new Set<string>();
|
| 27 |
|
| 28 |
function buildConversationTitle(prompt: string) {
|
|
@@ -171,6 +180,8 @@ export default function ImagePage() {
|
|
| 171 |
const [imagePrompt, setImagePrompt] = useState("");
|
| 172 |
const [imageCount, setImageCount] = useState("1");
|
| 173 |
const [imageMode, setImageMode] = useState<ImageConversationMode>("generate");
|
|
|
|
|
|
|
| 174 |
const [referenceImageFiles, setReferenceImageFiles] = useState<File[]>([]);
|
| 175 |
const [referenceImages, setReferenceImages] = useState<StoredReferenceImage[]>([]);
|
| 176 |
const [conversations, setConversations] = useState<ImageConversation[]>([]);
|
|
@@ -204,6 +215,11 @@ export default function ImagePage() {
|
|
| 204 |
|
| 205 |
const loadHistory = async () => {
|
| 206 |
try {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
const items = await listImageConversations();
|
| 208 |
const normalizedItems = await recoverConversationHistory(items);
|
| 209 |
if (cancelled) {
|
|
@@ -284,6 +300,16 @@ export default function ImagePage() {
|
|
| 284 |
}
|
| 285 |
}, [selectedConversationId]);
|
| 286 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
useEffect(() => {
|
| 288 |
if (selectedConversationId && !conversations.some((conversation) => conversation.id === selectedConversationId)) {
|
| 289 |
setSelectedConversationId(pickFallbackConversationId(conversations));
|
|
@@ -524,8 +550,8 @@ export default function ImagePage() {
|
|
| 524 |
try {
|
| 525 |
const data =
|
| 526 |
queuedTurn.mode === "edit"
|
| 527 |
-
? await editImage(referenceFiles, queuedTurn.prompt)
|
| 528 |
-
: await generateImage(queuedTurn.prompt);
|
| 529 |
const first = data.data?.[0];
|
| 530 |
if (!first?.b64_json) {
|
| 531 |
throw new Error("未返回图片数据");
|
|
@@ -691,6 +717,7 @@ export default function ImagePage() {
|
|
| 691 |
mode: imageMode,
|
| 692 |
referenceImages: imageMode === "edit" ? referenceImages : [],
|
| 693 |
count: parsedCount,
|
|
|
|
| 694 |
images: Array.from({ length: parsedCount }, (_, index) => ({
|
| 695 |
id: `${turnId}-${index}`,
|
| 696 |
status: "loading" as const,
|
|
@@ -732,18 +759,68 @@ export default function ImagePage() {
|
|
| 732 |
return (
|
| 733 |
<>
|
| 734 |
<section className="mx-auto grid h-[calc(100vh-5rem)] min-h-0 w-full max-w-[1380px] grid-cols-1 gap-3 px-3 pb-6 lg:grid-cols-[240px_minmax(0,1fr)]">
|
| 735 |
-
<
|
| 736 |
-
|
| 737 |
-
|
| 738 |
-
|
| 739 |
-
|
| 740 |
-
|
| 741 |
-
|
| 742 |
-
|
| 743 |
-
|
| 744 |
-
|
| 745 |
-
|
| 746 |
-
<div
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 747 |
<div
|
| 748 |
ref={resultsViewportRef}
|
| 749 |
className="hide-scrollbar min-h-0 flex-1 overflow-y-auto px-2 py-3 sm:px-4 sm:py-4"
|
|
@@ -760,6 +837,7 @@ export default function ImagePage() {
|
|
| 760 |
mode={imageMode}
|
| 761 |
prompt={imagePrompt}
|
| 762 |
imageCount={imageCount}
|
|
|
|
| 763 |
availableQuota={availableQuota}
|
| 764 |
activeTaskCount={activeTaskCount}
|
| 765 |
referenceImages={referenceImages}
|
|
@@ -768,6 +846,7 @@ export default function ImagePage() {
|
|
| 768 |
onModeChange={setImageMode}
|
| 769 |
onPromptChange={setImagePrompt}
|
| 770 |
onImageCountChange={setImageCount}
|
|
|
|
| 771 |
onSubmit={handleSubmit}
|
| 772 |
onPickReferenceImage={() => fileInputRef.current?.click()}
|
| 773 |
onReferenceImageChange={handleReferenceImageChange}
|
|
|
|
| 1 |
"use client";
|
| 2 |
|
| 3 |
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
| 4 |
+
import { History, Plus } from "lucide-react";
|
| 5 |
import { toast } from "sonner";
|
| 6 |
|
| 7 |
import { ImageComposer } from "@/app/image/components/image-composer";
|
| 8 |
import { ImageResults, type ImageLightboxItem } from "@/app/image/components/image-results";
|
| 9 |
import { ImageSidebar } from "@/app/image/components/image-sidebar";
|
| 10 |
import { ImageLightbox } from "@/components/image-lightbox";
|
| 11 |
+
import {
|
| 12 |
+
Dialog,
|
| 13 |
+
DialogContent,
|
| 14 |
+
DialogHeader,
|
| 15 |
+
DialogTitle,
|
| 16 |
+
} from "@/components/ui/dialog";
|
| 17 |
+
import { Button } from "@/components/ui/button";
|
| 18 |
import { editImage, fetchAccounts, generateImage, type Account } from "@/lib/api";
|
| 19 |
import {
|
| 20 |
clearImageConversations,
|
|
|
|
| 31 |
} from "@/store/image-conversations";
|
| 32 |
|
| 33 |
const ACTIVE_CONVERSATION_STORAGE_KEY = "chatgpt2api:image_active_conversation_id";
|
| 34 |
+
const IMAGE_SIZE_STORAGE_KEY = "chatgpt2api:image_last_size";
|
| 35 |
const activeConversationQueueIds = new Set<string>();
|
| 36 |
|
| 37 |
function buildConversationTitle(prompt: string) {
|
|
|
|
| 180 |
const [imagePrompt, setImagePrompt] = useState("");
|
| 181 |
const [imageCount, setImageCount] = useState("1");
|
| 182 |
const [imageMode, setImageMode] = useState<ImageConversationMode>("generate");
|
| 183 |
+
const [imageSize, setImageSize] = useState("1:1");
|
| 184 |
+
const [isHistoryOpen, setIsHistoryOpen] = useState(false);
|
| 185 |
const [referenceImageFiles, setReferenceImageFiles] = useState<File[]>([]);
|
| 186 |
const [referenceImages, setReferenceImages] = useState<StoredReferenceImage[]>([]);
|
| 187 |
const [conversations, setConversations] = useState<ImageConversation[]>([]);
|
|
|
|
| 215 |
|
| 216 |
const loadHistory = async () => {
|
| 217 |
try {
|
| 218 |
+
const storedSize = typeof window !== "undefined" ? window.localStorage.getItem(IMAGE_SIZE_STORAGE_KEY) : null;
|
| 219 |
+
if (storedSize) {
|
| 220 |
+
setImageSize(storedSize);
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
const items = await listImageConversations();
|
| 224 |
const normalizedItems = await recoverConversationHistory(items);
|
| 225 |
if (cancelled) {
|
|
|
|
| 300 |
}
|
| 301 |
}, [selectedConversationId]);
|
| 302 |
|
| 303 |
+
useEffect(() => {
|
| 304 |
+
if (typeof window === "undefined") {
|
| 305 |
+
return;
|
| 306 |
+
}
|
| 307 |
+
|
| 308 |
+
if (imageSize) {
|
| 309 |
+
window.localStorage.setItem(IMAGE_SIZE_STORAGE_KEY, imageSize);
|
| 310 |
+
}
|
| 311 |
+
}, [imageSize]);
|
| 312 |
+
|
| 313 |
useEffect(() => {
|
| 314 |
if (selectedConversationId && !conversations.some((conversation) => conversation.id === selectedConversationId)) {
|
| 315 |
setSelectedConversationId(pickFallbackConversationId(conversations));
|
|
|
|
| 550 |
try {
|
| 551 |
const data =
|
| 552 |
queuedTurn.mode === "edit"
|
| 553 |
+
? await editImage(referenceFiles, queuedTurn.prompt, queuedTurn.model, queuedTurn.size)
|
| 554 |
+
: await generateImage(queuedTurn.prompt, queuedTurn.model, queuedTurn.size);
|
| 555 |
const first = data.data?.[0];
|
| 556 |
if (!first?.b64_json) {
|
| 557 |
throw new Error("未返回图片数据");
|
|
|
|
| 717 |
mode: imageMode,
|
| 718 |
referenceImages: imageMode === "edit" ? referenceImages : [],
|
| 719 |
count: parsedCount,
|
| 720 |
+
size: imageSize,
|
| 721 |
images: Array.from({ length: parsedCount }, (_, index) => ({
|
| 722 |
id: `${turnId}-${index}`,
|
| 723 |
status: "loading" as const,
|
|
|
|
| 759 |
return (
|
| 760 |
<>
|
| 761 |
<section className="mx-auto grid h-[calc(100vh-5rem)] min-h-0 w-full max-w-[1380px] grid-cols-1 gap-3 px-3 pb-6 lg:grid-cols-[240px_minmax(0,1fr)]">
|
| 762 |
+
<div className="hidden h-full min-h-0 border-r border-stone-200/70 pr-3 lg:block">
|
| 763 |
+
<ImageSidebar
|
| 764 |
+
conversations={conversations}
|
| 765 |
+
isLoadingHistory={isLoadingHistory}
|
| 766 |
+
selectedConversationId={selectedConversationId}
|
| 767 |
+
onCreateDraft={handleCreateDraft}
|
| 768 |
+
onClearHistory={handleClearHistory}
|
| 769 |
+
onSelectConversation={setSelectedConversationId}
|
| 770 |
+
onDeleteConversation={handleDeleteConversation}
|
| 771 |
+
formatConversationTime={formatConversationTime}
|
| 772 |
+
/>
|
| 773 |
+
</div>
|
| 774 |
+
|
| 775 |
+
<Dialog open={isHistoryOpen} onOpenChange={setIsHistoryOpen}>
|
| 776 |
+
<DialogContent className="flex h-[80vh] w-[92vw] max-w-[420px] flex-col overflow-hidden rounded-[32px] border-stone-200 bg-white p-0 shadow-2xl">
|
| 777 |
+
<DialogHeader className="px-6 pt-6 pb-2">
|
| 778 |
+
<DialogTitle className="flex items-center gap-2 text-lg font-bold">
|
| 779 |
+
<History className="size-5" />
|
| 780 |
+
历史记录
|
| 781 |
+
</DialogTitle>
|
| 782 |
+
</DialogHeader>
|
| 783 |
+
<div className="min-h-0 flex-1 overflow-y-auto px-4 pb-8">
|
| 784 |
+
<ImageSidebar
|
| 785 |
+
conversations={conversations}
|
| 786 |
+
isLoadingHistory={isLoadingHistory}
|
| 787 |
+
selectedConversationId={selectedConversationId}
|
| 788 |
+
onCreateDraft={() => {
|
| 789 |
+
handleCreateDraft();
|
| 790 |
+
setIsHistoryOpen(false);
|
| 791 |
+
}}
|
| 792 |
+
onClearHistory={handleClearHistory}
|
| 793 |
+
onSelectConversation={(id) => {
|
| 794 |
+
setSelectedConversationId(id);
|
| 795 |
+
setIsHistoryOpen(false);
|
| 796 |
+
}}
|
| 797 |
+
onDeleteConversation={handleDeleteConversation}
|
| 798 |
+
formatConversationTime={formatConversationTime}
|
| 799 |
+
hideActionButtons
|
| 800 |
+
/>
|
| 801 |
+
</div>
|
| 802 |
+
</DialogContent>
|
| 803 |
+
</Dialog>
|
| 804 |
+
|
| 805 |
+
<div className="flex min-h-0 flex-col gap-3 sm:gap-4">
|
| 806 |
+
<div className="flex items-center justify-between gap-3 lg:hidden">
|
| 807 |
+
<Button
|
| 808 |
+
variant="outline"
|
| 809 |
+
className="h-10 flex-1 rounded-2xl border-stone-200 bg-white/85 text-stone-700 shadow-sm"
|
| 810 |
+
onClick={() => setIsHistoryOpen(true)}
|
| 811 |
+
>
|
| 812 |
+
<History className="mr-2 size-4" />
|
| 813 |
+
历史记录 ({conversations.length})
|
| 814 |
+
</Button>
|
| 815 |
+
<Button
|
| 816 |
+
className="h-10 rounded-2xl bg-stone-950 text-white shadow-sm"
|
| 817 |
+
onClick={handleCreateDraft}
|
| 818 |
+
>
|
| 819 |
+
<Plus className="size-4" />
|
| 820 |
+
新建
|
| 821 |
+
</Button>
|
| 822 |
+
</div>
|
| 823 |
+
|
| 824 |
<div
|
| 825 |
ref={resultsViewportRef}
|
| 826 |
className="hide-scrollbar min-h-0 flex-1 overflow-y-auto px-2 py-3 sm:px-4 sm:py-4"
|
|
|
|
| 837 |
mode={imageMode}
|
| 838 |
prompt={imagePrompt}
|
| 839 |
imageCount={imageCount}
|
| 840 |
+
imageSize={imageSize}
|
| 841 |
availableQuota={availableQuota}
|
| 842 |
activeTaskCount={activeTaskCount}
|
| 843 |
referenceImages={referenceImages}
|
|
|
|
| 846 |
onModeChange={setImageMode}
|
| 847 |
onPromptChange={setImagePrompt}
|
| 848 |
onImageCountChange={setImageCount}
|
| 849 |
+
onImageSizeChange={setImageSize}
|
| 850 |
onSubmit={handleSubmit}
|
| 851 |
onPickReferenceImage={() => fileInputRef.current?.click()}
|
| 852 |
onReferenceImageChange={handleReferenceImageChange}
|
web/src/components/top-nav.tsx
CHANGED
|
@@ -28,12 +28,12 @@ export function TopNav() {
|
|
| 28 |
}
|
| 29 |
|
| 30 |
return (
|
| 31 |
-
<header>
|
| 32 |
-
<div className="flex h-12 items-
|
| 33 |
-
<div className="flex
|
| 34 |
<Link
|
| 35 |
href="/image"
|
| 36 |
-
className="py-
|
| 37 |
>
|
| 38 |
chatgpt2api
|
| 39 |
</Link>
|
|
@@ -41,14 +41,14 @@ export function TopNav() {
|
|
| 41 |
href="https://github.com/basketikun/chatgpt2api"
|
| 42 |
target="_blank"
|
| 43 |
rel="noreferrer"
|
| 44 |
-
className="inline-flex items-center gap-1.5 py-
|
| 45 |
aria-label="GitHub repository"
|
| 46 |
>
|
| 47 |
<Github className="size-4" />
|
| 48 |
-
<span>GitHub</span>
|
| 49 |
</a>
|
| 50 |
</div>
|
| 51 |
-
<div className="flex justify-center gap-8">
|
| 52 |
{navItems.map((item) => {
|
| 53 |
const active = pathname === item.href;
|
| 54 |
return (
|
|
@@ -56,23 +56,23 @@ export function TopNav() {
|
|
| 56 |
key={item.href}
|
| 57 |
href={item.href}
|
| 58 |
className={cn(
|
| 59 |
-
"relative py-
|
| 60 |
active ? "font-semibold text-stone-950" : "text-stone-500 hover:text-stone-900",
|
| 61 |
)}
|
| 62 |
>
|
| 63 |
{item.label}
|
| 64 |
-
{active ? <span className="absolute inset-x-0 -bottom-[
|
| 65 |
</Link>
|
| 66 |
);
|
| 67 |
})}
|
| 68 |
</div>
|
| 69 |
-
<div className="flex
|
| 70 |
-
<span className="rounded-md bg-stone-100 px-2 py-1 text-[
|
| 71 |
v{webConfig.appVersion}
|
| 72 |
</span>
|
| 73 |
<button
|
| 74 |
type="button"
|
| 75 |
-
className="py-
|
| 76 |
onClick={() => void handleLogout()}
|
| 77 |
>
|
| 78 |
退出
|
|
|
|
| 28 |
}
|
| 29 |
|
| 30 |
return (
|
| 31 |
+
<header className="border-b border-stone-100/50 bg-white/80 backdrop-blur-md">
|
| 32 |
+
<div className="flex h-12 items-center justify-between px-3 sm:px-6">
|
| 33 |
+
<div className="flex items-center gap-2 sm:gap-3">
|
| 34 |
<Link
|
| 35 |
href="/image"
|
| 36 |
+
className="py-1 text-[14px] font-bold tracking-tight text-stone-950 transition hover:text-stone-700 sm:text-[15px]"
|
| 37 |
>
|
| 38 |
chatgpt2api
|
| 39 |
</Link>
|
|
|
|
| 41 |
href="https://github.com/basketikun/chatgpt2api"
|
| 42 |
target="_blank"
|
| 43 |
rel="noreferrer"
|
| 44 |
+
className="inline-flex items-center gap-1.5 py-1 text-sm text-stone-400 transition hover:text-stone-700"
|
| 45 |
aria-label="GitHub repository"
|
| 46 |
>
|
| 47 |
<Github className="size-4" />
|
| 48 |
+
<span className="hidden md:inline">GitHub</span>
|
| 49 |
</a>
|
| 50 |
</div>
|
| 51 |
+
<div className="flex flex-1 justify-center gap-3 sm:gap-8">
|
| 52 |
{navItems.map((item) => {
|
| 53 |
const active = pathname === item.href;
|
| 54 |
return (
|
|
|
|
| 56 |
key={item.href}
|
| 57 |
href={item.href}
|
| 58 |
className={cn(
|
| 59 |
+
"relative py-1 text-[13px] font-medium transition sm:text-[15px]",
|
| 60 |
active ? "font-semibold text-stone-950" : "text-stone-500 hover:text-stone-900",
|
| 61 |
)}
|
| 62 |
>
|
| 63 |
{item.label}
|
| 64 |
+
{active ? <span className="absolute inset-x-0 -bottom-[1px] h-0.5 bg-stone-950" /> : null}
|
| 65 |
</Link>
|
| 66 |
);
|
| 67 |
})}
|
| 68 |
</div>
|
| 69 |
+
<div className="flex items-center justify-end gap-2 sm:gap-3">
|
| 70 |
+
<span className="hidden rounded-md bg-stone-100 px-2 py-1 text-[10px] font-medium text-stone-500 sm:inline-block sm:text-[11px]">
|
| 71 |
v{webConfig.appVersion}
|
| 72 |
</span>
|
| 73 |
<button
|
| 74 |
type="button"
|
| 75 |
+
className="py-1 text-xs text-stone-400 transition hover:text-stone-700 sm:text-sm"
|
| 76 |
onClick={() => void handleLogout()}
|
| 77 |
>
|
| 78 |
退出
|
web/src/lib/api.ts
CHANGED
|
@@ -111,7 +111,7 @@ export async function updateAccount(
|
|
| 111 |
});
|
| 112 |
}
|
| 113 |
|
| 114 |
-
export async function generateImage(prompt: string, model?: ImageModel) {
|
| 115 |
return httpRequest<{ created: number; data: Array<{ b64_json: string; revised_prompt?: string }> }>(
|
| 116 |
"/v1/images/generations",
|
| 117 |
{
|
|
@@ -119,6 +119,7 @@ export async function generateImage(prompt: string, model?: ImageModel) {
|
|
| 119 |
body: {
|
| 120 |
prompt,
|
| 121 |
...(model ? { model } : {}),
|
|
|
|
| 122 |
n: 1,
|
| 123 |
response_format: "b64_json",
|
| 124 |
},
|
|
@@ -126,7 +127,7 @@ export async function generateImage(prompt: string, model?: ImageModel) {
|
|
| 126 |
);
|
| 127 |
}
|
| 128 |
|
| 129 |
-
export async function editImage(files: File | File[], prompt: string, model?: ImageModel) {
|
| 130 |
const formData = new FormData();
|
| 131 |
const uploadFiles = Array.isArray(files) ? files : [files];
|
| 132 |
|
|
@@ -137,6 +138,7 @@ export async function editImage(files: File | File[], prompt: string, model?: Im
|
|
| 137 |
if (model) {
|
| 138 |
formData.append("model", model);
|
| 139 |
}
|
|
|
|
| 140 |
formData.append("n", "1");
|
| 141 |
|
| 142 |
return httpRequest<{ created: number; data: Array<{ b64_json: string; revised_prompt?: string }> }>(
|
|
|
|
| 111 |
});
|
| 112 |
}
|
| 113 |
|
| 114 |
+
export async function generateImage(prompt: string, model?: ImageModel, size: string = "1:1") {
|
| 115 |
return httpRequest<{ created: number; data: Array<{ b64_json: string; revised_prompt?: string }> }>(
|
| 116 |
"/v1/images/generations",
|
| 117 |
{
|
|
|
|
| 119 |
body: {
|
| 120 |
prompt,
|
| 121 |
...(model ? { model } : {}),
|
| 122 |
+
size,
|
| 123 |
n: 1,
|
| 124 |
response_format: "b64_json",
|
| 125 |
},
|
|
|
|
| 127 |
);
|
| 128 |
}
|
| 129 |
|
| 130 |
+
export async function editImage(files: File | File[], prompt: string, model?: ImageModel, size: string = "1:1") {
|
| 131 |
const formData = new FormData();
|
| 132 |
const uploadFiles = Array.isArray(files) ? files : [files];
|
| 133 |
|
|
|
|
| 138 |
if (model) {
|
| 139 |
formData.append("model", model);
|
| 140 |
}
|
| 141 |
+
formData.append("size", size);
|
| 142 |
formData.append("n", "1");
|
| 143 |
|
| 144 |
return httpRequest<{ created: number; data: Array<{ b64_json: string; revised_prompt?: string }> }>(
|
web/src/store/image-conversations.ts
CHANGED
|
@@ -28,6 +28,7 @@ export type ImageTurn = {
|
|
| 28 |
mode: ImageConversationMode;
|
| 29 |
referenceImages: StoredReferenceImage[];
|
| 30 |
count: number;
|
|
|
|
| 31 |
images: StoredImage[];
|
| 32 |
createdAt: string;
|
| 33 |
status: ImageTurnStatus;
|
|
@@ -123,6 +124,7 @@ function normalizeTurn(turn: ImageTurn & Record<string, unknown>): ImageTurn {
|
|
| 123 |
mode: turn.mode === "edit" ? "edit" : "generate",
|
| 124 |
referenceImages: getLegacyReferenceImages(turn),
|
| 125 |
count: Math.max(1, Number(turn.count || normalizedImages.length || 1)),
|
|
|
|
| 126 |
images: normalizedImages,
|
| 127 |
createdAt: String(turn.createdAt || new Date().toISOString()),
|
| 128 |
status:
|
|
@@ -147,6 +149,7 @@ function normalizeConversation(conversation: ImageConversation & Record<string,
|
|
| 147 |
mode: conversation.mode === "edit" ? "edit" : "generate",
|
| 148 |
referenceImages: getLegacyReferenceImages(conversation),
|
| 149 |
count: Number(conversation.count || 1),
|
|
|
|
| 150 |
images: Array.isArray(conversation.images) ? (conversation.images as StoredImage[]) : [],
|
| 151 |
createdAt: String(conversation.createdAt || new Date().toISOString()),
|
| 152 |
status:
|
|
|
|
| 28 |
mode: ImageConversationMode;
|
| 29 |
referenceImages: StoredReferenceImage[];
|
| 30 |
count: number;
|
| 31 |
+
size: string;
|
| 32 |
images: StoredImage[];
|
| 33 |
createdAt: string;
|
| 34 |
status: ImageTurnStatus;
|
|
|
|
| 124 |
mode: turn.mode === "edit" ? "edit" : "generate",
|
| 125 |
referenceImages: getLegacyReferenceImages(turn),
|
| 126 |
count: Math.max(1, Number(turn.count || normalizedImages.length || 1)),
|
| 127 |
+
size: String(turn.size || "1:1"),
|
| 128 |
images: normalizedImages,
|
| 129 |
createdAt: String(turn.createdAt || new Date().toISOString()),
|
| 130 |
status:
|
|
|
|
| 149 |
mode: conversation.mode === "edit" ? "edit" : "generate",
|
| 150 |
referenceImages: getLegacyReferenceImages(conversation),
|
| 151 |
count: Number(conversation.count || 1),
|
| 152 |
+
size: String(conversation.size || "1:1"),
|
| 153 |
images: Array.isArray(conversation.images) ? (conversation.images as StoredImage[]) : [],
|
| 154 |
createdAt: String(conversation.createdAt || new Date().toISOString()),
|
| 155 |
status:
|