156 lines
5.9 KiB
TypeScript
156 lines
5.9 KiB
TypeScript
/**
|
||
* AI 写作 · 对话版(对话驱动写作台)。
|
||
*
|
||
* 与经典版 `AIWritingPage` 的关系:**并存的新入口,旧页面零改动**。
|
||
*
|
||
* 布局分两态(与用户预期对齐:「初始和原来一样,进入写作才进入新页面」):
|
||
* - idle(未开始写作、非历史回看):沿用经典版的初始双栏——左「写作助手 Q&A」
|
||
* (WritingSetupChat)+ 右「写作配置选项」表单(AIWritingPanel,可整块收起),
|
||
* 由 WritingFormProvider 承载表单状态;点表单「开始写作」或助手「开始写作」
|
||
* 后 status 离开 idle;
|
||
* - 写作中 / 历史回看:切到对话驱动工作台——左 WritingChatPanel(统一信息流 +
|
||
* 按钮版干预卡 + 底部智能输入框),右 AIWritingDraftPanel(草稿实时预览)。
|
||
*
|
||
* 引擎层完全复用:AIWritingProvider(状态机 + LangGraph useStream)、
|
||
* AIWritingHistoryProvider(历史回看)。路由:/page/canvas/ai-writing-chat。
|
||
*/
|
||
import { useRef } from 'react';
|
||
import { PanelRightOpen } from 'lucide-react';
|
||
import { TooltipProvider } from '@/components/ui/tooltip';
|
||
import type { GroupImperativeHandle } from 'react-resizable-panels';
|
||
|
||
import { cn } from '@/lib/utils';
|
||
import { CanvasThreadProvider } from '../contexts/CanvasThreadContext';
|
||
import { CanvasGraphProvider } from '../contexts/CanvasGraphContext';
|
||
import { CanvasUIProvider } from '../contexts/CanvasUIContext';
|
||
import { AIWritingProvider, useAIWriting } from '../contexts/AIWritingContext';
|
||
import { AIWritingHistoryProvider, useAIWritingHistory } from '../contexts/AIWritingHistoryContext';
|
||
import { WritingFormProvider, useWritingForm } from '../contexts/WritingFormContext';
|
||
import { AIWritingPanel } from '../components/ai-writing/AIWritingPanel';
|
||
import { AIWritingDraftPanel } from '../components/ai-writing/AIWritingDraftPanel';
|
||
import { WritingSetupChat } from '../components/ai-writing/WritingSetupChat';
|
||
import { WritingChatPanel } from '../components/ai-writing-chat/WritingChatPanel';
|
||
import {
|
||
ResizableHandle,
|
||
ResizablePanel,
|
||
ResizablePanelGroup,
|
||
} from '@/components/ui/resizable';
|
||
|
||
const CHAT_ID = 'awc-chat';
|
||
const DRAFT_ID = 'awc-draft';
|
||
// 对话区略宽于经典版左栏,以容纳信息流 + 干预卡
|
||
const DEFAULT_LAYOUT = { [CHAT_ID]: 40, [DRAFT_ID]: 60 };
|
||
|
||
/**
|
||
* idle 初始布局 —— 与经典版 AIWritingPage 的 idle 态一致:
|
||
* 左「写作助手 Q&A」恒为第一个子节点(位置稳定不重挂,避免 WritingSetupChat
|
||
* 内部线程丢失),右「写作配置表单」整块可收起(formExpanded 联动)。
|
||
*/
|
||
function SetupLayout() {
|
||
const { formExpanded, setFormExpanded } = useWritingForm();
|
||
|
||
return (
|
||
<div className="relative flex h-full">
|
||
<div className="min-h-0 flex-1 overflow-hidden">
|
||
<WritingSetupChat />
|
||
</div>
|
||
|
||
<div
|
||
className={cn(
|
||
'flex min-h-0 shrink-0 flex-col overflow-hidden transition-[width,min-width] duration-300 ease-in-out',
|
||
formExpanded ? 'w-[36%] min-w-[340px] max-w-[600px] border-l' : 'w-0 min-w-0',
|
||
)}
|
||
>
|
||
<div className="flex-1 min-h-0 overflow-hidden">
|
||
<AIWritingPanel hideAdvancedFields />
|
||
</div>
|
||
</div>
|
||
|
||
{!formExpanded && (
|
||
<button
|
||
type="button"
|
||
onClick={() => setFormExpanded(true)}
|
||
title="展开写作配置"
|
||
className="absolute right-3 top-2 z-10 flex h-7 w-7 items-center justify-center rounded-md border bg-background text-muted-foreground shadow-sm transition-colors hover:bg-muted hover:text-foreground"
|
||
>
|
||
<PanelRightOpen className="size-4" />
|
||
</button>
|
||
)}
|
||
</div>
|
||
);
|
||
}
|
||
|
||
/** 写作中 / 历史回看:对话驱动工作台(左信息流 + 右草稿预览)。 */
|
||
function ChatWorkbenchLayout() {
|
||
const layoutRef = useRef<GroupImperativeHandle>(null);
|
||
|
||
return (
|
||
<ResizablePanelGroup
|
||
orientation="horizontal"
|
||
groupRef={layoutRef}
|
||
defaultLayout={DEFAULT_LAYOUT}
|
||
className="h-full"
|
||
>
|
||
<ResizablePanel
|
||
id={CHAT_ID}
|
||
defaultSize={40}
|
||
minSize={25}
|
||
className="overflow-hidden flex flex-col"
|
||
>
|
||
<WritingChatPanel />
|
||
</ResizablePanel>
|
||
|
||
<ResizableHandle
|
||
withHandle
|
||
className="after:left-0 after:w-3 after:translate-x-0 [&>div]:translate-x-1.5"
|
||
/>
|
||
|
||
<ResizablePanel
|
||
id={DRAFT_ID}
|
||
defaultSize={60}
|
||
minSize={35}
|
||
className="overflow-hidden"
|
||
>
|
||
<AIWritingDraftPanel />
|
||
</ResizablePanel>
|
||
</ResizablePanelGroup>
|
||
);
|
||
}
|
||
|
||
function AIWritingChatBody() {
|
||
const { state } = useAIWriting();
|
||
const { isHistoryMode } = useAIWritingHistory();
|
||
|
||
// idle 且非历史回看 → 经典初始布局;开始写作 / 查看历史 → 对话驱动工作台。
|
||
// 两套布局都保持挂载态简单切换:SetupLayout 内的 WritingSetupChat 在开始写作后
|
||
// 卸载没有代价(它只负责启动前配置,线程与写作 graph 解耦)。
|
||
if (state.status === 'idle' && !isHistoryMode) {
|
||
return <SetupLayout />;
|
||
}
|
||
return <ChatWorkbenchLayout />;
|
||
}
|
||
|
||
export default function AIWritingChatPage() {
|
||
return (
|
||
<CanvasThreadProvider>
|
||
<CanvasGraphProvider>
|
||
<CanvasUIProvider>
|
||
<AIWritingHistoryProvider>
|
||
<AIWritingProvider>
|
||
<WritingFormProvider>
|
||
<TooltipProvider delayDuration={300}>
|
||
<div className="h-full flex flex-col overflow-hidden bg-background canvas-writing-bg">
|
||
<div className="flex-1 min-h-0 overflow-hidden">
|
||
<AIWritingChatBody />
|
||
</div>
|
||
</div>
|
||
</TooltipProvider>
|
||
</WritingFormProvider>
|
||
</AIWritingProvider>
|
||
</AIWritingHistoryProvider>
|
||
</CanvasUIProvider>
|
||
</CanvasGraphProvider>
|
||
</CanvasThreadProvider>
|
||
);
|
||
}
|