更新 lib/store.js

This commit is contained in:
QQ 2026-11-11 23:14:41 +08:00
parent cc1912ab92
commit 14f53ca248
1 changed files with 28 additions and 5 deletions

View File

@ -1,5 +1,5 @@
/**
* Settings sidecar: `$DSH_HOME/cloud-memory.json`.
* Settings sidecar: `$DSH_HOME/dsh-brain.json` (pre-rename `cloud-memory.json` is migrated on first load).
*
* Why a file and not the DSH credentials service: the desktop profile has no
* stable host-side credentials face this plugin can depend on (the client-side
@ -11,7 +11,7 @@
* 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 { mkdirSync, readFileSync, renameSync, statSync, writeFileSync, chmodSync } from 'node:fs';
import { copyFileSync, existsSync, 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";
@ -24,8 +24,29 @@ export function dshHome() {
}
/** Absolute path of the settings sidecar. */
export function settingsPath() {
return resolve(dshHome(), 'dsh-brain.json');
}
/** Pre-rename (<=0.4.x) settings sidecar name. */
export function legacySettingsPath() {
return resolve(dshHome(), 'cloud-memory.json');
}
/**
* One-time rename carry-over: when `target` does not exist yet but the
* pre-rename `legacy` file does, copy it over so settings and tokens survive
* the dsh-cloud-memory -> dsh-brain rename. The legacy file is left in place
* (never deleted automatically). Returns true when a copy happened.
*/
export function migrateLegacyFile(target, legacy) {
try {
if (target === legacy || existsSync(target) || !existsSync(legacy))
return false;
copyFileSync(legacy, target);
return true;
}
catch {
return false;
}
}
/** Read a token from an env var (an alternative to the sidecar for CI use). */
function envToken(name) {
const value = process.env[name];
@ -108,6 +129,8 @@ export class SettingsStore {
configDefaults;
constructor(rawConfig, file = settingsPath()) {
this.file = file;
if (file === settingsPath())
migrateLegacyFile(file, legacySettingsPath());
this.configDefaults = { ...DEFAULT_SETTINGS, ...settingsFromConfig(rawConfig) };
this.state = this.read();
}
@ -157,15 +180,15 @@ export class SettingsStore {
}
/** The activity:write bearer token (sidecar first, env fallback). */
activityToken() {
return this.state.activityToken ?? envToken('DSH_CLOUD_MEMORY_ACTIVITY_TOKEN') ?? '';
return this.state.activityToken ?? envToken('DSH_BRAIN_ACTIVITY_TOKEN') ?? envToken('DSH_CLOUD_MEMORY_ACTIVITY_TOKEN') ?? '';
}
/** The memory:read / memory:write bearer token. */
memoryToken() {
return this.state.memoryToken ?? envToken('DSH_CLOUD_MEMORY_MEMORY_TOKEN') ?? '';
return this.state.memoryToken ?? envToken('DSH_BRAIN_MEMORY_TOKEN') ?? envToken('DSH_CLOUD_MEMORY_MEMORY_TOKEN') ?? '';
}
/** The summary:write bearer token (optional; absent disables summaries). */
summaryToken() {
return this.state.summaryToken ?? envToken('DSH_CLOUD_MEMORY_SUMMARY_TOKEN') ?? '';
return this.state.summaryToken ?? envToken('DSH_BRAIN_SUMMARY_TOKEN') ?? envToken('DSH_CLOUD_MEMORY_SUMMARY_TOKEN') ?? '';
}
/** Masked snapshot for the browser card. */
describe() {