From c3c8b5ca2fe331d37ebc51753e627796d7484d02 Mon Sep 17 00:00:00 2001 From: cfngc4594 Date: Fri, 29 May 2026 14:32:33 +0800 Subject: [PATCH] feat(chat): add getCodeAnalysis tool for enhanced code feedback and metrics --- src/app/api/chat/route.ts | 46 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/src/app/api/chat/route.ts b/src/app/api/chat/route.ts index 050b895..efa572c 100644 --- a/src/app/api/chat/route.ts +++ b/src/app/api/chat/route.ts @@ -19,7 +19,9 @@ Help users understand problems, debug code, and improve solutions with clear, re Prefer concise answers for simple questions. For debugging or refactoring, be specific and practical: point to the likely cause, suggest a minimal fix, and mention any important trade-offs. Avoid insults, sarcasm, profanity, or comments that shame the user. -Use tools when the user's question depends on current problem context. Use getCurrentCode for the latest editor code, getEditorDiagnostics for current editor errors and warnings, getProblemDescription for the statement, getProblemSolution for the official solution, getTestcases for configured test cases, getSubmissionHistory for the user's submissions, and getSubmissionDetail for one submission's code and judge result. +Use tools when the user's question depends on current problem context. Use getCurrentCode for the latest editor code, getEditorDiagnostics for current editor errors and warnings, getProblemDescription for the statement, getProblemSolution for the official solution, getTestcases for configured test cases, getSubmissionHistory for the user's submissions, getSubmissionDetail for one submission's code and judge result, and getCodeAnalysis for code analysis scores, complexity, and feedback. + +When the user asks about overall score, scoring details, code analysis feedback, time complexity, space complexity, or any analysis metric, call getCodeAnalysis before answering. When providing code, keep it directly relevant to the user's question and avoid unnecessary rewrites. If the user is working on an algorithmic problem, do not reveal the full solution immediately unless they ask for it; start with hints or targeted guidance. @@ -104,6 +106,48 @@ Reply in the user's language.`; return { submissions }; }, }), + getCodeAnalysis: tool({ + description: + "Get code analysis scores, complexity, and feedback for one of the current user's submissions.", + parameters: z.object({ + submissionId: z + .string() + .optional() + .describe("Defaults to the submission currently open in the URL."), + }), + execute: async ({ submissionId: requestedSubmissionId }) => { + if (!userId) return { error: "User is not authenticated." }; + + const targetSubmissionId = requestedSubmissionId ?? submissionId; + if (!targetSubmissionId) return { error: "Missing submission id." }; + + const analysis = await prisma.codeAnalysis.findFirst({ + where: { + submissionId: targetSubmissionId, + submission: { + userId, + }, + }, + select: { + id: true, + submissionId: true, + status: true, + timeComplexity: true, + spaceComplexity: true, + overallScore: true, + styleScore: true, + readabilityScore: true, + efficiencyScore: true, + correctnessScore: true, + feedback: true, + createdAt: true, + updatedAt: true, + }, + }); + + return analysis ?? { error: "Code analysis not found." }; + }, + }), getSubmissionDetail: tool({ description: "Get code, compile output, and judge runs for one of the current user's submissions.",