Skip to content
This repository has been archived by the owner on May 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #181 from tipisai/improve/message-list
Browse files Browse the repository at this point in the history
Improve/message list
  • Loading branch information
Wangtaofeng authored May 7, 2024
2 parents 74aa434 + 8a4cdea commit 5294909
Show file tree
Hide file tree
Showing 25 changed files with 265 additions and 110 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { FC } from "react"
import AIAgentMessage from "@/page/WorkSpace/AI/components/AIAgentMessage"
import GroupAgentMessage from "@/page/WorkSpace/AI/components/GroupAgentMessage"
import UserMessage from "@/page/WorkSpace/AI/components/UserMessage"
import { isGroupMessage } from "@/utils/agent/typeHelper"
import { SenderType } from "../../../interface"
import { IMessageListItemProps } from "./interface"

const MessageListItem: FC<IMessageListItemProps> = ({
message,
currentUserID,
isReceiving,
isLastMessage,
}) => {
if (isGroupMessage(message)) {
return (
<GroupAgentMessage
key={message.threadID}
message={message}
disableTrigger={isReceiving && isLastMessage}
/>
)
} else if (
message.sender.senderType === SenderType.USER &&
message.sender.senderID === currentUserID
) {
return (
<UserMessage
key={message.threadID}
message={message}
disableTrigger={isReceiving && isLastMessage}
/>
)
} else {
return (
<AIAgentMessage
key={message.threadID}
message={message}
isLastMessage={isLastMessage}
isReceiving={isReceiving}
/>
)
}
}

export default MessageListItem
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ChatMessage, IGroupMessage } from "../../../interface"

export interface IMessageListItemProps {
message: IGroupMessage | ChatMessage
isReceiving: boolean
isLastMessage: boolean
currentUserID: string
}
65 changes: 27 additions & 38 deletions apps/agent/src/components/PreviewChat/MessageList/index.tsx
Original file line number Diff line number Diff line change
@@ -1,50 +1,39 @@
import { forwardRef } from "react"
import { isGroupMessage } from "@/utils/agent/typeHelper"
import AIAgentMessage from "../../../page/WorkSpace/AI/components/AIAgentMessage"
import GroupAgentMessage from "../../../page/WorkSpace/AI/components/GroupAgentMessage"
import UserMessage from "../../../page/WorkSpace/AI/components/UserMessage"
import { SenderType } from "../interface"
import MessageItem from "./components/MessageListItem"
import { IMessageListProps } from "./interface"
import { chatContainerStyle, maxWidthStyle } from "./style"

const MessageList = forwardRef<HTMLDivElement, IMessageListProps>(
(props, ref) => {
const { handleScroll, chatMessages, isReceiving, currentUserID } = props
const {
handleScroll,
chatMessages,
isReceiving,
currentUserID,
currentRenderMessage,
} = props
return (
<div ref={ref} css={chatContainerStyle} onScroll={handleScroll}>
<div css={maxWidthStyle}>
{chatMessages.map((message, i) => {
if (isGroupMessage(message)) {
return (
<GroupAgentMessage
key={message.threadID}
message={message}
isReceiving={isReceiving}
isLastMessage={i === chatMessages.length - 1}
/>
)
} else if (
message.sender.senderType === SenderType.USER &&
message.sender.senderID === currentUserID
) {
return (
<UserMessage
key={message.threadID}
message={message}
isReceiving={isReceiving}
/>
)
} else {
return (
<AIAgentMessage
key={message.threadID}
message={message}
isLastMessage={i === chatMessages.length - 1}
isReceiving={isReceiving}
/>
)
}
})}
{chatMessages.map((message, i) => (
<MessageItem
key={message.threadID}
message={message}
currentUserID={currentUserID}
isLastMessage={i === chatMessages.length - 1}
isReceiving={isReceiving}
/>
))}

{!!currentRenderMessage && (
<MessageItem
key={currentRenderMessage.threadID}
message={currentRenderMessage}
currentUserID={currentUserID}
isLastMessage
isReceiving={isReceiving}
/>
)}
</div>
</div>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export interface IMessageListProps {
chatMessages: (IGroupMessage | ChatMessage)[]
isReceiving: boolean
currentUserID: string
currentRenderMessage: IGroupMessage | ChatMessage | null
}
4 changes: 3 additions & 1 deletion apps/agent/src/components/PreviewChat/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import {
export const PreviewChat: FC<PreviewChatProps> = (props) => {
const { blockInput, onSendMessage, wsContextValue } = props

const { chatMessages, isReceiving, sendMessage } = wsContextValue
const { chatMessages, isReceiving, sendMessage, currentRenderMessage } =
wsContextValue

const { data: currentUserInfo } = useGetUserInfoQuery(null)

Expand Down Expand Up @@ -80,6 +81,7 @@ export const PreviewChat: FC<PreviewChatProps> = (props) => {
chatMessages={chatMessages}
isReceiving={isReceiving}
currentUserID={currentUserInfo?.userID ?? ""}
currentRenderMessage={currentRenderMessage}
/>
<div css={[inputTextContainerStyle, maxWidthStyle]}>
{blockInput ? (
Expand Down
1 change: 1 addition & 0 deletions apps/agent/src/components/PreviewChat/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export interface PreviewChatProps {
onSendMessage: (cheatMessage: ChatMessage) => void
wsContextValue: {
chatMessages: (IGroupMessage | ChatMessage)[]
currentRenderMessage: IGroupMessage | ChatMessage | null
isReceiving: boolean
sendMessage: IChatSendMessage
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const PreviewChatHistory: FC = memo(() => {
isReceiving,
sendMessage,
lastRunAgent,
currentRenderMessage,
} = useContext(AgentWSContext)
const [prompt, variables, knowledge, aiTools] = useWatch({
control,
Expand Down Expand Up @@ -57,6 +58,7 @@ const PreviewChatHistory: FC = memo(() => {
isReceiving,
sendMessage,
lastRunAgent,
currentRenderMessage,
}),
[
chatMessages,
Expand All @@ -65,6 +67,7 @@ const PreviewChatHistory: FC = memo(() => {
lastRunAgent,
sendMessage,
getReadyState,
currentRenderMessage,
],
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const AIAgentRunMobile: FC = () => {
connect,
isConnecting,
inRoomUsers,
currentRenderMessage,
} = useContext(AgentWSContext)

const wsContext = useMemo(
Expand All @@ -39,6 +40,7 @@ export const AIAgentRunMobile: FC = () => {
isReceiving,
sendMessage,
lastRunAgent,
currentRenderMessage,
}),
[
chatMessages,
Expand All @@ -47,6 +49,7 @@ export const AIAgentRunMobile: FC = () => {
lastRunAgent,
sendMessage,
getReadyState,
currentRenderMessage,
],
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const AIAgentRunPC: FC = () => {
sendMessage,
lastRunAgent,
tabID,
currentRenderMessage,
} = useContext(AgentWSContext)

const isVariablesDirty = getFieldState("variables").isDirty
Expand All @@ -65,6 +66,7 @@ export const AIAgentRunPC: FC = () => {
isReceiving,
sendMessage,
lastRunAgent,
currentRenderMessage,
}),
[
chatMessages,
Expand All @@ -73,6 +75,7 @@ export const AIAgentRunPC: FC = () => {
lastRunAgent,
sendMessage,
getReadyState,
currentRenderMessage,
],
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const AIAgentMessage: FC<AIAgentMessageProps> = (props) => {

const contentBody = (
<div css={messageContainerStyle} ref={containerRef}>
<MarkdownMessage isReceiving={isReceiving}>
<MarkdownMessage disableTrigger={isReceiving && isLastMessage}>
{message.message}
</MarkdownMessage>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
} from "./style"

export const GroupAgentMessage: FC<GroupAgentMessageProps> = (props) => {
const { message, isReceiving, isLastMessage } = props
const { message, disableTrigger } = props
const chatContext = useContext(ChatContext)
const isMobile = useIsMobile()

Expand Down Expand Up @@ -68,15 +68,15 @@ export const GroupAgentMessage: FC<GroupAgentMessageProps> = (props) => {
messageItem.status ?? MESSAGE_STATUS.ANALYZE_PENDING
}
messageResult={messageItem.messageResult}
isReceiving={isReceiving}
disableTrigger={disableTrigger}
/>
)
} else {
element = (
<PureMessage
message={messageItem.message}
isMobile={isMobile}
disableTrigger={isReceiving && isLastMessage}
disableTrigger={disableTrigger}
/>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ import { IGroupMessage } from "@/components/PreviewChat/interface"

export interface GroupAgentMessageProps {
message: IGroupMessage
isReceiving: boolean
isLastMessage: boolean
disableTrigger: boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ import {
} from "./style"

const CustomCode: FC<
CodeProps & Pick<MarkdownMessageProps, "codeStatus" | "isReceiving">
CodeProps & Pick<MarkdownMessageProps, "codeStatus" | "disableTrigger">
> = (props) => {
const { codeStatus = CODE_STATUS.DEFAULT, isReceiving } = props
const { codeStatus = CODE_STATUS.DEFAULT, disableTrigger } = props
const { t } = useTranslation()
const { message: messageAPI } = App.useApp()
const language =
/language-(\w+)/.exec(props.className || "")?.[1] ?? "markdown"

const handleCopyClick = () => {
if (isReceiving) {
if (disableTrigger) {
return
}
copyToClipboard(props.children?.[0])
Expand All @@ -43,7 +43,7 @@ const CustomCode: FC<
<div css={codeBlockContainerStyle(codeStatus)}>
<div css={codeBlockHeaderStyle(codeStatus)}>
<span>{language.toLocaleLowerCase()}</span>
<div css={copyStyle(isReceiving)} onClick={handleCopyClick}>
<div css={copyStyle(!!disableTrigger)} onClick={handleCopyClick}>
<Icon component={CopyIcon} size={16} />
<span>{t("editor.ai-agent.copy_code")}</span>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import CustomImage from "./components/CustomImage"
import { handleParseText } from "./utils"

export const MarkdownMessage = memo((props: MarkdownMessageProps) => {
const { children, isOwnMessage, codeStatus, isReceiving } = props
const { children, isOwnMessage, codeStatus, disableTrigger } = props

return (
<div css={markdownMessageContainerStyle}>
Expand Down Expand Up @@ -99,7 +99,7 @@ export const MarkdownMessage = memo((props: MarkdownMessageProps) => {
<CustomCode
{...props}
codeStatus={codeStatus}
isReceiving={isReceiving}
disableTrigger={disableTrigger}
/>
),
img: ({ src, alt }) => <CustomImage src={src} alt={alt} />,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export enum CODE_STATUS {
}

export interface MarkdownMessageProps {
isReceiving: boolean
disableTrigger?: boolean
isOwnMessage?: boolean
children?: string
codeStatus?: CODE_STATUS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const PureMessage: FC<PureMessageProps> = ({

const contentBody = (
<div css={pureMessageContainerStyle} ref={containerRef}>
<MarkdownMessage isReceiving={false}>{message}</MarkdownMessage>
<MarkdownMessage>{message}</MarkdownMessage>
</div>
)

Expand Down Expand Up @@ -91,7 +91,7 @@ export const SyncMessageCard: FC<SyncMessageCardProps> = ({
message,
messageStatus,
messageResult,
isReceiving,
disableTrigger,
}) => {
const { t } = useTranslation()
const [showMessage, setShowMessage] = useState(false)
Expand Down Expand Up @@ -156,7 +156,7 @@ export const SyncMessageCard: FC<SyncMessageCardProps> = ({
<>
<SyncMessageLine />
<div css={messageContainerStyle}>
<MarkdownMessage isReceiving={isReceiving}>
<MarkdownMessage disableTrigger={disableTrigger}>
{formatMessage}
</MarkdownMessage>
{errorInfo && (
Expand All @@ -168,7 +168,7 @@ export const SyncMessageCard: FC<SyncMessageCardProps> = ({
{t("homepage.tipi_chat.response.resonse")}
</span>
<MarkdownMessage
isReceiving={isReceiving}
disableTrigger={disableTrigger}
codeStatus={CODE_STATUS.ERROR}
>
{errorInfo}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { MESSAGE_STATUS } from "@/components/PreviewChat/interface"

export interface SyncMessageCardProps {
isReceiving: boolean
disableTrigger: boolean
message: string
messageStatus: MESSAGE_STATUS
messageResult?: string
Expand Down
Loading

0 comments on commit 5294909

Please sign in to comment.