wuyiqunLu commited on
Commit
7acedd7
1 Parent(s): d360551

fix: revalidatePath not work in api route handler (#95)

Browse files

https://nextjs.org/docs/app/building-your-application/caching#data-cache-and-client-side-router-cache
after ending, see the refreshed data
<img width="1524" alt="image"
src="https://github.com/landing-ai/vision-agent-ui/assets/132986242/65fb8076-26e9-4cb2-9e11-757a5db32a2c">

app/api/vision-agent/route.ts CHANGED
@@ -118,7 +118,6 @@ export const POST = withLogging(
118
  request,
119
  ) => {
120
  const { apiMessages, mediaUrl, chatId, messageId } = json;
121
- const messages: MessageUI[] = JSON.parse(apiMessages);
122
  const user = session?.user?.email ?? 'anonymous';
123
 
124
  const formData = new FormData();
@@ -323,6 +322,12 @@ export const POST = withLogging(
323
  request,
324
  '__AGENT_DONE',
325
  );
 
 
 
 
 
 
326
  controller.close();
327
  }
328
  }
 
118
  request,
119
  ) => {
120
  const { apiMessages, mediaUrl, chatId, messageId } = json;
 
121
  const user = session?.user?.email ?? 'anonymous';
122
 
123
  const formData = new FormData();
 
322
  request,
323
  '__AGENT_DONE',
324
  );
325
+ // This is just using to trigger the onFinish event in useChat hooks
326
+ // but the message isn't used anywhere
327
+ // https://github.com/vercel/ai/blob/main/packages/ui-utils/src/call-chat-api.ts#L118
328
+ controller.enqueue(
329
+ encoder.encode('0:' + JSON.stringify('DONE') + '\n'),
330
+ );
331
  controller.close();
332
  }
333
  }
lib/hooks/useVisionAgent.ts CHANGED
@@ -6,6 +6,7 @@ import { convertDBMessageToAPIMessage } from '../utils/message';
6
  import { useSetAtom } from 'jotai';
7
  import { selectedMessageId } from '@/state/chat';
8
  import { Message } from '@prisma/client';
 
9
 
10
  const useVisionAgent = (chat: ChatWithMessages) => {
11
  const { messages: dbMessages, id, mediaUrl } = chat;
@@ -15,6 +16,7 @@ const useVisionAgent = (chat: ChatWithMessages) => {
15
  // Temporary solution for now while single we have to pass mediaUrl separately outside of the messages
16
  const currMediaUrl = useRef<string>(mediaUrl);
17
  const currMessageId = useRef<string>(latestDbMessage?.id);
 
18
 
19
  const { append, isLoading, data, reload } = useChat({
20
  api: '/api/vision-agent',
@@ -24,6 +26,7 @@ const useVisionAgent = (chat: ChatWithMessages) => {
24
  }
25
  },
26
  onFinish: message => {
 
27
  setMessageId(currMessageId.current);
28
  },
29
  body: {
 
6
  import { useSetAtom } from 'jotai';
7
  import { selectedMessageId } from '@/state/chat';
8
  import { Message } from '@prisma/client';
9
+ import { useRouter } from 'next/navigation';
10
 
11
  const useVisionAgent = (chat: ChatWithMessages) => {
12
  const { messages: dbMessages, id, mediaUrl } = chat;
 
16
  // Temporary solution for now while single we have to pass mediaUrl separately outside of the messages
17
  const currMediaUrl = useRef<string>(mediaUrl);
18
  const currMessageId = useRef<string>(latestDbMessage?.id);
19
+ const router = useRouter();
20
 
21
  const { append, isLoading, data, reload } = useChat({
22
  api: '/api/vision-agent',
 
26
  }
27
  },
28
  onFinish: message => {
29
+ router.refresh();
30
  setMessageId(currMessageId.current);
31
  },
32
  body: {