Repository navigation
feat(studio): Support images in chat #364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
dd1842b
feat(studio): Support images in chat
steramae-nvidia 7c8dc3d
add dumb guard
steramae-nvidia 1165cd2
fix(studio): Address CodeRabbit review comments on image-chat PR
steramae-nvidia 743119f
remove ff
steramae-nvidia f53b42e
break up components, fix test
steramae-nvidia 20eb460
remove some claude comments
steramae-nvidia d29913a
try to fix timeout
steramae-nvidia ac05d85
Merge branch 'main' into steramae/image-chat
steramae-nvidia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
208 changes: 208 additions & 0 deletions
208
web/packages/common/src/components/AssistantChat/AssistantChatMessages.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,208 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { | ||
| ActionBarPrimitive, | ||
| ComposerPrimitive, | ||
| MessagePrimitive, | ||
| ThreadPrimitive, | ||
| type ToolCallMessagePartComponent, | ||
| } from '@assistant-ui/react'; | ||
| import { ComposerAttachmentsRow } from '@nemo/common/src/components/AssistantChat/ComposerAttachments'; | ||
| import type { AssistantChatMessageContentProps } from '@nemo/common/src/components/AssistantChat/types'; | ||
| import { MessageContent } from '@nemo/common/src/components/Chat/MessageContent'; | ||
| import { | ||
| Banner, | ||
| Button, | ||
| Flex, | ||
| Skeleton, | ||
| Text, | ||
| TextArea, | ||
| Tooltip, | ||
| } from '@nvidia/foundations-react-core'; | ||
| import { Check, Copy, ImagePlus, Pencil, RefreshCw, X } from 'lucide-react'; | ||
|
|
||
| interface MessageRenderProps { | ||
| messageContentProps?: AssistantChatMessageContentProps; | ||
| toolCallPartComponent?: ToolCallMessagePartComponent; | ||
| } | ||
|
|
||
| const ACTION_BUTTON_CLASS = | ||
| 'flex cursor-pointer size-8 items-center justify-center rounded text-base bg-surface-raised hover:bg-surface-sunken disabled:cursor-not-allowed disabled:opacity-50'; | ||
|
|
||
| const MESSAGE_ACTIONS_CLASS = | ||
| 'flex gap-density-xs opacity-0 transition-opacity group-hover/message:opacity-100 group-focus-within/message:opacity-100 [@media(hover:none)]:opacity-100'; | ||
|
|
||
| const AssistantChatMessageContent = ({ | ||
| messageContentProps, | ||
| toolCallPartComponent, | ||
| }: MessageRenderProps) => ( | ||
| <> | ||
| <MessagePrimitive.Parts | ||
| components={{ | ||
| Text: ({ text }) => <MessageContent content={text} {...messageContentProps} />, | ||
| Image: ({ image, filename }) => ( | ||
| <img | ||
| src={image} | ||
| alt={filename ?? 'Attached image'} | ||
| className="mt-density-xs max-h-64 w-auto rounded-lg border border-base object-contain" | ||
| /> | ||
| ), | ||
| tools: { Fallback: toolCallPartComponent }, | ||
| }} | ||
| /> | ||
| <MessagePrimitive.Error> | ||
| <Banner kind="inline" status="error" className="mt-density-sm"> | ||
| There was an error generating a response. | ||
| </Banner> | ||
| </MessagePrimitive.Error> | ||
| </> | ||
| ); | ||
|
|
||
| const CopyAction = () => ( | ||
| <Tooltip slotContent="Copy message"> | ||
| <ActionBarPrimitive.Copy aria-label="Copy message" className={ACTION_BUTTON_CLASS}> | ||
| <MessagePrimitive.If copied> | ||
| <Check size={16} /> | ||
| </MessagePrimitive.If> | ||
| <MessagePrimitive.If copied={false}> | ||
| <Copy size={16} /> | ||
| </MessagePrimitive.If> | ||
| </ActionBarPrimitive.Copy> | ||
| </Tooltip> | ||
| ); | ||
|
|
||
| export const AssistantMessage = ({ | ||
| hideAssistantMessageActions, | ||
| messageContentProps, | ||
| showRunningIndicator = true, | ||
| toolCallPartComponent, | ||
| }: MessageRenderProps & { | ||
| hideAssistantMessageActions?: boolean; | ||
| showRunningIndicator?: boolean; | ||
| }) => ( | ||
| <MessagePrimitive.Root | ||
| data-testid="assistant-chat-message" | ||
| data-testspeaker="assistant" | ||
| className="group/message self-stretch whitespace-normal" | ||
| > | ||
| <AssistantChatMessageContent | ||
| messageContentProps={messageContentProps} | ||
| toolCallPartComponent={toolCallPartComponent} | ||
| /> | ||
| {showRunningIndicator ? ( | ||
| <MessagePrimitive.If last> | ||
| <ThreadPrimitive.If running> | ||
| <div | ||
| className="mt-density-xs flex h-6 items-center" | ||
| data-testid="assistant-chat-running-indicator" | ||
| > | ||
| <Skeleton className="h-density-4 w-full" data-testid="assistant-chat-skeleton" /> | ||
| </div> | ||
| </ThreadPrimitive.If> | ||
| </MessagePrimitive.If> | ||
| ) : null} | ||
| {!hideAssistantMessageActions ? ( | ||
| <div | ||
| className="mt-density-xs flex h-8 items-center" | ||
| data-testid="assistant-chat-message-actions" | ||
| > | ||
| <ActionBarPrimitive.Root hideWhenRunning className={MESSAGE_ACTIONS_CLASS}> | ||
| <Tooltip slotContent="Regenerate response"> | ||
| <ActionBarPrimitive.Reload | ||
| aria-label="Regenerate response" | ||
| className={ACTION_BUTTON_CLASS} | ||
| > | ||
| <RefreshCw size={16} /> | ||
| </ActionBarPrimitive.Reload> | ||
| </Tooltip> | ||
| <CopyAction /> | ||
| </ActionBarPrimitive.Root> | ||
| </div> | ||
| ) : null} | ||
| </MessagePrimitive.Root> | ||
| ); | ||
|
|
||
| export const UserMessage = ({ messageContentProps, toolCallPartComponent }: MessageRenderProps) => ( | ||
| <MessagePrimitive.Root | ||
| data-testid="assistant-chat-message" | ||
| data-testspeaker="user" | ||
| className="group/message flex w-full flex-col items-end gap-density-xs whitespace-normal" | ||
| > | ||
| <div className="max-w-[80%] rounded-xl rounded-br-none bg-surface-overlay px-3 py-2"> | ||
| <AssistantChatMessageContent | ||
| messageContentProps={messageContentProps} | ||
| toolCallPartComponent={toolCallPartComponent} | ||
| /> | ||
| </div> | ||
| <div className="flex h-8 shrink-0 items-center"> | ||
| <ActionBarPrimitive.Root hideWhenRunning className={MESSAGE_ACTIONS_CLASS}> | ||
| <Tooltip slotContent="Edit message"> | ||
| <ActionBarPrimitive.Edit aria-label="Edit message" className={ACTION_BUTTON_CLASS}> | ||
| <Pencil size={16} /> | ||
| </ActionBarPrimitive.Edit> | ||
| </Tooltip> | ||
| <CopyAction /> | ||
| </ActionBarPrimitive.Root> | ||
| </div> | ||
| </MessagePrimitive.Root> | ||
| ); | ||
|
|
||
| export const UserEditComposer = ({ | ||
| enableImageAttachments = true, | ||
| }: { | ||
| enableImageAttachments?: boolean; | ||
| }) => ( | ||
| <MessagePrimitive.Root | ||
| data-testid="assistant-chat-edit-composer" | ||
| className="w-full max-w-[80%] self-end rounded-xl rounded-br-none bg-surface-overlay px-3 py-2" | ||
| > | ||
| {/* | ||
| Lay the textarea and the action row out as siblings in a column. Nesting | ||
| the buttons in the TextArea's `slotEnd` let a growing textarea push them | ||
| out of the bubble — a separate row pins them in place while the input | ||
| scrolls within `max-h-64`. | ||
| */} | ||
| <ComposerPrimitive.Root className="flex w-full flex-col gap-density-sm"> | ||
| {enableImageAttachments && <ComposerAttachmentsRow />} | ||
| <ComposerPrimitive.Input | ||
| aria-label="Edit message" | ||
| addAttachmentOnPaste={enableImageAttachments} | ||
| autoFocus | ||
| submitMode="enter" | ||
| rows={3} | ||
| render={<TextArea resizeable="auto" size="large" className="w-full max-h-64" />} | ||
| /> | ||
| <Flex gap="density-sm" align="center" justify="end"> | ||
| {enableImageAttachments && ( | ||
| <Tooltip slotContent="Add image"> | ||
| <ComposerPrimitive.AddAttachment asChild> | ||
| <Button | ||
| aria-label="Add image" | ||
| kind="tertiary" | ||
| size="small" | ||
| type="button" | ||
| className="mr-auto" | ||
| > | ||
| <ImagePlus size={16} /> | ||
| </Button> | ||
| </ComposerPrimitive.AddAttachment> | ||
| </Tooltip> | ||
| )} | ||
| <Tooltip slotContent="Cancel edit"> | ||
| <ComposerPrimitive.Cancel | ||
| aria-label="Cancel edit" | ||
| className="cursor-pointer flex size-8 items-center justify-center rounded border border-base bg-surface-raised hover:bg-surface-sunken disabled:cursor-not-allowed disabled:opacity-50" | ||
| > | ||
| <X /> | ||
| </ComposerPrimitive.Cancel> | ||
| </Tooltip> | ||
| <ComposerPrimitive.Send asChild> | ||
| <Button aria-label="Save edit" color="brand" size="small"> | ||
| <Text kind="label/regular/sm">Send</Text> | ||
| </Button> | ||
| </ComposerPrimitive.Send> | ||
| </Flex> | ||
| </ComposerPrimitive.Root> | ||
| </MessagePrimitive.Root> | ||
| ); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.