Skip to content
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

refactor: unify message rendering pattern for tools and annotations #387

Merged
merged 1 commit into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 18 additions & 24 deletions components/chat-messages.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Message } from 'ai'
import RelatedQuestions from './related-questions'
import { RenderMessage } from './render-message'
import { Spinner } from './ui/spinner'
import { useState, useEffect } from 'react'
Expand Down Expand Up @@ -30,37 +29,32 @@ export function ChatMessages({
messages.length -
1 -
[...messages].reverse().findIndex(msg => msg.role === 'user')

const showSpinner = isLoading && messages[messages.length - 1].role === 'user'

const getIsOpen = (id: string) => {
const index = messages.findIndex(msg => msg.id === id.split('-')[0])
return openStates[id] ?? index >= lastUserIndex
}

const handleOpenChange = (id: string, open: boolean) => {
setOpenStates(prev => ({
...prev,
[id]: open
}))
}

return (
<div className="relative mx-auto px-4 w-full">
{messages.map((message, index) => (
{messages.map(message => (
<div key={message.id} className="mb-4 flex flex-col gap-4">
<RenderMessage
message={message}
isOpen={openStates[message.id] ?? index >= lastUserIndex}
onOpenChange={(open: boolean) => {
setOpenStates(prev => ({
...prev,
[message.id]: open
}))
}}
messageId={message.id}
getIsOpen={getIsOpen}
onOpenChange={handleOpenChange}
onQuerySelect={onQuerySelect}
/>
{!message.toolInvocations && message.annotations && (
<RelatedQuestions
annotations={message.annotations}
onQuerySelect={onQuerySelect}
isOpen={
openStates[`${message.id}-related`] ?? index >= lastUserIndex
}
onOpenChange={(open: boolean) => {
setOpenStates(prev => ({
...prev,
[`${message.id}-related`]: open
}))
}}
/>
)}
</div>
))}
{showSpinner && <Spinner />}
Expand Down
37 changes: 26 additions & 11 deletions components/render-message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@ import { Message } from 'ai'
import { UserMessage } from './user-message'
import { ToolSection } from './tool-section'
import { AnswerSection } from './answer-section'
import RelatedQuestions from './related-questions'

interface RenderMessageProps {
message: Message
isOpen: boolean
onOpenChange: (open: boolean) => void
messageId: string
getIsOpen: (id: string) => boolean
onOpenChange: (id: string, open: boolean) => void
onQuerySelect: (query: string) => void
}

export function RenderMessage({
message,
isOpen,
onOpenChange
messageId,
getIsOpen,
onOpenChange,
onQuerySelect
}: RenderMessageProps) {
if (message.role === 'user') {
return <UserMessage message={message.content} />
Expand All @@ -25,19 +30,29 @@ export function RenderMessage({
<ToolSection
key={tool.toolCallId}
tool={tool}
isOpen={isOpen}
onOpenChange={onOpenChange}
isOpen={getIsOpen(messageId)}
onOpenChange={open => onOpenChange(messageId, open)}
/>
))}
</>
)
}

return (
<AnswerSection
content={message.content}
isOpen={isOpen}
onOpenChange={onOpenChange}
/>
<>
<AnswerSection
content={message.content}
isOpen={getIsOpen(messageId)}
onOpenChange={open => onOpenChange(messageId, open)}
/>
{!message.toolInvocations && message.annotations && (
<RelatedQuestions
annotations={message.annotations}
onQuerySelect={onQuerySelect}
isOpen={getIsOpen(`${messageId}-related`)}
onOpenChange={open => onOpenChange(`${messageId}-related`, open)}
/>
)}
</>
)
}