minimem 0.0.4 → 0.0.6

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.
@@ -0,0 +1,286 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/internal.ts
31
+ var internal_exports = {};
32
+ __export(internal_exports, {
33
+ buildFileEntry: () => buildFileEntry,
34
+ chunkMarkdown: () => chunkMarkdown,
35
+ cosineSimilarity: () => cosineSimilarity,
36
+ ensureDir: () => ensureDir,
37
+ extractChunkMetadata: () => extractChunkMetadata,
38
+ hashText: () => hashText,
39
+ isMemoryPath: () => isMemoryPath,
40
+ listMemoryFiles: () => listMemoryFiles,
41
+ logError: () => logError,
42
+ normalizeRelPath: () => normalizeRelPath,
43
+ parseEmbedding: () => parseEmbedding,
44
+ stripPrivateContent: () => stripPrivateContent,
45
+ truncateUtf16Safe: () => truncateUtf16Safe,
46
+ vectorToBlob: () => vectorToBlob
47
+ });
48
+ module.exports = __toCommonJS(internal_exports);
49
+ var import_node_crypto = __toESM(require("crypto"), 1);
50
+ var import_node_fs = __toESM(require("fs"), 1);
51
+ var import_promises = __toESM(require("fs/promises"), 1);
52
+ var import_node_path = __toESM(require("path"), 1);
53
+ function logError(context, error, debug) {
54
+ if (!debug) return;
55
+ const message = error instanceof Error ? error.message : String(error);
56
+ debug(`[${context}] Error: ${message}`);
57
+ }
58
+ function ensureDir(dir, debug) {
59
+ try {
60
+ import_node_fs.default.mkdirSync(dir, { recursive: true });
61
+ } catch (error) {
62
+ const nodeError = error;
63
+ if (nodeError.code !== "EEXIST") {
64
+ logError("ensureDir", error, debug);
65
+ }
66
+ }
67
+ return dir;
68
+ }
69
+ function normalizeRelPath(value) {
70
+ const trimmed = value.trim().replace(/^[./]+/, "");
71
+ return trimmed.replace(/\\/g, "/");
72
+ }
73
+ function isMemoryPath(relPath) {
74
+ const normalized = normalizeRelPath(relPath);
75
+ if (!normalized) return false;
76
+ if (normalized === "MEMORY.md" || normalized === "memory.md") return true;
77
+ return normalized.startsWith("memory/");
78
+ }
79
+ async function exists(filePath) {
80
+ try {
81
+ await import_promises.default.access(filePath);
82
+ return true;
83
+ } catch {
84
+ return false;
85
+ }
86
+ }
87
+ async function walkDir(dir, files) {
88
+ const entries = await import_promises.default.readdir(dir, { withFileTypes: true });
89
+ for (const entry of entries) {
90
+ const full = import_node_path.default.join(dir, entry.name);
91
+ if (entry.isDirectory()) {
92
+ await walkDir(full, files);
93
+ continue;
94
+ }
95
+ if (!entry.isFile()) continue;
96
+ if (!entry.name.endsWith(".md")) continue;
97
+ files.push(full);
98
+ }
99
+ }
100
+ async function listMemoryFiles(memoryDir) {
101
+ const result = [];
102
+ const memoryFile = import_node_path.default.join(memoryDir, "MEMORY.md");
103
+ const altMemoryFile = import_node_path.default.join(memoryDir, "memory.md");
104
+ const hasUpper = await exists(memoryFile);
105
+ const hasLower = await exists(altMemoryFile);
106
+ if (hasUpper && hasLower) {
107
+ let upperReal = memoryFile;
108
+ let lowerReal = altMemoryFile;
109
+ try {
110
+ upperReal = await import_promises.default.realpath(memoryFile);
111
+ } catch {
112
+ }
113
+ try {
114
+ lowerReal = await import_promises.default.realpath(altMemoryFile);
115
+ } catch {
116
+ }
117
+ if (upperReal !== lowerReal) {
118
+ throw new Error(
119
+ `Both MEMORY.md and memory.md exist in ${memoryDir}. Please remove one to avoid ambiguity.`
120
+ );
121
+ }
122
+ result.push(memoryFile);
123
+ } else if (hasUpper) {
124
+ result.push(memoryFile);
125
+ } else if (hasLower) {
126
+ result.push(altMemoryFile);
127
+ }
128
+ const memorySubDir = import_node_path.default.join(memoryDir, "memory");
129
+ if (await exists(memorySubDir)) {
130
+ await walkDir(memorySubDir, result);
131
+ }
132
+ if (result.length <= 1) return result;
133
+ const seen = /* @__PURE__ */ new Set();
134
+ const deduped = [];
135
+ for (const entry of result) {
136
+ let key = entry;
137
+ try {
138
+ key = await import_promises.default.realpath(entry);
139
+ } catch {
140
+ }
141
+ if (seen.has(key)) continue;
142
+ seen.add(key);
143
+ deduped.push(entry);
144
+ }
145
+ return deduped;
146
+ }
147
+ function hashText(value) {
148
+ return import_node_crypto.default.createHash("sha256").update(value).digest("hex");
149
+ }
150
+ async function buildFileEntry(absPath, memoryDir) {
151
+ const stat = await import_promises.default.stat(absPath);
152
+ const content = await import_promises.default.readFile(absPath, "utf-8");
153
+ const hash = hashText(content);
154
+ return {
155
+ path: import_node_path.default.relative(memoryDir, absPath).replace(/\\/g, "/"),
156
+ absPath,
157
+ mtimeMs: stat.mtimeMs,
158
+ size: stat.size,
159
+ hash
160
+ };
161
+ }
162
+ function stripPrivateContent(content) {
163
+ return content.replace(/<private>[\s\S]*?<\/private>/gi, (match) => {
164
+ const lineCount = match.split("\n").length;
165
+ return "\n".repeat(lineCount - 1);
166
+ });
167
+ }
168
+ function chunkMarkdown(content, chunking) {
169
+ const stripped = stripPrivateContent(content);
170
+ const lines = stripped.split("\n");
171
+ if (lines.length === 0) return [];
172
+ const maxChars = Math.max(32, chunking.tokens * 4);
173
+ const overlapChars = Math.max(0, chunking.overlap * 4);
174
+ const chunks = [];
175
+ let current = [];
176
+ let currentChars = 0;
177
+ const flush = () => {
178
+ if (current.length === 0) return;
179
+ const firstEntry = current[0];
180
+ const lastEntry = current[current.length - 1];
181
+ if (!firstEntry || !lastEntry) return;
182
+ const text = current.map((entry) => entry.line).join("\n");
183
+ const startLine = firstEntry.lineNo;
184
+ const endLine = lastEntry.lineNo;
185
+ chunks.push({
186
+ startLine,
187
+ endLine,
188
+ text,
189
+ hash: hashText(text)
190
+ });
191
+ };
192
+ const carryOverlap = () => {
193
+ if (overlapChars <= 0 || current.length === 0) {
194
+ current = [];
195
+ currentChars = 0;
196
+ return;
197
+ }
198
+ let acc = 0;
199
+ const kept = [];
200
+ for (let i = current.length - 1; i >= 0; i -= 1) {
201
+ const entry = current[i];
202
+ if (!entry) continue;
203
+ acc += entry.line.length + 1;
204
+ kept.unshift(entry);
205
+ if (acc >= overlapChars) break;
206
+ }
207
+ current = kept;
208
+ currentChars = kept.reduce((sum, entry) => sum + entry.line.length + 1, 0);
209
+ };
210
+ for (let i = 0; i < lines.length; i += 1) {
211
+ const line = lines[i] ?? "";
212
+ const lineNo = i + 1;
213
+ const segments = [];
214
+ if (line.length === 0) {
215
+ segments.push("");
216
+ } else {
217
+ for (let start = 0; start < line.length; start += maxChars) {
218
+ segments.push(line.slice(start, start + maxChars));
219
+ }
220
+ }
221
+ for (const segment of segments) {
222
+ const lineSize = segment.length + 1;
223
+ if (currentChars + lineSize > maxChars && current.length > 0) {
224
+ flush();
225
+ carryOverlap();
226
+ }
227
+ current.push({ line: segment, lineNo });
228
+ currentChars += lineSize;
229
+ }
230
+ }
231
+ flush();
232
+ return chunks;
233
+ }
234
+ function extractChunkMetadata(text) {
235
+ const typeMatch = text.match(/<!--\s*type:\s*([\w-]+)\s*-->/i);
236
+ return typeMatch ? { type: typeMatch[1].toLowerCase() } : {};
237
+ }
238
+ function parseEmbedding(raw) {
239
+ try {
240
+ const parsed = JSON.parse(raw);
241
+ return Array.isArray(parsed) ? parsed : [];
242
+ } catch {
243
+ return [];
244
+ }
245
+ }
246
+ function cosineSimilarity(a, b) {
247
+ if (a.length === 0 || b.length === 0) return 0;
248
+ const len = Math.min(a.length, b.length);
249
+ let dot = 0;
250
+ let normA = 0;
251
+ let normB = 0;
252
+ for (let i = 0; i < len; i += 1) {
253
+ const av = a[i] ?? 0;
254
+ const bv = b[i] ?? 0;
255
+ dot += av * bv;
256
+ normA += av * av;
257
+ normB += bv * bv;
258
+ }
259
+ if (normA === 0 || normB === 0) return 0;
260
+ return dot / (Math.sqrt(normA) * Math.sqrt(normB));
261
+ }
262
+ function truncateUtf16Safe(text, maxChars) {
263
+ if (text.length <= maxChars) return text;
264
+ return text.slice(0, maxChars);
265
+ }
266
+ function vectorToBlob(embedding) {
267
+ return Buffer.from(new Float32Array(embedding).buffer);
268
+ }
269
+ // Annotate the CommonJS export names for ESM import in node:
270
+ 0 && (module.exports = {
271
+ buildFileEntry,
272
+ chunkMarkdown,
273
+ cosineSimilarity,
274
+ ensureDir,
275
+ extractChunkMetadata,
276
+ hashText,
277
+ isMemoryPath,
278
+ listMemoryFiles,
279
+ logError,
280
+ normalizeRelPath,
281
+ parseEmbedding,
282
+ stripPrivateContent,
283
+ truncateUtf16Safe,
284
+ vectorToBlob
285
+ });
286
+ //# sourceMappingURL=internal.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/internal.ts"],"sourcesContent":["import crypto from \"node:crypto\";\nimport fsSync from \"node:fs\";\nimport fs from \"node:fs/promises\";\nimport path from \"node:path\";\n\n/**\n * Debug function type for optional logging\n */\nexport type DebugFn = (message: string, data?: Record<string, unknown>) => void;\n\n/**\n * Log an error with context (for debugging).\n * Only logs if a debug function is provided.\n *\n * @param context - A short identifier for where the error occurred\n * @param error - The error object or message\n * @param debug - Optional debug function to log to\n */\nexport function logError(\n context: string,\n error: unknown,\n debug?: DebugFn\n): void {\n if (!debug) return;\n\n const message = error instanceof Error ? error.message : String(error);\n debug(`[${context}] Error: ${message}`);\n}\n\nexport type MemoryFileEntry = {\n path: string;\n absPath: string;\n mtimeMs: number;\n size: number;\n hash: string;\n};\n\nexport type MemoryChunk = {\n startLine: number;\n endLine: number;\n text: string;\n hash: string;\n};\n\n/**\n * Ensure a directory exists, creating it if necessary.\n *\n * @param dir - The directory path to ensure exists\n * @param debug - Optional debug function for logging errors\n * @returns The directory path\n */\nexport function ensureDir(dir: string, debug?: DebugFn): string {\n try {\n fsSync.mkdirSync(dir, { recursive: true });\n } catch (error) {\n // Only swallow EEXIST errors (directory already exists)\n // Log other errors for debugging\n const nodeError = error as NodeJS.ErrnoException;\n if (nodeError.code !== \"EEXIST\") {\n logError(\"ensureDir\", error, debug);\n }\n }\n return dir;\n}\n\nexport function normalizeRelPath(value: string): string {\n const trimmed = value.trim().replace(/^[./]+/, \"\");\n return trimmed.replace(/\\\\/g, \"/\");\n}\n\nexport function isMemoryPath(relPath: string): boolean {\n const normalized = normalizeRelPath(relPath);\n if (!normalized) return false;\n if (normalized === \"MEMORY.md\" || normalized === \"memory.md\") return true;\n return normalized.startsWith(\"memory/\");\n}\n\nasync function exists(filePath: string): Promise<boolean> {\n try {\n await fs.access(filePath);\n return true;\n } catch {\n return false;\n }\n}\n\nasync function walkDir(dir: string, files: string[]) {\n const entries = await fs.readdir(dir, { withFileTypes: true });\n for (const entry of entries) {\n const full = path.join(dir, entry.name);\n if (entry.isDirectory()) {\n await walkDir(full, files);\n continue;\n }\n if (!entry.isFile()) continue;\n if (!entry.name.endsWith(\".md\")) continue;\n files.push(full);\n }\n}\n\nexport async function listMemoryFiles(memoryDir: string): Promise<string[]> {\n const result: string[] = [];\n const memoryFile = path.join(memoryDir, \"MEMORY.md\");\n const altMemoryFile = path.join(memoryDir, \"memory.md\");\n const hasUpper = await exists(memoryFile);\n const hasLower = await exists(altMemoryFile);\n\n // Prevent ambiguity: both MEMORY.md and memory.md cannot coexist\n // (unless they resolve to the same file on case-insensitive filesystems)\n if (hasUpper && hasLower) {\n let upperReal = memoryFile;\n let lowerReal = altMemoryFile;\n try { upperReal = await fs.realpath(memoryFile); } catch {}\n try { lowerReal = await fs.realpath(altMemoryFile); } catch {}\n if (upperReal !== lowerReal) {\n throw new Error(\n `Both MEMORY.md and memory.md exist in ${memoryDir}. ` +\n `Please remove one to avoid ambiguity.`\n );\n }\n // Same file (case-insensitive FS) — only include once\n result.push(memoryFile);\n } else if (hasUpper) {\n result.push(memoryFile);\n } else if (hasLower) {\n result.push(altMemoryFile);\n }\n\n const memorySubDir = path.join(memoryDir, \"memory\");\n if (await exists(memorySubDir)) {\n await walkDir(memorySubDir, result);\n }\n if (result.length <= 1) return result;\n const seen = new Set<string>();\n const deduped: string[] = [];\n for (const entry of result) {\n let key = entry;\n try {\n key = await fs.realpath(entry);\n } catch {}\n if (seen.has(key)) continue;\n seen.add(key);\n deduped.push(entry);\n }\n return deduped;\n}\n\nexport function hashText(value: string): string {\n return crypto.createHash(\"sha256\").update(value).digest(\"hex\");\n}\n\nexport async function buildFileEntry(\n absPath: string,\n memoryDir: string,\n): Promise<MemoryFileEntry> {\n const stat = await fs.stat(absPath);\n const content = await fs.readFile(absPath, \"utf-8\");\n const hash = hashText(content);\n return {\n path: path.relative(memoryDir, absPath).replace(/\\\\/g, \"/\"),\n absPath,\n mtimeMs: stat.mtimeMs,\n size: stat.size,\n hash,\n };\n}\n\n/**\n * Strip content between <private>...</private> tags.\n * Replaces private blocks with the same number of empty lines to preserve line numbering.\n */\nexport function stripPrivateContent(content: string): string {\n return content.replace(/<private>[\\s\\S]*?<\\/private>/gi, (match) => {\n const lineCount = match.split(\"\\n\").length;\n return \"\\n\".repeat(lineCount - 1);\n });\n}\n\nexport function chunkMarkdown(\n content: string,\n chunking: { tokens: number; overlap: number },\n): MemoryChunk[] {\n const stripped = stripPrivateContent(content);\n const lines = stripped.split(\"\\n\");\n if (lines.length === 0) return [];\n const maxChars = Math.max(32, chunking.tokens * 4);\n const overlapChars = Math.max(0, chunking.overlap * 4);\n const chunks: MemoryChunk[] = [];\n\n let current: Array<{ line: string; lineNo: number }> = [];\n let currentChars = 0;\n\n const flush = () => {\n if (current.length === 0) return;\n const firstEntry = current[0];\n const lastEntry = current[current.length - 1];\n if (!firstEntry || !lastEntry) return;\n const text = current.map((entry) => entry.line).join(\"\\n\");\n const startLine = firstEntry.lineNo;\n const endLine = lastEntry.lineNo;\n chunks.push({\n startLine,\n endLine,\n text,\n hash: hashText(text),\n });\n };\n\n const carryOverlap = () => {\n if (overlapChars <= 0 || current.length === 0) {\n current = [];\n currentChars = 0;\n return;\n }\n let acc = 0;\n const kept: Array<{ line: string; lineNo: number }> = [];\n for (let i = current.length - 1; i >= 0; i -= 1) {\n const entry = current[i];\n if (!entry) continue;\n acc += entry.line.length + 1;\n kept.unshift(entry);\n if (acc >= overlapChars) break;\n }\n current = kept;\n currentChars = kept.reduce((sum, entry) => sum + entry.line.length + 1, 0);\n };\n\n for (let i = 0; i < lines.length; i += 1) {\n const line = lines[i] ?? \"\";\n const lineNo = i + 1;\n const segments: string[] = [];\n if (line.length === 0) {\n segments.push(\"\");\n } else {\n for (let start = 0; start < line.length; start += maxChars) {\n segments.push(line.slice(start, start + maxChars));\n }\n }\n for (const segment of segments) {\n const lineSize = segment.length + 1;\n if (currentChars + lineSize > maxChars && current.length > 0) {\n flush();\n carryOverlap();\n }\n current.push({ line: segment, lineNo });\n currentChars += lineSize;\n }\n }\n flush();\n return chunks;\n}\n\n/**\n * Extract metadata from a chunk's text content.\n * Looks for HTML comments like <!-- type: decision --> in the chunk.\n */\nexport function extractChunkMetadata(text: string): { type?: string } {\n const typeMatch = text.match(/<!--\\s*type:\\s*([\\w-]+)\\s*-->/i);\n return typeMatch ? { type: typeMatch[1].toLowerCase() } : {};\n}\n\nexport function parseEmbedding(raw: string): number[] {\n try {\n const parsed = JSON.parse(raw) as number[];\n return Array.isArray(parsed) ? parsed : [];\n } catch {\n return [];\n }\n}\n\nexport function cosineSimilarity(a: number[], b: number[]): number {\n if (a.length === 0 || b.length === 0) return 0;\n const len = Math.min(a.length, b.length);\n let dot = 0;\n let normA = 0;\n let normB = 0;\n for (let i = 0; i < len; i += 1) {\n const av = a[i] ?? 0;\n const bv = b[i] ?? 0;\n dot += av * bv;\n normA += av * av;\n normB += bv * bv;\n }\n if (normA === 0 || normB === 0) return 0;\n return dot / (Math.sqrt(normA) * Math.sqrt(normB));\n}\n\nexport function truncateUtf16Safe(text: string, maxChars: number): string {\n if (text.length <= maxChars) return text;\n return text.slice(0, maxChars);\n}\n\n/**\n * Convert a numeric embedding vector to a Buffer suitable for sqlite-vec storage.\n * Uses Float32 encoding, which is the format expected by sqlite-vec's vector functions.\n */\nexport function vectorToBlob(embedding: number[]): Buffer {\n return Buffer.from(new Float32Array(embedding).buffer);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,yBAAmB;AACnB,qBAAmB;AACnB,sBAAe;AACf,uBAAiB;AAeV,SAAS,SACd,SACA,OACA,OACM;AACN,MAAI,CAAC,MAAO;AAEZ,QAAM,UAAU,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK;AACrE,QAAM,IAAI,OAAO,YAAY,OAAO,EAAE;AACxC;AAwBO,SAAS,UAAU,KAAa,OAAyB;AAC9D,MAAI;AACF,mBAAAA,QAAO,UAAU,KAAK,EAAE,WAAW,KAAK,CAAC;AAAA,EAC3C,SAAS,OAAO;AAGd,UAAM,YAAY;AAClB,QAAI,UAAU,SAA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M,KAAK,IAAI,EAAE,QAAQ,EAAE,MAAM;AACvC,MAAI,MAAM;AACV,MAAI,QAAQ;AACZ,MAAI,QAAQ;AACZ,WAAS,IAAI,GAAG,IAAI,KAAK,KAAK,GAAG;AAC/B,UAAM,KAAK,EAAE,CAAC,KAAK;AACnB,UAAM,KAAK,EAAE,CAAC,KAAK;AACnB,WAAO,KAAK;AACZ,aAAS,KAAK;AACd,aAAS,KAAK;AAAA,EAChB;AACA,MAAI,UAAU,KAAK,UAAU,EAAG,QAAO;AACvC,SAAO,OAAO,KAAK,KAAK,KAAK,IAAI,KAAK,KAAK,KAAK;AAClD;AAEO,SAAS,kBAAkB,MAAc,UAA0B;AACxE,MAAI,KAAK,UAAU,SAAU,QAAO;AACpC,SAAO,KAAK,MAAM,GAAG,QAAQ;AAC/B;AAMO,SAAS,aAAa,WAA6B;AACxD,SAAO,OAAO,KAAK,IAAI,aAAa,SAAS,EAAE,MAAM;AACvD;","names":["fsSync","fs","path","crypto"]}
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Debug function type for optional logging
3
+ */
4
+ type DebugFn = (message: string, data?: Record<string, unknown>) => void;
5
+ /**
6
+ * Log an error with context (for debugging).
7
+ * Only logs if a debug function is provided.
8
+ *
9
+ * @param context - A short identifier for where the error occurred
10
+ * @param error - The error object or message
11
+ * @param debug - Optional debug function to log to
12
+ */
13
+ declare function logError(context: string, error: unknown, debug?: DebugFn): void;
14
+ type MemoryFileEntry = {
15
+ path: string;
16
+ absPath: string;
17
+ mtimeMs: number;
18
+ size: number;
19
+ hash: string;
20
+ };
21
+ type MemoryChunk = {
22
+ startLine: number;
23
+ endLine: number;
24
+ text: string;
25
+ hash: string;
26
+ };
27
+ /**
28
+ * Ensure a directory exists, creating it if necessary.
29
+ *
30
+ * @param dir - The directory path to ensure exists
31
+ * @param debug - Optional debug function for logging errors
32
+ * @returns The directory path
33
+ */
34
+ declare function ensureDir(dir: string, debug?: DebugFn): string;
35
+ declare function normalizeRelPath(value: string): string;
36
+ declare function isMemoryPath(relPath: string): boolean;
37
+ declare function listMemoryFiles(memoryDir: string): Promise<string[]>;
38
+ declare function hashText(value: string): string;
39
+ declare function buildFileEntry(absPath: string, memoryDir: string): Promise<MemoryFileEntry>;
40
+ /**
41
+ * Strip content between <private>...</private> tags.
42
+ * Replaces private blocks with the same number of empty lines to preserve line numbering.
43
+ */
44
+ declare function stripPrivateContent(content: string): string;
45
+ declare function chunkMarkdown(content: string, chunking: {
46
+ tokens: number;
47
+ overlap: number;
48
+ }): MemoryChunk[];
49
+ /**
50
+ * Extract metadata from a chunk's text content.
51
+ * Looks for HTML comments like <!-- type: decision --> in the chunk.
52
+ */
53
+ declare function extractChunkMetadata(text: string): {
54
+ type?: string;
55
+ };
56
+ declare function parseEmbedding(raw: string): number[];
57
+ declare function cosineSimilarity(a: number[], b: number[]): number;
58
+ declare function truncateUtf16Safe(text: string, maxChars: number): string;
59
+ /**
60
+ * Convert a numeric embedding vector to a Buffer suitable for sqlite-vec storage.
61
+ * Uses Float32 encoding, which is the format expected by sqlite-vec's vector functions.
62
+ */
63
+ declare function vectorToBlob(embedding: number[]): Buffer;
64
+
65
+ export { type DebugFn, type MemoryChunk, type MemoryFileEntry, buildFileEntry, chunkMarkdown, cosineSimilarity, ensureDir, extractChunkMetadata, hashText, isMemoryPath, listMemoryFiles, logError, normalizeRelPath, parseEmbedding, stripPrivateContent, truncateUtf16Safe, vectorToBlob };
@@ -0,0 +1,298 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/session.ts
31
+ var session_exports = {};
32
+ __export(session_exports, {
33
+ addFrontmatter: () => addFrontmatter,
34
+ addSessionToContent: () => addSessionToContent,
35
+ extractSession: () => extractSession,
36
+ parseFrontmatter: () => parseFrontmatter,
37
+ serializeFrontmatter: () => serializeFrontmatter
38
+ });
39
+ module.exports = __toCommonJS(session_exports);
40
+ var os = __toESM(require("os"), 1);
41
+ function parseFrontmatter(content) {
42
+ const frontmatterRegex = /^---\n([\s\S]*?)\n---\n/;
43
+ const match = content.match(frontmatterRegex);
44
+ if (!match) {
45
+ return { frontmatter: void 0, body: content };
46
+ }
47
+ const yamlContent = match[1];
48
+ const body = content.slice(match[0].length);
49
+ try {
50
+ const frontmatter = parseSimpleYaml(yamlContent);
51
+ return { frontmatter, body };
52
+ } catch {
53
+ return { frontmatter: void 0, body: content };
54
+ }
55
+ }
56
+ function parseSimpleYaml(yaml) {
57
+ const lines = yaml.split("\n");
58
+ return parseYamlBlock(lines, 0, 0, lines.length).value;
59
+ }
60
+ function parseYamlBlock(lines, indent, startIdx, endIdx) {
61
+ const result = {};
62
+ let i = startIdx;
63
+ while (i < endIdx) {
64
+ const line = lines[i];
65
+ if (!line || !line.trim()) {
66
+ i++;
67
+ continue;
68
+ }
69
+ const lineIndent = getIndent(line);
70
+ if (lineIndent < indent) break;
71
+ if (lineIndent > indent) {
72
+ i++;
73
+ continue;
74
+ }
75
+ const keyMatch = line.match(/^(\s*)([\w-]+):\s*(.*)?$/);
76
+ if (!keyMatch) {
77
+ i++;
78
+ continue;
79
+ }
80
+ const [, , key, rawValue] = keyMatch;
81
+ const value = rawValue?.trim() ?? "";
82
+ if (value === "" || value === void 0) {
83
+ const nextNonEmpty = findNextNonEmptyLine(lines, i + 1, endIdx);
84
+ if (nextNonEmpty < endIdx) {
85
+ const nextLine = lines[nextNonEmpty];
86
+ const nextIndent = getIndent(nextLine);
87
+ if (nextIndent > indent) {
88
+ if (nextLine.trimStart().startsWith("- ")) {
89
+ const listResult = parseYamlList(lines, nextIndent, i + 1, endIdx);
90
+ result[key] = listResult.value;
91
+ i = listResult.nextIdx;
92
+ } else {
93
+ const blockResult = parseYamlBlock(lines, nextIndent, i + 1, endIdx);
94
+ result[key] = blockResult.value;
95
+ i = blockResult.nextIdx;
96
+ }
97
+ continue;
98
+ }
99
+ }
100
+ result[key] = null;
101
+ i++;
102
+ } else {
103
+ result[key] = parseYamlValue(value);
104
+ i++;
105
+ }
106
+ }
107
+ return { value: result, nextIdx: i };
108
+ }
109
+ function parseYamlList(lines, indent, startIdx, endIdx) {
110
+ const result = [];
111
+ let i = startIdx;
112
+ while (i < endIdx) {
113
+ const line = lines[i];
114
+ if (!line || !line.trim()) {
115
+ i++;
116
+ continue;
117
+ }
118
+ const lineIndent = getIndent(line);
119
+ if (lineIndent < indent) break;
120
+ if (lineIndent > indent) {
121
+ i++;
122
+ continue;
123
+ }
124
+ const trimmed = line.trimStart();
125
+ if (!trimmed.startsWith("- ")) break;
126
+ const itemContent = trimmed.slice(2).trim();
127
+ if (itemContent === "" || itemContent === void 0) {
128
+ const nextNonEmpty = findNextNonEmptyLine(lines, i + 1, endIdx);
129
+ if (nextNonEmpty < endIdx) {
130
+ const nextIndent = getIndent(lines[nextNonEmpty]);
131
+ if (nextIndent > indent) {
132
+ const blockResult = parseYamlBlock(lines, nextIndent, i + 1, endIdx);
133
+ result.push(blockResult.value);
134
+ i = blockResult.nextIdx;
135
+ continue;
136
+ }
137
+ }
138
+ result.push(null);
139
+ i++;
140
+ } else {
141
+ const kvMatch = itemContent.match(/^([\w-]+):\s*(.*)$/);
142
+ if (kvMatch) {
143
+ const obj = {};
144
+ const [, firstKey, firstVal] = kvMatch;
145
+ obj[firstKey] = parseYamlValue(firstVal?.trim() ?? "");
146
+ const itemKeyIndent = indent + 2;
147
+ let j = i + 1;
148
+ while (j < endIdx) {
149
+ const nextLine = lines[j];
150
+ if (!nextLine || !nextLine.trim()) {
151
+ j++;
152
+ continue;
153
+ }
154
+ const nextLineIndent = getIndent(nextLine);
155
+ if (nextLineIndent < itemKeyIndent) break;
156
+ if (nextLineIndent === itemKeyIndent) {
157
+ const nextKv = nextLine.match(/^\s*([\w-]+):\s*(.*)$/);
158
+ if (nextKv) {
159
+ const [, nk, nv] = nextKv;
160
+ obj[nk] = parseYamlValue(nv?.trim() ?? "");
161
+ j++;
162
+ continue;
163
+ }
164
+ }
165
+ break;
166
+ }
167
+ result.push(obj);
168
+ i = j;
169
+ } else {
170
+ result.push(parseYamlValue(itemContent));
171
+ i++;
172
+ }
173
+ }
174
+ }
175
+ return { value: result, nextIdx: i };
176
+ }
177
+ function getIndent(line) {
178
+ const match = line.match(/^(\s*)/);
179
+ return match ? match[1].length : 0;
180
+ }
181
+ function findNextNonEmptyLine(lines, from, end) {
182
+ for (let i = from; i < end; i++) {
183
+ if (lines[i]?.trim()) return i;
184
+ }
185
+ return end;
186
+ }
187
+ function parseYamlValue(value) {
188
+ if (value === "") return null;
189
+ if (value.startsWith('"') && value.endsWith('"') || value.startsWith("'") && value.endsWith("'")) {
190
+ return value.slice(1, -1);
191
+ }
192
+ if (value === "null" || value === "~") return null;
193
+ if (value === "true") return true;
194
+ if (value === "false") return false;
195
+ const num = Number(value);
196
+ if (!isNaN(num) && value !== "") return num;
197
+ if (value.startsWith("[") && value.endsWith("]")) {
198
+ const inner = value.slice(1, -1);
199
+ if (inner.trim() === "") return [];
200
+ return inner.split(",").map((s) => parseYamlValue(s.trim()));
201
+ }
202
+ return value;
203
+ }
204
+ function serializeFrontmatter(frontmatter) {
205
+ const lines = ["---"];
206
+ if (frontmatter.id) {
207
+ lines.push(`id: ${frontmatter.id}`);
208
+ }
209
+ if (frontmatter.type) {
210
+ lines.push(`type: ${frontmatter.type}`);
211
+ }
212
+ if (frontmatter.session) {
213
+ lines.push("session:");
214
+ const session = frontmatter.session;
215
+ if (session.id) lines.push(` id: ${session.id}`);
216
+ if (session.source) lines.push(` source: ${session.source}`);
217
+ if (session.project) lines.push(` project: ${formatPath(session.project)}`);
218
+ if (session.transcript) lines.push(` transcript: ${formatPath(session.transcript)}`);
219
+ }
220
+ if (frontmatter.created) {
221
+ lines.push(`created: ${frontmatter.created}`);
222
+ }
223
+ if (frontmatter.updated) {
224
+ lines.push(`updated: ${frontmatter.updated}`);
225
+ }
226
+ if (frontmatter.tags && frontmatter.tags.length > 0) {
227
+ lines.push(`tags: [${frontmatter.tags.join(", ")}]`);
228
+ }
229
+ if (frontmatter.domain && frontmatter.domain.length > 0) {
230
+ lines.push(`domain: [${frontmatter.domain.join(", ")}]`);
231
+ }
232
+ if (frontmatter.entities && frontmatter.entities.length > 0) {
233
+ lines.push(`entities: [${frontmatter.entities.join(", ")}]`);
234
+ }
235
+ if (frontmatter.confidence !== void 0) {
236
+ lines.push(`confidence: ${frontmatter.confidence}`);
237
+ }
238
+ if (frontmatter.source) {
239
+ lines.push("source:");
240
+ if (frontmatter.source.origin) lines.push(` origin: ${frontmatter.source.origin}`);
241
+ if (frontmatter.source.trajectories && frontmatter.source.trajectories.length > 0) {
242
+ lines.push(` trajectories: [${frontmatter.source.trajectories.join(", ")}]`);
243
+ }
244
+ if (frontmatter.source.agentId) lines.push(` agentId: ${frontmatter.source.agentId}`);
245
+ }
246
+ if (frontmatter.links && frontmatter.links.length > 0) {
247
+ lines.push("links:");
248
+ for (const link of frontmatter.links) {
249
+ lines.push(` - target: ${link.target}`);
250
+ lines.push(` relation: ${link.relation}`);
251
+ if (link.layer) lines.push(` layer: ${link.layer}`);
252
+ }
253
+ }
254
+ if (frontmatter.supersedes !== void 0) {
255
+ lines.push(`supersedes: ${frontmatter.supersedes === null ? "~" : frontmatter.supersedes}`);
256
+ }
257
+ lines.push("---");
258
+ return lines.join("\n") + "\n";
259
+ }
260
+ function addFrontmatter(content, frontmatter) {
261
+ const { frontmatter: existing, body } = parseFrontmatter(content);
262
+ const merged = {
263
+ ...existing,
264
+ ...frontmatter,
265
+ session: {
266
+ ...existing?.session,
267
+ ...frontmatter.session
268
+ }
269
+ };
270
+ if (!merged.created) {
271
+ merged.created = (/* @__PURE__ */ new Date()).toISOString();
272
+ }
273
+ merged.updated = (/* @__PURE__ */ new Date()).toISOString();
274
+ return serializeFrontmatter(merged) + body;
275
+ }
276
+ function addSessionToContent(content, session) {
277
+ return addFrontmatter(content, { session });
278
+ }
279
+ function formatPath(filePath) {
280
+ const home = os.homedir();
281
+ if (filePath.startsWith(home)) {
282
+ return "~" + filePath.slice(home.length);
283
+ }
284
+ return filePath;
285
+ }
286
+ function extractSession(content) {
287
+ const { frontmatter } = parseFrontmatter(content);
288
+ return frontmatter?.session;
289
+ }
290
+ // Annotate the CommonJS export names for ESM import in node:
291
+ 0 && (module.exports = {
292
+ addFrontmatter,
293
+ addSessionToContent,
294
+ extractSession,
295
+ parseFrontmatter,
296
+ serializeFrontmatter
297
+ });
298
+ //# sourceMappingURL=session.cjs.map