aegiscode 6.8.3 → 6.8.4
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/bin/aegiscode.js +35 -5
- package/package.json +1 -1
- package/scripts/predist.mjs +99 -0
- package/src/app.js +40 -8
- package/src/chatflow.js +181 -33
- package/src/commands.js +154 -17
- package/src/deps.js +2 -1
- package/src/events.js +37 -14
- package/src/mcp.js +683 -0
- package/src/panels.js +46 -13
- package/src/screen.js +130 -5
- package/src/title.js +217 -0
- package/src/tokens.js +74 -95
- package/src/update.js +75 -1
- package/vendor/desktop/lib/local/engine.js +24 -0
- package/vendor/desktop/lib/settings.js +67 -0
- package/vendor/desktop/renderer/usage.js +372 -41
- package/vendor/hosts/install.js +638 -0
- package/vendor/hosts/openai-shim.js +419 -0
- package/vendor/hosts/spec.js +164 -0
- package/vendor/hosts/targets.js +346 -0
- package/vendor/mcp/server.js +186 -0
package/src/tokens.js
CHANGED
|
@@ -3,92 +3,36 @@
|
|
|
3
3
|
/**
|
|
4
4
|
* Token + cost estimation. Tokens are estimated from text length (~4
|
|
5
5
|
* chars/token, the usual rule of thumb) and priced against the per-model rates
|
|
6
|
-
*
|
|
6
|
+
* in the SHARED table. Everything is labeled approximate in the UI.
|
|
7
7
|
*
|
|
8
|
-
*
|
|
8
|
+
* The rate table itself is not here — see the block below.
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
-
//
|
|
11
|
+
// ── The rate table is NOT defined here ─────────────────────────────────────
|
|
12
12
|
//
|
|
13
|
-
//
|
|
14
|
-
//
|
|
15
|
-
//
|
|
16
|
-
//
|
|
17
|
-
//
|
|
13
|
+
// This file used to carry its own copy of `RATES`, and that copy is how the
|
|
14
|
+
// DeepSeek row came to be wrong in one host and right in the other: the table
|
|
15
|
+
// existed twice (here and desktop/renderer/usage.js), so a rate read off the
|
|
16
|
+
// vendor's pricing page reached whichever copy someone happened to edit. The
|
|
17
|
+
// CLI's copy still said $0.14/$0.28 — the retired V4-Flash tier — while the
|
|
18
|
+
// desktop's had been corrected, and `/cost` would have disagreed with the GUI
|
|
19
|
+
// about the same turn on the same engine.
|
|
18
20
|
//
|
|
19
|
-
//
|
|
20
|
-
//
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
//
|
|
30
|
-
// This row was MISSING, and usageCost fell through to RATES.sonnet for every
|
|
31
|
-
// DeepSeek turn: $3.00/$15.00 per M against a real $0.14/$0.28 — 21x the
|
|
32
|
-
// input rate and 54x the output rate, on every direct DeepSeek call. That is
|
|
33
|
-
// the number that made a local turn look like it cost a fraction of the
|
|
34
|
-
// cloud's, when most of the reported gap was the meter itself.
|
|
35
|
-
//
|
|
36
|
-
// cacheRead is 0.1x input (aegis1 services/pricing.py
|
|
37
|
-
// CACHE_READ_FRACTION_BY_COMPANY["deepseek"] = 0.1 -> $0.014/M).
|
|
38
|
-
// cacheWrite is the input rate: DeepSeek bills a cache write as ordinary
|
|
39
|
-
// input tokens and charges no separate write premium, unlike Anthropic's
|
|
40
|
-
// 1.25x. A zero here would understate a session that writes cache.
|
|
41
|
-
deepseek: { input: 0.14, output: 0.28, cacheRead: 0.014, cacheWrite: 0.14 },
|
|
42
|
-
};
|
|
21
|
+
// So the table lives in exactly one place. `desktop/renderer/usage.js` is
|
|
22
|
+
// already the declared single source of truth for turning a provider's usage
|
|
23
|
+
// object into a display number (cli/src/deps.js resolves it, and
|
|
24
|
+
// test/cli-tools.test.mjs asserts the function identity), so the rates belong
|
|
25
|
+
// there too rather than in a second table that can only drift.
|
|
26
|
+
//
|
|
27
|
+
// `sharedpaths.js` is used rather than `deps.js` because deps.js eagerly
|
|
28
|
+
// requires the engine, the client and the tool registry — pulling all of that
|
|
29
|
+
// in to read a number would be a require cycle for anything in that graph.
|
|
30
|
+
const path = require('node:path');
|
|
43
31
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
* Anthropic spells its family *inside* the id — "claude-opus-4" matches no
|
|
47
|
-
* prefix — so prefix matching alone cannot see it.
|
|
48
|
-
*/
|
|
49
|
-
const RATE_FAMILIES = Object.keys(RATES).filter((k) => k !== 'default');
|
|
32
|
+
const { resolveShared } = require('./sharedpaths.js');
|
|
33
|
+
const shared = require(resolveShared(path.join('desktop', 'renderer', 'usage.js')));
|
|
50
34
|
|
|
51
|
-
|
|
52
|
-
* The rate row for a model id, provider, or alias. Exact id wins, then the
|
|
53
|
-
* longest matching prefix, then a family named inside the id, then the
|
|
54
|
-
* Sonnet-class default — the same resolution order contextWindowFor uses, so
|
|
55
|
-
* the cost meter and the context meter cannot disagree about which family a
|
|
56
|
-
* model belongs to.
|
|
57
|
-
*/
|
|
58
|
-
function ratesFor(model) {
|
|
59
|
-
const raw = String(model || '').toLowerCase();
|
|
60
|
-
if (!raw) return RATES.default;
|
|
61
|
-
// `/class byok` selects a "provider:model" id, and the provider half is a
|
|
62
|
-
// routing label, not a rate: rating it as written fell through every prefix
|
|
63
|
-
// to RATES.default, so an Opus BYOK turn was priced at Sonnet's $3/$15
|
|
64
|
-
// against the vendor's real $5/$25.
|
|
65
|
-
const ids = [raw];
|
|
66
|
-
const tail = raw.slice(raw.lastIndexOf(':') + 1);
|
|
67
|
-
if (tail && tail !== raw) ids.push(tail);
|
|
68
|
-
for (const id of ids) {
|
|
69
|
-
if (RATES[id]) return RATES[id];
|
|
70
|
-
let best = null;
|
|
71
|
-
let bestLen = 0;
|
|
72
|
-
for (const [prefix, rates] of Object.entries(RATES)) {
|
|
73
|
-
if (id.startsWith(prefix) && prefix.length > bestLen) {
|
|
74
|
-
best = rates;
|
|
75
|
-
bestLen = prefix.length;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
if (best) return best;
|
|
79
|
-
}
|
|
80
|
-
// Longest family-key hit wins, so an overlapping pair resolves the same way
|
|
81
|
-
// twice running rather than by object key order.
|
|
82
|
-
let family = null;
|
|
83
|
-
let familyLen = 0;
|
|
84
|
-
for (const key of RATE_FAMILIES) {
|
|
85
|
-
if (raw.includes(key) && key.length > familyLen) {
|
|
86
|
-
family = RATES[key];
|
|
87
|
-
familyLen = key.length;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
return family || RATES.default;
|
|
91
|
-
}
|
|
35
|
+
const { RATES, RATE_BASIS, UNPRICED_BY_DESIGN, isPeak, ratesFor, costBreakdown, cacheHitRate } = shared;
|
|
92
36
|
|
|
93
37
|
// System prompt + tool definitions overhead, roughly, in tokens.
|
|
94
38
|
const SYSTEM_TOKENS = 18000;
|
|
@@ -127,11 +71,11 @@ const CONTEXT_WINDOWS = {
|
|
|
127
71
|
function contextWindowFor(model) {
|
|
128
72
|
const raw = String(model || '').toLowerCase();
|
|
129
73
|
if (!raw) return CONTEXT_WINDOW;
|
|
130
|
-
// Same two-candidate lookup as ratesFor
|
|
131
|
-
// id ("deepseek:deepseek-v4-flash") carries a routing label in
|
|
132
|
-
// model, and matching it as written left the 1M-token DeepSeek
|
|
133
|
-
// as the 200k default — a meter that would have told a user
|
|
134
|
-
// of a context that was 4% full.
|
|
74
|
+
// Same two-candidate lookup as ratesFor in the shared table, for the same
|
|
75
|
+
// reason: a byok id ("deepseek:deepseek-v4-flash") carries a routing label in
|
|
76
|
+
// front of the model, and matching it as written left the 1M-token DeepSeek
|
|
77
|
+
// window reading as the 200k default — a meter that would have told a user
|
|
78
|
+
// they were at 20% of a context that was 4% full.
|
|
135
79
|
const ids = [raw];
|
|
136
80
|
const tail = raw.slice(raw.lastIndexOf(':') + 1);
|
|
137
81
|
if (tail && tail !== raw) ids.push(tail);
|
|
@@ -168,14 +112,24 @@ function transcriptUsage(transcript, model = 'sonnet') {
|
|
|
168
112
|
return { input, output, cacheRead, cacheWrite };
|
|
169
113
|
}
|
|
170
114
|
|
|
171
|
-
/**
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
115
|
+
/**
|
|
116
|
+
* Dollar cost of a usage record at the given model's rates.
|
|
117
|
+
*
|
|
118
|
+
* Returns a number for backward compatibility, but the basis is not lost: use
|
|
119
|
+
* `costBreakdown` when the caller needs to know whether the figure is real.
|
|
120
|
+
* (`usageCost` in the shared module, which also folds the record through
|
|
121
|
+
* `usageBuckets` first — so a wire usage object is normalized identically in
|
|
122
|
+
* both hosts, where this file's old copy read `usage.input` directly.)
|
|
123
|
+
*
|
|
124
|
+
* `model` has no `= 'sonnet'` default here either, and that matters for more
|
|
125
|
+
* than tidiness: a default parameter in THIS file would re-introduce exactly
|
|
126
|
+
* the split the shared module just removed, and only on the CLI side — so
|
|
127
|
+
* `usageCost(u)` would report `basis: 'exact'` in the terminal and
|
|
128
|
+
* `basis: 'default'` in the GUI for the same turn on the same engine. The
|
|
129
|
+
* whole point of delegating is that both hosts answer identically.
|
|
130
|
+
*/
|
|
131
|
+
function usageCost(usage, model, opts = {}) {
|
|
132
|
+
return shared.usageCost(usage, model, opts);
|
|
179
133
|
}
|
|
180
134
|
|
|
181
135
|
/** Full session accounting: per-bucket tokens, cost, and context used %. */
|
|
@@ -186,6 +140,14 @@ function sessionAccounting(transcript, model = 'sonnet') {
|
|
|
186
140
|
const history = usage.input + usage.output;
|
|
187
141
|
const used = system + tools + history;
|
|
188
142
|
const contextWindow = contextWindowFor(model);
|
|
143
|
+
// ONE breakdown, not a cost call plus a separate basis call: ratesFor reads
|
|
144
|
+
// the wall clock for peak/off-peak, so two evaluations straddling a peak
|
|
145
|
+
// boundary would report a peak cost with an off-peak basis (or vice versa) —
|
|
146
|
+
// two different claims about the same number, which is the thing this return
|
|
147
|
+
// shape exists to prevent. aegiscodex-dev's sessionAccounting is written the
|
|
148
|
+
// same way; if one host folds this into two calls again the two will disagree
|
|
149
|
+
// about the same turn, which is how the rate table split in the first place.
|
|
150
|
+
const breakdown = costBreakdown(usage, model);
|
|
189
151
|
return {
|
|
190
152
|
usage,
|
|
191
153
|
system,
|
|
@@ -194,7 +156,12 @@ function sessionAccounting(transcript, model = 'sonnet') {
|
|
|
194
156
|
used,
|
|
195
157
|
contextWindow,
|
|
196
158
|
pct: Math.min(100, Math.round((used / contextWindow) * 100)),
|
|
197
|
-
cost:
|
|
159
|
+
cost: breakdown.cost,
|
|
160
|
+
// Which rate row priced this. 'default' means the model is unpriced and
|
|
161
|
+
// `cost` is a Sonnet-class placeholder — surfaced rather than swallowed so
|
|
162
|
+
// /cost can label it instead of printing a fabricated figure.
|
|
163
|
+
costBasis: breakdown.basis,
|
|
164
|
+
costPriced: breakdown.priced,
|
|
198
165
|
};
|
|
199
166
|
}
|
|
200
167
|
|
|
@@ -204,8 +171,9 @@ function sessionAccounting(transcript, model = 'sonnet') {
|
|
|
204
171
|
* already includes system prompt + tools in cacheRead, so `used` counts
|
|
205
172
|
* input + output + cache instead of re-adding the constants.
|
|
206
173
|
*/
|
|
207
|
-
function accountingFromUsage(usage, model = 'sonnet', { exchanges = 0, real = false, costUsd } = {}) {
|
|
208
|
-
const
|
|
174
|
+
function accountingFromUsage(usage, model = 'sonnet', { exchanges = 0, real = false, costUsd, at } = {}) {
|
|
175
|
+
const breakdown = costBreakdown(usage, model, { at });
|
|
176
|
+
const cost = typeof costUsd === 'number' ? costUsd : breakdown.cost;
|
|
209
177
|
const used = real
|
|
210
178
|
? usage.input + usage.output + usage.cacheRead + usage.cacheWrite
|
|
211
179
|
: SYSTEM_TOKENS + TOOL_TOKENS + usage.input + usage.output;
|
|
@@ -219,6 +187,12 @@ function accountingFromUsage(usage, model = 'sonnet', { exchanges = 0, real = fa
|
|
|
219
187
|
contextWindow,
|
|
220
188
|
pct: Math.min(100, Math.round((used / contextWindow) * 100)),
|
|
221
189
|
cost,
|
|
190
|
+
// A settled charge (costUsd) is the ledger's own truth, so it is 'settled'
|
|
191
|
+
// rather than a table match — the pool's bill carries a margin and a
|
|
192
|
+
// prompt-cache discount the table cannot see, so the two must never be
|
|
193
|
+
// conflated.
|
|
194
|
+
costBasis: typeof costUsd === 'number' ? 'settled' : breakdown.basis,
|
|
195
|
+
costPriced: typeof costUsd === 'number' ? true : breakdown.priced,
|
|
222
196
|
exchanges,
|
|
223
197
|
real,
|
|
224
198
|
};
|
|
@@ -235,12 +209,17 @@ function fmtCost(d) {
|
|
|
235
209
|
|
|
236
210
|
module.exports = {
|
|
237
211
|
RATES,
|
|
212
|
+
RATE_BASIS,
|
|
213
|
+
UNPRICED_BY_DESIGN,
|
|
214
|
+
isPeak,
|
|
238
215
|
SYSTEM_TOKENS,
|
|
239
216
|
TOOL_TOKENS,
|
|
240
217
|
CONTEXT_WINDOW,
|
|
241
218
|
CONTEXT_WINDOWS,
|
|
242
219
|
contextWindowFor,
|
|
243
220
|
ratesFor,
|
|
221
|
+
costBreakdown,
|
|
222
|
+
cacheHitRate,
|
|
244
223
|
estimateTokens,
|
|
245
224
|
transcriptUsage,
|
|
246
225
|
usageCost,
|
package/src/update.js
CHANGED
|
@@ -7,7 +7,81 @@
|
|
|
7
7
|
* the same thing for `aegis-desktop`: two hosts, two package names, one
|
|
8
8
|
* checker. Resolved through deps.js so the in-repo and published-vendor
|
|
9
9
|
* layouts both work.
|
|
10
|
+
*
|
|
11
|
+
* On top of the shared checker this module carries the three pieces /upgrade
|
|
12
|
+
* needs — `compareVersions`, `upgradeAdvice` and `latestPublishedVersion` —
|
|
13
|
+
* ported from the reference (`aegiscodex-dev/src/session.js`). They belong
|
|
14
|
+
* here, next to `isNewer`/`updateLine`, so the CLI has ONE idea of "is there a
|
|
15
|
+
* newer published version": the welcome-box notice and /upgrade now answer
|
|
16
|
+
* from the same place and compare versions the same way. The shared module's
|
|
17
|
+
* own functions are spread through unchanged, keeping the identity the
|
|
18
|
+
* update-channel test pins (the shim and the shared file must be the SAME
|
|
19
|
+
* code, not a copy).
|
|
10
20
|
*/
|
|
11
21
|
|
|
22
|
+
const { execFileSync } = require('node:child_process');
|
|
23
|
+
|
|
12
24
|
const { resolveShared } = require('./deps.js');
|
|
13
|
-
|
|
25
|
+
|
|
26
|
+
const shared = require(resolveShared('client/update.js'));
|
|
27
|
+
|
|
28
|
+
const VERSION = require('../package.json').version;
|
|
29
|
+
/** Both hosts publish from npm; the CLI is `aegiscode`. */
|
|
30
|
+
const PKG = shared.PKG || 'aegiscode';
|
|
31
|
+
|
|
32
|
+
// ── /upgrade ─────────────────────────────────────────────────────────────────
|
|
33
|
+
|
|
34
|
+
/** Compare two dotted version strings: -1 | 0 | 1 (prerelease tags ignored). */
|
|
35
|
+
function compareVersions(a, b) {
|
|
36
|
+
const pa = String(a || '0').split('-')[0].split('.').map((n) => Number.parseInt(n, 10) || 0);
|
|
37
|
+
const pb = String(b || '0').split('-')[0].split('.').map((n) => Number.parseInt(n, 10) || 0);
|
|
38
|
+
for (let i = 0; i < Math.max(pa.length, pb.length); i++) {
|
|
39
|
+
const d = (pa[i] || 0) - (pb[i] || 0);
|
|
40
|
+
if (d) return d > 0 ? 1 : -1;
|
|
41
|
+
}
|
|
42
|
+
return 0;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** What /upgrade should say, given the local and published versions. */
|
|
46
|
+
function upgradeAdvice({ current = VERSION, latest = null, packageName = PKG } = {}) {
|
|
47
|
+
if (!latest) {
|
|
48
|
+
return {
|
|
49
|
+
upToDate: null,
|
|
50
|
+
lines: [` Could not read the npm registry — local version is ${current}.`],
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
const cmp = compareVersions(latest, current);
|
|
54
|
+
if (cmp > 0) {
|
|
55
|
+
return {
|
|
56
|
+
upToDate: false,
|
|
57
|
+
lines: [
|
|
58
|
+
` Installed: ${current}`,
|
|
59
|
+
` Published: ${latest}`,
|
|
60
|
+
` Upgrade: npm i -g ${packageName}@latest`,
|
|
61
|
+
],
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
upToDate: true,
|
|
66
|
+
lines: [` Installed ${current} is the published ${latest} — nothing to upgrade.`],
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Best-effort registry lookup (5s cap). Returns a version string or null. */
|
|
71
|
+
function latestPublishedVersion({ packageName = PKG, timeoutMs = 5000 } = {}) {
|
|
72
|
+
try {
|
|
73
|
+
const out = execFileSync('npm', ['view', packageName, 'version'], {
|
|
74
|
+
encoding: 'utf8', timeout: timeoutMs, stdio: ['ignore', 'pipe', 'ignore'],
|
|
75
|
+
});
|
|
76
|
+
const v = String(out).trim();
|
|
77
|
+
return /^\d+\.\d+\.\d+/.test(v) ? v : null;
|
|
78
|
+
} catch { return null; }
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
module.exports = {
|
|
82
|
+
...shared,
|
|
83
|
+
VERSION,
|
|
84
|
+
compareVersions,
|
|
85
|
+
upgradeAdvice,
|
|
86
|
+
latestPublishedVersion,
|
|
87
|
+
};
|
|
@@ -437,6 +437,18 @@ function createLocalEngine({
|
|
|
437
437
|
settings,
|
|
438
438
|
tools,
|
|
439
439
|
promptBuilder,
|
|
440
|
+
// Phase 20 — the level's recall breadth, injected by the host that owns the
|
|
441
|
+
// avatar ledger (desktop/main.js builds one from lib/avatar/turn.js). It is a
|
|
442
|
+
// SEAM rather than a call into lib/avatar/* because this engine is shared with
|
|
443
|
+
// the CLI (cli/src/deps.js loads this file): the engine must not grow a
|
|
444
|
+
// dependency on the desktop's avatar directory, and a host with no avatar
|
|
445
|
+
// simply passes nothing and gets the exact pre-Phase-20 turn.
|
|
446
|
+
//
|
|
447
|
+
// `recallProvider({ payload, env, system })` may be sync or async and returns
|
|
448
|
+
// either the final system prompt or `{ system }`. It runs once per turn,
|
|
449
|
+
// before the first dispatch, and its result is the system prompt every round
|
|
450
|
+
// of that turn travels with.
|
|
451
|
+
recallProvider,
|
|
440
452
|
env,
|
|
441
453
|
getConfirmMode,
|
|
442
454
|
// Injectable for tests; defaults to the real transport so every existing
|
|
@@ -1532,6 +1544,12 @@ function createLocalEngine({
|
|
|
1532
1544
|
statedMaxTokens: opts.statedMaxTokens ? doubledBudget(opts.statedMaxTokens) : undefined,
|
|
1533
1545
|
});
|
|
1534
1546
|
addUsage(res);
|
|
1547
|
+
// Same guard as line ~1499, for the same reason: an abort landing
|
|
1548
|
+
// during THIS dispatch also resolves as empty text, and without
|
|
1549
|
+
// this check that "no answer" would fall straight into the
|
|
1550
|
+
// empty-turn nudge below instead of ending the turn — a cancelled
|
|
1551
|
+
// request quietly buying a THIRD billed call.
|
|
1552
|
+
if (signal.aborted) return withTurnUsage(res);
|
|
1535
1553
|
}
|
|
1536
1554
|
|
|
1537
1555
|
const calls = toolSchemas.length ? extractToolCalls(res) : [];
|
|
@@ -1562,6 +1580,12 @@ function createLocalEngine({
|
|
|
1562
1580
|
tools: [],
|
|
1563
1581
|
});
|
|
1564
1582
|
addUsage(res);
|
|
1583
|
+
// Same guard again: a cancel landing during the synthesis pass
|
|
1584
|
+
// resolves empty too, and without this check it fell through to
|
|
1585
|
+
// emptyTurnError — a real exception, so the renderer painted a red
|
|
1586
|
+
// "no answer" failure over what was actually just the user hitting
|
|
1587
|
+
// Cancel.
|
|
1588
|
+
if (signal.aborted) return withTurnUsage(res);
|
|
1565
1589
|
if (!assistantText(res)) {
|
|
1566
1590
|
throw emptyTurnError({
|
|
1567
1591
|
cls,
|
|
@@ -74,6 +74,17 @@ const CONFIRM_MODE_NAMESPACE = '__confirmMode';
|
|
|
74
74
|
* what turns it off. Nothing secret lives here, so no encryption. */
|
|
75
75
|
const MEMORY_PERSIST_NAMESPACE = '__memoryPersist';
|
|
76
76
|
|
|
77
|
+
/** Reserved namespace for the avatar/companion preferences (Phase 21):
|
|
78
|
+
* `{ enabled, showLevel, motion }` — app-level, not a provider, so it stays out
|
|
79
|
+
* of the provider CRUD surface for the same reason the AEGIS key does. The
|
|
80
|
+
* persona itself is deliberately NOT stored here: it is a document, not a
|
|
81
|
+
* preference, and it lives at `<userData>/avatar/persona.json`, read and
|
|
82
|
+
* written by main through lib/avatar/store.js so a hand-edited or imported
|
|
83
|
+
* persona is validated on the way in (see docs/avatar-plan.md §3).
|
|
84
|
+
* Defaults are the shipped behaviour: the avatar is ON, the level readout is
|
|
85
|
+
* shown, and motion follows the OS `prefers-reduced-motion` setting. */
|
|
86
|
+
const AVATAR_NAMESPACE = '__avatar';
|
|
87
|
+
|
|
77
88
|
/** Namespaces the provider-config surface must never see or mutate. */
|
|
78
89
|
const RESERVED_NAMESPACES = Object.freeze([
|
|
79
90
|
AEGIS_KEY_NAMESPACE,
|
|
@@ -81,8 +92,12 @@ const RESERVED_NAMESPACES = Object.freeze([
|
|
|
81
92
|
QUICK_LAUNCHER_NAMESPACE,
|
|
82
93
|
CONFIRM_MODE_NAMESPACE,
|
|
83
94
|
MEMORY_PERSIST_NAMESPACE,
|
|
95
|
+
AVATAR_NAMESPACE,
|
|
84
96
|
]);
|
|
85
97
|
|
|
98
|
+
/** Motion modes the renderer understands (`system` = follow the OS). */
|
|
99
|
+
const AVATAR_MOTION_MODES = Object.freeze(['system', 'full', 'minimal']);
|
|
100
|
+
|
|
86
101
|
/** True for the AEGIS-key namespace(s) — provider CRUD must refuse these. */
|
|
87
102
|
function isReservedNamespace(provider) {
|
|
88
103
|
return RESERVED_NAMESPACES.includes(provider);
|
|
@@ -359,6 +374,53 @@ function createSettingsStore({ dir, safeStorage } = {}) {
|
|
|
359
374
|
return memoryPersistState();
|
|
360
375
|
}
|
|
361
376
|
|
|
377
|
+
// --- Avatar: reserved namespace, plain preferences (Phase 21) ------------
|
|
378
|
+
// The companion's on/off switch, its level readout, and its motion mode.
|
|
379
|
+
// Nothing secret lives here, so no encryption — same reasoning as the quick
|
|
380
|
+
// launcher and memory-persist namespaces above.
|
|
381
|
+
//
|
|
382
|
+
// Deliberately narrow: `setAvatar()` stores only these three known keys, so a
|
|
383
|
+
// renderer (or a hand-edited settings.json) cannot park arbitrary state — a
|
|
384
|
+
// persona document, a capability override — in a namespace main reads back.
|
|
385
|
+
// The persona is a separate, validated file (lib/avatar/store.js) for exactly
|
|
386
|
+
// that reason.
|
|
387
|
+
|
|
388
|
+
function getAvatar() {
|
|
389
|
+
const cfg = load()[AVATAR_NAMESPACE] || {};
|
|
390
|
+
return {
|
|
391
|
+
// Unset === true: the companion ships on; an explicit `false` is the only
|
|
392
|
+
// thing that turns it off (and it must survive a reload).
|
|
393
|
+
enabled: cfg.enabled === undefined ? true : Boolean(cfg.enabled),
|
|
394
|
+
showLevel: cfg.showLevel === undefined ? true : Boolean(cfg.showLevel),
|
|
395
|
+
motion: AVATAR_MOTION_MODES.includes(cfg.motion) ? cfg.motion : 'system',
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/** `{ ...preferences, source }` — mirrors memoryPersistState() so "off" and
|
|
400
|
+
* "never set" stay distinguishable in the UI and in tests. */
|
|
401
|
+
function avatarState() {
|
|
402
|
+
const cfg = load()[AVATAR_NAMESPACE] || {};
|
|
403
|
+
return {
|
|
404
|
+
...getAvatar(),
|
|
405
|
+
source: cfg.enabled === undefined ? 'default' : 'config',
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/** Partial merge of the three known keys. Unknown keys are dropped, not saved. */
|
|
410
|
+
function setAvatar(patch = {}) {
|
|
411
|
+
const current = getAvatar();
|
|
412
|
+
const p = patch && typeof patch === 'object' ? patch : {};
|
|
413
|
+
const next = {
|
|
414
|
+
enabled: p.enabled === undefined ? current.enabled : Boolean(p.enabled),
|
|
415
|
+
showLevel: p.showLevel === undefined ? current.showLevel : Boolean(p.showLevel),
|
|
416
|
+
motion: AVATAR_MOTION_MODES.includes(p.motion) ? p.motion : current.motion,
|
|
417
|
+
};
|
|
418
|
+
const data = load();
|
|
419
|
+
data[AVATAR_NAMESPACE] = next;
|
|
420
|
+
save(data);
|
|
421
|
+
return avatarState();
|
|
422
|
+
}
|
|
423
|
+
|
|
362
424
|
return {
|
|
363
425
|
file,
|
|
364
426
|
get,
|
|
@@ -379,6 +441,9 @@ function createSettingsStore({ dir, safeStorage } = {}) {
|
|
|
379
441
|
getMemoryPersist,
|
|
380
442
|
memoryPersistState,
|
|
381
443
|
setMemoryPersist,
|
|
444
|
+
getAvatar,
|
|
445
|
+
avatarState,
|
|
446
|
+
setAvatar,
|
|
382
447
|
};
|
|
383
448
|
}
|
|
384
449
|
|
|
@@ -389,6 +454,8 @@ module.exports = {
|
|
|
389
454
|
QUICK_LAUNCHER_NAMESPACE,
|
|
390
455
|
CONFIRM_MODE_NAMESPACE,
|
|
391
456
|
MEMORY_PERSIST_NAMESPACE,
|
|
457
|
+
AVATAR_NAMESPACE,
|
|
458
|
+
AVATAR_MOTION_MODES,
|
|
392
459
|
DEFAULT_QUICK_LAUNCHER_SHORTCUT,
|
|
393
460
|
RESERVED_NAMESPACES,
|
|
394
461
|
isReservedNamespace,
|