mem0.mjs
sha256:65ccb454656ea5acdea0a10e559b78bcde1eb6ff753ecc2911bc99d1c3d7cadd
feat(calendar): enforce agent context tiers in retrieval AP…
Human
minor
⚠ breaking
1 day ago
| 1 | /** |
| 2 | * Mem0 export importer. Path to Mem0 export JSON; one note per memory. |
| 3 | * Requires Mem0 export file format (Pydantic-style or API response). |
| 4 | */ |
| 5 | |
| 6 | import fs from 'fs'; |
| 7 | import path from 'path'; |
| 8 | import { writeNote } from '../write.mjs'; |
| 9 | import { normalizeSlug } from '../vault.mjs'; |
| 10 | |
| 11 | /** |
| 12 | * @param {string} input - Path to Mem0 export JSON |
| 13 | * @param {{ vaultPath: string, outputBase: string, project?: string, tags: string[], dryRun: boolean, onMemoryEvent?: Function }} ctx |
| 14 | * @returns {Promise<{ imported: { path: string, source_id?: string }[], count: number }>} |
| 15 | */ |
| 16 | export async function importMem0(input, ctx) { |
| 17 | const { vaultPath, outputBase, project, tags, dryRun, onMemoryEvent } = ctx; |
| 18 | const absInput = path.isAbsolute(input) ? input : path.resolve(process.cwd(), input); |
| 19 | if (!fs.existsSync(absInput) || !fs.statSync(absInput).isFile()) { |
| 20 | throw new Error(`Mem0 export file not found: ${input}`); |
| 21 | } |
| 22 | |
| 23 | const raw = fs.readFileSync(absInput, 'utf8'); |
| 24 | let data; |
| 25 | try { |
| 26 | data = JSON.parse(raw); |
| 27 | } catch (e) { |
| 28 | throw new Error(`Invalid Mem0 export JSON: ${e.message}`); |
| 29 | } |
| 30 | |
| 31 | const memories = Array.isArray(data) ? data : (data.memories || data.results || []); |
| 32 | const imported = []; |
| 33 | const now = new Date().toISOString().slice(0, 10); |
| 34 | |
| 35 | for (let i = 0; i < memories.length; i++) { |
| 36 | const m = memories[i]; |
| 37 | const body = m.memory || m.content || m.text || JSON.stringify(m); |
| 38 | const memId = m.id || m.memory_id || m.metadata?.id || `mem0_${i}`; |
| 39 | const sourceId = String(memId).slice(0, 128); |
| 40 | const date = m.created_at || m.updated_at || m.metadata?.created_at || now; |
| 41 | const d = typeof date === 'number' ? new Date(date * 1000).toISOString().slice(0, 10) : String(date).slice(0, 10); |
| 42 | const safeName = `mem0_${String(memId).replace(/[^a-zA-Z0-9_-]/g, '_').slice(0, 40)}`; |
| 43 | const outputRel = path.join(outputBase, `${safeName}.md`).replace(/\\/g, '/'); |
| 44 | |
| 45 | const frontmatter = { |
| 46 | source: 'mem0', |
| 47 | source_id: sourceId, |
| 48 | date: d, |
| 49 | ...(project && { project: normalizeSlug(project) }), |
| 50 | ...(tags.length && { tags }), |
| 51 | }; |
| 52 | |
| 53 | if (!dryRun) writeNote(vaultPath, outputRel, { body: String(body).trim(), frontmatter }); |
| 54 | imported.push({ path: outputRel, source_id: sourceId }); |
| 55 | |
| 56 | if (!dryRun && typeof onMemoryEvent === 'function') { |
| 57 | try { |
| 58 | onMemoryEvent({ |
| 59 | source: 'mem0', |
| 60 | source_id: sourceId, |
| 61 | date: d, |
| 62 | text: String(body).trim().slice(0, 500), |
| 63 | path: outputRel, |
| 64 | }); |
| 65 | } catch (_) {} |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return { imported, count: imported.length }; |
| 70 | } |
File History
2 commits
sha256:65ccb454656ea5acdea0a10e559b78bcde1eb6ff753ecc2911bc99d1c3d7cadd
feat(calendar): enforce agent context tiers in retrieval AP…
Human
minor
⚠
1 day ago
sha256:9103f98c89257ed2b01c237cea895dabb3e85ea337dccb1161c175e4422355b6
docs: accept Calendar Events v0 spec with Phase 0 security …
Human
1 day ago