Spaces:
Sleeping
Sleeping
wuyiqunLu
commited on
fix: some file with empty space not get encoded correctly (#110)
Browse filesUse the previous not worked image to load the chat and see success
result:
<img width="1061" alt="image"
src="https://github.com/landing-ai/vision-agent-ui/assets/132986242/646e8bcc-c3de-412e-aa79-24dbee5bc02f">
Also, the 400 error logged completely:
<img width="1390" alt="image"
src="https://github.com/landing-ai/vision-agent-ui/assets/132986242/19fe3eb5-51ad-40d7-b64a-a2ceae3afb69">
- app/api/vision-agent/route.ts +24 -6
- components/chat/ChatMessage.tsx +1 -1
- components/ui/Img.tsx +1 -5
- lib/aws.ts +1 -1
app/api/vision-agent/route.ts
CHANGED
@@ -119,7 +119,11 @@ export const POST = withLogging(
|
|
119 |
|
120 |
const formData = new FormData();
|
121 |
formData.append('input', apiMessages);
|
122 |
-
|
|
|
|
|
|
|
|
|
123 |
|
124 |
const fetchResponse = await fetch(
|
125 |
`${process.env.AGENT_HOST}/v1/agent/chat?agent_class=vision_agent&self_reflection=false`,
|
@@ -138,16 +142,30 @@ export const POST = withLogging(
|
|
138 |
|
139 |
if (!fetchResponse.ok && fetchResponse.body) {
|
140 |
const reader = fetchResponse.body.getReader();
|
|
|
141 |
return new StreamingTextResponse(
|
142 |
new ReadableStream({
|
143 |
async start(controller) {
|
144 |
try {
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
|
|
150 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
151 |
} catch (e) {
|
152 |
logger.error(session, (e as Error).message, request);
|
153 |
}
|
|
|
119 |
|
120 |
const formData = new FormData();
|
121 |
formData.append('input', apiMessages);
|
122 |
+
const [prefix, fileName] = mediaUrl.split(`${chatId}/`);
|
123 |
+
formData.append(
|
124 |
+
'image',
|
125 |
+
prefix + `${chatId}/` + encodeURIComponent(decodeURIComponent(fileName)),
|
126 |
+
);
|
127 |
|
128 |
const fetchResponse = await fetch(
|
129 |
`${process.env.AGENT_HOST}/v1/agent/chat?agent_class=vision_agent&self_reflection=false`,
|
|
|
142 |
|
143 |
if (!fetchResponse.ok && fetchResponse.body) {
|
144 |
const reader = fetchResponse.body.getReader();
|
145 |
+
let errorText = '';
|
146 |
return new StreamingTextResponse(
|
147 |
new ReadableStream({
|
148 |
async start(controller) {
|
149 |
try {
|
150 |
+
while (true) {
|
151 |
+
const { done, value } = await reader?.read();
|
152 |
+
if (done) {
|
153 |
+
break;
|
154 |
+
}
|
155 |
+
errorText += new TextDecoder().decode(value);
|
156 |
}
|
157 |
+
logger.error(
|
158 |
+
session,
|
159 |
+
{
|
160 |
+
message: errorText,
|
161 |
+
messageId,
|
162 |
+
chatId,
|
163 |
+
statusText: fetchResponse.statusText,
|
164 |
+
status: fetchResponse.status,
|
165 |
+
},
|
166 |
+
request,
|
167 |
+
);
|
168 |
+
controller.error(new Error(`Response error: ${errorText}`));
|
169 |
} catch (e) {
|
170 |
logger.error(session, (e as Error).message, request);
|
171 |
}
|
components/chat/ChatMessage.tsx
CHANGED
@@ -23,7 +23,7 @@ import { Button } from '../ui/Button';
|
|
23 |
import { Dialog, DialogContent, DialogTrigger } from '../ui/Dialog';
|
24 |
import Img from '../ui/Img';
|
25 |
import CodeResultDisplay from '../CodeResultDisplay';
|
26 |
-
import { useAtom
|
27 |
import { selectedMessageId } from '@/state/chat';
|
28 |
import { Message } from '@prisma/client';
|
29 |
import { Separator } from '../ui/Separator';
|
|
|
23 |
import { Dialog, DialogContent, DialogTrigger } from '../ui/Dialog';
|
24 |
import Img from '../ui/Img';
|
25 |
import CodeResultDisplay from '../CodeResultDisplay';
|
26 |
+
import { useAtom } from 'jotai';
|
27 |
import { selectedMessageId } from '@/state/chat';
|
28 |
import { Message } from '@prisma/client';
|
29 |
import { Separator } from '../ui/Separator';
|
components/ui/Img.tsx
CHANGED
@@ -26,11 +26,7 @@ const Img = React.forwardRef<
|
|
26 |
: (src as string);
|
27 |
return (
|
28 |
<Image
|
29 |
-
src={
|
30 |
-
srcString.includes('vision-agent-dev.s3')
|
31 |
-
? encodeURI(srcString)
|
32 |
-
: srcString
|
33 |
-
}
|
34 |
placeholder={placeholder}
|
35 |
width={dimensions.width}
|
36 |
height={dimensions.height}
|
|
|
26 |
: (src as string);
|
27 |
return (
|
28 |
<Image
|
29 |
+
src={srcString}
|
|
|
|
|
|
|
|
|
30 |
placeholder={placeholder}
|
31 |
width={dimensions.width}
|
32 |
height={dimensions.height}
|
lib/aws.ts
CHANGED
@@ -32,7 +32,7 @@ export const getPresignedUrl = async (
|
|
32 |
return {
|
33 |
id,
|
34 |
signedUrl: res.url,
|
35 |
-
publicUrl: `https://${process.env.AWS_BUCKET_NAME}.s3.${process.env.AWS_REGION}.amazonaws.com/${
|
36 |
fields: res.fields,
|
37 |
};
|
38 |
};
|
|
|
32 |
return {
|
33 |
id,
|
34 |
signedUrl: res.url,
|
35 |
+
publicUrl: `https://${process.env.AWS_BUCKET_NAME}.s3.${process.env.AWS_REGION}.amazonaws.com/${user}/${id}/${encodeURIComponent(fileName)}`,
|
36 |
fields: res.fields,
|
37 |
};
|
38 |
};
|