Skip to content

Commit 3c84bd6

Browse files
authored
Merge pull request #2 from fpr1m3/pai-history-path-env
2 parents 186a052 + 73d6c00 commit 3c84bd6

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

src/lib/paths.ts

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,38 @@
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-zA-Z_][a-zA-Z0-9_]*)|\$\{([a-zA-Z_][a-zA-Z0-9_]*)\}/g, (_, p1, p2) => {
13+
const varName = p1 || p2;
14+
return process.env[varName] || '';
15+
});
16+
17+
return normalize(path);
18+
}
219

320
export 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

738
export function getRawOutputsDir(baseDir: string): string {
@@ -14,4 +45,4 @@ export function getSessionsDir(baseDir: string): string {
1445

1546
export function getSkillsDir(baseDir: string): string {
1647
return join(baseDir, '.opencode', 'skills');
17-
}
48+
}

0 commit comments

Comments
 (0)