dsh-plugin-image-workbench 0.0.0-stage → 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.
- package/LICENSE +21 -0
- package/README.en.md +258 -0
- package/README.md +260 -2
- package/client.js +241 -0
- package/cordis.patch.yml +14 -0
- package/host.js +692 -0
- package/install.mjs +333 -0
- package/package.json +58 -4
- package/scripts/fetch-style-images.mjs +104 -0
- package/scripts/vendor-snapshot.py +154 -0
- package/server/access.py +26 -0
- package/server/app.py +1329 -0
- package/server/catalog.json +1 -0
- package/server/catalog.py +147 -0
- package/server/composer.py +391 -0
- package/server/drafts.py +158 -0
- package/server/history.py +575 -0
- package/server/imaging.py +172 -0
- package/server/pool.py +1335 -0
- package/server/pool_admin.py +921 -0
- package/server/pool_service.py +1073 -0
- package/server/requirements.txt +6 -0
- package/server/smoke_check.py +402 -0
- package/server/static/access.html +202 -0
- package/server/static/index.html +4725 -0
- package/vendor/chatgpt2api/README.md +13 -0
- package/vendor/chatgpt2api/SOURCE.json +11 -0
- package/vendor/chatgpt2api/runtime/uv.exe +0 -0
- package/vendor/chatgpt2api/source/.env.example +82 -0
- package/vendor/chatgpt2api/source/.python-version +1 -0
- package/vendor/chatgpt2api/source/LICENSE +21 -0
- package/vendor/chatgpt2api/source/README.md +391 -0
- package/vendor/chatgpt2api/source/VERSION +1 -0
- package/vendor/chatgpt2api/source/api/__init__.py +2 -0
- package/vendor/chatgpt2api/source/api/accounts.py +537 -0
- package/vendor/chatgpt2api/source/api/ai.py +217 -0
- package/vendor/chatgpt2api/source/api/app.py +62 -0
- package/vendor/chatgpt2api/source/api/errors.py +46 -0
- package/vendor/chatgpt2api/source/api/image_inputs.py +309 -0
- package/vendor/chatgpt2api/source/api/image_tasks.py +120 -0
- package/vendor/chatgpt2api/source/api/support.py +134 -0
- package/vendor/chatgpt2api/source/api/system.py +365 -0
- package/vendor/chatgpt2api/source/assets/atlascloud.svg +1 -0
- package/vendor/chatgpt2api/source/assets/ipwo.png +0 -0
- package/vendor/chatgpt2api/source/assets/kunrouai.svg +50 -0
- package/vendor/chatgpt2api/source/config.default.json +108 -0
- package/vendor/chatgpt2api/source/main.py +9 -0
- package/vendor/chatgpt2api/source/pyproject.toml +27 -0
- package/vendor/chatgpt2api/source/scripts/init_proxy_config.py +186 -0
- package/vendor/chatgpt2api/source/scripts/migrate_storage.py +163 -0
- package/vendor/chatgpt2api/source/scripts/privoxy-warp.conf +13 -0
- package/vendor/chatgpt2api/source/scripts/test_outlook_token_mailbox.py +385 -0
- package/vendor/chatgpt2api/source/scripts/test_storage.py +129 -0
- package/vendor/chatgpt2api/source/scripts/verify_oauth_refresh.py +92 -0
- package/vendor/chatgpt2api/source/services/__init__.py +0 -0
- package/vendor/chatgpt2api/source/services/account_service.py +1757 -0
- package/vendor/chatgpt2api/source/services/auth_service.py +253 -0
- package/vendor/chatgpt2api/source/services/backup_service.py +671 -0
- package/vendor/chatgpt2api/source/services/config.py +667 -0
- package/vendor/chatgpt2api/source/services/content_filter.py +242 -0
- package/vendor/chatgpt2api/source/services/cpa_service.py +315 -0
- package/vendor/chatgpt2api/source/services/editable_file_task_service.py +260 -0
- package/vendor/chatgpt2api/source/services/image_service.py +376 -0
- package/vendor/chatgpt2api/source/services/image_storage_service.py +405 -0
- package/vendor/chatgpt2api/source/services/image_tags_service.py +76 -0
- package/vendor/chatgpt2api/source/services/image_task_service.py +550 -0
- package/vendor/chatgpt2api/source/services/log_service.py +337 -0
- package/vendor/chatgpt2api/source/services/model_service.py +182 -0
- package/vendor/chatgpt2api/source/services/oauth_login_service.py +267 -0
- package/vendor/chatgpt2api/source/services/openai_backend_api.py +2763 -0
- package/vendor/chatgpt2api/source/services/openai_oauth.py +32 -0
- package/vendor/chatgpt2api/source/services/protocol/__init__.py +2 -0
- package/vendor/chatgpt2api/source/services/protocol/anthropic_v1_messages.py +307 -0
- package/vendor/chatgpt2api/source/services/protocol/chat_completion_cache.py +238 -0
- package/vendor/chatgpt2api/source/services/protocol/conversation.py +1644 -0
- package/vendor/chatgpt2api/source/services/protocol/error_response.py +124 -0
- package/vendor/chatgpt2api/source/services/protocol/openai_search.py +19 -0
- package/vendor/chatgpt2api/source/services/protocol/openai_v1_chat_complete.py +316 -0
- package/vendor/chatgpt2api/source/services/protocol/openai_v1_image_edit.py +86 -0
- package/vendor/chatgpt2api/source/services/protocol/openai_v1_image_generations.py +42 -0
- package/vendor/chatgpt2api/source/services/protocol/openai_v1_models.py +53 -0
- package/vendor/chatgpt2api/source/services/protocol/openai_v1_response.py +453 -0
- package/vendor/chatgpt2api/source/services/protocol/web_search_tool.py +160 -0
- package/vendor/chatgpt2api/source/services/proxy_service.py +648 -0
- package/vendor/chatgpt2api/source/services/storage/__init__.py +5 -0
- package/vendor/chatgpt2api/source/services/storage/base.py +38 -0
- package/vendor/chatgpt2api/source/services/storage/database_storage.py +194 -0
- package/vendor/chatgpt2api/source/services/storage/factory.py +104 -0
- package/vendor/chatgpt2api/source/services/storage/git_storage.py +183 -0
- package/vendor/chatgpt2api/source/services/storage/json_storage.py +95 -0
- package/vendor/chatgpt2api/source/services/sub2api_service.py +531 -0
- package/vendor/chatgpt2api/source/utils/__init__.py +0 -0
- package/vendor/chatgpt2api/source/utils/helper.py +469 -0
- package/vendor/chatgpt2api/source/utils/image_tokens.py +346 -0
- package/vendor/chatgpt2api/source/utils/log.py +110 -0
- package/vendor/chatgpt2api/source/utils/pkce.py +19 -0
- package/vendor/chatgpt2api/source/utils/pow.py +207 -0
- package/vendor/chatgpt2api/source/utils/sentinel.py +159 -0
- package/vendor/chatgpt2api/source/utils/turnstile.py +184 -0
- package/vendor/chatgpt2api/source/uv.lock +904 -0
- package/vendor/chatgpt2api/source/web/bun.lock +1501 -0
- package/vendor/chatgpt2api/source/web/components.json +24 -0
- package/vendor/chatgpt2api/source/web/eslint.config.mjs +17 -0
- package/vendor/chatgpt2api/source/web/next.config.ts +40 -0
- package/vendor/chatgpt2api/source/web/package.json +58 -0
- package/vendor/chatgpt2api/source/web/postcss.config.mjs +5 -0
- package/vendor/chatgpt2api/source/web/public/github.svg +1 -0
- package/vendor/chatgpt2api/source/web/public/next.svg +1 -0
- package/vendor/chatgpt2api/source/web/public/openai.svg +1 -0
- package/vendor/chatgpt2api/source/web/public/vercel.svg +1 -0
- package/vendor/chatgpt2api/source/web/src/app/accounts/components/account-import-dialog.tsx +925 -0
- package/vendor/chatgpt2api/source/web/src/app/accounts/page.tsx +1307 -0
- package/vendor/chatgpt2api/source/web/src/app/debug/components/chat-panel.tsx +224 -0
- package/vendor/chatgpt2api/source/web/src/app/debug/components/editable-file-panel.tsx +444 -0
- package/vendor/chatgpt2api/source/web/src/app/debug/components/ppt-panel.tsx +9 -0
- package/vendor/chatgpt2api/source/web/src/app/debug/components/psd-panel.tsx +9 -0
- package/vendor/chatgpt2api/source/web/src/app/debug/components/search-panel.tsx +172 -0
- package/vendor/chatgpt2api/source/web/src/app/debug/components/skill-panel.tsx +169 -0
- package/vendor/chatgpt2api/source/web/src/app/debug/components/types.ts +39 -0
- package/vendor/chatgpt2api/source/web/src/app/debug/page.tsx +59 -0
- package/vendor/chatgpt2api/source/web/src/app/favicon.ico +0 -0
- package/vendor/chatgpt2api/source/web/src/app/globals.css +231 -0
- package/vendor/chatgpt2api/source/web/src/app/image/components/image-composer.tsx +542 -0
- package/vendor/chatgpt2api/source/web/src/app/image/components/image-results.tsx +554 -0
- package/vendor/chatgpt2api/source/web/src/app/image/components/image-sidebar.tsx +176 -0
- package/vendor/chatgpt2api/source/web/src/app/image/page.tsx +1799 -0
- package/vendor/chatgpt2api/source/web/src/app/image-manager/page.tsx +732 -0
- package/vendor/chatgpt2api/source/web/src/app/layout.tsx +50 -0
- package/vendor/chatgpt2api/source/web/src/app/login/page.tsx +101 -0
- package/vendor/chatgpt2api/source/web/src/app/logs/page.tsx +356 -0
- package/vendor/chatgpt2api/source/web/src/app/page.tsx +30 -0
- package/vendor/chatgpt2api/source/web/src/app/settings/components/api-docs-card.tsx +341 -0
- package/vendor/chatgpt2api/source/web/src/app/settings/components/backup-settings-card.tsx +447 -0
- package/vendor/chatgpt2api/source/web/src/app/settings/components/base-url-card.tsx +71 -0
- package/vendor/chatgpt2api/source/web/src/app/settings/components/config-card.tsx +491 -0
- package/vendor/chatgpt2api/source/web/src/app/settings/components/cpa-pool-dialog.tsx +108 -0
- package/vendor/chatgpt2api/source/web/src/app/settings/components/cpa-pools-card.tsx +169 -0
- package/vendor/chatgpt2api/source/web/src/app/settings/components/import-browser-dialog.tsx +185 -0
- package/vendor/chatgpt2api/source/web/src/app/settings/components/proxy-runtime-card.tsx +379 -0
- package/vendor/chatgpt2api/source/web/src/app/settings/components/proxy-settings-card.tsx +127 -0
- package/vendor/chatgpt2api/source/web/src/app/settings/components/proxy-settings.tsx +200 -0
- package/vendor/chatgpt2api/source/web/src/app/settings/components/settings-header.tsx +12 -0
- package/vendor/chatgpt2api/source/web/src/app/settings/components/sub2api-connections.tsx +886 -0
- package/vendor/chatgpt2api/source/web/src/app/settings/components/third-party-apps-card.tsx +81 -0
- package/vendor/chatgpt2api/source/web/src/app/settings/components/user-keys-card.tsx +422 -0
- package/vendor/chatgpt2api/source/web/src/app/settings/page.tsx +135 -0
- package/vendor/chatgpt2api/source/web/src/app/settings/store.ts +1069 -0
- package/vendor/chatgpt2api/source/web/src/components/date-range-filter.tsx +49 -0
- package/vendor/chatgpt2api/source/web/src/components/header-actions.tsx +24 -0
- package/vendor/chatgpt2api/source/web/src/components/image-lightbox.tsx +434 -0
- package/vendor/chatgpt2api/source/web/src/components/image-thumbnail.tsx +46 -0
- package/vendor/chatgpt2api/source/web/src/components/theme-script.tsx +18 -0
- package/vendor/chatgpt2api/source/web/src/components/theme-toggle.tsx +14 -0
- package/vendor/chatgpt2api/source/web/src/components/top-nav.tsx +263 -0
- package/vendor/chatgpt2api/source/web/src/components/ui/animated-theme-toggler.tsx +256 -0
- package/vendor/chatgpt2api/source/web/src/components/ui/badge.tsx +41 -0
- package/vendor/chatgpt2api/source/web/src/components/ui/button.tsx +59 -0
- package/vendor/chatgpt2api/source/web/src/components/ui/calendar.tsx +46 -0
- package/vendor/chatgpt2api/source/web/src/components/ui/card.tsx +61 -0
- package/vendor/chatgpt2api/source/web/src/components/ui/checkbox.tsx +27 -0
- package/vendor/chatgpt2api/source/web/src/components/ui/dia-text-reveal.tsx +267 -0
- package/vendor/chatgpt2api/source/web/src/components/ui/dialog.tsx +132 -0
- package/vendor/chatgpt2api/source/web/src/components/ui/field.tsx +13 -0
- package/vendor/chatgpt2api/source/web/src/components/ui/input.tsx +19 -0
- package/vendor/chatgpt2api/source/web/src/components/ui/label.tsx +24 -0
- package/vendor/chatgpt2api/source/web/src/components/ui/morphing-text.tsx +149 -0
- package/vendor/chatgpt2api/source/web/src/components/ui/popover.tsx +38 -0
- package/vendor/chatgpt2api/source/web/src/components/ui/select.tsx +175 -0
- package/vendor/chatgpt2api/source/web/src/components/ui/sheet.tsx +143 -0
- package/vendor/chatgpt2api/source/web/src/components/ui/table.tsx +29 -0
- package/vendor/chatgpt2api/source/web/src/components/ui/tabs.tsx +91 -0
- package/vendor/chatgpt2api/source/web/src/components/ui/text-3d-flip.tsx +269 -0
- package/vendor/chatgpt2api/source/web/src/components/ui/textarea.tsx +18 -0
- package/vendor/chatgpt2api/source/web/src/components/version-release-dialog.tsx +127 -0
- package/vendor/chatgpt2api/source/web/src/constants/common-env.ts +6 -0
- package/vendor/chatgpt2api/source/web/src/hooks/use-version-check.ts +85 -0
- package/vendor/chatgpt2api/source/web/src/lib/api.ts +926 -0
- package/vendor/chatgpt2api/source/web/src/lib/auth-session.ts +26 -0
- package/vendor/chatgpt2api/source/web/src/lib/release.ts +25 -0
- package/vendor/chatgpt2api/source/web/src/lib/request.ts +93 -0
- package/vendor/chatgpt2api/source/web/src/lib/use-auth-guard.ts +89 -0
- package/vendor/chatgpt2api/source/web/src/lib/utils.ts +6 -0
- package/vendor/chatgpt2api/source/web/src/store/auth.ts +112 -0
- package/vendor/chatgpt2api/source/web/src/store/editable-file-history.ts +33 -0
- package/vendor/chatgpt2api/source/web/src/store/image-conversations.ts +318 -0
- package/vendor/chatgpt2api/source/web/tsconfig.json +42 -0
|
@@ -0,0 +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
|
+
}
|
|
@@ -0,0 +1,444 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useCallback, useEffect, useMemo, useState } from "react";
|
|
4
|
+
import type { ReactNode } from "react";
|
|
5
|
+
import { AlertCircle, CheckCircle2, Clock3, FileArchive, FileText, History, ImagePlus, LoaderCircle, Pencil, Play, Plus, RefreshCw, Trash2, XCircle } from "lucide-react";
|
|
6
|
+
|
|
7
|
+
import { Button } from "@/components/ui/button";
|
|
8
|
+
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog";
|
|
9
|
+
import { Input } from "@/components/ui/input";
|
|
10
|
+
import { Label } from "@/components/ui/label";
|
|
11
|
+
import { Textarea } from "@/components/ui/textarea";
|
|
12
|
+
import { httpRequest } from "@/lib/request";
|
|
13
|
+
import { cn } from "@/lib/utils";
|
|
14
|
+
import {
|
|
15
|
+
listDeletedEditableFileIds,
|
|
16
|
+
listEditableFileDrafts,
|
|
17
|
+
saveDeletedEditableFileIds,
|
|
18
|
+
saveEditableFileDrafts,
|
|
19
|
+
type EditableFileDraft,
|
|
20
|
+
} from "@/store/editable-file-history";
|
|
21
|
+
|
|
22
|
+
import type { EditableFileTask } from "./types";
|
|
23
|
+
|
|
24
|
+
type Props = {
|
|
25
|
+
title: string;
|
|
26
|
+
kind: "ppt" | "psd";
|
|
27
|
+
endpoint: string;
|
|
28
|
+
defaultPrompt: string;
|
|
29
|
+
imageRequired?: boolean;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const MAX_HISTORY = 20;
|
|
33
|
+
const DRAFT_ID = "__draft__";
|
|
34
|
+
const taskIdOf = (task: EditableFileTask | null | undefined) => task?.taskId || task?.id || "";
|
|
35
|
+
const isRunning = (task: EditableFileTask | null | undefined) => task?.status === "queued" || task?.status === "running";
|
|
36
|
+
const statusText = (status: string) => ({ queued: "排队中", running: "生成中", success: "已完成", error: "失败" }[status] || status);
|
|
37
|
+
const statusClass = (status: string) => status === "success" ? "border-emerald-200 bg-emerald-50 text-emerald-700 dark:border-emerald-900/60 dark:bg-emerald-950/20 dark:text-emerald-300" : status === "error" ? "border-rose-200 bg-rose-50 text-rose-700 dark:border-rose-900/60 dark:bg-rose-950/20 dark:text-rose-300" : "border-amber-200 bg-amber-50 text-amber-700 dark:border-amber-900/60 dark:bg-amber-950/20 dark:text-amber-300";
|
|
38
|
+
const formatElapsed = (seconds: number) => `${Math.floor(seconds / 60)}m ${String(seconds % 60).padStart(2, "0")}s`;
|
|
39
|
+
const titleOfPrompt = (prompt: string, fallback: string) => prompt.trim().replace(/\s+/g, " ").slice(0, 24) || fallback;
|
|
40
|
+
const createClientTaskId = () => `${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
|
41
|
+
|
|
42
|
+
const readFile = (file: File) => new Promise<string>((resolve, reject) => {
|
|
43
|
+
const reader = new FileReader();
|
|
44
|
+
reader.onload = () => resolve(String(reader.result || ""));
|
|
45
|
+
reader.onerror = () => reject(reader.error);
|
|
46
|
+
reader.readAsDataURL(file);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const mergeTasks = (current: EditableFileTask[], updates: EditableFileTask[]) => {
|
|
50
|
+
const next = [...current];
|
|
51
|
+
for (const update of updates) {
|
|
52
|
+
const id = taskIdOf(update);
|
|
53
|
+
if (!id) continue;
|
|
54
|
+
const index = next.findIndex((item) => taskIdOf(item) === id);
|
|
55
|
+
const merged = { ...(index >= 0 ? next[index] : {}), ...update, polled_at: Date.now() };
|
|
56
|
+
if (index >= 0) next[index] = merged;
|
|
57
|
+
else next.unshift(merged);
|
|
58
|
+
}
|
|
59
|
+
return next.slice(0, MAX_HISTORY);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const removeTasks = (current: EditableFileTask[], ids: string[]) => {
|
|
63
|
+
const missing = new Set(ids);
|
|
64
|
+
return missing.size ? current.filter((task) => !missing.has(taskIdOf(task))) : current;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const fileNameOf = (url: string) => {
|
|
68
|
+
try {
|
|
69
|
+
return decodeURIComponent(new URL(url).pathname.split("/").pop() || "");
|
|
70
|
+
} catch {
|
|
71
|
+
return decodeURIComponent(url.split("/").pop() || "");
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
function ResultFile({ href, icon, label }: { href?: string; icon: ReactNode; label: string }) {
|
|
76
|
+
if (!href) return null;
|
|
77
|
+
return (
|
|
78
|
+
<div className="flex items-center gap-3 rounded-md border border-stone-200 bg-stone-50/80 px-3 py-3 dark:border-white/10 dark:bg-white/[0.04]">
|
|
79
|
+
<div className="flex size-9 shrink-0 items-center justify-center rounded-md bg-white text-stone-700 shadow-sm dark:bg-white/10 dark:text-stone-200">
|
|
80
|
+
{icon}
|
|
81
|
+
</div>
|
|
82
|
+
<div className="min-w-0 flex-1">
|
|
83
|
+
<div className="text-sm font-semibold text-stone-950 dark:text-stone-50">{label}</div>
|
|
84
|
+
<div className="truncate text-xs text-stone-500 dark:text-stone-400">{fileNameOf(href)}</div>
|
|
85
|
+
</div>
|
|
86
|
+
<Button size="sm" asChild>
|
|
87
|
+
<a href={href} target="_blank" rel="noreferrer">下载</a>
|
|
88
|
+
</Button>
|
|
89
|
+
</div>
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function EditableFilePanel({ title, kind, endpoint, defaultPrompt, imageRequired }: Props) {
|
|
94
|
+
const [prompt, setPrompt] = useState(defaultPrompt);
|
|
95
|
+
const [images, setImages] = useState<string[]>([]);
|
|
96
|
+
const [tasks, setTasks] = useState<EditableFileTask[]>([]);
|
|
97
|
+
const [selectedId, setSelectedId] = useState("");
|
|
98
|
+
const [submitting, setSubmitting] = useState(false);
|
|
99
|
+
const [polling, setPolling] = useState(false);
|
|
100
|
+
const [error, setError] = useState("");
|
|
101
|
+
const [now, setNow] = useState(Date.now());
|
|
102
|
+
const [drafts, setDrafts] = useState<Record<string, EditableFileDraft>>({});
|
|
103
|
+
const [deletedIds, setDeletedIds] = useState<Set<string>>(new Set());
|
|
104
|
+
const [renamingId, setRenamingId] = useState("");
|
|
105
|
+
const [renamingTitle, setRenamingTitle] = useState("");
|
|
106
|
+
const [deleteConfirm, setDeleteConfirm] = useState<{ type: "one"; id: string } | { type: "all"; ids: string[] } | null>(null);
|
|
107
|
+
const visibleTasks = useMemo(() => tasks.filter((task) => task.kind === kind && !deletedIds.has(taskIdOf(task))).slice(0, MAX_HISTORY), [deletedIds, kind, tasks]);
|
|
108
|
+
const selectedTask = selectedId === DRAFT_ID ? null : visibleTasks.find((task) => taskIdOf(task) === selectedId) || visibleTasks[0] || null;
|
|
109
|
+
const running = visibleTasks.some(isRunning);
|
|
110
|
+
const runningIds = visibleTasks.filter(isRunning).map(taskIdOf).join(",");
|
|
111
|
+
|
|
112
|
+
const elapsedOf = (task: EditableFileTask | null | undefined) => {
|
|
113
|
+
if (!task) return 0;
|
|
114
|
+
const base = Math.max(0, Number(task.elapsed_seconds || 0));
|
|
115
|
+
return Math.max(0, isRunning(task) && task.polled_at ? base + Math.floor((now - task.polled_at) / 1000) : base);
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const fetchTasks = useCallback(async (ids: string[] = []) => {
|
|
119
|
+
const taskIds = Array.from(new Set(ids.filter(Boolean))).slice(0, MAX_HISTORY);
|
|
120
|
+
setPolling(true);
|
|
121
|
+
try {
|
|
122
|
+
const path = taskIds.length ? `/v1/editable-file-tasks?ids=${taskIds.map(encodeURIComponent).join(",")}` : "/v1/editable-file-tasks";
|
|
123
|
+
const result = await httpRequest<{ items: EditableFileTask[]; missing_ids?: string[] }>(path);
|
|
124
|
+
const missingIds = result.missing_ids || [];
|
|
125
|
+
const hidden = await listDeletedEditableFileIds(kind);
|
|
126
|
+
setTasks((current) => (taskIds.length ? mergeTasks(removeTasks(current, missingIds), result.items || []) : (result.items || [])).filter((task) => !hidden.has(taskIdOf(task))));
|
|
127
|
+
setSelectedId((current) => missingIds.includes(current) ? "" : current);
|
|
128
|
+
} catch (err) {
|
|
129
|
+
setError(err instanceof Error ? err.message : String(err));
|
|
130
|
+
} finally {
|
|
131
|
+
setPolling(false);
|
|
132
|
+
}
|
|
133
|
+
}, [kind]);
|
|
134
|
+
|
|
135
|
+
useEffect(() => {
|
|
136
|
+
void listEditableFileDrafts(kind).then(setDrafts);
|
|
137
|
+
void listDeletedEditableFileIds(kind).then(setDeletedIds);
|
|
138
|
+
void fetchTasks();
|
|
139
|
+
}, [fetchTasks, kind]);
|
|
140
|
+
|
|
141
|
+
useEffect(() => {
|
|
142
|
+
if (selectedId === DRAFT_ID) return;
|
|
143
|
+
const draft = drafts[selectedId];
|
|
144
|
+
const task = visibleTasks.find((item) => taskIdOf(item) === selectedId);
|
|
145
|
+
setPrompt(draft?.prompt || task?.prompt_preview || defaultPrompt);
|
|
146
|
+
setImages(Array.isArray(draft?.images) ? draft.images : []);
|
|
147
|
+
}, [defaultPrompt, drafts, selectedId, visibleTasks]);
|
|
148
|
+
|
|
149
|
+
useEffect(() => {
|
|
150
|
+
if (selectedId === DRAFT_ID) return;
|
|
151
|
+
if (visibleTasks.length && (!selectedId || !visibleTasks.some((task) => taskIdOf(task) === selectedId))) setSelectedId(taskIdOf(visibleTasks[0]));
|
|
152
|
+
if (!visibleTasks.length && selectedId) setSelectedId("");
|
|
153
|
+
}, [selectedId, visibleTasks]);
|
|
154
|
+
|
|
155
|
+
useEffect(() => {
|
|
156
|
+
if (!running) return;
|
|
157
|
+
const timer = window.setInterval(() => setNow(Date.now()), 1000);
|
|
158
|
+
return () => window.clearInterval(timer);
|
|
159
|
+
}, [running]);
|
|
160
|
+
|
|
161
|
+
useEffect(() => {
|
|
162
|
+
const ids = runningIds.split(",").filter(Boolean);
|
|
163
|
+
if (!ids.length) return;
|
|
164
|
+
const timer = window.setInterval(() => void fetchTasks(ids), 5000);
|
|
165
|
+
return () => window.clearInterval(timer);
|
|
166
|
+
}, [fetchTasks, runningIds]);
|
|
167
|
+
|
|
168
|
+
const appendFiles = async (files: FileList | null) => {
|
|
169
|
+
if (!files?.length) return;
|
|
170
|
+
const values = await Promise.all(Array.from(files).map(readFile));
|
|
171
|
+
setImages((current) => [...current, ...values]);
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
const persistDrafts = (updater: (current: Record<string, EditableFileDraft>) => Record<string, EditableFileDraft>) => {
|
|
175
|
+
setDrafts((current) => {
|
|
176
|
+
const next = updater(current);
|
|
177
|
+
void saveEditableFileDrafts(kind, next);
|
|
178
|
+
return next;
|
|
179
|
+
});
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
const createDraft = () => {
|
|
183
|
+
setError("");
|
|
184
|
+
setSelectedId(DRAFT_ID);
|
|
185
|
+
setPrompt(defaultPrompt);
|
|
186
|
+
setImages([]);
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
const renameTask = (id: string, title: string) => {
|
|
190
|
+
const trimmed = title.trim();
|
|
191
|
+
if (!id || !trimmed) return;
|
|
192
|
+
persistDrafts((current) => ({ ...current, [id]: { ...(current[id] || {}), title: trimmed } }));
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
const deleteTask = (id: string) => {
|
|
196
|
+
if (!id) return;
|
|
197
|
+
const nextDeleted = new Set(deletedIds);
|
|
198
|
+
nextDeleted.add(id);
|
|
199
|
+
void saveDeletedEditableFileIds(kind, nextDeleted);
|
|
200
|
+
setDeletedIds(nextDeleted);
|
|
201
|
+
setTasks((current) => current.filter((task) => taskIdOf(task) !== id));
|
|
202
|
+
persistDrafts((current) => {
|
|
203
|
+
const next = { ...current };
|
|
204
|
+
delete next[id];
|
|
205
|
+
return next;
|
|
206
|
+
});
|
|
207
|
+
if (selectedId === id) setSelectedId("");
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
const clearHistory = () => {
|
|
211
|
+
const ids = deleteConfirm?.type === "all" ? deleteConfirm.ids : tasks.filter((task) => task.kind === kind).map(taskIdOf).filter(Boolean);
|
|
212
|
+
if (!ids.length) return;
|
|
213
|
+
const nextDeleted = new Set([...deletedIds, ...ids]);
|
|
214
|
+
void saveDeletedEditableFileIds(kind, nextDeleted);
|
|
215
|
+
setDeletedIds(nextDeleted);
|
|
216
|
+
setTasks((current) => current.filter((task) => task.kind !== kind || !ids.includes(taskIdOf(task))));
|
|
217
|
+
persistDrafts((current) => {
|
|
218
|
+
const next = { ...current };
|
|
219
|
+
ids.forEach((id) => delete next[id]);
|
|
220
|
+
return next;
|
|
221
|
+
});
|
|
222
|
+
setSelectedId("");
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
const confirmDelete = () => {
|
|
226
|
+
if (!deleteConfirm) return;
|
|
227
|
+
if (deleteConfirm.type === "one") deleteTask(deleteConfirm.id);
|
|
228
|
+
else clearHistory();
|
|
229
|
+
setDeleteConfirm(null);
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
const submit = async () => {
|
|
233
|
+
setError("");
|
|
234
|
+
setSubmitting(true);
|
|
235
|
+
try {
|
|
236
|
+
const base64_images = images;
|
|
237
|
+
if (imageRequired && !base64_images.length) throw new Error("base64_images is empty");
|
|
238
|
+
const task = await httpRequest<EditableFileTask>(endpoint, { method: "POST", body: { client_task_id: createClientTaskId(), prompt, base64_images } });
|
|
239
|
+
const polledAt = Date.now();
|
|
240
|
+
const id = taskIdOf(task);
|
|
241
|
+
const draft = { prompt: prompt.trim(), images: base64_images, title: titleOfPrompt(prompt, title) };
|
|
242
|
+
const nextTask = { ...task, prompt_preview: draft.prompt, polled_at: polledAt };
|
|
243
|
+
setNow(polledAt);
|
|
244
|
+
persistDrafts((current) => ({ ...current, [id]: draft }));
|
|
245
|
+
setTasks((current) => mergeTasks(current.filter((item) => taskIdOf(item) !== taskIdOf(nextTask)), [nextTask]));
|
|
246
|
+
setSelectedId(id);
|
|
247
|
+
} catch (err) {
|
|
248
|
+
setError(err instanceof Error ? err.message : String(err));
|
|
249
|
+
} finally {
|
|
250
|
+
setSubmitting(false);
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
const refreshAll = () => void fetchTasks();
|
|
255
|
+
const selectTask = (id: string) => {
|
|
256
|
+
setRenamingId("");
|
|
257
|
+
setSelectedId(id);
|
|
258
|
+
const draft = drafts[id];
|
|
259
|
+
const task = visibleTasks.find((item) => taskIdOf(item) === id);
|
|
260
|
+
setPrompt(draft?.prompt || task?.prompt_preview || defaultPrompt);
|
|
261
|
+
setImages(Array.isArray(draft?.images) ? draft.images : []);
|
|
262
|
+
};
|
|
263
|
+
const startRename = (id: string) => {
|
|
264
|
+
setRenamingId(id);
|
|
265
|
+
setRenamingTitle(drafts[id]?.title || titleOfPrompt(drafts[id]?.prompt || "", `${kind.toUpperCase()} 任务`));
|
|
266
|
+
};
|
|
267
|
+
const commitRename = () => {
|
|
268
|
+
renameTask(renamingId, renamingTitle);
|
|
269
|
+
setRenamingId("");
|
|
270
|
+
setRenamingTitle("");
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
return (
|
|
274
|
+
<>
|
|
275
|
+
<div className="grid h-full min-h-[calc(100vh-148px)] overflow-hidden rounded-lg border border-stone-200 bg-white shadow-sm dark:border-white/10 dark:bg-stone-950 lg:grid-cols-[288px_420px_minmax(0,1fr)]">
|
|
276
|
+
<aside className="flex min-h-0 flex-col border-b border-stone-200 bg-stone-50/70 dark:border-white/10 dark:bg-white/[0.02] lg:border-r lg:border-b-0">
|
|
277
|
+
<div className="flex h-14 items-center justify-between border-b border-stone-200 px-4 dark:border-white/10">
|
|
278
|
+
<div className="flex items-center gap-2 text-sm font-semibold text-stone-950 dark:text-stone-50">
|
|
279
|
+
<History className="size-4" />
|
|
280
|
+
历史记录
|
|
281
|
+
</div>
|
|
282
|
+
<div className="flex gap-1">
|
|
283
|
+
<Button size="sm" variant="ghost" onClick={createDraft}>
|
|
284
|
+
<Plus />
|
|
285
|
+
</Button>
|
|
286
|
+
<Button size="sm" variant="ghost" onClick={refreshAll} disabled={polling}>
|
|
287
|
+
{polling ? <LoaderCircle className="animate-spin" /> : <RefreshCw />}
|
|
288
|
+
</Button>
|
|
289
|
+
<Button size="sm" variant="ghost" onClick={() => setDeleteConfirm({ type: "all", ids: tasks.filter((task) => task.kind === kind).map(taskIdOf).filter(Boolean) })} disabled={!visibleTasks.length}>
|
|
290
|
+
<Trash2 />
|
|
291
|
+
</Button>
|
|
292
|
+
</div>
|
|
293
|
+
</div>
|
|
294
|
+
<div className="min-h-0 flex-1 overflow-auto p-2">
|
|
295
|
+
{visibleTasks.length ? visibleTasks.map((task) => {
|
|
296
|
+
const id = taskIdOf(task);
|
|
297
|
+
const taskTitle = drafts[id]?.title || titleOfPrompt(task.prompt_preview || drafts[id]?.prompt || "", task.kind?.toUpperCase() || title);
|
|
298
|
+
return (
|
|
299
|
+
<div key={id} className={cn("group mb-2 rounded-md border px-3 py-2.5 transition", selectedId === id ? "border-stone-950 bg-white shadow-sm dark:border-white/60 dark:bg-white/10" : "border-transparent bg-transparent hover:border-stone-200 hover:bg-white dark:hover:border-white/10 dark:hover:bg-white/[0.05]")}>
|
|
300
|
+
<div className="flex items-start gap-2">
|
|
301
|
+
<button type="button" onClick={() => selectTask(id)} className="min-w-0 flex-1 text-left">
|
|
302
|
+
<div className="flex min-w-0 items-center justify-between gap-2">
|
|
303
|
+
{renamingId === id ? (
|
|
304
|
+
<input
|
|
305
|
+
value={renamingTitle}
|
|
306
|
+
onChange={(event) => setRenamingTitle(event.target.value)}
|
|
307
|
+
onBlur={commitRename}
|
|
308
|
+
onKeyDown={(event) => {
|
|
309
|
+
if (event.key === "Enter") commitRename();
|
|
310
|
+
if (event.key === "Escape") setRenamingId("");
|
|
311
|
+
}}
|
|
312
|
+
onClick={(event) => event.stopPropagation()}
|
|
313
|
+
className="min-w-0 flex-1 rounded border border-stone-300 bg-white px-2 py-1 text-sm font-semibold outline-none focus:border-stone-500 dark:border-white/15 dark:bg-stone-950"
|
|
314
|
+
autoFocus
|
|
315
|
+
/>
|
|
316
|
+
) : (
|
|
317
|
+
<span className="truncate text-sm font-semibold text-stone-950 dark:text-stone-50">{taskTitle}</span>
|
|
318
|
+
)}
|
|
319
|
+
<span className={cn("shrink-0 rounded-full border px-2 py-0.5 text-[11px]", statusClass(task.status))}>{statusText(task.status)}</span>
|
|
320
|
+
</div>
|
|
321
|
+
<div className="mt-2 flex items-center gap-2 text-xs text-stone-500 dark:text-stone-400">
|
|
322
|
+
<Clock3 className="size-3.5" />
|
|
323
|
+
<span className="tabular-nums">{formatElapsed(elapsedOf(task))}</span>
|
|
324
|
+
<span className="truncate">{task.created_at || id}</span>
|
|
325
|
+
</div>
|
|
326
|
+
{(task.prompt_preview || drafts[id]?.prompt) ? <div className="mt-2 line-clamp-2 text-xs leading-5 text-stone-500 dark:text-stone-400">{task.prompt_preview || drafts[id]?.prompt}</div> : null}
|
|
327
|
+
</button>
|
|
328
|
+
<div className="flex shrink-0 opacity-100 sm:opacity-0 sm:transition sm:group-hover:opacity-100">
|
|
329
|
+
<Button size="sm" variant="ghost" onClick={() => startRename(id)} className="size-7 px-0">
|
|
330
|
+
<Pencil className="size-3.5" />
|
|
331
|
+
</Button>
|
|
332
|
+
<Button size="sm" variant="ghost" onClick={() => setDeleteConfirm({ type: "one", id })} className="size-7 px-0 text-stone-400 hover:text-rose-500">
|
|
333
|
+
<Trash2 className="size-3.5" />
|
|
334
|
+
</Button>
|
|
335
|
+
</div>
|
|
336
|
+
</div>
|
|
337
|
+
</div>
|
|
338
|
+
);
|
|
339
|
+
}) : (
|
|
340
|
+
<div className="flex h-full items-center justify-center text-sm text-stone-400 dark:text-stone-500">暂无记录</div>
|
|
341
|
+
)}
|
|
342
|
+
</div>
|
|
343
|
+
</aside>
|
|
344
|
+
|
|
345
|
+
<section className="flex min-h-0 flex-col border-b border-stone-200 dark:border-white/10 lg:border-r lg:border-b-0">
|
|
346
|
+
<div className="flex h-14 items-center justify-between border-b border-stone-200 px-5 dark:border-white/10">
|
|
347
|
+
<h2 className="text-sm font-semibold text-stone-950 dark:text-stone-50">{title}</h2>
|
|
348
|
+
<Button size="sm" onClick={() => void submit()} disabled={submitting || running}>
|
|
349
|
+
{submitting ? <LoaderCircle className="animate-spin" /> : <Play />}
|
|
350
|
+
生成
|
|
351
|
+
</Button>
|
|
352
|
+
</div>
|
|
353
|
+
<div className="min-h-0 flex-1 space-y-5 overflow-auto p-5">
|
|
354
|
+
<div className="space-y-2">
|
|
355
|
+
<Label htmlFor={`${endpoint}-prompt`} className="text-xs font-semibold text-stone-700 dark:text-stone-300">需求</Label>
|
|
356
|
+
<Textarea id={`${endpoint}-prompt`} value={prompt} onChange={(event) => setPrompt(event.target.value)} className="min-h-56 rounded-md border-stone-200 bg-white text-sm leading-6 shadow-none dark:border-white/10 dark:bg-white/[0.03]" />
|
|
357
|
+
</div>
|
|
358
|
+
<div className="space-y-3">
|
|
359
|
+
<div className="flex items-center justify-between">
|
|
360
|
+
<Label className="text-xs font-semibold text-stone-700 dark:text-stone-300">参考图</Label>
|
|
361
|
+
<span className="rounded-full bg-stone-100 px-2 py-0.5 text-xs text-stone-500 dark:bg-white/10 dark:text-stone-400">{images.length}</span>
|
|
362
|
+
</div>
|
|
363
|
+
<label className="group flex h-24 cursor-pointer items-center justify-center gap-2 rounded-md border border-dashed border-stone-300 bg-stone-50 text-sm font-medium text-stone-600 transition hover:border-stone-950 hover:bg-white dark:border-white/15 dark:bg-white/[0.03] dark:text-stone-300 dark:hover:border-white/50">
|
|
364
|
+
<ImagePlus className="size-4 transition group-hover:scale-110" />
|
|
365
|
+
上传图片
|
|
366
|
+
<Input type="file" accept="image/*" multiple onChange={(event) => void appendFiles(event.target.files)} className="hidden" />
|
|
367
|
+
</label>
|
|
368
|
+
{images.length ? (
|
|
369
|
+
<div className="grid grid-cols-4 gap-2">
|
|
370
|
+
{images.slice(0, 8).map((src, index) => (
|
|
371
|
+
<img key={`${src.slice(0, 28)}-${index}`} src={src} alt="" className="aspect-square rounded-md border border-stone-200 object-cover dark:border-white/10" />
|
|
372
|
+
))}
|
|
373
|
+
</div>
|
|
374
|
+
) : null}
|
|
375
|
+
</div>
|
|
376
|
+
<div className="flex flex-wrap gap-2">
|
|
377
|
+
<Button size="sm" variant="outline" onClick={() => setImages([])}>
|
|
378
|
+
<Trash2 />
|
|
379
|
+
清空图片
|
|
380
|
+
</Button>
|
|
381
|
+
</div>
|
|
382
|
+
{error ? <div className="flex gap-2 rounded-md border border-rose-200 bg-rose-50/70 px-3 py-2 text-sm text-rose-700 dark:border-rose-900/60 dark:bg-rose-950/20 dark:text-rose-300"><AlertCircle className="mt-0.5 size-4 shrink-0" />{error}</div> : null}
|
|
383
|
+
</div>
|
|
384
|
+
</section>
|
|
385
|
+
|
|
386
|
+
<section className="flex min-h-0 flex-col">
|
|
387
|
+
<div className="flex h-14 items-center justify-between border-b border-stone-200 px-5 dark:border-white/10">
|
|
388
|
+
<h2 className="text-sm font-semibold text-stone-950 dark:text-stone-50">生成状态</h2>
|
|
389
|
+
{selectedTask ? <span className={cn("rounded-full border px-2.5 py-1 text-xs", statusClass(selectedTask.status))}>{statusText(selectedTask.status)}</span> : null}
|
|
390
|
+
</div>
|
|
391
|
+
<div className="min-h-0 flex-1 overflow-auto p-5">
|
|
392
|
+
{selectedTask ? (
|
|
393
|
+
<div className="space-y-5">
|
|
394
|
+
<div className="grid gap-3 sm:grid-cols-[150px_150px_minmax(0,1fr)]">
|
|
395
|
+
<div className="rounded-md border border-stone-200 bg-stone-50 p-4 dark:border-white/10 dark:bg-white/[0.03]">
|
|
396
|
+
<div className="text-xs text-stone-500 dark:text-stone-400">状态</div>
|
|
397
|
+
<div className="mt-2 flex items-center gap-2 text-sm font-semibold text-stone-950 dark:text-stone-50">
|
|
398
|
+
{selectedTask.status === "success" ? <CheckCircle2 className="size-4 text-emerald-500" /> : selectedTask.status === "error" ? <XCircle className="size-4 text-rose-500" /> : <LoaderCircle className="size-4 animate-spin text-amber-500" />}
|
|
399
|
+
{statusText(selectedTask.status)}
|
|
400
|
+
</div>
|
|
401
|
+
</div>
|
|
402
|
+
<div className="rounded-md border border-stone-200 bg-stone-50 p-4 dark:border-white/10 dark:bg-white/[0.03]">
|
|
403
|
+
<div className="text-xs text-stone-500 dark:text-stone-400">已执行</div>
|
|
404
|
+
<div className="mt-2 text-2xl font-semibold tabular-nums text-stone-950 dark:text-stone-50">{formatElapsed(elapsedOf(selectedTask))}</div>
|
|
405
|
+
</div>
|
|
406
|
+
<div className="rounded-md border border-stone-200 bg-stone-50 p-4 dark:border-white/10 dark:bg-white/[0.03]">
|
|
407
|
+
<div className="text-xs text-stone-500 dark:text-stone-400">Task ID</div>
|
|
408
|
+
<div className="mt-2 truncate font-mono text-xs text-stone-700 dark:text-stone-300">{taskIdOf(selectedTask)}</div>
|
|
409
|
+
</div>
|
|
410
|
+
</div>
|
|
411
|
+
|
|
412
|
+
{selectedTask.result ? (
|
|
413
|
+
<div className="space-y-3 rounded-md border border-stone-200 bg-white p-4 dark:border-white/10 dark:bg-white/[0.03]">
|
|
414
|
+
<div className="text-sm font-semibold text-stone-950 dark:text-stone-50">生成结果</div>
|
|
415
|
+
<ResultFile href={selectedTask.result.primary_url} icon={<FileText className="size-4" />} label={kind === "ppt" ? "PPT 文件" : "PSD 文件"} />
|
|
416
|
+
<ResultFile href={selectedTask.result.zip_url} icon={<FileArchive className="size-4" />} label="素材包" />
|
|
417
|
+
</div>
|
|
418
|
+
) : null}
|
|
419
|
+
|
|
420
|
+
{selectedTask.error ? <div className="rounded-md border border-rose-200 bg-rose-50/70 px-3 py-2 text-sm text-rose-700 dark:border-rose-900/60 dark:bg-rose-950/20 dark:text-rose-300">{selectedTask.error}</div> : null}
|
|
421
|
+
</div>
|
|
422
|
+
) : (
|
|
423
|
+
<div className="flex h-full min-h-80 items-center justify-center text-sm text-stone-400 dark:text-stone-500">暂无任务</div>
|
|
424
|
+
)}
|
|
425
|
+
</div>
|
|
426
|
+
</section>
|
|
427
|
+
</div>
|
|
428
|
+
<Dialog open={!!deleteConfirm} onOpenChange={(open) => !open && setDeleteConfirm(null)}>
|
|
429
|
+
<DialogContent className="rounded-xl" showCloseButton={false}>
|
|
430
|
+
<DialogHeader>
|
|
431
|
+
<DialogTitle>{deleteConfirm?.type === "all" ? "清空历史记录" : "删除历史记录"}</DialogTitle>
|
|
432
|
+
<DialogDescription>
|
|
433
|
+
{deleteConfirm?.type === "all" ? "确认清空当前类型的历史记录吗?" : "确认删除这条历史记录吗?"}
|
|
434
|
+
</DialogDescription>
|
|
435
|
+
</DialogHeader>
|
|
436
|
+
<DialogFooter>
|
|
437
|
+
<Button variant="outline" onClick={() => setDeleteConfirm(null)}>取消</Button>
|
|
438
|
+
<Button variant="destructive" onClick={confirmDelete}>确认删除</Button>
|
|
439
|
+
</DialogFooter>
|
|
440
|
+
</DialogContent>
|
|
441
|
+
</Dialog>
|
|
442
|
+
</>
|
|
443
|
+
);
|
|
444
|
+
}
|