Skip to content

Commit ef3f151

Browse files
authored
Add files via upload
1 parent 9218fcf commit ef3f151

2 files changed

Lines changed: 174 additions & 0 deletions

File tree

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { serve } from "https://deno.land/std@0.168.0/http/server.ts"
2+
3+
const corsHeaders = {
4+
'Access-Control-Allow-Origin': '*',
5+
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
6+
}
7+
8+
serve(async (req) => {
9+
if (req.method === 'OPTIONS') {
10+
return new Response('ok', { headers: corsHeaders })
11+
}
12+
13+
try {
14+
const { code, editPrompt } = await req.json()
15+
16+
const openaiApiKey = Deno.env.get('OPENAI_API_KEY')
17+
if (!openaiApiKey) {
18+
throw new Error('OpenAI API key not configured')
19+
}
20+
21+
const response = await fetch('https://api.openai.com/v1/chat/completions', {
22+
method: 'POST',
23+
headers: {
24+
'Authorization': `Bearer ${openaiApiKey}`,
25+
'Content-Type': 'application/json',
26+
},
27+
body: JSON.stringify({
28+
model: 'gpt-4.1-2025-04-14',
29+
messages: [
30+
{
31+
role: 'system',
32+
content: `You are a C++ code editor. Your task is to modify the provided C++ code based on the user's edit request.
33+
34+
Rules:
35+
1. Only output the modified C++ code, nothing else
36+
2. Maintain the original code style and formatting as much as possible
37+
3. Make only the changes requested by the user
38+
4. Ensure the code remains syntactically correct
39+
5. If the request is unclear or impossible, make your best interpretation
40+
6. Preserve comments and structure unless specifically asked to change them`
41+
},
42+
{
43+
role: 'user',
44+
content: `Here is the C++ code to edit:
45+
46+
\`\`\`cpp
47+
${code}
48+
\`\`\`
49+
50+
Edit request: ${editPrompt}
51+
52+
Please provide only the modified C++ code:`
53+
}
54+
],
55+
temperature: 0.3,
56+
max_tokens: 2000,
57+
}),
58+
})
59+
60+
if (!response.ok) {
61+
throw new Error(`OpenAI API error: ${response.status}`)
62+
}
63+
64+
const data = await response.json()
65+
const editedCode = data.choices[0]?.message?.content?.trim() || ''
66+
67+
// Clean up the response to remove any markdown formatting
68+
const cleanCode = editedCode
69+
.replace(/^```cpp\n?/g, '')
70+
.replace(/^```c\+\+\n?/g, '')
71+
.replace(/\n?```$/g, '')
72+
.trim()
73+
74+
return new Response(
75+
JSON.stringify({ editedCode: cleanCode }),
76+
{
77+
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
78+
},
79+
)
80+
} catch (error) {
81+
console.error('Error:', error)
82+
return new Response(
83+
JSON.stringify({ error: error.message }),
84+
{
85+
status: 500,
86+
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
87+
},
88+
)
89+
}
90+
})
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { serve } from "https://deno.land/std@0.168.0/http/server.ts"
2+
3+
const corsHeaders = {
4+
'Access-Control-Allow-Origin': '*',
5+
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
6+
}
7+
8+
serve(async (req) => {
9+
if (req.method === 'OPTIONS') {
10+
return new Response('ok', { headers: corsHeaders })
11+
}
12+
13+
try {
14+
const { code, input = "" } = await req.json()
15+
16+
// Use Judge0 API for code execution
17+
const judge0Response = await fetch('https://judge0-ce.p.rapidapi.com/submissions', {
18+
method: 'POST',
19+
headers: {
20+
'Content-Type': 'application/json',
21+
'X-RapidAPI-Key': Deno.env.get('RAPIDAPI_KEY') || '',
22+
'X-RapidAPI-Host': 'judge0-ce.p.rapidapi.com'
23+
},
24+
body: JSON.stringify({
25+
source_code: code,
26+
language_id: 54, // C++ (GCC 9.2.0)
27+
stdin: input,
28+
cpu_time_limit: 2,
29+
memory_limit: 128000
30+
})
31+
})
32+
33+
const submission = await judge0Response.json()
34+
35+
if (!submission.token) {
36+
throw new Error('Failed to submit code for execution')
37+
}
38+
39+
// Poll for result
40+
let result
41+
let attempts = 0
42+
const maxAttempts = 10
43+
44+
do {
45+
await new Promise(resolve => setTimeout(resolve, 1000))
46+
47+
const resultResponse = await fetch(`https://judge0-ce.p.rapidapi.com/submissions/${submission.token}`, {
48+
headers: {
49+
'X-RapidAPI-Key': Deno.env.get('RAPIDAPI_KEY') || '',
50+
'X-RapidAPI-Host': 'judge0-ce.p.rapidapi.com'
51+
}
52+
})
53+
54+
result = await resultResponse.json()
55+
attempts++
56+
} while (result.status.id <= 2 && attempts < maxAttempts)
57+
58+
return new Response(
59+
JSON.stringify({
60+
success: true,
61+
output: result.stdout || '',
62+
error: result.stderr || '',
63+
status: result.status.description,
64+
time: result.time,
65+
memory: result.memory
66+
}),
67+
{
68+
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
69+
status: 200,
70+
},
71+
)
72+
} catch (error) {
73+
return new Response(
74+
JSON.stringify({
75+
success: false,
76+
error: error.message || 'Failed to execute code'
77+
}),
78+
{
79+
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
80+
status: 500,
81+
},
82+
)
83+
}
84+
})

0 commit comments

Comments
 (0)