feat(ui): extend AskAI with chat history

This commit is contained in:
shenlan 2025-07-24 11:52:31 +08:00
parent 6589faf06c
commit b184f143fe
4 changed files with 25 additions and 9 deletions

View File

@ -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)`,
})
}

View File

@ -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>

View 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([])
}

View File

@ -26,6 +26,9 @@
], // 👈 添加别名路径
"@i18n/*": [
"i18n/*"
],
"@lib/*": [
"lib/*"
]
},
"types": [
@ -41,6 +44,7 @@
"app",
"components",
"i18n",
"lib",
".next/types/**/*.ts"
],
"exclude": [