Enhance prompt enrichment with local library context and web search results

This commit is contained in:
2026-03-19 21:36:36 +01:00
parent b253f9f44c
commit 68cb83d32a

View File

@@ -366,12 +366,30 @@ async function regenerateFromIndex(index, overrideUserText = null) {
const requestController = beginCancelableRequest(sessionId)
let enrichedPrompt = null
let enrichedPrompt = overrideUserText != null ? overrideUserText : (msgs[lastUserIdx]?.content || '')
let citationSources = []
const contextBlocks = []
try {
const selectedLibrary = getChatLibraryForSession(sessionId)
const promptText = overrideUserText != null ? overrideUserText : (msgs[lastUserIdx]?.content || '')
if (selectedLibrary?.states?.is_indexed) {
try {
const localContext = await fetchLocalLibraryContext(selectedLibrary.slug, promptText, requestController.signal)
if (localContext.contextBlock) {
contextBlocks.push(localContext.contextBlock)
}
if (Array.isArray(localContext.sources)) {
citationSources.push(...localContext.sources)
}
} catch (error) {
if (isAbortError(error)) throw error
console.warn('local library enrichment (regenerate) failed', error)
}
}
if (webSearchEnabled) {
try {
const promptText = overrideUserText != null ? overrideUserText : (msgs[lastUserIdx]?.content || '')
const historyForSearch = msgs
.slice(Math.max(0, lastUserIdx - 7), lastUserIdx + 1)
.map(m => ({ role: m.role, content: m.content || '' }))
@@ -393,9 +411,11 @@ async function regenerateFromIndex(index, overrideUserText = null) {
})
})
const data = await resp.json()
if (data && typeof data.enriched_prompt === 'string') {
enrichedPrompt = data.enriched_prompt
citationSources = Array.isArray(data.sources) ? data.sources : []
if (data && typeof data.context_block === 'string' && data.context_block.trim()) {
contextBlocks.push(data.context_block.trim())
}
if (Array.isArray(data?.sources)) {
citationSources.push(...data.sources)
}
} catch (error) {
if (isAbortError(error)) throw error
@@ -403,6 +423,13 @@ async function regenerateFromIndex(index, overrideUserText = null) {
}
}
citationSources = [...new Set(citationSources)]
if (contextBlocks.length > 0) {
enrichedPrompt = `${promptText}\n\n${contextBlocks.join('\n\n')}`
} else {
enrichedPrompt = null
}
if (streamOutput) {
const assistantMsgId = `msg-${Date.now()}-${Math.random()}`
let full = ''