更新 src/store.ts

This commit is contained in:
QQ 2026-11-11 23:15:49 +08:00
parent 30170c66f2
commit 6bce98f9cf
1 changed files with 27 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, 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. */