forked from lobehub/lobe-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ refactor: Refactor ChatList (lobehub#147)
- Loading branch information
1 parent
a977383
commit aa4216c
Showing
14 changed files
with
234 additions
and
200 deletions.
There are no files selected for viewing
This file contains 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
3 changes: 2 additions & 1 deletion
3
src/app/chat/features/Conversation/ChatList/Error/OpenAPIKey.tsx
This file contains 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
This file contains 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
This file contains 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
This file contains 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
56 changes: 17 additions & 39 deletions
56
src/app/chat/features/Conversation/ChatList/Error/index.tsx
This file contains 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 |
---|---|---|
@@ -1,48 +1,26 @@ | ||
import { IPluginErrorType, PluginErrorType } from '@lobehub/chat-plugin-sdk'; | ||
import { RenderErrorMessage } from '@lobehub/ui'; | ||
import { PluginErrorType } from '@lobehub/chat-plugin-sdk'; | ||
import { ChatListProps } from '@lobehub/ui'; | ||
|
||
import { ChatMessage } from '@/types/chatMessage'; | ||
import { ChatErrorType, ErrorType } from '@/types/fetch'; | ||
import { ChatErrorType } from '@/types/fetch'; | ||
|
||
import InvalidAccess from './InvalidAccess'; | ||
import OpenAPIKey from './OpenAPIKey'; | ||
import OpenAiBizError from './OpenAiBizError'; | ||
import PluginError from './Plugin/PluginError'; | ||
import PluginSettings from './Plugin/PluginSettings'; | ||
|
||
export const renderErrorMessage: RenderErrorMessage = (error, message: ChatMessage) => { | ||
switch (error.type as IPluginErrorType) { | ||
case PluginErrorType.PluginMarketIndexNotFound: | ||
case PluginErrorType.PluginMarketIndexInvalid: | ||
case PluginErrorType.PluginMetaInvalid: | ||
case PluginErrorType.PluginMetaNotFound: | ||
case PluginErrorType.PluginManifestInvalid: | ||
case PluginErrorType.PluginManifestNotFound: | ||
case PluginErrorType.PluginApiNotFound: | ||
case PluginErrorType.PluginApiParamsError: | ||
case PluginErrorType.PluginServerError: { | ||
return <PluginError content={(error as any).body} id={message.id} />; | ||
} | ||
case PluginErrorType.PluginSettingsInvalid: { | ||
return ( | ||
message.plugin?.identifier && ( | ||
<PluginSettings id={message.id} pluginIdentifier={message.plugin?.identifier} /> | ||
) | ||
); | ||
} | ||
} | ||
|
||
switch (error.type as ErrorType) { | ||
case ChatErrorType.InvalidAccessCode: { | ||
return <InvalidAccess id={message.id} />; | ||
} | ||
|
||
case ChatErrorType.NoAPIKey: { | ||
return <OpenAPIKey id={message.id} />; | ||
} | ||
|
||
case ChatErrorType.OpenAIBizError: { | ||
return <OpenAiBizError content={(error as any).body} id={message.id} />; | ||
} | ||
} | ||
export const renderErrorMessages: ChatListProps['renderErrorMessages'] = { | ||
[PluginErrorType.PluginMarketIndexNotFound]: PluginError, | ||
[PluginErrorType.PluginMarketIndexInvalid]: PluginError, | ||
[PluginErrorType.PluginMetaInvalid]: PluginError, | ||
[PluginErrorType.PluginMetaNotFound]: PluginError, | ||
[PluginErrorType.PluginManifestInvalid]: PluginError, | ||
[PluginErrorType.PluginManifestNotFound]: PluginError, | ||
[PluginErrorType.PluginApiNotFound]: PluginError, | ||
[PluginErrorType.PluginApiParamsError]: PluginError, | ||
[PluginErrorType.PluginServerError]: PluginError, | ||
[PluginErrorType.PluginSettingsInvalid]: PluginSettings, | ||
[ChatErrorType.InvalidAccessCode]: InvalidAccess, | ||
[ChatErrorType.NoAPIKey]: OpenAPIKey, | ||
[ChatErrorType.OpenAIBizError]: OpenAiBizError, | ||
}; |
85 changes: 0 additions & 85 deletions
85
src/app/chat/features/Conversation/ChatList/MessageExtra.tsx
This file was deleted.
Oops, something went wrong.
82 changes: 82 additions & 0 deletions
82
src/app/chat/features/Conversation/ChatList/Messages/Assistant.tsx
This file contains 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,82 @@ | ||
import { SiOpenai } from '@icons-pack/react-simple-icons'; | ||
import { ActionIconGroup, RenderAction, Tag, useChatListActionsBar } from '@lobehub/ui'; | ||
import { RenderMessage, RenderMessageExtra } from '@lobehub/ui/src'; | ||
import { createStyles } from 'antd-style'; | ||
import { memo } from 'react'; | ||
import { Flexbox } from 'react-layout-kit'; | ||
|
||
import { useSessionStore } from '@/store/session'; | ||
import { agentSelectors } from '@/store/session/slices/agentConfig'; | ||
import { isFunctionMessage } from '@/utils/message'; | ||
|
||
import FunctionCall from '../Plugins/FunctionCall'; | ||
import { DefautMessage } from './Default'; | ||
import { SystemActionsBar } from './System'; | ||
|
||
const useStyles = createStyles(({ css }) => ({ | ||
container: css` | ||
margin-top: 8px; | ||
`, | ||
plugin: css` | ||
display: flex; | ||
gap: 4px; | ||
align-items: center; | ||
width: fit-content; | ||
`, | ||
})); | ||
|
||
export const AssistantMessage: RenderMessage = memo( | ||
({ id, plugin, function_call, content, ...props }) => { | ||
const chatLoadingId = useSessionStore((s) => s.chatLoadingId); | ||
|
||
if (!isFunctionMessage(content)) return <DefautMessage content={content} id={id} {...props} />; | ||
|
||
const itemId = plugin?.identifier || function_call?.name; | ||
const command = plugin ?? function_call; | ||
const args = command?.arguments; | ||
const fcProps = { | ||
arguments: args, | ||
command, | ||
content: content, | ||
id: itemId, | ||
loading: id === chatLoadingId, | ||
}; | ||
|
||
return ( | ||
<div id={itemId}> | ||
<FunctionCall {...fcProps} /> | ||
</div> | ||
); | ||
}, | ||
); | ||
|
||
export const AssistantMessageExtra: RenderMessageExtra = memo(({ extra, function_call }) => { | ||
const { styles } = useStyles(); | ||
const model = useSessionStore(agentSelectors.currentAgentModel); | ||
|
||
const hasModelTag = extra?.fromModel && model !== extra?.fromModel; | ||
const hasFuncTag = !!function_call; | ||
if (!(hasModelTag || hasFuncTag)) return; | ||
|
||
return ( | ||
<Flexbox className={styles.container} horizontal> | ||
<div> | ||
<Tag icon={<SiOpenai size={'1em'} />}>{extra?.fromModel as string}</Tag> | ||
</div> | ||
</Flexbox> | ||
); | ||
}); | ||
|
||
export const AssistantActionsBar: RenderAction = memo(({ text, error, id, ...props }) => { | ||
const { regenerate, edit, copy, divider, del } = useChatListActionsBar(text); | ||
if (id === 'default') return; | ||
if (error) return <SystemActionsBar id={id} text={text} {...props} />; | ||
return ( | ||
<ActionIconGroup | ||
dropdownMenu={[edit, copy, regenerate, divider, del]} | ||
items={[regenerate, copy]} | ||
type="ghost" | ||
{...props} | ||
/> | ||
); | ||
}); |
6 changes: 6 additions & 0 deletions
6
src/app/chat/features/Conversation/ChatList/Messages/Default.tsx
This file contains 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,6 @@ | ||
import { RenderMessage } from '@lobehub/ui/src'; | ||
import { memo } from 'react'; | ||
|
||
export const DefautMessage: RenderMessage = memo(({ id, content }) => { | ||
return <div id={id}>{content}</div>; | ||
}); |
Oops, something went wrong.