Skip to content

Commit

Permalink
refactor: align for builderS
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 7, 2023
1 parent 2d6046d commit 9a542e9
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 137 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package cc.unitmesh.pick.prompt

data class Instruction(
val instruction: String,
val input: String,
val output: String,
)
Original file line number Diff line number Diff line change
@@ -1,145 +1,13 @@
package cc.unitmesh.pick.prompt

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
import cc.unitmesh.pick.picker.InstructionJob

@Serializable(InstructionTypeSerializer::class)
enum class InstructionType(val contentClass: KClass<out InstructionBuilder>) {
INLINE_COMPLETION(InlineCodeCompletionBuilder::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()
}

interface InstructionBuilder {
fun build(): InstructionData
}

data class InstructionData(
val instruction: String,
val input: String,
val output: String,
data class InstructionContext(
val job: InstructionJob
)

@Serializable
data class InlineCodeCompletionBuilder(
val instruction: String,
val output: String,
val language: String,
val beforeCursorCode: String,
) : InstructionBuilder {
override fun build(): InstructionData {
return InstructionData(
instruction,
output = output,
input = """```$language
|$beforeCursorCode
|```""".trimMargin()
)
}
}

@Serializable
data class InBlockCodeCompletion(
val instruction: String,
val output: String,
val language: String,
val beforeCursorCode: String,
val afterCursorCode: String,
) : InstructionBuilder {
override fun build(): InstructionData {
return InstructionData(
instruction,
output = output,
input = """```$language
|$beforeCursorCode
|$afterCursorCode
|```""".trimMargin()
)
}
}

class AfterBlockCodeCompletion(
val instruction: String,
val output: String,
val language: String,
val beforeCursorCode: String,
val afterCursorCode: String,
) : InstructionBuilder {
override fun build(): InstructionData {
return InstructionData(
instruction,
output = output,
input = """```$language
|$beforeCursorCode
|$afterCursorCode
|```""".trimMargin()
)
}
}


@Serializable
data class RelatedCodeCompletion(
val instruction: String,
val output: String,
val language: String,
val beforeCursorCode: String,
val relatedCode: String,
) : InstructionBuilder {
override fun build(): InstructionData {
return InstructionData(
instruction,
output = output,
input = """
| Compare this snippet:
|```$language
|$relatedCode
|```
|Code:
|```$language
|$beforeCursorCode
|```""${'"'}.trimMargin()
""".trimIndent()
)
}


}

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)
}
interface InstructionBuilder {
fun build(context: InstructionContext): Instruction
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package cc.unitmesh.pick.prompt

import cc.unitmesh.pick.prompt.builder.AfterBlockCodeCompletionBuilder
import cc.unitmesh.pick.prompt.builder.InBlockCodeCompletionBuilder
import cc.unitmesh.pick.prompt.builder.InlineCodeCompletionBuilder
import cc.unitmesh.pick.prompt.builder.RelatedCodeCompletionBuilder
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 InstructionBuilder>) {
INLINE_COMPLETION(InlineCodeCompletionBuilder::class),
IN_BLOCK_COMPLETION(InBlockCodeCompletionBuilder::class),
AFTER_BLOCK_COMPLETION(AfterBlockCodeCompletionBuilder::class),
/**
* the AutoDev with pre-build context
*/
RELATED_CODE_COMPLETION(RelatedCodeCompletionBuilder::class);

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

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
@@ -0,0 +1,24 @@
package cc.unitmesh.pick.prompt.builder

import cc.unitmesh.pick.prompt.Instruction
import cc.unitmesh.pick.prompt.InstructionBuilder
import cc.unitmesh.pick.prompt.InstructionContext

class AfterBlockCodeCompletionBuilder(
val instruction: String,
val output: String,
val language: String,
val beforeCursorCode: String,
val afterCursorCode: String,
) : InstructionBuilder {
override fun build(context: InstructionContext): Instruction {
return Instruction(
instruction,
output = output,
input = """```$language
|$beforeCursorCode
|$afterCursorCode
|```""".trimMargin()
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cc.unitmesh.pick.prompt.builder

import cc.unitmesh.pick.prompt.Instruction
import cc.unitmesh.pick.prompt.InstructionBuilder
import cc.unitmesh.pick.prompt.InstructionContext
import kotlinx.serialization.Serializable

@Serializable
class InBlockCodeCompletionBuilder(
val instruction: String,
val output: String,
val language: String,
val beforeCursorCode: String,
val afterCursorCode: String,
) : InstructionBuilder {
override fun build(context: InstructionContext): Instruction {
return Instruction(
instruction,
output = output,
input = """```$language
|$beforeCursorCode
|$afterCursorCode
|```""".trimMargin()
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cc.unitmesh.pick.prompt.builder

import cc.unitmesh.pick.prompt.Instruction
import cc.unitmesh.pick.prompt.InstructionBuilder
import cc.unitmesh.pick.prompt.InstructionContext
import kotlinx.serialization.Serializable

@Serializable
class InlineCodeCompletionBuilder(
val instruction: String,
val output: String,
val language: String,
val beforeCursorCode: String,
) : InstructionBuilder {
override fun build(context: InstructionContext): Instruction {
return Instruction(
instruction,
output = output,
input = """```$language
|$beforeCursorCode
|```""".trimMargin()
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cc.unitmesh.pick.prompt.builder

import cc.unitmesh.pick.prompt.Instruction
import cc.unitmesh.pick.prompt.InstructionBuilder
import cc.unitmesh.pick.prompt.InstructionContext
import kotlinx.serialization.Serializable

@Serializable
data class RelatedCodeCompletionBuilder(
val instruction: String,
val output: String,
val language: String,
val beforeCursorCode: String,
val relatedCode: String,
) : InstructionBuilder {
override fun build(context: InstructionContext): Instruction {
return Instruction(
instruction,
output = output,
input = """
| Compare this snippet:
|```$language
|$relatedCode
|```
|Code:
|```$language
|$beforeCursorCode
|```""${'"'}.trimMargin()
""".trimIndent()
)
}


}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cc.unitmesh.pick.prompt;


import cc.unitmesh.pick.prompt.builder.InlineCodeCompletionBuilder
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import org.junit.jupiter.api.Test
Expand Down

0 comments on commit 9a542e9

Please sign in to comment.