-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathbackend-settings-helpers.ts
More file actions
162 lines (150 loc) · 4.9 KB
/
Copy pathbackend-settings-helpers.ts
File metadata and controls
162 lines (150 loc) · 4.9 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
import type { PluginConfig } from "../types.js";
import {
BACKEND_DEFAULTS,
BACKEND_NUMBER_OPTION_BY_KEY,
BACKEND_NUMBER_OPTIONS,
BACKEND_TOGGLE_OPTIONS,
type BackendNumberSettingKey,
type BackendNumberSettingOption,
type BackendSettingFocusKey,
} from "./backend-settings-schema.js";
export function cloneBackendPluginConfig(config: PluginConfig): PluginConfig {
const fallbackChain = config.unsupportedCodexFallbackChain;
return {
...BACKEND_DEFAULTS,
...config,
unsupportedCodexFallbackChain:
fallbackChain && typeof fallbackChain === "object"
? { ...fallbackChain }
: {},
};
}
export function backendSettingsSnapshot(
config: PluginConfig,
): Record<string, unknown> {
const snapshot: Record<string, unknown> = {};
for (const option of BACKEND_TOGGLE_OPTIONS) {
snapshot[option.key] =
config[option.key] ?? BACKEND_DEFAULTS[option.key] ?? false;
}
for (const option of BACKEND_NUMBER_OPTIONS) {
snapshot[option.key] =
config[option.key] ?? BACKEND_DEFAULTS[option.key] ?? option.min;
}
return snapshot;
}
export function backendSettingsEqual(
left: PluginConfig,
right: PluginConfig,
): boolean {
return (
JSON.stringify(backendSettingsSnapshot(left)) ===
JSON.stringify(backendSettingsSnapshot(right))
);
}
export function formatBackendNumberValue(
option: BackendNumberSettingOption,
value: number,
): string {
if (option.unit === "percent") return `${Math.round(value)}%`;
if (option.unit === "count") return `${Math.round(value)}`;
if (value >= 60_000 && value % 60_000 === 0) {
return `${Math.round(value / 60_000)}m`;
}
if (value >= 1_000 && value % 1_000 === 0) {
return `${Math.round(value / 1_000)}s`;
}
return `${Math.round(value)}ms`;
}
function clampBackendNumber(
option: BackendNumberSettingOption,
value: number,
): number {
return Math.max(option.min, Math.min(option.max, Math.round(value)));
}
export function buildBackendSettingsPreview(
config: PluginConfig,
ui: ReturnType<typeof import("../ui/runtime.js").getUiRuntimeOptions>,
focus: BackendSettingFocusKey,
deps: {
highlightPreviewToken: (
text: string,
ui: ReturnType<typeof import("../ui/runtime.js").getUiRuntimeOptions>,
) => string;
},
): { label: string; hint: string } {
const liveSync =
config.liveAccountSync ?? BACKEND_DEFAULTS.liveAccountSync ?? true;
const affinity =
config.sessionAffinity ?? BACKEND_DEFAULTS.sessionAffinity ?? true;
const preemptive =
config.preemptiveQuotaEnabled ??
BACKEND_DEFAULTS.preemptiveQuotaEnabled ??
true;
const threshold5h =
config.preemptiveQuotaRemainingPercent5h ??
BACKEND_DEFAULTS.preemptiveQuotaRemainingPercent5h ??
5;
const threshold7d =
config.preemptiveQuotaRemainingPercent7d ??
BACKEND_DEFAULTS.preemptiveQuotaRemainingPercent7d ??
5;
const fetchTimeout =
config.fetchTimeoutMs ?? BACKEND_DEFAULTS.fetchTimeoutMs ?? 60_000;
const stallTimeout =
config.streamStallTimeoutMs ??
BACKEND_DEFAULTS.streamStallTimeoutMs ??
45_000;
const fetchTimeoutOption = BACKEND_NUMBER_OPTION_BY_KEY.get("fetchTimeoutMs");
const stallTimeoutOption = BACKEND_NUMBER_OPTION_BY_KEY.get(
"streamStallTimeoutMs",
);
const highlightIfFocused = (
key: BackendSettingFocusKey,
text: string,
): string => {
if (focus !== key) return text;
return deps.highlightPreviewToken(text, ui);
};
const label = [
`live sync ${highlightIfFocused("liveAccountSync", liveSync ? "on" : "off")}`,
`affinity ${highlightIfFocused("sessionAffinity", affinity ? "on" : "off")}`,
`preemptive ${highlightIfFocused("preemptiveQuotaEnabled", preemptive ? "on" : "off")}`,
].join(" | ");
const hint = [
`thresholds 5h<=${highlightIfFocused("preemptiveQuotaRemainingPercent5h", `${threshold5h}%`)}`,
`7d<=${highlightIfFocused("preemptiveQuotaRemainingPercent7d", `${threshold7d}%`)}`,
`timeouts ${highlightIfFocused("fetchTimeoutMs", fetchTimeoutOption ? formatBackendNumberValue(fetchTimeoutOption, fetchTimeout) : `${fetchTimeout}ms`)}/${highlightIfFocused("streamStallTimeoutMs", stallTimeoutOption ? formatBackendNumberValue(stallTimeoutOption, stallTimeout) : `${stallTimeout}ms`)}`,
].join(" | ");
return { label, hint };
}
export function buildBackendConfigPatch(
config: PluginConfig,
): Partial<PluginConfig> {
const patch: Partial<PluginConfig> = {};
for (const option of BACKEND_TOGGLE_OPTIONS) {
const value = config[option.key];
if (typeof value === "boolean") {
patch[option.key] = value;
}
}
for (const option of BACKEND_NUMBER_OPTIONS) {
const value = config[option.key];
if (typeof value === "number" && Number.isFinite(value)) {
patch[option.key] = clampBackendNumber(option, value);
}
}
return patch;
}
export function clampBackendNumberForTests(
settingKey: string,
value: number,
): number {
const option = BACKEND_NUMBER_OPTION_BY_KEY.get(
settingKey as BackendNumberSettingKey,
);
if (!option) {
throw new Error(`Unknown backend numeric setting key: ${settingKey}`);
}
return clampBackendNumber(option, value);
}