dsh-trivium 0.2.0

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.
@@ -0,0 +1,68 @@
1
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
2
+ import { homedir } from "node:os";
3
+ import { join } from "node:path";
4
+ import { looksHttpUrl } from "./embed.js";
5
+
6
+ export const SETTINGS_FILE = join(homedir(), ".dsh", "trivium.json");
7
+
8
+ export function readUiSettings() {
9
+ try {
10
+ const raw = JSON.parse(readFileSync(SETTINGS_FILE, "utf8"));
11
+ return raw && typeof raw === "object" ? raw : {};
12
+ } catch {
13
+ return {};
14
+ }
15
+ }
16
+
17
+ export function recallModeOf(opts = {}) {
18
+ const mode = opts.recallMode;
19
+ if (mode === "auto" || mode === "anchor" || mode === "off") return mode;
20
+ if (opts.autoRecall === true && opts.anchorRecall === true) return "off";
21
+ if (opts.anchorRecall === true) return "anchor";
22
+ if (opts.autoRecall === true) return "auto";
23
+ return "off";
24
+ }
25
+
26
+ export function applyRecallMode(target, mode) {
27
+ const next = mode === "auto" || mode === "anchor" ? mode : "off";
28
+ target.recallMode = next;
29
+ target.autoRecall = next === "auto";
30
+ target.anchorRecall = next === "anchor";
31
+ return target;
32
+ }
33
+
34
+ export function writeUiSettings(patch) {
35
+ mkdirSync(join(homedir(), ".dsh"), { recursive: true });
36
+ const prev = readUiSettings();
37
+ const next = { ...prev };
38
+ let mode = recallModeOf(prev);
39
+ if (patch.recallMode === "auto" || patch.recallMode === "anchor" || patch.recallMode === "off") {
40
+ mode = patch.recallMode;
41
+ } else if (patch.autoRecall === true && patch.anchorRecall === true) {
42
+ mode = "off";
43
+ } else if (patch.anchorRecall === true) {
44
+ mode = "anchor";
45
+ } else if (patch.autoRecall === true) {
46
+ mode = "auto";
47
+ } else {
48
+ if (patch.autoRecall === false && mode === "auto") mode = "off";
49
+ if (patch.anchorRecall === false && mode === "anchor") mode = "off";
50
+ }
51
+ applyRecallMode(next, mode);
52
+ if (typeof patch.extractEnabled === "boolean") next.extractEnabled = patch.extractEnabled;
53
+ if (typeof patch.embeddingEnabled === "boolean") next.embeddingEnabled = patch.embeddingEnabled;
54
+ if (typeof patch.embeddingUrl === "string") {
55
+ const url = patch.embeddingUrl.trim();
56
+ next.embeddingUrl = !url || looksHttpUrl(url) ? url : prev.embeddingUrl || "";
57
+ }
58
+ if (typeof patch.embeddingModel === "string") next.embeddingModel = patch.embeddingModel.trim();
59
+ if (typeof patch.embeddingApiKey === "string" && patch.embeddingApiKey.trim()) {
60
+ next.embeddingApiKey = patch.embeddingApiKey.trim();
61
+ }
62
+ writeFileSync(SETTINGS_FILE, JSON.stringify(next, null, 2), "utf8");
63
+ return next;
64
+ }
65
+
66
+ export function settingsFileExists() {
67
+ return existsSync(SETTINGS_FILE);
68
+ }