-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathtest_extension_filters.py
188 lines (157 loc) · 5.61 KB
/
test_extension_filters.py
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
176
177
178
179
180
181
182
183
184
185
186
187
188
from openai.types.responses import ResponseOutputMessage, ResponseOutputText
from agents import Agent, HandoffInputData
from agents.extensions.handoff_filters import remove_all_tools
from agents.items import (
HandoffOutputItem,
MessageOutputItem,
ToolCallOutputItem,
TResponseInputItem,
)
def fake_agent():
return Agent(
name="fake_agent",
)
def _get_message_input_item(content: str) -> TResponseInputItem:
return {
"role": "assistant",
"content": content,
}
def _get_function_result_input_item(content: str) -> TResponseInputItem:
return {
"call_id": "1",
"output": content,
"type": "function_call_output",
}
def _get_message_output_run_item(content: str) -> MessageOutputItem:
return MessageOutputItem(
agent=fake_agent(),
raw_item=ResponseOutputMessage(
id="1",
content=[ResponseOutputText(text=content, annotations=[], type="output_text")],
role="assistant",
status="completed",
type="message",
),
)
def _get_tool_output_run_item(content: str) -> ToolCallOutputItem:
return ToolCallOutputItem(
agent=fake_agent(),
raw_item={
"call_id": "1",
"output": content,
"type": "function_call_output",
},
output=content,
)
def _get_handoff_input_item(content: str) -> TResponseInputItem:
return {
"call_id": "1",
"output": content,
"type": "function_call_output",
}
def _get_handoff_output_run_item(content: str) -> HandoffOutputItem:
return HandoffOutputItem(
agent=fake_agent(),
raw_item={
"call_id": "1",
"output": content,
"type": "function_call_output",
},
source_agent=fake_agent(),
target_agent=fake_agent(),
)
def test_empty_data():
handoff_input_data = HandoffInputData(input_history=(), pre_handoff_items=(), new_items=())
filtered_data = remove_all_tools(handoff_input_data)
assert filtered_data == handoff_input_data
def test_str_historyonly():
handoff_input_data = HandoffInputData(input_history="Hello", pre_handoff_items=(), new_items=())
filtered_data = remove_all_tools(handoff_input_data)
assert filtered_data == handoff_input_data
def test_str_history_and_list():
handoff_input_data = HandoffInputData(
input_history="Hello",
pre_handoff_items=(),
new_items=(_get_message_output_run_item("Hello"),),
)
filtered_data = remove_all_tools(handoff_input_data)
assert filtered_data == handoff_input_data
def test_list_history_and_list():
handoff_input_data = HandoffInputData(
input_history=(_get_message_input_item("Hello"),),
pre_handoff_items=(_get_message_output_run_item("123"),),
new_items=(_get_message_output_run_item("World"),),
)
filtered_data = remove_all_tools(handoff_input_data)
assert filtered_data == handoff_input_data
def test_removes_tools_from_history():
handoff_input_data = HandoffInputData(
input_history=(
_get_message_input_item("Hello1"),
_get_function_result_input_item("World"),
_get_message_input_item("Hello2"),
),
pre_handoff_items=(
_get_tool_output_run_item("abc"),
_get_message_output_run_item("123"),
),
new_items=(_get_message_output_run_item("World"),),
)
filtered_data = remove_all_tools(handoff_input_data)
assert len(filtered_data.input_history) == 2
assert len(filtered_data.pre_handoff_items) == 1
assert len(filtered_data.new_items) == 1
def test_removes_tools_from_new_items():
handoff_input_data = HandoffInputData(
input_history=(),
pre_handoff_items=(),
new_items=(
_get_message_output_run_item("Hello"),
_get_tool_output_run_item("World"),
),
)
filtered_data = remove_all_tools(handoff_input_data)
assert len(filtered_data.input_history) == 0
assert len(filtered_data.pre_handoff_items) == 0
assert len(filtered_data.new_items) == 1
def test_removes_tools_from_new_items_and_history():
handoff_input_data = HandoffInputData(
input_history=(
_get_message_input_item("Hello1"),
_get_function_result_input_item("World"),
_get_message_input_item("Hello2"),
),
pre_handoff_items=(
_get_message_output_run_item("123"),
_get_tool_output_run_item("456"),
),
new_items=(
_get_message_output_run_item("Hello"),
_get_tool_output_run_item("World"),
),
)
filtered_data = remove_all_tools(handoff_input_data)
assert len(filtered_data.input_history) == 2
assert len(filtered_data.pre_handoff_items) == 1
assert len(filtered_data.new_items) == 1
def test_removes_handoffs_from_history():
handoff_input_data = HandoffInputData(
input_history=(
_get_message_input_item("Hello1"),
_get_handoff_input_item("World"),
),
pre_handoff_items=(
_get_message_output_run_item("Hello"),
_get_tool_output_run_item("World"),
_get_handoff_output_run_item("World"),
),
new_items=(
_get_message_output_run_item("Hello"),
_get_tool_output_run_item("World"),
_get_handoff_output_run_item("World"),
),
)
filtered_data = remove_all_tools(handoff_input_data)
assert len(filtered_data.input_history) == 1
assert len(filtered_data.pre_handoff_items) == 1
assert len(filtered_data.new_items) == 1