@angular-modernizer/adapter-mcp 0.1.4 → 0.3.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 (152) hide show
  1. package/README.md +125 -35
  2. package/dist/cli.js +65 -13
  3. package/dist/cli.js.map +1 -1
  4. package/dist/index.d.ts +8 -0
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +8 -0
  7. package/dist/index.js.map +1 -1
  8. package/dist/mcp-watcher.d.ts.map +1 -1
  9. package/dist/mcp-watcher.js +3 -0
  10. package/dist/mcp-watcher.js.map +1 -1
  11. package/dist/server/json-rpc-handler.d.ts +41 -0
  12. package/dist/server/json-rpc-handler.d.ts.map +1 -1
  13. package/dist/server/json-rpc-handler.js +85 -6
  14. package/dist/server/json-rpc-handler.js.map +1 -1
  15. package/dist/server/mcp-server.d.ts +31 -0
  16. package/dist/server/mcp-server.d.ts.map +1 -1
  17. package/dist/server/mcp-server.js +76 -23
  18. package/dist/server/mcp-server.js.map +1 -1
  19. package/dist/server/stdio-guard.d.ts +20 -0
  20. package/dist/server/stdio-guard.d.ts.map +1 -0
  21. package/dist/server/stdio-guard.js +48 -0
  22. package/dist/server/stdio-guard.js.map +1 -0
  23. package/dist/tools/analyze-build-impact.d.ts +169 -12
  24. package/dist/tools/analyze-build-impact.d.ts.map +1 -1
  25. package/dist/tools/analyze-build-impact.js +221 -36
  26. package/dist/tools/analyze-build-impact.js.map +1 -1
  27. package/dist/tools/analyze-parser-performance.d.ts.map +1 -1
  28. package/dist/tools/analyze-parser-performance.js +6 -4
  29. package/dist/tools/analyze-parser-performance.js.map +1 -1
  30. package/dist/tools/collect-feedback.d.ts +112 -1
  31. package/dist/tools/collect-feedback.d.ts.map +1 -1
  32. package/dist/tools/collect-feedback.js +240 -11
  33. package/dist/tools/collect-feedback.js.map +1 -1
  34. package/dist/tools/extract-libraries.d.ts +4 -0
  35. package/dist/tools/extract-libraries.d.ts.map +1 -1
  36. package/dist/tools/extract-libraries.js +40 -8
  37. package/dist/tools/extract-libraries.js.map +1 -1
  38. package/dist/tools/find-angular-symbol.d.ts +29 -6
  39. package/dist/tools/find-angular-symbol.d.ts.map +1 -1
  40. package/dist/tools/find-angular-symbol.js +51 -6
  41. package/dist/tools/find-angular-symbol.js.map +1 -1
  42. package/dist/tools/find-component-hosts.d.ts +114 -0
  43. package/dist/tools/find-component-hosts.d.ts.map +1 -0
  44. package/dist/tools/find-component-hosts.js +240 -0
  45. package/dist/tools/find-component-hosts.js.map +1 -0
  46. package/dist/tools/find-usages.d.ts +33 -4
  47. package/dist/tools/find-usages.d.ts.map +1 -1
  48. package/dist/tools/find-usages.js +102 -11
  49. package/dist/tools/find-usages.js.map +1 -1
  50. package/dist/tools/get-bootstrap-execution.d.ts +108 -0
  51. package/dist/tools/get-bootstrap-execution.d.ts.map +1 -0
  52. package/dist/tools/get-bootstrap-execution.js +243 -0
  53. package/dist/tools/get-bootstrap-execution.js.map +1 -0
  54. package/dist/tools/get-call-graph.d.ts +33 -4
  55. package/dist/tools/get-call-graph.d.ts.map +1 -1
  56. package/dist/tools/get-call-graph.js +76 -13
  57. package/dist/tools/get-call-graph.js.map +1 -1
  58. package/dist/tools/helpers/compact-output.d.ts +61 -0
  59. package/dist/tools/helpers/compact-output.d.ts.map +1 -0
  60. package/dist/tools/helpers/compact-output.js +83 -0
  61. package/dist/tools/helpers/compact-output.js.map +1 -0
  62. package/dist/tools/helpers/find-ts-config.d.ts +24 -1
  63. package/dist/tools/helpers/find-ts-config.d.ts.map +1 -1
  64. package/dist/tools/helpers/find-ts-config.js +213 -7
  65. package/dist/tools/helpers/find-ts-config.js.map +1 -1
  66. package/dist/tools/helpers/project-cache.d.ts +177 -0
  67. package/dist/tools/helpers/project-cache.d.ts.map +1 -0
  68. package/dist/tools/helpers/project-cache.js +313 -0
  69. package/dist/tools/helpers/project-cache.js.map +1 -0
  70. package/dist/tools/helpers/rule-runner.d.ts +67 -0
  71. package/dist/tools/helpers/rule-runner.d.ts.map +1 -0
  72. package/dist/tools/helpers/rule-runner.js +117 -0
  73. package/dist/tools/helpers/rule-runner.js.map +1 -0
  74. package/dist/tools/helpers/scan-plan.d.ts +45 -0
  75. package/dist/tools/helpers/scan-plan.d.ts.map +1 -0
  76. package/dist/tools/helpers/scan-plan.js +151 -0
  77. package/dist/tools/helpers/scan-plan.js.map +1 -0
  78. package/dist/tools/helpers/stack-frame-file-index.d.ts +10 -0
  79. package/dist/tools/helpers/stack-frame-file-index.d.ts.map +1 -0
  80. package/dist/tools/helpers/stack-frame-file-index.js +71 -0
  81. package/dist/tools/helpers/stack-frame-file-index.js.map +1 -0
  82. package/dist/tools/helpers/suggestion-generator.d.ts +1 -1
  83. package/dist/tools/helpers/suggestion-generator.d.ts.map +1 -1
  84. package/dist/tools/helpers/suggestion-generator.js.map +1 -1
  85. package/dist/tools/inspect-form-group.d.ts +93 -0
  86. package/dist/tools/inspect-form-group.d.ts.map +1 -0
  87. package/dist/tools/inspect-form-group.js +161 -0
  88. package/dist/tools/inspect-form-group.js.map +1 -0
  89. package/dist/tools/inspect-i18n.d.ts +85 -0
  90. package/dist/tools/inspect-i18n.d.ts.map +1 -0
  91. package/dist/tools/inspect-i18n.js +203 -0
  92. package/dist/tools/inspect-i18n.js.map +1 -0
  93. package/dist/tools/inspect-template.d.ts +89 -0
  94. package/dist/tools/inspect-template.d.ts.map +1 -0
  95. package/dist/tools/inspect-template.js +166 -0
  96. package/dist/tools/inspect-template.js.map +1 -0
  97. package/dist/tools/list-imports.d.ts +119 -0
  98. package/dist/tools/list-imports.d.ts.map +1 -0
  99. package/dist/tools/list-imports.js +171 -0
  100. package/dist/tools/list-imports.js.map +1 -0
  101. package/dist/tools/orchestrate-migration.d.ts +1 -0
  102. package/dist/tools/orchestrate-migration.d.ts.map +1 -1
  103. package/dist/tools/orchestrate-migration.js +33 -1
  104. package/dist/tools/orchestrate-migration.js.map +1 -1
  105. package/dist/tools/parse-stack-trace.d.ts +3 -1
  106. package/dist/tools/parse-stack-trace.d.ts.map +1 -1
  107. package/dist/tools/parse-stack-trace.js +18 -4
  108. package/dist/tools/parse-stack-trace.js.map +1 -1
  109. package/dist/tools/plan-migration.d.ts +89 -1
  110. package/dist/tools/plan-migration.d.ts.map +1 -1
  111. package/dist/tools/plan-migration.js +203 -7
  112. package/dist/tools/plan-migration.js.map +1 -1
  113. package/dist/tools/resolve-type.d.ts +3 -0
  114. package/dist/tools/resolve-type.d.ts.map +1 -1
  115. package/dist/tools/resolve-type.js +6 -2
  116. package/dist/tools/resolve-type.js.map +1 -1
  117. package/dist/tools/review-feedback.d.ts +7 -0
  118. package/dist/tools/review-feedback.d.ts.map +1 -1
  119. package/dist/tools/review-feedback.js +68 -16
  120. package/dist/tools/review-feedback.js.map +1 -1
  121. package/dist/tools/scan-all.d.ts +6 -6
  122. package/dist/tools/scan-all.d.ts.map +1 -1
  123. package/dist/tools/scan-all.js +7 -7
  124. package/dist/tools/scan-all.js.map +1 -1
  125. package/dist/tools/scan-angular.d.ts +1 -1
  126. package/dist/tools/scan-angular.js +1 -1
  127. package/dist/tools/scan-architecture.d.ts +25 -4
  128. package/dist/tools/scan-architecture.d.ts.map +1 -1
  129. package/dist/tools/scan-architecture.js +115 -134
  130. package/dist/tools/scan-architecture.js.map +1 -1
  131. package/dist/tools/scan-solid.d.ts +1 -1
  132. package/dist/tools/scan-solid.js +1 -1
  133. package/dist/tools/search-codebase.d.ts +32 -6
  134. package/dist/tools/search-codebase.d.ts.map +1 -1
  135. package/dist/tools/search-codebase.js +60 -14
  136. package/dist/tools/search-codebase.js.map +1 -1
  137. package/dist/tools/server-status.d.ts +96 -0
  138. package/dist/tools/server-status.d.ts.map +1 -0
  139. package/dist/tools/server-status.js +195 -0
  140. package/dist/tools/server-status.js.map +1 -0
  141. package/dist/tools/suggest-improvements.d.ts +3 -1
  142. package/dist/tools/suggest-improvements.d.ts.map +1 -1
  143. package/dist/tools/suggest-improvements.js +47 -10
  144. package/dist/tools/suggest-improvements.js.map +1 -1
  145. package/dist/tools/trace-output-flow.d.ts +111 -0
  146. package/dist/tools/trace-output-flow.d.ts.map +1 -0
  147. package/dist/tools/trace-output-flow.js +308 -0
  148. package/dist/tools/trace-output-flow.js.map +1 -0
  149. package/dist/types.d.ts +7 -0
  150. package/dist/types.d.ts.map +1 -1
  151. package/dist/types.js.map +1 -1
  152. package/package.json +13 -12
@@ -0,0 +1,83 @@
1
+ /**
2
+ * @angular-modernizer/adapter-mcp - Compact output
3
+ *
4
+ * Shared `summaryOnly` / `maxResults` handling for tools that return long
5
+ * lists (find-usages, find-angular-symbol, list-imports, analyze-build-impact).
6
+ *
7
+ * @remarks
8
+ * Contract for every tool that uses it:
9
+ * - `summaryOnly: true` omits the list fields and keeps counts and the text
10
+ * summary, so an agent can decide whether the full list is worth loading.
11
+ * - `maxResults: n` keeps the first `n` entries of each list and sets
12
+ * `truncated: true` when anything was cut. Counts always describe the full
13
+ * result, never the shortened list.
14
+ * Both are opt-in; without them the tools return everything, as before. The
15
+ * JSON-RPC handler's 4 MB limit stays the last line of defence.
16
+ */
17
+ import { relative, sep } from 'node:path';
18
+ /** JSON schema entries for the two shared parameters. */
19
+ export const COMPACT_OUTPUT_SCHEMA = {
20
+ summaryOnly: {
21
+ type: 'boolean',
22
+ description: 'Return counts and the text summary only, without the result list. Default: false.',
23
+ default: false,
24
+ },
25
+ maxResults: {
26
+ type: 'number',
27
+ description: 'Return at most this many list entries; the result is then marked truncated and counts still ' +
28
+ 'describe the full result. Default: no limit.',
29
+ },
30
+ };
31
+ /**
32
+ * Validates and reads `summaryOnly` and `maxResults` from raw tool arguments.
33
+ *
34
+ * @throws Error when `summaryOnly` is not a boolean or `maxResults` is not a
35
+ * positive integer.
36
+ */
37
+ export function parseCompactOptions(args) {
38
+ const { summaryOnly, maxResults } = args;
39
+ if (summaryOnly !== undefined && typeof summaryOnly !== 'boolean') {
40
+ throw new Error('summaryOnly must be a boolean');
41
+ }
42
+ if (maxResults !== undefined &&
43
+ (typeof maxResults !== 'number' ||
44
+ !Number.isInteger(maxResults) ||
45
+ maxResults < 1)) {
46
+ throw new Error('maxResults must be a positive integer');
47
+ }
48
+ return { summaryOnly: summaryOnly ?? false, maxResults };
49
+ }
50
+ /** Keeps the first `maxResults` entries of `items`. */
51
+ export function limitList(items, maxResults) {
52
+ if (maxResults === undefined || items.length <= maxResults) {
53
+ return { items: [...items], truncated: false };
54
+ }
55
+ return { items: items.slice(0, maxResults), truncated: true };
56
+ }
57
+ /** Summary line for a list cut by `maxResults`, or `undefined` if nothing was cut. */
58
+ export function truncationNote(shown, total, noun) {
59
+ return shown < total
60
+ ? `Showing ${shown} of ${total} ${noun} (maxResults). Counts cover all ${total}.`
61
+ : undefined;
62
+ }
63
+ /**
64
+ * Counts files per directory prefix of at most `depth` segments, relative to
65
+ * `root`, sorted by count (descending) and then by name.
66
+ *
67
+ * @example
68
+ * ```typescript
69
+ * countByDirectory(['/p/src/app/a/x.ts', '/p/src/app/a/y.ts'], '/p', 3);
70
+ * // { 'src/app/a': 2 }
71
+ * ```
72
+ */
73
+ export function countByDirectory(files, root, depth) {
74
+ const counts = new Map();
75
+ for (const file of files) {
76
+ const segments = relative(root, file).split(sep).slice(0, -1);
77
+ const key = segments.slice(0, depth).join('/') || '.';
78
+ counts.set(key, (counts.get(key) ?? 0) + 1);
79
+ }
80
+ const sorted = [...counts].sort(([nameA, countA], [nameB, countB]) => countB - countA || nameA.localeCompare(nameB));
81
+ return Object.fromEntries(sorted);
82
+ }
83
+ //# sourceMappingURL=compact-output.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compact-output.js","sourceRoot":"","sources":["../../../src/tools/helpers/compact-output.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAE,QAAQ,EAAE,GAAG,EAAE,MAAM,WAAW,CAAC;AAE1C,yDAAyD;AACzD,MAAM,CAAC,MAAM,qBAAqB,GAAG;IACnC,WAAW,EAAE;QACX,IAAI,EAAE,SAAS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AAI,mCAAmC,KAAK,GAAG;QACjF,CAAC,CAAC,SAAS,CAAC;AAChB,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,gBAAgB,CAC9B,KAAwB,EACxB,IAAY,EACZ,KAAa;IAEb,MAAM,MAAM,GAAG,IAAI,GAAG,EAAkB,CAAC;IACzC,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,MAAM,QAAQ,GAAG,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;QAC9D,MAAM,GAAG,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,GAAG,CAAC;QACtD,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;IAC9C,CAAC;IACD,MAAM,MAAM,GAAG,CAAC,GAAG,MAAM,CAAC,CAAC,IAAI,CAC7B,CAAC,CAAC,KAAK,EAAE,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,CAAC,EAAE,EAAE,CACnC,MAAM,GAAG,MAAM,IAAI,KAAK,CAAC,aAAa,CAAC,KAAK,CAAC,CAChD,CAAC;IACF,OAAO,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;AACpC,CAAC"}
@@ -1,8 +1,31 @@
1
1
  /**
2
2
  * @angular-modernizer/adapter-mcp - TypeScript Config Finder
3
+ *
4
+ * Shared by every MCP tool that builds a ts-morph `Project`.
5
+ *
6
+ * @remarks
7
+ * Angular CLI workspaces often use a "solution style" root `tsconfig.json`
8
+ * with `"files": []` plus `references`. Loading that root yields an (almost)
9
+ * empty program, so investigation tools would silently search nothing. The
10
+ * finder therefore prefers a config that actually contains source files:
11
+ *
12
+ * 1. the first existing candidate, if it has files
13
+ * 2. its `references` (recursively, `*.app.*` first, `*.spec.*` last)
14
+ * 3. `tsconfig.app.json` (root, then `src/`)
15
+ * 4. any other existing candidate with files
16
+ * 5. the first existing candidate (legacy behaviour)
17
+ *
18
+ * "Has files" is decided from the config JSON (own or inherited `files` /
19
+ * `include`, and whether the static directory of an `include` pattern
20
+ * exists) instead of the TypeScript API: loading `typescript` at module
21
+ * scope breaks the many adapter test suites that mock `node:fs`, and those
22
+ * suites also mock `@angular-modernizer/core`, so this helper stays free of
23
+ * workspace imports. An unreadable config counts as "has files", which keeps
24
+ * the legacy first-existing behaviour.
3
25
  */
4
26
  /**
5
- * Locate the first resolvable TypeScript config file under `rootPath`.
27
+ * Locate the tsconfig file that should back a ts-morph `Project` under
28
+ * `rootPath` (see module remarks for the selection order).
6
29
  *
7
30
  * @returns Absolute path to the tsconfig file, or `null` if none found.
8
31
  */
@@ -1 +1 @@
1
- {"version":3,"file":"find-ts-config.d.ts","sourceRoot":"","sources":["../../../src/tools/helpers/find-ts-config.ts"],"names":[],"mappings":"AAAA;;GAEG;AAkBH;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAQ5D"}
1
+ {"version":3,"file":"find-ts-config.d.ts","sourceRoot":"","sources":["../../../src/tools/helpers/find-ts-config.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAqNH;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAe5D"}
@@ -1,8 +1,30 @@
1
1
  /**
2
2
  * @angular-modernizer/adapter-mcp - TypeScript Config Finder
3
+ *
4
+ * Shared by every MCP tool that builds a ts-morph `Project`.
5
+ *
6
+ * @remarks
7
+ * Angular CLI workspaces often use a "solution style" root `tsconfig.json`
8
+ * with `"files": []` plus `references`. Loading that root yields an (almost)
9
+ * empty program, so investigation tools would silently search nothing. The
10
+ * finder therefore prefers a config that actually contains source files:
11
+ *
12
+ * 1. the first existing candidate, if it has files
13
+ * 2. its `references` (recursively, `*.app.*` first, `*.spec.*` last)
14
+ * 3. `tsconfig.app.json` (root, then `src/`)
15
+ * 4. any other existing candidate with files
16
+ * 5. the first existing candidate (legacy behaviour)
17
+ *
18
+ * "Has files" is decided from the config JSON (own or inherited `files` /
19
+ * `include`, and whether the static directory of an `include` pattern
20
+ * exists) instead of the TypeScript API: loading `typescript` at module
21
+ * scope breaks the many adapter test suites that mock `node:fs`, and those
22
+ * suites also mock `@angular-modernizer/core`, so this helper stays free of
23
+ * workspace imports. An unreadable config counts as "has files", which keeps
24
+ * the legacy first-existing behaviour.
3
25
  */
4
- import { existsSync } from 'node:fs';
5
- import { resolve } from 'node:path';
26
+ import { existsSync, readFileSync } from 'node:fs';
27
+ import { basename, dirname, extname, resolve } from 'node:path';
6
28
  /**
7
29
  * Candidate tsconfig filenames searched in order.
8
30
  * Covers standard Angular CLI layouts, library projects, and monorepo roots.
@@ -15,16 +37,200 @@ const TS_CONFIG_CANDIDATES = [
15
37
  'tsconfig.lib.json',
16
38
  'tsconfig.workspace.json',
17
39
  ];
40
+ const APP_CONFIG_FALLBACKS = ['tsconfig.app.json', 'src/tsconfig.app.json'];
41
+ const MAX_REFERENCE_DEPTH = 4;
42
+ const MAX_EXTENDS_DEPTH = 5;
43
+ function exists(path) {
44
+ try {
45
+ return existsSync(path);
46
+ }
47
+ catch {
48
+ return false;
49
+ }
50
+ }
51
+ /** Strips comments and trailing commas so tsconfig JSONC can be parsed. */
52
+ function stripJsonComments(text) {
53
+ let out = '';
54
+ let inString = false;
55
+ for (let i = 0; i < text.length; i++) {
56
+ const ch = text[i] ?? '';
57
+ const next = text[i + 1] ?? '';
58
+ if (inString) {
59
+ out += ch;
60
+ if (ch === '\\') {
61
+ out += next;
62
+ i++;
63
+ }
64
+ else if (ch === '"') {
65
+ inString = false;
66
+ }
67
+ continue;
68
+ }
69
+ if (ch === '"') {
70
+ inString = true;
71
+ out += ch;
72
+ continue;
73
+ }
74
+ if (ch === '/' && next === '/') {
75
+ const newline = text.indexOf('\n', i);
76
+ i = newline === -1 ? text.length : newline - 1;
77
+ continue;
78
+ }
79
+ if (ch === '/' && next === '*') {
80
+ const close = text.indexOf('*/', i + 2);
81
+ i = close === -1 ? text.length : close + 1;
82
+ continue;
83
+ }
84
+ out += ch;
85
+ }
86
+ return out.replace(/,(\s*[}\]])/g, '$1');
87
+ }
88
+ function readRawConfig(configPath) {
89
+ try {
90
+ const parsed = JSON.parse(stripJsonComments(readFileSync(configPath, 'utf-8')));
91
+ return typeof parsed === 'object' && parsed !== null
92
+ ? parsed
93
+ : null;
94
+ }
95
+ catch {
96
+ return null;
97
+ }
98
+ }
99
+ function stringArray(value) {
100
+ return Array.isArray(value)
101
+ ? value.filter((v) => typeof v === 'string')
102
+ : undefined;
103
+ }
104
+ /** Resolves `files` / `include` through relative `extends` chains. */
105
+ function effectiveInputs(configPath, raw, depth) {
106
+ const own = {
107
+ files: stringArray(raw.files),
108
+ include: stringArray(raw.include),
109
+ baseDir: dirname(configPath),
110
+ };
111
+ const ext = typeof raw.extends === 'string' ? raw.extends : undefined;
112
+ const isRelative = ext?.startsWith('.') === true;
113
+ if (ext === undefined || !isRelative || depth >= MAX_EXTENDS_DEPTH) {
114
+ return own;
115
+ }
116
+ const basePath = resolve(dirname(configPath), ext.endsWith('.json') ? ext : `${ext}.json`);
117
+ const baseRaw = exists(basePath) ? readRawConfig(basePath) : null;
118
+ if (baseRaw === null) {
119
+ return own;
120
+ }
121
+ const base = effectiveInputs(basePath, baseRaw, depth + 1);
122
+ return {
123
+ files: own.files ?? base.files,
124
+ include: own.include ?? base.include,
125
+ baseDir: own.files === undefined && own.include === undefined ? base.baseDir : own.baseDir,
126
+ };
127
+ }
128
+ /** Static directory prefix of an include glob (`src/app/**\/*.ts` -> `src/app`). */
129
+ function staticPrefix(pattern) {
130
+ const segments = pattern.split('/');
131
+ const index = segments.findIndex((seg) => /[*?[{]/.test(seg));
132
+ const kept = index === -1 ? segments : segments.slice(0, index);
133
+ return kept.join('/') || '.';
134
+ }
135
+ function parseTsConfig(configPath) {
136
+ const raw = readRawConfig(configPath);
137
+ if (raw === null) {
138
+ return { hasFiles: true, references: [] };
139
+ }
140
+ const references = Array.isArray(raw.references)
141
+ ? raw.references
142
+ .map((ref) => typeof ref === 'object' && ref !== null && 'path' in ref
143
+ ? ref.path
144
+ : undefined)
145
+ .filter((p) => typeof p === 'string')
146
+ .map((p) => {
147
+ const absolute = resolve(dirname(configPath), p);
148
+ return extname(absolute) === '.json'
149
+ ? absolute
150
+ : resolve(absolute, 'tsconfig.json');
151
+ })
152
+ : [];
153
+ const { files, include, baseDir } = effectiveInputs(configPath, raw, 0);
154
+ if (files === undefined && include === undefined) {
155
+ // TypeScript default: include everything under the config directory.
156
+ return { hasFiles: true, references };
157
+ }
158
+ const hasListedFiles = (files ?? []).some((f) => exists(resolve(baseDir, f)));
159
+ const hasIncludeRoot = (include ?? []).some((pattern) => exists(resolve(baseDir, staticPrefix(pattern))));
160
+ return { hasFiles: hasListedFiles || hasIncludeRoot, references };
161
+ }
162
+ function rankReference(path) {
163
+ const name = basename(path);
164
+ if (name.includes('.app.')) {
165
+ return 0;
166
+ }
167
+ if (name.includes('.spec.') || name.includes('.test.')) {
168
+ return 2;
169
+ }
170
+ return 1;
171
+ }
172
+ function findReferenceWithFiles(references, visited, depth) {
173
+ if (depth > MAX_REFERENCE_DEPTH) {
174
+ return null;
175
+ }
176
+ const ordered = [...references].sort((a, b) => rankReference(a) - rankReference(b));
177
+ for (const ref of ordered) {
178
+ if (visited.has(ref) || !exists(ref)) {
179
+ continue;
180
+ }
181
+ visited.add(ref);
182
+ const parsed = parseTsConfig(ref);
183
+ if (parsed.hasFiles) {
184
+ return ref;
185
+ }
186
+ const nested = findReferenceWithFiles(parsed.references, visited, depth + 1);
187
+ if (nested !== null) {
188
+ return nested;
189
+ }
190
+ }
191
+ return null;
192
+ }
18
193
  /**
19
- * Locate the first resolvable TypeScript config file under `rootPath`.
194
+ * Locate the tsconfig file that should back a ts-morph `Project` under
195
+ * `rootPath` (see module remarks for the selection order).
20
196
  *
21
197
  * @returns Absolute path to the tsconfig file, or `null` if none found.
22
198
  */
23
199
  export function findTsConfig(rootPath) {
24
- for (const candidate of TS_CONFIG_CANDIDATES) {
25
- const fullPath = resolve(rootPath, candidate);
26
- if (existsSync(fullPath)) {
27
- return fullPath;
200
+ const existing = TS_CONFIG_CANDIDATES.map((c) => resolve(rootPath, c)).filter(exists);
201
+ const first = existing[0];
202
+ if (first === undefined) {
203
+ return null;
204
+ }
205
+ try {
206
+ return findConfigWithFiles(rootPath, first, existing) ?? first;
207
+ }
208
+ catch {
209
+ // Never fail discovery because a config could not be inspected.
210
+ return first;
211
+ }
212
+ }
213
+ function findConfigWithFiles(rootPath, first, existing) {
214
+ const firstParsed = parseTsConfig(first);
215
+ if (firstParsed.hasFiles) {
216
+ return first;
217
+ }
218
+ const visited = new Set([first]);
219
+ const fromReference = findReferenceWithFiles(firstParsed.references, visited, 1);
220
+ if (fromReference !== null) {
221
+ return fromReference;
222
+ }
223
+ const fallbacks = [
224
+ ...APP_CONFIG_FALLBACKS.map((f) => resolve(rootPath, f)),
225
+ ...existing,
226
+ ];
227
+ for (const candidate of fallbacks) {
228
+ if (visited.has(candidate) || !exists(candidate)) {
229
+ continue;
230
+ }
231
+ visited.add(candidate);
232
+ if (parseTsConfig(candidate).hasFiles) {
233
+ return candidate;
28
234
  }
29
235
  }
30
236
  return null;
@@ -1 +1 @@
1
- {"version":3,"file":"find-ts-config.js","sourceRoot":"","sources":["../../../src/tools/helpers/find-ts-config.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC;;;GAGG;AACH,MAAM,oBAAoB,GAAG;IAC3B,eAAe;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AAC,CAAC,CAAC;IACzC,MAAM,aAAa,GAAG,sBAAsB,CAC1C,WAAW,CAAC,UAAU,EACtB,OAAO,EACP,CAAC,CACF,CAAC;IACF,IAAI,aAAa,KAAK,IAAI,EAAE,CAAC;QAC3B,OAAO,aAAa,CAAC;IACvB,CAAC;IAED,MAAM,SAAS,GAAG;QAChB,GAAG,oBAAoB,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC;QACxD,GAAG,QAAQ;KACZ,CAAC;IACF,KAAK,MAAM,SAAS,IAAI,SAAS,EAAE,CAAC;QAClC,IAAI,OAAO,CAAC,GAAG,CAAC,SAAS,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE,CAAC;YACjD,SAAS;QACX,CAAC;QACD,OAAO,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QACvB,IAAI,aAAa,CAAC,SAAS,CAAC,CAAC,QAAQ,EAAE,CAAC;YACtC,OAAO,SAAS,CAAC;QACnB,CAAC;IACH,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC"}
@@ -0,0 +1,177 @@
1
+ /**
2
+ * @angular-modernizer/adapter-mcp - Project Cache
3
+ *
4
+ * Keeps ts-morph `Project` instances alive across tool calls, keyed by the
5
+ * tsconfig path, so repeated investigation calls on the same workspace do not
6
+ * re-parse the whole program each time (seconds on a large Angular app).
7
+ *
8
+ * @remarks
9
+ * Same idea as `SharedProjectManager` in core (one long-lived Project instead
10
+ * of one per call), but keyed per tsconfig because the MCP server can serve
11
+ * several workspaces in one session. An entry is rebuilt when the tsconfig
12
+ * mtime changes. Source files are kept current file by file: the mtime of
13
+ * every program file is recorded at load and compared on each `get()` (about
14
+ * 6 ms for ngUWI's 3939 files); changed files are re-read, deleted ones
15
+ * removed. The type checker then rebuilds lazily on the next query. Files
16
+ * that were not in the program at load (new files) need a tsconfig touch or a
17
+ * server restart.
18
+ *
19
+ * Two limits bound the cache: `maxEntries` projects, and `maxHeapMb` of V8
20
+ * heap, checked on every `get()` because a project keeps growing after the
21
+ * load as the type checker resolves more of it, and on a miss also before
22
+ * the load. Over either limit the least recently used project is evicted;
23
+ * the project being requested never is.
24
+ * The heap is the signal, not the RSS: dropping a Project frees its heap
25
+ * (the TypeScript program, checker and ts-morph wrappers hang off it and
26
+ * nothing else keeps them), but V8 does not reliably unmap the pages, so the
27
+ * RSS can stay at its peak after an eviction and an RSS limit would then
28
+ * evict everything on every call. Freed pages are reused by the next load.
29
+ * When node runs with `--expose-gc` the cache collects once the raw heap
30
+ * reading passes the limit and after each eviction, so garbage does not count
31
+ * against the cache; otherwise it subtracts the heap growth recorded when the
32
+ * evicted project was loaded, and garbage makes it evict earlier.
33
+ */
34
+ import { Project } from 'ts-morph';
35
+ /** A cached project as reported by `server-status`. */
36
+ export interface CachedProjectInfo {
37
+ tsConfigPath: string;
38
+ sourceFiles: number;
39
+ loadedAt: string;
40
+ lastUsedAt: string;
41
+ /** Heap growth while the project was loaded, an estimate of its size. */
42
+ loadHeapMB: number;
43
+ /** Source files re-read or removed because they changed on disk. */
44
+ refreshedFiles: number;
45
+ lastRefreshAt?: string;
46
+ }
47
+ /** Why a project left the cache. */
48
+ export type EvictionReason = 'max-entries' | 'max-heap';
49
+ /** One eviction with the memory around it. */
50
+ export interface ProjectEviction {
51
+ tsConfigPath: string;
52
+ reason: EvictionReason;
53
+ at: string;
54
+ sourceFiles: number;
55
+ heapMB: {
56
+ before: number;
57
+ after: number;
58
+ };
59
+ rssMB: {
60
+ before: number;
61
+ after: number;
62
+ };
63
+ }
64
+ /** Evictions since the server started. */
65
+ export interface EvictionStats {
66
+ count: number;
67
+ last?: ProjectEviction;
68
+ }
69
+ /** Heap and RSS reader, plus a full collection when node exposes one. */
70
+ export interface MemoryProbe {
71
+ usage(): {
72
+ heapUsed: number;
73
+ rss: number;
74
+ };
75
+ collect?: () => void;
76
+ }
77
+ /** Limits of a project cache. */
78
+ export interface ProjectCacheLimits {
79
+ /** Most projects kept at once, at least 1. */
80
+ maxEntries: number;
81
+ /** Heap limit in MB checked after each load; 0 switches it off. */
82
+ maxHeapMb: number;
83
+ }
84
+ export interface ProjectCacheOptions extends Partial<ProjectCacheLimits> {
85
+ createProject?: (tsConfigFilePath: string) => Project;
86
+ memory?: MemoryProbe;
87
+ /** mtime of a file in ms, undefined when it cannot be read. */
88
+ statFile?: (filePath: string) => number | undefined;
89
+ }
90
+ /** Default number of workspaces kept in memory at once. */
91
+ export declare const DEFAULT_MAX_ENTRIES = 3;
92
+ /**
93
+ * Default heap limit. ngUWI (3939 files) takes 1.0 to 1.3 GB of heap once
94
+ * loaded and queried; V8's heap limit on the dev machines is about 4.1 GB.
95
+ * 2048 MB keeps ngUWI together with small workspaces, and evicts it before a
96
+ * second workspace of its size drives the process towards the V8 limit.
97
+ */
98
+ export declare const DEFAULT_MAX_HEAP_MB = 2048;
99
+ /** Bytes to MB, one decimal. */
100
+ export declare function toMB(bytes: number): number;
101
+ /** LRU cache of ts-morph Projects keyed by tsconfig path. */
102
+ export declare class ProjectCache {
103
+ private readonly entries;
104
+ private readonly maxEntries;
105
+ private readonly maxHeapMb;
106
+ private readonly createProject;
107
+ private readonly memory;
108
+ private readonly statFile;
109
+ private evictionCount;
110
+ private lastEviction;
111
+ /**
112
+ * @param options - Limits, project factory and memory probe; tests inject
113
+ * the last two.
114
+ */
115
+ constructor(options?: ProjectCacheOptions);
116
+ /** Returns the cached Project for `tsConfigPath`, building it on a miss. */
117
+ get(tsConfigPath: string): Project;
118
+ /** Number of cached projects. */
119
+ get size(): number;
120
+ /** Most projects kept at once. */
121
+ get capacity(): number;
122
+ /** Limits in effect. */
123
+ get limits(): ProjectCacheLimits;
124
+ /** Eviction count and the most recent eviction. */
125
+ get evictions(): EvictionStats;
126
+ /** Cached projects, least recently used first. */
127
+ describe(): CachedProjectInfo[];
128
+ /**
129
+ * Moves a fresh entry to the most recently used end and returns its
130
+ * project; drops a stale one. Kept apart from `get()` so no reference to a
131
+ * dropped entry is alive while `evictOverHeap` collects.
132
+ */
133
+ private touch;
134
+ /** Re-reads program files whose mtime changed and removes deleted ones. */
135
+ private refreshChangedFiles;
136
+ private recordMtimes;
137
+ private load;
138
+ private evictOverCount;
139
+ /**
140
+ * Evicts least recently used projects, never `current`, until the heap is
141
+ * back under `maxHeapMb`. Under the limit this is one `memoryUsage()` call;
142
+ * over it, a collection runs first so garbage does not count.
143
+ */
144
+ private evictOverHeap;
145
+ /** Drops `key`, records the eviction, returns the heap estimate after it. */
146
+ private evict;
147
+ /**
148
+ * Deletes `key` and returns the figures the eviction record needs. A
149
+ * separate frame, so the entry is unreachable once it returns and the
150
+ * following collection can free the project.
151
+ */
152
+ private remove;
153
+ }
154
+ /** Command-line flags and environment variables for the cache limits. */
155
+ export declare const PROJECT_CACHE_FLAGS: {
156
+ readonly maxEntries: {
157
+ readonly flag: "--max-projects";
158
+ readonly env: "ANGULAR_MODERNIZER_MAX_PROJECTS";
159
+ };
160
+ readonly maxHeapMb: {
161
+ readonly flag: "--max-heap-mb";
162
+ readonly env: "ANGULAR_MODERNIZER_MAX_HEAP_MB";
163
+ };
164
+ };
165
+ /** Limits read from argv and env, plus warnings for rejected values. */
166
+ export interface ParsedProjectCacheLimits {
167
+ options: Partial<ProjectCacheLimits>;
168
+ warnings: string[];
169
+ }
170
+ /**
171
+ * Reads the cache limits from `--max-projects` / `--max-heap-mb` (either
172
+ * `--flag=N` or `--flag N`) or their environment variables; the flag wins.
173
+ * `--max-heap-mb 0` switches the heap limit off. Invalid values are dropped
174
+ * with a warning so a typo does not stop the server.
175
+ */
176
+ export declare function parseProjectCacheLimits(argv: readonly string[], env: Readonly<Record<string, string | undefined>>): ParsedProjectCacheLimits;
177
+ //# sourceMappingURL=project-cache.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"project-cache.d.ts","sourceRoot":"","sources":["../../../src/tools/helpers/project-cache.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;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