Refactor API calls to use dedicated helper functions for web search context, chat messaging, and title generation
This commit is contained in:
91
src/App.jsx
91
src/App.jsx
@@ -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,
|
||||||
|
historyLimit: 8,
|
||||||
|
messages: historyForSearch,
|
||||||
|
model,
|
||||||
|
prompt: userMsg.content,
|
||||||
|
searxUrl,
|
||||||
signal: requestController.signal,
|
signal: requestController.signal,
|
||||||
body: JSON.stringify({
|
|
||||||
prompt: userMsg.content,
|
|
||||||
model,
|
|
||||||
messages: historyForSearch,
|
|
||||||
history_limit: 8,
|
|
||||||
searx_url: searxUrl || null,
|
|
||||||
engines: Array.isArray(searxEngines) ? searxEngines : null,
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
const data = await resp.json()
|
if (searchContext.contextBlock) {
|
||||||
if (data && typeof data.context_block === 'string' && data.context_block.trim()) {
|
contextBlocks.push(searchContext.contextBlock)
|
||||||
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' },
|
attachments: outgoingAttachments,
|
||||||
|
enrichedMessage: userMsg.content && contextBlocks.length > 0 ? enrichedPrompt : null,
|
||||||
|
message: userMsg.content,
|
||||||
|
model,
|
||||||
|
sessionId: targetSessionId,
|
||||||
signal: requestController.signal,
|
signal: requestController.signal,
|
||||||
body: JSON.stringify({
|
sources: citationSources || [],
|
||||||
session_id: targetSessionId,
|
stream: true,
|
||||||
model,
|
transcriptionModel,
|
||||||
message: userMsg.content,
|
visionModel,
|
||||||
enriched_message: userMsg.content && contextBlocks.length > 0 ? enrichedPrompt : null,
|
|
||||||
stream: true,
|
|
||||||
sources: citationSources || [],
|
|
||||||
attachments: outgoingAttachments,
|
|
||||||
vision_model: visionModel || null,
|
|
||||||
transcription_model: transcriptionModel || null,
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
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' },
|
attachments: outgoingAttachments,
|
||||||
|
enrichedMessage: userMsg.content && contextBlocks.length > 0 ? enrichedPrompt : null,
|
||||||
|
message: userMsg.content,
|
||||||
|
model,
|
||||||
|
sessionId: targetSessionId,
|
||||||
signal: requestController.signal,
|
signal: requestController.signal,
|
||||||
body: JSON.stringify({
|
sources: citationSources || [],
|
||||||
session_id: targetSessionId,
|
stream: false,
|
||||||
model,
|
transcriptionModel,
|
||||||
message: userMsg.content,
|
visionModel,
|
||||||
enriched_message: userMsg.content && contextBlocks.length > 0 ? enrichedPrompt : null,
|
|
||||||
stream: false,
|
|
||||||
sources: citationSources || [],
|
|
||||||
attachments: outgoingAttachments,
|
|
||||||
vision_model: visionModel || null,
|
|
||||||
transcription_model: transcriptionModel || null,
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
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' },
|
message: buildAttachmentTitleSeed(userMsg.content, outgoingAttachments),
|
||||||
body: JSON.stringify({
|
model,
|
||||||
session_id: targetSessionId,
|
sessionId: targetSessionId,
|
||||||
message: buildAttachmentTitleSeed(userMsg.content, outgoingAttachments),
|
|
||||||
model
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
.then(r => r.json())
|
.then(r => r.json())
|
||||||
.then(data => {
|
.then(data => {
|
||||||
|
|||||||
Reference in New Issue
Block a user