Spaces:
Sleeping
Sleeping
wuyiqunLu
commited on
feat: only send answer when resending messages (#25)
Browse fileshttps://app.asana.com/0/1204554785675703/1207171219059373/f
<img width="1242" alt="image"
src="https://github.com/landing-ai/vision-agent-ui/assets/132986242/1dd8f47c-fcac-4c35-ae30-1fed61e0edef">
- app/api/vision-agent/route.ts +20 -2
- lib/constants.ts +1 -0
- lib/hooks/useCleanedUpMessages.ts +3 -4
- lib/hooks/useVisionAgent.tsx +5 -5
app/api/vision-agent/route.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { StreamingTextResponse } from 'ai';
|
|
| 2 |
|
| 3 |
// import { auth } from '@/auth';
|
| 4 |
import { MessageBase } from '../../../lib/types';
|
|
|
|
| 5 |
|
| 6 |
// export const runtime = 'edge';
|
| 7 |
export const dynamic = 'force-dynamic';
|
|
@@ -23,9 +24,26 @@ export async function POST(req: Request) {
|
|
| 23 |
// }
|
| 24 |
|
| 25 |
const formData = new FormData();
|
| 26 |
-
formData.append(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
formData.append('image', url);
|
| 28 |
-
|
| 29 |
const fetchResponse = await fetch(
|
| 30 |
'https://api.dev.landing.ai/v1/agent/chat?agent_class=vision_agent&visualize_output=true',
|
| 31 |
// 'http://localhost:5050/v1/agent/chat?agent_class=vision_agent',
|
|
|
|
| 2 |
|
| 3 |
// import { auth } from '@/auth';
|
| 4 |
import { MessageBase } from '../../../lib/types';
|
| 5 |
+
import { CLEANED_SEPARATOR } from '@/lib/constants';
|
| 6 |
|
| 7 |
// export const runtime = 'edge';
|
| 8 |
export const dynamic = 'force-dynamic';
|
|
|
|
| 24 |
// }
|
| 25 |
|
| 26 |
const formData = new FormData();
|
| 27 |
+
formData.append(
|
| 28 |
+
'input',
|
| 29 |
+
JSON.stringify(
|
| 30 |
+
messages.map(message => {
|
| 31 |
+
if (message.role !== 'assistant') {
|
| 32 |
+
return message;
|
| 33 |
+
} else {
|
| 34 |
+
const splitedContent = message.content.split(CLEANED_SEPARATOR);
|
| 35 |
+
return {
|
| 36 |
+
...message,
|
| 37 |
+
content:
|
| 38 |
+
splitedContent.length > 1
|
| 39 |
+
? splitedContent[1].replace(/!\[answers.*?\.png\)/g, '')
|
| 40 |
+
: message.content,
|
| 41 |
+
};
|
| 42 |
+
}
|
| 43 |
+
}),
|
| 44 |
+
),
|
| 45 |
+
);
|
| 46 |
formData.append('image', url);
|
|
|
|
| 47 |
const fetchResponse = await fetch(
|
| 48 |
'https://api.dev.landing.ai/v1/agent/chat?agent_class=vision_agent&visualize_output=true',
|
| 49 |
// 'http://localhost:5050/v1/agent/chat?agent_class=vision_agent',
|
lib/constants.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
export const CLEANED_SEPARATOR = '|CLEANED|';
|
lib/hooks/useCleanedUpMessages.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
-
import { useMemo
|
| 2 |
-
import { MessageBase
|
| 3 |
-
import {
|
| 4 |
|
| 5 |
const PAIRS: Record<string, string> = {
|
| 6 |
'┍': '┑',
|
|
@@ -11,7 +11,6 @@ const PAIRS: Record<string, string> = {
|
|
| 11 |
|
| 12 |
const MIDDLE_STARTER = '┝';
|
| 13 |
const MIDDLE_SEPARATOR = '┿';
|
| 14 |
-
export const CLEANED_SEPARATOR = '|CLEANED|';
|
| 15 |
|
| 16 |
export const getCleanedUpMessages = ({
|
| 17 |
content,
|
|
|
|
| 1 |
+
import { useMemo } from 'react';
|
| 2 |
+
import { MessageBase } from '../types';
|
| 3 |
+
import { CLEANED_SEPARATOR } from '../constants';
|
| 4 |
|
| 5 |
const PAIRS: Record<string, string> = {
|
| 6 |
'┍': '┑',
|
|
|
|
| 11 |
|
| 12 |
const MIDDLE_STARTER = '┝';
|
| 13 |
const MIDDLE_SEPARATOR = '┿';
|
|
|
|
| 14 |
|
| 15 |
export const getCleanedUpMessages = ({
|
| 16 |
content,
|
lib/hooks/useVisionAgent.tsx
CHANGED
|
@@ -4,10 +4,8 @@ import { useEffect, useState } from 'react';
|
|
| 4 |
import { ChatEntity, MessageBase, SignedPayload } from '../types';
|
| 5 |
import { saveKVChatMessage } from '../kv/chat';
|
| 6 |
import { fetcher } from '../utils';
|
| 7 |
-
import {
|
| 8 |
-
|
| 9 |
-
CLEANED_SEPARATOR,
|
| 10 |
-
} from './useCleanedUpMessages';
|
| 11 |
|
| 12 |
const uploadBase64 = async (
|
| 13 |
base64: string,
|
|
@@ -15,7 +13,9 @@ const uploadBase64 = async (
|
|
| 15 |
chatId: string,
|
| 16 |
index: number,
|
| 17 |
) => {
|
| 18 |
-
const res = await fetch(
|
|
|
|
|
|
|
| 19 |
const blob = await res.blob();
|
| 20 |
const { signedUrl, publicUrl, fields } = await fetcher<SignedPayload>(
|
| 21 |
'/api/sign',
|
|
|
|
| 4 |
import { ChatEntity, MessageBase, SignedPayload } from '../types';
|
| 5 |
import { saveKVChatMessage } from '../kv/chat';
|
| 6 |
import { fetcher } from '../utils';
|
| 7 |
+
import { getCleanedUpMessages } from './useCleanedUpMessages';
|
| 8 |
+
import { CLEANED_SEPARATOR } from '../constants';
|
|
|
|
|
|
|
| 9 |
|
| 10 |
const uploadBase64 = async (
|
| 11 |
base64: string,
|
|
|
|
| 13 |
chatId: string,
|
| 14 |
index: number,
|
| 15 |
) => {
|
| 16 |
+
const res = await fetch(
|
| 17 |
+
'data:image/png;base64,' + base64.replace('base:64', ''),
|
| 18 |
+
);
|
| 19 |
const blob = await res.blob();
|
| 20 |
const { signedUrl, publicUrl, fields } = await fetcher<SignedPayload>(
|
| 21 |
'/api/sign',
|