From 8534f4adbe50d11636712d9813866a6c10fecbce Mon Sep 17 00:00:00 2001 From: starmorph Date: Mon, 19 May 2025 16:35:11 -0700 Subject: [PATCH] CR: ContentBlock abstraction --- .../thread/ContentBlocksPreview.tsx | 32 +++++++++++++ src/components/thread/index.tsx | 46 ++---------------- src/hooks/use-file-upload.tsx | 34 +++---------- src/lib/multimodal-utils.ts | 48 +++++++++++-------- 4 files changed, 72 insertions(+), 88 deletions(-) create mode 100644 src/components/thread/ContentBlocksPreview.tsx diff --git a/src/components/thread/ContentBlocksPreview.tsx b/src/components/thread/ContentBlocksPreview.tsx new file mode 100644 index 0000000..1c1caa0 --- /dev/null +++ b/src/components/thread/ContentBlocksPreview.tsx @@ -0,0 +1,32 @@ +import React from "react"; +import type { Base64ContentBlock } from "@langchain/core/messages"; +import { MultimodalPreview } from "../ui/MultimodalPreview"; + +interface ContentBlocksPreviewProps { + blocks: Base64ContentBlock[]; + onRemove: (idx: number) => void; + size?: "sm" | "md" | "lg"; + className?: string; +} + +export const ContentBlocksPreview: React.FC = ({ + blocks, + onRemove, + size = "md", + className = "", +}) => { + if (!blocks.length) return null; + return ( +
+ {blocks.map((block, idx) => ( + onRemove(idx)} + size={size} + /> + ))} +
+ ); +}; diff --git a/src/components/thread/index.tsx b/src/components/thread/index.tsx index e0fa3e0..ba69f01 100644 --- a/src/components/thread/index.tsx +++ b/src/components/thread/index.tsx @@ -37,10 +37,8 @@ import { TooltipProvider, TooltipTrigger, } from "../ui/tooltip"; -import { fileToImageBlock, fileToPDFBlock } from "@/lib/multimodal-utils"; -import type { Base64ContentBlock } from "@langchain/core/messages"; -import { MultimodalPreview } from "../ui/MultimodalPreview"; import { useFileUpload } from "@/hooks/use-file-upload"; +import { ContentBlocksPreview } from "./ContentBlocksPreview"; function StickyToBottomContent(props: { content: ReactNode; @@ -393,44 +391,10 @@ export function Thread() { onSubmit={handleSubmit} className="mx-auto grid max-w-3xl grid-rows-[1fr_auto] gap-2" > - {contentBlocks.filter((b) => b.type === "image").length > - 0 && ( -
- {contentBlocks - .filter((b) => b.type === "image") - .map((imageBlock, idx) => ( - removeBlock(idx)} - size="md" - /> - ))} -
- )} - {contentBlocks.filter( - (b) => - b.type === "file" && b.mime_type === "application/pdf", - ).length > 0 && ( -
- {contentBlocks - .filter( - (b) => - b.type === "file" && - b.mime_type === "application/pdf", - ) - .map((pdfBlock, idx) => ( - removeBlock(idx)} - size="md" - /> - ))} -
- )} +