2024-04-23 01:53:37 -04:00
|
|
|
"use server"
|
|
|
|
|
2024-04-27 21:24:20 -04:00
|
|
|
import { revalidatePath } from "next/cache"
|
|
|
|
|
2024-04-23 01:53:37 -04:00
|
|
|
export async function createSandbox(body: {
|
|
|
|
type: string
|
|
|
|
name: string
|
2024-04-27 16:22:35 -04:00
|
|
|
userId: string
|
2024-04-23 01:53:37 -04:00
|
|
|
visibility: string
|
|
|
|
}) {
|
2024-04-27 16:22:35 -04:00
|
|
|
const res = await fetch("http://localhost:8787/api/sandbox", {
|
|
|
|
method: "PUT",
|
2024-04-23 01:53:37 -04:00
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify(body),
|
|
|
|
})
|
2024-04-27 16:22:35 -04:00
|
|
|
|
|
|
|
return await res.text()
|
2024-04-23 01:53:37 -04:00
|
|
|
}
|
2024-04-27 21:24:20 -04:00
|
|
|
|
|
|
|
export async function updateSandbox(body: {
|
|
|
|
id: string
|
|
|
|
name?: string
|
|
|
|
visibility?: "public" | "private"
|
|
|
|
}) {
|
|
|
|
const res = await fetch("http://localhost:8787/api/sandbox", {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify(body),
|
|
|
|
})
|
|
|
|
|
|
|
|
revalidatePath("/dashboard")
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function deleteSandbox(id: string) {
|
|
|
|
const res = await fetch(`http://localhost:8787/api/sandbox?id=${id}`, {
|
|
|
|
method: "DELETE",
|
|
|
|
})
|
|
|
|
|
|
|
|
revalidatePath("/dashboard")
|
|
|
|
}
|
2024-05-01 01:29:16 -04:00
|
|
|
|
|
|
|
export async function shareSandbox(sandboxId: string, email: string) {
|
2024-05-01 01:53:49 -04:00
|
|
|
const res = await fetch("http://localhost:8787/api/sandbox/share", {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify({ sandboxId, email }),
|
|
|
|
})
|
|
|
|
const text = await res.text()
|
|
|
|
|
|
|
|
if (res.status !== 200) {
|
|
|
|
return { success: false, message: text }
|
2024-05-01 01:29:16 -04:00
|
|
|
}
|
2024-05-01 01:53:49 -04:00
|
|
|
|
|
|
|
revalidatePath(`/code/${sandboxId}`)
|
|
|
|
return { success: true, message: "Shared successfully." }
|
2024-05-01 01:29:16 -04:00
|
|
|
}
|
2024-05-01 02:22:02 -04:00
|
|
|
|
|
|
|
export async function unshareSandbox(sandboxId: string, userId: string) {
|
|
|
|
const res = await fetch("http://localhost:8787/api/sandbox/share", {
|
|
|
|
method: "DELETE",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify({ sandboxId, userId }),
|
|
|
|
})
|
|
|
|
|
|
|
|
revalidatePath(`/code/${sandboxId}`)
|
|
|
|
}
|
2024-05-02 18:05:18 -07:00
|
|
|
|
|
|
|
export async function generateCode(code: string, line: number) {
|
|
|
|
const res = await fetch(
|
|
|
|
"https://api.cloudflare.com/client/v4/accounts/d18f2f848da38e37adc9a34eab3d5ae2/ai/run/@cf/meta/llama-3-8b-instruct",
|
|
|
|
{
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Authorization: `Bearer ${process.env.CF_API_TOKEN}`,
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
messages: [
|
|
|
|
{
|
|
|
|
role: "system",
|
|
|
|
content:
|
|
|
|
"You are an expert coding assistant who reads from an existing code file, and suggests code to add to the file.",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: "user",
|
|
|
|
content: "", //todo
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|