-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathtool-matcher.ts
More file actions
68 lines (64 loc) · 1.78 KB
/
tool-matcher.ts
File metadata and controls
68 lines (64 loc) · 1.78 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
export type ToolDef =
// Asserts that the tool with this name was called successfully
| string
| {
// Name of the tool
name: string;
// Asserts that the tool arguments contain this string
argumentContains?: string;
// Asserts that the tool's success equals this value
successIs?: boolean;
};
export interface ParsedToolLog {
name: string;
args: string;
success: boolean;
duration_ms: number;
}
export function getToolName(toolDef: ToolDef): string {
if (typeof toolDef === "string") {
return toolDef;
}
return toolDef.name;
}
export function getToolArgumentsDebug(toolDef: ToolDef): string {
if (typeof toolDef !== "string") {
const out = [];
if (toolDef.successIs) {
out.push(`success=${toolDef.successIs}`);
// If you don't pass successIs, assert that it was successful
} else {
out.push(`success=true`);
}
if (toolDef.argumentContains) {
out.push(`contains=${toolDef.argumentContains}`);
}
return out.join(",");
}
// If you just pass a string, assert that the tool was successful
return "success=true";
}
export function toolArgumentsMatch(toolDef: ToolDef, log: ParsedToolLog): boolean {
let success = true;
if (typeof toolDef !== "string") {
if (toolDef.argumentContains) {
if (!log.args.includes(toolDef.argumentContains)) {
success = false;
}
}
if (toolDef.successIs !== undefined) {
if (log.success !== toolDef.successIs) {
success = false;
}
// If you don't pass successIs, assert that it was successful
} else if (!log.success) {
success = false;
}
// If you just pass a string, assert that the tool was successful
} else {
if (!log.success) {
success = false;
}
}
return success;
}