-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
172 additions
and
137 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
unit-picker/src/main/kotlin/cc/unitmesh/pick/prompt/Instruction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
142 changes: 5 additions & 137 deletions
142
unit-picker/src/main/kotlin/cc/unitmesh/pick/prompt/InstructionBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
|
51 changes: 51 additions & 0 deletions
51
unit-picker/src/main/kotlin/cc/unitmesh/pick/prompt/InstructionType.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...picker/src/main/kotlin/cc/unitmesh/pick/prompt/builder/AfterBlockCodeCompletionBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
) | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
unit-picker/src/main/kotlin/cc/unitmesh/pick/prompt/builder/InBlockCodeCompletionBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
) | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
unit-picker/src/main/kotlin/cc/unitmesh/pick/prompt/builder/InlineCodeCompletionBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
) | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
unit-picker/src/main/kotlin/cc/unitmesh/pick/prompt/builder/RelatedCodeCompletionBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
) | ||
} | ||
|
||
|
||
} |
1 change: 1 addition & 0 deletions
1
unit-picker/src/test/kotlin/cc/unitmesh/pick/prompt/InstructionBuilderTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters