index.ts
3.33 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
/**
* Judger Agent: Layout error diagnosis specialist.
*
* Simplified actor-critic pattern with JSON output via post-processor.
* Uses evoltjs Agent class for tool execution and Vision API support.
*/
import { Agent } from 'evoltagent';
import type { ModelConfig } from 'evoltagent';
import { HistoryTool } from '../../tools/history-tool';
import { HierarchyTool } from '../../tools/hierarchy-tool';
import type { ComponentHistory } from '../../types/validation-types';
import type { Dict } from '../../types/validation-types';
import { JUDGER_PROMPT } from './prompt';
import { getModelConfig } from '../../utils/config';
import { AGENT_CONTEXT_WINDOW_TOKENS, MAX_OUTPUT_TOKENS } from '../../constants';
import { parseJudgerResult, updateToolContext } from './utils';
/**
* Create judger agent (critic) with JSON output.
*
* @param options - Configuration options
* @param options.workspaceDir - Optional workspace directory to restrict file access
* @param options.structureTree - Component hierarchy tree
* @param options.componentPaths - Mapping from component_id to filesystem path
* @param options.history - Component history from previous iterations
* @returns Agent configured with FileEditor, ThinkTool, HierarchyTool, HistoryTool, and JSON post-processor
*/
export function createJudgerAgent(options: {
workspaceDir?: string;
structureTree?: Dict;
componentPaths?: Record<string, string>;
history?: ComponentHistory;
}): Agent {
const { workspaceDir, structureTree, componentPaths, history } = options;
let systemPrompt = JUDGER_PROMPT;
if (workspaceDir) {
systemPrompt += `\n\nWORKSPACE: ${workspaceDir}\nOnly access files within this workspace.`;
}
// System tools (string-based, globally registered)
const systemTools: string[] = ['FileEditor.read', 'FileEditor.find'];
// Update HierarchyTool instance context if structure provided
// Tools are already registered via @tools decorator - just update execute methods
if (structureTree) {
const hierarchyTool = new HierarchyTool();
hierarchyTool.setContext(structureTree, componentPaths || {});
const hierarchyToolNames = updateToolContext(
hierarchyTool as unknown as Record<string, (...args: unknown[]) => Promise<string>>,
'HierarchyTool',
['lookup', 'getSharedInstances']
);
systemTools.push(...hierarchyToolNames);
}
// Always register HistoryTool (even with empty history in iteration 1)
// The tool handles empty history gracefully and the prompt references it
const historyTool = new HistoryTool();
historyTool.setContext(history || {});
const historyToolNames = updateToolContext(
historyTool as unknown as Record<string, (...args: unknown[]) => Promise<string>>,
'HistoryTool',
['getComponentHistory', 'getIterationSummary']
);
systemTools.push(...historyToolNames);
const modelConfig: ModelConfig = {
...getModelConfig(),
contextWindowTokens: AGENT_CONTEXT_WINDOW_TOKENS,
maxOutputTokens: MAX_OUTPUT_TOKENS,
stream: true,
};
return new Agent({
name: 'JudgerAgent',
profile: 'Layout diagnosis specialist',
system: systemPrompt,
tools: systemTools,
modelConfig,
postProcessor: parseJudgerResult,
verbose: 2,
});
}