Fix Ask AI dialog to proxy requests via Next API (#518)

This commit is contained in:
shenlan 2025-10-14 16:04:54 +08:00 committed by GitHub
parent 13e7a5937b
commit d48846e5b5
3 changed files with 23 additions and 7 deletions

View File

@ -0,0 +1,21 @@
import { getServerServiceBaseUrl } from '@lib/serviceConfig'
export async function POST(req: Request) {
try {
const { question, history } = await req.json()
const apiBase = getServerServiceBaseUrl()
const response = await fetch(`${apiBase}/api/rag/query`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ question, history })
})
const data = await response.json().catch(() => null)
return Response.json(data ?? { error: 'Invalid response from server' }, {
status: response.status
})
} catch (error) {
const message = error instanceof Error ? error.message : 'Unknown error'
return Response.json({ error: message }, { status: 500 })
}
}

View File

@ -4,12 +4,10 @@ import { useState } from 'react'
import { Bot } from 'lucide-react'
import { AskAIDialog } from './AskAIDialog'
import { useAccess } from '@lib/accessControl'
import { getServerServiceBaseUrl } from '@lib/serviceConfig'
export function AskAIButton() {
const [open, setOpen] = useState(false)
const [minimized, setMinimized] = useState(false)
const apiBase = getServerServiceBaseUrl()
const { allowed, isLoading } = useAccess({ allowGuests: true })
if (!allowed && !isLoading) {
@ -32,7 +30,6 @@ export function AskAIButton() {
<AskAIDialog
open={open}
apiBase={apiBase}
onMinimize={() => {
setOpen(false)
setMinimized(true)

View File

@ -11,12 +11,10 @@ const MAX_CACHE_SIZE = 50
export function AskAIDialog({
open,
apiBase,
onMinimize,
onEnd
}: {
open: boolean
apiBase: string
onMinimize: () => void
onEnd: () => void
}) {
@ -171,7 +169,7 @@ export function AskAIDialog({
try {
let { answer, retrieved } = await streamChat(
`${apiBase}/api/rag/query`,
'/api/rag/query',
{ question: normalized, history },
updateAI
)
@ -184,7 +182,7 @@ export function AskAIDialog({
)
try {
const result = await streamChat(
`${apiBase}/api/askai`,
'/api/askai',
{ question: normalized, history },
updateAI
)