@inkeep/agents-core 0.58.14 → 0.58.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/dist/auth/auth-schema.d.ts +85 -85
  2. package/dist/auth/auth-validation-schemas.d.ts +135 -135
  3. package/dist/auth/auth.d.ts +9 -9
  4. package/dist/auth/auth.js +17 -45
  5. package/dist/auth/permissions.d.ts +9 -9
  6. package/dist/data-access/index.d.ts +4 -1
  7. package/dist/data-access/index.js +4 -1
  8. package/dist/data-access/manage/agentFull.js +28 -12
  9. package/dist/data-access/manage/agents.d.ts +25 -24
  10. package/dist/data-access/manage/agents.js +33 -33
  11. package/dist/data-access/manage/artifactComponents.d.ts +14 -14
  12. package/dist/data-access/manage/artifactComponents.js +14 -13
  13. package/dist/data-access/manage/audit-queries.js +6 -5
  14. package/dist/data-access/manage/contextConfigs.d.ts +12 -12
  15. package/dist/data-access/manage/contextConfigs.js +7 -6
  16. package/dist/data-access/manage/credentialReferences.js +12 -11
  17. package/dist/data-access/manage/dataComponents.d.ts +8 -8
  18. package/dist/data-access/manage/dataComponents.js +13 -11
  19. package/dist/data-access/manage/evalConfig.js +42 -41
  20. package/dist/data-access/manage/externalAgents.js +8 -7
  21. package/dist/data-access/manage/functionTools.d.ts +19 -23
  22. package/dist/data-access/manage/functionTools.js +27 -65
  23. package/dist/data-access/manage/functions.js +7 -10
  24. package/dist/data-access/manage/projects.js +37 -36
  25. package/dist/data-access/manage/scheduledTriggers.js +10 -6
  26. package/dist/data-access/manage/scheduledWorkflows.js +3 -2
  27. package/dist/data-access/manage/scope-helpers.d.ts +2 -1
  28. package/dist/data-access/manage/scope-helpers.js +2 -1
  29. package/dist/data-access/manage/skills.d.ts +17 -17
  30. package/dist/data-access/manage/skills.js +14 -9
  31. package/dist/data-access/manage/subAgentExternalAgentRelations.d.ts +24 -24
  32. package/dist/data-access/manage/subAgentRelations.d.ts +34 -34
  33. package/dist/data-access/manage/subAgentRelations.js +28 -26
  34. package/dist/data-access/manage/subAgentTeamAgentRelations.d.ts +24 -24
  35. package/dist/data-access/manage/subAgentTeamAgentRelations.js +16 -16
  36. package/dist/data-access/manage/subAgents.d.ts +12 -12
  37. package/dist/data-access/manage/subAgents.js +8 -7
  38. package/dist/data-access/manage/tools.d.ts +24 -24
  39. package/dist/data-access/manage/tools.js +8 -6
  40. package/dist/data-access/manage/triggers.js +12 -8
  41. package/dist/data-access/runtime/apiKeys.d.ts +16 -16
  42. package/dist/data-access/runtime/apiKeys.js +16 -12
  43. package/dist/data-access/runtime/apps.d.ts +8 -8
  44. package/dist/data-access/runtime/apps.js +8 -7
  45. package/dist/data-access/runtime/audit-queries.js +7 -7
  46. package/dist/data-access/runtime/auth.d.ts +18 -0
  47. package/dist/data-access/runtime/auth.js +35 -0
  48. package/dist/data-access/runtime/cascade-delete.js +29 -24
  49. package/dist/data-access/runtime/contextCache.d.ts +1 -0
  50. package/dist/data-access/runtime/contextCache.js +9 -8
  51. package/dist/data-access/runtime/conversations.d.ts +31 -31
  52. package/dist/data-access/runtime/conversations.js +8 -12
  53. package/dist/data-access/runtime/evalRuns.js +23 -22
  54. package/dist/data-access/runtime/github-work-app-installations.js +32 -21
  55. package/dist/data-access/runtime/ledgerArtifacts.js +9 -24
  56. package/dist/data-access/runtime/messages.d.ts +24 -24
  57. package/dist/data-access/runtime/messages.js +9 -8
  58. package/dist/data-access/runtime/projects.js +6 -5
  59. package/dist/data-access/runtime/scheduledTriggerInvocations.d.ts +3 -3
  60. package/dist/data-access/runtime/scheduledTriggerInvocations.js +16 -26
  61. package/dist/data-access/runtime/slack-work-app-mcp.js +6 -5
  62. package/dist/data-access/runtime/tasks.d.ts +12 -8
  63. package/dist/data-access/runtime/tasks.js +4 -5
  64. package/dist/data-access/runtime/triggerInvocations.js +4 -8
  65. package/dist/data-access/runtime/workAppSlack.js +21 -14
  66. package/dist/db/manage/manage-schema.d.ts +449 -449
  67. package/dist/db/runtime/runtime-schema.d.ts +332 -332
  68. package/dist/index.d.ts +6 -2
  69. package/dist/index.js +7 -1
  70. package/dist/retry/index.d.ts +3 -0
  71. package/dist/retry/index.js +4 -0
  72. package/dist/retry/retryable-errors.d.ts +10 -0
  73. package/dist/retry/retryable-errors.js +72 -0
  74. package/dist/retry/withRetry.d.ts +15 -0
  75. package/dist/retry/withRetry.js +37 -0
  76. package/dist/validation/dolt-schemas.d.ts +1 -1
  77. package/dist/validation/schemas.d.ts +412 -412
  78. package/package.json +1 -1
@@ -53,14 +53,14 @@ declare const createFunctionTool: (db: AgentsManageDatabaseClient) => (params: {
53
53
  data: FunctionToolApiInsert;
54
54
  scopes: AgentScopeConfig;
55
55
  }) => Promise<{
56
+ tenantId: string;
57
+ projectId: string;
58
+ agentId: string;
56
59
  id: string;
57
60
  name: string;
61
+ description: string | null;
58
62
  createdAt: string;
59
63
  updatedAt: string;
60
- description: string | null;
61
- projectId: string;
62
- tenantId: string;
63
- agentId: string;
64
64
  functionId: string;
65
65
  }>;
66
66
  /**
@@ -95,22 +95,18 @@ declare const upsertFunctionTool: (db: AgentsManageDatabaseClient) => (params: {
95
95
  data: FunctionToolApiInsert;
96
96
  scopes: AgentScopeConfig;
97
97
  }) => Promise<{
98
+ tenantId: string;
99
+ projectId: string;
100
+ agentId: string;
98
101
  id: string;
99
102
  name: string;
103
+ description: string | null;
100
104
  createdAt: string;
101
105
  updatedAt: string;
102
- description: string | null;
103
- projectId: string;
104
- tenantId: string;
105
- agentId: string;
106
106
  functionId: string;
107
107
  }>;
108
108
  declare const getFunctionToolsForSubAgent: (db: AgentsManageDatabaseClient) => (params: {
109
- scopes: {
110
- tenantId: string;
111
- projectId: string;
112
- agentId: string;
113
- };
109
+ scopes: AgentScopeConfig;
114
110
  subAgentId: string;
115
111
  pagination?: PaginationConfig;
116
112
  }) => Promise<{
@@ -161,17 +157,17 @@ declare const addFunctionToolToSubAgent: (db: AgentsManageDatabaseClient) => (pa
161
157
  needsApproval?: boolean;
162
158
  }> | null;
163
159
  }) => Promise<{
160
+ tenantId: string;
161
+ projectId: string;
162
+ agentId: string;
163
+ subAgentId: string;
164
164
  id: string;
165
165
  createdAt: string;
166
166
  updatedAt: string;
167
- subAgentId: string;
168
- projectId: string;
169
- tenantId: string;
170
- agentId: string;
171
- functionToolId: string;
172
167
  toolPolicies: Record<string, {
173
168
  needsApproval?: boolean;
174
169
  }> | null;
170
+ functionToolId: string;
175
171
  }>;
176
172
  /**
177
173
  * Update an agent-function tool relation
@@ -226,17 +222,17 @@ declare const associateFunctionToolWithSubAgent: (db: AgentsManageDatabaseClient
226
222
  needsApproval?: boolean;
227
223
  }> | null;
228
224
  }) => Promise<{
225
+ tenantId: string;
226
+ projectId: string;
227
+ agentId: string;
228
+ subAgentId: string;
229
229
  id: string;
230
230
  createdAt: string;
231
231
  updatedAt: string;
232
- subAgentId: string;
233
- projectId: string;
234
- tenantId: string;
235
- agentId: string;
236
- functionToolId: string;
237
232
  toolPolicies: Record<string, {
238
233
  needsApproval?: boolean;
239
234
  }> | null;
235
+ functionToolId: string;
240
236
  }>;
241
237
  //#endregion
242
238
  export { addFunctionToolToSubAgent, associateFunctionToolWithSubAgent, createFunctionTool, deleteFunctionTool, getFunctionToolById, getFunctionToolsForSubAgent, getSubAgentsUsingFunctionTool, isFunctionToolAssociatedWithSubAgent, listFunctionTools, removeFunctionToolFromSubAgent, updateFunctionTool, updateSubAgentFunctionToolRelation, upsertFunctionTool, upsertSubAgentFunctionToolRelation };
@@ -1,6 +1,7 @@
1
1
  import { functionTools, subAgentFunctionToolRelations } from "../../db/manage/manage-schema.js";
2
2
  import { getLogger } from "../../utils/logger.js";
3
3
  import { generateId } from "../../utils/conversations.js";
4
+ import { agentScopedWhere } from "./scope-helpers.js";
4
5
  import { and, count, desc, eq } from "drizzle-orm";
5
6
 
6
7
  //#region src/data-access/manage/functionTools.ts
@@ -9,7 +10,7 @@ const logger = getLogger("functionTools");
9
10
  * Get a function tool by ID (agent-scoped)
10
11
  */
11
12
  const getFunctionToolById = (db) => async (params) => {
12
- return (await db.select().from(functionTools).where(and(eq(functionTools.tenantId, params.scopes.tenantId), eq(functionTools.projectId, params.scopes.projectId), eq(functionTools.agentId, params.scopes.agentId), eq(functionTools.id, params.functionToolId))).limit(1))[0] ?? null;
13
+ return (await db.select().from(functionTools).where(and(agentScopedWhere(functionTools, params.scopes), eq(functionTools.id, params.functionToolId))).limit(1))[0] ?? null;
13
14
  };
14
15
  /**
15
16
  * List function tools (agent-scoped)
@@ -18,7 +19,7 @@ const listFunctionTools = (db) => async (params) => {
18
19
  const page = params.pagination?.page || 1;
19
20
  const limit = Math.min(params.pagination?.limit || 10, 100);
20
21
  const offset = (page - 1) * limit;
21
- const whereClause = and(eq(functionTools.tenantId, params.scopes.tenantId), eq(functionTools.projectId, params.scopes.projectId), eq(functionTools.agentId, params.scopes.agentId));
22
+ const whereClause = agentScopedWhere(functionTools, params.scopes);
22
23
  const [functionToolsDbResults, totalResult] = await Promise.all([db.select().from(functionTools).where(whereClause).limit(limit).offset(offset).orderBy(desc(functionTools.createdAt)), db.select({ count: count() }).from(functionTools).where(whereClause)]);
23
24
  const total = totalResult[0]?.count || 0;
24
25
  return {
@@ -36,11 +37,8 @@ const listFunctionTools = (db) => async (params) => {
36
37
  */
37
38
  const createFunctionTool = (db) => async (params) => {
38
39
  const { data, scopes } = params;
39
- const { tenantId, projectId, agentId } = scopes;
40
40
  const [created] = await db.insert(functionTools).values({
41
- tenantId,
42
- projectId,
43
- agentId,
41
+ ...scopes,
44
42
  id: data.id,
45
43
  name: data.name,
46
44
  description: data.description,
@@ -58,30 +56,25 @@ const updateFunctionTool = (db) => async (params) => {
58
56
  const [updated] = await db.update(functionTools).set({
59
57
  ...params.data,
60
58
  updatedAt: now
61
- }).where(and(eq(functionTools.tenantId, params.scopes.tenantId), eq(functionTools.projectId, params.scopes.projectId), eq(functionTools.agentId, params.scopes.agentId), eq(functionTools.id, params.functionToolId))).returning();
59
+ }).where(and(agentScopedWhere(functionTools, params.scopes), eq(functionTools.id, params.functionToolId))).returning();
62
60
  return updated ?? null;
63
61
  };
64
62
  /**
65
63
  * Delete a function tool (agent-scoped)
66
64
  */
67
65
  const deleteFunctionTool = (db) => async (params) => {
68
- const [deleted] = await db.delete(functionTools).where(and(eq(functionTools.tenantId, params.scopes.tenantId), eq(functionTools.projectId, params.scopes.projectId), eq(functionTools.agentId, params.scopes.agentId), eq(functionTools.id, params.functionToolId))).returning();
66
+ const [deleted] = await db.delete(functionTools).where(and(agentScopedWhere(functionTools, params.scopes), eq(functionTools.id, params.functionToolId))).returning();
69
67
  return !!deleted;
70
68
  };
71
69
  /**
72
70
  * Upsert a function tool (create if it doesn't exist, update if it does)
73
71
  */
74
72
  const upsertFunctionTool = (db) => async (params) => {
75
- const scopes = {
76
- tenantId: params.scopes.tenantId,
77
- projectId: params.scopes.projectId,
78
- agentId: params.scopes.agentId
79
- };
80
73
  if (await getFunctionToolById(db)({
81
- scopes,
74
+ scopes: params.scopes,
82
75
  functionToolId: params.data.id
83
76
  })) return await updateFunctionTool(db)({
84
- scopes,
77
+ scopes: params.scopes,
85
78
  functionToolId: params.data.id,
86
79
  data: {
87
80
  name: params.data.name,
@@ -91,7 +84,7 @@ const upsertFunctionTool = (db) => async (params) => {
91
84
  });
92
85
  return await createFunctionTool(db)({
93
86
  data: params.data,
94
- scopes
87
+ scopes: params.scopes
95
88
  });
96
89
  };
97
90
  const getFunctionToolsForSubAgent = (db) => {
@@ -99,9 +92,8 @@ const getFunctionToolsForSubAgent = (db) => {
99
92
  const page = params.pagination?.page || 1;
100
93
  const limit = Math.min(params.pagination?.limit || 1e3, 1e3);
101
94
  const offset = (page - 1) * limit;
102
- const { tenantId, projectId, agentId } = params.scopes;
103
95
  try {
104
- const whereClause = and(eq(subAgentFunctionToolRelations.tenantId, tenantId), eq(subAgentFunctionToolRelations.projectId, projectId), eq(subAgentFunctionToolRelations.agentId, agentId), eq(subAgentFunctionToolRelations.subAgentId, params.subAgentId));
96
+ const whereClause = and(agentScopedWhere(subAgentFunctionToolRelations, params.scopes), eq(subAgentFunctionToolRelations.subAgentId, params.subAgentId));
105
97
  const [data, totalResult] = await Promise.all([db.select({
106
98
  id: functionTools.id,
107
99
  name: functionTools.name,
@@ -127,9 +119,7 @@ const getFunctionToolsForSubAgent = (db) => {
127
119
  };
128
120
  } catch (error) {
129
121
  logger.error({
130
- tenantId,
131
- projectId,
132
- agentId,
122
+ ...params.scopes,
133
123
  subAgentId: params.subAgentId,
134
124
  error
135
125
  }, "Failed to get function tools for agent");
@@ -142,7 +132,6 @@ const getFunctionToolsForSubAgent = (db) => {
142
132
  */
143
133
  const upsertSubAgentFunctionToolRelation = (db) => async (params) => {
144
134
  const { scopes, subAgentId, functionToolId, toolPolicies, relationId } = params;
145
- const { tenantId, projectId, agentId } = scopes;
146
135
  if (relationId) return await updateSubAgentFunctionToolRelation(db)({
147
136
  scopes,
148
137
  relationId,
@@ -153,12 +142,10 @@ const upsertSubAgentFunctionToolRelation = (db) => async (params) => {
153
142
  }
154
143
  });
155
144
  try {
156
- const existingRelations = await db.select().from(subAgentFunctionToolRelations).where(and(eq(subAgentFunctionToolRelations.tenantId, tenantId), eq(subAgentFunctionToolRelations.projectId, projectId), eq(subAgentFunctionToolRelations.agentId, agentId), eq(subAgentFunctionToolRelations.subAgentId, subAgentId), eq(subAgentFunctionToolRelations.functionToolId, functionToolId))).limit(1);
145
+ const existingRelations = await db.select().from(subAgentFunctionToolRelations).where(and(agentScopedWhere(subAgentFunctionToolRelations, scopes), eq(subAgentFunctionToolRelations.subAgentId, subAgentId), eq(subAgentFunctionToolRelations.functionToolId, functionToolId))).limit(1);
157
146
  if (existingRelations.length > 0) {
158
147
  logger.info({
159
- tenantId,
160
- projectId,
161
- agentId,
148
+ ...scopes,
162
149
  subAgentId,
163
150
  functionToolId,
164
151
  relationId: existingRelations[0].id
@@ -168,9 +155,7 @@ const upsertSubAgentFunctionToolRelation = (db) => async (params) => {
168
155
  return await addFunctionToolToSubAgent(db)(params);
169
156
  } catch (error) {
170
157
  logger.error({
171
- tenantId,
172
- projectId,
173
- agentId,
158
+ ...scopes,
174
159
  subAgentId,
175
160
  functionToolId,
176
161
  error
@@ -184,22 +169,17 @@ const upsertSubAgentFunctionToolRelation = (db) => async (params) => {
184
169
  const addFunctionToolToSubAgent = (db) => {
185
170
  return async (params) => {
186
171
  const { scopes, subAgentId, functionToolId, toolPolicies } = params;
187
- const { tenantId, projectId, agentId } = scopes;
188
172
  try {
189
173
  const relationId = generateId();
190
174
  const [result] = await db.insert(subAgentFunctionToolRelations).values({
191
175
  id: relationId,
192
- tenantId,
193
- projectId,
194
- agentId,
176
+ ...scopes,
195
177
  subAgentId,
196
178
  functionToolId,
197
179
  ...toolPolicies !== void 0 ? { toolPolicies } : {}
198
180
  }).returning();
199
181
  logger.info({
200
- tenantId,
201
- projectId,
202
- agentId,
182
+ ...scopes,
203
183
  subAgentId,
204
184
  functionToolId,
205
185
  relationId
@@ -207,9 +187,7 @@ const addFunctionToolToSubAgent = (db) => {
207
187
  return result;
208
188
  } catch (error) {
209
189
  logger.error({
210
- tenantId,
211
- projectId,
212
- agentId,
190
+ ...scopes,
213
191
  subAgentId,
214
192
  functionToolId,
215
193
  error
@@ -224,26 +202,21 @@ const addFunctionToolToSubAgent = (db) => {
224
202
  const updateSubAgentFunctionToolRelation = (db) => {
225
203
  return async (params) => {
226
204
  const { scopes, relationId, data } = params;
227
- const { tenantId, projectId, agentId } = scopes;
228
205
  try {
229
206
  await db.update(subAgentFunctionToolRelations).set({
230
207
  subAgentId: data.subAgentId,
231
208
  functionToolId: data.functionToolId,
232
209
  ...data.toolPolicies !== void 0 ? { toolPolicies: data.toolPolicies } : {}
233
- }).where(and(eq(subAgentFunctionToolRelations.id, relationId), eq(subAgentFunctionToolRelations.tenantId, tenantId), eq(subAgentFunctionToolRelations.projectId, projectId), eq(subAgentFunctionToolRelations.agentId, agentId)));
210
+ }).where(and(agentScopedWhere(subAgentFunctionToolRelations, scopes), eq(subAgentFunctionToolRelations.id, relationId)));
234
211
  logger.info({
235
- tenantId,
236
- projectId,
237
- agentId,
212
+ ...scopes,
238
213
  relationId,
239
214
  data
240
215
  }, "SubAgent-function tool relation updated");
241
216
  return { id: relationId };
242
217
  } catch (error) {
243
218
  logger.error({
244
- tenantId,
245
- projectId,
246
- agentId,
219
+ ...scopes,
247
220
  relationId,
248
221
  data,
249
222
  error
@@ -258,17 +231,14 @@ const updateSubAgentFunctionToolRelation = (db) => {
258
231
  const getSubAgentsUsingFunctionTool = (db) => {
259
232
  return async (params) => {
260
233
  const { scopes, functionToolId } = params;
261
- const { tenantId, projectId, agentId } = scopes;
262
234
  try {
263
235
  return await db.select({
264
236
  subAgentId: subAgentFunctionToolRelations.subAgentId,
265
237
  createdAt: subAgentFunctionToolRelations.createdAt
266
- }).from(subAgentFunctionToolRelations).where(and(eq(subAgentFunctionToolRelations.tenantId, tenantId), eq(subAgentFunctionToolRelations.projectId, projectId), eq(subAgentFunctionToolRelations.agentId, agentId), eq(subAgentFunctionToolRelations.functionToolId, functionToolId)));
238
+ }).from(subAgentFunctionToolRelations).where(and(agentScopedWhere(subAgentFunctionToolRelations, scopes), eq(subAgentFunctionToolRelations.functionToolId, functionToolId)));
267
239
  } catch (error) {
268
240
  logger.error({
269
- tenantId,
270
- projectId,
271
- agentId,
241
+ ...scopes,
272
242
  functionToolId,
273
243
  error
274
244
  }, "Failed to get sub-agents using function tool");
@@ -282,22 +252,17 @@ const getSubAgentsUsingFunctionTool = (db) => {
282
252
  const removeFunctionToolFromSubAgent = (db) => {
283
253
  return async (params) => {
284
254
  const { scopes, subAgentId, functionToolId } = params;
285
- const { tenantId, projectId, agentId } = scopes;
286
255
  try {
287
- const removed = (await db.delete(subAgentFunctionToolRelations).where(and(eq(subAgentFunctionToolRelations.tenantId, tenantId), eq(subAgentFunctionToolRelations.projectId, projectId), eq(subAgentFunctionToolRelations.agentId, agentId), eq(subAgentFunctionToolRelations.subAgentId, subAgentId), eq(subAgentFunctionToolRelations.functionToolId, functionToolId))).returning()).length > 0;
256
+ const removed = (await db.delete(subAgentFunctionToolRelations).where(and(agentScopedWhere(subAgentFunctionToolRelations, scopes), eq(subAgentFunctionToolRelations.subAgentId, subAgentId), eq(subAgentFunctionToolRelations.functionToolId, functionToolId))).returning()).length > 0;
288
257
  if (removed) logger.info({
289
- tenantId,
290
- projectId,
291
- agentId,
258
+ ...scopes,
292
259
  subAgentId,
293
260
  functionToolId
294
261
  }, "Function tool removed from sub-agent");
295
262
  return removed;
296
263
  } catch (error) {
297
264
  logger.error({
298
- tenantId,
299
- projectId,
300
- agentId,
265
+ ...scopes,
301
266
  subAgentId,
302
267
  functionToolId,
303
268
  error
@@ -312,14 +277,11 @@ const removeFunctionToolFromSubAgent = (db) => {
312
277
  const isFunctionToolAssociatedWithSubAgent = (db) => {
313
278
  return async (params) => {
314
279
  const { scopes, subAgentId, functionToolId } = params;
315
- const { tenantId, projectId, agentId } = scopes;
316
280
  try {
317
- return (await db.select({ id: subAgentFunctionToolRelations.id }).from(subAgentFunctionToolRelations).where(and(eq(subAgentFunctionToolRelations.tenantId, tenantId), eq(subAgentFunctionToolRelations.projectId, projectId), eq(subAgentFunctionToolRelations.agentId, agentId), eq(subAgentFunctionToolRelations.subAgentId, subAgentId), eq(subAgentFunctionToolRelations.functionToolId, functionToolId))).limit(1)).length > 0;
281
+ return (await db.select({ id: subAgentFunctionToolRelations.id }).from(subAgentFunctionToolRelations).where(and(agentScopedWhere(subAgentFunctionToolRelations, scopes), eq(subAgentFunctionToolRelations.subAgentId, subAgentId), eq(subAgentFunctionToolRelations.functionToolId, functionToolId))).limit(1)).length > 0;
318
282
  } catch (error) {
319
283
  logger.error({
320
- tenantId,
321
- projectId,
322
- agentId,
284
+ ...scopes,
323
285
  subAgentId,
324
286
  functionToolId,
325
287
  error
@@ -1,4 +1,5 @@
1
1
  import { functions } from "../../db/manage/manage-schema.js";
2
+ import { projectScopedWhere } from "./scope-helpers.js";
2
3
  import { and, count, desc, eq } from "drizzle-orm";
3
4
 
4
5
  //#region src/data-access/manage/functions.ts
@@ -13,12 +14,12 @@ const upsertFunction = (db) => async (params) => {
13
14
  const { autoDetectDependencies } = await import("../../utils/detectDependencies.js");
14
15
  dependencies = autoDetectDependencies(data.executeCode);
15
16
  }
16
- if ((await db.select().from(functions).where(and(eq(functions.tenantId, tenantId), eq(functions.projectId, projectId), eq(functions.id, data.id))).limit(1)).length > 0) await db.update(functions).set({
17
+ if ((await db.select().from(functions).where(and(projectScopedWhere(functions, scopes), eq(functions.id, data.id))).limit(1)).length > 0) await db.update(functions).set({
17
18
  inputSchema: data.inputSchema,
18
19
  executeCode: data.executeCode,
19
20
  dependencies,
20
21
  updatedAt: (/* @__PURE__ */ new Date()).toISOString()
21
- }).where(and(eq(functions.tenantId, tenantId), eq(functions.projectId, projectId), eq(functions.id, data.id)));
22
+ }).where(and(projectScopedWhere(functions, scopes), eq(functions.id, data.id)));
22
23
  else await db.insert(functions).values({
23
24
  tenantId,
24
25
  projectId,
@@ -35,27 +36,24 @@ const upsertFunction = (db) => async (params) => {
35
36
  */
36
37
  const getFunction = (db) => async (params) => {
37
38
  const { functionId, scopes } = params;
38
- const { tenantId, projectId } = scopes;
39
- return (await db.select().from(functions).where(and(eq(functions.tenantId, tenantId), eq(functions.projectId, projectId), eq(functions.id, functionId))).limit(1))[0] || null;
39
+ return (await db.select().from(functions).where(and(projectScopedWhere(functions, scopes), eq(functions.id, functionId))).limit(1))[0] || null;
40
40
  };
41
41
  /**
42
42
  * List all functions for a project
43
43
  */
44
44
  const listFunctions = (db) => async (params) => {
45
45
  const { scopes } = params;
46
- const { tenantId, projectId } = scopes;
47
- return await db.select().from(functions).where(and(eq(functions.tenantId, tenantId), eq(functions.projectId, projectId)));
46
+ return await db.select().from(functions).where(projectScopedWhere(functions, scopes));
48
47
  };
49
48
  /**
50
49
  * List all functions for a project with pagination
51
50
  */
52
51
  const listFunctionsPaginated = (db) => async (params) => {
53
52
  const { scopes, pagination } = params;
54
- const { tenantId, projectId } = scopes;
55
53
  const page = pagination?.page || 1;
56
54
  const limit = Math.min(pagination?.limit || 10, 100);
57
55
  const offset = (page - 1) * limit;
58
- const whereClause = and(eq(functions.tenantId, tenantId), eq(functions.projectId, projectId));
56
+ const whereClause = projectScopedWhere(functions, scopes);
59
57
  const [data, totalResult] = await Promise.all([db.select().from(functions).where(whereClause).limit(limit).offset(offset).orderBy(desc(functions.createdAt)), db.select({ count: count() }).from(functions).where(whereClause)]);
60
58
  const total = totalResult[0]?.count || 0;
61
59
  return {
@@ -73,8 +71,7 @@ const listFunctionsPaginated = (db) => async (params) => {
73
71
  */
74
72
  const deleteFunction = (db) => async (params) => {
75
73
  const { functionId, scopes } = params;
76
- const { tenantId, projectId } = scopes;
77
- await db.delete(functions).where(and(eq(functions.tenantId, tenantId), eq(functions.projectId, projectId), eq(functions.id, functionId)));
74
+ await db.delete(functions).where(and(projectScopedWhere(functions, scopes), eq(functions.id, functionId)));
78
75
  };
79
76
 
80
77
  //#endregion
@@ -1,4 +1,5 @@
1
1
  import { agents, artifactComponents, contextConfigs, credentialReferences, dataComponents, externalAgents, projects, subAgentArtifactComponents, subAgentDataComponents, subAgentRelations, subAgentToolRelations, subAgents, tools } from "../../db/manage/manage-schema.js";
2
+ import { projectScopedWhere, tenantScopedWhere } from "./scope-helpers.js";
2
3
  import { and, count, desc, eq, inArray } from "drizzle-orm";
3
4
 
4
5
  //#region src/data-access/manage/projects.ts
@@ -8,22 +9,23 @@ import { and, count, desc, eq, inArray } from "drizzle-orm";
8
9
  */
9
10
  const listProjects = (db) => async (params) => {
10
11
  if (params.projectIds !== void 0 && params.projectIds.length === 0) return [];
11
- const whereClause = params.projectIds ? and(eq(projects.tenantId, params.tenantId), inArray(projects.id, params.projectIds)) : eq(projects.tenantId, params.tenantId);
12
+ const tenantScope = { tenantId: params.tenantId };
13
+ const whereClause = params.projectIds ? and(tenantScopedWhere(projects, tenantScope), inArray(projects.id, params.projectIds)) : tenantScopedWhere(projects, tenantScope);
12
14
  const projectsFromTable = await db.select({ projectId: projects.id }).from(projects).where(whereClause);
13
15
  if (projectsFromTable.length > 0) return projectsFromTable.map((p) => ({ projectId: p.projectId }));
14
16
  const projectIdSets = await Promise.all([
15
- db.selectDistinct({ projectId: subAgents.projectId }).from(subAgents).where(eq(subAgents.tenantId, params.tenantId)),
16
- db.selectDistinct({ projectId: agents.projectId }).from(agents).where(eq(agents.tenantId, params.tenantId)),
17
- db.selectDistinct({ projectId: tools.projectId }).from(tools).where(eq(tools.tenantId, params.tenantId)),
18
- db.selectDistinct({ projectId: contextConfigs.projectId }).from(contextConfigs).where(eq(contextConfigs.tenantId, params.tenantId)),
19
- db.selectDistinct({ projectId: externalAgents.projectId }).from(externalAgents).where(eq(externalAgents.tenantId, params.tenantId)),
20
- db.selectDistinct({ projectId: subAgentRelations.projectId }).from(subAgentRelations).where(eq(subAgentRelations.tenantId, params.tenantId)),
21
- db.selectDistinct({ projectId: subAgentToolRelations.projectId }).from(subAgentToolRelations).where(eq(subAgentToolRelations.tenantId, params.tenantId)),
22
- db.selectDistinct({ projectId: subAgentDataComponents.projectId }).from(subAgentDataComponents).where(eq(subAgentDataComponents.tenantId, params.tenantId)),
23
- db.selectDistinct({ projectId: subAgentArtifactComponents.projectId }).from(subAgentArtifactComponents).where(eq(subAgentArtifactComponents.tenantId, params.tenantId)),
24
- db.selectDistinct({ projectId: dataComponents.projectId }).from(dataComponents).where(eq(dataComponents.tenantId, params.tenantId)),
25
- db.selectDistinct({ projectId: artifactComponents.projectId }).from(artifactComponents).where(eq(artifactComponents.tenantId, params.tenantId)),
26
- db.selectDistinct({ projectId: credentialReferences.projectId }).from(credentialReferences).where(eq(credentialReferences.tenantId, params.tenantId))
17
+ db.selectDistinct({ projectId: subAgents.projectId }).from(subAgents).where(tenantScopedWhere(subAgents, tenantScope)),
18
+ db.selectDistinct({ projectId: agents.projectId }).from(agents).where(tenantScopedWhere(agents, tenantScope)),
19
+ db.selectDistinct({ projectId: tools.projectId }).from(tools).where(tenantScopedWhere(tools, tenantScope)),
20
+ db.selectDistinct({ projectId: contextConfigs.projectId }).from(contextConfigs).where(tenantScopedWhere(contextConfigs, tenantScope)),
21
+ db.selectDistinct({ projectId: externalAgents.projectId }).from(externalAgents).where(tenantScopedWhere(externalAgents, tenantScope)),
22
+ db.selectDistinct({ projectId: subAgentRelations.projectId }).from(subAgentRelations).where(tenantScopedWhere(subAgentRelations, tenantScope)),
23
+ db.selectDistinct({ projectId: subAgentToolRelations.projectId }).from(subAgentToolRelations).where(tenantScopedWhere(subAgentToolRelations, tenantScope)),
24
+ db.selectDistinct({ projectId: subAgentDataComponents.projectId }).from(subAgentDataComponents).where(tenantScopedWhere(subAgentDataComponents, tenantScope)),
25
+ db.selectDistinct({ projectId: subAgentArtifactComponents.projectId }).from(subAgentArtifactComponents).where(tenantScopedWhere(subAgentArtifactComponents, tenantScope)),
26
+ db.selectDistinct({ projectId: dataComponents.projectId }).from(dataComponents).where(tenantScopedWhere(dataComponents, tenantScope)),
27
+ db.selectDistinct({ projectId: artifactComponents.projectId }).from(artifactComponents).where(tenantScopedWhere(artifactComponents, tenantScope)),
28
+ db.selectDistinct({ projectId: credentialReferences.projectId }).from(credentialReferences).where(tenantScopedWhere(credentialReferences, tenantScope))
27
29
  ]);
28
30
  const allProjectIds = /* @__PURE__ */ new Set();
29
31
  projectIdSets.forEach((results) => {
@@ -43,7 +45,8 @@ const listProjectsPaginated = (db) => async (params) => {
43
45
  const page = params.pagination?.page || 1;
44
46
  const limit = params.pagination?.limit || 10;
45
47
  const offset = (page - 1) * limit;
46
- const whereClause = params.projectIds ? and(eq(projects.tenantId, params.tenantId), inArray(projects.id, params.projectIds)) : eq(projects.tenantId, params.tenantId);
48
+ const tenantScope = { tenantId: params.tenantId };
49
+ const whereClause = params.projectIds ? and(tenantScopedWhere(projects, tenantScope), inArray(projects.id, params.projectIds)) : tenantScopedWhere(projects, tenantScope);
47
50
  const [data, totalResult] = await Promise.all([db.select().from(projects).where(whereClause).limit(limit).offset(offset).orderBy(desc(projects.createdAt)), db.select({ count: count() }).from(projects).where(whereClause)]);
48
51
  const total = totalResult[0]?.count || 0;
49
52
  return {
@@ -60,13 +63,12 @@ const listProjectsPaginated = (db) => async (params) => {
60
63
  * Get resource counts for a specific project
61
64
  */
62
65
  const getProjectResourceCounts = (db) => async (params) => {
63
- const whereClause = (table) => and(eq(table.tenantId, params.tenantId), eq(table.projectId, params.projectId));
64
66
  const [subAgentResults, agentResults, toolResults, contextConfigResults, externalAgentResults] = await Promise.all([
65
- db.select({ count: subAgents.id }).from(subAgents).where(whereClause(subAgents)),
66
- db.select({ count: agents.id }).from(agents).where(whereClause(agents)),
67
- db.select({ count: tools.id }).from(tools).where(whereClause(tools)),
68
- db.select({ count: contextConfigs.id }).from(contextConfigs).where(whereClause(contextConfigs)),
69
- db.select({ count: externalAgents.id }).from(externalAgents).where(whereClause(externalAgents))
67
+ db.select({ count: subAgents.id }).from(subAgents).where(projectScopedWhere(subAgents, params)),
68
+ db.select({ count: agents.id }).from(agents).where(projectScopedWhere(agents, params)),
69
+ db.select({ count: tools.id }).from(tools).where(projectScopedWhere(tools, params)),
70
+ db.select({ count: contextConfigs.id }).from(contextConfigs).where(projectScopedWhere(contextConfigs, params)),
71
+ db.select({ count: externalAgents.id }).from(externalAgents).where(projectScopedWhere(externalAgents, params))
70
72
  ]);
71
73
  return {
72
74
  subAgents: subAgentResults.length,
@@ -80,13 +82,12 @@ const getProjectResourceCounts = (db) => async (params) => {
80
82
  * Check if a project exists (has any resources)
81
83
  */
82
84
  const projectExists = (db) => async (params) => {
83
- const whereClause = (table) => and(eq(table.tenantId, params.tenantId), eq(table.projectId, params.projectId));
84
85
  const checks = [
85
- db.select({ id: subAgents.id }).from(subAgents).where(whereClause(subAgents)).limit(1),
86
- db.select({ id: agents.id }).from(agents).where(whereClause(agents)).limit(1),
87
- db.select({ id: tools.id }).from(tools).where(whereClause(tools)).limit(1),
88
- db.select({ id: contextConfigs.id }).from(contextConfigs).where(whereClause(contextConfigs)).limit(1),
89
- db.select({ id: externalAgents.id }).from(externalAgents).where(whereClause(externalAgents)).limit(1)
86
+ db.select({ id: subAgents.id }).from(subAgents).where(projectScopedWhere(subAgents, params)).limit(1),
87
+ db.select({ id: agents.id }).from(agents).where(projectScopedWhere(agents, params)).limit(1),
88
+ db.select({ id: tools.id }).from(tools).where(projectScopedWhere(tools, params)).limit(1),
89
+ db.select({ id: contextConfigs.id }).from(contextConfigs).where(projectScopedWhere(contextConfigs, params)).limit(1),
90
+ db.select({ id: externalAgents.id }).from(externalAgents).where(projectScopedWhere(externalAgents, params)).limit(1)
90
91
  ];
91
92
  return (await Promise.all(checks)).some((result) => result.length > 0);
92
93
  };
@@ -100,7 +101,7 @@ const countProjects = (db) => async (params) => {
100
101
  * Get a single project by ID
101
102
  */
102
103
  const getProject = (db) => async (params) => {
103
- return await db.query.projects.findFirst({ where: and(eq(projects.tenantId, params.scopes.tenantId), eq(projects.id, params.scopes.projectId)) }) || null;
104
+ return await db.query.projects.findFirst({ where: and(tenantScopedWhere(projects, params.scopes), eq(projects.id, params.scopes.projectId)) }) || null;
104
105
  };
105
106
  /**
106
107
  * Create a new project
@@ -119,11 +120,12 @@ const createProject = (db) => async (params) => {
119
120
  */
120
121
  const updateProject = (db) => async (params) => {
121
122
  const now = (/* @__PURE__ */ new Date()).toISOString();
122
- const currentProject = await db.query.projects.findFirst({ where: and(eq(projects.tenantId, params.scopes.tenantId), eq(projects.id, params.scopes.projectId)) });
123
+ const projectWhere = and(tenantScopedWhere(projects, params.scopes), eq(projects.id, params.scopes.projectId));
124
+ const currentProject = await db.query.projects.findFirst({ where: projectWhere });
123
125
  const [updated] = await db.update(projects).set({
124
126
  ...params.data,
125
127
  updatedAt: now
126
- }).where(and(eq(projects.tenantId, params.scopes.tenantId), eq(projects.id, params.scopes.projectId))).returning();
128
+ }).where(projectWhere).returning();
127
129
  if (updated && params.data.stopWhen !== void 0) try {
128
130
  await cascadeStopWhenUpdates(db, params.scopes, currentProject?.stopWhen, params.data.stopWhen);
129
131
  } catch (error) {
@@ -135,7 +137,7 @@ const updateProject = (db) => async (params) => {
135
137
  * Check if a project exists in the projects table
136
138
  */
137
139
  const projectExistsInTable = (db) => async (params) => {
138
- return (await db.select({ id: projects.id }).from(projects).where(and(eq(projects.tenantId, params.scopes.tenantId), eq(projects.id, params.scopes.projectId))).limit(1)).length > 0;
140
+ return (await db.select({ id: projects.id }).from(projects).where(and(tenantScopedWhere(projects, params.scopes), eq(projects.id, params.scopes.projectId))).limit(1)).length > 0;
139
141
  };
140
142
  /**
141
143
  * Check if a project has any resources (used before deletion)
@@ -148,16 +150,15 @@ const projectHasResources = (db) => async (params) => {
148
150
  */
149
151
  const deleteProject = (db) => async (params) => {
150
152
  if (!await projectExistsInTable(db)({ scopes: params.scopes })) return false;
151
- await db.delete(projects).where(and(eq(projects.tenantId, params.scopes.tenantId), eq(projects.id, params.scopes.projectId)));
153
+ await db.delete(projects).where(and(tenantScopedWhere(projects, params.scopes), eq(projects.id, params.scopes.projectId)));
152
154
  return true;
153
155
  };
154
156
  /**
155
157
  * Cascade stopWhen updates from project to Agents and Sub Agents
156
158
  */
157
159
  async function cascadeStopWhenUpdates(db, scopes, oldStopWhen, newStopWhen) {
158
- const { tenantId, projectId } = scopes;
159
160
  if (oldStopWhen?.transferCountIs !== newStopWhen?.transferCountIs) {
160
- const agentsToUpdate = await db.query.agents.findMany({ where: and(eq(agents.tenantId, tenantId), eq(agents.projectId, projectId)) });
161
+ const agentsToUpdate = await db.query.agents.findMany({ where: projectScopedWhere(agents, scopes) });
161
162
  for (const agent of agentsToUpdate) {
162
163
  const agentStopWhen = agent.stopWhen;
163
164
  if (!agentStopWhen?.transferCountIs || agentStopWhen.transferCountIs === oldStopWhen?.transferCountIs) {
@@ -168,12 +169,12 @@ async function cascadeStopWhenUpdates(db, scopes, oldStopWhen, newStopWhen) {
168
169
  await db.update(agents).set({
169
170
  stopWhen: updatedStopWhen,
170
171
  updatedAt: (/* @__PURE__ */ new Date()).toISOString()
171
- }).where(and(eq(agents.tenantId, tenantId), eq(agents.projectId, projectId), eq(agents.id, agent.id)));
172
+ }).where(and(projectScopedWhere(agents, scopes), eq(agents.id, agent.id)));
172
173
  }
173
174
  }
174
175
  }
175
176
  if (oldStopWhen?.stepCountIs !== newStopWhen?.stepCountIs) {
176
- const agentsToUpdate = await db.query.subAgents.findMany({ where: and(eq(subAgents.tenantId, tenantId), eq(subAgents.projectId, projectId)) });
177
+ const agentsToUpdate = await db.query.subAgents.findMany({ where: projectScopedWhere(subAgents, scopes) });
177
178
  for (const agent of agentsToUpdate) {
178
179
  const agentStopWhen = agent.stopWhen;
179
180
  if (!agentStopWhen?.stepCountIs || agentStopWhen.stepCountIs === oldStopWhen?.stepCountIs) {
@@ -184,7 +185,7 @@ async function cascadeStopWhenUpdates(db, scopes, oldStopWhen, newStopWhen) {
184
185
  await db.update(subAgents).set({
185
186
  stopWhen: updatedStopWhen,
186
187
  updatedAt: (/* @__PURE__ */ new Date()).toISOString()
187
- }).where(and(eq(subAgents.tenantId, tenantId), eq(subAgents.projectId, projectId), eq(subAgents.id, agent.id)));
188
+ }).where(and(projectScopedWhere(subAgents, scopes), eq(subAgents.id, agent.id)));
188
189
  }
189
190
  }
190
191
  }
@@ -1,4 +1,5 @@
1
1
  import { scheduledTriggers } from "../../db/manage/manage-schema.js";
2
+ import { agentScopedWhere, projectScopedWhere } from "./scope-helpers.js";
2
3
  import { and, count, desc, eq } from "drizzle-orm";
3
4
 
4
5
  //#region src/data-access/manage/scheduledTriggers.ts
@@ -7,7 +8,7 @@ import { and, count, desc, eq } from "drizzle-orm";
7
8
  */
8
9
  const getScheduledTriggerById = (db) => async (params) => {
9
10
  const { scopes, scheduledTriggerId } = params;
10
- return await db.query.scheduledTriggers.findFirst({ where: and(eq(scheduledTriggers.tenantId, scopes.tenantId), eq(scheduledTriggers.projectId, scopes.projectId), eq(scheduledTriggers.agentId, scopes.agentId), eq(scheduledTriggers.id, scheduledTriggerId)) });
11
+ return await db.query.scheduledTriggers.findFirst({ where: and(agentScopedWhere(scheduledTriggers, scopes), eq(scheduledTriggers.id, scheduledTriggerId)) });
11
12
  };
12
13
  /**
13
14
  * List scheduled triggers for an agent with pagination
@@ -16,7 +17,7 @@ const listScheduledTriggersPaginated = (db) => async (params) => {
16
17
  const page = params.pagination?.page || 1;
17
18
  const limit = Math.min(params.pagination?.limit || 10, 100);
18
19
  const offset = (page - 1) * limit;
19
- const whereClause = and(eq(scheduledTriggers.tenantId, params.scopes.tenantId), eq(scheduledTriggers.projectId, params.scopes.projectId), eq(scheduledTriggers.agentId, params.scopes.agentId));
20
+ const whereClause = agentScopedWhere(scheduledTriggers, params.scopes);
20
21
  const [data, totalResult] = await Promise.all([db.select().from(scheduledTriggers).where(whereClause).limit(limit).offset(offset).orderBy(desc(scheduledTriggers.createdAt)), db.select({ count: count() }).from(scheduledTriggers).where(whereClause)]);
21
22
  const total = totalResult[0]?.count || 0;
22
23
  return {
@@ -43,13 +44,13 @@ const updateScheduledTrigger = (db) => async (params) => {
43
44
  ...params.data,
44
45
  updatedAt: (/* @__PURE__ */ new Date()).toISOString()
45
46
  };
46
- return (await db.update(scheduledTriggers).set(updateData).where(and(eq(scheduledTriggers.tenantId, params.scopes.tenantId), eq(scheduledTriggers.projectId, params.scopes.projectId), eq(scheduledTriggers.agentId, params.scopes.agentId), eq(scheduledTriggers.id, params.scheduledTriggerId))).returning())[0];
47
+ return (await db.update(scheduledTriggers).set(updateData).where(and(agentScopedWhere(scheduledTriggers, params.scopes), eq(scheduledTriggers.id, params.scheduledTriggerId))).returning())[0];
47
48
  };
48
49
  /**
49
50
  * Delete a scheduled trigger (agent-scoped)
50
51
  */
51
52
  const deleteScheduledTrigger = (db) => async (params) => {
52
- await db.delete(scheduledTriggers).where(and(eq(scheduledTriggers.tenantId, params.scopes.tenantId), eq(scheduledTriggers.projectId, params.scopes.projectId), eq(scheduledTriggers.agentId, params.scopes.agentId), eq(scheduledTriggers.id, params.scheduledTriggerId)));
53
+ await db.delete(scheduledTriggers).where(and(agentScopedWhere(scheduledTriggers, params.scopes), eq(scheduledTriggers.id, params.scheduledTriggerId)));
53
54
  };
54
55
  /**
55
56
  * Upsert a scheduled trigger (create if it doesn't exist, update if it does)
@@ -70,13 +71,16 @@ const upsertScheduledTrigger = (db) => async (params) => {
70
71
  * Operates across all agents in the project (not agent-scoped).
71
72
  */
72
73
  const deleteScheduledTriggersByRunAsUserId = (db) => async (params) => {
73
- await db.delete(scheduledTriggers).where(and(eq(scheduledTriggers.tenantId, params.tenantId), eq(scheduledTriggers.projectId, params.projectId), eq(scheduledTriggers.runAsUserId, params.runAsUserId)));
74
+ await db.delete(scheduledTriggers).where(and(projectScopedWhere(scheduledTriggers, {
75
+ tenantId: params.tenantId,
76
+ projectId: params.projectId
77
+ }), eq(scheduledTriggers.runAsUserId, params.runAsUserId)));
74
78
  };
75
79
  /**
76
80
  * List all scheduled triggers for an agent (non-paginated, used by agentFull)
77
81
  */
78
82
  const listScheduledTriggers = (db) => async (params) => {
79
- return await db.select().from(scheduledTriggers).where(and(eq(scheduledTriggers.tenantId, params.scopes.tenantId), eq(scheduledTriggers.projectId, params.scopes.projectId), eq(scheduledTriggers.agentId, params.scopes.agentId)));
83
+ return await db.select().from(scheduledTriggers).where(agentScopedWhere(scheduledTriggers, params.scopes));
80
84
  };
81
85
 
82
86
  //#endregion