summaryrefslogtreecommitdiff
path: root/src/paths.ts
blob: b008f4f47e77399767b90200e97bc5abf865a3af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
 * pi's agent config directory, and the file pi-browser-harness stores its
 * `/browser-target` choice in.
 *
 * This extension reads that file so a single `/browser-target` decision governs
 * both packages. It never writes it — the harness owns that setting, and two
 * writers of one file is a bug waiting to happen.
 *
 * pi exports `getAgentDir()`, but resolving it here would be a *runtime* import
 * of the host package from an installed extension's own tree. Every other pi
 * import in this codebase is `import type` and erased at build time, so the
 * three lines are replicated instead (pi's dist/config.js: `PI_CODING_AGENT_DIR`,
 * CONFIG_DIR_NAME `.pi`). Same reasoning as pi-browser-harness/src/profile/paths.ts.
 */

import { homedir } from "node:os";
import { join } from "node:path";

/** pi's env override for the agent dir (APP_NAME.toUpperCase() + "_CODING_AGENT_DIR"). */
const ENV_AGENT_DIR = "PI_CODING_AGENT_DIR";

/**
 * Expand a leading `~` exactly as pi's expandTildePath does — `~` and `~/…`
 * only, never `~\…`, on every platform. Being more lenient here would look in a
 * different directory than pi resolves from the same variable.
 */
const expandTildePiCompatible = (path: string): string => {
  if (path === "~") return homedir();
  if (path.startsWith("~/")) return homedir() + path.slice(1);
  return path;
};

/** pi's agent config directory — `$PI_CODING_AGENT_DIR` or `~/.pi/agent`. */
export const agentDir = (): string => {
  const fromEnv = process.env[ENV_AGENT_DIR];
  if (fromEnv) return expandTildePiCompatible(fromEnv);
  return join(homedir(), ".pi", "agent");
};

/** Where pi-browser-harness persists the `/browser-target` choice. */
export const targetFilePath = (): string => join(agentDir(), "browser-target.json");