mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 00:50:36 +08:00
The knowledge source picker fired all six source-library requests inside one Promise.all and only rendered after every kind settled. A large question bank (tens of thousands of practice entries accumulated server-side) kept the whole panel on a spinner, so a ready knowledge base could not be selected even after its own request had returned. Land each kind in its own slice as its request settles, and gate each consumer on the active kind's readiness instead of the aggregate. A ready KB is now selectable while the question bank is still loading or has failed; per-kind error alerts no longer paint a failure on a ready tab. The aggregate loading flag still clears once everything settles. Refs #2641
127 lines
4.4 KiB
TypeScript
127 lines
4.4 KiB
TypeScript
'use client'
|
|
import { useMemo, useState } from 'react'
|
|
import { useTranslation } from 'react-i18next'
|
|
import {
|
|
catalogTranslate,
|
|
useTopicSourceLibrary,
|
|
sourceRequest,
|
|
type SourceCandidate,
|
|
} from '@/hooks/useTopicSourceLibrary'
|
|
import { scopedUrl } from '@/lib/workspace-scope'
|
|
import PickerShell from '@/components/common/PickerShell'
|
|
import type { NotePage } from '@/lib/notes-api'
|
|
|
|
export default function NoteSourcePicker({
|
|
onClose,
|
|
onInsert,
|
|
onKnowledge,
|
|
}: {
|
|
onClose: () => void
|
|
onInsert: (title: string, markdown: string, url: string, id: string) => void
|
|
onKnowledge: (name: string, workspace: string) => void
|
|
}) {
|
|
const { t, i18n } = useTranslation()
|
|
const zh = i18n.language.startsWith('zh')
|
|
const tr = useMemo(() => catalogTranslate(t, zh), [t, zh])
|
|
const { library, loading } = useTopicSourceLibrary(tr)
|
|
const [query, setQuery] = useState('')
|
|
const [busy, setBusy] = useState(false)
|
|
const [error, setError] = useState('')
|
|
const items = [...library.drafts, ...library.chats, ...library.knowledgeBases].filter(
|
|
row => row.available && row.label.toLowerCase().includes(query.toLowerCase())
|
|
)
|
|
// Only these three kinds feed this picker, so wait on them rather than the
|
|
// slow question-bank request the shared hook also fires.
|
|
const pending = library.loadingKinds
|
|
? library.loadingKinds.some(kind => ['drafts', 'chats', 'knowledgeBases'].includes(kind))
|
|
: loading
|
|
const select = async (row: SourceCandidate) => {
|
|
setBusy(true)
|
|
try {
|
|
if (row.kind === 'knowledge_base') {
|
|
onKnowledge(row.sourceId, row.content_workspace_id ?? '')
|
|
onClose()
|
|
return
|
|
}
|
|
let text = row.excerpt || row.detail
|
|
if (row.kind === 'cowriter') {
|
|
const page = await sourceRequest<NotePage>(
|
|
`/api/documents/${row.sourceId}`,
|
|
row.content_workspace_id
|
|
)
|
|
text = page.content
|
|
} else if (row.kind === 'chat') {
|
|
const session = await sourceRequest<{ messages: { role: string; content: unknown }[] }>(
|
|
`/api/sessions/${row.sourceId}`,
|
|
row.content_workspace_id
|
|
)
|
|
text = (session.messages || [])
|
|
.filter(m => ['user', 'assistant'].includes(m.role))
|
|
.map(
|
|
m => `${m.role === 'user' ? t('Question') : t('Answer')}\n\n${String(m.content || '')}`
|
|
)
|
|
.join('\n\n')
|
|
.slice(0, 180000)
|
|
}
|
|
const path = row.kind === 'cowriter' ? `/co-writer/${row.sourceId}` : `/chat/${row.sourceId}`
|
|
onInsert(row.label, text, scopedUrl(path, row.content_workspace_id), row.sourceId)
|
|
onClose()
|
|
} catch (err) {
|
|
setError(String(err))
|
|
} finally {
|
|
setBusy(false)
|
|
}
|
|
}
|
|
return (
|
|
<PickerShell open onClose={onClose} ariaLabel={t('Reference a source')}>
|
|
<div className="w-[min(540px,90vw)] rounded-xl bg-[var(--card)] p-4">
|
|
<div className="mb-3 flex items-center justify-between">
|
|
<h2 className="text-sm font-medium">{t('Reference a source')}</h2>
|
|
<button className="dt-note-button" onClick={onClose}>
|
|
{t('Close')}
|
|
</button>
|
|
</div>
|
|
<input
|
|
autoFocus
|
|
aria-label={t('Search sources')}
|
|
placeholder={t('Search sources')}
|
|
className="mb-3 w-full rounded bg-[var(--secondary)] p-2 text-sm"
|
|
value={query}
|
|
onChange={e => setQuery(e.target.value)}
|
|
/>
|
|
<div className="max-h-96 overflow-y-auto">
|
|
{pending && items.length === 0 ? (
|
|
<p>…</p>
|
|
) : (
|
|
items.map(row => (
|
|
<button
|
|
key={row.key}
|
|
disabled={busy}
|
|
className="block w-full rounded px-2 py-2 text-left text-sm hover:bg-[var(--secondary)]"
|
|
onClick={() => void select(row)}
|
|
>
|
|
<span>{row.label}</span>
|
|
<span className="block text-xs text-[var(--muted-foreground)]">
|
|
{t(
|
|
row.kind === 'cowriter'
|
|
? 'Learning notebook'
|
|
: row.kind === 'chat'
|
|
? 'Conversation'
|
|
: 'Knowledge base'
|
|
)}{' '}
|
|
· {row.content_workspace_name}
|
|
</span>
|
|
</button>
|
|
))
|
|
)}
|
|
</div>
|
|
{error && (
|
|
<p role="alert" className="text-sm text-red-600">
|
|
{error}
|
|
</p>
|
|
)}
|
|
</div>
|
|
</PickerShell>
|
|
)
|
|
}
|