dsh-plugin-image-workbench 1.1.5 → 1.1.6

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.
Files changed (172) hide show
  1. package/LICENSE +21 -21
  2. package/README.en.md +5 -3
  3. package/README.md +7 -3
  4. package/client.js +241 -241
  5. package/cordis.patch.yml +14 -14
  6. package/host.js +51 -0
  7. package/install.mjs +333 -387
  8. package/package.json +3 -2
  9. package/scripts/fetch-style-images.mjs +104 -0
  10. package/scripts/vendor-snapshot.py +154 -154
  11. package/server/access.py +26 -26
  12. package/server/app.py +1329 -1329
  13. package/server/catalog.py +147 -147
  14. package/server/composer.py +391 -391
  15. package/server/drafts.py +158 -158
  16. package/server/history.py +575 -575
  17. package/server/imaging.py +172 -172
  18. package/server/pool.py +1335 -1335
  19. package/server/pool_admin.py +921 -921
  20. package/server/pool_service.py +1073 -1073
  21. package/server/requirements.txt +6 -6
  22. package/server/smoke_check.py +402 -402
  23. package/server/static/access.html +202 -202
  24. package/vendor/chatgpt2api/source/.env.example +82 -82
  25. package/vendor/chatgpt2api/source/.python-version +1 -1
  26. package/vendor/chatgpt2api/source/LICENSE +21 -21
  27. package/vendor/chatgpt2api/source/README.md +391 -391
  28. package/vendor/chatgpt2api/source/VERSION +1 -1
  29. package/vendor/chatgpt2api/source/api/__init__.py +2 -2
  30. package/vendor/chatgpt2api/source/api/accounts.py +537 -537
  31. package/vendor/chatgpt2api/source/api/ai.py +217 -217
  32. package/vendor/chatgpt2api/source/api/app.py +62 -62
  33. package/vendor/chatgpt2api/source/api/errors.py +46 -46
  34. package/vendor/chatgpt2api/source/api/image_inputs.py +309 -309
  35. package/vendor/chatgpt2api/source/api/image_tasks.py +120 -120
  36. package/vendor/chatgpt2api/source/api/support.py +134 -134
  37. package/vendor/chatgpt2api/source/api/system.py +365 -365
  38. package/vendor/chatgpt2api/source/assets/kunrouai.svg +50 -50
  39. package/vendor/chatgpt2api/source/config.default.json +108 -108
  40. package/vendor/chatgpt2api/source/main.py +9 -9
  41. package/vendor/chatgpt2api/source/pyproject.toml +27 -27
  42. package/vendor/chatgpt2api/source/scripts/init_proxy_config.py +186 -186
  43. package/vendor/chatgpt2api/source/scripts/migrate_storage.py +163 -163
  44. package/vendor/chatgpt2api/source/scripts/privoxy-warp.conf +13 -13
  45. package/vendor/chatgpt2api/source/scripts/test_outlook_token_mailbox.py +385 -385
  46. package/vendor/chatgpt2api/source/scripts/test_storage.py +129 -129
  47. package/vendor/chatgpt2api/source/scripts/verify_oauth_refresh.py +92 -92
  48. package/vendor/chatgpt2api/source/services/account_service.py +1757 -1757
  49. package/vendor/chatgpt2api/source/services/auth_service.py +253 -253
  50. package/vendor/chatgpt2api/source/services/backup_service.py +671 -671
  51. package/vendor/chatgpt2api/source/services/config.py +667 -667
  52. package/vendor/chatgpt2api/source/services/content_filter.py +242 -242
  53. package/vendor/chatgpt2api/source/services/cpa_service.py +315 -315
  54. package/vendor/chatgpt2api/source/services/editable_file_task_service.py +260 -260
  55. package/vendor/chatgpt2api/source/services/image_service.py +376 -376
  56. package/vendor/chatgpt2api/source/services/image_storage_service.py +405 -405
  57. package/vendor/chatgpt2api/source/services/image_tags_service.py +76 -76
  58. package/vendor/chatgpt2api/source/services/image_task_service.py +550 -550
  59. package/vendor/chatgpt2api/source/services/log_service.py +337 -337
  60. package/vendor/chatgpt2api/source/services/model_service.py +182 -182
  61. package/vendor/chatgpt2api/source/services/oauth_login_service.py +267 -267
  62. package/vendor/chatgpt2api/source/services/openai_backend_api.py +2763 -2763
  63. package/vendor/chatgpt2api/source/services/openai_oauth.py +32 -32
  64. package/vendor/chatgpt2api/source/services/protocol/__init__.py +2 -2
  65. package/vendor/chatgpt2api/source/services/protocol/anthropic_v1_messages.py +307 -307
  66. package/vendor/chatgpt2api/source/services/protocol/chat_completion_cache.py +238 -238
  67. package/vendor/chatgpt2api/source/services/protocol/conversation.py +1644 -1644
  68. package/vendor/chatgpt2api/source/services/protocol/error_response.py +124 -124
  69. package/vendor/chatgpt2api/source/services/protocol/openai_search.py +19 -19
  70. package/vendor/chatgpt2api/source/services/protocol/openai_v1_chat_complete.py +316 -316
  71. package/vendor/chatgpt2api/source/services/protocol/openai_v1_image_edit.py +86 -86
  72. package/vendor/chatgpt2api/source/services/protocol/openai_v1_image_generations.py +42 -42
  73. package/vendor/chatgpt2api/source/services/protocol/openai_v1_models.py +53 -53
  74. package/vendor/chatgpt2api/source/services/protocol/openai_v1_response.py +453 -453
  75. package/vendor/chatgpt2api/source/services/protocol/web_search_tool.py +160 -160
  76. package/vendor/chatgpt2api/source/services/proxy_service.py +648 -648
  77. package/vendor/chatgpt2api/source/services/storage/__init__.py +5 -5
  78. package/vendor/chatgpt2api/source/services/storage/base.py +38 -38
  79. package/vendor/chatgpt2api/source/services/storage/database_storage.py +194 -194
  80. package/vendor/chatgpt2api/source/services/storage/factory.py +104 -104
  81. package/vendor/chatgpt2api/source/services/storage/git_storage.py +183 -183
  82. package/vendor/chatgpt2api/source/services/storage/json_storage.py +95 -95
  83. package/vendor/chatgpt2api/source/services/sub2api_service.py +531 -531
  84. package/vendor/chatgpt2api/source/utils/helper.py +469 -469
  85. package/vendor/chatgpt2api/source/utils/image_tokens.py +346 -346
  86. package/vendor/chatgpt2api/source/utils/log.py +110 -110
  87. package/vendor/chatgpt2api/source/utils/pkce.py +19 -19
  88. package/vendor/chatgpt2api/source/utils/pow.py +207 -207
  89. package/vendor/chatgpt2api/source/utils/sentinel.py +159 -159
  90. package/vendor/chatgpt2api/source/utils/turnstile.py +184 -184
  91. package/vendor/chatgpt2api/source/uv.lock +904 -904
  92. package/vendor/chatgpt2api/source/web/bun.lock +1501 -1501
  93. package/vendor/chatgpt2api/source/web/components.json +24 -24
  94. package/vendor/chatgpt2api/source/web/eslint.config.mjs +17 -17
  95. package/vendor/chatgpt2api/source/web/next.config.ts +40 -40
  96. package/vendor/chatgpt2api/source/web/package.json +58 -58
  97. package/vendor/chatgpt2api/source/web/postcss.config.mjs +5 -5
  98. package/vendor/chatgpt2api/source/web/src/app/accounts/components/account-import-dialog.tsx +925 -925
  99. package/vendor/chatgpt2api/source/web/src/app/accounts/page.tsx +1307 -1307
  100. package/vendor/chatgpt2api/source/web/src/app/debug/components/chat-panel.tsx +224 -224
  101. package/vendor/chatgpt2api/source/web/src/app/debug/components/editable-file-panel.tsx +444 -444
  102. package/vendor/chatgpt2api/source/web/src/app/debug/components/ppt-panel.tsx +9 -9
  103. package/vendor/chatgpt2api/source/web/src/app/debug/components/psd-panel.tsx +9 -9
  104. package/vendor/chatgpt2api/source/web/src/app/debug/components/search-panel.tsx +172 -172
  105. package/vendor/chatgpt2api/source/web/src/app/debug/components/skill-panel.tsx +169 -169
  106. package/vendor/chatgpt2api/source/web/src/app/debug/components/types.ts +39 -39
  107. package/vendor/chatgpt2api/source/web/src/app/debug/page.tsx +59 -59
  108. package/vendor/chatgpt2api/source/web/src/app/globals.css +230 -230
  109. package/vendor/chatgpt2api/source/web/src/app/image/components/image-composer.tsx +542 -542
  110. package/vendor/chatgpt2api/source/web/src/app/image/components/image-results.tsx +554 -554
  111. package/vendor/chatgpt2api/source/web/src/app/image/components/image-sidebar.tsx +176 -176
  112. package/vendor/chatgpt2api/source/web/src/app/image/page.tsx +1799 -1799
  113. package/vendor/chatgpt2api/source/web/src/app/image-manager/page.tsx +732 -732
  114. package/vendor/chatgpt2api/source/web/src/app/layout.tsx +50 -50
  115. package/vendor/chatgpt2api/source/web/src/app/login/page.tsx +101 -101
  116. package/vendor/chatgpt2api/source/web/src/app/logs/page.tsx +356 -356
  117. package/vendor/chatgpt2api/source/web/src/app/page.tsx +30 -30
  118. package/vendor/chatgpt2api/source/web/src/app/settings/components/api-docs-card.tsx +341 -341
  119. package/vendor/chatgpt2api/source/web/src/app/settings/components/backup-settings-card.tsx +447 -447
  120. package/vendor/chatgpt2api/source/web/src/app/settings/components/base-url-card.tsx +71 -71
  121. package/vendor/chatgpt2api/source/web/src/app/settings/components/config-card.tsx +491 -491
  122. package/vendor/chatgpt2api/source/web/src/app/settings/components/cpa-pool-dialog.tsx +108 -108
  123. package/vendor/chatgpt2api/source/web/src/app/settings/components/cpa-pools-card.tsx +169 -169
  124. package/vendor/chatgpt2api/source/web/src/app/settings/components/import-browser-dialog.tsx +185 -185
  125. package/vendor/chatgpt2api/source/web/src/app/settings/components/proxy-runtime-card.tsx +379 -379
  126. package/vendor/chatgpt2api/source/web/src/app/settings/components/proxy-settings-card.tsx +127 -127
  127. package/vendor/chatgpt2api/source/web/src/app/settings/components/proxy-settings.tsx +200 -200
  128. package/vendor/chatgpt2api/source/web/src/app/settings/components/settings-header.tsx +12 -12
  129. package/vendor/chatgpt2api/source/web/src/app/settings/components/sub2api-connections.tsx +886 -886
  130. package/vendor/chatgpt2api/source/web/src/app/settings/components/third-party-apps-card.tsx +81 -81
  131. package/vendor/chatgpt2api/source/web/src/app/settings/components/user-keys-card.tsx +422 -422
  132. package/vendor/chatgpt2api/source/web/src/app/settings/page.tsx +135 -135
  133. package/vendor/chatgpt2api/source/web/src/app/settings/store.ts +1069 -1069
  134. package/vendor/chatgpt2api/source/web/src/components/date-range-filter.tsx +49 -49
  135. package/vendor/chatgpt2api/source/web/src/components/header-actions.tsx +24 -24
  136. package/vendor/chatgpt2api/source/web/src/components/image-lightbox.tsx +434 -434
  137. package/vendor/chatgpt2api/source/web/src/components/image-thumbnail.tsx +46 -46
  138. package/vendor/chatgpt2api/source/web/src/components/theme-script.tsx +18 -18
  139. package/vendor/chatgpt2api/source/web/src/components/theme-toggle.tsx +14 -14
  140. package/vendor/chatgpt2api/source/web/src/components/top-nav.tsx +263 -263
  141. package/vendor/chatgpt2api/source/web/src/components/ui/animated-theme-toggler.tsx +256 -256
  142. package/vendor/chatgpt2api/source/web/src/components/ui/badge.tsx +41 -41
  143. package/vendor/chatgpt2api/source/web/src/components/ui/button.tsx +59 -59
  144. package/vendor/chatgpt2api/source/web/src/components/ui/calendar.tsx +46 -46
  145. package/vendor/chatgpt2api/source/web/src/components/ui/card.tsx +61 -61
  146. package/vendor/chatgpt2api/source/web/src/components/ui/checkbox.tsx +27 -27
  147. package/vendor/chatgpt2api/source/web/src/components/ui/dia-text-reveal.tsx +267 -267
  148. package/vendor/chatgpt2api/source/web/src/components/ui/dialog.tsx +132 -132
  149. package/vendor/chatgpt2api/source/web/src/components/ui/field.tsx +13 -13
  150. package/vendor/chatgpt2api/source/web/src/components/ui/input.tsx +19 -19
  151. package/vendor/chatgpt2api/source/web/src/components/ui/label.tsx +24 -24
  152. package/vendor/chatgpt2api/source/web/src/components/ui/morphing-text.tsx +149 -149
  153. package/vendor/chatgpt2api/source/web/src/components/ui/popover.tsx +38 -38
  154. package/vendor/chatgpt2api/source/web/src/components/ui/select.tsx +175 -175
  155. package/vendor/chatgpt2api/source/web/src/components/ui/sheet.tsx +143 -143
  156. package/vendor/chatgpt2api/source/web/src/components/ui/table.tsx +29 -29
  157. package/vendor/chatgpt2api/source/web/src/components/ui/tabs.tsx +91 -91
  158. package/vendor/chatgpt2api/source/web/src/components/ui/text-3d-flip.tsx +269 -269
  159. package/vendor/chatgpt2api/source/web/src/components/ui/textarea.tsx +18 -18
  160. package/vendor/chatgpt2api/source/web/src/components/version-release-dialog.tsx +127 -127
  161. package/vendor/chatgpt2api/source/web/src/constants/common-env.ts +6 -6
  162. package/vendor/chatgpt2api/source/web/src/hooks/use-version-check.ts +85 -85
  163. package/vendor/chatgpt2api/source/web/src/lib/api.ts +926 -926
  164. package/vendor/chatgpt2api/source/web/src/lib/auth-session.ts +26 -26
  165. package/vendor/chatgpt2api/source/web/src/lib/release.ts +25 -25
  166. package/vendor/chatgpt2api/source/web/src/lib/request.ts +93 -93
  167. package/vendor/chatgpt2api/source/web/src/lib/use-auth-guard.ts +89 -89
  168. package/vendor/chatgpt2api/source/web/src/lib/utils.ts +6 -6
  169. package/vendor/chatgpt2api/source/web/src/store/auth.ts +112 -112
  170. package/vendor/chatgpt2api/source/web/src/store/editable-file-history.ts +33 -33
  171. package/vendor/chatgpt2api/source/web/src/store/image-conversations.ts +318 -318
  172. package/vendor/chatgpt2api/source/web/tsconfig.json +42 -42
@@ -1,542 +1,542 @@
1
- "use client";
2
- import { ArrowUp, ChevronDown, ImagePlus, Info, LoaderCircle, RectangleHorizontal, RectangleVertical, Square, X } from "lucide-react";
3
- import { useEffect, useMemo, useRef, useState, type ClipboardEvent, type DragEvent, type RefObject } from "react";
4
-
5
- import { ImageLightbox } from "@/components/image-lightbox";
6
- import { Button } from "@/components/ui/button";
7
- import { Input } from "@/components/ui/input";
8
- import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
9
- import { Textarea } from "@/components/ui/textarea";
10
- import type { ImageModel } from "@/lib/api";
11
- import { cn } from "@/lib/utils";
12
-
13
- type ImageComposerProps = {
14
- prompt: string;
15
- imageCount: string;
16
- imageRatio: string;
17
- imageTier: string;
18
- imageWidth: string;
19
- imageHeight: string;
20
- imageQuality: string;
21
- imageModel: ImageModel;
22
- imageModels: ImageModel[];
23
- availableQuota: string;
24
- activeTaskCount: number;
25
- referenceImages: Array<{ name: string; dataUrl: string }>;
26
- textareaRef: RefObject<HTMLTextAreaElement | null>;
27
- fileInputRef: RefObject<HTMLInputElement | null>;
28
- onPromptChange: (value: string) => void;
29
- onImageCountChange: (value: string) => void;
30
- onImageRatioChange: (value: string) => void;
31
- onImageTierChange: (value: string) => void;
32
- onImageWidthChange: (value: string) => void;
33
- onImageHeightChange: (value: string) => void;
34
- onImageQualityChange: (value: string) => void;
35
- onImageModelChange: (value: ImageModel) => void;
36
- onSubmit: () => void | Promise<void>;
37
- onPickReferenceImage: () => void;
38
- onReferenceImageChange: (files: File[]) => void | Promise<void>;
39
- onRemoveReferenceImage: (index: number) => void;
40
- };
41
-
42
- const imageFileNamePattern = /\.(avif|bmp|gif|heic|heif|ico|jpe?g|png|svg|tiff?|webp)$/i;
43
-
44
- function isImageFile(file: File) {
45
- return file.type.startsWith("image/") || (!file.type && imageFileNamePattern.test(file.name));
46
- }
47
-
48
- function hasDraggedImages(dataTransfer: DataTransfer) {
49
- const items = Array.from(dataTransfer.items || []);
50
- if (items.length > 0) {
51
- return items.some((item) => item.kind === "file" && (item.type.startsWith("image/") || !item.type));
52
- }
53
- return Array.from(dataTransfer.files || []).some(isImageFile);
54
- }
55
-
56
- function getDraggedImageFiles(dataTransfer: DataTransfer) {
57
- return Array.from(dataTransfer.files || []).filter(isImageFile);
58
- }
59
-
60
- const qualityOptions = [
61
- { value: "auto", label: "自动" },
62
- { value: "low", label: "低" },
63
- { value: "medium", label: "中" },
64
- { value: "high", label: "高" },
65
- ];
66
- const aspectOptions = [
67
- { ratio: "1:1", tier: "1k", width: "1024", height: "1024", label: "1:1", icon: Square },
68
- { ratio: "2:3", tier: "1k", width: "1024", height: "1536", label: "2:3", icon: RectangleVertical },
69
- { ratio: "3:2", tier: "1k", width: "1536", height: "1024", label: "3:2", icon: RectangleHorizontal },
70
- { ratio: "3:4", tier: "1k", width: "1024", height: "1365", label: "3:4", icon: RectangleVertical },
71
- { ratio: "4:3", tier: "1k", width: "1365", height: "1024", label: "4:3", icon: RectangleHorizontal },
72
- { ratio: "9:16", tier: "1k", width: "1088", height: "1920", label: "9:16", icon: RectangleVertical },
73
- { ratio: "16:9", tier: "1k", width: "1920", height: "1088", label: "16:9", icon: RectangleHorizontal },
74
- { ratio: "1:1", tier: "2k", width: "2048", height: "2048", label: "1:1(2k)", icon: Square },
75
- { ratio: "16:9", tier: "2k", width: "2560", height: "1440", label: "16:9(2k)", icon: RectangleHorizontal },
76
- { ratio: "9:16", tier: "2k", width: "1440", height: "2560", label: "9:16(2k)", icon: RectangleVertical },
77
- { ratio: "16:9", tier: "4k", width: "3840", height: "2160", label: "16:9(4k)", icon: RectangleHorizontal },
78
- { ratio: "9:16", tier: "4k", width: "2160", height: "3840", label: "9:16(4k)", icon: RectangleVertical },
79
- { ratio: "auto", tier: "auto", width: "1024", height: "1024", label: "auto", icon: null },
80
- ];
81
- const countOptions = Array.from({ length: 10 }, (_, index) => String(index + 1));
82
-
83
- export function ImageComposer({
84
- prompt,
85
- imageCount,
86
- imageRatio,
87
- imageTier,
88
- imageWidth,
89
- imageHeight,
90
- imageQuality,
91
- imageModel,
92
- imageModels,
93
- availableQuota,
94
- activeTaskCount,
95
- referenceImages,
96
- textareaRef,
97
- fileInputRef,
98
- onPromptChange,
99
- onImageCountChange,
100
- onImageRatioChange,
101
- onImageTierChange,
102
- onImageWidthChange,
103
- onImageHeightChange,
104
- onImageQualityChange,
105
- onImageModelChange,
106
- onSubmit,
107
- onPickReferenceImage,
108
- onReferenceImageChange,
109
- onRemoveReferenceImage,
110
- }: ImageComposerProps) {
111
- const [lightboxOpen, setLightboxOpen] = useState(false);
112
- const [lightboxIndex, setLightboxIndex] = useState(0);
113
- const [isSizeMenuOpen, setIsSizeMenuOpen] = useState(false);
114
- const [isDraggingImage, setIsDraggingImage] = useState(false);
115
- const [sizeMenuPos, setSizeMenuPos] = useState<{ top: number; left: number }>({ top: 0, left: 0 });
116
- const sizeMenuRef = useRef<HTMLDivElement>(null);
117
- const sizeMenuBtnRef = useRef<HTMLButtonElement>(null);
118
- const lightboxImages = useMemo(
119
- () => referenceImages.map((image, index) => ({ id: `${image.name}-${index}`, src: image.dataUrl })),
120
- [referenceImages],
121
- );
122
- const modelOptions = useMemo(
123
- () => imageModels.map((model) => ({ value: model, label: model })),
124
- [imageModels],
125
- );
126
- const qualityLabel = qualityOptions.find((option) => option.value === imageQuality)?.label || "自动";
127
- const ratioLabel = imageRatio === "auto" ? "auto" : `${imageRatio}(${imageTier})`;
128
- const imageSizeLabel = `${qualityLabel} · ${ratioLabel} · ${imageCount || 1} 张`;
129
- const selectedModelLabel = modelOptions.find((option) => option.value === imageModel)?.label || imageModel;
130
- const isCodexModel = imageModel.toLowerCase().includes("codex");
131
-
132
- useEffect(() => {
133
- if (!isSizeMenuOpen) {
134
- return;
135
- }
136
- const handlePointerDown = (event: MouseEvent) => {
137
- const target = event.target;
138
- if (
139
- target instanceof Element &&
140
- target.closest('[data-slot="select-content"], [data-slot="select-trigger"]')
141
- ) {
142
- return;
143
- }
144
- if (!sizeMenuRef.current?.contains(target as Node)) {
145
- setIsSizeMenuOpen(false);
146
- }
147
- };
148
- window.addEventListener("mousedown", handlePointerDown);
149
- return () => {
150
- window.removeEventListener("mousedown", handlePointerDown);
151
- };
152
- }, [isSizeMenuOpen]);
153
-
154
- const handleTextareaPaste = (event: ClipboardEvent<HTMLTextAreaElement>) => {
155
- const imageFiles = Array.from(event.clipboardData.files).filter((file) => file.type.startsWith("image/"));
156
- if (imageFiles.length === 0) {
157
- return;
158
- }
159
-
160
- event.preventDefault();
161
- void onReferenceImageChange(imageFiles);
162
- };
163
-
164
- const handleComposerDragEnter = (event: DragEvent<HTMLDivElement>) => {
165
- if (!hasDraggedImages(event.dataTransfer)) {
166
- return;
167
- }
168
-
169
- event.preventDefault();
170
- event.dataTransfer.dropEffect = "copy";
171
- setIsSizeMenuOpen(false);
172
- setIsDraggingImage(true);
173
- };
174
-
175
- const handleComposerDragOver = (event: DragEvent<HTMLDivElement>) => {
176
- if (!hasDraggedImages(event.dataTransfer)) {
177
- return;
178
- }
179
-
180
- event.preventDefault();
181
- event.dataTransfer.dropEffect = "copy";
182
- setIsDraggingImage(true);
183
- };
184
-
185
- const handleComposerDragLeave = (event: DragEvent<HTMLDivElement>) => {
186
- const nextTarget = event.relatedTarget;
187
- if (nextTarget instanceof Node && event.currentTarget.contains(nextTarget)) {
188
- return;
189
- }
190
- setIsDraggingImage(false);
191
- };
192
-
193
- const handleComposerDrop = (event: DragEvent<HTMLDivElement>) => {
194
- const imageFiles = getDraggedImageFiles(event.dataTransfer);
195
- if (event.dataTransfer.files.length > 0 || imageFiles.length > 0) {
196
- event.preventDefault();
197
- event.stopPropagation();
198
- }
199
-
200
- setIsDraggingImage(false);
201
- if (imageFiles.length === 0) {
202
- return;
203
- }
204
-
205
- void onReferenceImageChange(imageFiles);
206
- };
207
-
208
- return (
209
- <div className="shrink-0 flex justify-center px-1 sm:px-0">
210
- <div style={{ width: "min(980px, 100%)" }}>
211
- <input
212
- ref={fileInputRef}
213
- type="file"
214
- accept="image/*"
215
- multiple
216
- className="hidden"
217
- onChange={(event) => {
218
- void onReferenceImageChange(Array.from(event.target.files || []));
219
- }}
220
- />
221
-
222
- {referenceImages.length > 0 ? (
223
- <div className="mb-2 flex gap-2 overflow-x-auto px-1 pb-1 sm:mb-3 sm:flex-wrap sm:overflow-visible sm:pb-0">
224
- {referenceImages.map((image, index) => (
225
- <div key={`${image.name}-${index}`} className="relative size-14 shrink-0 sm:size-16">
226
- <button
227
- type="button"
228
- onClick={() => {
229
- setLightboxIndex(index);
230
- setLightboxOpen(true);
231
- }}
232
- className="group size-14 overflow-hidden rounded-2xl border border-stone-200 bg-stone-50 transition hover:border-stone-300 sm:size-16"
233
- aria-label={`预览参考图 ${image.name || index + 1}`}
234
- >
235
- <img
236
- src={image.dataUrl}
237
- alt={image.name || `参考图 ${index + 1}`}
238
- className="h-full w-full object-cover"
239
- />
240
- </button>
241
- <button
242
- type="button"
243
- onClick={(event) => {
244
- event.stopPropagation();
245
- onRemoveReferenceImage(index);
246
- }}
247
- className="absolute -right-1 -top-1 inline-flex size-5 items-center justify-center rounded-full border border-stone-200 bg-white text-stone-500 transition hover:border-stone-300 hover:text-stone-800"
248
- aria-label={`移除参考图 ${image.name || index + 1}`}
249
- >
250
- <X className="size-3" />
251
- </button>
252
- </div>
253
- ))}
254
- </div>
255
- ) : null}
256
-
257
- <div
258
- className={cn(
259
- "overflow-hidden rounded-[24px] border border-stone-200 bg-white shadow-[0_14px_60px_-42px_rgba(15,23,42,0.45)] transition dark:border-white/10 dark:bg-stone-950/80 sm:rounded-[32px] sm:shadow-none",
260
- isDraggingImage && "border-stone-900 bg-stone-50",
261
- )}
262
- >
263
- <div
264
- className="relative cursor-text"
265
- onDragEnter={handleComposerDragEnter}
266
- onDragOver={handleComposerDragOver}
267
- onDragLeave={handleComposerDragLeave}
268
- onDrop={handleComposerDrop}
269
- onClick={() => {
270
- textareaRef.current?.focus();
271
- }}
272
- >
273
- <ImageLightbox
274
- images={lightboxImages}
275
- currentIndex={lightboxIndex}
276
- open={lightboxOpen}
277
- onOpenChange={setLightboxOpen}
278
- onIndexChange={setLightboxIndex}
279
- />
280
- <Textarea
281
- ref={textareaRef}
282
- value={prompt}
283
- onChange={(event) => onPromptChange(event.target.value)}
284
- onPaste={handleTextareaPaste}
285
- placeholder={
286
- referenceImages.length > 0
287
- ? "描述你希望如何修改参考图"
288
- : "输入你想要生成的画面,也可直接粘贴图片"
289
- }
290
- onKeyDown={(event) => {
291
- if (event.key === "Enter" && !event.shiftKey) {
292
- event.preventDefault();
293
- void onSubmit();
294
- }
295
- }}
296
- className="min-h-[82px] resize-none rounded-[24px] border-0 bg-transparent px-4 pt-4 pb-2 text-[15px] leading-6 text-stone-900 shadow-none placeholder:text-stone-400 focus-visible:ring-0 dark:text-stone-100 dark:placeholder:text-stone-500 sm:min-h-[148px] sm:rounded-[32px] sm:px-6 sm:pt-6 sm:pb-20 sm:leading-7"
297
- />
298
- {isDraggingImage ? (
299
- <div className="pointer-events-none absolute inset-0 z-20 flex items-center justify-center rounded-[24px] border-2 border-dashed border-stone-900 bg-white/85 text-sm font-medium text-stone-900 backdrop-blur-[1px] sm:rounded-[32px]">
300
- <div className="flex items-center gap-2 rounded-full bg-stone-950 px-4 py-2 text-white shadow-lg">
301
- <ImagePlus className="size-4" />
302
- <span>松开以上传参考图</span>
303
- </div>
304
- </div>
305
- ) : null}
306
-
307
- <div className="rounded-b-[24px] border-t border-stone-100 bg-white px-3 pb-3 pt-2 dark:border-white/10 dark:bg-stone-950/95 sm:absolute sm:inset-x-0 sm:bottom-0 sm:rounded-b-none sm:border-t-0 sm:bg-gradient-to-t sm:from-white sm:via-white/95 sm:to-transparent sm:px-6 sm:pb-4 sm:pt-6 sm:dark:from-stone-950 sm:dark:via-stone-950/95 sm:dark:to-stone-950/0" onClick={(event) => event.stopPropagation()}>
308
- <div className="flex items-end justify-between gap-2 sm:gap-3">
309
- <div className="hide-scrollbar flex min-w-0 flex-1 flex-nowrap items-center gap-1.5 overflow-x-auto pb-0.5 sm:flex-wrap sm:gap-3 sm:overflow-visible sm:pb-0">
310
- <Button
311
- type="button"
312
- variant="outline"
313
- className="h-9 shrink-0 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"
314
- onClick={onPickReferenceImage}
315
- aria-label={referenceImages.length > 0 ? "添加参考图" : "上传"}
316
- >
317
- <ImagePlus className="size-3.5 sm:size-4" />
318
- <span className="hidden sm:inline">{referenceImages.length > 0 ? "添加参考图" : "上传"}</span>
319
- </Button>
320
- <div className="shrink-0 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">
321
- <span className="hidden sm:inline">剩余额度 </span>{availableQuota}
322
- </div>
323
- {activeTaskCount > 0 && (
324
- <div className="flex shrink-0 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">
325
- <LoaderCircle className="size-3 animate-spin" />
326
- {activeTaskCount}<span className="hidden sm:inline"> 个任务处理中</span>
327
- </div>
328
- )}
329
- <div className="relative flex h-9 min-w-0 shrink items-center rounded-full bg-transparent text-[11px] sm:h-auto sm:shrink-0 sm:text-[13px]">
330
- <button
331
- ref={sizeMenuBtnRef}
332
- type="button"
333
- className="inline-flex h-9 w-fit max-w-[calc(100vw-12rem)] items-center justify-between gap-2 rounded-full bg-stone-100 px-4 text-left text-xs font-semibold text-stone-900 sm:h-10 sm:max-w-none sm:text-sm"
334
- onClick={() => {
335
- if (!isSizeMenuOpen && sizeMenuBtnRef.current) {
336
- const rect = sizeMenuBtnRef.current.getBoundingClientRect();
337
- const menuWidth = Math.min(460, window.innerWidth - 32);
338
- setSizeMenuPos({ top: rect.top - 8, left: Math.max(16, Math.min(rect.left, window.innerWidth - menuWidth - 16)) });
339
- }
340
- setIsSizeMenuOpen((open) => !open);
341
- }}
342
- >
343
- <span className="truncate">{imageSizeLabel}</span>
344
- <ChevronDown className={cn("size-4 shrink-0 opacity-60 transition", isSizeMenuOpen && "rotate-180")} />
345
- </button>
346
- {isSizeMenuOpen ? (
347
- <div
348
- ref={sizeMenuRef}
349
- className="fixed z-[80] max-h-[62dvh] overflow-y-auto rounded-[24px] border border-stone-200/70 bg-white p-4 shadow-[0_30px_90px_-34px_rgba(15,23,42,0.42)] sm:max-h-none sm:overflow-visible"
350
- style={{
351
- top: sizeMenuPos.top,
352
- left: sizeMenuPos.left,
353
- transform: "translateY(-100%)",
354
- width: "min(460px, calc(100vw - 2rem))",
355
- }}
356
- >
357
- <h3 className="mb-3 text-base font-semibold text-stone-950">图像设置</h3>
358
- <div className="mb-3">
359
- <div className="mb-2 text-sm font-medium text-stone-900">模型</div>
360
- <Select
361
- value={imageModel}
362
- onValueChange={(value) => {
363
- onImageModelChange(value as ImageModel);
364
- }}
365
- >
366
- <SelectTrigger className="h-10 rounded-xl border-stone-200 bg-white text-sm shadow-none">
367
- <div className="flex min-w-0 items-center gap-2">
368
- <img
369
- src="/openai.svg"
370
- alt=""
371
- aria-hidden="true"
372
- className="size-4 shrink-0 text-stone-700"
373
- />
374
- <span className="truncate">{selectedModelLabel}</span>
375
- </div>
376
- </SelectTrigger>
377
- <SelectContent className="z-[120]">
378
- {modelOptions.map((option) => (
379
- <SelectItem
380
- key={option.value}
381
- value={option.value}
382
- className="pl-10"
383
- style={{
384
- backgroundImage: "url('/openai.svg')",
385
- backgroundRepeat: "no-repeat",
386
- backgroundPosition: "12px center",
387
- backgroundSize: "16px 16px",
388
- }}
389
- >
390
- {option.label}
391
- </SelectItem>
392
- ))}
393
- </SelectContent>
394
- </Select>
395
- </div>
396
- <div className="mb-3">
397
- <div className="mb-2 text-sm font-medium text-stone-900">质量</div>
398
- <div className="grid grid-cols-4 gap-2">
399
- {qualityOptions.map((option) => {
400
- const active = option.value === imageQuality;
401
- return (
402
- <button
403
- key={option.value}
404
- type="button"
405
- className={cn(
406
- "h-9 cursor-pointer rounded-full border border-stone-200 bg-white text-sm text-stone-800 transition hover:border-stone-300 hover:bg-stone-50",
407
- active && "border-stone-950 bg-white font-medium text-stone-950",
408
- )}
409
- onClick={() => onImageQualityChange(option.value)}
410
- >
411
- {option.label}
412
- </button>
413
- );
414
- })}
415
- </div>
416
- </div>
417
- <div className="mb-3">
418
- <div className="mb-2 flex items-center gap-1.5 text-sm font-medium text-stone-900">
419
- 尺寸 <Info className="size-3.5 text-stone-400" />
420
- </div>
421
- <div className="grid grid-cols-[1fr_auto_1fr] items-center gap-2">
422
- <div className="flex items-center rounded-lg bg-stone-100 px-3 py-1.5 text-sm text-stone-700">
423
- <span className="mr-2 text-stone-500">W</span>
424
- <Input
425
- type="number"
426
- inputMode="numeric"
427
- min="1"
428
- value={imageWidth}
429
- onChange={(event) => onImageWidthChange(event.target.value)}
430
- className="h-7 border-0 bg-transparent px-0 text-sm font-medium text-stone-800 shadow-none focus-visible:ring-0"
431
- />
432
- </div>
433
- <span className="text-stone-400">×</span>
434
- <div className="flex items-center rounded-lg bg-stone-100 px-3 py-1.5 text-sm text-stone-700">
435
- <span className="mr-2 text-stone-500">H</span>
436
- <Input
437
- type="number"
438
- inputMode="numeric"
439
- min="1"
440
- value={imageHeight}
441
- onChange={(event) => onImageHeightChange(event.target.value)}
442
- className="h-7 border-0 bg-transparent px-0 text-sm font-medium text-stone-800 shadow-none focus-visible:ring-0"
443
- />
444
- </div>
445
- </div>
446
- </div>
447
- <div className="mb-3">
448
- <div className="mb-2 flex items-center gap-1.5 text-sm font-medium text-stone-900">
449
- 宽高比 <Info className="size-3.5 text-stone-400" />
450
- </div>
451
- <div className="grid grid-cols-4 gap-2 sm:grid-cols-5">
452
- {aspectOptions.map((option) => {
453
- const active = option.ratio === imageRatio && option.tier === imageTier && option.width === imageWidth && option.height === imageHeight;
454
- const Icon = option.icon;
455
- const disabled = !isCodexModel && (option.tier === "2k" || option.tier === "4k");
456
- return (
457
- <button
458
- key={`${option.ratio}-${option.tier}-${option.label}`}
459
- type="button"
460
- disabled={disabled}
461
- className={cn(
462
- "flex h-[64px] cursor-pointer flex-col items-center justify-center gap-1 rounded-2xl border border-stone-200 bg-white text-sm text-stone-800 transition hover:border-stone-300 hover:bg-stone-50",
463
- active && "border-stone-950",
464
- disabled && "cursor-not-allowed border-stone-100 bg-stone-50 text-stone-300 hover:border-stone-100 hover:bg-stone-50",
465
- )}
466
- onClick={() => {
467
- if (disabled) {
468
- return;
469
- }
470
- onImageRatioChange(option.ratio);
471
- onImageTierChange(option.tier);
472
- onImageWidthChange(option.width);
473
- onImageHeightChange(option.height);
474
- }}
475
- >
476
- {Icon ? (
477
- <>
478
- <Icon className="size-3.5 stroke-[1.8]" />
479
- <span>{option.label}</span>
480
- </>
481
- ) : (
482
- <span>{option.label}</span>
483
- )}
484
- </button>
485
- );
486
- })}
487
- </div>
488
- </div>
489
- <div className="border-t border-stone-100 pt-3">
490
- <div className="mb-2 text-sm font-medium text-stone-900">生成数量</div>
491
- <div className="grid grid-cols-4 gap-2 sm:grid-cols-5">
492
- {countOptions.map((option) => {
493
- const active = imageCount === option;
494
- return (
495
- <button
496
- key={option}
497
- type="button"
498
- className={cn(
499
- "h-9 cursor-pointer rounded-full border border-stone-200 bg-white text-sm text-stone-800 transition hover:border-stone-300 hover:bg-stone-50",
500
- active && "border-stone-950 bg-white font-medium text-stone-950",
501
- )}
502
- onClick={() => onImageCountChange(option)}
503
- >
504
- {option} 张
505
- </button>
506
- );
507
- })}
508
- <Input
509
- type="number"
510
- inputMode="numeric"
511
- min="1"
512
- max="100"
513
- step="1"
514
- value={imageCount}
515
- onChange={(event) => onImageCountChange(event.target.value)}
516
- className="h-9 rounded-full border-stone-200 bg-white px-3 text-center text-sm font-medium text-stone-800 shadow-none focus-visible:ring-0"
517
- />
518
- </div>
519
- </div>
520
- </div>
521
- ) : null}
522
- </div>
523
-
524
- </div>
525
-
526
- <button
527
- type="button"
528
- onClick={() => void onSubmit()}
529
- disabled={!prompt.trim()}
530
- className="inline-flex size-10 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"
531
- aria-label={referenceImages.length > 0 ? "编辑图片" : "生成图片"}
532
- >
533
- <ArrowUp className="size-3.5 sm:size-4" />
534
- </button>
535
- </div>
536
- </div>
537
- </div>
538
- </div>
539
- </div>
540
- </div>
541
- );
542
- }
1
+ "use client";
2
+ import { ArrowUp, ChevronDown, ImagePlus, Info, LoaderCircle, RectangleHorizontal, RectangleVertical, Square, X } from "lucide-react";
3
+ import { useEffect, useMemo, useRef, useState, type ClipboardEvent, type DragEvent, type RefObject } from "react";
4
+
5
+ import { ImageLightbox } from "@/components/image-lightbox";
6
+ import { Button } from "@/components/ui/button";
7
+ import { Input } from "@/components/ui/input";
8
+ import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
9
+ import { Textarea } from "@/components/ui/textarea";
10
+ import type { ImageModel } from "@/lib/api";
11
+ import { cn } from "@/lib/utils";
12
+
13
+ type ImageComposerProps = {
14
+ prompt: string;
15
+ imageCount: string;
16
+ imageRatio: string;
17
+ imageTier: string;
18
+ imageWidth: string;
19
+ imageHeight: string;
20
+ imageQuality: string;
21
+ imageModel: ImageModel;
22
+ imageModels: ImageModel[];
23
+ availableQuota: string;
24
+ activeTaskCount: number;
25
+ referenceImages: Array<{ name: string; dataUrl: string }>;
26
+ textareaRef: RefObject<HTMLTextAreaElement | null>;
27
+ fileInputRef: RefObject<HTMLInputElement | null>;
28
+ onPromptChange: (value: string) => void;
29
+ onImageCountChange: (value: string) => void;
30
+ onImageRatioChange: (value: string) => void;
31
+ onImageTierChange: (value: string) => void;
32
+ onImageWidthChange: (value: string) => void;
33
+ onImageHeightChange: (value: string) => void;
34
+ onImageQualityChange: (value: string) => void;
35
+ onImageModelChange: (value: ImageModel) => void;
36
+ onSubmit: () => void | Promise<void>;
37
+ onPickReferenceImage: () => void;
38
+ onReferenceImageChange: (files: File[]) => void | Promise<void>;
39
+ onRemoveReferenceImage: (index: number) => void;
40
+ };
41
+
42
+ const imageFileNamePattern = /\.(avif|bmp|gif|heic|heif|ico|jpe?g|png|svg|tiff?|webp)$/i;
43
+
44
+ function isImageFile(file: File) {
45
+ return file.type.startsWith("image/") || (!file.type && imageFileNamePattern.test(file.name));
46
+ }
47
+
48
+ function hasDraggedImages(dataTransfer: DataTransfer) {
49
+ const items = Array.from(dataTransfer.items || []);
50
+ if (items.length > 0) {
51
+ return items.some((item) => item.kind === "file" && (item.type.startsWith("image/") || !item.type));
52
+ }
53
+ return Array.from(dataTransfer.files || []).some(isImageFile);
54
+ }
55
+
56
+ function getDraggedImageFiles(dataTransfer: DataTransfer) {
57
+ return Array.from(dataTransfer.files || []).filter(isImageFile);
58
+ }
59
+
60
+ const qualityOptions = [
61
+ { value: "auto", label: "自动" },
62
+ { value: "low", label: "低" },
63
+ { value: "medium", label: "中" },
64
+ { value: "high", label: "高" },
65
+ ];
66
+ const aspectOptions = [
67
+ { ratio: "1:1", tier: "1k", width: "1024", height: "1024", label: "1:1", icon: Square },
68
+ { ratio: "2:3", tier: "1k", width: "1024", height: "1536", label: "2:3", icon: RectangleVertical },
69
+ { ratio: "3:2", tier: "1k", width: "1536", height: "1024", label: "3:2", icon: RectangleHorizontal },
70
+ { ratio: "3:4", tier: "1k", width: "1024", height: "1365", label: "3:4", icon: RectangleVertical },
71
+ { ratio: "4:3", tier: "1k", width: "1365", height: "1024", label: "4:3", icon: RectangleHorizontal },
72
+ { ratio: "9:16", tier: "1k", width: "1088", height: "1920", label: "9:16", icon: RectangleVertical },
73
+ { ratio: "16:9", tier: "1k", width: "1920", height: "1088", label: "16:9", icon: RectangleHorizontal },
74
+ { ratio: "1:1", tier: "2k", width: "2048", height: "2048", label: "1:1(2k)", icon: Square },
75
+ { ratio: "16:9", tier: "2k", width: "2560", height: "1440", label: "16:9(2k)", icon: RectangleHorizontal },
76
+ { ratio: "9:16", tier: "2k", width: "1440", height: "2560", label: "9:16(2k)", icon: RectangleVertical },
77
+ { ratio: "16:9", tier: "4k", width: "3840", height: "2160", label: "16:9(4k)", icon: RectangleHorizontal },
78
+ { ratio: "9:16", tier: "4k", width: "2160", height: "3840", label: "9:16(4k)", icon: RectangleVertical },
79
+ { ratio: "auto", tier: "auto", width: "1024", height: "1024", label: "auto", icon: null },
80
+ ];
81
+ const countOptions = Array.from({ length: 10 }, (_, index) => String(index + 1));
82
+
83
+ export function ImageComposer({
84
+ prompt,
85
+ imageCount,
86
+ imageRatio,
87
+ imageTier,
88
+ imageWidth,
89
+ imageHeight,
90
+ imageQuality,
91
+ imageModel,
92
+ imageModels,
93
+ availableQuota,
94
+ activeTaskCount,
95
+ referenceImages,
96
+ textareaRef,
97
+ fileInputRef,
98
+ onPromptChange,
99
+ onImageCountChange,
100
+ onImageRatioChange,
101
+ onImageTierChange,
102
+ onImageWidthChange,
103
+ onImageHeightChange,
104
+ onImageQualityChange,
105
+ onImageModelChange,
106
+ onSubmit,
107
+ onPickReferenceImage,
108
+ onReferenceImageChange,
109
+ onRemoveReferenceImage,
110
+ }: ImageComposerProps) {
111
+ const [lightboxOpen, setLightboxOpen] = useState(false);
112
+ const [lightboxIndex, setLightboxIndex] = useState(0);
113
+ const [isSizeMenuOpen, setIsSizeMenuOpen] = useState(false);
114
+ const [isDraggingImage, setIsDraggingImage] = useState(false);
115
+ const [sizeMenuPos, setSizeMenuPos] = useState<{ top: number; left: number }>({ top: 0, left: 0 });
116
+ const sizeMenuRef = useRef<HTMLDivElement>(null);
117
+ const sizeMenuBtnRef = useRef<HTMLButtonElement>(null);
118
+ const lightboxImages = useMemo(
119
+ () => referenceImages.map((image, index) => ({ id: `${image.name}-${index}`, src: image.dataUrl })),
120
+ [referenceImages],
121
+ );
122
+ const modelOptions = useMemo(
123
+ () => imageModels.map((model) => ({ value: model, label: model })),
124
+ [imageModels],
125
+ );
126
+ const qualityLabel = qualityOptions.find((option) => option.value === imageQuality)?.label || "自动";
127
+ const ratioLabel = imageRatio === "auto" ? "auto" : `${imageRatio}(${imageTier})`;
128
+ const imageSizeLabel = `${qualityLabel} · ${ratioLabel} · ${imageCount || 1} 张`;
129
+ const selectedModelLabel = modelOptions.find((option) => option.value === imageModel)?.label || imageModel;
130
+ const isCodexModel = imageModel.toLowerCase().includes("codex");
131
+
132
+ useEffect(() => {
133
+ if (!isSizeMenuOpen) {
134
+ return;
135
+ }
136
+ const handlePointerDown = (event: MouseEvent) => {
137
+ const target = event.target;
138
+ if (
139
+ target instanceof Element &&
140
+ target.closest('[data-slot="select-content"], [data-slot="select-trigger"]')
141
+ ) {
142
+ return;
143
+ }
144
+ if (!sizeMenuRef.current?.contains(target as Node)) {
145
+ setIsSizeMenuOpen(false);
146
+ }
147
+ };
148
+ window.addEventListener("mousedown", handlePointerDown);
149
+ return () => {
150
+ window.removeEventListener("mousedown", handlePointerDown);
151
+ };
152
+ }, [isSizeMenuOpen]);
153
+
154
+ const handleTextareaPaste = (event: ClipboardEvent<HTMLTextAreaElement>) => {
155
+ const imageFiles = Array.from(event.clipboardData.files).filter((file) => file.type.startsWith("image/"));
156
+ if (imageFiles.length === 0) {
157
+ return;
158
+ }
159
+
160
+ event.preventDefault();
161
+ void onReferenceImageChange(imageFiles);
162
+ };
163
+
164
+ const handleComposerDragEnter = (event: DragEvent<HTMLDivElement>) => {
165
+ if (!hasDraggedImages(event.dataTransfer)) {
166
+ return;
167
+ }
168
+
169
+ event.preventDefault();
170
+ event.dataTransfer.dropEffect = "copy";
171
+ setIsSizeMenuOpen(false);
172
+ setIsDraggingImage(true);
173
+ };
174
+
175
+ const handleComposerDragOver = (event: DragEvent<HTMLDivElement>) => {
176
+ if (!hasDraggedImages(event.dataTransfer)) {
177
+ return;
178
+ }
179
+
180
+ event.preventDefault();
181
+ event.dataTransfer.dropEffect = "copy";
182
+ setIsDraggingImage(true);
183
+ };
184
+
185
+ const handleComposerDragLeave = (event: DragEvent<HTMLDivElement>) => {
186
+ const nextTarget = event.relatedTarget;
187
+ if (nextTarget instanceof Node && event.currentTarget.contains(nextTarget)) {
188
+ return;
189
+ }
190
+ setIsDraggingImage(false);
191
+ };
192
+
193
+ const handleComposerDrop = (event: DragEvent<HTMLDivElement>) => {
194
+ const imageFiles = getDraggedImageFiles(event.dataTransfer);
195
+ if (event.dataTransfer.files.length > 0 || imageFiles.length > 0) {
196
+ event.preventDefault();
197
+ event.stopPropagation();
198
+ }
199
+
200
+ setIsDraggingImage(false);
201
+ if (imageFiles.length === 0) {
202
+ return;
203
+ }
204
+
205
+ void onReferenceImageChange(imageFiles);
206
+ };
207
+
208
+ return (
209
+ <div className="shrink-0 flex justify-center px-1 sm:px-0">
210
+ <div style={{ width: "min(980px, 100%)" }}>
211
+ <input
212
+ ref={fileInputRef}
213
+ type="file"
214
+ accept="image/*"
215
+ multiple
216
+ className="hidden"
217
+ onChange={(event) => {
218
+ void onReferenceImageChange(Array.from(event.target.files || []));
219
+ }}
220
+ />
221
+
222
+ {referenceImages.length > 0 ? (
223
+ <div className="mb-2 flex gap-2 overflow-x-auto px-1 pb-1 sm:mb-3 sm:flex-wrap sm:overflow-visible sm:pb-0">
224
+ {referenceImages.map((image, index) => (
225
+ <div key={`${image.name}-${index}`} className="relative size-14 shrink-0 sm:size-16">
226
+ <button
227
+ type="button"
228
+ onClick={() => {
229
+ setLightboxIndex(index);
230
+ setLightboxOpen(true);
231
+ }}
232
+ className="group size-14 overflow-hidden rounded-2xl border border-stone-200 bg-stone-50 transition hover:border-stone-300 sm:size-16"
233
+ aria-label={`预览参考图 ${image.name || index + 1}`}
234
+ >
235
+ <img
236
+ src={image.dataUrl}
237
+ alt={image.name || `参考图 ${index + 1}`}
238
+ className="h-full w-full object-cover"
239
+ />
240
+ </button>
241
+ <button
242
+ type="button"
243
+ onClick={(event) => {
244
+ event.stopPropagation();
245
+ onRemoveReferenceImage(index);
246
+ }}
247
+ className="absolute -right-1 -top-1 inline-flex size-5 items-center justify-center rounded-full border border-stone-200 bg-white text-stone-500 transition hover:border-stone-300 hover:text-stone-800"
248
+ aria-label={`移除参考图 ${image.name || index + 1}`}
249
+ >
250
+ <X className="size-3" />
251
+ </button>
252
+ </div>
253
+ ))}
254
+ </div>
255
+ ) : null}
256
+
257
+ <div
258
+ className={cn(
259
+ "overflow-hidden rounded-[24px] border border-stone-200 bg-white shadow-[0_14px_60px_-42px_rgba(15,23,42,0.45)] transition dark:border-white/10 dark:bg-stone-950/80 sm:rounded-[32px] sm:shadow-none",
260
+ isDraggingImage && "border-stone-900 bg-stone-50",
261
+ )}
262
+ >
263
+ <div
264
+ className="relative cursor-text"
265
+ onDragEnter={handleComposerDragEnter}
266
+ onDragOver={handleComposerDragOver}
267
+ onDragLeave={handleComposerDragLeave}
268
+ onDrop={handleComposerDrop}
269
+ onClick={() => {
270
+ textareaRef.current?.focus();
271
+ }}
272
+ >
273
+ <ImageLightbox
274
+ images={lightboxImages}
275
+ currentIndex={lightboxIndex}
276
+ open={lightboxOpen}
277
+ onOpenChange={setLightboxOpen}
278
+ onIndexChange={setLightboxIndex}
279
+ />
280
+ <Textarea
281
+ ref={textareaRef}
282
+ value={prompt}
283
+ onChange={(event) => onPromptChange(event.target.value)}
284
+ onPaste={handleTextareaPaste}
285
+ placeholder={
286
+ referenceImages.length > 0
287
+ ? "描述你希望如何修改参考图"
288
+ : "输入你想要生成的画面,也可直接粘贴图片"
289
+ }
290
+ onKeyDown={(event) => {
291
+ if (event.key === "Enter" && !event.shiftKey) {
292
+ event.preventDefault();
293
+ void onSubmit();
294
+ }
295
+ }}
296
+ className="min-h-[82px] resize-none rounded-[24px] border-0 bg-transparent px-4 pt-4 pb-2 text-[15px] leading-6 text-stone-900 shadow-none placeholder:text-stone-400 focus-visible:ring-0 dark:text-stone-100 dark:placeholder:text-stone-500 sm:min-h-[148px] sm:rounded-[32px] sm:px-6 sm:pt-6 sm:pb-20 sm:leading-7"
297
+ />
298
+ {isDraggingImage ? (
299
+ <div className="pointer-events-none absolute inset-0 z-20 flex items-center justify-center rounded-[24px] border-2 border-dashed border-stone-900 bg-white/85 text-sm font-medium text-stone-900 backdrop-blur-[1px] sm:rounded-[32px]">
300
+ <div className="flex items-center gap-2 rounded-full bg-stone-950 px-4 py-2 text-white shadow-lg">
301
+ <ImagePlus className="size-4" />
302
+ <span>松开以上传参考图</span>
303
+ </div>
304
+ </div>
305
+ ) : null}
306
+
307
+ <div className="rounded-b-[24px] border-t border-stone-100 bg-white px-3 pb-3 pt-2 dark:border-white/10 dark:bg-stone-950/95 sm:absolute sm:inset-x-0 sm:bottom-0 sm:rounded-b-none sm:border-t-0 sm:bg-gradient-to-t sm:from-white sm:via-white/95 sm:to-transparent sm:px-6 sm:pb-4 sm:pt-6 sm:dark:from-stone-950 sm:dark:via-stone-950/95 sm:dark:to-stone-950/0" onClick={(event) => event.stopPropagation()}>
308
+ <div className="flex items-end justify-between gap-2 sm:gap-3">
309
+ <div className="hide-scrollbar flex min-w-0 flex-1 flex-nowrap items-center gap-1.5 overflow-x-auto pb-0.5 sm:flex-wrap sm:gap-3 sm:overflow-visible sm:pb-0">
310
+ <Button
311
+ type="button"
312
+ variant="outline"
313
+ className="h-9 shrink-0 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"
314
+ onClick={onPickReferenceImage}
315
+ aria-label={referenceImages.length > 0 ? "添加参考图" : "上传"}
316
+ >
317
+ <ImagePlus className="size-3.5 sm:size-4" />
318
+ <span className="hidden sm:inline">{referenceImages.length > 0 ? "添加参考图" : "上传"}</span>
319
+ </Button>
320
+ <div className="shrink-0 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">
321
+ <span className="hidden sm:inline">剩余额度 </span>{availableQuota}
322
+ </div>
323
+ {activeTaskCount > 0 && (
324
+ <div className="flex shrink-0 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">
325
+ <LoaderCircle className="size-3 animate-spin" />
326
+ {activeTaskCount}<span className="hidden sm:inline"> 个任务处理中</span>
327
+ </div>
328
+ )}
329
+ <div className="relative flex h-9 min-w-0 shrink items-center rounded-full bg-transparent text-[11px] sm:h-auto sm:shrink-0 sm:text-[13px]">
330
+ <button
331
+ ref={sizeMenuBtnRef}
332
+ type="button"
333
+ className="inline-flex h-9 w-fit max-w-[calc(100vw-12rem)] items-center justify-between gap-2 rounded-full bg-stone-100 px-4 text-left text-xs font-semibold text-stone-900 sm:h-10 sm:max-w-none sm:text-sm"
334
+ onClick={() => {
335
+ if (!isSizeMenuOpen && sizeMenuBtnRef.current) {
336
+ const rect = sizeMenuBtnRef.current.getBoundingClientRect();
337
+ const menuWidth = Math.min(460, window.innerWidth - 32);
338
+ setSizeMenuPos({ top: rect.top - 8, left: Math.max(16, Math.min(rect.left, window.innerWidth - menuWidth - 16)) });
339
+ }
340
+ setIsSizeMenuOpen((open) => !open);
341
+ }}
342
+ >
343
+ <span className="truncate">{imageSizeLabel}</span>
344
+ <ChevronDown className={cn("size-4 shrink-0 opacity-60 transition", isSizeMenuOpen && "rotate-180")} />
345
+ </button>
346
+ {isSizeMenuOpen ? (
347
+ <div
348
+ ref={sizeMenuRef}
349
+ className="fixed z-[80] max-h-[62dvh] overflow-y-auto rounded-[24px] border border-stone-200/70 bg-white p-4 shadow-[0_30px_90px_-34px_rgba(15,23,42,0.42)] sm:max-h-none sm:overflow-visible"
350
+ style={{
351
+ top: sizeMenuPos.top,
352
+ left: sizeMenuPos.left,
353
+ transform: "translateY(-100%)",
354
+ width: "min(460px, calc(100vw - 2rem))",
355
+ }}
356
+ >
357
+ <h3 className="mb-3 text-base font-semibold text-stone-950">图像设置</h3>
358
+ <div className="mb-3">
359
+ <div className="mb-2 text-sm font-medium text-stone-900">模型</div>
360
+ <Select
361
+ value={imageModel}
362
+ onValueChange={(value) => {
363
+ onImageModelChange(value as ImageModel);
364
+ }}
365
+ >
366
+ <SelectTrigger className="h-10 rounded-xl border-stone-200 bg-white text-sm shadow-none">
367
+ <div className="flex min-w-0 items-center gap-2">
368
+ <img
369
+ src="/openai.svg"
370
+ alt=""
371
+ aria-hidden="true"
372
+ className="size-4 shrink-0 text-stone-700"
373
+ />
374
+ <span className="truncate">{selectedModelLabel}</span>
375
+ </div>
376
+ </SelectTrigger>
377
+ <SelectContent className="z-[120]">
378
+ {modelOptions.map((option) => (
379
+ <SelectItem
380
+ key={option.value}
381
+ value={option.value}
382
+ className="pl-10"
383
+ style={{
384
+ backgroundImage: "url('/openai.svg')",
385
+ backgroundRepeat: "no-repeat",
386
+ backgroundPosition: "12px center",
387
+ backgroundSize: "16px 16px",
388
+ }}
389
+ >
390
+ {option.label}
391
+ </SelectItem>
392
+ ))}
393
+ </SelectContent>
394
+ </Select>
395
+ </div>
396
+ <div className="mb-3">
397
+ <div className="mb-2 text-sm font-medium text-stone-900">质量</div>
398
+ <div className="grid grid-cols-4 gap-2">
399
+ {qualityOptions.map((option) => {
400
+ const active = option.value === imageQuality;
401
+ return (
402
+ <button
403
+ key={option.value}
404
+ type="button"
405
+ className={cn(
406
+ "h-9 cursor-pointer rounded-full border border-stone-200 bg-white text-sm text-stone-800 transition hover:border-stone-300 hover:bg-stone-50",
407
+ active && "border-stone-950 bg-white font-medium text-stone-950",
408
+ )}
409
+ onClick={() => onImageQualityChange(option.value)}
410
+ >
411
+ {option.label}
412
+ </button>
413
+ );
414
+ })}
415
+ </div>
416
+ </div>
417
+ <div className="mb-3">
418
+ <div className="mb-2 flex items-center gap-1.5 text-sm font-medium text-stone-900">
419
+ 尺寸 <Info className="size-3.5 text-stone-400" />
420
+ </div>
421
+ <div className="grid grid-cols-[1fr_auto_1fr] items-center gap-2">
422
+ <div className="flex items-center rounded-lg bg-stone-100 px-3 py-1.5 text-sm text-stone-700">
423
+ <span className="mr-2 text-stone-500">W</span>
424
+ <Input
425
+ type="number"
426
+ inputMode="numeric"
427
+ min="1"
428
+ value={imageWidth}
429
+ onChange={(event) => onImageWidthChange(event.target.value)}
430
+ className="h-7 border-0 bg-transparent px-0 text-sm font-medium text-stone-800 shadow-none focus-visible:ring-0"
431
+ />
432
+ </div>
433
+ <span className="text-stone-400">×</span>
434
+ <div className="flex items-center rounded-lg bg-stone-100 px-3 py-1.5 text-sm text-stone-700">
435
+ <span className="mr-2 text-stone-500">H</span>
436
+ <Input
437
+ type="number"
438
+ inputMode="numeric"
439
+ min="1"
440
+ value={imageHeight}
441
+ onChange={(event) => onImageHeightChange(event.target.value)}
442
+ className="h-7 border-0 bg-transparent px-0 text-sm font-medium text-stone-800 shadow-none focus-visible:ring-0"
443
+ />
444
+ </div>
445
+ </div>
446
+ </div>
447
+ <div className="mb-3">
448
+ <div className="mb-2 flex items-center gap-1.5 text-sm font-medium text-stone-900">
449
+ 宽高比 <Info className="size-3.5 text-stone-400" />
450
+ </div>
451
+ <div className="grid grid-cols-4 gap-2 sm:grid-cols-5">
452
+ {aspectOptions.map((option) => {
453
+ const active = option.ratio === imageRatio && option.tier === imageTier && option.width === imageWidth && option.height === imageHeight;
454
+ const Icon = option.icon;
455
+ const disabled = !isCodexModel && (option.tier === "2k" || option.tier === "4k");
456
+ return (
457
+ <button
458
+ key={`${option.ratio}-${option.tier}-${option.label}`}
459
+ type="button"
460
+ disabled={disabled}
461
+ className={cn(
462
+ "flex h-[64px] cursor-pointer flex-col items-center justify-center gap-1 rounded-2xl border border-stone-200 bg-white text-sm text-stone-800 transition hover:border-stone-300 hover:bg-stone-50",
463
+ active && "border-stone-950",
464
+ disabled && "cursor-not-allowed border-stone-100 bg-stone-50 text-stone-300 hover:border-stone-100 hover:bg-stone-50",
465
+ )}
466
+ onClick={() => {
467
+ if (disabled) {
468
+ return;
469
+ }
470
+ onImageRatioChange(option.ratio);
471
+ onImageTierChange(option.tier);
472
+ onImageWidthChange(option.width);
473
+ onImageHeightChange(option.height);
474
+ }}
475
+ >
476
+ {Icon ? (
477
+ <>
478
+ <Icon className="size-3.5 stroke-[1.8]" />
479
+ <span>{option.label}</span>
480
+ </>
481
+ ) : (
482
+ <span>{option.label}</span>
483
+ )}
484
+ </button>
485
+ );
486
+ })}
487
+ </div>
488
+ </div>
489
+ <div className="border-t border-stone-100 pt-3">
490
+ <div className="mb-2 text-sm font-medium text-stone-900">生成数量</div>
491
+ <div className="grid grid-cols-4 gap-2 sm:grid-cols-5">
492
+ {countOptions.map((option) => {
493
+ const active = imageCount === option;
494
+ return (
495
+ <button
496
+ key={option}
497
+ type="button"
498
+ className={cn(
499
+ "h-9 cursor-pointer rounded-full border border-stone-200 bg-white text-sm text-stone-800 transition hover:border-stone-300 hover:bg-stone-50",
500
+ active && "border-stone-950 bg-white font-medium text-stone-950",
501
+ )}
502
+ onClick={() => onImageCountChange(option)}
503
+ >
504
+ {option} 张
505
+ </button>
506
+ );
507
+ })}
508
+ <Input
509
+ type="number"
510
+ inputMode="numeric"
511
+ min="1"
512
+ max="100"
513
+ step="1"
514
+ value={imageCount}
515
+ onChange={(event) => onImageCountChange(event.target.value)}
516
+ className="h-9 rounded-full border-stone-200 bg-white px-3 text-center text-sm font-medium text-stone-800 shadow-none focus-visible:ring-0"
517
+ />
518
+ </div>
519
+ </div>
520
+ </div>
521
+ ) : null}
522
+ </div>
523
+
524
+ </div>
525
+
526
+ <button
527
+ type="button"
528
+ onClick={() => void onSubmit()}
529
+ disabled={!prompt.trim()}
530
+ className="inline-flex size-10 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"
531
+ aria-label={referenceImages.length > 0 ? "编辑图片" : "生成图片"}
532
+ >
533
+ <ArrowUp className="size-3.5 sm:size-4" />
534
+ </button>
535
+ </div>
536
+ </div>
537
+ </div>
538
+ </div>
539
+ </div>
540
+ </div>
541
+ );
542
+ }