ChatCompletion.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { ChatContext } from '@/app/chat-context';
  2. import { apiInterceptors, getAppInfo } from '@/client/api';
  3. import MonacoEditor from '@/components/chat/monaco-editor';
  4. import ChatContent from '@/new-components/chat/content/ChatContent';
  5. import { ChatContentContext } from '@/pages/chat';
  6. import { IApp } from '@/types/app';
  7. import { IChatDialogueMessageSchema } from '@/types/chat';
  8. import { STORAGE_INIT_MESSAGE_KET, getInitMessage } from '@/utils';
  9. import { useAsyncEffect } from 'ahooks';
  10. import { Modal } from 'antd';
  11. import { cloneDeep } from 'lodash';
  12. import { useSearchParams } from 'next/navigation';
  13. import React, { useContext, useEffect, useMemo, useRef, useState } from 'react';
  14. import { v4 as uuid } from 'uuid';
  15. const ChatCompletion: React.FC = () => {
  16. const scrollableRef = useRef<HTMLDivElement>(null);
  17. const searchParams = useSearchParams();
  18. const chatId = searchParams?.get('id') ?? '';
  19. const { currentDialogInfo, model } = useContext(ChatContext);
  20. const { history, handleChat, refreshDialogList, setAppInfo, setModelValue, setTemperatureValue, setResourceValue } =
  21. useContext(ChatContentContext);
  22. const [jsonModalOpen, setJsonModalOpen] = useState(false);
  23. const [jsonValue, setJsonValue] = useState<string>('');
  24. const showMessages = useMemo(() => {
  25. const tempMessage: IChatDialogueMessageSchema[] = cloneDeep(history);
  26. return tempMessage
  27. .filter(item => ['view', 'human'].includes(item.role))
  28. .map(item => {
  29. return {
  30. ...item,
  31. key: uuid(),
  32. };
  33. });
  34. }, [history]);
  35. useAsyncEffect(async () => {
  36. const initMessage = getInitMessage();
  37. if (initMessage && initMessage.id === chatId) {
  38. const [, res] = await apiInterceptors(
  39. getAppInfo({
  40. ...currentDialogInfo,
  41. }),
  42. );
  43. if (res) {
  44. const paramKey: string[] = res?.param_need?.map(i => i.type) || [];
  45. const resModel = res?.param_need?.filter(item => item.type === 'model')[0]?.value || model;
  46. const temperature = res?.param_need?.filter(item => item.type === 'temperature')[0]?.value || 0.5;
  47. const resource = res?.param_need?.filter(item => item.type === 'resource')[0]?.bind_value;
  48. setAppInfo(res || ({} as IApp));
  49. setTemperatureValue(temperature || 0.5);
  50. setModelValue(resModel);
  51. setResourceValue(resource);
  52. await handleChat(initMessage.message, {
  53. app_code: res?.app_code,
  54. model_name: resModel,
  55. ...(paramKey?.includes('temperature') && { temperature }),
  56. ...(paramKey.includes('resource') && {
  57. select_param: typeof resource === 'string' ? resource : JSON.stringify(resource),
  58. }),
  59. });
  60. await refreshDialogList();
  61. localStorage.removeItem(STORAGE_INIT_MESSAGE_KET);
  62. }
  63. }
  64. }, [chatId, currentDialogInfo]);
  65. useEffect(() => {
  66. setTimeout(() => {
  67. scrollableRef.current?.scrollTo(0, scrollableRef.current?.scrollHeight);
  68. }, 50);
  69. }, [history, history[history.length - 1]?.context]);
  70. return (
  71. <div className='flex flex-col w-5/6 mx-auto' ref={scrollableRef}>
  72. {!!showMessages.length &&
  73. showMessages.map((content, index) => {
  74. return (
  75. <ChatContent
  76. key={index}
  77. content={content}
  78. onLinkClick={() => {
  79. setJsonModalOpen(true);
  80. setJsonValue(JSON.stringify(content?.context, null, 2));
  81. }}
  82. />
  83. );
  84. })}
  85. <Modal
  86. title='JSON Editor'
  87. open={jsonModalOpen}
  88. width='60%'
  89. cancelButtonProps={{
  90. hidden: true,
  91. }}
  92. onOk={() => {
  93. setJsonModalOpen(false);
  94. }}
  95. onCancel={() => {
  96. setJsonModalOpen(false);
  97. }}
  98. >
  99. <MonacoEditor className='w-full h-[500px]' language='json' value={jsonValue} />
  100. </Modal>
  101. </div>
  102. );
  103. };
  104. export default ChatCompletion;