|
@@ -440,7 +440,34 @@ export const generateAutoCompletion = async (
|
|
|
}
|
|
|
|
|
|
const response = res?.choices[0]?.message?.content ?? '';
|
|
|
- return response;
|
|
|
+
|
|
|
+
|
|
|
+ try {
|
|
|
+ const jsonStartIndex = response.indexOf('{');
|
|
|
+ const jsonEndIndex = response.lastIndexOf('}');
|
|
|
+
|
|
|
+ if (jsonStartIndex !== -1 && jsonEndIndex !== -1) {
|
|
|
+ const jsonResponse = response.substring(jsonStartIndex, jsonEndIndex + 1);
|
|
|
+
|
|
|
+ // Step 5: Parse the JSON block
|
|
|
+ const parsed = JSON.parse(jsonResponse);
|
|
|
+
|
|
|
+ // Step 6: If there's a "queries" key, return the queries array; otherwise, return an empty array
|
|
|
+ if (parsed && parsed.text) {
|
|
|
+ return parsed.text;
|
|
|
+ } else {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // If no valid JSON block found, return response as is
|
|
|
+ return response;
|
|
|
+ } catch (e) {
|
|
|
+ // Catch and safely return empty array on any parsing errors
|
|
|
+ console.error('Failed to parse response: ', e);
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
};
|
|
|
|
|
|
|