From 61480c0dc95aea8c139772eb3a2a94bf2dc6671c Mon Sep 17 00:00:00 2001 From: mic Date: Sun, 15 Jun 2025 00:22:10 +0200 Subject: [PATCH] Enhance tag extraction functionality in getTagsFromResponse method with improved JSON parsing and backward compatibility. see #365 --- js/mzta-utils-prompt.js | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/js/mzta-utils-prompt.js b/js/mzta-utils-prompt.js index fc71c36e..b0c0a96c 100644 --- a/js/mzta-utils-prompt.js +++ b/js/mzta-utils-prompt.js @@ -79,7 +79,36 @@ export const taPromptUtils = { return chatgpt_lang; }, + /** + * Extracts tags from the response text. + * @param {string} response_text - The response text from which to extract tags. + * @returns {Array} An array of tags extracted from the response text. + * + * The response text should be a JSON with this structure: + * { + * "tags": ["tag1", "tag2", ...] + * } + * + * For backwords compatibility, if the response text is not a valid JSON, + * it will try to split the text by commas and return the resulting array. + */ getTagsFromResponse(response_text){ - + let tags = []; + if(response_text && response_text.length > 0){ + try { + // Try to parse the response text as JSON + let response_json = JSON.parse(response_text); + if(response_json && Array.isArray(response_json.tags)){ + tags = response_json.tags; + } else if(response_json && response_json.tags && typeof response_json.tags === 'string'){ + // If tags is a string, split it by commas + tags = response_json.tags.split(',').map(tag => tag.trim()); + } + } catch (e) { + // If parsing fails, fallback to splitting by commas + tags = response_text.split(',').map(tag => tag.trim()); + } + } + return tags; } }; \ No newline at end of file