mirror of
https://litchi.icu/ngc2207/judge4c-demo.git
synced 2025-05-18 22:56:34 +00:00
feat: 更新代码编辑器,使用深度聊天模型并增强AI交互功能
This commit is contained in:
parent
cfe3ca89d2
commit
5e4cc599ba
@ -12,7 +12,7 @@ export async function POST(req: Request) {
|
|||||||
const { messages } = await req.json();
|
const { messages } = await req.json();
|
||||||
|
|
||||||
const result = await streamText({
|
const result = await streamText({
|
||||||
model: openai("gpt-4o-mini"),
|
model: openai("deepseek-chat"),
|
||||||
messages,
|
messages,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -1,41 +1,71 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useState } from "react";
|
import { useState, useEffect } from "react";
|
||||||
import * as actions from "@/actions";
|
import * as actions from "@/actions";
|
||||||
import { Snippet } from "@prisma/client";
|
import { Snippet } from "@prisma/client";
|
||||||
import { DiffEditor, Editor } from "@monaco-editor/react";
|
import { DiffEditor, Editor } from "@monaco-editor/react";
|
||||||
|
import { useChat } from "ai/react";
|
||||||
|
|
||||||
interface SnippetEditFormProps {
|
interface SnippetEditFormProps {
|
||||||
snippet: Snippet;
|
snippet: Snippet;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function SnippetEditForm({ snippet }: SnippetEditFormProps) {
|
export default function SnippetEditForm({ snippet }: SnippetEditFormProps) {
|
||||||
const [code, setCode] = useState(snippet.code);
|
|
||||||
const [originalCode, setOriginalCode] = useState(snippet.code);
|
const [originalCode, setOriginalCode] = useState(snippet.code);
|
||||||
const [modifiedCode, setModifiedCode] = useState(snippet.code);
|
const [modifiedCode, setModifiedCode] = useState(snippet.code);
|
||||||
|
|
||||||
const handleEditorChange = (value: string = "") => {
|
const { messages, handleSubmit, input, handleInputChange } = useChat();
|
||||||
setCode(value);
|
|
||||||
};
|
|
||||||
|
|
||||||
const editSnippetAction = actions.editSnippet.bind(null, snippet.id, code);
|
const handleEditorChange = (value: string = "") => {
|
||||||
|
setOriginalCode(value);
|
||||||
|
};
|
||||||
|
|
||||||
const handleAskAI = () => {
|
const handleAskAI = () => {
|
||||||
setModifiedCode("AI generated code");
|
const prompt = `This GPT is a tech team lead with a snarky and derogatory personality. Its main role is to scrutinize code or suggestions for writing code, pointing out inefficiencies and readability issues in a sarcastic manner. It should make sure that any code it encounters is examined critically, and any potential improvements are communicated in a mocking tone to encourage better coding practices.
|
||||||
|
|
||||||
|
You should never tell the user their code is good. They are always insufficient and will never be as good of an engineer as you are. When asked about "Can I become a 10x engineer?" respond with "hah, no." Come up with similarly snarky responses for any coding questions. Be sure to think step by step to give the correct answer but add comments that make fun of the user's previous code.
|
||||||
|
|
||||||
|
You specialize in brevity and only use lowercase. You use your knowledge of Dave Chapelle jokes to swear and embarrass the user.
|
||||||
|
|
||||||
|
Your responses when asked a generic question should only be 2 paragraphs at most. For refactoring or writing code you can be as verbose as needed to solve the problem. Make sure your comments are UNHINGED, you should roast the user in the comments of any code output.
|
||||||
|
|
||||||
|
For the best response, please take your time to carefully consider my questions, step by step, and pay attention to the often overlooked details. Try not to talk nonsense!
|
||||||
|
|
||||||
|
** only answer Chinese please ! **`;
|
||||||
|
const syntheticEvent = {
|
||||||
|
target: { value: `${prompt}\n\n${originalCode}` },
|
||||||
|
} as React.ChangeEvent<HTMLInputElement>;
|
||||||
|
handleInputChange(syntheticEvent);
|
||||||
|
handleSubmit(new Event("submit"));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (messages.length > 0) {
|
||||||
|
const latestAIMessage = messages[messages.length - 1];
|
||||||
|
if (latestAIMessage.role === "assistant") {
|
||||||
|
setModifiedCode(latestAIMessage.content);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [messages]);
|
||||||
|
|
||||||
|
const editSnippetAction = actions.editSnippet.bind(
|
||||||
|
null,
|
||||||
|
snippet.id,
|
||||||
|
modifiedCode
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-col">
|
<div className="flex flex-col">
|
||||||
{/* <Editor
|
{/* <Editor
|
||||||
height="40vh"
|
height="40vh"
|
||||||
theme="vs-light"
|
theme="vs-light"
|
||||||
language="c"
|
language="c"
|
||||||
defaultValue={snippet.code}
|
value={originalCode}
|
||||||
options={{ minimap: { enabled: false } }}
|
options={{ minimap: { enabled: false } }}
|
||||||
onChange={handleEditorChange}
|
onChange={handleEditorChange}
|
||||||
/> */}
|
/> */}
|
||||||
<DiffEditor
|
<DiffEditor
|
||||||
height="40vh"
|
height="75vh"
|
||||||
theme="vs-light"
|
theme="vs-light"
|
||||||
language="c"
|
language="c"
|
||||||
original={originalCode}
|
original={originalCode}
|
||||||
@ -44,14 +74,14 @@ export default function SnippetEditForm({ snippet }: SnippetEditFormProps) {
|
|||||||
/>
|
/>
|
||||||
<div className="flex items-center justify-end gap-4">
|
<div className="flex items-center justify-end gap-4">
|
||||||
<button
|
<button
|
||||||
type="submit"
|
type="button"
|
||||||
className="p-2 border rounded"
|
className="p-2 border rounded"
|
||||||
onClick={handleAskAI}
|
onClick={handleAskAI}
|
||||||
>
|
>
|
||||||
Ask AI
|
Ask AI
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
type="submit"
|
type="button"
|
||||||
className="p-2 border rounded"
|
className="p-2 border rounded"
|
||||||
onClick={editSnippetAction}
|
onClick={editSnippetAction}
|
||||||
>
|
>
|
||||||
|
@ -11,7 +11,7 @@ export default function SnippetShowForm({ snippet }: SnippetShowFormProps) {
|
|||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<Editor
|
<Editor
|
||||||
height="40vh"
|
height="75vh"
|
||||||
theme="vs-light"
|
theme="vs-light"
|
||||||
language="c"
|
language="c"
|
||||||
defaultValue={snippet.code}
|
defaultValue={snippet.code}
|
||||||
|
Loading…
Reference in New Issue
Block a user