cognitive-modules-cli 2.2.1 → 2.2.7
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.
- package/CHANGELOG.md +11 -0
- package/LICENSE +21 -0
- package/README.md +35 -29
- package/dist/cli.js +519 -23
- package/dist/commands/add.d.ts +33 -14
- package/dist/commands/add.js +383 -16
- package/dist/commands/compose.js +60 -23
- package/dist/commands/index.d.ts +4 -0
- package/dist/commands/index.js +4 -0
- package/dist/commands/init.js +23 -1
- package/dist/commands/migrate.d.ts +30 -0
- package/dist/commands/migrate.js +650 -0
- package/dist/commands/pipe.d.ts +1 -0
- package/dist/commands/pipe.js +31 -11
- package/dist/commands/remove.js +33 -2
- package/dist/commands/run.d.ts +2 -0
- package/dist/commands/run.js +61 -28
- package/dist/commands/search.d.ts +28 -0
- package/dist/commands/search.js +143 -0
- package/dist/commands/test.d.ts +65 -0
- package/dist/commands/test.js +454 -0
- package/dist/commands/update.d.ts +1 -0
- package/dist/commands/update.js +106 -14
- package/dist/commands/validate.d.ts +36 -0
- package/dist/commands/validate.js +97 -0
- package/dist/errors/index.d.ts +225 -0
- package/dist/errors/index.js +420 -0
- package/dist/mcp/server.js +84 -79
- package/dist/modules/composition.js +97 -32
- package/dist/modules/loader.js +4 -2
- package/dist/modules/runner.d.ts +72 -5
- package/dist/modules/runner.js +306 -59
- package/dist/modules/subagent.d.ts +6 -1
- package/dist/modules/subagent.js +18 -13
- package/dist/modules/validator.js +14 -6
- package/dist/providers/anthropic.d.ts +15 -0
- package/dist/providers/anthropic.js +147 -5
- package/dist/providers/base.d.ts +11 -0
- package/dist/providers/base.js +18 -0
- package/dist/providers/gemini.d.ts +15 -0
- package/dist/providers/gemini.js +122 -5
- package/dist/providers/ollama.d.ts +15 -0
- package/dist/providers/ollama.js +111 -3
- package/dist/providers/openai.d.ts +11 -0
- package/dist/providers/openai.js +133 -0
- package/dist/registry/client.d.ts +212 -0
- package/dist/registry/client.js +359 -0
- package/dist/registry/index.d.ts +4 -0
- package/dist/registry/index.js +4 -0
- package/dist/registry/tar.d.ts +8 -0
- package/dist/registry/tar.js +353 -0
- package/dist/server/http.js +301 -45
- package/dist/server/index.d.ts +2 -0
- package/dist/server/index.js +1 -0
- package/dist/server/sse.d.ts +13 -0
- package/dist/server/sse.js +22 -0
- package/dist/types.d.ts +32 -1
- package/dist/types.js +4 -1
- package/dist/version.d.ts +1 -0
- package/dist/version.js +4 -0
- package/package.json +31 -7
- package/dist/modules/composition.test.d.ts +0 -11
- package/dist/modules/composition.test.js +0 -450
- package/dist/modules/policy.test.d.ts +0 -10
- package/dist/modules/policy.test.js +0 -369
- package/src/cli.ts +0 -471
- package/src/commands/add.ts +0 -315
- package/src/commands/compose.ts +0 -185
- package/src/commands/index.ts +0 -13
- package/src/commands/init.ts +0 -94
- package/src/commands/list.ts +0 -33
- package/src/commands/pipe.ts +0 -76
- package/src/commands/remove.ts +0 -57
- package/src/commands/run.ts +0 -80
- package/src/commands/update.ts +0 -130
- package/src/commands/versions.ts +0 -79
- package/src/index.ts +0 -90
- package/src/mcp/index.ts +0 -5
- package/src/mcp/server.ts +0 -403
- package/src/modules/composition.test.ts +0 -558
- package/src/modules/composition.ts +0 -1674
- package/src/modules/index.ts +0 -9
- package/src/modules/loader.ts +0 -508
- package/src/modules/policy.test.ts +0 -455
- package/src/modules/runner.ts +0 -1983
- package/src/modules/subagent.ts +0 -277
- package/src/modules/validator.ts +0 -700
- package/src/providers/anthropic.ts +0 -89
- package/src/providers/base.ts +0 -29
- package/src/providers/deepseek.ts +0 -83
- package/src/providers/gemini.ts +0 -117
- package/src/providers/index.ts +0 -78
- package/src/providers/minimax.ts +0 -81
- package/src/providers/moonshot.ts +0 -82
- package/src/providers/ollama.ts +0 -83
- package/src/providers/openai.ts +0 -84
- package/src/providers/qwen.ts +0 -82
- package/src/server/http.ts +0 -316
- package/src/server/index.ts +0 -6
- package/src/types.ts +0 -599
- 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,225 @@
|
|
|
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
|
+
* Map a CEP error code to an HTTP status code.
|
|
117
|
+
*
|
|
118
|
+
* This is used to keep HTTP behavior consistent with the error model while
|
|
119
|
+
* allowing callers to attach context without rebuilding envelopes.
|
|
120
|
+
*/
|
|
121
|
+
export declare function httpStatusForErrorCode(code: string): number;
|
|
122
|
+
/**
|
|
123
|
+
* Create error envelope for HTTP API responses.
|
|
124
|
+
*
|
|
125
|
+
* @returns Tuple of [statusCode, body]
|
|
126
|
+
*/
|
|
127
|
+
export declare function makeHttpError(options: ErrorEnvelopeOptions & EnvelopeContext): [number, ContextualErrorEnvelope];
|
|
128
|
+
/**
|
|
129
|
+
* Create error envelope for MCP tool responses.
|
|
130
|
+
*/
|
|
131
|
+
export declare function makeMcpError(options: ErrorEnvelopeOptions & EnvelopeContext): {
|
|
132
|
+
content: Array<{
|
|
133
|
+
type: 'text';
|
|
134
|
+
text: string;
|
|
135
|
+
}>;
|
|
136
|
+
};
|
|
137
|
+
/**
|
|
138
|
+
* Convert error envelope to CLI-friendly error message.
|
|
139
|
+
*/
|
|
140
|
+
export declare function toCliError(envelope: CognitiveErrorEnvelope): string;
|
|
141
|
+
/**
|
|
142
|
+
* Convert CLI CommandResult-style error to standard envelope.
|
|
143
|
+
* Used for backward compatibility during migration.
|
|
144
|
+
*/
|
|
145
|
+
export declare function fromCliError(errorMessage: string, code?: ErrorCode): CognitiveErrorEnvelope;
|
|
146
|
+
/**
|
|
147
|
+
* Check if an error envelope indicates a recoverable error.
|
|
148
|
+
*/
|
|
149
|
+
export declare function isRecoverable(envelope: CognitiveErrorEnvelope): boolean;
|
|
150
|
+
/**
|
|
151
|
+
* Check if an error envelope has partial data.
|
|
152
|
+
*/
|
|
153
|
+
export declare function hasPartialData(envelope: CognitiveErrorEnvelope): boolean;
|
|
154
|
+
/**
|
|
155
|
+
* Check if an error envelope suggests retrying.
|
|
156
|
+
*/
|
|
157
|
+
export declare function shouldRetry(envelope: CognitiveErrorEnvelope): boolean;
|
|
158
|
+
/**
|
|
159
|
+
* Options for creating a success envelope.
|
|
160
|
+
*/
|
|
161
|
+
export interface SuccessEnvelopeOptions<T = unknown> {
|
|
162
|
+
data: T;
|
|
163
|
+
confidence?: number;
|
|
164
|
+
risk?: RiskLevel;
|
|
165
|
+
explain?: string;
|
|
166
|
+
trace_id?: string;
|
|
167
|
+
version?: string;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Standard success envelope structure.
|
|
171
|
+
*/
|
|
172
|
+
export interface CognitiveSuccessEnvelope<T = unknown> {
|
|
173
|
+
ok: true;
|
|
174
|
+
version: string;
|
|
175
|
+
meta: EnvelopeMeta;
|
|
176
|
+
data: T;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Create a standardized success envelope.
|
|
180
|
+
* Use this for consistent success responses across HTTP and MCP layers.
|
|
181
|
+
*/
|
|
182
|
+
export declare function makeSuccessEnvelope<T>(options: SuccessEnvelopeOptions<T>): CognitiveSuccessEnvelope<T>;
|
|
183
|
+
/**
|
|
184
|
+
* Create success envelope for MCP tool responses.
|
|
185
|
+
*/
|
|
186
|
+
export declare function makeMcpSuccess<T>(options: SuccessEnvelopeOptions<T>): {
|
|
187
|
+
content: Array<{
|
|
188
|
+
type: 'text';
|
|
189
|
+
text: string;
|
|
190
|
+
}>;
|
|
191
|
+
};
|
|
192
|
+
/**
|
|
193
|
+
* Create MODULE_NOT_FOUND error.
|
|
194
|
+
*/
|
|
195
|
+
export declare function moduleNotFoundError(moduleName: string, options?: {
|
|
196
|
+
suggestion?: string;
|
|
197
|
+
trace_id?: string;
|
|
198
|
+
}): CognitiveErrorEnvelope;
|
|
199
|
+
/**
|
|
200
|
+
* Create PARSE_ERROR error.
|
|
201
|
+
*/
|
|
202
|
+
export declare function parseError(details?: string, options?: {
|
|
203
|
+
trace_id?: string;
|
|
204
|
+
}): CognitiveErrorEnvelope;
|
|
205
|
+
/**
|
|
206
|
+
* Create RATE_LIMITED error.
|
|
207
|
+
*/
|
|
208
|
+
export declare function rateLimitedError(retryAfterMs: number, provider?: string): CognitiveErrorEnvelope;
|
|
209
|
+
/**
|
|
210
|
+
* Create INTERNAL_ERROR error.
|
|
211
|
+
*/
|
|
212
|
+
export declare function internalError(message: string, options?: {
|
|
213
|
+
details?: Record<string, unknown>;
|
|
214
|
+
trace_id?: string;
|
|
215
|
+
}): CognitiveErrorEnvelope;
|
|
216
|
+
/**
|
|
217
|
+
* Create MISSING_REQUIRED_FIELD error.
|
|
218
|
+
*/
|
|
219
|
+
export declare function missingFieldError(fieldName: string, options?: {
|
|
220
|
+
suggestion?: string;
|
|
221
|
+
}): CognitiveErrorEnvelope;
|
|
222
|
+
/**
|
|
223
|
+
* Create PERMISSION_DENIED error.
|
|
224
|
+
*/
|
|
225
|
+
export declare function permissionDeniedError(reason: string): CognitiveErrorEnvelope;
|