feat:前端修改(最后一次)

This commit is contained in:
2026-09-14 20:58:47 +08:00
parent d9c2c5e207
commit 0835335978
6 changed files with 395 additions and 90 deletions
+62 -22
View File
@@ -29,6 +29,22 @@ function handleUnauthorized(status: number) {
if (!window.location.pathname.startsWith("/login")) window.location.href = "/login";
}
/**
* 从错误响应体里取后端给的原因(如「会话不存在或已过期」)。
* 非 2xx 时后端返回的是 JSON(utils/response.fail),部分场景也会包成 SSE。
*/
function extractErrorMessage(body: string): string | undefined {
const text = body.trim();
if (!text) return undefined;
const jsonText = text.startsWith("data:") ? text.split("\n")[0].slice(5).trim() : text;
try {
const payload = JSON.parse(jsonText) as { message?: string; error?: string };
return payload?.message ?? payload?.error;
} catch {
return undefined;
}
}
/**
* 后端存在两套成功码,必须都认:
* - utils/response.success → code 200(工作台、风控、工单等)
@@ -126,6 +142,8 @@ export interface SSESummary {
traceId?: string;
/** error 事件里的提示文案。 */
error?: string;
/** 客服 Agent 命中的知识来源(裸 JSON 响应里的 sources)。 */
sources?: unknown[];
}
/**
@@ -152,7 +170,13 @@ export async function readSSE(
});
handleUnauthorized(response.status);
if (!response.ok) throw new ApiError("请求失败,请稍后重试", response.status);
if (!response.ok) {
const body = await response.text();
throw new ApiError(
extractErrorMessage(body) ?? "请求失败,请稍后重试",
response.status
);
}
const summary: SSESummary = { text: "" };
const raw = await response.text();
@@ -170,29 +194,45 @@ export async function readSSE(
continue; /* 忽略心跳与非 JSON 块 */
}
if (event.type === "text") {
summary.text += event.content ?? "";
} else if (event.type === "meta") {
summary.intent = event.intent;
summary.draftId = event.draft_id;
summary.queryId = event.query_id;
summary.traceId = event.trace_id;
} else if (event.type === "done") {
if (event.draft_id) summary.draftId = event.draft_id;
if (event.query_id) summary.queryId = event.query_id;
} else if (event.type === "error") {
summary.error = event.message ?? "助手返回错误,请稍后重试";
const record = event as Record<string, unknown>;
const type = typeof record.type === "string" ? record.type : undefined;
if (type === "text") {
summary.text += (record.content as string) ?? "";
} else if (type === "meta") {
summary.intent = record.intent as string | undefined;
summary.draftId = record.draft_id as string | undefined;
summary.queryId = record.query_id as string | undefined;
summary.traceId = record.trace_id as string | undefined;
} else if (type === "done") {
if (record.draft_id) summary.draftId = record.draft_id as string;
if (record.query_id) summary.queryId = record.query_id as string;
} else if (type === "error") {
summary.error = (record.message as string) ?? "助手返回错误,请稍后重试";
} else if (!type) {
/*
* 无 type 的响应——客服 Agent / 客户 Agent 不用事件包装,直接 dump 业务对象:
* service/customer_agent/chat.py → {answer, sources, intent, rewritten_query, trace_id}
* 另兼容 success() 包装的 {code, message, data}(取值时自动下钻一层 data)。
* 旧实现只认 type=text,导致这两个 Agent 的回复恒为空串(聊天框永远回「暂时没有找到合适的回答」)。
*/
const node =
record.data && typeof record.data === "object"
? (record.data as Record<string, unknown>)
: record;
const answer = node.answer ?? node.content ?? node.summary;
if (typeof answer === "string" && answer) {
summary.text += answer;
} else if (typeof record.message === "string" && record.message) {
summary.error = record.message;
}
if (typeof node.intent === "string") summary.intent = node.intent;
if (typeof node.query_id === "string") summary.queryId = node.query_id;
const trace = node.trace_id ?? record.trace_id;
if (typeof trace === "string") summary.traceId = trace;
if (Array.isArray(node.sources)) summary.sources = node.sources;
}
}
return summary;
}
/**
* 取 SSE 响应的正文文本(客服/客户 Agent 聊天用)。
* 旧实现只取第一个 data 行,命中 meta 事件时会拿到空串,这里改为聚合全部 text 事件。
*/
export async function apiStream(path: string, body: unknown): Promise<string> {
const summary = await readSSE(path, body);
return summary.error || summary.text;
}