/** * Step 2 沙箱分栏 —— 对齐主聊天 `ChatBox` 的 60/40 对话 | 产物预览布局。 * * 圆桌 Step 2 走自定义 SSE,没有 LangGraph SDK thread;这里用最小 ThreadContext stub * 供 `ArtifactFileDetail` / `useArtifactContent` 工作(真实 path 走 artifacts API 轮询)。 */ import type { BaseStream } from "@langchain/langgraph-sdk/react"; import { FilesIcon, XIcon } from "lucide-react"; import { useEffect, useMemo, useRef } from "react"; import type { GroupImperativeHandle } from "react-resizable-panels"; import { ConversationEmptyState } from "@/components/ai-elements/conversation"; import { Button } from "@/components/ui/button"; import { ResizableHandle, ResizablePanel, ResizablePanelGroup, } from "@/components/ui/resizable"; import { ArtifactFileDetail, ArtifactFileList, useArtifacts, } from "@/components/workspace/artifacts"; import { ThreadContext } from "@/components/workspace/messages/context"; import type { AgentThreadState } from "@/core/threads"; import { cn } from "@/lib/utils"; import { TaskArtifactSubmitButton } from "@/roundtable-planning/components/TaskArtifactSubmitButton"; import type { TaskArtifactSubmissionController } from "@/roundtable-planning/lib/task-artifact-submission"; const CLOSE_MODE = { chat: 100, artifacts: 0 }; const OPEN_MODE = { chat: 60, artifacts: 40 }; /** 从 write_file 虚拟地址反查流式工具调用中的正文,供任务产物提交直接使用。 */ function virtualMarkdownContent( artifact: string | null, messages: unknown[] | undefined, ): string | undefined { if (!artifact?.startsWith("write-file:") || !messages?.length) return undefined; try { const url = new URL(artifact); const toolCallId = url.searchParams.get("tool_call_id"); const path = decodeURIComponent(url.pathname).trim(); for (const message of messages) { if (!message || typeof message !== "object") continue; const toolCalls = (message as { tool_calls?: unknown }).tool_calls; if (!Array.isArray(toolCalls)) continue; for (const call of toolCalls) { if (!call || typeof call !== "object") continue; const toolCall = call as { id?: unknown; name?: unknown; args?: { path?: unknown; content?: unknown }; }; if (toolCall.name !== "write_file") continue; if (toolCallId && toolCall.id !== toolCallId) continue; if (!toolCallId && toolCall.args?.path !== path) continue; return typeof toolCall.args?.content === "string" ? toolCall.args.content : undefined; } } } catch { // 虚拟路径解析失败时交给提交层走它原有的真实文件读取兜底。 } return undefined; } interface Step2SandboxLayoutProps { children?: React.ReactNode; /** 当前预览文件所属的席位 thread id。 */ activeThreadId: string | null; /** 为 true 时右侧区域仅展示沙箱预览,占满可用宽度(无中间说明/对话区)。 */ artifactsFill?: boolean; /** artifactsFill 模式下用户点击沙箱关闭时的回调(退出预览、收起沙箱)。 */ onArtifactsClose?: () => void; /** 是否有流式 run 进行中 —— 开启 artifacts API 轮询。 */ isStreaming?: boolean; /** * 当前 active 席位的 stub ai messages —— 让 useArtifactContent 通过虚拟 URL * `write-file:...?message_id=...&tool_call_id=...` 直接读 tool_call.args.content, * 对齐主聊天 chats/:threadId 的实时预览体验。 */ seatMessages?: unknown[]; /** * 右侧面板的替代内容(如 Step3 流程图画布)——非空时强制展开右栏并替换沙箱预览, * 左侧消息列表保持可见;置空即恢复沙箱(文件预览/列表)。 */ sidePanel?: React.ReactNode; /** 任务深链页面的 Markdown 过程产物提交能力。 */ taskArtifactSubmission?: TaskArtifactSubmissionController; } export function Step2SandboxLayout({ children = null, activeThreadId, isStreaming = false, seatMessages, artifactsFill = false, onArtifactsClose, sidePanel, taskArtifactSubmission, }: Step2SandboxLayoutProps) { const layoutRef = useRef(null); const { artifacts: rawArtifacts, open: artifactsOpen, setOpen: setArtifactsOpen, selectedArtifact, deselect, } = useArtifacts(); const artifacts = rawArtifacts ?? []; const selectedArtifactContent = useMemo( () => virtualMarkdownContent(selectedArtifact, seatMessages), [selectedArtifact, seatMessages], ); const artifactPanelOpen = artifactsOpen; // sidePanel(流程图等)非空时即使沙箱没开也要把右栏撑开。 const panelOpen = artifactPanelOpen || !!sidePanel; useEffect(() => { if (artifactsFill || !layoutRef.current) return; layoutRef.current.setLayout(panelOpen ? OPEN_MODE : CLOSE_MODE); }, [panelOpen, artifactsFill]); const handleArtifactsClose = () => { deselect(); setArtifactsOpen(false); onArtifactsClose?.(); }; const threadStub = useMemo(() => { const messages = seatMessages ?? []; return { messages, isLoading: isStreaming, isThreadLoading: false, values: { title: "", messages, artifacts, }, } as unknown as BaseStream; }, [artifacts, isStreaming, seatMessages]); const previewThreadId = activeThreadId ?? ""; const selectedArtifactTarget = useMemo(() => { const target = { threadId: previewThreadId, path: selectedArtifact ?? "", content: selectedArtifactContent, }; return taskArtifactSubmission?.targetFor(target) ?? target; }, [ previewThreadId, selectedArtifact, selectedArtifactContent, taskArtifactSubmission, ]); // 8BF 等接入任务过程产物提交的页面,用提交按钮替换文件类型;其余普通/任务深链 // 会商仍保留原有文件类型徽标。非 Markdown 产物本身不能提交,也应显示类型。 const showTaskArtifactSubmit = Boolean( taskArtifactSubmission?.enabled && taskArtifactSubmission.isMarkdown(selectedArtifactTarget), ); const artifactPanelBody = (
{selectedArtifact && previewThreadId ? ( ) : undefined} headerLeadingActionBare={showTaskArtifactSubmit} /> ) : (
{!artifactsFill && (
)} {artifacts.length === 0 ? ( } title="未选择文件" description="智能体写入文件后将在此预览" /> ) : (

产出文件

{previewThreadId ? ( ) : null}
)}
)}
); if (artifactsFill) { return (
{sidePanel ?? artifactPanelBody}
); } return ( {children} {sidePanel ?? artifactPanelBody} ); }