feat(ui): extend AskAI with chat history
This commit is contained in:
parent
6589faf06c
commit
b184f143fe
@ -1,5 +1,8 @@
|
||||
export async function POST(req: Request) {
|
||||
const { question } = await req.json()
|
||||
// TODO: integrate backend GPT/RAG service
|
||||
return Response.json({ answer: `Answer to: "${question}"\n\n👉 (this is a mock reply)` })
|
||||
const { question, history } = await req.json()
|
||||
// TODO: integrate backend GPT/RAG service and knowledge base using history
|
||||
return Response.json({
|
||||
answer: `Answer to: "${question}"\n\n👉 (this is a mock reply)`,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@ -6,16 +6,19 @@ import { SourceHint } from './SourceHint'
|
||||
|
||||
export function AskAIDialog({ open, onClose }: { open: boolean; onClose: () => void }) {
|
||||
const [question, setQuestion] = useState('')
|
||||
const [answer, setAnswer] = useState<string | null>(null)
|
||||
const [messages, setMessages] = useState<{ sender: 'user' | 'ai'; text: string }[]>([])
|
||||
|
||||
async function handleAsk() {
|
||||
if (!question) return
|
||||
const userMessage = { sender: 'user' as const, text: question }
|
||||
const res = await fetch('/api/askai', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ question }),
|
||||
body: JSON.stringify({ question, history: messages }),
|
||||
})
|
||||
const data = await res.json()
|
||||
setAnswer(data.answer)
|
||||
const aiMessage = { sender: 'ai' as const, text: data.answer as string }
|
||||
setMessages((prev) => [...prev, userMessage, aiMessage])
|
||||
setQuestion('')
|
||||
}
|
||||
|
||||
if (!open) return null
|
||||
@ -46,11 +49,12 @@ export function AskAIDialog({ open, onClose }: { open: boolean; onClose: () => v
|
||||
Ask
|
||||
</button>
|
||||
|
||||
{answer && (
|
||||
{messages.length > 0 && (
|
||||
<div className="mt-6 border-t pt-4 text-gray-800">
|
||||
<div className="text-sm font-bold mb-2">Conversation:</div>
|
||||
<ChatBubble message={question} type="user" />
|
||||
<ChatBubble message={answer} type="ai" />
|
||||
{messages.map((m, idx) => (
|
||||
<ChatBubble key={idx} message={m.text} type={m.sender} />
|
||||
))}
|
||||
|
||||
<SourceHint />
|
||||
</div>
|
||||
|
||||
5
ui/homepage/lib/knowledgeBase.ts
Normal file
5
ui/homepage/lib/knowledgeBase.ts
Normal file
@ -0,0 +1,5 @@
|
||||
export async function fetchRelatedDocs(query: string): Promise<string[]> {
|
||||
// TODO: integrate actual knowledge base search
|
||||
console.warn('fetchRelatedDocs is using a mock implementation')
|
||||
return Promise.resolve([])
|
||||
}
|
||||
@ -26,6 +26,9 @@
|
||||
], // 👈 添加别名路径
|
||||
"@i18n/*": [
|
||||
"i18n/*"
|
||||
],
|
||||
"@lib/*": [
|
||||
"lib/*"
|
||||
]
|
||||
},
|
||||
"types": [
|
||||
@ -41,6 +44,7 @@
|
||||
"app",
|
||||
"components",
|
||||
"i18n",
|
||||
"lib",
|
||||
".next/types/**/*.ts"
|
||||
],
|
||||
"exclude": [
|
||||
|
||||
Loading…
Reference in New Issue
Block a user