Refactor API calls to use dedicated helper functions for web search context, chat messaging, and title generation

This commit is contained in:
2026-05-06 03:48:57 +02:00
parent 34da4bd6c5
commit 3440b48c8c

View File

@@ -1411,25 +1411,21 @@ async function sendMessage() {
if (userMsg.content && webSearchEnabled) { if (userMsg.content && webSearchEnabled) {
try { try {
const resp = await fetch(`${backendApiUrl}/websearch`, { const searchContext = await fetchWebSearchContext({
method: 'POST', apiBase: backendApiUrl,
headers: { 'Content-Type': 'application/json' }, engines: searxEngines,
signal: requestController.signal, historyLimit: 8,
body: JSON.stringify({
prompt: userMsg.content,
model,
messages: historyForSearch, messages: historyForSearch,
history_limit: 8, model,
searx_url: searxUrl || null, prompt: userMsg.content,
engines: Array.isArray(searxEngines) ? searxEngines : null, searxUrl,
signal: requestController.signal,
}) })
}) if (searchContext.contextBlock) {
const data = await resp.json() contextBlocks.push(searchContext.contextBlock)
if (data && typeof data.context_block === 'string' && data.context_block.trim()) {
contextBlocks.push(data.context_block.trim())
} }
if (Array.isArray(data?.sources)) { if (Array.isArray(searchContext.sources)) {
citationSources.push(...data.sources) citationSources.push(...searchContext.sources)
} }
} catch (error) { } catch (error) {
if (isAbortError(error)) throw error if (isAbortError(error)) throw error
@@ -1455,21 +1451,18 @@ async function sendMessage() {
) )
try { try {
const res = await fetch(`${backendApiUrl}/chat`, { const res = await postChatMessage({
method: 'POST', apiBase: backendApiUrl,
headers: { 'Content-Type': 'application/json' },
signal: requestController.signal,
body: JSON.stringify({
session_id: targetSessionId,
model,
message: userMsg.content,
enriched_message: userMsg.content && contextBlocks.length > 0 ? enrichedPrompt : null,
stream: true,
sources: citationSources || [],
attachments: outgoingAttachments, attachments: outgoingAttachments,
vision_model: visionModel || null, enrichedMessage: userMsg.content && contextBlocks.length > 0 ? enrichedPrompt : null,
transcription_model: transcriptionModel || null, message: userMsg.content,
}) model,
sessionId: targetSessionId,
signal: requestController.signal,
sources: citationSources || [],
stream: true,
transcriptionModel,
visionModel,
}) })
if (!res.ok) { if (!res.ok) {
const error = new Error(await readBackendErrorText(res)) const error = new Error(await readBackendErrorText(res))
@@ -1542,21 +1535,18 @@ async function sendMessage() {
return return
} }
} else { } else {
const res = await fetch(`${backendApiUrl}/chat`, { const res = await postChatMessage({
method: 'POST', apiBase: backendApiUrl,
headers: { 'Content-Type': 'application/json' },
signal: requestController.signal,
body: JSON.stringify({
session_id: targetSessionId,
model,
message: userMsg.content,
enriched_message: userMsg.content && contextBlocks.length > 0 ? enrichedPrompt : null,
stream: false,
sources: citationSources || [],
attachments: outgoingAttachments, attachments: outgoingAttachments,
vision_model: visionModel || null, enrichedMessage: userMsg.content && contextBlocks.length > 0 ? enrichedPrompt : null,
transcription_model: transcriptionModel || null, message: userMsg.content,
}) model,
sessionId: targetSessionId,
signal: requestController.signal,
sources: citationSources || [],
stream: false,
transcriptionModel,
visionModel,
}) })
if (!res.ok) { if (!res.ok) {
const error = new Error(await readBackendErrorText(res)) const error = new Error(await readBackendErrorText(res))
@@ -1599,14 +1589,11 @@ async function sendMessage() {
} }
if (isNewChat) { if (isNewChat) {
fetch(`${backendApiUrl}/generate-title`, { requestGeneratedTitle({
method: 'POST', apiBase: backendApiUrl,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
session_id: targetSessionId,
message: buildAttachmentTitleSeed(userMsg.content, outgoingAttachments), message: buildAttachmentTitleSeed(userMsg.content, outgoingAttachments),
model model,
}) sessionId: targetSessionId,
}) })
.then(r => r.json()) .then(r => r.json())
.then(data => { .then(data => {