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.
Files changed (101) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/LICENSE +21 -0
  3. package/README.md +35 -29
  4. package/dist/cli.js +519 -23
  5. package/dist/commands/add.d.ts +33 -14
  6. package/dist/commands/add.js +383 -16
  7. package/dist/commands/compose.js +60 -23
  8. package/dist/commands/index.d.ts +4 -0
  9. package/dist/commands/index.js +4 -0
  10. package/dist/commands/init.js +23 -1
  11. package/dist/commands/migrate.d.ts +30 -0
  12. package/dist/commands/migrate.js +650 -0
  13. package/dist/commands/pipe.d.ts +1 -0
  14. package/dist/commands/pipe.js +31 -11
  15. package/dist/commands/remove.js +33 -2
  16. package/dist/commands/run.d.ts +2 -0
  17. package/dist/commands/run.js +61 -28
  18. package/dist/commands/search.d.ts +28 -0
  19. package/dist/commands/search.js +143 -0
  20. package/dist/commands/test.d.ts +65 -0
  21. package/dist/commands/test.js +454 -0
  22. package/dist/commands/update.d.ts +1 -0
  23. package/dist/commands/update.js +106 -14
  24. package/dist/commands/validate.d.ts +36 -0
  25. package/dist/commands/validate.js +97 -0
  26. package/dist/errors/index.d.ts +225 -0
  27. package/dist/errors/index.js +420 -0
  28. package/dist/mcp/server.js +84 -79
  29. package/dist/modules/composition.js +97 -32
  30. package/dist/modules/loader.js +4 -2
  31. package/dist/modules/runner.d.ts +72 -5
  32. package/dist/modules/runner.js +306 -59
  33. package/dist/modules/subagent.d.ts +6 -1
  34. package/dist/modules/subagent.js +18 -13
  35. package/dist/modules/validator.js +14 -6
  36. package/dist/providers/anthropic.d.ts +15 -0
  37. package/dist/providers/anthropic.js +147 -5
  38. package/dist/providers/base.d.ts +11 -0
  39. package/dist/providers/base.js +18 -0
  40. package/dist/providers/gemini.d.ts +15 -0
  41. package/dist/providers/gemini.js +122 -5
  42. package/dist/providers/ollama.d.ts +15 -0
  43. package/dist/providers/ollama.js +111 -3
  44. package/dist/providers/openai.d.ts +11 -0
  45. package/dist/providers/openai.js +133 -0
  46. package/dist/registry/client.d.ts +212 -0
  47. package/dist/registry/client.js +359 -0
  48. package/dist/registry/index.d.ts +4 -0
  49. package/dist/registry/index.js +4 -0
  50. package/dist/registry/tar.d.ts +8 -0
  51. package/dist/registry/tar.js +353 -0
  52. package/dist/server/http.js +301 -45
  53. package/dist/server/index.d.ts +2 -0
  54. package/dist/server/index.js +1 -0
  55. package/dist/server/sse.d.ts +13 -0
  56. package/dist/server/sse.js +22 -0
  57. package/dist/types.d.ts +32 -1
  58. package/dist/types.js +4 -1
  59. package/dist/version.d.ts +1 -0
  60. package/dist/version.js +4 -0
  61. package/package.json +31 -7
  62. package/dist/modules/composition.test.d.ts +0 -11
  63. package/dist/modules/composition.test.js +0 -450
  64. package/dist/modules/policy.test.d.ts +0 -10
  65. package/dist/modules/policy.test.js +0 -369
  66. package/src/cli.ts +0 -471
  67. package/src/commands/add.ts +0 -315
  68. package/src/commands/compose.ts +0 -185
  69. package/src/commands/index.ts +0 -13
  70. package/src/commands/init.ts +0 -94
  71. package/src/commands/list.ts +0 -33
  72. package/src/commands/pipe.ts +0 -76
  73. package/src/commands/remove.ts +0 -57
  74. package/src/commands/run.ts +0 -80
  75. package/src/commands/update.ts +0 -130
  76. package/src/commands/versions.ts +0 -79
  77. package/src/index.ts +0 -90
  78. package/src/mcp/index.ts +0 -5
  79. package/src/mcp/server.ts +0 -403
  80. package/src/modules/composition.test.ts +0 -558
  81. package/src/modules/composition.ts +0 -1674
  82. package/src/modules/index.ts +0 -9
  83. package/src/modules/loader.ts +0 -508
  84. package/src/modules/policy.test.ts +0 -455
  85. package/src/modules/runner.ts +0 -1983
  86. package/src/modules/subagent.ts +0 -277
  87. package/src/modules/validator.ts +0 -700
  88. package/src/providers/anthropic.ts +0 -89
  89. package/src/providers/base.ts +0 -29
  90. package/src/providers/deepseek.ts +0 -83
  91. package/src/providers/gemini.ts +0 -117
  92. package/src/providers/index.ts +0 -78
  93. package/src/providers/minimax.ts +0 -81
  94. package/src/providers/moonshot.ts +0 -82
  95. package/src/providers/ollama.ts +0 -83
  96. package/src/providers/openai.ts +0 -84
  97. package/src/providers/qwen.ts +0 -82
  98. package/src/server/http.ts +0 -316
  99. package/src/server/index.ts +0 -6
  100. package/src/types.ts +0 -599
  101. package/tsconfig.json +0 -17
@@ -1,277 +0,0 @@
1
- /**
2
- * Subagent - Orchestrate module calls with isolated execution contexts.
3
- *
4
- * Supports:
5
- * - @call:module-name - Call another module
6
- * - @call:module-name(args) - Call with arguments
7
- * - context: fork - Isolated execution (no shared state)
8
- * - context: main - Shared execution (default)
9
- */
10
-
11
- import type {
12
- CognitiveModule,
13
- ModuleResult,
14
- ModuleInput,
15
- Provider,
16
- EnvelopeResponseV22
17
- } from '../types.js';
18
- import { loadModule, findModule, getDefaultSearchPaths } from './loader.js';
19
- import { runModule } from './runner.js';
20
-
21
- // =============================================================================
22
- // Types
23
- // =============================================================================
24
-
25
- export interface SubagentContext {
26
- parentId: string | null;
27
- depth: number;
28
- maxDepth: number;
29
- results: Record<string, unknown>;
30
- isolated: boolean;
31
- }
32
-
33
- export interface CallDirective {
34
- module: string;
35
- args: string;
36
- match: string;
37
- }
38
-
39
- export interface SubagentRunOptions {
40
- input?: ModuleInput;
41
- validateInput?: boolean;
42
- validateOutput?: boolean;
43
- maxDepth?: number;
44
- }
45
-
46
- // =============================================================================
47
- // Context Management
48
- // =============================================================================
49
-
50
- /**
51
- * Create a new root context
52
- */
53
- export function createContext(maxDepth: number = 5): SubagentContext {
54
- return {
55
- parentId: null,
56
- depth: 0,
57
- maxDepth,
58
- results: {},
59
- isolated: false
60
- };
61
- }
62
-
63
- /**
64
- * Fork context (isolated - no inherited results)
65
- */
66
- export function forkContext(ctx: SubagentContext, moduleName: string): SubagentContext {
67
- return {
68
- parentId: moduleName,
69
- depth: ctx.depth + 1,
70
- maxDepth: ctx.maxDepth,
71
- results: {},
72
- isolated: true
73
- };
74
- }
75
-
76
- /**
77
- * Extend context (shared - inherits results)
78
- */
79
- export function extendContext(ctx: SubagentContext, moduleName: string): SubagentContext {
80
- return {
81
- parentId: moduleName,
82
- depth: ctx.depth + 1,
83
- maxDepth: ctx.maxDepth,
84
- results: { ...ctx.results },
85
- isolated: false
86
- };
87
- }
88
-
89
- // =============================================================================
90
- // Call Parsing
91
- // =============================================================================
92
-
93
- // Pattern to match @call:module-name or @call:module-name(args)
94
- const CALL_PATTERN = /@call:([a-zA-Z0-9_-]+)(?:\(([^)]*)\))?/g;
95
-
96
- /**
97
- * Parse @call directives from text
98
- */
99
- export function parseCalls(text: string): CallDirective[] {
100
- const calls: CallDirective[] = [];
101
- let match: RegExpExecArray | null;
102
-
103
- // Reset regex state
104
- CALL_PATTERN.lastIndex = 0;
105
-
106
- while ((match = CALL_PATTERN.exec(text)) !== null) {
107
- calls.push({
108
- module: match[1],
109
- args: match[2] || '',
110
- match: match[0]
111
- });
112
- }
113
-
114
- return calls;
115
- }
116
-
117
- /**
118
- * Replace @call directives with their results
119
- */
120
- export function substituteCallResults(
121
- text: string,
122
- callResults: Record<string, unknown>
123
- ): string {
124
- let result = text;
125
-
126
- for (const [callStr, callResult] of Object.entries(callResults)) {
127
- const resultStr = typeof callResult === 'object'
128
- ? JSON.stringify(callResult, null, 2)
129
- : String(callResult);
130
-
131
- result = result.replace(callStr, `[Result from ${callStr}]:\n${resultStr}`);
132
- }
133
-
134
- return result;
135
- }
136
-
137
- // =============================================================================
138
- // Orchestrator
139
- // =============================================================================
140
-
141
- export class SubagentOrchestrator {
142
- private provider: Provider;
143
- private running: Set<string> = new Set();
144
- private cwd: string;
145
-
146
- constructor(provider: Provider, cwd: string = process.cwd()) {
147
- this.provider = provider;
148
- this.cwd = cwd;
149
- }
150
-
151
- /**
152
- * Run a module with subagent support.
153
- * Recursively resolves @call directives before final execution.
154
- */
155
- async run(
156
- moduleName: string,
157
- options: SubagentRunOptions = {},
158
- context?: SubagentContext
159
- ): Promise<ModuleResult> {
160
- const {
161
- input = {},
162
- validateInput = true,
163
- validateOutput = true,
164
- maxDepth = 5
165
- } = options;
166
-
167
- // Initialize context
168
- const ctx = context ?? createContext(maxDepth);
169
-
170
- // Check depth limit
171
- if (ctx.depth > ctx.maxDepth) {
172
- throw new Error(
173
- `Max subagent depth (${ctx.maxDepth}) exceeded. Check for circular calls.`
174
- );
175
- }
176
-
177
- // Prevent circular calls
178
- if (this.running.has(moduleName)) {
179
- throw new Error(`Circular call detected: ${moduleName}`);
180
- }
181
-
182
- this.running.add(moduleName);
183
-
184
- try {
185
- // Find and load module
186
- const searchPaths = getDefaultSearchPaths(this.cwd);
187
- const module = await findModule(moduleName, searchPaths);
188
-
189
- if (!module) {
190
- throw new Error(`Module not found: ${moduleName}`);
191
- }
192
-
193
- // Check if this module wants isolated execution
194
- const moduleContextMode = module.context ?? 'main';
195
-
196
- // Parse @call directives from prompt
197
- const calls = parseCalls(module.prompt);
198
- const callResults: Record<string, unknown> = {};
199
-
200
- // Resolve each @call directive
201
- for (const call of calls) {
202
- const childModule = call.module;
203
- const childArgs = call.args;
204
-
205
- // Prepare child input
206
- const childInput: ModuleInput = childArgs
207
- ? { query: childArgs, code: childArgs }
208
- : { ...input };
209
-
210
- // Determine child context
211
- const childContext = moduleContextMode === 'fork'
212
- ? forkContext(ctx, moduleName)
213
- : extendContext(ctx, moduleName);
214
-
215
- // Recursively run child module
216
- const childResult = await this.run(
217
- childModule,
218
- {
219
- input: childInput,
220
- validateInput: false, // Skip validation for @call args
221
- validateOutput
222
- },
223
- childContext
224
- );
225
-
226
- // Store result
227
- if (childResult.ok && 'data' in childResult) {
228
- callResults[call.match] = childResult.data;
229
- } else if ('error' in childResult) {
230
- callResults[call.match] = { error: childResult.error };
231
- }
232
- }
233
-
234
- // Substitute call results into prompt
235
- let modifiedModule = module;
236
- if (Object.keys(callResults).length > 0) {
237
- const modifiedPrompt = substituteCallResults(module.prompt, callResults);
238
- modifiedModule = {
239
- ...module,
240
- prompt: modifiedPrompt + '\n\n## Subagent Results Available\nThe @call results have been injected above. Use them in your response.\n'
241
- };
242
- }
243
-
244
- // Run the module
245
- const result = await runModule(modifiedModule, this.provider, {
246
- input,
247
- validateInput,
248
- validateOutput,
249
- verbose: false,
250
- useV22: true
251
- });
252
-
253
- // Store result in context
254
- if (result.ok && 'data' in result) {
255
- ctx.results[moduleName] = result.data;
256
- }
257
-
258
- return result;
259
-
260
- } finally {
261
- this.running.delete(moduleName);
262
- }
263
- }
264
- }
265
-
266
- /**
267
- * Convenience function to run a module with subagent support
268
- */
269
- export async function runWithSubagents(
270
- moduleName: string,
271
- provider: Provider,
272
- options: SubagentRunOptions & { cwd?: string } = {}
273
- ): Promise<ModuleResult> {
274
- const { cwd = process.cwd(), ...runOptions } = options;
275
- const orchestrator = new SubagentOrchestrator(provider, cwd);
276
- return orchestrator.run(moduleName, runOptions);
277
- }