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
@@ -0,0 +1,97 @@
1
+ /**
2
+ * cog validate - Validate a Cognitive Module's structure and examples
3
+ *
4
+ * Aligns with Python CLI's `cog validate` command.
5
+ */
6
+ import { findModule, getDefaultSearchPaths } from '../modules/index.js';
7
+ import { validateModule } from '../modules/validator.js';
8
+ /**
9
+ * Validate a cognitive module's structure and examples.
10
+ *
11
+ * @param nameOrPath Module name or path
12
+ * @param ctx Command context
13
+ * @param options Validation options
14
+ * @returns Validation result
15
+ */
16
+ export async function validate(nameOrPath, ctx, options = {}) {
17
+ try {
18
+ let modulePath;
19
+ let moduleName;
20
+ // Try to find as a named module first
21
+ const searchPaths = getDefaultSearchPaths(ctx.cwd);
22
+ const module = await findModule(nameOrPath, searchPaths);
23
+ if (module) {
24
+ modulePath = module.location;
25
+ moduleName = module.name;
26
+ }
27
+ else {
28
+ // Treat as a path
29
+ modulePath = nameOrPath;
30
+ }
31
+ // Run validation
32
+ const result = await validateModule(modulePath, options.v22 ?? false);
33
+ const validateResult = {
34
+ valid: result.valid,
35
+ modulePath,
36
+ moduleName,
37
+ errors: result.errors,
38
+ warnings: result.warnings,
39
+ };
40
+ return {
41
+ success: result.valid,
42
+ data: validateResult,
43
+ error: result.valid ? undefined : `Validation failed with ${result.errors.length} error(s)`,
44
+ };
45
+ }
46
+ catch (e) {
47
+ return {
48
+ success: false,
49
+ error: e instanceof Error ? e.message : String(e),
50
+ };
51
+ }
52
+ }
53
+ /**
54
+ * Validate all modules in the search paths.
55
+ *
56
+ * @param ctx Command context
57
+ * @param options Validation options
58
+ * @returns Batch validation results
59
+ */
60
+ export async function validateAll(ctx, options = {}) {
61
+ try {
62
+ const { listModules } = await import('../modules/loader.js');
63
+ const searchPaths = getDefaultSearchPaths(ctx.cwd);
64
+ const modules = await listModules(searchPaths);
65
+ const results = [];
66
+ let allValid = true;
67
+ for (const module of modules) {
68
+ const result = await validateModule(module.location, options.v22 ?? false);
69
+ results.push({
70
+ valid: result.valid,
71
+ modulePath: module.location,
72
+ moduleName: module.name,
73
+ errors: result.errors,
74
+ warnings: result.warnings,
75
+ });
76
+ if (!result.valid) {
77
+ allValid = false;
78
+ }
79
+ }
80
+ return {
81
+ success: allValid,
82
+ data: {
83
+ total: modules.length,
84
+ valid: results.filter(r => r.valid).length,
85
+ invalid: results.filter(r => !r.valid).length,
86
+ results,
87
+ },
88
+ error: allValid ? undefined : `${results.filter(r => !r.valid).length} module(s) failed validation`,
89
+ };
90
+ }
91
+ catch (e) {
92
+ return {
93
+ success: false,
94
+ error: e instanceof Error ? e.message : String(e),
95
+ };
96
+ }
97
+ }
@@ -0,0 +1,218 @@
1
+ /**
2
+ * Cognitive Modules - Unified Error Handling
3
+ *
4
+ * Provides consistent error structures across HTTP, MCP, and CLI layers.
5
+ * Based on ERROR-CODES.md specification.
6
+ */
7
+ import type { EnvelopeMeta, EnvelopeError, RiskLevel } from '../types.js';
8
+ /**
9
+ * Standard error codes per ERROR-CODES.md specification.
10
+ *
11
+ * Format: E{category}{sequence}
12
+ * - Category 1: Input errors
13
+ * - Category 2: Processing errors
14
+ * - Category 3: Output errors
15
+ * - Category 4: Runtime errors
16
+ * - Category 5-9: Module-specific (reserved)
17
+ */
18
+ export declare const ErrorCodes: {
19
+ readonly PARSE_ERROR: "E1000";
20
+ readonly INVALID_INPUT: "E1001";
21
+ readonly MISSING_REQUIRED_FIELD: "E1002";
22
+ readonly TYPE_MISMATCH: "E1003";
23
+ readonly UNSUPPORTED_VALUE: "E1004";
24
+ readonly INPUT_TOO_LARGE: "E1005";
25
+ readonly INVALID_REFERENCE: "E1006";
26
+ readonly LOW_CONFIDENCE: "E2001";
27
+ readonly TIMEOUT: "E2002";
28
+ readonly TOKEN_LIMIT: "E2003";
29
+ readonly NO_ACTION_POSSIBLE: "E2004";
30
+ readonly SEMANTIC_CONFLICT: "E2005";
31
+ readonly AMBIGUOUS_INPUT: "E2006";
32
+ readonly INSUFFICIENT_CONTEXT: "E2007";
33
+ readonly OUTPUT_SCHEMA_VIOLATION: "E3001";
34
+ readonly PARTIAL_RESULT: "E3002";
35
+ readonly MISSING_RATIONALE: "E3003";
36
+ readonly OVERFLOW_LIMIT: "E3004";
37
+ readonly INVALID_ENUM: "E3005";
38
+ readonly CONSTRAINT_VIOLATION: "E3006";
39
+ readonly INTERNAL_ERROR: "E4000";
40
+ readonly PROVIDER_UNAVAILABLE: "E4001";
41
+ readonly RATE_LIMITED: "E4002";
42
+ readonly CONTEXT_OVERFLOW: "E4003";
43
+ readonly CIRCULAR_DEPENDENCY: "E4004";
44
+ readonly MAX_DEPTH_EXCEEDED: "E4005";
45
+ readonly MODULE_NOT_FOUND: "E4006";
46
+ readonly PERMISSION_DENIED: "E4007";
47
+ readonly ENDPOINT_NOT_FOUND: "E4008";
48
+ readonly RESOURCE_NOT_FOUND: "E4009";
49
+ };
50
+ export type ErrorCode = typeof ErrorCodes[keyof typeof ErrorCodes];
51
+ /**
52
+ * Normalize error code to E-format.
53
+ * Accepts both legacy string codes and E-format codes.
54
+ */
55
+ export declare function normalizeErrorCode(code: string): ErrorCode;
56
+ /**
57
+ * Standard error envelope structure for all layers.
58
+ *
59
+ * Use this as the canonical error response format:
60
+ * - HTTP API: Return as JSON body with appropriate status code
61
+ * - MCP: Return as content[0].text (JSON stringified)
62
+ * - CLI: Convert to user-friendly message using toCliError()
63
+ */
64
+ export interface CognitiveErrorEnvelope {
65
+ ok: false;
66
+ version: string;
67
+ meta: EnvelopeMeta;
68
+ error: EnvelopeError;
69
+ partial_data?: unknown;
70
+ }
71
+ export interface EnvelopeContext {
72
+ module?: string;
73
+ provider?: string;
74
+ }
75
+ export type ContextualErrorEnvelope = CognitiveErrorEnvelope & EnvelopeContext;
76
+ /**
77
+ * Options for creating an error envelope.
78
+ * Exported for external use in type-safe error creation.
79
+ */
80
+ export interface ErrorEnvelopeOptions {
81
+ code: ErrorCode | string;
82
+ message: string;
83
+ recoverable?: boolean;
84
+ suggestion?: string;
85
+ retry_after_ms?: number;
86
+ details?: Record<string, unknown>;
87
+ partial_data?: unknown;
88
+ explain?: string;
89
+ confidence?: number;
90
+ risk?: RiskLevel;
91
+ trace_id?: string;
92
+ version?: string;
93
+ }
94
+ /**
95
+ * Create a standardized error envelope.
96
+ *
97
+ * @example
98
+ * // Simple error
99
+ * makeErrorEnvelope({
100
+ * code: ErrorCodes.MODULE_NOT_FOUND,
101
+ * message: "Module 'code-reviewer' not found",
102
+ * suggestion: "Use 'cog list' to see available modules"
103
+ * });
104
+ *
105
+ * @example
106
+ * // Error with retry info
107
+ * makeErrorEnvelope({
108
+ * code: ErrorCodes.RATE_LIMITED,
109
+ * message: "Rate limit exceeded",
110
+ * retry_after_ms: 60000
111
+ * });
112
+ */
113
+ export declare function makeErrorEnvelope(options: ErrorEnvelopeOptions): CognitiveErrorEnvelope;
114
+ export declare function attachContext<T extends object>(envelope: T, context?: EnvelopeContext): T & EnvelopeContext;
115
+ /**
116
+ * Create error envelope for HTTP API responses.
117
+ *
118
+ * @returns Tuple of [statusCode, body]
119
+ */
120
+ export declare function makeHttpError(options: ErrorEnvelopeOptions & EnvelopeContext): [number, ContextualErrorEnvelope];
121
+ /**
122
+ * Create error envelope for MCP tool responses.
123
+ */
124
+ export declare function makeMcpError(options: ErrorEnvelopeOptions & EnvelopeContext): {
125
+ content: Array<{
126
+ type: 'text';
127
+ text: string;
128
+ }>;
129
+ };
130
+ /**
131
+ * Convert error envelope to CLI-friendly error message.
132
+ */
133
+ export declare function toCliError(envelope: CognitiveErrorEnvelope): string;
134
+ /**
135
+ * Convert CLI CommandResult-style error to standard envelope.
136
+ * Used for backward compatibility during migration.
137
+ */
138
+ export declare function fromCliError(errorMessage: string, code?: ErrorCode): CognitiveErrorEnvelope;
139
+ /**
140
+ * Check if an error envelope indicates a recoverable error.
141
+ */
142
+ export declare function isRecoverable(envelope: CognitiveErrorEnvelope): boolean;
143
+ /**
144
+ * Check if an error envelope has partial data.
145
+ */
146
+ export declare function hasPartialData(envelope: CognitiveErrorEnvelope): boolean;
147
+ /**
148
+ * Check if an error envelope suggests retrying.
149
+ */
150
+ export declare function shouldRetry(envelope: CognitiveErrorEnvelope): boolean;
151
+ /**
152
+ * Options for creating a success envelope.
153
+ */
154
+ export interface SuccessEnvelopeOptions<T = unknown> {
155
+ data: T;
156
+ confidence?: number;
157
+ risk?: RiskLevel;
158
+ explain?: string;
159
+ trace_id?: string;
160
+ version?: string;
161
+ }
162
+ /**
163
+ * Standard success envelope structure.
164
+ */
165
+ export interface CognitiveSuccessEnvelope<T = unknown> {
166
+ ok: true;
167
+ version: string;
168
+ meta: EnvelopeMeta;
169
+ data: T;
170
+ }
171
+ /**
172
+ * Create a standardized success envelope.
173
+ * Use this for consistent success responses across HTTP and MCP layers.
174
+ */
175
+ export declare function makeSuccessEnvelope<T>(options: SuccessEnvelopeOptions<T>): CognitiveSuccessEnvelope<T>;
176
+ /**
177
+ * Create success envelope for MCP tool responses.
178
+ */
179
+ export declare function makeMcpSuccess<T>(options: SuccessEnvelopeOptions<T>): {
180
+ content: Array<{
181
+ type: 'text';
182
+ text: string;
183
+ }>;
184
+ };
185
+ /**
186
+ * Create MODULE_NOT_FOUND error.
187
+ */
188
+ export declare function moduleNotFoundError(moduleName: string, options?: {
189
+ suggestion?: string;
190
+ trace_id?: string;
191
+ }): CognitiveErrorEnvelope;
192
+ /**
193
+ * Create PARSE_ERROR error.
194
+ */
195
+ export declare function parseError(details?: string, options?: {
196
+ trace_id?: string;
197
+ }): CognitiveErrorEnvelope;
198
+ /**
199
+ * Create RATE_LIMITED error.
200
+ */
201
+ export declare function rateLimitedError(retryAfterMs: number, provider?: string): CognitiveErrorEnvelope;
202
+ /**
203
+ * Create INTERNAL_ERROR error.
204
+ */
205
+ export declare function internalError(message: string, options?: {
206
+ details?: Record<string, unknown>;
207
+ trace_id?: string;
208
+ }): CognitiveErrorEnvelope;
209
+ /**
210
+ * Create MISSING_REQUIRED_FIELD error.
211
+ */
212
+ export declare function missingFieldError(fieldName: string, options?: {
213
+ suggestion?: string;
214
+ }): CognitiveErrorEnvelope;
215
+ /**
216
+ * Create PERMISSION_DENIED error.
217
+ */
218
+ export declare function permissionDeniedError(reason: string): CognitiveErrorEnvelope;