feat: 新增配置与提示词偏好页
后端新增 preferences 与 prompt_customizations 表及对应接口, 支持 BYOK 模型渠道密钥落库(仅回显掩码)与按任务类型定制 平台提示词模板;前端新增 /settings 页面及入口。
This commit is contained in:
1 parent
ae6cdf1551
commit
b9df2f84ab
27 files changed
+1515
-22
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { Plus } from "lucide-react";
|
||||
import { Plus, Settings } from "lucide-react";
|
||||
import { useNavigate } from "react-router";
|
||||
import { createCanvas, listCanvases } from "@/services/api/canvas";
|
||||
|
||||
@@ -26,15 +26,25 @@ export function CanvasListPage() {
|
||||
AI 视频创作工作台
|
||||
</p>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => createMutation.mutate()}
|
||||
disabled={createMutation.isPending}
|
||||
className="flex items-center gap-1.5 rounded-md bg-[var(--color-accent)] px-3 py-1.5 text-sm text-white transition hover:opacity-90 disabled:opacity-50"
|
||||
>
|
||||
<Plus className="h-4 w-4" />
|
||||
新建画布
|
||||
</button>
|
||||
<div className="flex items-center gap-2">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => navigate("/settings")}
|
||||
className="flex items-center gap-1.5 rounded-md border border-[var(--color-panel-border)] px-3 py-1.5 text-sm text-[var(--color-ink-muted)] transition hover:border-[var(--color-accent)] hover:text-[var(--color-ink)]"
|
||||
>
|
||||
<Settings className="h-4 w-4" />
|
||||
配置
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => createMutation.mutate()}
|
||||
disabled={createMutation.isPending}
|
||||
className="flex items-center gap-1.5 rounded-md bg-[var(--color-accent)] px-3 py-1.5 text-sm text-white transition hover:opacity-90 disabled:opacity-50"
|
||||
>
|
||||
<Plus className="h-4 w-4" />
|
||||
新建画布
|
||||
</button>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
{canvasesQuery.isLoading ? <p className="text-sm text-[var(--color-ink-muted)]">加载中…</p> : null}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { ArrowLeft } from "lucide-react";
|
||||
import { ArrowLeft, Settings } from "lucide-react";
|
||||
import { useEffect } from "react";
|
||||
import { useNavigate, useParams } from "react-router";
|
||||
import { InfiniteCanvas } from "@/components/canvas/infinite-canvas";
|
||||
@@ -65,6 +65,14 @@ export function CanvasPage() {
|
||||
<span className="text-sm font-medium">
|
||||
{canvasQuery.data?.name ?? (canvasQuery.isError ? "画布加载失败" : "加载中…")}
|
||||
</span>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => navigate("/settings")}
|
||||
className="ml-auto flex items-center gap-1 text-xs text-[var(--color-ink-muted)] transition hover:text-[var(--color-ink)]"
|
||||
>
|
||||
<Settings className="h-3.5 w-3.5" />
|
||||
配置
|
||||
</button>
|
||||
</header>
|
||||
|
||||
<div className="flex min-h-0 flex-1">
|
||||
|
||||
@@ -0,0 +1,187 @@
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { Plus, Save, Trash2 } from "lucide-react";
|
||||
import { useEffect, useState } from "react";
|
||||
import { TextField } from "./form-field";
|
||||
import { getPreference, updatePreference } from "@/services/api/preference";
|
||||
import { readErrorMessage } from "@/services/http";
|
||||
import type { APIKeyDraft, Preference, PreferencePayload } from "@/types/preference";
|
||||
|
||||
function toDrafts(preference: Preference): APIKeyDraft[] {
|
||||
// 密钥不回显:apiKey 始终为空,留空提交时后端沿用旧值。
|
||||
return preference.apiKeys.map((item) => ({ ...item, apiKey: "" }));
|
||||
}
|
||||
|
||||
function toPayload(drafts: APIKeyDraft[]): PreferencePayload {
|
||||
return {
|
||||
apiKeys: drafts.map(({ id, provider, label, baseUrl, apiKey, model, enabled }) => ({
|
||||
id,
|
||||
provider,
|
||||
label,
|
||||
baseUrl,
|
||||
apiKey,
|
||||
model,
|
||||
enabled,
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
export function ApiKeysSection() {
|
||||
const queryClient = useQueryClient();
|
||||
const preferenceQuery = useQuery({ queryKey: ["preference"], queryFn: getPreference });
|
||||
|
||||
const [drafts, setDrafts] = useState<APIKeyDraft[]>([]);
|
||||
const [savedAt, setSavedAt] = useState<number | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (preferenceQuery.data) setDrafts(toDrafts(preferenceQuery.data));
|
||||
}, [preferenceQuery.data]);
|
||||
|
||||
const saveMutation = useMutation({
|
||||
mutationFn: (payload: PreferencePayload) => updatePreference(payload),
|
||||
onSuccess: (data) => {
|
||||
queryClient.setQueryData(["preference"], data);
|
||||
setDrafts(toDrafts(data));
|
||||
setSavedAt(Date.now());
|
||||
},
|
||||
});
|
||||
|
||||
function applyDrafts(next: APIKeyDraft[]) {
|
||||
setDrafts(next);
|
||||
setSavedAt(null);
|
||||
}
|
||||
|
||||
function updateItem(id: string, patch: Partial<APIKeyDraft>) {
|
||||
applyDrafts(drafts.map((item) => (item.id === id ? { ...item, ...patch } : item)));
|
||||
}
|
||||
|
||||
function addItem() {
|
||||
applyDrafts([
|
||||
...drafts,
|
||||
{
|
||||
id: crypto.randomUUID(),
|
||||
provider: "openai-compatible",
|
||||
label: "",
|
||||
baseUrl: "",
|
||||
model: "",
|
||||
enabled: true,
|
||||
hasKey: false,
|
||||
keyMask: "",
|
||||
apiKey: "",
|
||||
},
|
||||
]);
|
||||
}
|
||||
|
||||
return (
|
||||
<section className="rounded-lg border border-[var(--color-panel-border)] bg-[var(--color-panel)] p-4">
|
||||
<header className="flex items-start justify-between gap-3">
|
||||
<div>
|
||||
<h2 className="text-sm font-medium">模型渠道 API Keys</h2>
|
||||
<p className="mt-0.5 text-xs text-[var(--color-ink-muted)]">
|
||||
密钥保存在本机后端数据库,读取时只回显掩码;输入框留空表示沿用已保存的密钥。
|
||||
</p>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
onClick={addItem}
|
||||
className="flex shrink-0 items-center gap-1.5 rounded-md border border-[var(--color-panel-border)] px-2.5 py-1.5 text-xs text-[var(--color-ink-muted)] transition hover:border-[var(--color-accent)] hover:text-[var(--color-ink)]"
|
||||
>
|
||||
<Plus className="h-3.5 w-3.5" />
|
||||
添加渠道
|
||||
</button>
|
||||
</header>
|
||||
|
||||
{preferenceQuery.isError ? (
|
||||
<p className="mt-3 text-xs text-red-600">渠道配置加载失败,请确认后端已启动。</p>
|
||||
) : null}
|
||||
|
||||
{drafts.length === 0 ? (
|
||||
<p className="mt-4 text-xs text-[var(--color-ink-muted)]">
|
||||
还没有配置渠道。未配置时使用服务端环境变量里的默认渠道。
|
||||
</p>
|
||||
) : null}
|
||||
|
||||
<ul className="mt-4 flex flex-col gap-3">
|
||||
{drafts.map((item, index) => (
|
||||
<li key={item.id} className="rounded-md border border-[var(--color-panel-border)] p-3">
|
||||
<div className="flex items-center justify-between gap-2">
|
||||
<span className="text-xs font-medium">渠道 {index + 1}</span>
|
||||
<div className="flex items-center gap-3">
|
||||
<label className="flex cursor-pointer items-center gap-1.5 text-xs text-[var(--color-ink-muted)]">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={item.enabled}
|
||||
onChange={(event) => updateItem(item.id, { enabled: event.target.checked })}
|
||||
className="accent-[var(--color-accent)]"
|
||||
/>
|
||||
启用
|
||||
</label>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => applyDrafts(drafts.filter((draft) => draft.id !== item.id))}
|
||||
title="删除渠道"
|
||||
aria-label="删除渠道"
|
||||
className="text-[var(--color-ink-muted)] transition hover:text-red-600"
|
||||
>
|
||||
<Trash2 className="h-3.5 w-3.5" />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="mt-3 grid grid-cols-1 gap-3 sm:grid-cols-2">
|
||||
<TextField
|
||||
label="渠道标识"
|
||||
value={item.provider}
|
||||
onChange={(value) => updateItem(item.id, { provider: value })}
|
||||
placeholder="openai-compatible"
|
||||
/>
|
||||
<TextField
|
||||
label="显示名称"
|
||||
value={item.label}
|
||||
onChange={(value) => updateItem(item.id, { label: value })}
|
||||
placeholder="例如:自建中转"
|
||||
/>
|
||||
<TextField
|
||||
label="Base URL"
|
||||
type="url"
|
||||
value={item.baseUrl}
|
||||
onChange={(value) => updateItem(item.id, { baseUrl: value })}
|
||||
placeholder="https://api.example.com/v1"
|
||||
/>
|
||||
<TextField
|
||||
label="默认模型"
|
||||
value={item.model}
|
||||
onChange={(value) => updateItem(item.id, { model: value })}
|
||||
placeholder="例如:gpt-image-1"
|
||||
/>
|
||||
<div className="sm:col-span-2">
|
||||
<TextField
|
||||
label="API Key"
|
||||
type="password"
|
||||
value={item.apiKey}
|
||||
onChange={(value) => updateItem(item.id, { apiKey: value })}
|
||||
placeholder={item.hasKey ? `已保存 ${item.keyMask},留空不修改` : "sk-..."}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
|
||||
<div className="mt-4 flex items-center justify-end gap-3">
|
||||
{savedAt ? <span className="text-xs text-emerald-600">已保存</span> : null}
|
||||
{saveMutation.isError ? (
|
||||
<span className="text-xs text-red-600">保存失败:{readErrorMessage(saveMutation.error)}</span>
|
||||
) : null}
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => saveMutation.mutate(toPayload(drafts))}
|
||||
disabled={saveMutation.isPending}
|
||||
className="flex shrink-0 items-center gap-1.5 rounded-md bg-[var(--color-accent)] px-3 py-1.5 text-sm text-white transition hover:opacity-90 disabled:opacity-50"
|
||||
>
|
||||
<Save className="h-3.5 w-3.5" />
|
||||
{saveMutation.isPending ? "保存中…" : "保存渠道配置"}
|
||||
</button>
|
||||
</div>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
interface TextFieldProps {
|
||||
label: string;
|
||||
value: string;
|
||||
onChange: (value: string) => void;
|
||||
placeholder?: string;
|
||||
type?: "text" | "password" | "url";
|
||||
}
|
||||
|
||||
export function TextField({ label, value, onChange, placeholder, type = "text" }: TextFieldProps) {
|
||||
return (
|
||||
<label className="flex flex-col gap-1">
|
||||
<span className="text-xs text-[var(--color-ink-muted)]">{label}</span>
|
||||
<input
|
||||
type={type}
|
||||
value={value}
|
||||
placeholder={placeholder}
|
||||
autoComplete="off"
|
||||
onChange={(event) => onChange(event.target.value)}
|
||||
className="rounded-md border border-[var(--color-panel-border)] bg-[var(--color-panel)] px-2.5 py-1.5 text-sm outline-none transition focus:border-[var(--color-accent)]"
|
||||
/>
|
||||
</label>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import { ArrowLeft } from "lucide-react";
|
||||
import { useNavigate } from "react-router";
|
||||
import { ApiKeysSection } from "./api-keys-section";
|
||||
import { PromptPreferencesSection } from "./prompt-preferences-section";
|
||||
|
||||
export function SettingsPage() {
|
||||
const navigate = useNavigate();
|
||||
|
||||
return (
|
||||
<div className="mx-auto flex min-h-full w-full max-w-4xl flex-col gap-5 px-6 py-10">
|
||||
<header className="flex items-start gap-3">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => navigate("/")}
|
||||
className="mt-1 flex items-center gap-1 text-xs text-[var(--color-ink-muted)] transition hover:text-[var(--color-ink)]"
|
||||
>
|
||||
<ArrowLeft className="h-3.5 w-3.5" />
|
||||
画布列表
|
||||
</button>
|
||||
<div>
|
||||
<h1 className="text-lg font-semibold">配置与用户偏好</h1>
|
||||
<p className="mt-0.5 text-xs text-[var(--color-ink-muted)]">
|
||||
管理模型渠道密钥与提示词偏好,各分区独立保存
|
||||
</p>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<ApiKeysSection />
|
||||
<PromptPreferencesSection />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,275 @@
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { Check, Copy, RotateCcw, Save } from "lucide-react";
|
||||
import { useEffect, useState } from "react";
|
||||
import { cn } from "@/lib/utils";
|
||||
import {
|
||||
customizePrompt,
|
||||
listPromptPreferences,
|
||||
resetPromptCustomization,
|
||||
} from "@/services/api/prompt";
|
||||
import { readErrorMessage } from "@/services/http";
|
||||
import type { PromptCustomizationMode, PromptPreference } from "@/types/prompt";
|
||||
|
||||
const MODE_OPTIONS: { value: PromptCustomizationMode; label: string; description: string }[] = [
|
||||
{ value: "inherit", label: "跟随平台", description: "直接使用平台默认模板" },
|
||||
{ value: "append", label: "追加要求", description: "在平台模板后追加个人要求" },
|
||||
{ value: "rewrite", label: "高级改写", description: "完全使用自己的提示词" },
|
||||
];
|
||||
|
||||
export function PromptPreferencesSection() {
|
||||
const queryClient = useQueryClient();
|
||||
const preferencesQuery = useQuery({ queryKey: ["prompts"], queryFn: listPromptPreferences });
|
||||
|
||||
const preferences = preferencesQuery.data?.preferences ?? [];
|
||||
const [activeOperation, setActiveOperation] = useState("");
|
||||
const active: PromptPreference | undefined =
|
||||
preferences.find((item) => item.definition.operation === activeOperation) ?? preferences[0];
|
||||
|
||||
const [mode, setMode] = useState<PromptCustomizationMode>("inherit");
|
||||
const [appendContent, setAppendContent] = useState("");
|
||||
const [rewriteContent, setRewriteContent] = useState("");
|
||||
const [savedAt, setSavedAt] = useState<number | null>(null);
|
||||
const [copied, setCopied] = useState(false);
|
||||
|
||||
// 选中任务类型或服务端数据变化时,用已保存的定制重置草稿。
|
||||
useEffect(() => {
|
||||
const customization = active?.customization;
|
||||
setMode(customization?.mode ?? "inherit");
|
||||
setAppendContent(customization?.mode === "append" ? customization.content : "");
|
||||
setRewriteContent(
|
||||
customization?.mode === "rewrite" ? customization.content : (active?.template?.content ?? ""),
|
||||
);
|
||||
setSavedAt(null);
|
||||
}, [active]);
|
||||
|
||||
const savedMode = active?.customization?.mode ?? "inherit";
|
||||
const savedAppend = savedMode === "append" ? (active?.customization?.content ?? "") : "";
|
||||
const savedRewrite =
|
||||
savedMode === "rewrite"
|
||||
? (active?.customization?.content ?? "")
|
||||
: (active?.template?.content ?? "");
|
||||
const draftContent = mode === "append" ? appendContent : mode === "rewrite" ? rewriteContent : "";
|
||||
const savedContent = savedMode === "append" ? savedAppend : savedMode === "rewrite" ? savedRewrite : "";
|
||||
const dirty = mode !== savedMode || draftContent !== savedContent;
|
||||
|
||||
const templateContent = active?.template?.content ?? "当前没有可用的平台模板";
|
||||
const previewContent =
|
||||
mode === "inherit"
|
||||
? templateContent
|
||||
: mode === "append"
|
||||
? `${templateContent}\n\n【用户个性化创作要求】\n${appendContent}`
|
||||
: rewriteContent;
|
||||
|
||||
const saveMutation = useMutation({
|
||||
mutationFn: (input: { operation: string; mode: PromptCustomizationMode; content: string }) =>
|
||||
customizePrompt(input.operation, { mode: input.mode, content: input.content }),
|
||||
onSuccess: (data) => {
|
||||
const updated = data.preference;
|
||||
queryClient.setQueryData<{ preferences: PromptPreference[] }>(["prompts"], (current) =>
|
||||
current
|
||||
? {
|
||||
preferences: current.preferences.map((item) =>
|
||||
item.definition.operation === updated.definition.operation ? updated : item,
|
||||
),
|
||||
}
|
||||
: current,
|
||||
);
|
||||
setSavedAt(Date.now());
|
||||
},
|
||||
});
|
||||
|
||||
const resetMutation = useMutation({
|
||||
mutationFn: (operation: string) => resetPromptCustomization(operation),
|
||||
onSuccess: () => queryClient.invalidateQueries({ queryKey: ["prompts"] }),
|
||||
});
|
||||
|
||||
function selectOperation(operation: string) {
|
||||
if (operation === active?.definition.operation) return;
|
||||
if (dirty && !window.confirm("当前任务类型还有未保存的修改,切换后将丢失。确定切换吗?")) return;
|
||||
setActiveOperation(operation);
|
||||
}
|
||||
|
||||
function handleSave() {
|
||||
if (!active) return;
|
||||
saveMutation.mutate({ operation: active.definition.operation, mode, content: draftContent });
|
||||
}
|
||||
|
||||
function handleReset() {
|
||||
if (!active) return;
|
||||
if (!window.confirm(`将删除「${active.definition.label}」的个人定制,后续跟随平台模板。确定恢复吗?`)) {
|
||||
return;
|
||||
}
|
||||
resetMutation.mutate(active.definition.operation);
|
||||
}
|
||||
|
||||
async function handleCopy() {
|
||||
try {
|
||||
await navigator.clipboard.writeText(previewContent);
|
||||
setCopied(true);
|
||||
window.setTimeout(() => setCopied(false), 1500);
|
||||
} catch {
|
||||
// 剪贴板不可用时忽略
|
||||
}
|
||||
}
|
||||
|
||||
const busy = saveMutation.isPending || resetMutation.isPending;
|
||||
|
||||
return (
|
||||
<section className="rounded-lg border border-[var(--color-panel-border)] bg-[var(--color-panel)] p-4">
|
||||
<h2 className="text-sm font-medium">提示词偏好</h2>
|
||||
<p className="mt-0.5 text-xs text-[var(--color-ink-muted)]">
|
||||
按任务类型定制平台提示词模板。生成时按这里的配置组装最终提示词,可随时恢复平台模板。
|
||||
</p>
|
||||
|
||||
{preferencesQuery.isError ? (
|
||||
<p className="mt-3 text-xs text-red-600">提示词偏好加载失败,请确认后端已启动。</p>
|
||||
) : null}
|
||||
|
||||
{preferences.length === 0 && !preferencesQuery.isError ? (
|
||||
<p className="mt-4 text-xs text-[var(--color-ink-muted)]">加载中…</p>
|
||||
) : null}
|
||||
|
||||
{preferences.length > 0 ? (
|
||||
<div className="mt-4 flex flex-wrap gap-1.5">
|
||||
{preferences.map((item) => (
|
||||
<button
|
||||
key={item.definition.operation}
|
||||
type="button"
|
||||
onClick={() => selectOperation(item.definition.operation)}
|
||||
className={cn(
|
||||
"flex items-center gap-1.5 rounded-md border px-2.5 py-1.5 text-xs transition",
|
||||
item.definition.operation === active?.definition.operation
|
||||
? "border-[var(--color-accent)] text-[var(--color-ink)]"
|
||||
: "border-[var(--color-panel-border)] text-[var(--color-ink-muted)] hover:border-[var(--color-accent)] hover:text-[var(--color-ink)]",
|
||||
)}
|
||||
>
|
||||
{item.definition.label}
|
||||
{item.customization ? (
|
||||
<span className="inline-block h-1.5 w-1.5 rounded-full bg-[var(--color-accent)]" />
|
||||
) : null}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
{active ? (
|
||||
<>
|
||||
<div className="mt-4 rounded-md border border-[var(--color-panel-border)] p-3">
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
<span className="text-xs font-medium">{active.definition.label}</span>
|
||||
<span className="rounded bg-black/5 px-1.5 py-0.5 text-[10px] text-[var(--color-ink-muted)]">
|
||||
{active.definition.category}
|
||||
</span>
|
||||
<span className="rounded bg-black/5 px-1.5 py-0.5 text-[10px] text-[var(--color-ink-muted)]">
|
||||
平台模板 v{active.template?.version ?? 1}
|
||||
</span>
|
||||
{active.outdated ? (
|
||||
<span className="text-[10px] text-amber-600">平台模板已更新,建议复核定制内容</span>
|
||||
) : null}
|
||||
</div>
|
||||
<p className="mt-1 text-xs text-[var(--color-ink-muted)]">{active.definition.description}</p>
|
||||
{active.definition.variables.length > 0 ? (
|
||||
<p className="mt-1.5 text-[11px] leading-relaxed text-[var(--color-ink-muted)]">
|
||||
可用变量:{active.definition.variables.map((variable) => variable.placeholder).join(" ")}
|
||||
</p>
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
<div className="mt-4 grid grid-cols-1 gap-2 sm:grid-cols-3">
|
||||
{MODE_OPTIONS.map((option) => (
|
||||
<button
|
||||
key={option.value}
|
||||
type="button"
|
||||
onClick={() => {
|
||||
setMode(option.value);
|
||||
setSavedAt(null);
|
||||
}}
|
||||
className={cn(
|
||||
"rounded-md border px-3 py-2 text-left transition",
|
||||
mode === option.value
|
||||
? "border-[var(--color-accent)] bg-[var(--color-accent)]/5"
|
||||
: "border-[var(--color-panel-border)] hover:border-[var(--color-accent)]",
|
||||
)}
|
||||
>
|
||||
<span className="block text-xs font-medium">{option.label}</span>
|
||||
<span className="mt-0.5 block text-[11px] leading-relaxed text-[var(--color-ink-muted)]">
|
||||
{option.description}
|
||||
</span>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{mode === "inherit" ? (
|
||||
<p className="mt-4 text-xs text-[var(--color-ink-muted)]">
|
||||
当前跟随平台模板,生成时直接使用平台默认内容,无需填写。
|
||||
</p>
|
||||
) : (
|
||||
<div className="mt-4 flex flex-col gap-1">
|
||||
<span className="text-xs text-[var(--color-ink-muted)]">
|
||||
{mode === "append" ? "个人要求(追加在平台模板之后)" : "个人提示词(完全替换平台模板)"}
|
||||
</span>
|
||||
<textarea
|
||||
value={mode === "append" ? appendContent : rewriteContent}
|
||||
onChange={(event) => {
|
||||
if (mode === "append") setAppendContent(event.target.value);
|
||||
else setRewriteContent(event.target.value);
|
||||
setSavedAt(null);
|
||||
}}
|
||||
rows={9}
|
||||
placeholder={mode === "append" ? "例如:镜头语言偏电影感,避免出现文字" : "以平台模板为起点改写"}
|
||||
className="w-full resize-y rounded-md border border-[var(--color-panel-border)] bg-[var(--color-panel)] px-2.5 py-2 text-xs leading-relaxed outline-none transition focus:border-[var(--color-accent)]"
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="mt-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="text-xs text-[var(--color-ink-muted)]">生成时实际使用的提示词</span>
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleCopy}
|
||||
className="flex items-center gap-1 text-[11px] text-[var(--color-ink-muted)] transition hover:text-[var(--color-ink)]"
|
||||
>
|
||||
{copied ? <Check className="h-3 w-3" /> : <Copy className="h-3 w-3" />}
|
||||
{copied ? "已复制" : "复制"}
|
||||
</button>
|
||||
</div>
|
||||
<pre className="mt-1.5 max-h-64 overflow-auto whitespace-pre-wrap rounded-md border border-[var(--color-panel-border)] bg-black/[0.02] p-3 text-[11px] leading-relaxed text-[var(--color-ink-muted)]">
|
||||
{previewContent || "(空)"}
|
||||
</pre>
|
||||
</div>
|
||||
|
||||
<div className="mt-4 flex items-center justify-end gap-3">
|
||||
{savedAt ? <span className="text-xs text-emerald-600">已保存</span> : null}
|
||||
{saveMutation.isError ? (
|
||||
<span className="text-xs text-red-600">保存失败:{readErrorMessage(saveMutation.error)}</span>
|
||||
) : null}
|
||||
{resetMutation.isError ? (
|
||||
<span className="text-xs text-red-600">恢复失败:{readErrorMessage(resetMutation.error)}</span>
|
||||
) : null}
|
||||
{active.customization ? (
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleReset}
|
||||
disabled={busy}
|
||||
className="flex items-center gap-1.5 rounded-md border border-[var(--color-panel-border)] px-2.5 py-1.5 text-xs text-[var(--color-ink-muted)] transition hover:border-[var(--color-accent)] hover:text-[var(--color-ink)] disabled:opacity-50"
|
||||
>
|
||||
<RotateCcw className="h-3.5 w-3.5" />
|
||||
恢复平台模板
|
||||
</button>
|
||||
) : null}
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleSave}
|
||||
disabled={!dirty || busy}
|
||||
className="flex shrink-0 items-center gap-1.5 rounded-md bg-[var(--color-accent)] px-3 py-1.5 text-sm text-white transition hover:opacity-90 disabled:opacity-50"
|
||||
>
|
||||
<Save className="h-3.5 w-3.5" />
|
||||
{saveMutation.isPending ? "保存中…" : "保存提示词偏好"}
|
||||
</button>
|
||||
</div>
|
||||
</>
|
||||
) : null}
|
||||
</section>
|
||||
);
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
import { createBrowserRouter } from "react-router";
|
||||
import { CanvasPage } from "@/pages/canvas";
|
||||
import { CanvasListPage } from "@/pages/canvas-list";
|
||||
import { SettingsPage } from "@/pages/settings";
|
||||
|
||||
export const router = createBrowserRouter([
|
||||
{ path: "/", element: <CanvasListPage /> },
|
||||
{ path: "/canvas/:canvasId", element: <CanvasPage /> },
|
||||
{ path: "/settings", element: <SettingsPage /> },
|
||||
]);
|
||||
@@ -0,0 +1,13 @@
|
||||
import { request } from "@/services/http";
|
||||
import type { Preference, PreferencePayload } from "@/types/preference";
|
||||
|
||||
export function getPreference() {
|
||||
return request<Preference>("/preferences");
|
||||
}
|
||||
|
||||
export function updatePreference(payload: PreferencePayload) {
|
||||
return request<Preference>("/preferences", {
|
||||
method: "PUT",
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import { request } from "@/services/http";
|
||||
import type { PromptCustomizationMode, PromptPreference } from "@/types/prompt";
|
||||
|
||||
export function listPromptPreferences() {
|
||||
return request<{ preferences: PromptPreference[] }>("/prompts");
|
||||
}
|
||||
|
||||
/** 保存定制;mode 传 inherit 即放弃定制,后端会删除已有记录。 */
|
||||
export function customizePrompt(
|
||||
operation: string,
|
||||
input: { mode: PromptCustomizationMode; content: string },
|
||||
) {
|
||||
return request<{ preference: PromptPreference }>(`/prompts/${encodeURIComponent(operation)}`, {
|
||||
method: "PATCH",
|
||||
body: JSON.stringify(input),
|
||||
});
|
||||
}
|
||||
|
||||
export function resetPromptCustomization(operation: string) {
|
||||
return request<{ ok: boolean }>(`/prompts/${encodeURIComponent(operation)}`, { method: "DELETE" });
|
||||
}
|
||||
@@ -30,3 +30,15 @@ export async function request<T>(path: string, init: RequestInit = {}): Promise<
|
||||
}
|
||||
|
||||
export const apiBaseUrl = BASE_URL;
|
||||
|
||||
/** 后端错误体形如 {"error":"..."},取出来直接展示;解析失败时退回原始信息。 */
|
||||
export function readErrorMessage(error: unknown, fallback = "请稍后重试"): string {
|
||||
if (!(error instanceof Error)) return fallback;
|
||||
try {
|
||||
const parsed = JSON.parse(error.message) as { error?: string };
|
||||
if (parsed?.error) return parsed.error;
|
||||
} catch {
|
||||
// 非 JSON 响应,退回原始信息
|
||||
}
|
||||
return error.message || fallback;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/** 服务端返回的渠道配置,只含掩码,不含明文密钥。 */
|
||||
export interface APIKeyConfig {
|
||||
id: string;
|
||||
provider: string;
|
||||
label: string;
|
||||
baseUrl: string;
|
||||
model: string;
|
||||
enabled: boolean;
|
||||
hasKey: boolean;
|
||||
keyMask: string;
|
||||
}
|
||||
|
||||
export interface Preference {
|
||||
apiKeys: APIKeyConfig[];
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
/** 表单内的渠道配置:`apiKey` 只在本地编辑,留空表示沿用已保存的密钥。 */
|
||||
export interface APIKeyDraft extends APIKeyConfig {
|
||||
apiKey: string;
|
||||
}
|
||||
|
||||
export interface APIKeyPayload {
|
||||
id: string;
|
||||
provider: string;
|
||||
label: string;
|
||||
baseUrl: string;
|
||||
apiKey: string;
|
||||
model: string;
|
||||
enabled: boolean;
|
||||
}
|
||||
|
||||
export interface PreferencePayload {
|
||||
apiKeys: APIKeyPayload[];
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/** 提示词任务类型的输出形态。 */
|
||||
export type PromptOutputType = "text" | "json";
|
||||
|
||||
/** 用户定制模式:跟随平台模板 / 在模板后追加要求 / 完全改写。 */
|
||||
export type PromptCustomizationMode = "inherit" | "append" | "rewrite";
|
||||
|
||||
export interface PromptTemplateVariable {
|
||||
label: string;
|
||||
placeholder: string;
|
||||
}
|
||||
|
||||
/** 一个提示词任务类型的元信息。任务类型由后端内置目录提供。 */
|
||||
export interface PromptOperationDefinition {
|
||||
operation: string;
|
||||
label: string;
|
||||
category: string;
|
||||
description: string;
|
||||
outputType: PromptOutputType;
|
||||
variables: PromptTemplateVariable[];
|
||||
}
|
||||
|
||||
/** 平台默认模板。 */
|
||||
export interface PromptTemplate {
|
||||
operation: string;
|
||||
name: string;
|
||||
version: number;
|
||||
content: string;
|
||||
outputType: PromptOutputType;
|
||||
}
|
||||
|
||||
/** 用户对某个任务类型的提示词定制。 */
|
||||
export interface PromptCustomization {
|
||||
id: string;
|
||||
operation: string;
|
||||
mode: PromptCustomizationMode;
|
||||
content: string;
|
||||
baseTemplateVersion: number;
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
/** 一个任务类型的完整提示词配置。 */
|
||||
export interface PromptPreference {
|
||||
definition: PromptOperationDefinition;
|
||||
template: PromptTemplate | null;
|
||||
customization?: PromptCustomization;
|
||||
/** 定制依据的平台模板版本已落后于当前版本。 */
|
||||
outdated: boolean;
|
||||
}
|
||||
Reference in new issue
Block a user