utils.ts 4.08 KB
/**
 * 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;
}