Skip to content

Commit

Permalink
feat(chat): support customize thinking result component (#3702)
Browse files Browse the repository at this point in the history
* feat: supportthingResult custome Render

* feat: supportthingResult custome Render
  • Loading branch information
wangxiaojuan authored May 21, 2024
1 parent ec4a61a commit edfcc16
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
6 changes: 6 additions & 0 deletions packages/ai-native/src/browser/chat/chat.render.registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
ChatAIRoleRender,
ChatInputRender,
ChatThinkingRender,
ChatThinkingResultRender,
ChatUserRoleRender,
ChatWelcomeRender,
IChatRenderRegistry,
Expand All @@ -17,6 +18,7 @@ export class ChatRenderRegistry extends Disposable implements IChatRenderRegistr
public chatUserRoleRender?: ChatUserRoleRender;
public chatThinkingRender?: ChatThinkingRender;
public chatInputRender?: ChatInputRender;
public chatThinkingResultRender?: ChatThinkingResultRender;

registerWelcomeRender(render: ChatWelcomeRender): void {
this.chatWelcomeRender = render;
Expand All @@ -37,4 +39,8 @@ export class ChatRenderRegistry extends Disposable implements IChatRenderRegistr
registerInputRender(render: ChatInputRender): void {
this.chatInputRender = render;
}

registerThinkingResultRender(render: ChatThinkingResultRender): void {
this.chatThinkingResultRender = render;
}
}
12 changes: 11 additions & 1 deletion packages/ai-native/src/browser/components/ChatThinking.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export const ChatThinkingResult = ({
}: ITinkingProps) => {
const aiChatService = useInjectable<ChatInternalService>(IChatInternalService);
const [latestRequestId, setLatestRequestId] = useState(aiChatService.latestRequestId);
const chatRenderRegistry = useInjectable<ChatRenderRegistry>(ChatRenderRegistryToken);

useEffect(() => {
const dispose = aiChatService.onChangeRequestId((id) => {
Expand All @@ -92,6 +93,11 @@ export const ChatThinkingResult = ({
return () => dispose.dispose();
}, [aiChatService]);

const CustomThinkingResultRender = useMemo(
() => chatRenderRegistry.chatThinkingResultRender,
[chatRenderRegistry, chatRenderRegistry.chatThinkingResultRender],
);

const handleRegenerate = useCallback(() => {
if (onRegenerate) {
onRegenerate();
Expand All @@ -100,7 +106,11 @@ export const ChatThinkingResult = ({

const renderContent = useCallback(() => {
if (typeof hasMessage === 'boolean' ? !hasMessage : !message?.trim()) {
return <span>{localize('aiNative.chat.stop.immediately')}</span>;
const stopimmediately = localize('aiNative.chat.stop.immediately');
if (CustomThinkingResultRender) {
return <CustomThinkingResultRender thinkingResult={stopimmediately} />;
}
return <span>{stopimmediately}</span>;
}

return children;
Expand Down
2 changes: 2 additions & 0 deletions packages/ai-native/src/browser/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export type ChatUserRoleRender = (props: {
command?: string;
}) => React.ReactElement | React.JSX.Element;
export type ChatThinkingRender = (props: { thinkingText?: string }) => React.ReactElement | React.JSX.Element;
export type ChatThinkingResultRender = (props: { thinkingResult?: string }) => React.ReactElement | React.JSX.Element;
export type ChatInputRender = (props: {
onSend: (value: string, agentId?: string, command?: string) => void;
onValueChange?: (value: string) => void;
Expand Down Expand Up @@ -112,6 +113,7 @@ export interface IChatRenderRegistry {
*/
registerUserRoleRender(render: ChatUserRoleRender): void;
registerThinkingRender(render: ChatThinkingRender): void;
registerThinkingResultRender(render: ChatThinkingResultRender): void;
/**
* 输入框渲染
*/
Expand Down

0 comments on commit edfcc16

Please sign in to comment.