-
Notifications
You must be signed in to change notification settings - Fork 615
Expand file tree
/
Copy pathdb_models.py
More file actions
967 lines (803 loc) · 43.6 KB
/
db_models.py
File metadata and controls
967 lines (803 loc) · 43.6 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
from sqlalchemy import BigInteger, Boolean, Column, Integer, JSON, Numeric, Sequence, String, Text, TIMESTAMP, UniqueConstraint, Index, Float
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.sql import func
# Standard protocol labels used across A2A models
PROTOCOL_HTTP_JSON = "HTTP+JSON"
PROTOCOL_JSONRPC = "JSONRPC"
PROTOCOL_GRPC = "GRPC"
SCHEMA = "nexent"
# Shared doc strings for primary key columns
_PRIMARY_KEY_DOC = "Primary key, auto-increment"
_TENANT_ID_DOC = "Tenant ID for multi-tenancy isolation"
# Base class for tables without audit fields
class SimpleTableBase(DeclarativeBase):
pass
class TableBase(DeclarativeBase):
create_time = Column(TIMESTAMP(timezone=False),
server_default=func.now(), doc="Creation time")
update_time = Column(TIMESTAMP(timezone=False), server_default=func.now(
), onupdate=func.now(), doc="Update time")
created_by = Column(String(100), doc="Creator")
updated_by = Column(String(100), doc="Updater")
delete_flag = Column(String(1), default="N",
doc="Whether it is deleted. Optional values: Y/N")
pass
class ConversationRecord(TableBase):
"""
Overall information table for Q&A conversations
"""
__tablename__ = "conversation_record_t"
__table_args__ = {"schema": SCHEMA}
conversation_id = Column(Integer, Sequence(
"conversation_record_t_conversation_id_seq", schema=SCHEMA), primary_key=True, nullable=False)
conversation_title = Column(String(100), doc="Conversation title")
class ConversationMessage(TableBase):
"""
Holds the specific response message content in the conversation
"""
__tablename__ = "conversation_message_t"
__table_args__ = {"schema": SCHEMA}
message_id = Column(Integer, Sequence(
"conversation_message_t_message_id_seq", schema=SCHEMA), primary_key=True, nullable=False)
conversation_id = Column(
Integer, doc="Formal foreign key used to associate with the conversation")
message_index = Column(
Integer, doc="Sequence number for frontend display sorting")
message_role = Column(
String(30), doc="The role sending the message, such as system, assistant, user")
message_content = Column(String, doc="The complete content of the message")
minio_files = Column(
String, doc="Images or documents uploaded by the user on the chat page, stored as a list")
opinion_flag = Column(String(
1), doc="User evaluation of the conversation. Enumeration value \"Y\" represents a positive review, \"N\" represents a negative review")
class ConversationMessageUnit(TableBase):
"""
Holds the agent's output content in each message
"""
__tablename__ = "conversation_message_unit_t"
__table_args__ = {"schema": SCHEMA}
unit_id = Column(Integer, Sequence("conversation_message_unit_t_unit_id_seq",
schema=SCHEMA), primary_key=True, nullable=False)
message_id = Column(
Integer, doc="Formal foreign key used to associate with the message")
conversation_id = Column(
Integer, doc="Formal foreign key used to associate with the conversation")
unit_index = Column(
Integer, doc="Sequence number for frontend display sorting")
unit_type = Column(String(100), doc="Type of the smallest answer unit")
unit_content = Column(
String, doc="Complete content of the smallest reply unit")
class ConversationSourceImage(TableBase):
"""
Holds the search image source information of conversation messages
"""
__tablename__ = "conversation_source_image_t"
__table_args__ = {"schema": SCHEMA}
image_id = Column(Integer, Sequence(
"conversation_source_image_t_image_id_seq", schema=SCHEMA), primary_key=True, nullable=False)
conversation_id = Column(
Integer, doc="Formal foreign key used to associate with the conversation to which the search source belongs")
message_id = Column(
Integer, doc="Formal foreign key used to associate with the conversation message to which the search source belongs")
unit_id = Column(
Integer, doc="Formal foreign key used to associate with the smallest message unit (if any) to which the search source belongs")
image_url = Column(String, doc="URL address of the image")
cite_index = Column(
Integer, doc="[Reserved] Citation serial number for precise traceability")
search_type = Column(String(
100), doc="[Reserved] Search source type, used to distinguish the retrieval tool from which the record originates. Optional values: web/local")
class ConversationSourceSearch(TableBase):
"""
Holds the search text source information referenced by the response messages in the conversation
"""
__tablename__ = "conversation_source_search_t"
__table_args__ = {"schema": SCHEMA}
search_id = Column(Integer, Sequence(
"conversation_source_search_t_search_id_seq", schema=SCHEMA), primary_key=True, nullable=False)
unit_id = Column(
Integer, doc="Formal foreign key used to associate with the smallest message unit (if any) to which the search source belongs")
message_id = Column(
Integer, doc="Formal foreign key used to associate with the conversation message to which the search source belongs")
conversation_id = Column(
Integer, doc="Formal foreign key used to associate with the conversation to which the search source belongs")
source_type = Column(String(
100), doc="Source type, used to distinguish whether source_location is a URL or a path. Optional values: url/text")
source_title = Column(
String(400), doc="Title or file name of the search source")
source_location = Column(
String(400), doc="URL link or file path of the search source")
source_content = Column(String, doc="Original text of the search source")
score_overall = Column(Numeric(
7, 6), doc="Overall similarity score between the source and the user query, calculated by weighted average of details")
score_accuracy = Column(Numeric(7, 6), doc="Accuracy score")
score_semantic = Column(Numeric(7, 6), doc="Semantic similarity score")
published_date = Column(TIMESTAMP(
timezone=False), doc="Upload date of local files or network search date")
cite_index = Column(
Integer, doc="Citation serial number for precise traceability")
search_type = Column(String(
100), doc="Search source type, specifically describing the retrieval tool used for this search record. Optional values: web_search/knowledge_base_search")
tool_sign = Column(String(
30), doc="Simple tool identifier used to distinguish the index source in the summary text output by the large model")
class ModelRecord(TableBase):
"""
Model list defined by the user on the configuration page
"""
__tablename__ = "model_record_t"
__table_args__ = {"schema": SCHEMA}
model_id = Column(Integer, Sequence("model_record_t_model_id_seq", schema=SCHEMA),
primary_key=True, nullable=False, doc="Model ID, unique primary key")
model_repo = Column(String(100), doc="Model path address")
model_name = Column(String(100), nullable=False, doc="Model name")
model_factory = Column(String(
100), doc="Model vendor, determining the API key and the specific format of the model response. Currently defaults to OpenAI-API-Compatible.")
model_type = Column(
String(100), doc="Model type, such as chat, embedding, rerank, tts, asr")
api_key = Column(
String(500), doc="Model API key, used for authentication for some models")
base_url = Column(
String(500), doc="Base URL address for requesting remote model services")
max_tokens = Column(Integer, doc="Maximum available tokens of the model")
used_token = Column(
Integer, doc="Number of tokens already used by the model in Q&A")
display_name = Column(String(
100), doc="Model name directly displayed on the frontend, customized by the user")
connect_status = Column(String(
100), doc="Model connectivity status of the latest detection. Optional values: Detecting, Available, Unavailable")
tenant_id = Column(String(100), doc="Tenant ID for filtering")
expected_chunk_size = Column(
Integer, doc="Expected chunk size for embedding models, used during document chunking")
maximum_chunk_size = Column(
Integer, doc="Maximum chunk size for embedding models, used during document chunking")
ssl_verify = Column(
Boolean, default=True, doc="Whether to verify SSL certificates when connecting to this model API. Default is true. Set to false for local services without SSL support.")
chunk_batch = Column(
Integer, doc="Batch size for concurrent embedding requests during document chunking")
model_appid = Column(
String(100), doc="Application ID for model authentication (used by some STT/TTS providers like Volcano Engine)")
access_token = Column(
String(100), doc="Access token for model authentication (used by some STT/TTS providers like Volcano Engine)")
class ModelMonitoringRecord(SimpleTableBase):
"""
Model monitoring record table - stores per-request LLM performance metrics.
Uses SimpleTableBase to avoid audit fields (created_by, updated_by, etc.).
"""
__tablename__ = "model_monitoring_record_t"
__table_args__ = (
Index("ix_monitoring_model_id", "model_id"),
Index("ix_monitoring_tenant_id", "tenant_id"),
Index("ix_monitoring_agent_id", "agent_id"),
Index("ix_monitoring_create_time", "create_time"),
Index("ix_monitoring_is_error", "is_error"),
Index("ix_monitoring_model_time", "model_id", "create_time"),
Index("ix_monitoring_model_type", "model_type"),
{"schema": SCHEMA},
)
monitoring_id = Column(
Integer,
Sequence("model_monitoring_record_t_monitoring_id_seq", schema=SCHEMA),
primary_key=True,
nullable=False,
doc="Monitoring record ID, auto-increment primary key",
)
model_id = Column(
Integer, doc="Model ID, foreign key reference to model_record_t.model_id"
)
model_name = Column(
String(100), nullable=False, doc="Model name at the time of the request"
)
agent_id = Column(Integer, doc="Agent ID that initiated the request")
agent_name = Column(
String(100), doc="Agent name at the time of the request")
conversation_id = Column(
Integer, doc="Conversation ID associated with this request"
)
tenant_id = Column(
String(100), nullable=False, doc="Tenant ID for multi-tenant isolation"
)
user_id = Column(String(100), doc="User ID who initiated the request")
request_duration_ms = Column(
Integer, doc="Total request duration in milliseconds")
ttft_ms = Column(Integer, doc="Time to first token in milliseconds")
input_tokens = Column(Integer, doc="Number of input tokens")
output_tokens = Column(Integer, doc="Number of output tokens")
total_tokens = Column(Integer, doc="Total tokens (input + output)")
generation_rate = Column(
Float, doc="Token generation rate (tokens per second)")
is_streaming = Column(
Boolean, default=False, doc="Whether the request used streaming"
)
is_success = Column(
Boolean, default=True, doc="Whether the request completed successfully"
)
is_error = Column(
Boolean, default=False, doc="Whether the request resulted in an error"
)
error_type = Column(
String(50), doc="Error type classification (e.g., auth_error, rate_limit)"
)
error_message = Column(Text, doc="Error message details")
retry_count = Column(Integer, default=0, doc="Number of retry attempts")
operation = Column(
String(50), doc="Operation type (e.g., llm_completion, llm_chat)"
)
create_time = Column(
TIMESTAMP(timezone=False), server_default=func.now(), doc="Record creation time"
)
delete_flag = Column(String(1), default="N", doc="Soft delete flag: Y/N")
display_name = Column(String(200), doc="User-facing model display name")
model_type = Column(
String(20), default="llm", doc="Model type: llm, embedding, multi_embedding"
)
class ToolInfo(TableBase):
"""
Information table for prompt tools
"""
__tablename__ = "ag_tool_info_t"
__table_args__ = {"schema": SCHEMA}
tool_id = Column(Integer, primary_key=True, nullable=False, doc="ID")
name = Column(String(100), doc="Unique key name")
origin_name = Column(String(100), doc="Original name")
class_name = Column(
String(100), doc="Tool class name, used when the tool is instantiated")
description = Column(String(2048), doc="Prompt tool description")
source = Column(String(100), doc="Source")
author = Column(String(100), doc="Tool author")
usage = Column(String(100), doc="Usage")
params = Column(JSON, doc="Tool parameter information (json)")
inputs = Column(String(2048), doc="Prompt tool inputs description")
output_type = Column(String(100), doc="Prompt tool output description")
category = Column(String(100), doc="Tool category description")
is_available = Column(
Boolean, doc="Whether the tool can be used under the current main service")
class AgentInfo(TableBase):
"""
Information table for agents
"""
__tablename__ = "ag_tenant_agent_t"
__table_args__ = {"schema": SCHEMA}
agent_id = Column(Integer, Sequence(
"ag_tenant_agent_t_agent_id_seq", schema=SCHEMA), nullable=False, primary_key=True, autoincrement=True, doc="ID")
version_no = Column(Integer, default=0, nullable=False, primary_key=True, doc="Version number. 0 = draft/editing state, >=1 = published snapshot")
name = Column(String(100), doc="Agent name")
display_name = Column(String(100), doc="Agent display name")
description = Column(Text, doc="Description")
author = Column(String(100), doc="Agent author")
model_name = Column(String(100), doc="[DEPRECATED] Name of the model used, use model_id instead")
model_id = Column(Integer, doc="Model ID, foreign key reference to model_record_t.model_id")
max_steps = Column(Integer, doc="Maximum number of steps")
duty_prompt = Column(Text, doc="Duty prompt content")
constraint_prompt = Column(Text, doc="Constraint prompt content")
few_shots_prompt = Column(Text, doc="Few shots prompt content")
parent_agent_id = Column(Integer, doc="Parent Agent ID")
tenant_id = Column(String(100), doc="Belonging tenant")
enabled = Column(Boolean, doc="Enabled")
provide_run_summary = Column(
Boolean, doc="Whether to provide the running summary to the manager agent")
business_description = Column(
Text, doc="Manually entered by the user to describe the entire business process")
business_logic_model_name = Column(String(100), doc="Model name used for business logic prompt generation")
business_logic_model_id = Column(Integer, doc="Model ID used for business logic prompt generation, foreign key reference to model_record_t.model_id")
group_ids = Column(String, doc="Agent group IDs list")
is_new = Column(Boolean, default=False, doc="Whether this agent is marked as new for the user")
current_version_no = Column(Integer, nullable=True, doc="Current published version number. NULL means no version published yet")
ingroup_permission = Column(String(30), doc="In-group permission: EDIT, READ_ONLY, PRIVATE")
class ToolInstance(TableBase):
"""
Information table for tenant tool configuration.
"""
__tablename__ = "ag_tool_instance_t"
__table_args__ = {"schema": SCHEMA}
tool_instance_id = Column(
Integer,
Sequence("ag_tool_instance_t_tool_instance_id_seq", schema=SCHEMA),
primary_key=True,
nullable=False,
doc="ID"
)
tool_id = Column(Integer, doc="Tenant tool ID")
agent_id = Column(Integer, doc="Agent ID")
params = Column(JSON, doc="Parameter configuration")
user_id = Column(String(100), doc="User ID")
tenant_id = Column(String(100), doc="Tenant ID")
enabled = Column(Boolean, doc="Enabled")
version_no = Column(Integer, default=0, primary_key=True, nullable=False, doc="Version number. 0 = draft/editing state, >=1 = published snapshot")
class KnowledgeRecord(TableBase):
"""
Records the description and status information of knowledge bases
"""
__tablename__ = "knowledge_record_t"
__table_args__ = {"schema": "nexent"}
knowledge_id = Column(BigInteger, Sequence("knowledge_record_t_knowledge_id_seq", schema="nexent"),
primary_key=True, nullable=False, doc="Knowledge base ID, unique primary key")
index_name = Column(String(100), doc="Internal Elasticsearch index name")
knowledge_name = Column(String(100), doc="User-facing knowledge base name")
knowledge_describe = Column(String(3000), doc="Knowledge base description")
knowledge_sources = Column(String(300), doc="Knowledge base sources")
embedding_model_name = Column(String(200), doc="Embedding model name, used to record the embedding model used by the knowledge base")
embedding_model_id = Column(Integer, doc="Embedding model ID, foreign key reference to model_record_t.model_id")
tenant_id = Column(String(100), doc="Tenant ID")
group_ids = Column(String, doc="Knowledge base group IDs list")
ingroup_permission = Column(
String(30), doc="In-group permission: EDIT, READ_ONLY, PRIVATE")
summary_frequency = Column(String(10), nullable=True,
doc="Auto-summary frequency: '3h', '5h', '1d', '1w', or NULL (disabled)")
last_summary_time = Column(TIMESTAMP(timezone=False), nullable=True,
doc="Timestamp of last summary generation")
last_doc_update_time = Column(TIMESTAMP(timezone=False), nullable=True,
doc="Timestamp of last document add/delete operation")
class TenantConfig(TableBase):
"""
Tenant configuration information table
"""
__tablename__ = "tenant_config_t"
__table_args__ = {"schema": SCHEMA}
tenant_config_id = Column(Integer, Sequence(
"tenant_config_t_tenant_config_id_seq", schema=SCHEMA), primary_key=True, nullable=False, doc="ID")
tenant_id = Column(String(100), doc="Tenant ID")
user_id = Column(String(100), doc="User ID")
value_type = Column(String(
100), doc=" the data type of config_value, optional values: single/multi", default="single")
config_key = Column(String(100), doc="the key of the config")
config_value = Column(Text, doc="the value of the config")
class MemoryUserConfig(TableBase):
"""
Tenant configuration information table
"""
__tablename__ = "memory_user_config_t"
__table_args__ = {"schema": SCHEMA}
config_id = Column(Integer, Sequence("memory_user_config_t_config_id_seq",
schema=SCHEMA), primary_key=True, nullable=False, doc="ID")
tenant_id = Column(String(100), doc="Tenant ID")
user_id = Column(String(100), doc="User ID")
value_type = Column(String(
100), doc=" the data type of config_value, optional values: single/multi", default="single")
config_key = Column(String(100), doc="the key of the config")
config_value = Column(String(10000), doc="the value of the config")
class McpRecord(TableBase):
"""
MCP (Model Context Protocol) records table
"""
__tablename__ = "mcp_record_t"
__table_args__ = {"schema": SCHEMA}
mcp_id = Column(Integer, Sequence("mcp_record_t_mcp_id_seq", schema=SCHEMA),
primary_key=True, nullable=False, doc="MCP record ID, unique primary key")
tenant_id = Column(String(100), doc="Tenant ID")
user_id = Column(String(100), doc="User ID")
mcp_name = Column(String(100), doc="MCP name")
mcp_server = Column(String(500), doc="MCP server address")
status = Column(
Boolean,
default=None,
doc="MCP server connection status, True=connected, False=disconnected, None=unknown",
)
container_id = Column(
String(200),
doc="Docker container ID for MCP service, None for non-containerized MCP",
)
authorization_token = Column(
String(500),
doc="Authorization token for MCP server authentication (e.g., Bearer token)",
default=None,
)
class UserTenant(TableBase):
"""
User and tenant relationship table
"""
__tablename__ = "user_tenant_t"
__table_args__ = {"schema": SCHEMA}
user_tenant_id = Column(Integer, Sequence("user_tenant_t_user_tenant_id_seq", schema=SCHEMA),
primary_key=True, nullable=False, doc="User tenant relationship ID, unique primary key")
user_id = Column(String(100), nullable=False, doc="User ID")
tenant_id = Column(String(100), nullable=False, doc="Tenant ID")
user_role = Column(String(30), doc="User role: SUPER_ADMIN, ADMIN, DEV, USER")
user_email = Column(String(255), doc="User email address")
class AgentRelation(TableBase):
"""
Agent parent-child relationship table
"""
__tablename__ = "ag_agent_relation_t"
__table_args__ = {"schema": SCHEMA}
relation_id = Column(Integer, Sequence("ag_agent_relation_t_relation_id_seq", schema=SCHEMA), primary_key=True, nullable=False, doc="Relationship ID, primary key")
selected_agent_id = Column(Integer, primary_key=True, doc="Selected agent ID")
parent_agent_id = Column(Integer, doc="Parent agent ID")
tenant_id = Column(String(100), doc="Tenant ID")
version_no = Column(Integer, default=0, nullable=False, doc="Version number. 0 = draft/editing state, >=1 = published snapshot")
class PartnerMappingId(TableBase):
"""
External-Internal ID mapping table for partners
"""
__tablename__ = "partner_mapping_id_t"
__table_args__ = {"schema": SCHEMA}
mapping_id = Column(Integer, Sequence("partner_mapping_id_t_mapping_id_seq",
schema=SCHEMA), primary_key=True, nullable=False, doc="ID")
external_id = Column(
String(100), doc="The external id given by the outer partner")
internal_id = Column(
Integer, doc="The internal id of the other database table")
mapping_type = Column(String(
30), doc="Type of the external - internal mapping, value set: CONVERSATION")
tenant_id = Column(String(100), doc="Tenant ID")
user_id = Column(String(100), doc="User ID")
class TenantInvitationCode(TableBase):
"""
Tenant invitation code information table
"""
__tablename__ = "tenant_invitation_code_t"
__table_args__ = {"schema": SCHEMA}
invitation_id = Column(Integer, Sequence("tenant_invitation_code_t_invitation_id_seq", schema=SCHEMA),
primary_key=True, nullable=False, doc="Invitation ID, primary key")
tenant_id = Column(String(100), nullable=False,
doc="Tenant ID, foreign key")
invitation_code = Column(String(100), nullable=False,
unique=True, doc="Invitation code")
group_ids = Column(String, doc="Associated group IDs list")
capacity = Column(Integer, nullable=False, default=1,
doc="Invitation code capacity")
expiry_date = Column(TIMESTAMP(timezone=False),
doc="Invitation code expiry date")
status = Column(String(30), nullable=False,
doc="Invitation code status: IN_USE, EXPIRE, DISABLE, RUN_OUT")
code_type = Column(String(30), nullable=False,
doc="Invitation code type: ADMIN_INVITE, DEV_INVITE, USER_INVITE")
class TenantInvitationRecord(TableBase):
"""
Tenant invitation record table
"""
__tablename__ = "tenant_invitation_record_t"
__table_args__ = {"schema": SCHEMA}
invitation_record_id = Column(Integer, Sequence("tenant_invitation_record_t_invitation_record_id_seq", schema=SCHEMA),
primary_key=True, nullable=False, doc="Invitation record ID, primary key")
invitation_id = Column(Integer, nullable=False,
doc="Invitation ID, foreign key")
user_id = Column(String(100), nullable=False, doc="User ID")
class TenantGroupInfo(TableBase):
"""
Tenant group information table
"""
__tablename__ = "tenant_group_info_t"
__table_args__ = {"schema": SCHEMA}
group_id = Column(Integer, Sequence("tenant_group_info_t_group_id_seq", schema=SCHEMA),
primary_key=True, nullable=False, doc="Group ID, primary key")
tenant_id = Column(String(100), nullable=False,
doc="Tenant ID, foreign key")
group_name = Column(String(100), nullable=False, doc="Group name")
group_description = Column(String(500), doc="Group description")
class TenantGroupUser(TableBase):
"""
Tenant group user membership table
"""
__tablename__ = "tenant_group_user_t"
__table_args__ = {"schema": SCHEMA}
group_user_id = Column(Integer, Sequence("tenant_group_user_t_group_user_id_seq", schema=SCHEMA),
primary_key=True, nullable=False, doc="Group user ID, primary key")
group_id = Column(Integer, nullable=False, doc="Group ID, foreign key")
user_id = Column(String(100), nullable=False, doc="User ID, foreign key")
class RolePermission(SimpleTableBase):
"""
Role permission configuration table
Note: This table does not have audit fields (create_time, update_time, etc.)
"""
__tablename__ = "role_permission_t"
__table_args__ = {"schema": SCHEMA}
role_permission_id = Column(Integer, Sequence("role_permission_t_role_permission_id_seq", schema=SCHEMA),
primary_key=True, nullable=False, doc="Role permission ID, primary key")
user_role = Column(String(30), nullable=False,
doc="User role: SU, ADMIN, DEV, USER")
permission_category = Column(String(30), doc="Permission category")
permission_type = Column(String(30), doc="Permission type")
permission_subtype = Column(String(30), doc="Permission subtype")
class AgentVersion(TableBase):
"""
Agent version metadata table. Stores version info, release notes, and version lineage.
"""
__tablename__ = "ag_tenant_agent_version_t"
__table_args__ = {"schema": SCHEMA}
id = Column(BigInteger, Sequence("ag_tenant_agent_version_t_id_seq", schema=SCHEMA),
primary_key=True, nullable=False, doc=_PRIMARY_KEY_DOC)
tenant_id = Column(String(100), nullable=False, doc="Tenant ID")
agent_id = Column(Integer, nullable=False, doc="Agent ID")
version_no = Column(Integer, nullable=False, doc="Version number, starts from 1. Does not include 0 (draft)")
version_name = Column(String(100), doc="User-defined version name for display")
release_note = Column(Text, doc="Release notes / publish remarks")
source_version_no = Column(Integer, doc="Source version number. If this version is a rollback, record the source version")
source_type = Column(String(30), doc="Source type: NORMAL (normal publish) / ROLLBACK (rollback and republish)")
status = Column(String(30), default="RELEASED", doc="Version status: RELEASED / DISABLED / ARCHIVED")
is_a2a = Column(Boolean, default=False, doc="Whether this version is published as an A2A Server agent")
class UserTokenInfo(TableBase):
"""
User token (AK/SK) information table
"""
__tablename__ = "user_token_info_t"
__table_args__ = {"schema": SCHEMA}
token_id = Column(Integer, Sequence("user_token_info_t_token_id_seq", schema=SCHEMA),
primary_key=True, nullable=False, doc="Token ID, unique primary key")
access_key = Column(String(100), nullable=False, doc="Access Key (AK)")
user_id = Column(String(100), nullable=False, doc="User ID who owns this token")
class UserTokenUsageLog(TableBase):
"""
User token usage log table
"""
__tablename__ = "user_token_usage_log_t"
__table_args__ = {"schema": SCHEMA}
token_usage_id = Column(Integer, Sequence("user_token_usage_log_t_token_usage_id_seq", schema=SCHEMA),
primary_key=True, nullable=False, doc="Token usage log ID, unique primary key")
token_id = Column(Integer, nullable=False, doc="Foreign key to user_token_info_t.token_id")
call_function_name = Column(String(100), doc="API function name being called")
related_id = Column(Integer, doc="Related resource ID (e.g., conversation_id)")
meta_data = Column(JSONB, doc="Additional metadata for this usage log entry, stored as JSON")
class UserOAuthAccount(TableBase):
__tablename__ = "user_oauth_account_t"
__table_args__ = (
UniqueConstraint("provider", "provider_user_id", name="uq_oauth_provider_user"),
{"schema": SCHEMA},
)
oauth_account_id = Column(
Integer,
Sequence("user_oauth_account_t_oauth_account_id_seq", schema=SCHEMA),
primary_key=True,
nullable=False,
doc="OAuth account ID, primary key",
)
user_id = Column(String(100), nullable=False, doc="Supabase user UUID")
provider = Column(
String(30), nullable=False, doc="OAuth provider name: github, wechat"
)
provider_user_id = Column(
String(200), nullable=False, doc="User ID from the OAuth provider"
)
provider_email = Column(String(255), doc="Email address from the OAuth provider")
provider_username = Column(String(200), doc="Display name from the OAuth provider")
tenant_id = Column(String(100), doc="Tenant ID at time of linking")
class SkillInfo(TableBase):
"""
Skill information table - stores skill metadata and content.
"""
__tablename__ = "ag_skill_info_t"
__table_args__ = {"schema": SCHEMA}
skill_id = Column(Integer, Sequence("ag_skill_info_t_skill_id_seq", schema=SCHEMA),
primary_key=True, nullable=False, autoincrement=True, doc="Skill ID")
skill_name = Column(String(100), nullable=False, unique=True, doc="Unique skill name")
skill_description = Column(String(1000), doc="Skill description")
skill_tags = Column(JSON, doc="Skill tags as JSON array")
skill_content = Column(Text, doc="Skill content in markdown format")
params = Column(JSON, doc="Skill configuration parameters as JSON object")
source = Column(String(30), nullable=False, default="official",
doc="Skill source: official, custom, etc.")
class SkillToolRelation(TableBase):
"""
Skill-Tool relation table - many-to-many relationship between skills and tools.
"""
__tablename__ = "ag_skill_tools_rel_t"
__table_args__ = {"schema": SCHEMA}
rel_id = Column(Integer, Sequence("ag_skill_tools_rel_t_rel_id_seq", schema=SCHEMA),
primary_key=True, nullable=False, autoincrement=True, doc="Relation ID")
skill_id = Column(Integer, nullable=False, doc="Foreign key to ag_skill_info_t.skill_id")
tool_id = Column(Integer, nullable=False, doc="Foreign key to ag_tool_info_t.tool_id")
class SkillInstance(TableBase):
"""
Skill instance table - stores per-agent skill configuration.
Similar to ToolInstance, stores skill settings for each agent version.
Note: skill_description and skill_content removed - these are now retrieved from ag_skill_info_t.
"""
__tablename__ = "ag_skill_instance_t"
__table_args__ = {"schema": SCHEMA}
skill_instance_id = Column(
Integer,
Sequence("ag_skill_instance_t_skill_instance_id_seq", schema=SCHEMA),
primary_key=True,
nullable=False,
doc="Skill instance ID"
)
skill_id = Column(Integer, nullable=False, doc="Foreign key to ag_skill_info_t.skill_id")
agent_id = Column(Integer, nullable=False, doc="Agent ID")
user_id = Column(String(100), doc="User ID")
tenant_id = Column(String(100), doc="Tenant ID")
enabled = Column(Boolean, default=True, doc="Whether this skill is enabled for the agent")
version_no = Column(Integer, default=0, primary_key=True, nullable=False, doc="Version number. 0 = draft/editing state, >=1 = published snapshot")
class OuterApiService(TableBase):
"""
OpenAPI service table - stores MCP service information converted from OpenAPI specs.
Each record represents one MCP service with its OpenAPI specification.
"""
__tablename__ = "ag_outer_api_services"
__table_args__ = {"schema": SCHEMA}
id = Column(BigInteger, Sequence("ag_outer_api_services_id_seq", schema=SCHEMA),
primary_key=True, nullable=False, doc="Service ID, unique primary key")
mcp_service_name = Column(String(100), nullable=False, doc="MCP service name (unique identifier per tenant)")
description = Column(Text, doc="Service description from OpenAPI info")
openapi_json = Column(JSONB, doc="Complete OpenAPI JSON specification")
server_url = Column(String(500), doc="Base URL of the REST API server")
headers_template = Column(JSONB, doc="Default headers template as JSON")
tenant_id = Column(String(100), nullable=False, doc="Tenant ID for multi-tenancy")
is_available = Column(Boolean, default=True, doc="Whether the service is available")
# Alias for backward compatibility
OuterApiTool = OuterApiService
class A2ANacosConfig(TableBase):
"""
Nacos configuration for external A2A agent discovery.
Stores connection info and discovery scope.
"""
__tablename__ = "ag_a2a_nacos_config_t"
__table_args__ = {"schema": SCHEMA}
id = Column(BigInteger, primary_key=True, autoincrement=True, doc=_PRIMARY_KEY_DOC)
config_id = Column(String(64), unique=True, nullable=False, doc="Unique config identifier for API reference")
# Nacos connection
nacos_addr = Column(String(512), nullable=False, doc="Nacos server address, e.g., http://nacos-server:8848")
nacos_username = Column(String(100), doc="Nacos username for authentication")
nacos_password = Column(String(256), doc="Nacos password, encrypted at rest")
# Discovery scope
namespace_id = Column(String(100), default="public", doc="Nacos namespace for service discovery")
# Metadata
name = Column(String(100), nullable=False, doc="Display name for this Nacos config")
description = Column(Text, doc="Description of this Nacos configuration")
# Tenant isolation
tenant_id = Column(String(100), nullable=False, doc="Tenant ID for multi-tenancy")
# Status
is_active = Column(Boolean, default=True, doc="Whether this Nacos config is active")
last_scan_at = Column(TIMESTAMP(timezone=False), doc="Last time a scan was performed using this config")
class A2AExternalAgent(TableBase):
"""
External A2A agents discovered from URL or Nacos.
Caches Agent Cards for A2A Client role.
"""
__tablename__ = "ag_a2a_external_agent_t"
__table_args__ = {"schema": SCHEMA}
id = Column(BigInteger, primary_key=True, autoincrement=True, doc=_PRIMARY_KEY_DOC)
# Agent metadata (cached from Agent Card)
name = Column(String(255), nullable=False, doc="Agent name from Agent Card")
description = Column(Text, doc="Agent description from Agent Card")
version = Column(String(50), doc="Agent version from Agent Card, e.g., 1.2.0")
# Primary interface (extracted from supportedInterfaces for quick access)
# In A2A 1.0, this should store the http-json-rpc URL
agent_url = Column(String(512), nullable=False, doc="Primary A2A endpoint URL (http-json-rpc by default)")
# Protocol type for calling this agent: JSONRPC, HTTP+JSON, GRPC
protocol_type = Column(String(20), default=PROTOCOL_JSONRPC, doc="Protocol type for calling this agent")
# Capabilities
streaming = Column(Boolean, default=False, doc="Whether this agent supports SSE streaming")
# All supported interfaces (full JSON array from Agent Card)
# Format: [{protocolBinding, url, protocolVersion}, ...]
supported_interfaces = Column(JSON, doc="All supported interfaces array")
# Source information
source_type = Column(String(20), nullable=False, doc="Discovery source: url or nacos")
# For URL mode
source_url = Column(String(512), doc="Direct URL to agent card")
# For Nacos mode
nacos_config_id = Column(String(64), doc="Reference to Nacos config used for discovery")
nacos_agent_name = Column(String(255), doc="Original name used for Nacos query")
# Base URL for infrastructure health checks
base_url = Column(String(512), doc="Base URL for health checks (service root address), e.g., http://agent:8080")
# Tenant isolation
tenant_id = Column(String(100), nullable=False, doc=_TENANT_ID_DOC)
# Full original Agent Card
raw_card = Column(JSON, doc="Full original Agent Card JSON from discovery")
# Cache management
cached_at = Column(TIMESTAMP(timezone=False), doc="Timestamp when Agent Card was cached")
cache_expires_at = Column(TIMESTAMP(timezone=False), doc="Timestamp when cache expires")
# Health check status
is_available = Column(Boolean, default=True, doc="Whether this agent is currently reachable")
last_check_at = Column(TIMESTAMP(timezone=False), doc="Last health check timestamp")
last_check_result = Column(String(50), doc="Last health check result: OK, ERROR, TIMEOUT")
class A2AExternalAgentRelation(TableBase):
"""
Relation between local agent and external A2A agent.
Enables local agents to call external A2A agents as sub-agents.
"""
__tablename__ = "ag_a2a_external_agent_relation_t"
__table_args__ = (
UniqueConstraint(
"local_agent_id", "external_agent_id",
name="uq_local_external_agent",
deferrable=True,
),
{"schema": SCHEMA},
)
id = Column(BigInteger, primary_key=True, autoincrement=True, doc=_PRIMARY_KEY_DOC)
# Local agent (parent)
local_agent_id = Column(Integer, nullable=False, doc="Local parent agent ID")
# External A2A agent (sub-agent) - FK to ag_a2a_external_agent_t.id
external_agent_id = Column(BigInteger, nullable=False, doc="External A2A agent ID (FK to ag_a2a_external_agent_t.id)")
# Tenant isolation
tenant_id = Column(String(100), nullable=False, doc=_TENANT_ID_DOC)
# Status
is_enabled = Column(Boolean, default=True, doc="Whether this relation is active")
class A2AServerAgent(TableBase):
"""
Local agents registered as A2A Server endpoints.
Exposes Agent Cards for external A2A callers.
"""
__tablename__ = "ag_a2a_server_agent_t"
__table_args__ = {"schema": SCHEMA}
id = Column(BigInteger, primary_key=True, autoincrement=True, doc=_PRIMARY_KEY_DOC)
# Link to local agent
agent_id = Column(Integer, nullable=False, doc="Local agent ID")
# Ownership
user_id = Column(String(100), nullable=False, doc="Owner user ID")
tenant_id = Column(String(100), nullable=False, doc=_TENANT_ID_DOC)
# Generated endpoint ID
endpoint_id = Column(String(64), unique=True, nullable=False, doc="Generated endpoint ID")
# Basic info (extracted from local agent, can be overridden)
name = Column(String(255), nullable=False, doc="Agent name exposed in Agent Card")
description = Column(Text, doc="Agent description exposed in Agent Card")
version = Column(String(50), doc="Agent version exposed in Agent Card")
# Primary endpoint URL (http-json-rpc by default)
agent_url = Column(String(512), doc="Primary A2A endpoint URL (http-json-rpc by default)")
# Capabilities
streaming = Column(Boolean, default=False, doc="Whether this agent supports SSE streaming")
# All supported interfaces (A2A 1.0 compliant)
# Format: [{protocolBinding, url, protocolVersion}, ...]
supported_interfaces = Column(JSON, doc="All supported interfaces: [{protocolBinding, url, protocolVersion}, ...]")
# Agent Card customization (partial overrides only)
card_overrides = Column(JSON, doc="User customizations for Agent Card (partial override)")
# A2A Server status
is_enabled = Column(Boolean, default=False, doc="Whether A2A Server is enabled for this agent")
# Raw Agent Card (generated from settings, for debugging)
raw_card = Column(JSON, doc="Generated Agent Card JSON (for debugging)")
# Publishing timestamps
published_at = Column(TIMESTAMP(timezone=False), doc="Timestamp when A2A Server was last enabled")
unpublished_at = Column(TIMESTAMP(timezone=False), doc="Timestamp when A2A Server was disabled")
class A2ATask(SimpleTableBase):
"""
A2A tasks for tracking requests.
Task is the unit of work, not all requests need to create a task.
"""
__tablename__ = "ag_a2a_task_t"
__table_args__ = {"schema": SCHEMA}
# Core identifiers (following A2A spec)
id = Column(String(64), primary_key=True, doc="Task ID (A2A spec: taskId)")
context_id = Column(String(64), doc="Context ID for grouping related tasks")
# Endpoint and caller info
endpoint_id = Column(String(64), nullable=False, doc="Endpoint ID")
caller_user_id = Column(String(100), doc="User ID of the caller")
caller_tenant_id = Column(String(100), doc="Tenant ID of the caller")
# Request data
raw_request = Column(JSON, doc="Original A2A request payload")
# Task state (following A2A TaskState enum)
task_state = Column(String(50), nullable=False, server_default="TASK_STATE_SUBMITTED", doc="Task state: TASK_STATE_SUBMITTED, TASK_STATE_WORKING, TASK_STATE_COMPLETED, TASK_STATE_FAILED, TASK_STATE_CANCELED, TASK_STATE_INPUT_REQUIRED, TASK_STATE_REJECTED, TASK_STATE_AUTH_REQUIRED")
state_timestamp = Column(TIMESTAMP(timezone=False), doc="Task state last update timestamp")
# Task result
result_data = Column(JSON, doc="Task final result data")
# Timestamps
create_time = Column(TIMESTAMP(timezone=False), server_default=func.now(), doc="Task creation timestamp")
update_time = Column(TIMESTAMP(timezone=False), server_default=func.now(), onupdate=func.now(), doc="Task last update timestamp")
completed_at = Column(TIMESTAMP(timezone=False), doc="Task completion timestamp")
class A2AMessage(SimpleTableBase):
"""
A2A messages within tasks.
Stores conversation history for multi-turn interactions.
"""
__tablename__ = "ag_a2a_message_t"
__table_args__ = {"schema": SCHEMA}
# Core identifiers (following A2A spec)
message_id = Column(String(64), primary_key=True, doc="Message ID (A2A spec: messageId)")
task_id = Column(String(64), nullable=True, doc="Task ID this message belongs to (nullable for standalone/simple requests)")
# Message attributes
message_index = Column(Integer, nullable=False, doc="Order of message in the conversation")
role = Column(String(20), nullable=False, doc="Message sender role: user or agent")
# Message content (following A2A Part structure)
parts = Column(JSON, nullable=False, doc="Message parts following A2A Part structure")
meta_data = Column(JSON, doc="Optional metadata")
extensions = Column(JSON, doc="Extension URI list")
# References to other tasks (optional)
reference_task_ids = Column(JSON, doc="Referenced task IDs array for multi-turn scenarios")
# Timestamp
create_time = Column(TIMESTAMP(timezone=False), server_default=func.now(), doc="Message creation timestamp")
class A2AArtifact(SimpleTableBase):
"""
A2A artifacts. Stores the output/artifacts produced by a task.
"""
__tablename__ = "ag_a2a_artifact_t"
__table_args__ = {"schema": SCHEMA}
# Core identifiers (following A2A spec)
id = Column(String(64), primary_key=True, doc="Internal primary key")
artifact_id = Column(String(64), nullable=False, doc="Artifact ID (A2A spec: artifactId)")
task_id = Column(String(64), nullable=False, doc="Task ID this artifact belongs to")
# Artifact attributes
name = Column(String(255), doc="Human-readable artifact name")
description = Column(Text, doc="Artifact description")
parts = Column(JSON, nullable=False, doc="Artifact parts following A2A Part structure")
meta_data = Column(JSON, doc="Artifact metadata")
extensions = Column(JSON, doc="Extension URI list")
# Timestamp
create_time = Column(TIMESTAMP(timezone=False), server_default=func.now(), doc="Artifact creation timestamp")