@inkeep/agents-core 0.41.2 → 0.42.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.
- package/dist/api-client/base-client.d.ts +87 -8
- package/dist/api-client/base-client.js +174 -1
- package/dist/api-client/eval-api-client.d.ts +47 -0
- package/dist/api-client/eval-api-client.js +65 -0
- package/dist/api-client/index.d.ts +4 -0
- package/dist/api-client/index.js +5 -0
- package/dist/api-client/manage-api-client.d.ts +34 -0
- package/dist/api-client/manage-api-client.js +104 -0
- package/dist/auth/auth.d.ts +86 -20
- package/dist/auth/auth.js +55 -1
- package/dist/auth/authz/client.d.ts +81 -0
- package/dist/auth/authz/client.js +189 -0
- package/dist/auth/authz/config.d.ts +76 -0
- package/dist/auth/authz/config.js +76 -0
- package/dist/auth/authz/index.d.ts +5 -0
- package/dist/auth/authz/index.js +6 -0
- package/dist/auth/authz/permissions.d.ts +57 -0
- package/dist/auth/authz/permissions.js +83 -0
- package/dist/auth/authz/sync.d.ts +85 -0
- package/dist/auth/authz/sync.js +237 -0
- package/dist/auth/permissions.d.ts +13 -13
- package/dist/auth/permissions.js +2 -181
- package/dist/client-exports.d.ts +8 -3
- package/dist/client-exports.js +3 -2
- package/dist/constants/context-breakdown.d.ts +61 -0
- package/dist/constants/context-breakdown.js +124 -0
- package/dist/constants/otel-attributes.d.ts +4 -0
- package/dist/constants/otel-attributes.js +4 -0
- package/dist/context/ContextConfig.d.ts +2 -2
- package/dist/context/ContextConfig.js +3 -3
- package/dist/context/TemplateEngine.js +0 -1
- package/dist/context/index.d.ts +1 -5
- package/dist/context/index.js +1 -5
- package/dist/credential-stuffer/CredentialStuffer.d.ts +1 -1
- package/dist/data-access/index.d.ts +34 -26
- package/dist/data-access/index.js +34 -26
- package/dist/data-access/manage/agentFull.d.ts +36 -0
- package/dist/data-access/{agentFull.js → manage/agentFull.js} +205 -7
- package/dist/data-access/{agents.d.ts → manage/agents.d.ts} +23 -22
- package/dist/data-access/{agents.js → manage/agents.js} +52 -7
- package/dist/data-access/{artifactComponents.d.ts → manage/artifactComponents.d.ts} +21 -21
- package/dist/data-access/{artifactComponents.js → manage/artifactComponents.js} +5 -5
- package/dist/data-access/{contextConfigs.d.ts → manage/contextConfigs.d.ts} +14 -14
- package/dist/data-access/{contextConfigs.js → manage/contextConfigs.js} +3 -3
- package/dist/data-access/{credentialReferences.d.ts → manage/credentialReferences.d.ts} +17 -17
- package/dist/data-access/{credentialReferences.js → manage/credentialReferences.js} +2 -2
- package/dist/data-access/{dataComponents.d.ts → manage/dataComponents.d.ts} +20 -20
- package/dist/data-access/{dataComponents.js → manage/dataComponents.js} +7 -7
- package/dist/data-access/manage/evalConfig.d.ts +221 -0
- package/dist/data-access/manage/evalConfig.js +275 -0
- package/dist/data-access/{externalAgents.d.ts → manage/externalAgents.d.ts} +16 -16
- package/dist/data-access/{externalAgents.js → manage/externalAgents.js} +2 -2
- package/dist/data-access/{functionTools.d.ts → manage/functionTools.d.ts} +65 -15
- package/dist/data-access/{functionTools.js → manage/functionTools.js} +90 -8
- package/dist/data-access/{functions.d.ts → manage/functions.d.ts} +9 -9
- package/dist/data-access/{functions.js → manage/functions.js} +3 -3
- package/dist/data-access/manage/projectFull.d.ts +38 -0
- package/dist/data-access/{projectFull.js → manage/projectFull.js} +64 -65
- package/dist/data-access/manage/projectLifecycle.d.ts +119 -0
- package/dist/data-access/manage/projectLifecycle.js +234 -0
- package/dist/data-access/manage/projects.d.ts +75 -0
- package/dist/data-access/{projects.js → manage/projects.js} +15 -16
- package/dist/data-access/{subAgentExternalAgentRelations.d.ts → manage/subAgentExternalAgentRelations.d.ts} +19 -19
- package/dist/data-access/{subAgentExternalAgentRelations.js → manage/subAgentExternalAgentRelations.js} +2 -2
- package/dist/data-access/{subAgentRelations.d.ts → manage/subAgentRelations.d.ts} +29 -29
- package/dist/data-access/{subAgentRelations.js → manage/subAgentRelations.js} +3 -3
- package/dist/data-access/{subAgentTeamAgentRelations.d.ts → manage/subAgentTeamAgentRelations.d.ts} +19 -19
- package/dist/data-access/{subAgentTeamAgentRelations.js → manage/subAgentTeamAgentRelations.js} +2 -2
- package/dist/data-access/{subAgents.d.ts → manage/subAgents.d.ts} +13 -13
- package/dist/data-access/{subAgents.js → manage/subAgents.js} +4 -4
- package/dist/data-access/{tools.d.ts → manage/tools.d.ts} +26 -19
- package/dist/data-access/{tools.js → manage/tools.js} +57 -35
- package/dist/data-access/manage/triggers.d.ts +80 -0
- package/dist/data-access/manage/triggers.js +81 -0
- package/dist/data-access/{apiKeys.d.ts → runtime/apiKeys.d.ts} +17 -17
- package/dist/data-access/{apiKeys.js → runtime/apiKeys.js} +3 -3
- package/dist/data-access/runtime/cascade-delete.d.ts +77 -0
- package/dist/data-access/runtime/cascade-delete.js +111 -0
- package/dist/data-access/{contextCache.d.ts → runtime/contextCache.d.ts} +13 -13
- package/dist/data-access/{contextCache.js → runtime/contextCache.js} +5 -5
- package/dist/data-access/{conversations.d.ts → runtime/conversations.d.ts} +68 -19
- package/dist/data-access/{conversations.js → runtime/conversations.js} +13 -7
- package/dist/data-access/runtime/evalRuns.d.ts +120 -0
- package/dist/data-access/runtime/evalRuns.js +168 -0
- package/dist/data-access/{ledgerArtifacts.d.ts → runtime/ledgerArtifacts.d.ts} +13 -13
- package/dist/data-access/{ledgerArtifacts.js → runtime/ledgerArtifacts.js} +3 -3
- package/dist/data-access/{messages.d.ts → runtime/messages.d.ts} +15 -15
- package/dist/data-access/{messages.js → runtime/messages.js} +2 -2
- package/dist/data-access/{organizations.d.ts → runtime/organizations.d.ts} +16 -7
- package/dist/data-access/{organizations.js → runtime/organizations.js} +15 -3
- package/dist/data-access/runtime/projects.d.ts +62 -0
- package/dist/data-access/runtime/projects.js +90 -0
- package/dist/data-access/runtime/tasks.d.ts +55 -0
- package/dist/data-access/{tasks.js → runtime/tasks.js} +2 -2
- package/dist/data-access/runtime/triggerInvocations.d.ts +62 -0
- package/dist/data-access/runtime/triggerInvocations.js +54 -0
- package/dist/data-access/runtime/users.d.ts +19 -0
- package/dist/data-access/{users.js → runtime/users.js} +2 -2
- package/dist/data-access/validation.d.ts +4 -4
- package/dist/data-access/validation.js +1 -1
- package/dist/db/clean.d.ts +8 -4
- package/dist/db/clean.js +14 -105
- package/dist/db/delete.d.ts +1 -1
- package/dist/db/delete.js +7 -10
- package/dist/db/manage/dolt-cleanup.d.ts +51 -0
- package/dist/db/manage/dolt-cleanup.js +132 -0
- package/dist/db/manage/manage-client.d.ts +26 -0
- package/dist/db/manage/manage-client.js +68 -0
- package/dist/db/{schema.d.ts → manage/manage-schema.d.ts} +1459 -1285
- package/dist/db/{schema.js → manage/manage-schema.js} +433 -341
- package/dist/db/manage/test-manage-client.d.ts +27 -0
- package/dist/db/manage/test-manage-client.js +68 -0
- package/dist/db/runtime/runtime-client.d.ts +20 -0
- package/dist/db/runtime/runtime-client.js +30 -0
- package/dist/db/runtime/runtime-schema.d.ts +2834 -0
- package/dist/db/runtime/runtime-schema.js +483 -0
- package/dist/db/runtime/test-runtime-client.d.ts +27 -0
- package/dist/db/{test-client.js → runtime/test-runtime-client.js} +11 -25
- package/dist/dolt/branch.d.ts +62 -0
- package/dist/dolt/branch.js +82 -0
- package/dist/dolt/branches-api.d.ts +108 -0
- package/dist/dolt/branches-api.js +162 -0
- package/dist/dolt/commit.d.ts +94 -0
- package/dist/dolt/commit.js +103 -0
- package/dist/dolt/diff.d.ts +27 -0
- package/dist/dolt/diff.js +21 -0
- package/dist/dolt/index.d.ts +10 -0
- package/dist/dolt/index.js +11 -0
- package/dist/dolt/merge.d.ts +63 -0
- package/dist/dolt/merge.js +81 -0
- package/dist/dolt/migrate-all-branches.d.ts +4 -0
- package/dist/dolt/migrate-all-branches.js +78 -0
- package/dist/dolt/migrate-dolt.d.ts +1 -0
- package/dist/dolt/migrate-dolt.js +22 -0
- package/dist/dolt/ref-helpers.d.ts +19 -0
- package/dist/dolt/ref-helpers.js +65 -0
- package/dist/dolt/ref-middleware.d.ts +82 -0
- package/dist/dolt/ref-middleware.js +217 -0
- package/dist/dolt/ref-scope.d.ts +101 -0
- package/dist/dolt/ref-scope.js +231 -0
- package/dist/dolt/schema-sync.d.ts +134 -0
- package/dist/dolt/schema-sync.js +246 -0
- package/dist/env.d.ts +6 -4
- package/dist/env.js +3 -2
- package/dist/index.d.ts +71 -44
- package/dist/index.js +74 -47
- package/dist/types/entities.d.ts +81 -2
- package/dist/types/index.d.ts +3 -3
- package/dist/types/utility.d.ts +45 -4
- package/dist/utils/JsonTransformer.d.ts +44 -0
- package/dist/utils/JsonTransformer.js +112 -0
- package/dist/utils/apiKeys.d.ts +5 -1
- package/dist/utils/apiKeys.js +11 -1
- package/dist/utils/colors.d.ts +34 -0
- package/dist/utils/colors.js +49 -0
- package/dist/utils/credential-store-utils.d.ts +1 -1
- package/dist/utils/format-messages.d.ts +1 -1
- package/dist/utils/index.d.ts +7 -3
- package/dist/utils/index.js +7 -3
- package/dist/utils/internal-service-auth.d.ts +79 -0
- package/dist/utils/internal-service-auth.js +140 -0
- package/dist/utils/jwt-helpers.d.ts +56 -0
- package/dist/utils/jwt-helpers.js +90 -0
- package/dist/utils/service-token-auth.d.ts +9 -27
- package/dist/utils/service-token-auth.js +48 -96
- package/dist/utils/template-interpolation.d.ts +22 -0
- package/dist/utils/template-interpolation.js +62 -0
- package/dist/utils/third-party-mcp-servers/composio-client.js +23 -23
- package/dist/utils/trigger-auth.d.ts +62 -0
- package/dist/utils/trigger-auth.js +125 -0
- package/dist/validation/agentFull.js +2 -4
- package/dist/validation/dolt-schemas.d.ts +49 -0
- package/dist/validation/dolt-schemas.js +44 -0
- package/dist/validation/drizzle-schema-helpers.d.ts +4 -26
- package/dist/validation/drizzle-schema-helpers.js +5 -151
- package/dist/validation/index.d.ts +4 -3
- package/dist/validation/index.js +3 -2
- package/dist/validation/schemas.d.ts +17647 -4789
- package/dist/validation/schemas.js +328 -11
- package/drizzle/manage/0000_tearful_rhodey.sql +414 -0
- package/drizzle/manage/0001_broken_wendell_vaughn.sql +19 -0
- package/drizzle/manage/0002_bent_sunfire.sql +1 -0
- package/drizzle/manage/meta/0000_snapshot.json +2987 -0
- package/drizzle/manage/meta/0001_snapshot.json +3115 -0
- package/drizzle/manage/meta/0002_snapshot.json +3115 -0
- package/drizzle/manage/meta/_journal.json +27 -0
- package/drizzle/runtime/0008_silly_preak.sql +127 -0
- package/drizzle/runtime/0009_freezing_leo.sql +17 -0
- package/drizzle/runtime/meta/0008_snapshot.json +2263 -0
- package/drizzle/runtime/meta/0009_snapshot.json +2397 -0
- package/drizzle/{meta → runtime/meta}/_journal.json +14 -0
- package/package.json +48 -15
- package/dist/context/ContextFetcher.d.ts +0 -73
- package/dist/context/ContextFetcher.js +0 -291
- package/dist/context/ContextResolver.d.ts +0 -60
- package/dist/context/ContextResolver.js +0 -278
- package/dist/context/context.d.ts +0 -27
- package/dist/context/context.js +0 -128
- package/dist/context/contextCache.d.ts +0 -58
- package/dist/context/contextCache.js +0 -177
- package/dist/data-access/agentFull.d.ts +0 -33
- package/dist/data-access/projectFull.d.ts +0 -32
- package/dist/data-access/projects.d.ts +0 -71
- package/dist/data-access/tasks.d.ts +0 -45
- package/dist/data-access/users.d.ts +0 -19
- package/dist/db/client.d.ts +0 -20
- package/dist/db/client.js +0 -28
- package/dist/db/test-client.d.ts +0 -31
- package/dist/middleware/contextValidation.d.ts +0 -46
- package/dist/middleware/contextValidation.js +0 -280
- package/dist/middleware/index.d.ts +0 -2
- package/dist/middleware/index.js +0 -3
- package/dist/utils/execution.d.ts +0 -22
- package/dist/utils/execution.js +0 -25
- /package/drizzle/{0000_exotic_mysterio.sql → runtime/0000_exotic_mysterio.sql} +0 -0
- /package/drizzle/{0001_calm_sheva_callister.sql → runtime/0001_calm_sheva_callister.sql} +0 -0
- /package/drizzle/{0002_puzzling_goblin_queen.sql → runtime/0002_puzzling_goblin_queen.sql} +0 -0
- /package/drizzle/{0003_sweet_human_robot.sql → runtime/0003_sweet_human_robot.sql} +0 -0
- /package/drizzle/{0004_cuddly_shooting_star.sql → runtime/0004_cuddly_shooting_star.sql} +0 -0
- /package/drizzle/{0005_reflective_starfox.sql → runtime/0005_reflective_starfox.sql} +0 -0
- /package/drizzle/{0006_stale_thaddeus_ross.sql → runtime/0006_stale_thaddeus_ross.sql} +0 -0
- /package/drizzle/{0007_slim_karma.sql → runtime/0007_slim_karma.sql} +0 -0
- /package/drizzle/{meta → runtime/meta}/0000_snapshot.json +0 -0
- /package/drizzle/{meta → runtime/meta}/0001_snapshot.json +0 -0
- /package/drizzle/{meta → runtime/meta}/0003_snapshot.json +0 -0
- /package/drizzle/{meta → runtime/meta}/0005_snapshot.json +0 -0
- /package/drizzle/{meta → runtime/meta}/0006_snapshot.json +0 -0
- /package/drizzle/{meta → runtime/meta}/0007_snapshot.json +0 -0
|
@@ -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 };
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { AgentsManageDatabaseClient } from "../db/manage/manage-client.js";
|
|
2
|
+
|
|
3
|
+
//#region src/dolt/schema-sync.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The branch that serves as the source of truth for schema.
|
|
7
|
+
* All other branches sync their schema from this branch.
|
|
8
|
+
*/
|
|
9
|
+
declare const SCHEMA_SOURCE_BRANCH = "main";
|
|
10
|
+
/**
|
|
11
|
+
* Represents a single schema difference between two branches.
|
|
12
|
+
* Each row represents a table that differs between the branches,
|
|
13
|
+
* showing the full CREATE TABLE statements for comparison.
|
|
14
|
+
*/
|
|
15
|
+
type SchemaDiff = {
|
|
16
|
+
/** Table name in the source branch (e.g., "public.agent") */
|
|
17
|
+
fromTableName: string;
|
|
18
|
+
/** Table name in the target branch (e.g., "public.agent") */
|
|
19
|
+
toTableName: string;
|
|
20
|
+
/** Full CREATE TABLE statement from the source branch */
|
|
21
|
+
fromCreateStatement: string;
|
|
22
|
+
/** Full CREATE TABLE statement from the target branch */
|
|
23
|
+
toCreateStatement: string;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Result of a schema sync operation
|
|
27
|
+
*/
|
|
28
|
+
type SchemaSyncResult = {
|
|
29
|
+
/** Whether a sync was performed */
|
|
30
|
+
synced: boolean;
|
|
31
|
+
/** Whether there were schema differences detected */
|
|
32
|
+
hadDifferences: boolean;
|
|
33
|
+
/** The schema differences that were found */
|
|
34
|
+
differences?: SchemaDiff[];
|
|
35
|
+
/** Error message if sync failed */
|
|
36
|
+
error?: string;
|
|
37
|
+
/** The merge commit hash if sync was successful */
|
|
38
|
+
mergeCommitHash?: string;
|
|
39
|
+
/** Whether sync was skipped because another request holds the lock */
|
|
40
|
+
skippedDueToLock?: boolean;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Options for schema sync operations
|
|
44
|
+
*/
|
|
45
|
+
type SchemaSyncOptions = {
|
|
46
|
+
/** Automatically commit pending changes before syncing */
|
|
47
|
+
autoCommitPending?: boolean;
|
|
48
|
+
/** Custom commit message for the schema sync */
|
|
49
|
+
commitMessage?: string;
|
|
50
|
+
/** Author information for the commit */
|
|
51
|
+
author?: {
|
|
52
|
+
name: string;
|
|
53
|
+
email: string;
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Options for ensuring schema compatibility
|
|
58
|
+
*/
|
|
59
|
+
type EnsureSchemaSyncOptions = SchemaSyncOptions & {
|
|
60
|
+
/** Automatically sync schema if differences are found */
|
|
61
|
+
autoSync?: boolean;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Get the currently active branch
|
|
65
|
+
*/
|
|
66
|
+
declare const getActiveBranch: (db: AgentsManageDatabaseClient) => () => Promise<string>;
|
|
67
|
+
/**
|
|
68
|
+
* Get schema differences between the schema source branch and a target branch.
|
|
69
|
+
* Returns an empty array if schemas are identical.
|
|
70
|
+
*
|
|
71
|
+
* The comparison is done as dolt_schema_diff(targetBranch, SCHEMA_SOURCE_BRANCH),
|
|
72
|
+
* so fromCreateStatement shows the target branch's schema and toCreateStatement
|
|
73
|
+
* shows main's schema (what we want to sync to).
|
|
74
|
+
*
|
|
75
|
+
* @param targetBranch - The branch to compare against the schema source
|
|
76
|
+
* @returns Array of schema differences (one per table that differs)
|
|
77
|
+
*/
|
|
78
|
+
declare const getSchemaDiff: (db: AgentsManageDatabaseClient) => (targetBranch: string) => Promise<SchemaDiff[]>;
|
|
79
|
+
/**
|
|
80
|
+
* Check if a branch has schema differences from the schema source branch
|
|
81
|
+
*/
|
|
82
|
+
declare const hasSchemaDifferences: (db: AgentsManageDatabaseClient) => (targetBranch: string) => Promise<boolean>;
|
|
83
|
+
/**
|
|
84
|
+
* Check if the current branch has uncommitted changes
|
|
85
|
+
*/
|
|
86
|
+
declare const hasUncommittedChanges: (db: AgentsManageDatabaseClient) => () => Promise<boolean>;
|
|
87
|
+
/**
|
|
88
|
+
* Sync schema from the schema source branch (main) into the current branch.
|
|
89
|
+
* This performs a merge of the schema source branch into the current branch.
|
|
90
|
+
*
|
|
91
|
+
* Uses a non-blocking advisory lock to prevent duplicate syncs when multiple
|
|
92
|
+
* concurrent requests attempt to sync the same branch. If another request is
|
|
93
|
+
* already syncing, this function returns immediately with skippedDueToLock: true.
|
|
94
|
+
*
|
|
95
|
+
* Prerequisites:
|
|
96
|
+
* - Current branch must not have uncommitted changes (unless autoCommitPending is true)
|
|
97
|
+
* - Current branch must not be the schema source branch itself
|
|
98
|
+
*
|
|
99
|
+
* @param options - Sync options
|
|
100
|
+
* @returns Result of the sync operation
|
|
101
|
+
*/
|
|
102
|
+
declare const syncSchemaFromMain: (db: AgentsManageDatabaseClient) => (options?: SchemaSyncOptions) => Promise<SchemaSyncResult>;
|
|
103
|
+
/**
|
|
104
|
+
* Ensure the current branch has schema in sync with the schema source branch.
|
|
105
|
+
* This is a convenience function for checking and optionally syncing schema.
|
|
106
|
+
*
|
|
107
|
+
* Use cases:
|
|
108
|
+
* - Call before write operations to ensure schema compatibility
|
|
109
|
+
* - Call before merge operations to prevent schema conflicts
|
|
110
|
+
* - Call on branch checkout to keep branches up-to-date
|
|
111
|
+
*
|
|
112
|
+
* @param options - Options for the check and optional sync
|
|
113
|
+
* @returns Result indicating current sync status
|
|
114
|
+
*/
|
|
115
|
+
declare const ensureSchemaSync: (db: AgentsManageDatabaseClient) => (options?: EnsureSchemaSyncOptions) => Promise<SchemaSyncResult>;
|
|
116
|
+
/**
|
|
117
|
+
* Get a human-readable summary of schema differences
|
|
118
|
+
*/
|
|
119
|
+
declare const formatSchemaDiffSummary: (differences: SchemaDiff[]) => string;
|
|
120
|
+
/**
|
|
121
|
+
* Check if two branches have compatible schemas (both are in sync with main).
|
|
122
|
+
* Useful before merging two feature branches.
|
|
123
|
+
*
|
|
124
|
+
* @param branchA - First branch to check
|
|
125
|
+
* @param branchB - Second branch to check
|
|
126
|
+
* @returns Object indicating if both branches are schema-compatible
|
|
127
|
+
*/
|
|
128
|
+
declare const areBranchesSchemaCompatible: (db: AgentsManageDatabaseClient) => (branchA: string, branchB: string) => Promise<{
|
|
129
|
+
compatible: boolean;
|
|
130
|
+
branchADifferences: SchemaDiff[];
|
|
131
|
+
branchBDifferences: SchemaDiff[];
|
|
132
|
+
}>;
|
|
133
|
+
//#endregion
|
|
134
|
+
export { EnsureSchemaSyncOptions, SCHEMA_SOURCE_BRANCH, SchemaDiff, SchemaSyncOptions, SchemaSyncResult, areBranchesSchemaCompatible, ensureSchemaSync, formatSchemaDiffSummary, getActiveBranch, getSchemaDiff, hasSchemaDifferences, hasUncommittedChanges, syncSchemaFromMain };
|