1- import { join } from 'path' ;
1+ import { join , normalize } from 'path' ;
2+ import { homedir } from 'os' ;
3+ import { existsSync , mkdirSync } from 'fs' ;
4+
5+ function resolvePath ( path : string ) : string {
6+ // Handle ~ expansion
7+ if ( path . startsWith ( '~' ) ) {
8+ path = join ( homedir ( ) , path . slice ( 1 ) ) ;
9+ }
10+
11+ // Handle environment variable expansion ($VAR or ${VAR})
12+ path = path . replace ( / \$ ( [ a - z A - Z _ ] [ a - z A - Z 0 - 9 _ ] * ) | \$ \{ ( [ a - z A - Z _ ] [ a - z A - Z 0 - 9 _ ] * ) \} / g, ( _ , p1 , p2 ) => {
13+ const varName = p1 || p2 ;
14+ return process . env [ varName ] || '' ;
15+ } ) ;
16+
17+ return normalize ( path ) ;
18+ }
219
320export function getHistoryDir ( baseDir : string ) : string {
4- return join ( baseDir , '.opencode' , 'history' ) ;
21+ const envPath = process . env . PAI_HISTORY_PATH ;
22+ const defaultPath = '~/.config/opencode/history' ;
23+ const resolvedPath = resolvePath ( envPath || defaultPath ) ;
24+
25+ if ( ! existsSync ( resolvedPath ) ) {
26+ try {
27+ mkdirSync ( resolvedPath , { recursive : true } ) ;
28+ } catch ( error ) {
29+ // If mkdir fails (e.g. permissions), we log it but return the path anyway
30+ // The consumer will likely fail later, which is appropriate
31+ console . error ( `Failed to create history directory at ${ resolvedPath } :` , error ) ;
32+ }
33+ }
34+
35+ return resolvedPath ;
536}
637
738export function getRawOutputsDir ( baseDir : string ) : string {
@@ -14,4 +45,4 @@ export function getSessionsDir(baseDir: string): string {
1445
1546export function getSkillsDir ( baseDir : string ) : string {
1647 return join ( baseDir , '.opencode' , 'skills' ) ;
17- }
48+ }
0 commit comments