code-frontmatter 0.2.5 → 0.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.
package/README.md CHANGED
@@ -1,40 +1,49 @@
1
1
  ---
2
- intent: "README for Code Frontmatter project"
2
+ intent: "Project documentation and usage guide"
3
3
  role: documentation
4
- when_to_load: "Initial project setup or understanding CFM usage"
4
+ when_to_load: "Initial setup or reference"
5
5
  ---
6
6
 
7
7
  # Code Frontmatter (CFM)
8
8
 
9
- > **"Identity Cards" for your code files.**
10
- > **Let AI understand your entire project without reading every single line.**
9
+ [![npm version](https://img.shields.io/npm/v/code-frontmatter.svg?style=flat-square)](https://www.npmjs.com/package/code-frontmatter)
10
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg?style=flat-square)](https://opensource.org/licenses/MIT)
11
+ [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://makeapullrequest.com)
12
+
13
+ [中文文档](./README.zh-CN.md) | [English](./README.md)
14
+
15
+ > **The "Passport" for your code files.**
16
+ > **Empower AI to understand your entire repository with < 5% of the token cost.**
11
17
 
12
18
  ---
13
19
 
14
- Code Frontmatter (CFM) is an open standard and MCP (Model Context Protocol) Server that allows you to embed structured metadata (YAML) at the top of every source code file.
20
+ **Code Frontmatter (CFM)** is an open standard and [MCP (Model Context Protocol)](https://modelcontextprotocol.io/) Server that manages structured metadata within your source code.
15
21
 
16
- By scanning these lightweight headers, AI coding assistants (like Cursor, Windsurf, Claude Code) can build a cognitive map of your entire project with **minimal token cost**, eliminating hallucinations caused by context overload.
22
+ By adding a lightweight YAML header to your files, you promote your codebase from "text files" to a "semantic graph" that AI agents (like Cursor, Windsurf, Claude) can navigate instantly and accurately.
17
23
 
18
24
  ## 🚀 Why CFM?
19
25
 
20
- | Feature | Without CFM | With CFM |
21
- |---|---|---|
22
- | **Context Load** | Full project files (huge tokens) | Just headers (~5% tokens) |
23
- | **Accuracy** | High hallucination risk | Deterministic file intent |
24
- | **Cost** | $$$ Expensive API calls | $ Cheap & Efficient |
25
- | **Scale** | Fails on large repos | Works on 10k+ file repos |
26
+ AI coding assistants often struggle with large codebases:
27
+ 1. **Context Overflow**: Reading every file to understand the architecture consumes massive context windows ($$$).
28
+ 2. **Hallucination**: Guessing file purposes leads to bugs.
29
+ 3. **blindness**: Agents don't know *where* to look for specific logic.
30
+
31
+ **CFM solves this by exposing high-level intent, architectural roles, and dependency graphs without requiring the AI to read the code body.**
32
+
33
+ | Metric | Without CFM | With CFM |
34
+ | :--- | :--- | :--- |
35
+ | **Context Cost** | 100% (Full Source) | **~3-5% (Headers Only)** |
36
+ | **Search Speed** | Slow (Grep/Read) | **Instant (Semantic Index)** |
37
+ | **Accuracy** | Probabilistic | **Deterministic** |
26
38
 
27
39
  ## 📦 Installation
28
40
 
29
- To use CFM with your AI IDE (Cursor, Claude Desktop, etc.), install it as an MCP Server:
41
+ This project is designed as a **Model Context Protocol (MCP)** server.
30
42
 
31
- ```bash
32
- npx -y code-frontmatter
33
- ```
43
+ ### 1. For [Cursor](https://cursor.sh)
34
44
 
35
- ### Configuration
45
+ Add the following to your `~/.cursor/mcp.json` (or strictly via the Cursor Settings UI):
36
46
 
37
- #### For Cursor (`~/.cursor/mcp.json`)
38
47
  ```json
39
48
  {
40
49
  "mcpServers": {
@@ -46,7 +55,10 @@ npx -y code-frontmatter
46
55
  }
47
56
  ```
48
57
 
49
- #### For Claude Desktop
58
+ ### 2. For [Claude Desktop](https://claude.ai/download)
59
+
60
+ Add the following to your `claude_desktop_config.json`:
61
+
50
62
  ```json
51
63
  {
52
64
  "mcpServers": {
@@ -58,61 +70,89 @@ npx -y code-frontmatter
58
70
  }
59
71
  ```
60
72
 
61
- ## 🛠 Usage
73
+ ## 🤖 Instructions for AI Agents (Critical)
74
+
75
+ **To get the most out of CFM, you must instruct your AI assistant to use it.**
76
+ AI models are lazy; they will try to guess instead of calling tools unless explicitly told otherwise.
77
+
78
+ **Copy and paste the following into your Project Rules (`.cursorrules` / `.windsurfrules`) or Custom Instructions:**
79
+
80
+ ```markdown
81
+ # Code Frontmatter Protocol (CFM)
82
+
83
+ This project uses Code Frontmatter (CFM) to manage file metadata.
84
+ You MUST follow these rules strictly:
85
+
86
+ 1. **Initialize Context**: At the start of a session, ALWAYS call `cfm_read` (or `cfm_scan`) on the project root to build your mental map.
87
+ - DO NOT list all files with `ls -R`. Use `cfm_read` instead.
88
+ 2. **Search First**: When looking for specific functionality (e.g., "auth logic"), use `cfm_search` before reading file contents.
89
+ 3. **Maintain Headers**:
90
+ - When creating a new file, you MUST generate a valid CFM header (intent, role, exports).
91
+ - When significantly modifying code, you MUST update the `exports` and `ai_notes` in the header using `cfm_write`.
92
+ - KEEP `ai_notes` BRIEF. Only store permanent architectural constraints, not change logs.
93
+ ```
94
+
95
+ ## 🛠 Usage & Syntax
62
96
 
63
- ### 1. Add Frontmatter to Your Code
97
+ ### The Header Format
64
98
 
65
- Add a CFM header to the top of your files using your language's comment syntax.
99
+ CFM uses a YAML block inside your language's standard comment syntax.
66
100
 
67
- **JavaScript / TypeScript:**
68
- ```javascript
101
+ **TypeScript / JavaScript / Java / C#:**
102
+ ```typescript
69
103
  /*---
70
- intent: "Manages user authentication state and login logic"
104
+ intent: "Handles JWT token validation and rotation"
71
105
  role: service
72
106
  exports:
73
- - "login: Authenticate user with email/pass"
74
- - "logout: Clear session"
75
- depends_on: ["api-client.ts", "store.ts"]
76
- when_to_load: "Modifying auth flow or session handling"
107
+ - "verifyToken: Checks signature"
108
+ - "refreshToken: Issues new access token"
109
+ depends_on: ["config.ts", "db-client.ts"]
110
+ ai_notes: "Do not modify the secret key derivation logic."
77
111
  ---*/
78
112
 
79
- export function login(email, password) { ... }
113
+ export function verifyToken(token) { ... }
80
114
  ```
81
115
 
82
- **Python:**
116
+ **Python / Ruby / Shell / YAML:**
83
117
  ```python
84
118
  #---
85
- # intent: "Data model for User entity"
119
+ # intent: "User data model definition"
86
120
  # role: model
87
- # exports:
88
- # - "User: Standard user class"
121
+ # domain: "identity"
89
122
  # mutates_state: false
90
123
  #---
91
124
 
92
- class User: ...
125
+ class User(BaseModel): ...
93
126
  ```
94
127
 
95
- ### 2. Available Tools
128
+ ### Available Tools
96
129
 
97
- The MCP Server exposes the following tools to your AI assistant:
130
+ When installed as an MCP server, your AI gains these super-powers:
98
131
 
99
- - **`cfm_read(directory)`**: Scans your project and returns a structured index of all CFM headers.
100
- - **`cfm_search(query)`**: Search for files by keyword, role, or domain without reading full contents.
101
- - **`cfm_register_language(name, config)`**: Teach CFM how to parse headers for a new language on the fly.
102
-
103
- ## 📋 Schema
104
-
105
- A valid CFM header requires at least:
106
- - **`intent`**: What is this file for? (String)
107
- - **`role`**: What is its architectural role? (String, e.g., `component`, `service`, `util`)
108
- - **`exports`**: What key things does it export? (Array of Strings)
109
-
110
- Optional fields: `depends_on`, `when_to_load`, `side_effects`, `mutates_state`, `domain`, `ai_notes`.
132
+ * **`cfm_read({ directory })`**:
133
+ * Returns a high-level summary of all files (paths, intents, roles, exports) in one JSON object.
134
+ * *Best for: Initializing session context.*
135
+ * **`cfm_search({ query, role, domain })`**:
136
+ * Semantic search over the headers.
137
+ * *Best for: "Find where user billing is handled".*
138
+ * **`cfm_write({ file, intent, ... })`**:
139
+ * Writes or updates the header.
140
+ * *Best for: Creating new files or updating documentation.*
141
+ * **`cfm_register_language({ name, extensions, ... })`**:
142
+ * Teaches the server how to parse headers for custom file types.
111
143
 
112
144
  ## 🤝 Contributing
113
145
 
114
- We welcome contributions! Please follow the standard GitHub workflow.
146
+ We want to make this the industry standard for AI-Code interaction.
147
+ 1. Fork the repo.
148
+ 2. Create a feature branch.
149
+ 3. Submit a PR.
150
+
151
+ **Focus areas:**
152
+ - Parsers for more languages (Rust, Go, Swift).
153
+ - IDE Extensions (VS Code, JetBrains).
154
+ - Analysis tools.
115
155
 
116
156
  ## 📄 License
117
157
 
118
- MIT © 2026 coobi7
158
+ MIT © 2026 [coobi7](https://github.com/coobi7)
@@ -0,0 +1,156 @@
1
+ ---
2
+ intent: "项目文档和使用指南(中文版)"
3
+ role: documentation
4
+ when_to_load: "初次安装或查阅使用说明"
5
+ ---
6
+
7
+ # Code Frontmatter (CFM)
8
+
9
+ [![npm version](https://img.shields.io/npm/v/code-frontmatter.svg?style=flat-square)](https://www.npmjs.com/package/code-frontmatter)
10
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg?style=flat-square)](https://opensource.org/licenses/MIT)
11
+ [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://makeapullrequest.com)
12
+
13
+ > **代码文件的“身份证”。**
14
+ > **让 AI 仅用 < 5% 的 Token 成本就能理解你的整个代码库。**
15
+
16
+ ---
17
+
18
+ **Code Frontmatter (CFM)** 是一个开放标准和 [MCP (Model Context Protocol)](https://modelcontextprotocol.io/) 服务器,用于管理源代码文件中的结构化元数据。
19
+
20
+ 通过在文件顶部添加轻量级的 YAML 头部,你可以将代码库从普通的“文本文件集合”升级为一张“语义图谱”,让 AI 助手(如 Cursor、Windsurf、Claude)能够即时、准确地导航。
21
+
22
+ ## 🚀 为什么要用 CFM?
23
+
24
+ AI 编程助手在处理大型代码库时常面临以下问题:
25
+ 1. **上下文溢出**:读取所有文件以理解架构会消耗巨大的上下文窗口(且昂贵)。
26
+ 2. **幻觉**:AI 需猜测文件用途,容易导致 Bug。
27
+ 3. **盲区**:Agent 不知道去哪里寻找特定的逻辑。
28
+
29
+ **CFM 通过暴露高层意图、架构角色和依赖关系图,解决了这些问题,且无需 AI 读取代码正文。**
30
+
31
+ | 指标 | 不使用 CFM | 使用 CFM |
32
+ | :--- | :--- | :--- |
33
+ | **上下文成本** | 100% (全源码) | **~3-5% (仅头部)** |
34
+ | **检索速度** | 慢 (Grep/Read) | **即时 (语义索引)** |
35
+ | **准确性** | 概率性猜测 | **确定性** |
36
+
37
+ ## 📦 安装
38
+
39
+ 本项目设计为 **Model Context Protocol (MCP)** 服务器。
40
+
41
+ ### 1. 适用于 [Cursor](https://cursor.sh)
42
+
43
+ 将以下内容添加到你的 `~/.cursor/mcp.json`(或者通过 Cursor 设置界面添加):
44
+
45
+ ```json
46
+ {
47
+ "mcpServers": {
48
+ "code-frontmatter": {
49
+ "command": "npx",
50
+ "args": ["-y", "code-frontmatter"]
51
+ }
52
+ }
53
+ }
54
+ ```
55
+
56
+ ### 2. 适用于 [Claude Desktop](https://claude.ai/download)
57
+
58
+ 将以下内容添加到你的 `claude_desktop_config.json`:
59
+
60
+ ```json
61
+ {
62
+ "mcpServers": {
63
+ "code-frontmatter": {
64
+ "command": "npx",
65
+ "args": ["-y", "code-frontmatter"]
66
+ }
67
+ }
68
+ }
69
+ ```
70
+
71
+ ## 🤖 AI Agent 指引 (关键步骤)
72
+
73
+ **为了充分利用 CFM,你必须明确指示你的 AI 助手使用它。**
74
+ AI 模型通常比较“懒”,除非明确告知,否则它们倾向于猜测而不是调用工具。
75
+
76
+ **请将以下内容复制并粘贴到你的项目规则 (`.cursorrules` / `.windsurfrules`) 或自定义指令中:**
77
+
78
+ ```markdown
79
+ # Code Frontmatter Protocol (CFM)
80
+
81
+ 本项目使用 Code Frontmatter (CFM) 管理文件元数据。
82
+ 你必须严格遵守以下规则:
83
+
84
+ 1. **初始化上下文**:在会话开始时,务必在项目根目录调用 `cfm_read` (或 `cfm_scan`) 以构建你的心理地图。
85
+ - 不要使用 `ls -R` 列出所有文件。请使用 `cfm_read`。
86
+ 2. **搜索优先**:当查找特定功能(例如“认证逻辑”)时,在读取文件内容之前,先使用 `cfm_search`。
87
+ 3. **维护头部**:
88
+ - 创建新文件时,必须生成有效的 CFM 头部(intent, role, exports)。
89
+ - 修改代码时,必须使用 `cfm_write` 更新头部中的 `exports` 和 `ai_notes`。
90
+ - 保持 `ai_notes` 简练。只存储永久性的架构约束,不要记录变更日志。
91
+ ```
92
+
93
+ ## 🛠 使用方法 & 语法
94
+
95
+ ### 头部格式
96
+
97
+ CFM 在你所用语言的标准注释语法中使用 YAML 块。
98
+
99
+ **TypeScript / JavaScript / Java / C#:**
100
+ ```typescript
101
+ /*---
102
+ intent: "处理 JWT 令牌验证和轮换"
103
+ role: service
104
+ exports:
105
+ - "verifyToken: 检查签名"
106
+ - "refreshToken: 发发新访问令牌"
107
+ depends_on: ["config.ts", "db-client.ts"]
108
+ ai_notes: "不要修改密钥派生逻辑。"
109
+ ---*/
110
+
111
+ export function verifyToken(token) { ... }
112
+ ```
113
+
114
+ **Python / Ruby / Shell / YAML:**
115
+ ```python
116
+ #---
117
+ # intent: "用户数据模型定义"
118
+ # role: model
119
+ # domain: "identity"
120
+ # mutates_state: false
121
+ #---
122
+
123
+ class User(BaseModel): ...
124
+ ```
125
+
126
+ ### 可用工具
127
+
128
+ 作为 MCP 服务器安装后,你的 AI 将获得以下超能力:
129
+
130
+ * **`cfm_read({ directory })`**:
131
+ * 返回一个JSON对象,包含所有文件的概要(路径、意图、角色、导出)。
132
+ * *适用场景:初始化会话上下文。*
133
+ * **`cfm_search({ query, role, domain })`**:
134
+ * 对头部进行语义搜索。
135
+ * *适用场景:“查找哪处理用户计费”。*
136
+ * **`cfm_write({ file, intent, ... })`**:
137
+ * 写入或更新头部。
138
+ * *适用场景:创建新文件或更新文档。*
139
+ * **`cfm_register_language({ name, extensions, ... })`**:
140
+ * 教服务器如何解析自定义文件类型的头部。
141
+
142
+ ## 🤝 贡献代码
143
+
144
+ 我们希望将其打造为 AI-Code 交互的行业标准。
145
+ 1. Fork 本仓库。
146
+ 2. 创建一个特性分支。
147
+ 3. 提交 PR。
148
+
149
+ **关注领域:**
150
+ - 更多语言的解析器 (Rust, Go, Swift)。
151
+ - IDE 扩展 (VS Code, JetBrains)。
152
+ - 分析工具。
153
+
154
+ ## 📄 许可证
155
+
156
+ MIT © 2026 [coobi7](https://github.com/coobi7)
package/dist/index.d.ts CHANGED
@@ -1 +1,3 @@
1
1
  #!/usr/bin/env node
2
+ export {};
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":""}