index.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * Shared filesystem path helpers for DeepSeek Harness user data.
  3. *
  4. * @module @deepseek-ai/dsh-paths
  5. */
  6. import { homedir } from 'node:os'
  7. import { join, resolve } from 'node:path'
  8. /** Directory name for the default DeepSeek Harness home under the OS home. */
  9. export const DSH_HOME_DIR_NAME = '.dsh'
  10. /** Stable user-facing display form for the default DeepSeek Harness home. */
  11. export const DEFAULT_DSH_HOME_DISPLAY = `~/${DSH_HOME_DIR_NAME}`
  12. /** Environment variable that overrides the default DeepSeek Harness home. */
  13. export const DSH_HOME_ENV = 'DSH_HOME'
  14. /**
  15. * Resolve the default DeepSeek Harness home using Node's platform path rules.
  16. * @returns the absolute default harness home path.
  17. */
  18. export function defaultDshHome(): string {
  19. return join(homedir(), DSH_HOME_DIR_NAME)
  20. }
  21. /**
  22. * Expand supported tilde prefixes against the operating-system home.
  23. * @param path - configured path that may begin with `~`, `~/`, or `~\`.
  24. * @returns the expanded path, or the original value when no supported prefix is present.
  25. */
  26. export function expandHomePath(path: string): string {
  27. if (path === '~') return homedir()
  28. if (path.startsWith('~/') || path.startsWith('~\\')) return join(homedir(), path.slice(2))
  29. return path
  30. }
  31. /**
  32. * Resolve the single-root DeepSeek Harness home.
  33. *
  34. * Precedence, highest first: an explicit configured path, `$DSH_HOME`, then
  35. * `~/.dsh`. The harness keeps all user data under one root. An empty or
  36. * whitespace-only `$DSH_HOME` is treated as unset, so a blank override never
  37. * resolves the home to the current working directory.
  38. * @param configured - explicit harness-home override, which has highest precedence.
  39. * @param env - environment mapping used to read `DSH_HOME`.
  40. * @returns the normalized absolute harness home path.
  41. */
  42. export function resolveDshHome(configured?: string, env: Record<string, string | undefined> = process.env): string {
  43. const fromEnv = env[DSH_HOME_ENV]
  44. const selected = configured ?? (fromEnv !== undefined && fromEnv.trim().length > 0 ? fromEnv : defaultDshHome())
  45. return resolve(expandHomePath(selected))
  46. }
  47. /**
  48. * Join path segments onto the resolved DeepSeek Harness home.
  49. * @param segments - path segments appended to the Harness home; an empty list returns the home itself.
  50. * @returns the normalized absolute joined path.
  51. */
  52. export function dshHomePath(...segments: string[]): string {
  53. return join(resolveDshHome(), ...segments)
  54. }
  55. /**
  56. * Describe a resolved harness home symbolically for user-facing display.
  57. *
  58. * It never returns an absolute machine path: the default home is labelled
  59. * `~/.dsh`, and any configured home is labelled `$DSH_HOME`.
  60. * @param resolvedHome - the absolute path returned by {@link resolveDshHome}.
  61. * @returns `~/.dsh` for the default home, otherwise `$DSH_HOME`.
  62. */
  63. export function dshHomeDisplay(resolvedHome: string): string {
  64. return resolvedHome === resolve(defaultDshHome()) ? DEFAULT_DSH_HOME_DISPLAY : `$${DSH_HOME_ENV}`
  65. }