feat: 修订了调试功能

这个提交包含在:
2026-05-25 17:14:14 +08:00
父节点 8790fcb5a9
当前提交 61b20ca200
共有 17 个文件被更改,包括 838 次插入 和 61 次删除

查看文件

@ -56,6 +56,18 @@ const debugMenuItems = ref<DebugMenuItem[]>([])
const debugMenuLoading = ref(false)
const debugMenuError = ref('')
const debugActionLoading = ref<string | null>(null)
const debugFieldId = ref('')
const queryAnswerLoading = ref(false)
const undoLoading = ref(false)
type GuardedFieldInfo = {
field_id: string
source: string | null
tolerance: number | null
placeholder: string | null
unit: string | null
}
const guardedFields = ref<GuardedFieldInfo[]>([])
const currentExperiment = computed((): string => {
const hash = window.location.hash
@ -125,6 +137,20 @@ const loadDebugMenu = async () => {
}
}
const loadGuardedFields = async () => {
try {
const url = `${backendUrl.value}/debug/guarded-fields?experiment=${encodeURIComponent(currentExperiment.value)}`
const resp = await fetch(url)
if (resp.ok) {
const data = await resp.json()
guardedFields.value = (data?.fields || []) as GuardedFieldInfo[]
if (guardedFields.value.length > 0 && !debugFieldId.value) {
debugFieldId.value = guardedFields.value[0]!.field_id
}
}
} catch { /* ignore */ }
}
const executeDebugAction = async (item: DebugMenuItem) => {
debugActionLoading.value = item.action
const resultId = ++resultIdCounter
@ -163,6 +189,58 @@ const executeDebugAction = async (item: DebugMenuItem) => {
debugActionLoading.value = null
}
const queryAnswer = async () => {
const fid = debugFieldId.value.trim()
if (!fid) return
queryAnswerLoading.value = true
const resultId = ++resultIdCounter
const entry: DebugResult = {
id: resultId, timestamp: Date.now(),
label: `答案查询: ${fid}`,
endpoint: `GET ${backendUrl.value}/debug/answer?field_id=${fid}`,
method: 'GET', success: false, body: null, error: '',
}
try {
const url = `${backendUrl.value}/debug/answer?field_id=${encodeURIComponent(fid)}&experiment=${encodeURIComponent(currentExperiment.value)}`
const resp = await fetch(url)
const body = (await resp.json().catch(() => resp.text())) as unknown
entry.success = resp.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
queryAnswerLoading.value = false
}
const undoLastReading = async () => {
const fid = debugFieldId.value.trim()
if (!fid) return
undoLoading.value = true
const resultId = ++resultIdCounter
const entry: DebugResult = {
id: resultId, timestamp: Date.now(),
label: `回退: ${fid}`,
endpoint: `POST ${backendUrl.value}/value/undo?field_id=${fid}`,
method: 'POST', success: false, body: null, error: '',
}
try {
const url = `${backendUrl.value}/value/undo?field_id=${encodeURIComponent(fid)}&experiment=${encodeURIComponent(currentExperiment.value)}`
const resp = await fetch(url, { method: 'POST' })
const body = (await resp.json().catch(() => resp.text())) as unknown
entry.success = resp.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
undoLoading.value = false
}
const togglePanel = () => {
if (!isVisible.value) {
loadExperiences()
@ -211,6 +289,7 @@ onMounted(() => {
if (debugEnabled.value) {
loadExperiences()
loadDebugMenu()
loadGuardedFields()
}
})
@ -336,6 +415,39 @@ const navToExperience = (id: string) => {
</button>
</div>
<div class="debug-field" style="margin-top:0.5rem">
<label>fieldId 选择器</label>
<div class="debug-input-row">
<select
v-model="debugFieldId"
class="debug-input debug-select"
>
<option v-if="guardedFields.length === 0" value="">加载中...</option>
<option
v-for="gf in guardedFields"
:key="gf.field_id"
:value="gf.field_id"
>
{{ gf.field_id }} ({{ gf.source || 'manual' }} ±{{ gf.tolerance }})
</option>
</select>
<button
class="debug-btn debug-btn-sm"
:disabled="queryAnswerLoading || !debugFieldId"
@click="queryAnswer"
>
{{ queryAnswerLoading ? '...' : '查答案' }}
</button>
<button
class="debug-btn debug-btn-sm debug-btn-danger"
:disabled="undoLoading || !debugFieldId"
@click="undoLastReading"
>
{{ undoLoading ? '...' : '回退' }}
</button>
</div>
</div>
<div v-if="debugResults.length > 0" class="debug-results">
<div class="debug-logs-header">
<span>操作结果({{ debugResults.length }})</span>
@ -531,6 +643,11 @@ const navToExperience = (id: string) => {
border-color: #007bff;
}
.debug-select {
flex: 1;
cursor: pointer;
}
.debug-btn {
background: #007bff;
color: white;
@ -550,6 +667,14 @@ const navToExperience = (id: string) => {
cursor: not-allowed;
}
.debug-btn-danger {
background: #dc3545;
}
.debug-btn-danger:hover {
background: #c82333;
}
.debug-btn-sm {
padding: 0.35rem 0.6rem;
font-size: 0.8rem;

查看文件

@ -31,6 +31,12 @@ const fieldData = computed(() => experimentStore.data)
const areaResults = computed(() => experimentStore.areaResults)
const allFieldsValidated = computed(() => experimentStore.allFieldsValidated)
const dataVersion = computed(() => experimentStore.dataVersion)
const pendingField = computed(() => experimentStore.pendingField)
const controlStatus = computed(() => experimentStore.controlStatus)
const backendBaseUrl = computed(() => {
if (!props.validationUrl) return ''
return props.validationUrl.replace(/\/value\/?$/, '')
})
const configSummary = computed<ConfigSummary>(() => {
const fields: ConfigSummary['fields'] = []
@ -185,6 +191,9 @@ watch(allFieldsValidated, (newValue) => {
:area-results="areaResults"
:on-field-update="handleFieldUpdate"
:on-field-validate="handleFieldValidate"
:pending-field="pendingField"
:backend-base-url="backendBaseUrl"
:control-status="controlStatus"
:cell-inspect-mode="cellInspectMode"
:on-cell-inspect="(info: any) => emit('cell-inspect', info)"
/>

查看文件

@ -1,5 +1,5 @@
<script setup lang="ts">
import { computed, inject } from 'vue'
import { computed, inject, ref } from 'vue'
import type { GridSheet, GridCell, GridInputCell } from '@/types/experiment'
import ExperimentGridCell from './ExperimentGridCell.vue'
import { AIEvalContextKey } from '@/utils/aiEvalContext'
@ -7,10 +7,13 @@ import { renderTextWithLatex } from '@/utils/latex'
const props = defineProps<{
sheet: GridSheet
fieldData: Record<string, { value: any; validated: boolean; validationResult?: { accept: boolean; message: string } }>
fieldData: Record<string, { value: any; validated: boolean; validationResult?: { accept: boolean; message: string; severity?: string } }>
areaResults: Record<string, { value: any; formatted: string; error?: string }>
onFieldUpdate: (fieldId: string, value: any) => void
onFieldValidate: (fieldId: string) => void
pendingField?: string | null
backendBaseUrl?: string
controlStatus?: string | null
cellInspectMode?: boolean
onCellInspect?: (info: { sheetTitle?: string; cellType: string; label: string; value: any; unit?: string; validation?: any }) => void
}>()
@ -40,6 +43,31 @@ function getCellValidationResult(cell: GridCell) {
return props.fieldData[cell.fieldId]?.validationResult
}
function isCellPending(cell: GridCell): boolean {
if (!props.pendingField || !cell || cell.type !== 'input') return false
return (cell as GridInputCell).fieldId === props.pendingField
}
function isCellLocked(cell: GridCell): boolean {
if (!cell || cell.type !== 'input') return false
const inp = cell as GridInputCell
if (!inp.readingGuard?.enabled) return false
if (!props.pendingField) return inp.fieldId !== findFirstGuardedField()
return inp.fieldId !== props.pendingField
}
function findFirstGuardedField(): string {
if (!props.sheet.cells) return ''
for (const row of props.sheet.cells) {
for (const cell of row) {
if (cell && cell.type === 'input' && (cell as GridInputCell).readingGuard?.enabled) {
return (cell as GridInputCell).fieldId
}
}
}
return ''
}
function getCellAreaResult(cell: GridCell) {
if (!cell || cell.type !== 'computed') return undefined
return props.areaResults[cell.areaId]
@ -50,6 +78,8 @@ function onCellInput(fieldId: string, value: any) {
}
function onCellBlur(fieldId: string) {
const val = props.fieldData[fieldId]?.value
if (val === null || val === undefined || val === '') return
props.onFieldValidate(fieldId)
}
@ -87,6 +117,25 @@ function handleCellInspect(cell: GridCell) {
const isFirstRowHeader = computed(() => {
return props.sheet.columnHeaders && props.sheet.columnHeaders.length > 0
})
const controlBar = computed(() => props.sheet.controlBar?.items || [])
const displayStatus = computed(() => {
if (!props.controlStatus) return ''
if (localStorage.getItem('DEBUG') === 'true') return props.controlStatus
const s = props.controlStatus
if (s.startsWith('空闲')) return '就绪'
if (s.startsWith('上升中')) return '监控中'
if (s.startsWith('峰值已检测')) return '峰值就绪,请读数'
if (s.startsWith('冷却中')) return '监控中'
return props.controlStatus
})
function handleControlAction(item: { id: string; method?: string; endpoint?: string }) {
if (!item.endpoint || !props.backendBaseUrl) return
const url = props.backendBaseUrl + item.endpoint
fetch(url, { method: item.method || 'GET' }).catch(() => {})
}
</script>
<template>
@ -107,6 +156,24 @@ const isFirstRowHeader = computed(() => {
</th>
</tr>
</thead>
<thead v-if="controlBar.length > 0" class="grid-control-bar">
<tr>
<td :colspan="sheet.columns" class="control-bar-cell">
<div class="control-bar-row">
<template v-for="item in controlBar" :key="item.id">
<button
v-if="item.type === 'action'"
class="control-btn"
@click="handleControlAction(item)"
>
{{ item.label }}
</button>
</template>
<span v-if="controlStatus" class="control-status">{{ displayStatus }}</span>
</div>
</td>
</tr>
</thead>
<tbody>
<tr v-for="(row, ri) in effectiveRows.rows" :key="ri" class="grid-row">
<td
@ -126,6 +193,8 @@ const isFirstRowHeader = computed(() => {
:validated="getCellValidated(cell)"
:validation-result="getCellValidationResult(cell)"
:area-result="getCellAreaResult(cell)"
:is-pending="isCellPending(cell)"
:is-locked="isCellLocked(cell)"
:inspect-mode="cellInspectMode"
@input="(val: any) => onCellInput((cell as GridInputCell).fieldId, val)"
@blur="() => onCellBlur((cell as GridInputCell).fieldId)"
@ -196,4 +265,42 @@ const isFirstRowHeader = computed(() => {
.grid-cell-wrapper:first-child {
min-width: 70px;
}
.grid-control-bar {
border: none;
}
.control-bar-cell {
padding: 6px 10px;
background: #f0f4f8;
border-bottom: 2px solid #d0d4da;
}
.control-bar-row {
display: flex;
align-items: center;
gap: 8px;
flex-wrap: wrap;
}
.control-status {
font-size: 0.85rem;
color: #333;
font-weight: 600;
}
.control-btn {
background: #007bff;
color: white;
border: none;
border-radius: 6px;
padding: 0.35rem 0.6rem;
font-size: 0.8rem;
font-weight: 600;
cursor: pointer;
}
.control-btn:hover {
background: #0056b3;
}
</style>

查看文件

@ -9,9 +9,11 @@ const props = defineProps<{
cell: GridCell
value?: any
validated?: boolean
validationResult?: { accept: boolean; message: string }
validationResult?: { accept: boolean; message: string; severity?: string }
areaResult?: { value: any; formatted: string; error?: string }
inspectMode?: boolean
isPending?: boolean
isLocked?: boolean
}>()
const emit = defineEmits<{
@ -53,14 +55,15 @@ const bgColor = computed(() => {
if (isFocused.value) return '#e8f0fe'
if (props.validated && props.validationResult?.accept) {
return '#d4edda'
}
if (props.validated && props.validationResult && !props.validationResult.accept) {
if (props.validationResult && !props.validationResult.accept) {
return '#f8d7da'
}
if (props.value !== null && props.value !== undefined && props.value !== '') {
return '#fff3cd'
if (props.validated && props.validationResult?.accept) {
if (props.validationResult.severity === 'warning') return '#fff3cd'
return '#d4edda'
}
if (hasValue.value) {
return '#e8f0fe'
}
return '#ffffff'
})
@ -174,6 +177,8 @@ onBeforeUnmount(() => {
'cell-focused': isFocused,
'cell-inspect-mode': inspectMode,
'cell-inspect-hover': inspectMode && isHovering,
'cell-pending': isInput && isPending && !isFocused,
'cell-locked': isInput && isLocked,
}"
:style="{
backgroundColor: isLabel ? labelBgColor : isInput ? bgColor : isComputed ? (areaResult?.error ? '#f8d7da' : '#f0fff4') : 'transparent',
@ -191,6 +196,7 @@ onBeforeUnmount(() => {
class="cell-input-field"
:value="internalValue"
:placeholder="inputCell?.placeholder || ''"
:disabled="isLocked"
@input="handleInput"
@blur="handleBlur"
@focus="handleFocus"
@ -254,6 +260,23 @@ onBeforeUnmount(() => {
outline-offset: -1px;
}
.cell-pending {
outline: 3px solid #ff6b35;
outline-offset: -2px;
animation: pending-pulse 1.5s ease-in-out infinite;
box-shadow: 0 0 6px rgba(255, 107, 53, 0.35);
}
.cell-locked {
cursor: not-allowed;
pointer-events: none;
}
@keyframes pending-pulse {
0%, 100% { box-shadow: 0 0 4px rgba(255, 107, 53, 0.3); }
50% { box-shadow: 0 0 12px rgba(255, 107, 53, 0.6); }
}
.cell-inspect-mode {
cursor: pointer;
}

查看文件

@ -21,6 +21,8 @@ export const useExperimentStore = defineStore('experiment', () => {
const currentField: Ref<string | null> = ref(null)
const validationUrl: Ref<string | null> = ref(null)
const dataVersion = ref(0)
const pendingField: Ref<string | null> = ref(null)
const controlStatus: Ref<string | null> = ref(null)
const allFieldsValidated = computed(() => {
return Object.values(data.value).every((field) => field.validated)
@ -209,7 +211,14 @@ export const useExperimentStore = defineStore('experiment', () => {
const result: FieldValidationResponse = await response.json()
if (data.value[fieldId]) {
data.value[fieldId].validationResult = result
data.value[fieldId].validated = result.accept
data.value[fieldId].validated = true
data.value[fieldId].severity = result.severity
}
if (result.pending_field !== undefined) {
pendingField.value = result.pending_field
}
if (result.control_status !== undefined) {
controlStatus.value = result.control_status
}
dataVersion.value += 1
@ -227,7 +236,7 @@ export const useExperimentStore = defineStore('experiment', () => {
}
if (data.value[fieldId]) {
data.value[fieldId].validationResult = response
data.value[fieldId].validated = false
data.value[fieldId].validated = true
}
dataVersion.value += 1
return response
@ -331,6 +340,16 @@ export const useExperimentStore = defineStore('experiment', () => {
if (!response.ok) throw new Error(`加载配置失败: ${response.status}`)
const configData: ExperimentGridConfig = await response.json()
setConfig(configData, validationEndpoint)
if (validationEndpoint) {
try {
const resp = await fetch(validationEndpoint)
if (resp.ok) {
const vdata = await resp.json()
pendingField.value = vdata.pending_field || null
controlStatus.value = vdata.control_status || null
}
} catch { /* ignore */ }
}
} catch (err) {
error.value = err instanceof Error ? err.message : '加载配置失败'
console.error('加载实验配置失败:', err)
@ -347,6 +366,8 @@ export const useExperimentStore = defineStore('experiment', () => {
error,
currentField,
dataVersion,
pendingField,
controlStatus,
allFieldsValidated,
fieldValues,
allInputFields,

查看文件

@ -1,7 +1,8 @@
export interface ReadingGuardConfig {
enabled?: boolean
source?: 'device_peak' | 'device_frequency' | 'manual'
source?: 'device_peak' | 'device_frequency' | 'manual' | 'peak_ocr'
tolerance?: number
tolerance2?: number
}
export interface ValidationConfig {
@ -44,6 +45,18 @@ export interface GridComputedCell {
export type GridCell = GridLabelCell | GridInputCell | GridComputedCell | null
export interface ControlBarItem {
type: 'action'
id: string
label: string
method?: string
endpoint?: string
}
export interface ControlBar {
items: ControlBarItem[]
}
export interface GridSheet {
id: string
title?: string
@ -51,6 +64,7 @@ export interface GridSheet {
columnHeaders?: string[]
columnWidths?: string[]
rowHeaders?: string[]
controlBar?: ControlBar
cells: GridCell[][]
}
@ -63,12 +77,17 @@ export interface ExperimentGridConfig {
export interface FieldValidationResponse {
accept: boolean
message: string
pending_field?: string
severity?: string
reference?: number
control_status?: string
}
export interface ExperimentFieldData {
value: any
validated: boolean
validationResult?: FieldValidationResponse
severity?: string
lastUpdated?: Date
}