forked from agentclientprotocol/python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_contrib_session_state.py
More file actions
175 lines (158 loc) · 5.22 KB
/
test_contrib_session_state.py
File metadata and controls
175 lines (158 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
from __future__ import annotations
import pytest
from acp import clear_agent_message
from acp.contrib.session_state import SessionAccumulator
from acp.schema import (
AgentMessageChunk,
AgentPlanUpdate,
AvailableCommandsUpdate,
ContentToolCallContent,
CurrentModeUpdate,
PlanEntry,
SessionNotification,
TextContentBlock,
ToolCallProgress,
ToolCallStart,
UserMessageChunk,
)
def notification(session_id: str, update):
return SessionNotification(session_id=session_id, update=update)
def test_session_accumulator_merges_tool_calls():
acc = SessionAccumulator()
start = ToolCallStart(
session_update="tool_call",
tool_call_id="call-1",
title="Read file",
status="in_progress",
)
acc.apply(notification("s", start))
progress = ToolCallProgress(
session_update="tool_call_update",
tool_call_id="call-1",
status="completed",
content=[
ContentToolCallContent(
type="content",
content=TextContentBlock(type="text", text="Done"),
)
],
)
snapshot = acc.apply(notification("s", progress))
tool = snapshot.tool_calls["call-1"]
assert tool.status == "completed"
assert tool.title == "Read file"
assert tool.content and tool.content[0].content.text == "Done"
def test_session_accumulator_records_plan_and_mode():
acc = SessionAccumulator()
acc.apply(
notification(
"s",
AgentPlanUpdate(
session_update="plan",
entries=[
PlanEntry(content="Step 1", priority="medium", status="pending"),
],
),
)
)
snapshot = acc.apply(
notification("s", CurrentModeUpdate(session_update="current_mode_update", current_mode_id="coding"))
)
assert snapshot.plan_entries[0].content == "Step 1"
assert snapshot.current_mode_id == "coding"
def test_session_accumulator_tracks_messages_and_commands():
acc = SessionAccumulator()
acc.apply(
notification(
"s",
AvailableCommandsUpdate(
session_update="available_commands_update",
available_commands=[],
),
)
)
acc.apply(
notification(
"s",
UserMessageChunk(
session_update="user_message_chunk",
content=TextContentBlock(type="text", text="Hello"),
),
)
)
acc.apply(
notification(
"s",
AgentMessageChunk(
session_update="agent_message_chunk",
content=TextContentBlock(type="text", text="Hi!"),
),
)
)
snapshot = acc.snapshot()
user_content = snapshot.user_messages[0].content
agent_content = snapshot.agent_messages[0].content
assert isinstance(user_content, TextContentBlock)
assert isinstance(agent_content, TextContentBlock)
assert user_content.text == "Hello"
assert agent_content.text == "Hi!"
def test_session_accumulator_agent_message_clear_clears_accumulated_chunks():
acc = SessionAccumulator()
acc.apply(notification("s", AgentMessageChunk(session_update="agent_message_chunk", content=TextContentBlock(type="text", text="Thinking..."))))
acc.apply(notification("s", AgentMessageChunk(session_update="agent_message_chunk", content=TextContentBlock(type="text", text=" draft"))))
assert len(acc.snapshot().agent_messages) == 2
acc.apply(notification("s", clear_agent_message()))
assert len(acc.snapshot().agent_messages) == 0
acc.apply(notification("s", AgentMessageChunk(session_update="agent_message_chunk", content=TextContentBlock(type="text", text="Final result."))))
snapshot = acc.snapshot()
assert len(snapshot.agent_messages) == 1
assert snapshot.agent_messages[0].content.text == "Final result."
def test_session_accumulator_auto_resets_on_new_session():
acc = SessionAccumulator()
acc.apply(
notification(
"s1",
ToolCallStart(
session_update="tool_call",
tool_call_id="call-1",
title="First",
),
)
)
acc.apply(
notification(
"s2",
ToolCallStart(
session_update="tool_call",
tool_call_id="call-2",
title="Second",
),
)
)
snapshot = acc.snapshot()
assert snapshot.session_id == "s2"
assert "call-1" not in snapshot.tool_calls
assert "call-2" in snapshot.tool_calls
def test_session_accumulator_rejects_cross_session_when_auto_reset_disabled():
acc = SessionAccumulator(auto_reset_on_session_change=False)
acc.apply(
notification(
"s1",
ToolCallStart(
session_update="tool_call",
tool_call_id="call-1",
title="First",
),
)
)
with pytest.raises(ValueError):
acc.apply(
notification(
"s2",
ToolCallStart(
session_update="tool_call",
tool_call_id="call-2",
title="Second",
),
)
)