@goodandready/dsh-moa 0.2.8 → 0.2.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/design/DESIGN.md +35 -41
- package/lib/client.js +489 -463
- package/lib/moa-runner.js +228 -265
- package/lib/pricing.js +17 -3
- package/package.json +1 -1
package/lib/pricing.js
CHANGED
|
@@ -30,6 +30,14 @@ export const FALLBACK_RATES = { input: 0.50, output: 1.50, cacheHit: 0.15 }
|
|
|
30
30
|
let memoryCatalog = null
|
|
31
31
|
let lastFetchedAt = 0
|
|
32
32
|
|
|
33
|
+
/**
|
|
34
|
+
* Reset in-memory catalog cache (useful for tests and forced reload).
|
|
35
|
+
*/
|
|
36
|
+
export function resetMemoryCatalog() {
|
|
37
|
+
memoryCatalog = null
|
|
38
|
+
lastFetchedAt = 0
|
|
39
|
+
}
|
|
40
|
+
|
|
33
41
|
/**
|
|
34
42
|
* Load cached catalog from disk into memory.
|
|
35
43
|
*/
|
|
@@ -58,10 +66,11 @@ export function loadCachedCatalog(cachePath = CACHE_FILE) {
|
|
|
58
66
|
export async function fetchCatalog({
|
|
59
67
|
url = CATALOG_URL,
|
|
60
68
|
cachePath = CACHE_FILE,
|
|
61
|
-
signal =
|
|
69
|
+
signal = null,
|
|
62
70
|
} = {}) {
|
|
63
71
|
try {
|
|
64
|
-
const
|
|
72
|
+
const fetchSignal = signal || (typeof AbortSignal?.timeout === 'function' ? AbortSignal.timeout(5000) : undefined)
|
|
73
|
+
const res = await fetch(url, { signal: fetchSignal })
|
|
65
74
|
if (!res.ok) return loadCachedCatalog(cachePath)
|
|
66
75
|
const json = await res.json()
|
|
67
76
|
if (!json || !Array.isArray(json.data)) return loadCachedCatalog(cachePath)
|
|
@@ -109,7 +118,11 @@ export function refreshCatalogInBackground(cachePath = CACHE_FILE) {
|
|
|
109
118
|
const catalog = loadCachedCatalog(cachePath)
|
|
110
119
|
const isStale = !lastFetchedAt || Date.now() - lastFetchedAt > REFRESH_INTERVAL_MS
|
|
111
120
|
if (isStale) {
|
|
112
|
-
|
|
121
|
+
try {
|
|
122
|
+
fetchCatalog({ cachePath }).catch(() => {})
|
|
123
|
+
} catch {
|
|
124
|
+
// safe fallback
|
|
125
|
+
}
|
|
113
126
|
}
|
|
114
127
|
return catalog
|
|
115
128
|
}
|
|
@@ -192,3 +205,4 @@ export function estimateTokenCost(slot = {}, usage = {}, customPrices = {}, cach
|
|
|
192
205
|
rates,
|
|
193
206
|
}
|
|
194
207
|
}
|
|
208
|
+
|