2025-12-21 16:09:14 +09:00
|
|
|
/**
|
2025-12-21 16:11:49 +09:00
|
|
|
* Simple diagram history - matches Next.js app pattern
|
|
|
|
|
* Stores {xml, svg} entries in a circular buffer
|
2025-12-21 16:09:14 +09:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { log } from "./logger.js"
|
|
|
|
|
|
2025-12-21 16:11:49 +09:00
|
|
|
const MAX_HISTORY = 20
|
|
|
|
|
const historyStore = new Map<string, Array<{ xml: string; svg: string }>>()
|
2025-12-21 16:09:14 +09:00
|
|
|
|
2025-12-21 16:11:49 +09:00
|
|
|
export function addHistory(sessionId: string, xml: string, svg = ""): number {
|
2025-12-21 16:09:14 +09:00
|
|
|
let history = historyStore.get(sessionId)
|
|
|
|
|
if (!history) {
|
2025-12-21 16:11:49 +09:00
|
|
|
history = []
|
2025-12-21 16:09:14 +09:00
|
|
|
historyStore.set(sessionId, history)
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-21 16:11:49 +09:00
|
|
|
// Dedupe: skip if same as last entry
|
|
|
|
|
const last = history[history.length - 1]
|
|
|
|
|
if (last?.xml === xml) {
|
|
|
|
|
return history.length - 1
|
2025-12-21 16:09:14 +09:00
|
|
|
}
|
|
|
|
|
|
2025-12-21 16:11:49 +09:00
|
|
|
history.push({ xml, svg })
|
2025-12-21 16:09:14 +09:00
|
|
|
|
2025-12-21 16:11:49 +09:00
|
|
|
// Circular buffer
|
|
|
|
|
if (history.length > MAX_HISTORY) {
|
|
|
|
|
history.shift()
|
2025-12-21 16:09:14 +09:00
|
|
|
}
|
|
|
|
|
|
2025-12-21 16:11:49 +09:00
|
|
|
log.debug(`History: session=${sessionId}, entries=${history.length}`)
|
|
|
|
|
return history.length - 1
|
2025-12-21 16:09:14 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getHistory(
|
|
|
|
|
sessionId: string,
|
2025-12-21 16:11:49 +09:00
|
|
|
): Array<{ xml: string; svg: string }> {
|
|
|
|
|
return historyStore.get(sessionId) || []
|
2025-12-21 16:09:14 +09:00
|
|
|
}
|
|
|
|
|
|
2025-12-21 16:11:49 +09:00
|
|
|
export function getHistoryEntry(
|
2025-12-21 16:09:14 +09:00
|
|
|
sessionId: string,
|
2025-12-21 16:11:49 +09:00
|
|
|
index: number,
|
|
|
|
|
): { xml: string; svg: string } | undefined {
|
2025-12-21 16:09:14 +09:00
|
|
|
const history = historyStore.get(sessionId)
|
2025-12-21 16:11:49 +09:00
|
|
|
return history?.[index]
|
2025-12-21 16:09:14 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function clearHistory(sessionId: string): void {
|
2025-12-21 16:11:49 +09:00
|
|
|
historyStore.delete(sessionId)
|
2025-12-21 16:09:14 +09:00
|
|
|
}
|
2025-12-21 16:58:41 +09:00
|
|
|
|
|
|
|
|
export function updateLastHistorySvg(sessionId: string, svg: string): boolean {
|
|
|
|
|
const history = historyStore.get(sessionId)
|
|
|
|
|
if (!history || history.length === 0) return false
|
|
|
|
|
const last = history[history.length - 1]
|
|
|
|
|
if (!last.svg) {
|
|
|
|
|
last.svg = svg
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|