cognitive-modules-cli 2.2.1 → 2.5.0-beta.1
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/dist/cli.js +12 -65
- package/dist/commands/index.d.ts +0 -1
- package/dist/commands/index.js +0 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -5
- package/dist/modules/index.d.ts +0 -2
- package/dist/modules/index.js +0 -2
- package/dist/modules/loader.d.ts +2 -22
- package/dist/modules/loader.js +4 -167
- package/dist/modules/runner.d.ts +34 -348
- package/dist/modules/runner.js +708 -1263
- package/dist/modules/subagent.js +0 -2
- package/dist/providers/base.d.ts +45 -1
- package/dist/providers/base.js +67 -0
- package/dist/providers/openai.d.ts +27 -3
- package/dist/providers/openai.js +175 -3
- package/dist/types.d.ts +316 -93
- package/dist/types.js +120 -1
- package/package.json +1 -2
- package/src/cli.ts +12 -73
- package/src/commands/index.ts +0 -1
- package/src/index.ts +0 -35
- package/src/modules/index.ts +0 -2
- package/src/modules/loader.ts +6 -196
- package/src/modules/runner.ts +996 -1690
- package/src/modules/subagent.ts +0 -2
- package/src/providers/base.ts +86 -1
- package/src/providers/openai.ts +226 -4
- package/src/types.ts +462 -113
- package/tsconfig.json +1 -1
- package/dist/commands/compose.d.ts +0 -31
- package/dist/commands/compose.js +0 -148
- package/dist/modules/composition.d.ts +0 -251
- package/dist/modules/composition.js +0 -1265
- 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/dist/modules/validator.d.ts +0 -28
- package/dist/modules/validator.js +0 -629
- package/src/commands/compose.ts +0 -185
- package/src/modules/composition.test.ts +0 -558
- package/src/modules/composition.ts +0 -1674
- package/src/modules/policy.test.ts +0 -455
- package/src/modules/validator.ts +0 -700
package/dist/modules/runner.d.ts
CHANGED
|
@@ -1,370 +1,56 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Module Runner - Execute Cognitive Modules
|
|
3
|
-
* v2.
|
|
4
|
-
* v2.2.1: Version field, enhanced error taxonomy, observability hooks, streaming
|
|
3
|
+
* v2.5: Streaming response and multimodal support
|
|
5
4
|
*/
|
|
6
|
-
import type { Provider, CognitiveModule, ModuleResult, ModuleInput,
|
|
7
|
-
/**
|
|
8
|
-
* Validate data against JSON schema. Returns list of errors.
|
|
9
|
-
*/
|
|
10
|
-
export declare function validateData(data: unknown, schema: object, label?: string): string[];
|
|
11
|
-
/** Action types that can be checked against policies */
|
|
12
|
-
export type PolicyAction = 'network' | 'filesystem_write' | 'side_effects' | 'code_execution';
|
|
13
|
-
/** Result of a policy check */
|
|
14
|
-
export interface PolicyCheckResult {
|
|
15
|
-
allowed: boolean;
|
|
16
|
-
reason?: string;
|
|
17
|
-
policy?: string;
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Check if a tool is allowed by the module's tools policy.
|
|
21
|
-
*
|
|
22
|
-
* @param toolName The name of the tool to check
|
|
23
|
-
* @param module The cognitive module config
|
|
24
|
-
* @returns PolicyCheckResult indicating if the tool is allowed
|
|
25
|
-
*
|
|
26
|
-
* @example
|
|
27
|
-
* const result = checkToolPolicy('write_file', module);
|
|
28
|
-
* if (!result.allowed) {
|
|
29
|
-
* throw new Error(result.reason);
|
|
30
|
-
* }
|
|
31
|
-
*/
|
|
32
|
-
export declare function checkToolPolicy(toolName: string, module: CognitiveModule): PolicyCheckResult;
|
|
33
|
-
/**
|
|
34
|
-
* Check if an action is allowed by the module's policies.
|
|
35
|
-
*
|
|
36
|
-
* @param action The action to check (network, filesystem_write, etc.)
|
|
37
|
-
* @param module The cognitive module config
|
|
38
|
-
* @returns PolicyCheckResult indicating if the action is allowed
|
|
39
|
-
*
|
|
40
|
-
* @example
|
|
41
|
-
* const result = checkPolicy('network', module);
|
|
42
|
-
* if (!result.allowed) {
|
|
43
|
-
* throw new Error(result.reason);
|
|
44
|
-
* }
|
|
45
|
-
*/
|
|
46
|
-
export declare function checkPolicy(action: PolicyAction, module: CognitiveModule): PolicyCheckResult;
|
|
47
|
-
/**
|
|
48
|
-
* Check if a tool is allowed considering both tools policy and general policies.
|
|
49
|
-
* This performs a comprehensive check that:
|
|
50
|
-
* 1. Checks the tools policy (allowed/denied lists)
|
|
51
|
-
* 2. Maps the tool to policy actions and checks those
|
|
52
|
-
*
|
|
53
|
-
* @param toolName The name of the tool to check
|
|
54
|
-
* @param module The cognitive module config
|
|
55
|
-
* @returns PolicyCheckResult with detailed information
|
|
56
|
-
*
|
|
57
|
-
* @example
|
|
58
|
-
* const result = checkToolAllowed('write_file', module);
|
|
59
|
-
* if (!result.allowed) {
|
|
60
|
-
* return makeErrorResponse({
|
|
61
|
-
* code: 'POLICY_VIOLATION',
|
|
62
|
-
* message: result.reason,
|
|
63
|
-
* });
|
|
64
|
-
* }
|
|
65
|
-
*/
|
|
66
|
-
export declare function checkToolAllowed(toolName: string, module: CognitiveModule): PolicyCheckResult;
|
|
67
|
-
/**
|
|
68
|
-
* Validate that a list of tools are all allowed by the module's policies.
|
|
69
|
-
* Returns all violations found.
|
|
70
|
-
*
|
|
71
|
-
* @param toolNames List of tool names to check
|
|
72
|
-
* @param module The cognitive module config
|
|
73
|
-
* @returns Array of PolicyCheckResult for denied tools
|
|
74
|
-
*/
|
|
75
|
-
export declare function validateToolsAllowed(toolNames: string[], module: CognitiveModule): PolicyCheckResult[];
|
|
76
|
-
/**
|
|
77
|
-
* Get all denied actions for a module based on its policies.
|
|
78
|
-
* Useful for informing LLM about restrictions.
|
|
79
|
-
*/
|
|
80
|
-
export declare function getDeniedActions(module: CognitiveModule): PolicyAction[];
|
|
81
|
-
/**
|
|
82
|
-
* Get all denied tools for a module based on its tools policy.
|
|
83
|
-
*/
|
|
84
|
-
export declare function getDeniedTools(module: CognitiveModule): string[];
|
|
85
|
-
/**
|
|
86
|
-
* Get all allowed tools for a module (only meaningful in deny_by_default mode).
|
|
87
|
-
*/
|
|
88
|
-
export declare function getAllowedTools(module: CognitiveModule): string[] | null;
|
|
89
|
-
/** Tool call request from LLM */
|
|
90
|
-
export interface ToolCallRequest {
|
|
91
|
-
name: string;
|
|
92
|
-
arguments: Record<string, unknown>;
|
|
93
|
-
}
|
|
94
|
-
/** Tool call result */
|
|
95
|
-
export interface ToolCallResult {
|
|
96
|
-
success: boolean;
|
|
97
|
-
result?: unknown;
|
|
98
|
-
error?: {
|
|
99
|
-
code: string;
|
|
100
|
-
message: string;
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
/** Tool executor function type */
|
|
104
|
-
export type ToolExecutor = (args: Record<string, unknown>) => Promise<unknown>;
|
|
105
|
-
/**
|
|
106
|
-
* ToolCallInterceptor - Intercepts and validates tool calls against module policies.
|
|
107
|
-
*
|
|
108
|
-
* Use this class to wrap tool execution with policy enforcement:
|
|
109
|
-
*
|
|
110
|
-
* @example
|
|
111
|
-
* const interceptor = new ToolCallInterceptor(module);
|
|
112
|
-
*
|
|
113
|
-
* // Register tool executors
|
|
114
|
-
* interceptor.registerTool('read_file', async (args) => {
|
|
115
|
-
* return fs.readFile(args.path as string, 'utf-8');
|
|
116
|
-
* });
|
|
117
|
-
*
|
|
118
|
-
* // Execute tool with policy check
|
|
119
|
-
* const result = await interceptor.execute({
|
|
120
|
-
* name: 'write_file',
|
|
121
|
-
* arguments: { path: '/tmp/test.txt', content: 'hello' }
|
|
122
|
-
* });
|
|
123
|
-
*
|
|
124
|
-
* if (!result.success) {
|
|
125
|
-
* console.error('Tool blocked:', result.error);
|
|
126
|
-
* }
|
|
127
|
-
*/
|
|
128
|
-
export declare class ToolCallInterceptor {
|
|
129
|
-
private module;
|
|
130
|
-
private tools;
|
|
131
|
-
private callLog;
|
|
132
|
-
constructor(module: CognitiveModule);
|
|
133
|
-
/**
|
|
134
|
-
* Register a tool executor.
|
|
135
|
-
*/
|
|
136
|
-
registerTool(name: string, executor: ToolExecutor): void;
|
|
137
|
-
/**
|
|
138
|
-
* Register multiple tools at once.
|
|
139
|
-
*/
|
|
140
|
-
registerTools(tools: Record<string, ToolExecutor>): void;
|
|
141
|
-
/**
|
|
142
|
-
* Check if a tool call is allowed without executing it.
|
|
143
|
-
*/
|
|
144
|
-
checkAllowed(toolName: string): PolicyCheckResult;
|
|
145
|
-
/**
|
|
146
|
-
* Execute a tool call with policy enforcement.
|
|
147
|
-
*
|
|
148
|
-
* @param request The tool call request
|
|
149
|
-
* @returns ToolCallResult with success/error
|
|
150
|
-
*/
|
|
151
|
-
execute(request: ToolCallRequest): Promise<ToolCallResult>;
|
|
152
|
-
/**
|
|
153
|
-
* Execute multiple tool calls in sequence.
|
|
154
|
-
* Stops on first policy violation.
|
|
155
|
-
*/
|
|
156
|
-
executeMany(requests: ToolCallRequest[]): Promise<ToolCallResult[]>;
|
|
157
|
-
/**
|
|
158
|
-
* Get the call log for auditing.
|
|
159
|
-
*/
|
|
160
|
-
getCallLog(): Array<{
|
|
161
|
-
tool: string;
|
|
162
|
-
allowed: boolean;
|
|
163
|
-
timestamp: number;
|
|
164
|
-
}>;
|
|
165
|
-
/**
|
|
166
|
-
* Get summary of denied calls.
|
|
167
|
-
*/
|
|
168
|
-
getDeniedCalls(): Array<{
|
|
169
|
-
tool: string;
|
|
170
|
-
timestamp: number;
|
|
171
|
-
}>;
|
|
172
|
-
/**
|
|
173
|
-
* Clear the call log.
|
|
174
|
-
*/
|
|
175
|
-
clearLog(): void;
|
|
176
|
-
/**
|
|
177
|
-
* Get policy summary for this module.
|
|
178
|
-
*/
|
|
179
|
-
getPolicySummary(): {
|
|
180
|
-
deniedActions: PolicyAction[];
|
|
181
|
-
deniedTools: string[];
|
|
182
|
-
allowedTools: string[] | null;
|
|
183
|
-
toolsPolicy: 'allow_by_default' | 'deny_by_default' | undefined;
|
|
184
|
-
};
|
|
185
|
-
}
|
|
186
|
-
/**
|
|
187
|
-
* Create a policy-aware tool executor wrapper.
|
|
188
|
-
*
|
|
189
|
-
* @example
|
|
190
|
-
* const safeExecutor = createPolicyAwareExecutor(module, 'write_file', async (args) => {
|
|
191
|
-
* return fs.writeFile(args.path, args.content);
|
|
192
|
-
* });
|
|
193
|
-
*
|
|
194
|
-
* // This will throw if write_file is denied
|
|
195
|
-
* await safeExecutor({ path: '/tmp/test.txt', content: 'hello' });
|
|
196
|
-
*/
|
|
197
|
-
export declare function createPolicyAwareExecutor(module: CognitiveModule, toolName: string, executor: ToolExecutor): ToolExecutor;
|
|
198
|
-
/**
|
|
199
|
-
* Validate overflow.insights against module's max_items config.
|
|
200
|
-
*
|
|
201
|
-
* @param data The response data object
|
|
202
|
-
* @param module The cognitive module config
|
|
203
|
-
* @returns Array of errors if insights exceed limit
|
|
204
|
-
*/
|
|
205
|
-
export declare function validateOverflowLimits(data: Record<string, unknown>, module: CognitiveModule): string[];
|
|
206
|
-
/**
|
|
207
|
-
* Validate enum values against module's enum strategy.
|
|
208
|
-
* For strict mode, custom enum objects are not allowed.
|
|
209
|
-
*
|
|
210
|
-
* @param data The response data object
|
|
211
|
-
* @param module The cognitive module config
|
|
212
|
-
* @returns Array of errors if enum violations found
|
|
213
|
-
*/
|
|
214
|
-
export declare function validateEnumStrategy(data: Record<string, unknown>, module: CognitiveModule): string[];
|
|
215
|
-
/** Hook called before module execution */
|
|
216
|
-
export type BeforeCallHook = (moduleName: string, inputData: ModuleInput, moduleConfig: CognitiveModule) => void;
|
|
217
|
-
/** Hook called after successful module execution */
|
|
218
|
-
export type AfterCallHook = (moduleName: string, result: EnvelopeResponseV22<unknown>, latencyMs: number) => void;
|
|
219
|
-
/** Hook called when an error occurs */
|
|
220
|
-
export type ErrorHook = (moduleName: string, error: Error, partialResult: unknown | null) => void;
|
|
221
|
-
/**
|
|
222
|
-
* Decorator to register a before-call hook.
|
|
223
|
-
*
|
|
224
|
-
* @example
|
|
225
|
-
* onBeforeCall((moduleName, inputData, config) => {
|
|
226
|
-
* console.log(`Calling ${moduleName} with`, inputData);
|
|
227
|
-
* });
|
|
228
|
-
*/
|
|
229
|
-
export declare function onBeforeCall(hook: BeforeCallHook): BeforeCallHook;
|
|
230
|
-
/**
|
|
231
|
-
* Decorator to register an after-call hook.
|
|
232
|
-
*
|
|
233
|
-
* @example
|
|
234
|
-
* onAfterCall((moduleName, result, latencyMs) => {
|
|
235
|
-
* console.log(`${moduleName} completed in ${latencyMs}ms`);
|
|
236
|
-
* });
|
|
237
|
-
*/
|
|
238
|
-
export declare function onAfterCall(hook: AfterCallHook): AfterCallHook;
|
|
239
|
-
/**
|
|
240
|
-
* Decorator to register an error hook.
|
|
241
|
-
*
|
|
242
|
-
* @example
|
|
243
|
-
* onError((moduleName, error, partialResult) => {
|
|
244
|
-
* console.error(`Error in ${moduleName}:`, error);
|
|
245
|
-
* });
|
|
246
|
-
*/
|
|
247
|
-
export declare function onError(hook: ErrorHook): ErrorHook;
|
|
248
|
-
/**
|
|
249
|
-
* Register a hook programmatically.
|
|
250
|
-
*/
|
|
251
|
-
export declare function registerHook(hookType: 'before_call' | 'after_call' | 'error', hook: BeforeCallHook | AfterCallHook | ErrorHook): void;
|
|
252
|
-
/**
|
|
253
|
-
* Unregister a hook. Returns true if found and removed.
|
|
254
|
-
*/
|
|
255
|
-
export declare function unregisterHook(hookType: 'before_call' | 'after_call' | 'error', hook: BeforeCallHook | AfterCallHook | ErrorHook): boolean;
|
|
256
|
-
/**
|
|
257
|
-
* Clear all registered hooks.
|
|
258
|
-
*/
|
|
259
|
-
export declare function clearHooks(): void;
|
|
260
|
-
/** Error codes and their default properties */
|
|
261
|
-
export declare const ERROR_PROPERTIES: Record<string, {
|
|
262
|
-
recoverable: boolean;
|
|
263
|
-
retry_after_ms: number | null;
|
|
264
|
-
}>;
|
|
265
|
-
export interface MakeErrorResponseOptions {
|
|
266
|
-
code: string;
|
|
267
|
-
message: string;
|
|
268
|
-
explain?: string;
|
|
269
|
-
partialData?: unknown;
|
|
270
|
-
details?: Record<string, unknown>;
|
|
271
|
-
recoverable?: boolean;
|
|
272
|
-
retryAfterMs?: number;
|
|
273
|
-
confidence?: number;
|
|
274
|
-
risk?: RiskLevel;
|
|
275
|
-
}
|
|
276
|
-
/**
|
|
277
|
-
* Build a standardized error response with enhanced taxonomy.
|
|
278
|
-
*/
|
|
279
|
-
export declare function makeErrorResponse(options: MakeErrorResponseOptions): EnvelopeResponseV22<unknown>;
|
|
280
|
-
export interface MakeSuccessResponseOptions {
|
|
281
|
-
data: unknown;
|
|
282
|
-
confidence: number;
|
|
283
|
-
risk: RiskLevel;
|
|
284
|
-
explain: string;
|
|
285
|
-
latencyMs?: number;
|
|
286
|
-
model?: string;
|
|
287
|
-
traceId?: string;
|
|
288
|
-
}
|
|
289
|
-
/**
|
|
290
|
-
* Build a standardized success response.
|
|
291
|
-
*/
|
|
292
|
-
export declare function makeSuccessResponse(options: MakeSuccessResponseOptions): EnvelopeResponseV22<unknown>;
|
|
5
|
+
import type { Provider, CognitiveModule, ModuleResult, ModuleInput, StreamingChunk, MediaInput, ModalityType, RuntimeCapabilities } from '../types.js';
|
|
293
6
|
export interface RunOptions {
|
|
294
7
|
input?: ModuleInput;
|
|
295
8
|
args?: string;
|
|
296
9
|
verbose?: boolean;
|
|
297
|
-
validateInput?: boolean;
|
|
298
|
-
validateOutput?: boolean;
|
|
299
10
|
useEnvelope?: boolean;
|
|
300
11
|
useV22?: boolean;
|
|
301
12
|
enableRepair?: boolean;
|
|
302
|
-
traceId?: string;
|
|
303
|
-
model?: string;
|
|
304
13
|
}
|
|
305
14
|
export declare function runModule(module: CognitiveModule, provider: Provider, options?: RunOptions): Promise<ModuleResult>;
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
result?: EnvelopeResponseV22<unknown>;
|
|
316
|
-
error?: {
|
|
317
|
-
code: string;
|
|
318
|
-
message: string;
|
|
319
|
-
};
|
|
320
|
-
}
|
|
321
|
-
export interface StreamOptions {
|
|
322
|
-
input?: ModuleInput;
|
|
323
|
-
args?: string;
|
|
324
|
-
validateInput?: boolean;
|
|
325
|
-
validateOutput?: boolean;
|
|
326
|
-
useV22?: boolean;
|
|
327
|
-
enableRepair?: boolean;
|
|
328
|
-
traceId?: string;
|
|
329
|
-
model?: string;
|
|
15
|
+
export interface StreamRunOptions extends RunOptions {
|
|
16
|
+
/** Callback for each chunk */
|
|
17
|
+
onChunk?: (chunk: StreamingChunk) => void;
|
|
18
|
+
/** Callback for progress updates */
|
|
19
|
+
onProgress?: (percent: number, message?: string) => void;
|
|
20
|
+
/** Heartbeat interval in milliseconds (default: 15000) */
|
|
21
|
+
heartbeatInterval?: number;
|
|
22
|
+
/** Maximum stream duration in milliseconds (default: 300000) */
|
|
23
|
+
maxDuration?: number;
|
|
330
24
|
}
|
|
331
25
|
/**
|
|
332
|
-
* Run
|
|
26
|
+
* Run module with streaming response
|
|
333
27
|
*
|
|
334
|
-
*
|
|
335
|
-
* -
|
|
336
|
-
*
|
|
337
|
-
*
|
|
338
|
-
* - type="complete": Final complete result
|
|
339
|
-
* - type="error": Error occurred
|
|
340
|
-
*
|
|
341
|
-
* @example
|
|
342
|
-
* for await (const event of runModuleStream(module, provider, options)) {
|
|
343
|
-
* if (event.type === 'chunk') {
|
|
344
|
-
* process.stdout.write(event.chunk);
|
|
345
|
-
* } else if (event.type === 'complete') {
|
|
346
|
-
* console.log('Result:', event.result);
|
|
347
|
-
* }
|
|
348
|
-
* }
|
|
28
|
+
* @param module - The cognitive module to execute
|
|
29
|
+
* @param provider - The LLM provider
|
|
30
|
+
* @param options - Run options including streaming callbacks
|
|
31
|
+
* @yields Streaming chunks
|
|
349
32
|
*/
|
|
350
|
-
export declare function runModuleStream(module: CognitiveModule, provider: Provider, options?:
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
33
|
+
export declare function runModuleStream(module: CognitiveModule, provider: Provider, options?: StreamRunOptions): AsyncGenerator<StreamingChunk, ModuleResult | undefined, unknown>;
|
|
34
|
+
/**
|
|
35
|
+
* Load media file as base64
|
|
36
|
+
*/
|
|
37
|
+
export declare function loadMediaAsBase64(path: string): Promise<{
|
|
38
|
+
data: string;
|
|
39
|
+
media_type: string;
|
|
40
|
+
} | null>;
|
|
356
41
|
/**
|
|
357
|
-
*
|
|
358
|
-
* For backward compatibility. Throws on error instead of returning error envelope.
|
|
42
|
+
* Validate media input against module constraints
|
|
359
43
|
*/
|
|
360
|
-
export declare function
|
|
44
|
+
export declare function validateMediaInput(media: MediaInput, module: CognitiveModule, maxSizeMb?: number): {
|
|
45
|
+
valid: boolean;
|
|
46
|
+
error?: string;
|
|
47
|
+
code?: string;
|
|
48
|
+
};
|
|
361
49
|
/**
|
|
362
|
-
*
|
|
50
|
+
* Get runtime capabilities
|
|
363
51
|
*/
|
|
364
|
-
export declare function
|
|
365
|
-
export declare const extractMetaV22: typeof extractMeta;
|
|
52
|
+
export declare function getRuntimeCapabilities(): RuntimeCapabilities;
|
|
366
53
|
/**
|
|
367
|
-
*
|
|
54
|
+
* Check if runtime supports a specific modality
|
|
368
55
|
*/
|
|
369
|
-
export declare function
|
|
370
|
-
export declare const shouldEscalateV22: typeof shouldEscalate;
|
|
56
|
+
export declare function runtimeSupportsModality(modality: ModalityType, direction?: 'input' | 'output'): boolean;
|