samemory 0.0.0-stage → 0.0.1
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/LICENSE +21 -0
- package/README.md +4 -2
- package/dist/index.mjs +234 -0
- package/package.json +29 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 nazozokc
|
|
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
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { McpServer } from "@modelcontextprotocol/server";
|
|
2
|
+
import { StdioServerTransport } from "@modelcontextprotocol/server/stdio";
|
|
3
|
+
import { mkdir, readFile, readdir, rename, stat, writeFile } from "node:fs/promises";
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import { homedir } from "node:os";
|
|
7
|
+
import { randomUUIDv7 } from "node:crypto";
|
|
8
|
+
import Fuse from "fuse.js";
|
|
9
|
+
// src/memory_get/type.ts
|
|
10
|
+
const MemoryGetInputSchema = z.object({ id: z.string() });
|
|
11
|
+
// src/constant/app.ts
|
|
12
|
+
const MCP_NAME = "samemory";
|
|
13
|
+
const CONFIG_DIR = join(homedir(), ".config", "samemory", "config.json");
|
|
14
|
+
// src/constant/ConfigType.ts
|
|
15
|
+
const ConfigTypeSchema = z.object({ dir: z.string() });
|
|
16
|
+
// src/constant/ConfigDefault.ts
|
|
17
|
+
const ConfigDefault = { dir: join(homedir(), "samemory") };
|
|
18
|
+
// src/constant/ConfigRead.ts
|
|
19
|
+
const ConfigDir = async () => {
|
|
20
|
+
try {
|
|
21
|
+
const readConfig = await readFile(CONFIG_DIR, "utf-8");
|
|
22
|
+
const readConfigSchema = ConfigTypeSchema.safeParse(JSON.parse(readConfig));
|
|
23
|
+
if (readConfigSchema.error) return ConfigDefault.dir;
|
|
24
|
+
return readConfigSchema.data.dir;
|
|
25
|
+
} catch (error) {
|
|
26
|
+
return ConfigDefault.dir;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
// src/Globaltype.ts
|
|
30
|
+
const CategorySchema = z.enum([
|
|
31
|
+
"lessons",
|
|
32
|
+
"patterns",
|
|
33
|
+
"context",
|
|
34
|
+
"commands",
|
|
35
|
+
"preferences"
|
|
36
|
+
]);
|
|
37
|
+
const AgentMemorySchema = z.object({
|
|
38
|
+
id: z.string(),
|
|
39
|
+
category: CategorySchema,
|
|
40
|
+
tags: z.array(z.string()),
|
|
41
|
+
created: z.iso.datetime(),
|
|
42
|
+
agent: z.string(),
|
|
43
|
+
title: z.string(),
|
|
44
|
+
summary: z.string(),
|
|
45
|
+
content: z.string()
|
|
46
|
+
});
|
|
47
|
+
// src/memory_get/index.ts
|
|
48
|
+
const getFile = async (id) => {
|
|
49
|
+
const categories = [
|
|
50
|
+
"lessons",
|
|
51
|
+
"patterns",
|
|
52
|
+
"context",
|
|
53
|
+
"commands",
|
|
54
|
+
"preferences"
|
|
55
|
+
];
|
|
56
|
+
const readMemoryDir = join(await ConfigDir());
|
|
57
|
+
const readMemory = await readdir(readMemoryDir, "utf-8");
|
|
58
|
+
const isCategory = (value) => {
|
|
59
|
+
return categories.some((category) => category === value);
|
|
60
|
+
};
|
|
61
|
+
for (const memory of readMemory) {
|
|
62
|
+
if (!(await stat(join(await ConfigDir(), memory))).isDirectory()) continue;
|
|
63
|
+
if (!isCategory(memory)) continue;
|
|
64
|
+
const dir = join(await ConfigDir(), memory);
|
|
65
|
+
if (!(await readdir(dir, "utf-8")).includes(`${id}.json`)) continue;
|
|
66
|
+
const joinFile = join(await ConfigDir(), memory, `${id}.json`);
|
|
67
|
+
const readJSON = await readFile(joinFile, "utf-8");
|
|
68
|
+
const SafeSchema = AgentMemorySchema.safeParse(JSON.parse(readJSON));
|
|
69
|
+
if (!SafeSchema.success) continue;
|
|
70
|
+
return SafeSchema.data;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
// src/memory_save/type.ts
|
|
74
|
+
const AgentMemorySaveSchema = AgentMemorySchema.omit({
|
|
75
|
+
id: true,
|
|
76
|
+
created: true
|
|
77
|
+
});
|
|
78
|
+
// src/memory_save/index.ts
|
|
79
|
+
const writeMemoryFile = async ({ category, tags, agent, title, summary, content }) => {
|
|
80
|
+
const data = {
|
|
81
|
+
category,
|
|
82
|
+
tags,
|
|
83
|
+
agent,
|
|
84
|
+
title,
|
|
85
|
+
summary,
|
|
86
|
+
content
|
|
87
|
+
};
|
|
88
|
+
const SaveSchema = AgentMemorySaveSchema.safeParse(data);
|
|
89
|
+
if (!SaveSchema.success) throw new Error();
|
|
90
|
+
const savedata = {
|
|
91
|
+
id: randomUUIDv7(),
|
|
92
|
+
created: (/* @__PURE__ */ new Date()).toISOString(),
|
|
93
|
+
...SaveSchema.data
|
|
94
|
+
};
|
|
95
|
+
const SchemaCheck = AgentMemorySchema.safeParse(savedata);
|
|
96
|
+
if (!SchemaCheck.success) throw new Error();
|
|
97
|
+
const categories = SchemaCheck.data.category;
|
|
98
|
+
const directoryTemp = join(await ConfigDir(), categories, `${SchemaCheck.data.id}.json.tmp`);
|
|
99
|
+
const directory = join(await ConfigDir(), categories, `${SchemaCheck.data.id}.json`);
|
|
100
|
+
try {
|
|
101
|
+
const stringify = JSON.stringify(SchemaCheck.data, null, 2);
|
|
102
|
+
await writeFile(directoryTemp, stringify, "utf-8");
|
|
103
|
+
await rename(directoryTemp, directory);
|
|
104
|
+
return true;
|
|
105
|
+
} catch (error) {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
// src/memory_search/type.ts
|
|
110
|
+
const SearchSchema = z.object({
|
|
111
|
+
query: z.string(),
|
|
112
|
+
category: CategorySchema.optional(),
|
|
113
|
+
tags: z.array(z.string()).optional(),
|
|
114
|
+
limit: z.number().optional()
|
|
115
|
+
});
|
|
116
|
+
// src/memory_search/SearchData.ts
|
|
117
|
+
const SearchData = async () => {
|
|
118
|
+
const dir = await ConfigDir();
|
|
119
|
+
const readmemoryDir = await readdir(dir, "utf-8");
|
|
120
|
+
const res = [];
|
|
121
|
+
for (const memoryDir of readmemoryDir) {
|
|
122
|
+
if (!(await stat(join(dir, memoryDir))).isDirectory()) continue;
|
|
123
|
+
if (!CategorySchema.safeParse(memoryDir).success) continue;
|
|
124
|
+
const categoryreadDir = await readdir(join(dir, memoryDir), "utf-8");
|
|
125
|
+
for (const categoryDir of categoryreadDir) {
|
|
126
|
+
const filePath = join(dir, memoryDir, categoryDir);
|
|
127
|
+
if (!(await stat(filePath)).isFile()) continue;
|
|
128
|
+
const FileRead = await readFile(filePath, "utf-8");
|
|
129
|
+
const filesafeparse = AgentMemorySchema.safeParse(JSON.parse(FileRead));
|
|
130
|
+
if (!filesafeparse.success) continue;
|
|
131
|
+
res.push(filesafeparse.data);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return res;
|
|
135
|
+
};
|
|
136
|
+
// src/memory_search/index.ts
|
|
137
|
+
const Search = async ({ query, category, tags, limit }) => {
|
|
138
|
+
const limited = limit ?? 30;
|
|
139
|
+
let results = new Fuse(await SearchData(), {
|
|
140
|
+
includeScore: true,
|
|
141
|
+
includeMatches: true,
|
|
142
|
+
keys: [
|
|
143
|
+
"category",
|
|
144
|
+
"tags",
|
|
145
|
+
"agent",
|
|
146
|
+
"title",
|
|
147
|
+
"summary",
|
|
148
|
+
"content"
|
|
149
|
+
]
|
|
150
|
+
}).search(query);
|
|
151
|
+
if (category !== void 0) results = results.filter((result) => result.item.category === category);
|
|
152
|
+
if (tags !== void 0) results = results.filter((result) => tags.every((tag) => result.item.tags.includes(tag)));
|
|
153
|
+
return results.slice(0, limited).map((result) => result.item);
|
|
154
|
+
};
|
|
155
|
+
// src/main.ts
|
|
156
|
+
const lessons = join(await ConfigDir(), "lessons");
|
|
157
|
+
const patterns = join(await ConfigDir(), "patterns");
|
|
158
|
+
const context = join(await ConfigDir(), "context");
|
|
159
|
+
const commands = join(await ConfigDir(), "commands");
|
|
160
|
+
const preferences = join(await ConfigDir(), "preferences");
|
|
161
|
+
await mkdir(lessons, { recursive: true });
|
|
162
|
+
await mkdir(patterns, { recursive: true });
|
|
163
|
+
await mkdir(context, { recursive: true });
|
|
164
|
+
await mkdir(commands, { recursive: true });
|
|
165
|
+
await mkdir(preferences, { recursive: true });
|
|
166
|
+
const server = new McpServer({
|
|
167
|
+
name: MCP_NAME,
|
|
168
|
+
version: "0.0.1"
|
|
169
|
+
});
|
|
170
|
+
server.registerTool("memory_get", {
|
|
171
|
+
description: "Get agent memory",
|
|
172
|
+
inputSchema: MemoryGetInputSchema
|
|
173
|
+
}, async ({ id }) => {
|
|
174
|
+
const memory = await getFile(id);
|
|
175
|
+
if (!memory) return { content: [{
|
|
176
|
+
type: "text",
|
|
177
|
+
text: `Not Found ${id}`
|
|
178
|
+
}] };
|
|
179
|
+
return {
|
|
180
|
+
content: [{
|
|
181
|
+
type: "text",
|
|
182
|
+
text: "Memory retrieved successfully."
|
|
183
|
+
}],
|
|
184
|
+
structuredContent: memory
|
|
185
|
+
};
|
|
186
|
+
});
|
|
187
|
+
server.registerTool("memory_save", {
|
|
188
|
+
description: "Get agent memory",
|
|
189
|
+
inputSchema: AgentMemorySaveSchema
|
|
190
|
+
}, async ({ category, tags, agent, title, summary, content }) => {
|
|
191
|
+
if (await writeMemoryFile({
|
|
192
|
+
category,
|
|
193
|
+
tags,
|
|
194
|
+
agent,
|
|
195
|
+
title,
|
|
196
|
+
summary,
|
|
197
|
+
content
|
|
198
|
+
}) === true) return {
|
|
199
|
+
content: [{
|
|
200
|
+
type: "text",
|
|
201
|
+
text: "successfully save agent memory"
|
|
202
|
+
}],
|
|
203
|
+
structuredContent: { success: true }
|
|
204
|
+
};
|
|
205
|
+
return {
|
|
206
|
+
content: [{
|
|
207
|
+
type: "text",
|
|
208
|
+
text: "Failed save agent memory"
|
|
209
|
+
}],
|
|
210
|
+
structuredContent: { isError: true }
|
|
211
|
+
};
|
|
212
|
+
});
|
|
213
|
+
server.registerTool("memory_search", {
|
|
214
|
+
description: "Get agent memory",
|
|
215
|
+
inputSchema: SearchSchema
|
|
216
|
+
}, async ({ query, category, tags, limit }) => {
|
|
217
|
+
const result = await Search({
|
|
218
|
+
query,
|
|
219
|
+
category,
|
|
220
|
+
tags,
|
|
221
|
+
limit
|
|
222
|
+
});
|
|
223
|
+
return { content: [{
|
|
224
|
+
type: "text",
|
|
225
|
+
text: JSON.stringify(result, null, 2)
|
|
226
|
+
}] };
|
|
227
|
+
});
|
|
228
|
+
async function main() {
|
|
229
|
+
const transport = new StdioServerTransport();
|
|
230
|
+
await server.connect(transport);
|
|
231
|
+
}
|
|
232
|
+
// src/index.ts
|
|
233
|
+
main();
|
|
234
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "samemory",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"module": "src/index.ts",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"version": "0.0.1",
|
|
6
|
+
"bin": {
|
|
7
|
+
"samemory": "dist/index.mjs"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"url": "https://github.com/nazozokc/samemory.git"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"dist",
|
|
14
|
+
"README.md",
|
|
15
|
+
"package.json",
|
|
16
|
+
"LICENSE"
|
|
17
|
+
],
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@types/bun": "latest",
|
|
20
|
+
"tsdown": "latest"
|
|
21
|
+
},
|
|
22
|
+
"peerDependencies": {
|
|
23
|
+
"typescript": "^7"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@modelcontextprotocol/server": "^2.4.0",
|
|
27
|
+
"chokidar": "^6.0.0",
|
|
28
|
+
"fuse.js": "^7.5.0",
|
|
29
|
+
"zod": "^4.6.7"
|
|
30
|
+
}
|
|
31
|
+
}
|