diff --git a/apps/web/src/oj/problem/components/SubmissionEffects.vue b/apps/web/src/oj/problem/components/SubmissionEffects.vue
new file mode 100644
index 0000000..6114280
--- /dev/null
+++ b/apps/web/src/oj/problem/components/SubmissionEffects.vue
@@ -0,0 +1,205 @@
+
+
+
+
+
+
+
diff --git a/apps/web/src/oj/problem/components/SubmissionResult.vue b/apps/web/src/oj/problem/components/SubmissionResult.vue
index a47e8ef..250be47 100644
--- a/apps/web/src/oj/problem/components/SubmissionResult.vue
+++ b/apps/web/src/oj/problem/components/SubmissionResult.vue
@@ -2,7 +2,7 @@
import { Icon } from "@iconify/vue"
import { useThemeVars } from "naive-ui"
import { HINT_MIN_FAILURES, hintLevelLabel } from "@oj2/contract"
-import type { AiHintDone, JudgeCaseResult } from "@oj2/contract"
+import type { JudgeCaseResult } from "@oj2/contract"
import { JUDGE_STATUS, SubmissionStatus } from "utils/constants"
import {
submissionCaseResults,
@@ -14,12 +14,11 @@ import {
import type { Submission } from "utils/types"
import SubmissionResultTag from "shared/components/SubmissionResultTag.vue"
import { useProblemStore } from "oj/store/problem"
+import { useSubmissionStore } from "oj/store/submission"
import PythonErrorExplain from "./PythonErrorExplain.vue"
import RuntimeErrorExplain from "./RuntimeErrorExplain.vue"
import WrongAnswerExplain from "./WrongAnswerExplain.vue"
import LessonNext from "./LessonNext.vue"
-import { useAIStream } from "shared/composables/aiStream"
-import { submitHintFeedback } from "oj/api"
import { MdPreview } from "md-editor-v3"
import "md-editor-v3/lib/preview.css"
import { useDark } from "@vueuse/core"
@@ -32,61 +31,22 @@ const isDark = useDark()
const problemStore = useProblemStore()
const theme = useThemeVars()
-// AI 提示状态。
-// hintTarget 是后端发来的全文,hintContent 是已经"打"出来的那一截 ——
-// 后端从 2c 起整段生成、过滤通过才推(设计 2.6),一次就把全文发过来,
-// 逐字显示改在这边模拟。
-const hintTarget = ref("")
-const hintContent = ref("")
-const hintStream = useAIStream()
-// 整条流跑完才算结束:提示是一次推全文,没有「第一个字到了」的中间态
-const hintLoading = hintStream.running
-const hintError = ref("")
-// 这条提示在 ai_hint 里的 id,生成完由 done 事件带回来;后端落库失败时没有,就不出评价按钮
-const hintId = ref(null)
-// 这条提示是哪一级(-1 = 编译错误那一档,不在阶梯上),以及还能不能再往上要一级。
-// 两个都由后端算好在 done 里给,前端不自己推阶梯
-const hintLevel = ref(null)
-const hintCanEscalate = ref(false)
-const hintHelpful = ref(null)
-const hintFeedbackSending = ref(false)
-
-// 打字机:每 24ms 吐 3 个字,约 125 字/秒。步子不敢迈太小 ——
-// 每一帧都要让 MdPreview 重渲染一次 Markdown,机房那批机器扛不住逐字
-const TYPE_STEP = 3
-const TYPE_INTERVAL = 24
-let typingTimer: ReturnType | null = null
-const hintTyping = computed(() => hintContent.value.length < hintTarget.value.length)
-
-function stopTyping() {
- if (typingTimer === null) return
- clearInterval(typingTimer)
- typingTimer = null
-}
-
-function startTyping() {
- if (typingTimer !== null) return
- typingTimer = setInterval(() => {
- if (!hintTyping.value) {
- stopTyping()
- return
- }
- hintContent.value = hintTarget.value.slice(0, hintContent.value.length + TYPE_STEP)
- }, TYPE_INTERVAL)
-}
-
-function resetHint() {
- stopTyping()
- hintTarget.value = ""
- hintContent.value = ""
- hintError.value = ""
- hintId.value = null
- hintLevel.value = null
- hintCanEscalate.value = false
- hintHelpful.value = null
-}
-
-onUnmounted(stopTyping)
+// AI 提示的状态在 submission store 里(见 oj/problem/composables/useSubmissionHint.ts):
+// 这个组件在提交按钮那边,编辑器一卸载它就跟着卸载,已经生成的提示不能跟着没
+const {
+ hintTarget,
+ hintContent,
+ hintLoading,
+ hintError,
+ hintId,
+ hintLevel,
+ hintCanEscalate,
+ hintHelpful,
+ hintFeedbackSending,
+ hintTyping,
+ fetchHint,
+ sendHintFeedback,
+} = useSubmissionStore().hint
/** 判题机的临时目录名(`/judger/run/<32 位随机串>/`)对学生没有意义,只剩文件名 */
function stripJudgePath(text: string) {
@@ -188,59 +148,6 @@ const showAIHint = computed(() => {
)
})
-// 结果面板现在是 display-directive="show",关掉不再销毁组件,提示内容能留到重新打开。
-// 代价是换了一次提交它也留着,所以这里按提交 id 手动清一次 —— 否则新结果底下挂着
-// 上一次提交的提示,而且按钮已经被 v-if 藏了,学生没法重新分析。
-watch(
- () => props.submission?.id,
- () => {
- // 上一次提交的提示还在生成的话掐掉,不然它会写进新提交的面板里
- hintStream.abort()
- resetHint()
- },
-)
-
-// more = 学生点的是「再多一点提示」。升级只能由学生主动发起,而且后端还要看
-// 「上次开出这一级之后有没有再交过」,所以点了也未必真升 —— 以 done 里的 level 为准
-async function fetchHint(submissionId: string, more = false) {
- resetHint()
- try {
- await hintStream.run(
- "ai/hint",
- { submissionId, more },
- {
- onDelta(content) {
- hintTarget.value += content
- startTyping()
- },
- onDone(data) {
- hintId.value = data.hintId ?? null
- hintLevel.value = data.level ?? null
- hintCanEscalate.value = data.canEscalate === true
- },
- },
- )
- } catch (error) {
- hintError.value = (error as Error).message
- }
-}
-
-// 可以改票:点另一个就覆盖。失败了不打扰学生,按钮恢复原样就行 ——
-// 评价是给我们看的,不值得为它弹一条报错
-async function sendHintFeedback(helpful: boolean) {
- if (hintId.value === null || hintFeedbackSending.value) return
- if (hintHelpful.value === helpful) return
- hintFeedbackSending.value = true
- try {
- await submitHintFeedback(hintId.value, helpful)
- hintHelpful.value = helpful
- } catch {
- // 静默
- } finally {
- hintFeedbackSending.value = false
- }
-}
-
// 测试用例表格数据(只在部分通过时显示)
const infoTable = computed(() => {
const submission = props.submission
diff --git a/apps/web/src/oj/problem/components/SubmitCode.vue b/apps/web/src/oj/problem/components/SubmitCode.vue
index 7c80712..b00338d 100644
--- a/apps/web/src/oj/problem/components/SubmitCode.vue
+++ b/apps/web/src/oj/problem/components/SubmitCode.vue
@@ -1,319 +1,56 @@
+ display-directive 默认是 "if":面板一收起来整个 SubmissionResult 就被卸载。
+ AI 提示的内容现在在 store 里,卸了也不丢,但重新挂载要重渲染 Markdown、
+ 重新拉一次「这节课的下一题」,留着更省事。 -->
-
@@ -358,17 +94,4 @@ watch(
>
上次结果
-
-
-
-
-
diff --git a/apps/web/src/oj/problem/composables/useSubmissionHint.ts b/apps/web/src/oj/problem/composables/useSubmissionHint.ts
new file mode 100644
index 0000000..2eb8c5c
--- /dev/null
+++ b/apps/web/src/oj/problem/composables/useSubmissionHint.ts
@@ -0,0 +1,131 @@
+import type { AiHintDone } from "@oj2/contract"
+import { submitHintFeedback } from "oj/api"
+import { useAIStream } from "shared/composables/aiStream"
+
+/**
+ * 一次提交的 AI 提示。
+ *
+ * 原来这些状态长在结果面板(SubmissionResult)里,而结果面板在提交按钮那个组件里 ——
+ * 手机上切一下页签、桌面切一下分栏模式,编辑器连同它一起卸载,已经生成的提示没了,
+ * 那次 LLM 调用白花。现在由 submission store 持有(见 oj/store/submission.ts)。
+ *
+ * hintTarget 是后端发来的全文,hintContent 是已经「打」出来的那一截 ——
+ * 后端从 2c 起整段生成、过滤通过才推(设计 2.6),一次就把全文发过来,
+ * 逐字显示改在这边模拟。
+ */
+export function useSubmissionHint() {
+ const hintTarget = ref("")
+ const hintContent = ref("")
+ const hintStream = useAIStream()
+ // 整条流跑完才算结束:提示是一次推全文,没有「第一个字到了」的中间态
+ const hintLoading = hintStream.running
+ const hintError = ref("")
+ // 这条提示在 ai_hint 里的 id,生成完由 done 事件带回来;后端落库失败时没有,就不出评价按钮
+ const hintId = ref(null)
+ // 这条提示是哪一级(-1 = 编译错误那一档,不在阶梯上),以及还能不能再往上要一级。
+ // 两个都由后端算好在 done 里给,前端不自己推阶梯
+ const hintLevel = ref(null)
+ const hintCanEscalate = ref(false)
+ const hintHelpful = ref(null)
+ const hintFeedbackSending = ref(false)
+
+ // 打字机:每 24ms 吐 3 个字,约 125 字/秒。步子不敢迈太小 ——
+ // 每一帧都要让 MdPreview 重渲染一次 Markdown,机房那批机器扛不住逐字
+ const TYPE_STEP = 3
+ const TYPE_INTERVAL = 24
+ let typingTimer: ReturnType | null = null
+ const hintTyping = computed(() => hintContent.value.length < hintTarget.value.length)
+
+ function stopTyping() {
+ if (typingTimer === null) return
+ clearInterval(typingTimer)
+ typingTimer = null
+ }
+
+ function startTyping() {
+ if (typingTimer !== null) return
+ typingTimer = setInterval(() => {
+ if (!hintTyping.value) {
+ stopTyping()
+ return
+ }
+ hintContent.value = hintTarget.value.slice(0, hintContent.value.length + TYPE_STEP)
+ }, TYPE_INTERVAL)
+ }
+
+ function resetHint() {
+ stopTyping()
+ hintTarget.value = ""
+ hintContent.value = ""
+ hintError.value = ""
+ hintId.value = null
+ hintLevel.value = null
+ hintCanEscalate.value = false
+ hintHelpful.value = null
+ }
+
+ /** 换了一次提交:还在生成的那条掐掉(不然它会写进新提交的面板里),内容清空 */
+ function clearHint() {
+ hintStream.abort()
+ resetHint()
+ }
+
+ // more = 学生点的是「再多一点提示」。升级只能由学生主动发起,而且后端还要看
+ // 「上次开出这一级之后有没有再交过」,所以点了也未必真升 —— 以 done 里的 level 为准
+ async function fetchHint(submissionId: string, more = false) {
+ resetHint()
+ try {
+ await hintStream.run(
+ "ai/hint",
+ { submissionId, more },
+ {
+ onDelta(content) {
+ hintTarget.value += content
+ startTyping()
+ },
+ onDone(data) {
+ hintId.value = data.hintId ?? null
+ hintLevel.value = data.level ?? null
+ hintCanEscalate.value = data.canEscalate === true
+ },
+ },
+ )
+ } catch (error) {
+ hintError.value = (error as Error).message
+ }
+ }
+
+ // 可以改票:点另一个就覆盖。失败了不打扰学生,按钮恢复原样就行 ——
+ // 评价是给我们看的,不值得为它弹一条报错
+ async function sendHintFeedback(helpful: boolean) {
+ if (hintId.value === null || hintFeedbackSending.value) return
+ if (hintHelpful.value === helpful) return
+ hintFeedbackSending.value = true
+ try {
+ await submitHintFeedback(hintId.value, helpful)
+ hintHelpful.value = helpful
+ } catch {
+ // 静默
+ } finally {
+ hintFeedbackSending.value = false
+ }
+ }
+
+ if (getCurrentScope()) onScopeDispose(stopTyping)
+
+ return {
+ hintTarget,
+ hintContent,
+ hintLoading,
+ hintError,
+ hintId,
+ hintLevel,
+ hintCanEscalate,
+ hintHelpful,
+ hintFeedbackSending,
+ hintTyping,
+ fetchHint,
+ sendHintFeedback,
+ clearHint,
+ }
+}
diff --git a/apps/web/src/oj/problem/composables/useSubmissionMonitor.ts b/apps/web/src/oj/problem/composables/useSubmissionMonitor.ts
index 0203a4e..58e65a3 100644
--- a/apps/web/src/oj/problem/composables/useSubmissionMonitor.ts
+++ b/apps/web/src/oj/problem/composables/useSubmissionMonitor.ts
@@ -1,4 +1,4 @@
-import { ref, computed, onUnmounted } from "vue"
+import { ref, computed, getCurrentScope, onScopeDispose } from "vue"
import { useIntervalFn, useTimeoutFn } from "@vueuse/core"
import { getSubmission } from "oj/api"
import { SubmissionStatus } from "utils/constants"
@@ -153,9 +153,9 @@ export function useSubmissionMonitor() {
})
// ==================== 清理 ====================
- onUnmounted(() => {
- pausePolling()
- })
+ // 现在由 submission store 持有(见 oj/store/submission.ts),store 不销毁就一直在;
+ // 用 onScopeDispose 是为了在组件里调用时照样能收尾
+ if (getCurrentScope()) onScopeDispose(pausePolling)
return {
// 状态
diff --git a/apps/web/src/oj/problem/detail.vue b/apps/web/src/oj/problem/detail.vue
index b50649b..6d29a47 100644
--- a/apps/web/src/oj/problem/detail.vue
+++ b/apps/web/src/oj/problem/detail.vue
@@ -7,6 +7,9 @@ import { useProblemStore } from "oj/store/problem"
import { useScreenModeStore } from "shared/store/screenMode"
import { useMyFlowchartStore } from "shared/store/myFlowchart"
import { useUserStore } from "shared/store/user"
+import { useSubmissionStore } from "oj/store/submission"
+// 判完之后该发生的事(烟花、点评、回题单)。静态引入:它得在第一次判完之前就挂好
+import SubmissionEffects from "./components/SubmissionEffects.vue"
// 抽成具名 loader,便于进页面时与接口并行预取编辑器 chunk。
// 题库、比赛、题单三种入口用的是同一个编辑器:原来比赛和题单用的是另一个精简版,
@@ -131,6 +134,9 @@ watch(
},
)
onBeforeUnmount(() => {
+ // 提交状态在 store 里、离开页面也不会自己没:不清的话回到题目页(哪怕换了一道题),
+ // 结果面板里还是上一次的那条
+ useSubmissionStore().reset()
problem.value = null
errMsg.value = "无数据"
screenModeStore.resetScreenMode()
@@ -154,6 +160,7 @@ watch(
+
{
+ const problemStore = useProblemStore()
+ const codeStore = useCodeStore()
+ const collabStore = useCollabStore()
+
+ const monitor = useSubmissionMonitor()
+ const hint = useSubmissionHint()
+
+ /** 结果面板开着没有。放 store 里是为了按钮组件卸了再挂回来,面板还是原来的开合 */
+ const showResult = ref(false)
+ const isFormatting = ref(false)
+ const isSubmittingRequest = ref(false)
+
+ /**
+ * 提交前语法检查查出来的错误(CPython 的报错原文),有它时结果面板显示中文说明、
+ * 不显示上一次的判题结果。这次没交上去,所以不是一条提交、也不数进失败次数。
+ *
+ * 检查在服务端做(`/code/format` 里先用 CPython 编译一遍),原来是浏览器里的 Skulpt:
+ * 那个只能报「第 N 行有错」,而且要下载 ~226KB。
+ */
+ const syntaxErrorInfo = ref("")
+
+ const { start: startCooldown, isPending: isCooldown } = useTimeout(5000, {
+ controls: true,
+ immediate: false,
+ })
+
+ /**
+ * 交一次。`contestId` / `problemSetId` 是路由参数(空串 = 不是从那个入口进来的),
+ * 由调用方给:store 不看路由。
+ */
+ async function submit(entry: { contestId: string; problemSetId: string }) {
+ const problem = problemStore.problem
+ if (!problem) return
+ syntaxErrorInfo.value = ""
+
+ // 0. 提交前自动格式化(Python 用 ruff,C/C++ 用 clang-format,SQL 用 sqlparse)。
+ // Python 在格式化之前先由服务端的 CPython 查一遍语法,有错就不提交
+ const formatLang = LANGUAGE_FORMAT_VALUE[codeStore.code.language]
+ if (["python", "c", "cpp", "sql"].includes(formatLang)) {
+ isFormatting.value = true
+ try {
+ const res = await formatCode({
+ code: codeStore.code.value,
+ language: formatLang,
+ })
+ codeStore.setCode(res.code)
+ } catch (e) {
+ if (errorCode(e) === "syntax-error") {
+ // 仅 Python 会出现:message 是 CPython 的报错原文,交给 PythonErrorExplain 翻译
+ syntaxErrorInfo.value = errorMessage(e)
+ showResult.value = true
+ return
+ }
+ // server-error / 网络异常:格式化工具问题,静默降级,提交原代码
+ } finally {
+ isFormatting.value = false
+ }
+ }
+
+ // 1. 构建提交数据
+ const data: SubmitCodePayload = {
+ problemId: problem.id,
+ language: codeStore.code.language,
+ code: codeStore.code.value,
+ // 编辑过程信号,见 utils/editTrace.ts。协作的判断和 ProblemEditor 的 collabHere 同一个口径
+ trace: snapshotEditTrace(
+ collabStore.room !== null && collabStore.room.problemId === problem._id,
+ ),
+ }
+ if (entry.contestId) {
+ data.contestId = parseInt(entry.contestId)
+ }
+ // 从题单入口进来的,把来源题单一起报上去:提交列表要据此标出「来自题单」。
+ // 只是来源标记,题单进度仍由后端判完之后自己记账(judge/run.ts)
+ if (entry.problemSetId) {
+ data.problemSetId = parseInt(entry.problemSetId)
+ }
+ // 2. 提交代码到后端
+ isSubmittingRequest.value = true
+ try {
+ const res = await submitCode(data)
+ console.log(`[Submit] 代码已提交: ID=${res.submissionId}`)
+ // 交上了才清零;被限流 / 网络失败的话这一段接着记,下次提交一起报
+ restartEditTrace(codeStore.code.value.length)
+
+ // 3. 启动冷却 + 监控
+ startCooldown()
+ monitor.startMonitoring(res.submissionId)
+ showResult.value = true
+ } finally {
+ isSubmittingRequest.value = false
+ }
+ }
+
+ /**
+ * 收掉这一道题的所有东西:结果面板、语法错误、还在跟的那条提交、AI 提示。
+ * 提交本身照常判完落库,只是这边不再跟 —— 不然上一道还在判的那条判完,
+ * 会把**新题**标成已解决。
+ */
+ function reset() {
+ showResult.value = false
+ syntaxErrorInfo.value = ""
+ monitor.reset()
+ hint.clearHint()
+ }
+
+ // 题目页换题是同一个组件复用(顶栏题号直达、「下一题」都只换路由参数)
+ watch(
+ () => problemStore.problem?._id,
+ (next, previous) => {
+ if (!previous || next === previous) return
+ reset()
+ },
+ )
+
+ // 换了一次提交就清掉上一次的 AI 提示,否则新结果底下挂着上一次的提示,
+ // 而「让 AI 分析」按钮已经被藏了,学生没法重新分析
+ watch(() => monitor.submission.value?.id, hint.clearHint)
+
+ return {
+ submission: monitor.submission,
+ judging: monitor.judging,
+ pending: monitor.pending,
+ submitting: monitor.submitting,
+ showResult,
+ isFormatting,
+ isSubmittingRequest,
+ syntaxErrorInfo,
+ isCooldown,
+ submit,
+ reset,
+ // markRaw:不让 store 把它包成 reactive,组件解构出来的还是 ref
+ hint: markRaw(hint),
+ }
+})
diff --git a/apps/web/src/shared/composables/websocket.ts b/apps/web/src/shared/composables/websocket.ts
index cb684b4..8595fd9 100644
--- a/apps/web/src/shared/composables/websocket.ts
+++ b/apps/web/src/shared/composables/websocket.ts
@@ -1,5 +1,5 @@
import { createDiscreteApi } from "naive-ui"
-import { ref, onUnmounted, type Ref } from "vue"
+import { ref, getCurrentScope, onScopeDispose, type Ref } from "vue"
import { useAuthModalStore } from "shared/store/authModal"
import { useUserStore } from "shared/store/user"
@@ -467,7 +467,9 @@ export interface SubmissionUpdate extends WebSocketMessage {
* 带二十五行示例注释的 `createWebSocketComposable` 工厂,示例里那条通知通道并不
* 存在,三个真实的 composable 一个都没用它。现在只剩这一个。
*
- * 每次调用都新建一条连接(和原来一致,不是单例),并在组件卸载时摘掉 handler、断开。
+ * 每次调用都新建一条连接(和原来一致,不是单例),并在作用域销毁时摘掉 handler、断开。
+ * 用的是 onScopeDispose 而不是 onUnmounted:组件卸载时它照样触发,而在 Pinia store
+ * 里调用时 onUnmounted 根本挂不上(没有组件实例,只报一条警告),连接就永远断不掉。
*/
function useChannel(path: string, handler?: MessageHandler) {
const ws = new BaseWebSocket(path)
@@ -476,10 +478,12 @@ function useChannel(path: string, handler?: MessageH
// 中间那段窗口收到的广播没有任何 handler 接。窗口极小,但没有理由留着它。
if (handler) ws.addHandler(handler)
- onUnmounted(() => {
- if (handler) ws.removeHandler(handler)
- ws.disconnect()
- })
+ if (getCurrentScope()) {
+ onScopeDispose(() => {
+ if (handler) ws.removeHandler(handler)
+ ws.disconnect()
+ })
+ }
return {
connect: () => ws.connect(),