Skip to content

Commit

Permalink
refactor: extract API for template instruction #2
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Jan 7, 2024
1 parent 6ba7123 commit 6a7d163
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@ import cc.unitmesh.core.Instruction

interface TypedIns {
val type: InstructionBuilderType
fun unique(): Instruction

/**
* Build final instruction.
*/
fun toInstruction(): Instruction
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ data class ClassCommentIns(
) : TypedCommentIns() {
override val builderLevel: CommentBuilderType = CommentBuilderType.CLASS_LEVEL

override fun unique(): Instruction {
override fun toInstruction(): Instruction {
val instruction = "Write ${docInstruction.value} for given class " + dataStructure.NodeName + " .\n"
val input = "Code:\n```$language\n" + dataStructure.Content + "\n```"
val output = comment.content
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ data class MethodCommentIns(
) : TypedCommentIns() {
override val builderLevel: CommentBuilderType = CommentBuilderType.METHOD_LEVEL

override fun unique(): Instruction {
override fun toInstruction(): Instruction {
val instruction = "Write ${docInstruction.value} for given method " + function.Name + " .\n"
val input =
"### Current class:\n${currentDataStruct.toUml()}\n###\nCode:\n```$language\n${currentDataStruct.Content}\n```\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ data class BasicTestIns(
return Json.encodeToString(serializer(), this)
}

override fun unique(): Instruction {
override fun toInstruction(): Instruction {
val input = StringBuilder()

input.append("\n###")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ data class RelatedCodeIns(
return Json.encodeToString(serializer(), this)
}

override fun unique(): Instruction {
override fun toInstruction(): Instruction {
// Related code strategy
val relatedCode = if (relatedCode.isNotEmpty()) {
// todo: count be similar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ data class SimilarChunkIns(
return Json.encodeToString(serializer(), this)
}

override fun unique(): Instruction {
override fun toInstruction(): Instruction {
// Similar chunk strategy
val similarChunks = if (similarChunks.isNotBlank() && similarChunks.isNotEmpty()) {
// limit similarChunks to 30 lines
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cc.unitmesh.pick.worker
import cc.unitmesh.core.Instruction
import cc.unitmesh.core.SupportedLang
import cc.unitmesh.core.completion.InstructionBuilderType
import cc.unitmesh.core.completion.TypedIns
import cc.unitmesh.pick.threshold.ThresholdChecker
import cc.unitmesh.pick.worker.base.LangWorker
import cc.unitmesh.pick.worker.job.InstructionFileJob
Expand Down Expand Up @@ -105,14 +106,7 @@ class WorkerManager(private val context: WorkerContext) {
* @return a list of instructions after executing all workers and applying the threshold filter
*/
suspend fun runAll(): List<Instruction> {
val results = workers.map { (_, worker) ->
try {
worker.start()
} catch (e: Exception) {
e.printStackTrace()
emptyList()
}
}.flatten()
val results = startWorkers()

// take context.completionTypeSize for each type
val finalList: EnumMap<InstructionBuilderType, List<Instruction>> =
Expand All @@ -121,7 +115,8 @@ class WorkerManager(private val context: WorkerContext) {
var skipCount = 0
// filter output by threshold
results.mapNotNull {
val ins = it.unique()
/// Todo: add support for different template
val ins = it.toInstruction()
if (thresholdChecker.isMetThreshold(ins)) {
finalList[it.type] = finalList[it.type]?.plus(ins) ?: listOf(ins)
} else {
Expand All @@ -139,4 +134,26 @@ class WorkerManager(private val context: WorkerContext) {
finalList[it]?.shuffled()?.take(context.completionTypeSize) ?: emptyList()
}.flatten()
}

/**
* Suspends the execution and starts all the workers in the Kotlin language.
*
* This method starts all the workers defined in the `workers` map. Each worker is started by invoking its `start()` method.
* If any exception occurs during the start process, the exception is caught and an empty list is returned for that worker.
*
* @return a list of `TypedIns` objects representing the results of starting the workers. The list contains all the successful
* results from starting the workers, and any exceptions encountered during the start process are logged and ignored.
* If all workers fail to start, an empty list is returned.
*/
suspend fun startWorkers(): List<TypedIns> {
val results = workers.map { (_, worker) ->
try {
worker.start()
} catch (e: Exception) {
e.printStackTrace()
emptyList()
}
}.flatten()
return results
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cc.unitmesh.pick.builder.comment

import cc.unitmesh.core.SupportedLang
import cc.unitmesh.quality.documentation.CodeComment
import chapi.ast.kotlinast.KotlinAnalyser
import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -70,7 +69,7 @@ class Group<T>(val name: String) {

// Then
result.size shouldBe 3
result[0].unique().output shouldBe """
result[0].toInstruction().output shouldBe """
/**
* A group of *members*.
*
Expand All @@ -82,7 +81,7 @@ class Group<T>(val name: String) {
*/
""".trimIndent()

result[1].unique().output shouldBe """
result[1].toInstruction().output shouldBe """
/**
* Adds a [member] to this group.
* @return the new size of the group.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public class HelloController {

result.size shouldBe 3

result.first().unique().input shouldBe """
result.first().toInstruction().input shouldBe """
// Similar chunk:
```
// Compare this snippet from cc.unitmesh.testng.repository.BlogRepository
Expand Down

0 comments on commit 6a7d163

Please sign in to comment.