-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathsettings-defs.ts
More file actions
232 lines (203 loc) · 7.62 KB
/
Copy pathsettings-defs.ts
File metadata and controls
232 lines (203 loc) · 7.62 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/**
* UI adapter over the schema. Reads `ui.options` declared inline in
* settings-schema.ts and produces typed widget definitions for the
* settings selector.
*
* To add a new setting to the UI: declare it in `settings-schema.ts`
* with a `ui` block carrying `tab` and `group` (the group must be listed
* in `TAB_GROUPS[tab]`). If it needs a submenu, include `options: [...]`
* (or `options: "runtime"` for runtime-injected lists like themes).
*/
import { TERMINAL } from "@oh-my-pi/pi-tui";
import { Settings } from "../../config/settings";
import {
type AnyUiMetadata,
getDefault,
getEnumValues,
getPathsForTab,
getType,
getUi,
SETTING_TABS,
type SettingPath,
type SettingTab,
type SubmenuOption,
TAB_GROUPS,
} from "../../config/settings-schema";
// ═══════════════════════════════════════════════════════════════════════════
// UI Definition Types
// ═══════════════════════════════════════════════════════════════════════════
export type SettingValue = boolean | string;
interface BaseSettingDef {
path: SettingPath;
label: string;
description: string;
tab: SettingTab;
/** Section within the tab; items are ordered by TAB_GROUPS[tab] and rendered under a heading row. */
group?: string;
/**
* Optional visibility predicate. When supplied and returning false, the
* setting is hidden from the UI. Applies to every variant — booleans,
* enums, submenus, and text inputs.
*/
condition?: () => boolean;
}
export interface BooleanSettingDef extends BaseSettingDef {
type: "boolean";
}
export interface EnumSettingDef extends BaseSettingDef {
type: "enum";
values: readonly string[];
}
type OptionList = ReadonlyArray<SubmenuOption>;
export interface SubmenuSettingDef extends BaseSettingDef {
type: "submenu";
options: OptionList;
onPreview?: (value: string) => void;
onPreviewCancel?: (originalValue: string) => void;
}
export interface TextInputSettingDef extends BaseSettingDef {
type: "text";
}
export type SettingDef = BooleanSettingDef | EnumSettingDef | SubmenuSettingDef | TextInputSettingDef;
// ═══════════════════════════════════════════════════════════════════════════
// Condition Functions
// ═══════════════════════════════════════════════════════════════════════════
const CONDITIONS: Record<string, () => boolean> = {
hasImageProtocol: () => !!TERMINAL.imageProtocol,
advisorEnabled: () => {
try {
return Settings.instance.get("advisor.enabled") === true;
} catch {
return false;
}
},
hindsightActive: () => {
try {
return Settings.instance.get("memory.backend") === "hindsight";
} catch {
return false;
}
},
mnemopiActive: () => {
try {
return Settings.instance.get("memory.backend") === "mnemopi";
} catch {
return false;
}
},
codemapActive: () => {
try {
return Settings.instance.get("codemap.enabled") === true;
} catch {
return false;
}
},
autolearnActive: () => {
try {
return Settings.instance.get("autolearn.enabled") === true;
} catch {
return false;
}
},
autoThinkingActive: () => {
try {
return Settings.instance.get("defaultThinkingLevel") === "auto";
} catch {
return false;
}
},
planModeEnabled: () => {
try {
return Settings.instance.get("plan.enabled");
} catch {
return false;
}
},
};
// ═══════════════════════════════════════════════════════════════════════════
// Schema to UI Conversion
// ═══════════════════════════════════════════════════════════════════════════
function resolveOptions(ui: AnyUiMetadata): OptionList | "runtime" | undefined {
if (!ui.options) return undefined;
if (ui.options === "runtime") return "runtime";
return ui.options;
}
function pathToSettingDef(path: SettingPath): SettingDef | null {
const ui = getUi(path);
if (!ui) return null;
const schemaType = getType(path);
const condition = ui.condition ? CONDITIONS[ui.condition] : undefined;
const base = { path, label: ui.label, description: ui.description, tab: ui.tab, group: ui.group, condition };
if (schemaType === "boolean") {
return { ...base, type: "boolean" };
}
const options = resolveOptions(ui);
if (schemaType === "enum") {
if (options === undefined) {
return { ...base, type: "enum", values: getEnumValues(path) ?? [] };
}
// "runtime" is not a valid sentinel for enums — schema types prevent this,
// but treat defensively as an empty submenu.
return { ...base, type: "submenu", options: options === "runtime" ? [] : options };
}
if (schemaType === "number") {
// Numbers without options are intentionally hidden from the UI.
if (!options || options === "runtime") return null;
return { ...base, type: "submenu", options };
}
if (schemaType === "string") {
if (options === "runtime") {
// Empty list now; the selector layer (theme handling, etc.) injects choices.
return { ...base, type: "submenu", options: [] };
}
if (options) {
return { ...base, type: "submenu", options };
}
return { ...base, type: "text" };
}
return null;
}
// ═══════════════════════════════════════════════════════════════════════════
// Public API
// ═══════════════════════════════════════════════════════════════════════════
/** Cache of generated definitions */
let cachedDefs: SettingDef[] | null = null;
/** Get all setting definitions with UI */
export function getAllSettingDefs(): SettingDef[] {
if (cachedDefs) return cachedDefs;
const defs: SettingDef[] = [];
for (const tab of SETTING_TABS) {
for (const path of getPathsForTab(tab)) {
const def = pathToSettingDef(path);
if (def) defs.push(def);
}
}
cachedDefs = defs;
return defs;
}
/**
* Get settings for a specific tab, ordered by the tab's group layout
* (TAB_GROUPS). Ungrouped settings sort first; within a group, schema
* declaration order is preserved.
*/
export function getSettingsForTab(tab: SettingTab): SettingDef[] {
const defs = getAllSettingDefs().filter(def => def.tab === tab);
const order = TAB_GROUPS[tab];
const rank = (def: SettingDef): number => {
if (!def.group) return -1;
const index = order.indexOf(def.group);
return index >= 0 ? index : order.length;
};
return defs.sort((a, b) => rank(a) - rank(b));
}
/** Get a setting definition by path */
export function getSettingDef(path: SettingPath): SettingDef | undefined {
return getAllSettingDefs().find(def => def.path === path);
}
/** Get default value for display */
export function getDisplayDefault(path: SettingPath): string {
const value = getDefault(path);
if (value === undefined) return "";
if (typeof value === "boolean") return value ? "true" : "false";
return String(value);
}