Skip to content

Commit

Permalink
feat(writing): add prompt type for custom action prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Nov 2, 2023
1 parent 0c16166 commit 0bae8b0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
Expand Up @@ -5,15 +5,21 @@ import cc.unitmesh.template.TemplateRoleSplitter
import com.intellij.openapi.diagnostic.logger
import org.yaml.snakeyaml.Yaml

data class TeamActionPrompt(
enum class CustomActionType {
Default,
QuickAction,
}

data class CustomActionPrompt(
var interaction: InteractionType = InteractionType.AppendCursorStream,
var priority: Int = 0,
var type: CustomActionType = CustomActionType.Default,
var other: Map<String, Any> = mapOf(),
// the rest of the content is the chat messages
var msgs: List<LlmMsg.ChatMessage> = listOf(),
) {
companion object {
val logger = logger<TeamActionPrompt>()
val logger = logger<CustomActionPrompt>()

/**
* Parses the given content and returns a TeamActionPrompt object.
Expand Down Expand Up @@ -61,11 +67,11 @@ data class TeamActionPrompt(
* )
* ```
*/
fun fromContent(content: String): TeamActionPrompt {
fun fromContent(content: String): CustomActionPrompt {
val regex = """^---\s*\n(.*?)\n---\s*\n(.*)$""".toRegex(RegexOption.DOT_MATCHES_ALL)
val matchResult = regex.find(content)

val prompt = TeamActionPrompt()
val prompt = CustomActionPrompt()
if (matchResult != null) {
val frontMatter = matchResult.groupValues[1]
val yaml = Yaml()
Expand All @@ -82,7 +88,15 @@ data class TeamActionPrompt(
0
}

prompt.other = frontMatterMap.filterKeys { it != "interaction" && it != "priority" }
prompt.type = try {
CustomActionType.valueOf(frontMatterMap["type"] as String)
} catch (e: Exception) {
CustomActionType.Default
}

prompt.other = frontMatterMap.filterKeys {
it != "interaction" && it != "priority" && it != "type"
}

val chatContent = matchResult?.groupValues?.get(2) ?: content
prompt.msgs = parseChatMessage(chatContent)
Expand Down
Expand Up @@ -18,7 +18,7 @@ class TeamPromptsBuilder(private val project: Project) {
val promptName = it.nameWithoutExtension.replace("-", " ")
// load content of the prompt file
val promptContent = runReadAction { it.inputStream.readBytes().toString(Charsets.UTF_8) }
val actionPrompt = TeamActionPrompt.fromContent(promptContent)
val actionPrompt = CustomActionPrompt.fromContent(promptContent)

TeamPromptAction(promptName, actionPrompt)
}
Expand All @@ -27,5 +27,5 @@ class TeamPromptsBuilder(private val project: Project) {

data class TeamPromptAction(
val actionName: String,
val actionPrompt: TeamActionPrompt = TeamActionPrompt(),
val actionPrompt: CustomActionPrompt = CustomActionPrompt(),
)
Expand Up @@ -5,7 +5,7 @@ import io.kotest.matchers.shouldBe
import junit.framework.TestCase.assertEquals
import org.junit.Test

class TeamActionPromptTest {
class CustomActionPromptTest {

@Test
fun `should create TeamActionPrompt object from content with frontmatter`() {
Expand All @@ -24,7 +24,7 @@ class TeamActionPromptTest {
""".trimIndent()

// when
val prompt = TeamActionPrompt.fromContent(content)
val prompt = CustomActionPrompt.fromContent(content)

// then
assertEquals(InteractionType.AppendCursorStream, prompt.interaction)
Expand All @@ -47,7 +47,7 @@ class TeamActionPromptTest {
""".trimIndent()

// when
val prompt = TeamActionPrompt.fromContent(content)
val prompt = CustomActionPrompt.fromContent(content)

// then
assertEquals(InteractionType.AppendCursorStream, prompt.interaction)
Expand All @@ -58,4 +58,29 @@ class TeamActionPromptTest {
LlmMsg.ChatMessage(LlmMsg.ChatRole.User, "Chat message 2\n", null),
)
}

@Test
fun `should_handle_for_notion_like_summarize`() {
val content = """
---
type: QuickAction
name: Summarize
category: Generate
interaction: AppendCursorStream
---
```System```
You are an assistant helping summarize a document. Use this format, replacing text in brackets with the result. Do not include the brackets in the output:
Summary in [Identified language of the document]:
[One-paragaph summary of the document using the identified language.].
```User```
""".trimIndent()

val prompt = CustomActionPrompt.fromContent(content)
prompt.type shouldBe CustomActionType.QuickAction
}
}

0 comments on commit 0bae8b0

Please sign in to comment.