Skip to content

Commit

Permalink
feat: add instruction testing
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 6, 2023
1 parent 5a5840f commit 9b97f73
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 30 deletions.
96 changes: 68 additions & 28 deletions unit-picker/src/main/kotlin/cc/unitmesh/pick/output/Instruction.kt
Original file line number Diff line number Diff line change
@@ -1,42 +1,57 @@
package cc.unitmesh.pick.output

enum class InstructionType {
INLINE_CODE_COMPLETION,
IN_BLOCK_CODE_COMPLETION,
AFTER_BLOCK_CODE_COMPLETION,
RELATED_CODE_COMPLETION,
// CODE_DIFF,
// REFACTOR,
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.SerializationException
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlin.reflect.KClass

@Serializable(InstructionTypeSerializer::class)
enum class InstructionType(val contentClass: KClass<out Instruction>) {
INLINE_COMPLETION(InlineCodeCompletion::class),
IN_BLOCK_COMPLETION(InBlockCodeCompletion::class),
AFTER_BLOCK_COMPLETION(AfterBlockCodeCompletion::class),
/**
* the AutoDev with pre-build context
*/
RELATED_CODE_COMPLETION(RelatedCodeCompletion::class)
;

val type: String get() = name.lowercase()
}

sealed class Instruction(
val instructionType: InstructionType,
val instruction: String,
val output: String,
) {
abstract fun input(): String
sealed interface Instruction {
val output: String
val instruction: String
fun input(): String
}

class InlineCodeCompletion(
instruction: String,
output: String,
@Serializable
data class InlineCodeCompletion(
override val instruction: String,
override val output: String,
val language: String,
val beforeCursorCode: String,
) : Instruction(InstructionType.INLINE_CODE_COMPLETION, instruction, output) {
) : Instruction {
override fun input(): String {
return """```$language
|$beforeCursorCode
|```""".trimMargin()
}
}

class InBlockCodeCompletion(
instruction: String,
output: String,
@Serializable
data class InBlockCodeCompletion(
override val instruction: String,
override val output: String,
val language: String,
val beforeCursorCode: String,
val afterCursorCode: String,
) : Instruction(InstructionType.IN_BLOCK_CODE_COMPLETION, instruction, output) {
) : Instruction {
override fun input(): String {
return """```$language
|$beforeCursorCode
Expand All @@ -46,12 +61,12 @@ class InBlockCodeCompletion(
}

class AfterBlockCodeCompletion(
instruction: String,
output: String,
override val instruction: String,
override val output: String,
val language: String,
val beforeCursorCode: String,
val afterCursorCode: String,
) : Instruction(InstructionType.AFTER_BLOCK_CODE_COMPLETION, instruction, output) {
) : Instruction {
override fun input(): String {
return """```$language
|$beforeCursorCode
Expand All @@ -60,13 +75,14 @@ class AfterBlockCodeCompletion(
}
}

class RelatedCodeCompletion(
instruction: String,
output: String,
@Serializable
data class RelatedCodeCompletion(
override val instruction: String,
override val output: String,
val language: String,
val beforeCursorCode: String,
val relatedCode: String,
) : Instruction(InstructionType.RELATED_CODE_COMPLETION, instruction, output) {
) : Instruction {
override fun input(): String {
return """
| Compare this snippet:
Expand All @@ -80,3 +96,27 @@ class RelatedCodeCompletion(
}
}

object InstructionTypeSerializer : KSerializer<InstructionType> {
private val cache: MutableMap<String, InstructionType> = hashMapOf()

private fun getMessageType(type: String): InstructionType {
return cache.computeIfAbsent(type) { newType ->
InstructionType.values().firstOrNull { it.type == newType }
?: throw SerializationException("Unknown message type: $newType")
}
}

override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor(
InstructionType::class.qualifiedName!!, PrimitiveKind.STRING
)

override fun deserialize(decoder: Decoder): InstructionType {
return getMessageType(decoder.decodeString())
}

override fun serialize(encoder: Encoder, value: InstructionType) {
encoder.encodeString(value.type)
}
}


Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cc.unitmesh.pick.worker

import cc.unitmesh.pick.output.Instruction
import cc.unitmesh.pick.picker.PickJob
import chapi.ast.antlr.JavaParser
import chapi.ast.javaast.JavaAnalyser
Expand Down Expand Up @@ -41,7 +42,7 @@ class JavaLangWorker : LangWorker() {
job.container = JavaAnalyser().analysis(code, job.location)
}

override suspend fun start(): Unit = coroutineScope {
override suspend fun start(): List<Instruction> = coroutineScope {
// 1. read directory to a collection of files for FileJob
jobs.map {
println(it.container)
Expand All @@ -50,6 +51,7 @@ class JavaLangWorker : LangWorker() {
// 2. check package information from line 1?

// 3. build full project trees
return@coroutineScope listOf()
}

// check by history?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package cc.unitmesh.pick.worker

import cc.unitmesh.pick.output.Instruction
import cc.unitmesh.pick.picker.PickJob

abstract class LangWorker {
abstract fun addJob(job: PickJob)
abstract suspend fun start()
abstract suspend fun start() : List<Instruction>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cc.unitmesh.pick.output;


import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import org.junit.jupiter.api.Test

class InstructionTest {
@Test
fun shouldConvertInstructionToJson() {
val instruction = InlineCodeCompletion(
"Inline Code Completion",
"Input for inline code completion",
"java",
"import java.util.*;\n" +
"\n" +
"public class Main {\n" +
" public static void main(String[] args) {\n"
)

val output = Json.encodeToString(instruction)
println(output)
}
}

0 comments on commit 9b97f73

Please sign in to comment.