cognitive-runtime 0.1.0
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/README.md +124 -0
- package/dist/cli.d.ts +10 -0
- package/dist/cli.js +200 -0
- package/dist/commands/index.d.ts +7 -0
- package/dist/commands/index.js +7 -0
- package/dist/commands/init.d.ts +5 -0
- package/dist/commands/init.js +78 -0
- package/dist/commands/list.d.ts +5 -0
- package/dist/commands/list.js +28 -0
- package/dist/commands/pipe.d.ts +9 -0
- package/dist/commands/pipe.js +57 -0
- package/dist/commands/run.d.ts +12 -0
- package/dist/commands/run.js +48 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +11 -0
- package/dist/modules/index.d.ts +5 -0
- package/dist/modules/index.js +5 -0
- package/dist/modules/loader.d.ts +8 -0
- package/dist/modules/loader.js +91 -0
- package/dist/modules/runner.d.ts +12 -0
- package/dist/modules/runner.js +93 -0
- package/dist/providers/anthropic.d.ts +14 -0
- package/dist/providers/anthropic.js +70 -0
- package/dist/providers/base.d.ts +11 -0
- package/dist/providers/base.js +19 -0
- package/dist/providers/gemini.d.ts +19 -0
- package/dist/providers/gemini.js +94 -0
- package/dist/providers/index.d.ts +13 -0
- package/dist/providers/index.js +38 -0
- package/dist/providers/openai.d.ts +14 -0
- package/dist/providers/openai.js +67 -0
- package/dist/types.d.ts +53 -0
- package/dist/types.js +4 -0
- package/package.json +33 -0
- package/src/cli.ts +223 -0
- package/src/commands/index.ts +8 -0
- package/src/commands/init.ts +94 -0
- package/src/commands/list.ts +33 -0
- package/src/commands/pipe.ts +74 -0
- package/src/commands/run.ts +65 -0
- package/src/index.ts +39 -0
- package/src/modules/index.ts +6 -0
- package/src/modules/loader.ts +106 -0
- package/src/modules/runner.ts +121 -0
- package/src/providers/anthropic.ts +89 -0
- package/src/providers/base.ts +29 -0
- package/src/providers/gemini.ts +117 -0
- package/src/providers/index.ts +43 -0
- package/src/providers/openai.ts +84 -0
- package/src/types.ts +64 -0
- package/tsconfig.json +17 -0
package/README.md
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# Cognitive Runtime
|
|
2
|
+
|
|
3
|
+
**Structured AI Task Execution**
|
|
4
|
+
|
|
5
|
+
Cognitive Runtime is the next-generation execution engine for Cognitive Modules. It provides a clean, provider-agnostic runtime that treats LLMs as interchangeable backends.
|
|
6
|
+
|
|
7
|
+
## Philosophy
|
|
8
|
+
|
|
9
|
+
Following the **Cognitive Runtime + Provider** architecture:
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
┌─────────────────────────────────────┐
|
|
13
|
+
│ Cognitive Runtime │
|
|
14
|
+
│ ┌────────────────────────────────┐ │
|
|
15
|
+
│ │ Module System │ │
|
|
16
|
+
│ │ (load, parse, validate) │ │
|
|
17
|
+
│ └────────────────────────────────┘ │
|
|
18
|
+
│ ┌────────────────────────────────┐ │
|
|
19
|
+
│ │ Execution Engine │ │
|
|
20
|
+
│ │ (prompt, schema, contract) │ │
|
|
21
|
+
│ └────────────────────────────────┘ │
|
|
22
|
+
│ ┌────────────────────────────────┐ │
|
|
23
|
+
│ │ Provider Abstraction │ │
|
|
24
|
+
│ │ (gemini, openai, anthropic) │ │
|
|
25
|
+
│ └────────────────────────────────┘ │
|
|
26
|
+
└─────────────────────────────────────┘
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Installation
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
npm install -g cognitive-runtime
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Or run directly:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
npx cognitive-runtime --help
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Usage
|
|
42
|
+
|
|
43
|
+
### Run a Module
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
cog run code-reviewer --args "def foo(): pass"
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### List Modules
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
cog list
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Pipe Mode (stdin/stdout)
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
echo "review this code" | cog pipe --module code-reviewer
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Check Configuration
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
cog doctor
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Configuration
|
|
68
|
+
|
|
69
|
+
Set one of these environment variables:
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
export GEMINI_API_KEY="your-key" # Google Gemini
|
|
73
|
+
export OPENAI_API_KEY="your-key" # OpenAI
|
|
74
|
+
export ANTHROPIC_API_KEY="your-key" # Anthropic Claude
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Module Search Paths
|
|
78
|
+
|
|
79
|
+
Modules are searched in order:
|
|
80
|
+
|
|
81
|
+
1. `./cognitive/modules/` (project-local)
|
|
82
|
+
2. `./.cognitive/modules/` (project-local, hidden)
|
|
83
|
+
3. `~/.cognitive/modules/` (user-global)
|
|
84
|
+
|
|
85
|
+
## Programmatic API
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
import { getProvider, findModule, runModule } from 'cognitive-runtime';
|
|
89
|
+
|
|
90
|
+
const provider = getProvider('gemini');
|
|
91
|
+
const module = await findModule('code-reviewer', ['./cognitive/modules']);
|
|
92
|
+
|
|
93
|
+
if (module) {
|
|
94
|
+
const result = await runModule(module, provider, {
|
|
95
|
+
args: 'def foo(): pass',
|
|
96
|
+
});
|
|
97
|
+
console.log(result.output);
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Providers
|
|
102
|
+
|
|
103
|
+
| Provider | Environment Variable | Model Default |
|
|
104
|
+
|------------|------------------------|------------------------|
|
|
105
|
+
| Gemini | `GEMINI_API_KEY` | `gemini-2.0-flash` |
|
|
106
|
+
| OpenAI | `OPENAI_API_KEY` | `gpt-4o` |
|
|
107
|
+
| Anthropic | `ANTHROPIC_API_KEY` | `claude-sonnet-4` |
|
|
108
|
+
|
|
109
|
+
## Development
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
# Install dependencies
|
|
113
|
+
npm install
|
|
114
|
+
|
|
115
|
+
# Build
|
|
116
|
+
npm run build
|
|
117
|
+
|
|
118
|
+
# Run in development
|
|
119
|
+
npm run dev -- run code-reviewer --args "..."
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## License
|
|
123
|
+
|
|
124
|
+
MIT
|
package/dist/cli.d.ts
ADDED
package/dist/cli.js
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Cognitive Runtime CLI
|
|
4
|
+
*
|
|
5
|
+
* cog run <module> --args "..." - Run a module
|
|
6
|
+
* cog list - List available modules
|
|
7
|
+
* cog pipe --module <name> - Pipe mode (stdin/stdout)
|
|
8
|
+
* cog doctor - Check configuration
|
|
9
|
+
*/
|
|
10
|
+
import { parseArgs } from 'node:util';
|
|
11
|
+
import { getProvider, listProviders } from './providers/index.js';
|
|
12
|
+
import { run, list, pipe, init } from './commands/index.js';
|
|
13
|
+
const VERSION = '0.1.0';
|
|
14
|
+
async function main() {
|
|
15
|
+
const args = process.argv.slice(2);
|
|
16
|
+
const command = args[0];
|
|
17
|
+
if (!command || command === '--help' || command === '-h') {
|
|
18
|
+
printHelp();
|
|
19
|
+
process.exit(0);
|
|
20
|
+
}
|
|
21
|
+
if (command === '--version' || command === '-v') {
|
|
22
|
+
console.log(`Cognitive Runtime v${VERSION}`);
|
|
23
|
+
process.exit(0);
|
|
24
|
+
}
|
|
25
|
+
// Parse common options
|
|
26
|
+
const { values } = parseArgs({
|
|
27
|
+
args: args.slice(1),
|
|
28
|
+
options: {
|
|
29
|
+
args: { type: 'string', short: 'a' },
|
|
30
|
+
input: { type: 'string', short: 'i' },
|
|
31
|
+
module: { type: 'string', short: 'm' },
|
|
32
|
+
provider: { type: 'string', short: 'p' },
|
|
33
|
+
pretty: { type: 'boolean', default: false },
|
|
34
|
+
verbose: { type: 'boolean', short: 'V', default: false },
|
|
35
|
+
'no-validate': { type: 'boolean', default: false },
|
|
36
|
+
},
|
|
37
|
+
allowPositionals: true,
|
|
38
|
+
});
|
|
39
|
+
// Get provider
|
|
40
|
+
let provider;
|
|
41
|
+
try {
|
|
42
|
+
provider = getProvider(values.provider);
|
|
43
|
+
}
|
|
44
|
+
catch (e) {
|
|
45
|
+
console.error(`Error: ${e instanceof Error ? e.message : e}`);
|
|
46
|
+
process.exit(1);
|
|
47
|
+
}
|
|
48
|
+
const ctx = {
|
|
49
|
+
cwd: process.cwd(),
|
|
50
|
+
provider,
|
|
51
|
+
verbose: values.verbose,
|
|
52
|
+
};
|
|
53
|
+
try {
|
|
54
|
+
switch (command) {
|
|
55
|
+
case 'run': {
|
|
56
|
+
const moduleName = args[1];
|
|
57
|
+
if (!moduleName || moduleName.startsWith('-')) {
|
|
58
|
+
console.error('Usage: cog run <module> [--args "..."]');
|
|
59
|
+
process.exit(1);
|
|
60
|
+
}
|
|
61
|
+
const result = await run(moduleName, ctx, {
|
|
62
|
+
args: values.args,
|
|
63
|
+
input: values.input,
|
|
64
|
+
noValidate: values['no-validate'],
|
|
65
|
+
pretty: values.pretty,
|
|
66
|
+
verbose: values.verbose,
|
|
67
|
+
});
|
|
68
|
+
if (!result.success) {
|
|
69
|
+
console.error(`Error: ${result.error}`);
|
|
70
|
+
process.exit(1);
|
|
71
|
+
}
|
|
72
|
+
console.log(JSON.stringify(result.data, null, values.pretty ? 2 : 0));
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
case 'list': {
|
|
76
|
+
const result = await list(ctx);
|
|
77
|
+
if (!result.success) {
|
|
78
|
+
console.error(`Error: ${result.error}`);
|
|
79
|
+
process.exit(1);
|
|
80
|
+
}
|
|
81
|
+
const data = result.data;
|
|
82
|
+
if (data.modules.length === 0) {
|
|
83
|
+
console.log('No modules found.');
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
console.log('Available Modules:');
|
|
87
|
+
console.log('');
|
|
88
|
+
for (const m of data.modules) {
|
|
89
|
+
console.log(` ${m.name} (v${m.version})`);
|
|
90
|
+
console.log(` ${m.responsibility}`);
|
|
91
|
+
console.log(` ${m.location}`);
|
|
92
|
+
console.log('');
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
case 'pipe': {
|
|
98
|
+
const moduleName = values.module || args[1];
|
|
99
|
+
if (!moduleName) {
|
|
100
|
+
console.error('Usage: cog pipe --module <name>');
|
|
101
|
+
process.exit(1);
|
|
102
|
+
}
|
|
103
|
+
await pipe(ctx, {
|
|
104
|
+
module: moduleName,
|
|
105
|
+
noValidate: values['no-validate'],
|
|
106
|
+
});
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
case 'init': {
|
|
110
|
+
const moduleName = args[1];
|
|
111
|
+
const result = await init(ctx, moduleName);
|
|
112
|
+
if (!result.success) {
|
|
113
|
+
console.error(`Error: ${result.error}`);
|
|
114
|
+
process.exit(1);
|
|
115
|
+
}
|
|
116
|
+
const data = result.data;
|
|
117
|
+
console.log(data.message);
|
|
118
|
+
console.log(` Location: ${data.location}`);
|
|
119
|
+
if (data.files) {
|
|
120
|
+
console.log(` Files: ${data.files.join(', ')}`);
|
|
121
|
+
}
|
|
122
|
+
if (data.hint) {
|
|
123
|
+
console.log(` ${data.hint}`);
|
|
124
|
+
}
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
case 'doctor': {
|
|
128
|
+
console.log('Cognitive Runtime - Environment Check\n');
|
|
129
|
+
console.log('Providers:');
|
|
130
|
+
for (const p of listProviders()) {
|
|
131
|
+
const status = p.configured ? '✓ configured' : '– not configured';
|
|
132
|
+
console.log(` ${p.name}: ${status}`);
|
|
133
|
+
}
|
|
134
|
+
console.log('');
|
|
135
|
+
try {
|
|
136
|
+
const provider = getProvider();
|
|
137
|
+
console.log(`Active provider: ${provider.name}`);
|
|
138
|
+
}
|
|
139
|
+
catch {
|
|
140
|
+
console.log('Active provider: none (set an API key)');
|
|
141
|
+
}
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
default:
|
|
145
|
+
console.error(`Unknown command: ${command}`);
|
|
146
|
+
console.error('Run "cog --help" for usage.');
|
|
147
|
+
process.exit(1);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
catch (e) {
|
|
151
|
+
console.error(`Error: ${e instanceof Error ? e.message : e}`);
|
|
152
|
+
if (values.verbose && e instanceof Error) {
|
|
153
|
+
console.error(e.stack);
|
|
154
|
+
}
|
|
155
|
+
process.exit(1);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
function printHelp() {
|
|
159
|
+
console.log(`
|
|
160
|
+
Cognitive Runtime v${VERSION}
|
|
161
|
+
Structured AI Task Execution
|
|
162
|
+
|
|
163
|
+
USAGE:
|
|
164
|
+
cog <command> [options]
|
|
165
|
+
|
|
166
|
+
COMMANDS:
|
|
167
|
+
run <module> Run a Cognitive Module
|
|
168
|
+
list List available modules
|
|
169
|
+
pipe Pipe mode (stdin/stdout)
|
|
170
|
+
init [name] Initialize project or create module
|
|
171
|
+
doctor Check configuration
|
|
172
|
+
|
|
173
|
+
OPTIONS:
|
|
174
|
+
-a, --args <str> Arguments to pass to module
|
|
175
|
+
-i, --input <json> JSON input for module
|
|
176
|
+
-m, --module <name> Module name (for pipe)
|
|
177
|
+
-p, --provider <name> LLM provider (gemini, openai, anthropic)
|
|
178
|
+
--pretty Pretty-print JSON output
|
|
179
|
+
-V, --verbose Verbose output
|
|
180
|
+
--no-validate Skip schema validation
|
|
181
|
+
-v, --version Show version
|
|
182
|
+
-h, --help Show this help
|
|
183
|
+
|
|
184
|
+
EXAMPLES:
|
|
185
|
+
cog run code-reviewer --args "def foo(): pass"
|
|
186
|
+
cog list
|
|
187
|
+
echo "review this code" | cog pipe --module code-reviewer
|
|
188
|
+
cog init my-module
|
|
189
|
+
cog doctor
|
|
190
|
+
|
|
191
|
+
ENVIRONMENT:
|
|
192
|
+
GEMINI_API_KEY Google Gemini API key
|
|
193
|
+
OPENAI_API_KEY OpenAI API key
|
|
194
|
+
ANTHROPIC_API_KEY Anthropic API key
|
|
195
|
+
`);
|
|
196
|
+
}
|
|
197
|
+
main().catch(e => {
|
|
198
|
+
console.error('Fatal error:', e);
|
|
199
|
+
process.exit(1);
|
|
200
|
+
});
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* cog init - Initialize a new Cognitive project
|
|
3
|
+
*/
|
|
4
|
+
import * as fs from 'node:fs/promises';
|
|
5
|
+
import * as path from 'node:path';
|
|
6
|
+
const EXAMPLE_MODULE_MD = `---
|
|
7
|
+
name: my-module
|
|
8
|
+
version: 1.0.0
|
|
9
|
+
responsibility: Describe what this module does
|
|
10
|
+
excludes:
|
|
11
|
+
- Do not do X
|
|
12
|
+
- Do not do Y
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
Analyze the following input and provide a structured response:
|
|
16
|
+
|
|
17
|
+
$ARGUMENTS
|
|
18
|
+
|
|
19
|
+
Return a JSON object with your analysis.
|
|
20
|
+
`;
|
|
21
|
+
const EXAMPLE_SCHEMA_JSON = `{
|
|
22
|
+
"type": "object",
|
|
23
|
+
"properties": {
|
|
24
|
+
"result": {
|
|
25
|
+
"type": "string",
|
|
26
|
+
"description": "The main result of the analysis"
|
|
27
|
+
},
|
|
28
|
+
"confidence": {
|
|
29
|
+
"type": "number",
|
|
30
|
+
"minimum": 0,
|
|
31
|
+
"maximum": 1,
|
|
32
|
+
"description": "Confidence score"
|
|
33
|
+
},
|
|
34
|
+
"rationale": {
|
|
35
|
+
"type": "string",
|
|
36
|
+
"description": "Explanation of the reasoning"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"required": ["result", "confidence", "rationale"]
|
|
40
|
+
}
|
|
41
|
+
`;
|
|
42
|
+
export async function init(ctx, moduleName) {
|
|
43
|
+
const cognitiveDir = path.join(ctx.cwd, 'cognitive', 'modules');
|
|
44
|
+
try {
|
|
45
|
+
// Create directory structure
|
|
46
|
+
await fs.mkdir(cognitiveDir, { recursive: true });
|
|
47
|
+
// If module name provided, create a new module
|
|
48
|
+
if (moduleName) {
|
|
49
|
+
const moduleDir = path.join(cognitiveDir, moduleName);
|
|
50
|
+
await fs.mkdir(moduleDir, { recursive: true });
|
|
51
|
+
await fs.writeFile(path.join(moduleDir, 'MODULE.md'), EXAMPLE_MODULE_MD.replace('my-module', moduleName));
|
|
52
|
+
await fs.writeFile(path.join(moduleDir, 'schema.json'), EXAMPLE_SCHEMA_JSON);
|
|
53
|
+
return {
|
|
54
|
+
success: true,
|
|
55
|
+
data: {
|
|
56
|
+
message: `Created module: ${moduleName}`,
|
|
57
|
+
location: moduleDir,
|
|
58
|
+
files: ['MODULE.md', 'schema.json'],
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
// Just initialize the directory
|
|
63
|
+
return {
|
|
64
|
+
success: true,
|
|
65
|
+
data: {
|
|
66
|
+
message: 'Cognitive project initialized',
|
|
67
|
+
location: cognitiveDir,
|
|
68
|
+
hint: 'Run "cog init <module-name>" to create a new module',
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
catch (e) {
|
|
73
|
+
return {
|
|
74
|
+
success: false,
|
|
75
|
+
error: e instanceof Error ? e.message : String(e),
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* cog list - List available Cognitive Modules
|
|
3
|
+
*/
|
|
4
|
+
import { listModules, getDefaultSearchPaths } from '../modules/index.js';
|
|
5
|
+
export async function list(ctx) {
|
|
6
|
+
const searchPaths = getDefaultSearchPaths(ctx.cwd);
|
|
7
|
+
const modules = await listModules(searchPaths);
|
|
8
|
+
if (modules.length === 0) {
|
|
9
|
+
return {
|
|
10
|
+
success: true,
|
|
11
|
+
data: {
|
|
12
|
+
modules: [],
|
|
13
|
+
message: `No modules found in: ${searchPaths.join(', ')}`,
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
return {
|
|
18
|
+
success: true,
|
|
19
|
+
data: {
|
|
20
|
+
modules: modules.map(m => ({
|
|
21
|
+
name: m.name,
|
|
22
|
+
version: m.version,
|
|
23
|
+
responsibility: m.responsibility,
|
|
24
|
+
location: m.location,
|
|
25
|
+
})),
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* cog pipe - Pipe mode for stdin/stdout integration
|
|
3
|
+
*/
|
|
4
|
+
import type { CommandContext, CommandResult } from '../types.js';
|
|
5
|
+
export interface PipeOptions {
|
|
6
|
+
module: string;
|
|
7
|
+
noValidate?: boolean;
|
|
8
|
+
}
|
|
9
|
+
export declare function pipe(ctx: CommandContext, options: PipeOptions): Promise<CommandResult>;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* cog pipe - Pipe mode for stdin/stdout integration
|
|
3
|
+
*/
|
|
4
|
+
import * as readline from 'node:readline';
|
|
5
|
+
import { findModule, getDefaultSearchPaths, runModule } from '../modules/index.js';
|
|
6
|
+
export async function pipe(ctx, options) {
|
|
7
|
+
const searchPaths = getDefaultSearchPaths(ctx.cwd);
|
|
8
|
+
// Find module
|
|
9
|
+
const module = await findModule(options.module, searchPaths);
|
|
10
|
+
if (!module) {
|
|
11
|
+
return {
|
|
12
|
+
success: false,
|
|
13
|
+
error: `Module not found: ${options.module}`,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
// Read from stdin
|
|
17
|
+
const rl = readline.createInterface({
|
|
18
|
+
input: process.stdin,
|
|
19
|
+
output: process.stdout,
|
|
20
|
+
terminal: false,
|
|
21
|
+
});
|
|
22
|
+
const lines = [];
|
|
23
|
+
for await (const line of rl) {
|
|
24
|
+
lines.push(line);
|
|
25
|
+
}
|
|
26
|
+
const input = lines.join('\n');
|
|
27
|
+
try {
|
|
28
|
+
// Check if input is JSON
|
|
29
|
+
let inputData;
|
|
30
|
+
try {
|
|
31
|
+
inputData = JSON.parse(input);
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
// Not JSON, use as args
|
|
35
|
+
}
|
|
36
|
+
const result = await runModule(module, ctx.provider, {
|
|
37
|
+
args: inputData ? undefined : input,
|
|
38
|
+
input: inputData,
|
|
39
|
+
validateInput: !options.noValidate,
|
|
40
|
+
validateOutput: !options.noValidate,
|
|
41
|
+
});
|
|
42
|
+
// Output JSON to stdout
|
|
43
|
+
console.log(JSON.stringify(result.output));
|
|
44
|
+
return {
|
|
45
|
+
success: true,
|
|
46
|
+
data: result,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
catch (e) {
|
|
50
|
+
const error = e instanceof Error ? e.message : String(e);
|
|
51
|
+
console.error(JSON.stringify({ error }));
|
|
52
|
+
return {
|
|
53
|
+
success: false,
|
|
54
|
+
error,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* cog run - Run a Cognitive Module
|
|
3
|
+
*/
|
|
4
|
+
import type { CommandContext, CommandResult } from '../types.js';
|
|
5
|
+
export interface RunOptions {
|
|
6
|
+
args?: string;
|
|
7
|
+
input?: string;
|
|
8
|
+
noValidate?: boolean;
|
|
9
|
+
pretty?: boolean;
|
|
10
|
+
verbose?: boolean;
|
|
11
|
+
}
|
|
12
|
+
export declare function run(moduleName: string, ctx: CommandContext, options?: RunOptions): Promise<CommandResult>;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* cog run - Run a Cognitive Module
|
|
3
|
+
*/
|
|
4
|
+
import { findModule, getDefaultSearchPaths, runModule } from '../modules/index.js';
|
|
5
|
+
export async function run(moduleName, ctx, options = {}) {
|
|
6
|
+
const searchPaths = getDefaultSearchPaths(ctx.cwd);
|
|
7
|
+
// Find module
|
|
8
|
+
const module = await findModule(moduleName, searchPaths);
|
|
9
|
+
if (!module) {
|
|
10
|
+
return {
|
|
11
|
+
success: false,
|
|
12
|
+
error: `Module not found: ${moduleName}\nSearch paths: ${searchPaths.join(', ')}`,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
try {
|
|
16
|
+
// Parse input if provided as JSON
|
|
17
|
+
let inputData;
|
|
18
|
+
if (options.input) {
|
|
19
|
+
try {
|
|
20
|
+
inputData = JSON.parse(options.input);
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return {
|
|
24
|
+
success: false,
|
|
25
|
+
error: `Invalid JSON input: ${options.input}`,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
// Run module
|
|
30
|
+
const result = await runModule(module, ctx.provider, {
|
|
31
|
+
args: options.args,
|
|
32
|
+
input: inputData,
|
|
33
|
+
validateInput: !options.noValidate,
|
|
34
|
+
validateOutput: !options.noValidate,
|
|
35
|
+
verbose: options.verbose || ctx.verbose,
|
|
36
|
+
});
|
|
37
|
+
return {
|
|
38
|
+
success: true,
|
|
39
|
+
data: options.pretty ? result : result.output,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
catch (e) {
|
|
43
|
+
return {
|
|
44
|
+
success: false,
|
|
45
|
+
error: e instanceof Error ? e.message : String(e),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cognitive Runtime - Main Entry Point
|
|
3
|
+
*
|
|
4
|
+
* Exports all public APIs for programmatic use.
|
|
5
|
+
*/
|
|
6
|
+
export type { Provider, InvokeParams, InvokeResult, Message, CognitiveModule, ModuleResult, CommandContext, CommandResult, } from './types.js';
|
|
7
|
+
export { getProvider, listProviders, GeminiProvider, OpenAIProvider, AnthropicProvider, BaseProvider, } from './providers/index.js';
|
|
8
|
+
export { loadModule, findModule, listModules, getDefaultSearchPaths, runModule, } from './modules/index.js';
|
|
9
|
+
export { run, list, pipe } from './commands/index.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cognitive Runtime - Main Entry Point
|
|
3
|
+
*
|
|
4
|
+
* Exports all public APIs for programmatic use.
|
|
5
|
+
*/
|
|
6
|
+
// Providers
|
|
7
|
+
export { getProvider, listProviders, GeminiProvider, OpenAIProvider, AnthropicProvider, BaseProvider, } from './providers/index.js';
|
|
8
|
+
// Modules
|
|
9
|
+
export { loadModule, findModule, listModules, getDefaultSearchPaths, runModule, } from './modules/index.js';
|
|
10
|
+
// Commands
|
|
11
|
+
export { run, list, pipe } from './commands/index.js';
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Module Loader - Load and parse Cognitive Modules
|
|
3
|
+
*/
|
|
4
|
+
import type { CognitiveModule } from '../types.js';
|
|
5
|
+
export declare function loadModule(modulePath: string): Promise<CognitiveModule>;
|
|
6
|
+
export declare function findModule(name: string, searchPaths: string[]): Promise<CognitiveModule | null>;
|
|
7
|
+
export declare function listModules(searchPaths: string[]): Promise<CognitiveModule[]>;
|
|
8
|
+
export declare function getDefaultSearchPaths(cwd: string): string[];
|