@inkeep/agents-core 0.0.0-dev-20260120222159 → 0.0.0-dev-20260121022749

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 (64) hide show
  1. package/dist/auth/auth.d.ts +53 -53
  2. package/dist/auth/auth.js +1 -1
  3. package/dist/auth/permissions.d.ts +13 -13
  4. package/dist/context/ContextConfig.js +3 -3
  5. package/dist/data-access/index.d.ts +5 -5
  6. package/dist/data-access/index.js +5 -5
  7. package/dist/data-access/manage/agents.d.ts +37 -37
  8. package/dist/data-access/manage/artifactComponents.d.ts +12 -12
  9. package/dist/data-access/manage/contextConfigs.d.ts +12 -12
  10. package/dist/data-access/manage/dataComponents.d.ts +6 -6
  11. package/dist/data-access/manage/evalConfig.d.ts +12 -2
  12. package/dist/data-access/manage/evalConfig.js +25 -1
  13. package/dist/data-access/manage/functionTools.d.ts +14 -14
  14. package/dist/data-access/manage/projectFull.d.ts +0 -4
  15. package/dist/data-access/manage/projectFull.js +8 -21
  16. package/dist/data-access/manage/subAgentExternalAgentRelations.d.ts +24 -24
  17. package/dist/data-access/manage/subAgentRelations.d.ts +36 -36
  18. package/dist/data-access/manage/subAgentTeamAgentRelations.d.ts +18 -18
  19. package/dist/data-access/manage/subAgents.d.ts +27 -27
  20. package/dist/data-access/manage/tools.d.ts +41 -34
  21. package/dist/data-access/manage/tools.js +9 -1
  22. package/dist/data-access/manage/triggers.d.ts +0 -4
  23. package/dist/data-access/manage/triggers.js +1 -15
  24. package/dist/data-access/runtime/apiKeys.d.ts +20 -20
  25. package/dist/data-access/runtime/conversations.d.ts +31 -31
  26. package/dist/data-access/runtime/messages.d.ts +18 -18
  27. package/dist/data-access/runtime/organizations.d.ts +10 -1
  28. package/dist/data-access/runtime/organizations.js +13 -1
  29. package/dist/data-access/runtime/tasks.d.ts +7 -7
  30. package/dist/db/manage/manage-client.d.ts +3 -1
  31. package/dist/db/manage/manage-client.js +13 -1
  32. package/dist/db/manage/manage-schema.d.ts +375 -375
  33. package/dist/db/migrations/cleanup-old-trigger-auth.d.ts +1 -0
  34. package/dist/db/migrations/cleanup-old-trigger-auth.js +68 -0
  35. package/dist/db/runtime/runtime-schema.d.ts +181 -181
  36. package/dist/dolt/branch.d.ts +4 -53
  37. package/dist/dolt/branch.js +23 -81
  38. package/dist/dolt/branches-api.js +1 -1
  39. package/dist/dolt/index.d.ts +5 -3
  40. package/dist/dolt/index.js +5 -3
  41. package/dist/dolt/migrate-all-branches.js +1 -1
  42. package/dist/dolt/{ref.d.ts → ref-helpers.d.ts} +5 -2
  43. package/dist/dolt/{ref.js → ref-helpers.js} +15 -3
  44. package/dist/dolt/ref-middleware.d.ts +82 -0
  45. package/dist/dolt/ref-middleware.js +217 -0
  46. package/dist/dolt/ref-scope.d.ts +101 -0
  47. package/dist/dolt/ref-scope.js +231 -0
  48. package/dist/env.d.ts +2 -2
  49. package/dist/env.js +1 -1
  50. package/dist/index.d.ts +12 -10
  51. package/dist/index.js +15 -13
  52. package/dist/types/entities.d.ts +3 -2
  53. package/dist/types/index.d.ts +2 -2
  54. package/dist/utils/index.d.ts +2 -2
  55. package/dist/utils/index.js +2 -2
  56. package/dist/utils/third-party-mcp-servers/composio-client.js +23 -23
  57. package/dist/utils/trigger-auth.d.ts +37 -7
  58. package/dist/utils/trigger-auth.js +72 -77
  59. package/dist/validation/dolt-schemas.d.ts +1 -1
  60. package/dist/validation/index.d.ts +2 -2
  61. package/dist/validation/index.js +2 -2
  62. package/dist/validation/schemas.d.ts +473 -486
  63. package/dist/validation/schemas.js +23 -28
  64. package/package.json +2 -1
@@ -0,0 +1,101 @@
1
+ import { ResolvedRef } from "../validation/dolt-schemas.js";
2
+ import { AgentsManageDatabaseClient } from "../db/manage/manage-client.js";
3
+ import { Pool } from "pg";
4
+
5
+ //#region src/dolt/ref-scope.d.ts
6
+
7
+ /**
8
+ * Context stored in AsyncLocalStorage to detect nested withRef calls
9
+ */
10
+ interface RefScopeContext {
11
+ db: AgentsManageDatabaseClient;
12
+ ref: string;
13
+ connectionId: string;
14
+ }
15
+ /**
16
+ * Error thrown when nested withRef calls are detected with different refs
17
+ */
18
+ declare class NestedRefScopeError extends Error {
19
+ constructor(existingRef: string, attemptedRef: string);
20
+ }
21
+ /**
22
+ * Options for withRef function
23
+ */
24
+ interface WithRefOptions {
25
+ /**
26
+ * Whether to auto-commit changes after successful callback execution.
27
+ * Only applies to branch refs (tags/commits are immutable).
28
+ * @default false
29
+ */
30
+ commit?: boolean;
31
+ /**
32
+ * Commit message to use when committing changes.
33
+ * @default 'withRef auto-commit'
34
+ */
35
+ commitMessage?: string;
36
+ /**
37
+ * Author information for the commit.
38
+ * @default { name: 'agents-api', email: 'api@inkeep.com' }
39
+ */
40
+ author?: {
41
+ name: string;
42
+ email: string;
43
+ };
44
+ }
45
+ /**
46
+ * Execute a function with a database connection scoped to a specific ref (branch/tag/commit).
47
+ *
48
+ * This function:
49
+ * 1. Gets a dedicated connection from the pool
50
+ * 2. For branches: checks out the branch directly
51
+ * 3. For tags/commits: creates a temporary branch from the hash, then checks out
52
+ * 4. Executes the callback with a Drizzle client scoped to that connection
53
+ * 5. Optionally auto-commits changes on success (if commit: true)
54
+ * 6. Cleans up: checks out main, deletes temp branch if created, releases connection
55
+ *
56
+ * Important:
57
+ * - The callback should only perform database operations
58
+ * - Do NOT hold the connection while making external API calls
59
+ *
60
+ * @param pool - The PostgreSQL connection pool
61
+ * @param resolvedRef - The resolved ref (branch, tag, or commit)
62
+ * @param dataAccessFn - The function to execute with the scoped database client
63
+ * @param options - Optional configuration for commit behavior
64
+ * @returns The result of the callback function
65
+ *
66
+ * @example
67
+ * ```typescript
68
+ * // Simple read (no commit)
69
+ * const agent = await withRef(pool, resolvedRef, (db) =>
70
+ * getAgent(db, agentId)
71
+ * );
72
+ *
73
+ * // Write with auto-commit
74
+ * await withRef(pool, resolvedRef, async (db) => {
75
+ * await updateAgent(db, agentId, data);
76
+ * }, { commit: true, commitMessage: 'Update agent config' });
77
+ *
78
+ * // Batch multiple operations with commit
79
+ * const result = await withRef(pool, resolvedRef, async (db) => {
80
+ * await createCredential(db, credData);
81
+ * await updateTool(db, toolId, { credentialId });
82
+ * return { success: true };
83
+ * }, { commit: true, author: { name: 'oauth', email: 'oauth@inkeep.com' } });
84
+ * ```
85
+ */
86
+ declare function withRef<T>(pool: Pool, resolvedRef: ResolvedRef, dataAccessFn: (db: AgentsManageDatabaseClient) => Promise<T>, options?: WithRefOptions): Promise<T>;
87
+ /**
88
+ * Check if currently inside a withRef scope
89
+ */
90
+ declare function isInRefScope(): boolean;
91
+ /**
92
+ * Get the current ref scope context if inside one
93
+ */
94
+ declare function getCurrentRefScope(): RefScopeContext | undefined;
95
+ /**
96
+ * Get the database client from the current ref scope.
97
+ * Throws if not inside a withRef scope.
98
+ */
99
+ declare function getRefScopedDb(): AgentsManageDatabaseClient;
100
+ //#endregion
101
+ export { NestedRefScopeError, WithRefOptions, getCurrentRefScope, getRefScopedDb, isInRefScope, withRef };
@@ -0,0 +1,231 @@
1
+ import { manage_schema_exports } from "../db/manage/manage-schema.js";
2
+ import { getLogger } from "../utils/logger.js";
3
+ import { generateId } from "../utils/conversations.js";
4
+ import { doltAddAndCommit, doltReset, doltStatus } from "./commit.js";
5
+ import { checkoutBranch } from "./branches-api.js";
6
+ import { drizzle } from "drizzle-orm/node-postgres";
7
+ import { AsyncLocalStorage } from "node:async_hooks";
8
+
9
+ //#region src/dolt/ref-scope.ts
10
+ const logger = getLogger("ref-scope");
11
+ /**
12
+ * AsyncLocalStorage to track active ref scope and detect nesting
13
+ */
14
+ const refScopeStorage = new AsyncLocalStorage();
15
+ /**
16
+ * Error thrown when nested withRef calls are detected with different refs
17
+ */
18
+ var NestedRefScopeError = class extends Error {
19
+ constructor(existingRef, attemptedRef) {
20
+ super(`Nested withRef detected. Already in ref scope '${existingRef}', attempted to enter '${attemptedRef}'. Either reuse the existing db from the outer scope, or batch your operations in a single withRef call.`);
21
+ this.name = "NestedRefScopeError";
22
+ }
23
+ };
24
+ /**
25
+ * Execute a function with a database connection scoped to a specific ref (branch/tag/commit).
26
+ *
27
+ * This function:
28
+ * 1. Gets a dedicated connection from the pool
29
+ * 2. For branches: checks out the branch directly
30
+ * 3. For tags/commits: creates a temporary branch from the hash, then checks out
31
+ * 4. Executes the callback with a Drizzle client scoped to that connection
32
+ * 5. Optionally auto-commits changes on success (if commit: true)
33
+ * 6. Cleans up: checks out main, deletes temp branch if created, releases connection
34
+ *
35
+ * Important:
36
+ * - The callback should only perform database operations
37
+ * - Do NOT hold the connection while making external API calls
38
+ *
39
+ * @param pool - The PostgreSQL connection pool
40
+ * @param resolvedRef - The resolved ref (branch, tag, or commit)
41
+ * @param dataAccessFn - The function to execute with the scoped database client
42
+ * @param options - Optional configuration for commit behavior
43
+ * @returns The result of the callback function
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * // Simple read (no commit)
48
+ * const agent = await withRef(pool, resolvedRef, (db) =>
49
+ * getAgent(db, agentId)
50
+ * );
51
+ *
52
+ * // Write with auto-commit
53
+ * await withRef(pool, resolvedRef, async (db) => {
54
+ * await updateAgent(db, agentId, data);
55
+ * }, { commit: true, commitMessage: 'Update agent config' });
56
+ *
57
+ * // Batch multiple operations with commit
58
+ * const result = await withRef(pool, resolvedRef, async (db) => {
59
+ * await createCredential(db, credData);
60
+ * await updateTool(db, toolId, { credentialId });
61
+ * return { success: true };
62
+ * }, { commit: true, author: { name: 'oauth', email: 'oauth@inkeep.com' } });
63
+ * ```
64
+ */
65
+ async function withRef(pool, resolvedRef, dataAccessFn, options) {
66
+ const { commit = false, commitMessage = "withRef auto-commit", author } = options ?? {};
67
+ const startTime = Date.now();
68
+ const connectionId = generateId();
69
+ const existingScope = refScopeStorage.getStore();
70
+ if (existingScope) {
71
+ if (existingScope.ref === resolvedRef.name) {
72
+ logger.debug({
73
+ ref: resolvedRef.name,
74
+ existingConnectionId: existingScope.connectionId
75
+ }, "Reusing existing ref scope");
76
+ return dataAccessFn(existingScope.db);
77
+ }
78
+ throw new NestedRefScopeError(existingScope.ref, resolvedRef.name);
79
+ }
80
+ if (process.env.ENVIRONMENT === "test") {
81
+ const connection$1 = await pool.connect();
82
+ try {
83
+ const db = drizzle(connection$1, { schema: manage_schema_exports });
84
+ return await refScopeStorage.run({
85
+ db,
86
+ ref: resolvedRef.name,
87
+ connectionId
88
+ }, () => dataAccessFn(db));
89
+ } finally {
90
+ connection$1.release();
91
+ }
92
+ }
93
+ logger.debug({
94
+ ref: resolvedRef.name,
95
+ refType: resolvedRef.type,
96
+ connectionId
97
+ }, "Acquiring connection for ref scope");
98
+ const connection = await pool.connect();
99
+ let tempBranch = null;
100
+ try {
101
+ const db = drizzle(connection, { schema: manage_schema_exports });
102
+ if (resolvedRef.type === "branch") {
103
+ logger.debug({
104
+ branch: resolvedRef.name,
105
+ connectionId
106
+ }, "Checking out branch");
107
+ await checkoutBranch(db)({
108
+ branchName: resolvedRef.name,
109
+ syncSchema: false
110
+ });
111
+ } else {
112
+ tempBranch = `temp_${resolvedRef.type}_${Date.now()}_${generateId()}`;
113
+ logger.debug({
114
+ tempBranch,
115
+ hash: resolvedRef.hash,
116
+ refType: resolvedRef.type,
117
+ connectionId
118
+ }, "Creating temporary branch from ref");
119
+ await connection.query(`SELECT DOLT_CHECKOUT('-b', $1, $2)`, [tempBranch, resolvedRef.hash]);
120
+ }
121
+ const result = await refScopeStorage.run({
122
+ db,
123
+ ref: resolvedRef.name,
124
+ connectionId
125
+ }, () => dataAccessFn(db));
126
+ if (commit && resolvedRef.type === "branch") try {
127
+ if ((await doltStatus(db)()).length > 0) {
128
+ logger.info({
129
+ branch: resolvedRef.name,
130
+ message: commitMessage,
131
+ connectionId
132
+ }, "Auto-committing changes");
133
+ await doltAddAndCommit(db)({
134
+ message: commitMessage,
135
+ author: author ?? {
136
+ name: "agents-api",
137
+ email: "api@inkeep.com"
138
+ }
139
+ });
140
+ logger.info({
141
+ branch: resolvedRef.name,
142
+ connectionId
143
+ }, "Successfully committed changes");
144
+ }
145
+ } catch (commitError) {
146
+ logger.error({
147
+ error: commitError,
148
+ branch: resolvedRef.name,
149
+ connectionId
150
+ }, "Failed to auto-commit changes");
151
+ }
152
+ logger.debug({
153
+ ref: resolvedRef.name,
154
+ duration: Date.now() - startTime,
155
+ connectionId
156
+ }, "Ref scope completed successfully");
157
+ return result;
158
+ } catch (error) {
159
+ if (commit && resolvedRef.type === "branch") try {
160
+ const db = drizzle(connection, { schema: manage_schema_exports });
161
+ if ((await doltStatus(db)()).length > 0) {
162
+ await doltReset(db)();
163
+ logger.info({
164
+ branch: resolvedRef.name,
165
+ connectionId
166
+ }, "Reset uncommitted changes due to failed operation");
167
+ }
168
+ } catch (resetError) {
169
+ logger.error({
170
+ error: resetError,
171
+ branch: resolvedRef.name,
172
+ connectionId
173
+ }, "Failed to reset changes after error");
174
+ }
175
+ logger.error({
176
+ ref: resolvedRef.name,
177
+ duration: Date.now() - startTime,
178
+ connectionId,
179
+ error
180
+ }, "Ref scope failed");
181
+ throw error;
182
+ } finally {
183
+ try {
184
+ await connection.query(`SELECT DOLT_CHECKOUT('main')`);
185
+ if (tempBranch) {
186
+ logger.debug({
187
+ tempBranch,
188
+ connectionId
189
+ }, "Deleting temporary branch");
190
+ await connection.query(`SELECT DOLT_BRANCH('-D', $1)`, [tempBranch]);
191
+ }
192
+ } catch (cleanupError) {
193
+ logger.error({
194
+ error: cleanupError,
195
+ tempBranch,
196
+ connectionId
197
+ }, "Error during ref scope cleanup");
198
+ } finally {
199
+ connection.release();
200
+ logger.debug({
201
+ ref: resolvedRef.name,
202
+ duration: Date.now() - startTime,
203
+ connectionId
204
+ }, "Connection released");
205
+ }
206
+ }
207
+ }
208
+ /**
209
+ * Check if currently inside a withRef scope
210
+ */
211
+ function isInRefScope() {
212
+ return refScopeStorage.getStore() !== void 0;
213
+ }
214
+ /**
215
+ * Get the current ref scope context if inside one
216
+ */
217
+ function getCurrentRefScope() {
218
+ return refScopeStorage.getStore();
219
+ }
220
+ /**
221
+ * Get the database client from the current ref scope.
222
+ * Throws if not inside a withRef scope.
223
+ */
224
+ function getRefScopedDb() {
225
+ const scope = refScopeStorage.getStore();
226
+ if (!scope) throw new Error("Not inside a withRef scope. Wrap your code in withRef() to get a ref-scoped database client.");
227
+ return scope.db;
228
+ }
229
+
230
+ //#endregion
231
+ export { NestedRefScopeError, getCurrentRefScope, getRefScopedDb, isInRefScope, withRef };
package/dist/env.d.ts CHANGED
@@ -14,7 +14,7 @@ declare const envSchema: z.ZodObject<{
14
14
  POSTGRES_POOL_SIZE: z.ZodOptional<z.ZodString>;
15
15
  INKEEP_AGENTS_JWT_SIGNING_SECRET: z.ZodOptional<z.ZodString>;
16
16
  INKEEP_AGENTS_MANAGE_UI_URL: z.ZodOptional<z.ZodString>;
17
- INKEEP_AGENTS_MANAGE_API_URL: z.ZodOptional<z.ZodString>;
17
+ INKEEP_AGENTS_API_URL: z.ZodOptional<z.ZodString>;
18
18
  BETTER_AUTH_SECRET: z.ZodOptional<z.ZodString>;
19
19
  TRUSTED_ORIGIN: z.ZodOptional<z.ZodString>;
20
20
  OAUTH_PROXY_PRODUCTION_URL: z.ZodOptional<z.ZodString>;
@@ -26,7 +26,7 @@ declare const env: {
26
26
  POSTGRES_POOL_SIZE?: string | undefined;
27
27
  INKEEP_AGENTS_JWT_SIGNING_SECRET?: string | undefined;
28
28
  INKEEP_AGENTS_MANAGE_UI_URL?: string | undefined;
29
- INKEEP_AGENTS_MANAGE_API_URL?: string | undefined;
29
+ INKEEP_AGENTS_API_URL?: string | undefined;
30
30
  BETTER_AUTH_SECRET?: string | undefined;
31
31
  TRUSTED_ORIGIN?: string | undefined;
32
32
  OAUTH_PROXY_PRODUCTION_URL?: string | undefined;
package/dist/env.js CHANGED
@@ -43,7 +43,7 @@ const envSchema = z.object({
43
43
  POSTGRES_POOL_SIZE: z.string().optional(),
44
44
  INKEEP_AGENTS_JWT_SIGNING_SECRET: z.string().min(32, "INKEEP_AGENTS_JWT_SIGNING_SECRET must be at least 32 characters").optional(),
45
45
  INKEEP_AGENTS_MANAGE_UI_URL: z.string().optional(),
46
- INKEEP_AGENTS_MANAGE_API_URL: z.string().optional(),
46
+ INKEEP_AGENTS_API_URL: z.string().optional(),
47
47
  BETTER_AUTH_SECRET: z.string().optional(),
48
48
  TRUSTED_ORIGIN: z.string().optional(),
49
49
  OAUTH_PROXY_PRODUCTION_URL: z.string().optional()