@aexol/spectral 0.7.1 → 0.7.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (219) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/dist/agent/agents.js +1 -1
  3. package/dist/agent/index.js +199 -184
  4. package/dist/commands/serve.js +0 -3
  5. package/dist/designer/data/systems/renault/DESIGN.md +1 -1
  6. package/dist/designer/philosophies.js +668 -0
  7. package/dist/mcp/sampling-handler.js +1 -1
  8. package/dist/memory/commands/status.js +1 -1
  9. package/dist/memory/compaction.js +2 -2
  10. package/dist/memory/config.js +1 -1
  11. package/dist/memory/debug-log.js +1 -1
  12. package/dist/memory/hooks/compaction-hook.js +29 -0
  13. package/dist/memory/index.js +2 -0
  14. package/dist/memory/observer.js +2 -2
  15. package/dist/memory/project-observations-store.js +14 -0
  16. package/dist/memory/tokens.js +1 -1
  17. package/dist/memory/tools/read-project-observations.js +82 -0
  18. package/dist/memory/tools/recall-observation.js +2 -2
  19. package/dist/pi/agent-core/agent-loop.js +501 -0
  20. package/dist/pi/agent-core/agent.js +401 -0
  21. package/dist/pi/agent-core/harness/agent-harness.js +899 -0
  22. package/dist/pi/agent-core/harness/compaction/branch-summarization.js +173 -0
  23. package/dist/pi/agent-core/harness/compaction/compaction.js +532 -0
  24. package/dist/pi/agent-core/harness/compaction/utils.js +130 -0
  25. package/dist/pi/agent-core/harness/env/nodejs.js +485 -0
  26. package/dist/pi/agent-core/harness/messages.js +101 -0
  27. package/dist/pi/agent-core/harness/prompt-templates.js +229 -0
  28. package/dist/pi/agent-core/harness/session/jsonl-repo.js +100 -0
  29. package/dist/pi/agent-core/harness/session/jsonl-storage.js +230 -0
  30. package/dist/pi/agent-core/harness/session/memory-repo.js +41 -0
  31. package/dist/pi/agent-core/harness/session/memory-storage.js +113 -0
  32. package/dist/pi/agent-core/harness/session/repo-utils.js +38 -0
  33. package/dist/pi/agent-core/harness/session/session.js +196 -0
  34. package/dist/pi/agent-core/harness/session/uuid.js +49 -0
  35. package/dist/pi/agent-core/harness/skills.js +310 -0
  36. package/dist/pi/agent-core/harness/system-prompt.js +29 -0
  37. package/dist/pi/agent-core/harness/types.js +93 -0
  38. package/dist/pi/agent-core/harness/utils/shell-output.js +125 -0
  39. package/dist/pi/agent-core/harness/utils/truncate.js +289 -0
  40. package/dist/pi/agent-core/index.js +24 -0
  41. package/dist/pi/agent-core/node.js +2 -0
  42. package/dist/pi/agent-core/proxy.js +277 -0
  43. package/dist/pi/agent-core/types.js +1 -0
  44. package/dist/pi/ai/api-registry.js +43 -0
  45. package/dist/pi/ai/cli.js +120 -0
  46. package/dist/pi/ai/env-api-keys.js +169 -0
  47. package/dist/pi/ai/image-models.generated.js +441 -0
  48. package/dist/pi/ai/image-models.js +22 -0
  49. package/dist/pi/ai/images-api-registry.js +21 -0
  50. package/dist/pi/ai/images.js +13 -0
  51. package/dist/pi/ai/index.js +18 -0
  52. package/dist/pi/ai/models.generated.js +16220 -0
  53. package/dist/pi/ai/models.js +70 -0
  54. package/dist/pi/ai/oauth.js +1 -0
  55. package/dist/pi/ai/providers/anthropic.js +945 -0
  56. package/dist/pi/ai/providers/faux.js +367 -0
  57. package/dist/pi/ai/providers/github-copilot-headers.js +28 -0
  58. package/dist/pi/ai/providers/openai-completions.js +945 -0
  59. package/dist/pi/ai/providers/openai-prompt-cache.js +9 -0
  60. package/dist/pi/ai/providers/register-builtins.js +97 -0
  61. package/dist/pi/ai/providers/simple-options.js +40 -0
  62. package/dist/pi/ai/providers/transform-messages.js +183 -0
  63. package/dist/pi/ai/session-resources.js +21 -0
  64. package/dist/pi/ai/stream.js +26 -0
  65. package/dist/pi/ai/types.js +1 -0
  66. package/dist/pi/ai/utils/diagnostics.js +24 -0
  67. package/dist/pi/ai/utils/event-stream.js +80 -0
  68. package/dist/pi/ai/utils/hash.js +13 -0
  69. package/dist/pi/ai/utils/headers.js +7 -0
  70. package/dist/pi/ai/utils/json-parse.js +112 -0
  71. package/dist/pi/ai/utils/node-http-proxy.js +96 -0
  72. package/dist/pi/ai/utils/oauth/anthropic.js +334 -0
  73. package/dist/pi/ai/utils/oauth/device-code.js +54 -0
  74. package/dist/pi/ai/utils/oauth/github-copilot.js +270 -0
  75. package/dist/pi/ai/utils/oauth/index.js +121 -0
  76. package/dist/pi/ai/utils/oauth/oauth-page.js +104 -0
  77. package/dist/pi/ai/utils/oauth/openai-codex.js +384 -0
  78. package/dist/pi/ai/utils/oauth/pkce.js +30 -0
  79. package/dist/pi/ai/utils/oauth/types.js +1 -0
  80. package/dist/pi/ai/utils/overflow.js +150 -0
  81. package/dist/pi/ai/utils/sanitize-unicode.js +25 -0
  82. package/dist/pi/ai/utils/typebox-helpers.js +20 -0
  83. package/dist/pi/ai/utils/validation.js +280 -0
  84. package/dist/pi/coding-agent/bun/cli.js +7 -0
  85. package/dist/pi/coding-agent/bun/restore-sandbox-env.js +31 -0
  86. package/dist/pi/coding-agent/cli/args.js +340 -0
  87. package/dist/pi/coding-agent/cli/file-processor.js +82 -0
  88. package/dist/pi/coding-agent/cli/initial-message.js +21 -0
  89. package/dist/pi/coding-agent/cli.js +17 -0
  90. package/dist/pi/coding-agent/config.js +414 -0
  91. package/dist/pi/coding-agent/core/agent-session-runtime.js +299 -0
  92. package/dist/pi/coding-agent/core/agent-session-services.js +117 -0
  93. package/dist/pi/coding-agent/core/agent-session.js +2498 -0
  94. package/dist/pi/coding-agent/core/auth-guidance.js +20 -0
  95. package/dist/pi/coding-agent/core/auth-storage.js +441 -0
  96. package/dist/pi/coding-agent/core/bash-executor.js +110 -0
  97. package/dist/pi/coding-agent/core/compaction/branch-summarization.js +242 -0
  98. package/dist/pi/coding-agent/core/compaction/compaction.js +624 -0
  99. package/dist/pi/coding-agent/core/compaction/index.js +6 -0
  100. package/dist/pi/coding-agent/core/compaction/utils.js +152 -0
  101. package/dist/pi/coding-agent/core/defaults.js +1 -0
  102. package/dist/pi/coding-agent/core/diagnostics.js +1 -0
  103. package/dist/pi/coding-agent/core/event-bus.js +24 -0
  104. package/dist/pi/coding-agent/core/exec.js +74 -0
  105. package/dist/pi/coding-agent/core/export-html/ansi-to-html.js +248 -0
  106. package/dist/pi/coding-agent/core/export-html/index.js +225 -0
  107. package/dist/pi/coding-agent/core/export-html/tool-renderer.js +107 -0
  108. package/dist/pi/coding-agent/core/extensions/index.js +8 -0
  109. package/dist/pi/coding-agent/core/extensions/loader.js +485 -0
  110. package/dist/pi/coding-agent/core/extensions/runner.js +824 -0
  111. package/dist/pi/coding-agent/core/extensions/types.js +44 -0
  112. package/dist/pi/coding-agent/core/extensions/wrapper.js +21 -0
  113. package/dist/pi/coding-agent/core/footer-data-provider.js +309 -0
  114. package/dist/pi/coding-agent/core/http-dispatcher.js +47 -0
  115. package/dist/pi/coding-agent/core/index.js +11 -0
  116. package/dist/pi/coding-agent/core/keybindings.js +294 -0
  117. package/dist/pi/coding-agent/core/messages.js +122 -0
  118. package/dist/pi/coding-agent/core/model-registry.js +728 -0
  119. package/dist/pi/coding-agent/core/model-resolver.js +494 -0
  120. package/dist/pi/coding-agent/core/output-guard.js +58 -0
  121. package/dist/pi/coding-agent/core/package-manager.js +2020 -0
  122. package/dist/pi/coding-agent/core/prompt-templates.js +237 -0
  123. package/dist/pi/coding-agent/core/provider-display-names.js +32 -0
  124. package/dist/pi/coding-agent/core/resolve-config-value.js +125 -0
  125. package/dist/pi/coding-agent/core/resource-loader.js +733 -0
  126. package/dist/pi/coding-agent/core/sdk.js +282 -0
  127. package/dist/pi/coding-agent/core/session-cwd.js +37 -0
  128. package/dist/pi/coding-agent/core/session-manager.js +1146 -0
  129. package/dist/pi/coding-agent/core/settings-manager.js +794 -0
  130. package/dist/pi/coding-agent/core/skills.js +386 -0
  131. package/dist/pi/coding-agent/core/slash-commands.js +24 -0
  132. package/dist/pi/coding-agent/core/source-info.js +18 -0
  133. package/dist/pi/coding-agent/core/system-prompt.js +122 -0
  134. package/dist/pi/coding-agent/core/telemetry.js +8 -0
  135. package/dist/pi/coding-agent/core/timings.js +30 -0
  136. package/dist/pi/coding-agent/core/tools/bash.js +341 -0
  137. package/dist/pi/coding-agent/core/tools/edit-diff.js +344 -0
  138. package/dist/pi/coding-agent/core/tools/edit.js +324 -0
  139. package/dist/pi/coding-agent/core/tools/file-mutation-queue.js +36 -0
  140. package/dist/pi/coding-agent/core/tools/find.js +297 -0
  141. package/dist/pi/coding-agent/core/tools/grep.js +303 -0
  142. package/dist/pi/coding-agent/core/tools/index.js +111 -0
  143. package/dist/pi/coding-agent/core/tools/ls.js +168 -0
  144. package/dist/pi/coding-agent/core/tools/output-accumulator.js +183 -0
  145. package/dist/pi/coding-agent/core/tools/path-utils.js +61 -0
  146. package/dist/pi/coding-agent/core/tools/read.js +288 -0
  147. package/dist/pi/coding-agent/core/tools/render-utils.js +48 -0
  148. package/dist/pi/coding-agent/core/tools/tool-definition-wrapper.js +33 -0
  149. package/dist/pi/coding-agent/core/tools/truncate.js +214 -0
  150. package/dist/pi/coding-agent/core/tools/write.js +212 -0
  151. package/dist/pi/coding-agent/index.js +41 -0
  152. package/dist/pi/coding-agent/main.js +5 -0
  153. package/dist/pi/coding-agent/migrations.js +280 -0
  154. package/dist/pi/coding-agent/modes/index.js +7 -0
  155. package/dist/pi/coding-agent/modes/interactive/components/diff.js +132 -0
  156. package/dist/pi/coding-agent/modes/interactive/components/keybinding-hints.js +35 -0
  157. package/dist/pi/coding-agent/modes/interactive/components/visual-truncate.js +32 -0
  158. package/dist/pi/coding-agent/modes/interactive/interactive-mode.js +3 -0
  159. package/dist/pi/coding-agent/modes/interactive/theme/theme.js +1023 -0
  160. package/dist/pi/coding-agent/modes/print-mode.js +130 -0
  161. package/dist/pi/coding-agent/modes/rpc/jsonl.js +48 -0
  162. package/dist/pi/coding-agent/modes/rpc/rpc-client.js +409 -0
  163. package/dist/pi/coding-agent/modes/rpc/rpc-mode.js +600 -0
  164. package/dist/pi/coding-agent/modes/rpc/rpc-types.js +7 -0
  165. package/dist/pi/coding-agent/utils/ansi.js +51 -0
  166. package/dist/pi/coding-agent/utils/changelog.js +86 -0
  167. package/dist/pi/coding-agent/utils/child-process.js +87 -0
  168. package/dist/pi/coding-agent/utils/clipboard-image.js +244 -0
  169. package/dist/pi/coding-agent/utils/clipboard-native.js +13 -0
  170. package/dist/pi/coding-agent/utils/clipboard.js +116 -0
  171. package/dist/pi/coding-agent/utils/exif-orientation.js +157 -0
  172. package/dist/pi/coding-agent/utils/frontmatter.js +25 -0
  173. package/dist/pi/coding-agent/utils/fs-watch.js +24 -0
  174. package/dist/pi/coding-agent/utils/git.js +162 -0
  175. package/dist/pi/coding-agent/utils/html.js +39 -0
  176. package/dist/pi/coding-agent/utils/image-convert.js +38 -0
  177. package/dist/pi/coding-agent/utils/image-resize.js +136 -0
  178. package/dist/pi/coding-agent/utils/mime.js +68 -0
  179. package/dist/pi/coding-agent/utils/paths.js +91 -0
  180. package/dist/pi/coding-agent/utils/photon.js +120 -0
  181. package/dist/pi/coding-agent/utils/pi-user-agent.js +4 -0
  182. package/dist/pi/coding-agent/utils/shell.js +194 -0
  183. package/dist/pi/coding-agent/utils/sleep.js +16 -0
  184. package/dist/pi/coding-agent/utils/syntax-highlight.js +117 -0
  185. package/dist/pi/coding-agent/utils/tools-manager.js +327 -0
  186. package/dist/pi/coding-agent/utils/version-check.js +81 -0
  187. package/dist/pi/coding-agent/utils/windows-self-update.js +76 -0
  188. package/dist/pi/tui/autocomplete.js +631 -0
  189. package/dist/pi/tui/components/box.js +103 -0
  190. package/dist/pi/tui/components/cancellable-loader.js +34 -0
  191. package/dist/pi/tui/components/editor.js +1915 -0
  192. package/dist/pi/tui/components/image.js +88 -0
  193. package/dist/pi/tui/components/input.js +425 -0
  194. package/dist/pi/tui/components/loader.js +68 -0
  195. package/dist/pi/tui/components/markdown.js +633 -0
  196. package/dist/pi/tui/components/select-list.js +158 -0
  197. package/dist/pi/tui/components/settings-list.js +184 -0
  198. package/dist/pi/tui/components/spacer.js +22 -0
  199. package/dist/pi/tui/components/text.js +88 -0
  200. package/dist/pi/tui/components/truncated-text.js +50 -0
  201. package/dist/pi/tui/editor-component.js +1 -0
  202. package/dist/pi/tui/fuzzy.js +109 -0
  203. package/dist/pi/tui/index.js +31 -0
  204. package/dist/pi/tui/keybindings.js +173 -0
  205. package/dist/pi/tui/keys.js +1172 -0
  206. package/dist/pi/tui/kill-ring.js +43 -0
  207. package/dist/pi/tui/stdin-buffer.js +360 -0
  208. package/dist/pi/tui/terminal-image.js +335 -0
  209. package/dist/pi/tui/terminal.js +324 -0
  210. package/dist/pi/tui/tui.js +1076 -0
  211. package/dist/pi/tui/undo-stack.js +24 -0
  212. package/dist/pi/tui/utils.js +1016 -0
  213. package/dist/relay/dispatcher.js +30 -0
  214. package/dist/server/handlers/queue.js +52 -0
  215. package/dist/server/pi-bridge.js +9 -1
  216. package/dist/server/session-stream.js +76 -111
  217. package/dist/server/storage.js +154 -2
  218. package/dist/server/title-generator.js +14 -153
  219. package/package.json +24 -6
@@ -0,0 +1,82 @@
1
+ /**
2
+ * Process @file CLI arguments into text content and image attachments
3
+ */
4
+ import { access, readFile, stat } from "node:fs/promises";
5
+ import chalk from "chalk";
6
+ import { resolve } from "path";
7
+ import { resolveReadPath } from "../core/tools/path-utils.js";
8
+ import { formatDimensionNote, resizeImage } from "../utils/image-resize.js";
9
+ import { detectSupportedImageMimeTypeFromFile } from "../utils/mime.js";
10
+ /** Process @file arguments into text content and image attachments */
11
+ export async function processFileArguments(fileArgs, options) {
12
+ const autoResizeImages = options?.autoResizeImages ?? true;
13
+ let text = "";
14
+ const images = [];
15
+ for (const fileArg of fileArgs) {
16
+ // Expand and resolve path (handles ~ expansion and macOS screenshot Unicode spaces)
17
+ const absolutePath = resolve(resolveReadPath(fileArg, process.cwd()));
18
+ // Check if file exists
19
+ try {
20
+ await access(absolutePath);
21
+ }
22
+ catch {
23
+ console.error(chalk.red(`Error: File not found: ${absolutePath}`));
24
+ process.exit(1);
25
+ }
26
+ // Check if file is empty
27
+ const stats = await stat(absolutePath);
28
+ if (stats.size === 0) {
29
+ // Skip empty files
30
+ continue;
31
+ }
32
+ const mimeType = await detectSupportedImageMimeTypeFromFile(absolutePath);
33
+ if (mimeType) {
34
+ // Handle image file
35
+ const content = await readFile(absolutePath);
36
+ const base64Content = content.toString("base64");
37
+ let attachment;
38
+ let dimensionNote;
39
+ if (autoResizeImages) {
40
+ const resized = await resizeImage({ type: "image", data: base64Content, mimeType });
41
+ if (!resized) {
42
+ text += `<file name="${absolutePath}">[Image omitted: could not be resized below the inline image size limit.]</file>\n`;
43
+ continue;
44
+ }
45
+ dimensionNote = formatDimensionNote(resized);
46
+ attachment = {
47
+ type: "image",
48
+ mimeType: resized.mimeType,
49
+ data: resized.data,
50
+ };
51
+ }
52
+ else {
53
+ attachment = {
54
+ type: "image",
55
+ mimeType,
56
+ data: base64Content,
57
+ };
58
+ }
59
+ images.push(attachment);
60
+ // Add text reference to image with optional dimension note
61
+ if (dimensionNote) {
62
+ text += `<file name="${absolutePath}">${dimensionNote}</file>\n`;
63
+ }
64
+ else {
65
+ text += `<file name="${absolutePath}"></file>\n`;
66
+ }
67
+ }
68
+ else {
69
+ // Handle text file
70
+ try {
71
+ const content = await readFile(absolutePath, "utf-8");
72
+ text += `<file name="${absolutePath}">\n${content}\n</file>\n`;
73
+ }
74
+ catch (error) {
75
+ const message = error instanceof Error ? error.message : String(error);
76
+ console.error(chalk.red(`Error: Could not read file ${absolutePath}: ${message}`));
77
+ process.exit(1);
78
+ }
79
+ }
80
+ }
81
+ return { text, images };
82
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Combine stdin content, @file text, and the first CLI message into a single
3
+ * initial prompt for non-interactive mode.
4
+ */
5
+ export function buildInitialMessage({ parsed, fileText, fileImages, stdinContent, }) {
6
+ const parts = [];
7
+ if (stdinContent !== undefined) {
8
+ parts.push(stdinContent);
9
+ }
10
+ if (fileText) {
11
+ parts.push(fileText);
12
+ }
13
+ if (parsed.messages.length > 0) {
14
+ parts.push(parsed.messages[0]);
15
+ parsed.messages.shift();
16
+ }
17
+ return {
18
+ initialMessage: parts.length > 0 ? parts.join("") : undefined,
19
+ initialImages: fileImages && fileImages.length > 0 ? fileImages : undefined,
20
+ };
21
+ }
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * CLI entry point for the refactored coding agent.
4
+ * Uses main.ts with AgentSession and new mode modules.
5
+ *
6
+ * Test with: npx tsx src/cli-new.ts [args...]
7
+ */
8
+ import { APP_NAME } from "./config.js";
9
+ import { configureHttpDispatcher } from "./core/http-dispatcher.js";
10
+ import { main } from "./main.js";
11
+ process.title = APP_NAME;
12
+ process.env.PI_CODING_AGENT = "true";
13
+ process.emitWarning = (() => { });
14
+ // Configure undici's global dispatcher before provider SDKs issue requests.
15
+ // Runtime settings are applied once SettingsManager has loaded global/project settings.
16
+ configureHttpDispatcher();
17
+ main(process.argv.slice(2));
@@ -0,0 +1,414 @@
1
+ import { accessSync, constants, existsSync, readFileSync, realpathSync } from "fs";
2
+ import { homedir } from "os";
3
+ import { basename, dirname, join, resolve, sep, win32 } from "path";
4
+ import { fileURLToPath } from "url";
5
+ import { spawnProcessSync } from "./utils/child-process.js";
6
+ import { normalizePath } from "./utils/paths.js";
7
+ // =============================================================================
8
+ // Package Detection
9
+ // =============================================================================
10
+ const __filename = fileURLToPath(import.meta.url);
11
+ const __dirname = dirname(__filename);
12
+ /**
13
+ * Detect if we're running as a Bun compiled binary.
14
+ * Bun binaries have import.meta.url containing "$bunfs", "~BUN", or "%7EBUN" (Bun's virtual filesystem path)
15
+ */
16
+ export const isBunBinary = import.meta.url.includes("$bunfs") || import.meta.url.includes("~BUN") || import.meta.url.includes("%7EBUN");
17
+ /** Detect if Bun is the runtime (compiled binary or bun run) */
18
+ export const isBunRuntime = !!process.versions.bun;
19
+ function makeSelfUpdateCommand(installStep, uninstallStep) {
20
+ if (!uninstallStep)
21
+ return installStep;
22
+ return {
23
+ ...installStep,
24
+ display: `${uninstallStep.display} && ${installStep.display}`,
25
+ steps: [uninstallStep, installStep],
26
+ };
27
+ }
28
+ function makeSelfUpdateCommandStep(command, args) {
29
+ return {
30
+ command,
31
+ args,
32
+ display: [command, ...args].map((arg) => (/\s/.test(arg) ? `"${arg}"` : arg)).join(" "),
33
+ };
34
+ }
35
+ export function detectInstallMethod() {
36
+ if (isBunBinary) {
37
+ return "bun-binary";
38
+ }
39
+ const resolvedPath = `${__dirname}\0${process.execPath || ""}`.toLowerCase().replace(/\\/g, "/");
40
+ if (resolvedPath.includes("/pnpm/") || resolvedPath.includes("/.pnpm/")) {
41
+ return "pnpm";
42
+ }
43
+ if (resolvedPath.includes("/yarn/") || resolvedPath.includes("/.yarn/")) {
44
+ return "yarn";
45
+ }
46
+ if (isBunRuntime || resolvedPath.includes("/install/global/node_modules/")) {
47
+ return "bun";
48
+ }
49
+ if (resolvedPath.includes("/npm/") || resolvedPath.includes("/node_modules/")) {
50
+ return "npm";
51
+ }
52
+ return "unknown";
53
+ }
54
+ function getInferredNpmInstall() {
55
+ const packageDir = getPackageDir();
56
+ const path = process.platform === "win32" || packageDir.includes("\\") ? win32 : { basename, dirname };
57
+ const parent = path.dirname(packageDir);
58
+ let root;
59
+ if (path.basename(parent).startsWith("@") && path.basename(path.dirname(parent)) === "node_modules") {
60
+ root = path.dirname(parent);
61
+ }
62
+ else if (path.basename(parent) === "node_modules") {
63
+ root = parent;
64
+ }
65
+ if (!root)
66
+ return undefined;
67
+ const rootParent = path.dirname(root);
68
+ if (path.basename(rootParent) === "lib")
69
+ return { root, prefix: path.dirname(rootParent) };
70
+ // Windows global npm prefixes use `<prefix>\\node_modules`, which is
71
+ // indistinguishable from local project installs by path shape alone. Do not
72
+ // infer unsupported Windows custom prefixes without `npm root -g` evidence.
73
+ return undefined;
74
+ }
75
+ function getSelfUpdateCommandForMethod(method, installedPackageName, updatePackageName = installedPackageName, npmCommand) {
76
+ switch (method) {
77
+ case "bun-binary":
78
+ return undefined;
79
+ case "pnpm":
80
+ return makeSelfUpdateCommand(makeSelfUpdateCommandStep("pnpm", ["install", "-g", "--ignore-scripts", updatePackageName]), updatePackageName === installedPackageName
81
+ ? undefined
82
+ : makeSelfUpdateCommandStep("pnpm", ["remove", "-g", installedPackageName]));
83
+ case "yarn":
84
+ return makeSelfUpdateCommand(makeSelfUpdateCommandStep("yarn", ["global", "add", "--ignore-scripts", updatePackageName]), updatePackageName === installedPackageName
85
+ ? undefined
86
+ : makeSelfUpdateCommandStep("yarn", ["global", "remove", installedPackageName]));
87
+ case "bun":
88
+ return makeSelfUpdateCommand(makeSelfUpdateCommandStep("bun", ["install", "-g", "--ignore-scripts", updatePackageName]), updatePackageName === installedPackageName
89
+ ? undefined
90
+ : makeSelfUpdateCommandStep("bun", ["uninstall", "-g", installedPackageName]));
91
+ case "npm": {
92
+ const [command = "npm", ...npmArgs] = npmCommand ?? [];
93
+ const inferred = npmCommand?.length ? undefined : getInferredNpmInstall();
94
+ const prefixArgs = [...npmArgs, ...(inferred ? ["--prefix", inferred.prefix] : [])];
95
+ const installStep = makeSelfUpdateCommandStep(command, [
96
+ ...prefixArgs,
97
+ "install",
98
+ "-g",
99
+ "--ignore-scripts",
100
+ updatePackageName,
101
+ ]);
102
+ const uninstallStep = updatePackageName === installedPackageName
103
+ ? undefined
104
+ : makeSelfUpdateCommandStep(command, [...prefixArgs, "uninstall", "-g", installedPackageName]);
105
+ return makeSelfUpdateCommand(installStep, uninstallStep);
106
+ }
107
+ case "unknown":
108
+ return undefined;
109
+ }
110
+ }
111
+ function readCommandOutput(command, args, options = {}) {
112
+ const result = spawnProcessSync(command, args, {
113
+ encoding: "utf-8",
114
+ stdio: ["ignore", "pipe", "pipe"],
115
+ });
116
+ if (result.status === 0)
117
+ return result.stdout.trim() || undefined;
118
+ if (options.requireSuccess) {
119
+ const reason = result.error?.message || result.stderr.trim() || `exit code ${result.status ?? "unknown"}`;
120
+ throw new Error(`Failed to run ${[command, ...args].join(" ")}: ${reason}`);
121
+ }
122
+ return undefined;
123
+ }
124
+ function getGlobalPackageRoots(method, _packageName, npmCommand) {
125
+ switch (method) {
126
+ case "npm": {
127
+ const configured = !!npmCommand?.length;
128
+ const [command = "npm", ...npmArgs] = npmCommand ?? [];
129
+ if (configured && command === "bun") {
130
+ const bunBin = readCommandOutput(command, [...npmArgs, "pm", "bin", "-g"], {
131
+ requireSuccess: true,
132
+ });
133
+ const roots = [join(homedir(), ".bun", "install", "global", "node_modules")];
134
+ if (bunBin) {
135
+ roots.push(join(dirname(bunBin), "install", "global", "node_modules"));
136
+ }
137
+ return roots;
138
+ }
139
+ const root = readCommandOutput(command, [...npmArgs, "root", "-g"], {
140
+ requireSuccess: configured,
141
+ });
142
+ const inferred = configured ? undefined : getInferredNpmInstall();
143
+ return [root, inferred?.root].filter((x) => !!x);
144
+ }
145
+ case "pnpm": {
146
+ const root = readCommandOutput("pnpm", ["root", "-g"]);
147
+ return root ? [root, dirname(root)] : [];
148
+ }
149
+ case "yarn": {
150
+ const dir = readCommandOutput("yarn", ["global", "dir"]);
151
+ return dir ? [dir, join(dir, "node_modules")] : [];
152
+ }
153
+ case "bun": {
154
+ const bunBin = readCommandOutput("bun", ["pm", "bin", "-g"]);
155
+ const roots = [join(homedir(), ".bun", "install", "global", "node_modules")];
156
+ if (bunBin) {
157
+ roots.push(join(dirname(bunBin), "install", "global", "node_modules"));
158
+ }
159
+ return roots;
160
+ }
161
+ case "bun-binary":
162
+ case "unknown":
163
+ return [];
164
+ }
165
+ }
166
+ function normalizeExistingPathForComparison(path, resolveSymlinks) {
167
+ const resolvedPath = resolve(path);
168
+ if (!existsSync(resolvedPath)) {
169
+ return undefined;
170
+ }
171
+ let normalizedPath = resolvedPath;
172
+ if (resolveSymlinks) {
173
+ try {
174
+ normalizedPath = realpathSync(resolvedPath);
175
+ }
176
+ catch {
177
+ return undefined;
178
+ }
179
+ }
180
+ if (process.platform === "win32") {
181
+ normalizedPath = normalizedPath.toLowerCase();
182
+ }
183
+ return normalizedPath;
184
+ }
185
+ function getPathComparisonCandidates(path) {
186
+ return Array.from(new Set([normalizeExistingPathForComparison(path, false), normalizeExistingPathForComparison(path, true)].filter((candidate) => !!candidate)));
187
+ }
188
+ function getEntrypointPackageDir() {
189
+ const entrypoint = process.argv[1];
190
+ if (!entrypoint)
191
+ return undefined;
192
+ let dir = dirname(entrypoint);
193
+ while (dir !== dirname(dir)) {
194
+ if (existsSync(join(dir, "package.json"))) {
195
+ return dir;
196
+ }
197
+ dir = dirname(dir);
198
+ }
199
+ return undefined;
200
+ }
201
+ function isSelfUpdatePathWritable() {
202
+ const packageDir = getPackageDir();
203
+ try {
204
+ accessSync(packageDir, constants.W_OK);
205
+ accessSync(dirname(packageDir), constants.W_OK);
206
+ return true;
207
+ }
208
+ catch {
209
+ return false;
210
+ }
211
+ }
212
+ function isManagedByGlobalPackageManager(method, packageName, npmCommand) {
213
+ const packageDirs = [getPackageDir(), getEntrypointPackageDir()].filter((dir) => !!dir);
214
+ const packageDirCandidates = packageDirs.flatMap((dir) => getPathComparisonCandidates(dir));
215
+ return getGlobalPackageRoots(method, packageName, npmCommand).some((root) => {
216
+ return getPathComparisonCandidates(root).some((normalizedRoot) => {
217
+ const rootPrefix = normalizedRoot.endsWith(sep) ? normalizedRoot : `${normalizedRoot}${sep}`;
218
+ return packageDirCandidates.some((packageDir) => packageDir.startsWith(rootPrefix));
219
+ });
220
+ });
221
+ }
222
+ export function getSelfUpdateCommand(packageName, npmCommand, updatePackageName = packageName) {
223
+ const method = detectInstallMethod();
224
+ const command = getSelfUpdateCommandForMethod(method, packageName, updatePackageName, npmCommand);
225
+ if (!command || !isManagedByGlobalPackageManager(method, packageName, npmCommand) || !isSelfUpdatePathWritable()) {
226
+ return undefined;
227
+ }
228
+ return command;
229
+ }
230
+ export function getSelfUpdateUnavailableInstruction(packageName, npmCommand, updatePackageName = packageName) {
231
+ const method = detectInstallMethod();
232
+ if (method === "bun-binary") {
233
+ return `Download from: https://github.com/earendil-works/pi-mono/releases/latest`;
234
+ }
235
+ const command = getSelfUpdateCommandForMethod(method, packageName, updatePackageName, npmCommand);
236
+ if (command) {
237
+ if (isManagedByGlobalPackageManager(method, packageName, npmCommand) && !isSelfUpdatePathWritable()) {
238
+ return `This installation is managed by a global ${method} install, but the install path is not writable. Update it yourself with: ${command.display}`;
239
+ }
240
+ return `This installation is not managed by a global ${method} install. Update it with the package manager, wrapper, or source checkout that provides it.`;
241
+ }
242
+ return `Update ${updatePackageName} using the package manager, wrapper, or source checkout that provides this installation.`;
243
+ }
244
+ export function getUpdateInstruction(packageName) {
245
+ const method = detectInstallMethod();
246
+ const command = getSelfUpdateCommandForMethod(method, packageName);
247
+ if (command) {
248
+ return `Run: ${command.display}`;
249
+ }
250
+ return getSelfUpdateUnavailableInstruction(packageName);
251
+ }
252
+ // =============================================================================
253
+ // Package Asset Paths (shipped with executable)
254
+ // =============================================================================
255
+ /**
256
+ * Get the base directory for resolving package assets (themes, package.json, README.md, CHANGELOG.md).
257
+ * - For Bun binary: returns the directory containing the executable
258
+ * - For Node.js (dist/): returns __dirname (the dist/ directory)
259
+ * - For tsx (src/): returns parent directory (the package root)
260
+ */
261
+ export function getPackageDir() {
262
+ // Allow override via environment variable (useful for Nix/Guix where store paths tokenize poorly)
263
+ const envDir = process.env.PI_PACKAGE_DIR;
264
+ if (envDir) {
265
+ return normalizePath(envDir);
266
+ }
267
+ if (isBunBinary) {
268
+ // Bun binary: process.execPath points to the compiled executable
269
+ return dirname(process.execPath);
270
+ }
271
+ // Node.js: walk up from __dirname until we find package.json
272
+ let dir = __dirname;
273
+ while (dir !== dirname(dir)) {
274
+ if (existsSync(join(dir, "package.json"))) {
275
+ return dir;
276
+ }
277
+ dir = dirname(dir);
278
+ }
279
+ // Fallback (shouldn't happen)
280
+ return __dirname;
281
+ }
282
+ /**
283
+ * Get path to built-in themes directory (shipped with package)
284
+ * - For Bun binary: theme/ next to executable
285
+ * - For Node.js (dist/): dist/modes/interactive/theme/
286
+ * - For tsx (src/): src/modes/interactive/theme/
287
+ */
288
+ export function getThemesDir() {
289
+ if (isBunBinary) {
290
+ return join(getPackageDir(), "theme");
291
+ }
292
+ // Theme is in modes/interactive/theme/ relative to src/ or dist/
293
+ const packageDir = getPackageDir();
294
+ const srcOrDist = existsSync(join(packageDir, "src")) ? "src" : "dist";
295
+ return join(packageDir, srcOrDist, "modes", "interactive", "theme");
296
+ }
297
+ /**
298
+ * Get path to HTML export template directory (shipped with package)
299
+ * - For Bun binary: export-html/ next to executable
300
+ * - For Node.js (dist/): dist/core/export-html/
301
+ * - For tsx (src/): src/core/export-html/
302
+ */
303
+ export function getExportTemplateDir() {
304
+ if (isBunBinary) {
305
+ return join(getPackageDir(), "export-html");
306
+ }
307
+ const packageDir = getPackageDir();
308
+ const srcOrDist = existsSync(join(packageDir, "src")) ? "src" : "dist";
309
+ return join(packageDir, srcOrDist, "core", "export-html");
310
+ }
311
+ /** Get path to package.json */
312
+ export function getPackageJsonPath() {
313
+ return join(getPackageDir(), "package.json");
314
+ }
315
+ /** Get path to README.md */
316
+ export function getReadmePath() {
317
+ return resolve(join(getPackageDir(), "README.md"));
318
+ }
319
+ /** Get path to docs directory */
320
+ export function getDocsPath() {
321
+ return resolve(join(getPackageDir(), "docs"));
322
+ }
323
+ /** Get path to examples directory */
324
+ export function getExamplesPath() {
325
+ return resolve(join(getPackageDir(), "examples"));
326
+ }
327
+ /** Get path to CHANGELOG.md */
328
+ export function getChangelogPath() {
329
+ return resolve(join(getPackageDir(), "CHANGELOG.md"));
330
+ }
331
+ /**
332
+ * Get path to built-in interactive assets directory.
333
+ * - For Bun binary: assets/ next to executable
334
+ * - For Node.js (dist/): dist/modes/interactive/assets/
335
+ * - For tsx (src/): src/modes/interactive/assets/
336
+ */
337
+ export function getInteractiveAssetsDir() {
338
+ if (isBunBinary) {
339
+ return join(getPackageDir(), "assets");
340
+ }
341
+ const packageDir = getPackageDir();
342
+ const srcOrDist = existsSync(join(packageDir, "src")) ? "src" : "dist";
343
+ return join(packageDir, srcOrDist, "modes", "interactive", "assets");
344
+ }
345
+ /** Get path to a bundled interactive asset */
346
+ export function getBundledInteractiveAssetPath(name) {
347
+ return join(getInteractiveAssetsDir(), name);
348
+ }
349
+ const pkg = JSON.parse(readFileSync(getPackageJsonPath(), "utf-8"));
350
+ const piConfigName = pkg.piConfig?.name;
351
+ export const PACKAGE_NAME = pkg.name || "index.ts";
352
+ export const APP_NAME = piConfigName || "pi";
353
+ export const APP_TITLE = piConfigName ? APP_NAME : "π";
354
+ export const CONFIG_DIR_NAME = pkg.piConfig?.configDir || ".pi";
355
+ export const VERSION = pkg.version || "0.0.0";
356
+ // e.g., PI_CODING_AGENT_DIR or TAU_CODING_AGENT_DIR
357
+ export const ENV_AGENT_DIR = `${APP_NAME.toUpperCase()}_CODING_AGENT_DIR`;
358
+ export const ENV_SESSION_DIR = `${APP_NAME.toUpperCase()}_CODING_AGENT_SESSION_DIR`;
359
+ export function expandTildePath(path) {
360
+ return normalizePath(path);
361
+ }
362
+ const DEFAULT_SHARE_VIEWER_URL = "https://pi.dev/session/";
363
+ /** Get the share viewer URL for a gist ID */
364
+ export function getShareViewerUrl(gistId) {
365
+ const baseUrl = process.env.PI_SHARE_VIEWER_URL || DEFAULT_SHARE_VIEWER_URL;
366
+ return `${baseUrl}#${gistId}`;
367
+ }
368
+ // =============================================================================
369
+ // User Config Paths (~/.pi/agent/*)
370
+ // =============================================================================
371
+ /** Get the agent config directory (e.g., ~/.pi/agent/) */
372
+ export function getAgentDir() {
373
+ const envDir = process.env[ENV_AGENT_DIR];
374
+ if (envDir) {
375
+ return expandTildePath(envDir);
376
+ }
377
+ return join(homedir(), CONFIG_DIR_NAME, "agent");
378
+ }
379
+ /** Get path to user's custom themes directory */
380
+ export function getCustomThemesDir() {
381
+ return join(getAgentDir(), "themes");
382
+ }
383
+ /** Get path to models.json */
384
+ export function getModelsPath() {
385
+ return join(getAgentDir(), "models.json");
386
+ }
387
+ /** Get path to auth.json */
388
+ export function getAuthPath() {
389
+ return join(getAgentDir(), "auth.json");
390
+ }
391
+ /** Get path to settings.json */
392
+ export function getSettingsPath() {
393
+ return join(getAgentDir(), "settings.json");
394
+ }
395
+ /** Get path to tools directory */
396
+ export function getToolsDir() {
397
+ return join(getAgentDir(), "tools");
398
+ }
399
+ /** Get path to managed binaries directory (fd, rg) */
400
+ export function getBinDir() {
401
+ return join(getAgentDir(), "bin");
402
+ }
403
+ /** Get path to prompt templates directory */
404
+ export function getPromptsDir() {
405
+ return join(getAgentDir(), "prompts");
406
+ }
407
+ /** Get path to sessions directory */
408
+ export function getSessionsDir() {
409
+ return join(getAgentDir(), "sessions");
410
+ }
411
+ /** Get path to debug log file */
412
+ export function getDebugLogPath() {
413
+ return join(getAgentDir(), `${APP_NAME}-debug.log`);
414
+ }