version.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. /**
  2. * Resolved package version, computed once at module load.
  3. *
  4. * The version string is the rendezvous datum between cooperating daemon and
  5. * proxy processes: the daemon advertises its version in the hello line, and
  6. * the proxy refuses to share IPC across a mismatch (falls back to direct
  7. * mode). Keeping the resolution in one place avoids drift between the CLI
  8. * `--version` output (which reads `package.json` directly) and the daemon
  9. * handshake.
  10. *
  11. * Resolution strategy: read the bundled `package.json` two levels up from
  12. * this file — same relative position whether we're loaded from `src/mcp/` or
  13. * the `dist/mcp/` output, since `tsc` preserves the layout. If reading fails
  14. * (e.g. the package was unpacked oddly), fall back to "0.0.0-unknown" — a
  15. * sentinel that will never match a real version, so the proxy harmlessly
  16. * falls back to direct mode.
  17. */
  18. import * as fs from 'fs';
  19. import * as path from 'path';
  20. function readPackageVersion(): string {
  21. try {
  22. const pkgPath = path.join(__dirname, '..', '..', 'package.json');
  23. const raw = fs.readFileSync(pkgPath, 'utf8');
  24. const parsed = JSON.parse(raw);
  25. if (typeof parsed?.version === 'string' && parsed.version.length > 0) {
  26. return parsed.version;
  27. }
  28. } catch {
  29. // Fall through to sentinel.
  30. }
  31. return '0.0.0-unknown';
  32. }
  33. export const CodeGraphPackageVersion = readPackageVersion();