fix: legacy query generation support

This commit is contained in:
Timothy Jaeryang Baek 2024-11-26 13:44:19 -08:00
parent 63402c48a8
commit 13796fe3b3

View File

@ -367,15 +367,15 @@ export const generateQueries = async (
throw error;
}
try {
// Step 1: Safely extract the response string
const response = res?.choices[0]?.message?.content ?? '';
// Step 3: Find the relevant JSON block within the response
// Step 1: Safely extract the response string
const response = res?.choices[0]?.message?.content ?? '';
try {
const jsonStartIndex = response.indexOf('{');
const jsonEndIndex = response.lastIndexOf('}');
// Step 4: Check if we found a valid JSON block (with both `{` and `}`)
if (jsonStartIndex !== -1 && jsonEndIndex !== -1) {
const jsonResponse = response.substring(jsonStartIndex, jsonEndIndex + 1);
@ -391,11 +391,11 @@ export const generateQueries = async (
}
// If no valid JSON block found, return an empty array
return [];
return [response];
} catch (e) {
// Catch and safely return empty array on any parsing errors
console.error('Failed to parse response: ', e);
return [];
return [response];
}
};