-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkdlCodeSystem.js
More file actions
165 lines (143 loc) · 4.65 KB
/
kdlCodeSystem.js
File metadata and controls
165 lines (143 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { readFile, writeFile, mkdir } from 'fs/promises'
import { gunzipSync } from 'node:zlib'
import { fileURLToPath } from 'url'
import path from 'path'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const PACKAGE_REGISTRY = 'https://packages.simplifier.net/dvmd.kdl.r4'
const CACHE_DIR = path.resolve(__dirname, '../.kdl-cache')
const CACHE_FILE = path.join(CACHE_DIR, 'codesystem-kdl.json')
const REFRESH_INTERVAL_MS = 24 * 60 * 60 * 1000 // 24 hours
let cached = null // { codeSystem, meta, leafCodes, prompt, fetchedAt }
/**
* Returns the full KDL CodeSystem with metadata, leaf codes, and a
* pre-built prompt string for GPT. Fetches from Simplifier on first call
* and refreshes every 24h.
*/
export async function getKdlCodeSystem () {
if (cached && Date.now() - cached.fetchedAt < REFRESH_INTERVAL_MS) {
return cached
}
// Try disk cache first
const fromDisk = await loadFromDisk()
if (fromDisk && Date.now() - fromDisk.fetchedAt < REFRESH_INTERVAL_MS) {
cached = fromDisk
return cached
}
// Fetch fresh from Simplifier
try {
const cs = await fetchCodeSystem()
cached = buildCacheEntry(cs)
await saveToDisk(cached)
return cached
} catch (err) {
// If fetch fails but we have stale cache, use it
if (fromDisk) {
cached = fromDisk
return cached
}
// Last resort: bundled fallback
const fallback = await loadBundledFallback()
cached = buildCacheEntry(fallback)
return cached
}
}
/**
* Fetch the latest CodeSystem from Simplifier FHIR package registry.
*/
async function fetchCodeSystem () {
// Get latest version
const metaRes = await fetch(PACKAGE_REGISTRY)
if (!metaRes.ok) throw new Error(`Registry meta failed: ${metaRes.status}`)
const meta = await metaRes.json()
const latestVersion = meta['dist-tags']?.latest
if (!latestVersion) throw new Error('No latest version found in registry')
// Download package tarball
const tarUrl = `${PACKAGE_REGISTRY}/${latestVersion}`
const tarRes = await fetch(tarUrl)
if (!tarRes.ok) throw new Error(`Package download failed: ${tarRes.status}`)
// Parse the tarball to find codesystem-kdl.xml.json
const buffer = Buffer.from(await tarRes.arrayBuffer())
const csJson = extractCodeSystemFromTar(buffer)
return JSON.parse(csJson)
}
/**
* Minimal tar extraction — finds codesystem-kdl.xml.json in a gzipped tar.
*/
function extractCodeSystemFromTar (gzBuffer) {
const tar = gunzipSync(gzBuffer)
let offset = 0
while (offset < tar.length - 512) {
const header = tar.subarray(offset, offset + 512)
const name = header.toString('utf8', 0, 100).replace(/\0/g, '')
const sizeOctal = header.toString('utf8', 124, 136).replace(/\0/g, '').trim()
const size = parseInt(sizeOctal, 8) || 0
if (name.includes('codesystem-kdl')) {
return tar.subarray(offset + 512, offset + 512 + size).toString('utf8')
}
offset += 512 + Math.ceil(size / 512) * 512
}
throw new Error('codesystem-kdl not found in package tarball')
}
/**
* Build the cached entry from a FHIR CodeSystem resource.
*/
function buildCacheEntry (cs) {
const leafCodes = flattenLeafCodes(cs.concept || [])
const meta = {
url: cs.url,
version: cs.version,
title: cs.title,
date: cs.date,
publisher: cs.publisher,
status: cs.status,
count: leafCodes.length,
totalCount: cs.count
}
// Build compact code list for GPT prompt (only leaf codes = level 3)
const codeLines = leafCodes.map(c => {
const def = c.definition ? ` — ${c.definition}` : ''
return `${c.code}: ${c.display}${def}`
})
const prompt = codeLines.join('\n')
return { codeSystem: cs, meta, leafCodes, prompt, fetchedAt: Date.now() }
}
/**
* Recursively extract leaf codes (those without children = level 3 in KDL).
*/
function flattenLeafCodes (concepts) {
const result = []
for (const c of concepts) {
if (c.concept && c.concept.length > 0) {
result.push(...flattenLeafCodes(c.concept))
} else {
result.push({
code: c.code,
display: c.display,
definition: c.definition || ''
})
}
}
return result
}
async function loadFromDisk () {
try {
const raw = await readFile(CACHE_FILE, 'utf8')
return JSON.parse(raw)
} catch {
return null
}
}
async function saveToDisk (entry) {
try {
await mkdir(CACHE_DIR, { recursive: true })
await writeFile(CACHE_FILE, JSON.stringify(entry), 'utf8')
} catch {
// Non-critical
}
}
async function loadBundledFallback () {
const fallbackPath = path.resolve(__dirname, '../config/codesystem-kdl-fallback.json')
const raw = await readFile(fallbackPath, 'utf8')
return JSON.parse(raw)
}