use-summary.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { ChatContext } from '@/app/chat-context';
  2. import { apiInterceptors, getChatHistory } from '@/client/api';
  3. import { ChatHistoryResponse } from '@/types/chat';
  4. import { useCallback, useContext } from 'react';
  5. import useChat from './use-chat';
  6. const useSummary = () => {
  7. const { history, setHistory, chatId, model, docId } = useContext(ChatContext);
  8. const { chat } = useChat({ queryAgentURL: '/knowledge/document/summary' });
  9. const summary = useCallback(
  10. async (curDocId?: number) => {
  11. const [, res] = await apiInterceptors(getChatHistory(chatId));
  12. const tempHistory: ChatHistoryResponse = [
  13. ...res!,
  14. { role: 'human', context: '', model_name: model, order: 0, time_stamp: 0 },
  15. { role: 'view', context: '', model_name: model, order: 0, time_stamp: 0, retry: true },
  16. ];
  17. const index = tempHistory.length - 1;
  18. setHistory([...tempHistory]);
  19. await chat({
  20. data: {
  21. doc_id: curDocId || docId,
  22. model_name: model,
  23. },
  24. chatId,
  25. onMessage: message => {
  26. tempHistory[index].context = message;
  27. setHistory([...tempHistory]);
  28. },
  29. });
  30. },
  31. [history, model, docId, chatId],
  32. );
  33. return summary;
  34. };
  35. export default useSummary;