Skip to content

Commit

Permalink
✨ feat: 兼容插件模式
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Jul 23, 2023
1 parent 27e0318 commit aedfa53
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/Chat/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export type InternalChatContext = Omit<ChatContext, 'id'>;

export type MessageRoleType = 'user' | 'system' | 'assistant';
export type MessageRoleType = 'user' | 'system' | 'assistant' | 'function';

/**
* 聊天消息错误对象
Expand Down
26 changes: 17 additions & 9 deletions src/ChatItem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ export interface ChatItemProps {
/**
* @description The message content of the chat item
*/
message?: string;
message?: ReactNode;
messageExtra?: ReactNode;
/**
* @description Callback when the message content changes
* @param value - The new message content
*/
onChange?: (value: string) => void;

/**
* @description Callback when the editing mode changes
* @param editing - The new editing mode
Expand All @@ -66,6 +67,7 @@ export interface ChatItemProps {
* @description Whether the chat item is primary
*/
primary?: boolean;
renderMessage?: (content: ReactNode) => ReactNode;
/**
* @description Whether to show the title of the chat item
*/
Expand Down Expand Up @@ -100,6 +102,7 @@ const ChatItem = memo<ChatItemProps>(
onChange,
onEditingChange,
messageExtra,
renderMessage,
...properties
}) => {
const { cx, styles } = useStyles({
Expand All @@ -111,6 +114,18 @@ const ChatItem = memo<ChatItemProps>(
type,
});

const content = (
<EditableMessage
classNames={{ input: styles.editingInput }}
editButtonSize={'small'}
editing={editing}
onChange={onChange}
onEditingChange={onEditingChange}
value={String(message || '...')}
/>
);
const messageContent = renderMessage ? renderMessage(content) : content;

return (
<div className={cx(styles.container, className)} {...properties}>
<div className={styles.avatarContainer}>
Expand All @@ -136,14 +151,7 @@ const ChatItem = memo<ChatItemProps>(
<Alert className={styles.alert} showIcon {...alert} />
) : (
<Flexbox className={cx(styles.message, editing && styles.editingContainer)}>
<EditableMessage
classNames={{ input: styles.editingInput }}
editButtonSize={'small'}
editing={editing}
onChange={onChange}
onEditingChange={onEditingChange}
value={String(message || '...')}
/>
{messageContent}
{messageExtra && !editing ? (
<div className={styles.messageExtra}>{messageExtra}</div>
) : null}

Check warning on line 157 in src/ChatItem/index.tsx

View workflow job for this annotation

GitHub Actions / test

Use `undefined` instead of `null`
Expand Down
19 changes: 17 additions & 2 deletions src/ChatList/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { App } from 'antd';
import copy from 'copy-to-clipboard';
import { ReactNode, memo, useState } from 'react';
import { ReactNode, memo, useCallback, useState } from 'react';

import ChatItem, { ChatItemProps } from '@/ChatItem';
import type { DivProps } from '@/types';
Expand All @@ -9,7 +9,10 @@ import { ChatMessage } from '@/types/chatMessage';
import ActionsBar from './ActionsBar';
import { useStyles } from './style';

type MessageExtra = (props: ChatMessage) => ReactNode;
export type MessageExtra = (props: ChatMessage) => ReactNode;

export type RenderMessage = (content: ReactNode, message: ChatMessage) => ReactNode;

export interface ChatListProps extends DivProps {
/**
* @description Data of chat messages to be displayed
Expand All @@ -22,6 +25,7 @@ export interface ChatListProps extends DivProps {
*/
onActionClick?: (actionKey: string, messageId: string) => void;
onMessageChange?: (id: string, content: string) => void;
renderMessage?: RenderMessage;
renderMessageExtra?: MessageExtra;
/**
* @description Whether to show name of the chat item
Expand All @@ -39,6 +43,7 @@ export interface ItemProps extends ChatMessage {
MessageExtra?: MessageExtra;
onActionClick?: (actionKey: string, messageId: string) => void;
onMessageChange?: (id: string, content: string) => void;
renderMessage?: RenderMessage;
showTitle?: boolean;
type: 'docs' | 'chat';
}
Expand All @@ -49,13 +54,20 @@ const Item = ({
onActionClick,
onMessageChange,
type,
renderMessage,
...item
}: ItemProps) => {
const renderMessageExtra = MessageExtra ? <MessageExtra {...item} /> : undefined;

const [editing, setEditing] = useState(false);

const { message } = App.useApp();

const innerRenderMessage = useCallback(
(content: ReactNode) => renderMessage?.(content, item),
[renderMessage],
);

return (
<ChatItem
actions={
Expand Down Expand Up @@ -87,6 +99,7 @@ const Item = ({
onEditingChange={setEditing}
placement={type === 'chat' ? (item.role === 'user' ? 'right' : 'left') : 'left'}
primary={item.role === 'user'}
renderMessage={renderMessage ? innerRenderMessage : undefined}
showTitle={showTitle}
time={item.updateAt || item.createAt}
type={type === 'chat' ? 'block' : 'pure'}
Expand All @@ -103,6 +116,7 @@ const ChatList = memo<ChatListProps>(
type = 'chat',
showTitle,
onMessageChange,
renderMessage,
...props
}) => {
const { cx, styles } = useStyles();
Expand All @@ -115,6 +129,7 @@ const ChatList = memo<ChatListProps>(
key={item.id}
onActionClick={onActionClick}
onMessageChange={onMessageChange}
renderMessage={renderMessage}
showTitle={showTitle}
type={type}
{...item}
Expand Down
2 changes: 1 addition & 1 deletion src/types/llm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export interface LMParameters {
top_p?: number;
}

export type LLMRoleType = 'user' | 'system' | 'assistant';
export type LLMRoleType = 'user' | 'system' | 'assistant' | 'function';

export interface LLMMessage {
content: string;
Expand Down

0 comments on commit aedfa53

Please sign in to comment.