Commit e948cb6f by 杨明

优化

1 parent c73ec622
'use client' "use client";
import type { RefObject } from 'react' import type { RefObject } from "react";
import { createContext, useContext } from 'use-context-selector' import { createContext, useContext } from "use-context-selector";
import type { import type { Callback, ChatConfig, ChatItemInTree, Feedback } from "../types";
Callback, import type { ThemeBuilder } from "../embedded-chatbot/theme/theme-context";
ChatConfig,
ChatItemInTree,
Feedback,
} from '../types'
import type { ThemeBuilder } from '../embedded-chatbot/theme/theme-context'
import type { import type {
AppConversationData, AppConversationData,
AppData, AppData,
AppMeta, AppMeta,
ConversationItem, ConversationItem,
} from '@/models/share' } from "@/models/share";
import { noop } from 'lodash-es' import { noop } from "lodash-es";
export type ChatWithHistoryContextValue = { export type ChatWithHistoryContextValue = {
appInfoError?: any appInfoError?: any;
appInfoLoading?: boolean appInfoLoading?: boolean;
appMeta?: AppMeta appMeta?: AppMeta;
appData?: AppData appData?: AppData;
appParams?: ChatConfig appParams?: ChatConfig;
appChatListDataLoading?: boolean appChatListDataLoading?: boolean;
currentConversationId: string currentConversationId: string;
currentConversationItem?: ConversationItem currentConversationItem?: ConversationItem;
appPrevChatTree: ChatItemInTree[] appPrevChatTree: ChatItemInTree[];
pinnedConversationList: AppConversationData['data'] pinnedConversationList: AppConversationData["data"];
conversationList: AppConversationData['data'] conversationList: AppConversationData["data"];
newConversationInputs: Record<string, any> newConversationInputs: Record<string, any>;
newConversationInputsRef: RefObject<Record<string, any>> newConversationInputsRef: RefObject<Record<string, any>>;
handleNewConversationInputsChange: (v: Record<string, any>) => void handleNewConversationInputsChange: (v: Record<string, any>) => void;
inputsForms: any[] inputsForms: any[];
handleNewConversation: () => void handleNewConversation: (v?: Record<string, any>) => void;
handleStartChat: (callback?: any) => void handleStartChat: (callback?: any) => void;
handleChangeConversation: (conversationId: string) => void handleChangeConversation: (conversationId: string) => void;
handlePinConversation: (conversationId: string) => void handlePinConversation: (conversationId: string) => void;
handleUnpinConversation: (conversationId: string) => void handleUnpinConversation: (conversationId: string) => void;
handleDeleteConversation: (conversationId: string, callback: Callback) => void handleDeleteConversation: (
conversationRenaming: boolean conversationId: string,
handleRenameConversation: (conversationId: string, newName: string, callback: Callback) => void callback: Callback
handleNewConversationCompleted: (newConversationId: string) => void ) => void;
chatShouldReloadKey: string conversationRenaming: boolean;
isMobile: boolean handleRenameConversation: (
isInstalledApp: boolean conversationId: string,
appId?: string newName: string,
handleFeedback: (messageId: string, feedback: Feedback) => void callback: Callback
currentChatInstanceRef: RefObject<{ handleStop: () => void }> ) => void;
themeBuilder?: ThemeBuilder handleNewConversationCompleted: (newConversationId: string) => void;
sidebarCollapseState?: boolean chatShouldReloadKey: string;
handleSidebarCollapse: (state: boolean) => void isMobile: boolean;
clearChatList?: boolean isInstalledApp: boolean;
setClearChatList: (state: boolean) => void appId?: string;
isResponding?: boolean handleFeedback: (messageId: string, feedback: Feedback) => void;
setIsResponding: (state: boolean) => void, currentChatInstanceRef: RefObject<{ handleStop: () => void }>;
currentConversationInputs: Record<string, any> | null, themeBuilder?: ThemeBuilder;
setCurrentConversationInputs: (v: Record<string, any>) => void, sidebarCollapseState?: boolean;
} handleSidebarCollapse: (state: boolean) => void;
clearChatList?: boolean;
setClearChatList: (state: boolean) => void;
isResponding?: boolean;
setIsResponding: (state: boolean) => void;
currentConversationInputs: Record<string, any> | null;
setCurrentConversationInputs: (v: Record<string, any>) => void;
};
export const ChatWithHistoryContext = createContext<ChatWithHistoryContextValue>({ export const ChatWithHistoryContext =
currentConversationId: '', createContext<ChatWithHistoryContextValue>({
appPrevChatTree: [], currentConversationId: "",
pinnedConversationList: [], appPrevChatTree: [],
conversationList: [], pinnedConversationList: [],
newConversationInputs: {}, conversationList: [],
newConversationInputsRef: { current: {} }, newConversationInputs: {},
handleNewConversationInputsChange: noop, newConversationInputsRef: { current: {} },
inputsForms: [], handleNewConversationInputsChange: noop,
handleNewConversation: noop, inputsForms: [],
handleStartChat: noop, handleNewConversation: noop,
handleChangeConversation: noop, handleStartChat: noop,
handlePinConversation: noop, handleChangeConversation: noop,
handleUnpinConversation: noop, handlePinConversation: noop,
handleDeleteConversation: noop, handleUnpinConversation: noop,
conversationRenaming: false, handleDeleteConversation: noop,
handleRenameConversation: noop, conversationRenaming: false,
handleNewConversationCompleted: noop, handleRenameConversation: noop,
chatShouldReloadKey: '', handleNewConversationCompleted: noop,
isMobile: false, chatShouldReloadKey: "",
isInstalledApp: false, isMobile: false,
handleFeedback: noop, isInstalledApp: false,
currentChatInstanceRef: { current: { handleStop: noop } }, handleFeedback: noop,
sidebarCollapseState: false, currentChatInstanceRef: { current: { handleStop: noop } },
handleSidebarCollapse: noop, sidebarCollapseState: false,
clearChatList: false, handleSidebarCollapse: noop,
setClearChatList: noop, clearChatList: false,
isResponding: false, setClearChatList: noop,
setIsResponding: noop, isResponding: false,
currentConversationInputs: {}, setIsResponding: noop,
setCurrentConversationInputs: noop, currentConversationInputs: {},
}) setCurrentConversationInputs: noop,
export const useChatWithHistoryContext = () => useContext(ChatWithHistoryContext) });
export const useChatWithHistoryContext = () =>
useContext(ChatWithHistoryContext);
...@@ -214,6 +214,11 @@ export const useChatWithHistory = (installedAppInfo?: InstalledApp) => { ...@@ -214,6 +214,11 @@ export const useChatWithHistory = (installedAppInfo?: InstalledApp) => {
(newInputs: Record<string, any>) => { (newInputs: Record<string, any>) => {
newConversationInputsRef.current = newInputs; newConversationInputsRef.current = newInputs;
setNewConversationInputs(newInputs); setNewConversationInputs(newInputs);
console.log(
newConversationInputsRef,
newConversationInputs,
"newConversationInputs"
);
}, },
[] []
); );
...@@ -260,14 +265,14 @@ export const useChatWithHistory = (installedAppInfo?: InstalledApp) => { ...@@ -260,14 +265,14 @@ export const useChatWithHistory = (installedAppInfo?: InstalledApp) => {
}; };
}); });
}, [appParams]); }, [appParams]);
useEffect(() => { // useEffect(() => {
const conversationInputs: Record<string, any> = {}; // const conversationInputs: Record<string, any> = {};
inputsForms.forEach((item: any) => { // inputsForms.forEach((item: any) => {
conversationInputs[item.variable] = item.default || null; // conversationInputs[item.variable] = item.default || null;
}); // });
handleNewConversationInputsChange(conversationInputs); // handleNewConversationInputsChange(conversationInputs);
}, [handleNewConversationInputsChange, inputsForms]); // }, [handleNewConversationInputsChange, inputsForms]);
const { data: newConversation } = useSWR( const { data: newConversation } = useSWR(
newConversationId ? [isInstalledApp, appId, newConversationId] : null, newConversationId ? [isInstalledApp, appId, newConversationId] : null,
...@@ -414,11 +419,15 @@ export const useChatWithHistory = (installedAppInfo?: InstalledApp) => { ...@@ -414,11 +419,15 @@ export const useChatWithHistory = (installedAppInfo?: InstalledApp) => {
[handleConversationIdInfoChange, setClearChatList] [handleConversationIdInfoChange, setClearChatList]
); );
const handleNewConversation = useCallback( const handleNewConversation = useCallback(
(inputs?: Record<string, any>,isReset?:boolean) => { (inputs?: Record<string, any>) => {
console.log(123);
currentChatInstanceRef.current.handleStop(); currentChatInstanceRef.current.handleStop();
setShowNewConversationItemInList(true); setShowNewConversationItemInList(true);
handleChangeConversation(""); handleChangeConversation("");
!isReset && handleNewConversationInputsChange(inputs || {}); if (inputs) {
newConversationInputsRef.current = inputs;
setNewConversationInputs(inputs);
}
setClearChatList(true); setClearChatList(true);
}, },
[ [
......
...@@ -60,6 +60,11 @@ const ChatWithHistory: FC<ChatWithHistoryProps> = ({ className }) => { ...@@ -60,6 +60,11 @@ const ChatWithHistory: FC<ChatWithHistoryProps> = ({ className }) => {
// 示例:调用 ChatWrapper 的方法 // 示例:调用 ChatWrapper 的方法
const autoSend = () => { const autoSend = () => {
console.log(
chatWrapperRef.current,
presuppositionProblem,
"presuppositionProblem"
);
if (chatWrapperRef.current && presuppositionProblem) { if (chatWrapperRef.current && presuppositionProblem) {
chatWrapperRef.current.doSend({ chatWrapperRef.current.doSend({
message: presuppositionProblem, message: presuppositionProblem,
...@@ -70,38 +75,34 @@ const ChatWithHistory: FC<ChatWithHistoryProps> = ({ className }) => { ...@@ -70,38 +75,34 @@ const ChatWithHistory: FC<ChatWithHistoryProps> = ({ className }) => {
const [showConfirm, setShowConfirm] = useState(false); const [showConfirm, setShowConfirm] = useState(false);
const handleReset = () => { const handleReset = () => {
handleNewConversation(); handleNewConversation();
setTimeout(()=>{
handleNewConversation()
},1000)
setShowConfirm(false); setShowConfirm(false);
setTimeout(() => {
handleNewConversationInputsChange(newInputs);
}, 300);
}; };
const handleCancelConfirm = () => { const handleCancelConfirm = () => {
setShowConfirm(false); setShowConfirm(false);
}; };
const { const {
appInfoError, appInfoError,
appData,
appInfoLoading, appInfoLoading,
appChatListDataLoading, appChatListDataLoading,
chatShouldReloadKey, chatShouldReloadKey,
isMobile, isMobile,
handleNewConversation, handleNewConversation,
handleNewConversationInputsChange,
} = useChatWithHistoryContext(); } = useChatWithHistoryContext();
const hasSent = useRef(false); // 标记是否已发送 const hasSent = useRef(false); // 标记是否已发送
useEffect(() => { useEffect(() => {
//新建对话 //新建对话
if (!hasSent.current) { if (!hasSent.current && chatWrapperRef.current) {
handleNewConversation(); console.log("newInputs", newInputs);
handleNewConversation(newInputs);
setTimeout(() => { setTimeout(() => {
handleNewConversationInputsChange(newInputs); autoSend();
setTimeout(() => { }, 300);
autoSend();
}, 300);
}, 1000);
hasSent.current = true; // 标记已发送 hasSent.current = true; // 标记已发送
} }
}, []); }, [chatWrapperRef.current]);
const doClear = useCallback(() => { const doClear = useCallback(() => {
setShowConfirm(true); setShowConfirm(true);
}, [handleNewConversation]); }, [handleNewConversation]);
...@@ -123,6 +124,8 @@ const ChatWithHistory: FC<ChatWithHistoryProps> = ({ className }) => { ...@@ -123,6 +124,8 @@ const ChatWithHistory: FC<ChatWithHistoryProps> = ({ className }) => {
{showConfirm && ( {showConfirm && (
<Confirm <Confirm
title="重置对话" title="重置对话"
confirmText="确认"
cancelText="取消"
content="您确定要重置此对话吗?" content="您确定要重置此对话吗?"
isShow isShow
onCancel={handleCancelConfirm} onCancel={handleCancelConfirm}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!