feat(ai写作): 左栏智能体 Tab 内嵌问答并优化模块侧栏
AI 写作页默认展示智能体 Tab,在页内复用 ai-writing 模块配置与 AgentChatPage;确认类型后侧栏扁平展示类型名并支持整块收起动画。同步历史回看、流式续跑、改写菜单与 researcher 检索相关改动。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
c11307aa67
commit
5de74e90dc
21 files changed
+1379
-386
No files matched your search
@@ -81,11 +81,26 @@
|
||||
* -d '{"retention_days": 0}'
|
||||
* ──────────────────────────────────────────────────────────────────────────
|
||||
*/
|
||||
import { useRef, useState } from 'react';
|
||||
import { ArrowLeft, History, PanelLeftOpen, PanelRightOpen } from 'lucide-react';
|
||||
import { useEffect, useMemo, useRef, useState } from 'react';
|
||||
import {
|
||||
ArrowLeft,
|
||||
History,
|
||||
Loader2,
|
||||
PanelLeftClose,
|
||||
PanelLeftOpen,
|
||||
PanelRightOpen,
|
||||
} from 'lucide-react';
|
||||
import { TooltipProvider } from '@/components/ui/tooltip';
|
||||
import { cn } from '@/lib/utils';
|
||||
import type { GroupImperativeHandle } from 'react-resizable-panels';
|
||||
import {
|
||||
Navigate,
|
||||
Route,
|
||||
Routes,
|
||||
useLocation,
|
||||
useNavigate,
|
||||
useParams,
|
||||
} from 'react-router-dom';
|
||||
|
||||
import { CanvasThreadProvider } from '../contexts/CanvasThreadContext';
|
||||
import { CanvasGraphProvider } from '../contexts/CanvasGraphContext';
|
||||
@@ -96,7 +111,15 @@ import { WritingSetupChat } from '../components/ai-writing/WritingSetupChat';
|
||||
import { ResearchSidebar } from '../components/ai-writing/ResearchSidebar';
|
||||
import StudioNotebookDetailPage from '@/pages/StudioNotebookDetailPage';
|
||||
import StudioNotebooksPage from '@/pages/StudioNotebooksPage';
|
||||
import AgentChatPage from '@/pages/AgentChatPage';
|
||||
import { ChatRuntime } from '@/pages/WorkspaceRoutes';
|
||||
import {
|
||||
AgentModuleSidebar,
|
||||
useAgentModuleSidebarData,
|
||||
} from '@/components/workspace/agents/agent-module-sidebar';
|
||||
import { AgentModuleSettingsDialog } from '@/components/workspace/agents/agent-module-settings-dialog';
|
||||
import { getStoredAuth } from '@/core/auth';
|
||||
import { RouteBaseProvider } from '@/core/route-base';
|
||||
|
||||
/** 右侧内嵌视图:笔记本详情 / 所有空间列表 / null(写作) */
|
||||
type RightView = { kind: 'notebook'; id: number } | { kind: 'allSpaces' } | null;
|
||||
@@ -111,11 +134,83 @@ import {
|
||||
|
||||
// Panel IDs used as keys in the v4 Layout map
|
||||
const WRITING_ID = 'aw-writing';
|
||||
const DRAFT_ID = 'aw-draft';
|
||||
const DRAFT_ID = 'aw-draft';
|
||||
const AI_WRITING_AGENT_MODULE_KEY = 'ai-writing';
|
||||
const AI_WRITING_AGENT_MODULE_TITLE = 'AI 写作智能体';
|
||||
|
||||
// Match the original canvas page proportions (chat=35, artifact=65)
|
||||
const DEFAULT_LAYOUT = { [WRITING_ID]: 35, [DRAFT_ID]: 65 };
|
||||
|
||||
function useAIWritingRouteBase() {
|
||||
const { pathname } = useLocation();
|
||||
return useMemo(() => {
|
||||
const marker = '/ai-writing';
|
||||
const index = pathname.indexOf(marker);
|
||||
return index >= 0
|
||||
? pathname.slice(0, index + marker.length)
|
||||
: '/page/workspace/ai-writing';
|
||||
}, [pathname]);
|
||||
}
|
||||
|
||||
function useAIWritingAgentRouteState(base: string) {
|
||||
const { pathname } = useLocation();
|
||||
return useMemo(() => {
|
||||
const prefix = `${base}/agents/`;
|
||||
if (!pathname.startsWith(prefix)) {
|
||||
return { active: false, agentId: undefined, threadId: undefined };
|
||||
}
|
||||
const parts = pathname.slice(prefix.length).split('/');
|
||||
return {
|
||||
active: true,
|
||||
agentId: parts[0] ? decodeURIComponent(parts[0]) : undefined,
|
||||
threadId: parts[2] ? decodeURIComponent(parts[2]) : undefined,
|
||||
};
|
||||
}, [base, pathname]);
|
||||
}
|
||||
|
||||
function AIWritingAgentDefaultRedirect() {
|
||||
const { groups, isLoading } = useAgentModuleSidebarData(AI_WRITING_AGENT_MODULE_KEY);
|
||||
const firstAgent = groups.flatMap((group) => group.agents)[0];
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<div className="flex size-full items-center justify-center">
|
||||
<Loader2 className="size-5 animate-spin text-muted-foreground" />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (!firstAgent) {
|
||||
return (
|
||||
<div className="flex size-full items-center justify-center px-6 text-center text-sm text-muted-foreground">
|
||||
暂无可用智能体,请先在模块设置中配置或创建智能体。
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return <Navigate to={`agents/${firstAgent.id}/chats/new`} replace />;
|
||||
}
|
||||
|
||||
function AIWritingAgentChatRoute() {
|
||||
const { agent_id } = useParams<{ agent_id: string }>();
|
||||
return (
|
||||
<ChatRuntime>
|
||||
<AgentChatPage key={agent_id} />
|
||||
</ChatRuntime>
|
||||
);
|
||||
}
|
||||
|
||||
function AIWritingAgentRoutes({ base }: { base: string }) {
|
||||
return (
|
||||
<RouteBaseProvider value={base}>
|
||||
<Routes>
|
||||
<Route index element={<AIWritingAgentDefaultRedirect />} />
|
||||
<Route path="agents/:agent_id/chats/:thread_id" element={<AIWritingAgentChatRoute />} />
|
||||
</Routes>
|
||||
</RouteBaseProvider>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 写作配置表单面板(写作助手回填的目标),抽出复用:idle 时在右栏,开始写作后在左栏。
|
||||
*/
|
||||
@@ -230,6 +325,11 @@ function AIWritingLayout({ hideAdvancedFields, initialNotebookId, initialDocumen
|
||||
function AIWritingBody({ hideAdvancedFields, initialNotebookId, initialDocumentIds, studioToken }: { hideAdvancedFields: boolean } & NotebookProps) {
|
||||
const { state } = useAIWriting();
|
||||
const { isHistoryMode } = useAIWritingHistory();
|
||||
const location = useLocation();
|
||||
const navigate = useNavigate();
|
||||
const routeBase = useAIWritingRouteBase();
|
||||
const agentRoute = useAIWritingAgentRouteState(routeBase);
|
||||
const isAdmin = getStoredAuth()?.system_role === 'admin';
|
||||
|
||||
// 需求 0616 #5:左栏点「笔记本 / 所有空间」→ 在右侧内嵌打开对应页,替换右侧内容
|
||||
// (写作布局不卸载,仅 display:none 隐藏,避免 WritingSetupChat 重挂丢线程)。
|
||||
@@ -239,18 +339,38 @@ function AIWritingBody({ hideAdvancedFields, initialNotebookId, initialDocumentI
|
||||
|
||||
// 侧栏「收起」态:由页面承载,ResearchSidebar 顶部的收起按钮触发,收起后显示一条细窄展开条。
|
||||
const [sidebarCollapsed, setSidebarCollapsed] = useState(false);
|
||||
const [sidebarTab, setSidebarTab] = useState<'research' | 'agents'>('agents');
|
||||
const [settingsOpen, setSettingsOpen] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (agentRoute.active) {
|
||||
setSidebarTab('agents');
|
||||
}
|
||||
}, [agentRoute.active]);
|
||||
|
||||
const agentsActive = sidebarTab === 'agents';
|
||||
|
||||
// 研究模板侧栏在「写作配置(idle)」与「历史回看」两态都展示(点历史记录后保留侧栏、
|
||||
// 历史内容显示到右侧区域);写作进行中收起;嵌入态(studioToken)始终不展示。
|
||||
const showSidebar = !studioToken && (state.status === 'idle' || isHistoryMode);
|
||||
const showSidebar =
|
||||
!studioToken && (state.status === 'idle' || isHistoryMode || agentsActive);
|
||||
|
||||
return (
|
||||
<div className="flex-1 min-h-0 overflow-hidden flex">
|
||||
{/* 课题研究左栏(研究模板 + 笔记本 + 历史记录)。支持收起为细窄展开条。
|
||||
写作配置(idle) 与 历史回看 两态都显示;写作进行中整体收起。 */}
|
||||
{showSidebar &&
|
||||
(sidebarCollapsed ? (
|
||||
<div className="flex h-full w-11 shrink-0 flex-col items-center gap-2 border-r border-slate-100 bg-white py-3">
|
||||
{/* 左栏:课题研究 / 智能体。课题研究支持收起为细窄展开条。 */}
|
||||
{showSidebar && (
|
||||
<div
|
||||
className={cn(
|
||||
'relative h-full shrink-0 overflow-hidden border-r border-slate-100 bg-white text-slate-700 shadow-[1px_0_18px_rgba(15,23,42,0.03)] transition-[width] duration-300 ease-in-out',
|
||||
sidebarCollapsed ? 'w-11' : 'w-[320px]',
|
||||
)}
|
||||
>
|
||||
<div
|
||||
className={cn(
|
||||
'absolute inset-0 flex flex-col items-center gap-2 py-3 transition-opacity duration-200 ease-out',
|
||||
sidebarCollapsed ? 'opacity-100' : 'pointer-events-none opacity-0',
|
||||
)}
|
||||
>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setSidebarCollapsed(false)}
|
||||
@@ -262,21 +382,86 @@ function AIWritingBody({ hideAdvancedFields, initialNotebookId, initialDocumentI
|
||||
</button>
|
||||
<History className="size-4 text-slate-300" />
|
||||
</div>
|
||||
) : (
|
||||
<ResearchSidebar
|
||||
viewingNotebookId={viewingNotebookId}
|
||||
onOpenNotebook={(id) => setRightView({ kind: 'notebook', id })}
|
||||
onOpenAllSpaces={() => setRightView({ kind: 'allSpaces' })}
|
||||
onCollapse={() => setSidebarCollapsed(true)}
|
||||
/>
|
||||
))}
|
||||
<aside
|
||||
className={cn(
|
||||
'absolute inset-y-0 left-0 flex h-full w-[320px] flex-col overflow-hidden transition-all duration-300 ease-in-out',
|
||||
sidebarCollapsed ? 'pointer-events-none -translate-x-4 opacity-0' : 'translate-x-0 opacity-100',
|
||||
)}
|
||||
>
|
||||
<div className="flex shrink-0 items-center gap-2 border-b border-slate-100 px-3 py-2">
|
||||
<div className="grid flex-1 grid-cols-2 rounded-lg bg-slate-100 p-1">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
setSidebarTab('agents');
|
||||
setRightView(null);
|
||||
if (!agentRoute.active) navigate(routeBase);
|
||||
}}
|
||||
className={cn(
|
||||
'rounded-md px-2 py-1.5 text-xs font-semibold transition-colors',
|
||||
sidebarTab === 'agents'
|
||||
? 'bg-white text-slate-900 shadow-sm'
|
||||
: 'text-slate-500 hover:text-slate-800',
|
||||
)}
|
||||
>
|
||||
课题研究
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
setSidebarTab('research');
|
||||
setRightView(null);
|
||||
if (location.pathname !== routeBase) navigate(routeBase);
|
||||
}}
|
||||
className={cn(
|
||||
'rounded-md px-2 py-1.5 text-xs font-semibold transition-colors',
|
||||
sidebarTab === 'research'
|
||||
? 'bg-white text-slate-900 shadow-sm'
|
||||
: 'text-slate-500 hover:text-slate-800',
|
||||
)}
|
||||
>
|
||||
ai写作
|
||||
</button>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setSidebarCollapsed(true)}
|
||||
title="收起侧栏"
|
||||
aria-label="收起侧栏"
|
||||
className="rounded-md p-1 text-slate-400 transition-colors hover:bg-slate-50 hover:text-slate-700"
|
||||
>
|
||||
<PanelLeftClose className="size-4" />
|
||||
</button>
|
||||
</div>
|
||||
<div className="min-h-0 flex-1 overflow-hidden">
|
||||
{sidebarTab === 'research' ? (
|
||||
<ResearchSidebar
|
||||
viewingNotebookId={viewingNotebookId}
|
||||
onOpenNotebook={(id) => setRightView({ kind: 'notebook', id })}
|
||||
onOpenAllSpaces={() => setRightView({ kind: 'allSpaces' })}
|
||||
/>
|
||||
) : (
|
||||
<AgentModuleSidebar
|
||||
moduleKey={AI_WRITING_AGENT_MODULE_KEY}
|
||||
activeAgentId={agentRoute.agentId}
|
||||
activeThreadId={agentRoute.threadId}
|
||||
routeBase={routeBase}
|
||||
headerTitle={AI_WRITING_AGENT_MODULE_TITLE}
|
||||
className="w-full border-r-0"
|
||||
onOpenSettings={isAdmin ? () => setSettingsOpen(true) : undefined}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
</aside>
|
||||
</div>
|
||||
)}
|
||||
{/* 右侧:写作布局常驻(仅 display:none 隐藏,避免 WritingSetupChat 重挂丢线程),
|
||||
查看笔记本 / 所有空间时在其上叠加内嵌页。 */}
|
||||
<div className="relative flex-1 min-h-0 overflow-hidden">
|
||||
<div className={cn('absolute inset-0', overlayActive && 'hidden')}>
|
||||
<div className={cn('absolute inset-0', (overlayActive || agentsActive) && 'hidden')}>
|
||||
<AIWritingLayout hideAdvancedFields={hideAdvancedFields} initialNotebookId={initialNotebookId} initialDocumentIds={initialDocumentIds} studioToken={studioToken} />
|
||||
</div>
|
||||
{overlayActive && (
|
||||
{overlayActive && !agentsActive && (
|
||||
<div className="absolute inset-0 flex flex-col bg-background">
|
||||
<div className="flex shrink-0 items-center gap-1 border-b border-border px-4 py-2">
|
||||
<button
|
||||
@@ -313,7 +498,20 @@ function AIWritingBody({ hideAdvancedFields, initialNotebookId, initialDocumentI
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
{agentsActive && (
|
||||
<div className="absolute inset-0 bg-background">
|
||||
<AIWritingAgentRoutes base={routeBase} />
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
{isAdmin && (
|
||||
<AgentModuleSettingsDialog
|
||||
open={settingsOpen}
|
||||
onOpenChange={setSettingsOpen}
|
||||
moduleKey={AI_WRITING_AGENT_MODULE_KEY}
|
||||
moduleTitle={AI_WRITING_AGENT_MODULE_TITLE}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -331,30 +529,30 @@ export default function AIWritingPage({
|
||||
} = {}) {
|
||||
return (
|
||||
<CanvasThreadProvider>
|
||||
<CanvasGraphProvider>
|
||||
<CanvasUIProvider>
|
||||
<AIWritingHistoryProvider>
|
||||
<AIWritingProvider>
|
||||
<WritingFormProvider
|
||||
initialMaterialSource={studioToken ?? initialNotebookId != null ? 'notebook' : undefined}
|
||||
initialNotebookId={initialNotebookId}
|
||||
initialDocumentIds={initialDocumentIds}
|
||||
>
|
||||
<TooltipProvider delayDuration={300}>
|
||||
<div className="h-full flex flex-col overflow-hidden bg-background canvas-writing-bg">
|
||||
<AIWritingBody
|
||||
hideAdvancedFields={hideAdvancedFields}
|
||||
initialNotebookId={initialNotebookId}
|
||||
initialDocumentIds={initialDocumentIds}
|
||||
studioToken={studioToken}
|
||||
/>
|
||||
</div>
|
||||
</TooltipProvider>
|
||||
</WritingFormProvider>
|
||||
</AIWritingProvider>
|
||||
</AIWritingHistoryProvider>
|
||||
</CanvasUIProvider>
|
||||
</CanvasGraphProvider>
|
||||
<CanvasGraphProvider>
|
||||
<CanvasUIProvider>
|
||||
<AIWritingHistoryProvider>
|
||||
<AIWritingProvider>
|
||||
<WritingFormProvider
|
||||
initialMaterialSource={studioToken ?? initialNotebookId != null ? 'notebook' : undefined}
|
||||
initialNotebookId={initialNotebookId}
|
||||
initialDocumentIds={initialDocumentIds}
|
||||
>
|
||||
<TooltipProvider delayDuration={300}>
|
||||
<div className="h-full flex flex-col overflow-hidden bg-background canvas-writing-bg">
|
||||
<AIWritingBody
|
||||
hideAdvancedFields={hideAdvancedFields}
|
||||
initialNotebookId={initialNotebookId}
|
||||
initialDocumentIds={initialDocumentIds}
|
||||
studioToken={studioToken}
|
||||
/>
|
||||
</div>
|
||||
</TooltipProvider>
|
||||
</WritingFormProvider>
|
||||
</AIWritingProvider>
|
||||
</AIWritingHistoryProvider>
|
||||
</CanvasUIProvider>
|
||||
</CanvasGraphProvider>
|
||||
</CanvasThreadProvider>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user