Skip to content

Commit

Permalink
feat: add take strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 18, 2023
1 parent c9db1ee commit bc0169d
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
3 changes: 2 additions & 1 deletion unit-cli/src/main/kotlin/cc/unitmesh/runner/Picker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class PickerCommand : CliktCommand() {
val pickerOption = PickerOption(
url = code.repository,
branch = code.branch,
language = code.language
language = code.language,
maxCompletionInOneFile = 1
)

val content = SimpleCodePicker(pickerOption).execute()
Expand Down
26 changes: 26 additions & 0 deletions unit-picker/src/main/kotlin/cc/unitmesh/pick/prompt/Instruction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,30 @@ data class Instruction(
override fun toString(): String {
return Json.encodeToString(serializer(), this)
}

companion object {
/**
* Takes a list of instructions and applies a strategy to determine which instructions to return.
*
* @param instructions the list of instructions to be processed
* @return a new list of instructions based on the applied strategy
*/
fun takeStrategy(instructions: List<Instruction>, maxCompletionInOneFile: Int): List<Instruction> {
// if size is less than maxCompletionInOneFile, return all
if (instructions.size <= maxCompletionInOneFile) {
return instructions
}

// if maxCompletionInOneFile == 1, return the first one
if (maxCompletionInOneFile == 1) {
return listOf(instructions.first())
}

// if more than maxCompletionInOneFile, return the first maxCompletionInOneFile, and random the rest
val first = instructions.take(maxCompletionInOneFile)
val rest = instructions.drop(maxCompletionInOneFile)
val randomRest = rest.shuffled().take(maxCompletionInOneFile - 1)
return first + randomRest
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package cc.unitmesh.pick.prompt;

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

class InstructionTest {

@Test
fun `should_return_all_instructions_when_size_is_less_than_maxCompletionInOneFile`() {
// given
val instructions = listOf(
Instruction("instruction1", "input1", "output1"),
Instruction("instruction2", "input2", "output2"),
Instruction("instruction3", "input3", "output3")
)
val maxCompletionInOneFile = 5

// when
val result = Instruction.takeStrategy(instructions, maxCompletionInOneFile)

// then
assertEquals(instructions, result)
}

@Test
fun `should_return_first_instruction_when_maxCompletionInOneFile_is_1`() {
// given
val instructions = listOf(
Instruction("instruction1", "input1", "output1"),
Instruction("instruction2", "input2", "output2"),
Instruction("instruction3", "input3", "output3")
)
val maxCompletionInOneFile = 1
val expected = listOf(instructions[0])

// when
val result = Instruction.takeStrategy(instructions, maxCompletionInOneFile)

// then
assertEquals(expected, result)
}

@Test
fun `should_return_first_maxCompletionInOneFile_instructions_and_random_rest_when_size_is_greater_than_maxCompletionInOneFile`() {
// given
val instructions = listOf(
Instruction("instruction1", "input1", "output1"),
Instruction("instruction2", "input2", "output2"),
Instruction("instruction3", "input3", "output3"),
Instruction("instruction4", "input4", "output4"),
Instruction("instruction5", "input5", "output5")
)
val maxCompletionInOneFile = 3

// when
val result = Instruction.takeStrategy(instructions, maxCompletionInOneFile)

// then
assertEquals(maxCompletionInOneFile, 3)
assertEquals(instructions[0], result[0])
assertEquals(instructions[1], result[1])
assertEquals(instructions[2], result[2])
// Ensure that the remaining instructions are random
for (i in maxCompletionInOneFile until instructions.size) {
assert(instructions.contains(result[i]))
}
}
}

0 comments on commit bc0169d

Please sign in to comment.