refactor(api): remove AI worker, add ai api route, add usage tiers

- Remove separate AI worker service
- Added generation limits:
  FREE: 1000/month (For the beta version)
  PRO: 500/month
  ENTERPRISE: 1000/month
- Integrate AI functionality into main API routes
- Added monthly generations reset and usage tier upgrade API routes
- Upgrade tier page to be added along with profile page section
This commit is contained in:
Akhileshrangani4
2024-11-23 20:31:24 -05:00
parent 4db378b5f1
commit 34994a8c69
29 changed files with 590 additions and 4049 deletions

View File

@ -89,7 +89,8 @@ export const handleSend = async (
setIsGenerating: React.Dispatch<React.SetStateAction<boolean>>,
setIsLoading: React.Dispatch<React.SetStateAction<boolean>>,
abortControllerRef: React.MutableRefObject<AbortController | null>,
activeFileContent: string
activeFileContent: string,
isEditMode: boolean = false
) => {
// Return if input is empty and context is null
if (input.trim() === "" && !context) return
@ -129,17 +130,17 @@ export const handleSend = async (
}))
// Fetch AI response for chat message component
const response = await fetch(
`${process.env.NEXT_PUBLIC_AI_WORKER_URL}/api`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
const response = await fetch("/api/ai",
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
messages: anthropicMessages,
context: context || undefined,
activeFileContent: activeFileContent,
isEditMode: isEditMode,
}),
signal: abortControllerRef.current.signal,
}
@ -147,7 +148,8 @@ export const handleSend = async (
// Throw error if response is not ok
if (!response.ok) {
throw new Error("Failed to get AI response")
const error = await response.text()
throw new Error(error)
}
// Get reader for chat message component
@ -197,7 +199,7 @@ export const handleSend = async (
console.error("Error fetching AI response:", error)
const errorMessage = {
role: "assistant" as const,
content: "Sorry, I encountered an error. Please try again.",
content: error.message || "Sorry, I encountered an error. Please try again.",
}
setMessages((prev) => [...prev, errorMessage])
}