@datafrog-io/n2n-memory 1.0.2 → 1.0.3
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/CHANGELOG.md +60 -0
- package/LICENSE +21 -21
- package/README.md +89 -125
- package/build/core/memory-manager.js +141 -0
- package/build/core/memory-manager.js.map +1 -0
- package/build/core/memory-service.js +340 -0
- package/build/core/memory-service.js.map +1 -0
- package/build/handlers/mcp-handlers.js +258 -0
- package/build/handlers/mcp-handlers.js.map +1 -0
- package/build/index.js +28 -236
- package/build/index.js.map +1 -1
- package/build/tools/definitions.js +67 -0
- package/build/tools/definitions.js.map +1 -0
- package/build/tools/schemas.js +79 -0
- package/build/tools/schemas.js.map +1 -0
- package/build/types.js +8 -0
- package/build/types.js.map +1 -1
- package/build/utils/env.js +27 -0
- package/build/utils/env.js.map +1 -0
- package/build/utils/path-utils.js +62 -0
- package/build/utils/path-utils.js.map +1 -0
- package/package.json +22 -8
- package/build/memory-manager.js +0 -102
- package/build/memory-manager.js.map +0 -1
- package/docs/API_REFERENCE.md +0 -70
- package/docs/DESIGN.md +0 -52
- package/docs/DEVELOPMENT.md +0 -54
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
[中文版](./docs/CHANGELOG_zh.md)
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
All notable changes to this project will be documented in this file.
|
|
8
|
+
|
|
9
|
+
## [1.1.0] - 2024-12-19
|
|
10
|
+
|
|
11
|
+
### Added
|
|
12
|
+
- Bilingual documentation support:
|
|
13
|
+
- Separated `README` into English and Chinese.
|
|
14
|
+
- All supplemental docs (`API_REFERENCE`, `DESIGN`, `DEVELOPMENT`, `CHANGELOG`) are now bilingual and cross-linked.
|
|
15
|
+
- **Infrastructure Hardening**:
|
|
16
|
+
- Added `engines` field to `package.json` to enforce Node.js >= 24 and npm >= 11.5.1.
|
|
17
|
+
- Implemented runtime environment check (Node & npm) during MCP server startup to prevent deployment issues.
|
|
18
|
+
- **Dependency Maintenance**:
|
|
19
|
+
- Upgraded all dependencies to their latest stable versions (including `sinon`, `typescript`, `eslint`, etc.).
|
|
20
|
+
- Verified zero vulnerabilities and zero outdated packages.
|
|
21
|
+
- **High-Reliability Architecture**:
|
|
22
|
+
- Implemented **Dual-Buffer Service**:
|
|
23
|
+
- **Snapshot Buffer**: In-memory cache for ultra-fast reads.
|
|
24
|
+
- **Write Queue**: Sequentialized background writes to prevent data corruption.
|
|
25
|
+
- **Atomic Persistence**: Uses temporary file swaps to ensure JSON integrity during crashes.
|
|
26
|
+
- **Cross-Process Locking**: Integrated `proper-lockfile` to handle multiple IDE windows accessing the same project.
|
|
27
|
+
- **AI-Driven Path Pinning**: Updated tool descriptions to guide AI assistants to report project paths at session start.
|
|
28
|
+
- **Clean Architecture & SDK Modernization**:
|
|
29
|
+
- Migrated to the latest `@modelcontextprotocol/sdk` high-level `McpServer` API.
|
|
30
|
+
- Resolved `Server` class deprecation warnings (TS6385).
|
|
31
|
+
- Decentralized `index.ts` into a maintainable directory structure:
|
|
32
|
+
- `/core`: Domain logic (MemoryService, MemoryManager).
|
|
33
|
+
- `/tools`: Tool definitions and Zod schemas.
|
|
34
|
+
- `/handlers`: Pure request dispatching logic via `registerAll`.
|
|
35
|
+
- `/utils`: Environment and system utilities.
|
|
36
|
+
- **Hot/Cold Data Separation (Project Context)**:
|
|
37
|
+
- Introduced `context.json` to store high-frequency "Hot State" (active tasks, status, next steps).
|
|
38
|
+
- Isolated memory graph in `memory.json` to preserve architectural "Cold Knowledge".
|
|
39
|
+
- Implemented independent physical locking (`proper-lockfile`) and memory mutexes for `context.json`.
|
|
40
|
+
- New tool `n2n_update_context` with mandatory Git-Sync protocol enforcement.
|
|
41
|
+
- `n2n_read_graph` now performs a "Unified Read" returning both Knowledge Graph and Active Context.
|
|
42
|
+
- **N2N-SYNC Protocol Enforcement**:
|
|
43
|
+
- Hardcoded mandatory synchronization constraints into MCP tool metadata.
|
|
44
|
+
- Every session start (`n2n_read_graph`) now binds the AI to a "Memory Update before Git Commit" policy.
|
|
45
|
+
- Added `[MANDATORY PROTOCOL]` and `[HARD CONSTRAINT]` tags to write tools to prevent context drift during refactoring.
|
|
46
|
+
- Improved test coverage and fixed cross-module import paths.
|
|
47
|
+
- Initial `CHANGELOG.md` to track project evolution and technical discoveries.
|
|
48
|
+
|
|
49
|
+
### Fixed
|
|
50
|
+
- **NPM Publishing Compatibility**: Identified and documented a critical requirement for Trusted Publishing (OIDC).
|
|
51
|
+
- **Discovery**: NPM CLI version 10.x (shipped with Node.js 22) fails with `404 Not Found` when attempting OIDC-based publishing, even if the configuration is correct.
|
|
52
|
+
- **Requirement**: Must use **NPM CLI 11.5.1+** or Node.js 24.
|
|
53
|
+
- **Mitigation**: Added `npm install -g npm@latest` recommendation to CI/CD workflows.
|
|
54
|
+
|
|
55
|
+
## [1.0.0] - Prior to 2024-12-19
|
|
56
|
+
|
|
57
|
+
### Added
|
|
58
|
+
- Core MCP functionality for project-local memory isolation.
|
|
59
|
+
- Cognitive persistence at `[Project Root]/.mcp/memory.json`.
|
|
60
|
+
- Tools: `n2n_add_entities`, `n2n_add_observations`, `n2n_create_relations`, `n2n_read_graph`, `n2n_search`.
|
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2024 DataFrog IO
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 DataFrog IO
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,125 +1,89 @@
|
|
|
1
|
-
# n2n-memory
|
|
2
|
-
|
|
3
|
-
[
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
###
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
-
|
|
68
|
-
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
##### Cursor / VSCode (MCP 插件)
|
|
93
|
-
在 MCP 设置面板中添加:
|
|
94
|
-
- **Name**: `n2n-memory`
|
|
95
|
-
- **Type**: `command`
|
|
96
|
-
- **Command**: `npx -y @datafrog-io/n2n-memory`
|
|
97
|
-
|
|
98
|
-
#### 2. 使用指南 (Usage Guide)
|
|
99
|
-
|
|
100
|
-
本服务完全由路径驱动,AI 助手在调用工具时需要关注以下几点:
|
|
101
|
-
|
|
102
|
-
1. **绝对路径**: 调用任何 `n2n_*` 工具时,必须传入当前项目根目录的**绝对路径**(`projectPath`)。
|
|
103
|
-
2. **自动存储**: 记忆将自动保存在 `[项目路径]/.mcp/memory.json`。
|
|
104
|
-
3. **协作共享**: 建议将 `.mcp/memory.json` 提交至 Git 仓库,以便团队成员共享知识图谱。
|
|
105
|
-
|
|
106
|
-
##### 常用工具示例:
|
|
107
|
-
- `n2n_add_entities`: 创建新实体。
|
|
108
|
-
- `n2n_add_observations`: 追加观测事实。
|
|
109
|
-
- `n2n_create_relations`: 建立实体间联系。
|
|
110
|
-
- `n2n_read_graph`: 读取完整图谱。
|
|
111
|
-
- `n2n_search`: 关键词搜索图谱(支持实体名、类型、观测事实)。
|
|
112
|
-
|
|
113
|
-
---
|
|
114
|
-
|
|
115
|
-
## 📖 Related Docs | 文档指引
|
|
116
|
-
|
|
117
|
-
- **[Design Solution | 设计方案](./docs/DESIGN.md)**: Why project-level isolation?
|
|
118
|
-
- **[API Reference | API 参考手册](./docs/API_REFERENCE.md)**: Tool descriptions and schema.
|
|
119
|
-
- **[Development | 开发指南](./docs/DEVELOPMENT.md)**: How to build, test and extend.
|
|
120
|
-
|
|
121
|
-
## 📄 License | 许可证
|
|
122
|
-
This project is licensed under the [MIT License](./LICENSE). | 本项目采用 [MIT 许可证](./LICENSE)。
|
|
123
|
-
|
|
124
|
-
---
|
|
125
|
-
**N2N Studio [@n2ns](https://github.com/n2ns)**
|
|
1
|
+
# n2n-memory
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@datafrog-io/n2n-memory)
|
|
4
|
+
[](https://github.com/n2ns/n2n-memory/blob/main/LICENSE)
|
|
5
|
+
[](https://modelcontextprotocol.io)
|
|
6
|
+
[](https://nodejs.org)
|
|
7
|
+
[](https://github.com/n2ns)
|
|
8
|
+
[](https://datafrog.io)
|
|
9
|
+
|
|
10
|
+
[中文版](./docs/README_zh.md)
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
> **Context as code. Memory as asset.**
|
|
15
|
+
|
|
16
|
+
A specialized MCP server designed to solve "memory pollution" during AI-assisted cross-project development. It persists AI's cognitive fragments directly within each project's own directory.
|
|
17
|
+
|
|
18
|
+
### 🌟 Key Highlights
|
|
19
|
+
- **Project-Level Physical Isolation**: Memory files are stored at `[Project Root]/.mcp/memory.json`.
|
|
20
|
+
- **Git-Friendly**: JSON data is automatically sorted by key to generate clean and readable `git diff`.
|
|
21
|
+
- **Tool Agnostic**: Uses the `.mcp` naming convention, not tied to any specific AI brand or IDE plugin.
|
|
22
|
+
- **Assets for Your Code**: Memory stays with your code; team members can share AI's understanding of the architecture by simply pulling the repository.
|
|
23
|
+
- **Universal Compatibility**: Works with all MCP-enabled models including **Claude 4.5**, **Gemini 3 Pro/Flash**, **GPT-5/5.2**, and **DeepSeek V3.2**.
|
|
24
|
+
- **Privacy-First**: Built with security by design, keeping your data local and isolated.
|
|
25
|
+
|
|
26
|
+
### 🚀 Quick Start
|
|
27
|
+
|
|
28
|
+
#### 1. Installation & Config (IDE / Claude Desktop)
|
|
29
|
+
|
|
30
|
+
The easiest way to use this is via `npx`:
|
|
31
|
+
|
|
32
|
+
##### Claude Desktop
|
|
33
|
+
File Path: `%APPDATA%\Claude\claude_desktop_config.json`
|
|
34
|
+
|
|
35
|
+
```json
|
|
36
|
+
{
|
|
37
|
+
"mcpServers": {
|
|
38
|
+
"n2n-memory": {
|
|
39
|
+
"command": "npx",
|
|
40
|
+
"args": ["-y", "@datafrog-io/n2n-memory"]
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
##### Cursor / VSCode (MCP Plugin)
|
|
47
|
+
Add in the MCP settings panel:
|
|
48
|
+
- **Name**: `n2n-memory`
|
|
49
|
+
- **Type**: `command`
|
|
50
|
+
- **Command**: `npx -y @datafrog-io/n2n-memory`
|
|
51
|
+
|
|
52
|
+
#### 2. Usage Guide
|
|
53
|
+
|
|
54
|
+
This service is path-driven. AI assistants should pay attention to:
|
|
55
|
+
|
|
56
|
+
1. **Absolute Paths**: When calling any `n2n_*` tool, the absolute path of the current project root (`projectPath`) must be provided.
|
|
57
|
+
2. **Auto Storage**: Memory is automatically saved to `[ProjectPath]/.mcp/memory.json`.
|
|
58
|
+
3. **Collaboration**: It is recommended to commit `.mcp/memory.json` to your Git repository to share the knowledge graph with your team.
|
|
59
|
+
|
|
60
|
+
##### Available Tools:
|
|
61
|
+
- `n2n_add_entities`: Create new entities.
|
|
62
|
+
- `n2n_add_observations`: Append observations or facts.
|
|
63
|
+
- `n2n_create_relations`: Establish connections between entities.
|
|
64
|
+
- `n2n_read_graph`: Read project memory and active context (Supports `summaryMode` and `pagination`).
|
|
65
|
+
- `n2n_get_graph_summary`: Quickly fetch a lightweight index of all entities (Supports `pagination`).
|
|
66
|
+
- `n2n_update_context`: Update current task status and next steps.
|
|
67
|
+
- `n2n_search`: Search the graph via keywords (Supports `pagination`).
|
|
68
|
+
- `n2n_open_nodes`: Retrieve specific entities by name.
|
|
69
|
+
|
|
70
|
+
### 🗺️ Future Roadmap
|
|
71
|
+
- **Semantic Search**: Integration of minimalist Vector Embeddings for fuzzy memory retrieval.
|
|
72
|
+
- **Ontology Enforcement**: Optional schema for relation type consistency.
|
|
73
|
+
- **Time Travel**: Versioned snapshots for memory rollback.
|
|
74
|
+
|
|
75
|
+
---
|
|
76
|
+
|
|
77
|
+
## 📖 Related Docs
|
|
78
|
+
|
|
79
|
+
- **[Design Solution](./docs/DESIGN.md)**: Why project-level isolation?
|
|
80
|
+
- **[API Reference](./docs/API_REFERENCE.md)**: Tool descriptions and schema.
|
|
81
|
+
- **[Development](./docs/DEVELOPMENT.md)**: How to build, test and extend.
|
|
82
|
+
- **[Changelog](./CHANGELOG.md)**: Version history and incident recovery.
|
|
83
|
+
|
|
84
|
+
## 📄 License
|
|
85
|
+
This project is licensed under the [MIT License](./LICENSE).
|
|
86
|
+
|
|
87
|
+
---
|
|
88
|
+
|
|
89
|
+
**N2N Studio** — The AI Innovation Lab of [DataFrog.io](https://datafrog.io).
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import fs from "fs-extra";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { KnowledgeGraphSchema, ProjectContextSchema } from "../types.js";
|
|
4
|
+
export const MEMORY_FILE_PATH = ".mcp/memory.json";
|
|
5
|
+
export const CONTEXT_FILE_PATH = ".mcp/context.json";
|
|
6
|
+
export class MemoryManager {
|
|
7
|
+
static async readGraph(projectPath) {
|
|
8
|
+
const filePath = path.resolve(projectPath, MEMORY_FILE_PATH);
|
|
9
|
+
try {
|
|
10
|
+
if (await fs.pathExists(filePath)) {
|
|
11
|
+
const data = await fs.readJson(filePath);
|
|
12
|
+
return KnowledgeGraphSchema.parse(data);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
console.error(`[MemoryManager] Read error at ${filePath}:`, error);
|
|
17
|
+
}
|
|
18
|
+
return { entities: [], relations: [] };
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Writes the graph to disk atomically using a temporary file.
|
|
22
|
+
* Ensures git-friendly sorting before write.
|
|
23
|
+
*/
|
|
24
|
+
static async writeGraph(projectPath, graph) {
|
|
25
|
+
const filePath = path.resolve(projectPath, MEMORY_FILE_PATH);
|
|
26
|
+
const tempPath = `${filePath}.${Date.now()}.tmp`;
|
|
27
|
+
const dirPath = path.dirname(filePath);
|
|
28
|
+
try {
|
|
29
|
+
// Git-friendly sorting
|
|
30
|
+
graph.entities.sort((a, b) => a.name.localeCompare(b.name));
|
|
31
|
+
graph.entities.forEach(entity => {
|
|
32
|
+
if (entity.observations) {
|
|
33
|
+
entity.observations.sort();
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
entity.observations = [];
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
graph.relations.sort((a, b) => {
|
|
40
|
+
const fromComp = a.from.localeCompare(b.from);
|
|
41
|
+
if (fromComp !== 0)
|
|
42
|
+
return fromComp;
|
|
43
|
+
const toComp = a.to.localeCompare(b.to);
|
|
44
|
+
if (toComp !== 0)
|
|
45
|
+
return toComp;
|
|
46
|
+
return a.relationType.localeCompare(b.relationType);
|
|
47
|
+
});
|
|
48
|
+
await fs.ensureDir(dirPath);
|
|
49
|
+
// 1. Write to temporary file
|
|
50
|
+
await fs.writeJson(tempPath, graph, { spaces: 2 });
|
|
51
|
+
// 2. Atomic rename
|
|
52
|
+
await fs.move(tempPath, filePath, { overwrite: true });
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
// Cleanup temp file if it exists and write failed
|
|
56
|
+
if (await fs.pathExists(tempPath)) {
|
|
57
|
+
await fs.remove(tempPath);
|
|
58
|
+
}
|
|
59
|
+
throw new Error(`Failed to write memory file at ${filePath}: ${error instanceof Error ? error.message : String(error)}`);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
static async readContext(projectPath) {
|
|
63
|
+
const filePath = path.resolve(projectPath, CONTEXT_FILE_PATH);
|
|
64
|
+
try {
|
|
65
|
+
if (await fs.pathExists(filePath)) {
|
|
66
|
+
const data = await fs.readJson(filePath);
|
|
67
|
+
return ProjectContextSchema.parse(data);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
console.error(`[MemoryManager] Context read error at ${filePath}:`, error);
|
|
72
|
+
}
|
|
73
|
+
return { status: "PLANNING", nextSteps: [] };
|
|
74
|
+
}
|
|
75
|
+
static async writeContext(projectPath, context) {
|
|
76
|
+
const filePath = path.resolve(projectPath, CONTEXT_FILE_PATH);
|
|
77
|
+
const tempPath = `${filePath}.${Date.now()}.tmp`;
|
|
78
|
+
const dirPath = path.dirname(filePath);
|
|
79
|
+
try {
|
|
80
|
+
context.updatedAt = new Date().toISOString();
|
|
81
|
+
await fs.ensureDir(dirPath);
|
|
82
|
+
await fs.writeJson(tempPath, context, { spaces: 2 });
|
|
83
|
+
await fs.move(tempPath, filePath, { overwrite: true });
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
if (await fs.pathExists(tempPath)) {
|
|
87
|
+
await fs.remove(tempPath);
|
|
88
|
+
}
|
|
89
|
+
throw new Error(`Failed to write context file at ${filePath}: ${error instanceof Error ? error.message : String(error)}`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
static async openNodes(projectPath, names) {
|
|
93
|
+
const graph = await this.readGraph(projectPath);
|
|
94
|
+
const nameSet = new Set(names);
|
|
95
|
+
const filteredEntities = graph.entities.filter(e => nameSet.has(e.name));
|
|
96
|
+
const entityNames = new Set(filteredEntities.map(e => e.name));
|
|
97
|
+
const filteredRelations = graph.relations.filter(r => entityNames.has(r.from) && entityNames.has(r.to));
|
|
98
|
+
return {
|
|
99
|
+
entities: filteredEntities,
|
|
100
|
+
relations: filteredRelations
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
static async exportToMarkdown(projectPath, outputPath) {
|
|
104
|
+
const graph = await this.readGraph(projectPath);
|
|
105
|
+
const filePath = path.resolve(projectPath, outputPath || "KNOWLEDGE_GRAPH.md");
|
|
106
|
+
let md = "# Knowledge Graph\n\n";
|
|
107
|
+
md += `> Generated from \`${MEMORY_FILE_PATH}\`\n\n`;
|
|
108
|
+
md += "## Entities\n\n";
|
|
109
|
+
if (graph.entities.length === 0) {
|
|
110
|
+
md += "_No entities found._\n\n";
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
for (const entity of graph.entities) {
|
|
114
|
+
md += `### ${entity.name}\n\n`;
|
|
115
|
+
md += `- **Type**: \`${entity.entityType}\`\n`;
|
|
116
|
+
if (entity.observations.length > 0) {
|
|
117
|
+
md += `- **Observations**:\n`;
|
|
118
|
+
for (const obs of entity.observations) {
|
|
119
|
+
md += ` - ${obs}\n`;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
md += "\n";
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
md += "## Relations\n\n";
|
|
126
|
+
if (graph.relations.length === 0) {
|
|
127
|
+
md += "_No relations found._\n\n";
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
md += "| From | Relation | To |\n";
|
|
131
|
+
md += "|------|----------|----|\n";
|
|
132
|
+
for (const rel of graph.relations) {
|
|
133
|
+
md += `| ${rel.from} | ${rel.relationType} | ${rel.to} |\n`;
|
|
134
|
+
}
|
|
135
|
+
md += "\n";
|
|
136
|
+
}
|
|
137
|
+
await fs.writeFile(filePath, md, "utf-8");
|
|
138
|
+
return filePath;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
//# sourceMappingURL=memory-manager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory-manager.js","sourceRoot":"","sources":["../../src/core/memory-manager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,UAAU,CAAC;AAC1B,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAEH,oBAAoB,EAEpB,oBAAoB,EACvB,MAAM,aAAa,CAAC;AAErB,MAAM,CAAC,MAAM,gBAAgB,GAAG,kBAAkB,CAAC;AACnD,MAAM,CAAC,MAAM,iBAAiB,GAAG,mBAAmB,CAAC;AAErD,MAAM,OAAO,aAAa;IACtB,MAAM,CAAC,KAAK,CAAC,SAAS,CAAC,WAAmB;QACtC,MAAM,QAAQ,GAAG,IAAI,CAAC,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