cognitive-modules 0.1.0__py3-none-any.whl → 0.2.0__py3-none-any.whl

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.
cognitive/loader.py CHANGED
@@ -1,17 +1,22 @@
1
1
  """
2
- Module Loader - Load cognitive modules in both old and new formats.
2
+ Module Loader - Load cognitive modules in all formats.
3
3
 
4
- Old format (6 files):
4
+ Format v2 (recommended):
5
+ - module.yaml (machine-readable manifest)
6
+ - prompt.md (human-readable prompt)
7
+ - schema.json (input + output + error)
8
+ - tests/ (golden tests)
9
+
10
+ Format v1 (legacy, still supported):
11
+ - MODULE.md (YAML frontmatter + prompt)
12
+ - schema.json (input + output)
13
+
14
+ Format v0 (old, deprecated):
5
15
  - module.md (YAML frontmatter)
6
16
  - input.schema.json
7
17
  - output.schema.json
8
18
  - constraints.yaml
9
19
  - prompt.txt
10
- - examples/
11
-
12
- New format (2 files):
13
- - MODULE.md (YAML frontmatter + prompt)
14
- - schema.json (input + output combined)
15
20
  """
16
21
 
17
22
  import json
@@ -22,13 +27,15 @@ import yaml
22
27
 
23
28
 
24
29
  def detect_format(module_path: Path) -> str:
25
- """Detect module format: 'new' or 'old'."""
26
- if (module_path / "MODULE.md").exists():
27
- return "new"
30
+ """Detect module format: 'v2', 'v1', or 'v0'."""
31
+ if (module_path / "module.yaml").exists():
32
+ return "v2"
33
+ elif (module_path / "MODULE.md").exists():
34
+ return "v1"
28
35
  elif (module_path / "module.md").exists():
29
- return "old"
36
+ return "v0"
30
37
  else:
31
- raise FileNotFoundError(f"No MODULE.md or module.md found in {module_path}")
38
+ raise FileNotFoundError(f"No module.yaml, MODULE.md, or module.md found in {module_path}")
32
39
 
33
40
 
34
41
  def parse_frontmatter(content: str) -> tuple[dict, str]:
@@ -45,8 +52,79 @@ def parse_frontmatter(content: str) -> tuple[dict, str]:
45
52
  return frontmatter, body
46
53
 
47
54
 
48
- def load_new_format(module_path: Path) -> dict:
49
- """Load module in new format (MODULE.md + schema.json)."""
55
+ def load_v2_format(module_path: Path) -> dict:
56
+ """Load module in v2 format (module.yaml + prompt.md + schema.json)."""
57
+ # Load module.yaml
58
+ with open(module_path / "module.yaml", 'r', encoding='utf-8') as f:
59
+ manifest = yaml.safe_load(f)
60
+
61
+ # Load prompt.md
62
+ prompt_path = module_path / "prompt.md"
63
+ if prompt_path.exists():
64
+ with open(prompt_path, 'r', encoding='utf-8') as f:
65
+ prompt = f.read()
66
+ else:
67
+ prompt = ""
68
+
69
+ # Load schema.json
70
+ schema_path = module_path / "schema.json"
71
+ if schema_path.exists():
72
+ with open(schema_path, 'r', encoding='utf-8') as f:
73
+ schema = json.load(f)
74
+ input_schema = schema.get("input", {})
75
+ output_schema = schema.get("output", {})
76
+ error_schema = schema.get("error", {})
77
+ else:
78
+ input_schema = {}
79
+ output_schema = {}
80
+ error_schema = {}
81
+
82
+ # Extract constraints
83
+ constraints_raw = manifest.get("constraints", {})
84
+ constraints = {
85
+ "operational": {
86
+ "no_external_network": constraints_raw.get("no_network", True),
87
+ "no_side_effects": constraints_raw.get("no_side_effects", True),
88
+ "no_file_write": constraints_raw.get("no_file_write", True),
89
+ "no_inventing_data": constraints_raw.get("no_inventing_data", True),
90
+ },
91
+ "output_quality": {
92
+ "require_confidence": manifest.get("output", {}).get("require_confidence", True),
93
+ "require_rationale": manifest.get("output", {}).get("require_rationale", True),
94
+ "require_behavior_equivalence": manifest.get("output", {}).get("require_behavior_equivalence", False),
95
+ }
96
+ }
97
+
98
+ # Extract tools policy
99
+ tools = manifest.get("tools", {})
100
+
101
+ # Extract output contract
102
+ output_contract = manifest.get("output", {})
103
+
104
+ # Extract failure contract
105
+ failure_contract = manifest.get("failure", {})
106
+
107
+ return {
108
+ "name": manifest.get("name", module_path.name),
109
+ "version": manifest.get("version", "1.0.0"),
110
+ "responsibility": manifest.get("responsibility", ""),
111
+ "excludes": manifest.get("excludes", []),
112
+ "path": module_path,
113
+ "format": "v2",
114
+ "metadata": manifest,
115
+ "input_schema": input_schema,
116
+ "output_schema": output_schema,
117
+ "error_schema": error_schema,
118
+ "constraints": constraints,
119
+ "tools": tools,
120
+ "output_contract": output_contract,
121
+ "failure_contract": failure_contract,
122
+ "prompt": prompt,
123
+ }
124
+
125
+
126
+ def load_v1_format(module_path: Path) -> dict:
127
+ """Load module in v1 format (MODULE.md + schema.json)."""
50
128
  # Load MODULE.md
51
129
  with open(module_path / "MODULE.md", 'r', encoding='utf-8') as f:
52
130
  content = f.read()
@@ -79,8 +157,11 @@ def load_new_format(module_path: Path) -> dict:
79
157
 
80
158
  return {
81
159
  "name": metadata.get("name", module_path.name),
160
+ "version": metadata.get("version", "1.0.0"),
161
+ "responsibility": metadata.get("responsibility", ""),
162
+ "excludes": metadata.get("excludes", []),
82
163
  "path": module_path,
83
- "format": "new",
164
+ "format": "v1",
84
165
  "metadata": metadata,
85
166
  "input_schema": input_schema,
86
167
  "output_schema": output_schema,
@@ -89,8 +170,8 @@ def load_new_format(module_path: Path) -> dict:
89
170
  }
90
171
 
91
172
 
92
- def load_old_format(module_path: Path) -> dict:
93
- """Load module in old format (6 files)."""
173
+ def load_v0_format(module_path: Path) -> dict:
174
+ """Load module in v0 format (old 6-file format)."""
94
175
  # Load module.md
95
176
  with open(module_path / "module.md", 'r', encoding='utf-8') as f:
96
177
  content = f.read()
@@ -114,8 +195,11 @@ def load_old_format(module_path: Path) -> dict:
114
195
 
115
196
  return {
116
197
  "name": metadata.get("name", module_path.name),
198
+ "version": metadata.get("version", "1.0.0"),
199
+ "responsibility": metadata.get("responsibility", ""),
200
+ "excludes": [],
117
201
  "path": module_path,
118
- "format": "old",
202
+ "format": "v0",
119
203
  "metadata": metadata,
120
204
  "input_schema": input_schema,
121
205
  "output_schema": output_schema,
@@ -127,7 +211,37 @@ def load_old_format(module_path: Path) -> dict:
127
211
  def load_module(module_path: Path) -> dict:
128
212
  """Load a module, auto-detecting format."""
129
213
  fmt = detect_format(module_path)
130
- if fmt == "new":
131
- return load_new_format(module_path)
214
+ if fmt == "v2":
215
+ return load_v2_format(module_path)
216
+ elif fmt == "v1":
217
+ return load_v1_format(module_path)
132
218
  else:
133
- return load_old_format(module_path)
219
+ return load_v0_format(module_path)
220
+
221
+
222
+ def find_module(name: str, search_paths: list[Path]) -> Optional[dict]:
223
+ """Find and load a module by name from search paths."""
224
+ for base_path in search_paths:
225
+ module_path = base_path / name
226
+ if module_path.exists():
227
+ try:
228
+ return load_module(module_path)
229
+ except FileNotFoundError:
230
+ continue
231
+ return None
232
+
233
+
234
+ def list_modules(search_paths: list[Path]) -> list[dict]:
235
+ """List all modules in search paths."""
236
+ modules = []
237
+ for base_path in search_paths:
238
+ if not base_path.exists():
239
+ continue
240
+ for module_dir in base_path.iterdir():
241
+ if module_dir.is_dir():
242
+ try:
243
+ module = load_module(module_dir)
244
+ modules.append(module)
245
+ except FileNotFoundError:
246
+ continue
247
+ return modules
cognitive/registry.py CHANGED
@@ -31,7 +31,7 @@ SEARCH_PATHS = [
31
31
  USER_MODULES_DIR = Path.home() / ".cognitive" / "modules"
32
32
 
33
33
  # Default registry URL
34
- DEFAULT_REGISTRY_URL = "https://raw.githubusercontent.com/leizii/cognitive-modules/main/cognitive-registry.json"
34
+ DEFAULT_REGISTRY_URL = "https://raw.githubusercontent.com/ziel-io/cognitive-modules/main/cognitive-registry.json"
35
35
 
36
36
  # Local registry cache
37
37
  REGISTRY_CACHE = Path.home() / ".cognitive" / "registry-cache.json"
cognitive/templates.py CHANGED
@@ -83,7 +83,7 @@ EXAMPLE_OUTPUT = {
83
83
  def get_schema_template(name: str) -> dict:
84
84
  """Generate schema template as dict."""
85
85
  return {
86
- "$schema": "https://cognitive-modules.io/schema/v1",
86
+ "$schema": "https://ziel-io.github.io/cognitive-modules/schema/v1.json",
87
87
  "$id": name,
88
88
  "title": f"{name.replace('-', ' ').title()} Schema",
89
89
  "input": {
@@ -0,0 +1,418 @@
1
+ Metadata-Version: 2.4
2
+ Name: cognitive-modules
3
+ Version: 0.2.0
4
+ Summary: Structured LLM task runner with schema validation, confidence scoring, and subagent orchestration
5
+ Author: ziel-io
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/ziel-io/cognitive-modules
8
+ Project-URL: Documentation, https://github.com/ziel-io/cognitive-modules#readme
9
+ Project-URL: Repository, https://github.com/ziel-io/cognitive-modules
10
+ Project-URL: Issues, https://github.com/ziel-io/cognitive-modules/issues
11
+ Keywords: llm,cognitive,modules,ai,cli,schema,validation,prompt,agent,subagent
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: License :: OSI Approved :: MIT License
15
+ Classifier: Operating System :: OS Independent
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3.9
18
+ Classifier: Programming Language :: Python :: 3.10
19
+ Classifier: Programming Language :: Python :: 3.11
20
+ Classifier: Programming Language :: Python :: 3.12
21
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
22
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
23
+ Requires-Python: >=3.9
24
+ Description-Content-Type: text/markdown
25
+ License-File: LICENSE
26
+ Requires-Dist: jsonschema>=4.17.0
27
+ Requires-Dist: pyyaml>=6.0
28
+ Requires-Dist: typer>=0.9.0
29
+ Requires-Dist: rich>=13.0.0
30
+ Provides-Extra: openai
31
+ Requires-Dist: openai>=1.0.0; extra == "openai"
32
+ Provides-Extra: anthropic
33
+ Requires-Dist: anthropic>=0.18.0; extra == "anthropic"
34
+ Provides-Extra: ollama
35
+ Requires-Dist: requests>=2.28.0; extra == "ollama"
36
+ Provides-Extra: minimax
37
+ Requires-Dist: openai>=1.0.0; extra == "minimax"
38
+ Provides-Extra: all
39
+ Requires-Dist: openai>=1.0.0; extra == "all"
40
+ Requires-Dist: anthropic>=0.18.0; extra == "all"
41
+ Requires-Dist: requests>=2.28.0; extra == "all"
42
+ Provides-Extra: dev
43
+ Requires-Dist: pytest>=7.0.0; extra == "dev"
44
+ Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
45
+ Requires-Dist: build>=1.0.0; extra == "dev"
46
+ Requires-Dist: twine>=4.0.0; extra == "dev"
47
+ Requires-Dist: ruff>=0.1.0; extra == "dev"
48
+ Provides-Extra: docs
49
+ Requires-Dist: mkdocs-material>=9.0.0; extra == "docs"
50
+ Requires-Dist: pymdown-extensions>=10.0.0; extra == "docs"
51
+ Dynamic: license-file
52
+
53
+ # Cognitive Modules
54
+
55
+ [![CI](https://github.com/ziel-io/cognitive-modules/actions/workflows/ci.yml/badge.svg)](https://github.com/ziel-io/cognitive-modules/actions/workflows/ci.yml)
56
+ [![PyPI version](https://badge.fury.io/py/cognitive-modules.svg)](https://pypi.org/project/cognitive-modules/)
57
+ [![Python 3.9+](https://img.shields.io/badge/python-3.9+-blue.svg)](https://www.python.org/downloads/)
58
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
59
+
60
+ > 可验证的结构化 AI 任务规范
61
+
62
+ Cognitive Modules 是一种 AI 任务定义规范,专为需要**强约束、可验证、可审计**的生成任务设计。
63
+
64
+ ## 特性
65
+
66
+ - **强类型契约** - JSON Schema 双向验证输入输出
67
+ - **可解释输出** - 强制输出 `confidence` + `rationale`
68
+ - **子代理编排** - `@call:module` 支持模块间调用
69
+ - **参数传递** - `$ARGUMENTS` 运行时替换
70
+ - **多 LLM 支持** - OpenAI / Anthropic / MiniMax / Ollama
71
+ - **公共注册表** - `cog install registry:module-name`
72
+
73
+ ## 安装
74
+
75
+ ```bash
76
+ # 基础安装
77
+ pip install cognitive-modules
78
+
79
+ # 带 LLM 支持
80
+ pip install cognitive-modules[openai] # OpenAI
81
+ pip install cognitive-modules[anthropic] # Claude
82
+ pip install cognitive-modules[all] # 全部
83
+ ```
84
+
85
+ ## 快速开始
86
+
87
+ ```bash
88
+ # 配置 LLM
89
+ export LLM_PROVIDER=openai
90
+ export OPENAI_API_KEY=sk-xxx
91
+
92
+ # 或使用 MiniMax
93
+ export LLM_PROVIDER=minimax
94
+ export MINIMAX_API_KEY=sk-xxx
95
+
96
+ # 运行代码审查
97
+ cog run code-reviewer --args "def login(u,p): return db.query(f'SELECT * FROM users WHERE name={u}')" --pretty
98
+
99
+ # 运行任务排序
100
+ cog run task-prioritizer --args "修复bug(紧急), 写文档, 优化性能" --pretty
101
+
102
+ # 运行 API 设计
103
+ cog run api-designer --args "用户系统 CRUD API" --pretty
104
+ ```
105
+
106
+ ## 与 Skills 对比
107
+
108
+ | | Skills | Cognitive Modules |
109
+ |---|--------|------------------|
110
+ | 定位 | 轻量指令扩展 | 可验证的结构化任务 |
111
+ | 输入校验 | ❌ | ✅ JSON Schema |
112
+ | 输出校验 | ❌ | ✅ JSON Schema |
113
+ | 置信度 | ❌ | ✅ 必须 0-1 |
114
+ | 推理过程 | ❌ | ✅ 必须 rationale |
115
+ | 参数传递 | ✅ $ARGUMENTS | ✅ $ARGUMENTS |
116
+ | 子代理 | ✅ context: fork | ✅ @call + context |
117
+ | 验证工具 | ❌ | ✅ cog validate |
118
+ | 注册表 | ❌ | ✅ cog install |
119
+
120
+ ## CLI 命令
121
+
122
+ ```bash
123
+ # 模块管理
124
+ cog list # 列出已安装模块
125
+ cog info <module> # 查看模块详情
126
+ cog validate <module> # 验证模块结构
127
+
128
+ # 运行模块
129
+ cog run <module> input.json -o output.json --pretty
130
+ cog run <module> --args "需求描述" --pretty
131
+ cog run <module> --args "需求" --subagent # 启用子代理
132
+
133
+ # 创建模块
134
+ cog init <name> -d "描述"
135
+
136
+ # 安装/卸载
137
+ cog install github:user/repo/path
138
+ cog install registry:module-name
139
+ cog uninstall <module>
140
+
141
+ # 注册表
142
+ cog registry # 查看公共模块
143
+ cog search <query> # 搜索模块
144
+
145
+ # 环境检查
146
+ cog doctor
147
+ ```
148
+
149
+ ## 内置模块
150
+
151
+ | 模块 | 功能 | 示例 |
152
+ |------|------|------|
153
+ | `code-reviewer` | 代码审查 | `cog run code-reviewer --args "你的代码"` |
154
+ | `code-simplifier` | 代码简化 | `cog run code-simplifier --args "复杂代码"` |
155
+ | `task-prioritizer` | 任务优先级排序 | `cog run task-prioritizer --args "任务1,任务2"` |
156
+ | `api-designer` | REST API 设计 | `cog run api-designer --args "订单系统"` |
157
+ | `ui-spec-generator` | UI 规范生成 | `cog run ui-spec-generator --args "电商首页"` |
158
+ | `product-analyzer` | 产品分析(子代理示例) | `cog run product-analyzer --args "健康产品" -s` |
159
+
160
+ ## 模块格式
161
+
162
+ ### 新格式(推荐)
163
+
164
+ ```
165
+ my-module/
166
+ ├── MODULE.md # 元数据 + 指令
167
+ ├── schema.json # 输入输出 Schema
168
+ └── examples/
169
+ ├── input.json
170
+ └── output.json
171
+ ```
172
+
173
+ ### MODULE.md
174
+
175
+ ```yaml
176
+ ---
177
+ name: my-module
178
+ version: 1.0.0
179
+ responsibility: 一句话描述
180
+
181
+ excludes:
182
+ - 不做的事情
183
+
184
+ constraints:
185
+ no_network: true
186
+ no_inventing_data: true
187
+ require_confidence: true
188
+ require_rationale: true
189
+
190
+ context: fork # 可选:隔离执行
191
+ ---
192
+
193
+ # 指令
194
+
195
+ 根据用户需求 $ARGUMENTS 执行任务。
196
+
197
+ 可以调用其他模块:
198
+ @call:other-module($ARGUMENTS)
199
+ ```
200
+
201
+ ## 在 AI 工具中使用
202
+
203
+ ### Cursor / Codex CLI
204
+
205
+ 在项目根目录创建 `AGENTS.md`:
206
+
207
+ ```markdown
208
+ ## 代码审查
209
+
210
+ 当需要审查代码时:
211
+ 1. 读取 `~/.cognitive/modules/code-reviewer/MODULE.md`
212
+ 2. 按 schema.json 格式输出
213
+ 3. 包含 issues、summary、rationale、confidence
214
+ ```
215
+
216
+ ### 直接对话
217
+
218
+ ```
219
+ 读取 ~/.cognitive/modules/code-reviewer/MODULE.md,
220
+ 审查这段代码:def login(u,p): ...
221
+ ```
222
+
223
+ ## 配置 LLM
224
+
225
+ ```bash
226
+ # OpenAI
227
+ export LLM_PROVIDER=openai
228
+ export OPENAI_API_KEY=sk-xxx
229
+
230
+ # Anthropic Claude
231
+ export LLM_PROVIDER=anthropic
232
+ export ANTHROPIC_API_KEY=sk-ant-xxx
233
+
234
+ # MiniMax
235
+ export LLM_PROVIDER=minimax
236
+ export MINIMAX_API_KEY=sk-xxx
237
+
238
+ # Ollama(本地)
239
+ export LLM_PROVIDER=ollama
240
+
241
+ # 检查配置
242
+ cog doctor
243
+ ```
244
+
245
+ ## 创建新模块(完整流程)
246
+
247
+ 以 `code-simplifier` 为例:
248
+
249
+ ### Step 1: 创建目录结构
250
+
251
+ ```bash
252
+ mkdir -p cognitive/modules/code-simplifier
253
+ ```
254
+
255
+ ### Step 2: 编写 MODULE.md
256
+
257
+ ```bash
258
+ cat > cognitive/modules/code-simplifier/MODULE.md << 'EOF'
259
+ ---
260
+ name: code-simplifier
261
+ version: 1.0.0
262
+ responsibility: Simplify complex code while preserving functionality
263
+
264
+ excludes:
265
+ - Changing the code's behavior
266
+ - Adding new features
267
+ - Removing functionality
268
+
269
+ constraints:
270
+ no_network: true
271
+ no_side_effects: true
272
+ require_confidence: true
273
+ require_rationale: true
274
+ ---
275
+
276
+ # Code Simplifier Module
277
+
278
+ You are an expert at refactoring and simplifying code.
279
+
280
+ ## Input
281
+
282
+ Code to simplify: $ARGUMENTS
283
+
284
+ ## Simplification Strategies
285
+
286
+ 1. **Remove redundancy** - Eliminate duplicate code
287
+ 2. **Improve naming** - Use clear, descriptive names
288
+ 3. **Reduce nesting** - Flatten deep conditionals
289
+ 4. **Simplify logic** - Use built-in functions
290
+
291
+ ## Output Requirements
292
+
293
+ Return JSON containing:
294
+ - `simplified_code`: The simplified version
295
+ - `changes`: List of changes made
296
+ - `summary`: Brief description
297
+ - `rationale`: Explanation of decisions
298
+ - `confidence`: Confidence score [0-1]
299
+ EOF
300
+ ```
301
+
302
+ ### Step 3: 编写 schema.json
303
+
304
+ ```bash
305
+ cat > cognitive/modules/code-simplifier/schema.json << 'EOF'
306
+ {
307
+ "$schema": "https://ziel-io.github.io/cognitive-modules/schema/v1.json",
308
+ "input": {
309
+ "type": "object",
310
+ "properties": {
311
+ "code": { "type": "string" },
312
+ "language": { "type": "string" },
313
+ "$ARGUMENTS": { "type": "string" }
314
+ }
315
+ },
316
+ "output": {
317
+ "type": "object",
318
+ "required": ["simplified_code", "changes", "summary", "rationale", "confidence"],
319
+ "properties": {
320
+ "simplified_code": { "type": "string" },
321
+ "changes": {
322
+ "type": "array",
323
+ "items": {
324
+ "type": "object",
325
+ "properties": {
326
+ "type": { "type": "string" },
327
+ "description": { "type": "string" }
328
+ }
329
+ }
330
+ },
331
+ "summary": { "type": "string" },
332
+ "rationale": { "type": "string" },
333
+ "confidence": { "type": "number", "minimum": 0, "maximum": 1 }
334
+ }
335
+ }
336
+ }
337
+ EOF
338
+ ```
339
+
340
+ ### Step 4: 验证模块
341
+
342
+ ```bash
343
+ cog validate code-simplifier
344
+ cog list # 确认模块出现在列表中
345
+ ```
346
+
347
+ ### Step 5: 测试运行
348
+
349
+ ```bash
350
+ cog run code-simplifier --args "def calc(x): if x > 0: if x < 10: return x * 2 else: return x else: return 0" --pretty
351
+ ```
352
+
353
+ ### Step 6: 添加示例(可选)
354
+
355
+ ```bash
356
+ mkdir -p cognitive/modules/code-simplifier/examples
357
+ # 添加 input.json 和 output.json 作为测试用例
358
+ ```
359
+
360
+ ### 模块设计要点
361
+
362
+ | 要素 | 必须 | 说明 |
363
+ |------|------|------|
364
+ | `name` | ✅ | 唯一标识符,kebab-case |
365
+ | `version` | ✅ | 语义化版本 |
366
+ | `responsibility` | ✅ | 一句话描述职责 |
367
+ | `excludes` | ✅ | 明确列出不做的事 |
368
+ | `$ARGUMENTS` | ✅ | 支持命令行参数 |
369
+ | `confidence` | ✅ | 输出必须包含 0-1 置信度 |
370
+ | `rationale` | ✅ | 输出必须包含推理过程 |
371
+ | `schema.json` | ✅ | 定义输入输出契约 |
372
+
373
+ ## 开发
374
+
375
+ ```bash
376
+ # 克隆
377
+ git clone https://github.com/ziel-io/cognitive-modules.git
378
+ cd cognitive-modules
379
+
380
+ # 安装开发依赖
381
+ pip install -e ".[dev]"
382
+
383
+ # 运行测试
384
+ pytest tests/ -v
385
+
386
+ # 创建新模块(使用模板)
387
+ cog init my-module -d "模块描述"
388
+ cog validate my-module
389
+ ```
390
+
391
+ ## 项目结构
392
+
393
+ ```
394
+ cognitive-modules/
395
+ ├── src/cognitive/ # CLI 源码
396
+ │ ├── cli.py # 命令入口
397
+ │ ├── loader.py # 模块加载
398
+ │ ├── runner.py # 模块执行
399
+ │ ├── subagent.py # 子代理编排
400
+ │ ├── validator.py # 模块验证
401
+ │ ├── registry.py # 模块安装
402
+ │ ├── templates.py # 模块模板
403
+ │ └── providers/ # LLM 后端
404
+ ├── cognitive/modules/ # 内置模块
405
+ ├── tests/ # 单元测试
406
+ ├── SPEC.md # 规范文档
407
+ ├── INTEGRATION.md # 集成指南
408
+ └── cognitive-registry.json # 公共注册表
409
+ ```
410
+
411
+ ## 文档
412
+
413
+ - [SPEC.md](SPEC.md) - 完整规范(含上下文哲学)
414
+ - [INTEGRATION.md](INTEGRATION.md) - Agent 工具集成指南
415
+
416
+ ## License
417
+
418
+ MIT
@@ -0,0 +1,15 @@
1
+ cognitive/__init__.py,sha256=uSX5NuOWyW0qtq1bnxbzabZ0OQakAtsjF0MWbjQBvwE,401
2
+ cognitive/cli.py,sha256=q0vHCHFmig9gQ85KRyIrX6vrJZFm5nXF_bW5qQRGEPU,14608
3
+ cognitive/loader.py,sha256=i9ulJtd5Itj4QYhUYfuHOXtzCBBtY8L1EkMu5yM-0SU,8094
4
+ cognitive/registry.py,sha256=aBkkpg5PtL_tKDIOXpv6vblB7x_Ax0eVaWgejuQTfCE,8851
5
+ cognitive/runner.py,sha256=9uxBAGd3gtXnwUm5x2CjhXi_S1gd2nfjRfM3Q_1f30Q,4362
6
+ cognitive/subagent.py,sha256=fb7LWwNF6YcJtC_T1dK0EvzqWMBnav-kiCIpvVohEBw,8142
7
+ cognitive/templates.py,sha256=lKC197X9aQIA-npUvVCaplSwvhxjsH_KYVCQtrTZrL4,4712
8
+ cognitive/validator.py,sha256=1v1HUHYOlAc2sYCkIq_gnUMMnca0fdtQwr7UMBFpp04,12200
9
+ cognitive/providers/__init__.py,sha256=hqhVA1IEXpVtyCAteXhO5yD8a8ikQpVIPEKJVHLtRFY,7492
10
+ cognitive_modules-0.2.0.dist-info/licenses/LICENSE,sha256=NXFYUy2hPJdh3NHRxMChTnMiQD9k8zFxkmR7gWefexc,1064
11
+ cognitive_modules-0.2.0.dist-info/METADATA,sha256=d6Jm2ALcRX56UBBvKDWn8uiLx8MIcnUhK4RLl8x2uvg,11381
12
+ cognitive_modules-0.2.0.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
13
+ cognitive_modules-0.2.0.dist-info/entry_points.txt,sha256=PKHlfrFmve5K2349ryipySKbOOsKxo_vIq1NNT-iBV0,42
14
+ cognitive_modules-0.2.0.dist-info/top_level.txt,sha256=kGIfDucCKylo8cRBtxER_v3DHIea-Sol9x9YSJo1u3Y,10
15
+ cognitive_modules-0.2.0.dist-info/RECORD,,
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2026 leizii
3
+ Copyright (c) 2026 ziel-io
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -1,295 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: cognitive-modules
3
- Version: 0.1.0
4
- Summary: Structured LLM task runner with schema validation, confidence scoring, and subagent orchestration
5
- Author: leizii
6
- License: MIT
7
- Project-URL: Homepage, https://github.com/leizii/cognitive-modules
8
- Project-URL: Documentation, https://github.com/leizii/cognitive-modules#readme
9
- Project-URL: Repository, https://github.com/leizii/cognitive-modules
10
- Project-URL: Issues, https://github.com/leizii/cognitive-modules/issues
11
- Keywords: llm,cognitive,modules,ai,cli,schema,validation,prompt,agent,subagent
12
- Classifier: Development Status :: 4 - Beta
13
- Classifier: Intended Audience :: Developers
14
- Classifier: License :: OSI Approved :: MIT License
15
- Classifier: Operating System :: OS Independent
16
- Classifier: Programming Language :: Python :: 3
17
- Classifier: Programming Language :: Python :: 3.9
18
- Classifier: Programming Language :: Python :: 3.10
19
- Classifier: Programming Language :: Python :: 3.11
20
- Classifier: Programming Language :: Python :: 3.12
21
- Classifier: Topic :: Software Development :: Libraries :: Python Modules
22
- Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
23
- Requires-Python: >=3.9
24
- Description-Content-Type: text/markdown
25
- License-File: LICENSE
26
- Requires-Dist: jsonschema>=4.17.0
27
- Requires-Dist: pyyaml>=6.0
28
- Requires-Dist: typer>=0.9.0
29
- Requires-Dist: rich>=13.0.0
30
- Provides-Extra: openai
31
- Requires-Dist: openai>=1.0.0; extra == "openai"
32
- Provides-Extra: anthropic
33
- Requires-Dist: anthropic>=0.18.0; extra == "anthropic"
34
- Provides-Extra: ollama
35
- Requires-Dist: requests>=2.28.0; extra == "ollama"
36
- Provides-Extra: minimax
37
- Requires-Dist: openai>=1.0.0; extra == "minimax"
38
- Provides-Extra: all
39
- Requires-Dist: openai>=1.0.0; extra == "all"
40
- Requires-Dist: anthropic>=0.18.0; extra == "all"
41
- Requires-Dist: requests>=2.28.0; extra == "all"
42
- Provides-Extra: dev
43
- Requires-Dist: pytest>=7.0.0; extra == "dev"
44
- Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
45
- Requires-Dist: build>=1.0.0; extra == "dev"
46
- Requires-Dist: twine>=4.0.0; extra == "dev"
47
- Dynamic: license-file
48
-
49
- # Cognitive Modules
50
-
51
- > 可验证的结构化 AI 任务规范
52
-
53
- Cognitive Modules 是一种 AI 任务定义规范,专为需要**强约束、可验证、可审计**的生成任务设计。
54
-
55
- ## 与 Skills 的区别
56
-
57
- | | Skills | Cognitive Modules |
58
- |---|--------|------------------|
59
- | 定位 | 轻量指令扩展 | 可验证的结构化任务 |
60
- | 输入校验 | 无 | JSON Schema |
61
- | 输出校验 | 无 | JSON Schema |
62
- | 置信度 | 无 | 必须 0-1 |
63
- | 推理过程 | 无 | 必须 rationale |
64
- | 适用场景 | 快捷命令 | 规范生成、设计文档 |
65
-
66
- ## 快速开始
67
-
68
- ```bash
69
- # 克隆
70
- git clone https://github.com/leizii/cognitive-modules.git
71
- cd cognitive-modules
72
-
73
- # 安装依赖
74
- pip install typer rich jsonschema pyyaml
75
-
76
- # 运行
77
- ./cog --help
78
- ```
79
-
80
- ## CLI 命令
81
-
82
- ```bash
83
- # 模块管理
84
- cog list # 列出已安装模块
85
- cog info <module> # 查看模块详情
86
- cog validate <module> # 验证模块结构
87
-
88
- # 创建模块
89
- cog init <name> -d "描述" # 从模板创建新模块
90
-
91
- # 运行模块
92
- cog run <module> input.json -o output.json --pretty
93
-
94
- # 直接传参数(无需 JSON 文件)
95
- cog run <module> --args "你的需求描述" -o output.json
96
-
97
- # 启用子代理模式(支持 @call 调用其他模块)
98
- cog run <module> --args "需求" --subagent
99
-
100
- # 安装/卸载
101
- cog install <source> # 从 git/本地/注册表安装
102
- cog uninstall <module> # 卸载模块
103
-
104
- # 注册表
105
- cog registry # 查看公共模块
106
- cog search <query> # 搜索模块
107
-
108
- # 环境检查
109
- cog doctor # 检查 LLM 配置
110
- ```
111
-
112
- ## 安装来源
113
-
114
- ```bash
115
- # 从本地安装
116
- cog install ./path/to/module
117
-
118
- # 从 GitHub 安装
119
- cog install github:leizii/cognitive-modules/cognitive/modules/ui-spec-generator
120
-
121
- # 从注册表安装
122
- cog install registry:ui-spec-generator
123
- ```
124
-
125
- ## 模块格式
126
-
127
- ### 新格式(推荐,2 文件)
128
-
129
- ```
130
- my-module/
131
- ├── MODULE.md # 元数据 + 指令
132
- ├── schema.json # 输入输出 Schema
133
- └── examples/ # 可选
134
- ├── input.json
135
- └── output.json
136
- ```
137
-
138
- ### 旧格式(兼容,6 文件)
139
-
140
- ```
141
- my-module/
142
- ├── module.md
143
- ├── input.schema.json
144
- ├── output.schema.json
145
- ├── constraints.yaml
146
- ├── prompt.txt
147
- └── examples/
148
- ```
149
-
150
- ## MODULE.md 格式
151
-
152
- ```yaml
153
- ---
154
- name: my-module
155
- version: 1.0.0
156
- responsibility: 一句话描述
157
-
158
- excludes:
159
- - 不做的事情
160
-
161
- constraints:
162
- no_network: true
163
- no_inventing_data: true
164
- require_confidence: true
165
- require_rationale: true
166
- ---
167
-
168
- # 指令内容
169
-
170
- (这里写 prompt)
171
- ```
172
-
173
- ## 在 Codex / Cursor 中使用
174
-
175
- ### 方式 1:直接对话(零配置)
176
-
177
- ```
178
- 读取 ~/.cognitive/modules/ui-spec-generator/MODULE.md,
179
- 为电商首页生成 UI 规范,保存到 ui-spec.json
180
- ```
181
-
182
- ### 方式 2:AGENTS.md(项目约定)
183
-
184
- 在项目根目录创建 `AGENTS.md`:
185
-
186
- ```markdown
187
- ## UI 规范生成
188
-
189
- 当需要生成 UI 规范时:
190
- 1. 读取 `~/.cognitive/modules/ui-spec-generator/MODULE.md`
191
- 2. 按 schema.json 格式输出
192
- 3. 保存到 ui-spec.json
193
- ```
194
-
195
- ### 方式 3:包装成 Skill
196
-
197
- ```yaml
198
- # ~/.claude/skills/ui-spec/SKILL.md
199
- ---
200
- name: ui-spec
201
- description: 生成 UI 规范
202
- ---
203
- 执行 ~/.cognitive/modules/ui-spec-generator/MODULE.md
204
- ```
205
-
206
- ## 配置 LLM(仅 CLI 需要)
207
-
208
- ```bash
209
- # OpenAI
210
- export LLM_PROVIDER=openai
211
- export OPENAI_API_KEY=sk-xxx
212
-
213
- # Anthropic Claude
214
- export LLM_PROVIDER=anthropic
215
- export ANTHROPIC_API_KEY=sk-ant-xxx
216
-
217
- # Ollama(本地免费)
218
- export LLM_PROVIDER=ollama
219
-
220
- # 不配置则使用 stub(返回示例输出)
221
- ```
222
-
223
- ## 模块搜索路径
224
-
225
- 模块按以下顺序查找:
226
-
227
- 1. `./cognitive/modules/` - 项目本地
228
- 2. `~/.cognitive/modules/` - 用户全局
229
- 3. `$COGNITIVE_MODULES_PATH` - 自定义路径
230
-
231
- ## 创建新模块
232
-
233
- ```bash
234
- # 1. 创建骨架
235
- cog init my-module -d "模块职责描述"
236
-
237
- # 2. 编辑 MODULE.md 添加指令
238
- # 3. 编辑 schema.json 定义输入输出
239
- # 4. 验证
240
- cog validate my-module
241
-
242
- # 5. 全局安装(可选)
243
- cog install ./cognitive/modules/my-module
244
- ```
245
-
246
- ## 内置模块
247
-
248
- ### ui-spec-generator
249
-
250
- 将产品需求转换为前端可实现的 UI 规范。
251
-
252
- **输出包含**:
253
- - 信息架构(sections + hierarchy)
254
- - 组件定义(type, props, states)
255
- - 交互设计(events, transitions)
256
- - 响应式规则(breakpoints, layout)
257
- - 可访问性(WCAG 要求)
258
- - 验收标准(可测试条件)
259
- - 置信度 + 推理过程
260
-
261
- ```bash
262
- cog run ui-spec-generator examples/input.json -o ui-spec.json --pretty
263
- ```
264
-
265
- ## 项目结构
266
-
267
- ```
268
- cognitive-modules/
269
- ├── README.md # 本文件
270
- ├── SPEC.md # 规范文档
271
- ├── INTEGRATION.md # Agent 集成指南
272
- ├── AGENTS.md # Agent 约定示例
273
- ├── cognitive-registry.json # 公共模块注册表
274
- ├── src/cognitive/ # CLI 源码
275
- │ ├── cli.py # 命令入口
276
- │ ├── loader.py # 模块加载(支持新旧格式)
277
- │ ├── runner.py # 模块执行
278
- │ ├── validator.py # 模块验证
279
- │ ├── registry.py # 模块发现与安装
280
- │ ├── templates.py # 模块模板
281
- │ └── providers/ # LLM 后端
282
- ├── cognitive/modules/ # 内置模块
283
- │ └── ui-spec-generator/
284
- └── pyproject.toml
285
- ```
286
-
287
- ## 文档
288
-
289
- - [SPEC.md](SPEC.md) - 完整规范文档
290
- - [INTEGRATION.md](INTEGRATION.md) - Agent 工具集成指南
291
- - [AGENTS.md](AGENTS.md) - Agent 约定示例
292
-
293
- ## License
294
-
295
- MIT
@@ -1,15 +0,0 @@
1
- cognitive/__init__.py,sha256=uSX5NuOWyW0qtq1bnxbzabZ0OQakAtsjF0MWbjQBvwE,401
2
- cognitive/cli.py,sha256=q0vHCHFmig9gQ85KRyIrX6vrJZFm5nXF_bW5qQRGEPU,14608
3
- cognitive/loader.py,sha256=E_lSuy2ERBWfziOHr3f13nLq-wEYvBqIKrGlPk4PQyA,4031
4
- cognitive/registry.py,sha256=Adb9246LH5mj6aObpZs-QkqdB3iC7f9-3C0kItgfvrI,8850
5
- cognitive/runner.py,sha256=9uxBAGd3gtXnwUm5x2CjhXi_S1gd2nfjRfM3Q_1f30Q,4362
6
- cognitive/subagent.py,sha256=fb7LWwNF6YcJtC_T1dK0EvzqWMBnav-kiCIpvVohEBw,8142
7
- cognitive/templates.py,sha256=fEd1Tj5A12PUE256dC7-cnsulWzm6MTrcQ0yLPWrtx0,4692
8
- cognitive/validator.py,sha256=1v1HUHYOlAc2sYCkIq_gnUMMnca0fdtQwr7UMBFpp04,12200
9
- cognitive/providers/__init__.py,sha256=hqhVA1IEXpVtyCAteXhO5yD8a8ikQpVIPEKJVHLtRFY,7492
10
- cognitive_modules-0.1.0.dist-info/licenses/LICENSE,sha256=f7MIeafHCzp_CW2Oq1aAjmZjFUa3hBILmBEMl7AsK7k,1063
11
- cognitive_modules-0.1.0.dist-info/METADATA,sha256=wS_z7RLTFaawk0XDoqiRNoibRrfM6UH9hxRliNcUm28,7530
12
- cognitive_modules-0.1.0.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
13
- cognitive_modules-0.1.0.dist-info/entry_points.txt,sha256=PKHlfrFmve5K2349ryipySKbOOsKxo_vIq1NNT-iBV0,42
14
- cognitive_modules-0.1.0.dist-info/top_level.txt,sha256=kGIfDucCKylo8cRBtxER_v3DHIea-Sol9x9YSJo1u3Y,10
15
- cognitive_modules-0.1.0.dist-info/RECORD,,