@pi-unipi/core 2.2.0 → 2.4.0

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 (2) hide show
  1. package/package.json +1 -1
  2. package/utils.ts +37 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-unipi/core",
3
- "version": "2.2.0",
3
+ "version": "2.4.0",
4
4
  "description": "Shared utilities, event types, and constants for Unipi extension suite",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/utils.ts CHANGED
@@ -187,6 +187,43 @@ export function getInstalledPackageVersion(startDir: string, packageName: string
187
187
  return getPackageVersion(root);
188
188
  }
189
189
 
190
+ /** Cached pi version — resolved at most once per process. */
191
+ let cachedPiVersion: string | null = null;
192
+
193
+ /**
194
+ * Get the running Pi agent's version.
195
+ *
196
+ * Resolves by walking up from Pi's own entry point (`process.argv[1]`), which
197
+ * must be `realpath`'d first: the executable on PATH is typically a symlink
198
+ * (e.g. mise shims `~/.local/share/mise/installs/node/lts/bin/pi`), and the
199
+ * package.json lives next to the *real* `dist/cli.js`, not the link.
200
+ *
201
+ * Never spawns a subprocess. A previous implementation fell back to
202
+ * `execSync("pi --version")`, which cost ~350ms per call and still returned
203
+ * "unknown" because it matched against a `v` prefix that Pi no longer emits.
204
+ */
205
+ export function getPiVersion(): string {
206
+ if (cachedPiVersion !== null) return cachedPiVersion;
207
+
208
+ const PI_PACKAGE = "@earendil-works/pi-coding-agent";
209
+ const entry = process.argv[1];
210
+ if (entry) {
211
+ try {
212
+ const realEntry = fs.realpathSync(entry);
213
+ const root = findPackageRoot(path.dirname(realEntry), PI_PACKAGE);
214
+ if (root) {
215
+ cachedPiVersion = getPackageVersion(root);
216
+ return cachedPiVersion;
217
+ }
218
+ } catch {
219
+ // Fall through to "unknown".
220
+ }
221
+ }
222
+
223
+ cachedPiVersion = "unknown";
224
+ return cachedPiVersion;
225
+ }
226
+
190
227
  /**
191
228
  * Check if a module is available in node_modules.
192
229
  */