3131from ravendb .documents .operations .connection_string .put_connection_string_operation import PutConnectionStringOperation
3232from ravendb .tests .test_base import TestBase
3333
34-
3534CONNECTION_STRING_NAME = "conv-mock-cs"
3635AGENT_ID = "conv-mock-agent"
3736
@@ -56,9 +55,7 @@ def _action_result(action_name, tool_id, arguments, conversation_id="conversatio
5655 change_vector = change_vector ,
5756 response = None ,
5857 usage = _make_usage (),
59- action_requests = [
60- AiAgentActionRequest (name = action_name , tool_id = tool_id , arguments = json .dumps (arguments ))
61- ],
58+ action_requests = [AiAgentActionRequest (name = action_name , tool_id = tool_id , arguments = json .dumps (arguments ))],
6259 )
6360
6461
@@ -126,27 +123,33 @@ def _send(operation):
126123 return _send
127124
128125 def test_basic_conversation_returns_answer (self ):
129- with patch .object (self .store .maintenance , "send" ,
130- side_effect = self ._patched_send (lambda : _done_result (response = {"answer" : "Hello!" }))):
126+ with patch .object (
127+ self .store .maintenance ,
128+ "send" ,
129+ side_effect = self ._patched_send (lambda : _done_result (response = {"answer" : "Hello!" })),
130+ ):
131131 chat = self .store .ai .conversation (AGENT_ID , "conversations/" )
132132 chat .set_user_prompt ("Hi there" )
133133 result = chat .run ()
134134 self .assertEqual (AiConversationStatus .DONE , result .status )
135135 self .assertEqual ("Hello!" , result .answer ["answer" ])
136136
137137 def test_conversation_id_and_change_vector_are_stored (self ):
138- with patch .object (self .store .maintenance , "send" ,
139- side_effect = self ._patched_send (
140- lambda : _done_result (conversation_id = "conversations/99" , change_vector = "A:99" ))):
138+ with patch .object (
139+ self .store .maintenance ,
140+ "send" ,
141+ side_effect = self ._patched_send (
142+ lambda : _done_result (conversation_id = "conversations/99" , change_vector = "A:99" )
143+ ),
144+ ):
141145 chat = self .store .ai .conversation (AGENT_ID , "conversations/" )
142146 chat .set_user_prompt ("Hello" )
143147 chat .run ()
144148 self .assertEqual ("conversations/99" , chat ._conversation_id )
145149 self .assertEqual ("A:99" , chat ._change_vector )
146150
147151 def test_usage_is_populated (self ):
148- with patch .object (self .store .maintenance , "send" ,
149- side_effect = self ._patched_send (lambda : _done_result ())):
152+ with patch .object (self .store .maintenance , "send" , side_effect = self ._patched_send (lambda : _done_result ())):
150153 chat = self .store .ai .conversation (AGENT_ID , "conversations/" )
151154 chat .set_user_prompt ("Hello" )
152155 result = chat .run ()
@@ -156,16 +159,18 @@ def test_usage_is_populated(self):
156159 self .assertEqual (30 , result .usage .total_tokens )
157160
158161 def test_elapsed_is_populated (self ):
159- with patch .object (self .store .maintenance , "send" ,
160- side_effect = self ._patched_send (lambda : _done_result ())):
162+ with patch .object (self .store .maintenance , "send" , side_effect = self ._patched_send (lambda : _done_result ())):
161163 chat = self .store .ai .conversation (AGENT_ID , "conversations/" )
162164 chat .set_user_prompt ("Hello" )
163165 result = chat .run ()
164166 self .assertIsNotNone (result .elapsed )
165167
166168 def test_context_manager_usage (self ):
167- with patch .object (self .store .maintenance , "send" ,
168- side_effect = self ._patched_send (lambda : _done_result (response = {"answer" : "ctx" }))):
169+ with patch .object (
170+ self .store .maintenance ,
171+ "send" ,
172+ side_effect = self ._patched_send (lambda : _done_result (response = {"answer" : "ctx" })),
173+ ):
169174 with self .store .ai .conversation (AGENT_ID , "conversations/" ) as chat :
170175 chat .set_user_prompt ("Hello from context manager" )
171176 result = chat .run ()
@@ -178,18 +183,19 @@ def test_context_manager_usage(self):
178183
179184 def test_handle_invokes_handler_and_sends_response (self ):
180185 calls = []
181- responses = iter ([
182- _action_result ("store-result" , "tool-1" , {"result" : "data" }),
183- _done_result (response = {"answer" : "stored" }),
184- ])
186+ responses = iter (
187+ [
188+ _action_result ("store-result" , "tool-1" , {"result" : "data" }),
189+ _done_result (response = {"answer" : "stored" }),
190+ ]
191+ )
185192
186- with patch .object (self .store .maintenance , "send" ,
187- side_effect = self ._patched_send (lambda : next (responses ))):
193+ with patch .object (self .store .maintenance , "send" , side_effect = self ._patched_send (lambda : next (responses ))):
188194 chat = self .store .ai .conversation (AGENT_ID , "conversations/" )
189195 chat .set_user_prompt ("Store something" )
190- chat .handle ("store-result" ,
191- lambda args : calls .append (args ) or "ok" ,
192- AiHandleErrorStrategy . SEND_ERRORS_TO_MODEL )
196+ chat .handle (
197+ "store-result" , lambda args : calls .append (args ) or "ok" , AiHandleErrorStrategy . SEND_ERRORS_TO_MODEL
198+ )
193199 result = chat .run ()
194200
195201 self .assertEqual (AiConversationStatus .DONE , result .status )
@@ -198,17 +204,18 @@ def test_handle_invokes_handler_and_sends_response(self):
198204
199205 def test_receive_invokes_handler_with_request_and_args (self ):
200206 received = []
201- responses = iter ([
202- _action_result ("store-result" , "tool-2" , {"result" : "payload" }),
203- _done_result (),
204- ])
207+ responses = iter (
208+ [
209+ _action_result ("store-result" , "tool-2" , {"result" : "payload" }),
210+ _done_result (),
211+ ]
212+ )
205213
206214 def my_receiver (request , args ):
207215 received .append ((request , args ))
208216 chat .add_action_response (request .tool_id , "done" )
209217
210- with patch .object (self .store .maintenance , "send" ,
211- side_effect = self ._patched_send (lambda : next (responses ))):
218+ with patch .object (self .store .maintenance , "send" , side_effect = self ._patched_send (lambda : next (responses ))):
212219 chat = self .store .ai .conversation (AGENT_ID , "conversations/" )
213220 chat .set_user_prompt ("Do something" )
214221 chat .receive ("store-result" , my_receiver , AiHandleErrorStrategy .SEND_ERRORS_TO_MODEL )
@@ -222,19 +229,18 @@ def my_receiver(request, args):
222229 self .assertEqual ({"result" : "payload" }, args )
223230
224231 def test_multi_turn_action_loop (self ):
225- responses = iter ([
226- _action_result ("store-result" , "tool-a" , {"result" : "first" }),
227- _action_result ("store-result" , "tool-b" , {"result" : "second" }),
228- _done_result (response = {"answer" : "all done" }),
229- ])
230-
231- with patch .object (self .store .maintenance , "send" ,
232- side_effect = self ._patched_send (lambda : next (responses ))):
232+ responses = iter (
233+ [
234+ _action_result ("store-result" , "tool-a" , {"result" : "first" }),
235+ _action_result ("store-result" , "tool-b" , {"result" : "second" }),
236+ _done_result (response = {"answer" : "all done" }),
237+ ]
238+ )
239+
240+ with patch .object (self .store .maintenance , "send" , side_effect = self ._patched_send (lambda : next (responses ))):
233241 chat = self .store .ai .conversation (AGENT_ID , "conversations/" )
234242 chat .set_user_prompt ("Do two things" )
235- chat .handle ("store-result" ,
236- lambda args : "handled" ,
237- AiHandleErrorStrategy .SEND_ERRORS_TO_MODEL )
243+ chat .handle ("store-result" , lambda args : "handled" , AiHandleErrorStrategy .SEND_ERRORS_TO_MODEL )
238244 result = chat .run ()
239245
240246 self .assertEqual (AiConversationStatus .DONE , result .status )
@@ -244,16 +250,17 @@ def test_multi_turn_action_loop(self):
244250 # ------------------------------------------------------------------
245251
246252 def test_handler_error_send_to_model (self ):
247- responses = iter ([
248- _action_result ("store-result" , "tool-err" , {"result" : "x" }),
249- _done_result (response = {"answer" : "recovered" }),
250- ])
253+ responses = iter (
254+ [
255+ _action_result ("store-result" , "tool-err" , {"result" : "x" }),
256+ _done_result (response = {"answer" : "recovered" }),
257+ ]
258+ )
251259
252260 def bad_handler (args ):
253261 raise ValueError ("something went wrong" )
254262
255- with patch .object (self .store .maintenance , "send" ,
256- side_effect = self ._patched_send (lambda : next (responses ))):
263+ with patch .object (self .store .maintenance , "send" , side_effect = self ._patched_send (lambda : next (responses ))):
257264 chat = self .store .ai .conversation (AGENT_ID , "conversations/" )
258265 chat .set_user_prompt ("Trigger error" )
259266 chat .handle ("store-result" , bad_handler , AiHandleErrorStrategy .SEND_ERRORS_TO_MODEL )
@@ -269,8 +276,7 @@ def mock_send():
269276 def bad_handler (args ):
270277 raise RuntimeError ("fatal error" )
271278
272- with patch .object (self .store .maintenance , "send" ,
273- side_effect = self ._patched_send (mock_send )):
279+ with patch .object (self .store .maintenance , "send" , side_effect = self ._patched_send (mock_send )):
274280 chat = self .store .ai .conversation (AGENT_ID , "conversations/" )
275281 chat .set_user_prompt ("Trigger fatal error" )
276282 chat .handle ("store-result" , bad_handler , AiHandleErrorStrategy .RAISE_IMMEDIATELY )
@@ -283,17 +289,18 @@ def bad_handler(args):
283289
284290 def test_on_unhandled_action_is_called (self ):
285291 unhandled = []
286- responses = iter ([
287- _action_result ("unknown-action" , "t-99" , {"x" : 1 }),
288- _done_result (),
289- ])
292+ responses = iter (
293+ [
294+ _action_result ("unknown-action" , "t-99" , {"x" : 1 }),
295+ _done_result (),
296+ ]
297+ )
290298
291299 def on_unhandled (event_args ):
292300 unhandled .append (event_args )
293301 event_args .sender .add_action_response (event_args .action .tool_id , "fallback" )
294302
295- with patch .object (self .store .maintenance , "send" ,
296- side_effect = self ._patched_send (lambda : next (responses ))):
303+ with patch .object (self .store .maintenance , "send" , side_effect = self ._patched_send (lambda : next (responses ))):
297304 chat = self .store .ai .conversation (AGENT_ID , "conversations/" )
298305 chat .on_unhandled_action = on_unhandled
299306 chat .set_user_prompt ("Do something unhandled" )
@@ -305,12 +312,13 @@ def on_unhandled(event_args):
305312 self .assertEqual ("unknown-action" , unhandled [0 ].action .name )
306313
307314 def test_no_handler_raises_runtime_error (self ):
308- responses = iter ([
309- _action_result ("missing-action" , "t-0" , {}),
310- ])
315+ responses = iter (
316+ [
317+ _action_result ("missing-action" , "t-0" , {}),
318+ ]
319+ )
311320
312- with patch .object (self .store .maintenance , "send" ,
313- side_effect = self ._patched_send (lambda : next (responses ))):
321+ with patch .object (self .store .maintenance , "send" , side_effect = self ._patched_send (lambda : next (responses ))):
314322 chat = self .store .ai .conversation (AGENT_ID , "conversations/" )
315323 chat .set_user_prompt ("Trigger missing handler" )
316324 with self .assertRaises (RuntimeError ):
@@ -389,8 +397,7 @@ def test_stream_collects_chunks_and_returns_answer(self):
389397 def mock_send ():
390398 return _done_result (response = {"answer" : "streamed answer" })
391399
392- with patch .object (self .store .maintenance , "send" ,
393- side_effect = self ._patched_send (mock_send )):
400+ with patch .object (self .store .maintenance , "send" , side_effect = self ._patched_send (mock_send )):
394401 chat = self .store .ai .conversation (AGENT_ID , "conversations/" )
395402 chat .set_user_prompt ("Stream this" )
396403 result = chat .stream (stream_property_path = "answer" , on_chunk = chunks .append )
@@ -414,8 +421,7 @@ def test_required_actions_before_run_raises(self):
414421 _ = chat .required_actions
415422
416423 def test_required_actions_after_run_returns_list (self ):
417- with patch .object (self .store .maintenance , "send" ,
418- side_effect = self ._patched_send (lambda : _done_result ())):
424+ with patch .object (self .store .maintenance , "send" , side_effect = self ._patched_send (lambda : _done_result ())):
419425 chat = self .store .ai .conversation (AGENT_ID , "conversations/" )
420426 chat .set_user_prompt ("Hello" )
421427 chat .run ()
@@ -435,5 +441,3 @@ def test_duplicate_action_handler_raises(self):
435441
436442if __name__ == "__main__" :
437443 unittest .main ()
438-
439-
0 commit comments