@nerviq/cli 1.17.3 → 1.19.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 (190) hide show
  1. package/LICENSE +23 -23
  2. package/README.md +4 -4
  3. package/bin/cli.js +61 -274
  4. package/package.json +60 -60
  5. package/src/activity.js +1039 -1039
  6. package/src/adoption-advisor.js +299 -299
  7. package/src/aider/config-parser.js +166 -166
  8. package/src/aider/context.js +158 -158
  9. package/src/aider/deep-review.js +316 -316
  10. package/src/aider/domain-packs.js +303 -303
  11. package/src/aider/freshness.js +93 -93
  12. package/src/aider/governance.js +253 -253
  13. package/src/aider/interactive.js +334 -334
  14. package/src/aider/mcp-packs.js +329 -329
  15. package/src/aider/patch.js +214 -214
  16. package/src/aider/plans.js +186 -186
  17. package/src/aider/premium.js +360 -360
  18. package/src/aider/setup.js +404 -404
  19. package/src/aider/techniques.js +16 -16
  20. package/src/analyze.js +951 -951
  21. package/src/anti-patterns.js +485 -485
  22. package/src/audit/instruction-files.js +180 -180
  23. package/src/audit/recommendations.js +577 -577
  24. package/src/auto-suggest.js +154 -154
  25. package/src/badge.js +13 -13
  26. package/src/behavioral-drift.js +801 -801
  27. package/src/benchmark.js +67 -67
  28. package/src/catalog.js +103 -103
  29. package/src/certification.js +128 -128
  30. package/src/codex/config-parser.js +183 -183
  31. package/src/codex/context.js +223 -223
  32. package/src/codex/deep-review.js +493 -493
  33. package/src/codex/domain-packs.js +394 -394
  34. package/src/codex/freshness.js +84 -84
  35. package/src/codex/governance.js +192 -192
  36. package/src/codex/interactive.js +618 -618
  37. package/src/codex/mcp-packs.js +914 -914
  38. package/src/codex/patch.js +209 -209
  39. package/src/codex/plans.js +251 -251
  40. package/src/codex/premium.js +614 -614
  41. package/src/codex/setup.js +591 -591
  42. package/src/context.js +320 -320
  43. package/src/continuous-ops.js +681 -681
  44. package/src/copilot/activity.js +309 -309
  45. package/src/copilot/config-parser.js +280 -226
  46. package/src/copilot/context.js +218 -197
  47. package/src/copilot/deep-review.js +346 -346
  48. package/src/copilot/domain-packs.js +372 -372
  49. package/src/copilot/freshness.js +57 -57
  50. package/src/copilot/governance.js +222 -222
  51. package/src/copilot/interactive.js +406 -406
  52. package/src/copilot/mcp-packs.js +826 -826
  53. package/src/copilot/plans.js +253 -253
  54. package/src/copilot/premium.js +451 -451
  55. package/src/copilot/setup.js +488 -488
  56. package/src/copilot/techniques.js +219 -78
  57. package/src/cost-tracking.js +61 -61
  58. package/src/cursor/activity.js +301 -301
  59. package/src/cursor/config-parser.js +265 -265
  60. package/src/cursor/context.js +256 -256
  61. package/src/cursor/deep-review.js +334 -334
  62. package/src/cursor/domain-packs.js +368 -368
  63. package/src/cursor/freshness.js +65 -65
  64. package/src/cursor/governance.js +229 -229
  65. package/src/cursor/interactive.js +391 -391
  66. package/src/cursor/mcp-packs.js +828 -828
  67. package/src/cursor/plans.js +254 -254
  68. package/src/cursor/premium.js +469 -469
  69. package/src/cursor/setup.js +488 -488
  70. package/src/dashboard.js +493 -493
  71. package/src/deep-review.js +428 -428
  72. package/src/deprecation.js +98 -98
  73. package/src/diff-only.js +280 -280
  74. package/src/doctor.js +119 -119
  75. package/src/domain-pack-expansion.js +1033 -1033
  76. package/src/domain-packs.js +387 -387
  77. package/src/feedback.js +178 -178
  78. package/src/fix-engine.js +783 -0
  79. package/src/fix-prompts.js +122 -122
  80. package/src/formatters/sarif.js +115 -115
  81. package/src/freshness.js +74 -74
  82. package/src/gemini/config-parser.js +275 -275
  83. package/src/gemini/context.js +221 -221
  84. package/src/gemini/deep-review.js +559 -559
  85. package/src/gemini/domain-packs.js +393 -393
  86. package/src/gemini/freshness.js +66 -66
  87. package/src/gemini/governance.js +201 -201
  88. package/src/gemini/interactive.js +860 -860
  89. package/src/gemini/mcp-packs.js +915 -915
  90. package/src/gemini/plans.js +269 -269
  91. package/src/gemini/premium.js +760 -760
  92. package/src/gemini/setup.js +692 -692
  93. package/src/gemini/techniques.js +14 -14
  94. package/src/governance.js +72 -72
  95. package/src/harmony/add.js +68 -68
  96. package/src/harmony/advisor.js +333 -333
  97. package/src/harmony/canon.js +565 -565
  98. package/src/harmony/cli.js +591 -591
  99. package/src/harmony/drift.js +401 -401
  100. package/src/harmony/governance.js +313 -313
  101. package/src/harmony/memory.js +239 -239
  102. package/src/harmony/sync.js +475 -475
  103. package/src/harmony/watch.js +370 -370
  104. package/src/hook-validation.js +342 -342
  105. package/src/index.js +271 -271
  106. package/src/init.js +184 -184
  107. package/src/instruction-surfaces.js +185 -185
  108. package/src/integrations.js +144 -144
  109. package/src/interactive.js +118 -118
  110. package/src/locales/en.json +1 -1
  111. package/src/locales/es.json +1 -1
  112. package/src/mcp-packs.js +830 -830
  113. package/src/mcp-server.js +726 -726
  114. package/src/mcp-validation.js +337 -337
  115. package/src/nerviq-sync.json +7 -7
  116. package/src/opencode/config-parser.js +109 -109
  117. package/src/opencode/context.js +247 -247
  118. package/src/opencode/deep-review.js +313 -313
  119. package/src/opencode/domain-packs.js +262 -262
  120. package/src/opencode/freshness.js +66 -66
  121. package/src/opencode/governance.js +159 -159
  122. package/src/opencode/interactive.js +392 -392
  123. package/src/opencode/mcp-packs.js +705 -705
  124. package/src/opencode/patch.js +184 -184
  125. package/src/opencode/plans.js +231 -231
  126. package/src/opencode/premium.js +413 -413
  127. package/src/opencode/setup.js +449 -449
  128. package/src/opencode/techniques.js +27 -27
  129. package/src/operating-profile.js +574 -574
  130. package/src/org.js +152 -152
  131. package/src/permission-rules.js +218 -218
  132. package/src/plans.js +839 -839
  133. package/src/platform-change-manifest.js +86 -86
  134. package/src/plugins.js +110 -110
  135. package/src/policy-layers.js +210 -210
  136. package/src/profiles.js +124 -124
  137. package/src/prompt-injection.js +74 -74
  138. package/src/public-api.js +173 -173
  139. package/src/recommendation-rules.js +84 -84
  140. package/src/repo-archetype.js +386 -386
  141. package/src/secret-patterns.js +39 -39
  142. package/src/server.js +527 -527
  143. package/src/setup/analysis.js +607 -607
  144. package/src/setup/runtime.js +172 -172
  145. package/src/setup.js +677 -677
  146. package/src/shared/capabilities.js +194 -194
  147. package/src/source-urls.js +132 -132
  148. package/src/stack-checks.js +565 -565
  149. package/src/supplemental-checks.js +13 -13
  150. package/src/synergy/adaptive.js +261 -261
  151. package/src/synergy/compensation.js +137 -137
  152. package/src/synergy/evidence.js +193 -193
  153. package/src/synergy/learning.js +199 -199
  154. package/src/synergy/patterns.js +227 -227
  155. package/src/synergy/ranking.js +83 -83
  156. package/src/synergy/report.js +165 -165
  157. package/src/synergy/routing.js +146 -146
  158. package/src/techniques/api.js +407 -407
  159. package/src/techniques/automation.js +316 -316
  160. package/src/techniques/compliance.js +257 -257
  161. package/src/techniques/hygiene.js +294 -294
  162. package/src/techniques/instructions.js +243 -243
  163. package/src/techniques/observability.js +226 -226
  164. package/src/techniques/optimization.js +142 -142
  165. package/src/techniques/quality.js +318 -318
  166. package/src/techniques/security.js +237 -237
  167. package/src/techniques/shared.js +443 -443
  168. package/src/techniques/stacks.js +2294 -2294
  169. package/src/techniques/tools.js +106 -106
  170. package/src/techniques/workflow.js +413 -413
  171. package/src/techniques.js +81 -81
  172. package/src/terminology.js +73 -73
  173. package/src/token-estimate.js +35 -35
  174. package/src/usage-patterns.js +99 -99
  175. package/src/verification-metadata.js +145 -145
  176. package/src/watch.js +247 -247
  177. package/src/windsurf/activity.js +302 -302
  178. package/src/windsurf/config-parser.js +267 -267
  179. package/src/windsurf/context.js +249 -249
  180. package/src/windsurf/deep-review.js +337 -337
  181. package/src/windsurf/domain-packs.js +370 -370
  182. package/src/windsurf/freshness.js +36 -36
  183. package/src/windsurf/governance.js +231 -231
  184. package/src/windsurf/interactive.js +388 -388
  185. package/src/windsurf/mcp-packs.js +792 -792
  186. package/src/windsurf/plans.js +247 -247
  187. package/src/windsurf/premium.js +468 -468
  188. package/src/windsurf/setup.js +471 -471
  189. package/src/windsurf/techniques.js +17 -17
  190. package/src/workspace.js +375 -375
package/src/server.js CHANGED
@@ -1,527 +1,527 @@
1
- const http = require('http');
2
- const fs = require('fs');
3
- const path = require('path');
4
- const { URL } = require('url');
5
- const { version } = require('../package.json');
6
- const { audit } = require('./audit');
7
- const { harmonyAudit } = require('./harmony/audit');
8
- const { getCatalog } = require('./public-api');
9
-
10
- const SUPPORTED_PLATFORMS = [
11
- 'claude',
12
- 'codex',
13
- 'gemini',
14
- 'copilot',
15
- 'cursor',
16
- 'windsurf',
17
- 'aider',
18
- 'opencode',
19
- ];
20
- const SUPPORTED_PLATFORM_SET = new Set(SUPPORTED_PLATFORMS);
21
-
22
- function envelope(data) {
23
- return { data, meta: { version, timestamp: new Date().toISOString() } };
24
- }
25
-
26
- function sendJson(res, statusCode, payload) {
27
- const body = JSON.stringify(payload, null, 2);
28
- res.writeHead(statusCode, {
29
- 'Content-Type': 'application/json; charset=utf-8',
30
- 'Content-Length': Buffer.byteLength(body),
31
- 'Cache-Control': 'no-store',
32
- });
33
- res.end(body);
34
- }
35
-
36
- function resolveRequestDir(baseDir, rawDir) {
37
- const requested = rawDir || '.';
38
- const resolved = path.isAbsolute(requested)
39
- ? requested
40
- : path.resolve(baseDir, requested);
41
-
42
- if (!fs.existsSync(resolved)) {
43
- const error = new Error(`Directory not found: ${resolved}`);
44
- error.statusCode = 400;
45
- throw error;
46
- }
47
-
48
- return resolved;
49
- }
50
-
51
- function normalizePlatform(rawPlatform) {
52
- const platform = (rawPlatform || 'claude').toLowerCase();
53
- if (!SUPPORTED_PLATFORM_SET.has(platform)) {
54
- const error = new Error(`Unsupported platform '${rawPlatform}'.`);
55
- error.statusCode = 400;
56
- throw error;
57
- }
58
- return platform;
59
- }
60
-
61
- function buildEnvelopeSchema(dataSchema) {
62
- return {
63
- type: 'object',
64
- required: ['data', 'meta'],
65
- properties: {
66
- data: dataSchema,
67
- meta: { $ref: '#/components/schemas/ResponseMeta' },
68
- },
69
- };
70
- }
71
-
72
- function buildServeOpenApiSpec(options = {}) {
73
- const serverUrl = options.serverUrl || 'http://127.0.0.1:3000';
74
- const catalogSize = options.catalogSize == null ? getCatalog().length : options.catalogSize;
75
-
76
- return {
77
- openapi: '3.1.0',
78
- info: {
79
- title: 'Nerviq Local API',
80
- version,
81
- summary: 'Zero-dependency local REST surface for audit, harmony, catalog, and health data.',
82
- description: [
83
- 'Nerviq exposes a local-first HTTP API through `nerviq serve`.',
84
- 'Operational endpoints are GET-only, return JSON, and wrap successful payloads in `{ data, meta }` envelopes.',
85
- 'The OpenAPI document itself is available at `/api/openapi.json`.',
86
- ].join(' '),
87
- },
88
- servers: [
89
- {
90
- url: serverUrl,
91
- description: 'Current Nerviq serve instance',
92
- },
93
- ],
94
- tags: [
95
- { name: 'system', description: 'Server health and contract discovery.' },
96
- { name: 'audit', description: 'Repository audit and governance scoring.' },
97
- { name: 'harmony', description: 'Cross-platform alignment and drift analysis.' },
98
- { name: 'catalog', description: 'Unified public check catalog.' },
99
- ],
100
- paths: {
101
- '/api/openapi.json': {
102
- get: {
103
- tags: ['system'],
104
- operationId: 'getOpenApiSpec',
105
- summary: 'Return the live OpenAPI contract for this Nerviq serve instance.',
106
- responses: {
107
- 200: {
108
- description: 'OpenAPI 3.1 document for the active server surface.',
109
- content: {
110
- 'application/json': {
111
- schema: {
112
- type: 'object',
113
- required: ['openapi', 'info', 'paths'],
114
- properties: {
115
- openapi: { type: 'string', example: '3.1.0' },
116
- info: { type: 'object' },
117
- servers: { type: 'array', items: { type: 'object' } },
118
- paths: { type: 'object' },
119
- },
120
- },
121
- },
122
- },
123
- },
124
- 405: {
125
- $ref: '#/components/responses/MethodNotAllowed',
126
- },
127
- },
128
- },
129
- },
130
- '/api/health': {
131
- get: {
132
- tags: ['system'],
133
- operationId: 'getHealth',
134
- summary: 'Check local server readiness and catalog size.',
135
- responses: {
136
- 200: {
137
- description: 'Current server health envelope.',
138
- content: {
139
- 'application/json': {
140
- schema: { $ref: '#/components/schemas/HealthEnvelope' },
141
- },
142
- },
143
- },
144
- 405: {
145
- $ref: '#/components/responses/MethodNotAllowed',
146
- },
147
- 500: {
148
- $ref: '#/components/responses/InternalError',
149
- },
150
- },
151
- },
152
- },
153
- '/api/catalog': {
154
- get: {
155
- tags: ['catalog'],
156
- operationId: 'getCatalog',
157
- summary: 'Return the merged public check catalog.',
158
- responses: {
159
- 200: {
160
- description: 'Full catalog envelope.',
161
- content: {
162
- 'application/json': {
163
- schema: { $ref: '#/components/schemas/CatalogEnvelope' },
164
- },
165
- },
166
- },
167
- 405: {
168
- $ref: '#/components/responses/MethodNotAllowed',
169
- },
170
- 500: {
171
- $ref: '#/components/responses/InternalError',
172
- },
173
- },
174
- },
175
- },
176
- '/api/audit': {
177
- get: {
178
- tags: ['audit'],
179
- operationId: 'runAudit',
180
- summary: 'Run a Nerviq audit for one directory and one platform.',
181
- parameters: [
182
- { $ref: '#/components/parameters/DirParam' },
183
- { $ref: '#/components/parameters/PlatformParam' },
184
- ],
185
- responses: {
186
- 200: {
187
- description: 'Audit result envelope.',
188
- content: {
189
- 'application/json': {
190
- schema: { $ref: '#/components/schemas/AuditEnvelope' },
191
- },
192
- },
193
- },
194
- 400: {
195
- $ref: '#/components/responses/BadRequest',
196
- },
197
- 405: {
198
- $ref: '#/components/responses/MethodNotAllowed',
199
- },
200
- 500: {
201
- $ref: '#/components/responses/InternalError',
202
- },
203
- },
204
- },
205
- },
206
- '/api/harmony': {
207
- get: {
208
- tags: ['harmony'],
209
- operationId: 'runHarmonyAudit',
210
- summary: 'Run cross-platform harmony audit and drift analysis.',
211
- parameters: [
212
- { $ref: '#/components/parameters/DirParam' },
213
- ],
214
- responses: {
215
- 200: {
216
- description: 'Harmony result envelope.',
217
- content: {
218
- 'application/json': {
219
- schema: { $ref: '#/components/schemas/HarmonyEnvelope' },
220
- },
221
- },
222
- },
223
- 400: {
224
- $ref: '#/components/responses/BadRequest',
225
- },
226
- 405: {
227
- $ref: '#/components/responses/MethodNotAllowed',
228
- },
229
- 500: {
230
- $ref: '#/components/responses/InternalError',
231
- },
232
- },
233
- },
234
- },
235
- },
236
- components: {
237
- parameters: {
238
- DirParam: {
239
- name: 'dir',
240
- in: 'query',
241
- required: false,
242
- description: 'Directory to audit. Relative paths resolve from the server base directory. Defaults to `.`.',
243
- schema: {
244
- type: 'string',
245
- default: '.',
246
- },
247
- },
248
- PlatformParam: {
249
- name: 'platform',
250
- in: 'query',
251
- required: false,
252
- description: 'Target platform to audit. Defaults to `claude` when omitted.',
253
- schema: {
254
- type: 'string',
255
- enum: SUPPORTED_PLATFORMS,
256
- default: 'claude',
257
- },
258
- },
259
- },
260
- responses: {
261
- BadRequest: {
262
- description: 'Validation error such as unsupported platform or missing directory.',
263
- content: {
264
- 'application/json': {
265
- schema: { $ref: '#/components/schemas/ErrorResponse' },
266
- },
267
- },
268
- },
269
- MethodNotAllowed: {
270
- description: 'Only GET and OPTIONS are supported on this local API.',
271
- content: {
272
- 'application/json': {
273
- schema: { $ref: '#/components/schemas/ErrorResponse' },
274
- },
275
- },
276
- },
277
- InternalError: {
278
- description: 'Unexpected server-side failure while building the response.',
279
- content: {
280
- 'application/json': {
281
- schema: { $ref: '#/components/schemas/ErrorResponse' },
282
- },
283
- },
284
- },
285
- },
286
- schemas: {
287
- ErrorResponse: {
288
- type: 'object',
289
- required: ['error'],
290
- properties: {
291
- error: { type: 'string' },
292
- },
293
- },
294
- ResponseMeta: {
295
- type: 'object',
296
- required: ['version', 'timestamp'],
297
- properties: {
298
- version: {
299
- type: 'string',
300
- example: version,
301
- },
302
- timestamp: {
303
- type: 'string',
304
- format: 'date-time',
305
- },
306
- },
307
- },
308
- HealthPayload: {
309
- type: 'object',
310
- required: ['status', 'version', 'checks'],
311
- properties: {
312
- status: { type: 'string', example: 'ok' },
313
- version: { type: 'string', example: version },
314
- checks: { type: 'integer', example: catalogSize },
315
- },
316
- },
317
- CatalogEntry: {
318
- type: 'object',
319
- properties: {
320
- platform: { type: 'string', example: 'claude' },
321
- id: { type: 'string', example: 'CL-A01' },
322
- key: { type: 'string', example: 'claudeMd' },
323
- name: { type: 'string', example: 'CLAUDE.md project instructions' },
324
- category: { type: 'string', example: 'memory' },
325
- impact: { type: 'string', example: 'critical' },
326
- fix: { type: 'string' },
327
- sourceUrl: { type: 'string' },
328
- confidence: { type: 'number', minimum: 0, maximum: 1 },
329
- },
330
- additionalProperties: true,
331
- },
332
- AuditStack: {
333
- type: 'object',
334
- properties: {
335
- key: { type: 'string' },
336
- label: { type: 'string' },
337
- },
338
- additionalProperties: true,
339
- },
340
- AuditAction: {
341
- type: 'object',
342
- properties: {
343
- key: { type: 'string' },
344
- name: { type: 'string' },
345
- impact: { type: 'string' },
346
- fix: { type: 'string' },
347
- },
348
- additionalProperties: true,
349
- },
350
- AuditCheck: {
351
- type: 'object',
352
- properties: {
353
- key: { type: 'string' },
354
- name: { type: 'string' },
355
- impact: { type: 'string' },
356
- category: { type: 'string' },
357
- passed: {
358
- oneOf: [
359
- { type: 'boolean' },
360
- { type: 'null' },
361
- ],
362
- },
363
- },
364
- additionalProperties: true,
365
- },
366
- AuditPayload: {
367
- type: 'object',
368
- properties: {
369
- platform: { type: 'string', example: 'claude' },
370
- platformLabel: { type: 'string', example: 'Claude Code' },
371
- score: { type: 'integer', minimum: 0, maximum: 100 },
372
- organicScore: { type: 'integer', minimum: 0, maximum: 100 },
373
- earnedPoints: { type: 'integer' },
374
- maxPoints: { type: 'integer' },
375
- isScaffolded: { type: 'boolean' },
376
- passed: { type: 'integer' },
377
- failed: { type: 'integer' },
378
- skipped: { type: 'integer' },
379
- checkCount: { type: 'integer' },
380
- stacks: {
381
- type: 'array',
382
- items: { $ref: '#/components/schemas/AuditStack' },
383
- },
384
- topNextActions: {
385
- type: 'array',
386
- items: { $ref: '#/components/schemas/AuditAction' },
387
- },
388
- results: {
389
- type: 'array',
390
- items: { $ref: '#/components/schemas/AuditCheck' },
391
- },
392
- },
393
- additionalProperties: true,
394
- },
395
- HarmonyRecommendation: {
396
- type: 'object',
397
- properties: {
398
- priority: { type: 'string' },
399
- category: { type: 'string' },
400
- message: { type: 'string' },
401
- },
402
- additionalProperties: true,
403
- },
404
- ActivePlatform: {
405
- type: 'object',
406
- properties: {
407
- platform: { type: 'string' },
408
- label: { type: 'string' },
409
- },
410
- additionalProperties: true,
411
- },
412
- HarmonyPayload: {
413
- type: 'object',
414
- properties: {
415
- harmonyScore: { type: 'integer', minimum: 0, maximum: 100 },
416
- platformScores: {
417
- type: 'object',
418
- additionalProperties: { type: 'integer' },
419
- },
420
- drift: {
421
- type: 'object',
422
- additionalProperties: true,
423
- },
424
- recommendations: {
425
- type: 'array',
426
- items: { $ref: '#/components/schemas/HarmonyRecommendation' },
427
- },
428
- activePlatforms: {
429
- type: 'array',
430
- items: { $ref: '#/components/schemas/ActivePlatform' },
431
- },
432
- },
433
- additionalProperties: true,
434
- },
435
- HealthEnvelope: buildEnvelopeSchema({ $ref: '#/components/schemas/HealthPayload' }),
436
- CatalogEnvelope: buildEnvelopeSchema({
437
- type: 'array',
438
- items: { $ref: '#/components/schemas/CatalogEntry' },
439
- }),
440
- AuditEnvelope: buildEnvelopeSchema({ $ref: '#/components/schemas/AuditPayload' }),
441
- HarmonyEnvelope: buildEnvelopeSchema({ $ref: '#/components/schemas/HarmonyPayload' }),
442
- },
443
- },
444
- };
445
- }
446
-
447
- function createServer(options = {}) {
448
- const baseDir = path.resolve(options.baseDir || process.cwd());
449
-
450
- return http.createServer(async (req, res) => {
451
- res.setHeader('Access-Control-Allow-Origin', '*');
452
- res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
453
- res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
454
- if (req.method === 'OPTIONS') { res.writeHead(204); res.end(); return; }
455
-
456
- const requestUrl = new URL(req.url || '/', 'http://127.0.0.1');
457
-
458
- if (req.method !== 'GET') {
459
- sendJson(res, 405, { error: 'Method not allowed' });
460
- return;
461
- }
462
-
463
- try {
464
- if (requestUrl.pathname === '/api/openapi.json') {
465
- sendJson(res, 200, buildServeOpenApiSpec({
466
- serverUrl: `http://${req.headers.host || '127.0.0.1:3000'}`,
467
- }));
468
- return;
469
- }
470
-
471
- if (requestUrl.pathname === '/api/health') {
472
- sendJson(res, 200, envelope({
473
- status: 'ok',
474
- version,
475
- checks: getCatalog().length,
476
- }));
477
- return;
478
- }
479
-
480
- if (requestUrl.pathname === '/api/catalog') {
481
- sendJson(res, 200, envelope(getCatalog()));
482
- return;
483
- }
484
-
485
- if (requestUrl.pathname === '/api/audit') {
486
- const dir = resolveRequestDir(baseDir, requestUrl.searchParams.get('dir'));
487
- const platform = normalizePlatform(requestUrl.searchParams.get('platform'));
488
- const result = await audit({ dir, platform, silent: true });
489
- sendJson(res, 200, envelope(result));
490
- return;
491
- }
492
-
493
- if (requestUrl.pathname === '/api/harmony') {
494
- const dir = resolveRequestDir(baseDir, requestUrl.searchParams.get('dir'));
495
- const result = await harmonyAudit({ dir, silent: true });
496
- sendJson(res, 200, envelope(result));
497
- return;
498
- }
499
-
500
- sendJson(res, 404, { error: 'Not found' });
501
- } catch (error) {
502
- sendJson(res, error.statusCode || 500, {
503
- error: error.message,
504
- });
505
- }
506
- });
507
- }
508
-
509
- function startServer(options = {}) {
510
- const port = options.port == null ? 3000 : Number(options.port);
511
- const host = options.host || '127.0.0.1';
512
- const server = createServer(options);
513
-
514
- return new Promise((resolve, reject) => {
515
- server.once('error', reject);
516
- server.listen(port, host, () => {
517
- server.off('error', reject);
518
- resolve(server);
519
- });
520
- });
521
- }
522
-
523
- module.exports = {
524
- buildServeOpenApiSpec,
525
- createServer,
526
- startServer,
527
- };
1
+ const http = require('http');
2
+ const fs = require('fs');
3
+ const path = require('path');
4
+ const { URL } = require('url');
5
+ const { version } = require('../package.json');
6
+ const { audit } = require('./audit');
7
+ const { harmonyAudit } = require('./harmony/audit');
8
+ const { getCatalog } = require('./public-api');
9
+
10
+ const SUPPORTED_PLATFORMS = [
11
+ 'claude',
12
+ 'codex',
13
+ 'gemini',
14
+ 'copilot',
15
+ 'cursor',
16
+ 'windsurf',
17
+ 'aider',
18
+ 'opencode',
19
+ ];
20
+ const SUPPORTED_PLATFORM_SET = new Set(SUPPORTED_PLATFORMS);
21
+
22
+ function envelope(data) {
23
+ return { data, meta: { version, timestamp: new Date().toISOString() } };
24
+ }
25
+
26
+ function sendJson(res, statusCode, payload) {
27
+ const body = JSON.stringify(payload, null, 2);
28
+ res.writeHead(statusCode, {
29
+ 'Content-Type': 'application/json; charset=utf-8',
30
+ 'Content-Length': Buffer.byteLength(body),
31
+ 'Cache-Control': 'no-store',
32
+ });
33
+ res.end(body);
34
+ }
35
+
36
+ function resolveRequestDir(baseDir, rawDir) {
37
+ const requested = rawDir || '.';
38
+ const resolved = path.isAbsolute(requested)
39
+ ? requested
40
+ : path.resolve(baseDir, requested);
41
+
42
+ if (!fs.existsSync(resolved)) {
43
+ const error = new Error(`Directory not found: ${resolved}`);
44
+ error.statusCode = 400;
45
+ throw error;
46
+ }
47
+
48
+ return resolved;
49
+ }
50
+
51
+ function normalizePlatform(rawPlatform) {
52
+ const platform = (rawPlatform || 'claude').toLowerCase();
53
+ if (!SUPPORTED_PLATFORM_SET.has(platform)) {
54
+ const error = new Error(`Unsupported platform '${rawPlatform}'.`);
55
+ error.statusCode = 400;
56
+ throw error;
57
+ }
58
+ return platform;
59
+ }
60
+
61
+ function buildEnvelopeSchema(dataSchema) {
62
+ return {
63
+ type: 'object',
64
+ required: ['data', 'meta'],
65
+ properties: {
66
+ data: dataSchema,
67
+ meta: { $ref: '#/components/schemas/ResponseMeta' },
68
+ },
69
+ };
70
+ }
71
+
72
+ function buildServeOpenApiSpec(options = {}) {
73
+ const serverUrl = options.serverUrl || 'http://127.0.0.1:3000';
74
+ const catalogSize = options.catalogSize == null ? getCatalog().length : options.catalogSize;
75
+
76
+ return {
77
+ openapi: '3.1.0',
78
+ info: {
79
+ title: 'Nerviq Local API',
80
+ version,
81
+ summary: 'Zero-dependency local REST surface for audit, harmony, catalog, and health data.',
82
+ description: [
83
+ 'Nerviq exposes a local-first HTTP API through `nerviq serve`.',
84
+ 'Operational endpoints are GET-only, return JSON, and wrap successful payloads in `{ data, meta }` envelopes.',
85
+ 'The OpenAPI document itself is available at `/api/openapi.json`.',
86
+ ].join(' '),
87
+ },
88
+ servers: [
89
+ {
90
+ url: serverUrl,
91
+ description: 'Current Nerviq serve instance',
92
+ },
93
+ ],
94
+ tags: [
95
+ { name: 'system', description: 'Server health and contract discovery.' },
96
+ { name: 'audit', description: 'Repository audit and governance scoring.' },
97
+ { name: 'harmony', description: 'Cross-platform alignment and drift analysis.' },
98
+ { name: 'catalog', description: 'Unified public check catalog.' },
99
+ ],
100
+ paths: {
101
+ '/api/openapi.json': {
102
+ get: {
103
+ tags: ['system'],
104
+ operationId: 'getOpenApiSpec',
105
+ summary: 'Return the live OpenAPI contract for this Nerviq serve instance.',
106
+ responses: {
107
+ 200: {
108
+ description: 'OpenAPI 3.1 document for the active server surface.',
109
+ content: {
110
+ 'application/json': {
111
+ schema: {
112
+ type: 'object',
113
+ required: ['openapi', 'info', 'paths'],
114
+ properties: {
115
+ openapi: { type: 'string', example: '3.1.0' },
116
+ info: { type: 'object' },
117
+ servers: { type: 'array', items: { type: 'object' } },
118
+ paths: { type: 'object' },
119
+ },
120
+ },
121
+ },
122
+ },
123
+ },
124
+ 405: {
125
+ $ref: '#/components/responses/MethodNotAllowed',
126
+ },
127
+ },
128
+ },
129
+ },
130
+ '/api/health': {
131
+ get: {
132
+ tags: ['system'],
133
+ operationId: 'getHealth',
134
+ summary: 'Check local server readiness and catalog size.',
135
+ responses: {
136
+ 200: {
137
+ description: 'Current server health envelope.',
138
+ content: {
139
+ 'application/json': {
140
+ schema: { $ref: '#/components/schemas/HealthEnvelope' },
141
+ },
142
+ },
143
+ },
144
+ 405: {
145
+ $ref: '#/components/responses/MethodNotAllowed',
146
+ },
147
+ 500: {
148
+ $ref: '#/components/responses/InternalError',
149
+ },
150
+ },
151
+ },
152
+ },
153
+ '/api/catalog': {
154
+ get: {
155
+ tags: ['catalog'],
156
+ operationId: 'getCatalog',
157
+ summary: 'Return the merged public check catalog.',
158
+ responses: {
159
+ 200: {
160
+ description: 'Full catalog envelope.',
161
+ content: {
162
+ 'application/json': {
163
+ schema: { $ref: '#/components/schemas/CatalogEnvelope' },
164
+ },
165
+ },
166
+ },
167
+ 405: {
168
+ $ref: '#/components/responses/MethodNotAllowed',
169
+ },
170
+ 500: {
171
+ $ref: '#/components/responses/InternalError',
172
+ },
173
+ },
174
+ },
175
+ },
176
+ '/api/audit': {
177
+ get: {
178
+ tags: ['audit'],
179
+ operationId: 'runAudit',
180
+ summary: 'Run a Nerviq audit for one directory and one platform.',
181
+ parameters: [
182
+ { $ref: '#/components/parameters/DirParam' },
183
+ { $ref: '#/components/parameters/PlatformParam' },
184
+ ],
185
+ responses: {
186
+ 200: {
187
+ description: 'Audit result envelope.',
188
+ content: {
189
+ 'application/json': {
190
+ schema: { $ref: '#/components/schemas/AuditEnvelope' },
191
+ },
192
+ },
193
+ },
194
+ 400: {
195
+ $ref: '#/components/responses/BadRequest',
196
+ },
197
+ 405: {
198
+ $ref: '#/components/responses/MethodNotAllowed',
199
+ },
200
+ 500: {
201
+ $ref: '#/components/responses/InternalError',
202
+ },
203
+ },
204
+ },
205
+ },
206
+ '/api/harmony': {
207
+ get: {
208
+ tags: ['harmony'],
209
+ operationId: 'runHarmonyAudit',
210
+ summary: 'Run cross-platform harmony audit and drift analysis.',
211
+ parameters: [
212
+ { $ref: '#/components/parameters/DirParam' },
213
+ ],
214
+ responses: {
215
+ 200: {
216
+ description: 'Harmony result envelope.',
217
+ content: {
218
+ 'application/json': {
219
+ schema: { $ref: '#/components/schemas/HarmonyEnvelope' },
220
+ },
221
+ },
222
+ },
223
+ 400: {
224
+ $ref: '#/components/responses/BadRequest',
225
+ },
226
+ 405: {
227
+ $ref: '#/components/responses/MethodNotAllowed',
228
+ },
229
+ 500: {
230
+ $ref: '#/components/responses/InternalError',
231
+ },
232
+ },
233
+ },
234
+ },
235
+ },
236
+ components: {
237
+ parameters: {
238
+ DirParam: {
239
+ name: 'dir',
240
+ in: 'query',
241
+ required: false,
242
+ description: 'Directory to audit. Relative paths resolve from the server base directory. Defaults to `.`.',
243
+ schema: {
244
+ type: 'string',
245
+ default: '.',
246
+ },
247
+ },
248
+ PlatformParam: {
249
+ name: 'platform',
250
+ in: 'query',
251
+ required: false,
252
+ description: 'Target platform to audit. Defaults to `claude` when omitted.',
253
+ schema: {
254
+ type: 'string',
255
+ enum: SUPPORTED_PLATFORMS,
256
+ default: 'claude',
257
+ },
258
+ },
259
+ },
260
+ responses: {
261
+ BadRequest: {
262
+ description: 'Validation error such as unsupported platform or missing directory.',
263
+ content: {
264
+ 'application/json': {
265
+ schema: { $ref: '#/components/schemas/ErrorResponse' },
266
+ },
267
+ },
268
+ },
269
+ MethodNotAllowed: {
270
+ description: 'Only GET and OPTIONS are supported on this local API.',
271
+ content: {
272
+ 'application/json': {
273
+ schema: { $ref: '#/components/schemas/ErrorResponse' },
274
+ },
275
+ },
276
+ },
277
+ InternalError: {
278
+ description: 'Unexpected server-side failure while building the response.',
279
+ content: {
280
+ 'application/json': {
281
+ schema: { $ref: '#/components/schemas/ErrorResponse' },
282
+ },
283
+ },
284
+ },
285
+ },
286
+ schemas: {
287
+ ErrorResponse: {
288
+ type: 'object',
289
+ required: ['error'],
290
+ properties: {
291
+ error: { type: 'string' },
292
+ },
293
+ },
294
+ ResponseMeta: {
295
+ type: 'object',
296
+ required: ['version', 'timestamp'],
297
+ properties: {
298
+ version: {
299
+ type: 'string',
300
+ example: version,
301
+ },
302
+ timestamp: {
303
+ type: 'string',
304
+ format: 'date-time',
305
+ },
306
+ },
307
+ },
308
+ HealthPayload: {
309
+ type: 'object',
310
+ required: ['status', 'version', 'checks'],
311
+ properties: {
312
+ status: { type: 'string', example: 'ok' },
313
+ version: { type: 'string', example: version },
314
+ checks: { type: 'integer', example: catalogSize },
315
+ },
316
+ },
317
+ CatalogEntry: {
318
+ type: 'object',
319
+ properties: {
320
+ platform: { type: 'string', example: 'claude' },
321
+ id: { type: 'string', example: 'CL-A01' },
322
+ key: { type: 'string', example: 'claudeMd' },
323
+ name: { type: 'string', example: 'CLAUDE.md project instructions' },
324
+ category: { type: 'string', example: 'memory' },
325
+ impact: { type: 'string', example: 'critical' },
326
+ fix: { type: 'string' },
327
+ sourceUrl: { type: 'string' },
328
+ confidence: { type: 'number', minimum: 0, maximum: 1 },
329
+ },
330
+ additionalProperties: true,
331
+ },
332
+ AuditStack: {
333
+ type: 'object',
334
+ properties: {
335
+ key: { type: 'string' },
336
+ label: { type: 'string' },
337
+ },
338
+ additionalProperties: true,
339
+ },
340
+ AuditAction: {
341
+ type: 'object',
342
+ properties: {
343
+ key: { type: 'string' },
344
+ name: { type: 'string' },
345
+ impact: { type: 'string' },
346
+ fix: { type: 'string' },
347
+ },
348
+ additionalProperties: true,
349
+ },
350
+ AuditCheck: {
351
+ type: 'object',
352
+ properties: {
353
+ key: { type: 'string' },
354
+ name: { type: 'string' },
355
+ impact: { type: 'string' },
356
+ category: { type: 'string' },
357
+ passed: {
358
+ oneOf: [
359
+ { type: 'boolean' },
360
+ { type: 'null' },
361
+ ],
362
+ },
363
+ },
364
+ additionalProperties: true,
365
+ },
366
+ AuditPayload: {
367
+ type: 'object',
368
+ properties: {
369
+ platform: { type: 'string', example: 'claude' },
370
+ platformLabel: { type: 'string', example: 'Claude Code' },
371
+ score: { type: 'integer', minimum: 0, maximum: 100 },
372
+ organicScore: { type: 'integer', minimum: 0, maximum: 100 },
373
+ earnedPoints: { type: 'integer' },
374
+ maxPoints: { type: 'integer' },
375
+ isScaffolded: { type: 'boolean' },
376
+ passed: { type: 'integer' },
377
+ failed: { type: 'integer' },
378
+ skipped: { type: 'integer' },
379
+ checkCount: { type: 'integer' },
380
+ stacks: {
381
+ type: 'array',
382
+ items: { $ref: '#/components/schemas/AuditStack' },
383
+ },
384
+ topNextActions: {
385
+ type: 'array',
386
+ items: { $ref: '#/components/schemas/AuditAction' },
387
+ },
388
+ results: {
389
+ type: 'array',
390
+ items: { $ref: '#/components/schemas/AuditCheck' },
391
+ },
392
+ },
393
+ additionalProperties: true,
394
+ },
395
+ HarmonyRecommendation: {
396
+ type: 'object',
397
+ properties: {
398
+ priority: { type: 'string' },
399
+ category: { type: 'string' },
400
+ message: { type: 'string' },
401
+ },
402
+ additionalProperties: true,
403
+ },
404
+ ActivePlatform: {
405
+ type: 'object',
406
+ properties: {
407
+ platform: { type: 'string' },
408
+ label: { type: 'string' },
409
+ },
410
+ additionalProperties: true,
411
+ },
412
+ HarmonyPayload: {
413
+ type: 'object',
414
+ properties: {
415
+ harmonyScore: { type: 'integer', minimum: 0, maximum: 100 },
416
+ platformScores: {
417
+ type: 'object',
418
+ additionalProperties: { type: 'integer' },
419
+ },
420
+ drift: {
421
+ type: 'object',
422
+ additionalProperties: true,
423
+ },
424
+ recommendations: {
425
+ type: 'array',
426
+ items: { $ref: '#/components/schemas/HarmonyRecommendation' },
427
+ },
428
+ activePlatforms: {
429
+ type: 'array',
430
+ items: { $ref: '#/components/schemas/ActivePlatform' },
431
+ },
432
+ },
433
+ additionalProperties: true,
434
+ },
435
+ HealthEnvelope: buildEnvelopeSchema({ $ref: '#/components/schemas/HealthPayload' }),
436
+ CatalogEnvelope: buildEnvelopeSchema({
437
+ type: 'array',
438
+ items: { $ref: '#/components/schemas/CatalogEntry' },
439
+ }),
440
+ AuditEnvelope: buildEnvelopeSchema({ $ref: '#/components/schemas/AuditPayload' }),
441
+ HarmonyEnvelope: buildEnvelopeSchema({ $ref: '#/components/schemas/HarmonyPayload' }),
442
+ },
443
+ },
444
+ };
445
+ }
446
+
447
+ function createServer(options = {}) {
448
+ const baseDir = path.resolve(options.baseDir || process.cwd());
449
+
450
+ return http.createServer(async (req, res) => {
451
+ res.setHeader('Access-Control-Allow-Origin', '*');
452
+ res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
453
+ res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
454
+ if (req.method === 'OPTIONS') { res.writeHead(204); res.end(); return; }
455
+
456
+ const requestUrl = new URL(req.url || '/', 'http://127.0.0.1');
457
+
458
+ if (req.method !== 'GET') {
459
+ sendJson(res, 405, { error: 'Method not allowed' });
460
+ return;
461
+ }
462
+
463
+ try {
464
+ if (requestUrl.pathname === '/api/openapi.json') {
465
+ sendJson(res, 200, buildServeOpenApiSpec({
466
+ serverUrl: `http://${req.headers.host || '127.0.0.1:3000'}`,
467
+ }));
468
+ return;
469
+ }
470
+
471
+ if (requestUrl.pathname === '/api/health') {
472
+ sendJson(res, 200, envelope({
473
+ status: 'ok',
474
+ version,
475
+ checks: getCatalog().length,
476
+ }));
477
+ return;
478
+ }
479
+
480
+ if (requestUrl.pathname === '/api/catalog') {
481
+ sendJson(res, 200, envelope(getCatalog()));
482
+ return;
483
+ }
484
+
485
+ if (requestUrl.pathname === '/api/audit') {
486
+ const dir = resolveRequestDir(baseDir, requestUrl.searchParams.get('dir'));
487
+ const platform = normalizePlatform(requestUrl.searchParams.get('platform'));
488
+ const result = await audit({ dir, platform, silent: true });
489
+ sendJson(res, 200, envelope(result));
490
+ return;
491
+ }
492
+
493
+ if (requestUrl.pathname === '/api/harmony') {
494
+ const dir = resolveRequestDir(baseDir, requestUrl.searchParams.get('dir'));
495
+ const result = await harmonyAudit({ dir, silent: true });
496
+ sendJson(res, 200, envelope(result));
497
+ return;
498
+ }
499
+
500
+ sendJson(res, 404, { error: 'Not found' });
501
+ } catch (error) {
502
+ sendJson(res, error.statusCode || 500, {
503
+ error: error.message,
504
+ });
505
+ }
506
+ });
507
+ }
508
+
509
+ function startServer(options = {}) {
510
+ const port = options.port == null ? 3000 : Number(options.port);
511
+ const host = options.host || '127.0.0.1';
512
+ const server = createServer(options);
513
+
514
+ return new Promise((resolve, reject) => {
515
+ server.once('error', reject);
516
+ server.listen(port, host, () => {
517
+ server.off('error', reject);
518
+ resolve(server);
519
+ });
520
+ });
521
+ }
522
+
523
+ module.exports = {
524
+ buildServeOpenApiSpec,
525
+ createServer,
526
+ startServer,
527
+ };