dsh-plugin-image-workbench 1.1.5 → 1.1.7

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 (173) 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 +1371 -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/server/static/index.html +15 -0
  25. package/vendor/chatgpt2api/source/.env.example +82 -82
  26. package/vendor/chatgpt2api/source/.python-version +1 -1
  27. package/vendor/chatgpt2api/source/LICENSE +21 -21
  28. package/vendor/chatgpt2api/source/README.md +391 -391
  29. package/vendor/chatgpt2api/source/VERSION +1 -1
  30. package/vendor/chatgpt2api/source/api/__init__.py +2 -2
  31. package/vendor/chatgpt2api/source/api/accounts.py +537 -537
  32. package/vendor/chatgpt2api/source/api/ai.py +217 -217
  33. package/vendor/chatgpt2api/source/api/app.py +62 -62
  34. package/vendor/chatgpt2api/source/api/errors.py +46 -46
  35. package/vendor/chatgpt2api/source/api/image_inputs.py +309 -309
  36. package/vendor/chatgpt2api/source/api/image_tasks.py +120 -120
  37. package/vendor/chatgpt2api/source/api/support.py +134 -134
  38. package/vendor/chatgpt2api/source/api/system.py +365 -365
  39. package/vendor/chatgpt2api/source/assets/kunrouai.svg +50 -50
  40. package/vendor/chatgpt2api/source/config.default.json +108 -108
  41. package/vendor/chatgpt2api/source/main.py +9 -9
  42. package/vendor/chatgpt2api/source/pyproject.toml +27 -27
  43. package/vendor/chatgpt2api/source/scripts/init_proxy_config.py +186 -186
  44. package/vendor/chatgpt2api/source/scripts/migrate_storage.py +163 -163
  45. package/vendor/chatgpt2api/source/scripts/privoxy-warp.conf +13 -13
  46. package/vendor/chatgpt2api/source/scripts/test_outlook_token_mailbox.py +385 -385
  47. package/vendor/chatgpt2api/source/scripts/test_storage.py +129 -129
  48. package/vendor/chatgpt2api/source/scripts/verify_oauth_refresh.py +92 -92
  49. package/vendor/chatgpt2api/source/services/account_service.py +1757 -1757
  50. package/vendor/chatgpt2api/source/services/auth_service.py +253 -253
  51. package/vendor/chatgpt2api/source/services/backup_service.py +671 -671
  52. package/vendor/chatgpt2api/source/services/config.py +667 -667
  53. package/vendor/chatgpt2api/source/services/content_filter.py +242 -242
  54. package/vendor/chatgpt2api/source/services/cpa_service.py +315 -315
  55. package/vendor/chatgpt2api/source/services/editable_file_task_service.py +260 -260
  56. package/vendor/chatgpt2api/source/services/image_service.py +376 -376
  57. package/vendor/chatgpt2api/source/services/image_storage_service.py +405 -405
  58. package/vendor/chatgpt2api/source/services/image_tags_service.py +76 -76
  59. package/vendor/chatgpt2api/source/services/image_task_service.py +550 -550
  60. package/vendor/chatgpt2api/source/services/log_service.py +337 -337
  61. package/vendor/chatgpt2api/source/services/model_service.py +182 -182
  62. package/vendor/chatgpt2api/source/services/oauth_login_service.py +267 -267
  63. package/vendor/chatgpt2api/source/services/openai_backend_api.py +2763 -2763
  64. package/vendor/chatgpt2api/source/services/openai_oauth.py +32 -32
  65. package/vendor/chatgpt2api/source/services/protocol/__init__.py +2 -2
  66. package/vendor/chatgpt2api/source/services/protocol/anthropic_v1_messages.py +307 -307
  67. package/vendor/chatgpt2api/source/services/protocol/chat_completion_cache.py +238 -238
  68. package/vendor/chatgpt2api/source/services/protocol/conversation.py +1644 -1644
  69. package/vendor/chatgpt2api/source/services/protocol/error_response.py +124 -124
  70. package/vendor/chatgpt2api/source/services/protocol/openai_search.py +19 -19
  71. package/vendor/chatgpt2api/source/services/protocol/openai_v1_chat_complete.py +316 -316
  72. package/vendor/chatgpt2api/source/services/protocol/openai_v1_image_edit.py +86 -86
  73. package/vendor/chatgpt2api/source/services/protocol/openai_v1_image_generations.py +42 -42
  74. package/vendor/chatgpt2api/source/services/protocol/openai_v1_models.py +53 -53
  75. package/vendor/chatgpt2api/source/services/protocol/openai_v1_response.py +453 -453
  76. package/vendor/chatgpt2api/source/services/protocol/web_search_tool.py +160 -160
  77. package/vendor/chatgpt2api/source/services/proxy_service.py +648 -648
  78. package/vendor/chatgpt2api/source/services/storage/__init__.py +5 -5
  79. package/vendor/chatgpt2api/source/services/storage/base.py +38 -38
  80. package/vendor/chatgpt2api/source/services/storage/database_storage.py +194 -194
  81. package/vendor/chatgpt2api/source/services/storage/factory.py +104 -104
  82. package/vendor/chatgpt2api/source/services/storage/git_storage.py +183 -183
  83. package/vendor/chatgpt2api/source/services/storage/json_storage.py +95 -95
  84. package/vendor/chatgpt2api/source/services/sub2api_service.py +531 -531
  85. package/vendor/chatgpt2api/source/utils/helper.py +469 -469
  86. package/vendor/chatgpt2api/source/utils/image_tokens.py +346 -346
  87. package/vendor/chatgpt2api/source/utils/log.py +110 -110
  88. package/vendor/chatgpt2api/source/utils/pkce.py +19 -19
  89. package/vendor/chatgpt2api/source/utils/pow.py +207 -207
  90. package/vendor/chatgpt2api/source/utils/sentinel.py +159 -159
  91. package/vendor/chatgpt2api/source/utils/turnstile.py +184 -184
  92. package/vendor/chatgpt2api/source/uv.lock +904 -904
  93. package/vendor/chatgpt2api/source/web/bun.lock +1501 -1501
  94. package/vendor/chatgpt2api/source/web/components.json +24 -24
  95. package/vendor/chatgpt2api/source/web/eslint.config.mjs +17 -17
  96. package/vendor/chatgpt2api/source/web/next.config.ts +40 -40
  97. package/vendor/chatgpt2api/source/web/package.json +58 -58
  98. package/vendor/chatgpt2api/source/web/postcss.config.mjs +5 -5
  99. package/vendor/chatgpt2api/source/web/src/app/accounts/components/account-import-dialog.tsx +925 -925
  100. package/vendor/chatgpt2api/source/web/src/app/accounts/page.tsx +1307 -1307
  101. package/vendor/chatgpt2api/source/web/src/app/debug/components/chat-panel.tsx +224 -224
  102. package/vendor/chatgpt2api/source/web/src/app/debug/components/editable-file-panel.tsx +444 -444
  103. package/vendor/chatgpt2api/source/web/src/app/debug/components/ppt-panel.tsx +9 -9
  104. package/vendor/chatgpt2api/source/web/src/app/debug/components/psd-panel.tsx +9 -9
  105. package/vendor/chatgpt2api/source/web/src/app/debug/components/search-panel.tsx +172 -172
  106. package/vendor/chatgpt2api/source/web/src/app/debug/components/skill-panel.tsx +169 -169
  107. package/vendor/chatgpt2api/source/web/src/app/debug/components/types.ts +39 -39
  108. package/vendor/chatgpt2api/source/web/src/app/debug/page.tsx +59 -59
  109. package/vendor/chatgpt2api/source/web/src/app/globals.css +230 -230
  110. package/vendor/chatgpt2api/source/web/src/app/image/components/image-composer.tsx +542 -542
  111. package/vendor/chatgpt2api/source/web/src/app/image/components/image-results.tsx +554 -554
  112. package/vendor/chatgpt2api/source/web/src/app/image/components/image-sidebar.tsx +176 -176
  113. package/vendor/chatgpt2api/source/web/src/app/image/page.tsx +1799 -1799
  114. package/vendor/chatgpt2api/source/web/src/app/image-manager/page.tsx +732 -732
  115. package/vendor/chatgpt2api/source/web/src/app/layout.tsx +50 -50
  116. package/vendor/chatgpt2api/source/web/src/app/login/page.tsx +101 -101
  117. package/vendor/chatgpt2api/source/web/src/app/logs/page.tsx +356 -356
  118. package/vendor/chatgpt2api/source/web/src/app/page.tsx +30 -30
  119. package/vendor/chatgpt2api/source/web/src/app/settings/components/api-docs-card.tsx +341 -341
  120. package/vendor/chatgpt2api/source/web/src/app/settings/components/backup-settings-card.tsx +447 -447
  121. package/vendor/chatgpt2api/source/web/src/app/settings/components/base-url-card.tsx +71 -71
  122. package/vendor/chatgpt2api/source/web/src/app/settings/components/config-card.tsx +491 -491
  123. package/vendor/chatgpt2api/source/web/src/app/settings/components/cpa-pool-dialog.tsx +108 -108
  124. package/vendor/chatgpt2api/source/web/src/app/settings/components/cpa-pools-card.tsx +169 -169
  125. package/vendor/chatgpt2api/source/web/src/app/settings/components/import-browser-dialog.tsx +185 -185
  126. package/vendor/chatgpt2api/source/web/src/app/settings/components/proxy-runtime-card.tsx +379 -379
  127. package/vendor/chatgpt2api/source/web/src/app/settings/components/proxy-settings-card.tsx +127 -127
  128. package/vendor/chatgpt2api/source/web/src/app/settings/components/proxy-settings.tsx +200 -200
  129. package/vendor/chatgpt2api/source/web/src/app/settings/components/settings-header.tsx +12 -12
  130. package/vendor/chatgpt2api/source/web/src/app/settings/components/sub2api-connections.tsx +886 -886
  131. package/vendor/chatgpt2api/source/web/src/app/settings/components/third-party-apps-card.tsx +81 -81
  132. package/vendor/chatgpt2api/source/web/src/app/settings/components/user-keys-card.tsx +422 -422
  133. package/vendor/chatgpt2api/source/web/src/app/settings/page.tsx +135 -135
  134. package/vendor/chatgpt2api/source/web/src/app/settings/store.ts +1069 -1069
  135. package/vendor/chatgpt2api/source/web/src/components/date-range-filter.tsx +49 -49
  136. package/vendor/chatgpt2api/source/web/src/components/header-actions.tsx +24 -24
  137. package/vendor/chatgpt2api/source/web/src/components/image-lightbox.tsx +434 -434
  138. package/vendor/chatgpt2api/source/web/src/components/image-thumbnail.tsx +46 -46
  139. package/vendor/chatgpt2api/source/web/src/components/theme-script.tsx +18 -18
  140. package/vendor/chatgpt2api/source/web/src/components/theme-toggle.tsx +14 -14
  141. package/vendor/chatgpt2api/source/web/src/components/top-nav.tsx +263 -263
  142. package/vendor/chatgpt2api/source/web/src/components/ui/animated-theme-toggler.tsx +256 -256
  143. package/vendor/chatgpt2api/source/web/src/components/ui/badge.tsx +41 -41
  144. package/vendor/chatgpt2api/source/web/src/components/ui/button.tsx +59 -59
  145. package/vendor/chatgpt2api/source/web/src/components/ui/calendar.tsx +46 -46
  146. package/vendor/chatgpt2api/source/web/src/components/ui/card.tsx +61 -61
  147. package/vendor/chatgpt2api/source/web/src/components/ui/checkbox.tsx +27 -27
  148. package/vendor/chatgpt2api/source/web/src/components/ui/dia-text-reveal.tsx +267 -267
  149. package/vendor/chatgpt2api/source/web/src/components/ui/dialog.tsx +132 -132
  150. package/vendor/chatgpt2api/source/web/src/components/ui/field.tsx +13 -13
  151. package/vendor/chatgpt2api/source/web/src/components/ui/input.tsx +19 -19
  152. package/vendor/chatgpt2api/source/web/src/components/ui/label.tsx +24 -24
  153. package/vendor/chatgpt2api/source/web/src/components/ui/morphing-text.tsx +149 -149
  154. package/vendor/chatgpt2api/source/web/src/components/ui/popover.tsx +38 -38
  155. package/vendor/chatgpt2api/source/web/src/components/ui/select.tsx +175 -175
  156. package/vendor/chatgpt2api/source/web/src/components/ui/sheet.tsx +143 -143
  157. package/vendor/chatgpt2api/source/web/src/components/ui/table.tsx +29 -29
  158. package/vendor/chatgpt2api/source/web/src/components/ui/tabs.tsx +91 -91
  159. package/vendor/chatgpt2api/source/web/src/components/ui/text-3d-flip.tsx +269 -269
  160. package/vendor/chatgpt2api/source/web/src/components/ui/textarea.tsx +18 -18
  161. package/vendor/chatgpt2api/source/web/src/components/version-release-dialog.tsx +127 -127
  162. package/vendor/chatgpt2api/source/web/src/constants/common-env.ts +6 -6
  163. package/vendor/chatgpt2api/source/web/src/hooks/use-version-check.ts +85 -85
  164. package/vendor/chatgpt2api/source/web/src/lib/api.ts +926 -926
  165. package/vendor/chatgpt2api/source/web/src/lib/auth-session.ts +26 -26
  166. package/vendor/chatgpt2api/source/web/src/lib/release.ts +25 -25
  167. package/vendor/chatgpt2api/source/web/src/lib/request.ts +93 -93
  168. package/vendor/chatgpt2api/source/web/src/lib/use-auth-guard.ts +89 -89
  169. package/vendor/chatgpt2api/source/web/src/lib/utils.ts +6 -6
  170. package/vendor/chatgpt2api/source/web/src/store/auth.ts +112 -112
  171. package/vendor/chatgpt2api/source/web/src/store/editable-file-history.ts +33 -33
  172. package/vendor/chatgpt2api/source/web/src/store/image-conversations.ts +318 -318
  173. package/vendor/chatgpt2api/source/web/tsconfig.json +42 -42
@@ -1,224 +1,224 @@
1
- "use client";
2
-
3
- import { useState } from "react";
4
- import { ImagePlus, LoaderCircle, Send, X } from "lucide-react";
5
-
6
- import { Button } from "@/components/ui/button";
7
- import { Input } from "@/components/ui/input";
8
- import { Label } from "@/components/ui/label";
9
- import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
10
- import { Textarea } from "@/components/ui/textarea";
11
- import { httpRequest } from "@/lib/request";
12
-
13
- import { pretty, type ChatCompletionResponse, type ChatContentPart, type ChatMessage } from "./types";
14
-
15
- type SelectedImage = {
16
- id: string;
17
- name: string;
18
- size: number;
19
- url: string;
20
- };
21
-
22
- const MAX_IMAGE_BYTES = 10 * 1024 * 1024;
23
-
24
- function readImage(file: File): Promise<SelectedImage> {
25
- return new Promise((resolve, reject) => {
26
- if (!file.type.startsWith("image/")) {
27
- reject(new Error(`${file.name} 不是图片文件`));
28
- return;
29
- }
30
- if (file.size > MAX_IMAGE_BYTES) {
31
- reject(new Error(`${file.name} 超过 10MB`));
32
- return;
33
- }
34
- const reader = new FileReader();
35
- reader.onload = () => {
36
- const url = String(reader.result || "");
37
- if (!url.startsWith("data:image/")) {
38
- reject(new Error(`${file.name} 读取失败`));
39
- return;
40
- }
41
- resolve({
42
- id: `${file.name}-${file.size}-${file.lastModified}-${Math.random().toString(16).slice(2)}`,
43
- name: file.name,
44
- size: file.size,
45
- url,
46
- });
47
- };
48
- reader.onerror = () => reject(reader.error || new Error(`${file.name} 读取失败`));
49
- reader.readAsDataURL(file);
50
- });
51
- }
52
-
53
- function messageText(message: ChatMessage): string {
54
- if (typeof message.content === "string") {
55
- return message.content;
56
- }
57
- return message.content
58
- .filter((part): part is { type: "text"; text: string } => part.type === "text")
59
- .map((part) => part.text)
60
- .join("");
61
- }
62
-
63
- function messageImages(message: ChatMessage): string[] {
64
- if (!Array.isArray(message.content)) {
65
- return [];
66
- }
67
- return message.content
68
- .filter((part): part is { type: "image_url"; image_url: { url: string } } => part.type === "image_url")
69
- .map((part) => part.image_url.url);
70
- }
71
-
72
- export function ChatPanel() {
73
- const [model, setModel] = useState("auto");
74
- const [reasoningEffort, setReasoningEffort] = useState("");
75
- const [input, setInput] = useState("你好,先记住我的项目叫 chatgpt2api。");
76
- const [selectedImages, setSelectedImages] = useState<SelectedImage[]>([]);
77
- const [messages, setMessages] = useState<ChatMessage[]>([]);
78
- const [raw, setRaw] = useState<ChatCompletionResponse | null>(null);
79
- const [loading, setLoading] = useState(false);
80
- const [error, setError] = useState("");
81
-
82
- const handleImagesChange = async (files: FileList | null) => {
83
- if (!files?.length) return;
84
- setError("");
85
- try {
86
- const images = await Promise.all(Array.from(files).map(readImage));
87
- setSelectedImages((current) => [...current, ...images].slice(0, 4));
88
- } catch (err) {
89
- setError(err instanceof Error ? err.message : String(err));
90
- }
91
- };
92
-
93
- const sendChat = async () => {
94
- const text = input.trim();
95
- if (!text && !selectedImages.length) return;
96
- const content: string | ChatContentPart[] = selectedImages.length
97
- ? [
98
- ...(text ? [{ type: "text" as const, text }] : []),
99
- ...selectedImages.map((image) => ({ type: "image_url" as const, image_url: { url: image.url } })),
100
- ]
101
- : text;
102
- const nextMessages: ChatMessage[] = [...messages, { role: "user", content }];
103
- setMessages(nextMessages);
104
- setInput("");
105
- setSelectedImages([]);
106
- setLoading(true);
107
- setError("");
108
- try {
109
- const body = {
110
- model: model.trim() || "auto",
111
- messages: nextMessages,
112
- ...(reasoningEffort ? { reasoning_effort: reasoningEffort } : {}),
113
- };
114
- const result = await httpRequest<ChatCompletionResponse>("/v1/chat/completions", { method: "POST", body });
115
- setRaw(result);
116
- setMessages([...nextMessages, { role: "assistant", content: String(result.choices?.[0]?.message?.content || "") }]);
117
- } catch (err) {
118
- setError(err instanceof Error ? err.message : String(err));
119
- } finally {
120
- setLoading(false);
121
- }
122
- };
123
-
124
- const clearChat = () => {
125
- setMessages([]);
126
- setSelectedImages([]);
127
- setRaw(null);
128
- setError("");
129
- };
130
-
131
- return (
132
- <div className="grid h-full min-h-0 gap-8 lg:grid-cols-[360px_minmax(0,1fr)]">
133
- <section className="flex min-h-0 flex-col lg:border-r lg:border-stone-200/70 lg:pr-8 dark:lg:border-white/10">
134
- <div className="border-b border-stone-200/70 pb-3 dark:border-white/10">
135
- <h2 className="text-sm font-medium text-stone-500 dark:text-stone-400">请求</h2>
136
- </div>
137
- <div className="min-h-0 flex-1 space-y-4 overflow-auto pt-4">
138
- <div className="grid gap-3 sm:grid-cols-[minmax(0,1fr)_160px]">
139
- <div className="space-y-2">
140
- <Label htmlFor="chat-model">Model</Label>
141
- <Input id="chat-model" value={model} onChange={(event) => setModel(event.target.value)} className="rounded-md border-stone-200/70 bg-transparent shadow-none dark:border-white/10" />
142
- </div>
143
- <div className="space-y-2">
144
- <Label htmlFor="chat-reasoning-effort">思考强度</Label>
145
- <Select value={reasoningEffort || "default"} onValueChange={(value) => setReasoningEffort(value === "default" ? "" : value)}>
146
- <SelectTrigger id="chat-reasoning-effort" className="h-10 rounded-md border-stone-200/70 bg-transparent shadow-none dark:border-white/10">
147
- <SelectValue />
148
- </SelectTrigger>
149
- <SelectContent>
150
- <SelectItem value="default">默认</SelectItem>
151
- <SelectItem value="low">低</SelectItem>
152
- <SelectItem value="medium">中</SelectItem>
153
- <SelectItem value="high">高</SelectItem>
154
- <SelectItem value="xhigh">超高</SelectItem>
155
- </SelectContent>
156
- </Select>
157
- </div>
158
- </div>
159
- <div className="space-y-2">
160
- <Label htmlFor="chat-input">Message</Label>
161
- <Textarea id="chat-input" value={input} onChange={(event) => setInput(event.target.value)} className="min-h-32 rounded-md border-stone-200/70 bg-transparent shadow-none dark:border-white/10" />
162
- </div>
163
- <div className="space-y-2">
164
- <Label htmlFor="chat-images">图片</Label>
165
- <label htmlFor="chat-images" className="flex cursor-pointer items-center justify-center gap-2 rounded-md border border-dashed border-stone-300 bg-stone-50/70 px-3 py-3 text-sm text-stone-600 transition hover:border-stone-400 hover:bg-stone-100 dark:border-white/10 dark:bg-white/[0.03] dark:text-stone-300 dark:hover:bg-white/[0.06]">
166
- <ImagePlus className="size-4" />
167
- 选择图片
168
- </label>
169
- <input id="chat-images" type="file" accept="image/png,image/jpeg,image/webp,image/gif" multiple className="sr-only" onChange={(event) => {
170
- void handleImagesChange(event.target.files);
171
- event.currentTarget.value = "";
172
- }} />
173
- {selectedImages.length ? (
174
- <div className="grid grid-cols-2 gap-2">
175
- {selectedImages.map((image) => (
176
- <div key={image.id} className="group relative overflow-hidden rounded-md border border-stone-200 bg-white dark:border-white/10 dark:bg-white/[0.04]">
177
- <img src={image.url} alt={image.name} className="aspect-square w-full object-cover" />
178
- <button type="button" aria-label={`移除 ${image.name}`} onClick={() => setSelectedImages((current) => current.filter((item) => item.id !== image.id))} className="absolute top-1 right-1 flex size-7 items-center justify-center rounded-md bg-white/90 text-stone-700 shadow-sm transition hover:bg-white dark:bg-stone-950/90 dark:text-stone-100">
179
- <X className="size-4" />
180
- </button>
181
- <div className="absolute inset-x-0 bottom-0 truncate bg-white/90 px-2 py-1 text-xs text-stone-600 dark:bg-stone-950/90 dark:text-stone-300">{image.name}</div>
182
- </div>
183
- ))}
184
- </div>
185
- ) : null}
186
- </div>
187
- <div className="flex gap-2">
188
- <Button size="sm" onClick={() => void sendChat()} disabled={loading || (!input.trim() && !selectedImages.length)}>
189
- {loading ? <LoaderCircle className="animate-spin" /> : <Send />}
190
- 发送
191
- </Button>
192
- <Button size="sm" variant="outline" onClick={clearChat}>
193
- 清空
194
- </Button>
195
- </div>
196
- {error ? <div className="rounded-md border border-rose-200 bg-rose-50/60 px-3 py-2 text-sm text-rose-700 dark:border-rose-900/60 dark:bg-rose-950/20 dark:text-rose-300">{error}</div> : null}
197
- <Textarea value={raw ? pretty(raw) : "{\n \"messages\": []\n}"} readOnly className="min-h-72 resize-none rounded-md border-stone-200/70 bg-stone-50/50 p-4 font-mono text-xs leading-5 text-stone-600 shadow-none dark:border-white/10 dark:bg-white/[0.03] dark:text-stone-300" />
198
- </div>
199
- </section>
200
- <section className="flex min-h-0 flex-col">
201
- <div className="border-b border-stone-200/70 pb-3 dark:border-white/10">
202
- <h2 className="text-sm font-medium text-stone-500 dark:text-stone-400">对话</h2>
203
- </div>
204
- <div className="min-h-0 flex-1 space-y-4 overflow-auto pt-4">
205
- {messages.length ? messages.map((message, index) => (
206
- <div key={`${message.role}-${index}`} className="space-y-1.5 text-sm">
207
- <div className="text-xs font-medium uppercase tracking-wide text-stone-400 dark:text-stone-500">{message.role}</div>
208
- {messageImages(message).length ? (
209
- <div className="flex flex-wrap gap-2">
210
- {messageImages(message).map((url, imageIndex) => (
211
- <img key={`${index}-${imageIndex}`} src={url} alt="" className="h-28 w-28 rounded-md border border-stone-200 object-cover dark:border-white/10" />
212
- ))}
213
- </div>
214
- ) : null}
215
- {messageText(message) ? <div className="whitespace-pre-wrap leading-7 text-stone-700 dark:text-stone-300">{messageText(message)}</div> : null}
216
- </div>
217
- )) : (
218
- <div className="flex h-full items-center justify-center text-sm text-stone-400 dark:text-stone-500">暂无对话消息</div>
219
- )}
220
- </div>
221
- </section>
222
- </div>
223
- );
224
- }
1
+ "use client";
2
+
3
+ import { useState } from "react";
4
+ import { ImagePlus, LoaderCircle, Send, X } from "lucide-react";
5
+
6
+ import { Button } from "@/components/ui/button";
7
+ import { Input } from "@/components/ui/input";
8
+ import { Label } from "@/components/ui/label";
9
+ import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
10
+ import { Textarea } from "@/components/ui/textarea";
11
+ import { httpRequest } from "@/lib/request";
12
+
13
+ import { pretty, type ChatCompletionResponse, type ChatContentPart, type ChatMessage } from "./types";
14
+
15
+ type SelectedImage = {
16
+ id: string;
17
+ name: string;
18
+ size: number;
19
+ url: string;
20
+ };
21
+
22
+ const MAX_IMAGE_BYTES = 10 * 1024 * 1024;
23
+
24
+ function readImage(file: File): Promise<SelectedImage> {
25
+ return new Promise((resolve, reject) => {
26
+ if (!file.type.startsWith("image/")) {
27
+ reject(new Error(`${file.name} 不是图片文件`));
28
+ return;
29
+ }
30
+ if (file.size > MAX_IMAGE_BYTES) {
31
+ reject(new Error(`${file.name} 超过 10MB`));
32
+ return;
33
+ }
34
+ const reader = new FileReader();
35
+ reader.onload = () => {
36
+ const url = String(reader.result || "");
37
+ if (!url.startsWith("data:image/")) {
38
+ reject(new Error(`${file.name} 读取失败`));
39
+ return;
40
+ }
41
+ resolve({
42
+ id: `${file.name}-${file.size}-${file.lastModified}-${Math.random().toString(16).slice(2)}`,
43
+ name: file.name,
44
+ size: file.size,
45
+ url,
46
+ });
47
+ };
48
+ reader.onerror = () => reject(reader.error || new Error(`${file.name} 读取失败`));
49
+ reader.readAsDataURL(file);
50
+ });
51
+ }
52
+
53
+ function messageText(message: ChatMessage): string {
54
+ if (typeof message.content === "string") {
55
+ return message.content;
56
+ }
57
+ return message.content
58
+ .filter((part): part is { type: "text"; text: string } => part.type === "text")
59
+ .map((part) => part.text)
60
+ .join("");
61
+ }
62
+
63
+ function messageImages(message: ChatMessage): string[] {
64
+ if (!Array.isArray(message.content)) {
65
+ return [];
66
+ }
67
+ return message.content
68
+ .filter((part): part is { type: "image_url"; image_url: { url: string } } => part.type === "image_url")
69
+ .map((part) => part.image_url.url);
70
+ }
71
+
72
+ export function ChatPanel() {
73
+ const [model, setModel] = useState("auto");
74
+ const [reasoningEffort, setReasoningEffort] = useState("");
75
+ const [input, setInput] = useState("你好,先记住我的项目叫 chatgpt2api。");
76
+ const [selectedImages, setSelectedImages] = useState<SelectedImage[]>([]);
77
+ const [messages, setMessages] = useState<ChatMessage[]>([]);
78
+ const [raw, setRaw] = useState<ChatCompletionResponse | null>(null);
79
+ const [loading, setLoading] = useState(false);
80
+ const [error, setError] = useState("");
81
+
82
+ const handleImagesChange = async (files: FileList | null) => {
83
+ if (!files?.length) return;
84
+ setError("");
85
+ try {
86
+ const images = await Promise.all(Array.from(files).map(readImage));
87
+ setSelectedImages((current) => [...current, ...images].slice(0, 4));
88
+ } catch (err) {
89
+ setError(err instanceof Error ? err.message : String(err));
90
+ }
91
+ };
92
+
93
+ const sendChat = async () => {
94
+ const text = input.trim();
95
+ if (!text && !selectedImages.length) return;
96
+ const content: string | ChatContentPart[] = selectedImages.length
97
+ ? [
98
+ ...(text ? [{ type: "text" as const, text }] : []),
99
+ ...selectedImages.map((image) => ({ type: "image_url" as const, image_url: { url: image.url } })),
100
+ ]
101
+ : text;
102
+ const nextMessages: ChatMessage[] = [...messages, { role: "user", content }];
103
+ setMessages(nextMessages);
104
+ setInput("");
105
+ setSelectedImages([]);
106
+ setLoading(true);
107
+ setError("");
108
+ try {
109
+ const body = {
110
+ model: model.trim() || "auto",
111
+ messages: nextMessages,
112
+ ...(reasoningEffort ? { reasoning_effort: reasoningEffort } : {}),
113
+ };
114
+ const result = await httpRequest<ChatCompletionResponse>("/v1/chat/completions", { method: "POST", body });
115
+ setRaw(result);
116
+ setMessages([...nextMessages, { role: "assistant", content: String(result.choices?.[0]?.message?.content || "") }]);
117
+ } catch (err) {
118
+ setError(err instanceof Error ? err.message : String(err));
119
+ } finally {
120
+ setLoading(false);
121
+ }
122
+ };
123
+
124
+ const clearChat = () => {
125
+ setMessages([]);
126
+ setSelectedImages([]);
127
+ setRaw(null);
128
+ setError("");
129
+ };
130
+
131
+ return (
132
+ <div className="grid h-full min-h-0 gap-8 lg:grid-cols-[360px_minmax(0,1fr)]">
133
+ <section className="flex min-h-0 flex-col lg:border-r lg:border-stone-200/70 lg:pr-8 dark:lg:border-white/10">
134
+ <div className="border-b border-stone-200/70 pb-3 dark:border-white/10">
135
+ <h2 className="text-sm font-medium text-stone-500 dark:text-stone-400">请求</h2>
136
+ </div>
137
+ <div className="min-h-0 flex-1 space-y-4 overflow-auto pt-4">
138
+ <div className="grid gap-3 sm:grid-cols-[minmax(0,1fr)_160px]">
139
+ <div className="space-y-2">
140
+ <Label htmlFor="chat-model">Model</Label>
141
+ <Input id="chat-model" value={model} onChange={(event) => setModel(event.target.value)} className="rounded-md border-stone-200/70 bg-transparent shadow-none dark:border-white/10" />
142
+ </div>
143
+ <div className="space-y-2">
144
+ <Label htmlFor="chat-reasoning-effort">思考强度</Label>
145
+ <Select value={reasoningEffort || "default"} onValueChange={(value) => setReasoningEffort(value === "default" ? "" : value)}>
146
+ <SelectTrigger id="chat-reasoning-effort" className="h-10 rounded-md border-stone-200/70 bg-transparent shadow-none dark:border-white/10">
147
+ <SelectValue />
148
+ </SelectTrigger>
149
+ <SelectContent>
150
+ <SelectItem value="default">默认</SelectItem>
151
+ <SelectItem value="low">低</SelectItem>
152
+ <SelectItem value="medium">中</SelectItem>
153
+ <SelectItem value="high">高</SelectItem>
154
+ <SelectItem value="xhigh">超高</SelectItem>
155
+ </SelectContent>
156
+ </Select>
157
+ </div>
158
+ </div>
159
+ <div className="space-y-2">
160
+ <Label htmlFor="chat-input">Message</Label>
161
+ <Textarea id="chat-input" value={input} onChange={(event) => setInput(event.target.value)} className="min-h-32 rounded-md border-stone-200/70 bg-transparent shadow-none dark:border-white/10" />
162
+ </div>
163
+ <div className="space-y-2">
164
+ <Label htmlFor="chat-images">图片</Label>
165
+ <label htmlFor="chat-images" className="flex cursor-pointer items-center justify-center gap-2 rounded-md border border-dashed border-stone-300 bg-stone-50/70 px-3 py-3 text-sm text-stone-600 transition hover:border-stone-400 hover:bg-stone-100 dark:border-white/10 dark:bg-white/[0.03] dark:text-stone-300 dark:hover:bg-white/[0.06]">
166
+ <ImagePlus className="size-4" />
167
+ 选择图片
168
+ </label>
169
+ <input id="chat-images" type="file" accept="image/png,image/jpeg,image/webp,image/gif" multiple className="sr-only" onChange={(event) => {
170
+ void handleImagesChange(event.target.files);
171
+ event.currentTarget.value = "";
172
+ }} />
173
+ {selectedImages.length ? (
174
+ <div className="grid grid-cols-2 gap-2">
175
+ {selectedImages.map((image) => (
176
+ <div key={image.id} className="group relative overflow-hidden rounded-md border border-stone-200 bg-white dark:border-white/10 dark:bg-white/[0.04]">
177
+ <img src={image.url} alt={image.name} className="aspect-square w-full object-cover" />
178
+ <button type="button" aria-label={`移除 ${image.name}`} onClick={() => setSelectedImages((current) => current.filter((item) => item.id !== image.id))} className="absolute top-1 right-1 flex size-7 items-center justify-center rounded-md bg-white/90 text-stone-700 shadow-sm transition hover:bg-white dark:bg-stone-950/90 dark:text-stone-100">
179
+ <X className="size-4" />
180
+ </button>
181
+ <div className="absolute inset-x-0 bottom-0 truncate bg-white/90 px-2 py-1 text-xs text-stone-600 dark:bg-stone-950/90 dark:text-stone-300">{image.name}</div>
182
+ </div>
183
+ ))}
184
+ </div>
185
+ ) : null}
186
+ </div>
187
+ <div className="flex gap-2">
188
+ <Button size="sm" onClick={() => void sendChat()} disabled={loading || (!input.trim() && !selectedImages.length)}>
189
+ {loading ? <LoaderCircle className="animate-spin" /> : <Send />}
190
+ 发送
191
+ </Button>
192
+ <Button size="sm" variant="outline" onClick={clearChat}>
193
+ 清空
194
+ </Button>
195
+ </div>
196
+ {error ? <div className="rounded-md border border-rose-200 bg-rose-50/60 px-3 py-2 text-sm text-rose-700 dark:border-rose-900/60 dark:bg-rose-950/20 dark:text-rose-300">{error}</div> : null}
197
+ <Textarea value={raw ? pretty(raw) : "{\n \"messages\": []\n}"} readOnly className="min-h-72 resize-none rounded-md border-stone-200/70 bg-stone-50/50 p-4 font-mono text-xs leading-5 text-stone-600 shadow-none dark:border-white/10 dark:bg-white/[0.03] dark:text-stone-300" />
198
+ </div>
199
+ </section>
200
+ <section className="flex min-h-0 flex-col">
201
+ <div className="border-b border-stone-200/70 pb-3 dark:border-white/10">
202
+ <h2 className="text-sm font-medium text-stone-500 dark:text-stone-400">对话</h2>
203
+ </div>
204
+ <div className="min-h-0 flex-1 space-y-4 overflow-auto pt-4">
205
+ {messages.length ? messages.map((message, index) => (
206
+ <div key={`${message.role}-${index}`} className="space-y-1.5 text-sm">
207
+ <div className="text-xs font-medium uppercase tracking-wide text-stone-400 dark:text-stone-500">{message.role}</div>
208
+ {messageImages(message).length ? (
209
+ <div className="flex flex-wrap gap-2">
210
+ {messageImages(message).map((url, imageIndex) => (
211
+ <img key={`${index}-${imageIndex}`} src={url} alt="" className="h-28 w-28 rounded-md border border-stone-200 object-cover dark:border-white/10" />
212
+ ))}
213
+ </div>
214
+ ) : null}
215
+ {messageText(message) ? <div className="whitespace-pre-wrap leading-7 text-stone-700 dark:text-stone-300">{messageText(message)}</div> : null}
216
+ </div>
217
+ )) : (
218
+ <div className="flex h-full items-center justify-center text-sm text-stone-400 dark:text-stone-500">暂无对话消息</div>
219
+ )}
220
+ </div>
221
+ </section>
222
+ </div>
223
+ );
224
+ }