cognitive-modules-cli 2.2.0 → 2.2.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 (94) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/LICENSE +21 -0
  3. package/README.md +35 -29
  4. package/dist/cli.js +572 -28
  5. package/dist/commands/add.d.ts +33 -14
  6. package/dist/commands/add.js +222 -13
  7. package/dist/commands/compose.d.ts +31 -0
  8. package/dist/commands/compose.js +185 -0
  9. package/dist/commands/index.d.ts +5 -0
  10. package/dist/commands/index.js +5 -0
  11. package/dist/commands/init.js +23 -1
  12. package/dist/commands/migrate.d.ts +30 -0
  13. package/dist/commands/migrate.js +650 -0
  14. package/dist/commands/pipe.d.ts +1 -0
  15. package/dist/commands/pipe.js +31 -11
  16. package/dist/commands/remove.js +33 -2
  17. package/dist/commands/run.d.ts +1 -0
  18. package/dist/commands/run.js +37 -27
  19. package/dist/commands/search.d.ts +28 -0
  20. package/dist/commands/search.js +143 -0
  21. package/dist/commands/test.d.ts +65 -0
  22. package/dist/commands/test.js +454 -0
  23. package/dist/commands/update.d.ts +1 -0
  24. package/dist/commands/update.js +106 -14
  25. package/dist/commands/validate.d.ts +36 -0
  26. package/dist/commands/validate.js +97 -0
  27. package/dist/errors/index.d.ts +218 -0
  28. package/dist/errors/index.js +412 -0
  29. package/dist/index.d.ts +2 -2
  30. package/dist/index.js +5 -1
  31. package/dist/mcp/server.js +84 -79
  32. package/dist/modules/composition.d.ts +251 -0
  33. package/dist/modules/composition.js +1330 -0
  34. package/dist/modules/index.d.ts +2 -0
  35. package/dist/modules/index.js +2 -0
  36. package/dist/modules/loader.d.ts +22 -2
  37. package/dist/modules/loader.js +171 -6
  38. package/dist/modules/runner.d.ts +422 -1
  39. package/dist/modules/runner.js +1472 -71
  40. package/dist/modules/subagent.d.ts +6 -1
  41. package/dist/modules/subagent.js +20 -13
  42. package/dist/modules/validator.d.ts +28 -0
  43. package/dist/modules/validator.js +637 -0
  44. package/dist/providers/anthropic.d.ts +15 -0
  45. package/dist/providers/anthropic.js +147 -5
  46. package/dist/providers/base.d.ts +11 -0
  47. package/dist/providers/base.js +18 -0
  48. package/dist/providers/gemini.d.ts +15 -0
  49. package/dist/providers/gemini.js +122 -5
  50. package/dist/providers/ollama.d.ts +15 -0
  51. package/dist/providers/ollama.js +111 -3
  52. package/dist/providers/openai.d.ts +11 -0
  53. package/dist/providers/openai.js +133 -0
  54. package/dist/registry/client.d.ts +204 -0
  55. package/dist/registry/client.js +356 -0
  56. package/dist/registry/index.d.ts +4 -0
  57. package/dist/registry/index.js +4 -0
  58. package/dist/server/http.js +173 -42
  59. package/dist/types.d.ts +123 -8
  60. package/dist/types.js +4 -1
  61. package/dist/version.d.ts +1 -0
  62. package/dist/version.js +4 -0
  63. package/package.json +32 -7
  64. package/src/cli.ts +0 -410
  65. package/src/commands/add.ts +0 -315
  66. package/src/commands/index.ts +0 -12
  67. package/src/commands/init.ts +0 -94
  68. package/src/commands/list.ts +0 -33
  69. package/src/commands/pipe.ts +0 -76
  70. package/src/commands/remove.ts +0 -57
  71. package/src/commands/run.ts +0 -80
  72. package/src/commands/update.ts +0 -130
  73. package/src/commands/versions.ts +0 -79
  74. package/src/index.ts +0 -55
  75. package/src/mcp/index.ts +0 -5
  76. package/src/mcp/server.ts +0 -403
  77. package/src/modules/index.ts +0 -7
  78. package/src/modules/loader.ts +0 -318
  79. package/src/modules/runner.ts +0 -495
  80. package/src/modules/subagent.ts +0 -275
  81. package/src/providers/anthropic.ts +0 -89
  82. package/src/providers/base.ts +0 -29
  83. package/src/providers/deepseek.ts +0 -83
  84. package/src/providers/gemini.ts +0 -117
  85. package/src/providers/index.ts +0 -78
  86. package/src/providers/minimax.ts +0 -81
  87. package/src/providers/moonshot.ts +0 -82
  88. package/src/providers/ollama.ts +0 -83
  89. package/src/providers/openai.ts +0 -84
  90. package/src/providers/qwen.ts +0 -82
  91. package/src/server/http.ts +0 -316
  92. package/src/server/index.ts +0 -6
  93. package/src/types.ts +0 -495
  94. package/tsconfig.json +0 -17
package/src/types.ts DELETED
@@ -1,495 +0,0 @@
1
- /**
2
- * Cognitive Runtime - Core Types
3
- * Version 2.2 - With Control/Data plane separation, tier, overflow, extensible enums
4
- */
5
-
6
- // =============================================================================
7
- // Provider Interface
8
- // =============================================================================
9
-
10
- export interface Provider {
11
- name: string;
12
- invoke(params: InvokeParams): Promise<InvokeResult>;
13
- isConfigured(): boolean;
14
- }
15
-
16
- export interface InvokeParams {
17
- messages: Message[];
18
- jsonSchema?: object;
19
- temperature?: number;
20
- maxTokens?: number;
21
- }
22
-
23
- export interface Message {
24
- role: 'system' | 'user' | 'assistant';
25
- content: string;
26
- }
27
-
28
- export interface InvokeResult {
29
- content: string;
30
- usage?: {
31
- promptTokens: number;
32
- completionTokens: number;
33
- totalTokens: number;
34
- };
35
- }
36
-
37
- // =============================================================================
38
- // v2.2 Core Types
39
- // =============================================================================
40
-
41
- /** Module classification for schema strictness */
42
- export type ModuleTier = 'exec' | 'decision' | 'exploration';
43
-
44
- /** Schema validation strictness level */
45
- export type SchemaStrictness = 'high' | 'medium' | 'low';
46
-
47
- /** Risk level (used in both meta and changes) */
48
- export type RiskLevel = 'none' | 'low' | 'medium' | 'high';
49
-
50
- /** Enum extension strategy */
51
- export type EnumStrategy = 'strict' | 'extensible';
52
-
53
- /** Risk aggregation rule */
54
- export type RiskRule = 'max_changes_risk' | 'max_issues_risk' | 'explicit';
55
-
56
- // =============================================================================
57
- // Module Configuration (v2.2)
58
- // =============================================================================
59
-
60
- export interface CognitiveModule {
61
- // Core identity
62
- name: string;
63
- version: string;
64
- responsibility: string;
65
-
66
- // Constraints
67
- excludes: string[];
68
- constraints?: ModuleConstraints;
69
-
70
- // Unified policies (v2.1+)
71
- policies?: ModulePolicies;
72
-
73
- // Tools policy
74
- tools?: ToolsPolicy;
75
-
76
- // Output contract
77
- output?: OutputContract;
78
-
79
- // Failure contract
80
- failure?: FailureContract;
81
-
82
- // Runtime requirements
83
- runtimeRequirements?: RuntimeRequirements;
84
-
85
- // v2.2: Module tier
86
- tier?: ModuleTier;
87
-
88
- // v2.2: Schema strictness
89
- schemaStrictness?: SchemaStrictness;
90
-
91
- // v2.2: Overflow configuration
92
- overflow?: OverflowConfig;
93
-
94
- // v2.2: Enum configuration
95
- enums?: EnumConfig;
96
-
97
- // v2.2: Compatibility configuration
98
- compat?: CompatConfig;
99
-
100
- // v2.2: Meta configuration (including risk_rule)
101
- metaConfig?: MetaConfig;
102
-
103
- // Execution context
104
- context?: 'fork' | 'main';
105
-
106
- // Prompt (from prompt.md or MODULE.md body)
107
- prompt: string;
108
-
109
- // Schemas
110
- inputSchema?: object;
111
- outputSchema?: object; // v2.1 compat
112
- dataSchema?: object; // v2.2: same as outputSchema
113
- metaSchema?: object; // v2.2: control plane schema
114
- errorSchema?: object;
115
-
116
- // Metadata
117
- location: string;
118
- format: 'v0' | 'v1' | 'v2';
119
- formatVersion?: string; // v2.0, v2.1, v2.2
120
- }
121
-
122
- export interface ModuleConstraints {
123
- no_network?: boolean;
124
- no_side_effects?: boolean;
125
- no_file_write?: boolean;
126
- no_inventing_data?: boolean;
127
- behavior_equivalence_false_max_confidence?: number;
128
- }
129
-
130
- export interface ModulePolicies {
131
- network?: 'allow' | 'deny';
132
- filesystem_write?: 'allow' | 'deny';
133
- side_effects?: 'allow' | 'deny';
134
- code_execution?: 'allow' | 'deny';
135
- }
136
-
137
- export interface ToolsPolicy {
138
- policy?: 'allow_by_default' | 'deny_by_default';
139
- allowed: string[];
140
- denied?: string[];
141
- }
142
-
143
- export interface OutputContract {
144
- format?: 'json_strict' | 'json_lenient' | 'text';
145
- envelope?: boolean;
146
- require?: string[];
147
- require_confidence?: boolean;
148
- require_rationale?: boolean;
149
- require_behavior_equivalence?: boolean;
150
- }
151
-
152
- export interface FailureContract {
153
- contract?: 'error_union' | 'throw';
154
- partial_allowed?: boolean;
155
- must_return_error_schema?: boolean;
156
- schema?: object;
157
- }
158
-
159
- export interface RuntimeRequirements {
160
- structured_output?: boolean;
161
- max_input_tokens?: number;
162
- preferred_capabilities?: string[];
163
- }
164
-
165
- // =============================================================================
166
- // v2.2 New Configuration Types
167
- // =============================================================================
168
-
169
- /** Overflow configuration for extensions.insights */
170
- export interface OverflowConfig {
171
- enabled: boolean;
172
- recoverable?: boolean;
173
- max_items?: number;
174
- require_suggested_mapping?: boolean;
175
- }
176
-
177
- /** Enum extension configuration */
178
- export interface EnumConfig {
179
- strategy: EnumStrategy;
180
- unknown_tag?: string; // How to represent unknown enums (default: "custom")
181
- }
182
-
183
- /** Compatibility configuration for migration */
184
- export interface CompatConfig {
185
- accepts_v21_payload?: boolean;
186
- runtime_auto_wrap?: boolean;
187
- schema_output_alias?: 'data' | 'output';
188
- }
189
-
190
- /** Meta field configuration (v2.2) */
191
- export interface MetaConfig {
192
- required?: string[];
193
- risk_rule?: RiskRule;
194
- confidence?: { min?: number; max?: number };
195
- explain?: { max_chars?: number };
196
- }
197
-
198
- // =============================================================================
199
- // Envelope Types (v2.2)
200
- // =============================================================================
201
-
202
- /**
203
- * Control plane metadata - unified across all modules.
204
- * Used for routing, logging, UI cards, and middleware decisions.
205
- */
206
- export interface EnvelopeMeta {
207
- /** Confidence score [0, 1] - unified across all modules */
208
- confidence: number;
209
-
210
- /** Aggregated risk level: max(changes[*].risk) */
211
- risk: RiskLevel;
212
-
213
- /** Short explanation for middleware/UI (max 280 chars) */
214
- explain: string;
215
-
216
- /** Distributed tracing ID */
217
- trace_id?: string;
218
-
219
- /** Provider and model identifier */
220
- model?: string;
221
-
222
- /** Execution latency in milliseconds */
223
- latency_ms?: number;
224
- }
225
-
226
- /** Success response in v2.2 envelope format */
227
- export interface EnvelopeSuccessV22<T = unknown> {
228
- ok: true;
229
- meta: EnvelopeMeta;
230
- data: T;
231
- }
232
-
233
- /** Error response in v2.2 envelope format */
234
- export interface EnvelopeErrorV22 {
235
- ok: false;
236
- meta: EnvelopeMeta;
237
- error: {
238
- code: string;
239
- message: string;
240
- };
241
- partial_data?: unknown;
242
- }
243
-
244
- /** v2.2 envelope response (union type) */
245
- export type EnvelopeResponseV22<T = unknown> = EnvelopeSuccessV22<T> | EnvelopeErrorV22;
246
-
247
- // =============================================================================
248
- // Legacy Envelope Types (v2.1 - for backward compatibility)
249
- // =============================================================================
250
-
251
- export interface EnvelopeSuccessV21<T = unknown> {
252
- ok: true;
253
- data: T;
254
- }
255
-
256
- export interface EnvelopeErrorV21 {
257
- ok: false;
258
- error: {
259
- code: string;
260
- message: string;
261
- };
262
- partial_data?: unknown;
263
- }
264
-
265
- export type EnvelopeResponseV21<T = unknown> = EnvelopeSuccessV21<T> | EnvelopeErrorV21;
266
-
267
- /** Generic envelope response (supports both v2.1 and v2.2) */
268
- export type EnvelopeResponse<T = unknown> = EnvelopeResponseV22<T> | EnvelopeResponseV21<T>;
269
-
270
- // =============================================================================
271
- // Overflow Types (v2.2)
272
- // =============================================================================
273
-
274
- /** An insight that doesn't fit the schema but is valuable */
275
- export interface Insight {
276
- /** The observation or insight */
277
- text: string;
278
-
279
- /** Suggested field/enum to add to schema for future versions */
280
- suggested_mapping: string;
281
-
282
- /** Supporting evidence for this insight */
283
- evidence?: string;
284
- }
285
-
286
- /** Extensions container for overflow data */
287
- export interface Extensions {
288
- insights?: Insight[];
289
- }
290
-
291
- // =============================================================================
292
- // Extensible Enum Pattern (v2.2)
293
- // =============================================================================
294
-
295
- /**
296
- * Extensible enum type - allows both predefined values and custom extensions.
297
- *
298
- * Usage:
299
- * type ChangeType = ExtensibleEnum<'remove_redundancy' | 'simplify_logic' | 'other'>;
300
- *
301
- * Valid values:
302
- * - "remove_redundancy" (predefined)
303
- * - { custom: "inline_callback", reason: "Converted callback to arrow function" }
304
- */
305
- export type ExtensibleEnum<T extends string> = T | { custom: string; reason: string };
306
-
307
- // =============================================================================
308
- // Module Result Types
309
- // =============================================================================
310
-
311
- /** Base interface for module result data */
312
- export interface ModuleResultData {
313
- [key: string]: unknown;
314
- rationale: string;
315
- extensions?: Extensions;
316
- }
317
-
318
- /** v2.2 module result with meta and data separation */
319
- export interface ModuleResultV22 {
320
- ok: boolean;
321
- meta: EnvelopeMeta;
322
- data?: ModuleResultData;
323
- error?: {
324
- code: string;
325
- message: string;
326
- };
327
- partial_data?: unknown;
328
- raw?: string;
329
- }
330
-
331
- /** Legacy module result (v2.1) */
332
- export interface ModuleResultV21 {
333
- ok: boolean;
334
- data?: ModuleResultData & { confidence: number };
335
- error?: {
336
- code: string;
337
- message: string;
338
- };
339
- partial_data?: unknown;
340
- raw?: string;
341
- }
342
-
343
- /** Generic module result */
344
- export type ModuleResult = ModuleResultV22 | ModuleResultV21;
345
-
346
- // =============================================================================
347
- // Legacy Types (for backward compatibility)
348
- // =============================================================================
349
-
350
- export interface LegacyModuleResult {
351
- output: unknown;
352
- confidence: number;
353
- rationale: string;
354
- behaviorEquivalence?: boolean;
355
- raw?: string;
356
- }
357
-
358
- // =============================================================================
359
- // Command Types
360
- // =============================================================================
361
-
362
- export interface CommandContext {
363
- cwd: string;
364
- provider: Provider;
365
- verbose?: boolean;
366
- }
367
-
368
- export interface CommandResult {
369
- success: boolean;
370
- data?: unknown;
371
- error?: string;
372
- }
373
-
374
- // =============================================================================
375
- // Module Input
376
- // =============================================================================
377
-
378
- export interface ModuleInput {
379
- code?: string;
380
- query?: string;
381
- language?: string;
382
- options?: Record<string, unknown>;
383
- [key: string]: unknown;
384
- }
385
-
386
- // =============================================================================
387
- // Utility Types
388
- // =============================================================================
389
-
390
- /** Check if response is v2.2 format */
391
- export function isV22Envelope<T>(response: EnvelopeResponse<T>): response is EnvelopeResponseV22<T> {
392
- return 'meta' in response;
393
- }
394
-
395
- /** Check if response is successful */
396
- export function isEnvelopeSuccess<T>(
397
- response: EnvelopeResponse<T>
398
- ): response is EnvelopeSuccessV22<T> | EnvelopeSuccessV21<T> {
399
- return response.ok === true;
400
- }
401
-
402
- /** Extract meta from any envelope response */
403
- export function extractMeta<T>(
404
- response: EnvelopeResponse<T>,
405
- riskRule: RiskRule = 'max_changes_risk'
406
- ): EnvelopeMeta {
407
- if (isV22Envelope(response)) {
408
- return response.meta;
409
- }
410
-
411
- // Synthesize meta from v2.1 response
412
- if (response.ok) {
413
- const data = (response.data ?? {}) as Record<string, unknown>;
414
- return {
415
- confidence: (data.confidence as number) ?? 0.5,
416
- risk: aggregateRisk(data, riskRule),
417
- explain: ((data.rationale as string) ?? '').slice(0, 280) || 'No explanation',
418
- };
419
- } else {
420
- return {
421
- confidence: 0,
422
- risk: 'high',
423
- explain: response.error?.message?.slice(0, 280) ?? 'Error occurred',
424
- };
425
- }
426
- }
427
-
428
- /** Aggregate risk from list of items */
429
- function aggregateRiskFromList(items: Array<{ risk?: RiskLevel }>): RiskLevel {
430
- const riskLevels: Record<RiskLevel, number> = { none: 0, low: 1, medium: 2, high: 3 };
431
- const riskNames: RiskLevel[] = ['none', 'low', 'medium', 'high'];
432
-
433
- if (!items || items.length === 0) {
434
- return 'medium';
435
- }
436
-
437
- let maxLevel = 0;
438
- for (const item of items) {
439
- const level = riskLevels[item.risk ?? 'medium'];
440
- maxLevel = Math.max(maxLevel, level);
441
- }
442
-
443
- return riskNames[maxLevel];
444
- }
445
-
446
- /**
447
- * Aggregate risk based on configured rule.
448
- *
449
- * Rules:
450
- * - max_changes_risk: max(data.changes[*].risk) - default
451
- * - max_issues_risk: max(data.issues[*].risk) - for review modules
452
- * - explicit: return "medium", module should set risk explicitly
453
- */
454
- export function aggregateRisk(
455
- data: Record<string, unknown>,
456
- riskRule: RiskRule = 'max_changes_risk'
457
- ): RiskLevel {
458
- if (riskRule === 'max_changes_risk') {
459
- const changes = (data.changes as Array<{ risk?: RiskLevel }>) ?? [];
460
- return aggregateRiskFromList(changes);
461
- } else if (riskRule === 'max_issues_risk') {
462
- const issues = (data.issues as Array<{ risk?: RiskLevel }>) ?? [];
463
- return aggregateRiskFromList(issues);
464
- } else if (riskRule === 'explicit') {
465
- return 'medium'; // Module should override
466
- }
467
- // Fallback to changes
468
- const changes = (data.changes as Array<{ risk?: RiskLevel }>) ?? [];
469
- return aggregateRiskFromList(changes);
470
- }
471
-
472
- /** Check if result should be escalated to human review */
473
- export function shouldEscalate<T>(
474
- response: EnvelopeResponse<T>,
475
- confidenceThreshold: number = 0.7
476
- ): boolean {
477
- const meta = extractMeta(response);
478
-
479
- // Escalate if low confidence
480
- if (meta.confidence < confidenceThreshold) {
481
- return true;
482
- }
483
-
484
- // Escalate if high risk
485
- if (meta.risk === 'high') {
486
- return true;
487
- }
488
-
489
- // Escalate if error
490
- if (!response.ok) {
491
- return true;
492
- }
493
-
494
- return false;
495
- }
package/tsconfig.json DELETED
@@ -1,17 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2023",
4
- "lib": ["ES2023"],
5
- "module": "NodeNext",
6
- "moduleResolution": "NodeNext",
7
- "outDir": "./dist",
8
- "rootDir": "./src",
9
- "strict": true,
10
- "esModuleInterop": true,
11
- "skipLibCheck": true,
12
- "declaration": true,
13
- "resolveJsonModule": true
14
- },
15
- "include": ["src/**/*"],
16
- "exclude": ["node_modules", "dist"]
17
- }