添加聊天框消息解析功能;优化请求头处理;更新部分 api base url

This commit is contained in:
2026-03-27 18:00:30 +08:00
parent 8713e5a468
commit a101e79750
11 changed files with 464 additions and 193 deletions
+26 -16
View File
@@ -4,7 +4,12 @@ import { config } from "@config/config";
export type StreamEvent =
| { type: "token"; conversationId: string; content: string }
| { type: "done"; conversationId: string }
| { type: "error"; conversationId?: string; message: string; detail?: string };
| {
type: "error";
conversationId?: string;
message: string;
detail?: string;
};
type StreamOptions = {
message: string;
@@ -40,19 +45,23 @@ export const streamCopilotChat = async ({
}: StreamOptions) => {
let response: Response;
try {
response = await apiFetch(`${config.BACKEND_URL}/api/v1/copilot/chat/stream`, {
method: "POST",
signal,
headers: {
"Content-Type": "application/json",
Accept: "text/event-stream",
response = await apiFetch(
`${config.COPILOT_URL}/api/v1/copilot/chat/stream`,
{
method: "POST",
signal,
headers: {
"Content-Type": "application/json",
Accept: "text/event-stream",
},
body: JSON.stringify({
message,
conversation_id: conversationId,
}),
projectHeaderMode: "include",
skipAuthRedirect: true,
},
body: JSON.stringify({
message,
conversation_id: conversationId,
}),
skipAuthRedirect: true,
});
);
} catch (error) {
const detail = error instanceof Error ? error.message : String(error);
onEvent({
@@ -66,17 +75,18 @@ export const streamCopilotChat = async ({
if (!response.ok || !response.body) {
const detail = await response.text();
let message = "stream request failed";
if (response.status === 403) {
message = "Permission denied. Please contact administrator.";
} else if (response.status === 401) {
message = "Login expired. Please sign in again.";
}
onEvent({
type: "error",
message,
detail: (response.status === 403 || response.status === 401) ? undefined : detail,
detail:
response.status === 403 || response.status === 401 ? undefined : detail,
});
return;
}