-
Notifications
You must be signed in to change notification settings - Fork 617
Expand file tree
/
Copy pathagent_db.py
More file actions
430 lines (377 loc) · 15.3 KB
/
agent_db.py
File metadata and controls
430 lines (377 loc) · 15.3 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
import logging
from typing import List
from sqlalchemy import update
from database.client import get_db_session, as_dict, filter_property
from database.db_models import AgentInfo, ToolInstance, AgentRelation
from utils.str_utils import convert_list_to_string
logger = logging.getLogger("agent_db")
def search_agent_info_by_agent_id(agent_id: int, tenant_id: str, version_no: int = 0):
"""
Search agent info by agent_id.
Default version_no=0 queries the draft version.
Args:
agent_id: Agent ID
tenant_id: Tenant ID
version_no: Version number to filter. Default 0 = draft/editing state
"""
with get_db_session() as session:
agent = session.query(AgentInfo).filter(
AgentInfo.agent_id == agent_id,
AgentInfo.tenant_id == tenant_id,
AgentInfo.version_no == version_no,
AgentInfo.delete_flag != 'Y'
).first()
if not agent:
raise ValueError("agent not found")
agent_dict = as_dict(agent)
return agent_dict
def search_agent_id_by_agent_name(agent_name: str, tenant_id: str, version_no: int = 0):
"""
Search agent id by agent name.
Default version_no=0 queries the draft version.
Args:
agent_name: Agent name
tenant_id: Tenant ID
version_no: Version number to filter. Default 0 = draft/editing state
"""
with get_db_session() as session:
agent = session.query(AgentInfo).filter(
AgentInfo.name == agent_name,
AgentInfo.tenant_id == tenant_id,
AgentInfo.version_no == version_no,
AgentInfo.delete_flag != 'Y').first()
if not agent:
raise ValueError("agent not found")
return agent.agent_id
def search_blank_sub_agent_by_main_agent_id(tenant_id: str, version_no: int = 0):
"""
Search blank sub agent by main agent id.
Default version_no=0 queries the draft version.
Args:
tenant_id: Tenant ID
version_no: Version number to filter. Default 0 = draft/editing state
"""
with get_db_session() as session:
sub_agent = session.query(AgentInfo).filter(
AgentInfo.tenant_id == tenant_id,
AgentInfo.version_no == version_no,
AgentInfo.delete_flag != 'Y',
AgentInfo.enabled == False
).first()
if sub_agent:
return sub_agent.agent_id
else:
return None
def query_sub_agents_id_list(main_agent_id: int, tenant_id: str, version_no: int = 0):
"""
Query the sub agent id list by main agent id.
Default version_no=0 queries the draft version.
Args:
main_agent_id: Parent agent ID
tenant_id: Tenant ID
version_no: Version number to filter. Default 0 = draft/editing state
"""
with get_db_session() as session:
query = session.query(AgentRelation).filter(
AgentRelation.parent_agent_id == main_agent_id,
AgentRelation.tenant_id == tenant_id,
AgentRelation.version_no == version_no,
AgentRelation.delete_flag != 'Y')
relations = query.all()
return [relation.selected_agent_id for relation in relations]
def clear_agent_new_mark(agent_id: int, tenant_id: str, user_id: str, version_no: int = 0):
"""
Clear the NEW mark for an agent.
This clears the NEW mark for ALL versions of the agent, regardless of version_no parameter.
Args:
agent_id (int): Agent ID
tenant_id (str): Tenant ID
user_id (str): User ID (for audit purposes)
version_no: Version number (kept for API compatibility, but always clears all versions)
"""
with get_db_session() as session:
# Clear NEW mark for ALL versions of this agent
result = session.execute(
update(AgentInfo)
.where(
AgentInfo.agent_id == agent_id,
AgentInfo.tenant_id == tenant_id,
AgentInfo.delete_flag == 'N'
)
.values(is_new=False, updated_by=user_id)
)
# return number of rows affected
return result.rowcount
def mark_agents_as_new(agent_ids: list[int], tenant_id: str, user_id: str, version_no: int = 0):
"""
Mark a list of agents as new.
This marks ALL versions of the specified agents as new, regardless of version_no parameter.
Args:
agent_ids: List of Agent IDs
tenant_id: Tenant ID
user_id: User ID
version_no: Version number (kept for API compatibility, but always marks all versions)
"""
if not agent_ids:
return
with get_db_session() as session:
session.execute(
update(AgentInfo)
.where(
AgentInfo.agent_id.in_(agent_ids),
AgentInfo.tenant_id == tenant_id,
AgentInfo.delete_flag == 'N'
)
.values(is_new=True, updated_by=user_id)
)
def create_agent(agent_info, tenant_id: str, user_id: str):
"""
Create a new agent in the database (draft version, version_no=0).
:param agent_info: Dictionary containing agent information
:param tenant_id:
:param user_id:
:return: Created agent object
"""
info_with_metadata = dict(agent_info)
info_with_metadata.setdefault("max_steps", 5)
info_with_metadata.update({
"tenant_id": tenant_id,
"version_no": 0, # Default to draft version
"created_by": user_id,
"updated_by": user_id,
"is_new": True, # Mark new agents as new
})
with get_db_session() as session:
new_agent = AgentInfo(**filter_property(info_with_metadata, AgentInfo))
new_agent.delete_flag = 'N'
session.add(new_agent)
session.flush()
# Directly extract agent_id and return as dict
result = {
"agent_id": new_agent.agent_id,
"tenant_id": new_agent.tenant_id,
"name": new_agent.name,
"display_name": new_agent.display_name,
"description": new_agent.description,
"author": new_agent.author,
"model_id": new_agent.model_id,
"model_name": new_agent.model_name,
"max_steps": new_agent.max_steps,
"duty_prompt": new_agent.duty_prompt,
"constraint_prompt": new_agent.constraint_prompt,
"few_shots_prompt": new_agent.few_shots_prompt,
"parent_agent_id": new_agent.parent_agent_id,
"enabled": new_agent.enabled,
"provide_run_summary": new_agent.provide_run_summary,
"business_description": new_agent.business_description,
"business_logic_model_id": new_agent.business_logic_model_id,
"business_logic_model_name": new_agent.business_logic_model_name,
"group_ids": new_agent.group_ids,
"is_new": new_agent.is_new,
"current_version_no": new_agent.current_version_no,
"version_no": new_agent.version_no,
"created_by": new_agent.created_by,
"updated_by": new_agent.updated_by,
"delete_flag": new_agent.delete_flag,
}
return result
def update_agent(agent_id, agent_info, user_id, version_no: int = 0):
"""
Update an existing agent in the database.
Default version_no=0 updates the draft version.
Args:
agent_id: ID of the agent to update
agent_info: Dictionary containing updated agent information
tenant_id: Tenant ID
user_id: Optional user ID
version_no: Version number to filter. Default 0 = draft/editing state
Returns:
Updated agent object
"""
with (get_db_session() as session):
# update ag_tenant_agent_t
agent = session.query(AgentInfo).filter(
AgentInfo.agent_id == agent_id,
AgentInfo.version_no == version_no,
AgentInfo.delete_flag != 'Y'
).first()
if not agent:
raise ValueError("ag_tenant_agent_t Agent not found")
for key, value in filter_property(agent_info.__dict__, AgentInfo).items():
if value is None:
continue
if key == "group_ids":
value = convert_list_to_string(value)
setattr(agent, key, value)
agent.updated_by = user_id
def delete_agent_by_id(agent_id, tenant_id: str, user_id: str):
"""
Delete an agent in the database (all versions).
:param agent_id: ID of the agent to delete
:param tenant_id: Tenant ID for filtering, mandatory
:param user_id: Optional user ID for filtering
:return: None
"""
from sqlalchemy import update as sqlalchemy_update
with get_db_session() as session:
# Soft delete all agent versions (version_no >= 0)
session.execute(
sqlalchemy_update(AgentInfo)
.where(
AgentInfo.agent_id == agent_id,
AgentInfo.tenant_id == tenant_id
)
.values(delete_flag='Y', updated_by=user_id)
)
# Soft delete all tool instances (all versions)
session.execute(
sqlalchemy_update(ToolInstance)
.where(
ToolInstance.agent_id == agent_id,
ToolInstance.tenant_id == tenant_id
)
.values(delete_flag='Y', updated_by=user_id)
)
def query_all_agent_info_by_tenant_id(tenant_id: str, version_no: int = 0):
"""
Query all agent info by tenant id.
Default version_no=0 queries all draft versions.
Args:
tenant_id: Tenant ID
version_no: Version number to filter. Default 0 = draft/editing state
"""
with get_db_session() as session:
agents = session.query(AgentInfo).filter(
AgentInfo.tenant_id == tenant_id,
AgentInfo.version_no == version_no,
AgentInfo.delete_flag != 'Y'
).order_by(AgentInfo.create_time.desc()).all()
return [as_dict(agent) for agent in agents]
def insert_related_agent(parent_agent_id: int, child_agent_id: int, tenant_id: str, user_id: str, version_no: int = 0) -> bool:
"""
Insert a related agent.
Default version_no=0 creates the draft version.
Args:
parent_agent_id: Parent agent ID
child_agent_id: Child agent ID
tenant_id: Tenant ID
user_id: User ID
version_no: Version number. Default 0 = draft/editing state
"""
try:
relation_info = {
"parent_agent_id": parent_agent_id,
"selected_agent_id": child_agent_id,
"tenant_id": tenant_id,
"version_no": version_no,
"created_by": user_id,
"updated_by": user_id
}
with get_db_session() as session:
new_relation = AgentRelation(
**filter_property(relation_info, AgentRelation))
session.add(new_relation)
session.flush()
return True
except Exception as e:
logger.error(f"Failed to insert related agent: {str(e)}")
return False
def delete_related_agent(parent_agent_id: int, child_agent_id: int, tenant_id: str, user_id: str, version_no: int = 0) -> bool:
"""
Delete a related agent.
Default version_no=0 deletes the draft version.
Args:
parent_agent_id: Parent agent ID
child_agent_id: Child agent ID
tenant_id: Tenant ID
user_id: User ID
version_no: Version number to filter. Default 0 = draft/editing state
"""
try:
with get_db_session() as session:
session.query(AgentRelation).filter(
AgentRelation.parent_agent_id == parent_agent_id,
AgentRelation.selected_agent_id == child_agent_id,
AgentRelation.tenant_id == tenant_id,
AgentRelation.version_no == version_no
).update(
{AgentRelation.delete_flag: 'Y', 'updated_by': user_id})
return True
except Exception as e:
logger.error(f"Failed to delete related agent: {str(e)}")
return False
def update_related_agents(parent_agent_id: int, related_agent_ids: List[int], tenant_id: str, user_id: str, version_no: int = 0):
"""
Update related agents for a parent agent by replacing all existing relations.
Default version_no=0 updates the draft version.
This function handles both creation and deletion of relations in a single transaction.
Args:
parent_agent_id: ID of the parent agent
related_agent_ids: List of child agent IDs to be related
tenant_id: Tenant ID
user_id: User ID for audit trail
version_no: Version number to filter. Default 0 = draft/editing state
"""
with get_db_session() as session:
# Get current relations
current_relations = session.query(AgentRelation).filter(
AgentRelation.parent_agent_id == parent_agent_id,
AgentRelation.tenant_id == tenant_id,
AgentRelation.version_no == version_no,
AgentRelation.delete_flag != 'Y'
).all()
current_related_ids = {
rel.selected_agent_id for rel in current_relations}
new_related_ids = set(
related_agent_ids) if related_agent_ids else set()
# Find IDs to delete (in current but not in new)
ids_to_delete = current_related_ids - new_related_ids
# Find IDs to add (in new but not in current)
ids_to_add = new_related_ids - current_related_ids
# Soft delete removed relations
if ids_to_delete:
session.query(AgentRelation).filter(
AgentRelation.parent_agent_id == parent_agent_id,
AgentRelation.selected_agent_id.in_(ids_to_delete),
AgentRelation.tenant_id == tenant_id,
AgentRelation.version_no == version_no
).update(
{AgentRelation.delete_flag: 'Y', 'updated_by': user_id},
synchronize_session=False
)
# Add new relations
for child_agent_id in ids_to_add:
relation_info = {
"parent_agent_id": parent_agent_id,
"selected_agent_id": child_agent_id,
"tenant_id": tenant_id,
"version_no": version_no,
"created_by": user_id,
"updated_by": user_id
}
new_relation = AgentRelation(
**filter_property(relation_info, AgentRelation))
session.add(new_relation)
def delete_agent_relationship(agent_id: int, tenant_id: str, user_id: str, version_no: int = 0):
"""
Delete all relationships for an agent.
Default version_no=0 deletes the draft version.
Args:
agent_id: Agent ID
tenant_id: Tenant ID
user_id: User ID
version_no: Version number to filter. Default 0 = draft/editing state
"""
with get_db_session() as session:
session.query(AgentRelation).filter(
AgentRelation.parent_agent_id == agent_id,
AgentRelation.tenant_id == tenant_id,
AgentRelation.version_no == version_no
).update(
{AgentRelation.delete_flag: 'Y', 'updated_by': user_id})
session.query(AgentRelation).filter(
AgentRelation.selected_agent_id == agent_id,
AgentRelation.tenant_id == tenant_id,
AgentRelation.version_no == version_no
).update(
{AgentRelation.delete_flag: 'Y', 'updated_by': user_id})