chat-context.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import { apiInterceptors, getUsableModels, queryAdminList } from '@/client/api';
  2. import { ChatHistoryResponse, DialogueListResponse, IChatDialogueSchema } from '@/types/chat';
  3. import { UserInfoResponse } from '@/types/userinfo';
  4. import { getUserId } from '@/utils';
  5. import { STORAGE_THEME_KEY } from '@/utils/constants/index';
  6. import { useRequest } from 'ahooks';
  7. import { useSearchParams } from 'next/navigation';
  8. import { createContext, useEffect, useState } from 'react';
  9. type ThemeMode = 'dark' | 'light';
  10. interface IChatContext {
  11. mode: ThemeMode;
  12. isContract?: boolean;
  13. isMenuExpand?: boolean;
  14. scene: IChatDialogueSchema['chat_mode'] | (string & {});
  15. chatId: string;
  16. model: string;
  17. modelList: string[];
  18. dbParam?: string;
  19. agent: string;
  20. dialogueList?: DialogueListResponse;
  21. setAgent?: (val: string) => void;
  22. setMode: (mode: ThemeMode) => void;
  23. setModel: (val: string) => void;
  24. setIsContract: (val: boolean) => void;
  25. setIsMenuExpand: (val: boolean) => void;
  26. setDbParam: (val: string) => void;
  27. currentDialogue?: DialogueListResponse[0];
  28. history: ChatHistoryResponse;
  29. setHistory: (val: ChatHistoryResponse) => void;
  30. docId?: number;
  31. setDocId: (docId: number) => void;
  32. // 当前对话信息
  33. currentDialogInfo: {
  34. chat_scene: string;
  35. app_code: string;
  36. };
  37. setCurrentDialogInfo: (val: { chat_scene: string; app_code: string }) => void;
  38. adminList: UserInfoResponse[];
  39. refreshDialogList?: any;
  40. }
  41. function getDefaultTheme(): ThemeMode {
  42. const theme = localStorage.getItem(STORAGE_THEME_KEY) as ThemeMode;
  43. if (theme) return theme;
  44. return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
  45. }
  46. const ChatContext = createContext<IChatContext>({
  47. mode: 'light',
  48. scene: '',
  49. chatId: '',
  50. model: '',
  51. modelList: [],
  52. dbParam: undefined,
  53. dialogueList: [],
  54. agent: '',
  55. setAgent: () => {},
  56. setModel: () => {},
  57. setIsContract: () => {},
  58. setIsMenuExpand: () => {},
  59. setDbParam: () => void 0,
  60. setMode: () => void 0,
  61. history: [],
  62. setHistory: () => {},
  63. docId: undefined,
  64. setDocId: () => {},
  65. currentDialogInfo: {
  66. chat_scene: '',
  67. app_code: '',
  68. },
  69. setCurrentDialogInfo: () => {},
  70. adminList: [],
  71. refreshDialogList: () => {},
  72. });
  73. const ChatContextProvider = ({ children }: { children: React.ReactElement }) => {
  74. const searchParams = useSearchParams();
  75. const chatId = searchParams?.get('id') ?? '';
  76. const scene = searchParams?.get('scene') ?? '';
  77. const db_param = searchParams?.get('db_param') ?? '';
  78. const [isContract, setIsContract] = useState(false);
  79. const [model, setModel] = useState<string>('');
  80. const [isMenuExpand, setIsMenuExpand] = useState<boolean>(scene !== 'chat_dashboard');
  81. const [dbParam, setDbParam] = useState<string>(db_param);
  82. const [agent, setAgent] = useState<string>('');
  83. const [history, setHistory] = useState<ChatHistoryResponse>([]);
  84. const [docId, setDocId] = useState<number>();
  85. const [mode, setMode] = useState<ThemeMode>('light');
  86. // 管理员列表
  87. const [adminList, setAdminList] = useState<UserInfoResponse[]>([]);
  88. const [currentDialogInfo, setCurrentDialogInfo] = useState({
  89. chat_scene: '',
  90. app_code: '',
  91. });
  92. // 获取model
  93. const { data: modelList = [] } = useRequest(async () => {
  94. const [, res] = await apiInterceptors(getUsableModels());
  95. return res ?? [];
  96. });
  97. // 获取管理员列表
  98. const { run: queryAdminListRun } = useRequest(
  99. async () => {
  100. const [, res] = await apiInterceptors(queryAdminList({ role: 'admin' }));
  101. return res ?? [];
  102. },
  103. {
  104. onSuccess: data => {
  105. setAdminList(data);
  106. },
  107. manual: true,
  108. },
  109. );
  110. useEffect(() => {
  111. if (getUserId()) {
  112. queryAdminListRun();
  113. }
  114. // eslint-disable-next-line react-hooks/exhaustive-deps
  115. }, [queryAdminListRun, getUserId()]);
  116. useEffect(() => {
  117. setMode(getDefaultTheme());
  118. try {
  119. const dialogInfo = JSON.parse(localStorage.getItem('cur_dialog_info') || '');
  120. setCurrentDialogInfo(dialogInfo);
  121. } catch {
  122. setCurrentDialogInfo({
  123. chat_scene: '',
  124. app_code: '',
  125. });
  126. }
  127. }, []);
  128. useEffect(() => {
  129. setModel(modelList[0]);
  130. }, [modelList, modelList?.length]);
  131. const contextValue = {
  132. isContract,
  133. isMenuExpand,
  134. scene,
  135. chatId,
  136. model,
  137. modelList,
  138. dbParam: dbParam || db_param,
  139. agent,
  140. setAgent,
  141. mode,
  142. setMode,
  143. setModel,
  144. setIsContract,
  145. setIsMenuExpand,
  146. setDbParam,
  147. history,
  148. setHistory,
  149. docId,
  150. setDocId,
  151. currentDialogInfo,
  152. setCurrentDialogInfo,
  153. adminList,
  154. };
  155. return <ChatContext.Provider value={contextValue}>{children}</ChatContext.Provider>;
  156. };
  157. export { ChatContext, ChatContextProvider };