Skip to content

Commit 6265b01

Browse files
feat(content): zod and streaming response feature added
1 parent f30616c commit 6265b01

File tree

5 files changed

+57
-8
lines changed

5 files changed

+57
-8
lines changed

package-lock.json

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"lucide-react": "^0.456.0",
5454
"next-themes": "^0.4.3",
5555
"openai": "^4.72.0",
56+
"partial-json": "^0.1.7",
5657
"react": "^18.3.1",
5758
"react-ace": "^13.0.0",
5859
"react-day-picker": "^8.10.1",

src/App.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<<<<<<< HEAD
12
import leetCode from '@/assets/leetcode.png'
23
import React from 'react'
34
import Show from './components/Show'
@@ -13,6 +14,21 @@ const Popup: React.FC = () => {
1314
;(async function loadOpenAPIKey() {
1415
if (!chrome) return
1516
const apiKeyFromStorage = (await chrome.storage.local.get('apiKey')) as {
17+
=======
18+
import React from "react";
19+
import leetCode from "@/assets/leetcode.png";
20+
import { Input } from "./components/ui/input";
21+
import { Button } from "./components/ui/button";
22+
23+
const Popup: React.FC = () => {
24+
const [openAIKey, setOpenAIKey] = React.useState("");
25+
const [isLoaded, setIsLoaded] = React.useState(false);
26+
27+
React.useEffect(() => {
28+
(async function loadOpenAPIKey() {
29+
if (!chrome) return;
30+
const apiKeyFromStorage = (await chrome.storage.local.get("apiKey")) as {
31+
>>>>>>> e73346b (feat(content): zod and streaming response feature added)
1632
apiKey?: string;
1733
};
1834
if (apiKeyFromStorage.apiKey)
@@ -55,7 +71,11 @@ const Popup: React.FC = () => {
5571
value={openAIKey}
5672
onChange={(e) => setOpenAIKey(e.target.value)}
5773
placeholder="Ex. 0aBbnGgzXXXXXX"
74+
<<<<<<< HEAD
5875
className="bg-white text-black outline-none"
76+
=======
77+
className="bg-white outline-none"
78+
>>>>>>> e73346b (feat(content): zod and streaming response feature added)
5979
/>
6080

6181
<Button onClick={handleAddOpenAPIKey} className="dark">

src/constants/prompt.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,22 @@ Tone & Style:
4141
- Use emojis like 🌟, 🙌, or ✅ to make the conversation fun and engaging.
4242
- Avoid long, formal responses—be natural and conversational.
4343
44+
<<<<<<< HEAD
4445
Example JSON Response:
46+
=======
47+
48+
Problem Statement:
49+
\'\'\'
50+
{{problem_statement}}
51+
\'\'\'
52+
53+
User Programming Language: {{programming_language}}
54+
55+
User Code:
56+
\`\`\`{{programming_language}}
57+
{{user_code}}
58+
\`\`\`
59+
>>>>>>> e73346b (feat(content): zod and streaming response feature added)
4560
4661
{
4762
"output": {

src/content/content.tsx

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import React, { useRef } from 'react';
22
import { Button } from '@/components/ui/button';
3-
import { Bot, ClipboardCopy, Send, SendHorizontal } from 'lucide-react';
3+
import { Bot, ClipboardCopy, Send } from 'lucide-react';
44
import OpenAI from 'openai';
5-
65
import './style.css';
76
import { Input } from '@/components/ui/input';
87
import { SYSTEM_PROMPT } from '@/constants/prompt';
@@ -33,8 +32,14 @@ interface ChatBoxProps {
3332
};
3433
}
3534

35+
// Define the schema for the AI response
36+
export const AIResponseSchema = z.object({
37+
content: z
38+
.string()
39+
.describe("The content of the response in markdown format"),
40+
});
3641
interface ChatMessage {
37-
role: 'user' | 'assistant';
42+
role: "user" | "assistant";
3843
message: string;
3944
type: 'text' | 'markdown';
4045
assistantResponse?: {
@@ -75,17 +80,17 @@ function ChatBox({ context, visible }: ChatBoxProps) {
7580
const extractedCode = extractCode(userCurrentCodeContainer);
7681

7782
const systemPromptModified = SYSTEM_PROMPT.replace(
78-
'{{problem_statement}}',
83+
"{{problem_statement}}",
7984
context.problemStatement
8085
)
8186
.replace('{{programming_language}}', programmingLanguage)
8287
.replace('{{user_code}}', extractedCode);
8388

8489
const apiResponse = await openai.chat.completions.create({
85-
model: 'chatgpt-4o-latest',
86-
response_format: { type: 'json_object' },
90+
model: "gpt-4o-2024-08-06", // support json format
91+
response_format: zodResponseFormat(AIResponseSchema, "responseSchema"),
8792
messages: [
88-
{ role: 'system', content: systemPromptModified },
93+
{ role: "system", content: systemPromptModified },
8994
...chatHistory.map(
9095
(chat) =>
9196
({
@@ -126,7 +131,7 @@ function ChatBox({ context, visible }: ChatBoxProps) {
126131
const onSendMessage = () => {
127132
setChatHistory((prev) => [
128133
...prev,
129-
{ role: 'user', message: value, type: 'text' },
134+
{ role: "user", message: value, type: "text" },
130135
]);
131136
chatBoxRef.current?.scrollIntoView({ behavior: 'smooth' });
132137
setValue('');

0 commit comments

Comments
 (0)