Complete phase two benchmarks, plot previews and static export workflow

This commit is contained in:
2026-09-07 02:54:52 +08:00
parent 188434c113
commit ab0a6b3c13
59 changed files with 2535 additions and 83 deletions
@@ -0,0 +1,21 @@
import { apiClient } from './apiClient'
import DOMPurify from 'dompurify'
interface PlotWire {
result: { content: string; width: number; height: number; warnings: string[] } | null
diagnostics: { message: string; severity: string; line?: number }[]
node_count: number
}
const cache = new Map<string, Promise<{ svg: string; warnings: string[]; nodeCount: number }>>()
export function renderFunctionPlot(source: string, themeId = 'light') {
const key = JSON.stringify([source, themeId])
if (cache.has(key)) return cache.get(key)!
const result = apiClient.post<PlotWire>('/api/plots/function', { source, theme_id: themeId }, { timeoutMs: 30000 }).then(wire => ({
svg: DOMPurify.sanitize(wire.result?.content ?? '', { USE_PROFILES: { svg: true } }),
warnings: [...wire.diagnostics.map(d => `${d.message}${d.line ? ` (行 ${d.line})` : ''}`), ...(wire.result?.warnings ?? [])],
nodeCount: wire.node_count,
})).catch(error => { cache.delete(key); throw error })
if (cache.size >= 32) cache.delete(cache.keys().next().value!)
cache.set(key, result)
return result
}