wuyiqunLu commited on
Commit
76503b7
1 Parent(s): 314f2dc

feat: do not use edge for vision agent api (#26)

Browse files

cause edge functions are not support native node
https://nextjs.org/docs/messages/node-module-in-edge-runtime

Files changed (2) hide show
  1. app/api/vision-agent/route.ts +56 -54
  2. next.config.js +1 -1
app/api/vision-agent/route.ts CHANGED
@@ -2,67 +2,69 @@ import { StreamingTextResponse } from 'ai';
2
 
3
  // import { auth } from '@/auth';
4
  import { MessageBase } from '../../../lib/types';
5
-
6
  import { CLEANED_SEPARATOR } from '@/lib/constants';
7
 
8
- export const runtime = 'edge';
9
  export const dynamic = 'force-dynamic';
10
  export const maxDuration = 300; // This function can run for a maximum of 5 minutes
11
 
12
- export const POST = async (
13
- request: Request,
14
- ) => {
15
- const json = await request.json();
16
- const { messages, url } = json as {
17
- messages: MessageBase[];
18
- id: string;
19
- url: string;
20
- };
 
21
 
22
- // const session = await auth();
23
- // if (!session?.user?.email) {
24
- // return new Response('Unauthorized', {
25
- // status: 401,
26
- // });
27
- // }
28
 
29
- const formData = new FormData();
30
- formData.append(
31
- 'input',
32
- JSON.stringify(
33
- messages.map(message => {
34
- if (message.role !== 'assistant') {
35
- return message;
36
- } else {
37
- const splitedContent = message.content.split(CLEANED_SEPARATOR);
38
- return {
39
- ...message,
40
- content:
41
- splitedContent.length > 1
42
- ? splitedContent[1].replace(/!\[answers.*?\.png\)/g, '')
43
- : message.content,
44
- };
45
- }
46
- }),
47
- ),
48
- );
49
- formData.append('image', url);
50
 
51
- const fetchResponse = await fetch(
52
- 'https://api.dev.landing.ai/v1/agent/chat?agent_class=vision_agent&visualize_output=true',
53
- // 'http://localhost:5050/v1/agent/chat?agent_class=vision_agent',
54
- {
55
- method: 'POST',
56
- headers: {
57
- apikey: 'land_sk_DKeoYtaZZrYqJ9TMMiXe4BIQgJcZ0s3XAoB0JT3jv73FFqnr6k',
 
 
58
  },
59
- body: formData,
60
- },
61
- );
62
 
63
- if (fetchResponse.body) {
64
- return new StreamingTextResponse(fetchResponse.body);
65
- } else {
66
- return fetchResponse;
67
- }
68
- };
 
 
2
 
3
  // import { auth } from '@/auth';
4
  import { MessageBase } from '../../../lib/types';
5
+ import { withLogging } from '@/lib/logger';
6
  import { CLEANED_SEPARATOR } from '@/lib/constants';
7
 
8
+ // export const runtime = 'edge';
9
  export const dynamic = 'force-dynamic';
10
  export const maxDuration = 300; // This function can run for a maximum of 5 minutes
11
 
12
+ export const POST = withLogging(
13
+ async (
14
+ _session,
15
+ json: {
16
+ messages: MessageBase[];
17
+ id: string;
18
+ url: string;
19
+ },
20
+ ) => {
21
+ const { messages, url } = json;
22
 
23
+ // const session = await auth();
24
+ // if (!session?.user?.email) {
25
+ // return new Response('Unauthorized', {
26
+ // status: 401,
27
+ // });
28
+ // }
29
 
30
+ const formData = new FormData();
31
+ formData.append(
32
+ 'input',
33
+ JSON.stringify(
34
+ messages.map(message => {
35
+ if (message.role !== 'assistant') {
36
+ return message;
37
+ } else {
38
+ const splitedContent = message.content.split(CLEANED_SEPARATOR);
39
+ return {
40
+ ...message,
41
+ content:
42
+ splitedContent.length > 1
43
+ ? splitedContent[1].replace(/!\[answers.*?\.png\)/g, '')
44
+ : message.content,
45
+ };
46
+ }
47
+ }),
48
+ ),
49
+ );
50
+ formData.append('image', url);
51
 
52
+ const fetchResponse = await fetch(
53
+ 'https://api.dev.landing.ai/v1/agent/chat?agent_class=vision_agent&visualize_output=true',
54
+ // 'http://localhost:5050/v1/agent/chat?agent_class=vision_agent',
55
+ {
56
+ method: 'POST',
57
+ headers: {
58
+ apikey: 'land_sk_DKeoYtaZZrYqJ9TMMiXe4BIQgJcZ0s3XAoB0JT3jv73FFqnr6k',
59
+ },
60
+ body: formData,
61
  },
62
+ );
 
 
63
 
64
+ if (fetchResponse.body) {
65
+ return new StreamingTextResponse(fetchResponse.body);
66
+ } else {
67
+ return fetchResponse;
68
+ }
69
+ },
70
+ );
next.config.js CHANGED
@@ -11,5 +11,5 @@ module.exports = {
11
  },
12
  experimental: {
13
  serverComponentsExternalPackages: ['pino', 'pino-loki'],
14
- }
15
  };
 
11
  },
12
  experimental: {
13
  serverComponentsExternalPackages: ['pino', 'pino-loki'],
14
+ },
15
  };