matt HOFFNER commited on
Commit
45dbf34
β€’
1 Parent(s): f16de12
Files changed (1) hide show
  1. pages/api/functions/index.ts +14 -8
pages/api/functions/index.ts CHANGED
@@ -103,23 +103,29 @@ const serpEmbedApi = async ({ input }: any) => {
103
  const queryResult = await vectorStore.similaritySearch(input, VECTOR_STORE_SIZE);
104
  return queryResult;
105
  }
 
 
 
 
106
 
107
  export default async function handler(req: NextApiRequest, res: NextApiResponse) {
108
  const args = req.body.args as string;
109
  const functionName = req.body.name as string;
110
  const functionInput = JSON.parse(args);
111
 
 
 
 
 
 
 
 
112
  try {
113
- if (functionName === 'searchApi') {
114
- const result = await serpEmbedApi(functionInput);
115
- return res.status(200).send(result);
116
- } else {
117
- const result = await surferEmbedApi(functionInput)
118
- return res.status(200).send(result);
119
- }
120
  } catch (error) {
121
  console.error(error);
122
  // @ts-ignore
123
  return res.status(500).json({ error: error.message });
124
  }
125
- }
 
103
  const queryResult = await vectorStore.similaritySearch(input, VECTOR_STORE_SIZE);
104
  return queryResult;
105
  }
106
+ const handlers: any = {
107
+ searchApi: serpEmbedApi,
108
+ surferEmbedApi: surferEmbedApi
109
+ };
110
 
111
  export default async function handler(req: NextApiRequest, res: NextApiResponse) {
112
  const args = req.body.args as string;
113
  const functionName = req.body.name as string;
114
  const functionInput = JSON.parse(args);
115
 
116
+ const functionHandler = handlers[functionName];
117
+
118
+ if (!functionHandler) {
119
+ console.error(`Function "${functionName}" is not supported.`);
120
+ return res.status(500).json({ error: `Function "${functionName}" is not supported.` });
121
+ }
122
+
123
  try {
124
+ const result = await functionHandler(functionInput);
125
+ return res.status(200).send(result);
 
 
 
 
 
126
  } catch (error) {
127
  console.error(error);
128
  // @ts-ignore
129
  return res.status(500).json({ error: error.message });
130
  }
131
+ }