/* oxlint-disable eslint(max-lines) -- Share 与 Desktop 共用的只读 Row/turn presentation 需要保持在同一安全边界。 * 安全边界约束:本文件被匿名公开分享页(packages/web/src/share)直接引用,新增依赖必须考虑 * 公开页 bundle 体积与无 Desktop 宿主(window.zcode / PlatformProvider / tab store)的运行环境; * Desktop 专属能力(如 open-with 子树)一律由消费方经组件注入,不得静态 import。 */ import { createContext, Fragment, memo, type ComponentType, type ReactNode, useCallback, useContext, useEffect, useMemo, useState, } from "react"; import { ChevronRightIcon, FilePenLineIcon, FileTextIcon, FilesIcon, InfoIcon, MonitorIcon, SearchIcon, SquareTerminalIcon, WrenchIcon, } from "lucide-react"; import { getCompactToolCallStatusMessageId, type Locale } from "@zcode/shared"; import type { ArtifactRow, AssistantTextRow, ConversationRow, ReasoningRow, TimelineMarkerRow, ToolCallRow, UserInputRow, } from "@zcode/shared/zcode-protocol-v4"; import { MessageResponse, type MessageFileLinkTarget } from "@/components/ai-elements/message.js"; import { Reasoning, ReasoningContent, ReasoningTrigger, } from "@/components/ai-elements/reasoning.js"; import { Collapsible, CollapsibleContent, CollapsibleTrigger, } from "@/components/ui/collapsible.js"; import { TooltipProvider } from "@/components/ui/tooltip.js"; import { ToolLayout } from "@/ToolCallBlocks/ToolLayout.js"; import { ConversationUserInputBody } from "@/v4/ConversationUserInputBody.js"; import { ConversationUserInputContent } from "@/v4/ConversationUserInputContent.js"; import { PluginReferenceIconProvider } from "@/v4/pluginReferenceIconContext.js"; import { buildAssistantWorkRenderItems, type ConversationAssistantWorkRenderItem, } from "@/v4/conversationAssistantWorkItems.js"; import type { ConversationCuaGroupRenderItem } from "@/v4/conversationCuaGroups.js"; import { buildConversationTurnRenderUnits, type ConversationTurnRenderUnit, } from "@/v4/conversationTurnRenderUnits.js"; import type { AssistantWorkRow, ConversationTurnFlowItem } from "@/v4/conversationTurnFlowItems.js"; import type { ConversationTurnWorkSegment } from "@/v4/conversationTurnWorkSegments.js"; import { formatConversationWorkDuration } from "@/v4/conversationWorkDuration.js"; import { normalizeConversationShareMarkdown } from "@/v4/conversationShareMarkdown.js"; import { resolveToolCallIdentity } from "@/lib/toolIdentity.js"; import { DEFAULT_CODE_PREVIEW_SETTINGS, type CodePreviewSettings, } from "@/lib/codePreviewSettings.js"; import { ZCodeIntlProvider, useZCodeIntl } from "@/i18n/IntlProvider.js"; import type { Theme } from "@/useTheme.js"; import { FileDisplayIcon, resolveFileDisplayDescriptor } from "@/lib/fileDisplay.js"; import { formatAttachmentSize } from "@/lib/chatAttachmentMetadata.js"; import type { CodeViewerSource } from "@/lib/codeViewer.js"; import { isAbsoluteFilePath, joinFilePath } from "@/lib/path.js"; // 仅类型引用,构建期擦除:静态 import OpenSplitButton 会把其整棵 open-with 子树 // (platform hooks、tab store、workspace-file-tree/model、editorPreference)打进匿名 // 公开页 bundle,因此打开动作组件改由 Desktop 消费方经 artifactOpenAction 注入。 import type { OpenSplitButtonTarget } from "@/OpenSplitButton.js"; export interface ConversationShareReadonlyTimelineProps { rows: readonly ConversationRow[]; locale?: Locale; theme?: Theme; codePreviewSettings?: CodePreviewSettings; artifactUrls?: ReadonlyMap; artifactNames?: ReadonlyMap; artifactWorkspaceRelativePaths?: ReadonlyMap; workspacePath?: string; workspaceIdentity?: string; workspaceRemoteSessionId?: string; onOpenFileLink?: (target: MessageFileLinkTarget) => void; onOpenCodeViewer?: (source: CodeViewerSource) => void; /** * Desktop 侧注入的 artifact 打开动作组件(实现即 OpenSplitButton)。 * 公开分享页不传:一是公开页没有本地打开能力,二是避免 open-with 子树进公开页 bundle。 */ artifactOpenAction?: ConversationShareArtifactOpenAction; /** * 本 build 认不出、已被跳过的行数(见 decodeConversationShareRows)。 * * >0 时在时间线顶部出一条中性提示。必须提示:分享内容是跨版本流动的,老客户端/老落地页 * 镜像遇到新 row kind 时会静默少几行,用户没法自己发现——他会以为分享者就发了这些。 */ unsupportedRowCount?: number; } /** 注入契约:Desktop 侧提供的「带本地打开动作」组件,与 OpenSplitButton 的关键 props 对齐。 */ export type ConversationShareArtifactOpenAction = ComponentType<{ target: OpenSplitButtonTarget; onOpenFileLink?: (target: MessageFileLinkTarget) => void; onOpenCodeViewer?: (source: CodeViewerSource) => void; }>; interface ReadonlyLabels { history: string; computerUse: string; explore: string; execute: string; changes: string; artifactPreview: string; markerCompact: string; markerModelChange: string; unsupportedRows: string; } /** * 只读时间线里 marker 的文案。 * * 这里刻意不复刻实时时间线的全部状态细分(compact 的 started/failed/interrupted、 * modelChange 的 from→to、goalVerify 的迭代序号):发布会拦掉运行中的 marker, * 而 from→to 需要 model-provider store 才能解析显示名,只读历史里价值很低。 * 识别不了的类型返回 null,由调用方整行不渲染 —— 绝不回退成打印枚举名。 */ function resolveReadonlyMarkerLabel( marker: TimelineMarkerRow["marker"], labels: ReadonlyLabels, ): string | null { switch (marker.type) { case "compact": return marker.status === "success" ? labels.markerCompact : null; case "modelChange": return labels.markerModelChange; default: return null; } } const EMPTY_ARTIFACT_URLS: ReadonlyMap = new Map(); const EMPTY_ARTIFACT_NAMES: ReadonlyMap = new Map(); const EMPTY_ARTIFACT_WORKSPACE_RELATIVE_PATHS: ReadonlyMap = new Map(); const EMPTY_ATTACHMENTS: readonly unknown[] = []; interface ArtifactOpenContextValue { artifactWorkspaceRelativePaths: ReadonlyMap; workspacePath: string; workspaceIdentity?: string; workspaceRemoteSessionId?: string; onOpenFileLink?: (target: MessageFileLinkTarget) => void; onOpenCodeViewer?: (source: CodeViewerSource) => void; openAction?: ConversationShareArtifactOpenAction; } const ArtifactOpenContext = createContext(null); function resolveImportedArtifactPath( workspacePath: string, workspaceRelativePath: string | undefined, ): string | null { // 取舍:这里只校验形状(.zcode-share//shared-artifacts/ 四段),刻意不把 // 段 2 与导入记录的 shareId 交叉比对。元数据由本端导入服务自写(conversationShareService // 落盘时用 sanitizeFileSegment(share_id) 作目录名),自洽;若在 UI 侧比对,就得复制 // service 层的 sanitize 规则,两边漂移会让合法导入静默丢打开按钮,而收益仅是防住 // 「指向另一 share 目录」这种一致性噪声——路径仍被限制在 workspace 的 // .zcode-share/*/shared-artifacts/ 内,无越权读放大。 const normalizedPath = workspaceRelativePath?.trim(); if (!normalizedPath || isAbsoluteFilePath(normalizedPath)) { return null; } const segments = normalizedPath.replace(/\\/g, "/").split("/"); if ( segments.length !== 4 || segments.some((segment) => !segment || segment === "." || segment === "..") || segments[0] !== ".zcode-share" || segments[2] !== "shared-artifacts" ) { return null; } return joinFilePath(workspacePath, segments.join("/")); } function UserInputPresentation({ row, artifactUrls, }: { row: UserInputRow; artifactUrls: ReadonlyMap; }) { const attachments = (row.attachments ?? []).map((attachment) => { const artifactId = attachment.ref.split("/").at(-1) ?? ""; return { attachment, url: artifactUrls.get(artifactId) }; }); return (
{attachments.length > 0 ? (
{attachments.map(({ attachment, url }) => ( ))}
) : null}
); } const AssistantTextPresentation = memo(function AssistantTextPresentation({ row, theme, codePreviewSettings, artifactNames, onOpenExternalUrl, }: { row: AssistantTextRow; theme: Theme; codePreviewSettings: CodePreviewSettings; artifactNames: ReadonlyMap; onOpenExternalUrl: (url: string) => void; }) { const markdown = useMemo( () => normalizeConversationShareMarkdown(row.text, artifactNames), [artifactNames, row.text], ); return (
{markdown}
); }); const ReasoningPresentation = memo(function ReasoningPresentation({ row }: { row: ReasoningRow }) { const durationSeconds = row.durationMs === undefined ? undefined : Math.max(1, Math.ceil(row.durationMs / 1000)); return (
{row.text}
); }); function resolveToolStatusLabel( status: ToolCallRow["status"], formatMessage: (descriptor: { id: string }) => string, ) { const compactState = status === "inputStreaming" || status === "pendingApproval" ? "input-streaming" : status === "running" ? "input-available" : status === "success" ? "output-available" : "output-error"; return formatMessage({ id: getCompactToolCallStatusMessageId( compactState, status === "cancelled" ? "stopped" : undefined, ), }); } function readToolInputSummary(input: unknown, keys: readonly string[]): string | null { if (!input || typeof input !== "object" || Array.isArray(input)) return null; const record = input as Record; for (const key of keys) { const value = record[key]; if (typeof value === "string" && value.trim()) return value.trim(); } return null; } const ToolCallPresentation = memo(function ToolCallPresentation({ row, theme, codePreviewSettings, artifactNames, onOpenExternalUrl, }: { row: ToolCallRow; theme: Theme; codePreviewSettings: CodePreviewSettings; artifactNames: ReadonlyMap; onOpenExternalUrl: (url: string) => void; }) { const { intl } = useZCodeIntl(); const output = row.output?.text?.trim(); const markdown = useMemo( () => (output ? normalizeConversationShareMarkdown(output, artifactNames) : ""), [artifactNames, output], ); const statusLabel = resolveToolStatusLabel(row.status, intl.formatMessage); const identity = useMemo( () => resolveToolCallIdentity({ toolName: row.toolName, kind: row.toolName, input: row.input }), [row.input, row.toolName], ); const searchSummary = identity.family === "search" ? readToolInputSummary(row.input, ["query", "pattern", "prompt"]) : null; const presentation = identity.family === "search" ? { icon: