recallnest 2.2.0 → 2.3.0

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.
Files changed (249) hide show
  1. package/.env +4 -0
  2. package/CLAUDE.md +1 -1
  3. package/README.md +30 -5
  4. package/README_CN.md +30 -5
  5. package/config.json +55 -0
  6. package/connectors/examples/email-adapter.ts +73 -0
  7. package/connectors/examples/logs-adapter.ts +103 -0
  8. package/connectors/examples/rss-adapter.ts +95 -0
  9. package/logs/daily-recall.error.log +0 -0
  10. package/logs/daily-recall.log +60 -0
  11. package/logs/ingest-2026-04-11.log +110 -0
  12. package/logs/ingest-2026-04-12.log +90 -0
  13. package/logs/ingest-2026-04-13.log +87 -0
  14. package/logs/launchagent-stderr.log +0 -0
  15. package/logs/launchagent-stdout.log +0 -0
  16. package/package.json +1 -1
  17. package/src/cli.ts +30 -0
  18. package/src/connector-types.ts +57 -0
  19. package/src/conversation-importer.ts +25 -1
  20. package/src/data-checkup.ts +31 -1
  21. package/src/doctor.ts +29 -0
  22. package/src/ingest.ts +203 -0
  23. package/src/mcp-server.ts +2 -2
  24. package/src/obsidian-connector.ts +282 -0
  25. package/src/session-schema.ts +2 -2
  26. package/src/source-heartbeat.ts +108 -0
  27. package/.claude-plugin/CLAUDE.md +0 -29
  28. package/.claude-plugin/marketplace.json +0 -18
  29. package/.dockerignore +0 -11
  30. package/.github/workflows/ci.yml +0 -28
  31. package/.mcp.json +0 -9
  32. package/CHANGELOG.md +0 -51
  33. package/DEMO.md +0 -94
  34. package/Dockerfile +0 -20
  35. package/EVAL.md +0 -89
  36. package/FAILURES.md +0 -75
  37. package/OPERATIONS.md +0 -59
  38. package/ROADMAP.md +0 -245
  39. package/SKILL.md +0 -250
  40. package/assets/dashboard.png +0 -0
  41. package/assets/knowledge-graph.png +0 -0
  42. package/assets/recallnest-workbench.png +0 -0
  43. package/assets/screenshots/ui-full.png +0 -0
  44. package/assets/screenshots/ui-overview.png +0 -0
  45. package/assets/screenshots/ui-search.png +0 -0
  46. package/assets/ui/app.js +0 -1031
  47. package/assets/ui/index.html +0 -224
  48. package/assets/ui/styles.css +0 -781
  49. package/assets/wechat-qr.jpg +0 -0
  50. package/bun.lock +0 -337
  51. package/daily-reflections/2026-03-05.md +0 -15
  52. package/daily-reflections/2026-03-06.md +0 -15
  53. package/daily-reflections/2026-03-07.md +0 -7
  54. package/daily-reflections/2026-03-08.md +0 -34
  55. package/daily-reflections/2026-03-09.md +0 -35
  56. package/daily-reflections/2026-03-10.md +0 -36
  57. package/daily-reflections/2026-03-11.md +0 -35
  58. package/daily-reflections/2026-03-12.md +0 -36
  59. package/daily-reflections/2026-03-13.md +0 -35
  60. package/daily-reflections/2026-03-14.md +0 -35
  61. package/daily-reflections/2026-03-15.md +0 -35
  62. package/daily-reflections/2026-03-16.md +0 -41
  63. package/daily-reflections/2026-03-17.md +0 -10
  64. package/daily-reflections/2026-03-18.md +0 -10
  65. package/daily-reflections/2026-03-19.md +0 -10
  66. package/daily-reflections/2026-03-20.md +0 -10
  67. package/daily-reflections/2026-03-21.md +0 -10
  68. package/daily-reflections/2026-03-22.md +0 -10
  69. package/daily-reflections/2026-03-23.md +0 -10
  70. package/daily-reflections/2026-03-24.md +0 -10
  71. package/daily-reflections/2026-03-25.md +0 -32
  72. package/daily-reflections/2026-03-26.md +0 -37
  73. package/daily-reflections/2026-03-27.md +0 -35
  74. package/daily-reflections/2026-03-28.md +0 -35
  75. package/daily-reflections/2026-03-29.md +0 -35
  76. package/daily-reflections/2026-03-30.md +0 -36
  77. package/daily-reflections/2026-03-31.md +0 -35
  78. package/daily-reflections/2026-04-01.md +0 -35
  79. package/daily-reflections/2026-04-02.md +0 -35
  80. package/daily-reflections/2026-04-03.md +0 -37
  81. package/daily-reflections/2026-04-04.md +0 -37
  82. package/daily-reflections/2026-04-05.md +0 -35
  83. package/daily-reflections/2026-04-06.md +0 -36
  84. package/daily-reflections/2026-04-07.md +0 -35
  85. package/daily-reflections/2026-04-08.md +0 -35
  86. package/daily-reflections/2026-04-12.md +0 -35
  87. package/data/alias-map.example.json +0 -22
  88. package/docs/api-reference.md +0 -1291
  89. package/docs/architecture.md +0 -168
  90. package/docs/memory-boundary-contract.md +0 -226
  91. package/docs/memory-categories.md +0 -119
  92. package/docs/pipeline-audit-ultramemory.md +0 -177
  93. package/docs/plans/2026-03-08-query-expansion.md +0 -270
  94. package/docs/plans/2026-03-16-continuity-layer.md +0 -196
  95. package/docs/plans/2026-04-01-recall-boost.md +0 -100
  96. package/docs/plans/mcp-tiering-plan.md +0 -126
  97. package/docs/research/recall-quality-audit-2026-03-22.md +0 -153
  98. package/docs/self-evolution.md +0 -198
  99. package/docs/superpowers/plans/2026-03-19-ingest-pipeline-upgrade.md +0 -782
  100. package/docs/superpowers/plans/2026-04-11-constructive-retrieval-emotion.md +0 -1195
  101. package/docs/superpowers/specs/2026-03-19-ingest-pipeline-upgrade-design.md +0 -77
  102. package/docs/superpowers/specs/2026-03-23-pull-claude-desktop-design.md +0 -95
  103. package/docs/superpowers/specs/2026-04-11-constructive-retrieval-emotion-design.md +0 -398
  104. package/eval/continuity/results-2026-03-16.md +0 -117
  105. package/eval/continuity/results-2026-03-19.md +0 -453
  106. package/eval/continuity/runbook.md +0 -110
  107. package/eval/continuity/seed-cases.md +0 -54
  108. package/eval/reports/2026-03-06-abstract-query-baseline.md +0 -96
  109. package/eval/reports/2026-03-06-aws-ssh-baseline.md +0 -82
  110. package/eval/reports/2026-03-06-baseline.md +0 -82
  111. package/eval/reports/query-expansion.md +0 -138
  112. package/glama.json +0 -6
  113. package/lm +0 -19
  114. package/plan-philosophy-upgrades.md +0 -363
  115. package/plan-product-upgrades.md +0 -155
  116. package/plan-v1.2-distributable.md +0 -105
  117. package/plan.md +0 -140
  118. package/research-memory-evolution.md +0 -440
  119. package/research-rl-agent-memory.md +0 -330
  120. package/scripts/archive-distilled-junk.ts +0 -116
  121. package/scripts/backfill-anchors.ts +0 -130
  122. package/scripts/backfill-l0l1l2.ts +0 -98
  123. package/scripts/blind-scan.ts +0 -588
  124. package/scripts/cleanup-step1-exact-dups.ts +0 -117
  125. package/scripts/cleanup-step2-template-dups.ts +0 -113
  126. package/scripts/cleanup-step3-stale-content.ts +0 -149
  127. package/scripts/cleanup-step4-orphan-scopes.ts +0 -131
  128. package/scripts/delete-distilled-junk.ts +0 -88
  129. package/scripts/diagnose-vector-search.ts +0 -132
  130. package/scripts/distill-facts.ts +0 -305
  131. package/scripts/health-check.ts +0 -343
  132. package/scripts/incremental-ingest.sh +0 -71
  133. package/scripts/migrate-language.ts +0 -75
  134. package/scripts/mlp-health-check.ts +0 -382
  135. package/scripts/pull-claude-desktop.ts +0 -515
  136. package/scripts/re-ingest-cc.sh +0 -66
  137. package/scripts/rebalance-memory-health.ts +0 -181
  138. package/scripts/run-desktop-ingest.sh +0 -11
  139. package/scripts/start-server.sh +0 -12
  140. package/scripts/verify-emotion-decay.ts +0 -148
  141. package/scripts/viz-dashboard.html +0 -423
  142. package/scripts/viz-export.ts +0 -703
  143. package/src/__tests__/access-tracker-novelty.test.ts +0 -228
  144. package/src/__tests__/activity-counter.test.ts +0 -96
  145. package/src/__tests__/admission-control.test.ts +0 -206
  146. package/src/__tests__/anchor-generator.test.ts +0 -67
  147. package/src/__tests__/auto-consolidation.test.ts +0 -168
  148. package/src/__tests__/auto-recall.test.ts +0 -131
  149. package/src/__tests__/babel-integration.test.ts +0 -139
  150. package/src/__tests__/batch-dedup.test.ts +0 -65
  151. package/src/__tests__/capture-engine.test.ts +0 -1144
  152. package/src/__tests__/capture-heuristic.test.ts +0 -171
  153. package/src/__tests__/cascade-forget.test.ts +0 -210
  154. package/src/__tests__/category-min-scores.test.ts +0 -33
  155. package/src/__tests__/cc8-cc1-essential-context.test.ts +0 -298
  156. package/src/__tests__/circuit-breaker.test.ts +0 -99
  157. package/src/__tests__/cluster-consolidation.test.ts +0 -712
  158. package/src/__tests__/confidence-metadata.test.ts +0 -125
  159. package/src/__tests__/confidence-tracker.test.ts +0 -249
  160. package/src/__tests__/conflict-advisor.test.ts +0 -206
  161. package/src/__tests__/conflict-escalation.test.ts +0 -149
  162. package/src/__tests__/conflict-lifecycle.test.ts +0 -83
  163. package/src/__tests__/conflict-output.test.ts +0 -85
  164. package/src/__tests__/conflict-store.test.ts +0 -374
  165. package/src/__tests__/consolidation-engine.test.ts +0 -181
  166. package/src/__tests__/constructive-retrieval.test.ts +0 -509
  167. package/src/__tests__/context-collapse-renderer.test.ts +0 -214
  168. package/src/__tests__/context-composer-pins.test.ts +0 -64
  169. package/src/__tests__/context-composer-task-selection.test.ts +0 -24
  170. package/src/__tests__/context-reconstructor.test.ts +0 -65
  171. package/src/__tests__/context-renderer.test.ts +0 -91
  172. package/src/__tests__/continuity-eval.test.ts +0 -275
  173. package/src/__tests__/continuity-schema.test.ts +0 -97
  174. package/src/__tests__/conversation-importer.test.ts +0 -692
  175. package/src/__tests__/core-recall-eval.test.ts +0 -235
  176. package/src/__tests__/core-summary.test.ts +0 -109
  177. package/src/__tests__/data-checkup.test.ts +0 -225
  178. package/src/__tests__/decay-exemption.test.ts +0 -84
  179. package/src/__tests__/dedup-actions.test.ts +0 -58
  180. package/src/__tests__/dedup-guard.test.ts +0 -39
  181. package/src/__tests__/deterministic-id.test.ts +0 -39
  182. package/src/__tests__/distill-lock.test.ts +0 -176
  183. package/src/__tests__/doctor.test.ts +0 -303
  184. package/src/__tests__/dream-pipeline.test.ts +0 -232
  185. package/src/__tests__/drill-down.test.ts +0 -108
  186. package/src/__tests__/embedder.test.ts +0 -122
  187. package/src/__tests__/emotion-decay.test.ts +0 -46
  188. package/src/__tests__/emotion-detector.test.ts +0 -56
  189. package/src/__tests__/emotion-phase1-integration.test.ts +0 -153
  190. package/src/__tests__/emotion-retrieval.test.ts +0 -56
  191. package/src/__tests__/entity-resolver.test.ts +0 -58
  192. package/src/__tests__/ethics-layer.test.ts +0 -476
  193. package/src/__tests__/event-segmenter.test.ts +0 -153
  194. package/src/__tests__/feature-flags-integration.test.ts +0 -54
  195. package/src/__tests__/frequency-tracker.test.ts +0 -113
  196. package/src/__tests__/graph-export.test.ts +0 -489
  197. package/src/__tests__/hotness-blend.test.ts +0 -76
  198. package/src/__tests__/ingest.test.ts +0 -360
  199. package/src/__tests__/interference-detector.test.ts +0 -203
  200. package/src/__tests__/kg-extractor.test.ts +0 -184
  201. package/src/__tests__/kg-store.test.ts +0 -198
  202. package/src/__tests__/llm-consolidation.test.ts +0 -226
  203. package/src/__tests__/mcp-efficiency.test.ts +0 -288
  204. package/src/__tests__/memory-boundaries.test.ts +0 -237
  205. package/src/__tests__/memory-evolution-b2-b3-e1.test.ts +0 -243
  206. package/src/__tests__/memory-evolution.test.ts +0 -271
  207. package/src/__tests__/memory-health-rebalance.test.ts +0 -144
  208. package/src/__tests__/memory-lint.test.ts +0 -271
  209. package/src/__tests__/memory-output.test.ts +0 -215
  210. package/src/__tests__/multi-vector.test.ts +0 -242
  211. package/src/__tests__/narrative-architecture.test.ts +0 -456
  212. package/src/__tests__/pending-review.test.ts +0 -77
  213. package/src/__tests__/post-retrieval-filter.test.ts +0 -178
  214. package/src/__tests__/ppr-traversal.test.ts +0 -256
  215. package/src/__tests__/prediction-engine.test.ts +0 -349
  216. package/src/__tests__/predictive-prospective-memory.test.ts +0 -416
  217. package/src/__tests__/preference-guard-smoke.test.ts +0 -211
  218. package/src/__tests__/preference-matcher.test.ts +0 -230
  219. package/src/__tests__/preference-slots.test.ts +0 -205
  220. package/src/__tests__/prospective-memory.test.ts +0 -283
  221. package/src/__tests__/query-entity-detector.test.ts +0 -171
  222. package/src/__tests__/query-expander-alias.test.ts +0 -89
  223. package/src/__tests__/query-expander.test.ts +0 -34
  224. package/src/__tests__/recall-governor.test.ts +0 -227
  225. package/src/__tests__/recall-mode.test.ts +0 -64
  226. package/src/__tests__/result-synthesizer.test.ts +0 -198
  227. package/src/__tests__/resume-context.test.ts +0 -4898
  228. package/src/__tests__/retrieval-trace.test.ts +0 -213
  229. package/src/__tests__/retriever-boundary-weight.test.ts +0 -136
  230. package/src/__tests__/retroactive-boost.test.ts +0 -223
  231. package/src/__tests__/salience-scoring.test.ts +0 -106
  232. package/src/__tests__/scope-inventory.test.ts +0 -200
  233. package/src/__tests__/scope-policy.test.ts +0 -69
  234. package/src/__tests__/session-checkpoint.test.ts +0 -259
  235. package/src/__tests__/session-distiller.test.ts +0 -611
  236. package/src/__tests__/session-gc.test.ts +0 -215
  237. package/src/__tests__/skill-engine.test.ts +0 -546
  238. package/src/__tests__/skill-promotion.test.ts +0 -349
  239. package/src/__tests__/store-scope-audit.test.ts +0 -81
  240. package/src/__tests__/supersede-chain.test.ts +0 -154
  241. package/src/__tests__/temporal-parser.test.ts +0 -241
  242. package/src/__tests__/temporal-validity.test.ts +0 -160
  243. package/src/__tests__/term-registry.test.ts +0 -121
  244. package/src/__tests__/tool-output-compressor.test.ts +0 -133
  245. package/src/__tests__/topic-tag.test.ts +0 -113
  246. package/src/__tests__/trusted-memory.test.ts +0 -828
  247. package/src/__tests__/version-manager.test.ts +0 -213
  248. package/src/__tests__/workflow-observation.test.ts +0 -400
  249. package/src/__tests__/write-verifier.test.ts +0 -122
@@ -1,692 +0,0 @@
1
- import { describe, expect, it } from "bun:test";
2
-
3
- import {
4
- detectFormat,
5
- normalizeClaudeCode,
6
- normalizeClaudeAi,
7
- normalizeChatGPT,
8
- normalizeSlack,
9
- normalizePlaintext,
10
- normalizeConversation,
11
- ingestNormalizedMessages,
12
- type NormalizedMessage,
13
- type ConversationFormat,
14
- } from "../conversation-importer.js";
15
- import type { MemoryEntry, MemorySearchResult } from "../store.js";
16
-
17
- // ---------------------------------------------------------------------------
18
- // Mock helpers
19
- // ---------------------------------------------------------------------------
20
-
21
- function createMockStore() {
22
- const entries: MemoryEntry[] = [];
23
- let seq = 1;
24
- return {
25
- entries,
26
- store: {
27
- async store(entry: Omit<MemoryEntry, "id" | "timestamp"> & { id?: string }): Promise<MemoryEntry> {
28
- const stored: MemoryEntry = {
29
- ...entry,
30
- id: entry.id || `auto-${String(seq).padStart(12, "0")}`,
31
- timestamp: 1_700_000_000_000 + seq,
32
- metadata: entry.metadata || "{}",
33
- };
34
- seq += 1;
35
- entries.push(stored);
36
- return stored;
37
- },
38
- async update(id: string, updates: Partial<MemoryEntry>): Promise<MemoryEntry | null> {
39
- const idx = entries.findIndex((e) => e.id === id);
40
- if (idx < 0) return null;
41
- entries[idx] = { ...entries[idx], ...updates, timestamp: updates.timestamp ?? entries[idx].timestamp };
42
- return entries[idx];
43
- },
44
- async get(query: string, limit?: number): Promise<MemoryEntry[]> {
45
- return entries.slice(0, limit ?? entries.length);
46
- },
47
- async getById(id: string): Promise<MemoryEntry | null> {
48
- return entries.find((e) => e.id === id) ?? null;
49
- },
50
- async list(): Promise<MemoryEntry[]> {
51
- return entries;
52
- },
53
- async vectorSearch(vector: number[], limit: number): Promise<MemorySearchResult[]> {
54
- return entries.slice(0, limit).map((e) => ({ entry: e, score: 0.9 }));
55
- },
56
- },
57
- };
58
- }
59
-
60
- function createMockEmbedder() {
61
- return {
62
- embedPassage: async (_text: string): Promise<number[]> => new Array(768).fill(0.1),
63
- };
64
- }
65
-
66
- function createMockDeps() {
67
- const { store } = createMockStore();
68
- const embedder = createMockEmbedder();
69
- return { store, embedder };
70
- }
71
-
72
- // ---------------------------------------------------------------------------
73
- // Format detection
74
- // ---------------------------------------------------------------------------
75
-
76
- describe("detectFormat", () => {
77
- it("detects Claude Code JSONL", () => {
78
- const content = '{"type":"human","message":{"content":"hello"}}\n{"type":"assistant","message":{"content":"hi"}}';
79
- expect(detectFormat(content)).toBe("claude-code");
80
- });
81
-
82
- it("detects Claude.ai JSON with chat_conversations wrapper", () => {
83
- const content = JSON.stringify({
84
- chat_conversations: [{ chat_messages: [{ sender: "human", text: "hi" }] }],
85
- });
86
- expect(detectFormat(content)).toBe("claude-ai");
87
- });
88
-
89
- it("detects Claude.ai JSON as array", () => {
90
- const content = JSON.stringify([
91
- { chat_messages: [{ sender: "human", text: "hi" }] },
92
- ]);
93
- expect(detectFormat(content)).toBe("claude-ai");
94
- });
95
-
96
- it("detects ChatGPT conversations.json", () => {
97
- const content = JSON.stringify([{
98
- mapping: {
99
- root: { id: "root", parent: null, children: ["c1"], message: null },
100
- c1: { id: "c1", parent: "root", children: [], message: { author: { role: "user" }, content: { parts: ["hi"] } } },
101
- },
102
- }]);
103
- expect(detectFormat(content)).toBe("chatgpt");
104
- });
105
-
106
- it("detects ChatGPT single object", () => {
107
- const content = JSON.stringify({
108
- mapping: {
109
- root: { id: "root", parent: null, children: [], message: null },
110
- },
111
- });
112
- expect(detectFormat(content)).toBe("chatgpt");
113
- });
114
-
115
- it("detects Slack JSON as array", () => {
116
- const content = JSON.stringify([
117
- { user: "U123", text: "hello", ts: "1700000000.000000" },
118
- ]);
119
- expect(detectFormat(content)).toBe("slack");
120
- });
121
-
122
- it("detects Slack JSON with messages wrapper", () => {
123
- const content = JSON.stringify({
124
- messages: [{ user: "U123", text: "hello", ts: "1700000000.000000" }],
125
- });
126
- expect(detectFormat(content)).toBe("slack");
127
- });
128
-
129
- it("falls back to plaintext", () => {
130
- const content = "User: hello\nAssistant: hi there";
131
- expect(detectFormat(content)).toBe("plaintext");
132
- });
133
-
134
- it("falls back to plaintext for invalid JSON", () => {
135
- const content = "{ broken json";
136
- expect(detectFormat(content)).toBe("plaintext");
137
- });
138
- });
139
-
140
- // ---------------------------------------------------------------------------
141
- // Claude Code JSONL normalizer
142
- // ---------------------------------------------------------------------------
143
-
144
- describe("normalizeClaudeCode", () => {
145
- it("parses human and assistant messages", () => {
146
- const content = [
147
- JSON.stringify({ type: "human", message: { content: "What is 2+2?" } }),
148
- JSON.stringify({ type: "assistant", message: { content: "4" } }),
149
- ].join("\n");
150
-
151
- const result = normalizeClaudeCode(content);
152
- expect(result).toHaveLength(2);
153
- expect(result[0]).toEqual({ role: "user", content: "What is 2+2?" });
154
- expect(result[1]).toEqual({ role: "assistant", content: "4" });
155
- });
156
-
157
- it("handles array content parts", () => {
158
- const content = JSON.stringify({
159
- type: "assistant",
160
- message: {
161
- content: [
162
- { type: "text", text: "Part 1" },
163
- { type: "text", text: "Part 2" },
164
- ],
165
- },
166
- });
167
-
168
- const result = normalizeClaudeCode(content);
169
- expect(result).toHaveLength(1);
170
- expect(result[0].content).toBe("Part 1\nPart 2");
171
- });
172
-
173
- it("preserves timestamp when present", () => {
174
- const content = JSON.stringify({
175
- type: "human",
176
- message: { content: "hi" },
177
- timestamp: "2024-01-01T00:00:00Z",
178
- });
179
-
180
- const result = normalizeClaudeCode(content);
181
- expect(result[0].timestamp).toBe("2024-01-01T00:00:00Z");
182
- });
183
-
184
- it("skips blank lines and invalid JSON", () => {
185
- const content = "\n\n{not json}\n" + JSON.stringify({ type: "human", message: { content: "ok" } });
186
- const result = normalizeClaudeCode(content);
187
- expect(result).toHaveLength(1);
188
- expect(result[0].content).toBe("ok");
189
- });
190
-
191
- it("skips entries with empty content", () => {
192
- const content = JSON.stringify({ type: "human", message: { content: " " } });
193
- const result = normalizeClaudeCode(content);
194
- expect(result).toHaveLength(0);
195
- });
196
-
197
- it("recognizes message.role field", () => {
198
- const content = JSON.stringify({
199
- message: { role: "user", content: "hello from role" },
200
- });
201
- const result = normalizeClaudeCode(content);
202
- expect(result).toHaveLength(1);
203
- expect(result[0].role).toBe("user");
204
- });
205
-
206
- it("skips unknown types", () => {
207
- const content = JSON.stringify({ type: "tool_result", message: { content: "data" } });
208
- const result = normalizeClaudeCode(content);
209
- expect(result).toHaveLength(0);
210
- });
211
- });
212
-
213
- // ---------------------------------------------------------------------------
214
- // Claude.ai JSON normalizer
215
- // ---------------------------------------------------------------------------
216
-
217
- describe("normalizeClaudeAi", () => {
218
- it("parses chat_conversations wrapper", () => {
219
- const data = {
220
- chat_conversations: [{
221
- chat_messages: [
222
- { sender: "human", text: "How are you?" },
223
- { sender: "assistant", text: "I'm doing well!" },
224
- ],
225
- }],
226
- };
227
- const result = normalizeClaudeAi(JSON.stringify(data));
228
- expect(result).toHaveLength(2);
229
- expect(result[0]).toEqual({ role: "user", content: "How are you?" });
230
- expect(result[1]).toEqual({ role: "assistant", content: "I'm doing well!" });
231
- });
232
-
233
- it("parses array of conversations", () => {
234
- const data = [{
235
- chat_messages: [
236
- { sender: "human", text: "A" },
237
- { sender: "assistant", text: "B" },
238
- ],
239
- }, {
240
- chat_messages: [
241
- { sender: "human", text: "C" },
242
- ],
243
- }];
244
- const result = normalizeClaudeAi(JSON.stringify(data));
245
- expect(result).toHaveLength(3);
246
- });
247
-
248
- it("preserves created_at as timestamp", () => {
249
- const data = [{
250
- chat_messages: [{ sender: "human", text: "hi", created_at: "2024-06-15T10:00:00Z" }],
251
- }];
252
- const result = normalizeClaudeAi(JSON.stringify(data));
253
- expect(result[0].timestamp).toBe("2024-06-15T10:00:00Z");
254
- });
255
-
256
- it("skips empty text", () => {
257
- const data = [{
258
- chat_messages: [
259
- { sender: "human", text: "" },
260
- { sender: "assistant", text: " " },
261
- { sender: "human", text: "valid" },
262
- ],
263
- }];
264
- const result = normalizeClaudeAi(JSON.stringify(data));
265
- expect(result).toHaveLength(1);
266
- expect(result[0].content).toBe("valid");
267
- });
268
-
269
- it("skips unknown sender", () => {
270
- const data = [{
271
- chat_messages: [{ sender: "system", text: "ignored" }],
272
- }];
273
- const result = normalizeClaudeAi(JSON.stringify(data));
274
- expect(result).toHaveLength(0);
275
- });
276
-
277
- it("handles single conversation object", () => {
278
- const data = {
279
- chat_messages: [
280
- { sender: "human", text: "direct" },
281
- ],
282
- };
283
- const result = normalizeClaudeAi(JSON.stringify(data));
284
- expect(result).toHaveLength(1);
285
- expect(result[0].content).toBe("direct");
286
- });
287
- });
288
-
289
- // ---------------------------------------------------------------------------
290
- // ChatGPT normalizer
291
- // ---------------------------------------------------------------------------
292
-
293
- describe("normalizeChatGPT", () => {
294
- it("rebuilds message order from mapping tree", () => {
295
- const data = [{
296
- mapping: {
297
- root: { id: "root", parent: null, children: ["m1"], message: null },
298
- m1: {
299
- id: "m1", parent: "root", children: ["m2"],
300
- message: { author: { role: "user" }, content: { parts: ["Hello"] } },
301
- },
302
- m2: {
303
- id: "m2", parent: "m1", children: [],
304
- message: { author: { role: "assistant" }, content: { parts: ["Hi there!"] } },
305
- },
306
- },
307
- }];
308
-
309
- const result = normalizeChatGPT(JSON.stringify(data));
310
- expect(result).toHaveLength(2);
311
- expect(result[0]).toEqual({ role: "user", content: "Hello" });
312
- expect(result[1]).toEqual({ role: "assistant", content: "Hi there!" });
313
- });
314
-
315
- it("converts create_time to ISO timestamp", () => {
316
- const data = [{
317
- mapping: {
318
- root: { id: "root", parent: null, children: ["m1"], message: null },
319
- m1: {
320
- id: "m1", parent: "root", children: [],
321
- message: {
322
- author: { role: "user" },
323
- content: { parts: ["hi"] },
324
- create_time: 1700000000,
325
- },
326
- },
327
- },
328
- }];
329
-
330
- const result = normalizeChatGPT(JSON.stringify(data));
331
- expect(result[0].timestamp).toBeDefined();
332
- expect(new Date(result[0].timestamp!).getTime()).toBe(1700000000 * 1000);
333
- });
334
-
335
- it("handles multiple conversations", () => {
336
- const conv = (text: string) => ({
337
- mapping: {
338
- root: { id: "root", parent: null, children: ["m1"], message: null },
339
- m1: {
340
- id: "m1", parent: "root", children: [],
341
- message: { author: { role: "user" }, content: { parts: [text] } },
342
- },
343
- },
344
- });
345
-
346
- const result = normalizeChatGPT(JSON.stringify([conv("A"), conv("B")]));
347
- expect(result).toHaveLength(2);
348
- expect(result[0].content).toBe("A");
349
- expect(result[1].content).toBe("B");
350
- });
351
-
352
- it("joins multiple content parts", () => {
353
- const data = [{
354
- mapping: {
355
- root: { id: "root", parent: null, children: ["m1"], message: null },
356
- m1: {
357
- id: "m1", parent: "root", children: [],
358
- message: { author: { role: "user" }, content: { parts: ["Part 1", "Part 2"] } },
359
- },
360
- },
361
- }];
362
-
363
- const result = normalizeChatGPT(JSON.stringify(data));
364
- expect(result[0].content).toBe("Part 1\nPart 2");
365
- });
366
-
367
- it("skips non-string parts", () => {
368
- const data = [{
369
- mapping: {
370
- root: { id: "root", parent: null, children: ["m1"], message: null },
371
- m1: {
372
- id: "m1", parent: "root", children: [],
373
- message: {
374
- author: { role: "user" },
375
- content: { parts: ["text", { type: "image" }] },
376
- },
377
- },
378
- },
379
- }];
380
-
381
- const result = normalizeChatGPT(JSON.stringify(data));
382
- expect(result[0].content).toBe("text");
383
- });
384
-
385
- it("includes system messages", () => {
386
- const data = [{
387
- mapping: {
388
- root: { id: "root", parent: null, children: ["m1"], message: null },
389
- m1: {
390
- id: "m1", parent: "root", children: [],
391
- message: { author: { role: "system" }, content: { parts: ["You are helpful"] } },
392
- },
393
- },
394
- }];
395
-
396
- const result = normalizeChatGPT(JSON.stringify(data));
397
- expect(result).toHaveLength(1);
398
- expect(result[0].role).toBe("system");
399
- });
400
-
401
- it("skips nodes with empty text", () => {
402
- const data = [{
403
- mapping: {
404
- root: { id: "root", parent: null, children: ["m1"], message: null },
405
- m1: {
406
- id: "m1", parent: "root", children: [],
407
- message: { author: { role: "user" }, content: { parts: [""] } },
408
- },
409
- },
410
- }];
411
-
412
- const result = normalizeChatGPT(JSON.stringify(data));
413
- expect(result).toHaveLength(0);
414
- });
415
-
416
- it("handles single conversation object (not array)", () => {
417
- const data = {
418
- mapping: {
419
- root: { id: "root", parent: null, children: ["m1"], message: null },
420
- m1: {
421
- id: "m1", parent: "root", children: [],
422
- message: { author: { role: "user" }, content: { parts: ["solo"] } },
423
- },
424
- },
425
- };
426
-
427
- const result = normalizeChatGPT(JSON.stringify(data));
428
- expect(result).toHaveLength(1);
429
- expect(result[0].content).toBe("solo");
430
- });
431
- });
432
-
433
- // ---------------------------------------------------------------------------
434
- // Slack normalizer
435
- // ---------------------------------------------------------------------------
436
-
437
- describe("normalizeSlack", () => {
438
- it("parses messages array", () => {
439
- const data = [
440
- { user: "U123", text: "hey team", ts: "1700000000.000000" },
441
- { bot_id: "B456", text: "Hi, I am a bot", ts: "1700000001.000000" },
442
- ];
443
-
444
- const result = normalizeSlack(JSON.stringify(data));
445
- expect(result).toHaveLength(2);
446
- expect(result[0].role).toBe("user");
447
- expect(result[0].content).toBe("hey team");
448
- expect(result[1].role).toBe("assistant");
449
- expect(result[1].content).toBe("Hi, I am a bot");
450
- });
451
-
452
- it("converts ts to ISO timestamp", () => {
453
- const data = [{ user: "U123", text: "hi", ts: "1700000000.000000" }];
454
- const result = normalizeSlack(JSON.stringify(data));
455
- expect(result[0].timestamp).toBeDefined();
456
- expect(new Date(result[0].timestamp!).getTime()).toBe(1700000000 * 1000);
457
- });
458
-
459
- it("parses messages wrapper object", () => {
460
- const data = {
461
- messages: [
462
- { user: "U123", text: "wrapped", ts: "1700000000.000000" },
463
- ],
464
- };
465
- const result = normalizeSlack(JSON.stringify(data));
466
- expect(result).toHaveLength(1);
467
- expect(result[0].content).toBe("wrapped");
468
- });
469
-
470
- it("skips empty text", () => {
471
- const data = [
472
- { user: "U123", text: "", ts: "1700000000.000000" },
473
- { user: "U123", text: "valid", ts: "1700000001.000000" },
474
- ];
475
- const result = normalizeSlack(JSON.stringify(data));
476
- expect(result).toHaveLength(1);
477
- });
478
-
479
- it("returns empty array for unexpected structure", () => {
480
- const data = { unexpected: true };
481
- const result = normalizeSlack(JSON.stringify(data));
482
- expect(result).toHaveLength(0);
483
- });
484
- });
485
-
486
- // ---------------------------------------------------------------------------
487
- // Plaintext normalizer
488
- // ---------------------------------------------------------------------------
489
-
490
- describe("normalizePlaintext", () => {
491
- it("parses User:/Assistant: prefixes", () => {
492
- const content = "User: What is AI?\nAssistant: Artificial Intelligence.";
493
- const result = normalizePlaintext(content);
494
- expect(result).toHaveLength(2);
495
- expect(result[0]).toEqual({ role: "user", content: "What is AI?" });
496
- expect(result[1]).toEqual({ role: "assistant", content: "Artificial Intelligence." });
497
- });
498
-
499
- it("parses Human:/AI: prefixes", () => {
500
- const content = "Human: hello\nAI: hi there";
501
- const result = normalizePlaintext(content);
502
- expect(result).toHaveLength(2);
503
- expect(result[0].role).toBe("user");
504
- expect(result[1].role).toBe("assistant");
505
- });
506
-
507
- it("handles multi-line messages", () => {
508
- const content = "User: First line\nSecond line\nThird line\nAssistant: Reply";
509
- const result = normalizePlaintext(content);
510
- expect(result).toHaveLength(2);
511
- expect(result[0].content).toBe("First line\nSecond line\nThird line");
512
- expect(result[1].content).toBe("Reply");
513
- });
514
-
515
- it("handles System: prefix", () => {
516
- const content = "System: You are helpful\nUser: hi\nAssistant: hello";
517
- const result = normalizePlaintext(content);
518
- expect(result).toHaveLength(3);
519
- expect(result[0].role).toBe("system");
520
- });
521
-
522
- it("case-insensitive prefix matching", () => {
523
- const content = "user: lower\nASSISTANT: upper\nHuman: mixed";
524
- const result = normalizePlaintext(content);
525
- expect(result).toHaveLength(3);
526
- });
527
-
528
- it("returns empty for content without role prefixes", () => {
529
- const content = "Just some random text\nwithout any role prefixes.";
530
- const result = normalizePlaintext(content);
531
- expect(result).toHaveLength(0);
532
- });
533
-
534
- it("skips lines before first role prefix", () => {
535
- const content = "Preamble text\nUser: actual start";
536
- const result = normalizePlaintext(content);
537
- expect(result).toHaveLength(1);
538
- expect(result[0].content).toBe("actual start");
539
- });
540
- });
541
-
542
- // ---------------------------------------------------------------------------
543
- // normalizeConversation dispatcher
544
- // ---------------------------------------------------------------------------
545
-
546
- describe("normalizeConversation", () => {
547
- const formats: ConversationFormat[] = ["claude-code", "claude-ai", "chatgpt", "slack", "plaintext"];
548
-
549
- it("dispatches to correct normalizer for each format", () => {
550
- const claudeCode = '{"type":"human","message":{"content":"hi"}}';
551
- expect(normalizeConversation(claudeCode, "claude-code")).toHaveLength(1);
552
-
553
- const claudeAi = JSON.stringify([{ chat_messages: [{ sender: "human", text: "hi" }] }]);
554
- expect(normalizeConversation(claudeAi, "claude-ai")).toHaveLength(1);
555
-
556
- const chatgpt = JSON.stringify([{
557
- mapping: {
558
- root: { id: "root", parent: null, children: ["m1"], message: null },
559
- m1: { id: "m1", parent: "root", children: [], message: { author: { role: "user" }, content: { parts: ["hi"] } } },
560
- },
561
- }]);
562
- expect(normalizeConversation(chatgpt, "chatgpt")).toHaveLength(1);
563
-
564
- const slack = JSON.stringify([{ user: "U1", text: "hi", ts: "1700000000.0" }]);
565
- expect(normalizeConversation(slack, "slack")).toHaveLength(1);
566
-
567
- const plain = "User: hi\nAssistant: hello";
568
- expect(normalizeConversation(plain, "plaintext")).toHaveLength(2);
569
- });
570
- });
571
-
572
- // ---------------------------------------------------------------------------
573
- // ingestNormalizedMessages
574
- // ---------------------------------------------------------------------------
575
-
576
- describe("ingestNormalizedMessages", () => {
577
- it("stores messages through persistMemory", async () => {
578
- const deps = createMockDeps();
579
- const messages: NormalizedMessage[] = [
580
- { role: "user", content: "What is TypeScript?" },
581
- { role: "assistant", content: "A typed superset of JavaScript." },
582
- ];
583
-
584
- const result = await ingestNormalizedMessages(deps, messages, "project:test");
585
- expect(result.total).toBe(2);
586
- expect(result.stored).toBe(2);
587
- expect(result.rejected).toBe(0);
588
- expect(result.errors).toHaveLength(0);
589
- });
590
-
591
- it("reports rejected messages from admission control", async () => {
592
- const deps = createMockDeps();
593
- const messages: NormalizedMessage[] = [
594
- { role: "user", content: "hi" },
595
- ];
596
-
597
- const result = await ingestNormalizedMessages(deps, messages, "project:test");
598
- expect(result.total).toBe(1);
599
- expect(result.rejected).toBe(1);
600
- });
601
-
602
- it("handles empty messages array", async () => {
603
- const deps = createMockDeps();
604
- const result = await ingestNormalizedMessages(deps, [], "project:test");
605
- expect(result.total).toBe(0);
606
- expect(result.stored).toBe(0);
607
- });
608
- });
609
-
610
- // ---------------------------------------------------------------------------
611
- // Integration: detectFormat -> normalize -> roundtrip
612
- // ---------------------------------------------------------------------------
613
-
614
- describe("integration: detect + normalize roundtrip", () => {
615
- it("Claude Code JSONL roundtrip", () => {
616
- const content = [
617
- JSON.stringify({ type: "human", message: { content: "Question" } }),
618
- JSON.stringify({ type: "assistant", message: { content: "Answer" } }),
619
- ].join("\n");
620
-
621
- const format = detectFormat(content);
622
- expect(format).toBe("claude-code");
623
- const messages = normalizeConversation(content, format);
624
- expect(messages).toHaveLength(2);
625
- expect(messages[0].role).toBe("user");
626
- expect(messages[1].role).toBe("assistant");
627
- });
628
-
629
- it("ChatGPT roundtrip", () => {
630
- const data = [{
631
- mapping: {
632
- root: { id: "root", parent: null, children: ["m1"], message: null },
633
- m1: {
634
- id: "m1", parent: "root", children: ["m2"],
635
- message: { author: { role: "user" }, content: { parts: ["Q"] }, create_time: 1700000000 },
636
- },
637
- m2: {
638
- id: "m2", parent: "m1", children: [],
639
- message: { author: { role: "assistant" }, content: { parts: ["A"] }, create_time: 1700000001 },
640
- },
641
- },
642
- }];
643
- const content = JSON.stringify(data);
644
-
645
- const format = detectFormat(content);
646
- expect(format).toBe("chatgpt");
647
- const messages = normalizeConversation(content, format);
648
- expect(messages).toHaveLength(2);
649
- expect(messages[0].content).toBe("Q");
650
- expect(messages[1].content).toBe("A");
651
- });
652
-
653
- it("Slack roundtrip", () => {
654
- const data = [
655
- { user: "U1", text: "morning", ts: "1700000000.0" },
656
- { bot_id: "B1", text: "Good morning!", ts: "1700000001.0" },
657
- ];
658
- const content = JSON.stringify(data);
659
-
660
- const format = detectFormat(content);
661
- expect(format).toBe("slack");
662
- const messages = normalizeConversation(content, format);
663
- expect(messages).toHaveLength(2);
664
- expect(messages[0].role).toBe("user");
665
- expect(messages[1].role).toBe("assistant");
666
- });
667
-
668
- it("Claude.ai roundtrip", () => {
669
- const data = {
670
- chat_conversations: [{
671
- chat_messages: [
672
- { sender: "human", text: "Q" },
673
- { sender: "assistant", text: "A" },
674
- ],
675
- }],
676
- };
677
- const content = JSON.stringify(data);
678
-
679
- const format = detectFormat(content);
680
- expect(format).toBe("claude-ai");
681
- const messages = normalizeConversation(content, format);
682
- expect(messages).toHaveLength(2);
683
- });
684
-
685
- it("Plaintext roundtrip", () => {
686
- const content = "User: hi\nAssistant: hello";
687
- const format = detectFormat(content);
688
- expect(format).toBe("plaintext");
689
- const messages = normalizeConversation(content, format);
690
- expect(messages).toHaveLength(2);
691
- });
692
- });