Files
CodeNomad/packages/ui/src/lib/monaco/model-cache.ts
Shantur Rathore b59e85abda feat(ui): add Monaco changes/files right drawer viewers
Use OpenCode v2 file APIs for browsing and Monaco DiffEditor for session snapshot diffs, with local baseline language metadata and optional CDN language loading.
2026-02-09 21:00:40 +00:00

54 lines
1.3 KiB
TypeScript

type MonacoApi = any
type CachedModel = {
model: any
}
const MAX_MODELS = 5
// LRU map: newest at the end.
const models = new Map<string, CachedModel>()
function touch(key: string, entry: CachedModel) {
models.delete(key)
models.set(key, entry)
}
function evictIfNeeded() {
while (models.size > MAX_MODELS) {
const oldestKey = models.keys().next().value as string | undefined
if (!oldestKey) return
const entry = models.get(oldestKey)
models.delete(oldestKey)
try {
entry?.model.dispose()
} catch {
// ignore
}
}
}
export function getOrCreateTextModel(params: {
monaco: MonacoApi
cacheKey: string
value: string
languageId: string
}): any {
const existing = models.get(params.cacheKey)
if (existing) {
touch(params.cacheKey, existing)
if (existing.model.getValue() !== params.value) {
existing.model.setValue(params.value)
}
return existing.model
}
const uri = params.monaco.Uri.parse(`opencode://model/${encodeURIComponent(params.cacheKey)}`)
// Create as plaintext. We'll set the final language after its contribution is loaded.
const model = params.monaco.editor.createModel(params.value, "plaintext", uri)
const entry = { model }
models.set(params.cacheKey, entry)
evictIfNeeded()
return model
}