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,315 +0,0 @@
1
- /**
2
- * Add command - Install modules from GitHub
3
- *
4
- * cog add ziel-io/cognitive-modules -m code-simplifier
5
- * cog add https://github.com/org/repo --module name --tag v1.0.0
6
- */
7
-
8
- import { createWriteStream, existsSync, mkdirSync, rmSync, readdirSync, statSync, copyFileSync } from 'node:fs';
9
- import { writeFile, readFile, mkdir, rm } from 'node:fs/promises';
10
- import { pipeline } from 'node:stream/promises';
11
- import { Readable } from 'node:stream';
12
- import { join, basename } from 'node:path';
13
- import { homedir, tmpdir } from 'node:os';
14
- import { createGunzip } from 'node:zlib';
15
- import type { CommandContext, CommandResult } from '../types.js';
16
-
17
- // Module storage paths
18
- const USER_MODULES_DIR = join(homedir(), '.cognitive', 'modules');
19
- const INSTALLED_MANIFEST = join(homedir(), '.cognitive', 'installed.json');
20
-
21
- export interface AddOptions {
22
- module?: string;
23
- name?: string;
24
- branch?: string;
25
- tag?: string;
26
- }
27
-
28
- interface InstallManifest {
29
- [moduleName: string]: {
30
- source: string;
31
- githubUrl: string;
32
- modulePath?: string;
33
- tag?: string;
34
- branch?: string;
35
- version?: string;
36
- installedAt: string;
37
- installedTime: string;
38
- };
39
- }
40
-
41
- /**
42
- * Parse GitHub URL or shorthand
43
- */
44
- function parseGitHubUrl(url: string): { org: string; repo: string; fullUrl: string } {
45
- // Handle shorthand: org/repo
46
- if (!url.startsWith('http')) {
47
- url = `https://github.com/${url}`;
48
- }
49
-
50
- const match = url.match(/https:\/\/github\.com\/([^\/]+)\/([^\/]+)\/?/);
51
- if (!match) {
52
- throw new Error(`Invalid GitHub URL: ${url}`);
53
- }
54
-
55
- const org = match[1];
56
- const repo = match[2].replace(/\.git$/, '');
57
-
58
- return {
59
- org,
60
- repo,
61
- fullUrl: `https://github.com/${org}/${repo}`,
62
- };
63
- }
64
-
65
- /**
66
- * Download and extract ZIP from GitHub
67
- */
68
- async function downloadAndExtract(
69
- org: string,
70
- repo: string,
71
- ref: string,
72
- isTag: boolean
73
- ): Promise<string> {
74
- const zipUrl = isTag
75
- ? `https://github.com/${org}/${repo}/archive/refs/tags/${ref}.zip`
76
- : `https://github.com/${org}/${repo}/archive/refs/heads/${ref}.zip`;
77
-
78
- // Create temp directory
79
- const tempDir = join(tmpdir(), `cog-${Date.now()}`);
80
- mkdirSync(tempDir, { recursive: true });
81
-
82
- const zipPath = join(tempDir, 'repo.zip');
83
-
84
- // Download ZIP
85
- const response = await fetch(zipUrl, {
86
- headers: { 'User-Agent': 'cognitive-runtime/1.0' },
87
- });
88
-
89
- if (!response.ok) {
90
- throw new Error(`Failed to download: ${response.status} ${response.statusText}`);
91
- }
92
-
93
- // Save to file
94
- const fileStream = createWriteStream(zipPath);
95
- await pipeline(Readable.fromWeb(response.body as any), fileStream);
96
-
97
- // Extract using built-in unzip (available on most systems)
98
- const { execSync } = await import('node:child_process');
99
- execSync(`unzip -q "${zipPath}" -d "${tempDir}"`, { stdio: 'pipe' });
100
-
101
- // Find extracted directory
102
- const entries = readdirSync(tempDir).filter(
103
- e => e !== 'repo.zip' && statSync(join(tempDir, e)).isDirectory()
104
- );
105
-
106
- if (entries.length === 0) {
107
- throw new Error('ZIP file was empty');
108
- }
109
-
110
- return join(tempDir, entries[0]);
111
- }
112
-
113
- /**
114
- * Check if a directory is a valid module
115
- */
116
- function isValidModule(path: string): boolean {
117
- return (
118
- existsSync(join(path, 'module.yaml')) ||
119
- existsSync(join(path, 'MODULE.md')) ||
120
- existsSync(join(path, 'module.md'))
121
- );
122
- }
123
-
124
- /**
125
- * Find module within repository
126
- */
127
- function findModuleInRepo(repoRoot: string, modulePath: string): string {
128
- const possiblePaths = [
129
- join(repoRoot, modulePath),
130
- join(repoRoot, 'cognitive', 'modules', modulePath),
131
- join(repoRoot, 'modules', modulePath),
132
- ];
133
-
134
- for (const p of possiblePaths) {
135
- if (existsSync(p) && isValidModule(p)) {
136
- return p;
137
- }
138
- }
139
-
140
- throw new Error(
141
- `Module not found at: ${modulePath}\n` +
142
- `Searched in: ${possiblePaths.map(p => p.replace(repoRoot, '.')).join(', ')}`
143
- );
144
- }
145
-
146
- /**
147
- * Copy directory recursively
148
- */
149
- function copyDir(src: string, dest: string): void {
150
- mkdirSync(dest, { recursive: true });
151
-
152
- for (const entry of readdirSync(src)) {
153
- const srcPath = join(src, entry);
154
- const destPath = join(dest, entry);
155
-
156
- if (statSync(srcPath).isDirectory()) {
157
- copyDir(srcPath, destPath);
158
- } else {
159
- copyFileSync(srcPath, destPath);
160
- }
161
- }
162
- }
163
-
164
- /**
165
- * Get module version from module.yaml or MODULE.md
166
- */
167
- async function getModuleVersion(modulePath: string): Promise<string | undefined> {
168
- const yaml = await import('js-yaml');
169
-
170
- // Try v2 format
171
- const yamlPath = join(modulePath, 'module.yaml');
172
- if (existsSync(yamlPath)) {
173
- const content = await readFile(yamlPath, 'utf-8');
174
- const data = yaml.load(content) as { version?: string };
175
- return data?.version;
176
- }
177
-
178
- // Try v1 format
179
- const mdPath = existsSync(join(modulePath, 'MODULE.md'))
180
- ? join(modulePath, 'MODULE.md')
181
- : join(modulePath, 'module.md');
182
-
183
- if (existsSync(mdPath)) {
184
- const content = await readFile(mdPath, 'utf-8');
185
- if (content.startsWith('---')) {
186
- const parts = content.split('---');
187
- if (parts.length >= 3) {
188
- const meta = yaml.load(parts[1]) as { version?: string };
189
- return meta?.version;
190
- }
191
- }
192
- }
193
-
194
- return undefined;
195
- }
196
-
197
- /**
198
- * Record installation info
199
- */
200
- async function recordInstall(
201
- moduleName: string,
202
- info: InstallManifest[string]
203
- ): Promise<void> {
204
- let manifest: InstallManifest = {};
205
-
206
- if (existsSync(INSTALLED_MANIFEST)) {
207
- const content = await readFile(INSTALLED_MANIFEST, 'utf-8');
208
- manifest = JSON.parse(content);
209
- }
210
-
211
- manifest[moduleName] = info;
212
-
213
- await mkdir(join(homedir(), '.cognitive'), { recursive: true });
214
- await writeFile(INSTALLED_MANIFEST, JSON.stringify(manifest, null, 2));
215
- }
216
-
217
- /**
218
- * Get installation info for a module
219
- */
220
- export async function getInstallInfo(moduleName: string): Promise<InstallManifest[string] | null> {
221
- if (!existsSync(INSTALLED_MANIFEST)) {
222
- return null;
223
- }
224
-
225
- const content = await readFile(INSTALLED_MANIFEST, 'utf-8');
226
- const manifest: InstallManifest = JSON.parse(content);
227
- return manifest[moduleName] || null;
228
- }
229
-
230
- /**
231
- * Add a module from GitHub
232
- */
233
- export async function add(
234
- url: string,
235
- ctx: CommandContext,
236
- options: AddOptions = {}
237
- ): Promise<CommandResult> {
238
- const { org, repo, fullUrl } = parseGitHubUrl(url);
239
- const { module: modulePath, name, branch = 'main', tag } = options;
240
-
241
- // Determine ref (tag takes priority)
242
- const ref = tag || branch;
243
- const isTag = !!tag;
244
-
245
- let repoRoot: string;
246
- let sourcePath: string;
247
- let moduleName: string;
248
-
249
- try {
250
- // Download repository
251
- repoRoot = await downloadAndExtract(org, repo, ref, isTag);
252
-
253
- // Find module
254
- if (modulePath) {
255
- sourcePath = findModuleInRepo(repoRoot, modulePath);
256
- } else {
257
- // Use repo root as module
258
- if (!isValidModule(repoRoot)) {
259
- throw new Error(
260
- 'Repository root is not a valid module. Use --module to specify the module path.'
261
- );
262
- }
263
- sourcePath = repoRoot;
264
- }
265
-
266
- // Determine module name
267
- moduleName = name || basename(sourcePath);
268
-
269
- // Get version
270
- const version = await getModuleVersion(sourcePath);
271
-
272
- // Install to user modules dir
273
- const targetPath = join(USER_MODULES_DIR, moduleName);
274
-
275
- // Remove existing
276
- if (existsSync(targetPath)) {
277
- rmSync(targetPath, { recursive: true, force: true });
278
- }
279
-
280
- // Copy module
281
- await mkdir(USER_MODULES_DIR, { recursive: true });
282
- copyDir(sourcePath, targetPath);
283
-
284
- // Record installation info
285
- await recordInstall(moduleName, {
286
- source: sourcePath,
287
- githubUrl: fullUrl,
288
- modulePath,
289
- tag,
290
- branch,
291
- version,
292
- installedAt: targetPath,
293
- installedTime: new Date().toISOString(),
294
- });
295
-
296
- // Cleanup temp directory
297
- const tempDir = repoRoot.split('/').slice(0, -1).join('/');
298
- rmSync(tempDir, { recursive: true, force: true });
299
-
300
- return {
301
- success: true,
302
- data: {
303
- message: `Added: ${moduleName}${version ? ` v${version}` : ''}`,
304
- name: moduleName,
305
- version,
306
- location: targetPath,
307
- },
308
- };
309
- } catch (error) {
310
- return {
311
- success: false,
312
- error: error instanceof Error ? error.message : String(error),
313
- };
314
- }
315
- }
@@ -1,185 +0,0 @@
1
- /**
2
- * cog compose - Execute a Composed Cognitive Module Workflow
3
- *
4
- * Supports all composition patterns:
5
- * - Sequential: A → B → C
6
- * - Parallel: A → [B, C, D] → Aggregate
7
- * - Conditional: A → (condition) → B or C
8
- * - Iterative: A → (check) → A → ... → Done
9
- */
10
-
11
- import type { CommandContext, CommandResult } from '../types.js';
12
- import { findModule, getDefaultSearchPaths, executeComposition } from '../modules/index.js';
13
-
14
- export interface ComposeOptions {
15
- /** Direct text input */
16
- args?: string;
17
- /** JSON input data */
18
- input?: string;
19
- /** Maximum composition depth */
20
- maxDepth?: number;
21
- /** Timeout in milliseconds */
22
- timeout?: number;
23
- /** Include execution trace */
24
- trace?: boolean;
25
- /** Pretty print output */
26
- pretty?: boolean;
27
- /** Verbose mode */
28
- verbose?: boolean;
29
- }
30
-
31
- export async function compose(
32
- moduleName: string,
33
- ctx: CommandContext,
34
- options: ComposeOptions = {}
35
- ): Promise<CommandResult> {
36
- const searchPaths = getDefaultSearchPaths(ctx.cwd);
37
-
38
- // Find module
39
- const module = await findModule(moduleName, searchPaths);
40
- if (!module) {
41
- return {
42
- success: false,
43
- error: `Module not found: ${moduleName}\nSearch paths: ${searchPaths.join(', ')}`,
44
- };
45
- }
46
-
47
- try {
48
- // Parse input if provided as JSON
49
- let inputData: Record<string, unknown> = {};
50
- if (options.input) {
51
- try {
52
- inputData = JSON.parse(options.input);
53
- } catch {
54
- return {
55
- success: false,
56
- error: `Invalid JSON input: ${options.input}`,
57
- };
58
- }
59
- }
60
-
61
- // Handle --args as text input
62
- if (options.args) {
63
- inputData.query = options.args;
64
- inputData.code = options.args;
65
- }
66
-
67
- // Execute composition
68
- const result = await executeComposition(
69
- moduleName,
70
- inputData,
71
- ctx.provider,
72
- {
73
- cwd: ctx.cwd,
74
- maxDepth: options.maxDepth,
75
- timeoutMs: options.timeout
76
- }
77
- );
78
-
79
- if (options.verbose) {
80
- console.error('--- Composition Trace ---');
81
- for (const entry of result.trace) {
82
- const status = entry.success
83
- ? (entry.skipped ? '⏭️ SKIPPED' : '✅ OK')
84
- : '❌ FAILED';
85
- console.error(`${status} ${entry.module} (${entry.durationMs}ms)`);
86
- if (entry.reason) {
87
- console.error(` Reason: ${entry.reason}`);
88
- }
89
- }
90
- console.error(`--- Total: ${result.totalTimeMs}ms ---`);
91
- }
92
-
93
- // Return result
94
- if (options.trace) {
95
- // Include full result with trace
96
- return {
97
- success: result.ok,
98
- data: {
99
- ok: result.ok,
100
- result: result.result,
101
- moduleResults: result.moduleResults,
102
- trace: result.trace,
103
- totalTimeMs: result.totalTimeMs,
104
- error: result.error
105
- }
106
- };
107
- } else if (options.pretty) {
108
- return {
109
- success: result.ok,
110
- data: result.result,
111
- };
112
- } else {
113
- // For non-pretty mode, return data (success) or error (failure)
114
- if (result.ok && result.result) {
115
- return {
116
- success: true,
117
- data: (result.result as { data?: unknown }).data,
118
- };
119
- } else {
120
- return {
121
- success: false,
122
- error: result.error
123
- ? `${result.error.code}: ${result.error.message}`
124
- : 'Composition failed',
125
- data: result.moduleResults,
126
- };
127
- }
128
- }
129
- } catch (e) {
130
- return {
131
- success: false,
132
- error: e instanceof Error ? e.message : String(e),
133
- };
134
- }
135
- }
136
-
137
- /**
138
- * Show composition info for a module
139
- */
140
- export async function composeInfo(
141
- moduleName: string,
142
- ctx: CommandContext
143
- ): Promise<CommandResult> {
144
- const searchPaths = getDefaultSearchPaths(ctx.cwd);
145
-
146
- const module = await findModule(moduleName, searchPaths);
147
- if (!module) {
148
- return {
149
- success: false,
150
- error: `Module not found: ${moduleName}`,
151
- };
152
- }
153
-
154
- const composition = module.composition;
155
- if (!composition) {
156
- return {
157
- success: true,
158
- data: {
159
- name: module.name,
160
- hasComposition: false,
161
- message: 'Module does not have composition configuration'
162
- }
163
- };
164
- }
165
-
166
- return {
167
- success: true,
168
- data: {
169
- name: module.name,
170
- hasComposition: true,
171
- pattern: composition.pattern,
172
- requires: composition.requires?.map(d => ({
173
- name: d.name,
174
- version: d.version,
175
- optional: d.optional,
176
- fallback: d.fallback
177
- })),
178
- dataflowSteps: composition.dataflow?.length ?? 0,
179
- routingRules: composition.routing?.length ?? 0,
180
- maxDepth: composition.max_depth,
181
- timeoutMs: composition.timeout_ms,
182
- iteration: composition.iteration
183
- }
184
- };
185
- }
@@ -1,13 +0,0 @@
1
- /**
2
- * Commands - Re-export all commands
3
- */
4
-
5
- export * from './run.js';
6
- export * from './list.js';
7
- export * from './pipe.js';
8
- export * from './init.js';
9
- export * from './add.js';
10
- export * from './update.js';
11
- export * from './remove.js';
12
- export * from './versions.js';
13
- export * from './compose.js';
@@ -1,94 +0,0 @@
1
- /**
2
- * cog init - Initialize a new Cognitive project
3
- */
4
-
5
- import * as fs from 'node:fs/promises';
6
- import * as path from 'node:path';
7
- import type { CommandContext, CommandResult } from '../types.js';
8
-
9
- const EXAMPLE_MODULE_MD = `---
10
- name: my-module
11
- version: 1.0.0
12
- responsibility: Describe what this module does
13
- excludes:
14
- - Do not do X
15
- - Do not do Y
16
- ---
17
-
18
- Analyze the following input and provide a structured response:
19
-
20
- $ARGUMENTS
21
-
22
- Return a JSON object with your analysis.
23
- `;
24
-
25
- const EXAMPLE_SCHEMA_JSON = `{
26
- "type": "object",
27
- "properties": {
28
- "result": {
29
- "type": "string",
30
- "description": "The main result of the analysis"
31
- },
32
- "confidence": {
33
- "type": "number",
34
- "minimum": 0,
35
- "maximum": 1,
36
- "description": "Confidence score"
37
- },
38
- "rationale": {
39
- "type": "string",
40
- "description": "Explanation of the reasoning"
41
- }
42
- },
43
- "required": ["result", "confidence", "rationale"]
44
- }
45
- `;
46
-
47
- export async function init(ctx: CommandContext, moduleName?: string): Promise<CommandResult> {
48
- const cognitiveDir = path.join(ctx.cwd, 'cognitive', 'modules');
49
-
50
- try {
51
- // Create directory structure
52
- await fs.mkdir(cognitiveDir, { recursive: true });
53
-
54
- // If module name provided, create a new module
55
- if (moduleName) {
56
- const moduleDir = path.join(cognitiveDir, moduleName);
57
- await fs.mkdir(moduleDir, { recursive: true });
58
-
59
- await fs.writeFile(
60
- path.join(moduleDir, 'MODULE.md'),
61
- EXAMPLE_MODULE_MD.replace('my-module', moduleName)
62
- );
63
-
64
- await fs.writeFile(
65
- path.join(moduleDir, 'schema.json'),
66
- EXAMPLE_SCHEMA_JSON
67
- );
68
-
69
- return {
70
- success: true,
71
- data: {
72
- message: `Created module: ${moduleName}`,
73
- location: moduleDir,
74
- files: ['MODULE.md', 'schema.json'],
75
- },
76
- };
77
- }
78
-
79
- // Just initialize the directory
80
- return {
81
- success: true,
82
- data: {
83
- message: 'Cognitive project initialized',
84
- location: cognitiveDir,
85
- hint: 'Run "cog init <module-name>" to create a new module',
86
- },
87
- };
88
- } catch (e) {
89
- return {
90
- success: false,
91
- error: e instanceof Error ? e.message : String(e),
92
- };
93
- }
94
- }
@@ -1,33 +0,0 @@
1
- /**
2
- * cog list - List available Cognitive Modules
3
- */
4
-
5
- import type { CommandContext, CommandResult } from '../types.js';
6
- import { listModules, getDefaultSearchPaths } from '../modules/index.js';
7
-
8
- export async function list(ctx: CommandContext): Promise<CommandResult> {
9
- const searchPaths = getDefaultSearchPaths(ctx.cwd);
10
- const modules = await listModules(searchPaths);
11
-
12
- if (modules.length === 0) {
13
- return {
14
- success: true,
15
- data: {
16
- modules: [],
17
- message: `No modules found in: ${searchPaths.join(', ')}`,
18
- },
19
- };
20
- }
21
-
22
- return {
23
- success: true,
24
- data: {
25
- modules: modules.map(m => ({
26
- name: m.name,
27
- version: m.version,
28
- responsibility: m.responsibility,
29
- location: m.location,
30
- })),
31
- },
32
- };
33
- }
@@ -1,76 +0,0 @@
1
- /**
2
- * cog pipe - Pipe mode for stdin/stdout integration
3
- */
4
-
5
- import * as readline from 'node:readline';
6
- import type { CommandContext, CommandResult } from '../types.js';
7
- import { findModule, getDefaultSearchPaths, runModule } from '../modules/index.js';
8
-
9
- export interface PipeOptions {
10
- module: string;
11
- noValidate?: boolean;
12
- }
13
-
14
- export async function pipe(
15
- ctx: CommandContext,
16
- options: PipeOptions
17
- ): Promise<CommandResult> {
18
- const searchPaths = getDefaultSearchPaths(ctx.cwd);
19
-
20
- // Find module
21
- const module = await findModule(options.module, searchPaths);
22
- if (!module) {
23
- return {
24
- success: false,
25
- error: `Module not found: ${options.module}`,
26
- };
27
- }
28
-
29
- // Read from stdin
30
- const rl = readline.createInterface({
31
- input: process.stdin,
32
- output: process.stdout,
33
- terminal: false,
34
- });
35
-
36
- const lines: string[] = [];
37
- for await (const line of rl) {
38
- lines.push(line);
39
- }
40
-
41
- const input = lines.join('\n');
42
-
43
- try {
44
- // Check if input is JSON
45
- let inputData: Record<string, unknown> | undefined;
46
- try {
47
- inputData = JSON.parse(input);
48
- } catch {
49
- // Not JSON, use as args
50
- }
51
-
52
- const result = await runModule(module, ctx.provider, {
53
- args: inputData ? undefined : input,
54
- input: inputData,
55
- });
56
-
57
- // Output envelope format to stdout
58
- console.log(JSON.stringify(result));
59
-
60
- return {
61
- success: result.ok,
62
- data: result,
63
- };
64
- } catch (e) {
65
- const error = e instanceof Error ? e.message : String(e);
66
- // Output error in envelope format
67
- console.log(JSON.stringify({
68
- ok: false,
69
- error: { code: 'RUNTIME_ERROR', message: error }
70
- }));
71
- return {
72
- success: false,
73
- error,
74
- };
75
- }
76
- }