-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathuseCreateEvaluator.ts
More file actions
68 lines (59 loc) · 2.15 KB
/
Copy pathuseCreateEvaluator.ts
File metadata and controls
68 lines (59 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import type { EvaluatorConfig } from '../../../schema';
import { evaluatorPrimitive } from '../../primitives/registry';
import { withCommandRunTelemetry } from '../../telemetry/cli-command-run.js';
import { Level, standardize } from '../../telemetry/schemas/common-shapes.js';
import { useCallback, useEffect, useState } from 'react';
interface CreateEvaluatorConfig {
name: string;
level: string;
config: EvaluatorConfig;
kmsKeyArn?: string;
}
export function useCreateEvaluator() {
const [status, setStatus] = useState<{ state: 'idle' | 'loading' | 'success' | 'error'; error?: string }>({
state: 'idle',
});
const create = useCallback(async (config: CreateEvaluatorConfig) => {
setStatus({ state: 'loading' });
try {
const addResult = await withCommandRunTelemetry(
'add.evaluator',
{
evaluator_type: config.config.codeBased ? 'code-based' : 'llm-as-a-judge',
level: standardize(Level, config.level),
},
() =>
evaluatorPrimitive.add({
name: config.name,
level: config.level as 'SESSION' | 'TRACE' | 'TOOL_CALL',
config: config.config,
kmsKeyArn: config.kmsKeyArn,
})
);
if (!addResult.success) {
throw new Error(addResult.error ?? 'Failed to create evaluator');
}
setStatus({ state: 'success' });
return { ok: true as const, evaluatorName: config.name, codePath: addResult.codePath };
} catch (err) {
const message = err instanceof Error ? err.message : 'Failed to create evaluator.';
setStatus({ state: 'error', error: message });
return { ok: false as const, error: message };
}
}, []);
const reset = useCallback(() => {
setStatus({ state: 'idle' });
}, []);
return { status, createEvaluator: create, reset };
}
export function useExistingEvaluatorNames() {
const [names, setNames] = useState<string[]>([]);
useEffect(() => {
void evaluatorPrimitive.getAllNames().then(setNames);
}, []);
const refresh = useCallback(async () => {
const result = await evaluatorPrimitive.getAllNames();
setNames(result);
}, []);
return { names, refresh };
}