@lightdash-tools/client 0.6.0 → 0.8.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 (256) hide show
  1. package/README.md +2 -2
  2. package/dist/agentops/apply-agent-change.d.ts +3 -0
  3. package/dist/agentops/apply-agent-change.js +63 -0
  4. package/dist/agentops/apply-bundle-diff.d.ts +13 -0
  5. package/dist/agentops/apply-bundle-diff.js +44 -0
  6. package/dist/agentops/apply-bundle-diff.test.d.ts +1 -0
  7. package/dist/agentops/apply-bundle-diff.test.js +397 -0
  8. package/dist/agentops/apply-context.d.ts +14 -0
  9. package/dist/agentops/apply-context.js +6 -0
  10. package/dist/agentops/apply-evaluation-change.d.ts +3 -0
  11. package/dist/agentops/apply-evaluation-change.js +78 -0
  12. package/dist/agentops/fetch-bundle-state.d.ts +6 -0
  13. package/dist/agentops/fetch-bundle-state.js +20 -0
  14. package/dist/agentops/fetch-bundle-state.test.d.ts +1 -0
  15. package/dist/agentops/fetch-bundle-state.test.js +44 -0
  16. package/dist/agentops/index.d.ts +3 -0
  17. package/dist/agentops/index.js +9 -0
  18. package/dist/agentops/resolve-evaluation-run.d.ts +15 -0
  19. package/dist/agentops/resolve-evaluation-run.js +48 -0
  20. package/dist/api/v1/ai-agents/admin.d.ts +16 -0
  21. package/dist/api/v1/ai-agents/admin.js +29 -0
  22. package/dist/api/v1/ai-agents/agents.d.ts +18 -0
  23. package/dist/api/v1/ai-agents/agents.js +31 -0
  24. package/dist/api/v1/ai-agents/artifacts.d.ts +26 -0
  25. package/dist/api/v1/ai-agents/artifacts.js +41 -0
  26. package/dist/api/v1/ai-agents/artifacts.test.d.ts +1 -0
  27. package/dist/api/v1/ai-agents/artifacts.test.js +69 -0
  28. package/dist/api/v1/ai-agents/discovery.d.ts +28 -0
  29. package/dist/api/v1/ai-agents/discovery.js +46 -0
  30. package/dist/api/v1/ai-agents/discovery.test.d.ts +1 -0
  31. package/dist/api/v1/ai-agents/discovery.test.js +90 -0
  32. package/dist/api/v1/ai-agents/evaluations.d.ts +39 -0
  33. package/dist/api/v1/ai-agents/evaluations.js +70 -0
  34. package/dist/api/v1/ai-agents/facade.d.ts +79 -0
  35. package/dist/api/v1/ai-agents/facade.js +193 -0
  36. package/dist/api/v1/ai-agents/feedback.d.ts +10 -0
  37. package/dist/api/v1/ai-agents/feedback.js +15 -0
  38. package/dist/api/v1/ai-agents/feedback.test.d.ts +1 -0
  39. package/dist/api/v1/ai-agents/feedback.test.js +23 -0
  40. package/dist/api/v1/ai-agents/index.d.ts +1 -0
  41. package/dist/api/v1/ai-agents/index.js +5 -0
  42. package/dist/api/v1/ai-agents/mcp-servers.d.ts +20 -0
  43. package/dist/api/v1/ai-agents/mcp-servers.js +35 -0
  44. package/dist/api/v1/ai-agents/mcp-servers.test.d.ts +1 -0
  45. package/dist/api/v1/ai-agents/mcp-servers.test.js +66 -0
  46. package/dist/api/v1/ai-agents/request-options.d.ts +12 -0
  47. package/dist/api/v1/ai-agents/request-options.js +20 -0
  48. package/dist/api/v1/ai-agents/sql-approval.d.ts +10 -0
  49. package/dist/api/v1/ai-agents/sql-approval.js +15 -0
  50. package/dist/api/v1/ai-agents/sql-approval.test.d.ts +1 -0
  51. package/dist/api/v1/ai-agents/sql-approval.test.js +25 -0
  52. package/dist/api/v1/ai-agents/threads.d.ts +56 -0
  53. package/dist/api/v1/ai-agents/threads.js +97 -0
  54. package/dist/api/v1/ai-agents/threads.test.d.ts +1 -0
  55. package/dist/api/v1/ai-agents/threads.test.js +39 -0
  56. package/dist/api/v1/ai-agents.test.js +172 -89
  57. package/dist/api/v1/analytics.d.ts +10 -0
  58. package/dist/api/v1/analytics.js +14 -0
  59. package/dist/api/v1/analytics.test.d.ts +4 -0
  60. package/dist/api/v1/analytics.test.js +27 -0
  61. package/dist/api/v1/charts.d.ts +13 -2
  62. package/dist/api/v1/charts.js +26 -30
  63. package/dist/api/v1/charts.test.js +35 -28
  64. package/dist/api/v1/dashboards.d.ts +42 -2
  65. package/dist/api/v1/dashboards.js +39 -14
  66. package/dist/api/v1/dashboards.test.js +126 -12
  67. package/dist/api/v1/explores.d.ts +4 -2
  68. package/dist/api/v1/explores.js +20 -29
  69. package/dist/api/v1/explores.test.js +31 -21
  70. package/dist/api/v1/groups.d.ts +1 -1
  71. package/dist/api/v1/groups.js +25 -51
  72. package/dist/api/v1/groups.test.js +15 -24
  73. package/dist/api/v1/metrics.d.ts +2 -2
  74. package/dist/api/v1/metrics.js +4 -17
  75. package/dist/api/v1/metrics.test.js +6 -15
  76. package/dist/api/v1/organizations.d.ts +1 -1
  77. package/dist/api/v1/organizations.js +2 -13
  78. package/dist/api/v1/organizations.test.js +3 -12
  79. package/dist/api/v1/project-access.d.ts +1 -1
  80. package/dist/api/v1/project-access.js +18 -45
  81. package/dist/api/v1/project-access.test.js +27 -36
  82. package/dist/api/v1/projects.d.ts +1 -1
  83. package/dist/api/v1/projects.js +6 -21
  84. package/dist/api/v1/projects.test.js +9 -18
  85. package/dist/api/v1/query.d.ts +5 -2
  86. package/dist/api/v1/query.js +8 -18
  87. package/dist/api/v1/query.test.js +6 -15
  88. package/dist/api/v1/schedulers.d.ts +2 -2
  89. package/dist/api/v1/schedulers.js +4 -17
  90. package/dist/api/v1/schedulers.test.js +6 -15
  91. package/dist/api/v1/spaces.d.ts +1 -1
  92. package/dist/api/v1/spaces.js +18 -45
  93. package/dist/api/v1/spaces.test.js +27 -36
  94. package/dist/api/v1/tags.d.ts +1 -1
  95. package/dist/api/v1/tags.js +4 -17
  96. package/dist/api/v1/tags.test.js +6 -15
  97. package/dist/api/v1/users.d.ts +3 -1
  98. package/dist/api/v1/users.js +22 -39
  99. package/dist/api/v1/users.test.js +44 -30
  100. package/dist/api/v1/validation.d.ts +9 -2
  101. package/dist/api/v1/validation.js +16 -18
  102. package/dist/api/v1/validation.test.js +22 -15
  103. package/dist/api/v2/charts.d.ts +12 -0
  104. package/dist/api/v2/charts.js +18 -0
  105. package/dist/api/v2/charts.test.d.ts +4 -0
  106. package/dist/api/v2/charts.test.js +45 -0
  107. package/dist/api/v2/content.d.ts +20 -3
  108. package/dist/api/v2/content.js +18 -14
  109. package/dist/api/v2/content.test.js +44 -12
  110. package/dist/api/v2/dashboards.d.ts +16 -0
  111. package/dist/api/v2/dashboards.js +22 -0
  112. package/dist/api/v2/dashboards.test.d.ts +4 -0
  113. package/dist/api/v2/dashboards.test.js +61 -0
  114. package/dist/api/v2/organization-roles.d.ts +1 -1
  115. package/dist/api/v2/organization-roles.js +21 -50
  116. package/dist/api/v2/organization-roles.test.js +33 -42
  117. package/dist/api/v2/parameters.d.ts +20 -0
  118. package/dist/api/v2/parameters.js +22 -0
  119. package/dist/api/v2/parameters.test.d.ts +4 -0
  120. package/dist/api/v2/parameters.test.js +39 -0
  121. package/dist/api/v2/project-role-assignments.d.ts +1 -1
  122. package/dist/api/v2/project-role-assignments.js +12 -33
  123. package/dist/api/v2/project-role-assignments.test.js +18 -27
  124. package/dist/api/v2/query.d.ts +16 -1
  125. package/dist/api/v2/query.js +26 -29
  126. package/dist/api/v2/query.test.js +31 -24
  127. package/dist/api/v2/validation.d.ts +28 -0
  128. package/dist/api/v2/validation.js +23 -0
  129. package/dist/client.d.ts +24 -14
  130. package/dist/client.js +26 -16
  131. package/dist/client.test.js +2 -0
  132. package/dist/config.d.ts +35 -10
  133. package/dist/http/axios-client.d.ts +4 -4
  134. package/dist/http/http-client.d.ts +3 -14
  135. package/dist/http/http-client.js +20 -43
  136. package/dist/http/interceptors.d.ts +3 -3
  137. package/dist/http/interceptors.js +16 -3
  138. package/dist/http/rate-limiter.js +2 -13
  139. package/dist/http/rate-limiter.test.js +15 -24
  140. package/dist/http/unwrap-api-success.d.ts +26 -0
  141. package/dist/http/unwrap-api-success.js +25 -0
  142. package/dist/http/unwrap-api-success.test.d.ts +1 -0
  143. package/dist/http/unwrap-api-success.test.js +19 -0
  144. package/dist/index.d.ts +13 -3
  145. package/dist/index.js +17 -1
  146. package/dist/index.test.js +13 -5
  147. package/dist/pagination/fetch-all-pages.js +18 -29
  148. package/dist/pagination/fetch-all-pages.test.js +15 -24
  149. package/dist/types/api.d.ts +1 -1
  150. package/dist/utils/env.d.ts +11 -2
  151. package/dist/utils/env.js +41 -8
  152. package/dist/utils/env.test.js +14 -4
  153. package/dist/utils/logger.js +5 -4
  154. package/dist/utils/retry.js +17 -28
  155. package/dist/utils/retry.test.js +9 -18
  156. package/dist/utils/secret-string.test.d.ts +1 -0
  157. package/dist/utils/secret-string.test.js +25 -0
  158. package/package.json +12 -4
  159. package/src/agentops/apply-agent-change.ts +82 -0
  160. package/src/agentops/apply-bundle-diff.test.ts +453 -0
  161. package/src/agentops/apply-bundle-diff.ts +63 -0
  162. package/src/agentops/apply-context.ts +23 -0
  163. package/src/agentops/apply-evaluation-change.ts +125 -0
  164. package/src/agentops/fetch-bundle-state.test.ts +51 -0
  165. package/src/agentops/fetch-bundle-state.ts +33 -0
  166. package/src/agentops/index.ts +11 -0
  167. package/src/agentops/resolve-evaluation-run.ts +94 -0
  168. package/src/api/v1/ai-agents/admin.ts +41 -0
  169. package/src/api/v1/ai-agents/agents.ts +40 -0
  170. package/src/api/v1/ai-agents/artifacts.test.ts +84 -0
  171. package/src/api/v1/ai-agents/artifacts.ts +92 -0
  172. package/src/api/v1/ai-agents/discovery.test.ts +102 -0
  173. package/src/api/v1/ai-agents/discovery.ts +79 -0
  174. package/src/api/v1/ai-agents/evaluations.ts +152 -0
  175. package/src/api/v1/ai-agents/facade.ts +489 -0
  176. package/src/api/v1/ai-agents/feedback.test.ts +30 -0
  177. package/src/api/v1/ai-agents/feedback.ts +24 -0
  178. package/src/api/v1/ai-agents/index.ts +1 -0
  179. package/src/api/v1/ai-agents/mcp-servers.test.ts +81 -0
  180. package/src/api/v1/ai-agents/mcp-servers.ts +67 -0
  181. package/src/api/v1/ai-agents/request-options.ts +32 -0
  182. package/src/api/v1/ai-agents/sql-approval.test.ts +32 -0
  183. package/src/api/v1/ai-agents/sql-approval.ts +24 -0
  184. package/src/api/v1/ai-agents/threads.test.ts +55 -0
  185. package/src/api/v1/ai-agents/threads.ts +216 -0
  186. package/src/api/v1/ai-agents.test.ts +189 -13
  187. package/src/api/v1/analytics.test.ts +32 -0
  188. package/src/api/v1/analytics.ts +16 -0
  189. package/src/api/v1/charts.test.ts +26 -6
  190. package/src/api/v1/charts.ts +25 -4
  191. package/src/api/v1/dashboards.test.ts +142 -0
  192. package/src/api/v1/dashboards.ts +127 -2
  193. package/src/api/v1/explores.test.ts +22 -0
  194. package/src/api/v1/explores.ts +13 -2
  195. package/src/api/v1/groups.test.ts +2 -0
  196. package/src/api/v1/groups.ts +9 -7
  197. package/src/api/v1/metrics.test.ts +2 -0
  198. package/src/api/v1/metrics.ts +3 -2
  199. package/src/api/v1/organizations.test.ts +2 -0
  200. package/src/api/v1/organizations.ts +2 -1
  201. package/src/api/v1/project-access.test.ts +3 -1
  202. package/src/api/v1/project-access.ts +2 -1
  203. package/src/api/v1/projects.test.ts +2 -0
  204. package/src/api/v1/projects.ts +2 -1
  205. package/src/api/v1/query.test.ts +2 -0
  206. package/src/api/v1/query.ts +6 -2
  207. package/src/api/v1/schedulers.test.ts +2 -0
  208. package/src/api/v1/schedulers.ts +3 -2
  209. package/src/api/v1/spaces.test.ts +2 -0
  210. package/src/api/v1/spaces.ts +2 -1
  211. package/src/api/v1/tags.test.ts +2 -0
  212. package/src/api/v1/tags.ts +2 -1
  213. package/src/api/v1/users.test.ts +26 -0
  214. package/src/api/v1/users.ts +8 -2
  215. package/src/api/v1/validation.test.ts +20 -0
  216. package/src/api/v1/validation.ts +26 -2
  217. package/src/api/v2/charts.test.ts +53 -0
  218. package/src/api/v2/charts.ts +23 -0
  219. package/src/api/v2/content.test.ts +47 -0
  220. package/src/api/v2/content.ts +37 -3
  221. package/src/api/v2/dashboards.test.ts +71 -0
  222. package/src/api/v2/dashboards.ts +40 -0
  223. package/src/api/v2/organization-roles.test.ts +2 -0
  224. package/src/api/v2/organization-roles.ts +2 -1
  225. package/src/api/v2/parameters.test.ts +45 -0
  226. package/src/api/v2/parameters.ts +38 -0
  227. package/src/api/v2/project-role-assignments.test.ts +2 -0
  228. package/src/api/v2/project-role-assignments.ts +2 -1
  229. package/src/api/v2/query.test.ts +20 -0
  230. package/src/api/v2/query.ts +55 -1
  231. package/src/api/v2/validation.ts +48 -0
  232. package/src/client.test.ts +3 -0
  233. package/src/client.ts +33 -17
  234. package/src/config.ts +38 -10
  235. package/src/errors.test.ts +1 -0
  236. package/src/http/axios-client.ts +8 -5
  237. package/src/http/http-client.ts +8 -21
  238. package/src/http/interceptors.ts +23 -10
  239. package/src/http/rate-limiter.test.ts +1 -0
  240. package/src/http/rate-limiter.ts +3 -1
  241. package/src/http/unwrap-api-success.test.ts +23 -0
  242. package/src/http/unwrap-api-success.ts +40 -0
  243. package/src/index.test.ts +16 -7
  244. package/src/index.ts +37 -2
  245. package/src/pagination/fetch-all-pages.test.ts +2 -0
  246. package/src/types/api.ts +1 -1
  247. package/src/utils/env.test.ts +16 -4
  248. package/src/utils/env.ts +61 -10
  249. package/src/utils/logger.ts +6 -4
  250. package/src/utils/retry.test.ts +1 -0
  251. package/src/utils/retry.ts +2 -1
  252. package/src/utils/secret-string.test.ts +27 -0
  253. package/tsconfig.json +4 -2
  254. package/dist/api/v1/ai-agents.d.ts +0 -63
  255. package/dist/api/v1/ai-agents.js +0 -173
  256. package/src/api/v1/ai-agents.ts +0 -252
package/README.md CHANGED
@@ -95,7 +95,7 @@ const client = new LightdashClient({
95
95
  - `client.v1.organizations` – get current organization
96
96
  - `client.v1.explores` – list explores, get explore
97
97
  - `client.v1.charts` – list charts, get/upsert charts-as-code
98
- - `client.v1.dashboards` – list dashboards
98
+ - `client.v1.dashboards` – list dashboards; get/upsert dashboards-as-code
99
99
  - `client.v1.spaces` – list/get/create/update/delete spaces; grant/revoke user and group access to spaces
100
100
  - `client.v1.projectAccess` – list/grant/get/update/revoke project access for users; list/add/remove/update project access for groups (see [Assign or update project access for a list of users](https://docs.lightdash.com/api-reference/v1/recipes))
101
101
  - `client.v1.query` – run/compile metric queries
@@ -120,7 +120,7 @@ For custom endpoints use `client.getHttpClientV1()` and call `get`, `post`, `put
120
120
 
121
121
  - Commander.js: <https://www.npmjs.com/package/commander> <!-- markdown-link-check-disable-line -->
122
122
 
123
- This package depends on `@lightdash-tools/common` only (one-way: client → common). Types and API models are consumed from common; see [ADR 0004](../../docs/adr/0004-shared-api-models-in-common-package.md) for the architecture.
123
+ This package depends on `@lightdash-tools/common` only (one-way: client → common). Types and API models are consumed from common; see [ADR 0004](../../docs/adr/0003-shared-types-and-api-version-namespaces.md) for the architecture.
124
124
 
125
125
  ## Type Imports
126
126
 
@@ -0,0 +1,3 @@
1
+ import type { ApplyBundleContext } from './apply-context';
2
+ import type { BundleDiffChange } from '@lightdash-tools/common';
3
+ export declare function applyAgentChange(ctx: ApplyBundleContext, change: BundleDiffChange): Promise<boolean>;
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.applyAgentChange = applyAgentChange;
4
+ const apply_context_1 = require("./apply-context");
5
+ function findDesiredAgent(ctx, key) {
6
+ return ctx.bundle.spec.agents.find((a) => a.key === key);
7
+ }
8
+ function buildCreateAgentBody(desired, projectUuid) {
9
+ var _a, _b, _c, _d, _e;
10
+ return {
11
+ name: desired.name,
12
+ projectUuid,
13
+ description: (_a = desired.description) !== null && _a !== void 0 ? _a : null,
14
+ instruction: (_b = desired.instruction) !== null && _b !== void 0 ? _b : null,
15
+ tags: (_c = desired.tags) !== null && _c !== void 0 ? _c : null,
16
+ integrations: [],
17
+ imageUrl: null,
18
+ groupAccess: [],
19
+ userAccess: [],
20
+ spaceAccess: [],
21
+ enableDataAccess: (_d = desired.enableDataAccess) !== null && _d !== void 0 ? _d : false,
22
+ enableSelfImprovement: (_e = desired.enableSelfImprovement) !== null && _e !== void 0 ? _e : false,
23
+ version: 1,
24
+ };
25
+ }
26
+ async function applyAgentChange(ctx, change) {
27
+ var _a, _b, _c, _d, _e;
28
+ const { client, projectUuid, agentUuidByKey } = ctx;
29
+ if (change.operation === 'create') {
30
+ const desired = findDesiredAgent(ctx, change.key);
31
+ if (!desired) {
32
+ (0, apply_context_1.recordApplyFailure)(ctx, change, `Agent spec not found for key '${change.key}'`);
33
+ return false;
34
+ }
35
+ const created = await client.v1.aiAgents.createAgent(projectUuid, buildCreateAgentBody(desired, projectUuid));
36
+ agentUuidByKey.set(desired.key, created.uuid);
37
+ return true;
38
+ }
39
+ if (change.operation === 'update') {
40
+ const desired = findDesiredAgent(ctx, change.key);
41
+ if (!desired) {
42
+ (0, apply_context_1.recordApplyFailure)(ctx, change, `Agent spec not found for key '${change.key}'`);
43
+ return false;
44
+ }
45
+ const agentUuid = (_b = (_a = change.agentUuid) !== null && _a !== void 0 ? _a : desired.uuid) !== null && _b !== void 0 ? _b : agentUuidByKey.get(desired.key);
46
+ if (!agentUuid) {
47
+ (0, apply_context_1.recordApplyFailure)(ctx, change, `Could not resolve agent UUID for key '${change.key}'`);
48
+ return false;
49
+ }
50
+ await client.v1.aiAgents.updateAgent(projectUuid, agentUuid, Object.assign(Object.assign({ uuid: agentUuid, name: desired.name, description: (_c = desired.description) !== null && _c !== void 0 ? _c : null, instruction: (_d = desired.instruction) !== null && _d !== void 0 ? _d : null, tags: (_e = desired.tags) !== null && _e !== void 0 ? _e : null }, (desired.enableDataAccess !== undefined
51
+ ? { enableDataAccess: desired.enableDataAccess }
52
+ : {})), (desired.enableSelfImprovement !== undefined
53
+ ? { enableSelfImprovement: desired.enableSelfImprovement }
54
+ : {})));
55
+ agentUuidByKey.set(desired.key, agentUuid);
56
+ return true;
57
+ }
58
+ if (change.operation === 'delete') {
59
+ await client.v1.aiAgents.deleteAgent(projectUuid, change.key);
60
+ return true;
61
+ }
62
+ return false;
63
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Applies a computed bundle diff to Lightdash.
3
+ */
4
+ import type { ApplyBundleDiffFailure } from './apply-context';
5
+ import type { LightdashClient } from '../client';
6
+ import type { BundleDiffChange, LightdashAiAgentBundle } from '@lightdash-tools/common';
7
+ export type { ApplyBundleDiffFailure } from './apply-context';
8
+ export interface ApplyBundleDiffResult {
9
+ applied: number;
10
+ skipped: number;
11
+ failed: ApplyBundleDiffFailure[];
12
+ }
13
+ export declare function applyBundleDiff(client: LightdashClient, bundle: LightdashAiAgentBundle, changes: BundleDiffChange[]): Promise<ApplyBundleDiffResult>;
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ /**
3
+ * Applies a computed bundle diff to Lightdash.
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.applyBundleDiff = applyBundleDiff;
7
+ const apply_agent_change_1 = require("./apply-agent-change");
8
+ const apply_context_1 = require("./apply-context");
9
+ const apply_evaluation_change_1 = require("./apply-evaluation-change");
10
+ async function applyChange(ctx, change) {
11
+ if (change.resourceType === 'agent') {
12
+ return (0, apply_agent_change_1.applyAgentChange)(ctx, change);
13
+ }
14
+ if (change.resourceType === 'evaluation') {
15
+ return (0, apply_evaluation_change_1.applyEvaluationChange)(ctx, change);
16
+ }
17
+ return false;
18
+ }
19
+ async function applyBundleDiff(client, bundle, changes) {
20
+ const ctx = {
21
+ client,
22
+ bundle,
23
+ projectUuid: bundle.spec.projectUuid,
24
+ agentUuidByKey: new Map(),
25
+ failed: [],
26
+ };
27
+ let applied = 0;
28
+ let skipped = 0;
29
+ for (const change of changes) {
30
+ if (change.operation === 'noop') {
31
+ skipped++;
32
+ continue;
33
+ }
34
+ try {
35
+ if (await applyChange(ctx, change)) {
36
+ applied++;
37
+ }
38
+ }
39
+ catch (error) {
40
+ (0, apply_context_1.recordApplyFailure)(ctx, change, error instanceof Error ? error.message : String(error));
41
+ }
42
+ }
43
+ return { applied, skipped, failed: ctx.failed };
44
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,397 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const common_1 = require("@lightdash-tools/common");
4
+ const vitest_1 = require("vitest");
5
+ const apply_bundle_diff_1 = require("./apply-bundle-diff");
6
+ const PROJECT_UUID = '550e8400-e29b-41d4-a716-446655440000';
7
+ const AGENT_UUID = '660e8400-e29b-41d4-a716-446655440001';
8
+ const EVAL_UUID = '770e8400-e29b-41d4-a716-446655440002';
9
+ const STALE_AGENT_UUID = '660e8400-e29b-41d4-a716-446655440099';
10
+ const STALE_EVAL_UUID = '770e8400-e29b-41d4-a716-446655440099';
11
+ const bundle = {
12
+ apiVersion: 'lightdash.ai/v1alpha1',
13
+ kind: 'LightdashAiAgentBundle',
14
+ metadata: { name: 'test-bundle' },
15
+ spec: {
16
+ projectUuid: '550e8400-e29b-41d4-a716-446655440000',
17
+ agents: [
18
+ {
19
+ key: 'a1',
20
+ name: 'Agent One',
21
+ evaluations: [],
22
+ },
23
+ ],
24
+ },
25
+ };
26
+ (0, vitest_1.describe)('applyBundleDiff', () => {
27
+ (0, vitest_1.it)('records API failures without aborting remaining changes', async () => {
28
+ var _a;
29
+ const deleteAgent = vitest_1.vi
30
+ .fn()
31
+ .mockRejectedValueOnce(new Error('delete failed'))
32
+ .mockResolvedValueOnce(undefined);
33
+ const client = {
34
+ v1: {
35
+ aiAgents: {
36
+ createAgent: vitest_1.vi.fn(),
37
+ updateAgent: vitest_1.vi.fn(),
38
+ deleteAgent,
39
+ createEvaluation: vitest_1.vi.fn(),
40
+ updateEvaluation: vitest_1.vi.fn(),
41
+ deleteEvaluation: vitest_1.vi.fn(),
42
+ },
43
+ },
44
+ };
45
+ const changes = [
46
+ {
47
+ resourceType: 'agent',
48
+ operation: 'delete',
49
+ key: 'agent-1',
50
+ path: 'agents[agent-1]',
51
+ },
52
+ {
53
+ resourceType: 'agent',
54
+ operation: 'delete',
55
+ key: 'agent-2',
56
+ path: 'agents[agent-2]',
57
+ },
58
+ ];
59
+ const result = await (0, apply_bundle_diff_1.applyBundleDiff)(client, bundle, changes);
60
+ (0, vitest_1.expect)(deleteAgent).toHaveBeenCalledTimes(2);
61
+ (0, vitest_1.expect)(result.applied).toBe(1);
62
+ (0, vitest_1.expect)(result.failed).toHaveLength(1);
63
+ (0, vitest_1.expect)((_a = result.failed[0]) === null || _a === void 0 ? void 0 : _a.reason).toBe('delete failed');
64
+ });
65
+ (0, vitest_1.it)('applies name-matched agent update using agentUuid from diff', async () => {
66
+ const nameMatchedBundle = {
67
+ apiVersion: 'lightdash.ai/v1alpha1',
68
+ kind: 'LightdashAiAgentBundle',
69
+ metadata: { name: 'test-bundle' },
70
+ spec: {
71
+ projectUuid: PROJECT_UUID,
72
+ agents: [
73
+ {
74
+ key: 'a1',
75
+ name: 'Agent One',
76
+ instruction: 'Updated instruction',
77
+ evaluations: [],
78
+ },
79
+ ],
80
+ },
81
+ };
82
+ const current = {
83
+ projectUuid: PROJECT_UUID,
84
+ agents: [
85
+ {
86
+ agent: {
87
+ uuid: AGENT_UUID,
88
+ name: 'Agent One',
89
+ description: null,
90
+ instruction: 'Original instruction',
91
+ tags: null,
92
+ },
93
+ evaluations: [],
94
+ },
95
+ ],
96
+ };
97
+ const diff = (0, common_1.computeBundleDiff)(nameMatchedBundle, current);
98
+ const updateAgent = vitest_1.vi.fn().mockResolvedValue(undefined);
99
+ const client = {
100
+ v1: {
101
+ aiAgents: {
102
+ createAgent: vitest_1.vi.fn(),
103
+ updateAgent,
104
+ deleteAgent: vitest_1.vi.fn(),
105
+ createEvaluation: vitest_1.vi.fn(),
106
+ updateEvaluation: vitest_1.vi.fn(),
107
+ deleteEvaluation: vitest_1.vi.fn(),
108
+ },
109
+ },
110
+ };
111
+ const result = await (0, apply_bundle_diff_1.applyBundleDiff)(client, nameMatchedBundle, diff.changes);
112
+ (0, vitest_1.expect)(updateAgent).toHaveBeenCalledWith(PROJECT_UUID, AGENT_UUID, {
113
+ uuid: AGENT_UUID,
114
+ name: 'Agent One',
115
+ description: null,
116
+ instruction: 'Updated instruction',
117
+ tags: null,
118
+ });
119
+ (0, vitest_1.expect)(result.applied).toBe(1);
120
+ (0, vitest_1.expect)(result.failed).toHaveLength(0);
121
+ });
122
+ (0, vitest_1.it)('applies name-matched agent update when bundle uuid is stale', async () => {
123
+ const staleUuidBundle = {
124
+ apiVersion: 'lightdash.ai/v1alpha1',
125
+ kind: 'LightdashAiAgentBundle',
126
+ metadata: { name: 'test-bundle' },
127
+ spec: {
128
+ projectUuid: PROJECT_UUID,
129
+ agents: [
130
+ {
131
+ key: 'a1',
132
+ uuid: STALE_AGENT_UUID,
133
+ name: 'Agent One',
134
+ instruction: 'Updated instruction',
135
+ evaluations: [],
136
+ },
137
+ ],
138
+ },
139
+ };
140
+ const current = {
141
+ projectUuid: PROJECT_UUID,
142
+ agents: [
143
+ {
144
+ agent: {
145
+ uuid: AGENT_UUID,
146
+ name: 'Agent One',
147
+ description: null,
148
+ instruction: 'Original instruction',
149
+ tags: null,
150
+ },
151
+ evaluations: [],
152
+ },
153
+ ],
154
+ };
155
+ const diff = (0, common_1.computeBundleDiff)(staleUuidBundle, current);
156
+ const updateAgent = vitest_1.vi.fn().mockResolvedValue(undefined);
157
+ const client = {
158
+ v1: {
159
+ aiAgents: {
160
+ createAgent: vitest_1.vi.fn(),
161
+ updateAgent,
162
+ deleteAgent: vitest_1.vi.fn(),
163
+ createEvaluation: vitest_1.vi.fn(),
164
+ updateEvaluation: vitest_1.vi.fn(),
165
+ deleteEvaluation: vitest_1.vi.fn(),
166
+ },
167
+ },
168
+ };
169
+ const result = await (0, apply_bundle_diff_1.applyBundleDiff)(client, staleUuidBundle, diff.changes);
170
+ (0, vitest_1.expect)(updateAgent).toHaveBeenCalledWith(PROJECT_UUID, AGENT_UUID, vitest_1.expect.objectContaining({ uuid: AGENT_UUID, instruction: 'Updated instruction' }));
171
+ (0, vitest_1.expect)(updateAgent).not.toHaveBeenCalledWith(PROJECT_UUID, STALE_AGENT_UUID, vitest_1.expect.anything());
172
+ (0, vitest_1.expect)(result.applied).toBe(1);
173
+ (0, vitest_1.expect)(result.failed).toHaveLength(0);
174
+ });
175
+ (0, vitest_1.it)('applies title-matched evaluation update using evaluationUuid from diff', async () => {
176
+ const titleMatchedBundle = {
177
+ apiVersion: 'lightdash.ai/v1alpha1',
178
+ kind: 'LightdashAiAgentBundle',
179
+ metadata: { name: 'test-bundle' },
180
+ spec: {
181
+ projectUuid: PROJECT_UUID,
182
+ agents: [
183
+ {
184
+ key: 'a1',
185
+ uuid: AGENT_UUID,
186
+ name: 'Agent One',
187
+ evaluations: [
188
+ {
189
+ key: 'e1',
190
+ title: 'Smoke Tests',
191
+ prompts: [{ prompt: 'Updated prompt', expectedResponse: null }],
192
+ },
193
+ ],
194
+ },
195
+ ],
196
+ },
197
+ };
198
+ const current = {
199
+ projectUuid: PROJECT_UUID,
200
+ agents: [
201
+ {
202
+ agent: {
203
+ uuid: AGENT_UUID,
204
+ name: 'Agent One',
205
+ description: null,
206
+ instruction: null,
207
+ tags: null,
208
+ },
209
+ evaluations: [
210
+ {
211
+ evalUuid: EVAL_UUID,
212
+ title: 'Smoke Tests',
213
+ description: null,
214
+ prompts: [
215
+ {
216
+ type: 'string',
217
+ prompt: 'Original prompt',
218
+ expectedResponse: null,
219
+ },
220
+ ],
221
+ },
222
+ ],
223
+ },
224
+ ],
225
+ };
226
+ const diff = (0, common_1.computeBundleDiff)(titleMatchedBundle, current);
227
+ const updateEvaluation = vitest_1.vi.fn().mockResolvedValue(undefined);
228
+ const client = {
229
+ v1: {
230
+ aiAgents: {
231
+ createAgent: vitest_1.vi.fn(),
232
+ updateAgent: vitest_1.vi.fn(),
233
+ deleteAgent: vitest_1.vi.fn(),
234
+ createEvaluation: vitest_1.vi.fn(),
235
+ updateEvaluation,
236
+ deleteEvaluation: vitest_1.vi.fn(),
237
+ },
238
+ },
239
+ };
240
+ const result = await (0, apply_bundle_diff_1.applyBundleDiff)(client, titleMatchedBundle, diff.changes);
241
+ (0, vitest_1.expect)(updateEvaluation).toHaveBeenCalledWith(PROJECT_UUID, AGENT_UUID, EVAL_UUID, vitest_1.expect.objectContaining({
242
+ title: 'Smoke Tests',
243
+ prompts: [{ prompt: 'Updated prompt', expectedResponse: null }],
244
+ }));
245
+ (0, vitest_1.expect)(result.applied).toBe(1);
246
+ (0, vitest_1.expect)(result.failed).toHaveLength(0);
247
+ });
248
+ (0, vitest_1.it)('applies title-matched evaluation update when bundle uuid is stale', async () => {
249
+ const staleEvalBundle = {
250
+ apiVersion: 'lightdash.ai/v1alpha1',
251
+ kind: 'LightdashAiAgentBundle',
252
+ metadata: { name: 'test-bundle' },
253
+ spec: {
254
+ projectUuid: PROJECT_UUID,
255
+ agents: [
256
+ {
257
+ key: 'a1',
258
+ uuid: AGENT_UUID,
259
+ name: 'Agent One',
260
+ evaluations: [
261
+ {
262
+ key: 'e1',
263
+ uuid: STALE_EVAL_UUID,
264
+ title: 'Smoke Tests',
265
+ prompts: [{ prompt: 'Updated prompt', expectedResponse: null }],
266
+ },
267
+ ],
268
+ },
269
+ ],
270
+ },
271
+ };
272
+ const current = {
273
+ projectUuid: PROJECT_UUID,
274
+ agents: [
275
+ {
276
+ agent: {
277
+ uuid: AGENT_UUID,
278
+ name: 'Agent One',
279
+ description: null,
280
+ instruction: null,
281
+ tags: null,
282
+ },
283
+ evaluations: [
284
+ {
285
+ evalUuid: EVAL_UUID,
286
+ title: 'Smoke Tests',
287
+ description: null,
288
+ prompts: [
289
+ {
290
+ type: 'string',
291
+ prompt: 'Original prompt',
292
+ expectedResponse: null,
293
+ },
294
+ ],
295
+ },
296
+ ],
297
+ },
298
+ ],
299
+ };
300
+ const diff = (0, common_1.computeBundleDiff)(staleEvalBundle, current);
301
+ const updateEvaluation = vitest_1.vi.fn().mockResolvedValue(undefined);
302
+ const client = {
303
+ v1: {
304
+ aiAgents: {
305
+ createAgent: vitest_1.vi.fn(),
306
+ updateAgent: vitest_1.vi.fn(),
307
+ deleteAgent: vitest_1.vi.fn(),
308
+ createEvaluation: vitest_1.vi.fn(),
309
+ updateEvaluation,
310
+ deleteEvaluation: vitest_1.vi.fn(),
311
+ },
312
+ },
313
+ };
314
+ const result = await (0, apply_bundle_diff_1.applyBundleDiff)(client, staleEvalBundle, diff.changes);
315
+ (0, vitest_1.expect)(updateEvaluation).toHaveBeenCalledWith(PROJECT_UUID, AGENT_UUID, EVAL_UUID, vitest_1.expect.objectContaining({
316
+ title: 'Smoke Tests',
317
+ prompts: [{ prompt: 'Updated prompt', expectedResponse: null }],
318
+ }));
319
+ (0, vitest_1.expect)(updateEvaluation).not.toHaveBeenCalledWith(PROJECT_UUID, AGENT_UUID, STALE_EVAL_UUID, vitest_1.expect.anything());
320
+ (0, vitest_1.expect)(result.applied).toBe(1);
321
+ (0, vitest_1.expect)(result.failed).toHaveLength(0);
322
+ });
323
+ (0, vitest_1.it)('clears evaluation description when bundle omits it', async () => {
324
+ const bundleWithoutDescription = {
325
+ apiVersion: 'lightdash.ai/v1alpha1',
326
+ kind: 'LightdashAiAgentBundle',
327
+ metadata: { name: 'test-bundle' },
328
+ spec: {
329
+ projectUuid: PROJECT_UUID,
330
+ agents: [
331
+ {
332
+ key: 'a1',
333
+ uuid: AGENT_UUID,
334
+ name: 'Agent One',
335
+ evaluations: [
336
+ {
337
+ key: 'e1',
338
+ uuid: EVAL_UUID,
339
+ title: 'Smoke Tests',
340
+ prompts: [{ prompt: 'Same prompt', expectedResponse: null }],
341
+ },
342
+ ],
343
+ },
344
+ ],
345
+ },
346
+ };
347
+ const current = {
348
+ projectUuid: PROJECT_UUID,
349
+ agents: [
350
+ {
351
+ agent: {
352
+ uuid: AGENT_UUID,
353
+ name: 'Agent One',
354
+ description: null,
355
+ instruction: null,
356
+ tags: null,
357
+ },
358
+ evaluations: [
359
+ {
360
+ evalUuid: EVAL_UUID,
361
+ title: 'Smoke Tests',
362
+ description: 'Old description',
363
+ prompts: [
364
+ {
365
+ type: 'string',
366
+ prompt: 'Same prompt',
367
+ expectedResponse: null,
368
+ },
369
+ ],
370
+ },
371
+ ],
372
+ },
373
+ ],
374
+ };
375
+ const diff = (0, common_1.computeBundleDiff)(bundleWithoutDescription, current);
376
+ const updateEvaluation = vitest_1.vi.fn().mockResolvedValue(undefined);
377
+ const client = {
378
+ v1: {
379
+ aiAgents: {
380
+ createAgent: vitest_1.vi.fn(),
381
+ updateAgent: vitest_1.vi.fn(),
382
+ deleteAgent: vitest_1.vi.fn(),
383
+ createEvaluation: vitest_1.vi.fn(),
384
+ updateEvaluation,
385
+ deleteEvaluation: vitest_1.vi.fn(),
386
+ },
387
+ },
388
+ };
389
+ const result = await (0, apply_bundle_diff_1.applyBundleDiff)(client, bundleWithoutDescription, diff.changes);
390
+ (0, vitest_1.expect)(updateEvaluation).toHaveBeenCalledWith(PROJECT_UUID, AGENT_UUID, EVAL_UUID, vitest_1.expect.objectContaining({
391
+ title: 'Smoke Tests',
392
+ description: null,
393
+ }));
394
+ (0, vitest_1.expect)(result.applied).toBe(1);
395
+ (0, vitest_1.expect)(result.failed).toHaveLength(0);
396
+ });
397
+ });
@@ -0,0 +1,14 @@
1
+ import type { LightdashClient } from '../client';
2
+ import type { BundleDiffChange, LightdashAiAgentBundle } from '@lightdash-tools/common';
3
+ export interface ApplyBundleDiffFailure {
4
+ change: BundleDiffChange;
5
+ reason: string;
6
+ }
7
+ export interface ApplyBundleContext {
8
+ client: LightdashClient;
9
+ bundle: LightdashAiAgentBundle;
10
+ projectUuid: string;
11
+ agentUuidByKey: Map<string, string>;
12
+ failed: ApplyBundleDiffFailure[];
13
+ }
14
+ export declare function recordApplyFailure(ctx: ApplyBundleContext, change: BundleDiffChange, reason: string): void;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.recordApplyFailure = recordApplyFailure;
4
+ function recordApplyFailure(ctx, change, reason) {
5
+ ctx.failed.push({ change, reason });
6
+ }
@@ -0,0 +1,3 @@
1
+ import type { ApplyBundleContext } from './apply-context';
2
+ import type { BundleDiffChange } from '@lightdash-tools/common';
3
+ export declare function applyEvaluationChange(ctx: ApplyBundleContext, change: BundleDiffChange): Promise<boolean>;
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.applyEvaluationChange = applyEvaluationChange;
4
+ const apply_context_1 = require("./apply-context");
5
+ function findDesiredAgent(ctx, key) {
6
+ return ctx.bundle.spec.agents.find((a) => a.key === key);
7
+ }
8
+ function findDesiredEvaluation(agent, key) {
9
+ return agent.evaluations.find((e) => e.key === key);
10
+ }
11
+ function promptsToCreateBody(prompts) {
12
+ return prompts.map((p) => {
13
+ var _a, _b;
14
+ return 'prompt' in p
15
+ ? { prompt: p.prompt, expectedResponse: (_a = p.expectedResponse) !== null && _a !== void 0 ? _a : null }
16
+ : {
17
+ threadUuid: p.threadUuid,
18
+ promptUuid: p.promptUuid,
19
+ expectedResponse: (_b = p.expectedResponse) !== null && _b !== void 0 ? _b : null,
20
+ };
21
+ });
22
+ }
23
+ function buildUpdateEvaluationBody(desiredEval) {
24
+ var _a;
25
+ // OpenAPI types omit null, but the API accepts null to clear an existing description.
26
+ return {
27
+ title: desiredEval.title,
28
+ description: (_a = desiredEval.description) !== null && _a !== void 0 ? _a : null,
29
+ prompts: promptsToCreateBody(desiredEval.prompts),
30
+ };
31
+ }
32
+ function resolveAgentUuid(ctx, change, desiredAgent) {
33
+ var _a, _b;
34
+ return ((_b = (_a = change.agentUuid) !== null && _a !== void 0 ? _a : desiredAgent === null || desiredAgent === void 0 ? void 0 : desiredAgent.uuid) !== null && _b !== void 0 ? _b : (change.agentKey ? ctx.agentUuidByKey.get(change.agentKey) : undefined));
35
+ }
36
+ async function applyEvaluationChange(ctx, change) {
37
+ var _a, _b;
38
+ const { client, projectUuid } = ctx;
39
+ const desiredAgent = change.agentKey ? findDesiredAgent(ctx, change.agentKey) : undefined;
40
+ if (change.operation === 'create' || change.operation === 'update') {
41
+ const desiredEval = desiredAgent && change.key ? findDesiredEvaluation(desiredAgent, change.key) : undefined;
42
+ if (!desiredAgent) {
43
+ (0, apply_context_1.recordApplyFailure)(ctx, change, `Agent spec not found for key '${(_a = change.agentKey) !== null && _a !== void 0 ? _a : ''}'`);
44
+ return false;
45
+ }
46
+ if (!desiredEval) {
47
+ (0, apply_context_1.recordApplyFailure)(ctx, change, `Evaluation spec not found for key '${change.key}'`);
48
+ return false;
49
+ }
50
+ const agentUuid = resolveAgentUuid(ctx, change, desiredAgent);
51
+ if (!agentUuid) {
52
+ (0, apply_context_1.recordApplyFailure)(ctx, change, `Could not resolve agent UUID for evaluation key '${change.key}'`);
53
+ return false;
54
+ }
55
+ if (change.operation === 'create') {
56
+ const createEvalBody = Object.assign(Object.assign({ title: desiredEval.title }, (desiredEval.description != null ? { description: desiredEval.description } : {})), { prompts: promptsToCreateBody(desiredEval.prompts) });
57
+ await client.v1.aiAgents.createEvaluation(projectUuid, agentUuid, createEvalBody);
58
+ return true;
59
+ }
60
+ const evalUuid = (_b = change.evaluationUuid) !== null && _b !== void 0 ? _b : desiredEval.uuid;
61
+ if (!evalUuid) {
62
+ (0, apply_context_1.recordApplyFailure)(ctx, change, `Could not resolve evaluation UUID for key '${change.key}'`);
63
+ return false;
64
+ }
65
+ await client.v1.aiAgents.updateEvaluation(projectUuid, agentUuid, evalUuid, buildUpdateEvaluationBody(desiredEval));
66
+ return true;
67
+ }
68
+ if (change.operation === 'delete') {
69
+ const agentUuid = resolveAgentUuid(ctx, change, desiredAgent);
70
+ if (!agentUuid) {
71
+ (0, apply_context_1.recordApplyFailure)(ctx, change, `Could not resolve agent UUID for evaluation delete '${change.key}'`);
72
+ return false;
73
+ }
74
+ await client.v1.aiAgents.deleteEvaluation(projectUuid, agentUuid, change.key);
75
+ return true;
76
+ }
77
+ return false;
78
+ }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Fetches current Lightdash state for AgentOps bundle operations.
3
+ */
4
+ import type { LightdashClient } from '../client';
5
+ import type { BundleCurrentState, LightdashAiAgentBundle } from '@lightdash-tools/common';
6
+ export declare function fetchBundleCurrentState(client: LightdashClient, bundle: LightdashAiAgentBundle): Promise<BundleCurrentState>;