Commit e948cb6f by 杨明

优化

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