Refa: align chat and search restful APIs (#14229)

### What problem does this PR solve?

Refactor /api/v1/chats to be more RESTful.

### Type of change

- [x] Refactoring

---------

Co-authored-by: Jin Hai <haijin.chn@gmail.com>
This commit is contained in:
buua436
2026-04-22 10:49:11 +08:00
committed by GitHub
parent bfac0195df
commit 6baf74afc1
14 changed files with 362 additions and 160 deletions

View File

@@ -295,18 +295,17 @@ export const useSendMessageWithSse = () => {
return {
...d,
answer: newAnswer,
conversationId: body?.conversation_id,
conversationId: body?.session_id ?? body?.conversation_id,
chatBoxId: body.chatBoxId,
};
});
}
} catch (e) {
} catch {
// Swallow parse errors silently
}
}
} catch (e) {
if (e instanceof DOMException && e.name === 'AbortError') {
console.log('Request was aborted by user or logic.');
} catch (error) {
if (error instanceof DOMException && error.name === 'AbortError') {
break;
}
}
@@ -314,7 +313,7 @@ export const useSendMessageWithSse = () => {
setDoneValue(body, true);
resetAnswer();
return { data: await res, response };
} catch (e) {
} catch {
setDoneValue(body, true);
resetAnswer();
@@ -357,7 +356,7 @@ export const useSpeechWithSse = (url: string = api.chatsTts) => {
if (res?.code !== 0) {
message.error(res?.message);
}
} catch (error) {
} catch {
// Swallow errors silently
}
return response;

View File

@@ -98,8 +98,10 @@ export const useSendMessage = (controller: AbortController) => {
} & NextMessageInputOnPressEnterParameter) => {
const sessionId = currentConversationId ?? conversationId;
const res = await send(
api.completionUrl(chatId!, sessionId),
api.completionUrl,
{
chat_id: chatId,
session_id: sessionId,
messages: [
...(Array.isArray(messages) && messages?.length > 0
? messages

View File

@@ -67,8 +67,10 @@ export function useSendSingleMessage({
} & NextMessageInputOnPressEnterParameter) => {
const sessionId = currentConversationId ?? conversationId;
const res = await send(
api.completionUrl(chatId!, sessionId),
api.completionUrl,
{
chat_id: chatId,
session_id: sessionId,
messages: [
...(Array.isArray(messages) && messages?.length > 0
? messages
@@ -92,6 +94,7 @@ export function useSendSingleMessage({
[
derivedMessages,
conversationId,
chatId,
removeLatestMessage,
setValue,
send,

View File

@@ -308,7 +308,11 @@ export const useSendQuestion = (
related_search: boolean = false,
) => {
const { sharedId } = useGetSharedSearchParams();
const askUrl = sharedId ? api.askShare : api.ask;
const askUrl = sharedId
? api.askShare
: searchId
? api.searchCompletion(searchId)
: '';
const { send, answer, done, stopOutputMessage } = useSendMessageWithSse();
const { testChunk, loading } = useTestChunkRetrieval(tenantId);
@@ -331,12 +335,15 @@ export const useSendQuestion = (
setIsFirstRender(false);
setCurrentAnswer({} as IAnswer);
if (enableAI) {
if (!sharedId && !searchId) {
message.error('Search ID is required.');
return;
}
setSendingLoading(true);
send(askUrl, {
kb_ids: kbIds,
question: q,
tenantId,
search_id: searchId,
});
}
testChunk({
@@ -355,12 +362,14 @@ export const useSendQuestion = (
[
send,
testChunk,
askUrl,
kbIds,
fetchRelatedQuestions,
setPagination,
pagination.pageSize,
tenantId,
searchId,
sharedId,
related_search,
],
);

View File

@@ -17,7 +17,6 @@ const {
deleteMessage,
thumbup,
chatsTts,
ask,
chatsMindmap,
chatsRelatedQuestions,
uploadAndParse,
@@ -67,7 +66,7 @@ const methods = {
},
updateSession: {
url: updateSession,
method: 'put',
method: 'patch',
},
removeSessions: {
url: removeSessions,
@@ -79,16 +78,12 @@ const methods = {
},
thumbup: {
url: thumbup,
method: 'put',
method: 'patch',
},
chatsTts: {
url: chatsTts,
method: 'post',
},
ask: {
url: ask,
method: 'post',
},
chatsMindmap: {
url: chatsMindmap,
method: 'post',

View File

@@ -52,7 +52,7 @@ export default {
// plugin
llmTools: `${webAPI}/plugin/llm_tools`,
chatsTranscriptions: `${restAPIv1}/chats/transcriptions`,
chatsTranscriptions: `${restAPIv1}/chat/audio/transcription`,
// knowledge base
@@ -147,12 +147,12 @@ export default {
`${restAPIv1}/chats/${chatId}/sessions/${sessionId}/messages/${msgId}`,
thumbup: (chatId: string, sessionId: string, msgId: string) =>
`${restAPIv1}/chats/${chatId}/sessions/${sessionId}/messages/${msgId}/feedback`,
completionUrl: (chatId: string, sessionId: string) =>
`${restAPIv1}/chats/${chatId}/sessions/${sessionId}/completions`,
chatsTts: `${restAPIv1}/chats/tts`,
ask: `${restAPIv1}/chats/ask`,
chatsMindmap: `${restAPIv1}/chats/mindmap`,
chatsRelatedQuestions: `${restAPIv1}/chats/related_questions`,
completionUrl: `${restAPIv1}/chat/completions`,
chatsTts: `${restAPIv1}/chat/audio/speech`,
searchCompletion: (searchId: string) =>
`${restAPIv1}/searches/${searchId}/completion`,
chatsMindmap: `${restAPIv1}/chat/mindmap`,
chatsRelatedQuestions: `${restAPIv1}/chat/recommandation`,
// next chat
fetchExternalChatInfo: (id: string) => `${restAPIv1}/chatbots/${id}/info`,