@@ -13,6 +13,14 @@ import { getPlatformAdapter } from "../adapter/index.js";
1313import { formatErrorMessage } from "./format.js" ;
1414import { debugLog , debugWarn } from "./log.js" ;
1515
16+ function normalizeEnvPath ( value : string | undefined ) : string | undefined {
17+ const trimmed = value ?. trim ( ) ;
18+ if ( ! trimmed || trimmed === "undefined" || trimmed === "null" ) {
19+ return undefined ;
20+ }
21+ return trimmed ;
22+ }
23+
1624/**
1725 * Resolve the current user's home directory safely across platforms.
1826 *
@@ -39,23 +47,25 @@ export function getHomeDir(): string {
3947 return getPlatformAdapter ( ) . getTempDir ( ) ;
4048}
4149
42- function resolveOpenClawHomeDir ( ) : string {
43- const explicitHome = process . env . OPENCLAW_HOME ?. trim ( ) ;
50+ function resolveOpenClawMediaHomeDir ( ) : string {
51+ const explicitHome = normalizeEnvPath ( process . env . OPENCLAW_HOME ) ;
4452 if ( ! explicitHome ) {
4553 return getHomeDir ( ) ;
4654 }
47- if ( explicitHome === "~" ) {
48- return getHomeDir ( ) ;
49- }
5055 if ( explicitHome . startsWith ( "~/" ) || explicitHome . startsWith ( "~\\" ) ) {
51- return path . join ( getHomeDir ( ) , explicitHome . slice ( 2 ) ) ;
56+ const envHome = normalizeEnvPath ( process . env . HOME ) ?? normalizeEnvPath ( process . env . USERPROFILE ) ;
57+ return envHome ? path . resolve ( explicitHome . replace ( / ^ ~ (? = $ | [ \\ / ] ) / , envHome ) ) : getHomeDir ( ) ;
58+ }
59+ if ( explicitHome === "~" ) {
60+ const envHome = normalizeEnvPath ( process . env . HOME ) ?? normalizeEnvPath ( process . env . USERPROFILE ) ;
61+ return envHome ? path . resolve ( envHome ) : getHomeDir ( ) ;
5262 }
5363 return path . resolve ( explicitHome ) ;
5464}
5565
5666/** Return a path under `~/.openclaw/qqbot` without creating it. */
5767export function getQQBotDataPath ( ...subPaths : string [ ] ) : string {
58- return path . join ( resolveOpenClawHomeDir ( ) , ".openclaw" , "qqbot" , ...subPaths ) ;
68+ return path . join ( getHomeDir ( ) , ".openclaw" , "qqbot" , ...subPaths ) ;
5969}
6070
6171/** Return a path under `~/.openclaw/qqbot`, creating it on demand. */
@@ -74,7 +84,7 @@ export function getQQBotDataDir(...subPaths: string[]): string {
7484 * downloaded images and audio can be accessed by framework media tooling.
7585 */
7686export function getQQBotMediaPath ( ...subPaths : string [ ] ) : string {
77- return path . join ( resolveOpenClawHomeDir ( ) , ".openclaw" , "media" , "qqbot" , ...subPaths ) ;
87+ return path . join ( resolveOpenClawMediaHomeDir ( ) , ".openclaw" , "media" , "qqbot" , ...subPaths ) ;
7888}
7989
8090/** Return a path under `~/.openclaw/media/qqbot`, creating it on demand. */
@@ -97,7 +107,7 @@ export function getQQBotMediaDir(...subPaths: string[]): string {
97107 * the check anchored to a single, well-known directory.
98108 */
99109function getOpenClawMediaDir ( ) : string {
100- return path . join ( resolveOpenClawHomeDir ( ) , ".openclaw" , "media" ) ;
110+ return path . join ( resolveOpenClawMediaHomeDir ( ) , ".openclaw" , "media" ) ;
101111}
102112
103113// ---- Basic platform information ----
0 commit comments