forked from langgenius/dify
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtest_agent_service.py
More file actions
1028 lines (875 loc) · 39.9 KB
/
test_agent_service.py
File metadata and controls
1028 lines (875 loc) · 39.9 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
from unittest.mock import MagicMock, create_autospec, patch
import pytest
from faker import Faker
from core.plugin.impl.exc import PluginDaemonClientSideError
from models import Account
from models.model import AppModelConfig, Conversation, EndUser, Message, MessageAgentThought
from services.account_service import AccountService, TenantService
from services.agent_service import AgentService
from services.app_service import AppService
class TestAgentService:
"""Integration tests for AgentService using testcontainers."""
@pytest.fixture
def mock_external_service_dependencies(self):
"""Mock setup for external service dependencies."""
with (
patch("services.agent_service.PluginAgentClient") as mock_plugin_agent_client,
patch("services.agent_service.ToolManager") as mock_tool_manager,
patch("services.agent_service.AgentConfigManager") as mock_agent_config_manager,
patch("services.agent_service.current_user", create_autospec(Account, instance=True)) as mock_current_user,
patch("services.app_service.FeatureService") as mock_feature_service,
patch("services.app_service.EnterpriseService") as mock_enterprise_service,
patch("services.app_service.ModelManager") as mock_model_manager,
patch("services.account_service.FeatureService") as mock_account_feature_service,
):
# Setup default mock returns for agent service
mock_plugin_agent_client_instance = mock_plugin_agent_client.return_value
mock_plugin_agent_client_instance.fetch_agent_strategy_providers.return_value = [
MagicMock(
plugin_id="test_plugin",
declaration=MagicMock(
identity=MagicMock(name="test_provider"),
strategies=[MagicMock(identity=MagicMock(name="test_strategy"))],
),
)
]
mock_plugin_agent_client_instance.fetch_agent_strategy_provider.return_value = MagicMock(
plugin_id="test_plugin",
declaration=MagicMock(
identity=MagicMock(name="test_provider"),
strategies=[MagicMock(identity=MagicMock(name="test_strategy"))],
),
)
# Setup ToolManager mocks
mock_tool_manager.get_tool_icon.return_value = "test_icon"
mock_tool_manager.get_tool_label.return_value = MagicMock(
to_dict=lambda: {"en_US": "Test Tool", "zh_Hans": "测试工具"}
)
# Setup AgentConfigManager mocks
mock_agent_config = MagicMock()
mock_agent_config.tools = [
MagicMock(tool_name="test_tool", provider_type="test_provider", provider_id="test_id")
]
mock_agent_config_manager.convert.return_value = mock_agent_config
# Setup current_user mock
mock_current_user.timezone = "UTC"
# Setup default mock returns for app service
mock_feature_service.get_system_features.return_value.webapp_auth.enabled = False
mock_enterprise_service.WebAppAuth.update_app_access_mode.return_value = None
mock_enterprise_service.WebAppAuth.cleanup_webapp.return_value = None
# Setup default mock returns for account service
mock_account_feature_service.get_system_features.return_value.is_allow_register = True
# Mock ModelManager for model configuration
mock_model_instance = mock_model_manager.return_value
mock_model_instance.get_default_model_instance.return_value = None
mock_model_instance.get_default_provider_model_name.return_value = ("openai", "gpt-3.5-turbo")
yield {
"plugin_agent_client": mock_plugin_agent_client,
"tool_manager": mock_tool_manager,
"agent_config_manager": mock_agent_config_manager,
"current_user": mock_current_user,
"feature_service": mock_feature_service,
"enterprise_service": mock_enterprise_service,
"model_manager": mock_model_manager,
"account_feature_service": mock_account_feature_service,
}
def _create_test_app_and_account(self, db_session_with_containers, mock_external_service_dependencies):
"""
Helper method to create a test app and account for testing.
Args:
db_session_with_containers: Database session from testcontainers infrastructure
mock_external_service_dependencies: Mock dependencies
Returns:
tuple: (app, account) - Created app and account instances
"""
fake = Faker()
# Setup mocks for account creation
mock_external_service_dependencies[
"account_feature_service"
].get_system_features.return_value.is_allow_register = True
# Create account and tenant
account = AccountService.create_account(
email=fake.email(),
name=fake.name(),
interface_language="en-US",
password=fake.password(length=12),
)
TenantService.create_owner_tenant_if_not_exist(account, name=fake.company())
tenant = account.current_tenant
# Create app with realistic data
app_args = {
"name": fake.company(),
"description": fake.text(max_nb_chars=100),
"mode": "agent-chat",
"icon_type": "emoji",
"icon": "🤖",
"icon_background": "#FF6B6B",
"api_rph": 100,
"api_rpm": 10,
}
app_service = AppService()
app = app_service.create_app(tenant.id, app_args, account)
# Update the app model config to set agent_mode for agent-chat mode
if app.mode == "agent-chat" and app.app_model_config:
app.app_model_config.agent_mode = json.dumps({"enabled": True, "strategy": "react", "tools": []})
from extensions.ext_database import db
db.session.commit()
return app, account
def _create_test_conversation_and_message(self, db_session_with_containers, app, account):
"""
Helper method to create a test conversation and message with agent thoughts.
Args:
db_session_with_containers: Database session from testcontainers infrastructure
app: App instance
account: Account instance
Returns:
tuple: (conversation, message) - Created conversation and message instances
"""
fake = Faker()
from extensions.ext_database import db
# Create conversation
conversation = Conversation(
id=fake.uuid4(),
app_id=app.id,
from_account_id=account.id,
from_end_user_id=None,
name=fake.sentence(),
inputs={},
status="normal",
mode="chat",
from_source="api",
)
db.session.add(conversation)
db.session.commit()
# Create app model config
app_model_config = AppModelConfig(
id=fake.uuid4(),
app_id=app.id,
provider="openai",
model_id="gpt-3.5-turbo",
configs={},
model="gpt-3.5-turbo",
agent_mode=json.dumps({"enabled": True, "strategy": "react", "tools": []}),
)
db.session.add(app_model_config)
db.session.commit()
# Update conversation with app model config
conversation.app_model_config_id = app_model_config.id
db.session.commit()
# Create message
message = Message(
id=fake.uuid4(),
conversation_id=conversation.id,
app_id=app.id,
from_account_id=account.id,
from_end_user_id=None,
inputs={},
query=fake.text(max_nb_chars=100),
message=[{"role": "user", "text": fake.text(max_nb_chars=100)}],
answer=fake.text(max_nb_chars=200),
message_tokens=100,
message_unit_price=0.001,
answer_tokens=200,
answer_unit_price=0.001,
provider_response_latency=1.5,
currency="USD",
from_source="api",
)
db.session.add(message)
db.session.commit()
return conversation, message
def _create_test_agent_thoughts(self, db_session_with_containers, message):
"""
Helper method to create test agent thoughts for a message.
Args:
db_session_with_containers: Database session from testcontainers infrastructure
message: Message instance
Returns:
list: Created agent thoughts
"""
fake = Faker()
from extensions.ext_database import db
agent_thoughts = []
# Create first agent thought
thought1 = MessageAgentThought(
message_id=message.id,
position=1,
thought="I need to analyze the user's request",
tool="test_tool",
tool_labels_str=json.dumps({"test_tool": {"en_US": "Test Tool", "zh_Hans": "测试工具"}}),
tool_meta_str=json.dumps(
{
"test_tool": {
"error": None,
"time_cost": 0.5,
"tool_config": {"tool_provider_type": "test_provider", "tool_provider": "test_id"},
"tool_parameters": {},
}
}
),
tool_input=json.dumps({"test_tool": {"input": "test_input"}}),
observation=json.dumps({"test_tool": {"output": "test_output"}}),
tokens=50,
created_by_role="account",
created_by=message.from_account_id,
)
db.session.add(thought1)
agent_thoughts.append(thought1)
# Create second agent thought
thought2 = MessageAgentThought(
message_id=message.id,
position=2,
thought="Based on the analysis, I can provide a response",
tool="dataset_tool",
tool_labels_str=json.dumps({"dataset_tool": {"en_US": "Dataset Tool", "zh_Hans": "数据集工具"}}),
tool_meta_str=json.dumps(
{
"dataset_tool": {
"error": None,
"time_cost": 0.3,
"tool_config": {"tool_provider_type": "dataset-retrieval", "tool_provider": "dataset_id"},
"tool_parameters": {},
}
}
),
tool_input=json.dumps({"dataset_tool": {"query": "test_query"}}),
observation=json.dumps({"dataset_tool": {"results": "test_results"}}),
tokens=30,
created_by_role="account",
created_by=message.from_account_id,
)
db.session.add(thought2)
agent_thoughts.append(thought2)
db.session.commit()
return agent_thoughts
def test_get_agent_logs_success(self, db_session_with_containers, mock_external_service_dependencies):
"""
Test successful retrieval of agent logs with complete data.
"""
fake = Faker()
# Create test data
app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
conversation, message = self._create_test_conversation_and_message(db_session_with_containers, app, account)
agent_thoughts = self._create_test_agent_thoughts(db_session_with_containers, message)
# Execute the method under test
result = AgentService.get_agent_logs(app, str(conversation.id), str(message.id))
# Verify the result structure
assert result is not None
assert "meta" in result
assert "iterations" in result
assert "files" in result
# Verify meta information
meta = result["meta"]
assert meta["status"] == "success"
assert meta["executor"] == account.name
assert meta["iterations"] == 2
assert meta["agent_mode"] == "react"
assert meta["total_tokens"] == 300 # 100 + 200
assert meta["elapsed_time"] == 1.5
# Verify iterations
iterations = result["iterations"]
assert len(iterations) == 2
# Verify first iteration
first_iteration = iterations[0]
assert first_iteration["tokens"] == 50
assert first_iteration["thought"] == "I need to analyze the user's request"
assert len(first_iteration["tool_calls"]) == 1
tool_call = first_iteration["tool_calls"][0]
assert tool_call["tool_name"] == "test_tool"
assert tool_call["tool_label"] == {"en_US": "Test Tool", "zh_Hans": "测试工具"}
assert tool_call["status"] == "success"
assert tool_call["time_cost"] == 0.5
assert tool_call["tool_icon"] == "test_icon"
# Verify second iteration
second_iteration = iterations[1]
assert second_iteration["tokens"] == 30
assert second_iteration["thought"] == "Based on the analysis, I can provide a response"
assert len(second_iteration["tool_calls"]) == 1
dataset_tool_call = second_iteration["tool_calls"][0]
assert dataset_tool_call["tool_name"] == "dataset_tool"
assert dataset_tool_call["tool_label"] == {"en_US": "Dataset Tool", "zh_Hans": "数据集工具"}
assert dataset_tool_call["status"] == "success"
assert dataset_tool_call["time_cost"] == 0.3
assert dataset_tool_call["tool_icon"] == "" # dataset-retrieval tools have empty icon
def test_get_agent_logs_conversation_not_found(
self, db_session_with_containers, mock_external_service_dependencies
):
"""
Test error handling when conversation is not found.
"""
fake = Faker()
# Create test data
app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
# Execute the method under test with non-existent conversation
with pytest.raises(ValueError, match="Conversation not found"):
AgentService.get_agent_logs(app, fake.uuid4(), fake.uuid4())
def test_get_agent_logs_message_not_found(self, db_session_with_containers, mock_external_service_dependencies):
"""
Test error handling when message is not found.
"""
fake = Faker()
# Create test data
app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
conversation, message = self._create_test_conversation_and_message(db_session_with_containers, app, account)
# Execute the method under test with non-existent message
with pytest.raises(ValueError, match="Message not found"):
AgentService.get_agent_logs(app, str(conversation.id), fake.uuid4())
def test_get_agent_logs_with_end_user(self, db_session_with_containers, mock_external_service_dependencies):
"""
Test agent logs retrieval when conversation is from end user.
"""
fake = Faker()
# Create test data
app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
from extensions.ext_database import db
# Create end user
end_user = EndUser(
id=fake.uuid4(),
tenant_id=app.tenant_id,
app_id=app.id,
type="web_app",
is_anonymous=False,
session_id=fake.uuid4(),
name=fake.name(),
)
db.session.add(end_user)
db.session.commit()
# Create conversation with end user
conversation = Conversation(
id=fake.uuid4(),
app_id=app.id,
from_account_id=None,
from_end_user_id=end_user.id,
name=fake.sentence(),
inputs={},
status="normal",
mode="chat",
from_source="api",
)
db.session.add(conversation)
db.session.commit()
# Create app model config
app_model_config = AppModelConfig(
id=fake.uuid4(),
app_id=app.id,
provider="openai",
model_id="gpt-3.5-turbo",
configs={},
model="gpt-3.5-turbo",
agent_mode=json.dumps({"enabled": True, "strategy": "react", "tools": []}),
)
db.session.add(app_model_config)
db.session.commit()
# Update conversation with app model config
conversation.app_model_config_id = app_model_config.id
db.session.commit()
# Create message
message = Message(
id=fake.uuid4(),
conversation_id=conversation.id,
app_id=app.id,
from_account_id=None,
from_end_user_id=end_user.id,
inputs={},
query=fake.text(max_nb_chars=100),
message=[{"role": "user", "text": fake.text(max_nb_chars=100)}],
answer=fake.text(max_nb_chars=200),
message_tokens=100,
message_unit_price=0.001,
answer_tokens=200,
answer_unit_price=0.001,
provider_response_latency=1.5,
currency="USD",
from_source="api",
)
db.session.add(message)
db.session.commit()
# Execute the method under test
result = AgentService.get_agent_logs(app, str(conversation.id), str(message.id))
# Verify the result
assert result is not None
assert result["meta"]["executor"] == end_user.name
def test_get_agent_logs_with_unknown_executor(self, db_session_with_containers, mock_external_service_dependencies):
"""
Test agent logs retrieval when executor is unknown.
"""
fake = Faker()
# Create test data
app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
from extensions.ext_database import db
# Create conversation with non-existent account
conversation = Conversation(
id=fake.uuid4(),
app_id=app.id,
from_account_id=fake.uuid4(), # Non-existent account
from_end_user_id=None,
name=fake.sentence(),
inputs={},
status="normal",
mode="chat",
from_source="api",
)
db.session.add(conversation)
db.session.commit()
# Create app model config
app_model_config = AppModelConfig(
id=fake.uuid4(),
app_id=app.id,
provider="openai",
model_id="gpt-3.5-turbo",
configs={},
model="gpt-3.5-turbo",
agent_mode=json.dumps({"enabled": True, "strategy": "react", "tools": []}),
)
db.session.add(app_model_config)
db.session.commit()
# Update conversation with app model config
conversation.app_model_config_id = app_model_config.id
db.session.commit()
# Create message
message = Message(
id=fake.uuid4(),
conversation_id=conversation.id,
app_id=app.id,
from_account_id=fake.uuid4(), # Non-existent account
from_end_user_id=None,
inputs={},
query=fake.text(max_nb_chars=100),
message=[{"role": "user", "text": fake.text(max_nb_chars=100)}],
answer=fake.text(max_nb_chars=200),
message_tokens=100,
message_unit_price=0.001,
answer_tokens=200,
answer_unit_price=0.001,
provider_response_latency=1.5,
currency="USD",
from_source="api",
)
db.session.add(message)
db.session.commit()
# Execute the method under test
result = AgentService.get_agent_logs(app, str(conversation.id), str(message.id))
# Verify the result
assert result is not None
assert result["meta"]["executor"] == "Unknown"
def test_get_agent_logs_with_tool_error(self, db_session_with_containers, mock_external_service_dependencies):
"""
Test agent logs retrieval with tool errors.
"""
fake = Faker()
# Create test data
app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
conversation, message = self._create_test_conversation_and_message(db_session_with_containers, app, account)
from extensions.ext_database import db
# Create agent thought with tool error
thought_with_error = MessageAgentThought(
message_id=message.id,
position=1,
thought="I need to analyze the user's request",
tool="error_tool",
tool_labels_str=json.dumps({"error_tool": {"en_US": "Error Tool", "zh_Hans": "错误工具"}}),
tool_meta_str=json.dumps(
{
"error_tool": {
"error": "Tool execution failed",
"time_cost": 0.5,
"tool_config": {"tool_provider_type": "test_provider", "tool_provider": "test_id"},
"tool_parameters": {},
}
}
),
tool_input=json.dumps({"error_tool": {"input": "test_input"}}),
observation=json.dumps({"error_tool": {"output": "error_output"}}),
tokens=50,
created_by_role="account",
created_by=message.from_account_id,
)
db.session.add(thought_with_error)
db.session.commit()
# Execute the method under test
result = AgentService.get_agent_logs(app, str(conversation.id), str(message.id))
# Verify the result
assert result is not None
iterations = result["iterations"]
assert len(iterations) == 1
tool_call = iterations[0]["tool_calls"][0]
assert tool_call["status"] == "error"
assert tool_call["error"] == "Tool execution failed"
def test_get_agent_logs_without_agent_thoughts(
self, db_session_with_containers, mock_external_service_dependencies
):
"""
Test agent logs retrieval when message has no agent thoughts.
"""
fake = Faker()
# Create test data
app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
conversation, message = self._create_test_conversation_and_message(db_session_with_containers, app, account)
# Execute the method under test
result = AgentService.get_agent_logs(app, str(conversation.id), str(message.id))
# Verify the result
assert result is not None
assert result["meta"]["iterations"] == 0
assert len(result["iterations"]) == 0
def test_get_agent_logs_app_model_config_not_found(
self, db_session_with_containers, mock_external_service_dependencies
):
"""
Test error handling when app model config is not found.
"""
fake = Faker()
# Create test data
app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
from extensions.ext_database import db
# Remove app model config to test error handling
app.app_model_config_id = None
db.session.commit()
# Create conversation without app model config
conversation = Conversation(
id=fake.uuid4(),
app_id=app.id,
from_account_id=account.id,
from_end_user_id=None,
name=fake.sentence(),
inputs={},
status="normal",
mode="chat",
from_source="api",
app_model_config_id=None, # Explicitly set to None
)
db.session.add(conversation)
db.session.commit()
# Create message
message = Message(
id=fake.uuid4(),
conversation_id=conversation.id,
app_id=app.id,
from_account_id=account.id,
from_end_user_id=None,
inputs={},
query=fake.text(max_nb_chars=100),
message=[{"role": "user", "text": fake.text(max_nb_chars=100)}],
answer=fake.text(max_nb_chars=200),
message_tokens=100,
message_unit_price=0.001,
answer_tokens=200,
answer_unit_price=0.001,
provider_response_latency=1.5,
currency="USD",
from_source="api",
)
db.session.add(message)
db.session.commit()
# Execute the method under test
with pytest.raises(ValueError, match="App model config not found"):
AgentService.get_agent_logs(app, str(conversation.id), str(message.id))
def test_get_agent_logs_agent_config_not_found(
self, db_session_with_containers, mock_external_service_dependencies
):
"""
Test error handling when agent config is not found.
"""
fake = Faker()
# Create test data
app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
conversation, message = self._create_test_conversation_and_message(db_session_with_containers, app, account)
# Mock AgentConfigManager to return None
mock_external_service_dependencies["agent_config_manager"].convert.return_value = None
# Execute the method under test
with pytest.raises(ValueError, match="Agent config not found"):
AgentService.get_agent_logs(app, str(conversation.id), str(message.id))
def test_list_agent_providers_success(self, db_session_with_containers, mock_external_service_dependencies):
"""
Test successful listing of agent providers.
"""
fake = Faker()
# Create test data
app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
# Execute the method under test
result = AgentService.list_agent_providers(str(account.id), str(app.tenant_id))
# Verify the result
assert result is not None
assert len(result) == 1
assert result[0].plugin_id == "test_plugin"
# Verify the mock was called correctly
mock_plugin_client = mock_external_service_dependencies["plugin_agent_client"].return_value
mock_plugin_client.fetch_agent_strategy_providers.assert_called_once_with(str(app.tenant_id))
def test_get_agent_provider_success(self, db_session_with_containers, mock_external_service_dependencies):
"""
Test successful retrieval of specific agent provider.
"""
fake = Faker()
# Create test data
app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
provider_name = "test_provider"
# Execute the method under test
result = AgentService.get_agent_provider(str(account.id), str(app.tenant_id), provider_name)
# Verify the result
assert result is not None
assert result.plugin_id == "test_plugin"
# Verify the mock was called correctly
mock_plugin_client = mock_external_service_dependencies["plugin_agent_client"].return_value
mock_plugin_client.fetch_agent_strategy_provider.assert_called_once_with(str(app.tenant_id), provider_name)
def test_get_agent_provider_plugin_error(self, db_session_with_containers, mock_external_service_dependencies):
"""
Test error handling when plugin daemon client raises an error.
"""
fake = Faker()
# Create test data
app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
provider_name = "test_provider"
error_message = "Plugin not found"
# Mock PluginAgentClient to raise an error
mock_plugin_client = mock_external_service_dependencies["plugin_agent_client"].return_value
mock_plugin_client.fetch_agent_strategy_provider.side_effect = PluginDaemonClientSideError(error_message)
# Execute the method under test
with pytest.raises(ValueError, match=error_message):
AgentService.get_agent_provider(str(account.id), str(app.tenant_id), provider_name)
def test_get_agent_logs_with_complex_tool_data(
self, db_session_with_containers, mock_external_service_dependencies
):
"""
Test agent logs retrieval with complex tool data and multiple tools.
"""
fake = Faker()
# Create test data
app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
conversation, message = self._create_test_conversation_and_message(db_session_with_containers, app, account)
from extensions.ext_database import db
# Create agent thought with multiple tools
complex_thought = MessageAgentThought(
message_id=message.id,
position=1,
thought="I need to use multiple tools to complete this task",
tool="tool1;tool2;tool3",
tool_labels_str=json.dumps(
{
"tool1": {"en_US": "First Tool", "zh_Hans": "第一个工具"},
"tool2": {"en_US": "Second Tool", "zh_Hans": "第二个工具"},
"tool3": {"en_US": "Third Tool", "zh_Hans": "第三个工具"},
}
),
tool_meta_str=json.dumps(
{
"tool1": {
"error": None,
"time_cost": 0.5,
"tool_config": {"tool_provider_type": "test_provider", "tool_provider": "test_id"},
"tool_parameters": {"param1": "value1"},
},
"tool2": {
"error": "Tool 2 failed",
"time_cost": 0.3,
"tool_config": {"tool_provider_type": "another_provider", "tool_provider": "another_id"},
"tool_parameters": {"param2": "value2"},
},
"tool3": {
"error": None,
"time_cost": 0.7,
"tool_config": {"tool_provider_type": "dataset-retrieval", "tool_provider": "dataset_id"},
"tool_parameters": {"param3": "value3"},
},
}
),
tool_input=json.dumps(
{"tool1": {"input1": "data1"}, "tool2": {"input2": "data2"}, "tool3": {"input3": "data3"}}
),
observation=json.dumps(
{"tool1": {"output1": "result1"}, "tool2": {"output2": "result2"}, "tool3": {"output3": "result3"}}
),
tokens=100,
created_by_role="account",
created_by=message.from_account_id,
)
db.session.add(complex_thought)
db.session.commit()
# Execute the method under test
result = AgentService.get_agent_logs(app, str(conversation.id), str(message.id))
# Verify the result
assert result is not None
iterations = result["iterations"]
assert len(iterations) == 1
tool_calls = iterations[0]["tool_calls"]
assert len(tool_calls) == 3
# Verify first tool
assert tool_calls[0]["tool_name"] == "tool1"
assert tool_calls[0]["tool_label"] == {"en_US": "First Tool", "zh_Hans": "第一个工具"}
assert tool_calls[0]["status"] == "success"
assert tool_calls[0]["tool_parameters"] == {"param1": "value1"}
# Verify second tool (with error)
assert tool_calls[1]["tool_name"] == "tool2"
assert tool_calls[1]["tool_label"] == {"en_US": "Second Tool", "zh_Hans": "第二个工具"}
assert tool_calls[1]["status"] == "error"
assert tool_calls[1]["error"] == "Tool 2 failed"
# Verify third tool (dataset tool)
assert tool_calls[2]["tool_name"] == "tool3"
assert tool_calls[2]["tool_label"] == {"en_US": "Third Tool", "zh_Hans": "第三个工具"}
assert tool_calls[2]["status"] == "success"
assert tool_calls[2]["tool_icon"] == "" # dataset-retrieval tools have empty icon
def test_get_agent_logs_with_files(self, db_session_with_containers, mock_external_service_dependencies):
"""
Test agent logs retrieval with message files and agent thought files.
"""
fake = Faker()
# Create test data
app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
conversation, message = self._create_test_conversation_and_message(db_session_with_containers, app, account)
from core.file import FileTransferMethod, FileType
from extensions.ext_database import db
from models.enums import CreatorUserRole
# Add files to message
from models.model import MessageFile
assert message.from_account_id is not None
message_file1 = MessageFile(
message_id=message.id,
type=FileType.IMAGE,
transfer_method=FileTransferMethod.REMOTE_URL,
url="http://example.com/file1.jpg",
belongs_to="user",
created_by_role=CreatorUserRole.ACCOUNT,
created_by=message.from_account_id,
)
message_file2 = MessageFile(
message_id=message.id,
type=FileType.IMAGE,
transfer_method=FileTransferMethod.REMOTE_URL,
url="http://example.com/file2.png",
belongs_to="user",
created_by_role=CreatorUserRole.ACCOUNT,
created_by=message.from_account_id,
)
db.session.add(message_file1)
db.session.add(message_file2)
db.session.commit()
# Create agent thought with files
thought_with_files = MessageAgentThought(
message_id=message.id,
position=1,
thought="I need to process some files",
tool="file_tool",
tool_labels_str=json.dumps({"file_tool": {"en_US": "File Tool", "zh_Hans": "文件工具"}}),
tool_meta_str=json.dumps(
{
"file_tool": {
"error": None,
"time_cost": 0.5,
"tool_config": {"tool_provider_type": "test_provider", "tool_provider": "test_id"},
"tool_parameters": {},
}
}
),
tool_input=json.dumps({"file_tool": {"input": "test_input"}}),
observation=json.dumps({"file_tool": {"output": "test_output"}}),
message_files=json.dumps(["file1", "file2"]),
tokens=50,
created_by_role="account",
created_by=message.from_account_id,
)
db.session.add(thought_with_files)
db.session.commit()
# Execute the method under test
result = AgentService.get_agent_logs(app, str(conversation.id), str(message.id))
# Verify the result
assert result is not None
assert len(result["files"]) == 2
iterations = result["iterations"]
assert len(iterations) == 1
assert len(iterations[0]["files"]) == 2
assert "file1" in iterations[0]["files"]
assert "file2" in iterations[0]["files"]
def test_get_agent_logs_with_different_timezone(
self, db_session_with_containers, mock_external_service_dependencies
):
"""
Test agent logs retrieval with different timezone settings.
"""
fake = Faker()
# Create test data
app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
conversation, message = self._create_test_conversation_and_message(db_session_with_containers, app, account)
# Mock current_user with different timezone
mock_external_service_dependencies["current_user"].timezone = "Asia/Shanghai"
# Execute the method under test
result = AgentService.get_agent_logs(app, str(conversation.id), str(message.id))
# Verify the result
assert result is not None
assert "start_time" in result["meta"]
# Verify the timezone conversion
start_time = result["meta"]["start_time"]
assert "T" in start_time # ISO format
assert "+08:00" in start_time or "Z" in start_time # Timezone offset
def test_get_agent_logs_with_empty_tool_data(self, db_session_with_containers, mock_external_service_dependencies):
"""
Test agent logs retrieval with empty tool data.
"""
fake = Faker()
# Create test data
app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
conversation, message = self._create_test_conversation_and_message(db_session_with_containers, app, account)
from extensions.ext_database import db
# Create agent thought with empty tool data
empty_thought = MessageAgentThought(
message_id=message.id,
position=1,
thought="I need to analyze the user's request",
tool="", # Empty tool
tool_labels_str="{}", # Empty labels
tool_meta_str="{}", # Empty meta
tool_input="", # Empty input
observation="", # Empty observation
tokens=50,
created_by_role="account",
created_by=message.from_account_id,
)
db.session.add(empty_thought)
db.session.commit()
# Execute the method under test
result = AgentService.get_agent_logs(app, str(conversation.id), str(message.id))
# Verify the result
assert result is not None
iterations = result["iterations"]
assert len(iterations) == 1
# Verify empty tool calls
tool_calls = iterations[0]["tool_calls"]
assert len(tool_calls) == 0 # No tools to process
def test_get_agent_logs_with_malformed_json(self, db_session_with_containers, mock_external_service_dependencies):
"""
Test agent logs retrieval with malformed JSON data in tool fields.
"""
fake = Faker()
# Create test data
app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
conversation, message = self._create_test_conversation_and_message(db_session_with_containers, app, account)
from extensions.ext_database import db
# Create agent thought with malformed JSON
malformed_thought = MessageAgentThought(
message_id=message.id,
position=1,
thought="I need to analyze the user's request",
tool="test_tool",
tool_labels_str="invalid json", # Malformed JSON