@inkeep/agents-core 0.43.0 → 0.44.0

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.d.ts +4 -2
  2. package/dist/auth/auth.js +4 -3
  3. package/dist/auth/authz/client.d.ts +0 -1
  4. package/dist/auth/authz/client.js +0 -1
  5. package/dist/auth/authz/config.d.ts +1 -7
  6. package/dist/auth/authz/config.js +1 -9
  7. package/dist/auth/authz/index.d.ts +2 -2
  8. package/dist/auth/authz/index.js +2 -2
  9. package/dist/auth/authz/permissions.js +4 -8
  10. package/dist/auth/authz/sync.js +1 -11
  11. package/dist/auth/init.d.ts +1 -0
  12. package/dist/auth/init.js +115 -0
  13. package/dist/client-exports.d.ts +2 -6
  14. package/dist/client-exports.js +3 -7
  15. package/dist/constants/otel-attributes.d.ts +5 -0
  16. package/dist/constants/otel-attributes.js +8 -3
  17. package/dist/constants/signoz-queries.d.ts +1 -0
  18. package/dist/constants/signoz-queries.js +2 -1
  19. package/dist/data-access/index.d.ts +3 -2
  20. package/dist/data-access/index.js +3 -2
  21. package/dist/data-access/manage/agentFull.js +42 -1
  22. package/dist/data-access/manage/agents.d.ts +9 -9
  23. package/dist/data-access/manage/artifactComponents.d.ts +4 -4
  24. package/dist/data-access/manage/artifactComponents.js +1 -1
  25. package/dist/data-access/manage/contextConfigs.d.ts +12 -12
  26. package/dist/data-access/manage/dataComponents.d.ts +2 -2
  27. package/dist/data-access/manage/dataComponents.js +1 -1
  28. package/dist/data-access/manage/functionTools.d.ts +8 -8
  29. package/dist/data-access/manage/subAgentExternalAgentRelations.d.ts +12 -12
  30. package/dist/data-access/manage/subAgentRelations.d.ts +22 -22
  31. package/dist/data-access/manage/subAgentTeamAgentRelations.d.ts +12 -12
  32. package/dist/data-access/manage/subAgents.d.ts +3 -3
  33. package/dist/data-access/manage/tools.d.ts +26 -21
  34. package/dist/data-access/manage/tools.js +20 -1
  35. package/dist/data-access/runtime/apiKeys.d.ts +4 -4
  36. package/dist/data-access/runtime/cascade-delete.d.ts +48 -1
  37. package/dist/data-access/runtime/cascade-delete.js +52 -2
  38. package/dist/data-access/runtime/conversations.d.ts +19 -19
  39. package/dist/data-access/runtime/github-work-app-installations.d.ts +261 -0
  40. package/dist/data-access/runtime/github-work-app-installations.js +457 -0
  41. package/dist/data-access/runtime/messages.d.ts +15 -15
  42. package/dist/data-access/runtime/organizations.js +2 -2
  43. package/dist/data-access/runtime/tasks.d.ts +4 -4
  44. package/dist/db/manage/manage-schema.d.ts +318 -301
  45. package/dist/db/manage/manage-schema.js +1 -0
  46. package/dist/db/runtime/runtime-schema.d.ts +1009 -165
  47. package/dist/db/runtime/runtime-schema.js +173 -5
  48. package/dist/dolt/branch.js +1 -1
  49. package/dist/dolt/branches-api.js +1 -1
  50. package/dist/dolt/index.js +3 -3
  51. package/dist/dolt/migrate-all-branches.js +1 -1
  52. package/dist/dolt/ref-helpers.js +1 -1
  53. package/dist/dolt/ref-middleware.js +1 -1
  54. package/dist/dolt/ref-scope.js +1 -1
  55. package/dist/env.d.ts +6 -4
  56. package/dist/env.js +11 -10
  57. package/dist/index.d.ts +8 -9
  58. package/dist/index.js +18 -19
  59. package/dist/types/entities.d.ts +9 -2
  60. package/dist/types/index.d.ts +3 -3
  61. package/dist/types/utility.d.ts +16 -2
  62. package/dist/validation/agentFull.js +1 -1
  63. package/dist/validation/dolt-schemas.d.ts +1 -1
  64. package/dist/validation/drizzle-schema-helpers.d.ts +4 -23
  65. package/dist/validation/drizzle-schema-helpers.js +3 -30
  66. package/dist/validation/index.d.ts +2 -4
  67. package/dist/validation/index.js +4 -6
  68. package/dist/validation/schemas.d.ts +3233 -1440
  69. package/dist/validation/schemas.js +164 -79
  70. package/drizzle/manage/0006_fixed_umar.sql +1 -0
  71. package/drizzle/manage/meta/0006_snapshot.json +3148 -0
  72. package/drizzle/manage/meta/_journal.json +7 -0
  73. package/drizzle/runtime/0010_previous_black_knight.sql +84 -0
  74. package/drizzle/runtime/meta/0010_snapshot.json +3066 -0
  75. package/drizzle/runtime/meta/_journal.json +7 -0
  76. package/package.json +4 -2
  77. package/dist/validation/id-validation.d.ts +0 -24
  78. package/dist/validation/id-validation.js +0 -52
@@ -11,11 +11,11 @@ declare const getSubAgentTeamAgentRelationById: (db: AgentsManageDatabaseClient)
11
11
  id: string;
12
12
  createdAt: string;
13
13
  updatedAt: string;
14
- tenantId: string;
14
+ headers: Record<string, string> | null;
15
15
  agentId: string;
16
16
  projectId: string;
17
+ tenantId: string;
17
18
  subAgentId: string;
18
- headers: Record<string, string> | null;
19
19
  targetAgentId: string;
20
20
  } | undefined>;
21
21
  declare const listSubAgentTeamAgentRelations: (db: AgentsManageDatabaseClient) => (params: {
@@ -46,11 +46,11 @@ declare const getSubAgentTeamAgentRelations: (db: AgentsManageDatabaseClient) =>
46
46
  id: string;
47
47
  createdAt: string;
48
48
  updatedAt: string;
49
- tenantId: string;
49
+ headers: Record<string, string> | null;
50
50
  agentId: string;
51
51
  projectId: string;
52
+ tenantId: string;
52
53
  subAgentId: string;
53
- headers: Record<string, string> | null;
54
54
  targetAgentId: string;
55
55
  }[]>;
56
56
  declare const getSubAgentTeamAgentRelationsByAgent: (db: AgentsManageDatabaseClient) => (params: {
@@ -59,11 +59,11 @@ declare const getSubAgentTeamAgentRelationsByAgent: (db: AgentsManageDatabaseCli
59
59
  id: string;
60
60
  createdAt: string;
61
61
  updatedAt: string;
62
- tenantId: string;
62
+ headers: Record<string, string> | null;
63
63
  agentId: string;
64
64
  projectId: string;
65
+ tenantId: string;
65
66
  subAgentId: string;
66
- headers: Record<string, string> | null;
67
67
  targetAgentId: string;
68
68
  }[]>;
69
69
  declare const getSubAgentTeamAgentRelationsByTeamAgent: (db: AgentsManageDatabaseClient) => (params: {
@@ -212,11 +212,11 @@ declare const createSubAgentTeamAgentRelation: (db: AgentsManageDatabaseClient)
212
212
  id: string;
213
213
  createdAt: string;
214
214
  updatedAt: string;
215
- tenantId: string;
215
+ headers: Record<string, string> | null;
216
216
  agentId: string;
217
217
  projectId: string;
218
+ tenantId: string;
218
219
  subAgentId: string;
219
- headers: Record<string, string> | null;
220
220
  targetAgentId: string;
221
221
  }>;
222
222
  /**
@@ -229,11 +229,11 @@ declare const getSubAgentTeamAgentRelationByParams: (db: AgentsManageDatabaseCli
229
229
  id: string;
230
230
  createdAt: string;
231
231
  updatedAt: string;
232
- tenantId: string;
232
+ headers: Record<string, string> | null;
233
233
  agentId: string;
234
234
  projectId: string;
235
+ tenantId: string;
235
236
  subAgentId: string;
236
- headers: Record<string, string> | null;
237
237
  targetAgentId: string;
238
238
  } | undefined>;
239
239
  /**
@@ -250,11 +250,11 @@ declare const upsertSubAgentTeamAgentRelation: (db: AgentsManageDatabaseClient)
250
250
  id: string;
251
251
  createdAt: string;
252
252
  updatedAt: string;
253
- tenantId: string;
253
+ headers: Record<string, string> | null;
254
254
  agentId: string;
255
255
  projectId: string;
256
+ tenantId: string;
256
257
  subAgentId: string;
257
- headers: Record<string, string> | null;
258
258
  targetAgentId: string;
259
259
  }>;
260
260
  declare const updateSubAgentTeamAgentRelation: (db: AgentsManageDatabaseClient) => (params: {
@@ -13,9 +13,9 @@ declare const getSubAgentById: (db: AgentsManageDatabaseClient) => (params: {
13
13
  createdAt: string;
14
14
  updatedAt: string;
15
15
  description: string | null;
16
- tenantId: string;
17
16
  agentId: string;
18
17
  projectId: string;
18
+ tenantId: string;
19
19
  models: {
20
20
  base?: {
21
21
  model?: string | undefined;
@@ -44,9 +44,9 @@ declare const listSubAgents: (db: AgentsManageDatabaseClient) => (params: {
44
44
  createdAt: string;
45
45
  updatedAt: string;
46
46
  description: string | null;
47
- tenantId: string;
48
47
  agentId: string;
49
48
  projectId: string;
49
+ tenantId: string;
50
50
  models: {
51
51
  base?: {
52
52
  model?: string | undefined;
@@ -113,9 +113,9 @@ declare const createSubAgent: (db: AgentsManageDatabaseClient) => (params: SubAg
113
113
  createdAt: string;
114
114
  updatedAt: string;
115
115
  description: string | null;
116
- tenantId: string;
117
116
  agentId: string;
118
117
  projectId: string;
118
+ tenantId: string;
119
119
  models: {
120
120
  base?: {
121
121
  model?: string | undefined;
@@ -23,9 +23,9 @@ declare const getToolById: (db: AgentsManageDatabaseClient) => (params: {
23
23
  createdAt: string;
24
24
  updatedAt: string;
25
25
  description: string | null;
26
- tenantId: string;
27
- projectId: string;
28
26
  headers: Record<string, string> | null;
27
+ projectId: string;
28
+ tenantId: string;
29
29
  config: {
30
30
  type: "mcp";
31
31
  mcp: ToolMcpConfig;
@@ -35,6 +35,7 @@ declare const getToolById: (db: AgentsManageDatabaseClient) => (params: {
35
35
  imageUrl: string | null;
36
36
  capabilities: ToolServerCapabilities | null;
37
37
  lastError: string | null;
38
+ isWorkApp: boolean;
38
39
  } | null>;
39
40
  declare const getMcpToolById: (db: AgentsManageDatabaseClient) => (params: {
40
41
  scopes: ProjectScopeConfig;
@@ -62,6 +63,7 @@ declare const listTools: (db: AgentsManageDatabaseClient) => (params: {
62
63
  imageUrl: string | null;
63
64
  capabilities: ToolServerCapabilities | null;
64
65
  lastError: string | null;
66
+ isWorkApp: boolean;
65
67
  projectId: string;
66
68
  tenantId: string;
67
69
  id: string;
@@ -79,9 +81,9 @@ declare const createTool: (db: AgentsManageDatabaseClient) => (params: ToolInser
79
81
  createdAt: string;
80
82
  updatedAt: string;
81
83
  description: string | null;
82
- tenantId: string;
83
- projectId: string;
84
84
  headers: Record<string, string> | null;
85
+ projectId: string;
86
+ tenantId: string;
85
87
  config: {
86
88
  type: "mcp";
87
89
  mcp: ToolMcpConfig;
@@ -91,6 +93,7 @@ declare const createTool: (db: AgentsManageDatabaseClient) => (params: ToolInser
91
93
  imageUrl: string | null;
92
94
  capabilities: ToolServerCapabilities | null;
93
95
  lastError: string | null;
96
+ isWorkApp: boolean;
94
97
  }>;
95
98
  declare const updateTool: (db: AgentsManageDatabaseClient) => (params: {
96
99
  scopes: ProjectScopeConfig;
@@ -111,6 +114,7 @@ declare const updateTool: (db: AgentsManageDatabaseClient) => (params: {
111
114
  imageUrl: string | null;
112
115
  capabilities: ToolServerCapabilities | null;
113
116
  lastError: string | null;
117
+ isWorkApp: boolean;
114
118
  projectId: string;
115
119
  tenantId: string;
116
120
  id: string;
@@ -132,16 +136,16 @@ declare const addToolToAgent: (db: AgentsManageDatabaseClient) => (params: {
132
136
  id: string;
133
137
  createdAt: string;
134
138
  updatedAt: string;
135
- tenantId: string;
139
+ headers: Record<string, string> | null;
136
140
  agentId: string;
137
141
  projectId: string;
138
- subAgentId: string;
139
- headers: Record<string, string> | null;
140
- toolId: string;
141
- selectedTools: string[] | null;
142
+ tenantId: string;
142
143
  toolPolicies: Record<string, {
143
144
  needsApproval?: boolean;
144
145
  }> | null;
146
+ subAgentId: string;
147
+ toolId: string;
148
+ selectedTools: string[] | null;
145
149
  }>;
146
150
  declare const removeToolFromAgent: (db: AgentsManageDatabaseClient) => (params: {
147
151
  scopes: AgentScopeConfig;
@@ -151,16 +155,16 @@ declare const removeToolFromAgent: (db: AgentsManageDatabaseClient) => (params:
151
155
  id: string;
152
156
  createdAt: string;
153
157
  updatedAt: string;
154
- tenantId: string;
158
+ headers: Record<string, string> | null;
155
159
  agentId: string;
156
160
  projectId: string;
157
- subAgentId: string;
158
- headers: Record<string, string> | null;
159
- toolId: string;
160
- selectedTools: string[] | null;
161
+ tenantId: string;
161
162
  toolPolicies: Record<string, {
162
163
  needsApproval?: boolean;
163
164
  }> | null;
165
+ subAgentId: string;
166
+ toolId: string;
167
+ selectedTools: string[] | null;
164
168
  }>;
165
169
  /**
166
170
  * Upsert agent-tool relation (create if it doesn't exist, update if it does)
@@ -179,16 +183,16 @@ declare const upsertSubAgentToolRelation: (db: AgentsManageDatabaseClient) => (p
179
183
  id: string;
180
184
  createdAt: string;
181
185
  updatedAt: string;
182
- tenantId: string;
186
+ headers: Record<string, string> | null;
183
187
  agentId: string;
184
188
  projectId: string;
185
- subAgentId: string;
186
- headers: Record<string, string> | null;
187
- toolId: string;
188
- selectedTools: string[] | null;
189
+ tenantId: string;
189
190
  toolPolicies: Record<string, {
190
191
  needsApproval?: boolean;
191
192
  }> | null;
193
+ subAgentId: string;
194
+ toolId: string;
195
+ selectedTools: string[] | null;
192
196
  }>;
193
197
  /**
194
198
  * Upsert a tool (create if it doesn't exist, update if it does)
@@ -201,9 +205,9 @@ declare const upsertTool: (db: AgentsManageDatabaseClient) => (params: {
201
205
  createdAt: string;
202
206
  updatedAt: string;
203
207
  description: string | null;
204
- tenantId: string;
205
- projectId: string;
206
208
  headers: Record<string, string> | null;
209
+ projectId: string;
210
+ tenantId: string;
207
211
  config: {
208
212
  type: "mcp";
209
213
  mcp: ToolMcpConfig;
@@ -213,6 +217,7 @@ declare const upsertTool: (db: AgentsManageDatabaseClient) => (params: {
213
217
  imageUrl: string | null;
214
218
  capabilities: ToolServerCapabilities | null;
215
219
  lastError: string | null;
220
+ isWorkApp: boolean;
216
221
  }>;
217
222
  //#endregion
218
223
  export { addToolToAgent, createTool, dbResultToMcpTool, dbResultToMcpToolSkeleton, deleteTool, getMcpToolById, getToolById, listTools, removeToolFromAgent, updateTool, upsertSubAgentToolRelation, upsertTool };
@@ -1,17 +1,22 @@
1
1
  import { CredentialStoreType, MCPServerType, MCPTransportType } from "../../types/utility.js";
2
2
  import { subAgentToolRelations, tools } from "../../db/manage/manage-schema.js";
3
3
  import { detectAuthenticationRequired } from "../../utils/auth-detection.js";
4
+ import { env } from "../../env.js";
4
5
  import { getLogger } from "../../utils/logger.js";
5
6
  import { getCredentialStoreLookupKeyFromRetrievalParams } from "../../utils/credential-store-utils.js";
6
7
  import { CredentialStuffer } from "../../credential-stuffer/CredentialStuffer.js";
7
8
  import "../../credential-stuffer/index.js";
9
+ import { createAgentsRunDatabaseClient } from "../../db/runtime/runtime-client.js";
8
10
  import { generateId } from "../../utils/conversations.js";
9
11
  import { updateAgentToolRelation } from "./subAgentRelations.js";
12
+ import { getActiveBranch } from "../../dolt/schema-sync.js";
10
13
  import { toISODateString } from "../../utils/date.js";
11
14
  import { McpClient } from "../../utils/mcp-client.js";
12
15
  import { buildComposioMCPUrl } from "../../utils/third-party-mcp-servers/composio-client.js";
13
16
  import { isThirdPartyMCPServerAuthenticated } from "../../utils/third-party-mcp-servers/third-party-check.js";
14
17
  import "../../utils/index.js";
18
+ import { cascadeDeleteByTool } from "../runtime/cascade-delete.js";
19
+ import { isGithubWorkAppTool } from "../runtime/github-work-app-installations.js";
15
20
  import { getCredentialReference, getUserScopedCredentialReference } from "./credentialReferences.js";
16
21
  import { and, count, desc, eq } from "drizzle-orm";
17
22
  import { ErrorCode, McpError } from "@modelcontextprotocol/sdk/types.js";
@@ -114,6 +119,11 @@ const discoverToolsFromServer = async (tool, credentialReference, credentialStor
114
119
  sessionId: tool.config.mcp.transport?.sessionId
115
120
  };
116
121
  if (serverConfig.url) serverConfig.url = buildComposioMCPUrl(serverConfig.url.toString(), tool.tenantId, tool.projectId, tool.credentialScope === "user" ? "user" : "project", userId);
122
+ if (isGithubWorkAppTool(tool)) serverConfig.headers = {
123
+ ...serverConfig.headers,
124
+ "x-inkeep-tool-id": tool.id,
125
+ Authorization: `Bearer ${env.GITHUB_MCP_API_KEY}`
126
+ };
117
127
  const client = new McpClient({
118
128
  name: tool.name,
119
129
  server: serverConfig
@@ -307,7 +317,16 @@ const updateTool = (db) => async (params) => {
307
317
  };
308
318
  const deleteTool = (db) => async (params) => {
309
319
  const [deleted] = await db.delete(tools).where(and(eq(tools.tenantId, params.scopes.tenantId), eq(tools.projectId, params.scopes.projectId), eq(tools.id, params.toolId))).returning();
310
- return !!deleted;
320
+ if (!deleted) return false;
321
+ if (deleted.isWorkApp && deleted.config.mcp.server.url.includes("/github/mcp")) try {
322
+ if (await getActiveBranch(db)() === `${params.scopes.tenantId}_${params.scopes.projectId}_main`) await cascadeDeleteByTool(createAgentsRunDatabaseClient())({ toolId: params.toolId });
323
+ } catch (error) {
324
+ logger.debug({
325
+ error,
326
+ toolId: params.toolId
327
+ }, "Skipping cascade delete - active_branch() not available");
328
+ }
329
+ return true;
311
330
  };
312
331
  const addToolToAgent = (db) => async (params) => {
313
332
  const id = generateId();
@@ -12,9 +12,9 @@ declare const getApiKeyById: (db: AgentsRunDatabaseClient) => (params: {
12
12
  createdAt: string;
13
13
  updatedAt: string;
14
14
  expiresAt: string | null;
15
- tenantId: string;
16
15
  agentId: string;
17
16
  projectId: string;
17
+ tenantId: string;
18
18
  publicId: string;
19
19
  keyHash: string;
20
20
  keyPrefix: string;
@@ -26,9 +26,9 @@ declare const getApiKeyByPublicId: (db: AgentsRunDatabaseClient) => (publicId: s
26
26
  createdAt: string;
27
27
  updatedAt: string;
28
28
  expiresAt: string | null;
29
- tenantId: string;
30
29
  agentId: string;
31
30
  projectId: string;
31
+ tenantId: string;
32
32
  publicId: string;
33
33
  keyHash: string;
34
34
  keyPrefix: string;
@@ -43,9 +43,9 @@ declare const listApiKeys: (db: AgentsRunDatabaseClient) => (params: {
43
43
  createdAt: string;
44
44
  updatedAt: string;
45
45
  expiresAt: string | null;
46
- tenantId: string;
47
46
  agentId: string;
48
47
  projectId: string;
48
+ tenantId: string;
49
49
  publicId: string;
50
50
  keyHash: string;
51
51
  keyPrefix: string;
@@ -70,9 +70,9 @@ declare const createApiKey: (db: AgentsRunDatabaseClient) => (params: ApiKeyInse
70
70
  createdAt: string;
71
71
  updatedAt: string;
72
72
  expiresAt: string | null;
73
- tenantId: string;
74
73
  agentId: string;
75
74
  projectId: string;
75
+ tenantId: string;
76
76
  publicId: string;
77
77
  keyHash: string;
78
78
  keyPrefix: string;
@@ -73,5 +73,52 @@ declare const cascadeDeleteByContextConfig: (db: AgentsRunDatabaseClient) => (pa
73
73
  }) => Promise<{
74
74
  contextCacheDeleted: number;
75
75
  }>;
76
+ /**
77
+ * Result of a tool cascade delete operation
78
+ */
79
+ type ToolCascadeDeleteResult = {
80
+ mcpToolRepositoryAccessDeleted: number;
81
+ mcpToolAccessModeDeleted: boolean;
82
+ };
83
+ /**
84
+ * Delete all runtime entities for a specific MCP tool.
85
+ * Called when an MCP tool is deleted from the manage database.
86
+ *
87
+ * Cleans up:
88
+ * - workAppGitHubMcpToolRepositoryAccess entries
89
+ * - workAppGitHubMcpToolAccessMode entry
90
+ *
91
+ * @param db - Runtime database client
92
+ * @returns Function that performs the cascade delete
93
+ */
94
+ declare const cascadeDeleteByTool: (db: AgentsRunDatabaseClient) => (params: {
95
+ toolId: string;
96
+ }) => Promise<ToolCascadeDeleteResult>;
97
+ /**
98
+ * Result of a project cascade delete operation (GitHub access only)
99
+ */
100
+ type ProjectGitHubAccessCascadeDeleteResult = {
101
+ projectRepositoryAccessDeleted: number;
102
+ projectAccessModeDeleted: boolean;
103
+ mcpToolRepositoryAccessDeleted: number;
104
+ mcpToolAccessModesDeleted: number;
105
+ };
106
+ /**
107
+ * Delete all GitHub access runtime entities for a specific project.
108
+ * Called when a project is deleted from the manage database.
109
+ *
110
+ * Cleans up:
111
+ * - workAppGitHubProjectRepositoryAccess entries
112
+ * - workAppGitHubProjectAccessMode entry
113
+ * - workAppGitHubMcpToolRepositoryAccess entries (for tools in this project)
114
+ * - workAppGitHubMcpToolAccessMode entries (for tools in this project)
115
+ *
116
+ * @param db - Runtime database client
117
+ * @returns Function that performs the cascade delete
118
+ */
119
+ declare const cascadeDeleteGitHubAccessByProject: (db: AgentsRunDatabaseClient) => (params: {
120
+ tenantId: string;
121
+ projectId: string;
122
+ }) => Promise<ProjectGitHubAccessCascadeDeleteResult>;
76
123
  //#endregion
77
- export { CascadeDeleteResult, cascadeDeleteByAgent, cascadeDeleteByBranch, cascadeDeleteByContextConfig, cascadeDeleteByProject, cascadeDeleteBySubAgent };
124
+ export { CascadeDeleteResult, ProjectGitHubAccessCascadeDeleteResult, ToolCascadeDeleteResult, cascadeDeleteByAgent, cascadeDeleteByBranch, cascadeDeleteByContextConfig, cascadeDeleteByProject, cascadeDeleteBySubAgent, cascadeDeleteByTool, cascadeDeleteGitHubAccessByProject };
@@ -1,4 +1,4 @@
1
- import { apiKeys, contextCache, conversations, tasks } from "../../db/runtime/runtime-schema.js";
1
+ import { apiKeys, contextCache, conversations, tasks, workAppGitHubMcpToolAccessMode, workAppGitHubMcpToolRepositoryAccess, workAppGitHubProjectAccessMode, workAppGitHubProjectRepositoryAccess } from "../../db/runtime/runtime-schema.js";
2
2
  import { and, eq, inArray, sql } from "drizzle-orm";
3
3
 
4
4
  //#region src/data-access/runtime/cascade-delete.ts
@@ -35,6 +35,10 @@ const cascadeDeleteByProject = (db) => async (params) => {
35
35
  const conversationsResult = await db.delete(conversations).where(and(eq(conversations.tenantId, scopes.tenantId), eq(conversations.projectId, scopes.projectId), sql`${conversations.ref}->>'name' = ${fullBranchName}`)).returning();
36
36
  const tasksResult = await db.delete(tasks).where(and(eq(tasks.tenantId, scopes.tenantId), eq(tasks.projectId, scopes.projectId), sql`${tasks.ref}->>'name' = ${fullBranchName}`)).returning();
37
37
  const apiKeysResult = await db.delete(apiKeys).where(and(eq(apiKeys.tenantId, scopes.tenantId), eq(apiKeys.projectId, scopes.projectId))).returning();
38
+ await cascadeDeleteGitHubAccessByProject(db)({
39
+ tenantId: scopes.tenantId,
40
+ projectId: scopes.projectId
41
+ });
38
42
  return {
39
43
  conversationsDeleted: conversationsResult.length,
40
44
  tasksDeleted: tasksResult.length,
@@ -106,6 +110,52 @@ const cascadeDeleteByContextConfig = (db) => async (params) => {
106
110
  const { scopes, contextConfigId, fullBranchName } = params;
107
111
  return { contextCacheDeleted: (await db.delete(contextCache).where(and(eq(contextCache.tenantId, scopes.tenantId), eq(contextCache.projectId, scopes.projectId), eq(contextCache.contextConfigId, contextConfigId), sql`${contextCache.ref}->>'name' = ${fullBranchName}`)).returning()).length };
108
112
  };
113
+ /**
114
+ * Delete all runtime entities for a specific MCP tool.
115
+ * Called when an MCP tool is deleted from the manage database.
116
+ *
117
+ * Cleans up:
118
+ * - workAppGitHubMcpToolRepositoryAccess entries
119
+ * - workAppGitHubMcpToolAccessMode entry
120
+ *
121
+ * @param db - Runtime database client
122
+ * @returns Function that performs the cascade delete
123
+ */
124
+ const cascadeDeleteByTool = (db) => async (params) => {
125
+ const { toolId } = params;
126
+ const repositoryAccessResult = await db.delete(workAppGitHubMcpToolRepositoryAccess).where(eq(workAppGitHubMcpToolRepositoryAccess.toolId, toolId)).returning();
127
+ const accessModeResult = await db.delete(workAppGitHubMcpToolAccessMode).where(eq(workAppGitHubMcpToolAccessMode.toolId, toolId)).returning();
128
+ return {
129
+ mcpToolRepositoryAccessDeleted: repositoryAccessResult.length,
130
+ mcpToolAccessModeDeleted: accessModeResult.length > 0
131
+ };
132
+ };
133
+ /**
134
+ * Delete all GitHub access runtime entities for a specific project.
135
+ * Called when a project is deleted from the manage database.
136
+ *
137
+ * Cleans up:
138
+ * - workAppGitHubProjectRepositoryAccess entries
139
+ * - workAppGitHubProjectAccessMode entry
140
+ * - workAppGitHubMcpToolRepositoryAccess entries (for tools in this project)
141
+ * - workAppGitHubMcpToolAccessMode entries (for tools in this project)
142
+ *
143
+ * @param db - Runtime database client
144
+ * @returns Function that performs the cascade delete
145
+ */
146
+ const cascadeDeleteGitHubAccessByProject = (db) => async (params) => {
147
+ const { tenantId, projectId } = params;
148
+ const projectRepoAccessResult = await db.delete(workAppGitHubProjectRepositoryAccess).where(and(eq(workAppGitHubProjectRepositoryAccess.tenantId, tenantId), eq(workAppGitHubProjectRepositoryAccess.projectId, projectId))).returning();
149
+ const projectAccessModeResult = await db.delete(workAppGitHubProjectAccessMode).where(and(eq(workAppGitHubProjectAccessMode.tenantId, tenantId), eq(workAppGitHubProjectAccessMode.projectId, projectId))).returning();
150
+ const mcpToolRepoAccessResult = await db.delete(workAppGitHubMcpToolRepositoryAccess).where(and(eq(workAppGitHubMcpToolRepositoryAccess.tenantId, tenantId), eq(workAppGitHubMcpToolRepositoryAccess.projectId, projectId))).returning();
151
+ const mcpToolAccessModeResult = await db.delete(workAppGitHubMcpToolAccessMode).where(and(eq(workAppGitHubMcpToolAccessMode.tenantId, tenantId), eq(workAppGitHubMcpToolAccessMode.projectId, projectId))).returning();
152
+ return {
153
+ projectRepositoryAccessDeleted: projectRepoAccessResult.length,
154
+ projectAccessModeDeleted: projectAccessModeResult.length > 0,
155
+ mcpToolRepositoryAccessDeleted: mcpToolRepoAccessResult.length,
156
+ mcpToolAccessModesDeleted: mcpToolAccessModeResult.length
157
+ };
158
+ };
109
159
 
110
160
  //#endregion
111
- export { cascadeDeleteByAgent, cascadeDeleteByBranch, cascadeDeleteByContextConfig, cascadeDeleteByProject, cascadeDeleteBySubAgent };
161
+ export { cascadeDeleteByAgent, cascadeDeleteByBranch, cascadeDeleteByContextConfig, cascadeDeleteByProject, cascadeDeleteBySubAgent, cascadeDeleteByTool, cascadeDeleteGitHubAccessByProject };
@@ -19,18 +19,18 @@ declare const createConversation: (db: AgentsRunDatabaseClient) => (params: Conv
19
19
  createdAt: string;
20
20
  updatedAt: string;
21
21
  ref: {
22
- type: "commit" | "tag" | "branch";
22
+ type: "tag" | "commit" | "branch";
23
23
  name: string;
24
24
  hash: string;
25
25
  } | null;
26
26
  userId: string | null;
27
27
  metadata: ConversationMetadata | null;
28
- tenantId: string;
29
28
  agentId: string | null;
30
- activeSubAgentId: string;
29
+ projectId: string;
30
+ tenantId: string;
31
31
  title: string | null;
32
+ activeSubAgentId: string;
32
33
  lastContextResolution: string | null;
33
- projectId: string;
34
34
  }>;
35
35
  declare const updateConversation: (db: AgentsRunDatabaseClient) => (params: {
36
36
  scopes: ProjectScopeConfig;
@@ -43,7 +43,7 @@ declare const updateConversation: (db: AgentsRunDatabaseClient) => (params: {
43
43
  agentId: string | null;
44
44
  activeSubAgentId: string;
45
45
  ref: {
46
- type: "commit" | "tag" | "branch";
46
+ type: "tag" | "commit" | "branch";
47
47
  name: string;
48
48
  hash: string;
49
49
  } | null;
@@ -69,7 +69,7 @@ declare const updateConversationActiveSubAgent: (db: AgentsRunDatabaseClient) =>
69
69
  agentId: string | null;
70
70
  activeSubAgentId: string;
71
71
  ref: {
72
- type: "commit" | "tag" | "branch";
72
+ type: "tag" | "commit" | "branch";
73
73
  name: string;
74
74
  hash: string;
75
75
  } | null;
@@ -88,18 +88,18 @@ declare const getConversation: (db: AgentsRunDatabaseClient) => (params: {
88
88
  createdAt: string;
89
89
  updatedAt: string;
90
90
  ref: {
91
- type: "commit" | "tag" | "branch";
91
+ type: "tag" | "commit" | "branch";
92
92
  name: string;
93
93
  hash: string;
94
94
  } | null;
95
95
  userId: string | null;
96
96
  metadata: ConversationMetadata | null;
97
- tenantId: string;
98
97
  agentId: string | null;
99
- activeSubAgentId: string;
98
+ projectId: string;
99
+ tenantId: string;
100
100
  title: string | null;
101
+ activeSubAgentId: string;
101
102
  lastContextResolution: string | null;
102
- projectId: string;
103
103
  } | undefined>;
104
104
  declare const createOrGetConversation: (db: AgentsRunDatabaseClient) => (input: ConversationInsert) => Promise<{
105
105
  activeSubAgentId: string;
@@ -107,7 +107,7 @@ declare const createOrGetConversation: (db: AgentsRunDatabaseClient) => (input:
107
107
  tenantId: string;
108
108
  id: string;
109
109
  ref: {
110
- type: "commit" | "tag" | "branch";
110
+ type: "tag" | "commit" | "branch";
111
111
  name: string;
112
112
  hash: string;
113
113
  };
@@ -124,18 +124,18 @@ declare const createOrGetConversation: (db: AgentsRunDatabaseClient) => (input:
124
124
  createdAt: string;
125
125
  updatedAt: string;
126
126
  ref: {
127
- type: "commit" | "tag" | "branch";
127
+ type: "tag" | "commit" | "branch";
128
128
  name: string;
129
129
  hash: string;
130
130
  } | null;
131
131
  userId: string | null;
132
132
  metadata: ConversationMetadata | null;
133
- tenantId: string;
134
133
  agentId: string | null;
135
- activeSubAgentId: string;
134
+ projectId: string;
135
+ tenantId: string;
136
136
  title: string | null;
137
+ activeSubAgentId: string;
137
138
  lastContextResolution: string | null;
138
- projectId: string;
139
139
  }>;
140
140
  /**
141
141
  * Get conversation history with filtering and context management
@@ -156,18 +156,18 @@ declare const getActiveAgentForConversation: (db: AgentsRunDatabaseClient) => (p
156
156
  createdAt: string;
157
157
  updatedAt: string;
158
158
  ref: {
159
- type: "commit" | "tag" | "branch";
159
+ type: "tag" | "commit" | "branch";
160
160
  name: string;
161
161
  hash: string;
162
162
  } | null;
163
163
  userId: string | null;
164
164
  metadata: ConversationMetadata | null;
165
- tenantId: string;
166
165
  agentId: string | null;
167
- activeSubAgentId: string;
166
+ projectId: string;
167
+ tenantId: string;
168
168
  title: string | null;
169
+ activeSubAgentId: string;
169
170
  lastContextResolution: string | null;
170
- projectId: string;
171
171
  } | undefined>;
172
172
  /**
173
173
  * Set active agent for a conversation (upsert operation)