feat: 加入了调试器

docs: 完善了文档
这个提交包含在:
2026-05-23 21:40:58 +08:00
父节点 41813abe48
当前提交 8790fcb5a9
共有 32 个文件被更改,包括 4024 次插入 和 360 次删除

查看文件

@ -1,11 +1,34 @@
<script setup lang="ts">
import { ref, onMounted, watch } from 'vue'
import { ref, onMounted, watch, computed, nextTick } from 'vue'
import { useDraggable } from '@/composables/useDraggable'
import { aiLogs, clearAiLogs, getBaseUrl, setBaseUrl, type AiLogEntry } from '@/utils/aiClient'
type DebugMenuItem = {
label: string
action: string
method: string
endpoint: string
description: string
}
type DebugResult = {
id: number
timestamp: number
label: string
endpoint: string
method: string
success: boolean
body: unknown
error: string
}
let resultIdCounter = 0
const isVisible = ref(false)
const panelRef = ref<HTMLElement | null>(null)
const debugEnabled = ref(false)
const debugResults = ref<DebugResult[]>([])
const expandedResult = ref<number | null>(null)
const { style: panelStyle, onPointerDown } = useDraggable(panelRef, {
initialX: 100,
@ -27,15 +50,39 @@ const skipStageCheck = ref(false)
const aiProviderUrl = ref(getBaseUrl() || '')
const aiLogExpanded = ref<number | null>(null)
const LS_BACKEND_KEY = 'PhyExp.DebugBackendUrl'
const backendUrl = ref(localStorage.getItem(LS_BACKEND_KEY) || 'http://localhost:8000')
const debugMenuItems = ref<DebugMenuItem[]>([])
const debugMenuLoading = ref(false)
const debugMenuError = ref('')
const debugActionLoading = ref<string | null>(null)
const currentExperiment = computed((): string => {
const hash = window.location.hash
const m = hash.match(/expid=([^&]+)/)
if (m && m[1]) {
return m[1]
}
return 'cn.edu.muc.lxy.phyexp.voicespeed'
})
watch(skipStageCheck, (val) => {
;(window as any).__DEBUG_SKIP_STAGE__ = val
})
watch(backendUrl, (val) => {
localStorage.setItem(LS_BACKEND_KEY, val.trim())
})
const saveAiProvider = () => {
const trimmed = aiProviderUrl.value.trim()
setBaseUrl(trimmed || null)
}
const saveBackendUrl = () => {
localStorage.setItem(LS_BACKEND_KEY, backendUrl.value.trim())
}
const loadExperiences = async () => {
try {
const response = await fetch('/src/assets/experiences.json')
@ -58,6 +105,64 @@ const loadExperiences = async () => {
}
}
const loadDebugMenu = async () => {
debugMenuLoading.value = true
debugMenuError.value = ''
try {
const url = `${backendUrl.value}/debug/menu?experiment=${encodeURIComponent(currentExperiment.value)}`
const response = await fetch(url)
if (!response.ok) {
const text = await response.text()
throw new Error(`${response.status} ${response.statusText}: ${text}`)
}
const data = await response.json()
debugMenuItems.value = (data?.items || []) as DebugMenuItem[]
} catch (e: any) {
debugMenuError.value = e.message || String(e)
debugMenuItems.value = []
} finally {
debugMenuLoading.value = false
}
}
const executeDebugAction = async (item: DebugMenuItem) => {
debugActionLoading.value = item.action
const resultId = ++resultIdCounter
const entry: DebugResult = {
id: resultId,
timestamp: Date.now(),
label: item.label,
endpoint: `${item.method} ${backendUrl.value}${item.endpoint}`,
method: item.method,
success: false,
body: null,
error: '',
}
try {
const fullUrl = `${backendUrl.value}${item.endpoint}`
const opts: RequestInit = {
method: item.method,
headers: { 'Content-Type': 'application/json' },
}
if (item.method === 'POST') {
opts.body = JSON.stringify({ experiment: currentExperiment.value })
}
const response = await fetch(fullUrl, opts)
const contentType = response.headers.get('content-type') || ''
const body = contentType.includes('application/json') ? await response.json() : await response.text()
entry.success = response.ok
entry.body = body
} catch (e: any) {
entry.error = e.message || String(e)
}
debugResults.value.unshift(entry)
if (debugResults.value.length > 50) {
debugResults.value.length = 50
}
expandedResult.value = resultId
debugActionLoading.value = null
}
const togglePanel = () => {
if (!isVisible.value) {
loadExperiences()
@ -70,6 +175,15 @@ const toggleLogExpand = (id: number) => {
aiLogExpanded.value = aiLogExpanded.value === id ? null : id
}
const toggleResultExpand = (id: number) => {
expandedResult.value = expandedResult.value === id ? null : id
}
const clearDebugResults = () => {
debugResults.value.length = 0
expandedResult.value = null
}
const formatLogPreview = (entry: AiLogEntry): string => {
if (entry.type === 'error') return entry.error || 'Unknown error'
if (entry.type === 'request') return (entry.payload || '').slice(0, 100)
@ -81,11 +195,22 @@ const formatTime = (ts: number): string => {
return `${d.getHours().toString().padStart(2, '0')}:${d.getMinutes().toString().padStart(2, '0')}:${d.getSeconds().toString().padStart(2, '0')}`
}
const formatResultBody = (body: unknown): string => {
if (body === null || body === undefined) return 'null'
if (typeof body === 'string') return body
try {
return JSON.stringify(body, null, 2)
} catch {
return String(body)
}
}
onMounted(() => {
debugEnabled.value = localStorage.getItem('DEBUG') === 'true'
skipStageCheck.value = !!(window as any).__DEBUG_SKIP_STAGE__
if (debugEnabled.value) {
loadExperiences()
loadDebugMenu()
}
})
@ -174,6 +299,66 @@ const navToExperience = (id: string) => {
</div>
</div>
</div>
<div class="debug-section">
<h4>后端实验调试</h4>
<div class="debug-field">
<label>后端 URL</label>
<div class="debug-input-row">
<input
v-model="backendUrl"
placeholder="http://localhost:8000"
class="debug-input"
/>
<button class="debug-btn debug-btn-sm" @click="saveBackendUrl">保存</button>
</div>
</div>
<div class="debug-field">
<label>实验: {{ currentExperiment }}</label>
<button class="debug-btn debug-btn-sm" @click="loadDebugMenu" :disabled="debugMenuLoading">
{{ debugMenuLoading ? '加载中...' : '刷新菜单' }}
</button>
</div>
<div v-if="debugMenuError" class="debug-error">{{ debugMenuError }}</div>
<div v-if="debugMenuLoading && debugMenuItems.length === 0" class="debug-empty">加载调试菜单...</div>
<div v-if="!debugMenuLoading && debugMenuItems.length === 0 && !debugMenuError" class="debug-empty">该实验暂无调试选项</div>
<div v-for="item in debugMenuItems" :key="item.action" class="debug-menu-item">
<div class="debug-menu-info">
<span class="debug-menu-label">{{ item.label }}</span>
<span class="debug-menu-desc">{{ item.description }}</span>
</div>
<button
class="debug-btn debug-btn-sm"
:disabled="debugActionLoading === item.action"
@click="executeDebugAction(item)"
>
{{ debugActionLoading === item.action ? '...' : '执行' }}
</button>
</div>
<div v-if="debugResults.length > 0" class="debug-results">
<div class="debug-logs-header">
<span>操作结果({{ debugResults.length }})</span>
<button class="debug-btn debug-btn-sm" @click="clearDebugResults">清空</button>
</div>
<div
v-for="entry in debugResults"
:key="entry.id"
class="debug-log-entry"
:class="entry.success ? 'response' : 'error'"
>
<div class="debug-log-summary" @click="toggleResultExpand(entry.id)">
<span class="debug-log-badge">{{ entry.success ? '✅' : '❌' }}</span>
<span class="debug-log-time">{{ formatTime(entry.timestamp) }}</span>
<span class="debug-log-preview">{{ entry.label }}</span>
</div>
<div v-if="expandedResult === entry.id" class="debug-log-detail">
<div class="debug-log-meta">{{ entry.endpoint }}</div>
<pre class="debug-log-body">{{ entry.error || formatResultBody(entry.body) }}</pre>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
@ -204,8 +389,8 @@ const navToExperience = (id: string) => {
.debug-panel {
position: fixed;
width: 380px;
max-height: 550px;
width: 420px;
max-height: 600px;
background: white;
border-radius: 12px;
box-shadow: 0 8px 40px rgba(0, 0, 0, 0.2);
@ -281,6 +466,15 @@ const navToExperience = (id: string) => {
padding: 0.5rem 0;
}
.debug-error {
color: #dc3545;
font-size: 0.8rem;
padding: 0.3rem 0.5rem;
background: #f8d7da;
border-radius: 4px;
margin-bottom: 0.4rem;
}
.debug-item {
padding: 0.6rem 0.75rem;
border: 1px solid #e9ecef;
@ -351,6 +545,11 @@ const navToExperience = (id: string) => {
background: #0056b3;
}
.debug-btn:disabled {
background: #6c757d;
cursor: not-allowed;
}
.debug-btn-sm {
padding: 0.35rem 0.6rem;
font-size: 0.8rem;
@ -360,6 +559,10 @@ const navToExperience = (id: string) => {
margin-top: 0.75rem;
}
.debug-results {
margin-top: 0.75rem;
}
.debug-logs-header {
display: flex;
justify-content: space-between;
@ -370,6 +573,36 @@ const navToExperience = (id: string) => {
margin-bottom: 0.4rem;
}
.debug-menu-item {
display: flex;
justify-content: space-between;
align-items: center;
padding: 0.45rem 0.5rem;
border: 1px solid #e9ecef;
border-radius: 6px;
margin-bottom: 0.3rem;
gap: 0.5rem;
}
.debug-menu-info {
display: flex;
flex-direction: column;
min-width: 0;
flex: 1;
}
.debug-menu-label {
font-size: 0.85rem;
font-weight: 600;
color: #333;
}
.debug-menu-desc {
font-size: 0.72rem;
color: #888;
margin-top: 0.1rem;
}
.debug-log-entry {
border: 1px solid #eee;
border-radius: 6px;
@ -443,10 +676,10 @@ const navToExperience = (id: string) => {
.debug-log-body {
margin: 0;
font-size: 0.75rem;
font-size: 0.72rem;
white-space: pre-wrap;
word-break: break-all;
max-height: 160px;
max-height: 200px;
overflow-y: auto;
background: #fff;
border: 1px solid #eee;

查看文件

@ -3,6 +3,7 @@ import { computed, inject } from 'vue'
import type { GridSheet, GridCell, GridInputCell } from '@/types/experiment'
import ExperimentGridCell from './ExperimentGridCell.vue'
import { AIEvalContextKey } from '@/utils/aiEvalContext'
import { renderTextWithLatex } from '@/utils/latex'
const props = defineProps<{
sheet: GridSheet
@ -102,7 +103,7 @@ const isFirstRowHeader = computed(() => {
:key="hi"
class="grid-header-cell"
>
<span v-html="header" />
<span v-html="renderTextWithLatex(header)" />
</th>
</tr>
</thead>