Files
deepseek-harness/packages/util/paths/src/index.ts
T
Turtle 42e3cceb64 feat(paths): add resolveSessionsRoot as the one shared session-store root
Every surface that persists or lists sessions resolves one directory under
the Harness home, so history is shared across working directories instead
of scattered per project.
2026-07-29 21:14:49 +08:00

87 lines
3.5 KiB
TypeScript

/**
* Shared filesystem path helpers for DeepSeek Harness user data.
*
* @module @deepseek-ai/dsh-paths
*/
import { homedir } from 'node:os'
import { join, resolve } from 'node:path'
/** Directory name for the default DeepSeek Harness home under the OS home. */
export const DSH_HOME_DIR_NAME = '.dsh'
/** Stable user-facing display form for the default DeepSeek Harness home. */
export const DEFAULT_DSH_HOME_DISPLAY = `~/${DSH_HOME_DIR_NAME}`
/** Environment variable that overrides the default DeepSeek Harness home. */
export const DSH_HOME_ENV = 'DSH_HOME'
/**
* Resolve the default DeepSeek Harness home using Node's platform path rules.
* @returns the absolute default harness home path.
*/
export function defaultDshHome(): string {
return join(homedir(), DSH_HOME_DIR_NAME)
}
/**
* Expand supported tilde prefixes against the operating-system home.
* @param path - configured path that may begin with `~`, `~/`, or `~\`.
* @returns the expanded path, or the original value when no supported prefix is present.
*/
export function expandHomePath(path: string): string {
if (path === '~') return homedir()
if (path.startsWith('~/') || path.startsWith('~\\')) return join(homedir(), path.slice(2))
return path
}
/**
* Resolve the single-root DeepSeek Harness home.
*
* Precedence, highest first: an explicit configured path, `$DSH_HOME`, then
* `~/.dsh`. The harness keeps all user data under one root. An empty or
* whitespace-only `$DSH_HOME` is treated as unset, so a blank override never
* resolves the home to the current working directory.
* @param configured - explicit harness-home override, which has highest precedence.
* @param env - environment mapping used to read `DSH_HOME`.
* @returns the normalized absolute harness home path.
*/
export function resolveDshHome(configured?: string, env: Record<string, string | undefined> = process.env): string {
const fromEnv = env[DSH_HOME_ENV]
const selected = configured ?? (fromEnv !== undefined && fromEnv.trim().length > 0 ? fromEnv : defaultDshHome())
return resolve(expandHomePath(selected))
}
/** Directory name for persisted session logs under the Harness home. */
export const SESSIONS_DIR_NAME = 'sessions'
/**
* Resolve the shared session-store root under the Harness home.
*
* Every surface that persists sessions resolves this one directory, so history
* is shared across working directories instead of scattered per project. A
* persistence backend may still partition inside it. Two surfaces resolving
* different roots would silently split one user's history into disjoint stores,
* so this is a single owned fact rather than a per-caller `join`.
* @param configuredHome - explicit harness-home override, which has highest precedence.
* @param env - environment mapping used to read `DSH_HOME`.
* @returns the normalized absolute session-store root.
*/
export function resolveSessionsRoot(
configuredHome?: string, env: Record<string, string | undefined> = process.env,
): string {
return join(resolveDshHome(configuredHome, env), SESSIONS_DIR_NAME)
}
/**
* Describe a resolved harness home symbolically for user-facing display.
*
* It never returns an absolute machine path: the default home is labelled
* `~/.dsh`, and any configured home is labelled `$DSH_HOME`.
* @param resolvedHome - the absolute path returned by {@link resolveDshHome}.
* @returns `~/.dsh` for the default home, otherwise `$DSH_HOME`.
*/
export function dshHomeDisplay(resolvedHome: string): string {
return resolvedHome === resolve(defaultDshHome()) ? DEFAULT_DSH_HOME_DISPLAY : `$${DSH_HOME_ENV}`
}