From 71dbd3f9b50446fa8cab5a037516e95a5f8e17ba Mon Sep 17 00:00:00 2001 From: Tao_ta <745103130@qq.com> Date: Thu, 19 Sep 2024 18:08:48 +0800 Subject: [PATCH] feat: fix ai suggest task, make it more exact. --- spx-backend/internal/controller/llm.go | 2 +- spx-backend/internal/controller/llm_test.go | 2 +- .../editor/code-editor/coordinators/index.ts | 80 ++++++++++++------- 3 files changed, 54 insertions(+), 30 deletions(-) diff --git a/spx-backend/internal/controller/llm.go b/spx-backend/internal/controller/llm.go index ddce41bbf..d15cd1e9b 100644 --- a/spx-backend/internal/controller/llm.go +++ b/spx-backend/internal/controller/llm.go @@ -80,7 +80,7 @@ const ( explainTemplate = "Explain the code of user input. Your task is only about explain, you have to remember your task is explain. The user input will be two part as a json { UserInput, UserEntireProject }, the userInput will be a code snippet of where user want to get explain about, the UserEntireProject is the entire project helps you to understand the project. " commentTemplate = "Comment the code of user input. Your task is only about comment code, you have to remember your task is comment code. The user input will be two part as a json { UserInput, UserEntireProject }, the userInput will be a code snippet of where user want you to comment it, the UserEntireProject is the entire project helps you to understand the project. " fixCodeTemplate = "Fix the code if there is some problem. Your task is only about fix code if there is some problem, you have to remember your task is comment code. The user input will be two part as a json { UserInput, UserEntireProject }, the userInput will be a code snippet of where user want you to fix, the UserEntireProject is the entire project helps you to understand the project. " - suggestTaskTemplate = "Generate the suggest of code to complete the code, those suggest will follow behind user's cursor, users code will be inside of the delimiter. Your task is only about generate code suggest to complete the code, you have to remember your task is generate code suggest. This is the user's cursor: line: %d, column: %d. " + suggestTaskTemplate = "Generate the suggest of code to complete the code, those suggest will follow behind user's cursor. So the insert text will follow the user's cursor. Your task is only about generate code suggest to complete the code, you have to remember your task is generate code suggest. This is the user's cursor: line: %d, column: %d. " userInputTemplate = "Please answer me with this question. " // template about response responseTemplate = "You have to use the following template to generate the response, do keep remember this template will be your response. Your response will be a json object with following keys: {resp_message, []resp_questions}. Here is the explain about the response json object: response_message is the response message for the user you have to reply as markdown. response_suggests is the suggest question for the user to continue chat, you don't need to number the question's index, each question will only be one sentence for user to click. Which means the next input will be one of those questions. " diff --git a/spx-backend/internal/controller/llm_test.go b/spx-backend/internal/controller/llm_test.go index 66b94dfc8..fea32eefd 100644 --- a/spx-backend/internal/controller/llm_test.go +++ b/spx-backend/internal/controller/llm_test.go @@ -193,7 +193,7 @@ func TestLLM(t *testing.T) { t.Run("CheckTaskPrompt", func(t *testing.T) { params := createFullTaskParams() sysPrompt, userPrompt := params.taskPromptGenerator() - assert.Equal(t, "Generate the suggest of code to complete the code, those suggest will follow behind user's cursor, users code will be inside of the delimiter. Your task is only about generate code suggest to complete the code, you have to remember your task is generate code suggest. This is the user's cursor: line: 1, column: 10. You have to use the following template to generate the response, do keep remember this template will be your response. Your response will be a json object with following keys: {suggestions: []{label, insert_text}}. The number of list length is 3. Here is the explain about the response json object: label will shows inside of user's complete menu to tell people some info about the insert code, label will be only one word in most of time. insert_text is the code you have to insert into the user's code after user's cursor. You don't need to explain the json object. JUST JSON", sysPrompt, "System prompt generate failed.") + assert.Equal(t, "Generate the suggest of code to complete the code, those suggest will follow behind user's cursor. So the insert text will follow the user's cursor. Your task is only about generate code suggest to complete the code, you have to remember your task is generate code suggest. This is the user's cursor: line: 1, column: 10. You have to use the following template to generate the response, do keep remember this template will be your response. Your response will be a json object with following keys: {suggestions: []{label, insert_text}}. The number of list length is 3. Here is the explain about the response json object: label will shows inside of user's complete menu to tell people some info about the insert code, label will be only one word in most of time. insert_text is the code you have to insert into the user's code after user's cursor. You don't need to explain the json object. JUST JSON", sysPrompt, "System prompt generate failed.") assert.Equal(t, "Project: ProjectName:spxProject\nProjectVariables:Variable Type: Sprites, Name: Sprites1\nProjectCode:Code Type: Sprites, Name: Sprites1, Src: onStart => { \n \n }\n, Code arround user's cursor: onStart => { \n \n }", userPrompt, "User prompt generate failed.") }) diff --git a/spx-gui/src/components/editor/code-editor/coordinators/index.ts b/spx-gui/src/components/editor/code-editor/coordinators/index.ts index 63dc34609..2b77e3c8c 100644 --- a/spx-gui/src/components/editor/code-editor/coordinators/index.ts +++ b/spx-gui/src/components/editor/code-editor/coordinators/index.ts @@ -31,7 +31,8 @@ import { import { debounce } from '@/utils/utils' import { HoverProvider } from '@/components/editor/code-editor/coordinators/hoverProvider' import type { - TokenCategory, TokenId, + TokenCategory, + TokenId, UsageWithDoc } from '@/components/editor/code-editor/tokens/types' import { getAllTokens } from '@/components/editor/code-editor/tokens' @@ -189,33 +190,56 @@ export class Coordinator { ) }) - // this.suggest - // .startSuggestTask({ - // code: model.getValue(), - // position: { - // line: ctx.position.lineNumber, - // column: ctx.position.column - // } - // }) - // .then((items) => { - // addItems( - // items.map((item) => { - // return { - // icon: Icon.AIAbility, - // insertText: ctx.unitWord + item.insertText, - // label: ctx.unitWord + item.label, - // desc: ctx.unitWord + item.label, - // preview: { - // type: 'doc', - // layer: { - // level: DocPreviewLevel.Normal, - // content: '' - // } - // } - // } - // }) - // ) - // }) + function getContextualCode( + code: string, + position: { line: number; column: number }, + numLines: number + ) { + const lines = code.split('\n') + + const startLine = Math.max(0, position.line - numLines) + const endLine = Math.min(lines.length - 1, position.line + numLines) + + const contextualLines = lines.slice(startLine, endLine + 1) + + return contextualLines.join('\n') + } + + const code = model.getValue() + const position = { + line: ctx.position.lineNumber, + column: ctx.position.column + } + const numLines = 3 + const contextualCode = getContextualCode(code, position, numLines) + + this.suggest + .startSuggestTask({ + code: contextualCode, + position: { + line: position.line, + column: position.column + } + }) + .then((items) => { + addItems( + items.map((item) => { + return { + icon: Icon.AIAbility, + insertText: ctx.unitWord + item.insertText, + label: ctx.unitWord + item.label, + desc: ctx.unitWord + item.label, + preview: { + type: 'doc', + layer: { + level: DocPreviewLevel.Normal, + content: '' + } + } + } + }) + ) + }) } async implementsSelectionMenuProvider(