- 多智能体会商:Step1 落地左栏「业务链(public 前3) + 分析记录」;点链=勾选(autoChainResult),第一步澄清后点「进入研讨」才按链执行
- zzJC/zzXD:独立模块路由 /page/strategy/qa/{zzjc,zzxd},常驻左栏(按 agent 标签分组 + 仅本模块会话的聊天记录),点智能体模块内切换不跳页,默认落地配置类型下首个智能体
- zzJC/zzXD admin 设置:左栏齿轮→单选类型(TDesign)配置面板+智能体预览;后端 system_settings.agent_modules 持久化(GET 开放/PUT admin)+ test_agent_modules
- 课题研究:左栏「研究模板(文章类型) + 笔记本」;模板点击预填表单,笔记本点击设为素材来源(selectedNotebookId 上提 WritingFormContext)
- 通用问答:全量模式历史侧栏默认展开
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
430 lines
17 KiB
TypeScript
430 lines
17 KiB
TypeScript
import { useEffect, useMemo } from 'react';
|
||
import { PenLine, SlidersHorizontal } 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 { useStudioNotebooks, useNotebookSources } from '@/core/studio/hooks';
|
||
|
||
import type { AIWritingRequest } from '../../types/ai-writing';
|
||
import { useArticleTypes } from '../../hooks/useArticleTypes';
|
||
import { useWritingForm } from '../../contexts/WritingFormContext';
|
||
|
||
interface AIWritingFormProps {
|
||
onSubmit: (req: AIWritingRequest) => void;
|
||
disabled?: boolean;
|
||
/** workspace 入口:隐藏严格模式、目标读者 */
|
||
hideAdvancedFields?: boolean;
|
||
/** 预选空间 ID(传入时自动切换素材来源为「我的空间」) */
|
||
initialNotebookId?: number;
|
||
/** 预选文档 ID 列表 */
|
||
initialDocumentIds?: number[];
|
||
/** 外部直接提供的 studio bearer token,跳过 getTokenByEmail */
|
||
studioToken?: string;
|
||
}
|
||
|
||
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 ' +
|
||
'dark:border-[#1B477E] dark:bg-[#022248] dark:text-[#AACEFF] dark:placeholder:text-[#566B7F] ' +
|
||
'dark:[color-scheme:dark] dark:focus-visible:border-[#3b82f6] dark:focus-visible:ring-[#3b82f6]/30';
|
||
|
||
const DEFAULT_KEYWORD_COUNT = 1;
|
||
const DEFAULT_AUDIENCE = '普通读者';
|
||
type MaterialSource = 'general' | 'notebook';
|
||
const MAX_INTENT_LENGTH = 500;
|
||
|
||
const MATERIAL_SOURCE_OPTIONS: Array<{ value: MaterialSource; label: string; desc: string }> = [
|
||
{ value: 'general', label: '通用检索', desc: '用素材收集专家配置的技能检索(默认:知识库检索)' },
|
||
{ value: 'notebook', label: '我的空间', desc: '从空间中检索素材' },
|
||
];
|
||
|
||
const FIELD_LABEL_CLASS = 'text-[16px] font-semibold leading-none text-[#222] dark:text-[#AACEFF]';
|
||
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,
|
||
studioToken,
|
||
}: AIWritingFormProps) {
|
||
const {
|
||
intent, setIntent,
|
||
showOutline, setShowOutline,
|
||
outlineText, setOutlineText,
|
||
articleType, setArticleType,
|
||
audience, setAudience,
|
||
wordCountInput, setWordCountInput,
|
||
strictMode, setStrictMode,
|
||
materialSource, setMaterialSource,
|
||
// 笔记来源(search_space_id + document_id 列表)已上提到 WritingFormContext,
|
||
// 以便左栏「笔记本」点击可直接设为素材来源(需求 0616 #5)。
|
||
selectedNotebookId, setSelectedNotebookId,
|
||
selectedDocumentIds, setSelectedDocumentIds,
|
||
modelName, setModelName,
|
||
} = useWritingForm();
|
||
|
||
const { models, isLoading: modelsLoading } = useModels();
|
||
const { notebooks, isLoading: notebooksLoading } = useStudioNotebooks(!studioToken);
|
||
const { sources, isLoading: sourcesLoading } = useNotebookSources(selectedNotebookId, studioToken);
|
||
|
||
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 handleNotebookChange(val: string) {
|
||
setSelectedNotebookId(val ? Number(val) : null);
|
||
setSelectedDocumentIds([]);
|
||
}
|
||
|
||
function handleMaterialSourceChange(val: MaterialSource) {
|
||
setMaterialSource(val);
|
||
// studioToken 模式下空间和文档已锁定,切换素材来源时不清空
|
||
if (val !== 'notebook' && !studioToken) {
|
||
setSelectedNotebookId(null);
|
||
setSelectedDocumentIds([]);
|
||
}
|
||
}
|
||
|
||
function toggleDocument(docId: number) {
|
||
setSelectedDocumentIds(
|
||
selectedDocumentIds.includes(docId)
|
||
? selectedDocumentIds.filter((id) => id !== docId)
|
||
: [...selectedDocumentIds, docId],
|
||
);
|
||
}
|
||
|
||
function handleSubmit(e: React.FormEvent) {
|
||
e.preventDefault();
|
||
if (!intent.trim()) return;
|
||
const wordCount = Math.max(100, parseInt(wordCountInput, 10) || 800);
|
||
onSubmit({
|
||
userIntent: intent.trim(),
|
||
userOutline: outlineText.trim() || undefined,
|
||
articleType,
|
||
targetAudience: hideAdvancedFields
|
||
? DEFAULT_AUDIENCE
|
||
: audience.trim() || DEFAULT_AUDIENCE,
|
||
wordCountTarget: wordCount,
|
||
keywordCount: DEFAULT_KEYWORD_COUNT,
|
||
writingMode: hideAdvancedFields ? 'loose' : strictMode ? 'strict' : 'loose',
|
||
materialSource: materialSource,
|
||
modelName: selectedModel?.name || modelName || undefined,
|
||
// 只有选了"我的空间"才传笔记参数
|
||
noteSearchSpaceId: materialSource === 'notebook' ? (selectedNotebookId ?? undefined) : undefined,
|
||
noteSourceIds: materialSource === 'notebook' && selectedNotebookId != null ? selectedDocumentIds : undefined,
|
||
});
|
||
}
|
||
|
||
return (
|
||
<form onSubmit={handleSubmit} className="space-y-4 px-4 py-4">
|
||
<div className="rounded-2xl border border-[#ece5e4] bg-card p-5 shadow-sm dark:border-[#213D5E] dark:bg-[#02244C] dark:text-white [&_h3]:dark:text-white [&_label]:dark:text-white">
|
||
<div className="flex items-center gap-2.5 border-b border-[#ece5e4] pb-3 dark:border-[#213D5E]">
|
||
<SlidersHorizontal className="size-4 text-primary dark:text-white" />
|
||
<h3 className="text-[18px] font-semibold leading-none tracking-tight text-[#2f2f2f] dark:text-white">
|
||
写作配置选项
|
||
</h3>
|
||
</div>
|
||
|
||
<div className="mt-4 space-y-2">
|
||
<div className="flex items-center justify-between">
|
||
<label className={FIELD_LABEL_CLASS}>
|
||
写作意图 <span className="text-primary dark:text-white">*</span>
|
||
</label>
|
||
<span className="text-sm text-muted-foreground dark:text-white/70">
|
||
{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 space-y-2">
|
||
<button
|
||
type="button"
|
||
onClick={() => setShowOutline(!showOutline)}
|
||
disabled={disabled}
|
||
className="flex w-full items-center justify-between text-left"
|
||
>
|
||
<span className={FIELD_LABEL_CLASS}>
|
||
已有大纲
|
||
<span className="ml-1 text-sm font-normal text-muted-foreground dark:text-white/70">
|
||
(可选)
|
||
</span>
|
||
</span>
|
||
<span className="text-sm text-muted-foreground dark:text-white/70">
|
||
{showOutline ? '收起 ▴' : '我已有大纲,直接粘贴 ▾'}
|
||
</span>
|
||
</button>
|
||
{showOutline && (
|
||
<>
|
||
<Textarea
|
||
placeholder={
|
||
'粘贴你已有的大纲,将完全按你的大纲生成(支持一/二/三级目录)。例如:\n' +
|
||
'一、引言\n(一)背景\n(二)意义\n二、现状分析\n…'
|
||
}
|
||
value={outlineText}
|
||
onChange={(e) => setOutlineText(e.target.value)}
|
||
disabled={disabled}
|
||
rows={6}
|
||
className="resize-none rounded-2xl border-[#e8dedd] bg-white px-4 py-3 text-sm leading-6 shadow-sm"
|
||
/>
|
||
<p className="text-xs leading-relaxed text-muted-foreground/80">
|
||
填写后将跳过自动大纲规划,按你粘贴的大纲逐章生成;留空则由 AI 自动规划大纲。
|
||
</p>
|
||
</>
|
||
)}
|
||
</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 position="popper" side="bottom" sideOffset={4}>
|
||
{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={materialSource}
|
||
onValueChange={(v) => handleMaterialSourceChange(v as MaterialSource)}
|
||
disabled={disabled}
|
||
>
|
||
<SelectTrigger className={FIELD_TRIGGER_CLASS}>
|
||
<SelectValue placeholder="选择检索类型" />
|
||
</SelectTrigger>
|
||
<SelectContent>
|
||
{MATERIAL_SOURCE_OPTIONS.map((opt) => (
|
||
<SelectItem key={opt.value} value={opt.value} className="text-sm">
|
||
{opt.label}
|
||
<span className="ml-1 text-xs text-muted-foreground">— {opt.desc}</span>
|
||
</SelectItem>
|
||
))}
|
||
</SelectContent>
|
||
</Select>
|
||
|
||
{/* 选了"我的空间"才展示空间 / 文档选择器 */}
|
||
{materialSource === 'notebook' && (
|
||
<div className="mt-2 space-y-2">
|
||
{/* studioToken 模式:空间已锁定,不展示选择器 */}
|
||
{!studioToken && (
|
||
<Select
|
||
value={selectedNotebookId !== null ? String(selectedNotebookId) : ''}
|
||
onValueChange={handleNotebookChange}
|
||
disabled={disabled || notebooksLoading}
|
||
>
|
||
<SelectTrigger className={FIELD_TRIGGER_CLASS}>
|
||
<SelectValue placeholder={notebooksLoading ? '加载中…' : '选择空间'} />
|
||
</SelectTrigger>
|
||
<SelectContent>
|
||
{notebooks.map((nb) => (
|
||
<SelectItem key={nb.id} value={String(nb.id)} className="text-sm">
|
||
{nb.name}
|
||
</SelectItem>
|
||
))}
|
||
</SelectContent>
|
||
</Select>
|
||
)}
|
||
|
||
{selectedNotebookId !== null && (
|
||
<div className="space-y-1">
|
||
<p className="text-xs text-muted-foreground">
|
||
{sourcesLoading
|
||
? '文档加载中…'
|
||
: `选择文档(可多选;不选则搜整个空间${selectedDocumentIds.length > 0 ? `,已选 ${selectedDocumentIds.length} 篇` : ''})`}
|
||
</p>
|
||
{!sourcesLoading && (
|
||
<div className="max-h-36 overflow-y-auto rounded-lg border border-[#e8dedd] bg-white divide-y divide-[#f0e8e7]">
|
||
{sources.filter((s) => s.document?.id != null).map((s) => {
|
||
const docId = s.document!.id;
|
||
return (
|
||
<label key={s.id} className="flex items-center gap-2 px-3 py-2 cursor-pointer hover:bg-[#f9f5f5] select-none">
|
||
<input
|
||
type="checkbox"
|
||
checked={selectedDocumentIds.includes(docId)}
|
||
onChange={() => toggleDocument(docId)}
|
||
disabled={disabled}
|
||
className="size-3.5 shrink-0 accent-primary"
|
||
/>
|
||
<span className="text-xs text-foreground truncate">
|
||
{s.document?.title ?? `文档 ${docId}`}
|
||
</span>
|
||
</label>
|
||
);
|
||
})}
|
||
</div>
|
||
)}
|
||
</div>
|
||
)}
|
||
</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 position="popper" side="bottom" sideOffset={4}>
|
||
{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-5 h-12 w-full gap-2 rounded-2xl text-base font-semibold shadow-lg shadow-primary/20 transition-all hover:shadow-primary/30 active:scale-[0.98]"
|
||
>
|
||
<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>
|
||
);
|
||
}
|