From fabdb7c17a0a120d0eef9ac86d6b7df43f3e01f6 Mon Sep 17 00:00:00 2001 From: fly6516 Date: Fri, 16 May 2025 01:38:12 +0800 Subject: [PATCH] =?UTF-8?q?feat(ai):=20=E6=B7=BB=E5=8A=A0=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E4=BC=98=E5=8C=96=E5=8A=9F=E8=83=BD-=20=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=EF=BC=8C=E6=9B=BF=E6=8D=A2=E5=8E=9F=E6=9C=89=E7=9A=84=E5=A4=8D?= =?UTF-8?q?=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90=E5=8A=9F=E8=83=BD=20-=20?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=20AI=20=E9=85=8D=E7=BD=AE=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=EF=BC=8C=E7=BB=9F=E4=B8=80=E5=A4=84=E7=90=86=20OpenAI=20?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E9=85=8D=E7=BD=AE=20-=20=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E6=96=B0=E7=9A=84=E8=BE=93=E5=85=A5=E8=BE=93=E5=87=BA=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=EF=BC=8C=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=E7=BB=93?= =?UTF-8?q?=E6=9E=84=20-=20=E6=9B=B4=E6=96=B0=E6=8F=90=E7=A4=BA=E6=A8=A1?= =?UTF-8?q?=E6=9D=BF=EF=BC=8C=E5=BC=95=E5=AF=BC=20AI=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/actions/ai-improve.ts | 55 ++++++++++++++++++++++----------------- src/lib/ai.ts | 8 ++++++ src/types/ai-improve.ts | 17 ++++++++++++ 3 files changed, 56 insertions(+), 24 deletions(-) create mode 100644 src/lib/ai.ts create mode 100644 src/types/ai-improve.ts diff --git a/src/actions/ai-improve.ts b/src/actions/ai-improve.ts index 7e65716..85fa02d 100644 --- a/src/actions/ai-improve.ts +++ b/src/actions/ai-improve.ts @@ -1,34 +1,41 @@ -"use server"; - import { - AnalyzeComplexityResponse, - AnalyzeComplexityResponseSchema, - Complexity, -} from "@/types/complexity"; + OptimizeCodeInput, + OptimizeCodeOutput, + OptimizeCodeOutputSchema +} from "@/types/ai-improve"; import { openai } from "@/lib/ai"; import { CoreMessage, generateText } from "ai"; -export const analyzeComplexity = async ( - content: string -): Promise => { +export const optimizeCode = async ( + input: OptimizeCodeInput +): Promise => { const model = openai("gpt-4o-mini"); const prompt = ` - Analyze the time and space complexity of the following programming code snippet. - Determine the Big O notation from this list: ${Complexity.options.join(", ")}. - Provide your response as a JSON object with one key: - 1. "time": A string representing the time complexity (e.g., "O(N)", "O(N^2)"). - 2. "space": A string representing the space complexity (e.g., "O(1)", "O(N)"). + Analyze the following programming code for potential errors, inefficiencies or code style issues. + Provide an optimized version of the code with explanations. Focus on: + 1. Fixing any syntax errors + 2. Improving performance + 3. Enhancing code readability + 4. Following best practices - Code to analyze: - \`\`\` - ${content} - \`\`\` + Original code: + \`\`\` + ${input.code} + \`\`\` - Respond ONLY with the JSON object. Do not include any other text or markdown formatting like \`\`\`json before or after the object. - `; + Error message (if any): ${input.error || "No error message provided"} - const messages: CoreMessage[] = [{role: "user", content: prompt}]; + Respond ONLY with the JSON object containing the optimized code and explanations. + Format: + { + "optimizedCode": "optimized code here", + "explanation": "explanation of changes made", + "issuesFixed": ["list of issues fixed"] + } + `; + + const messages: CoreMessage[] = [{ role: "user", content: prompt }]; let text; try { @@ -53,12 +60,12 @@ export const analyzeComplexity = async ( } const validationResult = - AnalyzeComplexityResponseSchema.safeParse(llmResponseJson); + OptimizeCodeOutputSchema.safeParse(llmResponseJson); if (!validationResult.success) { console.error("Zod validation failed:", validationResult.error.format()); throw new Error("Response validation failed"); } - return validationResult; -} \ No newline at end of file + return validationResult.data; +}; \ No newline at end of file diff --git a/src/lib/ai.ts b/src/lib/ai.ts new file mode 100644 index 0000000..95e527c --- /dev/null +++ b/src/lib/ai.ts @@ -0,0 +1,8 @@ +import "server-only"; + +import { createOpenAI } from "@ai-sdk/openai"; + +export const openai = createOpenAI({ + apiKey: process.env.OPENAI_API_KEY || "", + baseURL: process.env.OPENAI_BASE_URL || "", +}); \ No newline at end of file diff --git a/src/types/ai-improve.ts b/src/types/ai-improve.ts new file mode 100644 index 0000000..6615eba --- /dev/null +++ b/src/types/ai-improve.ts @@ -0,0 +1,17 @@ +import { z } from "zod"; + +export const OptimizeCodeInputSchema = z.object({ + code: z.string(), + error: z.string().optional(), +}); + +export type OptimizeCodeInput = z.infer; + +export const OptimizeCodeOutputSchema = z.object({ + optimizedCode: z.string(), + explanation: z.string(), + issuesFixed: z.array(z.string()).optional(), +}); + +export type OptimizeCodeOutput = z.infer; +