wuyiqunLu commited on
Commit
8525542
1 Parent(s): 0c7d622

fix: should split old response (#97)

Browse files

Old conversation can be loaded:
<img width="1020" alt="image"
src="https://github.com/landing-ai/vision-agent-ui/assets/132986242/ef7df1e2-1e70-4d18-ba8d-e2574538f92a">

Files changed (1) hide show
  1. components/chat/ChatMessage.tsx +27 -3
components/chat/ChatMessage.tsx CHANGED
@@ -10,7 +10,6 @@ import {
10
  IconUser,
11
  IconGlowingDot,
12
  } from '@/components/ui/Icons';
13
- import { MessageUI } from '@/lib/types';
14
  import { WIPChunkBodyGroup, formatStreamLogs } from '@/lib/utils/content';
15
  import {
16
  Table,
@@ -29,6 +28,7 @@ import { selectedMessageId } from '@/state/chat';
29
  import { Message } from '@prisma/client';
30
  import { Separator } from '../ui/Separator';
31
  import { cn } from '@/lib/utils';
 
32
 
33
  export interface ChatMessageProps {
34
  message: Message;
@@ -36,6 +36,30 @@ export interface ChatMessageProps {
36
  wipAssistantMessage?: PrismaJson.MessageBody[];
37
  }
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  export const ChatMessage: React.FC<ChatMessageProps> = ({
40
  message,
41
  wipAssistantMessage,
@@ -48,9 +72,9 @@ export const ChatMessage: React.FC<ChatMessageProps> = ({
48
  formatStreamLogs(
49
  responseBody ??
50
  wipAssistantMessage ??
51
- (response ? JSON.parse(response) : ''),
52
  ),
53
- [response, wipAssistantMessage, result, responseBody],
54
  );
55
  // prioritize the result from the message over the WIP message
56
  const codeResult = result?.payload ?? finalResult;
 
10
  IconUser,
11
  IconGlowingDot,
12
  } from '@/components/ui/Icons';
 
13
  import { WIPChunkBodyGroup, formatStreamLogs } from '@/lib/utils/content';
14
  import {
15
  Table,
 
28
  import { Message } from '@prisma/client';
29
  import { Separator } from '../ui/Separator';
30
  import { cn } from '@/lib/utils';
31
+ import toast from 'react-hot-toast';
32
 
33
  export interface ChatMessageProps {
34
  message: Message;
 
36
  wipAssistantMessage?: PrismaJson.MessageBody[];
37
  }
38
 
39
+ const getParsedStreamLogs = (content: string) => {
40
+ const streamLogs = content.split('\n').filter(log => !!log);
41
+
42
+ const buffer = streamLogs.pop();
43
+ const parsedStreamLogs: WIPChunkBodyGroup[] = [];
44
+ try {
45
+ streamLogs.forEach(streamLog =>
46
+ parsedStreamLogs.push(JSON.parse(streamLog)),
47
+ );
48
+ } catch {
49
+ toast.error('Error parsing stream logs');
50
+ }
51
+
52
+ if (buffer) {
53
+ try {
54
+ const lastLog = JSON.parse(buffer);
55
+ parsedStreamLogs.push(lastLog);
56
+ } catch {
57
+ console.log(buffer);
58
+ }
59
+ }
60
+ return parsedStreamLogs;
61
+ };
62
+
63
  export const ChatMessage: React.FC<ChatMessageProps> = ({
64
  message,
65
  wipAssistantMessage,
 
72
  formatStreamLogs(
73
  responseBody ??
74
  wipAssistantMessage ??
75
+ (response ? getParsedStreamLogs(response) : []),
76
  ),
77
+ [response, wipAssistantMessage, responseBody],
78
  );
79
  // prioritize the result from the message over the WIP message
80
  const codeResult = result?.payload ?? finalResult;