更新 lib/store.js

This commit is contained in:
QQ 2026-11-12 00:01:58 +08:00
parent 6806a5bcf2
commit 9c4872f12a
1 changed files with 32 additions and 4 deletions

View File

@ -11,10 +11,10 @@
* The file holds the two bearer tokens and the UI-editable overrides. It is
* NEVER uploaded, never printed, and `describe()` only exposes a mask.
*/
import { copyFileSync, existsSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync, chmodSync } from 'node:fs';
import { existsSync, copyFileSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync, chmodSync } from 'node:fs';
import { dirname, resolve } from 'node:path';
import { homedir } from 'node:os';
import { DEFAULT_SETTINGS, normalizeBaseUrl, settingsFromConfig } from "./config.js";
import { DEFAULT_SETTINGS, isPlaceholderBaseUrl, normalizeBaseUrl, originOf, settingsFromConfig } from "./config.js";
/** `$DSH_HOME` (or `~/.dsh`) — the same resolution DSH core uses. */
export function dshHome() {
const fromEnv = process.env.DSH_HOME;
@ -26,7 +26,7 @@ export function dshHome() {
export function settingsPath() {
return resolve(dshHome(), 'dsh-brain.json');
}
/** Pre-rename (<=0.4.x) settings sidecar name. */
/** Pre-rename (<=0.7.x) settings sidecar name. */
export function legacySettingsPath() {
return resolve(dshHome(), 'cloud-memory.json');
}
@ -202,6 +202,8 @@ export class SettingsStore {
memoryToken: { configured: memory !== '', masked: memory === '' ? '' : maskToken(memory) },
summaryToken: { configured: summary !== '', masked: summary === '' ? '' : maskToken(summary) },
statePath: this.file,
baseUrlConfigured: !isPlaceholderBaseUrl(settings.baseUrl),
baseUrlOrigin: originOf(settings.baseUrl),
};
}
/**
@ -213,6 +215,9 @@ export class SettingsStore {
*/
patch(patch) {
const next = { ...this.state };
const beforeOrigin = originOf(this.settings().baseUrl);
/** Tokens this very request supplied; they survive an address change. */
const provided = [];
if (patch.enabled !== undefined) {
if (typeof patch.enabled !== 'boolean')
return { ok: false, error: 'enabled must be a boolean' };
@ -235,12 +240,14 @@ export class SettingsStore {
if (!activityToken.startsWith('cmt_'))
return { ok: false, error: 'activity token must start with cmt_' };
next.activityToken = activityToken;
provided.push('activity');
}
const memoryToken = cleanString(patch.memoryToken);
if (memoryToken !== undefined) {
if (!memoryToken.startsWith('cmt_'))
return { ok: false, error: 'memory token must start with cmt_' };
next.memoryToken = memoryToken;
provided.push('memory');
}
if (patch.requestTimeoutMs !== undefined) {
const requestTimeoutMs = clampInt(patch.requestTimeoutMs, 1000, 60_000);
@ -270,6 +277,7 @@ export class SettingsStore {
if (!summaryToken.startsWith('cmt_'))
return { ok: false, error: 'summary token must start with cmt_' };
next.summaryToken = summaryToken;
provided.push('summary');
}
if (patch.clearSummaryToken === true)
delete next.summaryToken;
@ -277,9 +285,29 @@ export class SettingsStore {
delete next.activityToken;
if (patch.clearMemoryToken === true)
delete next.memoryToken;
// Switching the service origin must not hand the previous service's
// bearer tokens to the new one: everything this request did not explicitly
// replace is dropped, and the caller is told so it can ask to re-authenticate.
const afterOrigin = originOf(next.baseUrl ?? this.configDefaults.baseUrl);
const clearedTokens = [];
if (beforeOrigin !== '' && afterOrigin !== '' && beforeOrigin !== afterOrigin) {
const credentials = [
['activity', 'activityToken'],
['memory', 'memoryToken'],
['summary', 'summaryToken'],
];
for (const [label, key] of credentials) {
if (next[key] === undefined)
continue;
if (provided.includes(label))
continue;
delete next[key];
clearedTokens.push(label);
}
}
this.write(next);
this.state = next;
return { ok: true };
return clearedTokens.length === 0 ? { ok: true } : { ok: true, clearedTokens };
}
/** File size in bytes, for a "sidecar exists" hint in the card. */
size() {