From 6bce98f9cf9bac693d2ae0d7daad0c8e4fa9420f Mon Sep 17 00:00:00 2001 From: QQ Date: Wed, 11 Nov 2026 23:15:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20src/store.ts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/store.ts | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/store.ts b/src/store.ts index 4223c74..4d90afa 100644 --- a/src/store.ts +++ b/src/store.ts @@ -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, type Settings } from './config.ts' @@ -54,9 +54,30 @@ export function dshHome(): string { /** Absolute path of the settings sidecar. */ export function settingsPath(): string { + return resolve(dshHome(), 'dsh-brain.json') +} + +/** Pre-rename (<=0.4.x) settings sidecar name. */ +export function legacySettingsPath(): string { 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: string, legacy: string): boolean { + 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: string): string | undefined { const value = process.env[name] @@ -129,6 +150,7 @@ export class SettingsStore { constructor(rawConfig: unknown, file: string = settingsPath()) { this.file = file + if (file === settingsPath()) migrateLegacyFile(file, legacySettingsPath()) this.configDefaults = { ...DEFAULT_SETTINGS, ...settingsFromConfig(rawConfig) } this.state = this.read() } @@ -174,17 +196,17 @@ export class SettingsStore { /** The activity:write bearer token (sidecar first, env fallback). */ activityToken(): string { - 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(): string { - 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(): string { - 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. */