From 4f70944a925abfdde4140a0fad01996eef4b2a95 Mon Sep 17 00:00:00 2001 From: "bobo.yang" Date: Mon, 21 Aug 2023 11:52:00 +0800 Subject: [PATCH] Refactor contextRefDefs.ts - Update the logic for parsing the response from the devchat prompt command. - Handle different response formats, including JSON, array, and plain text. - Extract symbols from the response and return them as an array. - Log the response for debugging purposes. --- src/context/contextRefDefs.ts | 73 ++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 27 deletions(-) diff --git a/src/context/contextRefDefs.ts b/src/context/contextRefDefs.ts index 1c4eb94..61e9ea5 100644 --- a/src/context/contextRefDefs.ts +++ b/src/context/contextRefDefs.ts @@ -24,37 +24,56 @@ async function getCurrentSelectText(): Promise { } async function getUndefinedSymbols(content: string): Promise { - // run devchat prompt command - const devChat = new DevChat(); - const chatOptions: ChatOptions = {}; + // run devchat prompt command + const devChat = new DevChat(); + const chatOptions: ChatOptions = {}; - const onData = (partialResponse) => { }; - const newContent = content + `\n只列出当前代码中缺少定义的符号列表,不需要其他处理。应答形式为markdown code block,例如: - \`\`\`json - ["f1", "f2"] - \`\`\` - 不能调用GPT function.`; + const onData = (partialResponse) => { }; + const newContent = content + `\n只列出当前代码中缺少定义的符号列表,不需要其他处理。应答形式为markdown code block,例如: + \`\`\`json + ["f1", "f2"] + \`\`\` + 不能调用GPT function.`; - const chatResponse = await devChat.chat(newContent, chatOptions, onData); - if (!chatResponse.isError) { - await devChat.delete(chatResponse['prompt-hash']); - } + const chatResponse = await devChat.chat(newContent, chatOptions, onData); + if (chatResponse && chatResponse.response) { + logger.channel()?.info(chatResponse.response); + } - // parse data in chatResponse.response - // data format as: - // ```json {data} ``` - // so, parse data between ```json and ``` - if (!chatResponse || !chatResponse.response) { - return []; - } - if (!chatResponse.response.match(/```json([\s\S]*)```/)) { - return []; - } + if (!chatResponse.isError) { + await devChat.delete(chatResponse['prompt-hash']); + } - logger.channel()?.info(`getUndefinedSymbols: ${chatResponse.response}`); - const responseText = chatResponse.response.match(/```json([\s\S]*)```/)![1]; - // parse responseText to json - return JSON.parse(responseText); + // parse data in chatResponse.response + // data format as: + // ```json {data} ``` + // or [data] + // or plain text + // so, parse data between ```json and ``` or directly parse the array, or return an empty array + if (!chatResponse || !chatResponse.response) { + return []; + } + + let responseText = chatResponse.response; + let symbols: string[]; + + if (responseText.startsWith("```json") && responseText.endsWith("```")) { + responseText = responseText.substring(7, responseText.length - 3); + try { + symbols = JSON.parse(responseText); + } catch (error) { + symbols = []; + } + } else { + try { + symbols = JSON.parse(responseText); + } catch (error) { + symbols = []; + } + } + + logger.channel()?.info(`getUndefinedSymbols: ${chatResponse.response}`); + return symbols; } async function getSymbolDefine(symbolList: string[]): Promise {