@pi-unipi/core 2.6.0 → 2.6.1
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/constants.ts +1 -0
- package/model-cache.ts +18 -12
- package/package.json +1 -1
package/constants.ts
CHANGED
package/model-cache.ts
CHANGED
|
@@ -9,14 +9,18 @@
|
|
|
9
9
|
import * as fs from "node:fs";
|
|
10
10
|
import * as path from "node:path";
|
|
11
11
|
|
|
12
|
-
/**
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
12
|
+
/** Resolve the model cache directory at call time (respects HOME changes). */
|
|
13
|
+
function cacheDir(): string {
|
|
14
|
+
return path.join(
|
|
15
|
+
process.env.HOME ?? process.env.USERPROFILE ?? "~",
|
|
16
|
+
".unipi/config",
|
|
17
|
+
);
|
|
18
|
+
}
|
|
17
19
|
|
|
18
|
-
/**
|
|
19
|
-
|
|
20
|
+
/** Resolve the model cache file path at call time. */
|
|
21
|
+
function cacheFile(): string {
|
|
22
|
+
return path.join(cacheDir(), "models-cache.json");
|
|
23
|
+
}
|
|
20
24
|
|
|
21
25
|
/** A single cached model entry */
|
|
22
26
|
export interface CachedModel {
|
|
@@ -42,8 +46,9 @@ export interface ModelCache {
|
|
|
42
46
|
*/
|
|
43
47
|
export function readModelCache(): CachedModel[] {
|
|
44
48
|
try {
|
|
45
|
-
|
|
46
|
-
|
|
49
|
+
const file = cacheFile();
|
|
50
|
+
if (!fs.existsSync(file)) return [];
|
|
51
|
+
const parsed = JSON.parse(fs.readFileSync(file, "utf-8"));
|
|
47
52
|
return Array.isArray(parsed.models) ? parsed.models : [];
|
|
48
53
|
} catch {
|
|
49
54
|
return [];
|
|
@@ -56,14 +61,15 @@ export function readModelCache(): CachedModel[] {
|
|
|
56
61
|
*/
|
|
57
62
|
export function writeModelCache(models: CachedModel[]): void {
|
|
58
63
|
try {
|
|
59
|
-
|
|
60
|
-
|
|
64
|
+
const dir = cacheDir();
|
|
65
|
+
if (!fs.existsSync(dir)) {
|
|
66
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
61
67
|
}
|
|
62
68
|
const cache: ModelCache = {
|
|
63
69
|
updatedAt: new Date().toISOString(),
|
|
64
70
|
models,
|
|
65
71
|
};
|
|
66
|
-
fs.writeFileSync(
|
|
72
|
+
fs.writeFileSync(cacheFile(), JSON.stringify(cache, null, 2) + "\n", "utf-8");
|
|
67
73
|
} catch {
|
|
68
74
|
// Best effort — cache is optional
|
|
69
75
|
}
|