utils.ts
4.08 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
* Utility functions for Judger Agent.
*/
import { SystemToolStore, FunctionCallingStore } from 'evoltagent';
import type { JudgerDiagnosis } from './types';
import { logger } from '../../utils/logger';
/**
* Extract JSON diagnosis from agent response.
* @param response - Agent response text (with TaskCompletion tags already stripped)
* @returns Parsed JudgerDiagnosis object
*/
export function parseJudgerResult(response: string): Promise<JudgerDiagnosis> {
// Check for empty response (agent may have hit limits or errors)
if (!response || response.trim().length === 0) {
logger.printInfoLog('判定智能体返回空响应,跳过本轮精修');
return Promise.resolve({
errorType: 'pixel_misalignment',
rootCause: 'Agent analysis unavailable, do not apply any edits',
visualEvidence: 'N/A',
codeEvidence: 'N/A',
refineInstructions: [],
toolsUsed: [],
});
}
// Extract JSON from markdown code block
// The evoltagent library strips <TaskCompletion> tags before calling postProcessor
const jsonMatch = response.match(/```json\n([\s\S]*?)\n```/);
if (!jsonMatch) {
logger.printInfoLog('判定智能体响应缺少 JSON 块,跳过本轮精修');
return Promise.resolve({
errorType: 'empty_response',
rootCause: 'Agent analysis unavailable, do not apply any edits',
visualEvidence: 'N/A',
codeEvidence: 'N/A',
refineInstructions: [],
toolsUsed: [],
});
}
const jsonStr = jsonMatch[1];
if (!jsonStr || jsonStr.trim().length === 0) {
logger.printInfoLog('判定智能体响应 JSON 为空,跳过本轮精修');
return Promise.resolve({
errorType: 'empty_response',
rootCause: 'Agent analysis unavailable, do not apply any edits',
visualEvidence: 'N/A',
codeEvidence: 'N/A',
refineInstructions: [],
toolsUsed: [],
});
}
try {
return Promise.resolve(JSON.parse(jsonStr) as JudgerDiagnosis);
} catch (error) {
logger.printInfoLog(`判定智能体响应 JSON 解析失败:${error instanceof Error ? error.message : String(error)}`);
return Promise.resolve({
errorType: 'empty_response',
rootCause: 'Agent analysis unavailable',
visualEvidence: 'N/A',
codeEvidence: 'N/A',
refineInstructions: [],
toolsUsed: [],
});
}
}
/**
* Update tool instance context for already-registered tools.
*
* Since tools are registered via @tools decorator at module load time,
* we only need to update their execute methods to point to instances
* with the correct per-execution context.
*
* @param toolInstance - Tool instance with context set via setContext()
* @param toolName - Base name for the tool (e.g., "HierarchyTool")
* @param methodNames - List of method names to update (e.g., ["lookup", "getSiblings"])
* @returns List of updated tool name strings (e.g., ["HierarchyTool.lookup"])
*/
export function updateToolContext(
toolInstance: Record<string, (...args: unknown[]) => Promise<string>>,
toolName: string,
methodNames: string[]
): string[] {
const updatedNames: string[] = [];
for (const methodName of methodNames) {
const raw = toolInstance[methodName];
if (!raw) {
continue;
}
const method = raw.bind(toolInstance);
const fullName = `${toolName}.${methodName}`;
// Tool should already be registered via @tools decorator - just update execute method
const systemTool = SystemToolStore.getTool(fullName);
if (systemTool) {
systemTool.execute = method;
updatedNames.push(fullName);
}
// Also update FunctionCallingStore
const functionCallingName = `${toolName}-${methodName}`;
const userTool = FunctionCallingStore.getTool(functionCallingName);
if (userTool) {
userTool.execute = method;
}
}
return updatedNames;
}