feat: 加入了调试器
docs: 完善了文档
这个提交包含在:
@ -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>
|
||||
|
||||
@ -1,23 +1,185 @@
|
||||
<script lang="ts">
|
||||
<script lang="ts" setup>
|
||||
import { ref, onMounted } from 'vue'
|
||||
import { ExperienceLoader } from '@/helper'
|
||||
import { defineComponent, ref, type Ref } from 'vue'
|
||||
import { type ExperienceMap } from '@/types/experience'
|
||||
import router from '@/router'
|
||||
import type { ExperienceType } from '@/types/experience'
|
||||
|
||||
export default defineComponent({
|
||||
setup() {
|
||||
const experiences: Ref<ExperienceMap> = ref({})
|
||||
ExperienceLoader().then((v) => {
|
||||
experiences.value = v.experiences
|
||||
})
|
||||
return {
|
||||
experiences,
|
||||
}
|
||||
},
|
||||
const experiences = ref<Record<string, ExperienceType>>({})
|
||||
|
||||
onMounted(async () => {
|
||||
const data = await ExperienceLoader()
|
||||
experiences.value = data.experiences
|
||||
})
|
||||
|
||||
const selectExperiment = (exp: ExperienceType) => {
|
||||
router.push({ path: '/exp', query: { expid: exp.id } })
|
||||
}
|
||||
|
||||
const expList = () => {
|
||||
return Object.entries(experiences.value)
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<span v-for="(exp, name) in experiences">
|
||||
{{ name }}
|
||||
<img :src="exp.image" />
|
||||
</span>
|
||||
<div class="select-page">
|
||||
<div class="select-container">
|
||||
<h1 class="page-title">AI+ 物理实验平台</h1>
|
||||
<p class="page-subtitle">请选择实验项目</p>
|
||||
<div class="experiment-grid">
|
||||
<div
|
||||
class="experiment-card"
|
||||
v-for="[name, exp] in expList()"
|
||||
:key="exp.id"
|
||||
@click="selectExperiment(exp)"
|
||||
>
|
||||
<div class="card-image" v-if="exp.image">
|
||||
<img :src="exp.image" :alt="exp.title" />
|
||||
</div>
|
||||
<div class="card-image placeholder" v-else>
|
||||
<span class="placeholder-icon">🧪</span>
|
||||
</div>
|
||||
<div class="card-content">
|
||||
<h2 class="card-title">{{ exp.title }}</h2>
|
||||
<p class="card-author" v-if="exp.author">{{ exp.author }}</p>
|
||||
<div class="card-stages">
|
||||
<span
|
||||
class="stage-tag"
|
||||
v-for="stage in exp.stage"
|
||||
:key="stage.name"
|
||||
>
|
||||
{{ stage.name }}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="footer">
|
||||
<a @click="router.push('/login')" class="logout-link">退出登录</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.select-page {
|
||||
min-height: 100vh;
|
||||
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
|
||||
font-family: 'Helvetica Neue', Arial, sans-serif;
|
||||
padding: 40px 20px;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.select-container {
|
||||
max-width: 1000px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
.page-title {
|
||||
font-size: 2.2rem;
|
||||
font-weight: bold;
|
||||
color: #333;
|
||||
text-align: center;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.page-subtitle {
|
||||
font-size: 1.1rem;
|
||||
color: #666;
|
||||
text-align: center;
|
||||
margin-bottom: 40px;
|
||||
}
|
||||
|
||||
.experiment-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
|
||||
gap: 24px;
|
||||
}
|
||||
|
||||
.experiment-card {
|
||||
background: white;
|
||||
border-radius: 12px;
|
||||
overflow: hidden;
|
||||
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
|
||||
cursor: pointer;
|
||||
transition: transform 0.25s ease, box-shadow 0.25s ease;
|
||||
}
|
||||
|
||||
.experiment-card:hover {
|
||||
transform: translateY(-6px);
|
||||
box-shadow: 0 12px 28px rgba(0, 0, 0, 0.15);
|
||||
}
|
||||
|
||||
.card-image {
|
||||
width: 100%;
|
||||
height: 180px;
|
||||
overflow: hidden;
|
||||
background: #e8ecf1;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.card-image img {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: cover;
|
||||
}
|
||||
|
||||
.card-image.placeholder {
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
}
|
||||
|
||||
.placeholder-icon {
|
||||
font-size: 3rem;
|
||||
}
|
||||
|
||||
.card-content {
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.card-title {
|
||||
font-size: 1.2rem;
|
||||
font-weight: 600;
|
||||
color: #222;
|
||||
margin: 0 0 8px 0;
|
||||
}
|
||||
|
||||
.card-author {
|
||||
font-size: 0.85rem;
|
||||
color: #888;
|
||||
margin: 0 0 12px 0;
|
||||
}
|
||||
|
||||
.card-stages {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 6px;
|
||||
}
|
||||
|
||||
.stage-tag {
|
||||
display: inline-block;
|
||||
padding: 3px 10px;
|
||||
background: #e8f0fe;
|
||||
color: #1a73e8;
|
||||
border-radius: 12px;
|
||||
font-size: 0.75rem;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.footer {
|
||||
text-align: center;
|
||||
margin-top: 40px;
|
||||
}
|
||||
|
||||
.logout-link {
|
||||
color: #888;
|
||||
font-size: 0.9rem;
|
||||
cursor: pointer;
|
||||
text-decoration: none;
|
||||
transition: color 0.2s;
|
||||
}
|
||||
|
||||
.logout-link:hover {
|
||||
color: #e74c3c;
|
||||
}
|
||||
</style>
|
||||
|
||||
@ -10,7 +10,7 @@ const DoLogin = async () => {
|
||||
const [result, status] = await UserInfo.doLogin(InputSID.value, InputName.value)
|
||||
if (result) {
|
||||
router.push({
|
||||
path: '/exp',
|
||||
path: '/select',
|
||||
})
|
||||
} else {
|
||||
TipsRef.value = status ? (status as string) : '登录失败'
|
||||
|
||||
在新工单中引用
屏蔽一个用户