@lightdash-tools/client 0.6.0 → 0.7.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 (237) 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 +5 -2
  62. package/dist/api/v1/charts.js +18 -30
  63. package/dist/api/v1/charts.test.js +19 -28
  64. package/dist/api/v1/dashboards.d.ts +11 -2
  65. package/dist/api/v1/dashboards.js +19 -14
  66. package/dist/api/v1/dashboards.test.js +54 -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 +5 -2
  101. package/dist/api/v1/validation.js +8 -18
  102. package/dist/api/v1/validation.test.js +6 -15
  103. package/dist/api/v2/content.d.ts +2 -2
  104. package/dist/api/v2/content.js +3 -14
  105. package/dist/api/v2/content.test.js +3 -12
  106. package/dist/api/v2/dashboards.d.ts +10 -0
  107. package/dist/api/v2/dashboards.js +14 -0
  108. package/dist/api/v2/dashboards.test.d.ts +4 -0
  109. package/dist/api/v2/dashboards.test.js +27 -0
  110. package/dist/api/v2/organization-roles.d.ts +1 -1
  111. package/dist/api/v2/organization-roles.js +21 -50
  112. package/dist/api/v2/organization-roles.test.js +33 -42
  113. package/dist/api/v2/project-role-assignments.d.ts +1 -1
  114. package/dist/api/v2/project-role-assignments.js +12 -33
  115. package/dist/api/v2/project-role-assignments.test.js +18 -27
  116. package/dist/api/v2/query.d.ts +1 -1
  117. package/dist/api/v2/query.js +10 -29
  118. package/dist/api/v2/query.test.js +15 -24
  119. package/dist/api/v2/validation.d.ts +28 -0
  120. package/dist/api/v2/validation.js +23 -0
  121. package/dist/client.d.ts +20 -14
  122. package/dist/client.js +22 -16
  123. package/dist/config.d.ts +35 -10
  124. package/dist/http/axios-client.d.ts +4 -4
  125. package/dist/http/http-client.d.ts +3 -3
  126. package/dist/http/http-client.js +19 -40
  127. package/dist/http/interceptors.d.ts +3 -3
  128. package/dist/http/interceptors.js +16 -3
  129. package/dist/http/rate-limiter.js +2 -13
  130. package/dist/http/rate-limiter.test.js +15 -24
  131. package/dist/index.d.ts +7 -2
  132. package/dist/index.js +12 -1
  133. package/dist/index.test.js +13 -5
  134. package/dist/pagination/fetch-all-pages.js +18 -29
  135. package/dist/pagination/fetch-all-pages.test.js +15 -24
  136. package/dist/types/api.d.ts +1 -1
  137. package/dist/utils/env.d.ts +11 -2
  138. package/dist/utils/env.js +41 -8
  139. package/dist/utils/env.test.js +14 -4
  140. package/dist/utils/logger.js +5 -4
  141. package/dist/utils/retry.js +17 -28
  142. package/dist/utils/retry.test.js +9 -18
  143. package/dist/utils/secret-string.test.d.ts +1 -0
  144. package/dist/utils/secret-string.test.js +25 -0
  145. package/package.json +12 -4
  146. package/src/agentops/apply-agent-change.ts +82 -0
  147. package/src/agentops/apply-bundle-diff.test.ts +453 -0
  148. package/src/agentops/apply-bundle-diff.ts +63 -0
  149. package/src/agentops/apply-context.ts +23 -0
  150. package/src/agentops/apply-evaluation-change.ts +125 -0
  151. package/src/agentops/fetch-bundle-state.test.ts +51 -0
  152. package/src/agentops/fetch-bundle-state.ts +33 -0
  153. package/src/agentops/index.ts +11 -0
  154. package/src/agentops/resolve-evaluation-run.ts +94 -0
  155. package/src/api/v1/ai-agents/admin.ts +41 -0
  156. package/src/api/v1/ai-agents/agents.ts +40 -0
  157. package/src/api/v1/ai-agents/artifacts.test.ts +84 -0
  158. package/src/api/v1/ai-agents/artifacts.ts +92 -0
  159. package/src/api/v1/ai-agents/discovery.test.ts +102 -0
  160. package/src/api/v1/ai-agents/discovery.ts +79 -0
  161. package/src/api/v1/ai-agents/evaluations.ts +152 -0
  162. package/src/api/v1/ai-agents/facade.ts +489 -0
  163. package/src/api/v1/ai-agents/feedback.test.ts +30 -0
  164. package/src/api/v1/ai-agents/feedback.ts +24 -0
  165. package/src/api/v1/ai-agents/index.ts +1 -0
  166. package/src/api/v1/ai-agents/mcp-servers.test.ts +81 -0
  167. package/src/api/v1/ai-agents/mcp-servers.ts +67 -0
  168. package/src/api/v1/ai-agents/request-options.ts +32 -0
  169. package/src/api/v1/ai-agents/sql-approval.test.ts +32 -0
  170. package/src/api/v1/ai-agents/sql-approval.ts +24 -0
  171. package/src/api/v1/ai-agents/threads.test.ts +55 -0
  172. package/src/api/v1/ai-agents/threads.ts +216 -0
  173. package/src/api/v1/ai-agents.test.ts +189 -13
  174. package/src/api/v1/analytics.test.ts +32 -0
  175. package/src/api/v1/analytics.ts +16 -0
  176. package/src/api/v1/charts.test.ts +8 -6
  177. package/src/api/v1/charts.ts +8 -4
  178. package/src/api/v1/dashboards.test.ts +57 -0
  179. package/src/api/v1/dashboards.ts +41 -2
  180. package/src/api/v1/explores.test.ts +22 -0
  181. package/src/api/v1/explores.ts +13 -2
  182. package/src/api/v1/groups.test.ts +2 -0
  183. package/src/api/v1/groups.ts +9 -7
  184. package/src/api/v1/metrics.test.ts +2 -0
  185. package/src/api/v1/metrics.ts +3 -2
  186. package/src/api/v1/organizations.test.ts +2 -0
  187. package/src/api/v1/organizations.ts +2 -1
  188. package/src/api/v1/project-access.test.ts +3 -1
  189. package/src/api/v1/project-access.ts +2 -1
  190. package/src/api/v1/projects.test.ts +2 -0
  191. package/src/api/v1/projects.ts +2 -1
  192. package/src/api/v1/query.test.ts +2 -0
  193. package/src/api/v1/query.ts +6 -2
  194. package/src/api/v1/schedulers.test.ts +2 -0
  195. package/src/api/v1/schedulers.ts +3 -2
  196. package/src/api/v1/spaces.test.ts +2 -0
  197. package/src/api/v1/spaces.ts +2 -1
  198. package/src/api/v1/tags.test.ts +2 -0
  199. package/src/api/v1/tags.ts +2 -1
  200. package/src/api/v1/users.test.ts +26 -0
  201. package/src/api/v1/users.ts +8 -2
  202. package/src/api/v1/validation.test.ts +2 -0
  203. package/src/api/v1/validation.ts +6 -2
  204. package/src/api/v2/content.test.ts +2 -0
  205. package/src/api/v2/content.ts +3 -2
  206. package/src/api/v2/dashboards.test.ts +32 -0
  207. package/src/api/v2/dashboards.ts +18 -0
  208. package/src/api/v2/organization-roles.test.ts +2 -0
  209. package/src/api/v2/organization-roles.ts +2 -1
  210. package/src/api/v2/project-role-assignments.test.ts +2 -0
  211. package/src/api/v2/project-role-assignments.ts +2 -1
  212. package/src/api/v2/query.test.ts +2 -0
  213. package/src/api/v2/query.ts +2 -1
  214. package/src/api/v2/validation.ts +48 -0
  215. package/src/client.test.ts +1 -0
  216. package/src/client.ts +27 -17
  217. package/src/config.ts +38 -10
  218. package/src/errors.test.ts +1 -0
  219. package/src/http/axios-client.ts +8 -5
  220. package/src/http/http-client.ts +8 -6
  221. package/src/http/interceptors.ts +23 -10
  222. package/src/http/rate-limiter.test.ts +1 -0
  223. package/src/http/rate-limiter.ts +3 -1
  224. package/src/index.test.ts +16 -7
  225. package/src/index.ts +16 -1
  226. package/src/pagination/fetch-all-pages.test.ts +2 -0
  227. package/src/types/api.ts +1 -1
  228. package/src/utils/env.test.ts +16 -4
  229. package/src/utils/env.ts +61 -10
  230. package/src/utils/logger.ts +6 -4
  231. package/src/utils/retry.test.ts +1 -0
  232. package/src/utils/retry.ts +2 -1
  233. package/src/utils/secret-string.test.ts +27 -0
  234. package/tsconfig.json +4 -2
  235. package/dist/api/v1/ai-agents.d.ts +0 -63
  236. package/dist/api/v1/ai-agents.js +0 -173
  237. package/src/api/v1/ai-agents.ts +0 -252
@@ -0,0 +1,453 @@
1
+ import { computeBundleDiff } from '@lightdash-tools/common';
2
+ import { describe, expect, it, vi } from 'vitest';
3
+
4
+ import { applyBundleDiff } from './apply-bundle-diff';
5
+
6
+ import type { LightdashClient } from '../client';
7
+ import type { BundleDiffChange, LightdashAiAgentBundle } from '@lightdash-tools/common';
8
+
9
+ const PROJECT_UUID = '550e8400-e29b-41d4-a716-446655440000';
10
+ const AGENT_UUID = '660e8400-e29b-41d4-a716-446655440001';
11
+ const EVAL_UUID = '770e8400-e29b-41d4-a716-446655440002';
12
+ const STALE_AGENT_UUID = '660e8400-e29b-41d4-a716-446655440099';
13
+ const STALE_EVAL_UUID = '770e8400-e29b-41d4-a716-446655440099';
14
+
15
+ const bundle: LightdashAiAgentBundle = {
16
+ apiVersion: 'lightdash.ai/v1alpha1',
17
+ kind: 'LightdashAiAgentBundle',
18
+ metadata: { name: 'test-bundle' },
19
+ spec: {
20
+ projectUuid: '550e8400-e29b-41d4-a716-446655440000',
21
+ agents: [
22
+ {
23
+ key: 'a1',
24
+ name: 'Agent One',
25
+ evaluations: [],
26
+ },
27
+ ],
28
+ },
29
+ };
30
+
31
+ describe('applyBundleDiff', () => {
32
+ it('records API failures without aborting remaining changes', async () => {
33
+ const deleteAgent = vi
34
+ .fn()
35
+ .mockRejectedValueOnce(new Error('delete failed'))
36
+ .mockResolvedValueOnce(undefined);
37
+ const client = {
38
+ v1: {
39
+ aiAgents: {
40
+ createAgent: vi.fn(),
41
+ updateAgent: vi.fn(),
42
+ deleteAgent,
43
+ createEvaluation: vi.fn(),
44
+ updateEvaluation: vi.fn(),
45
+ deleteEvaluation: vi.fn(),
46
+ },
47
+ },
48
+ } as unknown as LightdashClient;
49
+
50
+ const changes: BundleDiffChange[] = [
51
+ {
52
+ resourceType: 'agent',
53
+ operation: 'delete',
54
+ key: 'agent-1',
55
+ path: 'agents[agent-1]',
56
+ },
57
+ {
58
+ resourceType: 'agent',
59
+ operation: 'delete',
60
+ key: 'agent-2',
61
+ path: 'agents[agent-2]',
62
+ },
63
+ ];
64
+
65
+ const result = await applyBundleDiff(client, bundle, changes);
66
+
67
+ expect(deleteAgent).toHaveBeenCalledTimes(2);
68
+ expect(result.applied).toBe(1);
69
+ expect(result.failed).toHaveLength(1);
70
+ expect(result.failed[0]?.reason).toBe('delete failed');
71
+ });
72
+
73
+ it('applies name-matched agent update using agentUuid from diff', async () => {
74
+ const nameMatchedBundle: LightdashAiAgentBundle = {
75
+ apiVersion: 'lightdash.ai/v1alpha1',
76
+ kind: 'LightdashAiAgentBundle',
77
+ metadata: { name: 'test-bundle' },
78
+ spec: {
79
+ projectUuid: PROJECT_UUID,
80
+ agents: [
81
+ {
82
+ key: 'a1',
83
+ name: 'Agent One',
84
+ instruction: 'Updated instruction',
85
+ evaluations: [],
86
+ },
87
+ ],
88
+ },
89
+ };
90
+
91
+ const current = {
92
+ projectUuid: PROJECT_UUID,
93
+ agents: [
94
+ {
95
+ agent: {
96
+ uuid: AGENT_UUID,
97
+ name: 'Agent One',
98
+ description: null,
99
+ instruction: 'Original instruction',
100
+ tags: null,
101
+ },
102
+ evaluations: [],
103
+ },
104
+ ],
105
+ };
106
+
107
+ const diff = computeBundleDiff(nameMatchedBundle, current);
108
+ const updateAgent = vi.fn().mockResolvedValue(undefined);
109
+ const client = {
110
+ v1: {
111
+ aiAgents: {
112
+ createAgent: vi.fn(),
113
+ updateAgent,
114
+ deleteAgent: vi.fn(),
115
+ createEvaluation: vi.fn(),
116
+ updateEvaluation: vi.fn(),
117
+ deleteEvaluation: vi.fn(),
118
+ },
119
+ },
120
+ } as unknown as LightdashClient;
121
+
122
+ const result = await applyBundleDiff(client, nameMatchedBundle, diff.changes);
123
+
124
+ expect(updateAgent).toHaveBeenCalledWith(PROJECT_UUID, AGENT_UUID, {
125
+ uuid: AGENT_UUID,
126
+ name: 'Agent One',
127
+ description: null,
128
+ instruction: 'Updated instruction',
129
+ tags: null,
130
+ });
131
+ expect(result.applied).toBe(1);
132
+ expect(result.failed).toHaveLength(0);
133
+ });
134
+
135
+ it('applies name-matched agent update when bundle uuid is stale', async () => {
136
+ const staleUuidBundle: LightdashAiAgentBundle = {
137
+ apiVersion: 'lightdash.ai/v1alpha1',
138
+ kind: 'LightdashAiAgentBundle',
139
+ metadata: { name: 'test-bundle' },
140
+ spec: {
141
+ projectUuid: PROJECT_UUID,
142
+ agents: [
143
+ {
144
+ key: 'a1',
145
+ uuid: STALE_AGENT_UUID,
146
+ name: 'Agent One',
147
+ instruction: 'Updated instruction',
148
+ evaluations: [],
149
+ },
150
+ ],
151
+ },
152
+ };
153
+
154
+ const current = {
155
+ projectUuid: PROJECT_UUID,
156
+ agents: [
157
+ {
158
+ agent: {
159
+ uuid: AGENT_UUID,
160
+ name: 'Agent One',
161
+ description: null,
162
+ instruction: 'Original instruction',
163
+ tags: null,
164
+ },
165
+ evaluations: [],
166
+ },
167
+ ],
168
+ };
169
+
170
+ const diff = computeBundleDiff(staleUuidBundle, current);
171
+ const updateAgent = vi.fn().mockResolvedValue(undefined);
172
+ const client = {
173
+ v1: {
174
+ aiAgents: {
175
+ createAgent: vi.fn(),
176
+ updateAgent,
177
+ deleteAgent: vi.fn(),
178
+ createEvaluation: vi.fn(),
179
+ updateEvaluation: vi.fn(),
180
+ deleteEvaluation: vi.fn(),
181
+ },
182
+ },
183
+ } as unknown as LightdashClient;
184
+
185
+ const result = await applyBundleDiff(client, staleUuidBundle, diff.changes);
186
+
187
+ expect(updateAgent).toHaveBeenCalledWith(
188
+ PROJECT_UUID,
189
+ AGENT_UUID,
190
+ expect.objectContaining({ uuid: AGENT_UUID, instruction: 'Updated instruction' }),
191
+ );
192
+ expect(updateAgent).not.toHaveBeenCalledWith(PROJECT_UUID, STALE_AGENT_UUID, expect.anything());
193
+ expect(result.applied).toBe(1);
194
+ expect(result.failed).toHaveLength(0);
195
+ });
196
+
197
+ it('applies title-matched evaluation update using evaluationUuid from diff', async () => {
198
+ const titleMatchedBundle: LightdashAiAgentBundle = {
199
+ apiVersion: 'lightdash.ai/v1alpha1',
200
+ kind: 'LightdashAiAgentBundle',
201
+ metadata: { name: 'test-bundle' },
202
+ spec: {
203
+ projectUuid: PROJECT_UUID,
204
+ agents: [
205
+ {
206
+ key: 'a1',
207
+ uuid: AGENT_UUID,
208
+ name: 'Agent One',
209
+ evaluations: [
210
+ {
211
+ key: 'e1',
212
+ title: 'Smoke Tests',
213
+ prompts: [{ prompt: 'Updated prompt', expectedResponse: null }],
214
+ },
215
+ ],
216
+ },
217
+ ],
218
+ },
219
+ };
220
+
221
+ const current = {
222
+ projectUuid: PROJECT_UUID,
223
+ agents: [
224
+ {
225
+ agent: {
226
+ uuid: AGENT_UUID,
227
+ name: 'Agent One',
228
+ description: null,
229
+ instruction: null,
230
+ tags: null,
231
+ },
232
+ evaluations: [
233
+ {
234
+ evalUuid: EVAL_UUID,
235
+ title: 'Smoke Tests',
236
+ description: null,
237
+ prompts: [
238
+ {
239
+ type: 'string' as const,
240
+ prompt: 'Original prompt',
241
+ expectedResponse: null,
242
+ },
243
+ ],
244
+ },
245
+ ],
246
+ },
247
+ ],
248
+ };
249
+
250
+ const diff = computeBundleDiff(titleMatchedBundle, current);
251
+ const updateEvaluation = vi.fn().mockResolvedValue(undefined);
252
+ const client = {
253
+ v1: {
254
+ aiAgents: {
255
+ createAgent: vi.fn(),
256
+ updateAgent: vi.fn(),
257
+ deleteAgent: vi.fn(),
258
+ createEvaluation: vi.fn(),
259
+ updateEvaluation,
260
+ deleteEvaluation: vi.fn(),
261
+ },
262
+ },
263
+ } as unknown as LightdashClient;
264
+
265
+ const result = await applyBundleDiff(client, titleMatchedBundle, diff.changes);
266
+
267
+ expect(updateEvaluation).toHaveBeenCalledWith(
268
+ PROJECT_UUID,
269
+ AGENT_UUID,
270
+ EVAL_UUID,
271
+ expect.objectContaining({
272
+ title: 'Smoke Tests',
273
+ prompts: [{ prompt: 'Updated prompt', expectedResponse: null }],
274
+ }),
275
+ );
276
+ expect(result.applied).toBe(1);
277
+ expect(result.failed).toHaveLength(0);
278
+ });
279
+
280
+ it('applies title-matched evaluation update when bundle uuid is stale', async () => {
281
+ const staleEvalBundle: LightdashAiAgentBundle = {
282
+ apiVersion: 'lightdash.ai/v1alpha1',
283
+ kind: 'LightdashAiAgentBundle',
284
+ metadata: { name: 'test-bundle' },
285
+ spec: {
286
+ projectUuid: PROJECT_UUID,
287
+ agents: [
288
+ {
289
+ key: 'a1',
290
+ uuid: AGENT_UUID,
291
+ name: 'Agent One',
292
+ evaluations: [
293
+ {
294
+ key: 'e1',
295
+ uuid: STALE_EVAL_UUID,
296
+ title: 'Smoke Tests',
297
+ prompts: [{ prompt: 'Updated prompt', expectedResponse: null }],
298
+ },
299
+ ],
300
+ },
301
+ ],
302
+ },
303
+ };
304
+
305
+ const current = {
306
+ projectUuid: PROJECT_UUID,
307
+ agents: [
308
+ {
309
+ agent: {
310
+ uuid: AGENT_UUID,
311
+ name: 'Agent One',
312
+ description: null,
313
+ instruction: null,
314
+ tags: null,
315
+ },
316
+ evaluations: [
317
+ {
318
+ evalUuid: EVAL_UUID,
319
+ title: 'Smoke Tests',
320
+ description: null,
321
+ prompts: [
322
+ {
323
+ type: 'string' as const,
324
+ prompt: 'Original prompt',
325
+ expectedResponse: null,
326
+ },
327
+ ],
328
+ },
329
+ ],
330
+ },
331
+ ],
332
+ };
333
+
334
+ const diff = computeBundleDiff(staleEvalBundle, current);
335
+ const updateEvaluation = vi.fn().mockResolvedValue(undefined);
336
+ const client = {
337
+ v1: {
338
+ aiAgents: {
339
+ createAgent: vi.fn(),
340
+ updateAgent: vi.fn(),
341
+ deleteAgent: vi.fn(),
342
+ createEvaluation: vi.fn(),
343
+ updateEvaluation,
344
+ deleteEvaluation: vi.fn(),
345
+ },
346
+ },
347
+ } as unknown as LightdashClient;
348
+
349
+ const result = await applyBundleDiff(client, staleEvalBundle, diff.changes);
350
+
351
+ expect(updateEvaluation).toHaveBeenCalledWith(
352
+ PROJECT_UUID,
353
+ AGENT_UUID,
354
+ EVAL_UUID,
355
+ expect.objectContaining({
356
+ title: 'Smoke Tests',
357
+ prompts: [{ prompt: 'Updated prompt', expectedResponse: null }],
358
+ }),
359
+ );
360
+ expect(updateEvaluation).not.toHaveBeenCalledWith(
361
+ PROJECT_UUID,
362
+ AGENT_UUID,
363
+ STALE_EVAL_UUID,
364
+ expect.anything(),
365
+ );
366
+ expect(result.applied).toBe(1);
367
+ expect(result.failed).toHaveLength(0);
368
+ });
369
+
370
+ it('clears evaluation description when bundle omits it', async () => {
371
+ const bundleWithoutDescription: LightdashAiAgentBundle = {
372
+ apiVersion: 'lightdash.ai/v1alpha1',
373
+ kind: 'LightdashAiAgentBundle',
374
+ metadata: { name: 'test-bundle' },
375
+ spec: {
376
+ projectUuid: PROJECT_UUID,
377
+ agents: [
378
+ {
379
+ key: 'a1',
380
+ uuid: AGENT_UUID,
381
+ name: 'Agent One',
382
+ evaluations: [
383
+ {
384
+ key: 'e1',
385
+ uuid: EVAL_UUID,
386
+ title: 'Smoke Tests',
387
+ prompts: [{ prompt: 'Same prompt', expectedResponse: null }],
388
+ },
389
+ ],
390
+ },
391
+ ],
392
+ },
393
+ };
394
+
395
+ const current = {
396
+ projectUuid: PROJECT_UUID,
397
+ agents: [
398
+ {
399
+ agent: {
400
+ uuid: AGENT_UUID,
401
+ name: 'Agent One',
402
+ description: null,
403
+ instruction: null,
404
+ tags: null,
405
+ },
406
+ evaluations: [
407
+ {
408
+ evalUuid: EVAL_UUID,
409
+ title: 'Smoke Tests',
410
+ description: 'Old description',
411
+ prompts: [
412
+ {
413
+ type: 'string' as const,
414
+ prompt: 'Same prompt',
415
+ expectedResponse: null,
416
+ },
417
+ ],
418
+ },
419
+ ],
420
+ },
421
+ ],
422
+ };
423
+
424
+ const diff = computeBundleDiff(bundleWithoutDescription, current);
425
+ const updateEvaluation = vi.fn().mockResolvedValue(undefined);
426
+ const client = {
427
+ v1: {
428
+ aiAgents: {
429
+ createAgent: vi.fn(),
430
+ updateAgent: vi.fn(),
431
+ deleteAgent: vi.fn(),
432
+ createEvaluation: vi.fn(),
433
+ updateEvaluation,
434
+ deleteEvaluation: vi.fn(),
435
+ },
436
+ },
437
+ } as unknown as LightdashClient;
438
+
439
+ const result = await applyBundleDiff(client, bundleWithoutDescription, diff.changes);
440
+
441
+ expect(updateEvaluation).toHaveBeenCalledWith(
442
+ PROJECT_UUID,
443
+ AGENT_UUID,
444
+ EVAL_UUID,
445
+ expect.objectContaining({
446
+ title: 'Smoke Tests',
447
+ description: null,
448
+ }),
449
+ );
450
+ expect(result.applied).toBe(1);
451
+ expect(result.failed).toHaveLength(0);
452
+ });
453
+ });
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Applies a computed bundle diff to Lightdash.
3
+ */
4
+
5
+ import { applyAgentChange } from './apply-agent-change';
6
+ import { recordApplyFailure } from './apply-context';
7
+ import { applyEvaluationChange } from './apply-evaluation-change';
8
+
9
+ import type { ApplyBundleContext, ApplyBundleDiffFailure } from './apply-context';
10
+ import type { LightdashClient } from '../client';
11
+ import type { BundleDiffChange, LightdashAiAgentBundle } from '@lightdash-tools/common';
12
+
13
+ export type { ApplyBundleDiffFailure } from './apply-context';
14
+
15
+ export interface ApplyBundleDiffResult {
16
+ applied: number;
17
+ skipped: number;
18
+ failed: ApplyBundleDiffFailure[];
19
+ }
20
+
21
+ async function applyChange(ctx: ApplyBundleContext, change: BundleDiffChange): Promise<boolean> {
22
+ if (change.resourceType === 'agent') {
23
+ return applyAgentChange(ctx, change);
24
+ }
25
+ if (change.resourceType === 'evaluation') {
26
+ return applyEvaluationChange(ctx, change);
27
+ }
28
+ return false;
29
+ }
30
+
31
+ export async function applyBundleDiff(
32
+ client: LightdashClient,
33
+ bundle: LightdashAiAgentBundle,
34
+ changes: BundleDiffChange[],
35
+ ): Promise<ApplyBundleDiffResult> {
36
+ const ctx: ApplyBundleContext = {
37
+ client,
38
+ bundle,
39
+ projectUuid: bundle.spec.projectUuid,
40
+ agentUuidByKey: new Map<string, string>(),
41
+ failed: [],
42
+ };
43
+
44
+ let applied = 0;
45
+ let skipped = 0;
46
+
47
+ for (const change of changes) {
48
+ if (change.operation === 'noop') {
49
+ skipped++;
50
+ continue;
51
+ }
52
+
53
+ try {
54
+ if (await applyChange(ctx, change)) {
55
+ applied++;
56
+ }
57
+ } catch (error) {
58
+ recordApplyFailure(ctx, change, error instanceof Error ? error.message : String(error));
59
+ }
60
+ }
61
+
62
+ return { applied, skipped, failed: ctx.failed };
63
+ }
@@ -0,0 +1,23 @@
1
+ import type { LightdashClient } from '../client';
2
+ import type { BundleDiffChange, LightdashAiAgentBundle } from '@lightdash-tools/common';
3
+
4
+ export interface ApplyBundleDiffFailure {
5
+ change: BundleDiffChange;
6
+ reason: string;
7
+ }
8
+
9
+ export interface ApplyBundleContext {
10
+ client: LightdashClient;
11
+ bundle: LightdashAiAgentBundle;
12
+ projectUuid: string;
13
+ agentUuidByKey: Map<string, string>;
14
+ failed: ApplyBundleDiffFailure[];
15
+ }
16
+
17
+ export function recordApplyFailure(
18
+ ctx: ApplyBundleContext,
19
+ change: BundleDiffChange,
20
+ reason: string,
21
+ ): void {
22
+ ctx.failed.push({ change, reason });
23
+ }
@@ -0,0 +1,125 @@
1
+ import { recordApplyFailure } from './apply-context';
2
+
3
+ import type { ApplyBundleContext } from './apply-context';
4
+ import type {
5
+ BundleAgentSpec,
6
+ BundleDiffChange,
7
+ BundleEvaluationSpec,
8
+ CreateEvaluationBody,
9
+ UpdateEvaluationBody,
10
+ } from '@lightdash-tools/common';
11
+
12
+ function findDesiredAgent(ctx: ApplyBundleContext, key: string): BundleAgentSpec | undefined {
13
+ return ctx.bundle.spec.agents.find((a) => a.key === key);
14
+ }
15
+
16
+ function findDesiredEvaluation(
17
+ agent: BundleAgentSpec,
18
+ key: string,
19
+ ): BundleEvaluationSpec | undefined {
20
+ return agent.evaluations.find((e) => e.key === key);
21
+ }
22
+
23
+ function promptsToCreateBody(
24
+ prompts: BundleEvaluationSpec['prompts'],
25
+ ): CreateEvaluationBody['prompts'] {
26
+ return prompts.map((p) =>
27
+ 'prompt' in p
28
+ ? { prompt: p.prompt, expectedResponse: p.expectedResponse ?? null }
29
+ : {
30
+ threadUuid: p.threadUuid,
31
+ promptUuid: p.promptUuid,
32
+ expectedResponse: p.expectedResponse ?? null,
33
+ },
34
+ );
35
+ }
36
+
37
+ function buildUpdateEvaluationBody(desiredEval: BundleEvaluationSpec): UpdateEvaluationBody {
38
+ // OpenAPI types omit null, but the API accepts null to clear an existing description.
39
+ return {
40
+ title: desiredEval.title,
41
+ description: desiredEval.description ?? null,
42
+ prompts: promptsToCreateBody(desiredEval.prompts),
43
+ } as UpdateEvaluationBody;
44
+ }
45
+
46
+ function resolveAgentUuid(
47
+ ctx: ApplyBundleContext,
48
+ change: BundleDiffChange,
49
+ desiredAgent?: BundleAgentSpec,
50
+ ): string | undefined {
51
+ return (
52
+ change.agentUuid ??
53
+ desiredAgent?.uuid ??
54
+ (change.agentKey ? ctx.agentUuidByKey.get(change.agentKey) : undefined)
55
+ );
56
+ }
57
+
58
+ export async function applyEvaluationChange(
59
+ ctx: ApplyBundleContext,
60
+ change: BundleDiffChange,
61
+ ): Promise<boolean> {
62
+ const { client, projectUuid } = ctx;
63
+ const desiredAgent = change.agentKey ? findDesiredAgent(ctx, change.agentKey) : undefined;
64
+
65
+ if (change.operation === 'create' || change.operation === 'update') {
66
+ const desiredEval =
67
+ desiredAgent && change.key ? findDesiredEvaluation(desiredAgent, change.key) : undefined;
68
+ if (!desiredAgent) {
69
+ recordApplyFailure(ctx, change, `Agent spec not found for key '${change.agentKey ?? ''}'`);
70
+ return false;
71
+ }
72
+ if (!desiredEval) {
73
+ recordApplyFailure(ctx, change, `Evaluation spec not found for key '${change.key}'`);
74
+ return false;
75
+ }
76
+ const agentUuid = resolveAgentUuid(ctx, change, desiredAgent);
77
+ if (!agentUuid) {
78
+ recordApplyFailure(
79
+ ctx,
80
+ change,
81
+ `Could not resolve agent UUID for evaluation key '${change.key}'`,
82
+ );
83
+ return false;
84
+ }
85
+
86
+ if (change.operation === 'create') {
87
+ const createEvalBody: CreateEvaluationBody = {
88
+ title: desiredEval.title,
89
+ ...(desiredEval.description != null ? { description: desiredEval.description } : {}),
90
+ prompts: promptsToCreateBody(desiredEval.prompts),
91
+ };
92
+ await client.v1.aiAgents.createEvaluation(projectUuid, agentUuid, createEvalBody);
93
+ return true;
94
+ }
95
+
96
+ const evalUuid = change.evaluationUuid ?? desiredEval.uuid;
97
+ if (!evalUuid) {
98
+ recordApplyFailure(ctx, change, `Could not resolve evaluation UUID for key '${change.key}'`);
99
+ return false;
100
+ }
101
+ await client.v1.aiAgents.updateEvaluation(
102
+ projectUuid,
103
+ agentUuid,
104
+ evalUuid,
105
+ buildUpdateEvaluationBody(desiredEval),
106
+ );
107
+ return true;
108
+ }
109
+
110
+ if (change.operation === 'delete') {
111
+ const agentUuid = resolveAgentUuid(ctx, change, desiredAgent);
112
+ if (!agentUuid) {
113
+ recordApplyFailure(
114
+ ctx,
115
+ change,
116
+ `Could not resolve agent UUID for evaluation delete '${change.key}'`,
117
+ );
118
+ return false;
119
+ }
120
+ await client.v1.aiAgents.deleteEvaluation(projectUuid, agentUuid, change.key);
121
+ return true;
122
+ }
123
+
124
+ return false;
125
+ }
@@ -0,0 +1,51 @@
1
+ import { describe, expect, it, vi } from 'vitest';
2
+
3
+ import { fetchBundleCurrentState } from './fetch-bundle-state';
4
+
5
+ import type { LightdashClient } from '../client';
6
+ import type { LightdashAiAgentBundle } from '@lightdash-tools/common';
7
+
8
+ const bundle: LightdashAiAgentBundle = {
9
+ apiVersion: 'lightdash.ai/v1alpha1',
10
+ kind: 'LightdashAiAgentBundle',
11
+ metadata: { name: 'test-bundle' },
12
+ spec: {
13
+ projectUuid: '550e8400-e29b-41d4-a716-446655440000',
14
+ agents: [
15
+ {
16
+ key: 'a1',
17
+ name: 'Agent One',
18
+ evaluations: [],
19
+ },
20
+ ],
21
+ },
22
+ };
23
+
24
+ describe('fetchBundleCurrentState', () => {
25
+ it('fetches all project agents so orphan deletes can be computed', async () => {
26
+ const listAgents = vi.fn().mockResolvedValue([
27
+ { uuid: 'agent-in-bundle', name: 'Agent One' },
28
+ { uuid: 'orphan-agent', name: 'Orphan' },
29
+ ]);
30
+ const listEvaluations = vi.fn().mockResolvedValue([]);
31
+ const getEvaluation = vi.fn();
32
+
33
+ const client = {
34
+ v1: {
35
+ aiAgents: {
36
+ listAgents,
37
+ listEvaluations,
38
+ getEvaluation,
39
+ },
40
+ },
41
+ } as unknown as LightdashClient;
42
+
43
+ const state = await fetchBundleCurrentState(client, bundle);
44
+
45
+ expect(listAgents).toHaveBeenCalledWith(bundle.spec.projectUuid);
46
+ expect(state.agents.map((entry) => entry.agent.uuid)).toEqual([
47
+ 'agent-in-bundle',
48
+ 'orphan-agent',
49
+ ]);
50
+ });
51
+ });