Skip to content

Commit

Permalink
refactor: try to update codes
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Apr 16, 2023
1 parent 7a84358 commit 4fad27e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 16 deletions.
16 changes: 8 additions & 8 deletions src/main/kotlin/cc/unitmesh/devti/DevtiFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,41 +21,41 @@ class DevtiFlow(
// 1. check story detail is valid, if not, fill story detail
var storyDetail = story.description
if (!kanban.isValidStory(storyDetail)) {
logger.info("story detail is not valid, fill story detail")
logger.warn("story detail is not valid, fill story detail")

storyDetail = flowAction.fillStoryDetail(project, story.description)

logger.info("fill story detail: $storyDetail")
val newStory = SimpleStory(story.id, story.title, storyDetail)
kanban.updateStoryDetail(newStory)
}
logger.warn("user story detail: $storyDetail")

// 2. get suggest endpoint
val files: List<DtClass> = analyser?.controllerList() ?: emptyList()
logger.info("start devti flow")
logger.warn("start devti flow")
val targetEndpoint = flowAction.analysisEndpoint(storyDetail, files)
// use regex match *Controller from targetEndpoint
val controller = getController(targetEndpoint)
if (controller == null) {
logger.info("no controller found from: $targetEndpoint")
logger.warn("no controller found from: $targetEndpoint")
return
}

logger.info("target endpoint: $targetEndpoint")
logger.warn("target endpoint: $targetEndpoint")
val targetController = files.find { it.name == targetEndpoint }
if (targetController == null) {
logger.info("no controller found from: $targetEndpoint")
logger.warn("no controller found from: $targetEndpoint")
return
}

// 3. update endpoint method
val code = flowAction.needUpdateMethodForController(targetEndpoint, targetController)
logger.warn("update method code: $code")
analyser?.updateMethod(targetController.name, code)
logger.info("update method code: $code")
}

private fun getController(targetEndpoint: String): String? {
val regex = Regex("""\s+(\w+Controller)""")
val regex = Regex("""(\w+Controller)""")
val matchResult = regex.find(targetEndpoint)
return matchResult?.groupValues?.get(1)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class GptPromptText() {
val promptText: InputStream = this::class.java.classLoader.getResourceAsStream("prompts/update_controller_method.txt")!!
val promptTextString = promptText.bufferedReader().use { it.readText() }
return promptTextString
.replace("{controllerName}", targetClazz.name)
.replace("{controllers}", targetClazz.format())
.replace("{targetEndpoint}", targetEndpoint)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import cc.unitmesh.devti.analysis.DtClass
import cc.unitmesh.devti.kanban.SimpleProjectInfo
import cc.unitmesh.devti.prompt.AiAction
import cc.unitmesh.devti.prompt.DevtiFlowAction
import cc.unitmesh.devti.runconfig.DtRunState

Check warning on line 7 in src/main/kotlin/cc/unitmesh/devti/prompt/openai/OpenAIAction.kt

View workflow job for this annotation

GitHub Actions / Build

Unused import directive

Unused import directive
import com.aallam.openai.api.BetaOpenAI
import com.aallam.openai.api.chat.ChatCompletion
import com.aallam.openai.api.chat.ChatCompletionRequest
import com.aallam.openai.api.chat.ChatMessage
import com.aallam.openai.api.chat.ChatRole
import com.aallam.openai.api.model.ModelId
import com.aallam.openai.client.OpenAI
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.diagnostic.logger
import kotlinx.coroutines.runBlocking

class OpenAIAction(val openAIKey: String, val version: String) : AiAction, DevtiFlowAction {

Check notice on line 19 in src/main/kotlin/cc/unitmesh/devti/prompt/openai/OpenAIAction.kt

View workflow job for this annotation

GitHub Actions / Build

Class member can have 'private' visibility

Property 'openAIKey' could be private

Check warning on line 19 in src/main/kotlin/cc/unitmesh/devti/prompt/openai/OpenAIAction.kt

View workflow job for this annotation

GitHub Actions / Build

Constructor parameter is never used as a property

Constructor parameter is never used as a property
Expand Down Expand Up @@ -51,9 +54,14 @@ class OpenAIAction(val openAIKey: String, val version: String) : AiAction, Devti

override fun needUpdateMethodForController(targetEndpoint: String, clazz: DtClass): String {
val promptText = gptPromptText.fillUpdateMethod(targetEndpoint, clazz)
logger.info("needUpdateMethodForController prompt text: $promptText")
return runBlocking {
val prompt = prompt(promptText)
return@runBlocking prompt
}
}

companion object {
private val logger: Logger = logger<OpenAIAction>()
}
}
7 changes: 3 additions & 4 deletions src/main/kotlin/cc/unitmesh/devti/runconfig/DtRunState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ class DtRunState(
log.warn(createStory.toString())
log.warn(options.toString())

if (createStory == null) {
return null
}
// todo: check create story
val storyId = createStory?.storyId ?: 1

devtiFlow.start(createStory.storyId.toString())
devtiFlow.start(storyId.toString())

return null
}
Expand Down
7 changes: 3 additions & 4 deletions src/main/resources/prompts/update_controller_method.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
请根据下面的用户故事以及 BlogController 的相关信息,编写代码。要求如下:
请根据下面的用户故事以及 {controllerName} 的相关信息,修改代码。要求如下:

1. 使用 Java + Spring 编写,只返回 Controller 对应方法的代码,不需要编写 Service 层的代码。
2. 只返回代码,不需要编写测试代码。
3. 不做解释,只返回代码。
1. 使用 Java + Spring 编写,只返回 Controller 对应方法的代码。
2. 不做解释,只返回修改完的函数代码。

```
{controllers}
Expand Down

0 comments on commit 4fad27e

Please sign in to comment.