refactor(AI): 四份 SSE 请求代码合成 streamAI + useAIStream
学情分析、班级分析、班级 PK、AI 提示四处原来各有一份几乎一样的 fetch + SSE 解析, 错误处理各漏各的: - 裸 fetch 绕过了 axios 拦截器,未登录时只显示「AI 分析生成失败」而不弹登录框。 拦截器里的全站错误处理抽成 handleGlobalApiError,两条路共用 - 班级分析、班级 PK 离开页面不中止流。useAIStream 在作用域销毁时自动 abort - AI 提示换了一次提交不掐上一条,旧提示会写进新提交的面板 utils/stream.ts 的 streamAI 负责请求 + 解析 + 错误翻译;组件用 useAIStream (waiting = 第一个字还没到,running = 流没结束);学情分析在 store 里直接用 streamAI。 浏览器实跑核过:学情分析逐字流出、provider 500 时显示后端的固定中文文案、 未登录调用弹出登录框。班级 PK / 班级分析卸载即中止这条没有在浏览器里点过 (本机库里没有班级数据),逻辑在 useAIStream 的 onScopeDispose。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
09030240b8
commit
c43da01ff2
7 files changed
+202
-205
No files matched your search
+16
-49
@@ -1,5 +1,5 @@
|
||||
import type { DetailsData, DurationData, SolvedProblem } from "utils/types"
|
||||
import { aiStreamError, consumeJSONEventStream } from "utils/stream"
|
||||
import { streamAI } from "utils/stream"
|
||||
import {
|
||||
getAIDetailData,
|
||||
getAIDurationData,
|
||||
@@ -128,10 +128,10 @@ export const useAIStore = defineStore("ai", () => {
|
||||
|
||||
let aiController: AbortController | null = null
|
||||
|
||||
// 不用 useAIStream:store 没有会卸载的作用域,而且 loading.ai 要和其它几个
|
||||
// loading 放在同一个 reactive 里给页面用
|
||||
async function fetchAIAnalysis() {
|
||||
if (aiController) {
|
||||
aiController.abort()
|
||||
}
|
||||
aiController?.abort()
|
||||
const controller = new AbortController()
|
||||
aiController = controller
|
||||
|
||||
@@ -139,58 +139,25 @@ export const useAIStore = defineStore("ai", () => {
|
||||
mdContent.value = ""
|
||||
|
||||
try {
|
||||
const response = await fetch("/api/ai/analysis", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
await streamAI(
|
||||
"ai/analysis",
|
||||
{
|
||||
start: rangeStart.value,
|
||||
end: rangeEnd.value,
|
||||
duration: duration.value,
|
||||
username: targetUsername.value || undefined,
|
||||
}),
|
||||
signal: controller.signal,
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
throw await aiStreamError(response)
|
||||
}
|
||||
|
||||
let hasStarted = false
|
||||
|
||||
await consumeJSONEventStream(response, {
|
||||
signal: controller.signal,
|
||||
onEvent(event) {
|
||||
if (event === "end" && !hasStarted) {
|
||||
loading.ai = false
|
||||
}
|
||||
},
|
||||
onMessage(payload) {
|
||||
const parsed = payload as {
|
||||
type?: string
|
||||
content?: string
|
||||
message?: string
|
||||
}
|
||||
|
||||
if (parsed.type === "delta" && parsed.content) {
|
||||
if (!hasStarted) {
|
||||
hasStarted = true
|
||||
loading.ai = false
|
||||
}
|
||||
mdContent.value += parsed.content
|
||||
} else if (parsed.type === "error") {
|
||||
throw new Error(parsed.message || "AI 服务异常")
|
||||
} else if (parsed.type === "done" && !hasStarted) {
|
||||
{
|
||||
signal: controller.signal,
|
||||
onDelta(content) {
|
||||
loading.ai = false
|
||||
}
|
||||
mdContent.value += content
|
||||
},
|
||||
},
|
||||
})
|
||||
} catch (error: any) {
|
||||
if (controller.signal.aborted) {
|
||||
return
|
||||
}
|
||||
console.error("生成 AI 分析失败", error)
|
||||
const message = error?.message || "生成失败,请稍后再试"
|
||||
mdContent.value = `生成失败:${message}`
|
||||
)
|
||||
} catch (error) {
|
||||
if (controller.signal.aborted) return
|
||||
mdContent.value = `生成失败:${(error as Error).message}`
|
||||
} finally {
|
||||
if (aiController === controller) {
|
||||
aiController = null
|
||||
|
||||
Reference in new issue
Block a user