255 lines
9.0 KiB
TypeScript
255 lines
9.0 KiB
TypeScript
import { useEffect, useMemo, useState } from 'react';
|
|
import { PenLine } from 'lucide-react';
|
|
|
|
import { Button } from '@/components/ui/button';
|
|
import { Textarea } from '@/components/ui/textarea';
|
|
import {
|
|
Select,
|
|
SelectContent,
|
|
SelectItem,
|
|
SelectTrigger,
|
|
SelectValue,
|
|
} from '@/components/ui/select';
|
|
import { useModels } from '@/core/models/hooks';
|
|
|
|
import type { AIWritingRequest } from '../../types/ai-writing';
|
|
import { useArticleTypes } from '../../hooks/useArticleTypes';
|
|
|
|
interface AIWritingFormProps {
|
|
onSubmit: (req: AIWritingRequest) => void;
|
|
disabled?: boolean;
|
|
/** workspace 入口:隐藏严格模式、目标读者 */
|
|
hideAdvancedFields?: boolean;
|
|
}
|
|
|
|
const INPUT_CLASS =
|
|
'flex h-12 w-full rounded-xl border border-[#e8dedd] bg-white px-4 py-2 text-sm shadow-sm ' +
|
|
'placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 ' +
|
|
'focus-visible:ring-ring disabled:opacity-50';
|
|
|
|
const DEFAULT_KEYWORD_COUNT = 1;
|
|
const DEFAULT_AUDIENCE = '普通读者';
|
|
const MAX_INTENT_LENGTH = 500;
|
|
|
|
const FIELD_LABEL_CLASS = 'text-[16px] font-semibold leading-none text-[#222]';
|
|
const FIELD_TRIGGER_CLASS =
|
|
'h-12 w-full rounded-xl border-[#e8dedd] bg-white text-sm shadow-sm';
|
|
|
|
export function AIWritingForm({
|
|
onSubmit,
|
|
disabled,
|
|
hideAdvancedFields = false,
|
|
}: AIWritingFormProps) {
|
|
const [intent, setIntent] = useState('');
|
|
const [articleType, setArticleType] = useState('analysis');
|
|
const [audience, setAudience] = useState('普通读者');
|
|
const [wordCountInput, setWordCountInput] = useState('800');
|
|
const [strictMode, setStrictMode] = useState(false);
|
|
const [modelName, setModelName] = useState('');
|
|
const { models, isLoading: modelsLoading } = useModels();
|
|
|
|
const { articleTypes, loading: typesLoading, refresh: refreshTypes } = useArticleTypes();
|
|
|
|
// 首次 /api/models 返回后默认选第一项;配置变更导致当前值不在列表时也回退到首项。
|
|
useEffect(() => {
|
|
if (models.length === 0) return;
|
|
const stillExists = models.some((m) => m.name === modelName);
|
|
if (!modelName || !stillExists) {
|
|
setModelName(models[0].name);
|
|
}
|
|
}, [models, modelName]);
|
|
|
|
const selectedModel = useMemo(
|
|
() => models.find((m) => m.name === modelName) ?? models[0],
|
|
[models, modelName],
|
|
);
|
|
|
|
// 初始化时加载文章类型
|
|
useEffect(() => {
|
|
refreshTypes();
|
|
}, [refreshTypes]);
|
|
|
|
// 类型加载完成后:若当前选中值不在列表中则切换为第一项
|
|
useEffect(() => {
|
|
if (articleTypes.length === 0) return;
|
|
const keys = articleTypes.map((t) => t.key);
|
|
if (!keys.includes(articleType)) {
|
|
const first = articleTypes[0];
|
|
setArticleType(first.key);
|
|
setWordCountInput(String(first.default_word_count));
|
|
}
|
|
}, [articleTypes]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
|
|
// 切换文章类型时自动填入该类型的默认字数
|
|
function handleArticleTypeChange(key: string) {
|
|
setArticleType(key);
|
|
const found = articleTypes.find((t) => t.key === key);
|
|
if (found) {
|
|
setWordCountInput(String(found.default_word_count));
|
|
}
|
|
}
|
|
|
|
function handleWordCountBlur() {
|
|
const num = parseInt(wordCountInput, 10);
|
|
if (isNaN(num) || num < 100) {
|
|
setWordCountInput('800');
|
|
} else {
|
|
setWordCountInput(String(num));
|
|
}
|
|
}
|
|
|
|
function handleSubmit(e: React.FormEvent) {
|
|
e.preventDefault();
|
|
if (!intent.trim()) return;
|
|
const wordCount = Math.max(100, parseInt(wordCountInput, 10) || 800);
|
|
onSubmit({
|
|
userIntent: intent.trim(),
|
|
articleType,
|
|
targetAudience: hideAdvancedFields
|
|
? DEFAULT_AUDIENCE
|
|
: audience.trim() || DEFAULT_AUDIENCE,
|
|
wordCountTarget: wordCount,
|
|
keywordCount: DEFAULT_KEYWORD_COUNT,
|
|
writingMode: hideAdvancedFields ? 'loose' : strictMode ? 'strict' : 'loose',
|
|
modelName: selectedModel?.name || modelName || undefined,
|
|
});
|
|
}
|
|
|
|
return (
|
|
<form onSubmit={handleSubmit} className="space-y-4 px-4 py-4">
|
|
<div className="rounded-2xl border border-[#ebe3e2] bg-[#fcf8f8] p-4">
|
|
<div className="flex items-center gap-2 border-b border-[#ece5e4] pb-3">
|
|
<PenLine className="size-4 text-[#8b6f6f]" />
|
|
<h3 className="text-[18px] font-semibold leading-none text-[#2f2f2f]">写作配置选项</h3>
|
|
</div>
|
|
|
|
<div className="mt-4 space-y-2">
|
|
<div className="flex items-center justify-between">
|
|
<label className={FIELD_LABEL_CLASS}>写作意图</label>
|
|
<span className="text-sm text-muted-foreground">
|
|
{intent.length} / {MAX_INTENT_LENGTH} 字
|
|
</span>
|
|
</div>
|
|
<Textarea
|
|
placeholder="描述你想写的文章,例如:写一篇分析今年 AI 大模型发展趋势的文章,重点关注国内企业的布局..."
|
|
value={intent}
|
|
onChange={(e) => setIntent(e.target.value)}
|
|
disabled={disabled}
|
|
rows={5}
|
|
maxLength={MAX_INTENT_LENGTH}
|
|
className="resize-none rounded-2xl border-[#e8dedd] bg-white px-4 py-3 text-sm leading-6 shadow-sm"
|
|
/>
|
|
</div>
|
|
|
|
<div className="mt-4 grid grid-cols-2 gap-4">
|
|
<div className="min-w-0 space-y-2">
|
|
<label className={FIELD_LABEL_CLASS}>文章类型</label>
|
|
<Select
|
|
value={articleType}
|
|
onValueChange={handleArticleTypeChange}
|
|
disabled={disabled || typesLoading}
|
|
>
|
|
<SelectTrigger className={FIELD_TRIGGER_CLASS}>
|
|
<SelectValue placeholder={typesLoading ? '加载中…' : '选择类型'} />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
{articleTypes.map((t) => (
|
|
<SelectItem key={t.key} value={t.key} className="text-sm">
|
|
{t.label}
|
|
</SelectItem>
|
|
))}
|
|
</SelectContent>
|
|
</Select>
|
|
</div>
|
|
|
|
<div className="min-w-0 space-y-2">
|
|
<label className={FIELD_LABEL_CLASS}>目标字数</label>
|
|
<input
|
|
type="number"
|
|
min={100}
|
|
step={100}
|
|
className={INPUT_CLASS}
|
|
placeholder="800"
|
|
value={wordCountInput}
|
|
onChange={(e) => setWordCountInput(e.target.value)}
|
|
onBlur={handleWordCountBlur}
|
|
disabled={disabled}
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="mt-4 space-y-2">
|
|
<label className={FIELD_LABEL_CLASS}>写作模型</label>
|
|
<Select
|
|
value={selectedModel?.name ?? ''}
|
|
onValueChange={setModelName}
|
|
disabled={disabled || modelsLoading || models.length === 0}
|
|
>
|
|
<SelectTrigger className={FIELD_TRIGGER_CLASS}>
|
|
<SelectValue
|
|
placeholder={
|
|
modelsLoading
|
|
? '模型加载中…'
|
|
: models.length > 0
|
|
? '选择模型…'
|
|
: '暂无可用模型'
|
|
}
|
|
/>
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
{models.map((m) => (
|
|
<SelectItem key={m.name} value={m.name} className="text-sm">
|
|
{m.display_name || m.name}
|
|
</SelectItem>
|
|
))}
|
|
</SelectContent>
|
|
</Select>
|
|
</div>
|
|
|
|
<Button
|
|
type="submit"
|
|
disabled={disabled || !intent.trim()}
|
|
className="mt-4 h-12 w-full gap-2 rounded-2xl text-base font-semibold"
|
|
>
|
|
<PenLine className="size-4" />
|
|
开始 AI 写作
|
|
</Button>
|
|
</div>
|
|
|
|
{!hideAdvancedFields && (
|
|
<>
|
|
<div className="flex items-start gap-2 rounded-xl border border-[#e8dedd] bg-[#f9f5f5] px-3 py-3">
|
|
<input
|
|
type="checkbox"
|
|
id="strict-mode"
|
|
checked={strictMode}
|
|
onChange={(e) => setStrictMode(e.target.checked)}
|
|
disabled={disabled}
|
|
className="mt-0.5 size-3.5 shrink-0 cursor-pointer accent-primary"
|
|
/>
|
|
<label htmlFor="strict-mode" className="cursor-pointer select-none">
|
|
<span className="text-sm font-medium text-foreground/90">严格依据素材写作</span>
|
|
<p className="mt-0.5 text-xs leading-relaxed text-muted-foreground/80">
|
|
勾选后作家只依据检索素材写作,素材不足以支撑某章节时会暂停询问你;不勾选则可结合通用知识补充论述。
|
|
</p>
|
|
</label>
|
|
</div>
|
|
|
|
<div className="space-y-2">
|
|
<label className="text-sm font-semibold text-foreground">目标读者</label>
|
|
<input
|
|
type="text"
|
|
className={INPUT_CLASS}
|
|
placeholder="普通读者 / 行业专家 / 政策决策者…"
|
|
value={audience}
|
|
onChange={(e) => setAudience(e.target.value)}
|
|
disabled={disabled}
|
|
/>
|
|
</div>
|
|
</>
|
|
)}
|
|
|
|
</form>
|
|
);
|
|
}
|