Skip to content

Commit 87c4ba0

Browse files
adam-malek-JBOloloshechkin
authored andcommitted
Mark prompt builders with PromptDSL (#200)
Currently, it is possible to invoke the role-defining functions while already being inside them, for instance: ``` user { system { } } ``` which, despite being valid code, is confusing. We mark the TextContentBuilder with the PromptDSL annotation so that such calls, if intended, will need to explicitly specify the receiver.
1 parent 368b74a commit 87c4ba0

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ public class PromptBuilder internal constructor(
189189
*
190190
* This class provides methods for adding tool calls and tool results.
191191
*/
192+
@PromptDSL
192193
public inner class ToolMessageBuilder(public val clock: Clock) {
193194
/**
194195
* Adds a tool call message to the prompt.
@@ -199,7 +200,7 @@ public class PromptBuilder internal constructor(
199200
*/
200201
@Deprecated("Use call(id, tool, content) instead", ReplaceWith("call(id, tool, content)"))
201202
public fun call(call: Message.Tool.Call) {
202-
messages.add(call)
203+
this@PromptBuilder.messages.add(call)
203204
}
204205

205206
/**
@@ -225,10 +226,10 @@ public class PromptBuilder internal constructor(
225226
*/
226227
@Deprecated("Use result(id, tool, content) instead", ReplaceWith("result(id, tool, content)"))
227228
public fun result(result: Message.Tool.Result) {
228-
messages
229+
this@PromptBuilder.messages
229230
.indexOfLast { it is Message.Tool.Call && it.id == result.id }
230231
.takeIf { it != -1 }
231-
?.let { index -> messages.add(index + 1, result) }
232+
?.let { index -> this@PromptBuilder.messages.add(index + 1, result) }
232233
?: throw IllegalStateException("Failed to add tool result: no call message with id ${result.id}")
233234
}
234235

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package ai.koog.prompt.text
22

3+
import ai.koog.prompt.dsl.PromptDSL
4+
35
/**
46
* A utility class for building and manipulating textual content.
57
*
68
* Provides methods for constructing formatted strings with features such as inserting text,
79
* adding new lines, and applying padding. The builder pattern supports a fluent and convenient
810
* approach to managing text content.
911
*/
12+
@PromptDSL
1013
public open class TextContentBuilder {
1114
/**
1215
* Represents the position of a caret within a text document.

0 commit comments

Comments
 (0)