@inkeep/agents-core 0.41.2 → 0.43.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 (258) hide show
  1. package/dist/api-client/base-client.d.ts +87 -8
  2. package/dist/api-client/base-client.js +174 -1
  3. package/dist/api-client/eval-api-client.d.ts +47 -0
  4. package/dist/api-client/eval-api-client.js +65 -0
  5. package/dist/api-client/index.d.ts +4 -0
  6. package/dist/api-client/index.js +5 -0
  7. package/dist/api-client/manage-api-client.d.ts +34 -0
  8. package/dist/api-client/manage-api-client.js +104 -0
  9. package/dist/auth/auth.d.ts +86 -20
  10. package/dist/auth/auth.js +60 -2
  11. package/dist/auth/authz/client.d.ts +87 -0
  12. package/dist/auth/authz/client.js +196 -0
  13. package/dist/auth/authz/config.d.ts +103 -0
  14. package/dist/auth/authz/config.js +93 -0
  15. package/dist/auth/authz/index.d.ts +5 -0
  16. package/dist/auth/authz/index.js +6 -0
  17. package/dist/auth/authz/permissions.d.ts +53 -0
  18. package/dist/auth/authz/permissions.js +83 -0
  19. package/dist/auth/authz/sync.d.ts +106 -0
  20. package/dist/auth/authz/sync.js +321 -0
  21. package/dist/auth/permissions.d.ts +13 -13
  22. package/dist/auth/permissions.js +2 -181
  23. package/dist/client-exports.d.ts +9 -3
  24. package/dist/client-exports.js +4 -2
  25. package/dist/constants/context-breakdown.d.ts +61 -0
  26. package/dist/constants/context-breakdown.js +124 -0
  27. package/dist/constants/execution-limits-shared/defaults.d.ts +1 -1
  28. package/dist/constants/execution-limits-shared/defaults.js +1 -1
  29. package/dist/constants/execution-limits-shared/index.d.ts +1 -1
  30. package/dist/constants/otel-attributes.d.ts +4 -0
  31. package/dist/constants/otel-attributes.js +4 -0
  32. package/dist/context/ContextConfig.d.ts +2 -2
  33. package/dist/context/ContextConfig.js +3 -3
  34. package/dist/context/TemplateEngine.d.ts +0 -6
  35. package/dist/context/TemplateEngine.js +4 -19
  36. package/dist/context/index.d.ts +1 -5
  37. package/dist/context/index.js +1 -5
  38. package/dist/credential-stores/keychain-store.d.ts +20 -8
  39. package/dist/credential-stores/keychain-store.js +107 -43
  40. package/dist/credential-stuffer/CredentialStuffer.d.ts +1 -1
  41. package/dist/data-access/index.d.ts +34 -26
  42. package/dist/data-access/index.js +34 -26
  43. package/dist/data-access/manage/agentFull.d.ts +36 -0
  44. package/dist/data-access/{agentFull.js → manage/agentFull.js} +209 -7
  45. package/dist/data-access/{agents.d.ts → manage/agents.d.ts} +64 -63
  46. package/dist/data-access/{agents.js → manage/agents.js} +80 -27
  47. package/dist/data-access/{artifactComponents.d.ts → manage/artifactComponents.d.ts} +33 -33
  48. package/dist/data-access/{artifactComponents.js → manage/artifactComponents.js} +5 -5
  49. package/dist/data-access/{contextConfigs.d.ts → manage/contextConfigs.d.ts} +26 -26
  50. package/dist/data-access/{contextConfigs.js → manage/contextConfigs.js} +3 -3
  51. package/dist/data-access/{credentialReferences.d.ts → manage/credentialReferences.d.ts} +17 -17
  52. package/dist/data-access/{credentialReferences.js → manage/credentialReferences.js} +2 -2
  53. package/dist/data-access/{dataComponents.d.ts → manage/dataComponents.d.ts} +26 -26
  54. package/dist/data-access/{dataComponents.js → manage/dataComponents.js} +7 -7
  55. package/dist/data-access/manage/evalConfig.d.ts +221 -0
  56. package/dist/data-access/manage/evalConfig.js +275 -0
  57. package/dist/data-access/{externalAgents.d.ts → manage/externalAgents.d.ts} +16 -16
  58. package/dist/data-access/{externalAgents.js → manage/externalAgents.js} +2 -2
  59. package/dist/data-access/manage/functionTools.d.ts +242 -0
  60. package/dist/data-access/{functionTools.js → manage/functionTools.js} +124 -30
  61. package/dist/data-access/{functions.d.ts → manage/functions.d.ts} +9 -9
  62. package/dist/data-access/{functions.js → manage/functions.js} +3 -3
  63. package/dist/data-access/manage/projectFull.d.ts +38 -0
  64. package/dist/data-access/{projectFull.js → manage/projectFull.js} +64 -65
  65. package/dist/data-access/manage/projectLifecycle.d.ts +119 -0
  66. package/dist/data-access/manage/projectLifecycle.js +234 -0
  67. package/dist/data-access/manage/projects.d.ts +75 -0
  68. package/dist/data-access/{projects.js → manage/projects.js} +15 -16
  69. package/dist/data-access/{subAgentExternalAgentRelations.d.ts → manage/subAgentExternalAgentRelations.d.ts} +43 -43
  70. package/dist/data-access/{subAgentExternalAgentRelations.js → manage/subAgentExternalAgentRelations.js} +2 -2
  71. package/dist/data-access/{subAgentRelations.d.ts → manage/subAgentRelations.d.ts} +65 -65
  72. package/dist/data-access/{subAgentRelations.js → manage/subAgentRelations.js} +3 -3
  73. package/dist/data-access/{subAgentTeamAgentRelations.d.ts → manage/subAgentTeamAgentRelations.d.ts} +43 -43
  74. package/dist/data-access/{subAgentTeamAgentRelations.js → manage/subAgentTeamAgentRelations.js} +2 -2
  75. package/dist/data-access/{subAgents.d.ts → manage/subAgents.d.ts} +28 -28
  76. package/dist/data-access/{subAgents.js → manage/subAgents.js} +4 -4
  77. package/dist/data-access/{tools.d.ts → manage/tools.d.ts} +65 -52
  78. package/dist/data-access/{tools.js → manage/tools.js} +109 -64
  79. package/dist/data-access/manage/triggers.d.ts +106 -0
  80. package/dist/data-access/manage/triggers.js +81 -0
  81. package/dist/data-access/{apiKeys.d.ts → runtime/apiKeys.d.ts} +37 -37
  82. package/dist/data-access/{apiKeys.js → runtime/apiKeys.js} +3 -3
  83. package/dist/data-access/runtime/cascade-delete.d.ts +77 -0
  84. package/dist/data-access/runtime/cascade-delete.js +111 -0
  85. package/dist/data-access/{contextCache.d.ts → runtime/contextCache.d.ts} +13 -13
  86. package/dist/data-access/{contextCache.js → runtime/contextCache.js} +5 -5
  87. package/dist/data-access/{conversations.d.ts → runtime/conversations.d.ts} +80 -31
  88. package/dist/data-access/{conversations.js → runtime/conversations.js} +13 -7
  89. package/dist/data-access/runtime/evalRuns.d.ts +120 -0
  90. package/dist/data-access/runtime/evalRuns.js +168 -0
  91. package/dist/data-access/{ledgerArtifacts.d.ts → runtime/ledgerArtifacts.d.ts} +13 -13
  92. package/dist/data-access/{ledgerArtifacts.js → runtime/ledgerArtifacts.js} +3 -3
  93. package/dist/data-access/{messages.d.ts → runtime/messages.d.ts} +24 -24
  94. package/dist/data-access/{messages.js → runtime/messages.js} +2 -2
  95. package/dist/data-access/{organizations.d.ts → runtime/organizations.d.ts} +16 -7
  96. package/dist/data-access/{organizations.js → runtime/organizations.js} +16 -4
  97. package/dist/data-access/runtime/projects.d.ts +62 -0
  98. package/dist/data-access/runtime/projects.js +90 -0
  99. package/dist/data-access/runtime/tasks.d.ts +55 -0
  100. package/dist/data-access/{tasks.js → runtime/tasks.js} +2 -2
  101. package/dist/data-access/runtime/triggerInvocations.d.ts +62 -0
  102. package/dist/data-access/runtime/triggerInvocations.js +54 -0
  103. package/dist/data-access/runtime/users.d.ts +19 -0
  104. package/dist/data-access/{users.js → runtime/users.js} +2 -2
  105. package/dist/data-access/validation.d.ts +4 -4
  106. package/dist/data-access/validation.js +1 -1
  107. package/dist/db/clean.d.ts +8 -4
  108. package/dist/db/clean.js +14 -105
  109. package/dist/db/delete.d.ts +1 -1
  110. package/dist/db/delete.js +7 -10
  111. package/dist/db/manage/dolt-cleanup.d.ts +51 -0
  112. package/dist/db/manage/dolt-cleanup.js +132 -0
  113. package/dist/db/manage/manage-client.d.ts +26 -0
  114. package/dist/db/manage/manage-client.js +68 -0
  115. package/dist/db/{schema.d.ts → manage/manage-schema.d.ts} +1257 -969
  116. package/dist/db/{schema.js → manage/manage-schema.js} +436 -334
  117. package/dist/db/manage/test-manage-client.d.ts +27 -0
  118. package/dist/db/manage/test-manage-client.js +68 -0
  119. package/dist/db/runtime/runtime-client.d.ts +20 -0
  120. package/dist/db/runtime/runtime-client.js +30 -0
  121. package/dist/db/runtime/runtime-schema.d.ts +2834 -0
  122. package/dist/db/runtime/runtime-schema.js +483 -0
  123. package/dist/db/runtime/test-runtime-client.d.ts +27 -0
  124. package/dist/db/{test-client.js → runtime/test-runtime-client.js} +11 -25
  125. package/dist/db/utils.d.ts +6 -0
  126. package/dist/db/utils.js +42 -0
  127. package/dist/dolt/branch.d.ts +62 -0
  128. package/dist/dolt/branch.js +82 -0
  129. package/dist/dolt/branches-api.d.ts +108 -0
  130. package/dist/dolt/branches-api.js +162 -0
  131. package/dist/dolt/commit.d.ts +94 -0
  132. package/dist/dolt/commit.js +103 -0
  133. package/dist/dolt/diff.d.ts +27 -0
  134. package/dist/dolt/diff.js +21 -0
  135. package/dist/dolt/index.d.ts +10 -0
  136. package/dist/dolt/index.js +11 -0
  137. package/dist/dolt/merge.d.ts +63 -0
  138. package/dist/dolt/merge.js +81 -0
  139. package/dist/dolt/migrate-all-branches.d.ts +4 -0
  140. package/dist/dolt/migrate-all-branches.js +83 -0
  141. package/dist/dolt/migrate-dolt.d.ts +1 -0
  142. package/dist/dolt/migrate-dolt.js +25 -0
  143. package/dist/dolt/ref-helpers.d.ts +19 -0
  144. package/dist/dolt/ref-helpers.js +65 -0
  145. package/dist/dolt/ref-middleware.d.ts +82 -0
  146. package/dist/dolt/ref-middleware.js +217 -0
  147. package/dist/dolt/ref-scope.d.ts +101 -0
  148. package/dist/dolt/ref-scope.js +231 -0
  149. package/dist/dolt/schema-sync.d.ts +135 -0
  150. package/dist/dolt/schema-sync.js +255 -0
  151. package/dist/env.d.ts +6 -4
  152. package/dist/env.js +3 -2
  153. package/dist/index.d.ts +73 -46
  154. package/dist/index.js +76 -49
  155. package/dist/types/@napi-rs__keyring/index.d.ts +14 -0
  156. package/dist/types/entities.d.ts +81 -2
  157. package/dist/types/index.d.ts +3 -3
  158. package/dist/types/utility.d.ts +46 -5
  159. package/dist/types/utility.js +2 -1
  160. package/dist/utils/JsonTransformer.d.ts +42 -0
  161. package/dist/utils/JsonTransformer.js +103 -0
  162. package/dist/utils/apiKeys.d.ts +5 -1
  163. package/dist/utils/apiKeys.js +11 -1
  164. package/dist/utils/colors.d.ts +34 -0
  165. package/dist/utils/colors.js +49 -0
  166. package/dist/utils/credential-store-utils.d.ts +1 -1
  167. package/dist/utils/format-messages.d.ts +1 -1
  168. package/dist/utils/index.d.ts +8 -4
  169. package/dist/utils/index.js +8 -4
  170. package/dist/utils/internal-service-auth.d.ts +79 -0
  171. package/dist/utils/internal-service-auth.js +140 -0
  172. package/dist/utils/jmespath-utils.d.ts +152 -0
  173. package/dist/utils/jmespath-utils.js +213 -0
  174. package/dist/utils/jwt-helpers.d.ts +56 -0
  175. package/dist/utils/jwt-helpers.js +90 -0
  176. package/dist/utils/mcp-client.d.ts +1 -1
  177. package/dist/utils/mcp-client.js +1 -1
  178. package/dist/utils/service-token-auth.d.ts +9 -27
  179. package/dist/utils/service-token-auth.js +48 -96
  180. package/dist/utils/signature-validation.d.ts +2 -0
  181. package/dist/utils/signature-validation.js +3 -0
  182. package/dist/utils/template-interpolation.d.ts +22 -0
  183. package/dist/utils/template-interpolation.js +62 -0
  184. package/dist/utils/third-party-mcp-servers/composio-client.d.ts +13 -1
  185. package/dist/utils/third-party-mcp-servers/composio-client.js +47 -29
  186. package/dist/utils/third-party-mcp-servers/index.d.ts +2 -2
  187. package/dist/utils/third-party-mcp-servers/index.js +2 -2
  188. package/dist/utils/trigger-auth.d.ts +85 -0
  189. package/dist/utils/trigger-auth.js +233 -0
  190. package/dist/validation/agentFull.js +2 -4
  191. package/dist/validation/dolt-schemas.d.ts +49 -0
  192. package/dist/validation/dolt-schemas.js +44 -0
  193. package/dist/validation/drizzle-schema-helpers.d.ts +4 -26
  194. package/dist/validation/drizzle-schema-helpers.js +5 -151
  195. package/dist/validation/index.d.ts +5 -4
  196. package/dist/validation/index.js +4 -3
  197. package/dist/validation/render-validation.js +19 -0
  198. package/dist/validation/schemas.d.ts +18223 -5148
  199. package/dist/validation/schemas.js +559 -12
  200. package/dist/validation/stream-event-schemas.d.ts +96 -1
  201. package/dist/validation/stream-event-schemas.js +67 -2
  202. package/drizzle/manage/0000_tearful_rhodey.sql +414 -0
  203. package/drizzle/manage/0001_broken_wendell_vaughn.sql +19 -0
  204. package/drizzle/manage/0002_bent_sunfire.sql +1 -0
  205. package/drizzle/manage/0003_tiny_captain_universe.sql +8 -0
  206. package/drizzle/manage/0004_curious_phil_sheldon.sql +2 -0
  207. package/drizzle/manage/0005_silent_shatterstar.sql +53 -0
  208. package/drizzle/manage/meta/0000_snapshot.json +2987 -0
  209. package/drizzle/manage/meta/0001_snapshot.json +3115 -0
  210. package/drizzle/manage/meta/0002_snapshot.json +3115 -0
  211. package/drizzle/manage/meta/0003_snapshot.json +3134 -0
  212. package/drizzle/manage/meta/0004_snapshot.json +3141 -0
  213. package/drizzle/manage/meta/0005_snapshot.json +3141 -0
  214. package/drizzle/manage/meta/_journal.json +48 -0
  215. package/drizzle/runtime/0008_silly_preak.sql +127 -0
  216. package/drizzle/runtime/0009_freezing_leo.sql +17 -0
  217. package/drizzle/runtime/meta/0008_snapshot.json +2263 -0
  218. package/drizzle/runtime/meta/0009_snapshot.json +2397 -0
  219. package/drizzle/{meta → runtime/meta}/_journal.json +14 -0
  220. package/package.json +56 -18
  221. package/spicedb/schema.zed +114 -0
  222. package/dist/context/ContextFetcher.d.ts +0 -73
  223. package/dist/context/ContextFetcher.js +0 -291
  224. package/dist/context/ContextResolver.d.ts +0 -60
  225. package/dist/context/ContextResolver.js +0 -278
  226. package/dist/context/context.d.ts +0 -27
  227. package/dist/context/context.js +0 -128
  228. package/dist/context/contextCache.d.ts +0 -58
  229. package/dist/context/contextCache.js +0 -177
  230. package/dist/data-access/agentFull.d.ts +0 -33
  231. package/dist/data-access/functionTools.d.ts +0 -169
  232. package/dist/data-access/projectFull.d.ts +0 -32
  233. package/dist/data-access/projects.d.ts +0 -71
  234. package/dist/data-access/tasks.d.ts +0 -45
  235. package/dist/data-access/users.d.ts +0 -19
  236. package/dist/db/client.d.ts +0 -20
  237. package/dist/db/client.js +0 -28
  238. package/dist/db/test-client.d.ts +0 -31
  239. package/dist/middleware/contextValidation.d.ts +0 -46
  240. package/dist/middleware/contextValidation.js +0 -280
  241. package/dist/middleware/index.d.ts +0 -2
  242. package/dist/middleware/index.js +0 -3
  243. package/dist/utils/execution.d.ts +0 -22
  244. package/dist/utils/execution.js +0 -25
  245. /package/drizzle/{0000_exotic_mysterio.sql → runtime/0000_exotic_mysterio.sql} +0 -0
  246. /package/drizzle/{0001_calm_sheva_callister.sql → runtime/0001_calm_sheva_callister.sql} +0 -0
  247. /package/drizzle/{0002_puzzling_goblin_queen.sql → runtime/0002_puzzling_goblin_queen.sql} +0 -0
  248. /package/drizzle/{0003_sweet_human_robot.sql → runtime/0003_sweet_human_robot.sql} +0 -0
  249. /package/drizzle/{0004_cuddly_shooting_star.sql → runtime/0004_cuddly_shooting_star.sql} +0 -0
  250. /package/drizzle/{0005_reflective_starfox.sql → runtime/0005_reflective_starfox.sql} +0 -0
  251. /package/drizzle/{0006_stale_thaddeus_ross.sql → runtime/0006_stale_thaddeus_ross.sql} +0 -0
  252. /package/drizzle/{0007_slim_karma.sql → runtime/0007_slim_karma.sql} +0 -0
  253. /package/drizzle/{meta → runtime/meta}/0000_snapshot.json +0 -0
  254. /package/drizzle/{meta → runtime/meta}/0001_snapshot.json +0 -0
  255. /package/drizzle/{meta → runtime/meta}/0003_snapshot.json +0 -0
  256. /package/drizzle/{meta → runtime/meta}/0005_snapshot.json +0 -0
  257. /package/drizzle/{meta → runtime/meta}/0006_snapshot.json +0 -0
  258. /package/drizzle/{meta → runtime/meta}/0007_snapshot.json +0 -0
@@ -1,60 +0,0 @@
1
- import { ContextConfigSelect } from "../types/entities.js";
2
- import "../types/index.js";
3
- import { CredentialStoreRegistry } from "../credential-stores/CredentialStoreRegistry.js";
4
- import { DatabaseClient } from "../db/client.js";
5
-
6
- //#region src/context/ContextResolver.d.ts
7
- interface ResolvedContext {
8
- [templateKey: string]: unknown;
9
- }
10
- interface ContextResolutionOptions {
11
- triggerEvent: 'initialization' | 'invocation';
12
- conversationId: string;
13
- headers?: Record<string, unknown>;
14
- tenantId: string;
15
- }
16
- interface ContextResolutionResult {
17
- resolvedContext: ResolvedContext;
18
- headers: Record<string, unknown>;
19
- fetchedDefinitions: string[];
20
- cacheHits: string[];
21
- cacheMisses: string[];
22
- errors: Array<{
23
- definitionId: string;
24
- error: string;
25
- }>;
26
- skipped: Array<{
27
- definitionId: string;
28
- reason: string;
29
- }>;
30
- totalDurationMs: number;
31
- }
32
- declare class ContextResolver {
33
- private fetcher;
34
- private cache;
35
- private tenantId;
36
- private projectId;
37
- constructor(tenantId: string, projectId: string, dbClient: DatabaseClient, credentialStoreRegistry?: CredentialStoreRegistry);
38
- /**
39
- * Resolve all contexts for a given configuration and trigger event
40
- */
41
- resolve(contextConfig: ContextConfigSelect, options: ContextResolutionOptions): Promise<ContextResolutionResult>;
42
- /**
43
- * Resolve a single context variable
44
- */
45
- private resolveSingleFetchDefinition;
46
- /**
47
- * Resolve the headers for a given conversation
48
- */
49
- resolveHeaders(conversationId: string, contextConfigId: string): Promise<Record<string, unknown>>;
50
- /**
51
- * Create a hash of the headers for cache invalidation
52
- */
53
- private createRequestHash;
54
- /**
55
- * Clear cache
56
- */
57
- clearCache(tenantId: string, projectId: string, conversationId: string): Promise<void>;
58
- }
59
- //#endregion
60
- export { ContextResolutionOptions, ContextResolutionResult, ContextResolver, ResolvedContext };
@@ -1,278 +0,0 @@
1
- import { getLogger } from "../utils/logger.js";
2
- import { setSpanWithError } from "../utils/tracer-factory.js";
3
- import "../utils/index.js";
4
- import { tracer } from "../utils/tracer.js";
5
- import { ContextCache } from "./contextCache.js";
6
- import { ContextFetcher, MissingRequiredVariableError } from "./ContextFetcher.js";
7
- import crypto from "node:crypto";
8
- import { SpanStatusCode } from "@opentelemetry/api";
9
-
10
- //#region src/context/ContextResolver.ts
11
- const logger = getLogger("context-resolver");
12
- var ContextResolver = class {
13
- fetcher;
14
- cache;
15
- tenantId;
16
- projectId;
17
- constructor(tenantId, projectId, dbClient, credentialStoreRegistry) {
18
- this.tenantId = tenantId;
19
- this.projectId = projectId;
20
- this.fetcher = new ContextFetcher(tenantId, projectId, dbClient, credentialStoreRegistry);
21
- this.cache = new ContextCache(tenantId, projectId, dbClient);
22
- logger.info({
23
- tenantId: this.tenantId,
24
- hasCredentialSupport: !!credentialStoreRegistry
25
- }, "ContextResolver initialized");
26
- }
27
- /**
28
- * Resolve all contexts for a given configuration and trigger event
29
- */
30
- async resolve(contextConfig, options) {
31
- const startTime = Date.now();
32
- logger.info({
33
- contextConfigId: contextConfig.id,
34
- triggerEvent: options.triggerEvent,
35
- conversationId: options.conversationId
36
- }, "Starting context resolution");
37
- return tracer.startActiveSpan("context.resolve", { attributes: {
38
- "context.config_id": contextConfig.id,
39
- "context.trigger_event": options.triggerEvent
40
- } }, async (parentSpan) => {
41
- try {
42
- const result = {
43
- resolvedContext: {},
44
- headers: options.headers || {},
45
- fetchedDefinitions: [],
46
- cacheHits: [],
47
- cacheMisses: [],
48
- errors: [],
49
- skipped: [],
50
- totalDurationMs: 0
51
- };
52
- result.resolvedContext.headers = result.headers;
53
- const currentHeaders = await this.cache.get({
54
- conversationId: options.conversationId,
55
- contextConfigId: contextConfig.id,
56
- contextVariableKey: "headers"
57
- });
58
- if (options.headers && Object.keys(options.headers).length > 0) {
59
- await this.cache.invalidateHeaders(this.tenantId, this.projectId, options.conversationId, contextConfig.id);
60
- logger.info({
61
- conversationId: options.conversationId,
62
- contextConfigId: contextConfig.id
63
- }, "Invalidated headers in cache");
64
- await this.cache.set({
65
- contextConfigId: contextConfig.id,
66
- contextVariableKey: "headers",
67
- conversationId: options.conversationId,
68
- value: options.headers,
69
- tenantId: this.tenantId
70
- });
71
- logger.info({
72
- conversationId: options.conversationId,
73
- contextConfigId: contextConfig.id
74
- }, "Headers set in cache");
75
- } else if (currentHeaders) result.headers = currentHeaders.value;
76
- else result.headers = {};
77
- result.resolvedContext.headers = result.headers;
78
- const contextVariables = contextConfig.contextVariables || {};
79
- const contextVariableEntries = Object.entries(contextVariables);
80
- if (contextVariableEntries.length === 0) {
81
- logger.info({ contextConfigId: contextConfig.id }, "No context variables in context config");
82
- result.totalDurationMs = Date.now() - startTime;
83
- parentSpan.setStatus({ code: SpanStatusCode.OK });
84
- return result;
85
- }
86
- contextVariableEntries.filter(([, def]) => def.trigger === "initialization");
87
- const invocationDefs = contextVariableEntries.filter(([, def]) => def.trigger === "invocation");
88
- if (options.triggerEvent === "invocation" && invocationDefs.length > 0) await this.cache.invalidateInvocationDefinitions(this.tenantId, this.projectId, options.conversationId, contextConfig.id, invocationDefs.map(([, def]) => def.id));
89
- const requestHash = this.createRequestHash(result.headers);
90
- const fetchPromises = contextVariableEntries.map(([templateKey, definition]) => this.resolveSingleFetchDefinition(contextConfig, definition, templateKey, options, requestHash, result).catch((error) => {
91
- const errorMessage = error instanceof Error ? error.message : "Unknown error";
92
- if (error instanceof MissingRequiredVariableError) {
93
- logger.info({
94
- contextConfigId: contextConfig.id,
95
- definitionId: definition.id,
96
- templateKey,
97
- reason: errorMessage
98
- }, "Context fetch skipped due to missing required variable");
99
- result.skipped.push({
100
- definitionId: definition.id,
101
- reason: errorMessage
102
- });
103
- if (definition.defaultValue !== void 0) {
104
- result.resolvedContext[templateKey] = definition.defaultValue;
105
- logger.info({
106
- contextConfigId: contextConfig.id,
107
- definitionId: definition.id,
108
- templateKey
109
- }, "Using default value for skipped context variable");
110
- }
111
- return;
112
- }
113
- logger.error({
114
- contextConfigId: contextConfig.id,
115
- definitionId: definition.id,
116
- templateKey,
117
- error: errorMessage
118
- }, "Failed to resolve context variable");
119
- result.errors.push({
120
- definitionId: definition.id,
121
- error: errorMessage
122
- });
123
- if (definition.defaultValue !== void 0) {
124
- result.resolvedContext[templateKey] = definition.defaultValue;
125
- logger.info({
126
- contextConfigId: contextConfig.id,
127
- definitionId: definition.id,
128
- templateKey
129
- }, "Using default value for failed context variable");
130
- }
131
- }));
132
- await Promise.all(fetchPromises);
133
- result.totalDurationMs = Date.now() - startTime;
134
- parentSpan.addEvent("context.resolution.completed", {
135
- resolved_keys: Object.keys(result.resolvedContext),
136
- fetched_definitions: result.fetchedDefinitions,
137
- skipped_definitions: result.skipped.map((s) => s.definitionId)
138
- });
139
- if (result.errors.length > 0) parentSpan.setStatus({
140
- code: SpanStatusCode.ERROR,
141
- message: `Context resolution completed with errors`
142
- });
143
- else parentSpan.setStatus({ code: SpanStatusCode.OK });
144
- logger.info({
145
- contextConfigId: contextConfig.id,
146
- resolvedKeys: Object.keys(result.resolvedContext),
147
- fetchedDefinitions: result.fetchedDefinitions.length,
148
- cacheHits: result.cacheHits.length,
149
- cacheMisses: result.cacheMisses.length,
150
- errors: result.errors.length,
151
- skipped: result.skipped.length,
152
- totalDurationMs: result.totalDurationMs
153
- }, "Context resolution completed");
154
- return result;
155
- } catch (error) {
156
- const durationMs = Date.now() - startTime;
157
- setSpanWithError(parentSpan, error instanceof Error ? error : new Error(String(error)));
158
- logger.error({
159
- contextConfigId: contextConfig.id,
160
- error: error instanceof Error ? error.message : String(error),
161
- durationMs
162
- }, "Context resolution failed");
163
- throw error;
164
- } finally {
165
- parentSpan.end();
166
- }
167
- });
168
- }
169
- /**
170
- * Resolve a single context variable
171
- */
172
- async resolveSingleFetchDefinition(contextConfig, definition, templateKey, options, requestHash, result) {
173
- const cachedEntry = await this.cache.get({
174
- conversationId: options.conversationId,
175
- contextConfigId: contextConfig.id,
176
- contextVariableKey: templateKey,
177
- requestHash
178
- });
179
- if (cachedEntry) {
180
- result.resolvedContext[templateKey] = cachedEntry.value;
181
- result.cacheHits.push(definition.id);
182
- logger.debug({
183
- definitionId: definition.id,
184
- templateKey,
185
- conversationId: options.conversationId
186
- }, "Cache hit for context variable");
187
- return;
188
- }
189
- result.cacheMisses.push(definition.id);
190
- logger.debug({
191
- definitionId: definition.id,
192
- templateKey,
193
- conversationId: options.conversationId
194
- }, "Cache miss for context variable, fetching data");
195
- const definitionWithConversationId = {
196
- ...definition,
197
- fetchConfig: {
198
- ...definition.fetchConfig,
199
- conversationId: options.conversationId
200
- }
201
- };
202
- const fetchedData = await tracer.startActiveSpan("context-resolver.resolve_single_fetch_definition", { attributes: {
203
- "context.definition_id": definition.id,
204
- "context.template_key": templateKey,
205
- "context.url_template": definition.fetchConfig.url,
206
- "context.method": definition.fetchConfig.method,
207
- "context.trigger": definition.trigger
208
- } }, async (parentSpan) => {
209
- try {
210
- const { data, resolvedUrl } = await this.fetcher.fetch(definitionWithConversationId, result.resolvedContext);
211
- parentSpan.setAttribute("context.url", resolvedUrl);
212
- parentSpan.setStatus({ code: SpanStatusCode.OK });
213
- parentSpan.addEvent("context.fetch_success", {
214
- definition_id: definition.id,
215
- template_key: templateKey,
216
- source: resolvedUrl
217
- });
218
- return data;
219
- } catch (error) {
220
- if (error instanceof MissingRequiredVariableError) {
221
- parentSpan.setStatus({ code: SpanStatusCode.OK });
222
- parentSpan.addEvent("context.fetch_skipped", {
223
- definition_id: definition.id,
224
- template_key: templateKey,
225
- reason: error.message
226
- });
227
- } else setSpanWithError(parentSpan, error instanceof Error ? error : new Error(String(error)));
228
- throw error;
229
- } finally {
230
- parentSpan.end();
231
- }
232
- });
233
- result.resolvedContext[templateKey] = fetchedData;
234
- result.fetchedDefinitions.push(definition.id);
235
- await this.cache.set({
236
- contextConfigId: contextConfig.id,
237
- contextVariableKey: templateKey,
238
- conversationId: options.conversationId,
239
- value: fetchedData,
240
- requestHash,
241
- tenantId: this.tenantId
242
- });
243
- logger.debug({
244
- definitionId: definition.id,
245
- templateKey,
246
- conversationId: options.conversationId
247
- }, "Context variable resolved and cached");
248
- }
249
- /**
250
- * Resolve the headers for a given conversation
251
- */
252
- async resolveHeaders(conversationId, contextConfigId) {
253
- const cachedEntry = await this.cache.get({
254
- conversationId,
255
- contextConfigId,
256
- contextVariableKey: "headers"
257
- });
258
- if (cachedEntry) return cachedEntry.value;
259
- return {};
260
- }
261
- /**
262
- * Create a hash of the headers for cache invalidation
263
- */
264
- createRequestHash(headers) {
265
- const contextString = JSON.stringify(headers, Object.keys(headers).sort());
266
- return crypto.createHash("sha256").update(contextString).digest("hex").substring(0, 16);
267
- }
268
- /**
269
- * Clear cache
270
- */
271
- async clearCache(tenantId, projectId, conversationId) {
272
- await this.cache.clearConversation(tenantId, projectId, conversationId);
273
- logger.info({ conversationId }, "Context cache cleared for conversation");
274
- }
275
- };
276
-
277
- //#endregion
278
- export { ContextResolver };
@@ -1,27 +0,0 @@
1
- import { CredentialStoreRegistry } from "../credential-stores/CredentialStoreRegistry.js";
2
- import { ResolvedContext } from "./ContextResolver.js";
3
- import "../index.js";
4
- import { DatabaseClient } from "../db/client.js";
5
-
6
- //#region src/context/context.d.ts
7
- declare function determineContextTrigger(tenantId: string, projectId: string, conversationId: string, dbClient: DatabaseClient): Promise<'initialization' | 'invocation'>;
8
- declare function handleContextConfigChange(tenantId: string, projectId: string, conversationId: string, agentId: string, newContextConfigId: string, dbClient: DatabaseClient, credentialStores?: CredentialStoreRegistry): Promise<void>;
9
- declare function handleContextResolution({
10
- tenantId,
11
- projectId,
12
- agentId,
13
- conversationId,
14
- headers,
15
- dbClient,
16
- credentialStores
17
- }: {
18
- tenantId: string;
19
- projectId: string;
20
- agentId: string;
21
- conversationId: string;
22
- headers: Record<string, unknown>;
23
- dbClient: DatabaseClient;
24
- credentialStores?: CredentialStoreRegistry;
25
- }): Promise<ResolvedContext | null>;
26
- //#endregion
27
- export { determineContextTrigger, handleContextConfigChange, handleContextResolution };
@@ -1,128 +0,0 @@
1
- import { getLogger } from "../utils/logger.js";
2
- import { getContextConfigById } from "../data-access/contextConfigs.js";
3
- import { setSpanWithError } from "../utils/tracer-factory.js";
4
- import "../utils/index.js";
5
- import { getAgentWithDefaultSubAgent } from "../data-access/agents.js";
6
- import { getConversation, updateConversation } from "../data-access/conversations.js";
7
- import { tracer } from "../utils/tracer.js";
8
- import { ContextResolver } from "./ContextResolver.js";
9
- import "../index.js";
10
- import { SpanStatusCode } from "@opentelemetry/api";
11
-
12
- //#region src/context/context.ts
13
- const logger = getLogger("context");
14
- async function determineContextTrigger(tenantId, projectId, conversationId, dbClient) {
15
- const conversation = await getConversation(dbClient)({
16
- scopes: {
17
- tenantId,
18
- projectId
19
- },
20
- conversationId
21
- });
22
- if (!conversation || !conversation.lastContextResolution) return "initialization";
23
- return "invocation";
24
- }
25
- async function handleContextConfigChange(tenantId, projectId, conversationId, agentId, newContextConfigId, dbClient, credentialStores) {
26
- const conversation = await getConversation(dbClient)({
27
- scopes: {
28
- tenantId,
29
- projectId
30
- },
31
- conversationId
32
- });
33
- if (!conversation) return;
34
- if (conversation.lastContextResolution) {
35
- await new ContextResolver(tenantId, projectId, dbClient, credentialStores).clearCache(tenantId, projectId, conversationId);
36
- logger.info({
37
- conversationId,
38
- agentId,
39
- contextConfigId: newContextConfigId
40
- }, "Potential context config change for existing conversation, cache cleared");
41
- }
42
- }
43
- async function handleContextResolution({ tenantId, projectId, agentId, conversationId, headers, dbClient, credentialStores }) {
44
- return tracer.startActiveSpan("context.handle_context_resolution", { attributes: { "context.headers_keys": Object.keys(headers) } }, async (parentSpan) => {
45
- let agent;
46
- let trigger;
47
- try {
48
- agent = await getAgentWithDefaultSubAgent(dbClient)({ scopes: {
49
- tenantId,
50
- projectId,
51
- agentId
52
- } });
53
- if (!agent?.contextConfigId) {
54
- logger.debug({ agentId }, "No context config found for agent");
55
- return null;
56
- }
57
- await handleContextConfigChange(tenantId, projectId, conversationId, agentId, agent.contextConfigId, dbClient, credentialStores);
58
- trigger = await determineContextTrigger(tenantId, projectId, conversationId, dbClient);
59
- const contextConfig = await getContextConfigById(dbClient)({
60
- scopes: {
61
- tenantId,
62
- projectId,
63
- agentId
64
- },
65
- id: agent.contextConfigId
66
- });
67
- if (!contextConfig) {
68
- logger.warn({ contextConfigId: agent.contextConfigId }, "Context config not found, proceeding without context resolution");
69
- parentSpan.setStatus({ code: SpanStatusCode.ERROR });
70
- parentSpan.addEvent("context.config_not_found", { contextConfigId: agent.contextConfigId });
71
- return null;
72
- }
73
- const contextResult = await new ContextResolver(tenantId, projectId, dbClient, credentialStores).resolve(contextConfig, {
74
- triggerEvent: trigger,
75
- conversationId,
76
- headers,
77
- tenantId
78
- });
79
- const resolvedContext = {
80
- ...contextResult.resolvedContext,
81
- $env: process.env
82
- };
83
- await updateConversation(dbClient)({
84
- scopes: {
85
- tenantId,
86
- projectId
87
- },
88
- conversationId,
89
- data: { lastContextResolution: (/* @__PURE__ */ new Date()).toISOString() }
90
- });
91
- if (contextResult.errors.length > 0) parentSpan.setStatus({
92
- code: SpanStatusCode.ERROR,
93
- message: `Context resolution completed with errors`
94
- });
95
- else parentSpan.setStatus({ code: SpanStatusCode.OK });
96
- logger.info({
97
- conversationId,
98
- agentId,
99
- contextConfigId: contextConfig.id,
100
- trigger,
101
- resolvedKeys: Object.keys(resolvedContext),
102
- cacheHits: contextResult.cacheHits.length,
103
- cacheMisses: contextResult.cacheMisses.length,
104
- fetchedDefinitions: contextResult.fetchedDefinitions.length,
105
- errors: contextResult.errors.length
106
- }, "Context resolution completed (contextConfigId derived from agent)");
107
- return resolvedContext;
108
- } catch (error) {
109
- const errorMessage = error instanceof Error ? error.message : "Unknown error";
110
- parentSpan.setAttributes({
111
- "context.final_status": "failed",
112
- "context.error_message": errorMessage
113
- });
114
- setSpanWithError(parentSpan, error instanceof Error ? error : new Error(String(error)));
115
- logger.error({
116
- error: errorMessage,
117
- contextConfigId: agent?.contextConfigId,
118
- trigger: await determineContextTrigger(tenantId, projectId, conversationId, dbClient).catch(() => "unknown")
119
- }, "Failed to resolve context, proceeding without context resolution");
120
- return null;
121
- } finally {
122
- parentSpan.end();
123
- }
124
- });
125
- }
126
-
127
- //#endregion
128
- export { determineContextTrigger, handleContextConfigChange, handleContextResolution };
@@ -1,58 +0,0 @@
1
- import { DatabaseClient } from "../db/client.js";
2
-
3
- //#region src/context/contextCache.d.ts
4
- interface CacheEntry {
5
- contextConfigId: string;
6
- contextVariableKey: string;
7
- conversationId: string;
8
- value: unknown;
9
- requestHash?: string;
10
- tenantId: string;
11
- }
12
- /**
13
- * Context cache with request hash-based invalidation and graceful error handling.
14
- *
15
- * Implements conversation-scoped caching with smart cache invalidation based on
16
- * request hash changes. All cache errors are treated as cache misses to ensure
17
- * system reliability.
18
- */
19
- declare class ContextCache {
20
- private tenantId;
21
- private projectId;
22
- private dbClient;
23
- constructor(tenantId: string, projectId: string, dbClient: DatabaseClient);
24
- /**
25
- * Get cached context data for a conversation
26
- */
27
- get({
28
- conversationId,
29
- contextConfigId,
30
- contextVariableKey,
31
- requestHash
32
- }: {
33
- conversationId: string;
34
- contextConfigId: string;
35
- contextVariableKey: string;
36
- requestHash?: string;
37
- }): Promise<CacheEntry | null>;
38
- /**
39
- * Set cached context data for a conversation
40
- */
41
- set(entry: CacheEntry): Promise<void>;
42
- /**
43
- * Clear cache entries for a specific conversation
44
- */
45
- clearConversation(tenantId: string, projectId: string, conversationId: string): Promise<void>;
46
- /**
47
- * Clear all cache entries for a specific context configuration
48
- */
49
- clearContextConfig(tenantId: string, projectId: string, contextConfigId: string): Promise<void>;
50
- /**
51
- * Clean up expired or orphaned cache entries
52
- */
53
- cleanup(): Promise<void>;
54
- invalidateInvocationDefinitions(tenantId: string, projectId: string, conversationId: string, contextConfigId: string, definitionIds: string[]): Promise<void>;
55
- invalidateHeaders(tenantId: string, projectId: string, conversationId: string, contextConfigId: string): Promise<void>;
56
- }
57
- //#endregion
58
- export { CacheEntry, ContextCache };