Skip to content

Commit 25b860a

Browse files
authored
[prompt] Remove additional content builder from MessageContentBuilder, introduce TextContentBuilderBase (#331)
1 parent b9c5a55 commit 25b860a

File tree

20 files changed

+254
-285
lines changed

20 files changed

+254
-285
lines changed

agents/agents-core/src/commonMain/kotlin/ai/koog/agents/core/agent/entity/AIAgentSubgraph.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public open class AIAgentSubgraph<Input, Output>(
122122
replaceHistoryWithTLDR()
123123
updatePrompt {
124124
user {
125-
content { selectRelevantTools(tools, toolSelectionStrategy.subtaskDescription) }
125+
selectRelevantTools(tools, toolSelectionStrategy.subtaskDescription)
126126
}
127127
}
128128

agents/agents-core/src/commonMain/kotlin/ai/koog/agents/core/agent/session/AIAgentLLMWriteSession.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ public class AIAgentLLMWriteSession internal constructor(
445445
if (definition != null) {
446446
val prompt = prompt(prompt, clock) {
447447
user {
448-
content { definition.definition(this) }
448+
definition.definition(this)
449449
}
450450
}
451451
this.prompt = prompt

agents/agents-core/src/commonMain/kotlin/ai/koog/agents/core/dsl/extension/HistoryCompressionStrategies.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public abstract class HistoryCompressionStrategy {
4444
dropTrailingToolCalls()
4545
updatePrompt {
4646
user {
47-
content { summarizeInTLDR() }
47+
summarizeInTLDR()
4848
}
4949
}
5050
listOf(llmSession.requestLLMWithoutTools())

agents/agents-core/src/commonMain/kotlin/ai/koog/agents/core/prompt/Prompts.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package ai.koog.agents.core.prompt
22

33
import ai.koog.agents.core.tools.ToolDescriptor
44
import ai.koog.prompt.markdown.markdown
5-
import ai.koog.prompt.text.TextContentBuilder
5+
import ai.koog.prompt.text.TextContentBuilderBase
66

77
internal object Prompts {
8-
fun TextContentBuilder.selectRelevantTools(tools: List<ToolDescriptor>, subtaskDescription: String) =
8+
fun TextContentBuilderBase<*>.selectRelevantTools(tools: List<ToolDescriptor>, subtaskDescription: String) =
99
markdown {
1010
+"You will be now concentrating on solving the following task:"
1111
br()
@@ -31,7 +31,7 @@ internal object Prompts {
3131
+"Think carefully about the tools you select, and make sure they are relevant to the task."
3232
}
3333

34-
fun TextContentBuilder.summarizeInTLDR() =
34+
fun TextContentBuilderBase<*>.summarizeInTLDR() =
3535
markdown {
3636
+"Create a comprehensive summary of this conversation."
3737
br()

examples/src/main/kotlin/ai/koog/agents/example/attachments/InstagramPostDescriber.kt

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,18 @@ fun main() {
2525
system("You are professional assistant that can write cool and funny descriptions for Instagram posts.")
2626

2727
user {
28-
content {
29-
markdown {
30-
+"I want to create a new post on Instagram."
31-
br()
32-
+"Can you write something creative under my instagram post with the following photos?"
33-
br()
34-
h2("Requirements")
35-
bulleted {
36-
item("It must be very funny and creative")
37-
item("It must increase my chance of becoming an ultra-famous blogger!!!!")
38-
item("It not contain explicit content, harassment or bullying")
39-
item("It must be a short catching phrase")
40-
item("You must include relevant hashtags that would increase the visibility of my post")
41-
}
28+
markdown {
29+
+"I want to create a new post on Instagram."
30+
br()
31+
+"Can you write something creative under my instagram post with the following photos?"
32+
br()
33+
h2("Requirements")
34+
bulleted {
35+
item("It must be very funny and creative")
36+
item("It must increase my chance of becoming an ultra-famous blogger!!!!")
37+
item("It not contain explicit content, harassment or bullying")
38+
item("It must be a short catching phrase")
39+
item("You must include relevant hashtags that would increase the visibility of my post")
4240
}
4341
}
4442

integration-tests/src/jvmTest/kotlin/ai/koog/integration/tests/MultipleLLMPromptExecutorIntegrationTest.kt

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -683,12 +683,11 @@ class MultipleLLMPromptExecutorIntegrationTest {
683683
system("You are a helpful assistant that can analyze markdown files.")
684684

685685
user {
686-
content {
687-
markdown {
688-
"I'm sending you a markdown file with different markdown elements. "
689-
+"Please list all the markdown elements used in it and describe its structure clearly."
690-
}
686+
markdown {
687+
"I'm sending you a markdown file with different markdown elements. "
688+
+"Please list all the markdown elements used in it and describe its structure clearly."
691689
}
690+
692691
attachments {
693692
file(file.absolutePath, "text/markdown")
694693
}
@@ -699,13 +698,11 @@ class MultipleLLMPromptExecutorIntegrationTest {
699698
system("You are a helpful assistant that can analyze markdown files.")
700699

701700
user {
702-
content {
703-
markdown {
704-
"I'm sending you a markdown file with different markdown elements. "
705-
+"Please list all the markdown elements used in it and describe its structure clearly."
706-
newline()
707-
+file.readText()
708-
}
701+
markdown {
702+
"I'm sending you a markdown file with different markdown elements. "
703+
+"Please list all the markdown elements used in it and describe its structure clearly."
704+
newline()
705+
+file.readText()
709706
}
710707
}
711708
}
@@ -754,10 +751,8 @@ class MultipleLLMPromptExecutorIntegrationTest {
754751
system("You are a helpful assistant that can analyze images.")
755752

756753
user {
757-
content {
758-
markdown {
759-
+"I'm sending you an image. Please describe what you see in it and identify the image format if possible."
760-
}
754+
markdown {
755+
+"I'm sending you an image. Please describe what you see in it and identify the image format if possible."
761756
}
762757

763758
attachments {
@@ -837,13 +832,10 @@ class MultipleLLMPromptExecutorIntegrationTest {
837832
system("You are a helpful assistant that can analyze and process text.")
838833

839834
user {
840-
content {
841-
markdown {
842-
"I'm sending you a text file. Please analyze it and summarize its content."
843-
}
835+
markdown {
836+
"I'm sending you a text file. Please analyze it and summarize its content."
844837
}
845838

846-
847839
attachments {
848840
textFile(Path(file.absolutePath), "text/plain")
849841
}
@@ -854,12 +846,10 @@ class MultipleLLMPromptExecutorIntegrationTest {
854846
system("You are a helpful assistant that can analyze and process text.")
855847

856848
user {
857-
content {
858-
markdown {
859-
+"I'm sending you a text file. Please analyze it and summarize its content."
860-
newline()
861-
+file.readText()
862-
}
849+
markdown {
850+
+"I'm sending you a text file. Please analyze it and summarize its content."
851+
newline()
852+
+file.readText()
863853
}
864854
}
865855
}
@@ -926,10 +916,8 @@ class MultipleLLMPromptExecutorIntegrationTest {
926916
system("You are a helpful assistant that can analyze audio files.")
927917

928918
user {
929-
content {
930-
markdown {
931-
"I'm sending you an audio file. Please tell me a couple of words about it."
932-
}
919+
markdown {
920+
"I'm sending you an audio file. Please tell me a couple of words about it."
933921
}
934922

935923
attachments {
@@ -993,11 +981,10 @@ class MultipleLLMPromptExecutorIntegrationTest {
993981
system("You are a helpful assistant that can analyze different types of media files.")
994982

995983
user {
996-
content {
997-
markdown {
998-
+"I'm sending you an image. Please analyze it and tell me about its content."
999-
}
984+
markdown {
985+
+"I'm sending you an image. Please analyze it and tell me about its content."
1000986
}
987+
1001988
attachments {
1002989
image(Path(tempImageFile.absolutePath))
1003990
}
@@ -1033,10 +1020,8 @@ class MultipleLLMPromptExecutorIntegrationTest {
10331020
system("You are a helpful assistant that can analyze images.")
10341021

10351022
user {
1036-
content {
1037-
markdown {
1038-
+"I'm sending you an image from a URL. Please analyze it and tell me about its content."
1039-
}
1023+
markdown {
1024+
+"I'm sending you an image from a URL. Please analyze it and tell me about its content."
10401025
}
10411026

10421027
attachments {

integration-tests/src/jvmTest/kotlin/ai/koog/integration/tests/SingleLLMPromptExecutorIntegrationTest.kt

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -631,12 +631,11 @@ class SingleLLMPromptExecutorIntegrationTest {
631631
system("You are a helpful assistant that can analyze markdown files.")
632632

633633
user {
634-
content {
635-
markdown {
636-
"I'm sending you a markdown file with different markdown elements. "
637-
+"Please list all the markdown elements used in it and describe its structure clearly."
638-
}
634+
markdown {
635+
"I'm sending you a markdown file with different markdown elements. "
636+
+"Please list all the markdown elements used in it and describe its structure clearly."
639637
}
638+
640639
attachments {
641640
file(file.absolutePath, "text/markdown")
642641
}
@@ -647,13 +646,11 @@ class SingleLLMPromptExecutorIntegrationTest {
647646
system("You are a helpful assistant that can analyze markdown files.")
648647

649648
user {
650-
content {
651-
markdown {
652-
"I'm sending you a markdown file with different markdown elements. "
653-
+"Please list all the markdown elements used in it and describe its structure clearly."
654-
newline()
655-
+file.readText()
656-
}
649+
markdown {
650+
"I'm sending you a markdown file with different markdown elements. "
651+
+"Please list all the markdown elements used in it and describe its structure clearly."
652+
newline()
653+
+file.readText()
657654
}
658655
}
659656
}
@@ -705,10 +702,8 @@ class SingleLLMPromptExecutorIntegrationTest {
705702
system("You are a helpful assistant that can analyze images.")
706703

707704
user {
708-
content {
709-
markdown {
710-
+"I'm sending you an image. Please analyze it and identify the image format if possible."
711-
}
705+
markdown {
706+
+"I'm sending you an image. Please analyze it and identify the image format if possible."
712707
}
713708

714709
attachments {
@@ -791,10 +786,8 @@ class SingleLLMPromptExecutorIntegrationTest {
791786
system("You are a helpful assistant that can analyze and process text.")
792787

793788
user {
794-
content {
795-
markdown {
796-
"I'm sending you a text file. Please analyze it and summarize its content."
797-
}
789+
markdown {
790+
"I'm sending you a text file. Please analyze it and summarize its content."
798791
}
799792

800793

@@ -808,12 +801,10 @@ class SingleLLMPromptExecutorIntegrationTest {
808801
system("You are a helpful assistant that can analyze and process text.")
809802

810803
user {
811-
content {
812-
markdown {
813-
+"I'm sending you a text file. Please analyze it and summarize its content."
814-
newline()
815-
+file.readText()
816-
}
804+
markdown {
805+
+"I'm sending you a text file. Please analyze it and summarize its content."
806+
newline()
807+
+file.readText()
817808
}
818809
}
819810
}
@@ -883,10 +874,8 @@ class SingleLLMPromptExecutorIntegrationTest {
883874
system("You are a helpful assistant.")
884875

885876
user {
886-
content {
887-
markdown {
888-
"I'm sending you an audio file. Please tell me a couple of words about it."
889-
}
877+
markdown {
878+
"I'm sending you an audio file. Please tell me a couple of words about it."
890879
}
891880

892881
attachments {
@@ -952,10 +941,8 @@ class SingleLLMPromptExecutorIntegrationTest {
952941
system("You are a helpful assistant that can analyze different types of media files.")
953942

954943
user {
955-
content {
956-
markdown {
957-
+"I'm sending you an image. Please analyze them and tell me about their content."
958-
}
944+
markdown {
945+
+"I'm sending you an image. Please analyze them and tell me about their content."
959946
}
960947

961948
attachments {
@@ -997,10 +984,8 @@ class SingleLLMPromptExecutorIntegrationTest {
997984
system("You are a helpful assistant that can analyze images.")
998985

999986
user {
1000-
content {
1001-
markdown {
1002-
+"I'm sending you an image from a URL. Please analyze it and tell me about its content."
1003-
}
987+
markdown {
988+
+"I'm sending you an image from a URL. Please analyze it and tell me about its content."
1004989
}
1005990

1006991
attachments {

prompt/prompt-markdown/src/commonMain/kotlin/ai/koog/prompt/markdown/Markdown.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ai.koog.prompt.markdown
22

3+
import ai.koog.prompt.text.TextContentBuilderBase
34
import ai.koog.prompt.text.TextContentBuilder
45

56
/**
@@ -505,7 +506,7 @@ public inline fun StringBuilder.markdown(init: MarkdownContentBuilder.() -> Unit
505506
*
506507
* @param init The markdown content builder
507508
*/
508-
public inline fun TextContentBuilder.markdown(init: MarkdownContentBuilder.() -> Unit) {
509+
public inline fun TextContentBuilderBase<*>.markdown(init: MarkdownContentBuilder.() -> Unit) {
509510
text(MarkdownContentBuilder().apply(init).build())
510511
}
511512

prompt/prompt-model/src/commonMain/kotlin/ai/koog/prompt/dsl/MessageContentBuilder.kt

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ai.koog.prompt.dsl
22

33
import ai.koog.prompt.message.Attachment
4+
import ai.koog.prompt.text.TextContentBuilderBase
45
import ai.koog.prompt.text.TextContentBuilder
56

67
/**
@@ -10,17 +11,9 @@ import ai.koog.prompt.text.TextContentBuilder
1011
* @see AttachmentBuilder
1112
*/
1213
@PromptDSL
13-
public class MessageContentBuilder {
14-
private var content: String = ""
14+
public class MessageContentBuilder : TextContentBuilderBase<MessageContent>() {
1515
private var attachments: List<Attachment> = emptyList()
1616

17-
/**
18-
* Configures text content for this content builder.
19-
*/
20-
public fun content(body: TextContentBuilder.() -> Unit) {
21-
content = TextContentBuilder().apply(body).build()
22-
}
23-
2417
/**
2518
* Configures media attachments for this content builder.
2619
*/
@@ -31,7 +24,7 @@ public class MessageContentBuilder {
3124
/**
3225
* Builds and returns both the text content and attachments.
3326
*/
34-
public fun build(): MessageContent = MessageContent(content, attachments)
27+
override fun build(): MessageContent = MessageContent(textBuilder.toString(), attachments)
3528
}
3629

3730
/**

prompt/prompt-model/src/commonMain/kotlin/ai/koog/prompt/text/Text.kt

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)