Skip to content

Commit c6af3a2

Browse files
committed
add agent_message_clear
impls agentclientprotocol/agent-client-protocol#465
1 parent 11ecde2 commit c6af3a2

3 files changed

Lines changed: 52 additions & 0 deletions

File tree

acp-model/src/commonMain/kotlin/com/agentclientprotocol/model/SessionUpdate.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,18 @@ public sealed class SessionUpdate {
111111
val content: ContentBlock
112112
) : SessionUpdate()
113113

114+
/**
115+
* Clears the accumulated streamed content for the current agent message.
116+
*
117+
* Instructs the client to clear the content built from previous [AgentMessageChunk] updates.
118+
* Subsequent [AgentMessageChunk] updates will append from empty again.
119+
*
120+
* See: [agent_message_clear RFD](https://github.com/agentclientprotocol/agent-client-protocol/pull/465)
121+
*/
122+
@Serializable
123+
@SerialName("agent_message_clear")
124+
public data class AgentMessageClear() : SessionUpdate()
125+
114126
/**
115127
* Notification that a new tool call has been initiated.
116128
*/
@@ -276,6 +288,13 @@ internal object SessionUpdateSerializer : KSerializer<SessionUpdate> {
276288
base.forEach { (k, v) -> put(k, v) }
277289
}
278290
}
291+
is SessionUpdate.AgentMessageClear -> {
292+
val base = ACPJson.encodeToJsonElement(SessionUpdate.AgentMessageClear.serializer(), value).jsonObject
293+
buildJsonObject {
294+
put(SESSION_UPDATE_DISCRIMINATOR, "agent_message_clear")
295+
base.forEach { (k, v) -> put(k, v) }
296+
}
297+
}
279298
is SessionUpdate.ToolCall -> {
280299
val base = ACPJson.encodeToJsonElement(SessionUpdate.ToolCall.serializer(), value).jsonObject
281300
buildJsonObject {
@@ -364,6 +383,10 @@ internal object SessionUpdateSerializer : KSerializer<SessionUpdate> {
364383
SessionUpdate.AgentThoughtChunk.serializer(),
365384
jsonObject
366385
)
386+
"agent_message_clear" -> ACPJson.decodeFromJsonElement(
387+
SessionUpdate.AgentMessageClear.serializer(),
388+
jsonObject
389+
)
367390
"tool_call" -> ACPJson.decodeFromJsonElement(
368391
SessionUpdate.ToolCall.serializer(),
369392
jsonObject

acp-model/src/commonTest/kotlin/com/agentclientprotocol/model/SessionUpdateSerializerTest.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,19 @@ class SessionUpdateSerializerTest {
115115

116116
// Round-trip serialization tests
117117

118+
@Test
119+
fun `decodes agent_message_clear correctly`() {
120+
val payload = """
121+
{
122+
"sessionUpdate": "agent_message_clear"
123+
}
124+
""".trimIndent()
125+
126+
val update = ACPJson.decodeFromString(SessionUpdate.serializer(), payload)
127+
128+
assertTrue(update is SessionUpdate.AgentMessageClear)
129+
}
130+
118131
@Test
119132
fun `round-trip serialization for known types`() {
120133
val original = SessionUpdate.AgentMessageChunk(
@@ -129,6 +142,17 @@ class SessionUpdateSerializerTest {
129142
assertEquals("Test message", (decoded.content as ContentBlock.Text).text)
130143
}
131144

145+
@Test
146+
fun `round-trip serialization for AgentMessageClear`() {
147+
val original = SessionUpdate.AgentMessageClear()
148+
149+
val encoded = ACPJson.encodeToString(SessionUpdate.serializer(), original)
150+
assertTrue(encoded.contains("\"sessionUpdate\":\"agent_message_clear\""))
151+
152+
val decoded = ACPJson.decodeFromString(SessionUpdate.serializer(), encoded)
153+
assertTrue(decoded is SessionUpdate.AgentMessageClear)
154+
}
155+
132156
@Test
133157
fun `round-trip serialization for UsageUpdate`() {
134158
val original = SessionUpdate.UsageUpdate(
@@ -257,6 +281,7 @@ class SessionUpdateSerializerTest {
257281
"""{"sessionUpdate": "user_message_chunk", "content": {"type": "text", "text": "test"}}""",
258282
"""{"sessionUpdate": "agent_message_chunk", "content": {"type": "text", "text": "test"}}""",
259283
"""{"sessionUpdate": "agent_thought_chunk", "content": {"type": "text", "text": "test"}}""",
284+
"""{"sessionUpdate": "agent_message_clear"}""",
260285
"""{"sessionUpdate": "tool_call", "toolCallId": "t1", "title": "test"}""",
261286
"""{"sessionUpdate": "tool_call_update", "toolCallId": "t1"}""",
262287
"""{"sessionUpdate": "plan", "entries": []}""",

samples/kotlin-acp-client-sample/src/main/kotlin/com/agentclientprotocol/samples/util.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ fun SessionUpdate.render() {
1313
println("Agent thinks: ${this.content.render()}")
1414
}
1515

16+
is SessionUpdate.AgentMessageClear -> {
17+
println("Agent message cleared")
18+
}
19+
1620
is SessionUpdate.AvailableCommandsUpdate -> {
1721
println("Available commands updated:")
1822
}

0 commit comments

Comments
 (0)