273 lines
9.5 KiB
TypeScript
273 lines
9.5 KiB
TypeScript
/**
|
|
* 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<GroupImperativeHandle>(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<AgentThreadState>;
|
|
}, [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 = (
|
|
<div
|
|
className={cn(
|
|
"h-full bg-[#fffdfa] p-4",
|
|
!artifactsFill &&
|
|
"transition-transform duration-300 ease-in-out",
|
|
!artifactsFill && (artifactPanelOpen ? "translate-x-0" : "translate-x-full"),
|
|
)}
|
|
>
|
|
{selectedArtifact && previewThreadId ? (
|
|
<ArtifactFileDetail
|
|
className="size-full"
|
|
filepath={selectedArtifact}
|
|
threadId={previewThreadId}
|
|
onClose={artifactsFill && onArtifactsClose ? handleArtifactsClose : undefined}
|
|
// 会商「消息列表沙箱」按需求不脱敏产物内容(用户要拿原始文件)。
|
|
disableDesensitize
|
|
headerLeadingAction={showTaskArtifactSubmit ? (
|
|
<TaskArtifactSubmitButton
|
|
compact
|
|
controller={taskArtifactSubmission}
|
|
target={selectedArtifactTarget}
|
|
/>
|
|
) : undefined}
|
|
headerLeadingActionBare={showTaskArtifactSubmit}
|
|
/>
|
|
) : (
|
|
<div className="relative flex size-full justify-center">
|
|
{!artifactsFill && (
|
|
<div className="absolute top-1 right-1 z-30">
|
|
<Button
|
|
size="icon-sm"
|
|
variant="ghost"
|
|
onClick={() => setArtifactsOpen(false)}
|
|
>
|
|
<XIcon />
|
|
</Button>
|
|
</div>
|
|
)}
|
|
{artifacts.length === 0 ? (
|
|
<ConversationEmptyState
|
|
icon={<FilesIcon />}
|
|
title="未选择文件"
|
|
description="智能体写入文件后将在此预览"
|
|
/>
|
|
) : (
|
|
<div className="flex size-full max-w-(--container-width-sm) flex-col justify-center p-4 pt-8">
|
|
<header className="shrink-0">
|
|
<h2 className="text-lg font-medium">产出文件</h2>
|
|
</header>
|
|
<main className="min-h-0 grow">
|
|
{previewThreadId ? (
|
|
<ArtifactFileList
|
|
className="max-w-(--container-width-sm) p-4 pt-12"
|
|
files={artifacts}
|
|
threadId={previewThreadId}
|
|
/>
|
|
) : null}
|
|
</main>
|
|
</div>
|
|
)}
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
|
|
if (artifactsFill) {
|
|
return (
|
|
<ThreadContext.Provider value={{ thread: threadStub, isMock: false }}>
|
|
<div className="h-full min-h-0 flex-1 bg-[#fffdfa]">{sidePanel ?? artifactPanelBody}</div>
|
|
</ThreadContext.Provider>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<ThreadContext.Provider value={{ thread: threadStub, isMock: false }}>
|
|
<ResizablePanelGroup
|
|
id="roundtable-step2-panels"
|
|
orientation="horizontal"
|
|
defaultLayout={{ chat: 100, artifacts: 0 }}
|
|
groupRef={layoutRef}
|
|
className="h-full min-h-0 flex-1"
|
|
>
|
|
<ResizablePanel className="relative" defaultSize={100} id="chat">
|
|
{children}
|
|
</ResizablePanel>
|
|
<ResizableHandle
|
|
id="roundtable-step2-separator"
|
|
className={cn(
|
|
"opacity-33 hover:opacity-100",
|
|
!panelOpen && "pointer-events-none opacity-0",
|
|
)}
|
|
/>
|
|
<ResizablePanel
|
|
className={cn(
|
|
"bg-[#fffdfa] transition-all duration-300 ease-in-out",
|
|
!panelOpen && "opacity-0",
|
|
)}
|
|
id="artifacts"
|
|
>
|
|
{sidePanel ?? artifactPanelBody}
|
|
</ResizablePanel>
|
|
</ResizablePanelGroup>
|
|
</ThreadContext.Provider>
|
|
);
|
|
}
|