diff --git a/src/lib/apis/index.ts b/src/lib/apis/index.ts index a33610c31..ea20f5eda 100644 --- a/src/lib/apis/index.ts +++ b/src/lib/apis/index.ts @@ -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]; } };