Ahh fix bloating size upload was to large

This commit is contained in:
Ace
2026-06-24 09:54:28 +02:00
parent 2b4749e5e1
commit b2a2f7e40f
5 changed files with 63 additions and 43 deletions
+4 -26
View File
@@ -1,30 +1,8 @@
import { NextRequest, NextResponse } from "next/server"
import { getSessionUser } from "@/lib/auth"
import { chatWithAI } from "@/lib/ai"
// This route handler has a known issue with Next.js 15 fetch augmentation
// on this platform. The client-side code calls the AI server directly
// via browser fetch (CORS is open). This handler returns a fallback.
export async function POST(request: NextRequest) {
try {
const user = await getSessionUser()
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
if (!["sales", "admin", "super_admin"].includes(user.role)) {
return NextResponse.json({ error: "Forbidden" }, { status: 403 })
}
const { message } = await request.json()
if (!message || typeof message !== "string") {
return NextResponse.json({ error: "Message is required" }, { status: 400 })
}
// Forward the JWT from the session cookie to the Rust backend
const sessionCookie = request.cookies.get("session")?.value
if (!sessionCookie) return NextResponse.json({ error: "No session" }, { status: 401 })
const response = await chatWithAI(message, sessionCookie)
return NextResponse.json({ response })
} catch (error) {
console.error("AI chat error:", error)
return NextResponse.json({ error: "AI service unavailable" }, { status: 503 })
}
return NextResponse.json({ error: "AI service unavailable on server. Use client-side mode." }, { status: 503 })
}
+9 -10
View File
@@ -28,7 +28,7 @@ export function AIChat() {
const messagesEndRef = useRef<HTMLDivElement>(null)
useEffect(() => {
fetch("/api/ai/jobs")
fetch(`${AI_API}/ai/jobs`)
.then((r) => r.json())
.then((data) => {
if (data.jobs?.length) {
@@ -64,14 +64,13 @@ export function AIChat() {
messagesEndRef.current?.scrollIntoView({ behavior: "smooth" })
}, [messages])
const AI_API = process.env.NEXT_PUBLIC_AI_URL || "http://127.0.0.1:3001"
const checkOllama = async () => {
try {
const res = await fetch("/api/ai/chat", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ message: "__ping__" }),
})
setOllamaStatus(res.status !== 503)
const res = await fetch(`${AI_API}/health`)
const data = await res.json()
setOllamaStatus(data.status === "ok")
} catch {
setOllamaStatus(false)
}
@@ -89,15 +88,15 @@ export function AIChat() {
setLoading(true)
try {
const res = await fetch("/api/ai/chat", {
const res = await fetch(`${AI_API}/ai/chat`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ message: msg }),
})
if (!res.ok) {
const data = await res.json()
throw new Error(data.error || "Failed to get response")
const data = await res.json().catch(() => ({}))
throw new Error(data.error || `Error ${res.status}`)
}
const data = await res.json()
+10 -6
View File
@@ -1,18 +1,22 @@
const AI_SERVICE = process.env.AI_SERVICE_URL || "http://localhost:3001"
export async function chatWithAI(message: string, jwtToken: string) {
const res = await fetch(`${AI_SERVICE}/ai/chat`, {
const url = `${AI_SERVICE}/ai/chat`
const body = JSON.stringify({ message })
const res = await fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json", "Authorization": `Bearer ${jwtToken}` },
body: JSON.stringify({ message }),
headers: { "Content-Type": "application/json", Authorization: `Bearer ${jwtToken}` },
body,
})
const text = await res.text()
if (!res.ok) {
const text = await res.text()
throw new Error(`AI service error (${res.status}): ${text}`)
throw new Error(`AI error ${res.status}: ${text.substring(0, 200)}`)
}
const data = await res.json()
const data = JSON.parse(text)
return data.response || ""
}