/**
 * Feedback submission tool for judge evaluation.
 *
 * Allows the judge to submit structured evaluation results
 * after assessing the generated application.
 */
import { z } from "zod";
import { createToolFactory, ToolCategory } from "../../agent/tools2/types.js";
/**
 * Schema for criterion evaluation input.
 */
const criterionEvaluationSchema = z.object({
    criterion: z.string().describe("The criterion being evaluated"),
    passed: z.boolean().describe("Whether the criterion passed"),
    score: z.number().min(0).max(100).describe("Score from 0-100"),
    reasoning: z.string().describe("Detailed reasoning for the evaluation"),
    evidence: z
        .string()
        .optional()
        .describe("Supporting evidence (e.g., screenshot URL)"),
    suggestions: z
        .array(z.string())
        .optional()
        .describe("Improvement suggestions"),
});
/**
 * Schema for feedback submission input.
 */
const submitFeedbackSchema = z.object({
    overallScore: z.number().min(0).max(100).describe("Overall score from 0-100"),
    passed: z.boolean().describe("Overall pass/fail determination"),
    criteriaEvaluations: z
        .array(criterionEvaluationSchema)
        .describe("Individual criterion evaluations"),
    summary: z.string().describe("Executive summary of the evaluation"),
    suggestions: z
        .array(z.string())
        .describe("High-level suggestions for improvement"),
    strengths: z
        .array(z.string())
        .optional()
        .describe("Identified strengths in the implementation"),
    weaknesses: z
        .array(z.string())
        .optional()
        .describe("Identified weaknesses in the implementation"),
    confidence: z
        .number()
        .min(0)
        .max(1)
        .optional()
        .describe("Confidence level of the evaluation (0-1)"),
});
/**
 * Creates a feedback submission tool factory.
 *
 * This tool allows the judge to submit a comprehensive
 * evaluation after testing the application.
 */
export const submitFeedbackToolFactory = createToolFactory("submitFeedback", () => ({
    description: `Submit your final evaluation of the application.

Call this tool once you have:
1. Tested all functional requirements
2. Verified UI/UX requirements
3. Checked data integration
4. Gathered sufficient evidence

Provide a comprehensive assessment with:
- Overall score (0-100)
- Pass/fail determination
- Individual criterion evaluations with scores and reasoning
- Executive summary
- Specific suggestions for improvement
- Identified strengths and weaknesses
- Confidence level in your evaluation

Example:
{
  "overallScore": 85,
  "passed": true,
  "criteriaEvaluations": [
    {
      "criterion": "User can create new records",
      "passed": true,
      "score": 90,
      "reasoning": "The create functionality works well with proper validation",
      "evidence": "screenshot_create_form.png",
      "suggestions": ["Add confirmation dialog for create action"]
    }
  ],
  "summary": "The application successfully implements core CRUD operations with good UX",
  "suggestions": ["Improve error handling", "Add loading states"],
  "strengths": ["Clean UI", "Responsive design", "Good data validation"],
  "weaknesses": ["Limited error feedback", "No undo functionality"],
  "confidence": 0.9
}`,
    category: ToolCategory.GENERAL,
    inputSchema: submitFeedbackSchema,
    // This is a write operation in terms of evaluation
    readOnly: false,
    execute: async (input) => {
        // Validate that we have at least one criterion evaluation
        if (input.criteriaEvaluations.length === 0) {
            throw new Error("At least one criterion evaluation is required");
        }
        // Calculate weighted score if not matching overall
        const avgScore = input.criteriaEvaluations.reduce((sum, e) => sum + e.score, 0) /
            input.criteriaEvaluations.length;
        // Warn if overall score differs significantly from average
        if (Math.abs(input.overallScore - avgScore) > 20) {
            console.warn(`Overall score (${input.overallScore}) differs significantly from average criterion score (${avgScore})`);
        }
        // Construct the evaluation object
        const evaluation = {
            overallScore: input.overallScore,
            passed: input.passed,
            criteriaEvaluations: input.criteriaEvaluations,
            summary: input.summary,
            suggestions: input.suggestions,
            strengths: input.strengths,
            weaknesses: input.weaknesses,
            confidence: input.confidence,
            // Token usage will be filled by the executor
            tokensUsed: {
                input: 0,
                output: 0,
                total: 0,
            },
        };
        return {
            success: true,
            evaluation,
        };
    },
}));
/**
 * Helper function to validate evaluation completeness.
 *
 * @param evaluation - Evaluation to validate
 * @returns True if evaluation is complete
 */
export function isEvaluationComplete(evaluation) {
    return (evaluation.overallScore !== undefined &&
        evaluation.passed !== undefined &&
        evaluation.criteriaEvaluations.length > 0 &&
        evaluation.summary.length > 0 &&
        evaluation.suggestions.length > 0);
}
//# sourceMappingURL=submit-feedback.js.map