-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathtoolMetricsUtils.ts
More file actions
76 lines (66 loc) · 1.69 KB
/
toolMetricsUtils.ts
File metadata and controls
76 lines (66 loc) · 1.69 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
69
70
71
72
73
74
75
76
/**
* @license
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import type {ToolDefinition} from '../tools/ToolDefinition.js';
import {
transformArgName,
transformArgType,
getZodType,
PARAM_BLOCKLIST,
} from './ClearcutLogger.js';
/**
* Validates that all values in an enum are of the homogeneous primitive type.
* Returns the primitive type string. Throws an error if heterogeneous.
*/
export function validateEnumHomogeneity(values: unknown[]): string {
const firstType = typeof values[0];
for (const val of values) {
if (typeof val !== firstType) {
throw new Error('Heterogeneous enum types found');
}
}
return firstType;
}
export interface ArgMetric {
name: string;
argType: string;
}
export interface ToolMetric {
name: string;
args: ArgMetric[];
}
/**
* Generates tool metrics from tool definitions.
*/
export function generateToolMetrics(tools: ToolDefinition[]): ToolMetric[] {
return tools.map(tool => {
const args: ArgMetric[] = [];
for (const [name, schema] of Object.entries(tool.schema)) {
if (PARAM_BLOCKLIST.has(name)) {
continue;
}
const zodType = getZodType(schema);
const transformedName = transformArgName(zodType, name);
let argType = transformArgType(zodType);
if (argType === 'enum') {
let values;
if (schema._def.values?.length > 0) {
values = schema._def.values;
} else {
values = schema._def.innerType._def.values;
}
argType = validateEnumHomogeneity(values);
}
args.push({
name: transformedName,
argType,
});
}
return {
name: tool.name,
args,
};
});
}