Skip to content

Commit

Permalink
refactor: change MessageAddon & AIMessageContent to Providers
Browse files Browse the repository at this point in the history
  • Loading branch information
xujingli authored and BroKun committed Dec 17, 2024
1 parent b708cee commit 11a0abf
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/serious-chairs-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@difizen/magent-chat': patch
---

refactor: change MessageAddon & AIMessageContent to Providers
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import type {
BaseChatMessageModel,
} from '../../../chat-base/protocol.js';
import { AnswerState } from '../../../chat-base/protocol.js';
import { MesssageOpProvider } from '../../protocol.js';
import {
MesssageAddonViewProvider,
MesssageContentViewProvider,
MesssageOpProvider,
} from '../../protocol.js';
import type { ChatView } from '../../view.js';

interface AIMessageProps {
Expand Down Expand Up @@ -102,6 +106,9 @@ export const RecommendQuestions = (props: { message: BaseChatMessageModel }) =>
export const AIMessageContent = (props: AIMessageProps) => {
const item = useObserve(props.item);
const instance = useInject<ChatView>(ViewInstance);
const MesssageAddonView = useInject<MesssageAddonViewProvider>(
MesssageAddonViewProvider,
);
const Markdown = instance.Markdown;
const content: ReactNode = item.content;
if (!content) {
Expand Down Expand Up @@ -133,7 +140,7 @@ export const AIMessageContent = (props: AIMessageProps) => {
</span>
)}
</div>
<AIMessageAddon {...props} />
<MesssageAddonView {...props} />
</div>
);
} else {
Expand Down Expand Up @@ -169,7 +176,7 @@ export const AIMessageContent = (props: AIMessageProps) => {
className="chat-message-ai-fail"
/>
)}
<AIMessageAddon {...props} />
<MesssageAddonView {...props} />
</div>
);
}
Expand All @@ -181,6 +188,9 @@ export const AIMessage = (props: AIMessageProps) => {
const AvatarRender = instance.AvatarRender;

const MesssageOp = useInject<MesssageOpProvider>(MesssageOpProvider);
const MesssageContentView = useInject<MesssageContentViewProvider>(
MesssageContentViewProvider,
);

if (!conversation) {
return null;
Expand All @@ -192,7 +202,8 @@ export const AIMessage = (props: AIMessageProps) => {
<div className={classNames('chat-message-main', 'chat-message-main-ai')}>
<AvatarRender item={item} />
<div className={`chat-message-container`}>
<AIMessageContent {...props} />
{/* <AIMessageContent {...props} /> */}
<MesssageContentView {...props} />

<MesssageOp item={props.item} message={props.message} />

Expand Down
16 changes: 15 additions & 1 deletion web-packages/magent-chat/src/chat-view/module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { ManaModule } from '@difizen/mana-app';

import { ChatMessageRender } from './chat-message-render.js';
import { AIMessageAddon, AIMessageContent } from './components/message/ai-message.js';
import { MessageOp } from './components/message/message-op.js';
import { DefaultChatMessageItemRenderContribution } from './default-chat-messsage-render-contribution.js';
import { ChatMessageItemRenderContribution, MesssageOpProvider } from './protocol.js';
import {
ChatMessageItemRenderContribution,
MesssageOpProvider,
MesssageContentViewProvider,
MesssageAddonViewProvider,
} from './protocol.js';
import { ChatView } from './view.js';

export const ChatViewModule = ManaModule.create('magent-chat-view')
Expand All @@ -12,6 +18,14 @@ export const ChatViewModule = ManaModule.create('magent-chat-view')
token: MesssageOpProvider,
useValue: MessageOp,
},
{
token: MesssageContentViewProvider,
useValue: AIMessageContent,
},
{
token: MesssageAddonViewProvider,
useValue: AIMessageAddon,
},
ChatView,
DefaultChatMessageItemRenderContribution,
ChatMessageRender,
Expand Down
14 changes: 14 additions & 0 deletions web-packages/magent-chat/src/chat-view/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,17 @@ export type MesssageOpProvider = FC<{
}>;

export const MesssageOpProvider = Symbol('MesssageOpProvider');

export type MesssageContentViewProvider = FC<{
message: BaseChatMessageModel;
item: BaseChatMessageItemModel;
}>;

export const MesssageContentViewProvider = Symbol('MesssageContentViewProvider');

export type MesssageAddonViewProvider = FC<{
message: BaseChatMessageModel;
item: BaseChatMessageItemModel;
}>;

export const MesssageAddonViewProvider = Symbol('MesssageAddonViewProvider');

0 comments on commit 11a0abf

Please sign in to comment.