@lightdash-tools/common 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.
- package/dist/agentops/extract-yaml-project.d.ts +7 -0
- package/dist/agentops/extract-yaml-project.js +42 -0
- package/dist/agentops/formatters.d.ts +5 -0
- package/dist/agentops/formatters.js +47 -0
- package/dist/agentops/snapshots.d.ts +32 -0
- package/dist/agentops/snapshots.js +34 -0
- package/dist/agentops/types.d.ts +224 -0
- package/dist/agentops/types.js +430 -0
- package/dist/argument-validation.d.ts +25 -0
- package/dist/argument-validation.js +199 -0
- package/dist/argument-validation.test.d.ts +1 -0
- package/dist/argument-validation.test.js +183 -0
- package/dist/audit.d.ts +9 -2
- package/dist/audit.js +17 -2
- package/dist/audit.test.d.ts +1 -0
- package/dist/audit.test.js +73 -0
- package/dist/env.d.ts +8 -0
- package/dist/env.js +9 -1
- package/dist/index.d.ts +7 -1
- package/dist/index.js +14 -0
- package/dist/input-validation.d.ts +35 -0
- package/dist/input-validation.js +87 -0
- package/dist/input-validation.test.js +93 -0
- package/dist/operations/agent-safe.d.ts +7 -0
- package/dist/operations/agent-safe.js +16 -0
- package/dist/operations/ai-agents.d.ts +6 -0
- package/dist/operations/ai-agents.js +411 -0
- package/dist/operations/index.d.ts +5 -0
- package/dist/operations/index.js +16 -0
- package/dist/operations/parity.test.d.ts +1 -0
- package/dist/operations/parity.test.js +28 -0
- package/dist/operations/registry.d.ts +10 -0
- package/dist/operations/registry.js +35 -0
- package/dist/operations/registry.test.d.ts +1 -0
- package/dist/operations/registry.test.js +196 -0
- package/dist/operations/types.d.ts +69 -0
- package/dist/operations/types.js +70 -0
- package/dist/operations/types.test.d.ts +1 -0
- package/dist/operations/types.test.js +104 -0
- package/dist/operations/users.d.ts +6 -0
- package/dist/operations/users.js +60 -0
- package/dist/safety.d.ts +27 -2
- package/dist/safety.js +52 -5
- package/dist/safety.test.js +217 -4
- package/dist/types/generated/openapi-types.d.ts +32623 -11194
- package/dist/types/lightdash-api.d.ts +1 -1
- package/dist/types/v1/ai-agents.d.ts +112 -3
- package/dist/types/v1/charts.d.ts +5 -3
- package/dist/types/v1/dashboards.d.ts +13 -0
- package/dist/types/v1/index.d.ts +45 -8
- package/dist/types/v1/index.js +1 -1
- package/dist/types/v1/lightdash-api.d.ts +107 -16
- package/dist/types/v1/users.d.ts +4 -0
- package/dist/types/v2/content.d.ts +2 -0
- package/dist/types/v2/content.js +8 -0
- package/dist/types/v2/index.d.ts +11 -9
- package/dist/types/v2/index.js +1 -1
- package/package.json +16 -3
- package/src/agentops/extract-yaml-project.ts +42 -0
- package/src/agentops/formatters.ts +52 -0
- package/src/agentops/snapshots.ts +56 -0
- package/src/agentops/types.ts +599 -0
- package/src/argument-validation.test.ts +270 -0
- package/src/argument-validation.ts +240 -0
- package/src/audit.test.ts +87 -0
- package/src/audit.ts +21 -3
- package/src/env.ts +9 -0
- package/src/index.test.ts +1 -0
- package/src/index.ts +16 -1
- package/src/input-validation.test.ts +157 -1
- package/src/input-validation.ts +95 -0
- package/src/operations/agent-safe.ts +18 -0
- package/src/operations/ai-agents.ts +451 -0
- package/src/operations/index.ts +19 -0
- package/src/operations/parity.test.ts +35 -0
- package/src/operations/registry.test.ts +219 -0
- package/src/operations/registry.ts +44 -0
- package/src/operations/types.test.ts +148 -0
- package/src/operations/types.ts +168 -0
- package/src/operations/users.ts +67 -0
- package/src/safety.test.ts +276 -4
- package/src/safety.ts +72 -4
- package/src/types/generated/openapi-types.ts +33701 -11813
- package/src/types/lightdash-api.ts +39 -0
- package/src/types/v1/ai-agents.ts +133 -3
- package/src/types/v1/charts.ts +5 -3
- package/src/types/v1/dashboards.ts +19 -0
- package/src/types/v1/index.ts +51 -8
- package/src/types/v1/lightdash-api.ts +129 -20
- package/src/types/v1/users.ts +4 -0
- package/src/types/v2/content.ts +8 -0
- package/src/types/v2/index.ts +11 -9
- package/tsconfig.json +1 -0
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { GatePolicyEvaluation, LightdashAiEvaluationGate } from './types';
|
|
2
|
+
|
|
3
|
+
function escapeXml(text: string): string {
|
|
4
|
+
return text
|
|
5
|
+
.replace(/&/g, '&')
|
|
6
|
+
.replace(/</g, '<')
|
|
7
|
+
.replace(/>/g, '>')
|
|
8
|
+
.replace(/"/g, '"')
|
|
9
|
+
.replace(/'/g, ''');
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function formatGateJUnit(
|
|
13
|
+
gate: LightdashAiEvaluationGate,
|
|
14
|
+
evaluation: GatePolicyEvaluation,
|
|
15
|
+
): string {
|
|
16
|
+
const name = escapeXml(gate.metadata.name);
|
|
17
|
+
const failures = evaluation.passed
|
|
18
|
+
? ''
|
|
19
|
+
: ` <failure message="${escapeXml(evaluation.reasons.join('; '))}">Gate policy failed</failure>\n`;
|
|
20
|
+
return `<?xml version="1.0" encoding="UTF-8"?>\n<testsuite name="${name}" tests="1" failures="${evaluation.passed ? 0 : 1}">\n <testcase name="${name}" classname="agentops.evaluate-gate">\n${failures} </testcase>\n</testsuite>\n`;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function formatGateTimeoutJUnit(gate: LightdashAiEvaluationGate): string {
|
|
24
|
+
const name = escapeXml(gate.metadata.name);
|
|
25
|
+
return `<?xml version="1.0" encoding="UTF-8"?>\n<testsuite name="${name}" tests="1" failures="1">\n <testcase name="timeout"><failure>Timed out</failure></testcase>\n</testsuite>\n`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function formatGateTimeoutMarkdown(gate: LightdashAiEvaluationGate): string {
|
|
29
|
+
return `# Evaluation Gate: ${gate.metadata.name}\n\n**Result:** TIMEOUT\n`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function formatGateMarkdown(
|
|
33
|
+
gate: LightdashAiEvaluationGate,
|
|
34
|
+
evaluation: GatePolicyEvaluation,
|
|
35
|
+
): string {
|
|
36
|
+
const lines = [
|
|
37
|
+
`# Evaluation Gate: ${gate.metadata.name}`,
|
|
38
|
+
'',
|
|
39
|
+
`**Result:** ${evaluation.passed ? 'PASSED' : 'FAILED'} (exit ${evaluation.exitCode})`,
|
|
40
|
+
'',
|
|
41
|
+
'## Metrics',
|
|
42
|
+
`- Run status: ${evaluation.metrics.runStatus}`,
|
|
43
|
+
`- Passed assessments: ${evaluation.metrics.passedAssessments}`,
|
|
44
|
+
`- Failed assessments: ${evaluation.metrics.failedAssessments}`,
|
|
45
|
+
`- Pass rate: ${evaluation.metrics.passRate ?? 'n/a'}`,
|
|
46
|
+
'',
|
|
47
|
+
];
|
|
48
|
+
if (evaluation.reasons.length > 0) {
|
|
49
|
+
lines.push('## Reasons', ...evaluation.reasons.map((r) => `- ${r}`), '');
|
|
50
|
+
}
|
|
51
|
+
return lines.join('\n');
|
|
52
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { normalizeEvaluationPrompt } from './types';
|
|
2
|
+
|
|
3
|
+
import type { AgentStateSnapshot, EvaluationStateSnapshot, GateRunSnapshot } from './types';
|
|
4
|
+
|
|
5
|
+
export function toGateRunSnapshot(run: {
|
|
6
|
+
runUuid: string;
|
|
7
|
+
status: GateRunSnapshot['status'];
|
|
8
|
+
passedAssessments: number;
|
|
9
|
+
failedAssessments: number;
|
|
10
|
+
completedAt: string | null;
|
|
11
|
+
}): GateRunSnapshot {
|
|
12
|
+
return {
|
|
13
|
+
runUuid: run.runUuid,
|
|
14
|
+
status: run.status,
|
|
15
|
+
passedAssessments: run.passedAssessments,
|
|
16
|
+
failedAssessments: run.failedAssessments,
|
|
17
|
+
completedAt: run.completedAt,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function toAgentSnapshot(agent: {
|
|
22
|
+
uuid: string;
|
|
23
|
+
name: string;
|
|
24
|
+
description: string | null;
|
|
25
|
+
instruction: string | null;
|
|
26
|
+
tags: string[] | null;
|
|
27
|
+
enableDataAccess?: boolean;
|
|
28
|
+
enableSelfImprovement?: boolean;
|
|
29
|
+
}): AgentStateSnapshot {
|
|
30
|
+
return {
|
|
31
|
+
uuid: agent.uuid,
|
|
32
|
+
name: agent.name,
|
|
33
|
+
description: agent.description,
|
|
34
|
+
instruction: agent.instruction,
|
|
35
|
+
tags: agent.tags,
|
|
36
|
+
enableDataAccess: agent.enableDataAccess,
|
|
37
|
+
enableSelfImprovement: agent.enableSelfImprovement,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function toEvaluationSnapshot(evaluation: {
|
|
42
|
+
evalUuid: string;
|
|
43
|
+
title: string;
|
|
44
|
+
description: string | null;
|
|
45
|
+
prompts: Array<
|
|
46
|
+
| { type?: 'string'; prompt: string; expectedResponse: string | null }
|
|
47
|
+
| { type?: 'thread'; threadUuid: string; promptUuid: string; expectedResponse: string | null }
|
|
48
|
+
>;
|
|
49
|
+
}): EvaluationStateSnapshot {
|
|
50
|
+
return {
|
|
51
|
+
evalUuid: evaluation.evalUuid,
|
|
52
|
+
title: evaluation.title,
|
|
53
|
+
description: evaluation.description,
|
|
54
|
+
prompts: evaluation.prompts.map((p) => normalizeEvaluationPrompt(p)),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
@@ -0,0 +1,599 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AgentOps bundle and evaluation gate YAML types (RFC Phase 2).
|
|
3
|
+
* Kubernetes-style documents with apiVersion, kind, metadata, and spec.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { parse as parseYaml } from 'yaml';
|
|
7
|
+
import { z } from 'zod';
|
|
8
|
+
|
|
9
|
+
// ─── Shared metadata ─────────────────────────────────────────────────────────
|
|
10
|
+
|
|
11
|
+
const metadataSchema = z.object({
|
|
12
|
+
name: z.string().min(1),
|
|
13
|
+
labels: z.record(z.string(), z.string()).optional(),
|
|
14
|
+
annotations: z.record(z.string(), z.string()).optional(),
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
// ─── Bundle schemas ────────────────────────────────────────────────────────────
|
|
18
|
+
|
|
19
|
+
const bundleEvaluationPromptSchema = z.union([
|
|
20
|
+
z.object({
|
|
21
|
+
prompt: z.string().min(1),
|
|
22
|
+
expectedResponse: z.string().nullable().optional(),
|
|
23
|
+
}),
|
|
24
|
+
z.object({
|
|
25
|
+
threadUuid: z.string().uuid(),
|
|
26
|
+
promptUuid: z.string().uuid(),
|
|
27
|
+
expectedResponse: z.string().nullable().optional(),
|
|
28
|
+
}),
|
|
29
|
+
]);
|
|
30
|
+
|
|
31
|
+
const bundleEvaluationSchema = z.object({
|
|
32
|
+
key: z.string().min(1),
|
|
33
|
+
uuid: z.string().uuid().optional(),
|
|
34
|
+
title: z.string().min(1),
|
|
35
|
+
description: z.string().nullable().optional(),
|
|
36
|
+
prompts: z.array(bundleEvaluationPromptSchema).min(1),
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const bundleAgentSchema = z.object({
|
|
40
|
+
key: z.string().min(1),
|
|
41
|
+
uuid: z.string().uuid().optional(),
|
|
42
|
+
name: z.string().min(1),
|
|
43
|
+
description: z.string().nullable().optional(),
|
|
44
|
+
instruction: z.string().nullable().optional(),
|
|
45
|
+
tags: z.array(z.string()).nullable().optional(),
|
|
46
|
+
enableDataAccess: z.boolean().optional(),
|
|
47
|
+
enableSelfImprovement: z.boolean().optional(),
|
|
48
|
+
evaluations: z.array(bundleEvaluationSchema).optional().default([]),
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
export const lightdashAiAgentBundleSchema = z.object({
|
|
52
|
+
apiVersion: z.literal('lightdash.ai/v1alpha1'),
|
|
53
|
+
kind: z.literal('LightdashAiAgentBundle'),
|
|
54
|
+
metadata: metadataSchema,
|
|
55
|
+
spec: z.object({
|
|
56
|
+
projectUuid: z.string().uuid(),
|
|
57
|
+
agents: z.array(bundleAgentSchema).min(1),
|
|
58
|
+
}),
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
export type LightdashAiAgentBundle = z.infer<typeof lightdashAiAgentBundleSchema>;
|
|
62
|
+
export type BundleAgentSpec = z.infer<typeof bundleAgentSchema>;
|
|
63
|
+
export type BundleEvaluationSpec = z.infer<typeof bundleEvaluationSchema>;
|
|
64
|
+
|
|
65
|
+
// ─── Gate schemas ──────────────────────────────────────────────────────────────
|
|
66
|
+
|
|
67
|
+
export const gatePolicySchema = z.object({
|
|
68
|
+
minPassRate: z.number().min(0).max(1).optional(),
|
|
69
|
+
maxFailedAssessments: z.number().int().min(0).optional(),
|
|
70
|
+
minPassedAssessments: z.number().int().min(0).optional(),
|
|
71
|
+
requireAllPassed: z.boolean().optional(),
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
export const lightdashAiEvaluationGateSchema = z.object({
|
|
75
|
+
apiVersion: z.literal('lightdash.ai/v1alpha1'),
|
|
76
|
+
kind: z.literal('LightdashAiEvaluationGate'),
|
|
77
|
+
metadata: metadataSchema,
|
|
78
|
+
spec: z.object({
|
|
79
|
+
projectUuid: z.string().uuid(),
|
|
80
|
+
agentUuid: z.string().uuid(),
|
|
81
|
+
evaluationUuid: z.string().uuid(),
|
|
82
|
+
policy: gatePolicySchema,
|
|
83
|
+
runUuid: z.string().uuid().optional(),
|
|
84
|
+
triggerRun: z.boolean().optional().default(false),
|
|
85
|
+
}),
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
export type LightdashAiEvaluationGate = z.infer<typeof lightdashAiEvaluationGateSchema>;
|
|
89
|
+
export type GatePolicy = z.infer<typeof gatePolicySchema>;
|
|
90
|
+
|
|
91
|
+
// ─── Gate exit codes (RFC Phase 2) ─────────────────────────────────────────────
|
|
92
|
+
|
|
93
|
+
export const GateExitCode = {
|
|
94
|
+
PASSED: 0,
|
|
95
|
+
ERROR: 1,
|
|
96
|
+
INVALID: 2,
|
|
97
|
+
POLICY_FAILED: 3,
|
|
98
|
+
TIMEOUT: 4,
|
|
99
|
+
RUN_FAILED: 5,
|
|
100
|
+
RUN_IN_PROGRESS: 6,
|
|
101
|
+
} as const;
|
|
102
|
+
|
|
103
|
+
export type GateExitCodeValue = (typeof GateExitCode)[keyof typeof GateExitCode];
|
|
104
|
+
|
|
105
|
+
// ─── Diff / drift types ────────────────────────────────────────────────────────
|
|
106
|
+
|
|
107
|
+
export type DiffOperation = 'create' | 'delete' | 'noop' | 'update';
|
|
108
|
+
|
|
109
|
+
export type BundleResourceType = 'agent' | 'evaluation';
|
|
110
|
+
|
|
111
|
+
export interface BundleDiffChange {
|
|
112
|
+
resourceType: BundleResourceType;
|
|
113
|
+
operation: DiffOperation;
|
|
114
|
+
key: string;
|
|
115
|
+
/** Logical agent key from the bundle spec. */
|
|
116
|
+
agentKey?: string;
|
|
117
|
+
/** Resolved agent UUID when known (required for evaluation API calls). */
|
|
118
|
+
agentUuid?: string;
|
|
119
|
+
/** Resolved evaluation UUID when known (required for title-matched evaluation updates). */
|
|
120
|
+
evaluationUuid?: string;
|
|
121
|
+
path: string;
|
|
122
|
+
fields?: Record<string, { from: unknown; to: unknown }>;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export interface BundleDiffResult {
|
|
126
|
+
bundleName: string;
|
|
127
|
+
projectUuid: string;
|
|
128
|
+
changes: BundleDiffChange[];
|
|
129
|
+
hasDrift: boolean;
|
|
130
|
+
summary: {
|
|
131
|
+
creates: number;
|
|
132
|
+
updates: number;
|
|
133
|
+
deletes: number;
|
|
134
|
+
noops: number;
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Minimal agent shape for diffing (from API or desired state). */
|
|
139
|
+
export interface AgentStateSnapshot {
|
|
140
|
+
uuid: string;
|
|
141
|
+
name: string;
|
|
142
|
+
description: string | null;
|
|
143
|
+
instruction: string | null;
|
|
144
|
+
tags: string[] | null;
|
|
145
|
+
enableDataAccess?: boolean;
|
|
146
|
+
enableSelfImprovement?: boolean;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Minimal evaluation shape for diffing. */
|
|
150
|
+
export interface EvaluationStateSnapshot {
|
|
151
|
+
evalUuid: string;
|
|
152
|
+
title: string;
|
|
153
|
+
description: string | null;
|
|
154
|
+
prompts: Array<
|
|
155
|
+
| { type: 'string'; prompt: string; expectedResponse: string | null }
|
|
156
|
+
| { type: 'thread'; threadUuid: string; promptUuid: string; expectedResponse: string | null }
|
|
157
|
+
>;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export interface BundleCurrentState {
|
|
161
|
+
projectUuid: string;
|
|
162
|
+
agents: Array<{
|
|
163
|
+
agent: AgentStateSnapshot;
|
|
164
|
+
evaluations: EvaluationStateSnapshot[];
|
|
165
|
+
}>;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export interface GateRunSnapshot {
|
|
169
|
+
runUuid: string;
|
|
170
|
+
status: 'completed' | 'failed' | 'pending' | 'running';
|
|
171
|
+
passedAssessments: number;
|
|
172
|
+
failedAssessments: number;
|
|
173
|
+
completedAt: string | null;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export interface GatePolicyEvaluation {
|
|
177
|
+
exitCode: GateExitCodeValue;
|
|
178
|
+
passed: boolean;
|
|
179
|
+
reasons: string[];
|
|
180
|
+
metrics: {
|
|
181
|
+
passedAssessments: number;
|
|
182
|
+
failedAssessments: number;
|
|
183
|
+
totalAssessments: number;
|
|
184
|
+
passRate: number | null;
|
|
185
|
+
runStatus: GateRunSnapshot['status'];
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// ─── Parsing & validation ──────────────────────────────────────────────────────
|
|
190
|
+
|
|
191
|
+
function formatZodError(error: z.ZodError): string {
|
|
192
|
+
return error.issues.map((i) => `${i.path.join('.')}: ${i.message}`).join('; ');
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export function parseDocumentYaml(content: string): unknown {
|
|
196
|
+
const parsed: unknown = parseYaml(content);
|
|
197
|
+
if (parsed === null || typeof parsed !== 'object') {
|
|
198
|
+
throw new Error('YAML document must be a mapping object');
|
|
199
|
+
}
|
|
200
|
+
return parsed;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export function parseLightdashAiAgentBundle(content: string): LightdashAiAgentBundle {
|
|
204
|
+
const doc = parseDocumentYaml(content);
|
|
205
|
+
const result = lightdashAiAgentBundleSchema.safeParse(doc);
|
|
206
|
+
if (!result.success) {
|
|
207
|
+
throw new Error(`Invalid LightdashAiAgentBundle: ${formatZodError(result.error)}`);
|
|
208
|
+
}
|
|
209
|
+
return result.data;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export function parseLightdashAiEvaluationGate(content: string): LightdashAiEvaluationGate {
|
|
213
|
+
const doc = parseDocumentYaml(content);
|
|
214
|
+
const result = lightdashAiEvaluationGateSchema.safeParse(doc);
|
|
215
|
+
if (!result.success) {
|
|
216
|
+
throw new Error(`Invalid LightdashAiEvaluationGate: ${formatZodError(result.error)}`);
|
|
217
|
+
}
|
|
218
|
+
return result.data;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// ─── Prompt normalization ──────────────────────────────────────────────────────
|
|
222
|
+
|
|
223
|
+
export function normalizeEvaluationPrompt(
|
|
224
|
+
prompt:
|
|
225
|
+
| { prompt: string; expectedResponse?: string | null }
|
|
226
|
+
| { threadUuid: string; promptUuid: string; expectedResponse?: string | null },
|
|
227
|
+
): EvaluationStateSnapshot['prompts'][number] {
|
|
228
|
+
if ('prompt' in prompt) {
|
|
229
|
+
return {
|
|
230
|
+
type: 'string',
|
|
231
|
+
prompt: prompt.prompt,
|
|
232
|
+
expectedResponse: prompt.expectedResponse ?? null,
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
return {
|
|
236
|
+
type: 'thread',
|
|
237
|
+
threadUuid: prompt.threadUuid,
|
|
238
|
+
promptUuid: prompt.promptUuid,
|
|
239
|
+
expectedResponse: prompt.expectedResponse ?? null,
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function promptsEqual(
|
|
244
|
+
a: EvaluationStateSnapshot['prompts'],
|
|
245
|
+
b: EvaluationStateSnapshot['prompts'],
|
|
246
|
+
): boolean {
|
|
247
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function agentFieldsToCompare(agent: BundleAgentSpec): Array<keyof AgentStateSnapshot> {
|
|
251
|
+
const fields: Array<keyof AgentStateSnapshot> = ['name', 'description', 'instruction', 'tags'];
|
|
252
|
+
if (agent.enableDataAccess !== undefined) {
|
|
253
|
+
fields.push('enableDataAccess');
|
|
254
|
+
}
|
|
255
|
+
if (agent.enableSelfImprovement !== undefined) {
|
|
256
|
+
fields.push('enableSelfImprovement');
|
|
257
|
+
}
|
|
258
|
+
return fields;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/** API defaults omitted bundle booleans to false when the bundle explicitly sets them. */
|
|
262
|
+
function normalizeAgentBooleanFlag(value: boolean | undefined): boolean {
|
|
263
|
+
return value ?? false;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function normalizeTags(tags: string[] | null | undefined): string[] | null {
|
|
267
|
+
if (tags == null || tags.length === 0) {
|
|
268
|
+
return null;
|
|
269
|
+
}
|
|
270
|
+
return tags;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function pickAgentFields(agent: AgentStateSnapshot): Partial<AgentStateSnapshot> {
|
|
274
|
+
return {
|
|
275
|
+
name: agent.name,
|
|
276
|
+
description: agent.description,
|
|
277
|
+
instruction: agent.instruction,
|
|
278
|
+
tags: normalizeTags(agent.tags),
|
|
279
|
+
enableDataAccess: normalizeAgentBooleanFlag(agent.enableDataAccess),
|
|
280
|
+
enableSelfImprovement: normalizeAgentBooleanFlag(agent.enableSelfImprovement),
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function desiredAgentToSnapshot(agent: BundleAgentSpec, uuid: string): AgentStateSnapshot {
|
|
285
|
+
return {
|
|
286
|
+
uuid,
|
|
287
|
+
name: agent.name,
|
|
288
|
+
description: agent.description ?? null,
|
|
289
|
+
instruction: agent.instruction ?? null,
|
|
290
|
+
tags: normalizeTags(agent.tags ?? null),
|
|
291
|
+
...(agent.enableDataAccess !== undefined ? { enableDataAccess: agent.enableDataAccess } : {}),
|
|
292
|
+
...(agent.enableSelfImprovement !== undefined
|
|
293
|
+
? { enableSelfImprovement: agent.enableSelfImprovement }
|
|
294
|
+
: {}),
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function evaluationToSnapshot(
|
|
299
|
+
evalSpec: BundleEvaluationSpec,
|
|
300
|
+
evalUuid: string,
|
|
301
|
+
): EvaluationStateSnapshot {
|
|
302
|
+
return {
|
|
303
|
+
evalUuid,
|
|
304
|
+
title: evalSpec.title,
|
|
305
|
+
description: evalSpec.description ?? null,
|
|
306
|
+
prompts: evalSpec.prompts.map(normalizeEvaluationPrompt),
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
function findAgentMatch(
|
|
311
|
+
desired: BundleAgentSpec,
|
|
312
|
+
current: BundleCurrentState,
|
|
313
|
+
): { agent: AgentStateSnapshot; evaluations: EvaluationStateSnapshot[] } | undefined {
|
|
314
|
+
if (desired.uuid) {
|
|
315
|
+
const byUuid = current.agents.find((a) => a.agent.uuid === desired.uuid);
|
|
316
|
+
if (byUuid) return byUuid;
|
|
317
|
+
}
|
|
318
|
+
return current.agents.find((a) => a.agent.name === desired.name);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
function findEvaluationMatch(
|
|
322
|
+
desired: BundleEvaluationSpec,
|
|
323
|
+
evaluations: EvaluationStateSnapshot[],
|
|
324
|
+
): EvaluationStateSnapshot | undefined {
|
|
325
|
+
if (desired.uuid) {
|
|
326
|
+
const byUuid = evaluations.find((e) => e.evalUuid === desired.uuid);
|
|
327
|
+
if (byUuid) return byUuid;
|
|
328
|
+
}
|
|
329
|
+
return evaluations.find((e) => e.title === desired.title);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// ─── Diff computation ──────────────────────────────────────────────────────────
|
|
333
|
+
|
|
334
|
+
export function computeBundleDiff(
|
|
335
|
+
bundle: LightdashAiAgentBundle,
|
|
336
|
+
current: BundleCurrentState,
|
|
337
|
+
): BundleDiffResult {
|
|
338
|
+
const changes: BundleDiffChange[] = [];
|
|
339
|
+
const matchedAgentUuids = new Set<string>();
|
|
340
|
+
|
|
341
|
+
for (const desiredAgent of bundle.spec.agents) {
|
|
342
|
+
const match = findAgentMatch(desiredAgent, current);
|
|
343
|
+
const agentPath = `spec.agents.${desiredAgent.key}`;
|
|
344
|
+
|
|
345
|
+
if (!match) {
|
|
346
|
+
changes.push({
|
|
347
|
+
resourceType: 'agent',
|
|
348
|
+
operation: 'create',
|
|
349
|
+
key: desiredAgent.key,
|
|
350
|
+
path: agentPath,
|
|
351
|
+
});
|
|
352
|
+
for (const desiredEval of desiredAgent.evaluations) {
|
|
353
|
+
changes.push({
|
|
354
|
+
resourceType: 'evaluation',
|
|
355
|
+
operation: 'create',
|
|
356
|
+
key: desiredEval.key,
|
|
357
|
+
agentKey: desiredAgent.key,
|
|
358
|
+
path: `${agentPath}.evaluations.${desiredEval.key}`,
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
continue;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
matchedAgentUuids.add(match.agent.uuid);
|
|
365
|
+
const agentUuid = match.agent.uuid;
|
|
366
|
+
const desiredSnapshot = desiredAgentToSnapshot(desiredAgent, agentUuid);
|
|
367
|
+
const fieldDiffs: Record<string, { from: unknown; to: unknown }> = {};
|
|
368
|
+
|
|
369
|
+
for (const field of agentFieldsToCompare(desiredAgent)) {
|
|
370
|
+
const fromVal = pickAgentFields(match.agent)[field];
|
|
371
|
+
const toVal = pickAgentFields(desiredSnapshot)[field];
|
|
372
|
+
if (JSON.stringify(fromVal) !== JSON.stringify(toVal)) {
|
|
373
|
+
fieldDiffs[field] = { from: fromVal, to: toVal };
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
if (Object.keys(fieldDiffs).length > 0) {
|
|
378
|
+
changes.push({
|
|
379
|
+
resourceType: 'agent',
|
|
380
|
+
operation: 'update',
|
|
381
|
+
key: desiredAgent.key,
|
|
382
|
+
agentUuid,
|
|
383
|
+
path: agentPath,
|
|
384
|
+
fields: fieldDiffs,
|
|
385
|
+
});
|
|
386
|
+
} else {
|
|
387
|
+
changes.push({
|
|
388
|
+
resourceType: 'agent',
|
|
389
|
+
operation: 'noop',
|
|
390
|
+
key: desiredAgent.key,
|
|
391
|
+
agentUuid,
|
|
392
|
+
path: agentPath,
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
for (const desiredEval of desiredAgent.evaluations) {
|
|
397
|
+
const evalMatch = findEvaluationMatch(desiredEval, match.evaluations);
|
|
398
|
+
const evalPath = `${agentPath}.evaluations.${desiredEval.key}`;
|
|
399
|
+
|
|
400
|
+
if (!evalMatch) {
|
|
401
|
+
changes.push({
|
|
402
|
+
resourceType: 'evaluation',
|
|
403
|
+
operation: 'create',
|
|
404
|
+
key: desiredEval.key,
|
|
405
|
+
agentKey: desiredAgent.key,
|
|
406
|
+
agentUuid,
|
|
407
|
+
path: evalPath,
|
|
408
|
+
});
|
|
409
|
+
continue;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
const desiredEvalSnapshot = evaluationToSnapshot(desiredEval, evalMatch.evalUuid);
|
|
413
|
+
const evalFieldDiffs: Record<string, { from: unknown; to: unknown }> = {};
|
|
414
|
+
|
|
415
|
+
if (evalMatch.title !== desiredEvalSnapshot.title) {
|
|
416
|
+
evalFieldDiffs.title = { from: evalMatch.title, to: desiredEvalSnapshot.title };
|
|
417
|
+
}
|
|
418
|
+
if (evalMatch.description !== desiredEvalSnapshot.description) {
|
|
419
|
+
evalFieldDiffs.description = {
|
|
420
|
+
from: evalMatch.description,
|
|
421
|
+
to: desiredEvalSnapshot.description,
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
if (!promptsEqual(evalMatch.prompts, desiredEvalSnapshot.prompts)) {
|
|
425
|
+
evalFieldDiffs.prompts = { from: evalMatch.prompts, to: desiredEvalSnapshot.prompts };
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
if (Object.keys(evalFieldDiffs).length > 0) {
|
|
429
|
+
changes.push({
|
|
430
|
+
resourceType: 'evaluation',
|
|
431
|
+
operation: 'update',
|
|
432
|
+
key: desiredEval.key,
|
|
433
|
+
agentKey: desiredAgent.key,
|
|
434
|
+
agentUuid,
|
|
435
|
+
evaluationUuid: evalMatch.evalUuid,
|
|
436
|
+
path: evalPath,
|
|
437
|
+
fields: evalFieldDiffs,
|
|
438
|
+
});
|
|
439
|
+
} else {
|
|
440
|
+
changes.push({
|
|
441
|
+
resourceType: 'evaluation',
|
|
442
|
+
operation: 'noop',
|
|
443
|
+
key: desiredEval.key,
|
|
444
|
+
agentKey: desiredAgent.key,
|
|
445
|
+
agentUuid,
|
|
446
|
+
evaluationUuid: evalMatch.evalUuid,
|
|
447
|
+
path: evalPath,
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
for (const existingEval of match.evaluations) {
|
|
453
|
+
const isDesired = desiredAgent.evaluations.some(
|
|
454
|
+
(e) =>
|
|
455
|
+
(e.uuid != null && e.uuid === existingEval.evalUuid) || e.title === existingEval.title,
|
|
456
|
+
);
|
|
457
|
+
if (!isDesired) {
|
|
458
|
+
changes.push({
|
|
459
|
+
resourceType: 'evaluation',
|
|
460
|
+
operation: 'delete',
|
|
461
|
+
key: existingEval.evalUuid,
|
|
462
|
+
agentKey: desiredAgent.key,
|
|
463
|
+
agentUuid,
|
|
464
|
+
path: `${agentPath}.evaluations[${existingEval.evalUuid}]`,
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
for (const { agent, evaluations } of current.agents) {
|
|
471
|
+
const isDesired = bundle.spec.agents.some(
|
|
472
|
+
(a) => a.uuid === agent.uuid || a.name === agent.name,
|
|
473
|
+
);
|
|
474
|
+
if (!isDesired && !matchedAgentUuids.has(agent.uuid)) {
|
|
475
|
+
for (const evalItem of evaluations) {
|
|
476
|
+
changes.push({
|
|
477
|
+
resourceType: 'evaluation',
|
|
478
|
+
operation: 'delete',
|
|
479
|
+
key: evalItem.evalUuid,
|
|
480
|
+
agentUuid: agent.uuid,
|
|
481
|
+
path: `agents[${agent.uuid}].evaluations[${evalItem.evalUuid}]`,
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
changes.push({
|
|
485
|
+
resourceType: 'agent',
|
|
486
|
+
operation: 'delete',
|
|
487
|
+
key: agent.uuid,
|
|
488
|
+
path: `agents[${agent.uuid}]`,
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
const summary = {
|
|
494
|
+
creates: changes.filter((c) => c.operation === 'create').length,
|
|
495
|
+
updates: changes.filter((c) => c.operation === 'update').length,
|
|
496
|
+
deletes: changes.filter((c) => c.operation === 'delete').length,
|
|
497
|
+
noops: changes.filter((c) => c.operation === 'noop').length,
|
|
498
|
+
};
|
|
499
|
+
|
|
500
|
+
return {
|
|
501
|
+
bundleName: bundle.metadata.name,
|
|
502
|
+
projectUuid: bundle.spec.projectUuid,
|
|
503
|
+
changes,
|
|
504
|
+
hasDrift: summary.creates + summary.updates + summary.deletes > 0,
|
|
505
|
+
summary,
|
|
506
|
+
};
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
export function detectBundleDrift(
|
|
510
|
+
bundle: LightdashAiAgentBundle,
|
|
511
|
+
current: BundleCurrentState,
|
|
512
|
+
): { hasDrift: boolean; diff: BundleDiffResult } {
|
|
513
|
+
const diff = computeBundleDiff(bundle, current);
|
|
514
|
+
return { hasDrift: diff.hasDrift, diff };
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
// ─── Gate policy evaluation ────────────────────────────────────────────────────
|
|
518
|
+
|
|
519
|
+
export function getLightdashAiAgentBundleJsonSchema(): Record<string, unknown> {
|
|
520
|
+
return z.toJSONSchema(lightdashAiAgentBundleSchema) as Record<string, unknown>;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
export function getLightdashAiEvaluationGateJsonSchema(): Record<string, unknown> {
|
|
524
|
+
return z.toJSONSchema(lightdashAiEvaluationGateSchema) as Record<string, unknown>;
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
export function evaluateGatePolicy(policy: GatePolicy, run: GateRunSnapshot): GatePolicyEvaluation {
|
|
528
|
+
const reasons: string[] = [];
|
|
529
|
+
const totalAssessments = run.passedAssessments + run.failedAssessments;
|
|
530
|
+
const passRate = totalAssessments > 0 ? run.passedAssessments / totalAssessments : null;
|
|
531
|
+
|
|
532
|
+
if (run.status === 'failed') {
|
|
533
|
+
return {
|
|
534
|
+
exitCode: GateExitCode.RUN_FAILED,
|
|
535
|
+
passed: false,
|
|
536
|
+
reasons: ['Evaluation run failed'],
|
|
537
|
+
metrics: {
|
|
538
|
+
passedAssessments: run.passedAssessments,
|
|
539
|
+
failedAssessments: run.failedAssessments,
|
|
540
|
+
totalAssessments,
|
|
541
|
+
passRate,
|
|
542
|
+
runStatus: run.status,
|
|
543
|
+
},
|
|
544
|
+
};
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
if (run.status === 'pending' || run.status === 'running') {
|
|
548
|
+
return {
|
|
549
|
+
exitCode: GateExitCode.RUN_IN_PROGRESS,
|
|
550
|
+
passed: false,
|
|
551
|
+
reasons: [`Evaluation run is ${run.status}`],
|
|
552
|
+
metrics: {
|
|
553
|
+
passedAssessments: run.passedAssessments,
|
|
554
|
+
failedAssessments: run.failedAssessments,
|
|
555
|
+
totalAssessments,
|
|
556
|
+
passRate,
|
|
557
|
+
runStatus: run.status,
|
|
558
|
+
},
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
if (policy.minPassRate != null) {
|
|
563
|
+
if (passRate === null) {
|
|
564
|
+
reasons.push('minPassRate requires at least one assessment');
|
|
565
|
+
} else if (passRate < policy.minPassRate) {
|
|
566
|
+
reasons.push(`pass rate ${passRate.toFixed(4)} < minPassRate ${policy.minPassRate}`);
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
if (policy.maxFailedAssessments != null && run.failedAssessments > policy.maxFailedAssessments) {
|
|
571
|
+
reasons.push(
|
|
572
|
+
`failed assessments ${run.failedAssessments} > maxFailedAssessments ${policy.maxFailedAssessments}`,
|
|
573
|
+
);
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
if (policy.minPassedAssessments != null && run.passedAssessments < policy.minPassedAssessments) {
|
|
577
|
+
reasons.push(
|
|
578
|
+
`passed assessments ${run.passedAssessments} < minPassedAssessments ${policy.minPassedAssessments}`,
|
|
579
|
+
);
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
if (policy.requireAllPassed === true && run.failedAssessments > 0) {
|
|
583
|
+
reasons.push(`requireAllPassed: ${run.failedAssessments} failed assessment(s)`);
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
const passed = reasons.length === 0;
|
|
587
|
+
return {
|
|
588
|
+
exitCode: passed ? GateExitCode.PASSED : GateExitCode.POLICY_FAILED,
|
|
589
|
+
passed,
|
|
590
|
+
reasons,
|
|
591
|
+
metrics: {
|
|
592
|
+
passedAssessments: run.passedAssessments,
|
|
593
|
+
failedAssessments: run.failedAssessments,
|
|
594
|
+
totalAssessments,
|
|
595
|
+
passRate,
|
|
596
|
+
runStatus: run.status,
|
|
597
|
+
},
|
|
598
|
+
};
|
|
599
|
+
}
|