parent
							
								
									e1f286efb8
								
							
						
					
					
						commit
						4f92751ee3
					
				|  | @ -1,5 +1,5 @@ | ||||||
| import { ChatBody, Message } from '@/types/chat'; | import { Message } from '@/types/chat'; | ||||||
| import { GoogleSource } from '@/types/google'; | import { GoogleBody, GoogleSource } from '@/types/google'; | ||||||
| import { OPENAI_API_HOST } from '@/utils/app/const'; | import { OPENAI_API_HOST } from '@/utils/app/const'; | ||||||
| import { cleanSourceText } from '@/utils/server/google'; | import { cleanSourceText } from '@/utils/server/google'; | ||||||
| import { Readability } from '@mozilla/readability'; | import { Readability } from '@mozilla/readability'; | ||||||
|  | @ -9,14 +9,17 @@ import { NextApiRequest, NextApiResponse } from 'next'; | ||||||
| 
 | 
 | ||||||
| const handler = async (req: NextApiRequest, res: NextApiResponse<any>) => { | const handler = async (req: NextApiRequest, res: NextApiResponse<any>) => { | ||||||
|   try { |   try { | ||||||
|     const { messages, key, model } = req.body as ChatBody; |     const { messages, key, model, googleAPIKey, googleCSEId } = | ||||||
|  |       req.body as GoogleBody; | ||||||
| 
 | 
 | ||||||
|     const userMessage = messages[messages.length - 1]; |     const userMessage = messages[messages.length - 1]; | ||||||
| 
 | 
 | ||||||
|     const googleRes = await fetch( |     const googleRes = await fetch( | ||||||
|       `https://customsearch.googleapis.com/customsearch/v1?key=${ |       `https://customsearch.googleapis.com/customsearch/v1?key=${ | ||||||
|         process.env.GOOGLE_API_KEY |         googleAPIKey ? googleAPIKey : process.env.GOOGLE_API_KEY | ||||||
|       }&cx=${process.env.GOOGLE_CSE_ID}&q=${userMessage.content.trim()}&num=5`,
 |       }&cx=${ | ||||||
|  |         googleCSEId ? googleCSEId : process.env.GOOGLE_CSE_ID | ||||||
|  |       }&q=${userMessage.content.trim()}&num=5`,
 | ||||||
|     ); |     ); | ||||||
| 
 | 
 | ||||||
|     const googleData = await googleRes.json(); |     const googleData = await googleRes.json(); | ||||||
|  | @ -33,7 +36,16 @@ const handler = async (req: NextApiRequest, res: NextApiResponse<any>) => { | ||||||
|     const sourcesWithText: any = await Promise.all( |     const sourcesWithText: any = await Promise.all( | ||||||
|       sources.map(async (source) => { |       sources.map(async (source) => { | ||||||
|         try { |         try { | ||||||
|           const res = await fetch(source.link); |           const timeoutPromise = new Promise((_, reject) => | ||||||
|  |             setTimeout(() => reject(new Error('Request timed out')), 5000), | ||||||
|  |           ); | ||||||
|  | 
 | ||||||
|  |           const res = (await Promise.race([ | ||||||
|  |             fetch(source.link), | ||||||
|  |             timeoutPromise, | ||||||
|  |           ])) as any; | ||||||
|  | 
 | ||||||
|  |           // if (res) {
 | ||||||
|           const html = await res.text(); |           const html = await res.text(); | ||||||
| 
 | 
 | ||||||
|           const virtualConsole = new jsdom.VirtualConsole(); |           const virtualConsole = new jsdom.VirtualConsole(); | ||||||
|  | @ -56,9 +68,11 @@ const handler = async (req: NextApiRequest, res: NextApiResponse<any>) => { | ||||||
|               text: sourceText.slice(0, 2000), |               text: sourceText.slice(0, 2000), | ||||||
|             } as GoogleSource; |             } as GoogleSource; | ||||||
|           } |           } | ||||||
|  |           // }
 | ||||||
| 
 | 
 | ||||||
|           return null; |           return null; | ||||||
|         } catch (error) { |         } catch (error) { | ||||||
|  |           console.error(error); | ||||||
|           return null; |           return null; | ||||||
|         } |         } | ||||||
|       }), |       }), | ||||||
|  |  | ||||||
|  | @ -118,6 +118,21 @@ const Home: React.FC<HomeProps> = ({ | ||||||
|       }; |       }; | ||||||
| 
 | 
 | ||||||
|       const endpoint = getEndpoint(plugin); |       const endpoint = getEndpoint(plugin); | ||||||
|  |       let body; | ||||||
|  | 
 | ||||||
|  |       if (!plugin) { | ||||||
|  |         body = JSON.stringify(chatBody); | ||||||
|  |       } else { | ||||||
|  |         body = JSON.stringify({ | ||||||
|  |           ...chatBody, | ||||||
|  |           googleAPIKey: pluginKeys | ||||||
|  |             .find((key) => key.pluginId === 'google-search') | ||||||
|  |             ?.requiredKeys.find((key) => key.key === 'GOOGLE_API_KEY')?.value, | ||||||
|  |           googleCSEId: pluginKeys | ||||||
|  |             .find((key) => key.pluginId === 'google-search') | ||||||
|  |             ?.requiredKeys.find((key) => key.key === 'GOOGLE_CSE_ID')?.value, | ||||||
|  |         }); | ||||||
|  |       } | ||||||
| 
 | 
 | ||||||
|       const controller = new AbortController(); |       const controller = new AbortController(); | ||||||
|       const response = await fetch(endpoint, { |       const response = await fetch(endpoint, { | ||||||
|  | @ -126,7 +141,7 @@ const Home: React.FC<HomeProps> = ({ | ||||||
|           'Content-Type': 'application/json', |           'Content-Type': 'application/json', | ||||||
|         }, |         }, | ||||||
|         signal: controller.signal, |         signal: controller.signal, | ||||||
|         body: JSON.stringify(chatBody), |         body, | ||||||
|       }); |       }); | ||||||
| 
 | 
 | ||||||
|       if (!response.ok) { |       if (!response.ok) { | ||||||
|  |  | ||||||
|  | @ -1,4 +1,9 @@ | ||||||
| import { Message } from './chat'; | import { ChatBody, Message } from './chat'; | ||||||
|  | 
 | ||||||
|  | export interface GoogleBody extends ChatBody { | ||||||
|  |   googleAPIKey: string; | ||||||
|  |   googleCSEId: string; | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
| export interface GoogleResponse { | export interface GoogleResponse { | ||||||
|   message: Message; |   message: Message; | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue